afonso360 opened PR #6989 from afonso360:riscv-c
to bytecodealliance:main
:
:wave: Hey,
This PR makes introduces compressed instructions to the RISC-V backend as part of the C extension.
C extension introduction
The C Extension adds encodings for 16 bit instructions. These are allowed to be freely interleaved with uncompressed instructions (i.e. we don't need a mode switch like Thumb2).
Some C instruction formats cannot encode all registers, so they may only be emitted if they get assigned a certain physical register. Additionally C instructions (almost) always have an equivalent uncompressed instruction.
Implementation
I've implemented this as an encoding only change. So our VCode instructions remain the same, but if we happen to get the correct registers and all the stars line up we emit a compressed instruction instead of a full sized instruction.
Inst::emit
is now split into 3 stages:
- First we run the instruction through
Inst::allocate
which just transforms aInst
with virtual registers, into aInst
with physical registersWe then try to emit the instruction as a compressed instruction
* This may fail, and that's ok
* Since we now have the final physical registers we can check if it's possible to emit it as a compressed instructionIf the compressed emission fails we default to the uncompressed emission
This PR only enables 2 instructions
c.add
andc.mv
. Both of these use the CR format which allows the full range of registers. However they are enough to get us into trouble.c.mv
is emitted quite often, and is enough to "unalign" most of the rest of the code.Issues with capstone
Capstone supports decoding C instructions with a separate "Extra Mode".
Capstone currently cannot decode a number of instructions that we emit (i.e
Zca
,Zcb
,Zcs
or Vector Instructions). This is normally ok since it emits a.byte ...
directive and we can still read the V-Code to figure out what is going on.If I enable this "Extra Mode" by default it tries to decode these unknown instructions as C instructions and makes the whole thing unreadable.
My solution for this was to make C a non default extension, and only enable the Extra Mode when C is enabled. This lets us keep the normal disassembly for all current test cases. Mixing C instructions and Vector instructions still makes the whole thing unreadable, but at least its not the common case.
Runtests
I've made pretty much all runtests run with C as well as without. Even if the runtest will never directly emits a C instruction, it can emit for example a
c.mv
and emit the rest of the instructions "unaligned" which is also a configuration worth testing.
This is built on top of #6988 so it's worth waiting for that to be merged before looking into this. Additionally, it's probably a good idea to review this commit by commit instead of all at once.
afonso360 requested wasmtime-default-reviewers for a review on PR #6989.
afonso360 requested wasmtime-compiler-reviewers for a review on PR #6989.
afonso360 requested abrown for a review on PR #6989.
afonso360 updated PR #6989.
alexcrichton submitted PR review:
This all looks great to me, and thanks as always for the detail explanations!
Would it be possible to add detection of these features to
cranelift/native
?
afonso360 updated PR #6989.
afonso360 has enabled auto merge for PR #6989.
afonso360 merged PR #6989.
Last updated: Dec 23 2024 at 13:07 UTC