Skip to content

Commit 10ea547

Browse files
[mono][tests] Enable ILStrip after AOT compilation for library tests (#88167)
* Enable ILStrip during AOT compilation of library tests * Update System.Reflection tests to support noinlining attribute * [infra] Disable ILStrip for System.Diagnostics.Debug.Tests * [infra] Don't run ILStrip for library mode * Add ShouldILStrip to _ApplePropertyNames --------- Co-authored-by: Alexander Köplinger <[email protected]>
1 parent d40c654 commit 10ea547

File tree

9 files changed

+53
-14
lines changed

9 files changed

+53
-14
lines changed

eng/testing/tests.ioslike.targets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@
125125
<_ApplePropertyNames Include="UseConsoleUITemplate" />
126126
<_ApplePropertyNames Include="UseRuntimeComponents" />
127127
<_ApplePropertyNames Include="IncludesTestRunner" />
128+
<_ApplePropertyNames Include="ShouldILStrip" />
128129

129130
<_ApplePropertiesToPass
130131
Include="$(%(_ApplePropertyNames.Identity))"

src/libraries/System.Reflection.Metadata/tests/Metadata/Decoding/SignatureDecoderTests.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -258,14 +258,17 @@ public void PinnedAndUnpinnedLocals()
258258
Assert.Equal("DoSomething", reader.GetString(methodDef.Name));
259259

260260
MethodBodyBlock body = peReader.GetMethodBody(methodDef.RelativeVirtualAddress);
261-
StandaloneSignature localSignature = reader.GetStandaloneSignature(body.LocalSignature);
262-
263-
ImmutableArray<string> localTypes = localSignature.DecodeLocalSignature(provider, genericContext: null);
264-
265-
// Compiler can generate temporaries or re-order so just check the ones we expect are there.
266-
// (They could get optimized away too. If that happens in practice, change this test to use hard-coded signatures.)
267-
Assert.Contains("uint8[] pinned", localTypes);
268-
Assert.Contains("uint8[]", localTypes);
261+
var il = body.GetILBytes();
262+
// ILStrip replaces method body with the 'ret' IL opcode i.e. 0x2a
263+
if (!(il?.Length == 1 && il[0] == 0x2a)) {
264+
StandaloneSignature localSignature = reader.GetStandaloneSignature(body.LocalSignature);
265+
ImmutableArray<string> localTypes = localSignature.DecodeLocalSignature(provider, genericContext: null);
266+
267+
// Compiler can generate temporaries or re-order so just check the ones we expect are there.
268+
// (They could get optimized away too. If that happens in practice, change this test to use hard-coded signatures.)
269+
Assert.Contains("uint8[] pinned", localTypes);
270+
Assert.Contains("uint8[]", localTypes);
271+
}
269272
}
270273
}
271274

src/libraries/System.Reflection.MetadataLoadContext/tests/src/Tests/Constructor/ConstructorTests.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,15 @@ private static void TestConstructors1Worker(Type t)
3131
Assert.False(c.IsConstructedGenericMethod());
3232
Assert.False(c.IsGenericMethod);
3333
Assert.Equal(MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName, c.Attributes);
34-
Assert.Equal(MethodImplAttributes.IL, c.MethodImplementationFlags);
34+
if (c.MethodImplementationFlags.HasFlag(MethodImplAttributes.NoInlining))
35+
{
36+
// when the assembly was processed with ILStrip, the NoInlining flag is set
37+
Assert.Equal(MethodImplAttributes.IL | MethodImplAttributes.NoInlining, c.MethodImplementationFlags);
38+
}
39+
else
40+
{
41+
Assert.Equal(MethodImplAttributes.IL, c.MethodImplementationFlags);
42+
}
3543
Assert.Equal(CallingConventions.Standard | CallingConventions.HasThis, c.CallingConvention);
3644

3745
ParameterInfo[] ps = c.GetParameters();

src/libraries/System.Reflection.MetadataLoadContext/tests/src/Tests/Method/MethodTests.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,15 @@ private static void TestMethods1Worker(Type t)
3030
Assert.False(m.IsConstructedGenericMethod());
3131
Assert.False(m.IsGenericMethod);
3232
Assert.Equal(MethodAttributes.Public | MethodAttributes.HideBySig, m.Attributes);
33-
Assert.Equal(MethodImplAttributes.IL, m.MethodImplementationFlags);
33+
if (m.MethodImplementationFlags.HasFlag(MethodImplAttributes.NoInlining))
34+
{
35+
// when the assembly was processed with ILStrip, the NoInlining flag is set
36+
Assert.Equal(MethodImplAttributes.IL | MethodImplAttributes.NoInlining, m.MethodImplementationFlags);
37+
}
38+
else
39+
{
40+
Assert.Equal(MethodImplAttributes.IL, m.MethodImplementationFlags);
41+
}
3442
Assert.Equal(CallingConventions.Standard | CallingConventions.HasThis, m.CallingConvention);
3543

3644
Type theT = t.GetGenericArguments()[0];

src/libraries/System.Runtime/tests/System.Diagnostics.Debug.Tests/System.Diagnostics.Debug.Tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
<TestRuntime>true</TestRuntime>
66
<!-- Some tests need types like System.Diagnostics.DebugProvider which are only exposed from System.Private.CoreLib -->
77
<CompileUsingReferenceAssemblies>false</CompileUsingReferenceAssemblies>
8+
<!-- Active issue: https://github.com/dotnet/runtime/issues/87740 -->
9+
<ShouldILStrip>false</ShouldILStrip>
810
</PropertyGroup>
911
<ItemGroup>
1012
<DefaultReferenceExclusion Include="System.Collections" />

src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Reflection/MethodBaseTests.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Reflection;
6+
using Microsoft.DotNet.XUnitExtensions;
67
using Xunit;
78

89
#pragma warning disable 0219 // field is never used
@@ -55,6 +56,9 @@ public static void TestMethodBody()
5556
{
5657
MethodBase mbase = typeof(MethodBaseTests).GetMethod("MyOtherMethod", BindingFlags.Static | BindingFlags.Public);
5758
MethodBody mb = mbase.GetMethodBody();
59+
var il = mb.GetILAsByteArray();
60+
if (il?.Length == 1 && il[0] == 0x2a) // ILStrip replaces method bodies with the 'ret' IL opcode i.e. 0x2a
61+
throw new SkipTestException("The method body was processed using ILStrip.");
5862
var codeSize = mb.GetILAsByteArray().Length;
5963
Assert.True(mb.InitLocals); // local variables are initialized
6064

src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Reflection/MethodBodyTests.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Reflection;
6+
using Microsoft.DotNet.XUnitExtensions;
67
using Xunit;
78

89
#pragma warning disable 0219 // field is never used
@@ -17,6 +18,10 @@ public static void Test_MethodBody_ExceptionHandlingClause()
1718
MethodInfo mi = typeof(MethodBodyTests).GetMethod("MethodBodyExample", BindingFlags.NonPublic | BindingFlags.Static);
1819
MethodBody mb = mi.GetMethodBody();
1920

21+
var il = mb.GetILAsByteArray();
22+
if (il?.Length == 1 && il[0] == 0x2a) // ILStrip replaces method bodies with the 'ret' IL opcode i.e. 0x2a
23+
throw new SkipTestException("The method body was processed using ILStrip.");
24+
2025
Assert.True(mb.InitLocals); // local variables are initialized
2126
#if DEBUG
2227
Assert.Equal(2, mb.MaxStackSize);

src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Runtime/CompilerServices/MethodImplAttributeTests.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,17 @@ public static class MethodImplAttributeTests
1313
public static void AggressiveOptimizationTest()
1414
{
1515
MethodImplAttributes implAttributes = MethodBase.GetCurrentMethod().MethodImplementationFlags;
16-
Assert.Equal(MethodImplAttributes.AggressiveOptimization, implAttributes);
17-
Assert.Equal(MethodImplOptions.AggressiveOptimization, (MethodImplOptions)implAttributes);
16+
if (implAttributes.HasFlag(MethodImplAttributes.NoInlining))
17+
{
18+
// when the assembly was processed with ILStrip, the NoInlining flag is set
19+
Assert.Equal(MethodImplAttributes.AggressiveOptimization | MethodImplAttributes.NoInlining, implAttributes);
20+
Assert.Equal(MethodImplOptions.AggressiveOptimization | MethodImplOptions.NoInlining, (MethodImplOptions)implAttributes);
21+
}
22+
else
23+
{
24+
Assert.Equal(MethodImplAttributes.AggressiveOptimization, implAttributes);
25+
Assert.Equal(MethodImplOptions.AggressiveOptimization, (MethodImplOptions)implAttributes);
26+
}
1827
}
1928
}
2029
}

src/mono/msbuild/apple/build/AppleBuild.props

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@
1616
<UseMonoJustInterp Condition="'$(RunAOTCompilation)' == 'true' and '$(MonoForceInterpreter)' == 'true'">true</UseMonoJustInterp>
1717

1818
<StripDebugSymbols Condition="'$(StripDebugSymbols)' == ''" >false</StripDebugSymbols>
19-
<!-- Tracking issue: https://github.com/dotnet/runtime/issues/87740 -->
20-
<!-- <ShouldILStrip Condition="'$(RunAOTCompilation)' == 'true' and '$(MonoForceInterpreter)' != 'true'">true</ShouldILStrip> -->
2119

2220
<!-- TODO: We currently do not support bundling a static iOS-library with NativeAOT -->
2321
<_IsLibraryMode Condition="('$(UseNativeAOTRuntime)' != 'true' and '$(NativeLib)' != '') or ('$(UseNativeAOTRuntime)' == 'true' and '$(NativeLib)' == 'Shared')">true</_IsLibraryMode>
22+
<ShouldILStrip Condition="'$(ShouldILStrip)' == '' and '$(RunAOTCompilation)' == 'true' and '$(MonoForceInterpreter)' != 'true' and '$(_IsLibraryMode)' != 'true'">true</ShouldILStrip>
2423

2524
<_AotCompileTargetName Condition="'$(UseNativeAOTRuntime)' == 'true'">_AppleNativeAotCompile</_AotCompileTargetName>
2625
<_AotCompileTargetName Condition="'$(UseNativeAOTRuntime)' != 'true'">_AppleAotCompile</_AotCompileTargetName>

0 commit comments

Comments
 (0)