Commit 1ae7feaf
1ae7feaf16ceMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · +1 added
Description
docs: plan for --int-overflow=promote full coverage of test/bench/optcarrot
Surveys current state at HEAD (bc7e1a1): promote test 557/52/22, bench 36/7/14, optcarrot C compile fails with 72 errors (80%+ are bit-op on sp_Bigint *).
Buckets observed:
- A — bit operators (&, |, ^, <<, >>, ~) on bigint operands. Largest blocker, dominates optcarrot. Needs runtime helpers + codegen dispatch.
- B — builtin container helpers (sp_IntArray_push/get, sp_FloatArray set, spHash_) with bigint args/keys/values.
- C — block param bigint flowing into IntArray index sites.
- D — typed user method / FFI call sites with poly arg into bigint param (mirror of #639 IVW unbox, on the call boundary).
- E — bigint slot init / return from poly value.
- F — bigint -> string conversion leakage (puts, interpolation, <<).
- G — individual test failures unrelated to the buckets above.
7 phases, ~8-16 hours focused work. Phase 1 (bit-ops) unblocks optcarrot. Each phase keeps default mode (test 631/0/0, optcarrot checksum 59662 under --int-overflow=wrap) green.
Refs the existing promote-mode memory and inline-vs-macro pitfall notes.
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- 1ae7feaf16ce
- Parent
- c8272dacd68e
docs/promote-mode-plan.mdaddedView file
1+
# `--int-overflow=promote` 完全動作計画2+
3+
## ゴール4+
5+
`SPINEL_INT_OVERFLOW=promote` (`-DSP_INT_OVERFLOW_MODE_PROMOTE`) で:6+
- `make test` 全 pass7+
- `make bench` 全 pass8+
- `make optcarrot` PASS + checksum 59662 (Makefile から `--int-overflow=wrap` pin 除去)9+
10+
## 現状 (master HEAD, commit `bc7e1a1`)11+
12+
| Target | promote 結果 | 主要 error |13+
|---|---|---|14+
| `make test` | 557 / 52 fail / 22 error | 23 errors の多くは bit-ops / typed-call-args poly→bigint |15+
| `make bench` | 36 / 7 fail / 14 error | bit-ops, IntArray index with bigint, FFI float arg |16+
| `make optcarrot` (--int-overflow=promote) | C compile failed | 72 errors、 80%以上が bit-op (`&`, `\|`, `^`, `<<`, `>>`, `~`) |17+
18+
## Error バケット分類19+
20+
### Bucket A: Bit operators on `sp_Bigint *` (LARGEST — optcarrot の 80%)21+
22+
```23+
error: invalid operands to binary & (have 'sp_Bigint *' and 'long long int')24+
error: wrong type argument to bit-complement25+
```26+
27+
Ruby の `a & b`, `a | b`, `a ^ b`, `a << n`, `a >> n`, `~a` で operands が bigint の時 C 演算子が使えない。 optcarrot は CPU emulator なので bit-op が大量。28+
29+
**修正**: runtime helpers + codegen 分岐:30+
- `sp_bigint_and(a, b)`, `_or`, `_xor`, `_shl`, `_shr`, `_not`, `_inv`31+
- `compile_operator_expr` / `compile_bitop_expr` で bigint operands を検出 → helper call emit32+
- 混在 (bigint + int_literal) → 一方を unbox / box して揃える33+
34+
### Bucket B: Builtin container helpers with bigint args35+
36+
```37+
sp_IntArray_push(a, bigint_lv) ← needs sp_bigint_to_int38+
sp_IntArray_get(a, bigint_idx)39+
sp_FloatArray_set(a, idx, bigint_val) ← unbox + cast to mrb_float40+
sp_IntStrHash_set(h, bigint_key, val)41+
```42+
43+
memory に partial fix 言及あり。 残るのは `_push` / `_get` / `_set` の特定 site と FloatArray の bigint→float cast。44+
45+
### Bucket C: 反復 / index で bigint46+
47+
```48+
range.each { |i| arr[i] = ... } ← i bigint、 arr[i] で unbox 必要49+
n.times { |i| ... }50+
```51+
52+
block param が bigint で IntArray index 等に使われる shape。 既に部分対応済みだが取りこぼし。53+
54+
### Bucket D: FFI / typed user method call の poly → bigint coerce55+
56+
```57+
sp_Counter_cls_double(poly_val) ← param bigint、 arg poly58+
sp_mandelbrot(bigint, bigint) ← FFI param float、 arg bigint59+
```60+
61+
`compile_typed_call_args` で poly arg → bigint param の coerce を追加 (mirror of #639 IVW unbox)。 FFI :float / :int arg は bigint で `sp_bigint_to_int` → cast。62+
63+
### Bucket E: Bigint slot init / return from poly64+
65+
```66+
sp_Bigint * x = poly_expr ← .v.p extract 必要67+
return poly_expr (function return bigint)68+
```69+
70+
`compile_expr_for_expected_type` の poly→bigint arm が無い / 不完全。71+
72+
### Bucket F: Bigint → string 変換漏れ73+
74+
```75+
puts bigint_val76+
"prefix" + bigint_val77+
sp_int_to_s(bigint_val) ← should be sp_bigint_to_s78+
```79+
80+
memory に「文字列補間 / str_concat / str << の bigint→string 変換 (sp_bigint_to_s)」とあり partial fix 済み。 残りの emit site を探す。81+
82+
### Bucket G: Test/bench infrastructure issues83+
84+
promote と無関係に表面化する shape:85+
- `int_eq_nil_strict` — int slot が bigint 化したので nil check の挙動が変わる86+
- `pattern_pin` — pin pattern が int を期待してるが bigint で型不一致87+
- `const_self_ref_init_warns` — 出力 format 変化 (`0` vs `0LL`?)88+
89+
これらは個別調査。90+
91+
## Phases (実装順)92+
93+
### Phase 1 — Bit-op support for bigint (大ブロッカー解消)94+
95+
最大の塊。 これで optcarrot の 80% errors が消える。96+
97+
1.1 `lib/sp_runtime.h`: 新 helpers98+
- `sp_bigint_and(a, b)`, `_or(a, b)`, `_xor(a, b)` — 既存 GMP wrapper 経由99+
- `sp_bigint_shl(a, n)`, `_shr(a, n)` — n は mrb_int100+
- `sp_bigint_not(a)` — 1's complement (符号反転 + 1 で signed と同じ)101+
102+
1.2 `lib/sp_bigint.c`: GMP `mpz_and` / `mpz_ior` / `mpz_xor` / `mpz_mul_2exp` / `mpz_fdiv_q_2exp` / `mpz_com` ラップ103+
104+
1.3 `spinel_codegen.rb`:105+
- `compile_operator_expr` で `op in {"&", "|", "^", "<<", ">>"}` && (lt == bigint || rt == bigint) → helper106+
- `~` の単項 (`compile_call_expr` で recv bigint mname `~`) → `sp_bigint_not`107+
- mixed (bigint + int literal): literal を `sp_bigint_new_int` で box して helper、 OR 両方 unbox して C 演算子 → re-box108+
109+
1.4 `optcarrot` で動作確認、 checksum 59662 維持確認110+
111+
期待: optcarrot promote PASS、 test promote +20〜30 cases112+
113+
### Phase 2 — Builtin container helpers の bigint unbox 取りこぼし114+
115+
2.1 `sp_IntArray_push` emit site: arg が bigint なら `sp_bigint_to_int` を wrap116+
2.2 `sp_IntArray_get` / `_set` / `_index` 同様117+
2.3 `sp_FloatArray_set` の val が bigint なら `(mrb_float)sp_bigint_to_int(...)` で cast118+
2.4 `sp_*IntHash_*` で key/val bigint の unbox119+
120+
期待: bench bm_loops_times / bm_matmul / bm_fannkuch PASS。 test promote +10〜15 cases121+
122+
### Phase 3 — Typed user method call の poly→bigint coerce123+
124+
3.1 `compile_typed_call_args` (positional + kwarg):125+
- 引数の static type が `poly` で param expected が `bigint` → `(arg).v.p` 経由126+
- mirror of #639 の IVW unbox path127+
128+
3.2 FFI :float / :int arg with bigint:129+
- `compile_ffi_func_call` で arg が bigint なら `sp_bigint_to_int` → cast130+
131+
期待: nullable_poly_hash_arg_unbox 等 +5 tests、 bm_mandel_term / bm_so_mandelbrot 系 PASS132+
133+
### Phase 4 — Bigint slot init / return from poly134+
135+
4.1 `compile_expr_for_expected_type`: at == "poly" && expected == "bigint" → `(... ).v.p`136+
4.2 `compile_return_stmt`: rt == "poly" && method return == "bigint" → 同様 unbox137+
138+
期待: 残 test promote errors +5139+
140+
### Phase 5 — Bigint → string 変換取りこぼし141+
142+
5.1 `puts bigint`, string interpolation `"x=#{bigint}"`, `<<` chain の emit site で bigint operand → `sp_bigint_to_s` 経由143+
144+
期待: bench bm_csv_process / bm_jekyll_lite 系 PASS145+
146+
### Phase 6 — 個別 test failure 調査147+
148+
残 test failures を個別に潰す。 各々小さな shape:149+
- `int_eq_nil_strict` — bigint slot で nil compare 挙動150+
- `pattern_pin` — pattern matching の bigint scrutinee151+
- `const_self_ref_init_warns` — output format 微差152+
153+
期待: test promote 100% pass154+
155+
### Phase 7 — Bench 残り + Makefile 整理156+
157+
7.1 残 bench failures 個別に潰す158+
7.2 Makefile から `optcarrot` 用 `--int-overflow=wrap` pin 除去 (or `--int-overflow=$(SPINEL_INT_OVERFLOW)` defaulting to wrap だが promote でも通る)159+
160+
期待: bench promote 100% pass161+
162+
## 検証フロー (各 phase で)163+
164+
```bash165+
# default mode は壊さない166+
rm -rf build/test-results/167+
make -j8 test 2>&1 | tail -2 # 631/0/0 維持168+
make optcarrot 2>&1 | tail -3 # checksum 59662 維持169+
170+
# promote mode 進捗171+
rm -rf build/test-results/172+
SPINEL_INT_OVERFLOW=promote make \173+
CFLAGS="-O2 -Wno-all -Wno-unknown-warning-option -Wno-alloc-size-larger-than -DSP_INT_OVERFLOW_MODE_PROMOTE" \174+
-j8 test 2>&1 | tail -2175+
176+
SPINEL_INT_OVERFLOW=promote make \177+
CFLAGS="-O2 -Wno-all -Wno-unknown-warning-option -Wno-alloc-size-larger-than -DSP_INT_OVERFLOW_MODE_PROMOTE" \178+
-j8 bench 2>&1 | tail -2179+
180+
# optcarrot single181+
SPINEL_INT_OVERFLOW=promote ./spinel --int-overflow=promote build/optcarrot-single.rb -o /tmp/op_p182+
/tmp/op_p183+
```184+
185+
## 効果量予想 (累積)186+
187+
| Phase | test | bench | optcarrot |188+
|---|---|---|---|189+
| 現状 | 557/52/22 | 36/7/14 | C compile fail |190+
| 1完了 | ~580/45/15 | ~45/5/7 | **PASS** (checksum 59662) |191+
| 2完了 | ~595/40/8 | ~52/3/2 | PASS |192+
| 3完了 | ~610/15/4 | ~55/2/0 | PASS |193+
| 4完了 | ~620/8/2 | ~56/1/0 | PASS |194+
| 5完了 | ~625/5/0 | ~57/0/0 | PASS |195+
| 6完了 | **631/0/0** | 57/0/0 | PASS |196+
| 7完了 | 631/0/0 | **57/0/0** | PASS (Makefile clean) |197+
198+
## 工数概算199+
200+
- Phase 1 (bit-ops): 2-4 hours201+
- Phase 2 (containers): 1-2 hours202+
- Phase 3 (call args): 1-2 hours203+
- Phase 4 (return/slot): 0.5-1 hour204+
- Phase 5 (string conv): 0.5-1 hour205+
- Phase 6 (個別 test): 2-4 hours (varies)206+
- Phase 7 (bench + Makefile): 1-2 hours207+
208+
**合計**: 8-16 hours focused work209+
210+
## 進め方の注意211+
212+
- **default を壊さない**: 各 commit 直後に `make test` (default 631/0/0) + `make optcarrot` (wrap pin、 checksum 59662) を回す213+
- **commit を小刻みに**: bucket 1 つ ≒ 1 commit (memory `feedback_commit_often`)214+
- **inline 関数 vs macro**: `sp_int_*` は macro。 新規 helper も同じ pattern で (memory `feedback_promote_mode_inline_pitfall`)215+
- **stale cache 注意**: promote_int_to_bigint_globally 後の cache invalidation 漏れに警戒216+
- **記録**: 各 phase の commit 後、 残り errors / fails の数を memo して進捗トラック217+
218+
## Risks219+
220+
1. **optcarrot は --int-overflow=wrap が最速**: promote だと bigint helper call overhead で fps 低下が予想される。 Makefile のデフォルトは wrap のままにする (promote は opt-in mode という設計に沿う)221+
2. **bigint memory pressure**: 全 int slot が heap-allocated bigint になる → GC 圧と allocator churn。 既存 GC で対応できるか測定要222+
3. **既存 default test の non-determinism**: 不変なはずだが、 stale cache invalidation 経由で稀に regression が出る可能性あり。 毎 commit で default test 必須223+
4. **新 helper の signedness**: GMP の `mpz_and` 等は arbitrary-precision で signed 自然サポート。 spinel 側で signedness 維持確認224+
225+
## 開始 phase226+
227+
**Phase 1 (bit-ops)** を最優先。 optcarrot を unblock し、 影響が最も大きい。 着手後、 各 helper を 1 つずつ追加 → optcarrot で動作確認 → commit。