Ajumal Abdul Majeed via llvm-dev
2019-Apr-30 10:18 UTC
[llvm-dev] Doubt about adding a new instruction
Hi All, I am very new to LLVM and want to do the following. I want a C code to be compiled with clang. The compiled C code is actually intended to run in a RISCV simulator called spike. I was actually working with riscv64-unknown-elf-gcc and when I do riscv64-unknown-elf-objdump -dC a.out > a.dump, The a.dump will have my new instruction line like this. 101a0: c00017fb newinstruction a5 I was using inline assembly code and done some changes in the assembler (as mentioned in https://nitish2112.github.io/post/adding-instruction-riscv/) to get the above result, But now I want the instruction to be recognized by the clang itself and give me the above result. Kindly give me some suggestion to start and complete this task. Thank you, Ajumal -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20190430/c5c25d9d/attachment.html>
Tim Northover via llvm-dev
2019-Apr-30 12:14 UTC
[llvm-dev] Doubt about adding a new instruction
On Tue, 30 Apr 2019 at 11:18, Ajumal Abdul Majeed via llvm-dev <llvm-dev at lists.llvm.org> wrote:> I was using inline assembly code and done some changes in the assembler (as mentioned in https://nitish2112.github.io/post/adding-instruction-riscv/) to get the above result, But now I want the instruction to be recognized by the clang itself and give me the above result.>From the link it doesn't look like the encoding chosen fits perfectlyinto any of the existing classes, so you'll probably have to edit both lib/Target/RISCV/RISCVInstrFormats.td and RISCVInstrInfo.td. Your end goal is to create an instance of RVInst (RISCVInstrFormats.td, line 79) with the fields populated correctly. You could just do that directly, but it's probably best to at least try to fit in with the existing scheme, if nothing else as a much better learning exercise in how LLVM really works. So the key bits you should be looking at are: InstrFormats, line 57ish. These definitions go into bits 6-2 of the "RVInst". Your new instruction doesn't match any of these values, so you'll need to create an extra one. InstrInfo, line 310. ALU_rr is closest in effect to the instruction you want, and if you look at how it relates to RVInstR (InstrFormats again) you can see that it gives you the ability to set most of the fields you need to. But it obviously uses a wrong OPC_* from above. So you should probably duplicate that class with a slightly adjusted name and using your new OPC_WHATEVER instead. Finally, looking at the instructions that use ALU_rr, you see they just provide the remaining fixed bits of the instruction and the mnemonic (for example "def SRL" on line 405). Yours should do the same. That should be enough to get Clang's assembler and llvm-objdump to know about the new instruction. Cheers. Tim.