Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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/SwiftStructLowering)
endif()
24 changes: 24 additions & 0 deletions src/tests/Interop/Swift/SwiftStructLowering/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
project(SwiftStructLowering)
include ("${CLR_INTEROP_TEST_ROOT}/Interop.cmake")

set(SOURCE SwiftStructLowering)

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/SwiftStructLowering/SwiftStructLowering.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 SwiftStructLowering
{
private const string SwiftLib = "libSwiftStructLowering.dylib";

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

[UnmanagedCallConv(CallConvs = new Type[] { typeof(CallConvSwift) })]
[DllImport(SwiftLib, EntryPoint = "$s19SwiftStructLowering10swiftFunc02a0SiAA2F0V_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 = "$s19SwiftStructLowering10swiftFunc12a0SiAA2F1V_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 = "$s19SwiftStructLowering10swiftFunc22a0SiAA2F2V_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 = "$s19SwiftStructLowering10swiftFunc32a0SiAA2F3V_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");
}
}
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/SwiftStructLowering/SwiftStructLowering.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()
}