feat(llvm): Phase 132 - Pattern 1 exit value parity fix + Box-First refactoring

## Phase 132: Exit PHI Value Parity Fix

### Problem
Pattern 1 (Simple While) returned 0 instead of final loop variable value (3)
- VM: RC: 3  (correct)
- LLVM: Result: 0  (wrong)

### Root Cause (Two Layers)
1. **JoinIR/Boundary**: Missing exit_bindings → ExitLineReconnector not firing
2. **LLVM Python**: block_end_values snapshot dropping PHI values

### Fix
**JoinIR** (simple_while_minimal.rs):
- Jump(k_exit, [i_param]) passes exit value

**Boundary** (pattern1_minimal.rs):
- Added LoopExitBinding with carrier_name="i", role=LoopState
- Enables ExitLineReconnector to update variable_map

**LLVM** (block_lower.py):
- Use predeclared_ret_phis for reliable PHI filtering
- Protect builder.vmap PHIs from overwrites (SSOT principle)

### Result
-  VM: RC: 3
-  LLVM: Result: 3
-  VM/LLVM parity achieved

## Phase 132-Post: Box-First Refactoring

### Rust Side
**JoinModule::require_function()** (mod.rs):
- Encapsulate function search logic
- 10 lines → 1 line (90% reduction)
- Reusable for Pattern 2-5

### Python Side
**PhiManager Box** (phi_manager.py - new):
- Centralized PHI lifecycle management
- 47 lines → 8 lines (83% reduction)
- SSOT: builder.vmap owns PHIs
- Fail-Fast: No silent overwrites

**Integration**:
- LLVMBuilder: Added phi_manager
- block_lower.py: Delegated to PhiManager
- tagging.py: Register PHIs with manager

### Documentation
**New Files**:
- docs/development/architecture/exit-phi-design.md
- docs/development/current/main/investigations/phase132-llvm-exit-phi-wrong-result.md
- docs/development/current/main/phases/phase-132/

**Updated**:
- docs/development/current/main/10-Now.md
- docs/development/current/main/phase131-3-llvm-lowering-inventory.md

### Design Principles
- Box-First: Logic encapsulated in classes/methods
- SSOT: Single Source of Truth (builder.vmap for PHIs)
- Fail-Fast: Early explicit failures, no fallbacks
- Separation of Concerns: 3-layer architecture

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

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
nyash-codex
2025-12-15 03:17:31 +09:00
parent a955dd6b18
commit 447d4ea246
16 changed files with 669 additions and 54 deletions

View File

@ -1,5 +1,14 @@
# Self Current Task — Now (main)
## 20251215Phase 132 完了 ✅
**Phase 132: LLVM Exit PHI=0 根治修正 完了!**
- ループ exit PHI が 0 を返す問題を根治解決
- 原因2層: (1) JoinIR/Boundary が exit 値を境界に渡していない、(2) LLVM Python が PHI を落とす/上書きする
- 修正: Pattern 1 で exit 値を明示的に渡す + `predeclared_ret_phis` 使用 + `builder.vmap` の PHI 保護
- 結果: `/tmp/p1_return_i.hako` が 3 を返すVM 一致)
- 詳細: `investigations/phase132-llvm-exit-phi-wrong-result.md`
## 20251214現状サマリ
補足docs が増えて迷子になったときの「置き場所ルールSSOT」:
@ -79,7 +88,8 @@
- `docs/development/current/main/phase131-2-box-resolution-map.md`
- LLVMPython llvmlitelowering の棚卸しPhase 131-3..10:
- `docs/development/current/main/phase131-3-llvm-lowering-inventory.md`
- 状態: Case BPattern1/loop_min_whileは EMIT/LINK/RUN まで復旧済み。残りは Case C の JoinIR ループパターン穴
- 状態: Case BPattern1/loop_min_whileは EMIT/LINK/RUN まで復旧済み。Phase 132 で `return i` の VM/LLVM parity を固定
- Case C は別途 “Case C docs” を SSOT にして追跡する(状況は更新されるので、この箇所では断定しない)
- Case C の調査と実装計画:
- `docs/development/current/main/phase131-11-case-c-summary.md`
- `docs/development/current/main/case-c-infinite-loop-analysis.md`

View File

@ -0,0 +1,216 @@
# Phase 132: LLVM Exit PHI=0 Bug Investigation & Fix
**Date**: 2025-12-15
**Status**: ✅ Fixed
**Impact**: Critical - Exit PHIs from loops were returning 0 instead of correct values
## Problem Statement
LLVM backend was returning 0 for exit PHI values in simple while loops, while VM backend correctly returned the final loop variable value.
### Test Case
```nyash
static box Main {
main() {
local i = 0
loop(i < 3) { i = i + 1 }
return i // Should return 3, was returning 0 in LLVM
}
}
```
**Expected**: Result: 3 (matches VM)
**Actual (before fix)**: Result: 0
**MIR**: Correct (`bb3: %1 = phi [%3, bb6]; ret %1`)
**LLVM IR (before fix)**: `ret i64 0` (wrong!)
**LLVM IR (after fix)**: `ret i64 %"phi_1"` (correct!)
## Root Cause Analysis
この不具合は「2層」にまたがっていました:
1. JoinIR/Boundary 層で exit 値が境界を通っていないVM でも 0 になり得る)
2. LLVM Python 層で PHI の SSOT が壊れていて exit PHI が 0 になるVM は正常でも LLVM が壊れる)
このページは主に (2) の LLVM Python 層の根治を記録します。
(1) の修正は Phase 132 の一部として別途コード側で入っています(修正ファイル一覧を参照)。
### Investigation Path
1. **PHI filtering issue in vmap_cur initialization**
- ✅ Confirmed: Filter relied on `phi.basic_block` attribute
- Issue: llvmlite sets `phi.basic_block = None` until IR finalization
- Filter at block_lower.py:323-365 was silently dropping ALL PHIs
2. **builder.vmap overwrite issue**
- ✅ Confirmed: The real root cause!
### The Actual Bug
**Two separate issues** combined to cause the bug:
#### Issue 1: Unreliable PHI.basic_block Attribute
- llvmlite's PHI instructions have `basic_block = None` when created
- Filter logic at block_lower.py:326-340 relied on `phi.basic_block.name` comparison
- Since `basic_block` was always None, filter excluded ALL PHIs from vmap_cur
- **Fix**: Use `predeclared_ret_phis` dict instead of `basic_block` attribute
#### Issue 2: builder.vmap PHI Overwrites (Critical!)
At block_lower.py:437-448, Pass A syncs created values to builder.vmap:
```python
# Phase 131-7: Sync ALL created values to global vmap
for vid in created_ids:
val = vmap_cur.get(vid)
if val is not None:
builder.vmap[vid] = val # ❌ Unconditional overwrite!
```
**The Fatal Sequence**:
1. Pass A setup: Creates PHI v1 for bb3, stores in `builder.vmap[1]`
2. Pass A processes bb0:
- vmap_cur filters out v1 PHI (not from bb0)
- const v1 instruction writes to vmap_cur[1]
- **Line 444: Syncs vmap_cur[1] → builder.vmap[1], overwriting PHI!**
3. Pass A processes bb3:
- vmap_cur initialized from builder.vmap
- builder.vmap[1] is now the const (not PHI!)
- return v1 uses const 0 instead of PHI
## The Fix
### Fix 1: PHI Filtering (block_lower.py:320-347)
**Before** (unreliable basic_block check):
```python
if hasattr(_val, 'add_incoming'):
bb_of = getattr(getattr(_val, 'basic_block', None), 'name', None)
bb_name = getattr(bb, 'name', None)
keep = (bb_of == bb_name) # ❌ Always False! bb_of is None
```
**After** (use predeclared_ret_phis dict):
```python
if hasattr(_val, 'add_incoming'): # Is it a PHI?
phi_key = (int(bid), int(_vid))
if phi_key in predecl_phis:
keep = True # ✅ Reliable tracking
else:
keep = False # Avoid namespace collision
```
### Fix 2: Protect builder.vmap PHIs (block_lower.py:437-455)
**Before** (unconditional overwrite):
```python
for vid in created_ids:
val = vmap_cur.get(vid)
if val is not None:
builder.vmap[vid] = val # ❌ Overwrites PHIs!
```
**After** (PHI protection):
```python
for vid in created_ids:
val = vmap_cur.get(vid)
if val is not None:
existing = builder.vmap.get(vid)
# Don't overwrite existing PHIs - SSOT principle
if existing is not None and hasattr(existing, 'add_incoming'):
continue # ✅ Skip sync, preserve PHI
builder.vmap[vid] = val
```
## Verification
### Test Results
```bash
# ✅ LLVM matches VM
NYASH_LLVM_USE_HARNESS=1 NYASH_LLVM_STRICT=1 ./target/release/hakorune --backend llvm /tmp/p1_return_i.hako
# Output: Result: 3
# ✅ VM baseline
./target/release/hakorune --backend vm /tmp/p1_return_i.hako
# Output: RC: 3
```
### Generated LLVM IR Comparison
**Before** (wrong):
```llvm
bb3:
%"phi_1" = phi i64 [%"phi_3", %"bb6"]
ret i64 0 ; ❌ Hardcoded 0!
```
**After** (correct):
```llvm
bb3:
%"phi_1" = phi i64 [%"phi_3", %"bb6"]
ret i64 %"phi_1" ; ✅ Uses PHI value!
```
## Design Lessons
### The SSOT Principle
**builder.vmap is the Single Source of Truth for PHI nodes**:
- PHIs are created once in setup_phi_placeholders
- PHIs must NEVER be overwritten by later instructions
- vmap_cur is per-block and must filter PHIs correctly
### PHI Ownership Tracking
**llvmlite limitation**: PHI.basic_block is None until finalization
**Solution**: Explicit tracking via `predeclared_ret_phis: Dict[(block_id, value_id), PHI]`
### Fail-Fast vs Silent Failures
The original filter silently dropped PHIs via broad exception handling:
```python
except Exception:
keep = False # ❌ Silent failure!
```
**Better approach**: Explicit checks with trace logging for debugging.
## Related Issues
- Phase 131: Block_end_values SSOT system
- Phase 131-12: VMap snapshot investigation
- Phase 131-14-B: Jump-only block resolution
## Files Modified
### JoinIR/Boundary 層exit 値の SSOT を境界で明示)
- `src/mir/join_ir/lowering/simple_while_minimal.rs``Jump(k_exit, [i_param])`
- `src/mir/builder/control_flow/joinir/patterns/pattern1_minimal.rs``LoopExitBinding` を作って境界へ設定)
### LLVM Python 層PHI SSOT の維持)
- `src/llvm_py/builders/block_lower.py`
- PHI filtering を `predeclared_ret_phis` ベースへ変更(`phi.basic_block` 依存を排除)
- `builder.vmap` へ sync する際、既存 PHI を上書きしないPHI を SSOT として保護)
## Debug Environment Variables
```bash
NYASH_LLVM_STRICT=1 # Fail-fast on errors
NYASH_LLVM_TRACE_PHI=1 # PHI wiring traces
NYASH_LLVM_TRACE_VMAP=1 # VMap operation traces
NYASH_LLVM_DUMP_IR=/tmp/x.ll # Dump generated IR
```
## Acceptance Criteria
`/tmp/p1_return_i.hako` returns 3 in LLVM (was 0)
✅ STRICT mode enabled, no fallback to 0
✅ VM and LLVM results match
✅ No regression on Phase 131 test cases
✅ Generated LLVM IR uses `ret i64 %phi_1` not `ret i64 0`
## Next Steps
- [ ] Add regression test for exit PHI patterns
- [ ] Document PHI ownership model in the LLVM harness docs (SSOT: `phase131-3-llvm-lowering-inventory.md`)

View File

@ -12,9 +12,24 @@
| B2 | `/tmp/case_b_simple.hako` | ✅ | ✅ | ✅ | **PASS** - Simple print(42) without loop works |
| C | `apps/tests/llvm_stage3_loop_only.hako` | ✅ | ✅ | ⚠️ | **TAG-RUN** - Loop ok; print/concat path segfaults |
## Phase 132 Update (2025-12-15)
**MAJOR FIX**: Exit PHI wrong result bug fixed!
- **Issue**: Pattern 1 の `return i` が LLVM だけ 0 を返すVM は 3
- **Test**: `/tmp/p1_return_i.hako` が VM/LLVM ともに 3 を返すようになった
- **Root Cause (two-layer)**:
- JoinIR/Boundary: exit 値が境界を通っていなかったexit_bindings / Jump args
- LLVM Python: builder.vmap の PHI placeholder が上書きされ、`ret` が 0 を参照
- **Fix**:
- JoinIR/Boundary: Pattern 1 で exit 値を明示的に渡す + LoopExitBinding を境界へ設定
- LLVM Python: `predeclared_ret_phis` で PHI 所有を追跡し、PHI placeholder を上書きしない
- **Details**:
- Investigation: [phase132-llvm-exit-phi-wrong-result.md](investigations/phase132-llvm-exit-phi-wrong-result.md)
- Phase summary: [phases/phase-132/README.md](phases/phase-132/README.md)
## Root Causes Identified
### 1. TAG-EMIT: Loop PHI → Invalid LLVM IR (Case B)
### 1. TAG-EMIT: Loop PHI → Invalid LLVM IR (Case B) ✅ FIXED (Phase 131-10)
**File**: `apps/tests/loop_min_while.hako`

View File

@ -4,6 +4,7 @@
## 現在の Phase
- **Phase 132**: Exit Values Parity (VM == LLVM)
- **Phase 131**: LLVM Lowering & InfiniteEarlyExit パターン実装 🚀
- **Phase 33**: Box Theory Modularization
@ -41,4 +42,4 @@ phases/phase-131/
---
**最終更新**: 2025-12-14
**最終更新**: 2025-12-15

View File

@ -0,0 +1,66 @@
# Phase 132: Exit Values Parity (VM == LLVM)
**Date**: 2025-12-15
**Status**: ✅ Done
**Scope**: Pattern 1Simple Whileで「ループ終了後の `return i`」が VM/LLVM で一致することを固定する
---
## 背景
最小ケース:
```nyash
static box Main {
main() {
local i = 0
loop(i < 3) { i = i + 1 }
return i // 期待: 3
}
}
```
- VM: `RC: 3`
- LLVM: `Result: 0` ❌(修正前)
---
## 根本原因2層
### 1) JoinIR/Boundary 層: exit 値が境界を通っていない
- Pattern 1 の JoinIR で `k_exit` に渡す args が空のままだと、exit 側でホストの `variable_map["i"]` が更新されない。
- その結果 `return i` が初期値0を返し得る。
### 2) LLVM Python 層: PHI の SSOT が壊れている
- `phi.basic_block` が llvmlite で確定前は `None` になり得るため、PHI を落とす filter を信頼できない。
- `builder.vmap` にある PHI placeholder を、Pass A の “sync created values” が上書きしてしまい、`ret` が PHI ではなく `0` を参照することがある。
---
## 修正内容
### JoinIR/Boundary 層exit 値を明示的に渡す)
- `src/mir/join_ir/lowering/simple_while_minimal.rs`
- `Jump(k_exit, [i_param])` として exit 値を渡す
- `src/mir/builder/control_flow/joinir/patterns/pattern1_minimal.rs`
- `LoopExitBinding` を作成して `with_exit_bindings()` で境界に設定
### LLVM Python 層PHI SSOT を保護する)
- `src/llvm_py/builders/block_lower.py`
- PHI filtering を `predeclared_ret_phis` ベースにし、`phi.basic_block` 依存を排除
- `builder.vmap` の既存 PHIplaceholderを上書きしないPHI を SSOT として保護)
---
## 検証
- `/tmp/p1_return_i.hako` が VM/LLVM で `3` を返す
- `NYASH_LLVM_STRICT=1` でも “miss→0” に落ちないFail-Fast が維持される)
詳細ログ:
- `docs/development/current/main/investigations/phase132-llvm-exit-phi-wrong-result.md`