Commit 67cc1950

67cc1950a9eaMerged

Yusuke Endoh <[email protected]> · 6 months ago · 4 files · +1 added · ~3 modified

Description

transplant: analyze-perf state on top of master

Squash of experiment/analyze-perf onto origin/master via file overlay. Master's 19 new commits added bug fixes / features that haven't been ported to my split analyze.rb / codegen.rb yet — 16 new tests fail. Following commits port them.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
67cc1950a9ea
Parent
0adae7775bbf

MakefilemodifiedView file

@@ -120,11 +120,12 @@
120120 PRISM_LIB = build/libprism.a
121121
122122 CODEGEN_STAMP := build/stamps/spinel_codegen.rb.stamp
123+ANALYZE_STAMP := build/stamps/spinel_analyze.rb.stamp
123124 PARSE_STAMP := build/stamps/spinel_parse.c.stamp
124125
125126 .PHONY: all parse bootstrap codegen test retest clean-test-results regen-expected bench optcarrot clean install uninstall deps
126127
127-all: parse regexp spinel_codegen$(EXE)
128+all: parse regexp spinel_analyze$(EXE) spinel_codegen$(EXE)
128129
129130 # ---- Dependencies ----
130131 # Clone Prism into vendor/prism at the pinned version. Run this once
@@ -210,32 +211,52 @@
210211 regexp: $(SP_RT_LIB)
211212
212213 # ---- Build the codegen binary (fast path) ----
213-# `make spinel_codegen` (or the alias `make codegen`) compiles
214-# spinel_codegen.rb once via CRuby and links the result. This is
215-# enough to use the binary; for the full self-hosting fixed-point
216-# check (gen2.c == gen3.c), use `make bootstrap`.
214+# Build both binaries via CRuby (the bootstrap "round 1"). Each
215+# spinel_<phase>.rb is its own ~20K-line source; we compile each
216+# through the same parse → analyze → codegen → cc pipeline. Use
217+# `make bootstrap` for the full self-hosting fixed-point check.
218+
219+codegen: spinel_analyze$(EXE) spinel_codegen$(EXE)
217220
218-codegen: spinel_codegen$(EXE)
221+spinel_analyze$(EXE): $(ANALYZE_STAMP) spinel_parse$(EXE)
222+ ./spinel_parse$(EXE) spinel_analyze.rb build/analyze.ast
223+ ruby spinel_analyze.rb build/analyze.ast build/analyze.ir
224+ ruby spinel_codegen.rb build/analyze.ast build/analyze.ir build/analyze1.c
225+ $(CC) $(BOOTSTRAP_CFLAGS) -Ilib build/analyze1.c $(LDFLAGS) -lm -o spinel_analyze$(EXE)
219226
220227 spinel_codegen$(EXE): $(CODEGEN_STAMP) spinel_parse$(EXE)
221228 ./spinel_parse$(EXE) spinel_codegen.rb build/codegen.ast
222- ruby spinel_codegen.rb build/codegen.ast build/gen1.c
223- $(CC) $(BOOTSTRAP_CFLAGS) -Ilib build/gen1.c $(LDFLAGS) -lm -o spinel_codegen$(EXE)
229+ ruby spinel_analyze.rb build/codegen.ast build/codegen.ir
230+ ruby spinel_codegen.rb build/codegen.ast build/codegen.ir build/codegen1.c
231+ $(CC) $(BOOTSTRAP_CFLAGS) -Ilib build/codegen1.c $(LDFLAGS) -lm -o spinel_codegen$(EXE)
224232
225-# ---- Self-hosting verification (slow path) ----
226-# Re-runs the binary on its own AST to produce gen2.c, compiles bin2,
227-# re-runs bin2 to produce gen3.c, asserts gen2.c == gen3.c (the
228-# self-hosting fixed point). On success, replaces spinel_codegen with
229-# the verified bin2.
233+# ---- Self-hosting verification ----
234+# After CRuby builds spinel_{analyze,codegen}, run them on each
235+# source file to produce a "round 2" pair of binaries. Then run
236+# bin2_{analyze,codegen} on each source again to produce "round 3"
237+# outputs and assert byte equality. The fixpoint covers both the
238+# analysis IR and the emitted C, so any drift in either phase is
239+# caught.
230240
231-bootstrap: spinel_codegen$(EXE)
232- @echo "=== Bootstrap: gen2 (via spinel_codegen) ==="
233- ./spinel_codegen$(EXE) build/codegen.ast build/gen2.c
234- $(CC) $(BOOTSTRAP_CFLAGS) -Ilib build/gen2.c $(LDFLAGS) -lm -o build/bin2$(EXE)
235- @echo "=== Bootstrap: gen3 (via bin2) - verify ==="
236- ./build/bin2$(EXE) build/codegen.ast build/gen3.c
237- @diff build/gen2.c build/gen3.c > /dev/null && echo "gen2.c == gen3.c (bootstrap OK)" || (echo "BOOTSTRAP FAILED: gen2.c != gen3.c" && exit 1)
238- cp build/bin2$(EXE) spinel_codegen$(EXE)
241+bootstrap: spinel_analyze$(EXE) spinel_codegen$(EXE)
242+ @echo "=== Bootstrap round 2: analyze + codegen with spinel_{analyze,codegen} ==="
243+ ./spinel_analyze$(EXE) build/analyze.ast build/analyze2.ir
244+ ./spinel_codegen$(EXE) build/analyze.ast build/analyze2.ir build/analyze2.c
245+ $(CC) $(BOOTSTRAP_CFLAGS) -Ilib build/analyze2.c $(LDFLAGS) -lm -o build/bin2_analyze$(EXE)
246+ ./spinel_analyze$(EXE) build/codegen.ast build/codegen2.ir
247+ ./spinel_codegen$(EXE) build/codegen.ast build/codegen2.ir build/codegen2.c
248+ $(CC) $(BOOTSTRAP_CFLAGS) -Ilib build/codegen2.c $(LDFLAGS) -lm -o build/bin2_codegen$(EXE)
249+ @echo "=== Bootstrap round 3: analyze + codegen with bin2_* — verify ==="
250+ ./build/bin2_analyze$(EXE) build/analyze.ast build/analyze3.ir
251+ ./build/bin2_codegen$(EXE) build/analyze.ast build/analyze3.ir build/analyze3.c
252+ ./build/bin2_analyze$(EXE) build/codegen.ast build/codegen3.ir
253+ ./build/bin2_codegen$(EXE) build/codegen.ast build/codegen3.ir build/codegen3.c
254+ @diff build/analyze2.ir build/analyze3.ir > /dev/null && echo "analyze.rb: IR fixpoint OK" || (echo "BOOTSTRAP FAILED: analyze.rb IR diverged" && exit 1)
255+ @diff build/analyze2.c build/analyze3.c > /dev/null && echo "analyze.rb: C fixpoint OK" || (echo "BOOTSTRAP FAILED: analyze.rb C diverged" && exit 1)
256+ @diff build/codegen2.ir build/codegen3.ir > /dev/null && echo "codegen.rb: IR fixpoint OK" || (echo "BOOTSTRAP FAILED: codegen.rb IR diverged" && exit 1)
257+ @diff build/codegen2.c build/codegen3.c > /dev/null && echo "codegen.rb: C fixpoint OK" || (echo "BOOTSTRAP FAILED: codegen.rb C diverged" && exit 1)
258+ cp build/bin2_analyze$(EXE) spinel_analyze$(EXE)
259+ cp build/bin2_codegen$(EXE) spinel_codegen$(EXE)
239260
240261 # ---- Test ----
241262
@@ -267,12 +288,13 @@
267288 @$(MAKE) --no-print-directory test
268289
269290 # The .ok target is the test's stamp; mtime tracking gives per-test
270-# caching for free. Order-only spinel_parse$(EXE) / spinel_codegen$(EXE)
271-# stop a bootstrap relink from invalidating every test.
272-build/test-results/%.ok: test/%.rb $(SP_RT_LIB) $(CODEGEN_STAMP) $(PARSE_STAMP) | spinel_parse$(EXE) spinel_codegen$(EXE)
291+# caching for free. Order-only spinel_parse$(EXE) / spinel_analyze$(EXE)
292+# / spinel_codegen$(EXE) stop a bootstrap relink from invalidating every test.
293+build/test-results/%.ok: test/%.rb $(SP_RT_LIB) $(CODEGEN_STAMP) $(ANALYZE_STAMP) $(PARSE_STAMP) | spinel_parse$(EXE) spinel_analyze$(EXE) spinel_codegen$(EXE)
273294 @mkdir -p build/test-results
274295 @tmpdir=$$(mktemp -d /tmp/spinel-test.XXXXXX); \
275296 ast=$$tmpdir/test.ast; \
297+ ir=$$tmpdir/test.ir; \
276298 cfile=$$tmpdir/test.c; \
277299 bin=$$tmpdir/test_bin$(EXE); \
278300 exp=$$tmpdir/expected; \
@@ -281,7 +303,8 @@
281303 if [ -f "$<.args" ]; then args=$$(cat "$<.args"); fi; \
282304 rm -f "[email protected]"; \
283305 ./spinel_parse$(EXE) "$<" "$$ast" 2>/dev/null && \
284- ./spinel_codegen$(EXE) "$$ast" "$$cfile" 2>/dev/null && \
306+ ./spinel_analyze$(EXE) "$$ast" "$$ir" 2>/dev/null && \
307+ ./spinel_codegen$(EXE) "$$ast" "$$ir" "$$cfile" 2>/dev/null && \
285308 $(CC) $(CFLAGS) -Werror $(SEC_FLAGS) -Ilib -c "$$cfile" -o "$$cfile.o" 2>/dev/null && \
286309 $(CC) $(CFLAGS) "$$cfile.o" $(SP_RT_LIB) $(LDFLAGS) -lm $(GC_FLAGS) -o "$$bin" 2>/dev/null; \
287310 if [ $$? -eq 0 ]; then \
@@ -334,7 +357,7 @@
334357 LC_ALL=C sed 's/\r$$//' [email protected] > $@; \
335358 rm -f [email protected]
336359
337-bench: spinel_parse$(EXE) $(SP_RT_LIB) spinel_codegen$(EXE)
360+bench: spinel_parse$(EXE) $(SP_RT_LIB) spinel_analyze$(EXE) spinel_codegen$(EXE)
338361 @if [ -z "$(TIMEOUT_BIN)" ]; then echo "Note: no 'timeout' command found; running without time limits."; fi
339362 @total=$$(ls benchmark/*.rb | wc -l); \
340363 if [ -t 1 ]; then tty=1; else tty=0; fi; \
@@ -344,7 +367,8 @@
344367 bn=$$(basename "$$f" .rb); \
345368 if [ "$$tty" = 1 ]; then printf '\r\033[K [%d/%d] %s' "$$i" "$$total" "$$bn"; fi; \
346369 $(TIMEOUT10) ./spinel_parse$(EXE) "$$f" /tmp/_sp_b.ast 2>/dev/null && \
347- $(TIMEOUT10) ./spinel_codegen$(EXE) /tmp/_sp_b.ast /tmp/_sp_b.c 2>/dev/null && \
370+ $(TIMEOUT10) ./spinel_analyze$(EXE) /tmp/_sp_b.ast /tmp/_sp_b.ir 2>/dev/null && \
371+ $(TIMEOUT10) ./spinel_codegen$(EXE) /tmp/_sp_b.ast /tmp/_sp_b.ir /tmp/_sp_b.c 2>/dev/null && \
348372 $(CC) $(CFLAGS) -Werror $(SEC_FLAGS) -Ilib -c /tmp/_sp_b.c -o /tmp/_sp_b.c.o 2>/dev/null && \
349373 $(CC) $(CFLAGS) /tmp/_sp_b.c.o $(SP_RT_LIB) $(LDFLAGS) -lm $(GC_FLAGS) -o /tmp/_sp_b_bin$(EXE) 2>/dev/null; \
350374 if [ $$? -eq 0 ]; then \
@@ -376,7 +400,7 @@
376400 fi; \
377401 done; \
378402 if [ "$$tty" = 1 ]; then printf '\r\033[K'; fi; \
379- rm -f /tmp/_sp_b.ast /tmp/_sp_b.c /tmp/_sp_b.c.o /tmp/_sp_b_bin$(EXE) /tmp/_sp_b_exp /tmp/_sp_b_act /tmp/_sp_b_exp.n /tmp/_sp_b_act.n; \
403+ rm -f /tmp/_sp_b.ast /tmp/_sp_b.ir /tmp/_sp_b.c /tmp/_sp_b.c.o /tmp/_sp_b_bin$(EXE) /tmp/_sp_b_exp /tmp/_sp_b_act /tmp/_sp_b_exp.n /tmp/_sp_b_act.n; \
380404 echo "Benchmarks: $$pass pass, $$fail fail, $$err error, $$skip skip"; \
381405 if [ $$fail -ne 0 ] || [ $$err -ne 0 ]; then exit 1; fi
382406
@@ -393,7 +417,7 @@
393417 OPTCARROT_REPO := https://github.com/mame/optcarrot.git
394418 OPTCARROT_BRANCH := experiment/spinel
395419
396-optcarrot: spinel_parse$(EXE) $(SP_RT_LIB) spinel_codegen$(EXE)
420+optcarrot: spinel_parse$(EXE) $(SP_RT_LIB) spinel_analyze$(EXE) spinel_codegen$(EXE)
397421 @if [ ! -d $(OPTCARROT_DIR) ]; then \
398422 git clone --depth=1 --branch=$(OPTCARROT_BRANCH) $(OPTCARROT_REPO) $(OPTCARROT_DIR); \
399423 fi
@@ -417,7 +441,9 @@
417441 install -d $(SPNLDIR)/lib
418442 install -m 755 spinel $(SPNLDIR)/
419443 install -m 755 spinel_parse$(EXE) $(SPNLDIR)/
444+ install -m 755 spinel_analyze$(EXE) $(SPNLDIR)/
420445 install -m 755 spinel_codegen$(EXE) $(SPNLDIR)/
446+ install -m 644 spinel_analyze.rb $(SPNLDIR)/
421447 install -m 644 spinel_codegen.rb $(SPNLDIR)/
422448 install -m 644 lib/libspinel_rt.a $(SPNLDIR)/lib/
423449 install -m 644 lib/sp_runtime.h $(SPNLDIR)/lib/
@@ -433,4 +459,4 @@
433459
434460 clean:
435461 rm -rf build/
436- rm -f spinel_parse$(EXE) spinel_codegen$(EXE)
462+ rm -f spinel_parse$(EXE) spinel_analyze$(EXE) spinel_codegen$(EXE)