Is it possible to use LLVM instructions for vector operations not implemented in the standard library?

Zig provides a basic set of operations on vectors: Documentation - The Zig Programming Language. Is it possible to use LLVM instructions or some other form of direct, perhaps platform dependent intrinsics, to get direct access to the CPU SIMD instructions, which are not implemented in the standard library? For example gather/scatter instructions, i.e. LLVM Language Reference Manual — LLVM 23.0.0git documentation.

I have not used Zig yet, just kicking the tires.

Here is an example of dynamic shuffle that Zig does not support, but LLVM does:

1 Like

Maybe this will be possible with builtins in the future.

If I understand this correctly, there are two ways available now to use a not supported SIMD instruction: extern fn and asm(), right?

Yes. I’m honestly not sure if there is an advantage of using the LLVM intrisics over plain inline asm, maybe you could just use asm and have it work on all compiler backends.

In theory LLVM could know a bunch of things about the intrinsics and optimize stuff like reordering them as it chooses, choosing registers or even do constant folding - of course, keeping semantics. Raw asm blocks are AFAIK just treated as a black box and the optimizations a compiler is able to do with them depend largely on what clobbers and parameters you set.

2 Likes