refactor(llvm): Phase 132-P1 follow-up - bind block_end_values and clear predeclared PHIs

This commit is contained in:
nyash-codex
2025-12-15 11:47:58 +09:00
parent e0fb6fecf6
commit a15d1e97e6
3 changed files with 41 additions and 1 deletions

View File

@ -27,6 +27,16 @@
- 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)
## Phase 132-P1 Update (2025-12-15)
**STRUCTURAL FIX**: Function-local state isolation in LLVM Python backend
- **Issue**: block id / value id が “関数をまたいで” 衝突し得るsnapshot / cache / PHI state の漏洩)
- **Fix**: `FunctionLowerContext` Box で function-scoped state を隔離し、tuple-key workaround を不要化
- **Code**:
- `src/llvm_py/context/function_lower_context.py`
- `src/llvm_py/builders/function_lower.py`
## Root Causes Identified
### 1. TAG-EMIT: Loop PHI → Invalid LLVM IR (Case B) ✅ FIXED (Phase 131-10)

View File

@ -110,3 +110,24 @@ JoinIR merge の契約検証debug buildで **早期に Fail-Fast** する
期待される振る舞い:
- debug build では compile/run 中に panic で即停止(原因と修正案が出る)
- release build の既定挙動は変えない(検証は `debug_assertions` のみ)
---
## 追加: Phase 132-P1LLVM PythonFunctionLowerContext Box
Phase 131132 で顕在化した「関数間の状態漏洩block id / value id の衝突)」を、
tuple-key の小手先ではなく **構造で根治**するために、関数ローカル状態を 1 箱に隔離した。
- 実装: `src/llvm_py/context/function_lower_context.py`
- 統合: `src/llvm_py/builders/function_lower.py`
箱の責務SSOT:
- `block_end_values`snapshot
- `def_blocks`
- `jump_only_blocks`
- `phi_manager`
- resolver cachesi64/ptr/f64/end_i64 など)
狙い:
- 関数単位で state を自動破棄し、cross-function collision を “起こせない構造” にする
- 追加の手動クリアや tuple-key を不要にする

View File

@ -51,6 +51,14 @@ def lower_function(builder, func_data: Dict[str, Any]):
builder.bb_map.clear()
except Exception:
builder.bb_map = {}
# Phase 132-P1: Clear per-function predeclared PHI placeholders (avoid cross-function leakage)
try:
builder.predeclared_ret_phis.clear()
except Exception:
try:
builder.predeclared_ret_phis = {}
except Exception:
pass
# Phase 132-P1: Create function-local context Box
# This automatically isolates all function-scoped state
@ -63,6 +71,7 @@ def lower_function(builder, func_data: Dict[str, Any]):
builder.phi_manager = context.phi_manager
builder.block_phi_incomings = context.block_phi_incomings
builder.def_blocks = context.def_blocks
builder.block_end_values = context.block_end_values
# Bind resolver to context (redirects caches to context storage)
builder.resolver.bind_context(context)
@ -311,7 +320,7 @@ def lower_function(builder, func_data: Dict[str, Any]):
vmap=builder.vmap,
bb_map=builder.bb_map,
preds=builder.preds,
block_end_values=builder.block_end_values,
block_end_values=context.block_end_values,
resolver=builder.resolver,
trace_phi=os.environ.get('NYASH_LLVM_TRACE_PHI') == '1',
verbose=os.environ.get('NYASH_CLI_VERBOSE') == '1',