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
if ((r != 0) && ((r ^ b) < 0)) r += b; return r;}/* Overflow-checked integer arithmetic (BIGINT.md option β: raise on overflow, keep locals at native mrb_int width). __builtin_*_overflow compiles to a single ALU op + conditional branch on every supported platform; the branch is well-predicted in hot loops so the cost is close to one extra instruction per op. Implemented as GCC statement-expression macros rather than static inline functions: the inline variant produces wrong optcarrot output even with __attribute__((always_inline)) (observed: checksum diverges from 59662 to 4096, with no overflow ever firing). I suspect a UB-assumption interaction between the optimizer and the function-call-shaped expression that the macro form bypasses by keeping every operand in its surrounding expression context. */#define sp_int_add(a, b) ({ mrb_int _sp_a = (a), _sp_b = (b), _sp_r; \ if (__builtin_add_overflow(_sp_a, _sp_b, &_sp_r)) sp_raise_cls("RangeError", "integer overflow in +"); \ _sp_r; })#define sp_int_sub(a, b) ({ mrb_int _sp_a = (a), _sp_b = (b), _sp_r; \ if (__builtin_sub_overflow(_sp_a, _sp_b, &_sp_r)) sp_raise_cls("RangeError", "integer overflow in -"); \ _sp_r; })#define sp_int_mul(a, b) ({ mrb_int _sp_a = (a), _sp_b = (b), _sp_r; \ if (__builtin_mul_overflow(_sp_a, _sp_b, &_sp_r)) sp_raise_cls("RangeError", "integer overflow in *"); \ _sp_r; })#define sp_int_neg(a) ({ mrb_int _sp_a = (a), _sp_r; \ if (__builtin_sub_overflow((mrb_int)0, _sp_a, &_sp_r)) sp_raise_cls("RangeError", "integer overflow in -@"); \ _sp_r; })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;}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;}spinel_codegen.rbmodifiedView file
Loading diff…
test/int_overflow_raises.rbaddedView file
Loading diff…
test/int_overflow_raises.rb.expectedaddedView file
Loading diff…