Skip to content

Commit 78b45ad

Browse files
Don't always do optimistic HW intrinsic expansion (#89282)
"Optimistic" expansion means we generate a method body for `IsSupported` that returns true/false depending on the currently running CPU. Don't do this when `--instruction-set:native` was specified because native should mean "exactly this". Also don't do this when optimizing for size (saves 0.6% on hello world). I was going back and forth whether to do this when user specifies instruction sets manually, but decided it would be a "breaking" change.
1 parent 08aba69 commit 78b45ad

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

src/coreclr/tools/Common/InstructionSetHelpers.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace System.CommandLine
1616
internal static partial class Helpers
1717
{
1818
public static InstructionSetSupport ConfigureInstructionSetSupport(string instructionSet, int maxVectorTBitWidth, bool isVectorTOptimistic, TargetArchitecture targetArchitecture, TargetOS targetOS,
19-
string mustNotBeMessage, string invalidImplicationMessage, Logger logger)
19+
string mustNotBeMessage, string invalidImplicationMessage, Logger logger, bool optimizingForSize = false)
2020
{
2121
InstructionSetSupportBuilder instructionSetSupportBuilder = new(targetArchitecture);
2222

@@ -38,8 +38,16 @@ public static InstructionSetSupport ConfigureInstructionSetSupport(string instru
3838
}
3939
}
4040

41+
// Whether to allow optimistically expanding the instruction sets beyond what was specified.
42+
// We seed this from optimizingForSize - if we're size-optimizing, we don't want to unnecessarily
43+
// compile both branches of IsSupported checks.
44+
bool allowOptimistic = !optimizingForSize;
45+
4146
if (instructionSet == "native")
4247
{
48+
// We're compiling for a specific chip
49+
allowOptimistic = false;
50+
4351
if (GetTargetArchitecture(null) != targetArchitecture)
4452
{
4553
throw new CommandLineException("Instruction set 'native' not supported when cross-compiling to a different architecture.");
@@ -123,7 +131,7 @@ public static InstructionSetSupport ConfigureInstructionSetSupport(string instru
123131
InstructionSetSupportBuilder optimisticInstructionSetSupportBuilder = new InstructionSetSupportBuilder(instructionSetSupportBuilder);
124132

125133
// Optimistically assume some instruction sets are present.
126-
if (targetArchitecture == TargetArchitecture.X86 || targetArchitecture == TargetArchitecture.X64)
134+
if (allowOptimistic && (targetArchitecture == TargetArchitecture.X86 || targetArchitecture == TargetArchitecture.X64))
127135
{
128136
// We set these hardware features as opportunistically enabled as most of hardware in the wild supports them.
129137
// Note that we do not indicate support for AVX, or any other instruction set which uses the VEX encodings as

src/coreclr/tools/aot/ILCompiler/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ public int Run()
8888
TargetArchitecture targetArchitecture = Get(_command.TargetArchitecture);
8989
TargetOS targetOS = Get(_command.TargetOS);
9090
InstructionSetSupport instructionSetSupport = Helpers.ConfigureInstructionSetSupport(Get(_command.InstructionSet), Get(_command.MaxVectorTBitWidth), isVectorTOptimistic, targetArchitecture, targetOS,
91-
"Unrecognized instruction set {0}", "Unsupported combination of instruction sets: {0}/{1}", logger);
91+
"Unrecognized instruction set {0}", "Unsupported combination of instruction sets: {0}/{1}", logger,
92+
optimizingForSize: _command.OptimizationMode == OptimizationMode.PreferSize);
9293

9394
string systemModuleName = Get(_command.SystemModuleName);
9495
string reflectionData = Get(_command.ReflectionData);

0 commit comments

Comments
 (0)