Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanup: handle -Zmutable-noalias like -Zbox-noalias #106474

Merged
merged 1 commit into from
Jan 6, 2023
Merged
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
10 changes: 0 additions & 10 deletions compiler/rustc_codegen_llvm/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)];

Expand Down Expand Up @@ -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.
Expand Down
7 changes: 1 addition & 6 deletions compiler/rustc_target/src/abi/call/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Expand Down
19 changes: 7 additions & 12 deletions compiler/rustc_ty_utils/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,22 +256,21 @@ 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
//
// `&T` where `T` contains no `UnsafeCell<U>` is immutable,
// 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,
};
Expand All @@ -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);
}
}
}
}
Expand Down
23 changes: 23 additions & 0 deletions src/test/codegen/noalias-flag.rs
Original file line number Diff line number Diff line change
@@ -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
}