Julia equivalent of C compiler intrinsics?

llvmcall with CPU intrinsics. It is a pain, though. For an example, see https://discourse.julialang.org/t/compiling-to-branch-table/16599/9 and consider that the sext of vector comparison results is essential (this is what x86 does natively) while SIMD.jl uses zext. This is why the pmovmskb/ _mm_movemask_epi8 composes well with pcmpeqb / _mm_cmpeq_epi8, and does not compose with SIMD.jl comparison.

The preferred way is to figure out the age old C idiom for whatever you want to do, because llvm was made for clang, and both clang and your processor’s instruction set were made for old C idioms. Hence this will often compile to something good.

The second preferred way is to hope that somebody has written a julia wrapper. Only afterwards one should look whether llvm exposes your intrinsic, under which crazy name and with what crazy calling convention.

Of course llvm only exposes the intrinsic if they have no idiom (in llvm IR!) that reliably compiles to whatever instruction you want, but good luck finding a documentation for that. I passionately hate the llvm docs for processor intrinsics. I often reverse the calling convention from the llvm unit tests; alternatively, compile from C to llvm IR with clang and reverse engineer that.

4 Likes