fix(joinir): Phase 220-C condition variable remap and self-copy skip

- Pre-populate remap with condition_bindings (join_value → host_value)
- Skip self-copy param bindings to avoid `%6 = copy %6`
- ConditionEnv remap verified: ValueId(101) → ValueId(6) correctly

Note: RC=0 issue remains - ExprResult routing to be investigated in Phase 221

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
nyash-codex
2025-12-10 03:20:09 +09:00
parent 2fc2cf74d1
commit 757ba6b877
3 changed files with 112 additions and 20 deletions

View File

@ -87,19 +87,12 @@ pub(in crate::mir::builder) fn merge_joinir_mir_blocks(
let (mut used_values, value_to_func_name, function_params) =
value_collector::collect_values(mir_module, &remapper, debug)?;
// Phase 171-fix: Add condition_bindings' join_values to used_values for remapping
if let Some(boundary) = boundary {
for binding in &boundary.condition_bindings {
if debug {
eprintln!(
"[cf_loop/joinir] Phase 171-fix: Adding condition binding '{}' JoinIR {:?} to used_values",
binding.name, binding.join_value
);
}
used_values.insert(binding.join_value);
}
// Phase 220-C: Condition bindings should NOT be added to used_values
// They will be remapped directly to HOST values in remap_values()
// (Removed Phase 171-fix logic)
// Phase 172-3: Add exit_bindings' join_exit_values to used_values for remapping
// Phase 172-3: Add exit_bindings' join_exit_values to used_values for remapping
if let Some(boundary) = boundary {
for binding in &boundary.exit_bindings {
if debug {
eprintln!(
@ -197,7 +190,8 @@ pub(in crate::mir::builder) fn merge_joinir_mir_blocks(
}
// Phase 3: Remap ValueIds (with reserved PHI dsts protection)
remap_values(builder, &used_values, &mut remapper, &reserved_phi_dsts, debug)?;
// Phase 220-C: Pre-populate condition_bindings BEFORE remap_values
remap_values(builder, &used_values, &mut remapper, &reserved_phi_dsts, boundary, debug)?;
// Phase 177-3 DEBUG: Verify remapper state after Phase 3
eprintln!("[DEBUG-177] === Remapper state after Phase 3 ===");
@ -618,11 +612,15 @@ pub(in crate::mir::builder) fn merge_joinir_mir_blocks(
/// Phase 201-A: Accept reserved ValueIds that must not be reused.
/// These are PHI dst ValueIds that will be created by LoopHeaderPhiBuilder.
/// We must skip these IDs to prevent carrier value corruption.
///
/// Phase 220-C: Pre-populate condition_bindings to use HOST ValueIds directly.
/// Condition variables should NOT get new allocations - they use HOST values.
fn remap_values(
builder: &mut crate::mir::builder::MirBuilder,
used_values: &std::collections::BTreeSet<ValueId>,
remapper: &mut crate::mir::builder::joinir_id_remapper::JoinIrIdRemapper,
reserved_ids: &std::collections::HashSet<ValueId>,
boundary: Option<&JoinInlineBoundary>,
debug: bool,
) -> Result<(), String> {
if debug {
@ -630,7 +628,32 @@ fn remap_values(
used_values.len(), reserved_ids.len());
}
// Phase 220-C: Pre-populate condition_bindings BEFORE normal remapping
// Condition variables use HOST ValueIds directly (no new allocation)
if let Some(boundary) = boundary {
for binding in &boundary.condition_bindings {
remapper.set_value(binding.join_value, binding.host_value);
if debug {
eprintln!(
"[cf_loop/joinir] Phase 220-C: Condition '{}': JoinIR {:?} → HOST {:?} (no allocation)",
binding.name, binding.join_value, binding.host_value
);
}
}
}
for old_value in used_values {
// Phase 220-C: Skip if already mapped (e.g., condition_bindings)
if remapper.get_value(*old_value).is_some() {
if debug {
eprintln!(
"[cf_loop/joinir] Phase 220-C: Skipping {:?} (already mapped)",
old_value
);
}
continue;
}
// Phase 201-A: Allocate new ValueId, skipping reserved PHI dsts
let new_value = loop {
let candidate = builder.next_value_id();