Commit 0fd967b1

0fd967b16ab7Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified

Description

docs(promote): record block-param widen exclusion (901) and poly->scalar unbox (909)

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

Details

Commit
0fd967b16ab7
Parent
83914908c1ff

docs/promote-mode-plan.mdmodifiedView file

@@ -75,9 +75,10 @@
7575 - **masgn target boxing(694f3f89, +7)**: scalar-RHS local(+typed-zero rest default)/tuple-element local・cvar/typed-array destructure get/trailing rights get を box。codegen_stmt.c。
7676 - **leading-splat masgn(91c022ce, +1)**: `*xs, last=[..]` の post-splat fixed target を temp 経由 box。splat_destructure 緑。codegen_stmt.c:3672。(promote 860 到達)
7777 - **★決定的洞察**: widen pass は `scope->locals[].type`/`ivar_types`/`cvar_types` だけ書き換え、**node-type cache は更新しない**。ゆえに `comp_ntype(c, target_node)` は **widen 前のスカラ型(int 等)を返す**。slot が poly かは必ず `scope_local(scope, name)->type`(or ivar/cvar の type 配列)で判定すること。rvalue を box する型は `comp_ntype(value)`(真の値型)で取る。この non-update のおかげで rest default の typed-zero(`default_value(int)="0"`)も復元でき、`emit_boxed_text(int,"0")`=`sp_box_int(0)` で quirk 一致。
78-- **残 ERR=101 の内訳(first-error 集計 `/tmp/perr_lines.txt` の手法で再取得可)**: B1 assign-poly の未カバー site = **leading-splat masgn**(`*xs, last=`、trailing を temp 経由で bind)+ **rightward pattern**(`expr => a,b` は MultiWriteNode でなく MatchRequiredNode の別経路)。**B4 builtin-arg coerce(~29)**= poly arg を builtin の int/typed param へ(`sp_IntArray_push`/`sp_poly_to_i`/`sp_poly_to_s`/`sp_bigint_new_int`/`sp_range_include`/user `<=>` 等、call-arg site で coerce、最も散在)。他に return mismatch・invalid binary operands・closure capture(front-end)・float coerce が少数。FAIL=33 は別途(コンパイルは通るが出力差)。
79-- **★次の大物 = ブロック反復 block-param(analyze レベル、codegen boxing では直せない)**: `[1,2,3,4].filter_map{|x| x*2 if x.even?}` 系。widen は block-param の scope local を poly 化するが、**最終 infer_type pass(analyze.c:2296 の全 node 再 infer)が typed-array 反復の block-param を element 型(int)に再導出して widen を打ち消す**。一方 `even?` 等一部の codegen 経路は scope local(poly)を読む → **同一 body 内で同じ `x` が int(`sp_int_mul`)と poly(`sp_poly_to_i`)に混在**し、反復束縛も `mrb_int lv_x__bp = Array_get(...)`(element 型 decl)が外側 `sp_RbVal lv_x__bp`(widen slot)を **shadow 二重宣言**。codegen で decl を poly 化すると body の `sp_int_mul(poly,..)` が新たに壊れる(=codegen 単独で不可)。**正解は analyze 側で block-param 型解決を widen と一貫させる**(typed-array 反復 param も promote で poly に固定、final infer で上書きさせない)。**inference 変更=default/optcarrot 回帰リスク**ゆえ専用に慎重に。each/map/select/filter_map/reduce 系の最大グループ(sample 10 中 4 がこの shadow pattern)。
80-- **次の一手(codegen 級)**: B4(builtin call-arg coerce、genuine な poly arg→int builtin param のみ)。`compile_typed_call_args` 相当の call-arg emission で poly arg→int param に `sp_poly_to_i`、float→`sp_poly_to_f`、bigint→`.v.p` を挿す中央 coerce。並行で rightward pattern(`expr=>a,b`=MatchRequiredNode 別経路)。手法=`/tmp/ptest.sh <name>`(単体 promote compile+diff)、`/tmp/perragg.sh`(全 ERR 並列 first-error 集計→`/tmp/perr_lines.txt`)。
78+- **★block-param widen 除外(0ed24068, 860→901、+41!)= 本セッション最大の勝因**: block-param 反復(`[1,2,3,4].filter_map{|x| x*2 if x.even?}`)は最大グループだった。**真因**: widen は block-param の scope local を poly 化するが、block emitter は use_shadow で param を element 型(int)に retype して body を再 infer する。だが **`even?` 等メソッド呼びの inference は receiver を cache から読むだけで再 infer しない**(`*` 演算子は operand を infer_type で更新する)ので、`x*2`=int / `x.even?`=poly に混在し、`mrb_int lv_x` shadow が外側 `sp_RbVal lv_x`(widen)を二重宣言。**解=block-param を widen から除外**(`is_block_param` skip、analyze.c:2275)。block-param は反復コレクションの element 型で型付くべきで、widen は害だけ。method param は引き続き widen(promote の本旨)。compile error と一致する output-mismatch FAIL も一掃。**当初は「analyze で型を一貫 poly 化」を想定したが、逆に「block-param は widen しない」が正解だった。**
79+- **poly→scalar unbox(29dbedea, 901→909)**: block-param を int に保ったことで、poly source(widen された yield 値・ivar・trampoline temp)を int block-param へ束縛する逆方向が必要に。emit_block_invoke の yield 束縛 + instance_exec の is_exec/tramp 束縛に `bt scalar && at poly → emit_unbox_text` arm を追加(box arm の対称)。yield/forward_args_block/block_forward_*/inlined_yield_self_locals/instance_exec_mixed_args 緑。
80+- **残 ERR=64 / FAIL=20(`/tmp/perr2.txt` の手法で再取得)**: **B4 builtin-arg coerce(~19)**= poly arg を builtin の int/typed param へ(`sp_bigint_new_int`/`sp_range_include`/`sp_str_sub_range(_r)`/user `<=>`=`_lt_set_gt`/`sp_collect_s` 等、call-arg site で散在 coerce)。**B6 残(poly→int 代入)**= まだ拾い切れてない束縛 site。**B1 残(int→poly 代入、8)**。**closure capture(front-end, 5)**=`unsupported closure capturing a non-integer variable`(widen で capture 変数が poly 化→closure が非 int capture 非対応、front-end 制限)。他に return mismatch(3)/invalid binary operands(4)/invalid initializer(2)/`unsupported p argument`(clamp)等。**FAIL=20** はコンパイル通るが出力差(例: `proc` が proc 戻り値で `6`→`0`)、別途調査。
81+- **次の一手**: (1) B4 builtin call-arg の中央 coerce(poly arg→int param=`sp_poly_to_i`、float=`sp_poly_to_f`、bigint=`.v.p`)。(2) closure-capture front-end(capture 変数 poly 対応 or widen 除外)。(3) FAIL=20 の出力差(proc 等)。手法=`/tmp/ptest.sh <name>`、`/tmp/perragg.sh`→`/tmp/perr2.txt`、`/tmp/ptest_lastc` に最後の cfile。
8182
8283 ### (参考)legacy 方式
8384 legacy backend(`legacy/spinel_codegen.rb` L55, L3037-3203)は promote で全 int slot を `sp_Bigint*` に widen(method ABI = `(void*, sp_Bigint*...) -> sp_Bigint*`)。採用せず(上記理由)。