-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
[mono][swift-interop] Support for Swift struct lowering in callback returns #104949
Merged
matouskozak
merged 9 commits into
dotnet:main
from
matouskozak:swift-interop/callbacks-struct-returns
Jul 18, 2024
+170
−98
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a080c25
Enable full SwiftCallbackAbiStress tests
matouskozak 3da512e
[arm64] Swift struct lowering in callback returns
matouskozak 10ce8f1
[amd64] Swift struct lowering in callback returns
matouskozak 0390f16
[x64] refactor return buffer save condition
matouskozak 6e0bbce
fix missing break in epilog
matouskozak f12e8d2
replace NOT_IMPLEMENTED with not_reached assert
matouskozak c67df1c
arg_set_val switch case indent
matouskozak 9f0fa6c
refactor LLVM call info comments
matouskozak 540f888
Apply suggestions from code review (by Jeremi)
matouskozak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -988,7 +988,8 @@ get_call_info (MonoMemPool *mp, MonoMethodSignature *sig) | |
/* | ||
* We need to set this even when sig->pinvoke is FALSE, because the `cinfo` gets copied to the | ||
* `cfg->arch` on the first pass. However, later in `amd64_handle_swift_return_buffer_reg` we | ||
* condition the Swift return buffer handling only to P/Invoke calls. | ||
* condition the Swift return buffer handling only to P/Invoke calls. This however can trigger | ||
* a false positive in some scenarios where the Swift return buffer is not needed. | ||
*/ | ||
cinfo->need_swift_return_buffer = TRUE; | ||
} | ||
|
@@ -1286,17 +1287,17 @@ arg_get_val (CallContext *ccontext, ArgInfo *ainfo, gpointer dest) | |
int storage_type = ainfo->pair_storage [k]; | ||
int reg_storage = ainfo->pair_regs [k]; | ||
switch (storage_type) { | ||
case ArgInIReg: | ||
*(gsize*)(storage + ainfo->offsets [k]) = ccontext->gregs [reg_storage]; | ||
break; | ||
case ArgInFloatSSEReg: | ||
*(float*)(storage + ainfo->offsets [k]) = *(float*)&ccontext->fregs [reg_storage]; | ||
break; | ||
case ArgInDoubleSSEReg: | ||
*(double*)(storage + ainfo->offsets [k]) = ccontext->fregs [reg_storage]; | ||
break; | ||
default: | ||
g_assert_not_reached (); | ||
case ArgInIReg: | ||
*(gsize*)(storage + ainfo->offsets [k]) = ccontext->gregs [reg_storage]; | ||
break; | ||
case ArgInFloatSSEReg: | ||
*(float*)(storage + ainfo->offsets [k]) = *(float*)&ccontext->fregs [reg_storage]; | ||
break; | ||
case ArgInDoubleSSEReg: | ||
*(double*)(storage + ainfo->offsets [k]) = ccontext->fregs [reg_storage]; | ||
break; | ||
default: | ||
g_assert_not_reached (); | ||
} | ||
} | ||
break; | ||
|
@@ -1314,11 +1315,13 @@ arg_set_val (CallContext *ccontext, ArgInfo *ainfo, gpointer src) | |
{ | ||
g_assert (arg_need_temp (ainfo)); | ||
|
||
host_mgreg_t *src_cast = (host_mgreg_t*)src; | ||
for (int k = 0; k < ainfo->nregs; k++) { | ||
int storage_type = ainfo->pair_storage [k]; | ||
int reg_storage = ainfo->pair_regs [k]; | ||
switch (storage_type) { | ||
switch (ainfo->storage) { | ||
case ArgValuetypeInReg: { | ||
host_mgreg_t *src_cast = (host_mgreg_t*)src; | ||
for (int k = 0; k < ainfo->nregs; k++) { | ||
int storage_type = ainfo->pair_storage [k]; | ||
int reg_storage = ainfo->pair_regs [k]; | ||
switch (storage_type) { | ||
case ArgInIReg: | ||
ccontext->gregs [reg_storage] = *src_cast; | ||
break; | ||
|
@@ -1328,9 +1331,38 @@ arg_set_val (CallContext *ccontext, ArgInfo *ainfo, gpointer src) | |
break; | ||
default: | ||
g_assert_not_reached (); | ||
} | ||
src_cast++; | ||
} | ||
src_cast++; | ||
break; | ||
} | ||
#ifdef MONO_ARCH_HAVE_SWIFTCALL | ||
case ArgSwiftValuetypeLoweredRet: { | ||
char *storage = (char*)src; | ||
for (int k = 0; k < ainfo->nregs; k++) { | ||
int storage_type = ainfo->pair_storage [k]; | ||
int reg_storage = ainfo->pair_regs [k]; | ||
switch (storage_type) { | ||
case ArgInIReg: | ||
ccontext->gregs [reg_storage] = *(gsize*)(storage + ainfo->offsets [k]); | ||
break; | ||
case ArgInFloatSSEReg: | ||
*(float*)&ccontext->fregs [reg_storage] = *(float*)(storage + ainfo->offsets [k]); | ||
break; | ||
case ArgInDoubleSSEReg: | ||
ccontext->fregs [reg_storage] = *(double*)(storage + ainfo->offsets [k]); | ||
break; | ||
default: | ||
g_assert_not_reached (); | ||
} | ||
} | ||
break; | ||
} | ||
#endif /* MONO_ARCH_HAVE_SWIFTCALL */ | ||
default: | ||
g_assert_not_reached (); | ||
} | ||
|
||
} | ||
|
||
gpointer | ||
|
@@ -2368,6 +2400,9 @@ mono_arch_get_llvm_call_info (MonoCompile *cfg, MonoMethodSignature *sig) | |
linfo->ret.storage = LLVMArgVtypeRetAddr; | ||
linfo->vret_arg_index = cinfo->vret_arg_index; | ||
break; | ||
case ArgSwiftValuetypeLoweredRet: | ||
linfo->ret.storage = LLVMArgNone; // LLVM compilation of pinvoke wrappers is not supported, see emit_method_inner in mini-llvm.c | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should it assert here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, we can't assert here. See #104949 (comment). |
||
break; | ||
default: | ||
g_assert_not_reached (); | ||
break; | ||
|
@@ -2434,6 +2469,9 @@ mono_arch_get_llvm_call_info (MonoCompile *cfg, MonoMethodSignature *sig) | |
case ArgValuetypeAddrOnStack: | ||
linfo->args [i].storage = LLVMArgVtypeAddr; | ||
break; | ||
case ArgSwiftError: | ||
linfo->args [i].storage = LLVMArgNone; // LLVM compilation of pinvoke wrappers is not supported, see emit_method_inner in mini-llvm.c | ||
break; | ||
default: | ||
cfg->exception_message = g_strdup ("ainfo->storage"); | ||
cfg->disable_llvm = TRUE; | ||
|
@@ -8393,8 +8431,19 @@ MONO_RESTORE_WARNING | |
|
||
if (sig->ret->type != MONO_TYPE_VOID) { | ||
/* Save volatile arguments to the stack */ | ||
if (cfg->vret_addr && (cfg->vret_addr->opcode != OP_REGVAR)) | ||
amd64_mov_membase_reg (code, cfg->vret_addr->inst_basereg, cfg->vret_addr->inst_offset, cinfo->ret.reg, 8); | ||
if (cfg->vret_addr && (cfg->vret_addr->opcode != OP_REGVAR)) { | ||
#ifdef MONO_ARCH_HAVE_SWIFTCALL | ||
if (mono_method_signature_has_ext_callconv (sig, MONO_EXT_CALLCONV_SWIFTCALL) && sig->pinvoke && | ||
cfg->method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED && | ||
cfg->arch.cinfo->need_swift_return_buffer && cinfo->ret.reg == AMD64_R10) { | ||
// Save the return buffer passed by the Swift caller | ||
amd64_mov_membase_reg (code, cfg->vret_addr->inst_basereg, cfg->vret_addr->inst_offset, SWIFT_RETURN_BUFFER_REG, 8); | ||
} else | ||
#endif | ||
{ | ||
amd64_mov_membase_reg (code, cfg->vret_addr->inst_basereg, cfg->vret_addr->inst_offset, cinfo->ret.reg, 8); | ||
} | ||
} | ||
} | ||
|
||
#ifdef MONO_ARCH_HAVE_SWIFTCALL | ||
|
@@ -8680,7 +8729,8 @@ mono_arch_emit_epilog (MonoCompile *cfg) | |
|
||
/* Load returned vtypes into registers if needed */ | ||
cinfo = cfg->arch.cinfo; | ||
if (cinfo->ret.storage == ArgValuetypeInReg) { | ||
switch (cinfo->ret.storage) { | ||
case ArgValuetypeInReg: { | ||
ArgInfo *ainfo = &cinfo->ret; | ||
MonoInst *inst = cfg->ret; | ||
|
||
|
@@ -8701,6 +8751,33 @@ mono_arch_emit_epilog (MonoCompile *cfg) | |
g_assert_not_reached (); | ||
} | ||
} | ||
break; | ||
} | ||
#ifdef MONO_ARCH_HAVE_SWIFTCALL | ||
case ArgSwiftValuetypeLoweredRet: { | ||
if (cfg->method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED) { | ||
ArgInfo *ainfo = &cinfo->ret; | ||
MonoInst *ins = cfg->ret; | ||
|
||
for (int i = 0; i < ainfo->nregs; i++) { | ||
switch (ainfo->pair_storage [i]) { | ||
case ArgInIReg: | ||
amd64_mov_reg_membase (code, ainfo->pair_regs [i], ins->inst_basereg, ins->inst_offset + ainfo->offsets [i], sizeof (target_mgreg_t)); | ||
break; | ||
case ArgInFloatSSEReg: | ||
amd64_movss_reg_membase (code, ainfo->pair_regs [i], ins->inst_basereg, ins->inst_offset + ainfo->offsets [i]); | ||
break; | ||
case ArgInDoubleSSEReg: | ||
amd64_movsd_reg_membase (code, ainfo->pair_regs [i], ins->inst_basereg, ins->inst_offset + ainfo->offsets [i]); | ||
break; | ||
default: | ||
g_assert_not_reached (); | ||
} | ||
} | ||
} | ||
break; | ||
} | ||
#endif /* MONO_ARCH_HAVE_SWIFTCALL */ | ||
} | ||
|
||
if (cfg->arch.omit_fp) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
What are those false positive triggering scenarios?
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.
Mostly cases where struct is large enough that it would normally (outside swiftcall) be returned by a reference but small enough that during swiftcall it is returned via registers.