From 6f4c4bba85ad3dd6baeb8939aaed38f6f414f926 Mon Sep 17 00:00:00 2001 From: Rodrigo Quelhas Date: Mon, 17 Aug 2026 10:16:07 +0100 Subject: [PATCH 1/2] Add --analyzed-ir-hook to hand the datum to a consumer's Scheme file `--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 --- compiler/compactc.ss | 25 +++++++++++++++++++ compiler/config-params.ss | 3 +++ compiler/passes.ss | 12 ++++++--- compiler/save-analyzed-ir-passes.ss | 3 ++- .../save-analyzed-ir.ss | 17 +++++++++---- 5 files changed, 50 insertions(+), 10 deletions(-) diff --git a/compiler/compactc.ss b/compiler/compactc.ss index fbc5b0f4b..71dbf50b4 100755 --- a/compiler/compactc.ss +++ b/compiler/compactc.ss @@ -91,8 +91,29 @@ The following flags, if present, affect the compiler's behavior as follows: execute circuits from this file with an interpreter. The file is one S-expression datum. The format follows the intermediate language, so it can change between compiler versions. + + --analyzed-ir-hook loads the Scheme file at and calls + the procedure it defines under the name `hook` with the same datum + --analyzed-ir writes, and with the target directory as a string. The + compiler writes no analyzed-ir.sexp in this mode: the hook decides what + to do with the datum, so a consumer can render it in the encoding its + own tools want. The datum is ordinary data, so the file needs no import + from the compiler. ")) +;; The loaded file sees only the bindings the boot file already provides, so a +;; hook that takes the datum needs no library from the compiler and the binary +;; keeps its libraries pruned. +(define (load-hook pathname) + (define (complain) + (external-errorf "the file loaded by --analyzed-ir-hook must define a procedure named hook")) + (check-pathname pathname) + (load pathname) + (unless (top-level-bound? 'hook (interaction-environment)) (complain)) + (let ([hook (eval 'hook (interaction-environment))]) + (unless (procedure? hook) (complain)) + hook)) + (usage " ... ") (parameterize ([reset-handler abort]) @@ -110,6 +131,7 @@ The following flags, if present, affect the compiler's behavior as follows: [(--trace-search)] [(--trace-passes)] [(--analyzed-ir)] + [(--analyzed-ir-hook) (string analyzed-ir-hook-pathname)] [(--feature-zkir-v3)]) (string source-pathname) (string target-directory-pathname)) @@ -120,6 +142,9 @@ The following flags, if present, affect the compiler's behavior as follows: [no-communications-commitment ?--no-communications-commitment] [feature-zkir-v3 ?--feature-zkir-v3] [write-analyzed-ir ?--analyzed-ir] + [analyzed-ir-hook + (and ?--analyzed-ir-hook + (load-hook analyzed-ir-hook-pathname))] [compact-path (if ?--compact-path (split-search-path search-list) (compact-path))] [trace-search ?--trace-search]) (when source-root (register-source-root! source-root)) diff --git a/compiler/config-params.ss b/compiler/config-params.ss index cc05b0f71..f34c99f0a 100644 --- a/compiler/config-params.ss +++ b/compiler/config-params.ss @@ -43,4 +43,7 @@ (export-parameter feature-zkir-v3 #f) (export-parameter write-analyzed-ir #f) + + ;; The procedure --analyzed-ir-hook loaded, or #f to write the file. + (export-parameter analyzed-ir-hook #f) ) diff --git a/compiler/passes.ss b/compiler/passes.ss index d4aabd4fe..8ba1937d7 100644 --- a/compiler/passes.ss +++ b/compiler/passes.ss @@ -146,10 +146,14 @@ (with-target-ports '((contract-info.json . "compiler/contract-info.json")) (run-passes save-contract-info-passes analyzed-ir proof-circuit-name*)) - (when (write-analyzed-ir) - (with-target-ports - '((analyzed-ir.sexp . "compiler/analyzed-ir.sexp")) - (run-passes save-analyzed-ir-passes analyzed-ir proof-circuit-name*))) + (cond + [(analyzed-ir-hook) + ;; The hook takes the datum, so open no file for it. + (run-passes save-analyzed-ir-passes analyzed-ir proof-circuit-name*)] + [(write-analyzed-ir) + (with-target-ports + '((analyzed-ir.sexp . "compiler/analyzed-ir.sexp")) + (run-passes save-analyzed-ir-passes analyzed-ir proof-circuit-name*))]) (with-target-ports (map (lambda (sym) (cons sym (format "zkir/~a.zkir" sym))) proof-circuit-name*) diff --git a/compiler/save-analyzed-ir-passes.ss b/compiler/save-analyzed-ir-passes.ss index 72fbbcd2e..0fad5b251 100644 --- a/compiler/save-analyzed-ir-passes.ss +++ b/compiler/save-analyzed-ir-passes.ss @@ -25,7 +25,8 @@ (compiler-version) (language-version) (runtime-version) - (pass-helpers)) + (pass-helpers) + (config-params)) (include "save-analyzed-ir-passes/extract-analyzed-ir.ss") diff --git a/compiler/save-analyzed-ir-passes/save-analyzed-ir.ss b/compiler/save-analyzed-ir-passes/save-analyzed-ir.ss index cd80359a3..ae1208b3a 100644 --- a/compiler/save-analyzed-ir-passes/save-analyzed-ir.ss +++ b/compiler/save-analyzed-ir-passes/save-analyzed-ir.ss @@ -16,9 +16,16 @@ #!chezscheme (define (save-analyzed-ir ir proof-circuit-name*) - (let ([op (get-target-port 'analyzed-ir.sexp)]) - ;; Parentheses only: brackets are a Chez pretty-printing style, and a - ;; non-Scheme reader should not need to treat them as paren synonyms. - (parameterize ([print-brackets #f]) - (pretty-print (extract-analyzed-ir ir proof-circuit-name*) op))) + (let ([sexp (extract-analyzed-ir ir proof-circuit-name*)]) + (cond + ;; The hook takes the datum and the directory as arguments, so it needs + ;; no import from the compiler and runs in a binary built without + ;; visible libraries. + [(analyzed-ir-hook) => (lambda (hook) (hook sexp (target-directory)))] + [else + (let ([op (get-target-port 'analyzed-ir.sexp)]) + ;; Parentheses only: brackets are a Chez pretty-printing style, and a + ;; non-Scheme reader should not need to treat them as paren synonyms. + (parameterize ([print-brackets #f]) + (pretty-print sexp op)))])) ir) From c97dd2543e4b378b2de50345f54a106434f4eb3b Mon Sep 17 00:00:00 2001 From: Rodrigo Quelhas Date: Mon, 17 Aug 2026 14:06:05 +0100 Subject: [PATCH 2/2] Report hook-file errors as user errors, and run the hook last 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 --- compiler/compactc.ss | 16 ++++++++++------ compiler/passes.ss | 18 ++++++++++-------- tests-e2e/src/resources/compiler_man_page.txt | 4 ++-- 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/compiler/compactc.ss b/compiler/compactc.ss index 71dbf50b4..99549a839 100755 --- a/compiler/compactc.ss +++ b/compiler/compactc.ss @@ -95,8 +95,8 @@ The following flags, if present, affect the compiler's behavior as follows: --analyzed-ir-hook loads the Scheme file at and calls the procedure it defines under the name `hook` with the same datum --analyzed-ir writes, and with the target directory as a string. The - compiler writes no analyzed-ir.sexp in this mode: the hook decides what - to do with the datum, so a consumer can render it in the encoding its + compiler writes no analyzed-ir.sexp in this mode. The hook runs after + the compile succeeds, and it decides what to do with the datum, so a consumer can render it in the encoding its own tools want. The datum is ordinary data, so the file needs no import from the compiler. ")) @@ -108,6 +108,8 @@ The following flags, if present, affect the compiler's behavior as follows: (define (complain) (external-errorf "the file loaded by --analyzed-ir-hook must define a procedure named hook")) (check-pathname pathname) + (unless (file-exists? pathname) + (external-errorf "the file named by --analyzed-ir-hook does not exist: ~a" pathname)) (load pathname) (unless (top-level-bound? 'hook (interaction-environment)) (complain)) (let ([hook (eval 'hook (interaction-environment))]) @@ -142,14 +144,16 @@ The following flags, if present, affect the compiler's behavior as follows: [no-communications-commitment ?--no-communications-commitment] [feature-zkir-v3 ?--feature-zkir-v3] [write-analyzed-ir ?--analyzed-ir] - [analyzed-ir-hook - (and ?--analyzed-ir-hook - (load-hook analyzed-ir-hook-pathname))] [compact-path (if ?--compact-path (split-search-path search-list) (compact-path))] [trace-search ?--trace-search]) (when source-root (register-source-root! source-root)) (handle-exceptions ?--vscode - (generate-everything source-pathname target-directory-pathname)))] + ;; Load inside the handler: a missing or malformed hook file is + ;; user input, and its error belongs in the same format as the rest. + (parameterize ([analyzed-ir-hook + (and ?--analyzed-ir-hook + (load-hook analyzed-ir-hook-pathname))]) + (generate-everything source-pathname target-directory-pathname))))] [((flags [(--help) $ (begin (print-help) (exit))] [(--version) $ (begin (print-compiler-version) (exit))] [(--language-version) $ (begin (print-language-version) (exit))] diff --git a/compiler/passes.ss b/compiler/passes.ss index 8ba1937d7..a07458584 100644 --- a/compiler/passes.ss +++ b/compiler/passes.ss @@ -146,14 +146,10 @@ (with-target-ports '((contract-info.json . "compiler/contract-info.json")) (run-passes save-contract-info-passes analyzed-ir proof-circuit-name*)) - (cond - [(analyzed-ir-hook) - ;; The hook takes the datum, so open no file for it. - (run-passes save-analyzed-ir-passes analyzed-ir proof-circuit-name*)] - [(write-analyzed-ir) - (with-target-ports - '((analyzed-ir.sexp . "compiler/analyzed-ir.sexp")) - (run-passes save-analyzed-ir-passes analyzed-ir proof-circuit-name*))]) + (when (and (write-analyzed-ir) (not (analyzed-ir-hook))) + (with-target-ports + '((analyzed-ir.sexp . "compiler/analyzed-ir.sexp")) + (run-passes save-analyzed-ir-passes analyzed-ir proof-circuit-name*))) (with-target-ports (map (lambda (sym) (cons sym (format "zkir/~a.zkir" sym))) proof-circuit-name*) @@ -202,6 +198,12 @@ (run-passes manifest-passes circuit-ir output-directory-pathname output-subdirectories))) + ;; Last, so a failure anywhere above leaves no hook + ;; output behind: the compiler does not track the + ;; files a hook writes and cannot roll them back. + (when (analyzed-ir-hook) + ;; The hook takes the datum, so open no file for it. + (run-passes save-analyzed-ir-passes analyzed-ir proof-circuit-name*)) (when final-pass (internal-errorf 'generate-everything "never encountered final pass ~s" final-pass)))])))))))])) (define-pass extract-circuit-names : Lflattened (ir) -> * (ls) diff --git a/tests-e2e/src/resources/compiler_man_page.txt b/tests-e2e/src/resources/compiler_man_page.txt index 6ac34a4e4..ba71373eb 100644 --- a/tests-e2e/src/resources/compiler_man_page.txt +++ b/tests-e2e/src/resources/compiler_man_page.txt @@ -69,7 +69,7 @@ The following flags, if present, affect the compiler's behavior as follows: --analyzed-ir-hook loads the Scheme file at and calls the procedure it defines under the name `hook` with the same datum --analyzed-ir writes, and with the target directory as a string. The - compiler writes no analyzed-ir.sexp in this mode: the hook decides what - to do with the datum, so a consumer can render it in the encoding its + compiler writes no analyzed-ir.sexp in this mode. The hook runs after + the compile succeeds, and it decides what to do with the datum, so a consumer can render it in the encoding its own tools want. The datum is ordinary data, so the file needs no import from the compiler.