rust/compiler/rustc_middle/src/ty
Matthias Krüger 7d78885a8e
Rollup merge of #111891 - rustbox:feat/riscv-isr-cconv, r=jackh726
feat: `riscv-interrupt-{m,s}` calling conventions

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
2023-08-09 22:59:58 +02:00
..
consts inline format!() args up to and including rustc_middle 2023-07-30 13:18:33 +02:00
context Don't leak the function that is called on drop 2023-05-23 14:53:36 +00:00
inhabitedness refactor(rustc_middle): Substs -> GenericArg 2023-07-14 13:27:35 +01:00
print Rollup merge of #114548 - fee1-dead-contrib:migrate-to-trans, r=davidtwco 2023-08-09 06:32:25 +02:00
_match.rs Move TyCtxt::mk_x to Ty::new_x where applicable 2023-07-05 20:27:07 +01:00
abstract_const.rs Make everything builtin! 2023-07-25 16:08:58 +00:00
adjustment.rs Rename adjustment::PointerCast and variants using it to PointerCoercion 2023-07-07 18:17:16 +02:00
adt.rs Store the laziness of type aliases in the DefKind 2023-08-07 15:54:31 +02:00
assoc.rs Rollup merge of #113698 - compiler-errors:rpitit-check, r=spastorino 2023-07-14 19:33:29 +02:00
binding.rs Make everything builtin! 2023-07-25 16:08:58 +00:00
cast.rs Remove mir::CastKind::Misc 2022-10-06 15:32:41 +03:00
closure.rs Migrate a trait selection error to use diagnostic translation 2023-08-07 05:26:38 +00:00
codec.rs Remove constness from ParamEnv 2023-07-27 15:50:42 +00:00
consts.rs inline format!() args up to and including rustc_middle 2023-07-30 13:18:33 +02:00
context.rs Auto merge of #114545 - fee1-dead-contrib:lower-impl-effect, r=oli-obk 2023-08-08 19:23:41 +00:00
diagnostics.rs Store the laziness of type aliases in the DefKind 2023-08-07 15:54:31 +02:00
erase_regions.rs Move expansion of query macros in rustc_middle to rustc_middle::query 2023-05-15 08:49:13 +02:00
error.rs inline format!() args up to and including rustc_middle 2023-07-30 13:18:33 +02:00
fast_reject.rs add FIXME 2023-07-20 11:05:52 +02:00
flags.rs refactor(rustc_middle): Substs -> GenericArg 2023-07-14 13:27:35 +01:00
fold.rs Move TyCtxt::mk_x to Ty::new_x where applicable 2023-07-05 20:27:07 +01:00
generic_args.rs Rename arg_iter to iter_instantiated 2023-07-17 21:04:12 +00:00
generics.rs Rename arg_iter to iter_instantiated 2023-07-17 21:04:12 +00:00
impls_ty.rs refactor(rustc_middle): Substs -> GenericArg 2023-07-14 13:27:35 +01:00
instance.rs inline format!() args up to and including rustc_middle 2023-07-30 13:18:33 +02:00
layout.rs feat: riscv-interrupt-{m,s} calling conventions 2023-08-08 18:09:56 -07:00
list.rs Add a new trait to Debug things with an infcx available 2023-07-06 11:36:39 +01:00
mod.rs avoid more ty::Binder:dummy 2023-08-03 14:16:26 +02:00
normalize_erasing_regions.rs inline format!() args up to and including rustc_middle 2023-07-30 13:18:33 +02:00
opaque_types.rs inline format!() args up to and including rustc_middle 2023-07-30 13:18:33 +02:00
parameterized.rs Use u64 for incr comp allocation offsets 2023-07-14 17:03:34 -04:00
relate.rs Remove constness from TraitPredicate 2023-08-02 15:38:00 +00:00
rvalue_scopes.rs Enable potential_query_instability lint in rustc_hir_typeck. 2023-07-14 10:10:14 +02:00
structural_impls.rs Remove constness from TraitPredicate 2023-08-02 15:38:00 +00:00
sty.rs Store the laziness of type aliases in the DefKind 2023-08-07 15:54:31 +02:00
trait_def.rs refactor(rustc_middle): Substs -> GenericArg 2023-07-14 13:27:35 +01:00
typeck_results.rs refactor(rustc_middle): Substs -> GenericArg 2023-07-14 13:27:35 +01:00
util.rs Store the laziness of type aliases in the DefKind 2023-08-07 15:54:31 +02:00
visit.rs Placeholder nit 2023-08-03 20:05:40 +00:00
vtable.rs inline format!() args up to and including rustc_middle 2023-07-30 13:18:33 +02:00
walk.rs refactor(rustc_middle): Substs -> GenericArg 2023-07-14 13:27:35 +01:00