feat: annotate printed ACIR with Noir source locations and snippets - #13431
Open
asterite wants to merge 2 commits into
Open
feat: annotate printed ACIR with Noir source locations and snippets#13431asterite wants to merge 2 commits into
asterite wants to merge 2 commits into
Conversation
Add a --with-acir-locations flag that, combined with --print-acir, prints a `// file:line:col: snippet` comment above each run of ACIR opcodes compiled from the same source span, using the call stacks already recorded in DebugInfo. Inlined opcodes carry a compact `(via caller1 <- caller2)` trail back to the user's call site, and an unattributed opcode following an annotated run is marked explicitly so it isn't mistaken for part of that run. The acir display functions take the annotations as opaque strings since the acir crate has no access to Location/DebugInfo. Annotations are `//` comments, which the ACIR parser already skips, so annotated output still parses back to the same circuit. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Move the ACIR opcode annotation builder from noirc_driver into a new noirc_artifacts::annotations module so it can work directly on compiled artifacts, and use it in `noir-inspector print-acir --with-locations`. Since artifacts embed both the debug symbols and the source file map, the ACIR of an existing artifact can be annotated with the originating Noir locations and snippets without the source tree or a recompile. Works for both program and contract artifacts. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Everything needed to map a compiled constraint back to the Noir expression that produced it already ships in every artifact —
DebugInforecords a full inlined call stack of byte-precise source spans per ACIR opcode, andfile_mapcarries the source text — but none of it is exposed.--print-acirprints bare opcodes, so a reader (human or AI) auditing whether the constraints faithfully represent the source has to infer the mapping from scratch.This adds a way to read that provenance directly.
What's added
nargo compile --print-acir --with-acir-locationsannotates each run of ACIR opcodes with the source it was compiled from:Inlined code carries a trail back to the user's call site, e.g.
// poseidon/mod.nr:161:20: tt * tt (via mod.nr:60:17 <- perm.nr:14:13 <- src/main.nr:4:17).noir-inspector print-acir <artifact.json> --with-locationsproduces the same output from a compiled artifact alone — artifacts embed both the debug symbols and the source file map, so no source tree or recompile is needed. This is the audit-a-thing-someone-handed-you case.Both work for program and contract artifacts.
Behavior details
// no source locationrather than silently inheriting the previous comment. Leading unattributed opcodes (entry-point range checks) stay bare.//comments, which the ACIR parser already skips. A test asserts that annotated output parses back to the identical circuit.Implementation
The
acircrate has no access toLocation/DebugInfo, sodisplay_circuit/display_programtake annotations as opaqueBTreeMap<usize, String>and just print them. Resolution lives in the newnoirc_artifacts::annotationsmodule, shared bynoirc_driverandnoir-inspectorso the two outputs can't drift.No behavior change without the flag: existing
--print-aciroutput is byte-identical (theDisplayimpls passNone).Testing
noirc_driver/tests/print_acir.rs: run dedupe, inlined caller chain, multi-line snippet collapsing, gap marking, and the round-trip-parse guarantee.tooling/inspector/tests/print_acir_tests.rs(with and without the flag). They use a different test program frominfo_tests.rsso the two test binaries don't race on artifact files.poseidon_bn254_hash_width_3(deep stdlib inlining) and on a contract artifact with multiple entry points.Why
The motivating use case is letting an AI validate that a circuit's constraints soundly encode its Noir source. As a check that the output is actually sufficient for that, I ran an audit sweep over 30 constrained test programs using this flag — the annotations were enough to verify signed-arithmetic overflow encodings, byte/bit-decomposition canonicity, predicate gating, and Brillig-hint binding in each case.