216 points · by payasr · 7y ago · 94 comments
What I've found is that you need to ignore most of Rust: use/load raw pointers, don't use slices, unroll manually, vectorize manually, and check preconditions manually. You'll still get the amazing type system, but the code will have to be more C-like than Rust-like.
* raw pointers: LLVM is pretty good at optimizing C code. Rust specific optimization needs some work. (edit: I assumed arrays here, so you'll need the pointer for offsets; references are still okay. You'd also use the pointers for iterating instead of the usual slice iteration patterns)
* no slices: index checking is expensive, not to the CPU, the CPU rarely misses the check branches, but to the optimizer. I've found these are mostly left un-elided, even after inlining.
* no slices: slice indexing uses overflow checking. For Rav1e's case, the block/plane sizes mean that doing the index calculation using `u32` will never overflow, so calculating the offsets using u32 is fine (I'll have to switch to using a pseudo u24 integer for GPUs though, because u32 is still expensive on them).
* unroll manually: LLVM would probably do more of this with profiling info, but I've never found it (this is subjective!) to do any unrolling w/o. Maybe if all the other items here are also done...
* vectorize manually: Similar to unrolling. I've observed only limited automatic vectorization.
* And to get safety back: check, check, and check before calling the fast kernel! Ie wrap the kernel function with one that does all the checks elided in the kernel.
Source: Wrote https://github.com/xiph/rav1e/pull/1716, which speeds up the non-asm encodes by over 2x!
Edit: Although, on rereading the above post, I see diamondlovesyou did mention indices, so... not really sure what's going on.
Even for slices, using get_unchecked(1..) to get a smaller subslice without bounds checking might be better than pointer arithmetic as long as the slice lengths get optimized away (i.e. they are never used and never passed to non-inlined functions).
AFAIK this does not work atm due to a codegen bug in llvm (which can also affect code using restrict in C in some cases). This bug will get fixed one day, but most likely another bug will be revealed at this point… This part of LLVM was never really used in C as much as in Rust, so they keep finding bugs in it. Hopefully it will get fine in the long run, but I'm not holding my breath.
``` for (i, val) in my_slice.iter().enumerate() { let x = *val + 9999; } ```
You have to pay the price of compatibility with the existing toolchain even if it's not your explicit goal.
the primary reason THIS code didn't optimize as well is that you can't pass ffast-math to llvm from rust in any useful way (yet)
Seems a little gung ho to disable guaranteed index checking in a video codec no? I know you still do the checks, but it sounds like it's not in a statically guaranteed way.
You can still have them enabled for debugging, at least. (Something not generally possible in C/C++, sadly.)
Also depends how much it affects performance. It it doubles it, ok I'll accept the risk. 10% faster? No thanks I'd rather play it safe.
f64::mul_add(self, a: f64, b: f64) -> f64
Adding it to the code indeed allows the LLVM to generate the "vfma" instruction. But it didn't significantly improve performance, on my machine at least. $ ./iterators 1000
Normalized Average time = 0.0000000011943495282455513
sumb=89259.51980374461
$ ./mul_add 1000
Normalized Average time = 0.0000000011861410852805122
sumb=89259.52037960211
Maybe the performance gap is not due to what the author thought...[1] https://doc.rust-lang.org/std/primitive.f64.html#method.mul_...
"The program" contains two hot loops. Judging from the assembly code you linked in a sibling comment, only the second of these loops was vectorized, the first one wasn't. This slower non-vectorized loop will still dominate execution time.
And for whatever it's worth, dropping the original article's
for i in 0..n{
b[i]=b[i]+(r/beta)*(c[i]-a[i]*b[i])
}
in place of your loop using iterators and FMA, you still get nicely vectorized code (though without FMA) for this loop: .LBB6_120:
vmovupd zmm2, zmmword ptr [r12 + 8*rcx]
vmovupd zmm3, zmmword ptr [r12 + 8*rcx + 64]
vmovupd zmm4, zmmword ptr [r12 + 8*rcx + 128]
vmovupd zmm5, zmmword ptr [r12 + 8*rcx + 192]
vmovupd zmm6, zmmword ptr [r14 + 8*rcx]
vmovupd zmm7, zmmword ptr [r14 + 8*rcx + 64]
vmovupd zmm8, zmmword ptr [r14 + 8*rcx + 128]
vmovupd zmm9, zmmword ptr [r14 + 8*rcx + 192]
vmulpd zmm10, zmm2, zmmword ptr [rbx + 8*rcx]
vsubpd zmm6, zmm6, zmm10
vmulpd zmm10, zmm3, zmmword ptr [rbx + 8*rcx + 64]
vsubpd zmm7, zmm7, zmm10
...
Neither FMA nor iterators make a difference for whether this loop is vectorized, so any speedups are necessarily limited.also, sometimes a SIMD instruction is used but only on 1 lane at a time. this is actually common with floating point code.
Edit: After some search I found these:
https://github.com/rust-lang/rust/issues/21690
https://doc.rust-lang.org/core/intrinsics/fn.fadd_fast.html
Writing a wrapper around f64 that uses these intrinsics shouldn't be too hard. I don't program in Rust though.
For this example you'd probably want -fassociative-math and not the other stuff that may result in incorrect code. -ffast-math was not actually used in the clang compilation and it's possible that the -fvectorize that was used picks a sensible mix of options.
Here's a preliminary discussion:
https://internals.rust-lang.org/t/pre-rfc-whats-the-best-way...
Trying to do an RFC process for this could be useful. The rust development process seems to be pretty good at thinking deeply about these things.
As a C++ programmer who routinely uses fast-math "until something breaks" with DSP code, I would find that capability very attractive.
writing a wrapper is not trivial, at all.
what Rust could do though, is add a wrapped float type, that the compiler will forward to llvm saying "you can ffast-math these" That is the approach Rust tends to take for these kinds of things, though no plans are in the works to do this to make floats vectorizeable yet. Maybe we should start such plans?
It's worth explaining why. So far as I understand the situation, dependencies may assume and depend on the absence of ffast-math. Enabling it globally for a compilation is generally considered to be a footgun for this reason.
In my experience -Ofast/-ffast-math yields very impressive results for FP code.
If you can tolerate platform-specific variation in the trailing parts of floating point numbers, it's wonderful.
C++ compilers have flags to enable it globally. gcc and clang include the optimization in -Ofast.
Rust allows you to choose at a code level (but usually people don't know about it). Perhaps it should also have a global fast-math flag that would automatically optimize it. Pros and cons to that.
GCC emits FMA instructions at -O2 without -ffast-math.
It is interesting to note that, whereas most ffast-math optimization will trade precision for reduced computing time, adding an FMA can only improve the precision of the output (and thus it is a safe optimization).
enum List<T> {
Nil,
Cons(T, List<T>)
}
instead, you have to box/reference-ify the recursive occurrence: enum List<T> {
Nil,
Cons(T, Box<List<T>>)
}
so in certain circumstances it doesn't let you "coproduct" two types together, you might need to modify one a bit, which makes it a technically-not-exactly-a-coproduct (i think). a bit of a stretch but it sort of makes sense next to a by-reference-only ML langs where you can (co)product anything as you please(btw, it's the same for recursive products)
---
1 - https://users.rust-lang.org/t/recursive-enum-types/2938/2
enum List<'a, T> {
Nil,
Cons(T, &'a List<'a, T>)
}
fn main() {
let list = List::Cons("hello", &List::Nil);
}
Box is usually chosen because it's a good default choice.We may add a wrapping type, similar to what we do for integer behavior. But in general, adding flags to change major behavior is not something we do.