Skip to content

Commit fffc650

Browse files
authored
Rollup merge of #99436 - Nilstrieb:toggle-box-noalias, r=fee1-dead
Add flag to configure `noalias` on `Box<T>` The aliasing rules of `Box<T>` are still not decided, but currently, `Box<T>` is unique and gets `noalias`. To aid making an informed decision about the future of `Box<T>`, this PR adds a flag `-Zbox-noalias` to configure `noalias` for `Box<T>` (for example, for benchmarking). The same flag already exists for `&mut T` `noalias`, where it was added because it was the problem of various miscompilations in LLVM. For more information, see rust-lang/unsafe-code-guidelines#326
2 parents eecfdfb + 7c900c9 commit fffc650

File tree

6 files changed

+27
-2
lines changed

6 files changed

+27
-2
lines changed

compiler/rustc_interface/src/tests.rs

+1
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,7 @@ fn test_unstable_options_tracking_hash() {
718718
tracked!(asm_comments, true);
719719
tracked!(assume_incomplete_release, true);
720720
tracked!(binary_dep_depinfo, true);
721+
tracked!(box_noalias, Some(false));
721722
tracked!(
722723
branch_protection,
723724
Some(BranchProtection {

compiler/rustc_middle/src/ty/layout.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -3266,7 +3266,12 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
32663266
// this attribute doesn't make it UB for the pointed-to data to be undef.
32673267
attrs.set(ArgAttribute::NoUndef);
32683268

3269-
// `Box` pointer parameters never alias because ownership is transferred
3269+
// The aliasing rules for `Box<T>` are still not decided, but currently we emit
3270+
// `noalias` for it. This can be turned off using an unstable flag.
3271+
// See https://github.com/rust-lang/unsafe-code-guidelines/issues/326
3272+
let noalias_for_box =
3273+
self.tcx().sess.opts.unstable_opts.box_noalias.unwrap_or(true);
3274+
32703275
// `&mut` pointer parameters never alias other parameters,
32713276
// or mutable global data
32723277
//
@@ -3281,7 +3286,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
32813286
// `-Zmutable-noalias` debugging option.
32823287
let no_alias = match kind {
32833288
PointerKind::Shared | PointerKind::UniqueBorrowed => false,
3284-
PointerKind::UniqueOwned => true,
3289+
PointerKind::UniqueOwned => noalias_for_box,
32853290
PointerKind::Frozen => !is_return,
32863291
};
32873292
if no_alias {

compiler/rustc_session/src/options.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1209,6 +1209,8 @@ options! {
12091209
binary_dep_depinfo: bool = (false, parse_bool, [TRACKED],
12101210
"include artifacts (sysroot, crate dependencies) used during compilation in dep-info \
12111211
(default: no)"),
1212+
box_noalias: Option<bool> = (None, parse_opt_bool, [TRACKED],
1213+
"emit noalias metadata for box (default: yes)"),
12121214
branch_protection: Option<BranchProtection> = (None, parse_branch_protection, [TRACKED],
12131215
"set options for branch target identification and pointer authentication on AArch64"),
12141216
cf_protection: CFProtection = (CFProtection::None, parse_cfprotection, [TRACKED],

src/test/codegen/noalias-box-off.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// compile-flags: -O -Z box-noalias=no
2+
3+
#![crate_type = "lib"]
4+
5+
// CHECK-LABEL: @box_should_not_have_noalias_if_disabled(
6+
// CHECK-NOT: noalias
7+
#[no_mangle]
8+
pub fn box_should_not_have_noalias_if_disabled(_b: Box<u8>) {}

src/test/codegen/noalias-box.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// compile-flags: -O
2+
3+
#![crate_type = "lib"]
4+
5+
// CHECK-LABEL: @box_should_have_noalias_by_default(
6+
// CHECK: noalias
7+
#[no_mangle]
8+
pub fn box_should_have_noalias_by_default(_b: Box<u8>) {}

src/test/rustdoc-ui/z-help.stdout

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
-Z asm-comments=val -- generate comments into the assembly (may change behavior) (default: no)
55
-Z assert-incr-state=val -- assert that the incremental cache is in given state: either `loaded` or `not-loaded`.
66
-Z binary-dep-depinfo=val -- include artifacts (sysroot, crate dependencies) used during compilation in dep-info (default: no)
7+
-Z box-noalias=val -- emit noalias metadata for box (default: yes)
78
-Z branch-protection=val -- set options for branch target identification and pointer authentication on AArch64
89
-Z cf-protection=val -- instrument control-flow architecture protection
910
-Z cgu-partitioning-strategy=val -- the codegen unit partitioning strategy to use

0 commit comments

Comments
 (0)