Skip to content

Commit 087fc81

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 5eccfc3 + 58e49fa commit 087fc81

File tree

209 files changed

+1090
-1582
lines changed

Some content is hidden

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

209 files changed

+1090
-1582
lines changed

compiler/rustc_const_eval/src/const_eval/eval_queries.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::mem;
22

33
use either::{Left, Right};
44

5+
use rustc_data_structures::stack::ensure_sufficient_stack;
56
use rustc_hir::def::DefKind;
67
use rustc_middle::mir::interpret::{AllocId, ErrorHandled, InterpErrorInfo};
78
use rustc_middle::mir::pretty::write_allocation_bytes;
@@ -305,10 +306,13 @@ pub fn eval_to_allocation_raw_provider<'tcx>(
305306
// they do not have to behave "as if" they were evaluated at runtime.
306307
CompileTimeInterpreter::new(CanAccessStatics::from(is_static), CheckAlignment::Error),
307308
);
308-
eval_in_interpreter(ecx, cid, is_static)
309+
// Evaluating this MIR in the interpreter may in turn require evaluating constants to
310+
// allocations. As the recursion depends on what the user wrote, we need to protect ourselves
311+
// from stack overflow.
312+
ensure_sufficient_stack(|| eval_in_interpreter(ecx, cid, is_static))
309313
}
310314

311-
pub fn eval_in_interpreter<'mir, 'tcx>(
315+
fn eval_in_interpreter<'mir, 'tcx>(
312316
mut ecx: InterpCx<'mir, 'tcx, CompileTimeInterpreter<'mir, 'tcx>>,
313317
cid: GlobalId<'tcx>,
314318
is_static: bool,

0 commit comments

Comments
 (0)