How to use: @llvm.ptrmask


I have been thinking about way to better make use of @llvm.ptrmask.
In C, one can craft ASM instructions to be passed through the compiler, and passed to the assembler.
#include <stdio.h>
#include <stdlib.h>
int myA = 1;
int myB;
int main(int argc, char** argv) {
asm (
“pushq %rax \n\t;”
“pushq %rbx \n\t”
“movl myA(%rip),%eax \n\t”
“movl $01, %ebx\n\t”
“addl %ebx, %eax\n\t”
“movl %eax,myB(%rip) \n\t”
“popq %rbx \n\t”
“popq %rax \n\t”
printf("%d", myB);
return (EXIT_SUCCESS);

Is there a way to, in C, be able to craft LLVM instructions to be passed through CLANG and passed directly to LLVM ?

Kind Regards


The only way is through the use of __builtin_* functions.
In the case of @llvm.ptrmask I don’t think there’s a corresponding __builtin function. So right now you have to rely on the compiler to recognize the idiom and use the right instruction.

One thing you can do to access some LLVM intrinsics from C/C++ code is to abuse the ASM name feature to generate an external function which turns out to be an intrinsic, e.g.:

extern int* ptrmask(int* ptr, unsigned long mask) asm("llvm.ptrmask.p0.i64");
int* mask_pointer(int* num) {
    return ptrmask(num, 0x7);

(This approach does require you to implement LLVM’s name mangling yourself–note that the name mangling in this example assumes opaque pointers.)