-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Adding custom instruction to the elf tool-chain #837
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
You can check this issue at first, this is helpful,
If you want to add the instructions in gcc, then it's more complicate. If you just want to add the instructions in binutils, then only build the binutils is enough, you don't need to build the entire toolchain.
If you want to disassemble the instructions by gdb, then yes, you should update both of them. If you need new operands or relocation for your custom instructions, then you need to update more files. Otherwise, just update the files you mentioned should be enough.
Generally, no need to configure again. But it is better to run "make clean" in your build folder before running "make". As the previous said, if you don't need to add the custom instructions in gcc, then you can just rebuild the binutils/gdb. cd /build-[binutils|gdb]-[newlib|linux]/ |
Hi Nelson , |
Hi, |
Hi, |
Hi,
|
If you want the compiler to emit new instructions, you have to modify the compiler. You have probably only modified binutils so far. The compiler work is much more involved than the binutils work, and depends on the complexity of the instruction. For simple instructions, you can just add a pattern to the machine description file. For complex instructions you may need to write a new optimization pass or modify an existing one. For a mod instruction, this can be done with a md file pattern. See the gcc/config/riscv/riscv.md file and search for "DIVISION and REMAINDER". The patterns here use macros "any_div" and "insn" that are defined above. If mod is same as rem, then you can modify the define_code_attr insn and substitute mod for rem. Otherwise you might need to write a new pattern. You can find docs for this file in gcc/docs/md.texi or read the online internals docs |
Hi Jim ,
My purpose was to implement custom instruction to reduce the cycle but replacing the mod with rem will not solve my purpose it seems since it will only change the name. In the riscv-opc.h files in both the binutils and gdb folder I also found that there are lot of instruction declaration named custom. Are they already implemented with some functionality because I can see the match and mask bit for them or in other words can I use them for custom instruction purpose if they are unused, because in that way I do not need to define a pattern myself rather I can use the functionality if it solves my purpose. |
If you want a custom instruction you have to do a lot of work. There is no easy solution here. I don't know what the "custom" stuff is for. I think it is some obsolete broken feature. Perhaps a feature that was replaced by the .insn feature. .insn is documented in the gas manual, and allows you to generate custom instruction encodings without modifying the gas sources. |
Hi Jim, |
I filed Binutils has testsuites that you can use to check for errors. Try "make check". It is a good idea to add new testcases when adding new features. You can also test binutils by building the compiler and running the compiler testsuite. How many files you need to modify depends on the complexity of the instruction you are adding. In the simplest case, you only need to modify riscv-opc.h and riscv-opc.c. Basically, write a testcase, and when you can assemble and disassemble it then you are done. You continue making changes until you reach this point. |
Fixed and committed to mainline, thanks. Nelson |
Hi Jim,
I suppose that the functionality of the absolute has already been implemented. Can you please help me out with in which section of the file should I look to confirm the absolute functionality since I want to implement the functionality for integer.
I was trying to define in this way for the integer part after going through the "parameterized name" from the docs that you have mentioned previously but then I am confused about how to write the rest of the portion of the define_insn. |
Hi Jim,
I am confused about this line: "abs.\t%0,%1" and set_attr "mode" " . What should be in place of UNITMODE. |
mode definition is also in the riscv.md
Since both of you op0 and op1 are in SImode, and your target is 32bit, I believe it would be ok to use SI Some extra info from gccint you probably need: |
if like you said, you just want an abssi2, then adjust Further more, if you want something like abssi2, absdi2 for SI and DImode for |
Hi Levy,
Regarding restriction of the of the match operand that you have mentioned , I want the custom instruction only for 32 bit data. That is the reason I have replaced ANYI with SI in particular as was mentioned in the Now according to my understanding from the overview section of the Machine-Desc document the template given above is inserted into instruction list. So is that all if I want get an absolute functionality implemented in the toolchain or do I need to make any more modifications in this file or any other file like if u can figure out my doubt from the chain mail above.
As you see abs function is taking instructions srai, xor and sub. But all I want is that when the compiler sees the abs function in C code it should show abs instead of all the three instructions. So how do I implement the functionality for the compiler to recognize it. Any suggestion will be of great help. |
The md file you wrote should work now, but like mentioned by Jim and Nelson, "If you want a custom instruction you have to do a lot of work. There is no easy solution here." Because C code and assembly code are not that 1 on 1 match (or say, equal), therefore we need a compiler to do a series of "translation". First, you need to know where a certain function is implemented, for your case, abs() is defined in /glibc(or newlib)/stdlib/abs.c: Narrowing down this a bit, how do other patterns in the .md file get matched (have a look at optabs.def), then trace it backward. in your case I believe the most feasible way is to call the inline assembly: And my other suggestion includes using Jim says a lot of work and no easy, he means it. :) |
Hi, I tried Adding the custom instruction to spike ISA simulator, and have done below given steps: In the riscv-isa-sim/riscv/encoding.h add the following lines: Create a file riscv-isa-sim/riscv/insns/mod.h and add these lines: Add this file to riscv-isa-sim/riscv/riscv.mk.in In riscv-isa-sim/spike_main/disasm.cc add the following lines: And then I again build it, but its showing the following error: Please let me know if am I missing any steps that I need to do?Or anything else that can be done to add new instructions in riscv |
On Wed, Mar 31, 2021 at 11:31 AM AnushaGoel14 ***@***.***> wrote:
../spike_main/disasm.cc: In constructor
‘disassembler_t::disassembler_t(int)’:
../spike_main/disasm.cc:295:38: error: ‘match_mod’ was not declared in
this scope
add_insn(new disasm_insn_t(name, match_##code, mask_##code | (extra),
*VA_ARGS*));
The match_* symbols are defined where encoding.h is included, which
suggests that there is something wrong with your encoding.h changes. Try
adding --save-temps to the gcc command used to build the disasm.cc file,
and then look at the disasm.ii file to check that you got the expected
result. You can use the command used to compile disasm.cc in the build.log
file.
Jim
|
Hi @subhajit26 |
Hi @chenc6,
Luckily, I did not face any such fatal error while
implementing custom instructions.
Regards,
Subhajit.
…On Thu, May 13, 2021 at 2:37 AM chenc6 ***@***.***> wrote:
Hi @subhajit26 <https://github.com/subhajit26>
Did you see the Fatal error: duplicate add (maybe other instructions) when
you put your new instructions in the subset?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#837 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AQUE5YBRTU6O3M4Y376WHDDTNMNN7ANCNFSM4XAMIYJQ>
.
|
I believe that error is because your opcode is matching with the opcode of the add instruction, so there are basically two instructions which have the same opcode now. Just change the funct7 opcode and you should be fine |
Hi @subhajit26, |
Hi,
I am trying to get familiar with the whole flow of adding custom instruction to the elf tool-chain and I am planning to implement a mod instruction since it is easily accessible over the internet. Since tool-chain building is very time consuming process I need to confirm certain things before I go ahead with custom instruction flow. First, there are 2 riscv-opc.c and riscv-opc.h files in riscv-gdb and riscv-binutils folder. Do I need to update both of them in order to get the custom instruction implemented successfully.
Second, do I need to run this command "./configure --prefix=/opt/riscv
make"
again after adding the custom instruction because while building for the first time I could see that as soon as make is executed the tool-chain started cloning and building. So my doubt is that if I start building again will the opc files contain those updated information . Please suggest.
Please also suggest do I need to make any modification in the Makefile regarding the addition of custom instruction.
The text was updated successfully, but these errors were encountered: