Skip to content

Commit

Permalink
cranelift: Resolve CLIF aliases before lowering (#8606)
Browse files Browse the repository at this point in the history
The egraph pass was already doing this, when it ran, and it never adds
any aliases. So do it slightly earlier and unconditionally, and avoid
needing to resolve any aliases during lowering.
  • Loading branch information
jameysharp authored May 15, 2024
1 parent 79347c0 commit c477424
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 14 deletions.
2 changes: 2 additions & 0 deletions cranelift/codegen/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ impl Context {
self.eliminate_unreachable_code(isa)?;
self.remove_constant_phis(isa)?;

self.func.dfg.resolve_all_aliases();

if opt_level != OptLevel::None {
self.egraph_pass(isa, ctrl_plane)?;
}
Expand Down
4 changes: 0 additions & 4 deletions cranelift/codegen/src/egraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -567,10 +567,6 @@ impl<'a> EgraphPass<'a> {
/// only refer to its subset that exists at this stage, to
/// maintain acyclicity.)
fn remove_pure_and_optimize(&mut self) {
// This pass relies on every value having a unique name, so first
// eliminate any value aliases.
self.func.dfg.resolve_all_aliases();

let mut cursor = FuncCursor::new(self.func);
let mut value_to_opt_value: SecondaryMap<Value, Value> =
SecondaryMap::with_default(Value::reserved_value());
Expand Down
20 changes: 10 additions & 10 deletions cranelift/codegen/src/machinst/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ impl<'func, I: VCodeInst> Lower<'func, I> {
// Iterate over all values used by all instructions, noting an
// additional use on each operand.
for arg in f.dfg.inst_values(inst) {
let arg = f.dfg.resolve_aliases(arg);
debug_assert!(f.dfg.value_is_real(arg));
let old = value_ir_uses[arg];
if force_multiple {
trace!(
Expand All @@ -555,15 +555,15 @@ impl<'func, I: VCodeInst> Lower<'func, I> {
}
while let Some(iter) = stack.last_mut() {
if let Some(value) = iter.next() {
let value = f.dfg.resolve_aliases(value);
debug_assert!(f.dfg.value_is_real(value));
trace!(" -> DFS reaches {}", value);
if value_ir_uses[value] == ValueUseState::Multiple {
// Truncate DFS here: no need to go further,
// as whole subtree must already be Multiple.
// With debug asserts, check one level of
// that invariant at least.
debug_assert!(uses(value).into_iter().flatten().all(|arg| {
let arg = f.dfg.resolve_aliases(arg);
debug_assert!(f.dfg.value_is_real(arg));
value_ir_uses[arg] == ValueUseState::Multiple
}));
continue;
Expand Down Expand Up @@ -835,13 +835,12 @@ impl<'func, I: VCodeInst> Lower<'func, I> {

fn get_value_labels<'a>(&'a self, val: Value, depth: usize) -> Option<&'a [ValueLabelStart]> {
if let Some(ref values_labels) = self.f.dfg.values_labels {
debug_assert!(self.f.dfg.value_is_real(val));
trace!(
"get_value_labels: val {} -> {} -> {:?}",
"get_value_labels: val {} -> {:?}",
val,
self.f.dfg.resolve_aliases(val),
values_labels.get(&self.f.dfg.resolve_aliases(val))
values_labels.get(&val)
);
let val = self.f.dfg.resolve_aliases(val);
match values_labels.get(&val) {
Some(&ValueLabelAssignments::Starts(ref list)) => Some(&list[..]),
Some(&ValueLabelAssignments::Alias { value, .. }) if depth < 10 => {
Expand Down Expand Up @@ -971,7 +970,7 @@ impl<'func, I: VCodeInst> Lower<'func, I> {

let mut branch_arg_vregs: SmallVec<[Reg; 16]> = smallvec![];
for &arg in branch_args {
let arg = self.f.dfg.resolve_aliases(arg);
debug_assert!(self.f.dfg.value_is_real(arg));
let regs = self.put_value_in_regs(arg);
branch_arg_vregs.extend_from_slice(regs.regs());
}
Expand Down Expand Up @@ -1222,7 +1221,8 @@ impl<'func, I: VCodeInst> Lower<'func, I> {
/// not be codegen'd (it has been integrated into the current instruction).
pub fn input_as_value(&self, ir_inst: Inst, idx: usize) -> Value {
let val = self.f.dfg.inst_args(ir_inst)[idx];
self.f.dfg.resolve_aliases(val)
debug_assert!(self.f.dfg.value_is_real(val));
val
}

/// Like `get_input_as_source_or_const` but with a `Value`.
Expand Down Expand Up @@ -1322,7 +1322,7 @@ impl<'func, I: VCodeInst> Lower<'func, I> {

/// Put the given value into register(s) and return the assigned register.
pub fn put_value_in_regs(&mut self, val: Value) -> ValueRegs<Reg> {
let val = self.f.dfg.resolve_aliases(val);
debug_assert!(self.f.dfg.value_is_real(val));
trace!("put_value_in_regs: val {}", val);

if let Some(inst) = self.f.dfg.value_def(val).inst() {
Expand Down

0 comments on commit c477424

Please sign in to comment.