feat(joinir): Phase 132-P3 - Exit PHI collision early detection

Added verify_exit_phi_no_collision() to contract_checks.rs for early detection
of ValueId collisions between exit PHIs and other instructions.

Problem detected:
- If exit_phi_builder uses builder.value_gen.next() (module-level) instead of
  func.next_value_id() (function-level), ValueIds can collide:

  Example:
  - bb0: %1 = const 0   (counter init)
  - bb3: %1 = phi ...   (exit PHI - collision!)

Previous behavior:
- Error only detected at LLVM backend runtime
- Cryptic error: "Cannot overwrite PHI dst=1"

New behavior:
- Panic at Rust compile time (debug build)
- Clear error message with fix suggestion:
  "Exit PHI dst %1 collides with instruction in block 0
   Fix: Use func.next_value_id() in exit_phi_builder.rs"

Benefits:
- 🔥 Fail-Fast: Catch errors during Rust compilation, not LLVM execution
- 📋 Clear messages: Exact collision point + fix suggestion
- 🧪 Testable: verify_exit_phi_no_collision() can be unit tested

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
nyash-codex
2025-12-15 06:00:48 +09:00
parent bd07b7f41f
commit 771bf6b0d1
7 changed files with 211 additions and 16 deletions

View File

@ -2,7 +2,7 @@
**Date**: 2025-12-15
**Status**: ✅ Done
**Scope**: Pattern 1Simple Whileで「ループ終了後の `return i`」が VM/LLVM で一致することを固定する
**Scope**: ループ exit 値exit PHI / boundaryが VM/LLVM で一致することを固定するPattern 1 と Case C を含む)
---
@ -64,3 +64,30 @@ static box Main {
詳細ログ:
- `docs/development/current/main/investigations/phase132-llvm-exit-phi-wrong-result.md`
---
## 追加: Phase 132-P2Case CExit PHI ValueId collision fix
Case C`apps/tests/llvm_stage3_loop_only.hako`)の LLVM EXE 検証で、
**exit PHI の ValueId 衝突**が原因で `Result: 0` になる問題が見つかった。
### Root Cause
- `src/mir/builder/control_flow/joinir/merge/exit_phi_builder.rs`
PHI dst の割り当てに `builder.value_gen.next()`module-levelを使っており、
同一関数内で ValueId が衝突し得た。
### Fix
- PHI dst の割り当てを `func.next_value_id()`function-levelへ統一。
コミット:
- `bd07b7f4 fix(joinir): Phase 132-P2 - Exit PHI ValueId collision fix`
### Verification
- Pattern 1: VM/LLVM ともに `3`
- Case C: VM/LLVM ともに `Result: 3`
調査ログ:
- `docs/development/current/main/investigations/phase132-case-c-llvm-exe.md`