Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit a994209

Browse files
committed
Auto merge of rust-lang#116012 - cjgillot:gvn-const, r=oli-obk
Implement constant propagation on top of MIR SSA analysis This implements the idea I proposed in rust-lang#110719 (comment) Based on rust-lang#109597 The value numbering "GVN" pass formulates each rvalue that appears in MIR with an abstract form (the `Value` enum), and assigns an integer `VnIndex` to each. This abstract form can be used to deduplicate values, reusing an earlier local that holds the same value instead of recomputing. This part is proposed in rust-lang#109597. From this abstract representation, we can perform more involved simplifications, for example in rust-lang#111344. With the abstract representation `Value`, we can also attempt to evaluate each to a constant using the interpreter. This builds a `VnIndex -> OpTy` map. From this map, we can opportunistically replace an operand or a rvalue with a constant if their value has an associated `OpTy`. The most relevant commit is [Evaluated computed values to constants.](rust-lang@2767c49)" r? `@oli-obk`
2 parents 495203b + 1293d25 commit a994209

File tree

208 files changed

+1084
-1580
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

208 files changed

+1084
-1580
lines changed

compiler/rustc_mir_transform/src/const_prop.rs

Lines changed: 11 additions & 515 deletions
Large diffs are not rendered by default.

compiler/rustc_mir_transform/src/gvn.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ pub struct GVN;
109109

110110
impl<'tcx> MirPass<'tcx> for GVN {
111111
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
112-
sess.mir_opt_level() >= 4
112+
sess.mir_opt_level() >= 2
113113
}
114114

115115
#[instrument(level = "trace", skip(self, tcx, body))]

compiler/rustc_mir_transform/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,6 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
587587
// destroy the SSA property. It should still happen before const-propagation, so the
588588
// latter pass will leverage the created opportunities.
589589
&separate_const_switch::SeparateConstSwitch,
590-
&const_prop::ConstProp,
591590
&gvn::GVN,
592591
&simplify::SimplifyLocals::AfterGVN,
593592
&dataflow_const_prop::DataflowConstProp,

tests/codegen/inherit_overflow.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// compile-flags: -Zmir-enable-passes=+Inline,+ConstProp --crate-type lib
1+
// compile-flags: -Zmir-enable-passes=+Inline,+GVN --crate-type lib
22
// revisions: ASSERT NOASSERT
33
//[ASSERT] compile-flags: -Coverflow-checks=on
44
//[NOASSERT] compile-flags: -Coverflow-checks=off

tests/coverage/async2.cov-map

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,17 @@ Number of file 0 mappings: 1
77
- Code(Counter(0)) at (prev + 13, 1) to (start + 0, 23)
88

99
Function name: async2::async_func::{closure#0}
10-
Raw bytes (28): 0x[01, 01, 02, 01, 05, 05, 02, 04, 01, 0d, 17, 03, 09, 05, 03, 0a, 02, 06, 02, 02, 06, 00, 07, 07, 01, 01, 00, 02]
10+
Raw bytes (26): 0x[01, 01, 01, 05, 00, 04, 01, 0d, 17, 03, 09, 05, 03, 0a, 02, 06, 00, 02, 06, 00, 07, 03, 01, 01, 00, 02]
1111
Number of files: 1
1212
- file 0 => global file 1
13-
Number of expressions: 2
14-
- expression 0 operands: lhs = Counter(0), rhs = Counter(1)
15-
- expression 1 operands: lhs = Counter(1), rhs = Expression(0, Sub)
13+
Number of expressions: 1
14+
- expression 0 operands: lhs = Counter(1), rhs = Zero
1615
Number of file 0 mappings: 4
1716
- Code(Counter(0)) at (prev + 13, 23) to (start + 3, 9)
1817
- Code(Counter(1)) at (prev + 3, 10) to (start + 2, 6)
19-
- Code(Expression(0, Sub)) at (prev + 2, 6) to (start + 0, 7)
20-
= (c0 - c1)
21-
- Code(Expression(1, Add)) at (prev + 1, 1) to (start + 0, 2)
22-
= (c1 + (c0 - c1))
18+
- Code(Zero) at (prev + 2, 6) to (start + 0, 7)
19+
- Code(Expression(0, Add)) at (prev + 1, 1) to (start + 0, 2)
20+
= (c1 + Zero)
2321

2422
Function name: async2::async_func_just_println
2523
Raw bytes (9): 0x[01, 01, 00, 01, 01, 15, 01, 00, 24]

tests/coverage/partial_eq.cov-map

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,18 @@ Number of file 0 mappings: 2
2525
- Code(Zero) at (prev + 0, 32) to (start + 0, 33)
2626

2727
Function name: <partial_eq::Version as core::cmp::PartialOrd>::partial_cmp
28-
Raw bytes (22): 0x[01, 01, 04, 07, 0b, 05, 09, 0f, 15, 0d, 11, 02, 01, 04, 27, 00, 28, 03, 00, 30, 00, 31]
28+
Raw bytes (22): 0x[01, 01, 04, 07, 0b, 00, 09, 0f, 15, 00, 11, 02, 01, 04, 27, 00, 28, 03, 00, 30, 00, 31]
2929
Number of files: 1
3030
- file 0 => global file 1
3131
Number of expressions: 4
3232
- expression 0 operands: lhs = Expression(1, Add), rhs = Expression(2, Add)
33-
- expression 1 operands: lhs = Counter(1), rhs = Counter(2)
33+
- expression 1 operands: lhs = Zero, rhs = Counter(2)
3434
- expression 2 operands: lhs = Expression(3, Add), rhs = Counter(5)
35-
- expression 3 operands: lhs = Counter(3), rhs = Counter(4)
35+
- expression 3 operands: lhs = Zero, rhs = Counter(4)
3636
Number of file 0 mappings: 2
3737
- Code(Counter(0)) at (prev + 4, 39) to (start + 0, 40)
3838
- Code(Expression(0, Add)) at (prev + 0, 48) to (start + 0, 49)
39-
= ((c1 + c2) + ((c3 + c4) + c5))
39+
= ((Zero + c2) + ((Zero + c4) + c5))
4040

4141
Function name: <partial_eq::Version as core::fmt::Debug>::fmt
4242
Raw bytes (9): 0x[01, 01, 00, 01, 01, 04, 11, 00, 16]

tests/incremental/hashes/for_loops.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,9 @@ pub fn change_iterable() {
103103
}
104104

105105
#[cfg(not(any(cfail1,cfail4)))]
106-
#[rustc_clean(cfg="cfail2", except="hir_owner_nodes, promoted_mir")]
106+
#[rustc_clean(cfg="cfail2", except="hir_owner_nodes, promoted_mir, optimized_mir")]
107107
#[rustc_clean(cfg="cfail3")]
108-
#[rustc_clean(cfg="cfail5", except="hir_owner_nodes, promoted_mir")]
108+
#[rustc_clean(cfg="cfail5", except="hir_owner_nodes, promoted_mir, optimized_mir")]
109109
#[rustc_clean(cfg="cfail6")]
110110
pub fn change_iterable() {
111111
let mut _x = 0;

tests/incremental/string_constant.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// revisions: cfail1 cfail2
2-
// compile-flags: -Z query-dep-graph
2+
// compile-flags: -Z query-dep-graph -Copt-level=0
33
// build-pass (FIXME(62277): could be check-pass?)
44

55
#![allow(warnings)]

tests/mir-opt/const_allocation.main.ConstProp.after.32bit.mir renamed to tests/mir-opt/const_allocation.main.GVN.after.32bit.mir

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// MIR for `main` after ConstProp
1+
// MIR for `main` after GVN
22

33
fn main() -> () {
44
let mut _0: ();
@@ -7,10 +7,10 @@ fn main() -> () {
77

88
bb0: {
99
StorageLive(_1);
10-
StorageLive(_2);
10+
nop;
1111
_2 = const {ALLOC9: &&[(Option<i32>, &[&str])]};
1212
_1 = (*_2);
13-
StorageDead(_2);
13+
nop;
1414
StorageDead(_1);
1515
_0 = const ();
1616
return;

tests/mir-opt/const_allocation.main.ConstProp.after.64bit.mir renamed to tests/mir-opt/const_allocation.main.GVN.after.64bit.mir

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// MIR for `main` after ConstProp
1+
// MIR for `main` after GVN
22

33
fn main() -> () {
44
let mut _0: ();
@@ -7,10 +7,10 @@ fn main() -> () {
77

88
bb0: {
99
StorageLive(_1);
10-
StorageLive(_2);
10+
nop;
1111
_2 = const {ALLOC9: &&[(Option<i32>, &[&str])]};
1212
_1 = (*_2);
13-
StorageDead(_2);
13+
nop;
1414
StorageDead(_1);
1515
_0 = const ();
1616
return;

0 commit comments

Comments
 (0)