Skip to content

Commit f48a99e

Browse files
authored
Parse CallConvSwift in CoreCLR/NativeAOT (#96707)
1 parent 5155263 commit f48a99e

File tree

12 files changed

+60
-10
lines changed

12 files changed

+60
-10
lines changed

src/coreclr/inc/corhdr.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1715,6 +1715,7 @@ typedef enum LoadHintEnum
17151715
#define CMOD_CALLCONV_NAME_STDCALL "CallConvStdcall"
17161716
#define CMOD_CALLCONV_NAME_THISCALL "CallConvThiscall"
17171717
#define CMOD_CALLCONV_NAME_FASTCALL "CallConvFastcall"
1718+
#define CMOD_CALLCONV_NAME_SWIFT "CallConvSwift"
17181719
#define CMOD_CALLCONV_NAME_SUPPRESSGCTRANSITION "CallConvSuppressGCTransition"
17191720
#define CMOD_CALLCONV_NAME_MEMBERFUNCTION "CallConvMemberFunction"
17201721

src/coreclr/inc/corinfo.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,8 @@ enum class CorInfoCallConvExtension
772772
// New calling conventions supported with the extensible calling convention encoding go here.
773773
CMemberFunction,
774774
StdcallMemberFunction,
775-
FastcallMemberFunction
775+
FastcallMemberFunction,
776+
Swift
776777
};
777778

778779
#ifdef TARGET_X86

src/coreclr/jit/importercalls.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5605,7 +5605,8 @@ void Compiler::impCheckForPInvokeCall(
56055605
// return here without inlining the native call.
56065606
if (unmanagedCallConv == CorInfoCallConvExtension::Managed ||
56075607
unmanagedCallConv == CorInfoCallConvExtension::Fastcall ||
5608-
unmanagedCallConv == CorInfoCallConvExtension::FastcallMemberFunction)
5608+
unmanagedCallConv == CorInfoCallConvExtension::FastcallMemberFunction ||
5609+
unmanagedCallConv == CorInfoCallConvExtension::Swift)
56095610
{
56105611
return;
56115612
}

src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1589,6 +1589,9 @@ private static CorInfoCallConvExtension ToCorInfoCallConvExtension(UnmanagedCall
15891589
case UnmanagedCallingConventions.Fastcall:
15901590
result = CorInfoCallConvExtension.Fastcall;
15911591
break;
1592+
case UnmanagedCallingConventions.Swift:
1593+
result = CorInfoCallConvExtension.Swift;
1594+
break;
15921595
default:
15931596
ThrowHelper.ThrowInvalidProgramException();
15941597
result = CorInfoCallConvExtension.Managed; // unreachable
@@ -4124,7 +4127,7 @@ private uint getJitFlags(ref CORJIT_FLAGS flags, uint sizeInBytes)
41244127

41254128
#if READYTORUN
41264129
// TODO: enable this check in full AOT
4127-
if (Marshaller.IsMarshallingRequired(this.MethodBeingCompiled.Signature, Array.Empty<ParameterMetadata>(), ((MetadataType)this.MethodBeingCompiled.OwningType).Module)) // Only blittable arguments
4130+
if (Marshaller.IsMarshallingRequired(this.MethodBeingCompiled.Signature, ((MetadataType)this.MethodBeingCompiled.OwningType).Module, this.MethodBeingCompiled.GetUnmanagedCallersOnlyMethodCallingConventions())) // Only blittable arguments
41284131
{
41294132
ThrowHelper.ThrowInvalidProgramException(ExceptionStringID.InvalidProgramNonBlittableTypes, this.MethodBeingCompiled);
41304133
}

src/coreclr/tools/Common/JitInterface/CorInfoTypes.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,8 @@ public enum CorInfoCallConvExtension
392392
// New calling conventions supported with the extensible calling convention encoding go here.
393393
CMemberFunction,
394394
StdcallMemberFunction,
395-
FastcallMemberFunction
395+
FastcallMemberFunction,
396+
Swift
396397
}
397398

398399
public enum CORINFO_CALLINFO_FLAGS

src/coreclr/tools/Common/TypeSystem/Interop/UnmanagedCallingConventions.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public enum UnmanagedCallingConventions
4040
// Unmanaged = 0x00000009, - this one is always translated to cdecl/stdcall
4141

4242
// The ones higher than 0xF are defined by the type system
43-
// There are no such calling conventions yet.
43+
Swift = 0x00000010
4444
}
4545

4646
public static class CallingConventionExtensions
@@ -135,6 +135,25 @@ public static UnmanagedCallingConventions GetPInvokeMethodCallingConventions(thi
135135
return result;
136136
}
137137

138+
public static UnmanagedCallingConventions GetDelegateCallingConventions(this TypeDesc delegateType)
139+
{
140+
Debug.Assert(delegateType.IsDelegate);
141+
142+
if (delegateType is EcmaType ecmaDelegate)
143+
{
144+
MethodSignatureFlags unmanagedCallConv = ecmaDelegate.GetDelegatePInvokeFlags().UnmanagedCallingConvention;
145+
if (unmanagedCallConv != MethodSignatureFlags.None)
146+
{
147+
Debug.Assert((int)MethodSignatureFlags.UnmanagedCallingConventionCdecl == (int)UnmanagedCallingConventions.Cdecl
148+
&& (int)MethodSignatureFlags.UnmanagedCallingConventionStdCall == (int)UnmanagedCallingConventions.Stdcall
149+
&& (int)MethodSignatureFlags.UnmanagedCallingConventionThisCall == (int)UnmanagedCallingConventions.Thiscall);
150+
return (UnmanagedCallingConventions)unmanagedCallConv;
151+
}
152+
}
153+
154+
return GetPlatformDefaultUnmanagedCallingConvention(delegateType.Context);
155+
}
156+
138157
private static UnmanagedCallingConventions GetUnmanagedCallingConventionFromAttribute(CustomAttributeValue<TypeDesc> attributeWithCallConvsArray, TypeSystemContext context)
139158
{
140159
ImmutableArray<CustomAttributeTypedArgument<TypeDesc>> callConvArray = default;
@@ -181,6 +200,7 @@ private static UnmanagedCallingConventions AccumulateCallingConventions(Unmanage
181200
"CallConvThiscall" => UnmanagedCallingConventions.Thiscall,
182201
"CallConvSuppressGCTransition" => UnmanagedCallingConventions.IsSuppressGcTransition,
183202
"CallConvMemberFunction" => UnmanagedCallingConventions.IsMemberFunction,
203+
"CallConvSwift" => UnmanagedCallingConventions.Swift,
184204
_ => null
185205
};
186206

@@ -218,6 +238,7 @@ public static EmbeddedSignatureData[] EncodeAsEmbeddedSignatureData(this Unmanag
218238
UnmanagedCallingConventions.Stdcall => "CallConvStdcall",
219239
UnmanagedCallingConventions.Fastcall => "CallConvFastcall",
220240
UnmanagedCallingConventions.Thiscall => "CallConvThiscall",
241+
UnmanagedCallingConventions.Swift => "CallConvSwift",
221242
_ => throw new InvalidProgramException()
222243
});
223244
}

src/coreclr/tools/aot/ILCompiler.ReadyToRun/Interop/IL/Marshaller.ReadyToRun.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,18 @@ public static bool IsMarshallingRequired(MethodDesc targetMethod)
130130
return false;
131131
}
132132

133+
public static bool IsMarshallingRequired(MethodSignature methodSig, ModuleDesc moduleContext, UnmanagedCallingConventions callingConvention)
134+
{
135+
Marshaller[] marshallers = GetMarshallersForSignature(methodSig, System.Array.Empty<ParameterMetadata>(), moduleContext);
136+
for (int i = 0; i < marshallers.Length; i++)
137+
{
138+
if (marshallers[i].IsMarshallingRequired())
139+
return true;
140+
}
141+
142+
return false;
143+
}
144+
133145
public static bool IsMarshallingRequired(MethodSignature methodSig, ParameterMetadata[] paramMetadata, ModuleDesc moduleContext)
134146
{
135147
Marshaller[] marshallers = GetMarshallersForSignature(methodSig, paramMetadata, moduleContext);

src/coreclr/vm/callconvbuilder.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ namespace
5353
BASE_CALL_CONV(CMOD_CALLCONV_NAME_CDECL, C) \
5454
BASE_CALL_CONV(CMOD_CALLCONV_NAME_STDCALL, Stdcall) \
5555
BASE_CALL_CONV(CMOD_CALLCONV_NAME_THISCALL, Thiscall) \
56-
BASE_CALL_CONV(CMOD_CALLCONV_NAME_FASTCALL, Fastcall)
56+
BASE_CALL_CONV(CMOD_CALLCONV_NAME_FASTCALL, Fastcall) \
57+
BASE_CALL_CONV(CMOD_CALLCONV_NAME_SWIFT, Swift)
5758

5859
#define DECLARE_MOD_CALL_CONVS \
5960
CALL_CONV_MODIFIER(CMOD_CALLCONV_NAME_SUPPRESSGCTRANSITION, CALL_CONV_MOD_SUPPRESSGCTRANSITION) \
@@ -176,6 +177,8 @@ namespace
176177
return CorInfoCallConvExtension::FastcallMemberFunction;
177178
case CorInfoCallConvExtension::Thiscall:
178179
return CorInfoCallConvExtension::Thiscall;
180+
case CorInfoCallConvExtension::Swift:
181+
return CorInfoCallConvExtension::Swift;
179182
default:
180183
_ASSERTE("Calling convention is not an unmanaged base calling convention.");
181184
return baseCallConv;

src/coreclr/vm/corelib.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,7 @@ DEFINE_CLASS(CALLCONV_THISCALL, CompilerServices, CallConvThi
724724
DEFINE_CLASS(CALLCONV_FASTCALL, CompilerServices, CallConvFastcall)
725725
DEFINE_CLASS(CALLCONV_SUPPRESSGCTRANSITION, CompilerServices, CallConvSuppressGCTransition)
726726
DEFINE_CLASS(CALLCONV_MEMBERFUNCTION, CompilerServices, CallConvMemberFunction)
727+
DEFINE_CLASS(CALLCONV_SWIFT, CompilerServices, CallConvSwift)
727728

728729
DEFINE_CLASS_U(Interop, SafeHandle, SafeHandle)
729730
DEFINE_FIELD_U(_ctorStackTrace, SafeHandle, m_ctorStackTrace)

src/coreclr/vm/dllimport.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3167,7 +3167,6 @@ HRESULT NDirect::HasNAT_LAttribute(IMDInternalImport *pInternalImport, mdToken t
31673167
return S_FALSE;
31683168
}
31693169

3170-
31713170
// Either MD or signature & module must be given.
31723171
/*static*/
31733172
BOOL NDirect::MarshalingRequired(
@@ -4257,7 +4256,8 @@ static void CreateNDirectStubAccessMetadata(
42574256
{
42584257
if (unmgdCallConv == CorInfoCallConvExtension::Managed ||
42594258
unmgdCallConv == CorInfoCallConvExtension::Fastcall ||
4260-
unmgdCallConv == CorInfoCallConvExtension::FastcallMemberFunction)
4259+
unmgdCallConv == CorInfoCallConvExtension::FastcallMemberFunction ||
4260+
unmgdCallConv == CorInfoCallConvExtension::Swift)
42614261
{
42624262
COMPlusThrow(kTypeLoadException, IDS_INVALID_PINVOKE_CALLCONV);
42634263
}

src/coreclr/vm/stubgen.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2819,6 +2819,9 @@ void ILStubLinker::SetStubTargetCallingConv(CorInfoCallConvExtension callConv)
28192819
m_nativeFnSigBuilder.AddCallConvModOpt(GetToken(CoreLibBinder::GetClass(CLASS__CALLCONV_FASTCALL)));
28202820
m_nativeFnSigBuilder.AddCallConvModOpt(GetToken(CoreLibBinder::GetClass(CLASS__CALLCONV_MEMBERFUNCTION)));
28212821
break;
2822+
case CorInfoCallConvExtension::Swift:
2823+
m_nativeFnSigBuilder.AddCallConvModOpt(GetToken(CoreLibBinder::GetClass(CLASS__CALLCONV_SWIFT)));
2824+
break;
28222825
default:
28232826
_ASSERTE("Unknown calling convention. Unable to encode it in the native function pointer signature.");
28242827
break;

src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Swift/SwiftTypes.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
3+
using System.Runtime.CompilerServices;
34

45
namespace System.Runtime.InteropServices.Swift
56
{
@@ -19,7 +20,8 @@ namespace System.Runtime.InteropServices.Swift
1920
/// </code>
2021
/// </para>
2122
/// </remarks>
22-
[CLSCompliantAttribute(false)]
23+
[CLSCompliant(false)]
24+
[Intrinsic]
2325
public readonly unsafe struct SwiftSelf
2426
{
2527
/// <summary>
@@ -52,7 +54,8 @@ public SwiftSelf(void* value)
5254
/// </code>
5355
/// </para>
5456
/// </remarks>
55-
[CLSCompliantAttribute(false)]
57+
[CLSCompliant(false)]
58+
[Intrinsic]
5659
public readonly unsafe struct SwiftError
5760
{
5861
/// <summary>

0 commit comments

Comments
 (0)