Files
hakorune/docs/private/papers/paper-e-loop-signal-ir/POC_PLAN.md

39 lines
2.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# LoopForm IR / LifeBox Model — PoC計画安全な縦スライス
## コア要件(不変条件)
- ループ正規形: すべての制御は `loop.begin → loop.iter → loop.branch → loop.end` の列で現れ、dispatchブロックで合流PHIはdispatch直後のみ
- Signal整合性: `LoopSignal<T> = Next/Break/Yield/Return` は i8タグ単一SSAペイロード。4タグは厳密相互排他。
- 単一継続点: Loopごとに1出口break/return合流点。Loop1は必ず即時畳み込みinlineでゼロコスト化。
- 可逆性: LoopForm → Core13 への逆Loweringは常に可能フラグでON/OFF
## IR最小仕様再掲
- Signal: 表層 `LoopSignal<T>` / 実体 `{ i8 tag, T payload }`tag=0/1/2/3
- 命令4種中間正規形:
- `loop.begin %lp`(メタ: ソース位置/ID
- `loop.iter %sig, %lp, %state` — LoopBox 1反復を実行して Signal を返す
- `loop.branch %sig { onNext: L1, onBreak: L2, onYield?: L3, onReturn?: L4 }`
- `loop.end %lp`
- LoopBox契約: どのBoxも `init → step(state) → fini`。通常Boxは `step` が1回で `Break(result)`=Loop1
## 逆Lowering規則抜粋
- scope: Loop1iter→即Break→end→ 直列化
- while: iter→Nextで継続、Breakで終了dispatch合流→ br/jump へ
- if: Loop1条件→ next/break(path))→ branch+phi へ
- return: `Break(Return v)` に統一→最上位dispatch合流で `Return v`
## PoC範囲
- 命令セット: 4命令 + Signal型のみ。対象は while / scope / ifLoop1に限定。
- 逆Lowering: Core13へ完全可逆VM/Interpreter 経路の実行一致)。
- 最適化: Loop1インライン畳み込みYieldなしのstate省略、dispatch一点合流を先行実装。
## 受入基準
- 構造: dispatch合流点のみでPHIPrinter/Verifierで検出可能
- 結果一致: 5スモークconst/return, add, branch+phi, while, nested-branchが現行MIRと一致。
- トレース: covered/unsupported と decisions(allow,fallback) をLoopForm/逆Lowering双方で採取できる。
## 拡張の切り口(後続)
- 関数/returnの完全Loop化: ReturnをSignalに残すか、Loop外のReturn命令に落とすかをflag化。
- generator/async: YieldをSignalに昇格。state継続スロットを LoopBox側で保持状態箱を1st-classに
- 効果モデル: effect latticePure/RO/SideをSignalに乗せ、dispatchでbarrier位置を決めやすく。
- AOT/JSON v0: mir_version/extensionsでLoopFormは“中間正規形”として明示。