Hacker News new | ask | show | jobs
by parenthesis 1478 days ago
How can one go about making one's code apt for a compiler to be able to do these kinds of things?
4 comments

A good way is to have your data arranged in structs of arrays rather than in arrays of structs. This allows the compiler to generate code which just loads in linear sections of memory to SIMD registers. It’s also just more cache efficient in general.

Check out data oriented design if you aren’t already familiar.

Note that sometimes it's sufficient to have arrays of structs of arrays. That is you don't need to necessarily go whole hog.
That very much depends on access patterns. If you’re performing an operation I’ve ever object with a certain field, struct of array makes sense. If you’re doing an operation which uses many fields on some arbitrary dynamic randomly ordered subset of objects, then array of structs will yield better because at least you recover some memory locality.
You study autovectorizers, then you enable autovectorization warning flags, and carefully read your compilers output.

If the compiler says autovectorization failed, you rewrite the code until the autovectorizer works.

https://docs.microsoft.com/en-us/cpp/build/reference/qvec-re...

hm, I have had limited success with such nudging - it's certainly not a programming model with a predictable outcome. And as soon as the compiler or its flags are updated, maybe the situation changes.
I also wonder if there's any compiler that allows you to hint that you expect certain optinizations to occur (like vectorization), and if they do not, fails to compile at all.
Don’t bother. Autovectorization almost fundamentally doesn’t work.

You can write code using explicit vectors and it’s not too bad, though less cross platform than you’d like.

An actual answer involves things like restrict and alignment keywords.

Explicit vectors can be made portable (to SSE4/AVX2/AVX-512/NEON/SVE/SVE2/RISC-V V/WASM SIMD) using the Highway library which gives you wrapper functions over the platform's intrinsics, emulating missing functionality where required: github.com/google/highway. Disclosure: I am the main author.