Skip to content

Commit 85b9750

Browse files
committed
Fix handling of x18 in AArch64 inline assembly on ohos/trusty or with -Zfixed-x18
1 parent 7db7489 commit 85b9750

File tree

9 files changed

+93
-51
lines changed

9 files changed

+93
-51
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_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1611,6 +1611,7 @@ symbols! {
16111611
repr_simd,
16121612
repr_transparent,
16131613
require,
1614+
reserve_x18: "reserve-x18",
16141615
residual,
16151616
result,
16161617
result_ffi_guarantees,

compiler/rustc_target/src/asm/aarch64.rs

+13-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::fmt;
22

33
use rustc_data_structures::fx::FxIndexSet;
4-
use rustc_span::Symbol;
4+
use rustc_span::{Symbol, sym};
55

66
use super::{InlineAsmArch, InlineAsmType, ModifierInfo};
77
use crate::spec::{RelocModel, Target};
@@ -71,18 +71,26 @@ impl AArch64InlineAsmRegClass {
7171
}
7272
}
7373

74-
pub(crate) fn target_reserves_x18(target: &Target) -> bool {
75-
target.os == "android" || target.os == "fuchsia" || target.is_like_osx || target.is_like_windows
74+
pub(crate) fn target_reserves_x18(target: &Target, target_features: &FxIndexSet<Symbol>) -> bool {
75+
// See isX18ReservedByDefault in LLVM for targets reserve x18 by default:
76+
// https://github.com/llvm/llvm-project/blob/llvmorg-19.1.0/llvm/lib/TargetParser/AArch64TargetParser.cpp#L102-L105
77+
// Note that +reserve-x18 is currently not set for the above targets.
78+
target.os == "android"
79+
|| target.os == "fuchsia"
80+
|| target.env == "ohos"
81+
|| target.is_like_osx
82+
|| target.is_like_windows
83+
|| target_features.contains(&sym::reserve_x18)
7684
}
7785

7886
fn reserved_x18(
7987
_arch: InlineAsmArch,
8088
_reloc_model: RelocModel,
81-
_target_features: &FxIndexSet<Symbol>,
89+
target_features: &FxIndexSet<Symbol>,
8290
target: &Target,
8391
_is_clobber: bool,
8492
) -> Result<(), &'static str> {
85-
if target_reserves_x18(target) {
93+
if target_reserves_x18(target, target_features) {
8694
Err("x18 is a reserved register on this target")
8795
} else {
8896
Ok(())

compiler/rustc_target/src/asm/mod.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -941,6 +941,7 @@ impl InlineAsmClobberAbi {
941941
pub fn parse(
942942
arch: InlineAsmArch,
943943
target: &Target,
944+
target_features: &FxIndexSet<Symbol>,
944945
name: Symbol,
945946
) -> Result<Self, &'static [&'static str]> {
946947
let name = name.as_str();
@@ -963,11 +964,13 @@ impl InlineAsmClobberAbi {
963964
_ => Err(&["C", "system", "efiapi", "aapcs"]),
964965
},
965966
InlineAsmArch::AArch64 => match name {
966-
"C" | "system" | "efiapi" => Ok(if aarch64::target_reserves_x18(target) {
967-
InlineAsmClobberAbi::AArch64NoX18
968-
} else {
969-
InlineAsmClobberAbi::AArch64
970-
}),
967+
"C" | "system" | "efiapi" => {
968+
Ok(if aarch64::target_reserves_x18(target, target_features) {
969+
InlineAsmClobberAbi::AArch64NoX18
970+
} else {
971+
InlineAsmClobberAbi::AArch64
972+
})
973+
}
971974
_ => Err(&["C", "system", "efiapi"]),
972975
},
973976
InlineAsmArch::Arm64EC => match name {

compiler/rustc_target/src/spec/targets/aarch64_unknown_linux_ohos.rs

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ pub(crate) fn target() -> Target {
1616
data_layout: "e-m:e-p270:32:32-p271:32:32-p272:64:64-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128-Fn32".into(),
1717
arch: "aarch64".into(),
1818
options: TargetOptions {
19-
features: "+reserve-x18".into(),
2019
mcount: "\u{1}_mcount".into(),
2120
stack_probes: StackProbeType::Inline,
2221
supported_sanitizers: SanitizerSet::ADDRESS

compiler/rustc_target/src/target_features.rs

+6
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,12 @@ const AARCH64_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
226226
("rcpc2", Stable, &["rcpc"]),
227227
// FEAT_LRCPC3
228228
("rcpc3", Unstable(sym::aarch64_unstable_target_feature), &["rcpc2"]),
229+
// This is needed for inline assembly, but shouldn't be stabilized as-is
230+
// since it should be enabled globally using -Zfixed-x18, not
231+
// #[target_feature].
232+
// Note that cfg(target_feature = "reserve-x18") is currently not set for
233+
// targets that reserve x18 by default.
234+
("reserve-x18", Unstable(sym::aarch64_unstable_target_feature), &[]),
229235
// FEAT_RDM
230236
("rdm", Stable, &["neon"]),
231237
// FEAT_SB

tests/codegen/asm/aarch64-clobbers.rs

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//@ revisions: aarch64 aarch64_fixed_x18 aarch64_no_x18 aarch64_reserve_x18 arm64ec
2+
//@[aarch64] compile-flags: --target aarch64-unknown-linux-gnu
3+
//@[aarch64] needs-llvm-components: aarch64
4+
//@[aarch64_fixed_x18] compile-flags: --target aarch64-unknown-linux-gnu -Zfixed-x18
5+
//@[aarch64_fixed_x18] needs-llvm-components: aarch64
6+
//@[aarch64_no_x18] compile-flags: --target aarch64-pc-windows-msvc
7+
//@[aarch64_no_x18] needs-llvm-components: aarch64
8+
// aarch64-unknown-trusty uses aarch64-unknown-unknown-musl which doesn't
9+
// reserve x18 by default as llvm_target, and pass +reserve-x18 in target-spec.
10+
//@[aarch64_reserve_x18] compile-flags: --target aarch64-unknown-trusty
11+
//@[aarch64_reserve_x18] needs-llvm-components: aarch64
12+
//@[arm64ec] compile-flags: --target arm64ec-pc-windows-msvc
13+
//@[arm64ec] needs-llvm-components: aarch64
14+
// ignore-tidy-linelength
15+
16+
#![crate_type = "rlib"]
17+
#![feature(no_core, rustc_attrs, lang_items)]
18+
#![no_core]
19+
20+
#[lang = "sized"]
21+
trait Sized {}
22+
23+
#[rustc_builtin_macro]
24+
macro_rules! asm {
25+
() => {};
26+
}
27+
28+
// CHECK-LABEL: @cc_clobber
29+
// CHECK: call void asm sideeffect "", "~{cc}"()
30+
#[no_mangle]
31+
pub unsafe fn cc_clobber() {
32+
asm!("", options(nostack, nomem));
33+
}
34+
35+
// CHECK-LABEL: @no_clobber
36+
// CHECK: call void asm sideeffect "", ""()
37+
#[no_mangle]
38+
pub unsafe fn no_clobber() {
39+
asm!("", options(nostack, nomem, preserves_flags));
40+
}
41+
42+
// CHECK-LABEL: @clobber_abi
43+
// aarch64: asm sideeffect "", "={w0},={w1},={w2},={w3},={w4},={w5},={w6},={w7},={w8},={w9},={w10},={w11},={w12},={w13},={w14},={w15},={w16},={w17},={w18},={w30},={q0},={q1},={q2},={q3},={q4},={q5},={q6},={q7},={q8},={q9},={q10},={q11},={q12},={q13},={q14},={q15},={q16},={q17},={q18},={q19},={q20},={q21},={q22},={q23},={q24},={q25},={q26},={q27},={q28},={q29},={q30},={q31},~{p0},~{p1},~{p2},~{p3},~{p4},~{p5},~{p6},~{p7},~{p8},~{p9},~{p10},~{p11},~{p12},~{p13},~{p14},~{p15},~{ffr}"()
44+
// aarch64_fixed_x18: asm sideeffect "", "={w0},={w1},={w2},={w3},={w4},={w5},={w6},={w7},={w8},={w9},={w10},={w11},={w12},={w13},={w14},={w15},={w16},={w17},={w30},={q0},={q1},={q2},={q3},={q4},={q5},={q6},={q7},={q8},={q9},={q10},={q11},={q12},={q13},={q14},={q15},={q16},={q17},={q18},={q19},={q20},={q21},={q22},={q23},={q24},={q25},={q26},={q27},={q28},={q29},={q30},={q31},~{p0},~{p1},~{p2},~{p3},~{p4},~{p5},~{p6},~{p7},~{p8},~{p9},~{p10},~{p11},~{p12},~{p13},~{p14},~{p15},~{ffr}"()
45+
// aarch64_no_x18: asm sideeffect "", "={w0},={w1},={w2},={w3},={w4},={w5},={w6},={w7},={w8},={w9},={w10},={w11},={w12},={w13},={w14},={w15},={w16},={w17},={w30},={q0},={q1},={q2},={q3},={q4},={q5},={q6},={q7},={q8},={q9},={q10},={q11},={q12},={q13},={q14},={q15},={q16},={q17},={q18},={q19},={q20},={q21},={q22},={q23},={q24},={q25},={q26},={q27},={q28},={q29},={q30},={q31},~{p0},~{p1},~{p2},~{p3},~{p4},~{p5},~{p6},~{p7},~{p8},~{p9},~{p10},~{p11},~{p12},~{p13},~{p14},~{p15},~{ffr}"()
46+
// aarch64_reserve_x18: asm sideeffect "", "={w0},={w1},={w2},={w3},={w4},={w5},={w6},={w7},={w8},={w9},={w10},={w11},={w12},={w13},={w14},={w15},={w16},={w17},={w30},={q0},={q1},={q2},={q3},={q4},={q5},={q6},={q7},={q8},={q9},={q10},={q11},={q12},={q13},={q14},={q15},={q16},={q17},={q18},={q19},={q20},={q21},={q22},={q23},={q24},={q25},={q26},={q27},={q28},={q29},={q30},={q31},~{p0},~{p1},~{p2},~{p3},~{p4},~{p5},~{p6},~{p7},~{p8},~{p9},~{p10},~{p11},~{p12},~{p13},~{p14},~{p15},~{ffr}"()
47+
// arm64ec: asm sideeffect "", "={w0},={w1},={w2},={w3},={w4},={w5},={w6},={w7},={w8},={w9},={w10},={w11},={w12},={w15},={w16},={w17},={w30},={q0},={q1},={q2},={q3},={q4},={q5},={q6},={q7},={q8},={q9},={q10},={q11},={q12},={q13},={q14},={q15}"()
48+
#[no_mangle]
49+
pub unsafe fn clobber_abi() {
50+
asm!("", clobber_abi("C"), options(nostack, nomem, preserves_flags));
51+
}

tests/codegen/asm/arm64ec-clobbers.rs

-36
This file was deleted.

0 commit comments

Comments
 (0)