Similar to prior support added for the mips430, avr, and x86 targets
this change implements the rough equivalent of clang's
[`__attribute__((interrupt))`][clang-attr] for riscv targets, enabling
e.g.
```rust
static mut CNT: usize = 0;
pub extern "riscv-interrupt-m" fn isr_m() {
unsafe {
CNT += 1;
}
}
```
to produce highly effective assembly like:
```asm
pub extern "riscv-interrupt-m" fn isr_m() {
420003a0: 1141 addi sp,sp,-16
unsafe {
CNT += 1;
420003a2: c62a sw a0,12(sp)
420003a4: c42e sw a1,8(sp)
420003a6: 3fc80537 lui a0,0x3fc80
420003aa: 63c52583 lw a1,1596(a0) # 3fc8063c <_ZN12esp_riscv_rt3CNT17hcec3e3a214887d53E.0>
420003ae: 0585 addi a1,a1,1
420003b0: 62b52e23 sw a1,1596(a0)
}
}
420003b4: 4532 lw a0,12(sp)
420003b6: 45a2 lw a1,8(sp)
420003b8: 0141 addi sp,sp,16
420003ba: 30200073 mret
```
(disassembly via `riscv64-unknown-elf-objdump -C -S --disassemble ./esp32c3-hal/target/riscv32imc-unknown-none-elf/release/examples/gpio_interrupt`)
This outcome is superior to hand-coded interrupt routines which, lacking
visibility into any non-assembly body of the interrupt handler, have to
be very conservative and save the [entire CPU state to the stack
frame][full-frame-save]. By instead asking LLVM to only save the
registers that it uses, we defer the decision to the tool with the best
context: it can more accurately account for the cost of spills if it
knows that every additional register used is already at the cost of an
implicit spill.
At the LLVM level, this is apparently [implemented by] marking every
register as "[callee-save]," matching the semantics of an interrupt
handler nicely (it has to leave the CPU state just as it found it after
its `{m|s}ret`).
This approach is not suitable for every interrupt handler, as it makes
no attempt to e.g. save the state in a user-accessible stack frame. For
a full discussion of those challenges and tradeoffs, please refer to
[the interrupt calling conventions RFC][rfc].
Inside rustc, this implementation differs from prior art because LLVM
does not expose the "all-saved" function flavor as a calling convention
directly, instead preferring to use an attribute that allows for
differentiating between "machine-mode" and "superivsor-mode" interrupts.
Finally, some effort has been made to guide those who may not yet be
aware of the differences between machine-mode and supervisor-mode
interrupts as to why no `riscv-interrupt` calling convention is exposed
through rustc, and similarly for why `riscv-interrupt-u` makes no
appearance (as it would complicate future LLVM upgrades).
[clang-attr]: https://clang.llvm.org/docs/AttributeReference.html#interrupt-risc-v
[full-frame-save]: 9281af2ecf/src/lib.rs (L440-L469)
[implemented by]: b7fb2a3fec/llvm/lib/Target/RISCV/RISCVRegisterInfo.cpp (L61-L67)
[callee-save]: 973f1fe7a8/llvm/lib/Target/RISCV/RISCVCallingConv.td (L30-L37)
[rfc]: https://github.com/rust-lang/rfcs/pull/3246
Cranelift codegen backend for rust
The goal of this project is to create an alternative codegen backend for the rust compiler based on Cranelift. This has the potential to improve compilation times in debug mode. If your project doesn't use any of the things listed under "Not yet supported", it should work fine. If not please open an issue.
Building and testing
$ git clone https://github.com/bjorn3/rustc_codegen_cranelift
$ cd rustc_codegen_cranelift
$ ./y.sh prepare
$ ./y.sh build
To run the test suite replace the last command with:
$ ./test.sh
For more docs on how to build and test see build_system/usage.txt or the help message of ./y.sh.
Precompiled builds
Alternatively you can download a pre built version from the releases page.
Extract the dist directory in the archive anywhere you want.
If you want to use cargo clif build instead of having to specify the full path to the cargo-clif executable, you can add the bin subdirectory of the extracted dist directory to your PATH.
(tutorial for Windows, and for Linux/MacOS).
Usage
rustc_codegen_cranelift can be used as a near-drop-in replacement for cargo build or cargo run for existing projects.
Assuming $cg_clif_dir is the directory you cloned this repo into and you followed the instructions (y.sh prepare and y.sh build or test.sh).
In the directory with your project (where you can do the usual cargo build), run:
$ $cg_clif_dir/dist/cargo-clif build
This will build your project with rustc_codegen_cranelift instead of the usual LLVM backend.
For additional ways to use rustc_codegen_cranelift like the JIT mode see usage.md.
Building and testing with changes in rustc code
This is useful when changing code in rustc_codegen_cranelift as part of changing main Rust repository.
This can happen, for example, when you are implementing a new compiler intrinsic.
Instruction below uses $RustCheckoutDir as substitute for any folder where you cloned Rust repository.
You need to do this steps to successfully compile and use the cranelift backend with your changes in rustc code:
cd $RustCheckoutDir- Run
python x.py setupand choose option for compiler (b). - Build compiler and necessary tools:
python x.py build --stage=2 compiler library/std src/tools/rustdoc src/tools/rustfmt- (Optional) You can also build cargo by adding
src/tools/cargoto previous command.
- (Optional) You can also build cargo by adding
- Copy exectutable files from
./build/host/stage2-tools/<your hostname triple>/releaseto./build/host/stage2/bin/. Note that you would need to do this every time you rebuiltrustrepository. - Copy cargo from another toolchain:
cp $(rustup which cargo) .build/<your hostname triple>/stage2/bin/cargo- Another option is to build it at step 3 and copy with other executables at step 4.
- Link your new
rustcto toolchain:rustup toolchain link stage2 ./build/host/stage2/. - (Windows only) compile the build system:
rustc +stage2 -O build_system/main.rs -o y.exe. - You need to prefix every
./y.sh(oryif you builtbuild_system/main.rsasy) command byrustup run stage2to make cg_clif use your local changes in rustc.
rustup run stage2 ./y.sh preparerustup run stage2 ./y.sh build- (Optional) run tests:
rustup run stage2 ./y.sh test
- Now you can use your cg_clif build to compile other Rust programs, e.g. you can open any Rust crate and run commands like
$RustCheckoutDir/compiler/rustc_codegen_cranelift/dist/cargo-clif build --release.
Configuration
See the documentation on the BackendConfig struct in config.rs for all
configuration options.
Not yet supported
- Inline assembly (no cranelift support)
- On UNIX there is support for invoking an external assembler for
global_asm!andasm!.
- On UNIX there is support for invoking an external assembler for
- SIMD (tracked here,
std::simdfully works,std::archis partially supported) - Unwinding on panics (no cranelift support,
-Cpanic=abortis enabled by default)
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you shall be dual licensed as above, without any additional terms or conditions.