Skip to content

Commit

Permalink
[release/9.0] Switch SwiftSelf<T> position requirement to last (#10…
Browse files Browse the repository at this point in the history
…8856)

* JIT: Switch `SwiftSelf<T>` position requirement to last

This is added as the last parameter by the Swift compiler, so to support
instance calls taking parameters this needs to come last.

* Disable failing test on Mono

---------

Co-authored-by: Jakob Botsch Nielsen <jakob.botsch.nielsen@gmail.com>
Co-authored-by: Jeff Schwartz <jeffschw@microsoft.com>
  • Loading branch information
3 people authored Oct 15, 2024
1 parent b182a0b commit dd301b7
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/coreclr/jit/importercalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2203,9 +2203,9 @@ void Compiler::impPopArgsForSwiftCall(GenTreeCall* call, CORINFO_SIG_INFO* sig,
BADCODE("Duplicate SwiftSelf parameter");
}

if (argIndex != 0)
if (argIndex != (sig->numArgs - 1))
{
BADCODE("SwiftSelf<T> must be the first argument in the signature");
BADCODE("SwiftSelf<T> must be the last argument in the signature");
}

selfType = info.compCompHnd->getTypeInstantiationArgument(argClass, 0);
Expand Down
4 changes: 2 additions & 2 deletions src/mono/mono/metadata/marshal.c
Original file line number Diff line number Diff line change
Expand Up @@ -3714,9 +3714,9 @@ mono_marshal_get_native_wrapper (MonoMethod *method, gboolean check_exceptions,
break;
} else if (param_klass == swift_error || param_klass == swift_error_ptr) {
swift_error_args++;
} else if (param_gklass && (param_gklass->container_class == swift_self_t) && i > 0) {
} else if (param_gklass && (param_gklass->container_class == swift_self_t) && (i != method->signature->param_count - 1)) {
swift_error_args = swift_self_args = 0;
mono_error_set_generic_error (emitted_error, "System", "InvalidProgramException", "SwiftSelf<T> must be the first argument in the signature.");
mono_error_set_generic_error (emitted_error, "System", "InvalidProgramException", "SwiftSelf<T> must be the last argument in the signature.");
break;
} else if (param_gklass && (param_gklass->container_class == swift_self_t) && m_type_is_byref (method->signature->params [i])) {
swift_error_args = swift_self_args = 0;
Expand Down
27 changes: 25 additions & 2 deletions src/tests/Interop/Swift/SwiftSelfContext/SwiftSelfContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ public struct FrozenNonEnregisteredStruct
}

[UnmanagedCallConv(CallConvs = new Type[] { typeof(CallConvSwift) })]
[DllImport(SwiftLib, EntryPoint = "$s16SwiftSelfContext24FrozenEnregisteredStructV3Sums5Int64VyF")]
[DllImport(SwiftLib, EntryPoint = "$s16SwiftSelfContext24FrozenEnregisteredStructV3sums5Int64VyF")]
public static extern long SumFrozenEnregisteredStruct(SwiftSelf<FrozenEnregisteredStruct> self);

[UnmanagedCallConv(CallConvs = new Type[] { typeof(CallConvSwift) })]
[DllImport(SwiftLib, EntryPoint = "$s16SwiftSelfContext27FrozenNonEnregisteredStructV3Sums5Int64VyF")]
[DllImport(SwiftLib, EntryPoint = "$s16SwiftSelfContext27FrozenNonEnregisteredStructV3sums5Int64VyF")]
public static extern long SumFrozenNonEnregisteredStruct(SwiftSelf<FrozenNonEnregisteredStruct> self);

[Fact]
Expand All @@ -81,4 +81,27 @@ public unsafe static void TestSelfIsFrozenNonEnregisteredStruct()
long sum = SumFrozenNonEnregisteredStruct(new SwiftSelf<FrozenNonEnregisteredStruct>(new FrozenNonEnregisteredStruct { A = 10, B = 20, C = 30, D = 40, E = 50 }));
Assert.Equal(150, sum);
}

[UnmanagedCallConv(CallConvs = new Type[] { typeof(CallConvSwift) })]
[DllImport(SwiftLib, EntryPoint = "$s16SwiftSelfContext24FrozenEnregisteredStructV16sumWithExtraArgs1c1dS2f_SftF")]
public static extern float SumFrozenEnregisteredStructWithExtraArgs(float c, float d, SwiftSelf<FrozenEnregisteredStruct> self);

[UnmanagedCallConv(CallConvs = new Type[] { typeof(CallConvSwift) })]
[DllImport(SwiftLib, EntryPoint = "$s16SwiftSelfContext27FrozenNonEnregisteredStructV16sumWithExtraArgs1f1gS2f_SftF")]
public static extern float SumFrozenNonEnregisteredStructWithExtraArgs(float f, float g, SwiftSelf<FrozenNonEnregisteredStruct> self);

[Fact]
public unsafe static void TestSelfIsFrozenEnregisteredStructWithExtraArgs()
{
float sum = SumFrozenEnregisteredStructWithExtraArgs(3f, 4f, new SwiftSelf<FrozenEnregisteredStruct>(new FrozenEnregisteredStruct { A = 10, B = 20 }));
Assert.Equal(37f, sum);
}

[Fact]
[SkipOnMono("https://github.com/dotnet/runtime/issues/108855")]
public unsafe static void TestSelfIsFrozenNonEnregisteredStructWithExtraArgs()
{
float sum = SumFrozenNonEnregisteredStructWithExtraArgs(3f, 4f, new SwiftSelf<FrozenNonEnregisteredStruct>(new FrozenNonEnregisteredStruct { A = 10, B = 20, C = 30, D = 40, E = 50 }));
Assert.Equal(157f, sum);
}
}
14 changes: 11 additions & 3 deletions src/tests/Interop/Swift/SwiftSelfContext/SwiftSelfContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ public struct FrozenEnregisteredStruct
let a : Int64;
let b : Int64;

public func Sum() -> Int64 {
public func sum() -> Int64 {
return a + b
}

public func sumWithExtraArgs(c: Float, d: Float) -> Float {
return Float(a + b) + c + d
}
}

@frozen
Expand All @@ -39,7 +43,11 @@ public struct FrozenNonEnregisteredStruct {
let d : Int64;
let e : Int64;

public func Sum() -> Int64 {
public func sum() -> Int64 {
return a + b + c + d + e
}
}

public func sumWithExtraArgs(f: Float, g: Float) -> Float {
return Float(a + b + c + d + e) + f + g
}
}

0 comments on commit dd301b7

Please sign in to comment.