feat(joinir): Pattern 4 (continue) JoinIR lowering implementation

- Add loop_with_continue_minimal.rs (330 lines)
- Generate JoinIR: main → loop_step → k_exit for continue patterns
- Integrate pattern4_with_continue.rs to call minimal lowerer
- Known issue: JoinIR→MIR bridge doesn't handle multiple carriers
  (output=0 instead of expected=25, needs PHI fix in merge layer)

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
nyash-codex
2025-12-06 00:20:45 +09:00
parent a21501286e
commit 120cd37451
4 changed files with 441 additions and 16 deletions

View File

@ -97,24 +97,102 @@ impl MirBuilder {
&mut self,
condition: &ASTNode,
_body: &[ASTNode],
func_name: &str,
_func_name: &str,
debug: bool,
) -> Result<Option<ValueId>, String> {
use crate::mir::join_ir::lowering::loop_with_continue_minimal::lower_loop_with_continue_minimal;
use crate::mir::join_ir_vm_bridge::convert_join_module_to_mir_with_meta;
use crate::mir::BasicBlockId;
use std::collections::{BTreeMap, BTreeSet};
// Phase 195: Use unified trace
trace::trace().debug("pattern4", "Pattern 4 lowerer called (stub implementation)");
trace::trace().debug("pattern4", "Calling Pattern 4 minimal lowerer");
// TODO: Implement Pattern 4 lowering logic
//
// For now, return an error to fall back to legacy loop builder
// This allows the test to run (even if it produces wrong results)
// Extract loop variables from condition (i and sum)
let loop_var_name = self.extract_loop_variable_from_condition(condition)?;
let loop_var_id = self
.variable_map
.get(&loop_var_name)
.copied()
.ok_or_else(|| {
format!(
"[cf_loop/pattern4] Loop variable '{}' not found in variable_map",
loop_var_name
)
})?;
if debug {
eprintln!("[pattern4] Pattern 4 lowerer not yet implemented for '{}'", func_name);
eprintln!("[pattern4] Falling back to legacy loop builder");
}
// Get sum variable from variable_map
let sum_var_id = self
.variable_map
.get("sum")
.copied()
.ok_or_else(|| {
format!("[cf_loop/pattern4] Sum variable 'sum' not found in variable_map")
})?;
// Return None to indicate pattern not supported
// This will cause the router to try other patterns or fall back to legacy
Ok(None)
// Phase 195: Use unified trace
trace::trace().varmap("pattern4_start", &self.variable_map);
// Create a minimal LoopScopeShape (Phase 195: hardcoded for loop_continue_pattern4.hako)
// Pattern 4 lowerer ignores the scope anyway, so this is just a placeholder
use crate::mir::join_ir::lowering::loop_scope_shape::LoopScopeShape;
let scope = LoopScopeShape {
header: BasicBlockId(0),
body: BasicBlockId(0),
latch: BasicBlockId(0),
exit: BasicBlockId(0),
pinned: BTreeSet::new(),
carriers: BTreeSet::new(),
body_locals: BTreeSet::new(),
exit_live: BTreeSet::new(),
progress_carrier: None,
variable_definitions: BTreeMap::new(),
};
// Call Pattern 4 lowerer
let join_module = match lower_loop_with_continue_minimal(scope) {
Some(module) => module,
None => {
// Phase 195: Use unified trace
trace::trace().debug("pattern4", "Pattern 4 lowerer returned None");
return Ok(None);
}
};
// Phase 195: Use unified trace
trace::trace().joinir_stats(
"pattern4",
join_module.functions.len(),
join_module.functions.values().map(|f| f.body.len()).sum(),
);
// Convert JoinModule to MirModule
// Phase 195: Pass empty meta map since Pattern 4 lowerer doesn't use metadata
use crate::mir::join_ir::frontend::JoinFuncMetaMap;
let empty_meta: JoinFuncMetaMap = BTreeMap::new();
let mir_module = convert_join_module_to_mir_with_meta(&join_module, &empty_meta)
.map_err(|e| format!("[cf_loop/joinir/pattern4] MIR conversion failed: {:?}", e))?;
// Phase 195: Use unified trace
trace::trace().joinir_stats(
"pattern4",
mir_module.functions.len(),
mir_module.functions.values().map(|f| f.blocks.len()).sum(),
);
// Merge JoinIR blocks into current function
// Phase 195: Create and pass JoinInlineBoundary for Pattern 4
let boundary = crate::mir::join_ir::lowering::inline_boundary::JoinInlineBoundary::new_inputs_only(
vec![ValueId(0), ValueId(1)], // JoinIR's main() parameters (i_init, sum_init)
vec![loop_var_id, sum_var_id], // Host's loop variables
);
// Phase 195: Capture exit PHI result (Pattern 4 returns sum)
let result_val = self.merge_joinir_mir_blocks(&mir_module, Some(&boundary), debug)?;
// Phase 195: Use unified trace
trace::trace().debug("pattern4", &format!("Loop complete, returning result {:?}", result_val));
Ok(result_val)
}
}