Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_ast_lowering/src/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
// operands list.
let mut clobbered = FxHashSet::default();
for (abi, (_, abi_span)) in clobber_abis {
for &clobber in abi.clobbered_regs() {
for clobber in abi.clobbered_regs(&self.tcx.sess.target) {
// Don't emit a clobber for a register already clobbered
if clobbered.contains(&clobber) {
continue;
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_cranelift/src/inline_asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ impl<'tcx> InlineAssemblyGenerator<'_, 'tcx> {
sym::C,
)
.unwrap()
.clobbered_regs();
.clobbered_regs(&self.tcx.sess.target);
for (i, reg) in self.registers.iter().enumerate().filter_map(|(i, r)| r.map(|r| (i, r))) {
let mut need_save = true;
// If the register overlaps with a register clobbered by function call, then
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_codegen_llvm/src/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,7 @@ fn reg_to_llvm(reg: InlineAsmRegOrRegClass, layout: Option<&TyAndLayout<'_>>) ->
) => unreachable!("clobber-only"),
Xtensa(XtensaInlineAsmRegClass::freg) => "f",
Xtensa(XtensaInlineAsmRegClass::reg) => "r",
Xtensa(XtensaInlineAsmRegClass::qreg) => "q",
Xtensa(XtensaInlineAsmRegClass::sreg | XtensaInlineAsmRegClass::breg) => {
unreachable!("clobber-only")
}
Expand Down Expand Up @@ -1106,6 +1107,7 @@ fn dummy_output_type<'ll>(cx: &CodegenCx<'ll, '_>, reg: InlineAsmRegClass) -> &'
) => unreachable!("clobber-only"),
Xtensa(XtensaInlineAsmRegClass::reg) => cx.type_i32(),
Xtensa(XtensaInlineAsmRegClass::freg) => cx.type_f32(),
Xtensa(XtensaInlineAsmRegClass::qreg) => cx.type_vector(cx.type_i8(), 16),
Xtensa(XtensaInlineAsmRegClass::sreg | XtensaInlineAsmRegClass::breg) => {
unreachable!("clobber-only")
}
Expand Down
22 changes: 19 additions & 3 deletions compiler/rustc_target/src/asm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1121,19 +1121,19 @@ impl InlineAsmClobberAbi {
}

/// Returns the set of registers which are clobbered by this ABI.
pub fn clobbered_regs(self) -> &'static [InlineAsmReg] {
pub fn clobbered_regs(self, target: &crate::spec::Target) -> Vec<InlineAsmReg> {
macro_rules! clobbered_regs {
($arch:ident $arch_reg:ident {
$(
$reg:ident,
)*
}) => {
&[
vec![
$(InlineAsmReg::$arch($arch_reg::$reg),)*
]
};
}
match self {
let mut regs = match self {
InlineAsmClobberAbi::X86 => clobbered_regs! {
X86 X86InlineAsmReg {
ax, cx, dx,
Expand Down Expand Up @@ -1461,6 +1461,22 @@ impl InlineAsmClobberAbi {
b8, b9, b10, b11, b12, b13, b14, b15,
}
},
};

// QR registers are part of the ESP32-S3's volatile register set, but
// do not exist on other Xtensa CPUs.
if self == InlineAsmClobberAbi::Xtensa && target.options.cpu == "esp32s3" {
regs.extend([
InlineAsmReg::Xtensa(XtensaInlineAsmReg::q0),
InlineAsmReg::Xtensa(XtensaInlineAsmReg::q1),
InlineAsmReg::Xtensa(XtensaInlineAsmReg::q2),
InlineAsmReg::Xtensa(XtensaInlineAsmReg::q3),
InlineAsmReg::Xtensa(XtensaInlineAsmReg::q4),
InlineAsmReg::Xtensa(XtensaInlineAsmReg::q5),
InlineAsmReg::Xtensa(XtensaInlineAsmReg::q6),
InlineAsmReg::Xtensa(XtensaInlineAsmReg::q7),
]);
}
regs
}
}
24 changes: 24 additions & 0 deletions compiler/rustc_target/src/asm/xtensa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def_reg_class! {
Xtensa XtensaInlineAsmRegClass {
reg,
freg,
qreg,
sreg,
breg,
}
Expand Down Expand Up @@ -44,6 +45,7 @@ impl XtensaInlineAsmRegClass {
Self::reg => types! { _: I8, I16, I32; },
Self::freg => types! { fp: F32; },
Self::sreg | Self::breg => &[],
Self::qreg => types! { _: VecI8(16), VecI16(8), VecI32(4); },
}
}
}
Expand All @@ -62,6 +64,20 @@ fn has_fp(
}
}

fn has_esp32s3(
_arch: InlineAsmArch,
_reloc_model: RelocModel,
_target_features: &FxIndexSet<Symbol>,
target: &Target,
_is_clobber: bool,
) -> Result<(), &'static str> {
if target.options.cpu == "esp32s3" {
Ok(())
} else {
Err("QR registers are only available on the ESP32-S3")
}
}

// The frame pointer is a7 under the windowed ABI and a15 otherwise. Always
// reject it as an inline asm operand: LLVM can force its use for variable-
// sized stack frames (e.g. via cross-language LTO with C).
Expand Down Expand Up @@ -181,6 +197,14 @@ def_regs! {
f13: freg = ["f13"] % has_fp,
f14: freg = ["f14"] % has_fp,
f15: freg = ["f15"] % has_fp,
q0: qreg = ["q0"] % has_esp32s3,
q1: qreg = ["q1"] % has_esp32s3,
q2: qreg = ["q2"] % has_esp32s3,
q3: qreg = ["q3"] % has_esp32s3,
q4: qreg = ["q4"] % has_esp32s3,
q5: qreg = ["q5"] % has_esp32s3,
q6: qreg = ["q6"] % has_esp32s3,
q7: qreg = ["q7"] % has_esp32s3,
// Clobber-only special registers (caller-saved per ISA §8.1.7, except
// LITBASE); needed for `clobber_abi`.
sar: sreg = ["sar"],
Expand Down
88 changes: 88 additions & 0 deletions tests/assembly-llvm/asm/xtensa-qreg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
//@ add-minicore
//@ assembly-output: emit-asm
//@ compile-flags: --target xtensa-esp32s3-none-elf -Ctarget-feature=-windowed -Zmerge-functions=disabled
//@ min-llvm-version: 23
//@ needs-llvm-components: xtensa

#![feature(no_core, asm_experimental_arch, repr_simd)]
#![crate_type = "rlib"]
#![no_core]

extern crate minicore;
use minicore::simd::*;
use minicore::*;

#[allow(non_camel_case_types)]
#[repr(simd)]
struct u8x16([u8; 16]);
impl Copy for u8x16 {}

// CHECK-LABEL: qr_add:
// CHECK: #APP
// CHECK: ee.vadds.s8 q{{[0-7]}}, q{{[0-7]}}, q{{[0-7]}}
// CHECK: #NO_APP
#[no_mangle]
pub unsafe fn qr_add() {
let mut a: i8x16;
let b: i8x16;
asm!("ee.zero.q {a}", a = out(qreg) a);
asm!("ee.zero.q {b}", b = out(qreg) b);
asm!("ee.vadds.s8 {dst}, {dst}, {rhs}", dst = inout(qreg) a, rhs = in(qreg) b);
}

// Exercise the custom `#[repr(simd)]` type shape from issue #265. Keep the
// vectors local so this test does not depend on the currently unsupported
// Xtensa vector function ABI.
// CHECK-LABEL: qr_custom_simd:
// CHECK: #APP
// CHECK: ee.vadds.s8 q{{[0-7]}}, q{{[0-7]}}, q{{[0-7]}}
// CHECK: #NO_APP
#[no_mangle]
pub unsafe fn qr_custom_simd() {
let mut a: u8x16;
let b: u8x16;
asm!("ee.zero.q {a}", a = out(qreg) a);
asm!("ee.zero.q {b}", b = out(qreg) b);
asm!("ee.vadds.s8 {dst}, {dst}, {rhs}", dst = inout(qreg) a, rhs = in(qreg) b);
}

// Constant array initializers must be lowered through a constant-pool load
// instead of reaching instruction selection as an unsupported BUILD_VECTOR.
// CHECK-LABEL: qr_custom_simd_literal:
// CHECK: ee.vadds.s8 q{{[0-7]}}, q{{[0-7]}}, q{{[0-7]}}
#[no_mangle]
pub unsafe fn qr_custom_simd_literal() {
let mut a = u8x16([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]);
let b: u8x16;
asm!("ee.zero.q {b}", b = out(qreg) b);
asm!("ee.vadds.s8 {dst}, {dst}, {rhs}", dst = inout(qreg) a, rhs = in(qreg) b);
}

// The ISA exposes a 32-bit word from a QR register; EXTUI then selects a byte.
// Exercise the raw instruction sequence for reading a 32-bit word and byte.
// CHECK-LABEL: qr_custom_simd_lane0:
// CHECK: ee.movi.32.a q{{[0-7]}}, a{{[0-9]+}}, 0
// CHECK: extui a{{[0-9]+}}, a{{[0-9]+}}, 0, 8
#[no_mangle]
pub unsafe fn qr_custom_simd_lane0() -> u8 {
let q: u8x16;
asm!("ee.zero.q {0}", out(qreg) q);
let word: u32;
let byte: u32;
asm!(
"ee.movi.32.a {q}, {word}, 0",
"extui {byte}, {word}, 0, 8",
q = in(qreg) q,
word = out(reg) word,
byte = out(reg) byte,
);
byte as u8
}

// The ESP32-S3 QR registers are caller-saved and must be part of the C ABI
// clobber set on this target.
// CHECK-LABEL: qr_clobber_abi:
#[no_mangle]
pub unsafe fn qr_clobber_abi() {
asm!("", clobber_abi("C"));
}