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

[release/9.0] Fix InlineArray swift lowering in mono #108483

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/mono/mono/metadata/marshal.c
Original file line number Diff line number Diff line change
Expand Up @@ -6798,11 +6798,15 @@ static void record_struct_field_physical_lowering (guint8* lowered_bytes, MonoTy

static void record_inlinearray_struct_physical_lowering (guint8* lowered_bytes, MonoClass* klass, guint32 offset)
{
int align;
int type_offset = MONO_ABI_SIZEOF (MonoObject);

// Get the first field and record its physical lowering N times
MonoClassField* field = mono_class_get_fields_internal (klass, NULL);
gpointer iter = NULL;
MonoClassField* field = mono_class_get_fields_internal (klass, &iter);
MonoType* fieldType = field->type;
for (int i = 0; i < m_class_inlinearray_value(klass); ++i) {
record_struct_field_physical_lowering(lowered_bytes, fieldType, offset + m_field_get_offset(field) + i * mono_type_size(fieldType, NULL));
record_struct_field_physical_lowering(lowered_bytes, fieldType, offset + m_field_get_offset(field) + i * mono_type_size(fieldType, &align) - type_offset);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/tests/Interop/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,5 @@ if(CLR_CMAKE_TARGET_APPLE)
add_subdirectory(Swift/SwiftInvalidCallConv)
add_subdirectory(Swift/SwiftRetAbiStress)
add_subdirectory(Swift/SwiftSelfContext)
add_subdirectory(Swift/SwiftInlineArray)
endif()
24 changes: 24 additions & 0 deletions src/tests/Interop/Swift/SwiftInlineArray/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
project(SwiftInlineArray)
include ("${CLR_INTEROP_TEST_ROOT}/Interop.cmake")

set(SOURCE SwiftInlineArray)

if (NOT SWIFT_COMPILER_TARGET AND CLR_CMAKE_TARGET_APPLE)
set(SWIFT_PLATFORM "macosx")
set(SWIFT_PLATFORM_SUFFIX "")
if (NOT CMAKE_OSX_DEPLOYMENT_TARGET)
set(CMAKE_OSX_DEPLOYMENT_TARGET "12.0")
endif()
set(SWIFT_DEPLOYMENT_TARGET ${CMAKE_OSX_DEPLOYMENT_TARGET})
set(SWIFT_COMPILER_TARGET "${CMAKE_OSX_ARCHITECTURES}-apple-${SWIFT_PLATFORM}${SWIFT_DEPLOYMENT_TARGET}${SWIFT_PLATFORM_SUFFIX}")
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
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${SOURCE}.swift
COMMENT "Generating ${SOURCE} library"
)

install(FILES ${CMAKE_CURRENT_BINARY_DIR}/lib${SOURCE}.dylib
DESTINATION bin
)
153 changes: 153 additions & 0 deletions src/tests/Interop/Swift/SwiftInlineArray/SwiftInlineArray.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Swift;
using Xunit;

public class SwiftInlineArray
{
private const string SwiftLib = "libSwiftInlineArray.dylib";

[InlineArray(32)]
struct F0
{
private byte _element0;
}

[UnmanagedCallConv(CallConvs = new Type[] { typeof(CallConvSwift) })]
[DllImport(SwiftLib, EntryPoint = "$s16SwiftInlineArray10swiftFunc02a0SiAA2F0V_tF")]
private static extern nint SwiftFunc0(F0 a0);

[Fact]
public static unsafe void TestFuncWithByteInlineArray()
{
F0 a0 = default;
byte* ptr = (byte*)&a0;

ptr[0] = (byte)122;
ptr[1] = (byte)223;
ptr[2] = (byte)66;
ptr[3] = (byte)1;
ptr[4] = (byte)135;
ptr[5] = (byte)209;
ptr[6] = (byte)54;
ptr[7] = (byte)221;
ptr[8] = (byte)24;
ptr[9] = (byte)104;
ptr[10] = (byte)21;
ptr[11] = (byte)222;
ptr[12] = (byte)156;
ptr[13] = (byte)241;
ptr[14] = (byte)97;
ptr[15] = (byte)141;
ptr[16] = (byte)239;
ptr[17] = (byte)184;
ptr[18] = (byte)69;
ptr[19] = (byte)247;
ptr[20] = (byte)134;
ptr[21] = (byte)121;
ptr[22] = (byte)204;
ptr[23] = (byte)45;
ptr[24] = (byte)112;
ptr[25] = (byte)166;
ptr[26] = (byte)220;
ptr[27] = (byte)221;
ptr[28] = (byte)86;
ptr[29] = (byte)197;
ptr[30] = (byte)178;
ptr[31] = (byte)29;


long result = SwiftFunc0(a0);
Assert.Equal(8091295595945034296, result);
Console.WriteLine("OK");
}


[InlineArray(8)]
struct F1
{
private int _element0;
}

[UnmanagedCallConv(CallConvs = new Type[] { typeof(CallConvSwift) })]
[DllImport(SwiftLib, EntryPoint = "$s16SwiftInlineArray10swiftFunc12a0SiAA2F1V_tF")]
private static extern nint SwiftFunc1(F1 a0);

[Fact]
public static unsafe void TestFuncWithIntInlineArray()
{
F1 a0 = default;
int* ptr = (int*)&a0;

ptr[0] = (int)-1172606642;
ptr[1] = (int)2004011304;
ptr[2] = (int)-1751053775;
ptr[3] = (int)-1361536584;
ptr[4] = (int)1578364919;
ptr[5] = (int)1205365715;
ptr[6] = (int)-883274792;
ptr[7] = (int)-550660826;


long result = SwiftFunc1(a0);
Assert.Equal(8444261314257660732, result);
Console.WriteLine("OK");
}

[InlineArray(6)]
struct F2
{
private ulong _element0;
}

[UnmanagedCallConv(CallConvs = new Type[] { typeof(CallConvSwift) })]
[DllImport(SwiftLib, EntryPoint = "$s16SwiftInlineArray10swiftFunc22a0SiAA2F2V_tF")]
private static extern nint SwiftFunc2(F2 a0);

[Fact]
public static unsafe void TestFuncWithLargeInlineArray()
{
F2 a0 = default;
ulong* ptr = (ulong*)&a0;

ptr[0] = (ulong)163054281557578879;
ptr[1] = (ulong)3715665182263428629;
ptr[2] = (ulong)15352099497683712058;
ptr[3] = (ulong)9456667702469177637;
ptr[4] = (ulong)5768234261922277852;
ptr[5] = (ulong)17154681812528174574;


long result = SwiftFunc2(a0);
Assert.Equal(-627554439188077294, result);
Console.WriteLine("OK");
}

[InlineArray(1)]
struct F3
{
private byte _element0;
}

[UnmanagedCallConv(CallConvs = new Type[] { typeof(CallConvSwift) })]
[DllImport(SwiftLib, EntryPoint = "$s16SwiftInlineArray10swiftFunc32a0SiAA2F3V_tF")]
private static extern nint SwiftFunc3(F3 a0);

[Fact]
public static unsafe void TestFuncWithSingleElementInlineArray()
{
F3 a0 = default;
byte* ptr = (byte*)&a0;

ptr[0] = (byte)177;


long result = SwiftFunc3(a0);
Assert.Equal(-5808468912223652740, result);
Console.WriteLine("OK");
}
}
16 changes: 16 additions & 0 deletions src/tests/Interop/Swift/SwiftInlineArray/SwiftInlineArray.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<!-- Needed for CLRTestTargetUnsupported, CMakeProjectReference -->
<RequiresProcessIsolation>true</RequiresProcessIsolation>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<!-- Swift interop is supported on Apple platforms only -->
<CLRTestTargetUnsupported Condition="'$(TargetsOSX)' != 'true' and '$(TargetsAppleMobile)' != 'true'">true</CLRTestTargetUnsupported>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildProjectName).cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="$(TestLibraryProjectPath)" />
<CMakeProjectReference Include="CMakeLists.txt" />
</ItemGroup>
</Project>
102 changes: 102 additions & 0 deletions src/tests/Interop/Swift/SwiftInlineArray/SwiftInlineArray.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

struct HasherFNV1a {

private var hash: UInt = 14_695_981_039_346_656_037
private let prime: UInt = 1_099_511_628_211

mutating func combine<T>(_ val: T) {
for byte in withUnsafeBytes(of: val, Array.init) {
hash ^= UInt(byte)
hash = hash &* prime
}
}

func finalize() -> Int {
Int(truncatingIfNeeded: hash)
}
}

@frozen public struct F0 {
public var elements: (UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8)
}

public func swiftFunc0(a0: F0) -> Int {
var hasher = HasherFNV1a()
hasher.combine(a0.elements.0);
hasher.combine(a0.elements.1);
hasher.combine(a0.elements.2);
hasher.combine(a0.elements.3);
hasher.combine(a0.elements.4);
hasher.combine(a0.elements.5);
hasher.combine(a0.elements.6);
hasher.combine(a0.elements.7);
hasher.combine(a0.elements.8);
hasher.combine(a0.elements.9);
hasher.combine(a0.elements.10);
hasher.combine(a0.elements.11);
hasher.combine(a0.elements.12);
hasher.combine(a0.elements.13);
hasher.combine(a0.elements.14);
hasher.combine(a0.elements.15);
hasher.combine(a0.elements.16);
hasher.combine(a0.elements.17);
hasher.combine(a0.elements.18);
hasher.combine(a0.elements.19);
hasher.combine(a0.elements.20);
hasher.combine(a0.elements.21);
hasher.combine(a0.elements.22);
hasher.combine(a0.elements.23);
hasher.combine(a0.elements.24);
hasher.combine(a0.elements.25);
hasher.combine(a0.elements.26);
hasher.combine(a0.elements.27);
hasher.combine(a0.elements.28);
hasher.combine(a0.elements.29);
hasher.combine(a0.elements.30);
hasher.combine(a0.elements.31);
return hasher.finalize()
}

@frozen public struct F1 {
public var elements: (Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32)
}

public func swiftFunc1(a0: F1) -> Int {
var hasher = HasherFNV1a()
hasher.combine(a0.elements.0);
hasher.combine(a0.elements.1);
hasher.combine(a0.elements.2);
hasher.combine(a0.elements.3);
hasher.combine(a0.elements.4);
hasher.combine(a0.elements.5);
hasher.combine(a0.elements.6);
hasher.combine(a0.elements.7);
return hasher.finalize()
}

@frozen public struct F2 {
public var elements: (UInt64, UInt64, UInt64, UInt64, UInt64, UInt64)
}

public func swiftFunc2(a0: F2) -> Int {
var hasher = HasherFNV1a()
hasher.combine(a0.elements.0);
hasher.combine(a0.elements.1);
hasher.combine(a0.elements.2);
hasher.combine(a0.elements.3);
hasher.combine(a0.elements.4);
hasher.combine(a0.elements.5);
return hasher.finalize()
}

@frozen public struct F3 {
public var element: UInt8
}

public func swiftFunc3(a0: F3) -> Int {
var hasher = HasherFNV1a()
hasher.combine(a0.element);
return hasher.finalize()
}
Loading