Files
hakorune/docs/development/current/main/phase213-if-sum-implementation.md
nyash-codex 338d1aecf1 feat(joinir): Phase 213 AST-based if-sum lowerer for Pattern 3
Implement dual-mode architecture for Pattern 3 (Loop with If-Else PHI):

- Add is_simple_if_sum_pattern() detection helper
  - Detects 1 CounterLike + 1-2 AccumulationLike carrier patterns
  - Unit tests for various carrier compositions

- Add dual-mode dispatch in Pattern3 lowerer
  - ctx.is_if_sum_pattern() branches to AST-based vs legacy PoC
  - Legacy mode preserved for backward compatibility

- Create loop_with_if_phi_if_sum.rs (~420 lines)
  - AST extraction: loop condition, if condition, updates
  - JoinIR generation: main, loop_step, k_exit structure
  - Helper functions: extract_loop_condition, extract_if_condition, etc.

- Extend PatternPipelineContext for Pattern 3
  - is_if_sum_pattern() detection using LoopUpdateSummary
  - extract_if_statement() helper for body analysis

Note: E2E RC=2 not yet achieved due to pre-existing Pattern 3
pipeline issue (loop back branch targets wrong block). This
affects both if-sum and legacy modes. Fix planned for Phase 214.

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-10 00:54:46 +09:00

99 lines
3.2 KiB
Markdown

# 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**:
1. **if-sum mode**: AST-based lowering for simple patterns (e.g., `phase212_if_sum_min.hako`)
2. **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
1. **`loop_update_summary.rs`**
- Added `is_simple_if_sum_pattern()` method
- Detects: 1 CounterLike + 1-2 AccumulationLike carriers
2. **`pattern_pipeline.rs`**
- Added `is_if_sum_pattern()` to PatternPipelineContext
- Added `extract_if_statement()` helper
3. **`pattern3_with_if_phi.rs`**
- Dual-mode dispatch: `ctx.is_if_sum_pattern()` → branch
- `lower_pattern3_if_sum()`: calls AST-based lowerer
- `lower_pattern3_legacy()`: existing hardcoded logic
4. **`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`)
- Generates JoinIR with dynamic values
### Pattern Detection
```rust
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 `bb5` instead of loop header `bb4`
- 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
1. **Phase 214+**: Fix Pattern 3 JoinIR → MIR pipeline
- Investigate loop back branch target
- Ensure PHI updates are correctly wired
2. **Phase 213-B** (optional): Support variable limits
- Currently only integer literals supported
- `i < len` where `len` is 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.rs`