Make DataFlowGraph::insts public, but restricted (#5450)

We have some operations defined on DataFlowGraph purely to work around borrow-checker issues with InstructionData and other data on DataFlowGraph. Part of the problem is that indexing the DFG directly hides the fact that we're only indexing the insts field of the DFG.

This PR makes the insts field of the DFG public, but wraps it in a newtype that only allows indexing. This means that the borrow checker is better able to tell when operations on memory held by the DFG won't conflict, which comes up frequently when mutating ValueLists held by InstructionData.
This commit is contained in:
Trevor Elliott
2022-12-16 10:46:09 -08:00
committed by GitHub
parent 6323b0f9f4
commit 25bf8e0e67
31 changed files with 182 additions and 178 deletions

View File

@@ -676,7 +676,7 @@ impl<'a> FunctionBuilder<'a> {
/// **Note:** You are responsible for maintaining the coherence with the arguments of
/// other jump instructions.
pub fn change_jump_destination(&mut self, inst: Inst, new_dest: Block) {
let old_dest = self.func.dfg[inst]
let old_dest = self.func.dfg.insts[inst]
.branch_destination_mut()
.expect("you want to change the jump destination of a non-jump instruction");
self.func_ctx.ssa.remove_block_predecessor(*old_dest, inst);

View File

@@ -611,7 +611,7 @@ impl SSABuilder {
}
// Redo the match from `analyze_branch` but this time capture mutable references
match &mut func.dfg[branch] {
match &mut func.dfg.insts[branch] {
InstructionData::BranchTable {
destination, table, ..
} => {
@@ -1192,7 +1192,7 @@ mod tests {
ssa.use_var(&mut func, x_var, I32, block0);
assert_eq!(func.dfg.num_block_params(block0), 0);
assert_eq!(
func.dfg[func.layout.first_inst(block0).unwrap()].opcode(),
func.dfg.insts[func.layout.first_inst(block0).unwrap()].opcode(),
Opcode::Iconst
);
}
@@ -1213,7 +1213,7 @@ mod tests {
ssa.seal_block(block0, &mut func);
assert_eq!(func.dfg.num_block_params(block0), 0);
assert_eq!(
func.dfg[func.layout.first_inst(block0).unwrap()].opcode(),
func.dfg.insts[func.layout.first_inst(block0).unwrap()].opcode(),
Opcode::Iconst
);
}