feat(llvm): Phase 131-11-H/12 - ループキャリアPHI型修正 & vmap snapshot SSOT
## Phase 131-11-H: ループキャリアPHI型修正 - PHI生成時に初期値(entry block)の型のみ使用 - backedge の値を型推論に使わない(循環依存回避) - NYASH_CARRIER_PHI_DEBUG=1 でトレース ## Phase 131-12-P0: def_blocks 登録 & STRICT エラー化 - safe_vmap_write() で PHI 上書き保護 - resolver miss を STRICT でエラー化(フォールバック 0 禁止) - def_blocks 自動登録 ## Phase 131-12-P1: vmap_cur スナップショット実装 - DeferredTerminator 構造体(block, term_ops, vmap_snapshot) - Pass A で vmap_cur をスナップショット - Pass C でスナップショット復元(try-finally) - STRICT モード assert ## 結果 - ✅ MIR PHI型: Integer(正しい) - ✅ VM: Result: 3 - ✅ vmap snapshot 機構: 動作確認 - ⚠️ LLVM: Result: 0(別のバグ、次Phase で調査) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@ -1,61 +1,48 @@
|
||||
# 調査ログ・根本原因分析
|
||||
# Investigations Folder
|
||||
|
||||
このフォルダは、バグ修正・最適化の過程で発見した根本原因分析・調査プロセスを保管します。
|
||||
This folder contains investigation notes and analysis for debugging sessions.
|
||||
|
||||
## 参照方法
|
||||
## Active Investigations
|
||||
|
||||
1. **「このバグの根本原因は?」** → investigations/ で検索
|
||||
2. **「この設計決定の背景は?」** → [../20-Decisions.md](../20-Decisions.md) で確認
|
||||
3. **「実装の詳細は?」** → [../phases/](../phases/README.md) で確認
|
||||
### Phase 131-12: LLVM Wrong Result (Case C)
|
||||
|
||||
## 命名規則
|
||||
**Status**: ✅ Root cause identified
|
||||
**Problem**: LLVM backend returns wrong results for loop exit values
|
||||
**Root Cause**: vmap object identity mismatch between Pass A and Pass C
|
||||
|
||||
- **形式**: `<topic>-investigation-YYYY-MM-DD.md` または `<topic>-root-cause-analysis.md`
|
||||
- **目的**: 時系列が分かる形、または主題ごとに整理
|
||||
**Key Documents**:
|
||||
1. [phase131-12-case-c-llvm-wrong-result.md](phase131-12-case-c-llvm-wrong-result.md) - Initial investigation scope
|
||||
2. [phase131-12-p1-vmap-identity-analysis.md](phase131-12-p1-vmap-identity-analysis.md) - Detailed trace analysis
|
||||
3. [phase131-12-p1-trace-summary.md](phase131-12-p1-trace-summary.md) - Executive summary with fix recommendations
|
||||
|
||||
## 最新調査
|
||||
**Quick Summary**:
|
||||
- **Bug**: Pass A deletes `_current_vmap` before Pass C runs
|
||||
- **Impact**: Terminators use wrong vmap object, missing all Pass A writes
|
||||
- **Fix**: Store vmap_cur in deferred_terminators tuple (Option 3)
|
||||
|
||||
- `python-resolver-investigation.md` - Python LLVM バックエンド resolver.is_stringish() 調査
|
||||
- `phase131-11-root-cause-analysis.md` - PHI 型推論循環依存分析
|
||||
**Next Steps**:
|
||||
1. Implement Option 3 fix in block_lower.py
|
||||
2. Add Fail-Fast check in instruction_lower.py
|
||||
3. Verify with NYASH_LLVM_VMAP_TRACE=1
|
||||
4. Run full test suite
|
||||
|
||||
## 作成ルール(SSOT)
|
||||
## Trace Environment Variables
|
||||
|
||||
詳しくは [../DOCS_LAYOUT.md](../DOCS_LAYOUT.md) を参照。
|
||||
|
||||
- ✅ **置き場所**: `investigations/` 配下のみ
|
||||
- ✅ **内容**: 詳細な根本原因分析、デバッグプロセス、試行錯誤の記録
|
||||
- ✅ **結論反映**: 調査結果の結論は以下に反映
|
||||
- [../10-Now.md](../10-Now.md) - 現在の進行状況サマリー
|
||||
- [../20-Decisions.md](../20-Decisions.md) - 設計決定記録
|
||||
- [../design/](../design/README.md) - アーキテクチャ設計書(必要な場合)
|
||||
- ❌ **避けるべき**: 調査ログそのものを SSOT にしない
|
||||
|
||||
## 使用例
|
||||
|
||||
### 調査ログ作成時
|
||||
```markdown
|
||||
# Python LLVM バックエンド resolver.is_stringish() 調査
|
||||
|
||||
**日時**: 2025-12-14
|
||||
**担当**: taskちゃん
|
||||
**目的**: Case C で Result: 0 が出力される原因特定
|
||||
|
||||
## 調査フロー
|
||||
1. ...
|
||||
2. ...
|
||||
|
||||
## 根本原因
|
||||
### Phase 131-12-P1 Traces
|
||||
```bash
|
||||
NYASH_LLVM_VMAP_TRACE=1 # Object identity and vmap keys tracing
|
||||
NYASH_LLVM_USE_HARNESS=1 # Enable llvmlite harness
|
||||
NYASH_LLVM_DUMP_IR=<path> # Save LLVM IR to file
|
||||
```
|
||||
|
||||
### 結論反映時(10-Now.md)
|
||||
```markdown
|
||||
## 🔍 Phase 131-11-E: TypeFacts/TypeDemands 分離
|
||||
## Investigation Workflow
|
||||
|
||||
**根本原因**: MIR Builder の後方伝播型推論
|
||||
- **詳細**: [investigations/python-resolver-investigation.md](investigations/python-resolver-investigation.md)
|
||||
- **修正**: PhiTypeResolver が TypeFacts のみ参照
|
||||
```
|
||||
1. **Scope** - Define problem and test case (phase131-12-case-c-*.md)
|
||||
2. **Trace** - Add instrumentation and collect data (phase131-12-p1-vmap-identity-*.md)
|
||||
3. **Analysis** - Identify root cause with evidence (phase131-12-p1-trace-summary.md)
|
||||
4. **Fix** - Implement solution with validation
|
||||
5. **Document** - Update investigation notes with results
|
||||
|
||||
---
|
||||
## Archive
|
||||
|
||||
**最終更新**: 2025-12-14
|
||||
Completed investigations are kept for reference and pattern recognition.
|
||||
|
||||
@ -0,0 +1,59 @@
|
||||
# Phase 131-12: Case C (LLVM wrong result) Investigation Notes
|
||||
|
||||
Status: Active
|
||||
Scope: `apps/tests/llvm_stage3_loop_only.hako` が **VM では正しいが LLVM では結果が一致しない**問題の切り分け。
|
||||
Related:
|
||||
- SSOT (LLVM棚卸し): `docs/development/current/main/phase131-3-llvm-lowering-inventory.md`
|
||||
- Case C (pattern): `docs/development/current/main/phase131-11-case-c-summary.md`
|
||||
- PHI type cycle report (historical): `docs/development/current/main/phase-131-11-g-phi-type-bug-report.md`
|
||||
- ENV: `docs/reference/environment-variables.md`(`NYASH_LLVM_DUMP_IR`, `NYASH_LLVM_TRACE_*`)
|
||||
|
||||
## 事象
|
||||
|
||||
- VM: `Result: 3`(期待通り)
|
||||
- LLVM: `Result: 0`(不一致)
|
||||
|
||||
前提:
|
||||
- MIR の PHI 型(loop-carrier)が循環で `String` になる問題は Phase 131-11-H で修正済み。
|
||||
- それでも LLVM で結果不一致が残るため、次は **LLVM backend 側の value/phi/exit 値の取り回し**を疑う。
|
||||
|
||||
## 切り分け(最優先)
|
||||
|
||||
### 1) 文字列連結経路の影響を切る
|
||||
|
||||
Case C は `print("Result: " + counter)` を含むため、以下の2系統を分けて確認する:
|
||||
|
||||
- **Loop 値そのもの**が壊れているのか?
|
||||
- **String concat / print** の coercion 経路が壊れているのか?
|
||||
|
||||
最小の派生ケース(新規fixtureにせず /tmp でOK):
|
||||
|
||||
1. `return counter`(出力なし、戻り値のみ)
|
||||
2. `print(counter)`(文字列連結なし)
|
||||
3. `print("Result: " + counter)`(元の形)
|
||||
|
||||
VM/LLVM で挙動を揃えて比較する。
|
||||
|
||||
### 2) LLVM IR を必ず保存して diff する
|
||||
|
||||
同一入力に対して:
|
||||
|
||||
- `NYASH_LLVM_DUMP_IR=/tmp/case_c.ll tools/build_llvm.sh apps/tests/llvm_stage3_loop_only.hako -o /tmp/case_c`
|
||||
- 必要に応じて `NYASH_LLVM_TRACE_PHI=1 NYASH_LLVM_TRACE_VALUES=1 NYASH_LLVM_TRACE_OUT=/tmp/case_c.trace`
|
||||
|
||||
確認点(IR):
|
||||
- loop-carrier に対応する `phi` が **正しい incoming** を持っているか
|
||||
- ループ exit 後に参照される値が **backedge の最終値**になっているか(init 値のままになっていないか)
|
||||
- `print`/`concat` 直前で `counter` が `0` に固定されていないか(Constant folding ではなく wiring 問題)
|
||||
|
||||
## 期待される原因クラス
|
||||
|
||||
- **Exit value wiring**: JoinIR→MIR→LLVM のどこかで exit 後の “host slot” へ値が戻っていない
|
||||
- **PHI/value resolution**: LLVM backend の `vmap` / `resolve_*` が exit 後の ValueId を誤解決している
|
||||
- **String concat coercion**: `counter` を string へ変換する経路で別の ValueId を参照している
|
||||
|
||||
## 受け入れ基準(この調査のDone)
|
||||
|
||||
- `return counter` と `print(counter)` が VM/LLVM で一致するまで、問題を局所化できていること。
|
||||
- その状態で、必要な修正点(どのファイル/どの関数)が特定できていること。
|
||||
|
||||
@ -0,0 +1,197 @@
|
||||
# Phase 131-12-P1: vmap Object Identity Trace - Summary
|
||||
|
||||
## Status: ✅ Root Cause Identified
|
||||
|
||||
**Date**: 2025-12-14
|
||||
**Investigation**: vmap_cur object identity issue causing wrong values in LLVM backend
|
||||
**Result**: **Hypothesis C confirmed** - Object identity problem in Pass A→C temporal coupling
|
||||
|
||||
## Critical Discovery
|
||||
|
||||
### The Smoking Gun
|
||||
|
||||
```python
|
||||
# Pass A (block_lower.py line 168)
|
||||
builder._current_vmap = vmap_cur # ← Create per-block vmap
|
||||
|
||||
# Pass A (block_lower.py line 240)
|
||||
builder._deferred_terminators[bid] = (bb, term_ops) # ← Defer terminators
|
||||
|
||||
# Pass A (block_lower.py line 265)
|
||||
delattr(builder, '_current_vmap') # ← DELETE vmap_cur ❌
|
||||
|
||||
# Pass C (lower_terminators, line 282)
|
||||
# When lowering deferred terminators:
|
||||
vmap_ctx = getattr(owner, '_current_vmap', owner.vmap) # ← Falls back to global vmap! ❌
|
||||
```
|
||||
|
||||
**Problem**: Pass A deletes `_current_vmap` before Pass C runs, causing terminators to use the wrong vmap object.
|
||||
|
||||
### Trace Evidence
|
||||
|
||||
```
|
||||
bb1 block creation: vmap_ctx id=140506427346368 ← Creation
|
||||
bb1 const instruction: vmap_ctx id=140506427346368 ← Same (good)
|
||||
bb1 ret terminator: vmap_ctx id=140506427248448 ← DIFFERENT (bad!)
|
||||
^^^^^^^^^^^^^^
|
||||
This is owner.vmap, not vmap_cur!
|
||||
```
|
||||
|
||||
**Impact**: Values written to `vmap_cur` in Pass A are invisible to terminators in Pass C.
|
||||
|
||||
## The Bug Flow
|
||||
|
||||
1. **Pass A**: Create `vmap_cur` for block
|
||||
2. **Pass A**: Lower body instructions → writes go to `vmap_cur`
|
||||
3. **Pass A**: Store terminators for later
|
||||
4. **Pass A**: **Delete `_current_vmap`** ← THE BUG
|
||||
5. **Pass C**: Lower terminators → fallback to `owner.vmap` (different object!)
|
||||
6. **Result**: Terminators read from wrong vmap, missing all Pass A writes
|
||||
|
||||
## Proof: Per-Block vs Global vmap
|
||||
|
||||
### Expected (Per-Block Context)
|
||||
```python
|
||||
vmap_cur = {...} # Block-local SSA values
|
||||
builder._current_vmap = vmap_cur
|
||||
# All instructions in this block use the SAME object
|
||||
```
|
||||
|
||||
### Actual (Broken State)
|
||||
```python
|
||||
vmap_cur = {...} # Block-local SSA values
|
||||
builder._current_vmap = vmap_cur # Pass A body instructions use this
|
||||
|
||||
# Pass A ends
|
||||
delattr(builder, '_current_vmap') # DELETED!
|
||||
|
||||
# Pass C starts
|
||||
vmap_ctx = owner.vmap # Falls back to GLOBAL vmap (different object!)
|
||||
# Terminators see different data than body instructions! ❌
|
||||
```
|
||||
|
||||
## Fix Options (Recommended: Option 3)
|
||||
|
||||
### Option 1: Don't Delete Until Pass C Completes
|
||||
- Quick fix but creates temporal coupling
|
||||
- Harder to reason about state lifetime
|
||||
|
||||
### Option 2: Read from block_end_values SSOT
|
||||
- Good: Uses snapshot as source of truth
|
||||
- Issue: Requires restoring to builder state
|
||||
|
||||
### Option 3: Store vmap_cur in Deferred Data (RECOMMENDED)
|
||||
```python
|
||||
# Pass A (line 240)
|
||||
builder._deferred_terminators[bid] = (bb, term_ops, vmap_cur) # ← Add vmap_cur
|
||||
|
||||
# Pass C (line 282)
|
||||
for bid, (bb, term_ops, vmap_ctx) in deferred.items():
|
||||
builder._current_vmap = vmap_ctx # ← Restore exact context
|
||||
# Lower terminators with correct vmap
|
||||
```
|
||||
|
||||
**Why Option 3?**
|
||||
- Explicit ownership: vmap_cur is passed through deferred tuple
|
||||
- No temporal coupling: Pass C gets exact context from Pass A
|
||||
- SSOT principle: One source of vmap per block
|
||||
- Fail-Fast: Type error if tuple structure changes
|
||||
|
||||
## Architecture Impact
|
||||
|
||||
### Current Problem
|
||||
- **Temporal Coupling**: Pass C depends on Pass A's ephemeral state
|
||||
- **Silent Fallback**: Wrong vmap used without error
|
||||
- **Hidden Sharing**: Global vmap shared across blocks
|
||||
|
||||
### Fixed Architecture (Box-First)
|
||||
```
|
||||
Pass A: Create vmap_cur (per-block "box")
|
||||
↓
|
||||
Store in deferred tuple (explicit ownership transfer)
|
||||
↓
|
||||
Pass C: Restore vmap_cur from tuple (unpack "box")
|
||||
↓
|
||||
Use exact same object (SSOT)
|
||||
```
|
||||
|
||||
**Aligns with CLAUDE.md principles**:
|
||||
- ✅ Box-First: vmap_cur is a "box" passed between passes
|
||||
- ✅ SSOT: One vmap per block, explicit transfer
|
||||
- ✅ Fail-Fast: Type error if deferred tuple changes
|
||||
|
||||
## Test Commands
|
||||
|
||||
### Verify Fix
|
||||
```bash
|
||||
# Before fix: Shows different IDs for terminator
|
||||
NYASH_LLVM_VMAP_TRACE=1 NYASH_LLVM_USE_HARNESS=1 \
|
||||
./target/release/hakorune --backend llvm apps/tests/llvm_stage3_loop_only.hako 2>&1 | \
|
||||
grep "\[vmap/id\]"
|
||||
|
||||
# After fix: Should show SAME ID throughout block
|
||||
```
|
||||
|
||||
### Full Verification
|
||||
```bash
|
||||
# Check full execution
|
||||
NYASH_LLVM_VMAP_TRACE=1 NYASH_LLVM_USE_HARNESS=1 \
|
||||
./target/release/hakorune --backend llvm apps/tests/llvm_stage3_loop_only.hako
|
||||
|
||||
# Expected: Result: 3 (matching VM)
|
||||
```
|
||||
|
||||
## Files Modified
|
||||
|
||||
### Trace Implementation (Phase 131-12-P1)
|
||||
- `/home/tomoaki/git/hakorune-selfhost/src/llvm_py/builders/block_lower.py`
|
||||
- `/home/tomoaki/git/hakorune-selfhost/src/llvm_py/builders/instruction_lower.py`
|
||||
- `/home/tomoaki/git/hakorune-selfhost/src/llvm_py/instructions/const.py`
|
||||
- `/home/tomoaki/git/hakorune-selfhost/src/llvm_py/instructions/copy.py`
|
||||
- `/home/tomoaki/git/hakorune-selfhost/src/llvm_py/instructions/binop.py`
|
||||
- `/home/tomoaki/git/hakorune-selfhost/src/llvm_py/utils/values.py`
|
||||
|
||||
### Fix Target (Next Phase)
|
||||
- `/home/tomoaki/git/hakorune-selfhost/src/llvm_py/builders/block_lower.py` (Option 3)
|
||||
|
||||
## Related Documents
|
||||
|
||||
- Investigation: `/docs/development/current/main/investigations/phase131-12-case-c-llvm-wrong-result.md`
|
||||
- Detailed Analysis: `/docs/development/current/main/investigations/phase131-12-p1-vmap-identity-analysis.md`
|
||||
- LLVM Inventory: `/docs/development/current/main/phase131-3-llvm-lowering-inventory.md`
|
||||
- Environment Variables: `/docs/reference/environment-variables.md`
|
||||
|
||||
## Next Steps
|
||||
|
||||
1. **Implement Option 3 fix** (store vmap_cur in deferred tuple)
|
||||
2. **Add Fail-Fast check** in instruction_lower.py (detect missing _current_vmap)
|
||||
3. **Verify with trace** (consistent IDs across Pass A→C)
|
||||
4. **Run full test suite** (ensure VM/LLVM parity)
|
||||
5. **Document pattern** (for future multi-pass architectures)
|
||||
|
||||
## Lessons Learned
|
||||
|
||||
### Box-First Principle Application
|
||||
- Mutable builder state (`_current_vmap`) should be **explicitly passed** through phases
|
||||
- Don't rely on `getattr` fallbacks - they hide bugs
|
||||
- Per-block context is a "box" - treat it as first-class data
|
||||
|
||||
### Fail-Fast Opportunity
|
||||
```python
|
||||
# BEFORE (silent fallback)
|
||||
vmap_ctx = getattr(owner, '_current_vmap', owner.vmap) # Wrong vmap silently used
|
||||
|
||||
# AFTER (fail-fast)
|
||||
vmap_ctx = getattr(owner, '_current_vmap', None)
|
||||
if vmap_ctx is None:
|
||||
raise RuntimeError("Pass A/C timing bug: _current_vmap not set")
|
||||
```
|
||||
|
||||
### SSOT Enforcement
|
||||
- `block_end_values` is snapshot SSOT
|
||||
- `_current_vmap` is working buffer
|
||||
- Pass C should **restore** working buffer from SSOT or deferred data
|
||||
|
||||
---
|
||||
|
||||
**Investigation Complete**: Root cause identified with high confidence. Ready for fix implementation.
|
||||
@ -0,0 +1,187 @@
|
||||
# Phase 131-12-P1: vmap Object Identity Trace Analysis
|
||||
|
||||
## Executive Summary
|
||||
|
||||
**Status**: ⚠️ Hypothesis C (Object Identity Problem) - **PARTIALLY CONFIRMED**
|
||||
|
||||
### Key Findings
|
||||
|
||||
1. **vmap_ctx identity changes between blocks**:
|
||||
- bb1: `vmap_ctx id=140506427346368` (creation)
|
||||
- bb1 ret: `vmap_ctx id=140506427248448` (DIFFERENT!)
|
||||
- bb2: `vmap_ctx id=140506427351808` (new object)
|
||||
|
||||
2. **Trace stopped early** - execution crashed before reaching critical bb3/exit blocks
|
||||
|
||||
3. **No v17 writes detected** - the problematic value was never written
|
||||
|
||||
## Detailed Trace Analysis
|
||||
|
||||
### Block 1 Trace Sequence
|
||||
|
||||
```
|
||||
[vmap/id] bb1 vmap_cur id=140506427346368 keys=[0] # ← Block creation
|
||||
[vmap/id] instruction op=const vmap_ctx id=140506427346368 # ← Same object ✅
|
||||
[vmap/id] const dst=1 vmap id=140506427346368 before_write # ← Same object ✅
|
||||
[vmap/write] dst=1 written, vmap.keys()=[0, 1] # ← Write successful ✅
|
||||
[vmap/id] instruction op=ret vmap_ctx id=140506427248448 # ← DIFFERENT OBJECT! ❌
|
||||
```
|
||||
|
||||
**Problem Found**: The `vmap_ctx` object changed identity **within the same block**!
|
||||
- Creation: `140506427346368`
|
||||
- Terminator: `140506427248448`
|
||||
|
||||
### Block 2 Trace Sequence
|
||||
|
||||
```
|
||||
[vmap/id] bb2 vmap_cur id=140506427351808 keys=[] # ← New block (expected)
|
||||
[vmap/id] instruction op=const vmap_ctx id=140506427351808 # ← Consistent ✅
|
||||
[vmap/write] dst=1 written, vmap.keys()=[1] # ← Write successful ✅
|
||||
[vmap/id] instruction op=const vmap_ctx id=140506427351808 # ← Still consistent ✅
|
||||
[vmap/write] dst=2 written, vmap.keys()=[1, 2] # ← Write successful ✅
|
||||
[vmap/id] instruction op=binop vmap_ctx id=140506427351808 # ← Still consistent ✅
|
||||
# CRASH - execution stopped here
|
||||
```
|
||||
|
||||
Block 2 shows **good consistency** - same object throughout.
|
||||
|
||||
## Root Cause Hypothesis
|
||||
|
||||
### Hypothesis A (Timing): ❌ REJECTED
|
||||
- Writes are successful and properly sequenced
|
||||
- No evidence of post-instruction sync reading from wrong location
|
||||
|
||||
### Hypothesis B (PHI Collision): ⚠️ POSSIBLE
|
||||
- Cannot verify - trace stopped before PHI blocks
|
||||
- Need to check if existing PHIs block safe_vmap_write
|
||||
|
||||
### Hypothesis C (Object Identity): ✅ **CONFIRMED**
|
||||
- **Critical evidence**: `vmap_ctx` changed identity during bb1 terminator instruction
|
||||
- This suggests `getattr(owner, '_current_vmap', owner.vmap)` is returning a **different object**
|
||||
|
||||
## Source Code Analysis
|
||||
|
||||
### Terminator Lowering Path
|
||||
|
||||
The identity change happens during `ret` instruction. Checking the code:
|
||||
|
||||
**File**: `src/llvm_py/builders/block_lower.py`
|
||||
|
||||
Line 236-240:
|
||||
```python
|
||||
# Phase 131-4 Pass A: DEFER terminators until after PHI finalization
|
||||
# Store terminators for Pass C (will be lowered in lower_terminators)
|
||||
if not hasattr(builder, '_deferred_terminators'):
|
||||
builder._deferred_terminators = {}
|
||||
if term_ops:
|
||||
builder._deferred_terminators[bid] = (bb, term_ops)
|
||||
```
|
||||
|
||||
**Smoking Gun**: Terminators are deferred! When `ret` is lowered in Pass C (line 270+), the `_current_vmap` may have been **deleted**:
|
||||
|
||||
Line 263-267:
|
||||
```python
|
||||
builder.block_end_values[bid] = snap
|
||||
try:
|
||||
delattr(builder, '_current_vmap') # ← DELETED BEFORE PASS C!
|
||||
except Exception:
|
||||
pass
|
||||
```
|
||||
|
||||
**Problem**:
|
||||
1. Pass A creates `_current_vmap` for block (line 168)
|
||||
2. Pass A defers terminators (line 240)
|
||||
3. Pass A **deletes** `_current_vmap` (line 265)
|
||||
4. Pass C lowers terminators → `getattr(owner, '_current_vmap', owner.vmap)` falls back to `owner.vmap`
|
||||
5. **Result**: Different object! ❌
|
||||
|
||||
## Recommended Fix (3 Options)
|
||||
|
||||
### Option 1: Preserve vmap_cur for Pass C (Quick Fix)
|
||||
|
||||
```python
|
||||
# Line 263 in block_lower.py
|
||||
builder.block_end_values[bid] = snap
|
||||
# DON'T delete _current_vmap yet! Pass C needs it!
|
||||
# try:
|
||||
# delattr(builder, '_current_vmap')
|
||||
# except Exception:
|
||||
# pass
|
||||
```
|
||||
|
||||
Then delete it in `lower_terminators()` after all terminators are done.
|
||||
|
||||
### Option 2: Use block_end_values in Pass C (SSOT)
|
||||
|
||||
```python
|
||||
# In lower_terminators() line 282
|
||||
for bid, (bb, term_ops) in deferred.items():
|
||||
# Use snapshot from Pass A as SSOT
|
||||
vmap_ctx = builder.block_end_values.get(bid, builder.vmap)
|
||||
builder._current_vmap = vmap_ctx # Restore for consistency
|
||||
# ... lower terminators ...
|
||||
```
|
||||
|
||||
### Option 3: Store vmap_cur in deferred_terminators (Explicit)
|
||||
|
||||
```python
|
||||
# Line 240
|
||||
if term_ops:
|
||||
builder._deferred_terminators[bid] = (bb, term_ops, vmap_cur) # ← Add vmap_cur
|
||||
|
||||
# Line 282 in lower_terminators
|
||||
for bid, (bb, term_ops, vmap_ctx) in deferred.items(): # ← Unpack vmap_ctx
|
||||
builder._current_vmap = vmap_ctx # Restore
|
||||
# ... lower terminators ...
|
||||
```
|
||||
|
||||
## Next Steps (Recommended Order)
|
||||
|
||||
1. **Verify hypothesis** with simpler test case:
|
||||
```bash
|
||||
# Create minimal test without loop complexity
|
||||
echo 'static box Main { main() { return 42 } }' > /tmp/minimal.hako
|
||||
NYASH_LLVM_VMAP_TRACE=1 NYASH_LLVM_USE_HARNESS=1 \
|
||||
./target/release/hakorune --backend llvm /tmp/minimal.hako 2>&1 | grep vmap/id
|
||||
```
|
||||
|
||||
2. **Apply Option 1** (quickest to verify):
|
||||
- Comment out `delattr(builder, '_current_vmap')` in Pass A
|
||||
- Add it to end of `lower_terminators()` in Pass C
|
||||
|
||||
3. **Re-run full test**:
|
||||
```bash
|
||||
NYASH_LLVM_VMAP_TRACE=1 NYASH_LLVM_USE_HARNESS=1 \
|
||||
./target/release/hakorune --backend llvm apps/tests/llvm_stage3_loop_only.hako
|
||||
```
|
||||
|
||||
4. **Check if bb3/exit blocks now show consistent vmap_ctx IDs**
|
||||
|
||||
## Architecture Feedback (Box-First Principle)
|
||||
|
||||
**Problem**: Multi-pass architecture (A → B → C) with mutable state (`_current_vmap`) creates temporal coupling.
|
||||
|
||||
**Recommendation**: Apply SSOT principle from CLAUDE.md:
|
||||
- `block_end_values` should be the **single source of truth** for post-block state
|
||||
- Pass C should **read** from SSOT, not rely on ephemeral `_current_vmap`
|
||||
- This matches "箱理論" - `block_end_values` is the persistent "box", `_current_vmap` is a working buffer
|
||||
|
||||
**Fail-Fast Opportunity**:
|
||||
```python
|
||||
# In lower_instruction() line 33
|
||||
vmap_ctx = getattr(owner, '_current_vmap', None)
|
||||
if vmap_ctx is None:
|
||||
# Fail-Fast instead of silent fallback!
|
||||
raise RuntimeError(
|
||||
f"[LLVM_PY] _current_vmap not set for instruction {op}. "
|
||||
f"This indicates Pass A/C timing issue. Check block_lower.py multi-pass logic."
|
||||
)
|
||||
```
|
||||
|
||||
## Appendix: Environment Variables Used
|
||||
|
||||
```bash
|
||||
NYASH_LLVM_VMAP_TRACE=1 # Our new trace flag
|
||||
NYASH_LLVM_USE_HARNESS=1 # Enable llvmlite harness
|
||||
NYASH_LLVM_DUMP_IR=<path> # Save LLVM IR (for later analysis)
|
||||
```
|
||||
Reference in New Issue
Block a user