Repository navigation
Description
Description
I'm porting an algorithm from scalar double arithmetics to SIMD using the Hardware Intrinsics. After some testing I concluded that the performance of the SIMD version is worst. Now it can be that I'm just not good at using SIMD instructions. However looking at the asm produced by the JIT, I think there may be a problem.
Configuration
.NET Core 5.0.0 (CoreCLR 5.0.20.21406, CoreFX 5.0.20.21406), X64 RyuJIT
Regression?
Data
Look at one example:
var v02 = Avx.HorizontalAdd(v00, v01); // r = (xh + yh) | - xl - yl)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
00007ffc`ca6fdca4 c5f9284dd0 vmovapd xmm1,xmmword ptr [rbp-30h]
00007ffc`ca6fdca9 c5f17c4dc0 vhaddpd xmm1,xmm1,xmmword ptr [rbp-40h]
00007ffc`ca6fdcae c5f9294db0 vmovapd xmmword ptr [rbp-50h],xmm1
var v03 = Avx.Subtract(v00, v02);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
00007ffc`ca6fdcb3 c5f9284dd0 vmovapd xmm1,xmmword ptr [rbp-30h]
00007ffc`ca6fdcb8 c5f15c4db0 vsubpd xmm1,xmm1,xmmword ptr [rbp-50h]
00007ffc`ca6fdcbd c5f9294da0 vmovapd xmmword ptr [rbp-60h],xmm1
This comes from the disassembly of BenchmarkDotet.
Also benchmark results:
| Method | EnvironmentVariables | Mean | Error | StdDev | Median | Max |
|---|---|---|---|---|---|---|
| AdditionDouble | COMPlus_EnableHWIntrinsic=0 | 1.112 ns | 0.0658 ns | 0.1187 ns | 1.100 ns | 1.400 ns |
| AdditionDouble2 | COMPlus_EnableHWIntrinsic=0 | 104.808 ns | 2.8832 ns | 6.0183 ns | 102.300 ns | 124.500 ns |
| AdditionDouble | COMPlus_EnableHWIntrinsic=1 | 1.065 ns | 0.0645 ns | 0.0985 ns | 1.100 ns | 1.200 ns |
| AdditionDouble2 | COMPlus_EnableHWIntrinsic=1 | 196.530 ns | 9.3874 ns | 26.4772 ns | 178.950 ns | 268.100 ns |
Analysis
If you look closely you see that each instruction seem to be taken in isolation, with its own register allocation, instead of being global to the method. This means a LOT more memory load/store than seem necessary. There is a lot of register to play with beside xmm1...
The documentation on Hardware Intrinsics states that for some time in the compilation tree intrinsics are seen as method. Maybe they are seen as method for a bit too long and so each "method" see some register allocation but only in its own local "method" context.
category:cq
theme:register-allocator
skill-level:expert
cost:medium
Activity
AndyAyersMS commentedon Jun 1, 2020
@ebfortin the disassembly above looks like it is from un-optimized code. Usually BenchmarkDotNet is pretty good at ensuring that it benchmarks only optimized code, but in this case I wonder.
Can you share your test case, or some representative sample?
AndyAyersMS commentedon Jun 1, 2020
Going to mark this as future, may reconsider once we have more information.
ebfortin commentedon Jun 1, 2020
I will provide some more details later tonight. I'll put the method code being evaluated. The benchmark configuration used and other stuff.
ebfortin commentedon Jun 1, 2020
Code from BenchmarkDotnet to define the benchmarks.
Configuration for the benchmark.
Code of operator+() called from the Benchmark.
From BenchmarkDotnet run showing that no debugger is attached.
EgorBo commentedon Jun 1, 2020
Add22looks pretty good to me:The only thing it seems is this repro:
Codegen:
it loads
0twice to perform negations (I am not even mentioning it could do negation only once on top of result)cc @tannergooding
EgorBo commentedon Jun 1, 2020
I wonder if we can expand GT_NEG to
0 - xso VN/CSE can pick0up (and then optimize to GT_NEG back in lowering if necessary)tannergooding commentedon Jun 1, 2020
The loading
0twice is basically the same as: #37079.Horizontal Adds aren't necessarily the most performance instructions and it might be better to rearrange the data so you can use normal addition/subtraction instead. It looks like it might be possible at first glance, but I didn't confirm...
Constructing and deconstructing the vector also has some minimal overhead that wouldn't be great in a loop.
ebfortin commentedon Jun 1, 2020
And where is this diassembly coming from? Why would un-optimized code be used by BenchmarkDotnet? Because the code that is produced on my side has basically one load before, and one store after any SIMD instruction. And the lower performance comes from there, not in the choice of SIMD instruction. Also what are the condition in the JIT to emit optimized code? Because I see a problem if on some computer it's optimized, on other it's not. It's too variable.
ebfortin commentedon Jun 2, 2020
I tested it with COMPlus_TC_QuickJit set to 0. A HUGE difference. So indeed the code was not optimized. Which is demonstrated with the output from BenchmarkDotnet diassembler below:
And if I look at the benchmark, I see that my SIMD code is bad and need improvement, like what @tannergooding was suggesting. Now that the un-optimized variable is out of the way.
However it raises the question : Why wouldn't the Tiered compilation kicks in for this workload? Method being evaluated is too small? Run time is too low?
And a side question : What is AgressiveOptimization doing? Does it affect only C# compilation? Any effect on the JIT? Would be great to have the ability to force optimization, in the JIT, on a per method basis. Would make it possible to ship librairies with critical methods that are sure to get optimized right away. And letting the JIT decide for the others.
AndyAyersMS commentedon Jun 2, 2020
@adamsitnik any idea why BenchmarkDotNet can't get to Tier1 code in the test case above?
AggressiveOptimizationwill indeed bypass Tier0 and generate Tier1 code the first time a method is jitted. But it shouldn't be needed in most cases.adamsitnik commentedon Jun 2, 2020
Most probably it's a side effect of using
[IterationSetup]which makes BDN invoke the benchmark only once per iteration, so it's not executed more than 30 times during the warmup stage. Using[IterationSetup]is not recommended: https://github.com/dotnet/performance/blob/master/docs/microbenchmark-design-guidelines.md#IterationSetup@ebfortin could you please try to switch using
[GlobalSetup]instead? https://github.com/dotnet/performance/blob/master/docs/microbenchmark-design-guidelines.md#globalsetup If you really need a new pair of double for every invocation then it would be best to generate a big array of doubles in the[IterationSetup]and just iterate over it in the benchmark.ebfortin commentedon Jun 2, 2020
I just tried with [GlobalSetup] instead. I get the same un-optimized run.
If I use AgressiveOptimization on Add22 then it optimize that method but not the rest, so performance is 1 order of magnitude worst overall than full optimization.
So no problems with the register allocators. The optimizer is working, clearly. All good. But in what circumstanced it gets invoked is not quite clear. I was expecting it to be used on this benchmark.
ebfortin commentedon Jun 7, 2020
I'm still struggling to understand when the runtime optimize and when it doesn't. The only moment I get clear optimized code is by using COMPlus_TC_QuickJit=0. If I use the MSBuild property or runtimeconfig.json it doesn't do anything. I still get the awfully bad performance.
If it's something I should check with the BenchmarkDotNet team tell me and I will close this here. But I have the feeling something not by design is going on. And the results seem to be pointing to it.
kunalspathak commentedon Apr 9, 2021
@ebfortin - I tried BenchmarkDotNet with the code you have shared and I see optimized code getting generated.
C# benchmark
Ran with:
dotnet Test.dll -filter *AdditionDouble2* --corerun %CORE_RUN%Assembly code