Skip to content

Commit 89ae19e

Browse files
Rollup merge of rust-lang#133422 - taiki-e:riscv-e-clobber-abi, r=Amanieu
Fix clobber_abi in RV32E and RV64E inline assembly Currently clobber_abi in RV32E and RV64E inline assembly is implemented using InlineAsmClobberAbi::RiscV, but broken since x16-x31 cannot be used in RV32E and RV64E. ``` error: cannot use register `x16`: register can't be used with the `e` target feature --> <source>:42:14 | 42 | asm!("", clobber_abi("C"), options(nostack, nomem, preserves_flags)); | ^^^^^^^^^^^^^^^^ error: cannot use register `x17`: register can't be used with the `e` target feature --> <source>:42:14 | 42 | asm!("", clobber_abi("C"), options(nostack, nomem, preserves_flags)); | ^^^^^^^^^^^^^^^^ error: cannot use register `x28`: register can't be used with the `e` target feature --> <source>:42:14 | 42 | asm!("", clobber_abi("C"), options(nostack, nomem, preserves_flags)); | ^^^^^^^^^^^^^^^^ error: cannot use register `x29`: register can't be used with the `e` target feature --> <source>:42:14 | 42 | asm!("", clobber_abi("C"), options(nostack, nomem, preserves_flags)); | ^^^^^^^^^^^^^^^^ error: cannot use register `x30`: register can't be used with the `e` target feature --> <source>:42:14 | 42 | asm!("", clobber_abi("C"), options(nostack, nomem, preserves_flags)); | ^^^^^^^^^^^^^^^^ error: cannot use register `x31`: register can't be used with the `e` target feature --> <source>:42:14 | 42 | asm!("", clobber_abi("C"), options(nostack, nomem, preserves_flags)); | ^^^^^^^^^^^^^^^^ ``` r? `@Amanieu` `@rustbot` label O-riscv +A-inline-assembly
2 parents bda2851 + 736c397 commit 89ae19e

File tree

5 files changed

+95
-6
lines changed

5 files changed

+95
-6
lines changed

compiler/rustc_ast_lowering/src/asm.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
8282
let mut clobber_abis = FxIndexMap::default();
8383
if let Some(asm_arch) = asm_arch {
8484
for (abi_name, abi_span) in &asm.clobber_abis {
85-
match asm::InlineAsmClobberAbi::parse(asm_arch, &self.tcx.sess.target, *abi_name) {
85+
match asm::InlineAsmClobberAbi::parse(
86+
asm_arch,
87+
&self.tcx.sess.target,
88+
&self.tcx.sess.unstable_target_features,
89+
*abi_name,
90+
) {
8691
Ok(abi) => {
8792
// If the abi was already in the list, emit an error
8893
match clobber_abis.get(&abi) {

compiler/rustc_codegen_cranelift/src/inline_asm.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -476,9 +476,14 @@ impl<'tcx> InlineAssemblyGenerator<'_, 'tcx> {
476476
let mut new_slot = |x| new_slot_fn(&mut slot_size, x);
477477

478478
// Allocate stack slots for saving clobbered registers
479-
let abi_clobber = InlineAsmClobberAbi::parse(self.arch, &self.tcx.sess.target, sym::C)
480-
.unwrap()
481-
.clobbered_regs();
479+
let abi_clobber = InlineAsmClobberAbi::parse(
480+
self.arch,
481+
&self.tcx.sess.target,
482+
&self.tcx.sess.unstable_target_features,
483+
sym::C,
484+
)
485+
.unwrap()
486+
.clobbered_regs();
482487
for (i, reg) in self.registers.iter().enumerate().filter_map(|(i, r)| r.map(|r| (i, r))) {
483488
let mut need_save = true;
484489
// If the register overlaps with a register clobbered by function call, then

compiler/rustc_target/src/asm/mod.rs

+32-1
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,7 @@ pub enum InlineAsmClobberAbi {
929929
AArch64NoX18,
930930
Arm64EC,
931931
RiscV,
932+
RiscVE,
932933
LoongArch,
933934
PowerPC,
934935
S390x,
@@ -941,6 +942,7 @@ impl InlineAsmClobberAbi {
941942
pub fn parse(
942943
arch: InlineAsmArch,
943944
target: &Target,
945+
target_features: &FxIndexSet<Symbol>,
944946
name: Symbol,
945947
) -> Result<Self, &'static [&'static str]> {
946948
let name = name.as_str();
@@ -975,7 +977,11 @@ impl InlineAsmClobberAbi {
975977
_ => Err(&["C", "system"]),
976978
},
977979
InlineAsmArch::RiscV32 | InlineAsmArch::RiscV64 => match name {
978-
"C" | "system" | "efiapi" => Ok(InlineAsmClobberAbi::RiscV),
980+
"C" | "system" | "efiapi" => Ok(if riscv::is_e(target_features) {
981+
InlineAsmClobberAbi::RiscVE
982+
} else {
983+
InlineAsmClobberAbi::RiscV
984+
}),
979985
_ => Err(&["C", "system", "efiapi"]),
980986
},
981987
InlineAsmArch::LoongArch64 => match name {
@@ -1148,6 +1154,31 @@ impl InlineAsmClobberAbi {
11481154
v24, v25, v26, v27, v28, v29, v30, v31,
11491155
}
11501156
},
1157+
InlineAsmClobberAbi::RiscVE => clobbered_regs! {
1158+
RiscV RiscVInlineAsmReg {
1159+
// Refs:
1160+
// - ILP32E https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/draft-20240829-13bfa9f54634cb60d86b9b333e109f077805b4b3/riscv-cc.adoc#ilp32e-calling-convention
1161+
// - LP64E https://github.com/riscv-non-isa/riscv-elf-psabi-doc/pull/299
1162+
1163+
// ra
1164+
x1,
1165+
// t0-t2
1166+
x5, x6, x7,
1167+
// a0-a5
1168+
x10, x11, x12, x13, x14, x15,
1169+
// ft0-ft7
1170+
f0, f1, f2, f3, f4, f5, f6, f7,
1171+
// fa0-fa7
1172+
f10, f11, f12, f13, f14, f15, f16, f17,
1173+
// ft8-ft11
1174+
f28, f29, f30, f31,
1175+
1176+
v0, v1, v2, v3, v4, v5, v6, v7,
1177+
v8, v9, v10, v11, v12, v13, v14, v15,
1178+
v16, v17, v18, v19, v20, v21, v22, v23,
1179+
v24, v25, v26, v27, v28, v29, v30, v31,
1180+
}
1181+
},
11511182
InlineAsmClobberAbi::LoongArch => clobbered_regs! {
11521183
LoongArch LoongArchInlineAsmReg {
11531184
// ra

compiler/rustc_target/src/asm/riscv.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,18 @@ impl RiscVInlineAsmRegClass {
5454
}
5555
}
5656

57+
pub(crate) fn is_e(target_features: &FxIndexSet<Symbol>) -> bool {
58+
target_features.contains(&sym::e)
59+
}
60+
5761
fn not_e(
5862
_arch: InlineAsmArch,
5963
_reloc_model: RelocModel,
6064
target_features: &FxIndexSet<Symbol>,
6165
_target: &Target,
6266
_is_clobber: bool,
6367
) -> Result<(), &'static str> {
64-
if target_features.contains(&sym::e) {
68+
if is_e(target_features) {
6569
Err("register can't be used with the `e` target feature")
6670
} else {
6771
Ok(())

tests/codegen/asm/riscv-clobbers.rs

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//@ assembly-output: emit-asm
2+
//@ revisions: rv32i rv64i rv32e
3+
//@[rv32i] compile-flags: --target riscv32i-unknown-none-elf
4+
//@[rv32i] needs-llvm-components: riscv
5+
//@[rv64i] compile-flags: --target riscv64imac-unknown-none-elf
6+
//@[rv64i] needs-llvm-components: riscv
7+
//@[rv32e] compile-flags: --target riscv32e-unknown-none-elf
8+
//@[rv32e] needs-llvm-components: riscv
9+
// ignore-tidy-linelength
10+
11+
#![crate_type = "rlib"]
12+
#![feature(no_core, rustc_attrs, lang_items)]
13+
#![no_core]
14+
15+
#[lang = "sized"]
16+
trait Sized {}
17+
18+
#[rustc_builtin_macro]
19+
macro_rules! asm {
20+
() => {};
21+
}
22+
23+
// CHECK-LABEL: @flags_clobber
24+
// CHECK: call void asm sideeffect "", "~{vtype},~{vl},~{vxsat},~{vxrm}"()
25+
#[no_mangle]
26+
pub unsafe fn flags_clobber() {
27+
asm!("", options(nostack, nomem));
28+
}
29+
30+
// CHECK-LABEL: @no_clobber
31+
// CHECK: call void asm sideeffect "", ""()
32+
#[no_mangle]
33+
pub unsafe fn no_clobber() {
34+
asm!("", options(nostack, nomem, preserves_flags));
35+
}
36+
37+
// CHECK-LABEL: @clobber_abi
38+
// rv32i: asm sideeffect "", "={x1},={x5},={x6},={x7},={x10},={x11},={x12},={x13},={x14},={x15},={x16},={x17},={x28},={x29},={x30},={x31},~{f0},~{f1},~{f2},~{f3},~{f4},~{f5},~{f6},~{f7},~{f10},~{f11},~{f12},~{f13},~{f14},~{f15},~{f16},~{f17},~{f28},~{f29},~{f30},~{f31},~{v0},~{v1},~{v2},~{v3},~{v4},~{v5},~{v6},~{v7},~{v8},~{v9},~{v10},~{v11},~{v12},~{v13},~{v14},~{v15},~{v16},~{v17},~{v18},~{v19},~{v20},~{v21},~{v22},~{v23},~{v24},~{v25},~{v26},~{v27},~{v28},~{v29},~{v30},~{v31}"()
39+
// rv64i: asm sideeffect "", "={x1},={x5},={x6},={x7},={x10},={x11},={x12},={x13},={x14},={x15},={x16},={x17},={x28},={x29},={x30},={x31},~{f0},~{f1},~{f2},~{f3},~{f4},~{f5},~{f6},~{f7},~{f10},~{f11},~{f12},~{f13},~{f14},~{f15},~{f16},~{f17},~{f28},~{f29},~{f30},~{f31},~{v0},~{v1},~{v2},~{v3},~{v4},~{v5},~{v6},~{v7},~{v8},~{v9},~{v10},~{v11},~{v12},~{v13},~{v14},~{v15},~{v16},~{v17},~{v18},~{v19},~{v20},~{v21},~{v22},~{v23},~{v24},~{v25},~{v26},~{v27},~{v28},~{v29},~{v30},~{v31}"()
40+
// rv32e: asm sideeffect "", "={x1},={x5},={x6},={x7},={x10},={x11},={x12},={x13},={x14},={x15},~{f0},~{f1},~{f2},~{f3},~{f4},~{f5},~{f6},~{f7},~{f10},~{f11},~{f12},~{f13},~{f14},~{f15},~{f16},~{f17},~{f28},~{f29},~{f30},~{f31},~{v0},~{v1},~{v2},~{v3},~{v4},~{v5},~{v6},~{v7},~{v8},~{v9},~{v10},~{v11},~{v12},~{v13},~{v14},~{v15},~{v16},~{v17},~{v18},~{v19},~{v20},~{v21},~{v22},~{v23},~{v24},~{v25},~{v26},~{v27},~{v28},~{v29},~{v30},~{v31}"()
41+
#[no_mangle]
42+
pub unsafe fn clobber_abi() {
43+
asm!("", clobber_abi("C"), options(nostack, nomem, preserves_flags));
44+
}

0 commit comments

Comments
 (0)