Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d0297fa

Browse files
authoredMay 29, 2024
Unrolled build for rust-lang#124655
Rollup merge of rust-lang#124655 - Darksonn:fixed-x18, r=lqd,estebank Add `-Zfixed-x18` This PR is a follow-up to rust-lang#124323 that proposes a different implementation. Please read the description of that PR for motivation. See the equivalent flag in [the clang docs](https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-ffixed-x18). MCP: rust-lang/compiler-team#748 Fixes rust-lang#121970 r? rust-lang/compiler
2 parents e9b7aa0 + 4aafecb commit d0297fa

File tree

8 files changed

+100
-1
lines changed

8 files changed

+100
-1
lines changed
 

‎compiler/rustc_codegen_llvm/messages.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ codegen_llvm_error_creating_import_library =
1818
codegen_llvm_error_writing_def_file =
1919
Error writing .DEF file: {$error}
2020
21+
codegen_llvm_fixed_x18_invalid_arch = the `-Zfixed-x18` flag is not supported on the `{$arch}` architecture
22+
2123
codegen_llvm_from_llvm_diag = {$message}
2224
2325
codegen_llvm_from_llvm_optimization_diag = {$filename}:{$line}:{$column} {$pass_name} ({$kind}): {$message}

‎compiler/rustc_codegen_llvm/src/errors.rs

+6
Original file line numberDiff line numberDiff line change
@@ -254,3 +254,9 @@ pub struct MismatchedDataLayout<'a> {
254254
pub(crate) struct InvalidTargetFeaturePrefix<'a> {
255255
pub feature: &'a str,
256256
}
257+
258+
#[derive(Diagnostic)]
259+
#[diag(codegen_llvm_fixed_x18_invalid_arch)]
260+
pub(crate) struct FixedX18InvalidArch<'a> {
261+
pub arch: &'a str,
262+
}

‎compiler/rustc_codegen_llvm/src/llvm_util.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::back::write::create_informational_target_machine;
22
use crate::errors::{
3-
InvalidTargetFeaturePrefix, PossibleFeature, TargetFeatureDisableOrEnable,
3+
FixedX18InvalidArch, InvalidTargetFeaturePrefix, PossibleFeature, TargetFeatureDisableOrEnable,
44
UnknownCTargetFeature, UnknownCTargetFeaturePrefix, UnstableCTargetFeature,
55
};
66
use crate::llvm;
@@ -615,6 +615,15 @@ pub(crate) fn global_llvm_features(sess: &Session, diagnostics: bool) -> Vec<Str
615615
.flatten();
616616
features.extend(feats);
617617

618+
// -Zfixed-x18
619+
if sess.opts.unstable_opts.fixed_x18 {
620+
if sess.target.arch != "aarch64" {
621+
sess.dcx().emit_fatal(FixedX18InvalidArch { arch: &sess.target.arch });
622+
} else {
623+
features.push("+reserve-x18".into());
624+
}
625+
}
626+
618627
if diagnostics && let Some(f) = check_tied_features(sess, &featsmap) {
619628
sess.dcx().emit_err(TargetFeatureDisableOrEnable {
620629
features: f,

‎compiler/rustc_interface/src/tests.rs

+1
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,7 @@ fn test_unstable_options_tracking_hash() {
773773
tracked!(emit_thin_lto, false);
774774
tracked!(export_executable_symbols, true);
775775
tracked!(fewer_names, Some(true));
776+
tracked!(fixed_x18, true);
776777
tracked!(flatten_format_args, false);
777778
tracked!(force_unstable_if_unmarked, true);
778779
tracked!(fuel, Some(("abc".to_string(), 99)));

‎compiler/rustc_session/src/options.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1678,6 +1678,8 @@ options! {
16781678
fewer_names: Option<bool> = (None, parse_opt_bool, [TRACKED],
16791679
"reduce memory use by retaining fewer names within compilation artifacts (LLVM-IR) \
16801680
(default: no)"),
1681+
fixed_x18: bool = (false, parse_bool, [TRACKED],
1682+
"make the x18 register reserved on AArch64 (default: no)"),
16811683
flatten_format_args: bool = (true, parse_bool, [TRACKED],
16821684
"flatten nested format_args!() and literals into a simplified format_args!() call \
16831685
(default: yes)"),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# `fixed-x18`
2+
3+
This option prevents the compiler from using the x18 register. It is only
4+
supported on aarch64.
5+
6+
From the [ABI spec][arm-abi]:
7+
8+
> X18 is the platform register and is reserved for the use of platform ABIs.
9+
> This is an additional temporary register on platforms that don't assign a
10+
> special meaning to it.
11+
12+
This flag only has an effect when the x18 register would otherwise be considered
13+
a temporary register. When the flag is applied, x18 is always a reserved
14+
register.
15+
16+
This flag is intended for use with the shadow call stack sanitizer. Generally,
17+
when that sanitizer is enabled, the x18 register is used to store a pointer to
18+
the shadow stack. Enabling this flag prevents the compiler from overwriting the
19+
shadow stack pointer with temporary data, which is necessary for the sanitizer
20+
to work correctly.
21+
22+
Currently, the `-Zsanitizer=shadow-call-stack` flag is only supported on
23+
platforms that always treat x18 as a reserved register, and the `-Zfixed-x18`
24+
flag is not required to use the sanitizer on such platforms. However, the
25+
sanitizer may be supported on targets where this is not the case in the future.
26+
27+
It is undefined behavior for `-Zsanitizer=shadow-call-stack` code to call into
28+
code where x18 is a temporary register. On the other hand, when you are *not*
29+
using the shadow call stack sanitizer, compilation units compiled with and
30+
without the `-Zfixed-x18` flag are compatible with each other.
31+
32+
[arm-abi]: https://developer.arm.com/documentation/den0024/a/The-ABI-for-ARM-64-bit-Architecture/Register-use-in-the-AArch64-Procedure-Call-Standard/Parameters-in-general-purpose-registers

‎tests/codegen/fixed-x18.rs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Test that the `reserve-x18` target feature is (not) emitted when
2+
// the `-Zfixed-x18` flag is (not) set.
3+
4+
//@ revisions: unset set
5+
//@ needs-llvm-components: aarch64
6+
//@ compile-flags: --target aarch64-unknown-none
7+
//@ [set] compile-flags: -Zfixed-x18
8+
9+
#![crate_type = "lib"]
10+
#![feature(no_core, lang_items)]
11+
#![no_core]
12+
13+
#[lang = "sized"]
14+
trait Sized {}
15+
16+
#[no_mangle]
17+
pub fn foo() {
18+
// CHECK: @foo() unnamed_addr #0
19+
20+
// unset-NOT: attributes #0 = { {{.*}}"target-features"="{{[^"]*}}+reserve-x18{{.*}} }
21+
// set: attributes #0 = { {{.*}}"target-features"="{{[^"]*}}+reserve-x18{{.*}} }
22+
}

‎tests/ui/abi/fixed_x18.rs

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// This tests that -Zfixed-x18 causes a compilation failure on targets other than aarch64.
2+
// Behavior on aarch64 is tested by tests/codegen/fixed-x18.rs.
3+
//
4+
//@ revisions: x64 i686 arm riscv32 riscv64
5+
//@ error-pattern: the `-Zfixed-x18` flag is not supported
6+
//@ dont-check-compiler-stderr
7+
//
8+
//@ compile-flags: -Zfixed-x18
9+
//@ [x64] needs-llvm-components: x86
10+
//@ [x64] compile-flags: --target=x86_64-unknown-linux-gnu --crate-type=rlib
11+
//@ [i686] needs-llvm-components: x86
12+
//@ [i686] compile-flags: --target=i686-unknown-linux-gnu --crate-type=rlib
13+
//@ [arm] needs-llvm-components: arm
14+
//@ [arm] compile-flags: --target=armv7-unknown-linux-gnueabihf --crate-type=rlib
15+
//@ [riscv32] needs-llvm-components: riscv
16+
//@ [riscv32] compile-flags: --target=riscv32i-unknown-none-elf --crate-type=rlib
17+
//@ [riscv64] needs-llvm-components: riscv
18+
//@ [riscv64] compile-flags: --target=riscv64gc-unknown-none-elf --crate-type=rlib
19+
20+
#![crate_type = "lib"]
21+
#![feature(no_core, lang_items)]
22+
#![no_core]
23+
24+
#[lang = "sized"]
25+
trait Sized {}

0 commit comments

Comments
 (0)
Failed to load comments.