feat(joinir): Phase 188 Pattern 1 Core Implementation + Phase 189 Planning
Phase 188 Status: Planning & Foundation Complete (100%) Completed Tasks: ✅ Task 188-1: Error Inventory (5 patterns identified) ✅ Task 188-2: Pattern Classification (3 patterns selected) ✅ Task 188-3: Design (51KB comprehensive blueprint) ✅ Task 188-4: Implementation Foundation (1,802 lines scaffolding) ✅ Task 188-5: Verification & Documentation ✅ Pattern 1 Core Implementation: Detection + Lowering + Routing Pattern 1 Implementation (322 lines): - Pattern Detection: is_simple_while_pattern() in loop_pattern_detection.rs - JoinIR Lowering: lower_simple_while_to_joinir() in simple_while_minimal.rs (219 lines) - Generates 3 functions: entry, loop_step (tail-recursive), k_exit - Implements condition negation: exit_cond = !(i < 3) - Tail-recursive Call pattern with state propagation - Routing: Added "main" to function routing list in control_flow.rs - Build: ✅ SUCCESS (0 errors, 34 warnings) Infrastructure Blocker Identified: - merge_joinir_mir_blocks() only handles single-function JoinIR modules - Pattern 1 generates 3 functions (entry + loop_step + k_exit) - Current implementation only merges first function → loop body never executes - Root cause: control_flow.rs line ~850 takes only .next() function Phase 189 Planning Complete: - Goal: Refactor merge_joinir_mir_blocks() for multi-function support - Strategy: Sequential Merge (Option A) - merge all functions in call order - Effort estimate: 5.5-7.5 hours - Deliverables: README.md (16KB), current-analysis.md (15KB), QUICKSTART.md (5.8KB) Files Modified/Created: - src/mir/loop_pattern_detection.rs (+50 lines) - Pattern detection - src/mir/join_ir/lowering/simple_while_minimal.rs (+219 lines) - Lowering - src/mir/join_ir/lowering/loop_patterns.rs (+803 lines) - Foundation skeleton - src/mir/join_ir/lowering/mod.rs (+2 lines) - Module registration - src/mir/builder/control_flow.rs (+1 line) - Routing fix - src/mir/builder/loop_frontend_binding.rs (+20 lines) - Binding updates - tools/test_phase188_foundation.sh (executable) - Foundation verification - CURRENT_TASK.md (updated) - Phase 188/189 status Next: Phase 189 implementation (merge_joinir_mir_blocks refactor) 🤖 Generated with Claude Code Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@ -28,8 +28,10 @@ pub mod if_phi_context; // Phase 61-1
|
||||
pub mod if_phi_spec; // Phase 61-2
|
||||
pub mod if_select; // Phase 33
|
||||
pub mod loop_form_intake;
|
||||
pub mod loop_patterns; // Phase 188: Pattern-based loop lowering (3 patterns)
|
||||
pub mod loop_scope_shape;
|
||||
pub mod loop_to_join;
|
||||
pub mod simple_while_minimal; // Phase 188-Impl-1: Pattern 1 minimal lowerer
|
||||
pub mod min_loop;
|
||||
pub mod skip_ws;
|
||||
pub mod stage1_using_resolver;
|
||||
@ -53,6 +55,7 @@ pub use stageb_funcscanner::lower_stageb_funcscanner_to_joinir;
|
||||
|
||||
// Phase 33: If/Else → Select lowering entry point
|
||||
use crate::mir::join_ir::JoinInst;
|
||||
use crate::mir::loop_form::LoopForm; // Phase 188: Loop pattern lowering
|
||||
use crate::mir::{BasicBlockId, MirFunction};
|
||||
|
||||
/// Phase 33-9.1: Loop lowering対象関数の判定
|
||||
@ -311,6 +314,163 @@ pub fn try_lower_if_to_joinir(
|
||||
result
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Phase 188: Loop Pattern-Based Lowering Router
|
||||
// ============================================================================
|
||||
|
||||
/// Phase 188: Try to lower loop to JoinIR using pattern-based approach
|
||||
///
|
||||
/// This function routes loop lowering to specific pattern handlers based on
|
||||
/// loop structure characteristics. It tries patterns in order of complexity:
|
||||
///
|
||||
/// 1. **Pattern 1: Simple While** (foundational, easiest)
|
||||
/// 2. **Pattern 2: Break** (medium complexity)
|
||||
/// 3. **Pattern 3: If-Else PHI** (leverages existing If lowering)
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `loop_form` - The loop structure to lower
|
||||
/// * `lowerer` - The LoopToJoinLowerer builder (provides ValueId allocation, etc.)
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// * `Some(JoinInst)` - Successfully lowered to JoinIR
|
||||
/// * `None` - No pattern matched (fallback to existing lowering)
|
||||
///
|
||||
/// # Pattern Selection Strategy
|
||||
///
|
||||
/// Patterns are tried sequentially. First matching pattern wins.
|
||||
/// If no pattern matches, returns `Ok(None)` to trigger fallback.
|
||||
///
|
||||
/// ## Pattern 1: Simple While Loop
|
||||
/// - **Condition**: Empty break/continue targets, single latch
|
||||
/// - **Handler**: `loop_patterns::lower_simple_while_to_joinir()`
|
||||
/// - **Priority**: First (most common, simplest)
|
||||
///
|
||||
/// ## Pattern 2: Loop with Conditional Break
|
||||
/// - **Condition**: Non-empty break_targets, exactly 1 break
|
||||
/// - **Handler**: `loop_patterns::lower_loop_with_break_to_joinir()`
|
||||
/// - **Priority**: Second (common, medium complexity)
|
||||
///
|
||||
/// ## Pattern 3: Loop with If-Else PHI
|
||||
/// - **Condition**: Empty break/continue, if-else in body
|
||||
/// - **Handler**: `loop_patterns::lower_loop_with_conditional_phi_to_joinir()`
|
||||
/// - **Priority**: Third (reuses If lowering infrastructure)
|
||||
///
|
||||
/// # Integration Point
|
||||
///
|
||||
/// This function should be called from loop lowering entry points:
|
||||
/// - `loop_to_join.rs::LoopToJoinLowerer::lower_loop()`
|
||||
/// - `loop_form_intake.rs::handle_loop_form()`
|
||||
///
|
||||
/// # Example Usage
|
||||
///
|
||||
/// ```rust,ignore
|
||||
/// use crate::mir::join_ir::lowering::try_lower_loop_pattern_to_joinir;
|
||||
///
|
||||
/// // In loop lowering entry point:
|
||||
/// if let Some(joinir_inst) = try_lower_loop_pattern_to_joinir(&loop_form, &mut lowerer) {
|
||||
/// // Pattern matched, use JoinIR
|
||||
/// return Some(joinir_inst);
|
||||
/// }
|
||||
/// // No pattern matched, use existing lowering
|
||||
/// existing_loop_lowering(&loop_form, &mut lowerer)
|
||||
/// ```
|
||||
///
|
||||
/// # Reference
|
||||
///
|
||||
/// See design.md for complete pattern specifications and transformation rules:
|
||||
/// `docs/private/roadmap2/phases/phase-188-joinir-loop-pattern-expansion/design.md`
|
||||
///
|
||||
/// # TODO (Phase 188 Task 188-4 Implementation)
|
||||
///
|
||||
/// This function is a skeleton. Implementation steps:
|
||||
///
|
||||
/// 1. **Implement Pattern Detection** (Step 1: 6-8h)
|
||||
/// - Complete `loop_pattern_detection::is_simple_while_pattern()`
|
||||
/// - Test with `apps/tests/loop_min_while.hako`
|
||||
///
|
||||
/// 2. **Implement Pattern 1 Lowering** (Step 1: 6-8h)
|
||||
/// - Complete `loop_patterns::lower_simple_while_to_joinir()`
|
||||
/// - Verify no [joinir/freeze] error
|
||||
///
|
||||
/// 3. **Implement Pattern 2** (Step 2: 6-10h)
|
||||
/// - Complete `loop_pattern_detection::is_loop_with_break_pattern()`
|
||||
/// - Complete `loop_patterns::lower_loop_with_break_to_joinir()`
|
||||
/// - Test with `apps/tests/joinir_min_loop.hako`
|
||||
///
|
||||
/// 4. **Implement Pattern 3** (Step 3: 6-10h)
|
||||
/// - Complete `loop_pattern_detection::is_loop_with_conditional_phi_pattern()`
|
||||
/// - Complete `loop_patterns::lower_loop_with_conditional_phi_to_joinir()`
|
||||
/// - Test with `apps/tests/loop_if_phi.hako`
|
||||
///
|
||||
/// 5. **Integration Testing** (Step 4: 2-4h)
|
||||
/// - Run all 3 tests with JoinIR-only
|
||||
/// - Verify no regressions
|
||||
/// - Document results
|
||||
///
|
||||
/// **Total Estimated Effort**: 18-28 hours
|
||||
pub fn try_lower_loop_pattern_to_joinir(
|
||||
_loop_form: &LoopForm,
|
||||
_lowerer: &mut LoopToJoinLowerer,
|
||||
) -> Option<JoinInst> {
|
||||
// TODO: Implement pattern routing logic
|
||||
//
|
||||
// Pattern 1: Simple While Loop (easiest, most common)
|
||||
// ====================================================
|
||||
//
|
||||
// ```rust
|
||||
// use crate::mir::loop_pattern_detection::is_simple_while_pattern;
|
||||
// use crate::mir::join_ir::lowering::loop_patterns::lower_simple_while_to_joinir;
|
||||
//
|
||||
// if is_simple_while_pattern(loop_form) {
|
||||
// if let Some(inst) = lower_simple_while_to_joinir(loop_form, lowerer) {
|
||||
// return Some(inst);
|
||||
// }
|
||||
// }
|
||||
// ```
|
||||
//
|
||||
// Pattern 2: Loop with Conditional Break (medium complexity)
|
||||
// ===========================================================
|
||||
//
|
||||
// ```rust
|
||||
// use crate::mir::loop_pattern_detection::is_loop_with_break_pattern;
|
||||
// use crate::mir::join_ir::lowering::loop_patterns::lower_loop_with_break_to_joinir;
|
||||
//
|
||||
// if is_loop_with_break_pattern(loop_form) {
|
||||
// if let Some(inst) = lower_loop_with_break_to_joinir(loop_form, lowerer) {
|
||||
// return Some(inst);
|
||||
// }
|
||||
// }
|
||||
// ```
|
||||
//
|
||||
// Pattern 3: Loop with If-Else PHI (leverages existing If lowering)
|
||||
// ==================================================================
|
||||
//
|
||||
// ```rust
|
||||
// use crate::mir::loop_pattern_detection::is_loop_with_conditional_phi_pattern;
|
||||
// use crate::mir::join_ir::lowering::loop_patterns::lower_loop_with_conditional_phi_to_joinir;
|
||||
//
|
||||
// if is_loop_with_conditional_phi_pattern(loop_form) {
|
||||
// if let Some(inst) = lower_loop_with_conditional_phi_to_joinir(loop_form, lowerer) {
|
||||
// return Some(inst);
|
||||
// }
|
||||
// }
|
||||
// ```
|
||||
//
|
||||
// No Pattern Matched (fallback to existing lowering)
|
||||
// ===================================================
|
||||
//
|
||||
// ```rust
|
||||
// // No pattern matched, return None to trigger fallback
|
||||
// None
|
||||
// ```
|
||||
|
||||
// For now, return None (no pattern matched)
|
||||
// This allows existing lowering to continue working
|
||||
None
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
Reference in New Issue
Block a user