Skip to content
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

fix: print ssa blocks without recursion #6715

Merged
merged 7 commits into from
Dec 6, 2024
Merged
Changes from 3 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
36 changes: 20 additions & 16 deletions compiler/noirc_evaluator/src/ssa/ir/printer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! This file is for pretty-printing the SSA IR in a human-readable form for debugging.
use std::{
collections::HashSet,
collections::{HashSet, VecDeque},
fmt::{Formatter, Result},
};

Expand All @@ -21,27 +21,31 @@ use super::{
/// Helper function for Function's Display impl to pretty-print the function with the given formatter.
pub(crate) fn display_function(function: &Function, f: &mut Formatter) -> Result {
writeln!(f, "{} fn {} {} {{", function.runtime(), function.name(), function.id())?;
display_block_with_successors(function, function.entry_block(), &mut HashSet::new(), f)?;
display_function_blocks(function, f)?;
write!(f, "}}")
}

/// Displays a block followed by all of its successors recursively.
/// This uses a HashSet to keep track of the visited blocks. Otherwise
/// there would be infinite recursion for any loops in the IR.
pub(crate) fn display_block_with_successors(
function: &Function,
block_id: BasicBlockId,
visited: &mut HashSet<BasicBlockId>,
f: &mut Formatter,
) -> Result {
display_block(function, block_id, f)?;
visited.insert(block_id);
/// Displays all of a function's blocks by printing the entry block and its successors, recursively.
pub(crate) fn display_function_blocks(function: &Function, f: &mut Formatter) -> Result {
// The block chain to print might be really long so we use a deque instead of recursion
// to avoid potentially hitting stack overflow (see https://github.com/noir-lang/noir/issues/6520)
let mut blocks_to_print = VecDeque::new();
blocks_to_print.push_back(function.entry_block());

for successor in function.dfg[block_id].successors() {
if !visited.contains(&successor) {
display_block_with_successors(function, successor, visited, f)?;
// Keep track of the visited blocks. Otherwise there would be infinite recursion for any loops in the IR.
let mut visited = HashSet::new();

while let Some(block_id) = blocks_to_print.pop_front() {
asterite marked this conversation as resolved.
Show resolved Hide resolved
if !visited.insert(block_id) {
continue;
};
display_block(function, block_id, f)?;

for successor in function.dfg[block_id].successors() {
blocks_to_print.push_back(successor);
aakoshh marked this conversation as resolved.
Show resolved Hide resolved
}
}

Ok(())
}

Expand Down
Loading