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

@ -25,13 +25,13 @@
//!
//! fn loop_step(i):
//! exit_cond = !(i < 3)
//! Jump(k_exit, [], cond=exit_cond) // early return if i >= 3
//! print(i) // body
//! i_next = i + 1 // increment
//! Call(loop_step, [i_next]) // tail recursion
//! Jump(k_exit, [i], cond=exit_cond) // Phase 132: pass i to k_exit
//! print(i) // body
//! i_next = i + 1 // increment
//! Call(loop_step, [i_next]) // tail recursion
//!
//! fn k_exit():
//! return 0
//! fn k_exit(i_exit): // Phase 132: receives loop variable
//! return i_exit // Phase 132: return loop value
//! ```
//!
//! ## Design Notes
@ -118,7 +118,8 @@ pub(crate) fn lower_simple_while_minimal(
let i_next = alloc_value(); // ValueId(8) - i + 1
// k_exit locals
let const_0_exit = alloc_value(); // ValueId(9) - exit return value
// Phase 132: i_exit receives loop variable from Jump
let i_exit = alloc_value(); // ValueId(9) - exit parameter (loop variable)
// ==================================================================
// main() function
@ -181,10 +182,11 @@ pub(crate) fn lower_simple_while_minimal(
operand: cmp_lt,
}));
// Jump(k_exit, [], cond=exit_cond)
// Phase 132: Jump(k_exit, [i_param], cond=exit_cond)
// Pass loop variable to exit continuation for return value parity
loop_step_func.body.push(JoinInst::Jump {
cont: k_exit_id.as_cont(),
args: vec![],
args: vec![i_param],
cond: Some(exit_cond),
});
@ -224,19 +226,14 @@ pub(crate) fn lower_simple_while_minimal(
join_module.add_function(loop_step_func);
// ==================================================================
// k_exit() function
// k_exit(i_exit) function - Phase 132: receives loop variable
// ==================================================================
let mut k_exit_func = JoinFunction::new(k_exit_id, "k_exit".to_string(), vec![]);
// return 0 (Pattern 1 has no exit values)
// Phase 188-Impl-3: Use pre-allocated const_0_exit (ValueId(9))
k_exit_func.body.push(JoinInst::Compute(MirLikeInst::Const {
dst: const_0_exit,
value: ConstValue::Integer(0),
}));
let mut k_exit_func = JoinFunction::new(k_exit_id, "k_exit".to_string(), vec![i_exit]);
// Phase 132: return i_exit (loop variable at exit)
// This ensures VM/LLVM parity for `return i` after loop
k_exit_func.body.push(JoinInst::Ret {
value: Some(const_0_exit),
value: Some(i_exit),
});
join_module.add_function(k_exit_func);

View File

@ -585,6 +585,18 @@ impl JoinModule {
pub fn mark_normalized(&mut self) {
self.phase = JoinIrPhase::Normalized;
}
// Phase 132-Post: Box-First principle - encapsulate function search logic
/// Find function by name
pub fn get_function_by_name(&self, name: &str) -> Option<&JoinFunction> {
self.functions.values().find(|f| f.name == name)
}
/// Find function by name or panic with descriptive message
pub fn require_function(&self, name: &str, context: &str) -> &JoinFunction {
self.get_function_by_name(name)
.unwrap_or_else(|| panic!("{}: missing required function '{}'", context, name))
}
}
impl Default for JoinModule {