Skip to content

Commit

Permalink
Fix gvn alias bug
Browse files Browse the repository at this point in the history
  • Loading branch information
sbillig committed Jul 14, 2024
1 parent 45a065f commit 4a6a1bd
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 31 deletions.
25 changes: 22 additions & 3 deletions crates/codegen/src/optim/gvn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1334,6 +1334,8 @@ struct RedundantCodeRemover<'a> {

/// Record resolved value phis.
resolved_value_phis: FxHashMap<ValuePhi, Value>,

renames: FxHashMap<Value, Value>,
}

impl<'a> RedundantCodeRemover<'a> {
Expand All @@ -1342,7 +1344,23 @@ impl<'a> RedundantCodeRemover<'a> {
solver,
avail_set: SecondaryMap::default(),
resolved_value_phis: FxHashMap::default(),
renames: FxHashMap::default(),
}
}

fn change_to_alias(&mut self, func: &mut Function, value: Value, target: Value) {
func.dfg.change_to_alias(value, target);
self.renames.insert(value, target);
}

fn resolve_alias(&self, mut value: Value) -> Value {
for _ in 0..1 + self.renames.len() {
match self.renames.get(&value) {
Some(v) => value = *v,
None => return value,
}
}
panic!("alias loop detected");
}

/// The entry function of redundant code removal.
Expand Down Expand Up @@ -1421,7 +1439,7 @@ impl<'a> RedundantCodeRemover<'a> {

// Use representative value if the class is in avail set.
if let Some(value) = avails.get(&class) {
func.dfg.change_to_alias(insn_result, *value);
self.change_to_alias(func, insn_result, *value);
inserter.remove_insn(func);
continue;
}
Expand Down Expand Up @@ -1469,7 +1487,8 @@ impl<'a> RedundantCodeRemover<'a> {
ty,
block,
);
func.dfg.change_to_alias(insn_result, value);

self.change_to_alias(func, insn_result, value);
inserter.remove_insn(func);
continue;
}
Expand Down Expand Up @@ -1537,7 +1556,7 @@ impl<'a> RedundantCodeRemover<'a> {
for (value_phi, phi_block) in &phi_insn.args {
let resolved =
self.resolve_value_phi(func, inserter, value_phi, ty, *phi_block);
phi.append_phi_arg(resolved, *phi_block);
phi.append_phi_arg(self.resolve_alias(resolved), *phi_block);
}

// Insert new phi insn to top of the phi_insn block.
Expand Down
1 change: 1 addition & 0 deletions crates/ir/src/dfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ impl DataFlowGraph {
}

pub fn remove_branch_dest(&mut self, insn: Insn, dest: Block) {
// xxx remove user
self.insns[insn].remove_branch_dest(dest)
}

Expand Down
37 changes: 9 additions & 28 deletions crates/ir/src/insn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,10 @@ impl<'a> fmt::Display for DisplayInsn<'a> {
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum InsnData {
/// Unary instructions.
Unary {
code: UnaryOp,
args: [Value; 1],
},
Unary { code: UnaryOp, args: [Value; 1] },

/// Binary instructions.
Binary {
code: BinaryOp,
args: [Value; 2],
},
Binary { code: BinaryOp, args: [Value; 2] },

/// Cast operations.
Cast {
Expand Down Expand Up @@ -86,15 +80,10 @@ pub enum InsnData {
},

/// Unconditional jump instruction.
Jump {
dests: [Block; 1],
},
Jump { dests: [Block; 1] },

/// Conditional jump instruction.
Branch {
args: [Value; 1],
dests: [Block; 2],
},
Branch { args: [Value; 1], dests: [Block; 2] },

/// Indirect jump instruction.
BrTable {
Expand All @@ -104,18 +93,13 @@ pub enum InsnData {
},

/// Allocate a memory on the stack frame for the given type.
Alloca {
ty: Type,
},
Alloca { ty: Type },

/// Return.
Return {
args: Option<Value>,
},
Return { args: Option<Value> },

Gep {
args: SmallVec<[Value; 8]>,
},
/// Get element pointer.
Gep { args: SmallVec<[Value; 8]> },

/// Phi function.
Phi {
Expand Down Expand Up @@ -239,6 +223,7 @@ impl InsnData {
if Some(dest) == *default {
*default = None;
} else {
// xxx remove user
table.retain(|block| dest != *block);
}

Expand Down Expand Up @@ -374,10 +359,6 @@ impl InsnData {
}
}

pub fn replace_arg(&mut self, new_arg: Value, idx: usize) {
self.args_mut()[idx] = new_arg;
}

pub fn is_phi(&self) -> bool {
matches!(self, InsnData::Phi { .. })
}
Expand Down

0 comments on commit 4a6a1bd

Please sign in to comment.