Skip to content

Commit 56cfbe6

Browse files
author
jfecher
authored
chore: Cleanup unrolling pass (#6743)
1 parent 332ba79 commit 56cfbe6

File tree

1 file changed

+26
-43
lines changed

1 file changed

+26
-43
lines changed

compiler/noirc_evaluator/src/ssa/opt/unrolling.rs

Lines changed: 26 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -279,10 +279,10 @@ impl Loop {
279279
&self,
280280
function: &Function,
281281
cfg: &ControlFlowGraph,
282-
) -> Result<Option<FieldElement>, CallStack> {
283-
let pre_header = self.get_pre_header(function, cfg)?;
284-
let jump_value = get_induction_variable(function, pre_header)?;
285-
Ok(function.dfg.get_numeric_constant(jump_value))
282+
) -> Option<FieldElement> {
283+
let pre_header = self.get_pre_header(function, cfg).ok()?;
284+
let jump_value = get_induction_variable(function, pre_header).ok()?;
285+
function.dfg.get_numeric_constant(jump_value)
286286
}
287287

288288
/// Find the upper bound of the loop in the loop header and return it
@@ -332,14 +332,10 @@ impl Loop {
332332
&self,
333333
function: &Function,
334334
cfg: &ControlFlowGraph,
335-
) -> Result<Option<(FieldElement, FieldElement)>, CallStack> {
336-
let Some(lower) = self.get_const_lower_bound(function, cfg)? else {
337-
return Ok(None);
338-
};
339-
let Some(upper) = self.get_const_upper_bound(function) else {
340-
return Ok(None);
341-
};
342-
Ok(Some((lower, upper)))
335+
) -> Option<(FieldElement, FieldElement)> {
336+
let lower = self.get_const_lower_bound(function, cfg)?;
337+
let upper = self.get_const_upper_bound(function)?;
338+
Some((lower, upper))
343339
}
344340

345341
/// Unroll a single loop in the function.
@@ -552,32 +548,29 @@ impl Loop {
552548
&self,
553549
function: &Function,
554550
cfg: &ControlFlowGraph,
555-
) -> Result<HashSet<ValueId>, CallStack> {
551+
) -> Option<HashSet<ValueId>> {
556552
// We need to traverse blocks from the pre-header up to the block entry point.
557-
let pre_header = self.get_pre_header(function, cfg)?;
553+
let pre_header = self.get_pre_header(function, cfg).ok()?;
558554
let function_entry = function.entry_block();
559555

560556
// The algorithm in `find_blocks_in_loop` expects to collect the blocks between the header and the back-edge of the loop,
561557
// but technically works the same if we go from the pre-header up to the function entry as well.
562558
let blocks = Self::find_blocks_in_loop(function_entry, pre_header, cfg).blocks;
563559

564560
// Collect allocations in all blocks above the header.
565-
let allocations = blocks.iter().flat_map(|b| {
566-
function.dfg[*b]
567-
.instructions()
568-
.iter()
561+
let allocations = blocks.iter().flat_map(|block| {
562+
let instructions = function.dfg[*block].instructions().iter();
563+
instructions
569564
.filter(|i| matches!(&function.dfg[**i], Instruction::Allocate))
570-
.map(|i| {
571-
// Get the value into which the allocation was stored.
572-
function.dfg.instruction_results(*i)[0]
573-
})
565+
// Get the value into which the allocation was stored.
566+
.map(|i| function.dfg.instruction_results(*i)[0])
574567
});
575568

576569
// Collect reference parameters of the function itself.
577570
let params =
578571
function.parameters().iter().filter(|p| function.dfg.value_is_reference(**p)).copied();
579572

580-
Ok(params.chain(allocations).collect())
573+
Some(params.chain(allocations).collect())
581574
}
582575

583576
/// Count the number of load and store instructions of specific variables in the loop.
@@ -608,13 +601,11 @@ impl Loop {
608601

609602
/// Count the number of instructions in the loop, including the terminating jumps.
610603
fn count_all_instructions(&self, function: &Function) -> usize {
611-
self.blocks
612-
.iter()
613-
.map(|block| {
614-
let block = &function.dfg[*block];
615-
block.instructions().len() + block.terminator().map(|_| 1).unwrap_or_default()
616-
})
617-
.sum()
604+
let iter = self.blocks.iter().map(|block| {
605+
let block = &function.dfg[*block];
606+
block.instructions().len() + block.terminator().is_some() as usize
607+
});
608+
iter.sum()
618609
}
619610

620611
/// Count the number of increments to the induction variable.
@@ -645,18 +636,11 @@ impl Loop {
645636
function: &Function,
646637
cfg: &ControlFlowGraph,
647638
) -> Option<BoilerplateStats> {
648-
let Ok(Some((lower, upper))) = self.get_const_bounds(function, cfg) else {
649-
return None;
650-
};
651-
let Some(lower) = lower.try_to_u64() else {
652-
return None;
653-
};
654-
let Some(upper) = upper.try_to_u64() else {
655-
return None;
656-
};
657-
let Ok(refs) = self.find_pre_header_reference_values(function, cfg) else {
658-
return None;
659-
};
639+
let (lower, upper) = self.get_const_bounds(function, cfg)?;
640+
let lower = lower.try_to_u64()?;
641+
let upper = upper.try_to_u64()?;
642+
let refs = self.find_pre_header_reference_values(function, cfg)?;
643+
660644
let (loads, stores) = self.count_loads_and_stores(function, &refs);
661645
let increments = self.count_induction_increments(function);
662646
let all_instructions = self.count_all_instructions(function);
@@ -1147,7 +1131,6 @@ mod tests {
11471131

11481132
let (lower, upper) = loops.yet_to_unroll[0]
11491133
.get_const_bounds(function, &loops.cfg)
1150-
.expect("should find bounds")
11511134
.expect("bounds are numeric const");
11521135

11531136
assert_eq!(lower, FieldElement::from(0u32));

0 commit comments

Comments
 (0)