Skip to content

Commit 6444e3e

Browse files
committed
[r2r] Don't compile methods if the target doesn't support any of their CompExactlyDependsOn instruction sets
Unless they are decorated with the CompHasFallback attribute.
1 parent 34eb5a3 commit 6444e3e

4 files changed

Lines changed: 39 additions & 2 deletions

File tree

src/coreclr/tools/aot/ILCompiler.ReadyToRun/JitInterface/CorInfoImpl.ReadyToRun.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,7 @@ public static bool ShouldCodeNotBeCompiledIntoFinalImage(InstructionSetSupport i
573573
var handle = ecmaMethod.Handle;
574574

575575
List<TypeDesc> compExactlyDependsOnList = null;
576+
bool hasCompHasFallback = false;
576577

577578
foreach (var attributeHandle in metadataReader.GetMethodDefinition(handle).GetCustomAttributes())
578579
{
@@ -606,25 +607,41 @@ public static bool ShouldCodeNotBeCompiledIntoFinalImage(InstructionSetSupport i
606607
compExactlyDependsOnList.Add(typeForBypass);
607608
}
608609
}
610+
else if (metadataReader.StringComparer.Equals(nameHandle, "CompHasFallbackAttribute"))
611+
{
612+
hasCompHasFallback = true;
613+
}
609614
}
610615
}
611616

612617
if (compExactlyDependsOnList != null && compExactlyDependsOnList.Count > 0)
613618
{
619+
bool anySupported = false;
620+
614621
foreach (var intrinsicType in compExactlyDependsOnList)
615622
{
616623
InstructionSet instructionSet = InstructionSetParser.LookupPlatformIntrinsicInstructionSet(intrinsicType.Context.Target.Architecture, intrinsicType);
617624
// If the instruction set is ILLEGAL, it means it is never supported by the current architecture so the behavior at runtime is known
618625
if (instructionSet != InstructionSet.ILLEGAL)
619626
{
620-
if (!instructionSetSupport.IsInstructionSetSupported(instructionSet) &&
621-
!instructionSetSupport.IsInstructionSetExplicitlyUnsupported(instructionSet))
627+
if (instructionSetSupport.IsInstructionSetSupported(instructionSet))
628+
{
629+
anySupported = true;
630+
}
631+
else if (!instructionSetSupport.IsInstructionSetExplicitlyUnsupported(instructionSet))
622632
{
623633
// If we reach here this is an instruction set generally supported on this platform, but we don't know what the behavior will be at runtime
624634
return true;
625635
}
626636
}
627637
}
638+
639+
if (!anySupported && !hasCompHasFallback)
640+
{
641+
// If none of the instruction sets are supported (all are either illegal or explicitly unsupported),
642+
// skip compilation unless the method has a functional fallback path
643+
return true;
644+
}
628645
}
629646

630647
// No reason to bypass compilation and code generation.

src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,7 @@
837837
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\CompilerServices\CallingConventions.cs" />
838838
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\CompilerServices\CollectionBuilderAttribute.cs" />
839839
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\CompilerServices\CompExactlyDependsOnAttribute.cs" />
840+
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\CompilerServices\CompHasFallbackAttribute.cs" />
840841
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\CompilerServices\CompilationRelaxations.cs" />
841842
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\CompilerServices\CompilationRelaxationsAttribute.cs" />
842843
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\CompilerServices\CompilerFeatureRequiredAttribute.cs" />
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Diagnostics;
5+
6+
namespace System.Runtime.CompilerServices
7+
{
8+
// Use this attribute alongside CompExactlyDependsOnAttribute to indicate that a method has
9+
// a functional fallback path and should still be compiled into the Ready2Run image even when
10+
// none of the CompExactlyDependsOn instruction sets are supported.
11+
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor, AllowMultiple = false, Inherited = false)]
12+
#if MONO
13+
[Conditional("unnecessary")] // Mono doesn't use Ready2Run so we can remove this attribute to reduce size
14+
#endif
15+
internal sealed class CompHasFallbackAttribute : Attribute
16+
{
17+
}
18+
}

src/libraries/System.Private.CoreLib/src/System/SearchValues/SearchValues.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ internal interface IRuntimeConst
301301
/// </summary>
302302
[MethodImpl(MethodImplOptions.AggressiveInlining)]
303303
[CompExactlyDependsOn(typeof(Ssse3))]
304+
[CompHasFallback]
304305
internal static Vector128<byte> ShuffleNativeModified(Vector128<byte> vector, Vector128<byte> indices)
305306
{
306307
if (Ssse3.IsSupported)

0 commit comments

Comments
 (0)