Unify condition lowering logic across Pattern 2/4 with trait-based API. New infrastructure: - condition_lowering_box.rs: ConditionLoweringBox trait + ConditionContext (293 lines) - ExprLowerer implements ConditionLoweringBox trait (+51 lines) Pattern migrations: - Pattern 2 (loop_with_break_minimal.rs): Use trait API - Pattern 4 (loop_with_continue_minimal.rs): Use trait API Benefits: - Unified condition lowering interface - Extensible for future lowering strategies - Clean API boundary between patterns and lowering logic - Zero code duplication Test results: 911/911 PASS (+2 new tests) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
3.3 KiB
3.3 KiB
Phase 213: Pattern 3 If-Sum AST-based Lowerer
Overview
Phase 213 implements an AST-based JoinIR lowerer for "simple if-sum" patterns in Pattern 3 (Loop with If-Else PHI).
Design Decision: Approach B
Dual-mode architecture:
- if-sum mode: AST-based lowering for simple patterns (e.g.,
phase212_if_sum_min.hako) - legacy mode: Hardcoded PoC lowering for existing tests (e.g.,
loop_if_phi.hako)
This approach:
- Minimizes risk by keeping legacy code path intact
- Only generalizes for detected if-sum patterns
- Enables incremental migration
Implementation
Files Modified/Created
-
loop_update_summary.rs- Added
is_simple_if_sum_pattern()method - Detects: 1 CounterLike + 1-2 AccumulationLike carriers
- Added
-
pattern_pipeline.rs- Added
is_if_sum_pattern()to PatternPipelineContext - Added
extract_if_statement()helper
- Added
-
pattern3_with_if_phi.rs- Dual-mode dispatch:
ctx.is_if_sum_pattern()→ branch lower_pattern3_if_sum(): calls AST-based lowererlower_pattern3_legacy(): existing hardcoded logic
- Dual-mode dispatch:
-
loop_with_if_phi_if_sum.rs(NEW)- AST-based if-sum lowerer (~420 lines)
- Extracts from AST:
- Loop condition (
i < len) - If condition (
i > 0) - Then update (
sum = sum + 1) - Counter update (
i = i + 1)
- Loop condition (
- Generates JoinIR with dynamic values
Pattern Detection
pub fn is_simple_if_sum_pattern(&self) -> bool {
if self.counter_count() != 1 { return false; }
if self.accumulation_count() < 1 { return false; }
if self.accumulation_count() > 2 { return false; }
true
}
AST Extraction
The lowerer extracts pattern components from AST:
extract_loop_condition():i < 3→ (var="i", op=Lt, limit=3)extract_if_condition():i > 0→ (var="i", op=Gt, value=0)extract_then_update():sum = sum + 1→ (var="sum", addend=1)extract_counter_update():i = i + 1→ (var="i", step=1)
Testing Results
AST Extraction: Working ✅
[joinir/pattern3/if-sum] Loop condition: i Lt 3
[joinir/pattern3/if-sum] If condition: i Gt 0
[joinir/pattern3/if-sum] Then update: sum += 1
[joinir/pattern3/if-sum] Counter update: i += 1
Known Issue: Pattern 3 Pipeline
Both if-sum mode and legacy mode return RC=0 instead of expected values.
This is a pre-existing issue in the JoinIR → MIR conversion pipeline (Phase 33-21/177):
- Loop back branch targets
bb5instead of loop headerbb4 - PHI nodes not properly updated on loop back
This issue is NOT specific to Phase 213 - the legacy loop_if_phi.hako has the same problem.
Future Work
-
Phase 214+: Fix Pattern 3 JoinIR → MIR pipeline
- Investigate loop back branch target
- Ensure PHI updates are correctly wired
-
Phase 213-B (optional): Support variable limits
- Currently only integer literals supported
i < lenwherelenis a variable → Phase 214+
File Locations
- Lowerer:
src/mir/join_ir/lowering/loop_with_if_phi_if_sum.rs - Dispatcher:
src/mir/builder/control_flow/joinir/patterns/pattern3_with_if_phi.rs - Pattern detection:
src/mir/join_ir/lowering/loop_update_summary.rs - Pipeline context:
src/mir/builder/control_flow/joinir/patterns/pattern_pipeline.rsStatus: Active
Scope: If-sum 実装(JoinIR v2)