Skip to content

Add --run-hook to run a user Scheme processor over the pass outputs - #13

Closed
RomarQ wants to merge 2 commits into
mainfrom
rp/run-hook
Closed

RomarQ wants to merge 2 commits into
mainfrom
rp/run-hook

Conversation

@RomarQ

@RomarQ RomarQ commented Aug 14, 2026 •

Copy link
Copy Markdown
Owner

Summary

Add compactc --run-hook <file.ss>: load a user Scheme file and call the procedure it defines under the name hook after every compiler pass, with the raw intermediate representation. The flag threads the file's procedure through the test-hook argument that generate-everything already has (compiler/passes.ss:52), the same plumbing the unit tests use (compiler/test.ss:355). No pass changes, no new files: the diff is the flag in compactc.ss and library visibility in the flake.nix build.

This implements the hook half of the direction from the 2026-08-12 community call on a language-agnostic contract representation: give a consumer a hook to capture the output of a chosen pass, and let the consumer write its own Scheme processor for it.

Why a hook, not the printed IR

An SDK that wants to execute circuits needs information that the unparsed (printed) form of the IR does not carry: the ledger operations' Impact VM templates (vm-code), the export table, the exported/pure id flags, the native entries, and the event emit code. All of that exists only in process. A hook runs in process, so a consumer-owned processor can serialize whatever its SDK needs, and the project does not commit to any output format or version.

Packaging: hooks are enabled by default

Importing the compiler's libraries from loaded code requires compile-whole-program to keep the libraries visible (libs-visible?), and visibility defeats whole-program pruning. This branch enables it in the default build, so every compactc can run hooks. The costs, measured on this machine (macOS, Chez 10.3.0):

pruned (before) visible (this branch)
Binary size 2,996,036 bytes 7,945,794 bytes (2.65x)
Startup, --version, 5-run average 66 ms 174 ms
Full compile of the bulletin-board example, --skip-zk, 3-run average 76 ms 187 ms

The time delta is constant across workloads, so it is boot-time registration of the visible libraries, paid once per invocation. Selective visibility would not help: libs-visible? is a single boolean, and the one library a hook cannot live without, (langs) with the nanopass language definitions, is 6.7 MB of the 12.2 MB of all library objects, so a hypothetical hooks-only subset costs nearly the same. If the size or latency on the default path is unacceptable, the previous commit on this branch packaged visibility as an opt-in compactc-hooks variant instead, with the default binary unchanged; both forms are in the branch history.

The hook contract

The hook receives (pass-name unparser pretty-formats . pass-output-values) after every pass. A minimal example that reports each exported circuit and its purity:

(import (except (chezscheme) errorf)
        (langs) (nanopass) (utils) (pass-helpers))

(define hook
  (lambda (pass-name unparse formats . x*)
    (when (eq? pass-name 'save-contract-info)
      (nanopass-case (Lloweredemit Program) (car x*)
        [(program ,src (,contract-type* ...) ((,export-name* ,name*) ...) ,pelt* ...)
         (for-each
           (lambda (pelt)
             (nanopass-case (Lloweredemit Program-Element) pelt
               [(circuit ,src ,function-name (,arg* ...) ,type ,expr)
                (printf "circuit ~s exported=~s pure=~s\n"
                        (id-sym function-name)
                        (id-exported? function-name)
                        (id-pure? function-name))]
               [else (void)]))
           pelt*)]))))
compactc --skip-zk --run-hook my-hook.ss contract.compact out/

Notes for hook authors, learned while building a real one:

  • A hook sees each pass's output values, not its inputs. save-contract-info returns the analyzed Lloweredemit program; proof-circuit-name* reaches no hook directly and can be recovered from the Lflattened program at the final circuit pass, which runs earlier.
  • (target-directory) from (pass-helpers) names the output directory.
  • The hook compiles against internal, unversioned structures. A hook written for one compiler version can need changes for the next. This is deliberate: the project commits to nothing beyond the flag.

Verification

  • nix build .#compactc succeeds. The unit suite is a separate step, nix develop --command ./compiler/go: packages.compactc defines a checkPhase but never sets doCheck = true, so nix build alone does not run it.
  • Without the flag, output is unchanged: contract-info.json for the bulletin-board example is byte-identical to a pristine build of the base commit.
  • With the flag, a 950-line real hook (a contract-IR emitter for a Rust SDK) runs over five test contracts (including emit events, serialize, and a cross-contract call) and produces identical output to an equivalent in-tree pass.

Status

Draft for review in this fork before proposing upstream. The companion draft, #16, adds an in-tree flag-gated analyzed-ir.sexp artifact built on the same analyzed IR. The two are alternative transports for the same consumer, and the upstream discussion can weigh them together.

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

github-actions Bot commented Aug 14, 2026 •

Copy link
Copy Markdown

Plugin Test Summary

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

Results for commit e706c06.

♻️ This comment has been updated with latest results.

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

Copy link
Copy Markdown

Compactc E2E Test Summary

3 106 tests   3 106 ✅  22m 26s ⏱️
    1 suites      0 💤
    1 files        0 ❌

Results for commit e706c06.

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