Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/noirc_evaluator/src/ssa/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ pub fn primary_passes(options: &SsaEvaluatorOptions) -> Vec<SsaPass> {
),
SsaPass::new(Ssa::simplify_cfg, "Simplifying"),
SsaPass::new(Ssa::mem2reg, "Mem2Reg"),
SsaPass::new(Ssa::simplify_cfg, "Simplifying"),
SsaPass::new(Ssa::flatten_cfg, "Flattening"),
SsaPass::new(Ssa::remove_bit_shifts, "Removing Bit Shifts"),
// Run mem2reg once more with the flattened CFG to catch any remaining loads/stores
Expand Down
61 changes: 61 additions & 0 deletions compiler/noirc_evaluator/src/ssa/opt/flatten_cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,73 @@ impl Ssa {
}

for function in self.functions.values_mut() {
#[cfg(debug_assertions)]
flatten_cfg_pre_check(function);

flatten_function_cfg(function, &no_predicates);

// Disabled as we're getting failures, I would expect this to pass however.
// #[cfg(debug_assertions)]
// flatten_cfg_post_check(function);
}
self
}
}

/// Pre-check condition for [Ssa::flatten_cfg].
///
/// Succeeds if:
/// - no block contains a jmpif with a constant condition.
///
/// Otherwise panics.
#[cfg(debug_assertions)]
fn flatten_cfg_pre_check(function: &Function) {
function.reachable_blocks().iter().for_each(|block| {
if let TerminatorInstruction::JmpIf { condition, .. } =
function.dfg[*block].unwrap_terminator()
{
if function
.dfg
.get_numeric_constant(*condition)
.is_some_and(|c| c.is_zero() || c.is_one())
{
// We have this check here as if we do not, the flattening pass will generate groups of opcodes which are
// under a zero predicate. These opcodes are not always removed by the die pass and so bloat the code.
//
// We could add handling for this inside of the pass itself but it's simpler to just run `simplify_cfg`
// immediately before flattening.
panic!(
"Function {} has a jmpif with a constant condition {condition}",
function.id()
);
}
}
});
}

/// Post-check condition for [Ssa::flatten_cfg].
///
/// Panics if:
/// - Any `enable_side_effects u1 0` instructions exist.
///
/// Otherwise succeeds.
#[cfg(debug_assertions)]
#[allow(dead_code)]
fn flatten_cfg_post_check(function: &Function) {
function.reachable_blocks().iter().for_each(|block| {
function.dfg[*block].instructions().iter().for_each(|instruction| {
if let Instruction::EnableSideEffectsIf { condition } = function.dfg[*instruction] {
if function.dfg.get_numeric_constant(condition).is_some_and(|c| c.is_zero()) {
panic!(
"Function {} has an enable_side_effects u1 0 instruction",
function.id()
);
}
}
});
});
}

pub(crate) struct Context<'f> {
pub(crate) inserter: FunctionInserter<'f>,

Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Loading