-
Notifications
You must be signed in to change notification settings - Fork 222
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
feat: Push ArrayGet instructions backwards through IfElse instructions to avoid expensive array merges #5570
base: master
Are you sure you want to change the base?
Changes from 3 commits
8b2ac2a
9a912ca
b4885a5
494b2b8
50ca2ef
539c5f9
c8df51f
660b428
7e8b0fb
f7f8763
c81bb94
7319ddb
1f34c75
30b79de
1ea506f
b3dfe8c
33ba5cf
b46a38b
294b39c
70368e2
c084359
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
use crate::ssa::{ | ||
ir::{ | ||
dfg::CallStack, function::Function, instruction::Instruction, map::Id, types::Type, | ||
value::ValueId, | ||
}, | ||
ssa_gen::Ssa, | ||
}; | ||
use fxhash::FxHashMap as HashMap; | ||
|
||
impl Ssa { | ||
// Given an original IfElse instruction is this: | ||
// | ||
// v10 = if v0 then v2 else if v1 then v3 | ||
// | ||
// and a later ArrayGet instruction is this: | ||
// | ||
// v11 = array_get v4, index v4 | ||
// | ||
// we optimize it to this: | ||
// | ||
// v12 = array_get v2, index v4 | ||
// v13 = array_get v3, index v4 | ||
// v14 = if v0 then v12 else if v1 then v13 | ||
#[tracing::instrument(level = "trace", skip(self))] | ||
pub(crate) fn array_get_optimization(mut self) -> Self { | ||
for function in self.functions.values_mut() { | ||
// This should match the check in flatten_cfg | ||
if let crate::ssa::ir::function::RuntimeType::Brillig = function.runtime() { | ||
continue; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I copied this from other passes: I'm not sure what it means though. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This just means that we only want to apply this SSA pass to functions which are being compiled to constrained ACIR rather than unconstrained Brillig. We do this as some optimisations are specific to each runtime. This optimisation would benefit both runtimes however so we should remove this. |
||
|
||
Context::default().optimize_array_get(function); | ||
} | ||
|
||
self | ||
} | ||
} | ||
|
||
#[derive(Default)] | ||
struct Context { | ||
// Given an IfElse instruction, here we map its result to that instruction. | ||
// We only capture such values if the IfElse values are arrays. | ||
result_if_else: HashMap<ValueId, Id<Instruction>>, | ||
} | ||
|
||
impl Context { | ||
fn optimize_array_get(&mut self, function: &mut Function) { | ||
let block = function.entry_block(); | ||
let dfg = &mut function.dfg; | ||
let instructions = dfg[block].take_instructions(); | ||
|
||
for instruction_id in instructions { | ||
let instruction = dfg[instruction_id].clone(); | ||
asterite marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
match &instruction { | ||
Instruction::IfElse { then_value, .. } => { | ||
let then_value = *then_value; | ||
|
||
// Only apply this optimization to IfElse where values are arrays | ||
let Type::Array(..) = dfg.type_of_value(then_value) else { | ||
continue; | ||
}; | ||
|
||
let results = dfg.instruction_results(instruction_id); | ||
let result = results[0]; | ||
self.result_if_else.insert(result, instruction_id); | ||
|
||
dfg[block].instructions_mut().push(instruction_id); | ||
} | ||
Instruction::ArrayGet { array, index } => { | ||
asterite marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// If this array get is for an array that is the result of a previous IfElse... | ||
if let Some(if_else) = self.result_if_else.get(array) { | ||
if let Instruction::IfElse { | ||
asterite marked this conversation as resolved.
Show resolved
Hide resolved
|
||
then_condition, | ||
then_value, | ||
else_condition, | ||
else_value, | ||
.. | ||
} = &dfg[*if_else] | ||
{ | ||
let then_condition = *then_condition; | ||
let then_value = *then_value; | ||
let else_condition = *else_condition; | ||
let else_value = *else_value; | ||
|
||
let then_value_type = dfg.type_of_value(then_value); | ||
|
||
let Type::Array(element_type, _) = then_value_type else { | ||
panic!("ice: expected array type, got {:?}", then_value_type); | ||
}; | ||
let element_type: &Vec<Type> = &element_type; | ||
|
||
// Given the original IfElse instruction is this: | ||
// | ||
// v10 = if v0 then v2 else if v1 then v3 | ||
// | ||
// and the ArrayGet instruction is this: | ||
// | ||
// v11 = array_get v4, index v4 | ||
|
||
// First create an instruction like this, for the then branch: | ||
// | ||
// v12 = array_get v2, index v4 | ||
let then_result = dfg.insert_instruction_and_results( | ||
Instruction::ArrayGet { array: then_value, index: *index }, | ||
block, | ||
Some(element_type.clone()), | ||
asterite marked this conversation as resolved.
Show resolved
Hide resolved
|
||
CallStack::new(), // TODO: check callstack | ||
asterite marked this conversation as resolved.
Show resolved
Hide resolved
|
||
); | ||
let then_result = then_result.first(); | ||
|
||
// Then create an instruction like this, for the else branch: | ||
// | ||
// v13 = array_get v3, index v4 | ||
let else_result = dfg.insert_instruction_and_results( | ||
Instruction::ArrayGet { array: else_value, index: *index }, | ||
block, | ||
Some(element_type.clone()), | ||
CallStack::new(), // TODO: check callstack | ||
); | ||
let else_result = else_result.first(); | ||
|
||
// Finally create an IfElse instruction like this: | ||
// | ||
// v14 = if v0 then v12 else if v1 then v13 | ||
let new_result = dfg.insert_instruction_and_results( | ||
Instruction::IfElse { | ||
then_condition: then_condition, | ||
then_value: then_result, | ||
else_condition: else_condition, | ||
else_value: else_result, | ||
}, | ||
block, | ||
None, // TODO: are these needed? | ||
CallStack::new(), // TODO: check callstack | ||
); | ||
let new_result = new_result.first(); | ||
|
||
// And replace the original instruction's value with this final value | ||
let results = dfg.instruction_results(instruction_id); | ||
let result = results[0]; | ||
dfg.set_value_from_id(result, new_result); | ||
|
||
continue; | ||
} | ||
} | ||
|
||
dfg[block].instructions_mut().push(instruction_id); | ||
} | ||
_ => { | ||
dfg[block].instructions_mut().push(instruction_id); | ||
} | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This name is temporary, we should find a name that better describes this pass.