From d165a6d70844020e4958ded4f80e30d64dd0bac1 Mon Sep 17 00:00:00 2001 From: Erik Desjardins Date: Wed, 4 Jan 2023 19:24:42 -0500 Subject: [PATCH] cleanup: handle -Zmutable-noalias like -Zbox-noalias --- compiler/rustc_codegen_llvm/src/abi.rs | 10 ---------- compiler/rustc_target/src/abi/call/mod.rs | 7 +------ compiler/rustc_ty_utils/src/abi.rs | 19 +++++++------------ src/test/codegen/noalias-flag.rs | 23 +++++++++++++++++++++++ 4 files changed, 31 insertions(+), 28 deletions(-) create mode 100644 src/test/codegen/noalias-flag.rs diff --git a/compiler/rustc_codegen_llvm/src/abi.rs b/compiler/rustc_codegen_llvm/src/abi.rs index a6fd2a7de6bd0..546540dfd7623 100644 --- a/compiler/rustc_codegen_llvm/src/abi.rs +++ b/compiler/rustc_codegen_llvm/src/abi.rs @@ -34,13 +34,6 @@ pub trait ArgAttributesExt { ); } -fn should_use_mutable_noalias(cx: &CodegenCx<'_, '_>) -> bool { - // LLVM prior to version 12 had known miscompiles in the presence of - // noalias attributes (see #54878), but we don't support earlier - // versions at all anymore. We now enable mutable noalias by default. - cx.tcx.sess.opts.unstable_opts.mutable_noalias.unwrap_or(true) -} - const ABI_AFFECTING_ATTRIBUTES: [(ArgAttribute, llvm::AttributeKind); 1] = [(ArgAttribute::InReg, llvm::AttributeKind::InReg)]; @@ -88,9 +81,6 @@ fn get_attrs<'ll>(this: &ArgAttributes, cx: &CodegenCx<'ll, '_>) -> SmallVec<[&' attrs.push(llattr.create_attr(cx.llcx)); } } - if regular.contains(ArgAttribute::NoAliasMutRef) && should_use_mutable_noalias(cx) { - attrs.push(llvm::AttributeKind::NoAlias.create_attr(cx.llcx)); - } } else if cx.tcx.sess.opts.unstable_opts.sanitizer.contains(SanitizerSet::MEMORY) { // If we're not optimising, *but* memory sanitizer is on, emit noundef, since it affects // memory sanitizer's behavior. diff --git a/compiler/rustc_target/src/abi/call/mod.rs b/compiler/rustc_target/src/abi/call/mod.rs index a5ffaebea0b98..3de46a967f23a 100644 --- a/compiler/rustc_target/src/abi/call/mod.rs +++ b/compiler/rustc_target/src/abi/call/mod.rs @@ -71,12 +71,7 @@ mod attr_impl { const NonNull = 1 << 3; const ReadOnly = 1 << 4; const InReg = 1 << 5; - // Due to past miscompiles in LLVM, we use a separate attribute for - // &mut arguments, so that the codegen backend can decide whether - // or not to actually emit the attribute. It can also be controlled - // with the `-Zmutable-noalias` debugging option. - const NoAliasMutRef = 1 << 6; - const NoUndef = 1 << 7; + const NoUndef = 1 << 6; } } } diff --git a/compiler/rustc_ty_utils/src/abi.rs b/compiler/rustc_ty_utils/src/abi.rs index 73d2d278f93f3..f8a5691f29de0 100644 --- a/compiler/rustc_ty_utils/src/abi.rs +++ b/compiler/rustc_ty_utils/src/abi.rs @@ -256,6 +256,11 @@ fn adjust_for_rust_scalar<'tcx>( // See https://github.com/rust-lang/unsafe-code-guidelines/issues/326 let noalias_for_box = cx.tcx.sess.opts.unstable_opts.box_noalias.unwrap_or(true); + // LLVM prior to version 12 had known miscompiles in the presence of noalias attributes + // (see #54878), so it was conditionally disabled, but we don't support earlier + // versions at all anymore. We still support turning it off using -Zmutable-noalias. + let noalias_mut_ref = cx.tcx.sess.opts.unstable_opts.mutable_noalias.unwrap_or(true); + // `&mut` pointer parameters never alias other parameters, // or mutable global data // @@ -263,15 +268,9 @@ fn adjust_for_rust_scalar<'tcx>( // and can be marked as both `readonly` and `noalias`, as // LLVM's definition of `noalias` is based solely on memory // dependencies rather than pointer equality - // - // Due to past miscompiles in LLVM, we apply a separate NoAliasMutRef attribute - // for UniqueBorrowed arguments, so that the codegen backend can decide whether - // or not to actually emit the attribute. It can also be controlled with the - // `-Zmutable-noalias` debugging option. let no_alias = match kind { - PointerKind::SharedMutable - | PointerKind::UniqueBorrowed - | PointerKind::UniqueBorrowedPinned => false, + PointerKind::SharedMutable | PointerKind::UniqueBorrowedPinned => false, + PointerKind::UniqueBorrowed => noalias_mut_ref, PointerKind::UniqueOwned => noalias_for_box, PointerKind::Frozen => true, }; @@ -284,10 +283,6 @@ fn adjust_for_rust_scalar<'tcx>( if kind == PointerKind::Frozen && !is_return { attrs.set(ArgAttribute::ReadOnly); } - - if kind == PointerKind::UniqueBorrowed && !is_return { - attrs.set(ArgAttribute::NoAliasMutRef); - } } } } diff --git a/src/test/codegen/noalias-flag.rs b/src/test/codegen/noalias-flag.rs new file mode 100644 index 0000000000000..a9ec61e286d0d --- /dev/null +++ b/src/test/codegen/noalias-flag.rs @@ -0,0 +1,23 @@ +// compile-flags: -O -Zmutable-noalias=no + +#![crate_type = "lib"] + +// `-Zmutable-noalias=no` should disable noalias on mut refs... + +// CHECK-LABEL: @test_mut_ref( +// CHECK-NOT: noalias +// CHECK-SAME: %x +#[no_mangle] +pub fn test_mut_ref(x: &mut i32) -> &mut i32 { + x +} + +// ...but not on shared refs + +// CHECK-LABEL: @test_ref( +// CHECK-SAME: noalias +// CHECK-SAME: %x +#[no_mangle] +pub fn test_ref(x: &i32) -> &i32 { + x +}