Commit 656e4490

656e4490896eMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 8 files · +2 added · ~6 modified

Description

Auto-promote block-loop accumulators to bigint in promote mode

--int-overflow=promote only avoided overflow when the analyzer could prove a local grows without bound, and that detection (infer_bigint_loop_locals) scanned while/until bodies only. A factorial written the idiomatic way -- f = 1; n.times { f *= i } -- therefore still overflowed and raised, even though promote mode is exactly the mode asking us not to.

Extend the same self-referential-multiply detection to block-iteration loops (times/each/upto/downto/step/loop/each_with_index), gated on a new g_promote_mode flag set from main.c. The default and wrap paths stay while-only, so the gates and the wrap-pinned optcarrot see no change (a block-loop bigint widening would wreck optcarrot's hot .times { x = x * y } inner loops -- the original reason the detection was while-only).

Also fix a latent bug this exposed: emit_op_assign's captured-cell path was int-only, so n.times { h *= 3 } with a now-bigint h ran sp_int_mul on the bigint cell and silently truncated. Route bigint cells through the bigint helpers.

Wire the Makefile promote test mode to actually pass --int-overflow=promote to the front-end and -DSP_INT_OVERFLOW_MODE_PROMOTE to cc (it previously only filtered the test list), and add a promote-only regression test (filtered out of raise/wrap runs, where it would correctly raise).

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
656e4490896e
Parent
7b31ed9d93d5

MakefilemodifiedView file

@@ -268,6 +268,14 @@
268268 # --int-overflow=promote the same code auto-promotes and output diverges.
269269 ifeq ($(SPINEL_INT_OVERFLOW),promote)
270270 TESTS := $(filter-out test/int_overflow_raises.rb,$(TESTS))
271+# Drive the spinel front-end and the C compile in promote mode so the test
272+# rule actually exercises the auto-promotion path end to end.
273+SP_OV_FLAG := --int-overflow=promote
274+SP_OV_DEFINE := -DSP_INT_OVERFLOW_MODE_PROMOTE
275+else
276+# `promote_*` tests overflow on purpose and only have defined output under
277+# --int-overflow=promote; in raise/wrap mode they would (correctly) raise.
278+TESTS := $(filter-out test/promote_%.rb,$(TESTS))
271279 endif
272280 # sp_net is POSIX-only; on Windows the TU compiles to stubs and the smoke
273281 # test's output diverges. Skip it there.
@@ -402,8 +410,8 @@
402410 args=""; \
403411 if [ -f "$<.args" ]; then args=$$(cat "$<.args"); fi; \
404412 rm -f "[email protected]"; \
405- $(SPINEL) "$<" -c --no-line-map -o "$$cfile" 2>/dev/null && \
406- $(CC) $(CFLAGS) -Werror $(TEST_WARN_SUPPRESS) $(SEC_FLAGS) -Ilib "$$cfile" $(SP_RT_LIB) $(LDFLAGS) -lm $(GC_FLAGS) -o "$$bin" 2>/dev/null; \
413+ $(SPINEL) "$<" $(SP_OV_FLAG) -c --no-line-map -o "$$cfile" 2>/dev/null && \
414+ $(CC) $(CFLAGS) $(SP_OV_DEFINE) -Werror $(TEST_WARN_SUPPRESS) $(SEC_FLAGS) -Ilib "$$cfile" $(SP_RT_LIB) $(LDFLAGS) -lm $(GC_FLAGS) -o "$$bin" 2>/dev/null; \
407415 if [ $$? -eq 0 ]; then \
408416 if [ -f "$<.expected" ]; then \
409417 LC_ALL=C sed 's/\r$$//' "$<.expected" >"$$exp.n"; \