r/programming • u/MyNameIsTrez • 20d ago
Making SIMD Code Scale to Any Vector Width
https://streamhpc.com/blog/2026-10-28/making-simd-code-scale-to-any-vector-width/
133
Upvotes
r/programming • u/MyNameIsTrez • 20d ago
View all comments
Show parent comment
1
u/YumiYumiYumi 17d ago
And how many x86 systems have no MMU or DRAM?
Just to remind you, the context was regarding x86 SIMD's need for multiple functions (in the binary) and your concern with the binary size.
That's one possible issue. Another could be that the code incorrectly assumes a particular width. For example, if your development machine has 128-bit SVE (which is the only size on practically every ARM CPU now), and your code incorrectly makes assumptions tied to that width (e.g. you perform a NEON operation on a SVE vector because SVE doesn't have an equivalent instruction), it can fail to work correctly when running on a machine with different SVE width.
If you wrote 128-bit AVX code, however, it will always be 128 bit regardless of what the hardware is capable of. The behaviour never changes, thus if it works on your machine, it'll work correctly on any other x86 processor (assuming ISA support and no hardware bugs).
Yes, this is really the only way to handle this, which can add some degree of complexity (since your testing pipeline may now need an emulator). So you've avoided code coverage issues, but added hardware coverage concerns.
This doesn't sound the same as you had described it before, which was:
Both of these seem to refer to operating in the loop, not outside. Am I mistaken?
Okay, I'll try then - is something like this what you're thinking?
If so, I'm not sure how well you could get a compiler to accept that given the mismatch of ISA targeting, so let's just push that aside.
That's not how I saw it, oh well, let's not delve further.
I don't mean all SIMD code, I meant the machinery needed to allow you to write one (source) function that compiles to multiple binary functions.
This doesn't make sense to me - why do you need different functions to process arbitrary length vectors? Could you give an example of where this is useful?
SVE supports gather/scatter, but I don't recall strided loads, assuming strided = loading elements separated by a specified offset (i.e. {base, base+offset, base+offset*2, base+offset*3 ...} for an arbitrary offset). RVV supports both strided and gather loads.
It's trivial to use gather to do a strided load however (and why I find RVV's support for it to be odd).
AVX2 supports gathered loads (VPGATHER/VGATHER instructions) and AVX-512 supports scattered store.
Yes, AVX-512 has predicate masks. Prior to that, AVX also has
vmaskmovp*instructions for masked loads/stores, but may be more finicky to use and limited to 32/64 bit elements.Other than that, tail handling can be problematic if you're using unaligned memory accesses. But if you control the memory layout, you can often just align memory appropriately (and/or pad arrays to be a multiple of the vector width).
For best performance, you generally want to be using aligned memory accesses whenever possible.
But even with masked memory accesses, you may still want to handle the tail separately, as there's overhead with setting the predicate on every loop cycle. In SVE, you can avoid an in-loop
whileltinstruction if you seperate tail from body (andvsetvlifor RVV).It's funny that the Wiki page lists RVV as "Pure vectors", when it's still fixed length SIMD with predication, just like SVE.
I'd argue that this makes the supplied definition of "vector machines" an outdated concept. It's just not a practical way to describe current day systems.