Skip to content

JIT: Use sentinel arg to clear error register pre-Swift call - #99428

Merged
amanasifkhalid merged 7 commits into
dotnet:mainfrom
amanasifkhalid:swift-error-well-known
Mar 8, 2024
Merged

amanasifkhalid merged 7 commits into
dotnet:mainfrom
amanasifkhalid:swift-error-well-known

Conversation

@amanasifkhalid

Copy link
Copy Markdown
Contributor

Fixes #99414. By adding a well-known zero argument to Swift calls with error handling that is always passed via the error register, we can get the JIT to clear the error register before the call, and also prevent the function address from being passed in the error register.

@ghost ghost added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Mar 7, 2024
@amanasifkhalid

Copy link
Copy Markdown
Contributor Author

@jakobbotsch PTAL, thanks!

@amanasifkhalid

Copy link
Copy Markdown
Contributor Author

/azp run runtime-coreclr jitstressregs

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

// GTF_CALL_M_SWIFT_ERROR_HANDLING indicates the call has a SwiftError* argument,
// so the error register value will eventually be consumed post-call.
if ((call->gtCallMoreFlags & GTF_CALL_M_SWIFT_ERROR_HANDLING) != 0)
if (call->HasSwiftErrorHandling())

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this ends up being expensive TP wise you can add some handling in AddedWellKnownArg to make it cheaper.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it'll be cheap with the check for the calling convention in HasSwiftErrorHandling

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I don't think this will be too expensive in the general case, though if we want to speed up the Swift error handling case, I can add some new state to CallArgs to avoid the lookup.

@amanasifkhalid amanasifkhalid Mar 7, 2024 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, updating this state in AddedWellKnownArg would probably affect TP in the general case. I'll probably have to separate this logic out.

Comment thread src/coreclr/jit/gentree.h Outdated
bool HasSwiftErrorHandling()
{
// Most calls aren't Swift calls, so short-circuit this check by checking the calling convention first.
return (unmgdCallConv == CorInfoCallConvExtension::Swift) &&

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Accessing unmgdCallConv here is UB when that's not the active union member, so this probably needs a few other checks.

@amanasifkhalid

Copy link
Copy Markdown
Contributor Author

jitstressregs passed. Gonna push a trivial revision.

Comment thread src/coreclr/jit/gentree.h Outdated
#endif
#ifdef SWIFT_SUPPORT
// True if we are calling a Swift method that may throw.
bool m_hasSwiftErrorHandling : 1;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was the other approach costing TP, or why are we going with this?

I would follow the pattern in AddedWellKnownArg instead, unless it really turns out to be costly. I think the consistency and correct-by-construction handling is preferable.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(But I liked your initial approach. I don't think the iteration of the args for swift calls is a problem.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was the other approach costing TP, or why are we going with this?

The cost of that approach was trivial, though I realized we check if a call uses Swift error handling in a few places where the call may be managed, so we'd have to call GetUnmanagedCallConv instead of checking unmgdCallConv directly to avoid UB. Considering we already use this AddedWellKnownArg state handling in a few places, it seemed cleaner to just copy that approach.

I would follow the pattern in AddedWellKnownArg instead, unless it really turns out to be costly. I think the consistency and correct-by-construction handling is preferable.

Got it, fixed.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AddedWellKnownArg is used just to improve performance. If we can avoid introducing extra state to maintain and there is no discernable TP cost of it, then I think we should do that.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked the TP impact locally, and the AddedWellKnownArg actually seems to be a bit more costly in the general case (~0.01-0.02% impact versus 0.07%). Even with the additional branch in HasSwiftErrorHandling from the call to GetUnmanagedCallConv, that seems like the way to go. I'll revert to it.

Comment thread src/coreclr/jit/gentree.cpp Outdated
case WellKnownArg::SwiftError:
m_hasSwiftErrorHandling = true;
break;
#endif // SWIFT_SUPPORT

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RemovedWellKnownArg should also be updated.

@amanasifkhalid
amanasifkhalid merged commit 4c94de4 into dotnet:main Mar 8, 2024
@amanasifkhalid
amanasifkhalid deleted the swift-error-well-known branch March 8, 2024 12:26
@github-actions github-actions Bot locked and limited conversation to collaborators Apr 9, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[jitstressregs] Interop/Swift/SwiftErrorHandling/SwiftErrorHandling fails

2 participants