Skip to content

Commit

Permalink
Emit noalias on &mut parameters by default
Browse files Browse the repository at this point in the history
This used to be disabled due to LLVM bugs in the handling of
noalias information in conjunction with unwinding. However,
according to #31681 all known LLVM bugs have been fixed by
LLVM 6.0, so it's probably time to reenable this optimization.

Noalias annotations will not be emitted by default if either
-C panic=abort (as previously) or LLVM >= 6.0 (new).

-Z mutable-noalias=no is left as an escape-hatch to allow
debugging problems suspected to stem from this change.
  • Loading branch information
nikic committed May 17, 2018
1 parent 90463a6 commit 1230813
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/librustc/session/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1239,8 +1239,8 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
"print the result of the monomorphization collection pass"),
mir_opt_level: usize = (1, parse_uint, [TRACKED],
"set the MIR optimization level (0-3, default: 1)"),
mutable_noalias: bool = (false, parse_bool, [TRACKED],
"emit noalias metadata for mutable references"),
mutable_noalias: Option<bool> = (None, parse_opt_bool, [TRACKED],
"emit noalias metadata for mutable references (default: yes on LLVM >= 6)"),
arg_align_attributes: bool = (false, parse_bool, [TRACKED],
"emit align metadata for reference arguments"),
dump_mir: Option<String> = (None, parse_opt_string, [UNTRACKED],
Expand Down
10 changes: 8 additions & 2 deletions src/librustc_codegen_llvm/type_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use abi::{FnType, FnTypeExt};
use common::*;
use llvm;
use rustc::hir;
use rustc::ty::{self, Ty, TypeFoldable};
use rustc::ty::layout::{self, Align, LayoutOf, Size, TyLayout};
Expand Down Expand Up @@ -428,8 +429,13 @@ impl<'tcx> LayoutLlvmExt<'tcx> for TyLayout<'tcx> {
PointerKind::Shared
},
hir::MutMutable => {
if cx.tcx.sess.opts.debugging_opts.mutable_noalias ||
cx.tcx.sess.panic_strategy() == PanicStrategy::Abort {
// Only emit noalias annotations for LLVM >= 6 or in panic=abort
// mode, as prior versions had many bugs in conjunction with
// unwinding. See also issue #31681.
let mutable_noalias = cx.tcx.sess.opts.debugging_opts.mutable_noalias
.unwrap_or(unsafe { llvm::LLVMRustVersionMajor() >= 6 }
|| cx.tcx.sess.panic_strategy() == PanicStrategy::Abort);
if mutable_noalias {
PointerKind::UniqueBorrowed
} else {
PointerKind::Shared
Expand Down
10 changes: 4 additions & 6 deletions src/test/codegen/function-arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

// compile-flags: -C no-prepopulate-passes
// ignore-tidy-linelength
// min-llvm-version 6.0

#![crate_type = "lib"]
#![feature(custom_attribute)]
Expand Down Expand Up @@ -52,16 +53,14 @@ pub fn named_borrow<'r>(_: &'r i32) {
pub fn unsafe_borrow(_: &UnsafeInner) {
}

// CHECK: @mutable_unsafe_borrow(i16* dereferenceable(2) %arg0)
// CHECK: @mutable_unsafe_borrow(i16* noalias dereferenceable(2) %arg0)
// ... unless this is a mutable borrow, those never alias
// ... except that there's this LLVM bug that forces us to not use noalias, see #29485
#[no_mangle]
pub fn mutable_unsafe_borrow(_: &mut UnsafeInner) {
}

// CHECK: @mutable_borrow(i32* dereferenceable(4) %arg0)
// CHECK: @mutable_borrow(i32* noalias dereferenceable(4) %arg0)
// FIXME #25759 This should also have `nocapture`
// ... there's this LLVM bug that forces us to not use noalias, see #29485
#[no_mangle]
pub fn mutable_borrow(_: &mut i32) {
}
Expand Down Expand Up @@ -103,9 +102,8 @@ pub fn helper(_: usize) {
pub fn slice(_: &[u8]) {
}

// CHECK: @mutable_slice([0 x i8]* nonnull %arg0.0, [[USIZE]] %arg0.1)
// CHECK: @mutable_slice([0 x i8]* noalias nonnull %arg0.0, [[USIZE]] %arg0.1)
// FIXME #25759 This should also have `nocapture`
// ... there's this LLVM bug that forces us to not use noalias, see #29485
#[no_mangle]
pub fn mutable_slice(_: &mut [u8]) {
}
Expand Down

0 comments on commit 1230813

Please sign in to comment.