Files
hakorune/docs/development/current/main/phases/phase-273/README.md

197 lines
8.1 KiB
Markdown
Raw Normal View History

# Phase 273: Plan Extractor (Pure) + PlanLowerer SSOT
Status: ✅ P0/P1/P2 completed (2025-12-22)
Goal:
- pattern 列挙の裾広がりを止める。
- pattern は "検出して Plan を返すだけ" に降格し、CFG/PHI/block/value の生成責務を 1 箇所に閉じ込める。
- P1: DomainPlan → CorePlan の 2層構造で "収束" を強める
---
## P2 完了 (2025-12-22)
P2 では Pattern7split scanを Plan ラインへ移行し、P1 の CorePlan を保ったまま “収束圧” を上げた。
- ✅ Pattern7: Extractor → DomainPlan → Normalizer → CorePlan → LowererMIR/Frag/emit_fragへ統一
- ✅ CoreLoopPlan: `block_effects / phis / frag / final_values` で一般化Pattern6/7 が同一 CorePlan に収束)
- ✅ CoreEffectPlan: `dst: Option<ValueId>` + `effects: EffectMask` で副作用(例: `push`)を表現可能にした
- ✅ Lowerer: “split” の知識を持たず、CorePlan のみを処理pattern-agnostic 維持)
## P1 完了 (2025-12-22)
### アーキテクチャ
```
DomainPlan (Pattern固有)
↓ PlanNormalizer (SSOT)
CorePlan (固定語彙 - 構造ノードのみ)
↓ PlanVerifier (fail-fast)
↓ PlanLowerer
MIR (block/value/phi)
```
### SSOT Entry Point
**Files**:
- `src/mir/builder/control_flow/plan/mod.rs` - DomainPlan/CorePlan 型定義
- `src/mir/builder/control_flow/plan/normalizer.rs` - PlanNormalizerDomainPlan → CorePlan
- `src/mir/builder/control_flow/plan/verifier.rs` - PlanVerifierfail-fast 検証)
- `src/mir/builder/control_flow/plan/lowerer.rs` - PlanLowererCorePlan → MIR
### 原則
- Extractor は **pure**builder 触り厳禁、DomainPlan を返すのみ)
- Normalizer は **SSOT**pattern 固有知識はここに集約)
- CorePlan の式は **ValueId 参照のみ**String 禁止 → 第2の言語処理系を作らない
- Lowerer は **pattern-agnostic**CorePlan のみを処理)
- terminator SSOT: Frag → emit_frag()
---
## CorePlan 固定語彙
```rust
pub enum CorePlan {
Seq(Vec<CorePlan>),
Loop(CoreLoopPlan),
If(CoreIfPlan),
Effect(CoreEffectPlan),
Exit(CoreExitPlan),
}
pub enum CoreEffectPlan {
MethodCall { dst, object, method, args, effects },
BinOp { dst, lhs, op, rhs },
Compare { dst, lhs, op, rhs },
Const { dst, value },
}
```
**増殖禁止ルール**:
- ード種別variantの追加は禁止
- `EffectPlan::ScanInit` のような scan専用 variant は禁止
- データ(フィールド、パラメータ)の追加は許容
---
## P1 Implementation Summary
**Files changed** (7 total):
- Modified: `src/mir/builder/control_flow/plan/mod.rs` - DomainPlan/CorePlan 型定義 (~220 lines)
- New: `src/mir/builder/control_flow/plan/normalizer.rs` - PlanNormalizer (~290 lines)
- New: `src/mir/builder/control_flow/plan/verifier.rs` - PlanVerifier (~180 lines)
- Modified: `src/mir/builder/control_flow/plan/lowerer.rs` - CorePlan 対応 (~250 lines)
- Modified: `src/mir/builder/control_flow/joinir/patterns/pattern6_scan_with_init.rs` - DomainPlan 返却
- Modified: `src/mir/builder/control_flow/joinir/patterns/router.rs` - Normalizer + Verifier 経由
**Regression test**:
- ✅ phase254_p0_index_of_vm.sh (fixed needle, forward scan)
- ✅ phase258_p0_index_of_string_vm.sh (dynamic needle)
---
## LLVM harness の落とし穴Phase 258 で露出)
Phase 258 の `index_of_string`dynamic needleで、VM では正しいのに LLVM で `Result: 0` になるケースが露出した。
原因は Phase 273 P1 の本線DomainPlan→CorePlan→emit_fragではなく、LLVM harness / AOT ランタイム側の “契約” だった。
### 1) `params` を使わないと引数が silently に潰れる
MIR JSON の `params`ValueId の引数順を使わず、heuristic で「未定義の use」を拾うと、
`box` フィールド等を見落とした場合に **v1 が arg0 に誤マップ**され、needle が haystack と同一扱いになる。
- Fix: `src/llvm_py/builders/function_lower.py``func_data["params"]` を SSOT として優先する
### 2) “raw integer vs handle” 衝突で `Result` が 0 になる
AOT ランタイムnyrt`ny_main()` の返り値が **raw i64****handle(i64)** かを区別できない。
正しい raw 返り値(例: `6`)が、たまたま生成済みの handle id と衝突すると、IntegerBox ではないため `Result: 0` になりうる。
- Fix: `crates/nyash_kernel/src/lib.rs` の exit_code 抽出で、handle が IntegerBox 以外なら raw i64 として扱う
## References
- JoinIR SSOT overview: `docs/development/current/main/joinir-architecture-overview.md`
- Frag SSOT: `docs/development/current/main/design/edgecfg-fragments.md`
- Phase 272Pattern6/7, Frag適用: `docs/development/current/main/phases/phase-272/README.md`
## Instructions
- P0 Claude Code: `docs/development/current/main/phases/phase-273/P0-CLAUDE.md`
- P1 Claude Code: `docs/development/current/main/phases/phase-273/P1-CLAUDE.md`
- P2 Completion: `docs/development/current/main/phases/phase-273/P2-COMPLETION.md`
docs(plan): Phase 273 P4 - Plan Line SSOT Documentation Finalization Phase 273 P4 では、Plan ライン(Extractor → Normalizer → Verifier → Lowerer)を "current operational SSOT" として文書化し、アーキテクチャの収束を明文化した。 ## Changes ### router.rs docstring 更新 - "Phase 273 P3: Plan Line is Current SSOT for Pattern6/7" セクション追加 - ルーティング戦略を明示(Plan entry points → legacy table) - SSOT Entry Points を列挙(Pattern6/7 Normalizer, Pattern1-5 各 Lowerer) ### phase-273/README.md 更新 - P3 completion section 追加(generalized CoreLoopPlan 移行完了) - P3+ Legacy Removal section 追加(~174 lines 削除記録) - P4 Proposal section 追加(Documentation Finalization チェックリスト) - SSOT Documentation Entry Points リスト追加(5 つの SSOT 入口) ### joinir-architecture-overview.md 更新 - Section 2.1.2 "Plan-Based Patterns (Pattern6-7, Phase 273 P3)" 追加 - Plan Extractor, Normalizer, Verifier, Lowerer の Box 構造を文書化 - Plan line vs JoinIR line 比較表追加(収束性・SSOT 特性の対比) - SSOT characteristics リスト追加(Normalizer SSOT, emit_frag SSOT 等) ## SSOT Entry Points(Phase 273 P3 完了時点) 1. **ルーティング**: `router.rs::route_loop_pattern()` - Pattern6/7 Plan entry points 2. **型定義**: `plan/mod.rs` - DomainPlan/CorePlan 固定語彙 3. **正規化**: `plan/normalizer.rs` - Pattern 固有知識一元管理 4. **検証**: `plan/verifier.rs` - fail-fast 不変条件(V2-V9) 5. **降格**: `plan/lowerer.rs` - Pattern-agnostic MIR emission ## Test - ✅ VM regression: phase254_p0_index_of_vm.sh (PASS) - ✅ LLVM regression: phase258_p0_index_of_string_llvm_exe.sh (PASS) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2025-12-23 00:34:38 +09:00
- P3 Claude Code: `docs/development/current/main/phases/phase-273/P3-CLAUDE.md`
docs(plan): Phase 273 P4 - Plan Line SSOT Documentation Finalization Phase 273 P4 では、Plan ライン(Extractor → Normalizer → Verifier → Lowerer)を "current operational SSOT" として文書化し、アーキテクチャの収束を明文化した。 ## Changes ### router.rs docstring 更新 - "Phase 273 P3: Plan Line is Current SSOT for Pattern6/7" セクション追加 - ルーティング戦略を明示(Plan entry points → legacy table) - SSOT Entry Points を列挙(Pattern6/7 Normalizer, Pattern1-5 各 Lowerer) ### phase-273/README.md 更新 - P3 completion section 追加(generalized CoreLoopPlan 移行完了) - P3+ Legacy Removal section 追加(~174 lines 削除記録) - P4 Proposal section 追加(Documentation Finalization チェックリスト) - SSOT Documentation Entry Points リスト追加(5 つの SSOT 入口) ### joinir-architecture-overview.md 更新 - Section 2.1.2 "Plan-Based Patterns (Pattern6-7, Phase 273 P3)" 追加 - Plan Extractor, Normalizer, Verifier, Lowerer の Box 構造を文書化 - Plan line vs JoinIR line 比較表追加(収束性・SSOT 特性の対比) - SSOT characteristics リスト追加(Normalizer SSOT, emit_frag SSOT 等) ## SSOT Entry Points(Phase 273 P3 完了時点) 1. **ルーティング**: `router.rs::route_loop_pattern()` - Pattern6/7 Plan entry points 2. **型定義**: `plan/mod.rs` - DomainPlan/CorePlan 固定語彙 3. **正規化**: `plan/normalizer.rs` - Pattern 固有知識一元管理 4. **検証**: `plan/verifier.rs` - fail-fast 不変条件(V2-V9) 5. **降格**: `plan/lowerer.rs` - Pattern-agnostic MIR emission ## Test - ✅ VM regression: phase254_p0_index_of_vm.sh (PASS) - ✅ LLVM regression: phase258_p0_index_of_string_llvm_exe.sh (PASS) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2025-12-23 00:34:38 +09:00
## P3 完了 (2025-12-23)
docs(plan): Phase 273 P4 - Plan Line SSOT Documentation Finalization Phase 273 P4 では、Plan ライン(Extractor → Normalizer → Verifier → Lowerer)を "current operational SSOT" として文書化し、アーキテクチャの収束を明文化した。 ## Changes ### router.rs docstring 更新 - "Phase 273 P3: Plan Line is Current SSOT for Pattern6/7" セクション追加 - ルーティング戦略を明示(Plan entry points → legacy table) - SSOT Entry Points を列挙(Pattern6/7 Normalizer, Pattern1-5 各 Lowerer) ### phase-273/README.md 更新 - P3 completion section 追加(generalized CoreLoopPlan 移行完了) - P3+ Legacy Removal section 追加(~174 lines 削除記録) - P4 Proposal section 追加(Documentation Finalization チェックリスト) - SSOT Documentation Entry Points リスト追加(5 つの SSOT 入口) ### joinir-architecture-overview.md 更新 - Section 2.1.2 "Plan-Based Patterns (Pattern6-7, Phase 273 P3)" 追加 - Plan Extractor, Normalizer, Verifier, Lowerer の Box 構造を文書化 - Plan line vs JoinIR line 比較表追加(収束性・SSOT 特性の対比) - SSOT characteristics リスト追加(Normalizer SSOT, emit_frag SSOT 等) ## SSOT Entry Points(Phase 273 P3 完了時点) 1. **ルーティング**: `router.rs::route_loop_pattern()` - Pattern6/7 Plan entry points 2. **型定義**: `plan/mod.rs` - DomainPlan/CorePlan 固定語彙 3. **正規化**: `plan/normalizer.rs` - Pattern 固有知識一元管理 4. **検証**: `plan/verifier.rs` - fail-fast 不変条件(V2-V9) 5. **降格**: `plan/lowerer.rs` - Pattern-agnostic MIR emission ## Test - ✅ VM regression: phase254_p0_index_of_vm.sh (PASS) - ✅ LLVM regression: phase258_p0_index_of_string_llvm_exe.sh (PASS) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2025-12-23 00:34:38 +09:00
P3 では Pattern6 を generalized CoreLoopPlan に移行し、legacy fallback を撤去して Plan ラインの収束を完成させた。
docs(plan): Phase 273 P4 - Plan Line SSOT Documentation Finalization Phase 273 P4 では、Plan ライン(Extractor → Normalizer → Verifier → Lowerer)を "current operational SSOT" として文書化し、アーキテクチャの収束を明文化した。 ## Changes ### router.rs docstring 更新 - "Phase 273 P3: Plan Line is Current SSOT for Pattern6/7" セクション追加 - ルーティング戦略を明示(Plan entry points → legacy table) - SSOT Entry Points を列挙(Pattern6/7 Normalizer, Pattern1-5 各 Lowerer) ### phase-273/README.md 更新 - P3 completion section 追加(generalized CoreLoopPlan 移行完了) - P3+ Legacy Removal section 追加(~174 lines 削除記録) - P4 Proposal section 追加(Documentation Finalization チェックリスト) - SSOT Documentation Entry Points リスト追加(5 つの SSOT 入口) ### joinir-architecture-overview.md 更新 - Section 2.1.2 "Plan-Based Patterns (Pattern6-7, Phase 273 P3)" 追加 - Plan Extractor, Normalizer, Verifier, Lowerer の Box 構造を文書化 - Plan line vs JoinIR line 比較表追加(収束性・SSOT 特性の対比) - SSOT characteristics リスト追加(Normalizer SSOT, emit_frag SSOT 等) ## SSOT Entry Points(Phase 273 P3 完了時点) 1. **ルーティング**: `router.rs::route_loop_pattern()` - Pattern6/7 Plan entry points 2. **型定義**: `plan/mod.rs` - DomainPlan/CorePlan 固定語彙 3. **正規化**: `plan/normalizer.rs` - Pattern 固有知識一元管理 4. **検証**: `plan/verifier.rs` - fail-fast 不変条件(V2-V9) 5. **降格**: `plan/lowerer.rs` - Pattern-agnostic MIR emission ## Test - ✅ VM regression: phase254_p0_index_of_vm.sh (PASS) - ✅ LLVM regression: phase258_p0_index_of_string_llvm_exe.sh (PASS) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2025-12-23 00:34:38 +09:00
- ✅ Pattern6: generalized CoreLoopPlan`frag/block_effects/phis/final_values`)へ完全移行
- ✅ CoreLoopPlan: すべてのフィールドを必須化(`Option` 削除)
- ✅ Lowerer: `lower_loop_legacy()` 撤去、CorePlan SSOT 化Fail-Fast
- ✅ PlanLowerer: Pattern 固有参照(`emit_scan_with_init_edgecfg()` 等)を完全削除
- ✅ route_loop_pattern(): Plan ラインを明示的 SSOT として文書化
docs(plan): Phase 273 P4 - Plan Line SSOT Documentation Finalization Phase 273 P4 では、Plan ライン(Extractor → Normalizer → Verifier → Lowerer)を "current operational SSOT" として文書化し、アーキテクチャの収束を明文化した。 ## Changes ### router.rs docstring 更新 - "Phase 273 P3: Plan Line is Current SSOT for Pattern6/7" セクション追加 - ルーティング戦略を明示(Plan entry points → legacy table) - SSOT Entry Points を列挙(Pattern6/7 Normalizer, Pattern1-5 各 Lowerer) ### phase-273/README.md 更新 - P3 completion section 追加(generalized CoreLoopPlan 移行完了) - P3+ Legacy Removal section 追加(~174 lines 削除記録) - P4 Proposal section 追加(Documentation Finalization チェックリスト) - SSOT Documentation Entry Points リスト追加(5 つの SSOT 入口) ### joinir-architecture-overview.md 更新 - Section 2.1.2 "Plan-Based Patterns (Pattern6-7, Phase 273 P3)" 追加 - Plan Extractor, Normalizer, Verifier, Lowerer の Box 構造を文書化 - Plan line vs JoinIR line 比較表追加(収束性・SSOT 特性の対比) - SSOT characteristics リスト追加(Normalizer SSOT, emit_frag SSOT 等) ## SSOT Entry Points(Phase 273 P3 完了時点) 1. **ルーティング**: `router.rs::route_loop_pattern()` - Pattern6/7 Plan entry points 2. **型定義**: `plan/mod.rs` - DomainPlan/CorePlan 固定語彙 3. **正規化**: `plan/normalizer.rs` - Pattern 固有知識一元管理 4. **検証**: `plan/verifier.rs` - fail-fast 不変条件(V2-V9) 5. **降格**: `plan/lowerer.rs` - Pattern-agnostic MIR emission ## Test - ✅ VM regression: phase254_p0_index_of_vm.sh (PASS) - ✅ LLVM regression: phase258_p0_index_of_string_llvm_exe.sh (PASS) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2025-12-23 00:34:38 +09:00
### SSOT Documentation Entry Points
P3 完了により、以下が Plan ライン SSOT の入口となった:
1. **ルーティング SSOT**: `src/mir/builder/control_flow/joinir/patterns/router.rs::route_loop_pattern()`
- Pattern6/7 Plan-based entry pointslines 294-354
- Legacy patterns (1-5, 8-9) LOOP_PATTERNS tablelines 362-368
2. **型定義 SSOT**: `src/mir/builder/control_flow/plan/mod.rs`
- `DomainPlan { ScanWithInit, SplitScan, ... }`
- `CorePlan { Seq, Loop, If, Effect, Exit }`
- `CoreLoopPlan { block_effects, phis, frag, final_values }`
3. **正規化 SSOT**: `src/mir/builder/control_flow/plan/normalizer.rs`
- Pattern 固有知識の一元管理ScanWithInit/SplitScan normalization
4. **検証 SSOT**: `src/mir/builder/control_flow/plan/verifier.rs`
- fail-fast 不変条件チェックV2-V9
5. **降格 SSOT**: `src/mir/builder/control_flow/plan/lowerer.rs`
- Pattern 知識なし、CorePlan のみ処理
- emit_frag() で terminator SSOT
### P3+ Legacy Removal (2025-12-23)
P3 完了後、さらにレガシーコードを削除:
-`emit_scan_with_init_edgecfg()` 関数削除(~144 lines
-`CoreCarrierInfo` 構造体削除(~15 lines
-`verify_carrier()` 関数削除(~15 lines
- ✅ 未使用 import 削除cargo fix、~30 files
**Total lines removed**: ~174 lines (net reduction)
---
## P4 Proposal (Documentation Finalization)
P4 では、アーキテクチャドキュメントを Plan ラインで完全更新し、「現行 SSOT」を明確に標記する
1. **router.rs docstring 更新**
- "Phase 273 P3: Plan Line is Current SSOT for Pattern6/7" 追加
- ルーティング戦略を明示Plan entry points → legacy table
- SSOT Entry Points を列挙
2. **joinir-architecture-overview.md 更新**
- Section 2.1.2 追加Plan-based patterns 専用セクション)
- Section 0 の「target shape」を「current operational shape」に更新
- routing order diagram を Plan entry points 込みで再描画
3. **Phase 273 README.md 更新**
- P3 completion section 追加
- P4 proposal → "Documentation Finalization"
---
## Future Work (P5+)
1. **Pattern7/8/9 DomainPlan 追加**: Split, BoolPredicate 等を DomainPlan に追加
2. **Normalizer 拡張**: 各 DomainPlan → CorePlan 変換
3. **全 Pattern の Plan 化**: Pattern1-5 を段階的に Plan 化