Compiler emits inappropriate SSE2 instructions for f64 math #116359
Labels
A-codegen
Area: Code generation
A-floating-point
Area: Floating point numbers and arithmetic
A-LLVM
Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.
C-bug
Category: This is a bug.
I-slow
Issue: Problems and improvements with respect to performance of generated code.
O-x86_64
Target: x86-64 processors (like x86_64-*)
T-compiler
Relevant to the compiler team, which will review and decide on the PR/issue.
In my audio DSP application, the simple function that applies a biquad filter to a waveform sometimes runs much slower than expected.
The results are correct, just the performance is affected.
This only happens on Intel CPUs, when compiling with the default target settings.
See the discussion and investigation here: https://users.rust-lang.org/t/unexplained-order-of-magnitude-drop-in-performance
The affected function:
This is taken from the example published here: https://github.com/HEnquist/sse_subnormal_issue
The slowness has been traced down to subnormal numbers. This slows down Intel CPUs by an order of magnitude, while AMD cpus are nearly unaffected.
There are however no subnormals in any of the involved variables. The problem comes from that the compiler uses an SSE2 vector multiplication to do a single multiplication, without ensuring that the unused lane in the registers contain proper floating point values. The speed depends on what happens to be in the xmm4 register when entering the function. When it's a "bad" value, performance is terrible.
This is the assembly of the process_waveform function:
The problem occurs in xmm4. This register only occurs twice, first when loading a value into the high lane, which leaves the lower lane unchanged:
Then it is multiplied with xmm7, storing the result in xmm7:
Finally, only the high lane is copied from xmm7 to xmm1, and the garbage result in the lower lane is never used:
This gives the correct result, but the unused lower lane will multiply whatever was in xmm4 before entering the function. There is a high likelihood that this is not a normal floating point value, for example uint64 value 2 gets interpreted as a very small subnormal number (9.88e-324). And this makes the multiplication very slow.
I suppose it uses the MULPD instruction to keep using SSE and avoiding mixing SSE and normal FPU instructions. But when doing this, I would expect it to ensure that any unused lane contains good values that don't cause any trouble.
Meta
rustc --version --verbose
:Backtrace
The text was updated successfully, but these errors were encountered: