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
This commit is contained in:
commit
7d78885a8e
33 changed files with 513 additions and 69 deletions
|
@ -603,6 +603,25 @@ pub enum Conv {
|
|||
AmdGpuKernel,
|
||||
AvrInterrupt,
|
||||
AvrNonBlockingInterrupt,
|
||||
|
||||
RiscvInterrupt {
|
||||
kind: RiscvInterruptKind,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, HashStable_Generic)]
|
||||
pub enum RiscvInterruptKind {
|
||||
Machine,
|
||||
Supervisor,
|
||||
}
|
||||
|
||||
impl RiscvInterruptKind {
|
||||
pub fn as_str(&self) -> &'static str {
|
||||
match self {
|
||||
Self::Machine => "machine",
|
||||
Self::Supervisor => "supervisor",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Metadata describing how the arguments to a native function
|
||||
|
@ -753,6 +772,12 @@ impl FromStr for Conv {
|
|||
"AmdGpuKernel" => Ok(Conv::AmdGpuKernel),
|
||||
"AvrInterrupt" => Ok(Conv::AvrInterrupt),
|
||||
"AvrNonBlockingInterrupt" => Ok(Conv::AvrNonBlockingInterrupt),
|
||||
"RiscvInterrupt(machine)" => {
|
||||
Ok(Conv::RiscvInterrupt { kind: RiscvInterruptKind::Machine })
|
||||
}
|
||||
"RiscvInterrupt(supervisor)" => {
|
||||
Ok(Conv::RiscvInterrupt { kind: RiscvInterruptKind::Supervisor })
|
||||
}
|
||||
_ => Err(format!("'{s}' is not a valid value for entry function call convention.")),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -92,6 +92,7 @@ impl<A: ToJson> ToJson for Option<A> {
|
|||
|
||||
impl ToJson for crate::abi::call::Conv {
|
||||
fn to_json(&self) -> Json {
|
||||
let buf: String;
|
||||
let s = match self {
|
||||
Self::C => "C",
|
||||
Self::Rust => "Rust",
|
||||
|
@ -110,6 +111,10 @@ impl ToJson for crate::abi::call::Conv {
|
|||
Self::AmdGpuKernel => "AmdGpuKernel",
|
||||
Self::AvrInterrupt => "AvrInterrupt",
|
||||
Self::AvrNonBlockingInterrupt => "AvrNonBlockingInterrupt",
|
||||
Self::RiscvInterrupt { kind } => {
|
||||
buf = format!("RiscvInterrupt({})", kind.as_str());
|
||||
&buf
|
||||
}
|
||||
};
|
||||
Json::String(s.to_owned())
|
||||
}
|
||||
|
|
|
@ -38,6 +38,8 @@ pub enum Abi {
|
|||
PlatformIntrinsic,
|
||||
Unadjusted,
|
||||
RustCold,
|
||||
RiscvInterruptM,
|
||||
RiscvInterruptS,
|
||||
}
|
||||
|
||||
impl Abi {
|
||||
|
@ -107,11 +109,29 @@ const AbiDatas: &[AbiData] = &[
|
|||
AbiData { abi: Abi::PlatformIntrinsic, name: "platform-intrinsic" },
|
||||
AbiData { abi: Abi::Unadjusted, name: "unadjusted" },
|
||||
AbiData { abi: Abi::RustCold, name: "rust-cold" },
|
||||
AbiData { abi: Abi::RiscvInterruptM, name: "riscv-interrupt-m" },
|
||||
AbiData { abi: Abi::RiscvInterruptS, name: "riscv-interrupt-s" },
|
||||
];
|
||||
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
pub enum AbiUnsupported {
|
||||
Unrecognized,
|
||||
Reason { explain: &'static str },
|
||||
}
|
||||
|
||||
/// Returns the ABI with the given name (if any).
|
||||
pub fn lookup(name: &str) -> Option<Abi> {
|
||||
AbiDatas.iter().find(|abi_data| name == abi_data.name).map(|&x| x.abi)
|
||||
pub fn lookup(name: &str) -> Result<Abi, AbiUnsupported> {
|
||||
AbiDatas.iter().find(|abi_data| name == abi_data.name).map(|&x| x.abi).ok_or_else(|| match name {
|
||||
"riscv-interrupt" => AbiUnsupported::Reason {
|
||||
explain: "please use one of riscv-interrupt-m or riscv-interrupt-s for machine- or supervisor-level interrupts, respectively",
|
||||
},
|
||||
"riscv-interrupt-u" => AbiUnsupported::Reason {
|
||||
explain: "user-mode interrupt handlers have been removed from LLVM pending standardization, see: https://reviews.llvm.org/D149314",
|
||||
},
|
||||
|
||||
_ => AbiUnsupported::Unrecognized,
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
pub fn all_names() -> Vec<&'static str> {
|
||||
|
@ -200,6 +220,10 @@ pub fn is_stable(name: &str) -> Result<(), AbiDisabled> {
|
|||
feature: sym::abi_avr_interrupt,
|
||||
explain: "avr-interrupt and avr-non-blocking-interrupt ABIs are experimental and subject to change",
|
||||
}),
|
||||
"riscv-interrupt-m" | "riscv-interrupt-s" => Err(AbiDisabled::Unstable {
|
||||
feature: sym::abi_riscv_interrupt,
|
||||
explain: "riscv-interrupt ABIs are experimental and subject to change",
|
||||
}),
|
||||
"C-cmse-nonsecure-call" => Err(AbiDisabled::Unstable {
|
||||
feature: sym::abi_c_cmse_nonsecure_call,
|
||||
explain: "C-cmse-nonsecure-call ABI is experimental and subject to change",
|
||||
|
@ -260,6 +284,8 @@ impl Abi {
|
|||
PlatformIntrinsic => 32,
|
||||
Unadjusted => 33,
|
||||
RustCold => 34,
|
||||
RiscvInterruptM => 35,
|
||||
RiscvInterruptS => 36,
|
||||
};
|
||||
debug_assert!(
|
||||
AbiDatas
|
||||
|
|
|
@ -4,19 +4,19 @@ use super::*;
|
|||
#[test]
|
||||
fn lookup_Rust() {
|
||||
let abi = lookup("Rust");
|
||||
assert!(abi.is_some() && abi.unwrap().data().name == "Rust");
|
||||
assert!(abi.is_ok() && abi.unwrap().data().name == "Rust");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lookup_cdecl() {
|
||||
let abi = lookup("cdecl");
|
||||
assert!(abi.is_some() && abi.unwrap().data().name == "cdecl");
|
||||
assert!(abi.is_ok() && abi.unwrap().data().name == "cdecl");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lookup_baz() {
|
||||
let abi = lookup("baz");
|
||||
assert!(abi.is_none());
|
||||
assert!(matches!(abi, Err(AbiUnsupported::Unrecognized)))
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
@ -2270,6 +2270,7 @@ impl Target {
|
|||
PtxKernel => self.arch == "nvptx64",
|
||||
Msp430Interrupt => self.arch == "msp430",
|
||||
AmdGpuKernel => self.arch == "amdgcn",
|
||||
RiscvInterruptM | RiscvInterruptS => ["riscv32", "riscv64"].contains(&&self.arch[..]),
|
||||
AvrInterrupt | AvrNonBlockingInterrupt => self.arch == "avr",
|
||||
Wasm => ["wasm32", "wasm64"].contains(&&self.arch[..]),
|
||||
Thiscall { .. } => self.arch == "x86",
|
||||
|
@ -2701,7 +2702,7 @@ impl Target {
|
|||
let name = (stringify!($key_name)).replace("_", "-");
|
||||
obj.remove(&name).and_then(|o| o.as_str().and_then(|s| {
|
||||
match lookup_abi(s) {
|
||||
Some(abi) => base.$key_name = Some(abi),
|
||||
Ok(abi) => base.$key_name = Some(abi),
|
||||
_ => return Some(Err(format!("'{}' is not a valid value for abi", s))),
|
||||
}
|
||||
Some(Ok(()))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue