Skip to content

Commit afe8c45

Browse files
committed
Auto merge of #106474 - erikdesjardins:noalias, r=bjorn3
cleanup: handle -Zmutable-noalias like -Zbox-noalias r? `@bjorn3` cc `@RalfJung` this will conflict with #106180
2 parents 7bbbaab + d165a6d commit afe8c45

File tree

4 files changed

+31
-28
lines changed

4 files changed

+31
-28
lines changed

compiler/rustc_codegen_llvm/src/abi.rs

-10
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,6 @@ pub trait ArgAttributesExt {
3434
);
3535
}
3636

37-
fn should_use_mutable_noalias(cx: &CodegenCx<'_, '_>) -> bool {
38-
// LLVM prior to version 12 had known miscompiles in the presence of
39-
// noalias attributes (see #54878), but we don't support earlier
40-
// versions at all anymore. We now enable mutable noalias by default.
41-
cx.tcx.sess.opts.unstable_opts.mutable_noalias.unwrap_or(true)
42-
}
43-
4437
const ABI_AFFECTING_ATTRIBUTES: [(ArgAttribute, llvm::AttributeKind); 1] =
4538
[(ArgAttribute::InReg, llvm::AttributeKind::InReg)];
4639

@@ -88,9 +81,6 @@ fn get_attrs<'ll>(this: &ArgAttributes, cx: &CodegenCx<'ll, '_>) -> SmallVec<[&'
8881
attrs.push(llattr.create_attr(cx.llcx));
8982
}
9083
}
91-
if regular.contains(ArgAttribute::NoAliasMutRef) && should_use_mutable_noalias(cx) {
92-
attrs.push(llvm::AttributeKind::NoAlias.create_attr(cx.llcx));
93-
}
9484
} else if cx.tcx.sess.opts.unstable_opts.sanitizer.contains(SanitizerSet::MEMORY) {
9585
// If we're not optimising, *but* memory sanitizer is on, emit noundef, since it affects
9686
// memory sanitizer's behavior.

compiler/rustc_target/src/abi/call/mod.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,7 @@ mod attr_impl {
7171
const NonNull = 1 << 3;
7272
const ReadOnly = 1 << 4;
7373
const InReg = 1 << 5;
74-
// Due to past miscompiles in LLVM, we use a separate attribute for
75-
// &mut arguments, so that the codegen backend can decide whether
76-
// or not to actually emit the attribute. It can also be controlled
77-
// with the `-Zmutable-noalias` debugging option.
78-
const NoAliasMutRef = 1 << 6;
79-
const NoUndef = 1 << 7;
74+
const NoUndef = 1 << 6;
8075
}
8176
}
8277
}

compiler/rustc_ty_utils/src/abi.rs

+7-12
Original file line numberDiff line numberDiff line change
@@ -256,22 +256,21 @@ fn adjust_for_rust_scalar<'tcx>(
256256
// See https://github.com/rust-lang/unsafe-code-guidelines/issues/326
257257
let noalias_for_box = cx.tcx.sess.opts.unstable_opts.box_noalias.unwrap_or(true);
258258

259+
// LLVM prior to version 12 had known miscompiles in the presence of noalias attributes
260+
// (see #54878), so it was conditionally disabled, but we don't support earlier
261+
// versions at all anymore. We still support turning it off using -Zmutable-noalias.
262+
let noalias_mut_ref = cx.tcx.sess.opts.unstable_opts.mutable_noalias.unwrap_or(true);
263+
259264
// `&mut` pointer parameters never alias other parameters,
260265
// or mutable global data
261266
//
262267
// `&T` where `T` contains no `UnsafeCell<U>` is immutable,
263268
// and can be marked as both `readonly` and `noalias`, as
264269
// LLVM's definition of `noalias` is based solely on memory
265270
// dependencies rather than pointer equality
266-
//
267-
// Due to past miscompiles in LLVM, we apply a separate NoAliasMutRef attribute
268-
// for UniqueBorrowed arguments, so that the codegen backend can decide whether
269-
// or not to actually emit the attribute. It can also be controlled with the
270-
// `-Zmutable-noalias` debugging option.
271271
let no_alias = match kind {
272-
PointerKind::SharedMutable
273-
| PointerKind::UniqueBorrowed
274-
| PointerKind::UniqueBorrowedPinned => false,
272+
PointerKind::SharedMutable | PointerKind::UniqueBorrowedPinned => false,
273+
PointerKind::UniqueBorrowed => noalias_mut_ref,
275274
PointerKind::UniqueOwned => noalias_for_box,
276275
PointerKind::Frozen => true,
277276
};
@@ -284,10 +283,6 @@ fn adjust_for_rust_scalar<'tcx>(
284283
if kind == PointerKind::Frozen && !is_return {
285284
attrs.set(ArgAttribute::ReadOnly);
286285
}
287-
288-
if kind == PointerKind::UniqueBorrowed && !is_return {
289-
attrs.set(ArgAttribute::NoAliasMutRef);
290-
}
291286
}
292287
}
293288
}

src/test/codegen/noalias-flag.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// compile-flags: -O -Zmutable-noalias=no
2+
3+
#![crate_type = "lib"]
4+
5+
// `-Zmutable-noalias=no` should disable noalias on mut refs...
6+
7+
// CHECK-LABEL: @test_mut_ref(
8+
// CHECK-NOT: noalias
9+
// CHECK-SAME: %x
10+
#[no_mangle]
11+
pub fn test_mut_ref(x: &mut i32) -> &mut i32 {
12+
x
13+
}
14+
15+
// ...but not on shared refs
16+
17+
// CHECK-LABEL: @test_ref(
18+
// CHECK-SAME: noalias
19+
// CHECK-SAME: %x
20+
#[no_mangle]
21+
pub fn test_ref(x: &i32) -> &i32 {
22+
x
23+
}

0 commit comments

Comments
 (0)