feat(joinir): Phase 49-4 multi-target routing with graceful fallback
- Add ArrayExtBox.filter/2 as second JoinIR mainline target - Fix function name arity: print_tokens is /0 (no implicit me in arity) - Construct proper JSON v0 format with defs array for JoinIR Frontend - Add catch_unwind for graceful fallback on unsupported patterns - Add 3 array_filter tests (smoke, fallback, A/B comparison) - All 6 Phase 49 tests passing Dev flags: - HAKO_JOINIR_PRINT_TOKENS_MAIN=1: JsonTokenizer.print_tokens/0 - HAKO_JOINIR_ARRAY_FILTER_MAIN=1: ArrayExtBox.filter/2 Note: Currently all loops fall back to legacy LoopBuilder due to JoinIR Frontend expecting hardcoded variable names (i, acc, n). Full JoinIR integration pending variable scope support in Phase 50+. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@ -24,10 +24,14 @@ impl super::MirBuilder {
|
||||
///
|
||||
/// # Phase 49: JoinIR Frontend Mainline Integration
|
||||
///
|
||||
/// This is the unified entry point for all loop lowering. When enabled via
|
||||
/// `HAKO_JOINIR_PRINT_TOKENS_MAIN=1`, specific functions (starting with
|
||||
/// `JsonTokenizer.print_tokens/1`) are routed through JoinIR Frontend instead
|
||||
/// of the traditional LoopBuilder path.
|
||||
/// This is the unified entry point for all loop lowering. Specific functions
|
||||
/// are routed through JoinIR Frontend instead of the traditional LoopBuilder path
|
||||
/// when enabled via dev flags:
|
||||
///
|
||||
/// - `HAKO_JOINIR_PRINT_TOKENS_MAIN=1`: JsonTokenizer.print_tokens/0
|
||||
/// - `HAKO_JOINIR_ARRAY_FILTER_MAIN=1`: ArrayExtBox.filter/2
|
||||
///
|
||||
/// Note: Arity does NOT include implicit `me` receiver.
|
||||
pub(super) fn cf_loop(
|
||||
&mut self,
|
||||
condition: ASTNode,
|
||||
@ -51,16 +55,21 @@ impl super::MirBuilder {
|
||||
///
|
||||
/// Returns `Ok(Some(value))` if the current function should use JoinIR Frontend,
|
||||
/// `Ok(None)` to fall through to the legacy LoopBuilder path.
|
||||
///
|
||||
/// # Phase 49-4: Multi-target support
|
||||
///
|
||||
/// Targets are enabled via separate dev flags:
|
||||
/// - `HAKO_JOINIR_PRINT_TOKENS_MAIN=1`: JsonTokenizer.print_tokens/0
|
||||
/// - `HAKO_JOINIR_ARRAY_FILTER_MAIN=1`: ArrayExtBox.filter/2
|
||||
///
|
||||
/// Note: Arity in function names does NOT include implicit `me` receiver.
|
||||
/// - Instance method `print_tokens()` → `/0` (no explicit params)
|
||||
/// - Static method `filter(arr, pred)` → `/2` (two params)
|
||||
fn try_cf_loop_joinir(
|
||||
&mut self,
|
||||
condition: &ASTNode,
|
||||
body: &[ASTNode],
|
||||
) -> Result<Option<ValueId>, String> {
|
||||
// Phase 49-2: Check if feature is enabled
|
||||
if std::env::var("HAKO_JOINIR_PRINT_TOKENS_MAIN").ok().as_deref() != Some("1") {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
// Get current function name
|
||||
let func_name = self
|
||||
.current_function
|
||||
@ -68,8 +77,19 @@ impl super::MirBuilder {
|
||||
.map(|f| f.signature.name.clone())
|
||||
.unwrap_or_default();
|
||||
|
||||
// Phase 49-2: Only handle print_tokens for now
|
||||
if func_name != "JsonTokenizer.print_tokens/1" {
|
||||
// Phase 49-4: Multi-target routing with separate dev flags
|
||||
// Note: Arity does NOT include implicit `me` receiver
|
||||
let is_target = match func_name.as_str() {
|
||||
"JsonTokenizer.print_tokens/0" => {
|
||||
std::env::var("HAKO_JOINIR_PRINT_TOKENS_MAIN").ok().as_deref() == Some("1")
|
||||
}
|
||||
"ArrayExtBox.filter/2" => {
|
||||
std::env::var("HAKO_JOINIR_ARRAY_FILTER_MAIN").ok().as_deref() == Some("1")
|
||||
}
|
||||
_ => false,
|
||||
};
|
||||
|
||||
if !is_target {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
@ -90,10 +110,21 @@ impl super::MirBuilder {
|
||||
/// Phase 49-3: JoinIR Frontend integration implementation
|
||||
///
|
||||
/// # Pipeline
|
||||
/// 1. Build Loop AST → Program JSON
|
||||
/// 1. Build Loop AST → JSON v0 format (with "defs" array)
|
||||
/// 2. AstToJoinIrLowerer::lower_program_json() → JoinModule
|
||||
/// 3. convert_join_module_to_mir_with_meta() → MirModule
|
||||
/// 4. Merge MIR blocks into current_function
|
||||
///
|
||||
/// # Phase 49-4 Note
|
||||
///
|
||||
/// JoinIR Frontend expects a complete function definition with:
|
||||
/// - local variable initializations
|
||||
/// - loop body
|
||||
/// - return statement
|
||||
///
|
||||
/// Since cf_loop only has access to the loop condition and body,
|
||||
/// we construct a minimal JSON v0 wrapper with function name "simple"
|
||||
/// to match the JoinIR Frontend's expected pattern.
|
||||
fn cf_loop_joinir_impl(
|
||||
&mut self,
|
||||
condition: &ASTNode,
|
||||
@ -106,34 +137,79 @@ impl super::MirBuilder {
|
||||
use crate::mir::join_ir_vm_bridge::convert_join_module_to_mir_with_meta;
|
||||
use crate::mir::types::ConstValue;
|
||||
|
||||
// Step 1: Build Loop AST wrapped in a minimal function
|
||||
let loop_ast = ASTNode::Loop {
|
||||
condition: Box::new(condition.clone()),
|
||||
body: body.to_vec(),
|
||||
span: Span::unknown(),
|
||||
};
|
||||
// Step 1: Convert condition and body to JSON
|
||||
let condition_json = ast_to_json(condition);
|
||||
let body_json: Vec<serde_json::Value> = body.iter().map(|s| ast_to_json(s)).collect();
|
||||
|
||||
// Wrap in a minimal function for JoinIR lowering
|
||||
// JoinIR Frontend expects a function body, not just a loop
|
||||
let wrapper_func = ASTNode::Program {
|
||||
statements: vec![loop_ast],
|
||||
span: Span::unknown(),
|
||||
};
|
||||
|
||||
// Step 2: Convert to JSON
|
||||
let program_json = ast_to_json(&wrapper_func);
|
||||
// Step 2: Construct JSON v0 format with "defs" array
|
||||
// The function is named "simple" to match JoinIR Frontend's pattern matching
|
||||
let program_json = serde_json::json!({
|
||||
"defs": [
|
||||
{
|
||||
"name": "simple",
|
||||
"params": [],
|
||||
"body": {
|
||||
"type": "Block",
|
||||
"body": [
|
||||
// Placeholder locals (JoinIR Frontend will infer from loop)
|
||||
{
|
||||
"type": "Loop",
|
||||
"condition": condition_json,
|
||||
"body": body_json
|
||||
},
|
||||
// Placeholder return
|
||||
{
|
||||
"type": "Return",
|
||||
"value": null
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
if debug {
|
||||
eprintln!(
|
||||
"[cf_loop/joinir] Generated JSON for {}: {}",
|
||||
"[cf_loop/joinir] Generated JSON v0 for {}: {}",
|
||||
func_name,
|
||||
serde_json::to_string_pretty(&program_json).unwrap_or_default()
|
||||
);
|
||||
}
|
||||
|
||||
// Step 3: Lower to JoinIR
|
||||
let mut lowerer = AstToJoinIrLowerer::new();
|
||||
let join_module = lowerer.lower_program_json(&program_json);
|
||||
// Phase 49-4: Use catch_unwind for graceful fallback on unsupported patterns
|
||||
// The JoinIR Frontend may panic if the loop doesn't match expected patterns
|
||||
// (e.g., missing variable initializations like "i must be initialized")
|
||||
let join_module = {
|
||||
let json_clone = program_json.clone();
|
||||
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
|
||||
let mut lowerer = AstToJoinIrLowerer::new();
|
||||
lowerer.lower_program_json(&json_clone)
|
||||
}));
|
||||
|
||||
match result {
|
||||
Ok(module) => module,
|
||||
Err(e) => {
|
||||
// Extract panic message for debugging
|
||||
let panic_msg = if let Some(s) = e.downcast_ref::<&str>() {
|
||||
s.to_string()
|
||||
} else if let Some(s) = e.downcast_ref::<String>() {
|
||||
s.clone()
|
||||
} else {
|
||||
"unknown panic".to_string()
|
||||
};
|
||||
|
||||
if debug {
|
||||
eprintln!(
|
||||
"[cf_loop/joinir] JoinIR lowering failed for {}: {}, falling back to legacy",
|
||||
func_name, panic_msg
|
||||
);
|
||||
}
|
||||
// Return None to fall back to legacy LoopBuilder
|
||||
return Ok(None);
|
||||
}
|
||||
}
|
||||
};
|
||||
// Phase 49-3 MVP: Use empty meta map (full if-analysis is Phase 40+ territory)
|
||||
let join_meta = JoinFuncMetaMap::new();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user