refactor(joinir): Phase 189 - Remove hardcoded 'sum' variable, generalize exit PHI connection
Key improvements: 1. **Eliminate hardcoded variable name**: Replace hardcoded "sum" with generic ValueId-based variable_map updates. Add new JoinInlineBoundary constructor `new_with_input_and_host_outputs()` for Pattern 3. 2. **Generalize output slot mapping**: Exit PHI result now updates all host_outputs entries in variable_map, not just hardcoded "sum". Prepares for future multi-carrier patterns. 3. **PHI preservation in blocks**: Fix block finalization to preserve existing PHI instructions (from handle_select) instead of overwriting them. 4. **Stable function name→ValueId mapping**: Ensure consistent ValueId assignment for tail call detection across multi-function merges. 5. **Enhanced debugging**: Add detailed logging in block converter and meta analysis for PHI verification. Files modified: - src/mir/builder/control_flow.rs: Remove hardcoded "sum", use boundary outputs - src/mir/join_ir/lowering/inline_boundary.rs: Add new constructor - src/mir/join_ir_vm_bridge/joinir_block_converter.rs: Stable mappings, PHI preservation - src/mir/join_ir_vm_bridge/meta.rs: Debug output for PHI tracking - src/mir/builder/joinir_id_remapper.rs: PHI value remapping - src/mir/builder/joinir_inline_boundary_injector.rs: Span preservation Test status: Pattern 3 (loop_if_phi.hako) still produces correct result (sum=9, RC=9) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: ChatGPT <noreply@openai.com> Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@ -1,3 +1,4 @@
|
||||
use crate::ast::Span;
|
||||
use crate::mir::optimizer::MirOptimizer;
|
||||
use crate::mir::optimizer_stats::OptimizationStats;
|
||||
use crate::mir::{MirInstruction as I, MirModule};
|
||||
@ -15,10 +16,15 @@ pub fn optimize_boxfield_operations(
|
||||
for (_bb_id, block) in &mut function.blocks {
|
||||
let mut changed = 0usize;
|
||||
let mut out: Vec<I> = Vec::with_capacity(block.instructions.len());
|
||||
let mut out_spans: Vec<Span> = Vec::with_capacity(block.instruction_spans.len());
|
||||
let mut i = 0usize;
|
||||
while i < block.instructions.len() {
|
||||
// Get span for current instruction (or unknown if missing)
|
||||
let span_i = block.instruction_spans.get(i).copied().unwrap_or_else(Span::unknown);
|
||||
|
||||
// Look ahead for simple store-followed-by-load on same box/index
|
||||
if i + 1 < block.instructions.len() {
|
||||
let span_i1 = block.instruction_spans.get(i + 1).copied().unwrap_or_else(Span::unknown);
|
||||
match (&block.instructions[i], &block.instructions[i + 1]) {
|
||||
(
|
||||
I::BoxCall {
|
||||
@ -42,10 +48,12 @@ pub fn optimize_boxfield_operations(
|
||||
// Replace the second with Copy from just-stored value
|
||||
let src_val = a1[1];
|
||||
out.push(block.instructions[i].clone());
|
||||
out_spans.push(span_i);
|
||||
out.push(I::Copy {
|
||||
dst: *dst2,
|
||||
src: src_val,
|
||||
});
|
||||
out_spans.push(span_i1);
|
||||
changed += 1;
|
||||
i += 2;
|
||||
continue;
|
||||
@ -55,10 +63,12 @@ pub fn optimize_boxfield_operations(
|
||||
}
|
||||
}
|
||||
out.push(block.instructions[i].clone());
|
||||
out_spans.push(span_i);
|
||||
i += 1;
|
||||
}
|
||||
if changed > 0 {
|
||||
block.instructions = out;
|
||||
block.instruction_spans = out_spans;
|
||||
stats.boxfield_optimizations += changed;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user