Skip to content

Commit 0a9c208

Browse files
More tests
1 parent 09ef1ca commit 0a9c208

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/TypePreinit.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,7 @@ private Status TryScanMethod(MethodIL methodIL, Value[] parameters, Stack<Method
779779
}
780780
else if (popped.ValueKind == StackValueKind.ByRef
781781
&& (opcode == ILOpcode.conv_i || opcode == ILOpcode.conv_u)
782-
&& reader.PeekILOpcode() is >= ILOpcode.ldind_i1 and <= ILOpcode.ldind_i)
782+
&& (reader.PeekILOpcode() is >= ILOpcode.ldind_i1 and <= ILOpcode.ldind_ref) || reader.PeekILOpcode() == ILOpcode.ldobj)
783783
{
784784
// In the interpreter memory model, there's no conversion from a byref to an integer.
785785
// Roslyn however sometimes emits a sequence of conv_u followed by ldind and we can

src/tests/nativeaot/SmokeTests/Preinitialization/Preinitialization.cs

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ private static int Main()
5353
TestStaticInterfaceMethod.Run();
5454
TestConstrainedCall.Run();
5555
TestTypeHandles.Run();
56+
TestIndirectLoads.Run();
5657
#else
5758
Console.WriteLine("Preinitialization is disabled in multimodule builds for now. Skipping test.");
5859
#endif
@@ -1059,13 +1060,19 @@ static DefaultInstanceAccess()
10591060

10601061
class MoreOperations
10611062
{
1062-
public readonly static int Length;
1063+
public readonly static int IntsLength;
1064+
public readonly static int StringLength;
1065+
public readonly static char FirstChar;
10631066

10641067
private static ReadOnlySpan<int> Ints => new int[] { 5, 6, 7, 8 };
10651068

1069+
private static ReadOnlySpan<char> GetString() => "Hello World!";
1070+
10661071
static MoreOperations()
10671072
{
1068-
Length = Ints.Length;
1073+
IntsLength = Ints.Length;
1074+
StringLength = GetString().Length;
1075+
FirstChar = GetString()[0];
10691076
}
10701077
}
10711078

@@ -1083,7 +1090,9 @@ public static void Run()
10831090
DefaultInstanceAccess.Sum.ToString(); // make sure cctor is looked at
10841091

10851092
Assert.IsPreinitialized(typeof(MoreOperations));
1086-
Assert.AreEqual(4, MoreOperations.Length);
1093+
Assert.AreEqual(4, MoreOperations.IntsLength);
1094+
Assert.AreEqual(12, MoreOperations.StringLength);
1095+
Assert.AreEqual('H', MoreOperations.FirstChar);
10871096
}
10881097
}
10891098

@@ -1192,6 +1201,28 @@ public static void Run()
11921201
}
11931202
}
11941203

1204+
class TestIndirectLoads
1205+
{
1206+
static unsafe U Read<T, U>(T val) where T : unmanaged where U : unmanaged
1207+
=> *(U*)&val;
1208+
1209+
class LdindTester
1210+
{
1211+
public static sbyte SByte = Read<byte, sbyte>(byte.MaxValue);
1212+
public static short Short = Read<ushort, short>(ushort.MaxValue);
1213+
public static int Int = Read<uint, int>(uint.MaxValue);
1214+
public static long Long = Read<ulong, long>(ulong.MaxValue);
1215+
}
1216+
1217+
public static void Run()
1218+
{
1219+
Assert.IsPreinitialized(typeof(LdindTester));
1220+
Assert.AreEqual(-1, LdindTester.SByte);
1221+
Assert.AreEqual(-1, LdindTester.Short);
1222+
Assert.AreEqual(-1, LdindTester.Int);
1223+
Assert.AreEqual(-1, LdindTester.Long);
1224+
}
1225+
}
11951226

11961227
static class Assert
11971228
{

0 commit comments

Comments
 (0)