Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A couple of small refactorings to the egraph elaboration pass #7304

Merged
merged 1 commit into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions cranelift/codegen/src/egraph/cost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@ use crate::ir::Opcode;
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) struct Cost(u32);
impl Cost {
pub(crate) fn at_level(&self, loop_level: usize) -> Cost {
let loop_level = std::cmp::min(2, loop_level);
let multiplier = 1u32 << ((10 * loop_level) as u32);
Cost(self.0.saturating_mul(multiplier)).finite()
}

pub(crate) fn infinity() -> Cost {
// 2^32 - 1 is, uh, pretty close to infinite... (we use `Cost`
// only for heuristics and always saturate so this suffices!)
Expand Down
49 changes: 21 additions & 28 deletions cranelift/codegen/src/egraph/elaborate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use super::Stats;
use crate::dominator_tree::DominatorTree;
use crate::fx::FxHashSet;
use crate::ir::{Block, Function, Inst, Value, ValueDef};
use crate::loop_analysis::{Loop, LoopAnalysis, LoopLevel};
use crate::loop_analysis::{Loop, LoopAnalysis};
use crate::scoped_hash_map::ScopedHashMap;
use crate::trace;
use crate::unionfind::UnionFind;
Expand Down Expand Up @@ -211,26 +211,24 @@ impl<'a> Elaborator<'a> {
// at this point, only the side-effecting skeleton),
// then it must be computed and thus we give it zero
// cost.
ValueDef::Result(inst, _) if self.func.layout.inst_block(inst).is_some() => {
best[value] = (Cost::zero(), value);
}
ValueDef::Result(inst, _) => {
trace!(" -> value {}: result, computing cost", value);
let inst_data = &self.func.dfg.insts[inst];
let loop_level = self
.func
.layout
.inst_block(inst)
.map(|block| self.loop_analysis.loop_level(block))
.unwrap_or(LoopLevel::root());
// N.B.: at this point we know that the opcode is
// pure, so `pure_op_cost`'s precondition is
// satisfied.
let cost = self.func.dfg.inst_values(inst).fold(
pure_op_cost(inst_data.opcode()).at_level(loop_level.level()),
|cost, value| cost + best[value].0,
);
best[value] = (cost, value);
if let Some(_) = self.func.layout.inst_block(inst) {
best[value] = (Cost::zero(), value);
} else {
trace!(" -> value {}: result, computing cost", value);
let inst_data = &self.func.dfg.insts[inst];
// N.B.: at this point we know that the opcode is
// pure, so `pure_op_cost`'s precondition is
// satisfied.
let cost = self
.func
.dfg
.inst_values(inst)
.fold(pure_op_cost(inst_data.opcode()), |cost, value| {
cost + best[value].0
});
best[value] = (cost, value);
}
}
};
debug_assert_ne!(best[value].0, Cost::infinity());
Expand Down Expand Up @@ -259,12 +257,9 @@ impl<'a> Elaborator<'a> {
}

fn process_elab_stack(&mut self) {
while let Some(entry) = self.elab_stack.last() {
while let Some(entry) = self.elab_stack.pop() {
match entry {
&ElabStackEntry::Start { value, before } => {
// We always replace the Start entry, so pop it now.
self.elab_stack.pop();

ElabStackEntry::Start { value, before } => {
debug_assert_ne!(value, Value::reserved_value());
let value = self.func.dfg.resolve_aliases(value);

Expand Down Expand Up @@ -376,15 +371,13 @@ impl<'a> Elaborator<'a> {
}
}

&ElabStackEntry::PendingInst {
ElabStackEntry::PendingInst {
inst,
result_idx,
num_args,
remat,
before,
} => {
self.elab_stack.pop();

trace!(
"PendingInst: {} result {} args {} remat {} before {}",
inst,
Expand Down