fix(joinir): Phase 287 P2 - Pattern6 nested loop latch overwrite fix

Fix infinite loop in Pattern6 (nested loop minimal) caused by main→loop_step
overwriting k_inner_exit→loop_step latch values.

Root cause: JoinIR main entry block was incorrectly treated as BackEdge,
causing it to overwrite the correct latch incoming values set by the true
back edge (k_inner_exit → loop_step).

Solution:
- Restrict latch recording to TailCallKind::BackEdge only
- Treat only MAIN's entry block as entry-like (not loop_step's entry block)
- Add debug_assert! to detect double latch set in future

Refactoring:
- Extract latch recording to latch_incoming_recorder module (SSOT)
- Add boundary.loop_header_func_name for explicit header identification
- Strengthen tail_call_classifier with is_source_entry_like parameter

Tests: apps/tests/phase1883_nested_minimal.hako → RC:9 (was infinite loop)
Smoke: 154/154 PASS, no regressions

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
2025-12-27 09:39:29 +09:00
parent bbfc3c1d94
commit a04b48416e
20 changed files with 401 additions and 171 deletions

View File

@ -1,7 +1,7 @@
# Phase 188.3: Nested loop lowering (1-level) — make Pattern 6 real
**Date**: 2025-12-27
**Status**: In progress (Phase 2 done: selection / Phase 3: lowering)
**Status**: Complete (Pattern6 lowering + merge/latch fix)
**Prereq**: Phase 188.2 Option A is complete (StepTree depth SSOT + strict Fail-Fast)
---
@ -44,15 +44,19 @@ Phase 188.2 の strict ガードは「depth > 2 を明示エラー」にして
## Current Code Status (reality)
Phase 188.3 は “選択ロジックPattern6選定” まで実装済みで、lowering が未実装stubな状態
Phase 188.3 は “選択ロジックPattern6選定→ lowering → merge/rewriter 安定化” まで完了している
- Selection SSOT: `src/mir/builder/control_flow/joinir/routing.rs``choose_pattern_kind()`
- cheap check → StepTree → AST validation
- `max_loop_depth == 2` かつ “Pattern6 lowerable” のときだけ `Pattern6NestedLoopMinimal` を返す
- Lowering stub: `src/mir/builder/control_flow/joinir/patterns/pattern6_nested_minimal.rs`
- 現在は `Err("[Pattern6] ... not yet implemented")` で Fail-Fast
この README のゴールは、stub を “実装済み” にして fixture を通すこと。
- Lowering: `src/mir/builder/control_flow/joinir/patterns/pattern6_nested_minimal.rs`
- JoinIR pipeline で `inner_step/k_inner_exit/k_exit` を含む関数群を生成して merge する
- Fixture: `apps/tests/phase1883_nested_minimal.hako`RC=9
- Merge/Rewrite contractSSOT:
- `latch_incoming` を記録してよいのは `TailCallKind::BackEdge` のみLoopEntry は上書き禁止)
- entry-like は “JoinIR main の entry block のみ”
- 二重 latch は `debug_assert!` で fail-fast
- 実装: `src/mir/builder/control_flow/joinir/merge/instruction_rewriter.rs`, `src/mir/builder/control_flow/joinir/merge/loop_header_phi_info.rs`
---
@ -167,6 +171,7 @@ JoinIR merge は「JoinIR の param ValueId は SSA 命令で定義されない
- `./tools/smokes/v2/run.sh --profile quick` が常にグリーン維持
- integration selfhost が FAIL=0 を維持
- 追加した nested loop fixture が PASSJoinIR lowering が使われたことをログ/タグで確認可能)
- 実測: `./target/release/hakorune --backend vm apps/tests/phase1883_nested_minimal.hako` → RC=9
---
@ -181,6 +186,10 @@ JoinIR merge は「JoinIR の param ValueId は SSA 命令で定義されない
- **Phase 188.3**: depth=2 の最小形を “確実に通す” + PoC fixture を smoke 固定
- **Phase 188.4+**: write-backouter carrier reconnectionと “再帰 lowering の一般化depthを増やしても壊れない” を docs-first で設計してから実装
### Post-completion (refactoring window)
実装完了後のリファクタ(意味論不変)を挟む場合は、`P2-REFACTORING-INSTRUCTIONS.md` を入口にする。
### Planned cleanup (after Phase 188.3)
Pattern6 を通す過程で露出しやすい “暗黙ルール” を SSOT 化して、今後の nested/generalization を楽にする: