From 3cdfdf95718ae329c5c1dc166b3becdd76369f5d Mon Sep 17 00:00:00 2001 From: Leonard Chan Date: Wed, 9 Sep 2026 23:07:03 +0000 Subject: [PATCH] fuchsia: dynamically enable fix-cortex-a53-835769 for generic CPUs Previously, `+fix-cortex-a53-835769` was statically included in `base.features` for `aarch64-unknown-fuchsia`. This caused the Cortex-A53 erratum workaround to be unconditionally enabled regardless of the selected target CPU (e.g., when passing `-C target-cpu=cortex-a73`). The behavior we want is to have it enabled by default only for the generic CPU (armv8-a), and it should be togglable via the normal `-Ctarget-features=` flag. This matches the target-feature logic in clang. AI: Note gemini was used to help verify this does match Clang's behavior and write out the matrix of different test invocations. I reviewed this code to the best of my ability before submitting. --- compiler/rustc_codegen_ssa/src/target_features.rs | 14 +++++++++++++- .../src/spec/targets/aarch64_unknown_fuchsia.rs | 2 +- .../aarch64-fuchsia-target-features.rs | 13 ++++++++++++- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/target_features.rs b/compiler/rustc_codegen_ssa/src/target_features.rs index 7dad0cc1732dd..606c817034560 100644 --- a/compiler/rustc_codegen_ssa/src/target_features.rs +++ b/compiler/rustc_codegen_ssa/src/target_features.rs @@ -10,7 +10,7 @@ use rustc_middle::ty::TyCtxt; use rustc_session::Session; use rustc_session::diagnostics::feature_err; use rustc_span::{Span, Symbol, edit_distance, sym}; -use rustc_target::spec::{Arch, SanitizerSet}; +use rustc_target::spec::{Arch, Os, SanitizerSet}; use rustc_target::target_features::{RUSTC_SPECIFIC_FEATURES, Stability}; use smallvec::SmallVec; @@ -451,6 +451,18 @@ pub fn target_spec_to_backend_features<'a>( extend_backend_features("ptx70", true); } + // When targetting aarch64 Fuchsia, the +fix-cortex-a53-835769 should be applied by default + // whenever using the generic (armv8-a) CPU. This matches Clang's behavior for aarch64 Fuchsia. + if sess.target.os == Os::Fuchsia + && sess.target.arch == Arch::AArch64 + && matches!( + sess.opts.cg.target_cpu.as_deref().unwrap_or(&sess.target.cpu), + "generic" | "cortex-a53" + ) + { + extend_backend_features("fix-cortex-a53-835769", true); + } + // Compute implied features parse_rust_feature_list( sess, diff --git a/compiler/rustc_target/src/spec/targets/aarch64_unknown_fuchsia.rs b/compiler/rustc_target/src/spec/targets/aarch64_unknown_fuchsia.rs index 8dede888055f7..d16110cfd66f9 100644 --- a/compiler/rustc_target/src/spec/targets/aarch64_unknown_fuchsia.rs +++ b/compiler/rustc_target/src/spec/targets/aarch64_unknown_fuchsia.rs @@ -5,7 +5,7 @@ use crate::spec::{ pub(crate) fn target() -> Target { let mut base = base::fuchsia::opts(); base.cpu = "generic".into(); - base.features = "+v8a,+crc,+aes,+sha2,+neon,+fix-cortex-a53-835769".into(); + base.features = "+v8a,+crc,+aes,+sha2,+neon".into(); base.max_atomic_width = Some(128); base.stack_probes = StackProbeType::Inline; base.supported_sanitizers = SanitizerSet::ADDRESS diff --git a/tests/codegen-llvm/aarch64-fuchsia-target-features.rs b/tests/codegen-llvm/aarch64-fuchsia-target-features.rs index 32cdc2ce8a553..c9d1a4da4f569 100644 --- a/tests/codegen-llvm/aarch64-fuchsia-target-features.rs +++ b/tests/codegen-llvm/aarch64-fuchsia-target-features.rs @@ -1,8 +1,19 @@ //@ add-minicore +//@ revisions: DEFAULT GENERIC A53 A73 A73_ENABLE DEFAULT_DISABLE //@ compile-flags: --crate-type=rlib --target=aarch64-unknown-fuchsia //@ needs-llvm-components: aarch64 +//@ [GENERIC] compile-flags: -C target-cpu=generic +//@ [A53] compile-flags: -C target-cpu=cortex-a53 +//@ [A73] compile-flags: -C target-cpu=cortex-a73 +//@ [A73_ENABLE] compile-flags: -C target-cpu=cortex-a73 -C target-feature=+fix-cortex-a53-835769 +//@ [DEFAULT_DISABLE] compile-flags: -C target-feature=-fix-cortex-a53-835769 -// CHECK: attributes #0 = { {{.*}}"target-features"="{{.*}}+fix-cortex-a53-835769{{.*}}" } +// DEFAULT: attributes #0 = { {{.*}}"target-features"="{{.*}}+fix-cortex-a53-835769{{.*}}" } +// GENERIC: attributes #0 = { {{.*}}"target-features"="{{.*}}+fix-cortex-a53-835769{{.*}}" } +// A53: attributes #0 = { {{.*}}"target-features"="{{.*}}+fix-cortex-a53-835769{{.*}}" } +// A73-NOT: fix-cortex-a53-835769 +// A73_ENABLE: attributes #0 = { {{.*}}"target-features"="{{.*}}+fix-cortex-a53-835769{{.*}}" } +// DEFAULT_DISABLE: attributes #0 = { {{.*}}"target-features"="{{.*}}-fix-cortex-a53-835769" } #![feature(no_core, lang_items)] #![no_core]