Skip to content

Commit 6ac229c

Browse files
committed
Don't compute optimized PointerKind for unoptimized builds
This saves us both the Freeze/Unpin queries, and avoids placing noalias attributes, which have a compile-time impact on LLVM even in optnone builds (due to always_inline functions).
1 parent 39ed643 commit 6ac229c

File tree

4 files changed

+27
-20
lines changed

4 files changed

+27
-20
lines changed

compiler/rustc_middle/src/ty/layout.rs

+24-17
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_hir as hir;
1111
use rustc_hir::lang_items::LangItem;
1212
use rustc_index::bit_set::BitSet;
1313
use rustc_index::vec::{Idx, IndexVec};
14-
use rustc_session::{DataTypeKind, FieldInfo, SizeKind, VariantInfo};
14+
use rustc_session::{config::OptLevel, DataTypeKind, FieldInfo, SizeKind, VariantInfo};
1515
use rustc_span::symbol::{Ident, Symbol};
1616
use rustc_span::DUMMY_SP;
1717
use rustc_target::abi::call::{
@@ -2318,23 +2318,30 @@ where
23182318
ty::Ref(_, ty, mt) if offset.bytes() == 0 => {
23192319
let address_space = addr_space_of_ty(ty);
23202320
let tcx = cx.tcx();
2321-
let kind = match mt {
2322-
hir::Mutability::Not => {
2323-
if ty.is_freeze(tcx.at(DUMMY_SP), cx.param_env()) {
2324-
PointerKind::Frozen
2325-
} else {
2326-
PointerKind::Shared
2321+
let kind = if tcx.sess.opts.optimize == OptLevel::No {
2322+
// Use conservative pointer kind if not optimizing. This saves us the
2323+
// Freeze/Unpin queries, and can save time in the codegen backend (noalias
2324+
// attributes in LLVM have compile-time cost even in unoptimized builds).
2325+
PointerKind::Shared
2326+
} else {
2327+
match mt {
2328+
hir::Mutability::Not => {
2329+
if ty.is_freeze(tcx.at(DUMMY_SP), cx.param_env()) {
2330+
PointerKind::Frozen
2331+
} else {
2332+
PointerKind::Shared
2333+
}
23272334
}
2328-
}
2329-
hir::Mutability::Mut => {
2330-
// References to self-referential structures should not be considered
2331-
// noalias, as another pointer to the structure can be obtained, that
2332-
// is not based-on the original reference. We consider all !Unpin
2333-
// types to be potentially self-referential here.
2334-
if ty.is_unpin(tcx.at(DUMMY_SP), cx.param_env()) {
2335-
PointerKind::UniqueBorrowed
2336-
} else {
2337-
PointerKind::Shared
2335+
hir::Mutability::Mut => {
2336+
// References to self-referential structures should not be considered
2337+
// noalias, as another pointer to the structure can be obtained, that
2338+
// is not based-on the original reference. We consider all !Unpin
2339+
// types to be potentially self-referential here.
2340+
if ty.is_unpin(tcx.at(DUMMY_SP), cx.param_env()) {
2341+
PointerKind::UniqueBorrowed
2342+
} else {
2343+
PointerKind::Shared
2344+
}
23382345
}
23392346
}
23402347
};

src/test/codegen/function-arguments.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// compile-flags: -C no-prepopulate-passes
1+
// compile-flags: -O -C no-prepopulate-passes
22
// ignore-tidy-linelength
33
// min-system-llvm-version: 12.0
44

src/test/codegen/noalias-unpin.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// compile-flags: -Z mutable-noalias=yes
1+
// compile-flags: -O -Z mutable-noalias=yes
22

33
#![crate_type = "lib"]
44

src/test/codegen/packed.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// ignore-tidy-linelength
2-
// compile-flags: -C no-prepopulate-passes
2+
// compile-flags: -O -C no-prepopulate-passes
33

44
#![crate_type = "lib"]
55

0 commit comments

Comments
 (0)