Skip to content
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

Merged
merged 16 commits into from Mar 21, 2024

Conversation

jakobbotsch
Copy link
Member

@jakobbotsch jakobbotsch commented Mar 20, 2024

Adds very rudimentary support for Swift reverse pinvokes. Since Swift function pointers are based on SwiftSelf also adds basic support for the SwiftSelf parameter. Structs are not supported yet, and neither is SwiftError.

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 that genFnPrologCalleeRegArgs 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).

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Mar 20, 2024
Comment on lines -1738 to -1743

regType = compiler->GetEightByteType(structDesc, nCnt);
#ifdef DEBUG
regType = compiler->mangleVarArgsType(regType);
assert(genMapRegNumToRegArgNum((nCnt == 0 ? regNum : otherRegNum), regType) != (unsigned)-1);
#endif // DEBUG
Copy link
Member Author

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.

@jakobbotsch jakobbotsch marked this pull request as ready for review March 20, 2024 16:36
@jakobbotsch
Copy link
Member Author

jakobbotsch commented Mar 20, 2024

cc @dotnet/jit-contrib PTAL @amanasifkhalid

FYI @kotlarmilos @matouskozak @jkurdek

}
catch (Exception ex)
{
*(ExceptionDispatchInfo*)self.Value = ExceptionDispatchInfo.Capture(ex);
Copy link
Member

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?

Copy link
Member Author

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);
Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member

Choose a reason for hiding this comment

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

Ok, thanks for info.

Copy link
Member

@amanasifkhalid amanasifkhalid left a 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);
Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member Author

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
Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member

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;
Copy link
Member

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?

Copy link
Member Author

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);
Copy link
Member

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...

Copy link
Member Author

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))
Copy link
Member

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.

Copy link
Member Author

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.

@amanasifkhalid
Copy link
Member

It might be a good idea to run jitstressregs (or a similar pipeline) on this PR

@jakobbotsch
Copy link
Member Author

/azp run runtime-coreclr jitstress, runtime-coreclr jitstressregs, runtime-coreclr jitstress2-jitstressregs

Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@amanasifkhalid
Copy link
Member

Looks like jitstressregs is failing with the assert genRegMask(argDsc->GetArgReg()) & (RBM_ARG_REGS) in lsrabuild.cpp (log)

@jakobbotsch
Copy link
Member Author

Looks like jitstressregs is failing with the assert genRegMask(argDsc->GetArgReg()) & (RBM_ARG_REGS) in lsrabuild.cpp (log)

Pushed some commits to fix this and address the rest of the feedback.

@jakobbotsch
Copy link
Member Author

/azp run runtime-coreclr jitstress, runtime-coreclr jitstressregs, runtime-coreclr jitstress2-jitstressregs

Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@jakobbotsch
Copy link
Member Author

Failures are #100035, #99810, #100047

@jakobbotsch jakobbotsch merged commit de774ff into dotnet:main Mar 21, 2024
159 of 170 checks passed
@jakobbotsch jakobbotsch deleted the swift-reverse-pinvoke branch March 21, 2024 17:12
@github-actions github-actions bot locked and limited conversation to collaborators Apr 21, 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.

None yet

3 participants