Commit d399c827

d399c8278022Merged

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

Description

feat(codegen,runtime): integer overflow detection raises RangeError

Phase 1 of the BIGINT.md plan: overflow check on every int + / - / * op via _builtin*_overflow intrinsics, raising RangeError on overflow rather than silently wrapping at int64 boundaries. Keeps locals at native mrb_int width (option β -- "preserve spinel's fast-AOT pitch"), so the runtime cost is one well-predicted branch per arithmetic op in the hot path.

The helpers (sp_int_add / sp_int_sub / sp_int_mul / sp_int_neg) are defined as GCC statement-expression macros in sp_runtime.h rather than static inline functions. The inline-function variant (even with attribute((always_inline))) produced a wrong optcarrot checksum (4096 instead of 59662) despite no overflow ever firing -- the optimizer's view of the function-call-shaped expression differs from the bare arithmetic in a way the macro form bypasses by keeping every operand in its surrounding expression context. Macros also let __builtin_add_overflow's constant arms fold cleanly at call sites with literal operands.

Codegen wraps the int+int / int-int / int*int / -int arms in compile_call_expr's + / - / * dispatch with the helper calls. Float and mixed-type ops keep the bare C operators (no overflow trap on float, and int/float mixed is handled by C's implicit conversion).

Performance: optcarrot fps drops from ~670 to ~634 (about 5.5% regression) on a single -O3 -flto build. Bootstrap converges, 616 tests pass, optcarrot checksum 59662.

The promote-to-bigint half of BIGINT.md is deferred: the user's preferred shape is raise-by-default + opt-in promote via a spinel flag and via pattern detection of obviously-bigint code. Both are followups; this commit installs the raise mechanism.

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

Details

Commit
d399c8278022
Parent
7b74e1972df2

lib/sp_runtime.hmodifiedView file

@@ -103,6 +103,31 @@
103103 if ((r != 0) && ((r ^ b) < 0)) r += b;
104104 return r;
105105 }
106+/* Overflow-checked integer arithmetic (BIGINT.md option β: raise on
107+ overflow, keep locals at native mrb_int width). __builtin_*_overflow
108+ compiles to a single ALU op + conditional branch on every supported
109+ platform; the branch is well-predicted in hot loops so the cost is
110+ close to one extra instruction per op.
111+
112+ Implemented as GCC statement-expression macros rather than static
113+ inline functions: the inline variant produces wrong optcarrot
114+ output even with __attribute__((always_inline)) (observed: checksum
115+ diverges from 59662 to 4096, with no overflow ever firing). I
116+ suspect a UB-assumption interaction between the optimizer and the
117+ function-call-shaped expression that the macro form bypasses by
118+ keeping every operand in its surrounding expression context. */
119+#define sp_int_add(a, b) ({ mrb_int _sp_a = (a), _sp_b = (b), _sp_r; \
120+ if (__builtin_add_overflow(_sp_a, _sp_b, &_sp_r)) sp_raise_cls("RangeError", "integer overflow in +"); \
121+ _sp_r; })
122+#define sp_int_sub(a, b) ({ mrb_int _sp_a = (a), _sp_b = (b), _sp_r; \
123+ if (__builtin_sub_overflow(_sp_a, _sp_b, &_sp_r)) sp_raise_cls("RangeError", "integer overflow in -"); \
124+ _sp_r; })
125+#define sp_int_mul(a, b) ({ mrb_int _sp_a = (a), _sp_b = (b), _sp_r; \
126+ if (__builtin_mul_overflow(_sp_a, _sp_b, &_sp_r)) sp_raise_cls("RangeError", "integer overflow in *"); \
127+ _sp_r; })
128+#define sp_int_neg(a) ({ mrb_int _sp_a = (a), _sp_r; \
129+ if (__builtin_sub_overflow((mrb_int)0, _sp_a, &_sp_r)) sp_raise_cls("RangeError", "integer overflow in -@"); \
130+ _sp_r; })
106131
107132 static mrb_int sp_gcd(mrb_int a,mrb_int b){if(a<0)a=-a;if(b<0)b=-b;while(b){mrb_int t=b;b=a%b;a=t;}return a;}
108133 static mrb_int sp_lcm(mrb_int a,mrb_int b){if(a==0||b==0)return 0;mrb_int g=sp_gcd(a,b);if(a<0)a=-a;if(b<0)b=-b;return (a/g)*b;}