Skip to content

Add --analyzed-ir-hook to hand the datum to a consumer's Scheme file - #17

Closed
RomarQ wants to merge 3 commits into
rp/normalized-irfrom
rp/analyzed-ir-hook
Closed

RomarQ wants to merge 3 commits into
rp/normalized-irfrom
rp/analyzed-ir-hook

Conversation

@RomarQ

@RomarQ RomarQ commented Aug 17, 2026

Copy link
Copy Markdown
Owner

Stacked on #16. Only the last commit belongs to this PR.

--analyzed-ir writes the analyzed program to compiler/analyzed-ir.sexp. This flag calls a user procedure with the same value instead, and writes no file. A consumer then renders the datum in the encoding its own tools want.

compactc --skip-zk --analyzed-ir-hook my-hook.ss contract.compact out/
;;; No import. The datum is data.
(define hook
  (lambda (sexp target-directory)
    (call-with-output-file (string-append target-directory "/compiler/circuits.txt")
      (lambda (op)
        (for-each
          (lambda (form)
            (when (and (pair? form) (eq? (car form) 'circuit))
              (fprintf op "~a ~a\n" (cadr form) (cadr (caddr form)))))
          (cdr sexp)))
      'replace)))

Why this costs nothing

The hook takes the datum and the target directory as arguments. The datum is ordinary data: symbols, strings, exact integers, bytevectors, booleans and lists. So the loaded file imports nothing from the compiler, and compile-whole-program keeps its libraries pruned.

A hook that reads the live intermediate representation cannot do that. To take a nanopass record apart you write nanopass-case, which is a macro, so the file must import (langs) and the whole program has to keep its libraries visible. langs.so is 6,906,038 bytes, over half of all library objects, which is where the binary growth in #13 comes from.

Measured on this machine:

build binary
#16, --analyzed-ir only 3,012,931 bytes
this branch 3,014,486 bytes
#13, --run-hook 7,945,794 bytes

The startup cost that comes with visible libraries, about 110 ms per invocation, is likewise absent.

The contract

(hook sexp target-directory).

  • sexp is the datum --analyzed-ir prints, before printing.
  • target-directory is a string, so the hook writes files without importing (pass-helpers).
  • The file must define a top-level procedure named hook.

Two mistakes report themselves:

$ compactc --skip-zk --analyzed-ir-hook no-hook.ss c.compact out/
Exception: the file loaded by --analyzed-ir-hook must define a procedure named hook

$ compactc --skip-zk --analyzed-ir-hook imports-langs.ss c.compact out/
Exception: library (nanopass-extension) not found

The second is the point: the libraries stay invisible, so a hook cannot reach into the compiler.

Verification

nix build .#compactc is green, including the full unit suite in the checkPhase.

  • The hook receives exactly what the flag writes. For nine sources (bboard, counter, events, ser, ccc, loops, slices, mint-probe, zerocash) a hook that pretty-prints the datum produces a file byte-identical to the one --analyzed-ir writes.
  • --analyzed-ir output is unchanged: bboard is byte-identical to the reference consumer's committed fixture.
  • Without either flag, the target directory is unchanged and contract-info.json is byte-identical to a build of the base branch.
  • With the hook, no analyzed-ir.sexp is created; only what the hook writes.

Relation to #13

#13 also runs a user Scheme file, but it hands over the live records after every pass, and pays for that with visible libraries. Its own PR reports the cost. This PR gives a consumer the same ability to own the output format, with none of the cost, because the compiler serializes first.

It does not answer the other half of #13's argument, that the project should carry no format at all. It cannot: the datum has to come from somewhere, and here it comes from the pass in #16. That is the trade. If the project accepts owning the pass, this flag makes the output format a consumer's choice.

`--analyzed-ir` prints the analyzed program to a file. This flag calls a
user procedure with the same value instead, so a consumer chooses the
encoding its own tools want.

The hook takes the datum and the target directory as arguments. The datum
is ordinary data: symbols, strings, exact integers, bytevectors, booleans
and lists. So the loaded file imports nothing from the compiler, and the
binary keeps its libraries pruned. A hook that reads the live intermediate
representation cannot do this, because `nanopass-case` needs `(langs)` at
expansion time, and `(langs)` is over half of all library objects.

Measured on this machine: 3,014,486 bytes, against 3,012,931 for the
branch this builds on.

Signed-off-by: Rodrigo Quelhas <rodrigo_quelhas@outlook.pt>
@github-actions

github-actions Bot commented Aug 17, 2026 •

Copy link
Copy Markdown

Plugin Test Summary

 1 files  ±0   3 suites  ±0   1s ⏱️ ±0s
21 tests ±0  21 ✅ ±0  0 💤 ±0  0 ❌ ±0 
23 runs  ±0  23 ✅ ±0  0 💤 ±0  0 ❌ ±0 

Results for commit c97dd25. ± Comparison against base commit ccef80e.

♻️ This comment has been updated with latest results.

RomarQ added 2 commits August 17, 2026 14:04
The same three fixes as the sibling flag. The checked-in `--help` fixture
gains the paragraph. `load-hook` moves inside `handle-exceptions`, since a
`parameterize` initializer runs before the body and a missing file printed a
raw Chez exception. And the hook call moves to the end of the successful
path, because the compiler does not track the files a hook writes and so
cannot roll them back.

Signed-off-by: Rodrigo Quelhas <rodrigo_quelhas@outlook.pt>
RomarQ added a commit that referenced this pull request Aug 17, 2026
`passes.ss` binds four intermediate representations in one `let*`:
`lsrc-ir`, `frontend-ir`, `analyzed-ir` and `circuit-ir`. The hook receives
the third, so the flag says which one.

This is the name #17 uses for its own hook. The two are alternatives, so
only one can land, and naming them alike keeps the choice about the
contract rather than the spelling.

Signed-off-by: Rodrigo Quelhas <rodrigo_quelhas@outlook.pt>
@RomarQ

RomarQ commented Aug 17, 2026

Copy link
Copy Markdown
Owner Author

Closing in favour of #18.

Both hand a consumer's Scheme file the analyzed IR, and both keep the binary pruned. #18 hands over the analyzed program itself rather than the datum printed from it, so the emitter moves out of the compiler entirely instead of the compiler keeping the 391-line pass and the consumer owning only the rendering. #18 reproduces this route's artifact byte for byte across all 215 example contracts that compile, so nothing here is lost.

The three review fixes this PR received (the checked-in --help fixture, loading the hook file inside handle-exceptions, and running the hook after the compile succeeds) are all in #18.

The branch stays for reference.

@RomarQ RomarQ closed this Aug 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant