Skip to content

Commit 7c9a410

Browse files
committed
Skip post-mono opts on mono bodies
1 parent 8ac6827 commit 7c9a410

File tree

9 files changed

+221
-184
lines changed

9 files changed

+221
-184
lines changed

compiler/rustc_mir_transform/src/lib.rs

+12-23
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ use rustc_hir as hir;
2525
use rustc_hir::def::{CtorKind, DefKind};
2626
use rustc_hir::def_id::LocalDefId;
2727
use rustc_index::IndexVec;
28+
use rustc_middle::mir::visit::Visitor;
2829
use rustc_middle::mir::{
2930
AnalysisPhase, Body, CallSource, ClearCrossCrate, ConstOperand, ConstQualifs, LocalDecl,
30-
MirPhase, Operand, Place, ProjectionElem, Promoted, RuntimePhase, Rvalue, START_BLOCK,
31-
SourceInfo, Statement, StatementKind, TerminatorKind,
31+
Location, MirPhase, NullOp, Operand, Place, ProjectionElem, Promoted, RuntimePhase, Rvalue,
32+
START_BLOCK, SourceInfo, Statement, StatementKind, TerminatorKind,
3233
};
3334
use rustc_middle::ty::{self, Instance, TyCtxt, TypeVisitableExt};
3435
use rustc_middle::util::Providers;
@@ -793,49 +794,37 @@ fn inner_optimized_mir(tcx: TyCtxt<'_>, did: LocalDefId) -> Body<'_> {
793794

794795
run_optimization_passes(tcx, &mut body);
795796

796-
let mut vis = MonoCompatVisitor { contains_alias: false, contains_ubcheck: false };
797+
let mut vis = MonoCompatVisitor { contains_ubcheck: false };
797798
vis.visit_body(&body);
798799

799800
// If the MIR is already monomorphic, we can transform it to codegen MIR right away.
800801
if !tcx.generics_of(did).requires_monomorphization(tcx)
801802
&& !vis.contains_ubcheck
802-
&& !vis.contains_alias
803+
&& !body.has_aliases()
803804
{
804805
let instance = Instance::mono(tcx, did.into());
805806
body = instance.instantiate_mir_and_normalize_erasing_regions(
806807
tcx,
807808
ty::TypingEnv::fully_monomorphized(),
808809
ty::EarlyBinder::bind(body),
809810
);
810-
transform_to_codegen_mir(tcx, &mut body);
811+
812+
// Monomoprhizing this body didn't reveal any new information that is useful for
813+
// optimizations, so we just run passes that make the MIR ready for codegen backends.
814+
pm::run_passes_no_validate(tcx, &mut body, &[&add_call_guards::CriticalCallEdges], None);
811815
}
812816

813817
body
814818
}
815819

816-
use rustc_middle::mir::visit::{TyContext, Visitor};
817-
use rustc_middle::mir::{Location, NullOp};
818-
use rustc_middle::ty::{Ty, TypeFlags};
819-
820820
// FIXME: This visitor looks for properties of MIR that would forbid using optimized MIR as codegen
821821
// MIR.
822-
// Currently, it looks for NullOp::UbChecks because that must be resolved at codegen, and also for
823-
// type projections because those are the only way I've found to realize that we are generating MIR
824-
// for a body with an unsatisafiable predicate. Such bodies cannot be normalized, so we must not
825-
// try to create codegen MIR for them.
822+
// Currently, it looks for NullOp::UbChecks because that must be resolved at codegen.
826823
struct MonoCompatVisitor {
827-
contains_alias: bool,
828824
contains_ubcheck: bool,
829825
}
830826

831827
impl<'tcx> Visitor<'tcx> for MonoCompatVisitor {
832-
fn visit_ty(&mut self, ty: Ty<'tcx>, _: TyContext) {
833-
debug!("{:?} {:?}", ty, ty.flags());
834-
if ty.has_aliases() || ty.flags().contains(TypeFlags::HAS_TY_PROJECTION) {
835-
self.contains_alias = true;
836-
}
837-
}
838-
839828
fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, location: Location) {
840829
if let Rvalue::NullaryOp(NullOp::UbChecks, _) = rvalue {
841830
self.contains_ubcheck = true;
@@ -847,13 +836,13 @@ impl<'tcx> Visitor<'tcx> for MonoCompatVisitor {
847836
pub fn build_codegen_mir<'tcx>(tcx: TyCtxt<'tcx>, instance: Instance<'tcx>) -> &'tcx Body<'tcx> {
848837
let body = tcx.instance_mir(instance.def);
849838

850-
let mut vis = MonoCompatVisitor { contains_alias: false, contains_ubcheck: false };
839+
let mut vis = MonoCompatVisitor { contains_ubcheck: false };
851840
vis.visit_body(&body);
852841

853842
// MIR for monomorphic defs has already been fully optimized in optimized_mir.
854843
let body = if instance.args.non_erasable_generics().next().is_some()
855844
|| vis.contains_ubcheck
856-
|| vis.contains_alias
845+
|| body.has_aliases()
857846
{
858847
let mut body = instance.instantiate_mir_and_normalize_erasing_regions(
859848
tcx,

tests/codegen/issues/issue-122600-ptr-discriminant-update.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
#![crate_type = "lib"]
44

55
// The bug here was that it was loading and storing the whole value.
6-
// It's ok to load the discriminant so that we preserve the UB from
7-
// `unreachable_unchecked`, but it must only store the constant discriminant of `B`.
6+
// It's ok for it to load the discriminant,
7+
// to preserve the UB from `unreachable_unchecked`,
8+
// but it better only store the constant discriminant of `B`.
89

910
pub enum State {
1011
A([u8; 753]),
@@ -22,7 +23,7 @@ pub unsafe fn update(s: *mut State) {
2223
// CHECK-NOT: 75{{3|4}}
2324

2425
// CHECK: %[[TAG:.+]] = load i8, ptr %s, align 1
25-
// CHECK-NEXT: and i8 %[[TAG]], 1
26+
// CHECK-NEXT: trunc nuw i8 %[[TAG]] to i1
2627

2728
// CHECK-NOT: load
2829
// CHECK-NOT: store

tests/codegen/sanitizer/kcfi/emit-type-metadata-trait-objects.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ pub fn foo5(a: &dyn Trait5<Type5, 32>) {
136136
let b = &[Type5; 32];
137137
a.quux(&b);
138138
// CHECK-LABEL: define{{.*}}4foo5{{.*}}!{{<unknown kind #36>|kcfi_type}} !{{[0-9]+}}
139-
// CHECK: call void %{{[0-9]}}(ptr align 1 {{%[a-z](\.0)*|%_[0-9]+]}}, ptr align 1 {{%[a-z](\.0)*|%[0-9]+}}){{.*}}[ "kcfi"(i32 [[TYPE5:[[:print:]]+]]) ]
139+
// CHECK: call void %{{[0-9]}}(ptr align 1 {{%[a-z](\.0)*|%_[0-9]+]}}, ptr align 1 {{%[a-z](\.0)*|%_[0-9]+}}){{.*}}[ "kcfi"(i32 [[TYPE5:[[:print:]]+]]) ]
140140
}
141141

142142
pub fn bar5() {
@@ -145,7 +145,7 @@ pub fn bar5() {
145145
let b = &a as &dyn Trait5<Type5, 32>;
146146
b.quux(&a);
147147
// CHECK-LABEL: define{{.*}}4bar5{{.*}}!{{<unknown kind #36>|kcfi_type}} !{{[0-9]+}}
148-
// CHECK: call void %{{[0-9]}}(ptr align 1 {{%[a-z](\.0)*|%_[0-9]+]}}, ptr align 1 {{%[a-z](\.0)*|%[0-9]+}}){{.*}}[ "kcfi"(i32 [[TYPE5:[[:print:]]+]]) ]
148+
// CHECK: call void %{{[0-9]}}(ptr align 1 {{%[a-z](\.0)*|%_[0-9]+]}}, ptr align 1 {{%[a-z](\.0)*|%_[0-9]+}}){{.*}}[ "kcfi"(i32 [[TYPE5:[[:print:]]+]]) ]
149149
}
150150

151151
// CHECK: !{{[0-9]+}} = !{i32 [[TYPE1]]}

tests/coverage/issue-84561.cov-map

+12-11
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,20 @@ Number of file 0 mappings: 1
5959
Highest counter ID seen: c0
6060

6161
Function name: issue_84561::test3
62-
Raw bytes (277): 0x[01, 01, 09, 0d, 11, 0d, 15, 0d, 19, 1d, 21, 25, 29, 25, 29, 25, 29, 25, 29, 25, 2d, 33, 01, 08, 01, 03, 0f, 05, 04, 09, 01, 0f, 09, 02, 05, 04, 0f, 09, 05, 05, 00, 0f, 09, 01, 05, 00, 0f, 09, 01, 09, 01, 0f, 0d, 02, 05, 00, 0f, 0d, 01, 05, 00, 0f, 00, 00, 20, 00, 30, 0d, 01, 05, 03, 0f, 00, 03, 20, 00, 30, 00, 00, 33, 00, 41, 00, 00, 4b, 00, 5a, 0d, 01, 05, 00, 0f, 00, 05, 09, 03, 10, 00, 05, 0d, 00, 1b, 00, 02, 0d, 00, 1c, 0d, 04, 09, 02, 0f, 0d, 06, 05, 00, 0f, 0d, 04, 05, 00, 0f, 0d, 04, 09, 01, 0f, 0d, 05, 08, 00, 0f, 11, 01, 09, 00, 13, 02, 05, 09, 00, 13, 0d, 05, 08, 00, 0f, 15, 01, 09, 00, 13, 00, 03, 0d, 00, 1d, 06, 03, 09, 00, 13, 00, 03, 0d, 00, 1d, 0d, 03, 05, 00, 0f, 0d, 01, 0c, 00, 13, 19, 01, 0d, 00, 13, 0a, 02, 0d, 00, 13, 1d, 04, 05, 02, 13, 21, 03, 0d, 00, 13, 0e, 02, 0d, 00, 13, 25, 03, 05, 00, 0f, 25, 01, 0c, 00, 13, 29, 01, 0d, 00, 17, 29, 04, 0d, 00, 13, 1e, 02, 0d, 00, 17, 1e, 01, 14, 00, 1b, 00, 01, 15, 00, 1b, 1e, 02, 15, 00, 1b, 1e, 04, 0d, 00, 13, 22, 03, 09, 00, 19, 2d, 02, 05, 00, 0f, 2d, 03, 09, 00, 22, 00, 02, 05, 00, 0f, 00, 03, 09, 00, 2c, 00, 02, 01, 00, 02]
62+
Raw bytes (279): 0x[01, 01, 0a, 0d, 11, 0d, 15, 0d, 19, 1d, 21, 29, 2d, 25, 29, 25, 29, 25, 29, 27, 31, 29, 2d, 33, 01, 08, 01, 03, 0f, 05, 04, 09, 01, 0f, 09, 02, 05, 04, 0f, 09, 05, 05, 00, 0f, 09, 01, 05, 00, 0f, 09, 01, 09, 01, 0f, 0d, 02, 05, 00, 0f, 0d, 01, 05, 00, 0f, 00, 00, 20, 00, 30, 0d, 01, 05, 03, 0f, 00, 03, 20, 00, 30, 00, 00, 33, 00, 41, 00, 00, 4b, 00, 5a, 0d, 01, 05, 00, 0f, 00, 05, 09, 03, 10, 00, 05, 0d, 00, 1b, 00, 02, 0d, 00, 1c, 0d, 04, 09, 02, 0f, 0d, 06, 05, 00, 0f, 0d, 04, 05, 00, 0f, 0d, 04, 09, 01, 0f, 0d, 05, 08, 00, 0f, 11, 01, 09, 00, 13, 02, 05, 09, 00, 13, 0d, 05, 08, 00, 0f, 15, 01, 09, 00, 13, 00, 03, 0d, 00, 1d, 06, 03, 09, 00, 13, 00, 03, 0d, 00, 1d, 0d, 03, 05, 00, 0f, 0d, 01, 0c, 00, 13, 19, 01, 0d, 00, 13, 0a, 02, 0d, 00, 13, 1d, 04, 05, 02, 13, 21, 03, 0d, 00, 13, 0e, 02, 0d, 00, 13, 27, 03, 05, 00, 0f, 25, 01, 0c, 00, 13, 29, 01, 0d, 00, 17, 29, 04, 0d, 00, 13, 1e, 02, 0d, 00, 17, 1e, 01, 14, 00, 1b, 00, 01, 15, 00, 1b, 1e, 02, 15, 00, 1b, 2d, 04, 0d, 00, 13, 22, 03, 09, 00, 19, 31, 02, 05, 00, 0f, 31, 03, 09, 00, 22, 00, 02, 05, 00, 0f, 00, 03, 09, 00, 2c, 00, 02, 01, 00, 02]
6363
Number of files: 1
6464
- file 0 => global file 1
65-
Number of expressions: 9
65+
Number of expressions: 10
6666
- expression 0 operands: lhs = Counter(3), rhs = Counter(4)
6767
- expression 1 operands: lhs = Counter(3), rhs = Counter(5)
6868
- expression 2 operands: lhs = Counter(3), rhs = Counter(6)
6969
- expression 3 operands: lhs = Counter(7), rhs = Counter(8)
70-
- expression 4 operands: lhs = Counter(9), rhs = Counter(10)
70+
- expression 4 operands: lhs = Counter(10), rhs = Counter(11)
7171
- expression 5 operands: lhs = Counter(9), rhs = Counter(10)
7272
- expression 6 operands: lhs = Counter(9), rhs = Counter(10)
7373
- expression 7 operands: lhs = Counter(9), rhs = Counter(10)
74-
- expression 8 operands: lhs = Counter(9), rhs = Counter(11)
74+
- expression 8 operands: lhs = Expression(9, Add), rhs = Counter(12)
75+
- expression 9 operands: lhs = Counter(10), rhs = Counter(11)
7576
Number of file 0 mappings: 51
7677
- Code(Counter(0)) at (prev + 8, 1) to (start + 3, 15)
7778
- Code(Counter(1)) at (prev + 4, 9) to (start + 1, 15)
@@ -113,7 +114,8 @@ Number of file 0 mappings: 51
113114
- Code(Counter(8)) at (prev + 3, 13) to (start + 0, 19)
114115
- Code(Expression(3, Sub)) at (prev + 2, 13) to (start + 0, 19)
115116
= (c7 - c8)
116-
- Code(Counter(9)) at (prev + 3, 5) to (start + 0, 15)
117+
- Code(Expression(9, Add)) at (prev + 3, 5) to (start + 0, 15)
118+
= (c10 + c11)
117119
- Code(Counter(9)) at (prev + 1, 12) to (start + 0, 19)
118120
- Code(Counter(10)) at (prev + 1, 13) to (start + 0, 23)
119121
- Code(Counter(10)) at (prev + 4, 13) to (start + 0, 19)
@@ -124,14 +126,13 @@ Number of file 0 mappings: 51
124126
- Code(Zero) at (prev + 1, 21) to (start + 0, 27)
125127
- Code(Expression(7, Sub)) at (prev + 2, 21) to (start + 0, 27)
126128
= (c9 - c10)
127-
- Code(Expression(7, Sub)) at (prev + 4, 13) to (start + 0, 19)
128-
= (c9 - c10)
129+
- Code(Counter(11)) at (prev + 4, 13) to (start + 0, 19)
129130
- Code(Expression(8, Sub)) at (prev + 3, 9) to (start + 0, 25)
130-
= (c9 - c11)
131-
- Code(Counter(11)) at (prev + 2, 5) to (start + 0, 15)
132-
- Code(Counter(11)) at (prev + 3, 9) to (start + 0, 34)
131+
= ((c10 + c11) - c12)
132+
- Code(Counter(12)) at (prev + 2, 5) to (start + 0, 15)
133+
- Code(Counter(12)) at (prev + 3, 9) to (start + 0, 34)
133134
- Code(Zero) at (prev + 2, 5) to (start + 0, 15)
134135
- Code(Zero) at (prev + 3, 9) to (start + 0, 44)
135136
- Code(Zero) at (prev + 2, 1) to (start + 0, 2)
136-
Highest counter ID seen: c11
137+
Highest counter ID seen: c12
137138

0 commit comments

Comments
 (0)