From fabf6791d04101c60bc079996806027df20dd283 Mon Sep 17 00:00:00 2001 From: "Aman Khalid (from Dev Box)" Date: Thu, 29 Feb 2024 13:35:47 -0500 Subject: [PATCH 1/4] Enable test locally --- src/tests/Interop/CMakeLists.txt | 1 + src/tests/Interop/Swift/SwiftSelfContext/CMakeLists.txt | 2 +- .../Interop/Swift/SwiftSelfContext/SwiftSelfContext.csproj | 4 ++-- src/tests/issues.targets | 3 --- 4 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/tests/Interop/CMakeLists.txt b/src/tests/Interop/CMakeLists.txt index 80958de80bea5c..32fa7da1230ecf 100644 --- a/src/tests/Interop/CMakeLists.txt +++ b/src/tests/Interop/CMakeLists.txt @@ -63,6 +63,7 @@ add_subdirectory(LayoutClass) add_subdirectory(PInvoke/DateTime) add_subdirectory(DisabledRuntimeMarshalling) add_subdirectory(MonoAPI/Native) +add_subdirectory(Swift/SwiftSelfContext) if(CLR_CMAKE_TARGET_WIN32) add_subdirectory(ExecInDefAppDom) add_subdirectory(PInvoke/Attributes/LCID) diff --git a/src/tests/Interop/Swift/SwiftSelfContext/CMakeLists.txt b/src/tests/Interop/Swift/SwiftSelfContext/CMakeLists.txt index 8d7efa1eea6757..1052f3d05ba76e 100644 --- a/src/tests/Interop/Swift/SwiftSelfContext/CMakeLists.txt +++ b/src/tests/Interop/Swift/SwiftSelfContext/CMakeLists.txt @@ -11,7 +11,7 @@ if (NOT SWIFT_COMPILER_TARGET AND CLR_CMAKE_TARGET_OSX) endif() add_custom_target(${SOURCE} ALL - COMMAND xcrun swiftc -target ${SWIFT_COMPILER_TARGET} -emit-library ${CMAKE_CURRENT_SOURCE_DIR}/${SOURCE}.swift -o ${CMAKE_CURRENT_BINARY_DIR}/lib${SOURCE}.dylib + COMMAND swiftc -emit-library ${CMAKE_CURRENT_SOURCE_DIR}/${SOURCE}.swift -o ${CMAKE_CURRENT_BINARY_DIR}/lib${SOURCE}.dylib DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${SOURCE}.swift COMMENT "Generating ${SOURCE} library" ) diff --git a/src/tests/Interop/Swift/SwiftSelfContext/SwiftSelfContext.csproj b/src/tests/Interop/Swift/SwiftSelfContext/SwiftSelfContext.csproj index 49be10b9393911..0b5387f7900a9a 100644 --- a/src/tests/Interop/Swift/SwiftSelfContext/SwiftSelfContext.csproj +++ b/src/tests/Interop/Swift/SwiftSelfContext/SwiftSelfContext.csproj @@ -4,9 +4,9 @@ true true - true + - true + diff --git a/src/tests/issues.targets b/src/tests/issues.targets index 2ab3b36bf3fb72..9138cc7b845dc7 100644 --- a/src/tests/issues.targets +++ b/src/tests/issues.targets @@ -78,9 +78,6 @@ https://github.com/dotnet/runtime/issues/93631 - - https://github.com/dotnet/runtime/issues/93631 - From 843333fe47eafc34eb49b0009a409bc2e67c4d53 Mon Sep 17 00:00:00 2001 From: "Aman Khalid (from Dev Box)" Date: Thu, 29 Feb 2024 18:29:29 -0500 Subject: [PATCH 2/4] Set SwiftSelf register --- src/coreclr/jit/compiler.h | 2 +- src/coreclr/jit/gentree.cpp | 9 ++++++++ src/coreclr/jit/gentree.h | 4 +++- src/coreclr/jit/importercalls.cpp | 36 ++++++++++++++++++++++++------- src/coreclr/jit/morph.cpp | 5 ++++- src/coreclr/jit/targetamd64.h | 2 +- src/coreclr/jit/targetarm64.h | 2 +- 7 files changed, 47 insertions(+), 13 deletions(-) diff --git a/src/coreclr/jit/compiler.h b/src/coreclr/jit/compiler.h index d94aca0e4b7958..2f9bb87ed8e28b 100644 --- a/src/coreclr/jit/compiler.h +++ b/src/coreclr/jit/compiler.h @@ -4367,7 +4367,7 @@ class Compiler void impCheckForPInvokeCall( GenTreeCall* call, CORINFO_METHOD_HANDLE methHnd, CORINFO_SIG_INFO* sig, unsigned mflags, BasicBlock* block); GenTreeCall* impImportIndirectCall(CORINFO_SIG_INFO* sig, const DebugInfo& di = DebugInfo()); - void impPopArgsForUnmanagedCall(GenTreeCall* call, CORINFO_SIG_INFO* sig, /* OUT */ CallArg** swiftErrorArg, /* OUT */ CallArg** swiftSelfArg); + void impPopArgsForUnmanagedCall(GenTreeCall* call, CORINFO_SIG_INFO* sig, /* OUT */ CallArg** swiftErrorArg); #ifdef SWIFT_SUPPORT void impAppendSwiftErrorStore(GenTreeCall* call, CallArg* const swiftErrorArg); diff --git a/src/coreclr/jit/gentree.cpp b/src/coreclr/jit/gentree.cpp index 08ee377991256a..51df91195fc041 100644 --- a/src/coreclr/jit/gentree.cpp +++ b/src/coreclr/jit/gentree.cpp @@ -1843,6 +1843,13 @@ regNumber CallArgs::GetCustomRegister(Compiler* comp, CorInfoCallConvExtension c case WellKnownArg::DispatchIndirectCallTarget: return REG_DISPATCH_INDIRECT_CALL_ADDR; #endif + +#ifdef SWIFT_SUPPORT + case WellKnownArg::SwiftSelf: + assert(cc == CorInfoCallConvExtension::Swift); + return REG_SWIFT_SELF; +#endif // SWIFT_SUPPORT + default: break; } @@ -13091,6 +13098,8 @@ const char* Compiler::gtGetWellKnownArgNameForArgMsg(WellKnownArg arg) case WellKnownArg::ValidateIndirectCallTarget: case WellKnownArg::DispatchIndirectCallTarget: return "cfg tgt"; + case WellKnownArg::SwiftSelf: + return "swift self"; default: return nullptr; } diff --git a/src/coreclr/jit/gentree.h b/src/coreclr/jit/gentree.h index 30d8b2dbd42cb9..a56a0e13ef85c7 100644 --- a/src/coreclr/jit/gentree.h +++ b/src/coreclr/jit/gentree.h @@ -4401,7 +4401,7 @@ enum class CFGCallKind class CallArgs; -enum class WellKnownArg +enum class WellKnownArg : unsigned { None, ThisPointer, @@ -4419,6 +4419,7 @@ enum class WellKnownArg R2RIndirectionCell, ValidateIndirectCallTarget, DispatchIndirectCallTarget, + SwiftSelf, }; #ifdef DEBUG @@ -4728,6 +4729,7 @@ class CallArg CORINFO_CLASS_HANDLE GetSignatureClassHandle() { return m_signatureClsHnd; } var_types GetSignatureType() { return m_signatureType; } WellKnownArg GetWellKnownArg() { return m_wellKnownArg; } + void SetWellKnownArg(const WellKnownArg argType) { m_wellKnownArg = argType; } bool IsTemp() { return m_isTmp; } // clang-format on diff --git a/src/coreclr/jit/importercalls.cpp b/src/coreclr/jit/importercalls.cpp index f0ae5cf97bbaae..83c4e3ff43ca94 100644 --- a/src/coreclr/jit/importercalls.cpp +++ b/src/coreclr/jit/importercalls.cpp @@ -98,10 +98,9 @@ var_types Compiler::impImportCall(OPCODE opcode, CORINFO_SIG_INFO calliSig; NewCallArg extraArg; - // Swift calls may use special register types that require additional IR to handle, - // so if we're importing a Swift call, look for these types in the signature + // Swift calls that might throw use a SwiftError* arg that requires additional IR to handle, + // so if we're importing a Swift call, look for this type in the signature CallArg* swiftErrorArg = nullptr; - CallArg* swiftSelfArg = nullptr; /*------------------------------------------------------------------------- * First create the call node @@ -670,7 +669,7 @@ var_types Compiler::impImportCall(OPCODE opcode, checkForSmallType = true; - impPopArgsForUnmanagedCall(call->AsCall(), sig, &swiftErrorArg, &swiftSelfArg); + impPopArgsForUnmanagedCall(call->AsCall(), sig, &swiftErrorArg); goto DONE; } @@ -1840,8 +1839,7 @@ GenTreeCall* Compiler::impImportIndirectCall(CORINFO_SIG_INFO* sig, const DebugI void Compiler::impPopArgsForUnmanagedCall(GenTreeCall* call, CORINFO_SIG_INFO* sig, - /* OUT */ CallArg** swiftErrorArg, - /* OUT */ CallArg** swiftSelfArg) + /* OUT */ CallArg** swiftErrorArg) { assert(call->gtFlags & GTF_CALL_UNMANAGED); @@ -1867,6 +1865,7 @@ void Compiler::impPopArgsForUnmanagedCall(GenTreeCall* call, #ifdef SWIFT_SUPPORT unsigned short swiftErrorIndex = sig->numArgs; + unsigned short swiftSelfIndex = sig->numArgs; // We are importing an unmanaged Swift call, which might require special parameter handling if (call->unmgdCallConv == CorInfoCallConvExtension::Swift) @@ -1915,13 +1914,29 @@ void Compiler::impPopArgsForUnmanagedCall(GenTreeCall* call, swiftErrorIndex = argIndex; checkEntireStack = true; } - // TODO: Handle SwiftSelf, SwiftAsync + else if ((strcmp(className, "SwiftSelf") == 0) && + (strcmp(namespaceName, "System.Runtime.InteropServices.Swift") == 0)) + { + // We expect a SwiftSelf struct to be passed, not a pointer/reference + if (argIsByrefOrPtr) + { + BADCODE("Expected SwiftSelf struct, got pointer/reference"); + } + + if (swiftSelfIndex != sig->numArgs) + { + BADCODE("Duplicate SwiftSelf parameter"); + } + + swiftSelfIndex = argIndex; + } + // TODO: Handle SwiftAsync } // Don't need to reverse args for Swift calls argsToReverse = 0; - // If using one of the Swift register types, check entire stack for side effects + // If using SwiftError*, check entire stack for side effects if (checkEntireStack) { impSpillSideEffects(true, CHECK_SPILL_ALL DEBUGARG("Spill for swift calls")); @@ -2006,6 +2021,11 @@ void Compiler::impPopArgsForUnmanagedCall(GenTreeCall* call, assert(swiftErrorArg != nullptr); *swiftErrorArg = &arg; } + else if (argIndex == swiftSelfIndex) + { + // Found the SwiftSelf arg + arg.SetWellKnownArg(WellKnownArg::SwiftSelf); + } // TODO: SwiftSelf, SwiftAsync #endif // SWIFT_SUPPORT diff --git a/src/coreclr/jit/morph.cpp b/src/coreclr/jit/morph.cpp index b68f4518dfcba2..79a91a780cec10 100644 --- a/src/coreclr/jit/morph.cpp +++ b/src/coreclr/jit/morph.cpp @@ -765,6 +765,8 @@ const char* getWellKnownArgName(WellKnownArg arg) return "ValidateIndirectCallTarget"; case WellKnownArg::DispatchIndirectCallTarget: return "DispatchIndirectCallTarget"; + case WellKnownArg::SwiftSelf: + return "SwiftSelf"; } return "N/A"; @@ -3179,7 +3181,8 @@ GenTreeCall* Compiler::fgMorphArgs(GenTreeCall* call) } // TODO-ARGS: Review this, is it really necessary to treat them specially here? - if (call->gtArgs.IsNonStandard(this, call, &arg) && arg.AbiInfo.IsPassedInRegisters()) + // Exception: Lower SwiftSelf struct arg to GT_LCL_FLD + if (call->gtArgs.IsNonStandard(this, call, &arg) && arg.AbiInfo.IsPassedInRegisters() && (arg.GetWellKnownArg() != WellKnownArg::SwiftSelf)) { flagsSummary |= argx->gtFlags; continue; diff --git a/src/coreclr/jit/targetamd64.h b/src/coreclr/jit/targetamd64.h index 147355c2474a24..09d6729f9dc6cb 100644 --- a/src/coreclr/jit/targetamd64.h +++ b/src/coreclr/jit/targetamd64.h @@ -566,6 +566,6 @@ #define SWIFT_SUPPORT #define REG_SWIFT_ERROR REG_R12 #define RBM_SWIFT_ERROR RBM_R12 - #define SWIFT_SELF_REG REG_R13 + #define REG_SWIFT_SELF REG_R13 // clang-format on diff --git a/src/coreclr/jit/targetarm64.h b/src/coreclr/jit/targetarm64.h index 74a14535f15075..ce038021bc525a 100644 --- a/src/coreclr/jit/targetarm64.h +++ b/src/coreclr/jit/targetarm64.h @@ -373,6 +373,6 @@ #define SWIFT_SUPPORT #define REG_SWIFT_ERROR REG_R21 #define RBM_SWIFT_ERROR RBM_R21 - #define SWIFT_SELF_REG REG_R20 + #define REG_SWIFT_SELF REG_R20 // clang-format on From 3cfebf096f3ef2347feba5de6aa12e03ae8a4126 Mon Sep 17 00:00:00 2001 From: "Aman Khalid (from Dev Box)" Date: Thu, 29 Feb 2024 18:44:50 -0500 Subject: [PATCH 3/4] Style --- src/coreclr/jit/importercalls.cpp | 2 +- src/coreclr/jit/morph.cpp | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/coreclr/jit/importercalls.cpp b/src/coreclr/jit/importercalls.cpp index 83c4e3ff43ca94..4fb75b7d398092 100644 --- a/src/coreclr/jit/importercalls.cpp +++ b/src/coreclr/jit/importercalls.cpp @@ -2026,7 +2026,7 @@ void Compiler::impPopArgsForUnmanagedCall(GenTreeCall* call, // Found the SwiftSelf arg arg.SetWellKnownArg(WellKnownArg::SwiftSelf); } -// TODO: SwiftSelf, SwiftAsync +// TODO: SwiftAsync #endif // SWIFT_SUPPORT argIndex++; diff --git a/src/coreclr/jit/morph.cpp b/src/coreclr/jit/morph.cpp index 79a91a780cec10..4da215607458eb 100644 --- a/src/coreclr/jit/morph.cpp +++ b/src/coreclr/jit/morph.cpp @@ -3182,7 +3182,8 @@ GenTreeCall* Compiler::fgMorphArgs(GenTreeCall* call) // TODO-ARGS: Review this, is it really necessary to treat them specially here? // Exception: Lower SwiftSelf struct arg to GT_LCL_FLD - if (call->gtArgs.IsNonStandard(this, call, &arg) && arg.AbiInfo.IsPassedInRegisters() && (arg.GetWellKnownArg() != WellKnownArg::SwiftSelf)) + if (call->gtArgs.IsNonStandard(this, call, &arg) && arg.AbiInfo.IsPassedInRegisters() && + (arg.GetWellKnownArg() != WellKnownArg::SwiftSelf)) { flagsSummary |= argx->gtFlags; continue; From 434575de164a653af0500511f3456c15d10815a8 Mon Sep 17 00:00:00 2001 From: "Aman Khalid (from Dev Box)" Date: Thu, 29 Feb 2024 18:54:36 -0500 Subject: [PATCH 4/4] Revert test changes --- src/tests/Interop/CMakeLists.txt | 1 - src/tests/Interop/Swift/SwiftSelfContext/CMakeLists.txt | 2 +- .../Interop/Swift/SwiftSelfContext/SwiftSelfContext.csproj | 4 +--- 3 files changed, 2 insertions(+), 5 deletions(-) diff --git a/src/tests/Interop/CMakeLists.txt b/src/tests/Interop/CMakeLists.txt index 32fa7da1230ecf..80958de80bea5c 100644 --- a/src/tests/Interop/CMakeLists.txt +++ b/src/tests/Interop/CMakeLists.txt @@ -63,7 +63,6 @@ add_subdirectory(LayoutClass) add_subdirectory(PInvoke/DateTime) add_subdirectory(DisabledRuntimeMarshalling) add_subdirectory(MonoAPI/Native) -add_subdirectory(Swift/SwiftSelfContext) if(CLR_CMAKE_TARGET_WIN32) add_subdirectory(ExecInDefAppDom) add_subdirectory(PInvoke/Attributes/LCID) diff --git a/src/tests/Interop/Swift/SwiftSelfContext/CMakeLists.txt b/src/tests/Interop/Swift/SwiftSelfContext/CMakeLists.txt index 1052f3d05ba76e..8d7efa1eea6757 100644 --- a/src/tests/Interop/Swift/SwiftSelfContext/CMakeLists.txt +++ b/src/tests/Interop/Swift/SwiftSelfContext/CMakeLists.txt @@ -11,7 +11,7 @@ if (NOT SWIFT_COMPILER_TARGET AND CLR_CMAKE_TARGET_OSX) endif() add_custom_target(${SOURCE} ALL - COMMAND swiftc -emit-library ${CMAKE_CURRENT_SOURCE_DIR}/${SOURCE}.swift -o ${CMAKE_CURRENT_BINARY_DIR}/lib${SOURCE}.dylib + COMMAND xcrun swiftc -target ${SWIFT_COMPILER_TARGET} -emit-library ${CMAKE_CURRENT_SOURCE_DIR}/${SOURCE}.swift -o ${CMAKE_CURRENT_BINARY_DIR}/lib${SOURCE}.dylib DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${SOURCE}.swift COMMENT "Generating ${SOURCE} library" ) diff --git a/src/tests/Interop/Swift/SwiftSelfContext/SwiftSelfContext.csproj b/src/tests/Interop/Swift/SwiftSelfContext/SwiftSelfContext.csproj index 0b5387f7900a9a..89eda99352fd20 100644 --- a/src/tests/Interop/Swift/SwiftSelfContext/SwiftSelfContext.csproj +++ b/src/tests/Interop/Swift/SwiftSelfContext/SwiftSelfContext.csproj @@ -4,9 +4,7 @@ true true - - - + true