Skip to content

Commit 759dc57

Browse files
authored
feat(debug): Print ssa locations along with ssa (#9001)
1 parent c686a3e commit 759dc57

21 files changed

Lines changed: 238 additions & 102 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiler/noirc_driver/src/lib.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -790,9 +790,13 @@ pub fn compile_no_check(
790790

791791
let SsaProgramArtifact { program, debug, warnings, names, brillig_names, error_types, .. } =
792792
if options.minimal_ssa {
793-
create_program_with_minimal_passes(program, &ssa_evaluator_options)?
793+
create_program_with_minimal_passes(
794+
program,
795+
&ssa_evaluator_options,
796+
&context.file_manager,
797+
)?
794798
} else {
795-
create_program(program, &ssa_evaluator_options)?
799+
create_program(program, &ssa_evaluator_options, Some(&context.file_manager))?
796800
};
797801

798802
let abi = gen_abi(context, &main_function, return_visibility, error_types);

compiler/noirc_evaluator/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ cfg-if.workspace = true
3434
smallvec = { version = "1.13.2", features = ["serde"] }
3535
vec-collections = "0.4.3"
3636
petgraph.workspace = true
37+
fm.workspace = true
3738

3839
[dev-dependencies]
3940
proptest.workspace = true

compiler/noirc_evaluator/src/acir/tests/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,6 @@ fn brillig_stdlib_calls_with_regular_brillig_call() {
560560
let ssa = builder.finish();
561561
// We need to generate Brillig artifacts for the regular Brillig function and pass them to the ACIR generation pass.
562562
let brillig = ssa.to_brillig(&BrilligOptions::default());
563-
println!("{}", ssa);
564563

565564
let (acir_functions, brillig_functions, _, _) = ssa
566565
.generate_entry_point_index()
@@ -645,7 +644,6 @@ fn brillig_stdlib_calls_with_multiple_acir_calls() {
645644
let ssa = builder.finish();
646645
// We need to generate Brillig artifacts for the regular Brillig function and pass them to the ACIR generation pass.
647646
let brillig = ssa.to_brillig(&BrilligOptions::default());
648-
println!("{}", ssa);
649647

650648
let (acir_functions, brillig_functions, _, _) = ssa
651649
.generate_entry_point_index()

compiler/noirc_evaluator/src/ssa.rs

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ where
287287
let ssa_gen_span_guard = ssa_gen_span.enter();
288288
let mut builder = builder.with_skip_passes(options.skip_passes.clone()).run_passes(primary)?;
289289
let passed = std::mem::take(&mut builder.passed);
290+
let files = builder.files;
290291
let mut ssa = builder.finish();
291292

292293
let mut ssa_level_warnings = vec![];
@@ -300,12 +301,16 @@ where
300301
let ssa_gen_span = span!(Level::TRACE, "ssa_generation");
301302
let ssa_gen_span_guard = ssa_gen_span.enter();
302303

303-
let mut ssa =
304-
SsaBuilder::from_ssa(ssa, options.ssa_logging.clone(), options.print_codegen_timings)
305-
.with_passed(passed)
306-
.with_skip_passes(options.skip_passes.clone())
307-
.run_passes(&secondary(&brillig))?
308-
.finish();
304+
let mut ssa = SsaBuilder::from_ssa(
305+
ssa,
306+
options.ssa_logging.clone(),
307+
options.print_codegen_timings,
308+
files,
309+
)
310+
.with_passed(passed)
311+
.with_skip_passes(options.skip_passes.clone())
312+
.run_passes(&secondary(&brillig))?
313+
.finish();
309314

310315
if !options.skip_underconstrained_check {
311316
ssa_level_warnings.extend(time(
@@ -353,6 +358,7 @@ pub fn optimize_into_acir<S>(
353358
options: &SsaEvaluatorOptions,
354359
primary: &[SsaPass],
355360
secondary: S,
361+
files: Option<&fm::FileManager>,
356362
) -> Result<ArtifactsAndWarnings, RuntimeError>
357363
where
358364
S: for<'b> Fn(&'b Brillig) -> Vec<SsaPass<'b>>,
@@ -362,6 +368,7 @@ where
362368
options.ssa_logging.clone(),
363369
options.print_codegen_timings,
364370
&options.emit_ssa,
371+
files,
365372
)?;
366373

367374
optimize_ssa_builder_into_acir(builder, options, primary, secondary)
@@ -436,8 +443,9 @@ impl SsaProgramArtifact {
436443
pub fn create_program(
437444
program: Program,
438445
options: &SsaEvaluatorOptions,
446+
files: Option<&fm::FileManager>,
439447
) -> Result<SsaProgramArtifact, RuntimeError> {
440-
create_program_with_passes(program, options, &primary_passes(options), secondary_passes)
448+
create_program_with_passes(program, options, &primary_passes(options), secondary_passes, files)
441449
}
442450

443451
/// Compiles the [`Program`] into [`ACIR`][acvm::acir::circuit::Program] using the minimum amount of SSA passes.
@@ -448,6 +456,7 @@ pub fn create_program(
448456
pub fn create_program_with_minimal_passes(
449457
program: Program,
450458
options: &SsaEvaluatorOptions,
459+
files: &fm::FileManager,
451460
) -> Result<SsaProgramArtifact, RuntimeError> {
452461
for func in &program.functions {
453462
assert!(
@@ -456,7 +465,7 @@ pub fn create_program_with_minimal_passes(
456465
func.name
457466
);
458467
}
459-
create_program_with_passes(program, options, &minimal_passes(), |_| vec![])
468+
create_program_with_passes(program, options, &minimal_passes(), |_| vec![], Some(files))
460469
}
461470

462471
/// Compiles the [`Program`] into [`ACIR`][acvm::acir::circuit::Program] by running it through
@@ -467,6 +476,7 @@ pub fn create_program_with_passes<S>(
467476
options: &SsaEvaluatorOptions,
468477
primary: &[SsaPass],
469478
secondary: S,
479+
files: Option<&fm::FileManager>,
470480
) -> Result<SsaProgramArtifact, RuntimeError>
471481
where
472482
S: for<'b> Fn(&'b Brillig) -> Vec<SsaPass<'b>>,
@@ -480,7 +490,7 @@ where
480490
let ArtifactsAndWarnings(
481491
(generated_acirs, generated_brillig, brillig_function_names, error_types),
482492
ssa_level_warnings,
483-
) = optimize_into_acir(program, options, primary, secondary)?;
493+
) = optimize_into_acir(program, options, primary, secondary, files)?;
484494

485495
assert_eq!(
486496
generated_acirs.len(),
@@ -630,7 +640,7 @@ fn split_public_and_private_inputs(
630640
}
631641

632642
// This is just a convenience object to bundle the ssa with `print_ssa_passes` for debug printing.
633-
pub struct SsaBuilder {
643+
pub struct SsaBuilder<'local> {
634644
/// The SSA being built; it is the input and the output of every pass ran by the builder.
635645
pub ssa: Ssa,
636646
/// Options to control which SSA passes to print.
@@ -642,14 +652,19 @@ pub struct SsaBuilder {
642652
pub passed: HashMap<String, usize>,
643653
/// List of SSA pass message fragments that we want to skip, for testing purposes.
644654
pub skip_passes: Vec<String>,
655+
656+
/// Providing a file manager is optional - if provided it can be used to print source
657+
/// locations along with each ssa instructions when debugging.
658+
pub files: Option<&'local fm::FileManager>,
645659
}
646660

647-
impl SsaBuilder {
661+
impl<'local> SsaBuilder<'local> {
648662
pub fn from_program(
649663
program: Program,
650664
ssa_logging: SsaLogging,
651665
print_codegen_timings: bool,
652666
emit_ssa: &Option<PathBuf>,
667+
files: Option<&'local fm::FileManager>,
653668
) -> Result<Self, RuntimeError> {
654669
let ssa = ssa_gen::generate_ssa(program)?;
655670
if let Some(emit_ssa) = emit_ssa {
@@ -661,14 +676,20 @@ impl SsaBuilder {
661676
let ssa_path = emit_ssa.with_extension("ssa.json");
662677
write_to_file(&serde_json::to_vec(&ssa).unwrap(), &ssa_path);
663678
}
664-
Ok(Self::from_ssa(ssa, ssa_logging, print_codegen_timings).print("Initial SSA"))
679+
Ok(Self::from_ssa(ssa, ssa_logging, print_codegen_timings, files).print("Initial SSA"))
665680
}
666681

667-
pub fn from_ssa(ssa: Ssa, ssa_logging: SsaLogging, print_codegen_timings: bool) -> Self {
682+
pub fn from_ssa(
683+
ssa: Ssa,
684+
ssa_logging: SsaLogging,
685+
print_codegen_timings: bool,
686+
files: Option<&'local fm::FileManager>,
687+
) -> Self {
668688
Self {
669689
ssa_logging,
670690
print_codegen_timings,
671691
ssa,
692+
files,
672693
passed: Default::default(),
673694
skip_passes: Default::default(),
674695
}
@@ -745,7 +766,7 @@ impl SsaBuilder {
745766
};
746767

747768
if print_ssa_pass {
748-
println!("After {msg}:\n{}", self.ssa);
769+
println!("After {msg}:\n{}", self.ssa.print_with(self.files));
749770
}
750771
self
751772
}

0 commit comments

Comments
 (0)