Unable to compile rvv vector intrinsics with clang

I am trying to compile risc-v vector intrinsic functions using clang-17. Following is my source code:

#include<riscv_vector.h>


int main(){
	unsigned long vl = vsetvl_e64m8(400);										

}

When I compile it using the following command:

/home/user3/LLVM_RISCV/llvm-project/build/bin/clang --target=riscv64 test.c -S -emit-llvm -o -

I get the following errors:

In file included from test.c:1:
/home/user3/LLVM_RISCV/llvm-project/build/lib/clang/17/include/riscv_vector.h:18:2: error: "Vector intrinsics require the vector extension."
#error "Vector intrinsics require the vector extension."
 ^
test.c:5:21: error: call to undeclared function 'vsetvl_e64m8'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
        unsigned long vl = vsetvl_e64m8(4);                                                                             
                           ^
2 errors generated.
  1. How can I specify to clang that I am trying to use risc-v Vector extension and how can I get rid of these errors?
    Please help.

Following the error, it wants to see __riscv_vector. That’s added here llvm-project/RISCV.cpp at a7de5c82bbbcd534d3a11e719eea513e3435752b · llvm/llvm-project · GitHub. So you likely need to add something along the lines of -march=<something>zve32x.

I see one example in the clang tests -march=rv64izve32x1p0.

There are a couple of questions:

  1. Is riscv64, aka riscv64-unknown-elf, a bare-metal triple, really what you want?
  2. What ISA does your target hardware have? Bare-metal triples default to rv64imac, whereas hosted (BSDs, Linux, etc) default to rv64gc.

As already mentioned, if you want to change the set of extensions then you use the -march option. If your core has the full-fat V extension then add v to the end, whereas if it’s an embedded core then you may need to subset it with the various Zve* instead.

So I tried running the same code using the following command:

clang -march=rv64izve32x1p0 test.c

I am now getting just this error:

test.c:5:21: error: call to undeclared function 'vsetvl_e64m8'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
        unsigned long vl = vsetvl_e64m8(4);                                                                             
                           ^
1 error generated.

I have built my llvm using the instructions given on this page.

All RVV intrinsics have prefix __riscv_ now. So it should be __riscv_vsetvl_e64m8.
References: