Skip to content

Commit

Permalink
Auto merge of rust-lang#116012 - cjgillot:gvn-const, r=oli-obk
Browse files Browse the repository at this point in the history
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`
  • Loading branch information
bors committed Dec 23, 2023
2 parents 5eccfc3 + 58e49fa commit 087fc81
Show file tree
Hide file tree
Showing 209 changed files with 1,090 additions and 1,582 deletions.
8 changes: 6 additions & 2 deletions compiler/rustc_const_eval/src/const_eval/eval_queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::mem;

use either::{Left, Right};

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

pub fn eval_in_interpreter<'mir, 'tcx>(
fn eval_in_interpreter<'mir, 'tcx>(
mut ecx: InterpCx<'mir, 'tcx, CompileTimeInterpreter<'mir, 'tcx>>,
cid: GlobalId<'tcx>,
is_static: bool,
Expand Down
Loading

0 comments on commit 087fc81

Please sign in to comment.