feat(joinir): Phase 200-B/C/D capture analysis + Phase 201-A reserved_value_ids infra
Phase 200-B: FunctionScopeCaptureAnalyzer implementation - analyze_captured_vars_v2() with structural loop matching - CapturedEnv for immutable function-scope variables - ParamRole::Condition for condition-only variables Phase 200-C: ConditionEnvBuilder extension - build_with_captures() integrates CapturedEnv into ConditionEnv - fn_body propagation through LoopPatternContext to Pattern 2 Phase 200-D: E2E verification - capture detection working for base, limit, n etc. - Test files: phase200d_capture_minimal.hako, phase200d_capture_in_condition.hako Phase 201-A: MirBuilder reserved_value_ids infrastructure - reserved_value_ids: HashSet<ValueId> field in MirBuilder - next_value_id() skips reserved IDs - merge/mod.rs sets/clears reserved IDs around JoinIR merge Phase 201: JoinValueSpace design document - Param/Local/PHI disjoint regions design - API: alloc_param(), alloc_local(), reserve_phi() - Migration plan for Pattern 1-4 lowerers 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@ -100,6 +100,31 @@ impl LoopHeaderPhiInfo {
|
||||
pub fn all_latch_set(&self) -> bool {
|
||||
self.carrier_phis.values().all(|e| e.latch_incoming.is_some())
|
||||
}
|
||||
|
||||
/// Phase 201-A: Get reserved ValueIds (PHI dsts that must not be overwritten)
|
||||
///
|
||||
/// Returns a set of ValueIds that are used as PHI destinations in the loop header.
|
||||
/// These IDs must not be reused for other purposes (e.g., Const instructions)
|
||||
/// to prevent corruption of carrier values.
|
||||
///
|
||||
/// Includes:
|
||||
/// - All carrier PHI dst ValueIds
|
||||
/// - Expression result PHI dst (if present)
|
||||
pub fn reserved_value_ids(&self) -> std::collections::HashSet<ValueId> {
|
||||
let mut reserved = std::collections::HashSet::new();
|
||||
|
||||
// Add all carrier PHI dsts
|
||||
for entry in self.carrier_phis.values() {
|
||||
reserved.insert(entry.phi_dst);
|
||||
}
|
||||
|
||||
// Add expression result PHI dst (if present)
|
||||
if let Some(dst) = self.expr_result_phi {
|
||||
reserved.insert(dst);
|
||||
}
|
||||
|
||||
reserved
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@ -111,8 +111,93 @@ pub(in crate::mir::builder) fn merge_joinir_mir_blocks(
|
||||
}
|
||||
}
|
||||
|
||||
// Phase 3: Remap ValueIds
|
||||
remap_values(builder, &used_values, &mut remapper, debug)?;
|
||||
// Phase 201-A: Build loop header PHI info BEFORE Phase 3
|
||||
//
|
||||
// We need to allocate PHI dst ValueIds before remap_values() runs,
|
||||
// to prevent conflicts where a Const instruction gets a ValueId that
|
||||
// will later be used as a PHI dst, causing carrier value corruption.
|
||||
//
|
||||
// This is a reordering of Phase 3 and Phase 3.5 logic.
|
||||
let mut loop_header_phi_info = if let Some(boundary) = boundary {
|
||||
if let Some(loop_var_name) = &boundary.loop_var_name {
|
||||
// Get entry function and block for building PHI info
|
||||
let (entry_func_name, entry_func) = mir_module
|
||||
.functions
|
||||
.iter()
|
||||
.next()
|
||||
.ok_or("JoinIR module has no functions (Phase 201-A)")?;
|
||||
let entry_block_remapped = remapper
|
||||
.get_block(entry_func_name, entry_func.entry_block)
|
||||
.ok_or_else(|| format!("Entry block not found for {} (Phase 201-A)", entry_func_name))?;
|
||||
|
||||
// Get host's current block as the entry edge
|
||||
let host_entry_block = builder.current_block.ok_or(
|
||||
"Phase 201-A: No current block when building header PHIs"
|
||||
)?;
|
||||
|
||||
// Get loop variable's initial value from HOST
|
||||
let loop_var_init = boundary.host_inputs.first().copied().ok_or(
|
||||
"Phase 201-A: No host_inputs in boundary for loop_var_init"
|
||||
)?;
|
||||
|
||||
// Extract other carriers from exit_bindings
|
||||
let other_carriers: Vec<(String, ValueId)> = boundary.exit_bindings
|
||||
.iter()
|
||||
.filter(|b| b.carrier_name != *loop_var_name)
|
||||
.map(|b| (b.carrier_name.clone(), b.host_slot))
|
||||
.collect();
|
||||
|
||||
if debug {
|
||||
eprintln!(
|
||||
"[cf_loop/joinir] Phase 201-A: Pre-building header PHIs for loop_var='{}' at {:?}",
|
||||
loop_var_name, entry_block_remapped
|
||||
);
|
||||
eprintln!(
|
||||
"[cf_loop/joinir] loop_var_init={:?}, carriers={:?}",
|
||||
loop_var_init,
|
||||
other_carriers.iter().map(|(n, _)| n.as_str()).collect::<Vec<_>>()
|
||||
);
|
||||
}
|
||||
|
||||
// Build PHI info (this allocates PHI dst ValueIds)
|
||||
LoopHeaderPhiBuilder::build(
|
||||
builder,
|
||||
entry_block_remapped,
|
||||
host_entry_block,
|
||||
loop_var_name,
|
||||
loop_var_init,
|
||||
&other_carriers,
|
||||
boundary.expr_result.is_some(),
|
||||
debug,
|
||||
)?
|
||||
} else {
|
||||
LoopHeaderPhiInfo::empty(remapper.get_block(
|
||||
mir_module.functions.iter().next().unwrap().0,
|
||||
mir_module.functions.iter().next().unwrap().1.entry_block
|
||||
).unwrap())
|
||||
}
|
||||
} else {
|
||||
LoopHeaderPhiInfo::empty(remapper.get_block(
|
||||
mir_module.functions.iter().next().unwrap().0,
|
||||
mir_module.functions.iter().next().unwrap().1.entry_block
|
||||
).unwrap())
|
||||
};
|
||||
|
||||
// Phase 201-A: Get reserved PHI dst ValueIds and set in MirBuilder
|
||||
let reserved_phi_dsts = loop_header_phi_info.reserved_value_ids();
|
||||
if debug && !reserved_phi_dsts.is_empty() {
|
||||
eprintln!("[cf_loop/joinir] Phase 201-A: Reserved PHI dsts: {:?}", reserved_phi_dsts);
|
||||
}
|
||||
|
||||
// Phase 201-A: Set reserved IDs in MirBuilder so next_value_id() skips them
|
||||
// This protects against carrier corruption when break conditions emit Const instructions
|
||||
builder.reserved_value_ids = reserved_phi_dsts.clone();
|
||||
if debug && !builder.reserved_value_ids.is_empty() {
|
||||
eprintln!("[cf_loop/joinir] Phase 201-A: Set builder.reserved_value_ids = {:?}", builder.reserved_value_ids);
|
||||
}
|
||||
|
||||
// Phase 3: Remap ValueIds (with reserved PHI dsts protection)
|
||||
remap_values(builder, &used_values, &mut remapper, &reserved_phi_dsts, debug)?;
|
||||
|
||||
// Phase 177-3 DEBUG: Verify remapper state after Phase 3
|
||||
eprintln!("[DEBUG-177] === Remapper state after Phase 3 ===");
|
||||
@ -138,86 +223,13 @@ pub(in crate::mir::builder) fn merge_joinir_mir_blocks(
|
||||
}
|
||||
eprintln!("[DEBUG-177] ==============================");
|
||||
|
||||
// Phase 3.5: Build loop header PHIs (if loop pattern with loop_var_name)
|
||||
// Phase 3.5: Override remapper for function parameters to use PHI dsts
|
||||
//
|
||||
// We need to know PHI dsts before instruction_rewriter runs, so that:
|
||||
// 1. Tail call handling can set latch_incoming
|
||||
// 2. Return handling can use PHI dsts for exit values
|
||||
let (entry_func_name, entry_func) = mir_module
|
||||
.functions
|
||||
.iter()
|
||||
.next()
|
||||
.ok_or("JoinIR module has no functions (Phase 3.5)")?;
|
||||
let entry_block_remapped = remapper
|
||||
.get_block(entry_func_name, entry_func.entry_block)
|
||||
.ok_or_else(|| format!("Entry block not found for {} (Phase 3.5)", entry_func_name))?;
|
||||
|
||||
// Phase 33-16: Get host's current block as the entry edge (the block that jumps INTO the loop)
|
||||
let host_entry_block = builder.current_block.ok_or(
|
||||
"Phase 33-16: No current block when building header PHIs"
|
||||
)?;
|
||||
|
||||
let mut loop_header_phi_info = if let Some(boundary) = boundary {
|
||||
// Phase 201-A: This phase now uses the loop_header_phi_info built before Phase 3.
|
||||
// The PHI dst allocation has been moved earlier to prevent ValueId conflicts.
|
||||
if let Some(boundary) = boundary {
|
||||
if let Some(loop_var_name) = &boundary.loop_var_name {
|
||||
// Phase 33-16: Get loop variable's initial value from HOST (not JoinIR's ValueId(0))
|
||||
// boundary.host_inputs[0] is the host ValueId that holds the initial loop var value
|
||||
let loop_var_init = boundary.host_inputs.first().copied().ok_or(
|
||||
"Phase 33-16: No host_inputs in boundary for loop_var_init"
|
||||
)?;
|
||||
|
||||
if debug {
|
||||
eprintln!(
|
||||
"[cf_loop/joinir] Phase 3.5: Building header PHIs for loop_var='{}' at {:?}",
|
||||
loop_var_name, entry_block_remapped
|
||||
);
|
||||
eprintln!(
|
||||
"[cf_loop/joinir] loop_var_init={:?} (from boundary.host_inputs[0])",
|
||||
loop_var_init
|
||||
);
|
||||
eprintln!(
|
||||
"[cf_loop/joinir] host_entry_block={:?} (where initial value comes from)",
|
||||
host_entry_block
|
||||
);
|
||||
}
|
||||
|
||||
// Phase 33-20: Extract other carriers from exit_bindings
|
||||
// Skip the loop variable (it's handled separately) and collect other carriers
|
||||
eprintln!(
|
||||
"[cf_loop/joinir] Phase 33-20 DEBUG: exit_bindings count={}, loop_var_name={:?}",
|
||||
boundary.exit_bindings.len(),
|
||||
loop_var_name
|
||||
);
|
||||
for b in boundary.exit_bindings.iter() {
|
||||
eprintln!(
|
||||
"[cf_loop/joinir] Phase 33-20 DEBUG: exit_binding: carrier_name={:?}, host_slot={:?}",
|
||||
b.carrier_name, b.host_slot
|
||||
);
|
||||
}
|
||||
|
||||
let other_carriers: Vec<(String, ValueId)> = boundary.exit_bindings
|
||||
.iter()
|
||||
.filter(|b| b.carrier_name != *loop_var_name)
|
||||
.map(|b| (b.carrier_name.clone(), b.host_slot))
|
||||
.collect();
|
||||
|
||||
if debug && !other_carriers.is_empty() {
|
||||
eprintln!(
|
||||
"[cf_loop/joinir] Phase 33-20: Found {} other carriers from exit_bindings: {:?}",
|
||||
other_carriers.len(),
|
||||
other_carriers.iter().map(|(n, _)| n.as_str()).collect::<Vec<_>>()
|
||||
);
|
||||
}
|
||||
|
||||
let phi_info = LoopHeaderPhiBuilder::build(
|
||||
builder,
|
||||
entry_block_remapped, // header_block (JoinIR's entry block = loop header)
|
||||
host_entry_block, // entry_block (host's block that jumps to loop header)
|
||||
loop_var_name,
|
||||
loop_var_init,
|
||||
&other_carriers, // Phase 33-20: Pass other carriers from exit_bindings
|
||||
boundary.expr_result.is_some(), // expr_result_is_loop_var
|
||||
debug,
|
||||
)?;
|
||||
// Phase 201-A: PHI info is already built (before Phase 3) - just use it
|
||||
|
||||
// Phase 33-21: Override remapper for loop_step's parameters
|
||||
//
|
||||
@ -295,13 +307,13 @@ pub(in crate::mir::builder) fn merge_joinir_mir_blocks(
|
||||
);
|
||||
eprintln!(
|
||||
"[DEBUG-177] Phase 33-21: carrier_phis count: {}, names: {:?}",
|
||||
phi_info.carrier_phis.len(),
|
||||
phi_info.carrier_phis.iter().map(|(n, _)| n.as_str()).collect::<Vec<_>>()
|
||||
loop_header_phi_info.carrier_phis.len(),
|
||||
loop_header_phi_info.carrier_phis.iter().map(|(n, _)| n.as_str()).collect::<Vec<_>>()
|
||||
);
|
||||
// Map main's parameters to header PHI dsts
|
||||
// main params: [i_init, carrier1_init, ...]
|
||||
// carrier_phis: [("i", entry), ("sum", entry), ...]
|
||||
for (idx, (carrier_name, entry)) in phi_info.carrier_phis.iter().enumerate() {
|
||||
for (idx, (carrier_name, entry)) in loop_header_phi_info.carrier_phis.iter().enumerate() {
|
||||
if let Some(&main_param) = main_params.get(idx) {
|
||||
// Phase 177-3: Don't override condition_bindings
|
||||
if condition_binding_ids.contains(&main_param) {
|
||||
@ -323,7 +335,7 @@ pub(in crate::mir::builder) fn merge_joinir_mir_blocks(
|
||||
// Phase 177-3-B: Handle body-only carriers
|
||||
// These are carriers in carrier_phis that are NOT in main function params.
|
||||
// They appear in condition_bindings (added by Phase 176-5) but need PHI remapping.
|
||||
for (carrier_name, entry) in &phi_info.carrier_phis {
|
||||
for (carrier_name, entry) in &loop_header_phi_info.carrier_phis {
|
||||
// Check if this carrier has a condition_binding
|
||||
if let Some(binding) = boundary.condition_bindings.iter().find(|cb| cb.name == *carrier_name) {
|
||||
// Skip if it's a true condition-only variable (already protected above)
|
||||
@ -376,7 +388,7 @@ pub(in crate::mir::builder) fn merge_joinir_mir_blocks(
|
||||
// Check if this param was already handled by Phase 177-3-B
|
||||
let already_mapped = boundary.condition_bindings.iter().any(|cb| {
|
||||
cb.join_value == *loop_step_param &&
|
||||
phi_info.carrier_phis.iter().any(|(name, _)| name == &cb.name)
|
||||
loop_header_phi_info.carrier_phis.iter().any(|(name, _)| name == &cb.name)
|
||||
});
|
||||
if already_mapped {
|
||||
eprintln!(
|
||||
@ -394,8 +406,8 @@ pub(in crate::mir::builder) fn merge_joinir_mir_blocks(
|
||||
if let Some(param_idx) = loop_step_params.iter().position(|p| p == loop_step_param) {
|
||||
// Map params[i] to carrier_order[i]
|
||||
if let (Some(carrier_name), Some(entry)) = (
|
||||
phi_info.get_carrier_at_index(param_idx),
|
||||
phi_info.get_entry_at_index(param_idx),
|
||||
loop_header_phi_info.get_carrier_at_index(param_idx),
|
||||
loop_header_phi_info.get_entry_at_index(param_idx),
|
||||
) {
|
||||
eprintln!(
|
||||
"[DEBUG-177] Phase 177-STRUCT-2: REMAP loop_step param[{}] {:?} → {:?} (carrier '{}')",
|
||||
@ -410,7 +422,7 @@ pub(in crate::mir::builder) fn merge_joinir_mir_blocks(
|
||||
if function_params.get(main_func_name).is_none() && function_params.get(loop_step_func_name).is_none() {
|
||||
// Fallback: Use old behavior (ValueId(0), ValueId(1), ...)
|
||||
// This handles patterns that don't have loop_step function
|
||||
if let Some(phi_dst) = phi_info.get_carrier_phi(loop_var_name) {
|
||||
if let Some(phi_dst) = loop_header_phi_info.get_carrier_phi(loop_var_name) {
|
||||
// Phase 177-3: Don't override condition_bindings
|
||||
if !condition_binding_ids.contains(&ValueId(0)) {
|
||||
remapper.set_value(ValueId(0), phi_dst);
|
||||
@ -427,11 +439,11 @@ pub(in crate::mir::builder) fn merge_joinir_mir_blocks(
|
||||
}
|
||||
}
|
||||
// Phase 177-STRUCT-2: Use carrier_order for deterministic iteration
|
||||
for (idx, carrier_name) in phi_info.carrier_order.iter().enumerate() {
|
||||
for (idx, carrier_name) in loop_header_phi_info.carrier_order.iter().enumerate() {
|
||||
if carrier_name == loop_var_name {
|
||||
continue;
|
||||
}
|
||||
let entry = match phi_info.carrier_phis.get(carrier_name) {
|
||||
let entry = match loop_header_phi_info.carrier_phis.get(carrier_name) {
|
||||
Some(e) => e,
|
||||
None => continue,
|
||||
};
|
||||
@ -464,13 +476,9 @@ pub(in crate::mir::builder) fn merge_joinir_mir_blocks(
|
||||
);
|
||||
}
|
||||
|
||||
phi_info
|
||||
} else {
|
||||
LoopHeaderPhiInfo::empty(entry_block_remapped)
|
||||
// Phase 201-A: loop_header_phi_info already built (no assignment needed)
|
||||
}
|
||||
} else {
|
||||
LoopHeaderPhiInfo::empty(entry_block_remapped)
|
||||
};
|
||||
}
|
||||
|
||||
// Phase 4: Merge blocks and rewrite instructions
|
||||
// Phase 33-16: Pass mutable loop_header_phi_info for latch_incoming tracking
|
||||
@ -541,17 +549,12 @@ pub(in crate::mir::builder) fn merge_joinir_mir_blocks(
|
||||
|
||||
let exit_block_id = merge_result.exit_block_id;
|
||||
|
||||
// Jump from current block to entry function's entry block
|
||||
// (Reuse entry_func_name and entry_block_remapped from Phase 3.5)
|
||||
let entry_block = entry_block_remapped;
|
||||
// Phase 201-A: Get entry block from loop_header_phi_info
|
||||
// The header_block in loop_header_phi_info is the remapped entry block
|
||||
let entry_block = loop_header_phi_info.header_block;
|
||||
|
||||
if debug {
|
||||
eprintln!("[cf_loop/joinir] Entry function name: {}", entry_func_name);
|
||||
eprintln!(
|
||||
"[cf_loop/joinir] Entry function's entry_block (JoinIR local): {:?}",
|
||||
entry_func.entry_block
|
||||
);
|
||||
eprintln!("[cf_loop/joinir] Entry block (remapped): {:?}", entry_block);
|
||||
eprintln!("[cf_loop/joinir] Entry block (from loop_header_phi_info): {:?}", entry_block);
|
||||
eprintln!(
|
||||
"[cf_loop/joinir] Current block before emit_jump: {:?}",
|
||||
builder.current_block
|
||||
@ -598,22 +601,48 @@ pub(in crate::mir::builder) fn merge_joinir_mir_blocks(
|
||||
}
|
||||
}
|
||||
|
||||
// Phase 201-A: Clear reserved ValueIds after merge completes
|
||||
// Future loops will set their own reserved IDs
|
||||
if !builder.reserved_value_ids.is_empty() {
|
||||
if debug {
|
||||
eprintln!("[cf_loop/joinir] Phase 201-A: Clearing reserved_value_ids (was {:?})", builder.reserved_value_ids);
|
||||
}
|
||||
builder.reserved_value_ids.clear();
|
||||
}
|
||||
|
||||
Ok(exit_phi_result_id)
|
||||
}
|
||||
|
||||
/// Phase 3: Allocate new ValueIds for all collected values
|
||||
///
|
||||
/// 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.
|
||||
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>,
|
||||
debug: bool,
|
||||
) -> Result<(), String> {
|
||||
if debug {
|
||||
eprintln!("[cf_loop/joinir] Phase 3: Remapping {} ValueIds", used_values.len());
|
||||
eprintln!("[cf_loop/joinir] Phase 3: Remapping {} ValueIds (reserved: {})",
|
||||
used_values.len(), reserved_ids.len());
|
||||
}
|
||||
|
||||
for old_value in used_values {
|
||||
let new_value = builder.next_value_id();
|
||||
// Phase 201-A: Allocate new ValueId, skipping reserved PHI dsts
|
||||
let new_value = loop {
|
||||
let candidate = builder.next_value_id();
|
||||
if !reserved_ids.contains(&candidate) {
|
||||
break candidate;
|
||||
}
|
||||
// Skip reserved ID - will try next one
|
||||
if debug {
|
||||
eprintln!("[cf_loop/joinir] Phase 201-A: Skipping reserved PHI dst {:?}", candidate);
|
||||
}
|
||||
};
|
||||
|
||||
remapper.set_value(*old_value, new_value);
|
||||
if debug {
|
||||
eprintln!(
|
||||
|
||||
@ -125,16 +125,16 @@ impl ConditionEnvBuilder {
|
||||
env
|
||||
}
|
||||
|
||||
/// Build ConditionEnv with optional captured variables (Phase 200-A v2 entry point)
|
||||
/// Build ConditionEnv with optional captured variables (Phase 200-B implementation)
|
||||
///
|
||||
/// # Phase 200-A Status
|
||||
/// # Phase 200-B Implementation
|
||||
///
|
||||
/// Currently ignores `captured` parameter and delegates to existing implementation.
|
||||
/// Integration with CapturedEnv will be implemented in Phase 200-B.
|
||||
/// Adds captured function-scoped variables to ConditionEnv and generates
|
||||
/// condition_bindings for the boundary builder.
|
||||
///
|
||||
/// # Future Behavior (Phase 200-B+)
|
||||
/// # Behavior
|
||||
///
|
||||
/// - Add captured variables to ConditionEnv
|
||||
/// - Add captured variables to ConditionEnv.captured field
|
||||
/// - Generate condition_bindings for captured vars in boundary
|
||||
/// - Track captured vars separately from loop params
|
||||
/// - Ensure captured vars do NOT participate in header PHI or exit_bindings
|
||||
@ -142,45 +142,91 @@ impl ConditionEnvBuilder {
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `loop_var_name` - Loop parameter name (e.g., "i", "pos")
|
||||
/// * `_captured` - Function-scoped captured variables (Phase 200-B+)
|
||||
/// * `_boundary` - Boundary builder for adding condition_bindings (Phase 200-B+)
|
||||
/// * `captured` - Function-scoped captured variables with host ValueIds
|
||||
/// * `boundary` - Boundary builder for adding condition_bindings
|
||||
/// * `variable_map` - Host function's variable_map to resolve host ValueIds
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// ConditionEnv with loop parameter mapping (Phase 200-A: same as build_loop_param_only)
|
||||
/// ConditionEnv with loop parameter and captured variables
|
||||
///
|
||||
/// # Example (Future Phase 200-B)
|
||||
/// # Example
|
||||
///
|
||||
/// ```ignore
|
||||
/// let captured = analyze_captured_vars(fn_body, loop_ast, scope);
|
||||
/// let mut boundary = JoinInlineBoundaryBuilder::new();
|
||||
/// let env = ConditionEnvBuilder::build_with_captures(
|
||||
/// "pos",
|
||||
/// &captured, // Contains "digits" with host ValueId(42)
|
||||
/// &captured, // Contains "digits"
|
||||
/// &mut boundary,
|
||||
/// &variable_map, // To resolve "digits" → ValueId(42)
|
||||
/// );
|
||||
/// // Phase 200-B: env will contain "pos" → ValueId(0), "digits" → ValueId(1)
|
||||
/// // Phase 200-B: boundary.condition_bindings will have entry for "digits"
|
||||
/// // env.params: "pos" → ValueId(0)
|
||||
/// // env.captured: "digits" → ValueId(1)
|
||||
/// // boundary.condition_bindings: [ConditionBinding { name: "digits", host_value: ValueId(42), join_value: ValueId(1) }]
|
||||
/// ```
|
||||
pub fn build_with_captures(
|
||||
loop_var_name: &str,
|
||||
_captured: &CapturedEnv,
|
||||
_boundary: &mut JoinInlineBoundaryBuilder,
|
||||
captured: &CapturedEnv,
|
||||
boundary: &mut JoinInlineBoundaryBuilder,
|
||||
variable_map: &BTreeMap<String, ValueId>,
|
||||
) -> ConditionEnv {
|
||||
// Phase 200-A: Delegate to existing implementation
|
||||
// TODO(Phase 200-B): Integrate captured vars into ConditionEnv
|
||||
//
|
||||
// Integration steps:
|
||||
// 1. Start with loop parameter in env (ValueId(0))
|
||||
// 2. For each captured var:
|
||||
// a. Allocate JoinIR-local ValueId (starting from 1)
|
||||
// b. Add to ConditionEnv (var.name → join_id)
|
||||
// c. Add to boundary.condition_bindings (host_id ↔ join_id)
|
||||
// d. Mark as ParamRole::Condition (not Carrier or LoopParam)
|
||||
// 3. Ensure captured vars are NOT in exit_bindings (condition-only)
|
||||
// 4. Return populated ConditionEnv
|
||||
use std::env;
|
||||
|
||||
Self::build_loop_param_only(loop_var_name)
|
||||
let debug = env::var("NYASH_CAPTURE_DEBUG").is_ok();
|
||||
|
||||
if debug {
|
||||
eprintln!("[capture/env_builder] Building ConditionEnv with {} captured vars", captured.vars.len());
|
||||
}
|
||||
|
||||
// Step 1: Build base ConditionEnv with loop params (existing logic)
|
||||
let mut env = Self::build_loop_param_only(loop_var_name);
|
||||
|
||||
// Step 2: Add captured vars as ParamRole::Condition
|
||||
for var in &captured.vars {
|
||||
// 2a: Resolve host_id from variable_map
|
||||
let host_id = match variable_map.get(&var.name) {
|
||||
Some(&id) => id,
|
||||
None => {
|
||||
if debug {
|
||||
eprintln!("[capture/env_builder] WARNING: Captured var '{}' not found in variable_map, skipping", var.name);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
// 2b: Add to boundary with Condition role
|
||||
boundary.add_param_with_role(&var.name, host_id, crate::mir::join_ir::lowering::inline_boundary_builder::ParamRole::Condition);
|
||||
|
||||
// 2c: Get JoinIR ValueId from boundary
|
||||
let join_id = boundary.get_condition_binding(&var.name)
|
||||
.expect("captured var should be in boundary after add_param_with_role");
|
||||
|
||||
// 2d: Add to ConditionEnv.captured map
|
||||
env.captured.insert(var.name.clone(), join_id);
|
||||
|
||||
if debug {
|
||||
eprintln!("[capture/env_builder] Added captured var '{}': host={:?}, join={:?}", var.name, host_id, join_id);
|
||||
}
|
||||
}
|
||||
|
||||
// Step 3: Debug guard - Condition params must NOT be in PHI candidates
|
||||
#[cfg(debug_assertions)]
|
||||
for var in &captured.vars {
|
||||
if env.contains(&var.name) && !env.is_captured(&var.name) {
|
||||
panic!(
|
||||
"Captured var '{}' must not be in loop params (ParamRole conflict)",
|
||||
var.name
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if debug {
|
||||
let param_count = env.iter().count();
|
||||
eprintln!("[capture/env_builder] Final ConditionEnv: {} params, {} captured", param_count, env.captured.len());
|
||||
}
|
||||
|
||||
env
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -72,17 +72,19 @@ pub fn can_lower(builder: &MirBuilder, ctx: &super::router::LoopPatternContext)
|
||||
/// Phase 194: Lowering function for Pattern 2
|
||||
///
|
||||
/// Wrapper around cf_loop_pattern2_with_break to match router signature
|
||||
/// Phase 200-C: Pass fn_body to cf_loop_pattern2_with_break
|
||||
pub fn lower(
|
||||
builder: &mut MirBuilder,
|
||||
ctx: &super::router::LoopPatternContext,
|
||||
) -> Result<Option<ValueId>, String> {
|
||||
builder.cf_loop_pattern2_with_break(ctx.condition, ctx.body, ctx.func_name, ctx.debug)
|
||||
builder.cf_loop_pattern2_with_break_impl(ctx.condition, ctx.body, ctx.func_name, ctx.debug, ctx.fn_body)
|
||||
}
|
||||
|
||||
impl MirBuilder {
|
||||
/// Phase 179-B: Pattern 2 (Loop with Conditional Break) minimal lowerer
|
||||
///
|
||||
/// **Refactored**: Now uses PatternPipelineContext for unified preprocessing
|
||||
/// **Phase 200-C**: Added fn_body parameter for capture analysis
|
||||
///
|
||||
/// # Pipeline (Phase 179-B)
|
||||
/// 1. Build preprocessing context → PatternPipelineContext
|
||||
@ -99,6 +101,19 @@ impl MirBuilder {
|
||||
_body: &[ASTNode],
|
||||
_func_name: &str,
|
||||
debug: bool,
|
||||
) -> Result<Option<ValueId>, String> {
|
||||
// Phase 200-C: Delegate to impl function with fn_body=None for backward compatibility
|
||||
self.cf_loop_pattern2_with_break_impl(condition, _body, _func_name, debug, None)
|
||||
}
|
||||
|
||||
/// Phase 200-C: Pattern 2 implementation with optional fn_body for capture analysis
|
||||
fn cf_loop_pattern2_with_break_impl(
|
||||
&mut self,
|
||||
condition: &ASTNode,
|
||||
_body: &[ASTNode],
|
||||
_func_name: &str,
|
||||
debug: bool,
|
||||
fn_body: Option<&[ASTNode]>,
|
||||
) -> Result<Option<ValueId>, String> {
|
||||
use crate::mir::join_ir::lowering::loop_with_break_minimal::lower_loop_with_break_minimal;
|
||||
|
||||
@ -129,9 +144,34 @@ impl MirBuilder {
|
||||
// Phase 195: Use unified trace
|
||||
trace::trace().varmap("pattern2_start", &self.variable_map);
|
||||
|
||||
// Phase 171-172: Use ConditionEnvBuilder for unified construction (Issue 5)
|
||||
// Phase 200-C: Integrate capture analysis
|
||||
use crate::mir::loop_pattern_detection::function_scope_capture::{analyze_captured_vars_v2, CapturedEnv};
|
||||
use super::condition_env_builder::ConditionEnvBuilder;
|
||||
use crate::mir::join_ir::lowering::condition_env::ConditionBinding;
|
||||
|
||||
eprintln!("[pattern2/phase200c] fn_body is {}", if fn_body.is_some() { "SOME" } else { "NONE" });
|
||||
|
||||
let captured_env = if let Some(fn_body_ref) = fn_body {
|
||||
eprintln!("[pattern2/phase200c] fn_body has {} nodes", fn_body_ref.len());
|
||||
|
||||
// Phase 200-C: Use v2 API with structural matching
|
||||
// Pass condition and body directly instead of constructing loop AST
|
||||
analyze_captured_vars_v2(fn_body_ref, condition, _body, &scope)
|
||||
} else {
|
||||
eprintln!("[pattern2/phase200c] fn_body is None, using empty CapturedEnv");
|
||||
// fn_body not available - use empty CapturedEnv
|
||||
CapturedEnv::new()
|
||||
};
|
||||
|
||||
eprintln!("[pattern2/capture] Phase 200-C: Captured {} variables",
|
||||
captured_env.vars.len());
|
||||
for var in &captured_env.vars {
|
||||
eprintln!("[pattern2/capture] '{}': host_id={:?}, immutable={}",
|
||||
var.name, var.host_id, var.is_immutable);
|
||||
}
|
||||
|
||||
// Phase 200-C: Use existing path and manually add captured variables
|
||||
// TODO Phase 200-D: Refactor to use build_with_captures with boundary builder
|
||||
let (mut env, mut condition_bindings) = ConditionEnvBuilder::build_for_break_condition(
|
||||
condition,
|
||||
&loop_var_name,
|
||||
@ -139,6 +179,26 @@ impl MirBuilder {
|
||||
loop_var_id,
|
||||
)?;
|
||||
|
||||
// Phase 200-C: Manually add captured variables to env for E2E testing
|
||||
// This is a temporary approach until Phase 200-D refactors the boundary creation
|
||||
for var in &captured_env.vars {
|
||||
if let Some(&host_id) = self.variable_map.get(&var.name) {
|
||||
// Allocate a JoinIR ValueId for this captured variable
|
||||
let join_id = crate::mir::ValueId(env.len() as u32);
|
||||
env.insert(var.name.clone(), join_id);
|
||||
|
||||
// Add to condition_bindings for boundary processing
|
||||
condition_bindings.push(ConditionBinding {
|
||||
name: var.name.clone(),
|
||||
host_value: host_id,
|
||||
join_value: join_id,
|
||||
});
|
||||
|
||||
eprintln!("[pattern2/capture] Manually added captured '{}' to env: host={:?}, join={:?}",
|
||||
var.name, host_id, join_id);
|
||||
}
|
||||
}
|
||||
|
||||
// Phase 190-impl-D: Calculate ValueId offset for body-local variables
|
||||
// JoinIR main() params are: [ValueId(0), ValueId(1), ...] for (loop_var, carrier1, carrier2, ...)
|
||||
// Body-local variables must start AFTER all carrier params to avoid collision.
|
||||
|
||||
@ -54,6 +54,10 @@ pub struct LoopPatternContext<'a> {
|
||||
|
||||
/// Phase 192: Pattern classification based on features
|
||||
pub pattern_kind: LoopPatternKind,
|
||||
|
||||
/// Phase 200-C: Optional function body AST for capture analysis
|
||||
/// None if not available, Some(&[ASTNode]) if function body is accessible
|
||||
pub fn_body: Option<&'a [ASTNode]>,
|
||||
}
|
||||
|
||||
impl<'a> LoopPatternContext<'a> {
|
||||
@ -87,8 +91,22 @@ impl<'a> LoopPatternContext<'a> {
|
||||
has_break,
|
||||
features,
|
||||
pattern_kind,
|
||||
fn_body: None, // Phase 200-C: Default to None
|
||||
}
|
||||
}
|
||||
|
||||
/// Phase 200-C: Create context with fn_body for capture analysis
|
||||
pub fn with_fn_body(
|
||||
condition: &'a ASTNode,
|
||||
body: &'a [ASTNode],
|
||||
func_name: &'a str,
|
||||
debug: bool,
|
||||
fn_body: &'a [ASTNode],
|
||||
) -> Self {
|
||||
let mut ctx = Self::new(condition, body, func_name, debug);
|
||||
ctx.fn_body = Some(fn_body);
|
||||
ctx
|
||||
}
|
||||
}
|
||||
|
||||
/// Phase 193: Feature extraction moved to ast_feature_extractor module
|
||||
|
||||
@ -139,7 +139,17 @@ impl MirBuilder {
|
||||
// Phase 194: Use table-driven router instead of if/else chain
|
||||
use super::patterns::{route_loop_pattern, LoopPatternContext};
|
||||
|
||||
let ctx = LoopPatternContext::new(condition, body, &func_name, debug);
|
||||
// Phase 200-C: Pass fn_body_ast to LoopPatternContext if available
|
||||
// Clone fn_body_ast to avoid borrow checker issues
|
||||
let fn_body_clone = self.fn_body_ast.clone();
|
||||
eprintln!("[routing] fn_body_ast is {} for '{}'", if fn_body_clone.is_some() { "SOME" } else { "NONE" }, func_name);
|
||||
let ctx = if let Some(ref fn_body) = fn_body_clone {
|
||||
eprintln!("[routing] Creating ctx with fn_body ({} nodes)", fn_body.len());
|
||||
LoopPatternContext::with_fn_body(condition, body, &func_name, debug, fn_body)
|
||||
} else {
|
||||
LoopPatternContext::new(condition, body, &func_name, debug)
|
||||
};
|
||||
|
||||
if let Some(result) = route_loop_pattern(self, &ctx)? {
|
||||
trace::trace().routing("router", func_name, "Pattern router succeeded");
|
||||
return Ok(Some(result));
|
||||
|
||||
Reference in New Issue
Block a user