Repository navigation
JIT: Raise the cutoff for lowering switches to direct branches - #138460
NinoFloris wants to merge 3 commits into
Conversation
|
Azure Pipelines: Successfully started running 5 pipeline(s). 10 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
|
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch |
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
|
The benchmark actually tests 3 patterns? Uniform slightly regresses (not sure for amd), so we could go fully neutral (or better) by making the cutoff 3, but in that case we should determine arm64's cutoff independently. TP is more worrying, but we could do this in the emitter too, which should avoid the added BB-related work. |
|
@EgorBot -intel -azure_linux_cobalt200 --envvars DOTNET_JitDisasm:Dispatch* DOTNET_JitDisasmOnlyOptimized:1 using System.Runtime.CompilerServices;
using BenchmarkDotNet.Attributes;
public class SmallSwitch
{
private const int Count = 131072;
private byte[] keys3, keys4, keys7;
[Params("Uniform", "Cyclic", "HotFirst", "HotLast")]
public string Pattern { get; set; } = "Uniform";
[GlobalSetup]
public void Setup()
{
keys3 = MakeKeys(3);
keys4 = MakeKeys(4);
keys7 = MakeKeys(7);
}
private byte[] MakeKeys(int cases)
{
var keys = new byte[Count];
uint state = 174526;
for (int i = 0; i < keys.Length; i++)
{
state = unchecked(state * 1664525 + 1013904223);
int key = (int)((state >> 16) % (uint)(cases + 1));
if (Pattern == "Cyclic")
key = i % (cases + 1);
else if ((state >> 24) < 230)
{
if (Pattern == "HotFirst") key = 0;
if (Pattern == "HotLast") key = cases - 1;
}
keys[i] = (byte)key;
}
return keys;
}
[Benchmark(OperationsPerInvoke = Count)]
public int Run3()
{
int x = 1;
foreach (byte key in keys3)
x = Dispatch3(key, x);
return x;
}
[MethodImpl(MethodImplOptions.NoInlining)]
private static int Dispatch3(int key, int x) => key switch
{
0 => x * 3 + 5,
1 => (x << 5) ^ 7,
2 => x * 5 - 11,
_ => ~x
};
[Benchmark(OperationsPerInvoke = Count)]
public int Run4()
{
int x = 1;
foreach (byte key in keys4)
x = Dispatch4(key, x);
return x;
}
[MethodImpl(MethodImplOptions.NoInlining)]
private static int Dispatch4(int key, int x) => key switch
{
0 => x * 3 + 5,
1 => (x << 5) ^ 7,
2 => x * 5 - 11,
3 => (x >> 3) + 17,
_ => ~x
};
[Benchmark(OperationsPerInvoke = Count)]
public int Run7()
{
int x = 1;
foreach (byte key in keys7)
x = Dispatch7(key, x);
return x;
}
[MethodImpl(MethodImplOptions.NoInlining)]
private static int Dispatch7(int key, int x) => key switch
{
0 => x * 3 + 5,
1 => (x << 5) ^ 7,
2 => x * 5 - 11,
3 => (x >> 3) + 17,
4 => (x * 11) ^ 85,
5 => (x >> 6) - 85,
6 => (x << 7) + 78,
_ => ~x
};
} |
|
Summarized: On Cobalt 200, direct branches reduced dispatch time for uniform keys at all three widths. At seven cases, uniform went from 6.263 to 5.785 ns (−7.6%) and cyclic went from 1.822 to 1.597 ns (−12.3%). On Intel, the three-case switch improved by 9.4% on uniform keys and 18.2% on cyclic keys. Change in mean time per dispatch versus main. Negative is faster. Case counts exclude the default arm.
† Intel 4 and 7 are controls. Both builds emitted jump tables there. Intel 4 Uniform took 13.4% longer on the PR; the Tier 1 code had a different block layout. Intel 4 HotLast took 14.2% longer even with identical printed dispatch instructions. Each toolchain had one process launch per benchmark, so those timings alone do not establish a cutoff effect. With three cases, Cyclic repeats Mean time per dispatch (ns), main → PR
|

I did a bit of comparing of swift vs C# codegen for https://ben-chmarks.com/gemini-neon/dlist/
The jump table (and spills that get fixed by #137509) stood out on arm64.
From https://ben-chmarks.com/benchmarks/dlist/lang/csharp/Driver.cs
We now stop emitting a jump table for this switch on ARM64. We chose the 7 + default cutoff empirically on an M1 Max. For dense switches like this one, LLVM’s corresponding cutoffs are 3 + default on x64 and normally 9 + default on ARM64.
See results #138460 (comment)