-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JIT: Add basic support for Swift reverse pinvokes #100018
Conversation
|
||
regType = compiler->GetEightByteType(structDesc, nCnt); | ||
#ifdef DEBUG | ||
regType = compiler->mangleVarArgsType(regType); | ||
assert(genMapRegNumToRegArgNum((nCnt == 0 ? regNum : otherRegNum), regType) != (unsigned)-1); | ||
#endif // DEBUG |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have removed this assert in a couple of places because I don't think it is asserting anything meaningful. The existence and mapping of genMapRegNumToRegArgNum
is really an internal detail of how genFnPrologCalleeRegArgs
works, and I expect to be able to remove it once that function is rewritten.
cc @dotnet/jit-contrib PTAL @amanasifkhalid |
src/tests/Interop/Swift/SwiftCallbackAbiStress/SwiftCallbackAbiStress.cs
Show resolved
Hide resolved
} | ||
catch (Exception ex) | ||
{ | ||
*(ExceptionDispatchInfo*)self.Value = ExceptionDispatchInfo.Capture(ex); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How does this get returned to the C# side?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The pinvokes in C# land are passing pointers to the ExceptionDispatchInfo
as "self", and then check whether the it was set by this code after the return.
if ((strcmp(className, "SwiftSelf") == 0) && (strcmp(namespaceName, "System.Runtime.InteropServices.Swift") == 0)) | ||
{ | ||
LclVarDsc* varDsc = varDscInfo->varDsc; | ||
varDsc->SetArgReg(REG_SWIFT_SELF); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this mean that the reg value is loaded into the SwiftSelf argument?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The JIT can directly reference and use IL locals in its internal IR, and each of the parameter locals are marked with information about how they're passed (in a register or on stack). That's what the code here is doing.
It's ultimately up to the register allocator to decide what register every local is allocated into. For parameter locals LSRA will prefer to keep it in the same register that it was passed in, but it might also be allocated to another register (in which case a move from the initial register is needed in the prolog) or to stack (in which case a store to the stack frame is needed).
The PR here doesn't allow enregistering SwiftSelf
so we will always end up keeping it on the stack frame. That's because the case where there are conflicts in the move graph is not yet handled for this register (e.g. we can have cases where the SwiftSelf register x20
needs to go to x0
and x0
needs to go to x20
). It needs a bit of work but I was expecting to work on the relevant logic due to another issue, so I will handle both of these when I do that work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, thanks for info.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, just a few comments. Thanks!
} | ||
catch (Exception ex) | ||
{ | ||
*(ExceptionDispatchInfo*)self.Value = ExceptionDispatchInfo.Capture(ex); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Once SwiftError
works with reverse Pinvoke, I assume we're going to update this error handling to use the SwiftError
arg instead, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I think that would make sense.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although handling it through SwiftError
will require allocating a GCHandle
, so the current approach will probably end up being simpler.
@@ -0,0 +1,482 @@ | |||
#pragma warning disable CS8500 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need the // Licensed to the .NET Foundation under one or more agreements.
header comments at the top of these new files?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, I should add those.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think some of the other interop tests like SwiftAbiStress are missing it too, but feel free to address that in a follow-up PR.
@@ -3859,6 +3859,7 @@ class Compiler | |||
// mechanism passes the address of the return address to a runtime helper | |||
// where it is used to detect tail-call chains. | |||
unsigned lvaRetAddrVar; | |||
unsigned lvaSwiftSelfArg; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since all the places where we check lvaSwiftSelfArg
are under #ifdef SWIFT_SUPPORT
, should we ifdef this, and where we initialize it in Compiler::lvaInit
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I'll do that.
@@ -3982,6 +3983,8 @@ class Compiler | |||
CORINFO_ARG_LIST_HANDLE varList, | |||
CORINFO_SIG_INFO* varSig); | |||
|
|||
bool lvaInitSpecialSwiftParam(InitVarDscInfo* varDscInfo, CorInfoType type, CORINFO_CLASS_HANDLE typeHnd); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on the naming of this method, I guess we'll be using this to do the SwiftError*
type lookup too...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I was thinking that same logic could live here.
@@ -6131,6 +6143,14 @@ void CodeGen::genFnProlog() | |||
intRegState.rsCalleeRegArgMaskLiveIn &= ~RBM_SECRET_STUB_PARAM; | |||
} | |||
|
|||
#ifdef SWIFT_SUPPORT | |||
if ((compiler->lvaSwiftSelfArg != BAD_VAR_NUM) && ((intRegState.rsCalleeRegArgMaskLiveIn & RBM_SWIFT_SELF) != 0)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are there situations where lvaSwiftSelfArg
is valid, but the reg mask doesn't include the Swift self reg? If not, maybe we should assert that the reg mask includes it if lvaSwiftSelfArg
is set.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, if lvaSwiftSelfArg
is dead on entry then it shouldn't be in the live-in set.
It might be a good idea to run jitstressregs (or a similar pipeline) on this PR |
/azp run runtime-coreclr jitstress, runtime-coreclr jitstressregs, runtime-coreclr jitstress2-jitstressregs |
Azure Pipelines successfully started running 3 pipeline(s). |
Looks like jitstressregs is failing with the assert |
Pushed some commits to fix this and address the rest of the feedback. |
/azp run runtime-coreclr jitstress, runtime-coreclr jitstressregs, runtime-coreclr jitstress2-jitstressregs |
Azure Pipelines successfully started running 3 pipeline(s). |
Adds very rudimentary support for Swift reverse pinvokes. Since Swift function pointers are based on
SwiftSelf
also adds basic support for theSwiftSelf
parameter. Structs are not supported yet, and neither isSwiftError
.Adds 10 simple tests just so that we have something. I'm going to regenerate these (and probably increase the number to 100) once we have the struct support.
The
SwiftSelf
parameter is not enregisterable because it is passed in a register thatgenFnPrologCalleeRegArgs
does not handle. I have other plans to rewrite this function in the future (#99286 (comment)), so I didn't want to bother spending time on it here (it is not a trivial extension given how it works).