Skip to content

Commit

Permalink
[mono][tests] Enable ILStrip after AOT compilation for library tests (#…
Browse files Browse the repository at this point in the history
…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 <alex.koeplinger@outlook.com>
  • Loading branch information
kotlarmilos and akoeplinger authored Jan 30, 2024
1 parent d40c654 commit 10ea547
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 14 deletions.
1 change: 1 addition & 0 deletions eng/testing/tests.ioslike.targets
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@
<_ApplePropertyNames Include="UseConsoleUITemplate" />
<_ApplePropertyNames Include="UseRuntimeComponents" />
<_ApplePropertyNames Include="IncludesTestRunner" />
<_ApplePropertyNames Include="ShouldILStrip" />

<_ApplePropertiesToPass
Include="$(%(_ApplePropertyNames.Identity))"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,14 +258,17 @@ public void PinnedAndUnpinnedLocals()
Assert.Equal("DoSomething", reader.GetString(methodDef.Name));

MethodBodyBlock body = peReader.GetMethodBody(methodDef.RelativeVirtualAddress);
StandaloneSignature localSignature = reader.GetStandaloneSignature(body.LocalSignature);

ImmutableArray<string> localTypes = localSignature.DecodeLocalSignature(provider, genericContext: null);

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

// Compiler can generate temporaries or re-order so just check the ones we expect are there.
// (They could get optimized away too. If that happens in practice, change this test to use hard-coded signatures.)
Assert.Contains("uint8[] pinned", localTypes);
Assert.Contains("uint8[]", localTypes);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,15 @@ private static void TestConstructors1Worker(Type t)
Assert.False(c.IsConstructedGenericMethod());
Assert.False(c.IsGenericMethod);
Assert.Equal(MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName, c.Attributes);
Assert.Equal(MethodImplAttributes.IL, c.MethodImplementationFlags);
if (c.MethodImplementationFlags.HasFlag(MethodImplAttributes.NoInlining))
{
// when the assembly was processed with ILStrip, the NoInlining flag is set
Assert.Equal(MethodImplAttributes.IL | MethodImplAttributes.NoInlining, c.MethodImplementationFlags);
}
else
{
Assert.Equal(MethodImplAttributes.IL, c.MethodImplementationFlags);
}
Assert.Equal(CallingConventions.Standard | CallingConventions.HasThis, c.CallingConvention);

ParameterInfo[] ps = c.GetParameters();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,15 @@ private static void TestMethods1Worker(Type t)
Assert.False(m.IsConstructedGenericMethod());
Assert.False(m.IsGenericMethod);
Assert.Equal(MethodAttributes.Public | MethodAttributes.HideBySig, m.Attributes);
Assert.Equal(MethodImplAttributes.IL, m.MethodImplementationFlags);
if (m.MethodImplementationFlags.HasFlag(MethodImplAttributes.NoInlining))
{
// when the assembly was processed with ILStrip, the NoInlining flag is set
Assert.Equal(MethodImplAttributes.IL | MethodImplAttributes.NoInlining, m.MethodImplementationFlags);
}
else
{
Assert.Equal(MethodImplAttributes.IL, m.MethodImplementationFlags);
}
Assert.Equal(CallingConventions.Standard | CallingConventions.HasThis, m.CallingConvention);

Type theT = t.GetGenericArguments()[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
<TestRuntime>true</TestRuntime>
<!-- Some tests need types like System.Diagnostics.DebugProvider which are only exposed from System.Private.CoreLib -->
<CompileUsingReferenceAssemblies>false</CompileUsingReferenceAssemblies>
<!-- Active issue: https://github.com/dotnet/runtime/issues/87740 -->
<ShouldILStrip>false</ShouldILStrip>
</PropertyGroup>
<ItemGroup>
<DefaultReferenceExclusion Include="System.Collections" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Reflection;
using Microsoft.DotNet.XUnitExtensions;
using Xunit;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Reflection;
using Microsoft.DotNet.XUnitExtensions;
using Xunit;

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

var il = mb.GetILAsByteArray();
if (il?.Length == 1 && il[0] == 0x2a) // ILStrip replaces method bodies with the 'ret' IL opcode i.e. 0x2a
throw new SkipTestException("The method body was processed using ILStrip.");

Assert.True(mb.InitLocals); // local variables are initialized
#if DEBUG
Assert.Equal(2, mb.MaxStackSize);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,17 @@ public static class MethodImplAttributeTests
public static void AggressiveOptimizationTest()
{
MethodImplAttributes implAttributes = MethodBase.GetCurrentMethod().MethodImplementationFlags;
Assert.Equal(MethodImplAttributes.AggressiveOptimization, implAttributes);
Assert.Equal(MethodImplOptions.AggressiveOptimization, (MethodImplOptions)implAttributes);
if (implAttributes.HasFlag(MethodImplAttributes.NoInlining))
{
// when the assembly was processed with ILStrip, the NoInlining flag is set
Assert.Equal(MethodImplAttributes.AggressiveOptimization | MethodImplAttributes.NoInlining, implAttributes);
Assert.Equal(MethodImplOptions.AggressiveOptimization | MethodImplOptions.NoInlining, (MethodImplOptions)implAttributes);
}
else
{
Assert.Equal(MethodImplAttributes.AggressiveOptimization, implAttributes);
Assert.Equal(MethodImplOptions.AggressiveOptimization, (MethodImplOptions)implAttributes);
}
}
}
}
3 changes: 1 addition & 2 deletions src/mono/msbuild/apple/build/AppleBuild.props
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@
<UseMonoJustInterp Condition="'$(RunAOTCompilation)' == 'true' and '$(MonoForceInterpreter)' == 'true'">true</UseMonoJustInterp>

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

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

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

0 comments on commit 10ea547

Please sign in to comment.