Skip to content

Commit

Permalink
Auto merge of #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 #110719 (comment)

Based on #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 #109597.

From this abstract representation, we can perform more involved simplifications, for example in #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.](2767c49)"

r? `@oli-obk`
  • Loading branch information
bors committed Dec 30, 2023
2 parents 03b5019 + 6d7f063 commit 8d76d07
Show file tree
Hide file tree
Showing 209 changed files with 1,088 additions and 1,581 deletions.
5 changes: 4 additions & 1 deletion compiler/rustc_infer/src/infer/relate/generalize.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::mem;

use rustc_data_structures::sso::SsoHashMap;
use rustc_data_structures::stack::ensure_sufficient_stack;
use rustc_hir::def_id::DefId;
use rustc_middle::infer::unify_key::{ConstVarValue, ConstVariableValue};
use rustc_middle::ty::error::TypeError;
Expand Down Expand Up @@ -215,7 +216,9 @@ where
let old_ambient_variance = self.ambient_variance;
self.ambient_variance = self.ambient_variance.xform(variance);
debug!(?self.ambient_variance, "new ambient variance");
let r = self.relate(a, b)?;
// Recursive calls to `relate` can overflow the stack. For example a deeper version of
// `ui/associated-consts/issue-93775.rs`.
let r = ensure_sufficient_stack(|| self.relate(a, b))?;
self.ambient_variance = old_ambient_variance;
Ok(r)
}
Expand Down

0 comments on commit 8d76d07

Please sign in to comment.