Auto merge of #106227 - bryangarza:ctfe-limit, r=oli-obk

Use stable metric for const eval limit instead of current terminator-based logic

This patch adds a `MirPass` that inserts a new MIR instruction `ConstEvalCounter` to any loops and function calls in the CFG. This instruction is used during Const Eval to count against the `const_eval_limit`, and emit the `StepLimitReached` error, replacing the current logic which uses Terminators only.

The new method of counting loops and function calls should be more stable across compiler versions (i.e., not cause crates that compiled successfully before, to no longer compile when changes to the MIR generation/optimization are made).

Also see: #103877
This commit is contained in:
bors 2023-01-29 04:11:27 +00:00
commit 3cdd0197e7
50 changed files with 400 additions and 20 deletions

View file

@ -135,7 +135,47 @@ pub fn dominators<G: ControlFlowGraph>(graph: G) -> Dominators<G::Node> {
// This loop computes the semi[w] for w.
semi[w] = w;
for v in graph.predecessors(pre_order_to_real[w]) {
// Reachable vertices may have unreachable predecessors, so ignore any of them
// TL;DR: Reachable vertices may have unreachable predecessors, so ignore any of them.
//
// Ignore blocks which are not connected to the entry block.
//
// The algorithm that was used to traverse the graph and build the
// `pre_order_to_real` and `real_to_pre_order` vectors does so by
// starting from the entry block and following the successors.
// Therefore, any blocks not reachable from the entry block will be
// set to `None` in the `pre_order_to_real` vector.
//
// For example, in this graph, A and B should be skipped:
//
// ┌─────┐
// │ │
// └──┬──┘
// │
// ┌──▼──┐ ┌─────┐
// │ │ │ A │
// └──┬──┘ └──┬──┘
// │ │
// ┌───────┴───────┐ │
// │ │ │
// ┌──▼──┐ ┌──▼──┐ ┌──▼──┐
// │ │ │ │ │ B │
// └──┬──┘ └──┬──┘ └──┬──┘
// │ └──────┬─────┘
// ┌──▼──┐ │
// │ │ │
// └──┬──┘ ┌──▼──┐
// │ │ │
// │ └─────┘
// ┌──▼──┐
// │ │
// └──┬──┘
// │
// ┌──▼──┐
// │ │
// └─────┘
//
// ...this may be the case if a MirPass modifies the CFG to remove
// or rearrange certain blocks/edges.
let Some(v) = real_to_pre_order[v] else {
continue
};
@ -264,13 +304,18 @@ fn compress(
}
}
/// Tracks the list of dominators for each node.
#[derive(Clone, Debug)]
pub struct Dominators<N: Idx> {
post_order_rank: IndexVec<N, usize>,
// Even though we track only the immediate dominator of each node, it's
// possible to get its full list of dominators by looking up the dominator
// of each dominator. (See the `impl Iterator for Iter` definition).
immediate_dominators: IndexVec<N, Option<N>>,
}
impl<Node: Idx> Dominators<Node> {
/// Whether the given Node has an immediate dominator.
pub fn is_reachable(&self, node: Node) -> bool {
self.immediate_dominators[node].is_some()
}
@ -280,6 +325,8 @@ impl<Node: Idx> Dominators<Node> {
self.immediate_dominators[node].unwrap()
}
/// Provides an iterator over each dominator up the CFG, for the given Node.
/// See the `impl Iterator for Iter` definition to understand how this works.
pub fn dominators(&self, node: Node) -> Iter<'_, Node> {
assert!(self.is_reachable(node), "node {node:?} is not reachable");
Iter { dominators: self, node: Some(node) }