How to run Clang-LLVM for RISCV target machine for Bitmanip extension?

Hi,

Thank you in advance.

I am a beginner and trying to run Clang-LLVM for RISCV target machine for Bitmanip extension.I have some doubts regarding the Clang flags used for bitmanip like experimental-zbb.

Currently we have built LLVM using Embescom LLVM and their riscv-bitmanip branch.The following was the way I compile and run the code.

clang -O -c hello.c --target=riscv64
riscv64-unknown-elf-gcc hello.o -o hello -march=rv64imac -mabi=lp64
spike pk hello

Using this compilation after generating the assembly using the following I could not find bitmanip instructions generated like bext,bseti ,etc.I was getting these instructions generated in assembly by using gcc12.

I have used the following

riscv64-unknown-elf-objdump -d -S hello.o

to generate the assembly from the object file which is generated from ClangLLVM.

  1. Which are the flags I need to use to get riscv bitmanip instructions in the assembly file?

  2. Is this the right way to run Clang-LLVM for riscv -bitmanip instructions?

  3. Could you suggest a way to generate assembly from the compilation step itself?

  4. Also it would be helpful if anyone could help me with a beginner tutorial for LLVM development like printing ,intermediate code file and debugging LLVM.

  5. I could not find LLVM bitmanip support for ratified in original LLVM repo.Is there a branch for bitmanip instructions in it?

Thanks & Regards

Richu Norman

1 Like

Hi,

Now some of the bitmanip extensions have been ratified and marked as non-experimental, you do not need any of the previous bitmanip-specific builds of things, any LLVM 14 build (if using a stable release), or any upstream main branch as of January 13th 2022 will work for generating ratified bitmanip. The underlying support has been in LLVM for a number of years upstream (requiring the -menable-experimental-extensions flag), but LLVM 14 was the first to support the ratified extensions without the experimental flag.

When you are calling clang, to generate bitmanip instructions, you also need to provide a -march string to it that includes one (or more) of the supported bitmanip extensions (zba, zbb, zbc, zbs), and it will generate these extensions.

As an example, if I use the file

long hello(long a, long b) {
  return a&~b;
}

If I put zbb in the -march string, then I would expect the andn instruction from that extension to be generated (here I’m using -S to output assembly and -o- to send that to stdout rather than a file):

$ clang --target=riscv64 -O -S -o- hello.c -march=rv64imaczbb 
	.text
	.attribute	4, 16
	.attribute	5, "rv64i2p0_m2p0_a2p0_c2p0_zbb1p0"
	.file	"hello.c"
	.globl	hello
	.p2align	2
	.type	hello,@function
hello:
	andn	a0, a0, a1
	ret
.Lfunc_end0:
	.size	hello, .Lfunc_end0-hello

Hope this helps.

Thank you so much simonpcook.

Thank you so much for your valuable time.
I am using unratified instructions from b extensions.
Particularly zbp

clang --target=riscv64 -O -S -o- packu.c -march=rv64izbp -menable-experimental-extensions>packu.txt
clang-14: error: invalid arch name ‘rv64izbp’, experimental extension requires explicit version number zbp

Also I have tried the following which run without error but I could not generate instruction.

clang --target=riscv64 -O -S -o- packu.c -march=rv64imac -menable-experimental-extensions

Could you suggest the right way (‘-march’) for including unratified instructions?

@richunorman -march=rv64izbp0p93 should work for you (as the error message say, you need to explicitly indicate the version number when using an unratified extension).

thank you .

clang --target=riscv64 -O -S -o- packu.c -march=rv64izbp0p93 -menable-experimental-extensions>packu.txt

That worked.

1 Like