feat(joinir): Phase 212.5 Structural if detection for Pattern 3 routing

Phase 212.5 で発見した「ループ内 if が Pattern 1 に誤ルーティング」問題を修正。
構造ベース if 検出により、単一キャリアの if-update パターンも Pattern 3 へ正しくルーティング可能に。

## Changes

### 1. AST Feature Extractor (ast_feature_extractor.rs)
- **Added**: `detect_if_in_body()` function
  - Detects ANY if statement in loop body (not just if-else)
  - Enables structural if detection vs carrier-count heuristic
- **Modified**: `extract_features()`
  - Uses `detect_if_in_body()` for `has_if` detection
  - Removes dependency on carrier count for if detection

### 2. Loop Pattern Classification (loop_pattern_detection/mod.rs)
- **Modified**: `classify()` function
  - Pattern 3: `carrier_count > 1` → `has_if && carrier_count >= 1`
  - Pattern 1: `!has_if_else_phi` → `!has_if`
  - Now routes single-carrier if-update patterns to Pattern 3

## Verification

Test case: `apps/tests/phase212_if_sum_min.hako`

### Before (Phase 212):
-  Routed to Pattern 1 (wrong)
-  if statement disappeared in MIR
-  Carriers: only `i` (sum missing)

### After (Phase 212.5):
-  Routed to Pattern 3 (correct!)
-  MIR contains PHI nodes: `%31 = phi [%25, bb9], [%29, bb10]`
-  Carriers: `i`, `sum`, `count` detected

Pattern routing log:
```
[joinir/pattern3] Generated JoinIR for Loop with If-Else PHI
[joinir/pattern3] Carriers: i (counter), sum (accumulator), count (counter)
```

## Known Limitation

Pattern 3 lowerer (`lower_loop_with_if_phi_pattern`) is currently a
test-only hardcoded implementation:
- Loop condition: `i <= 5` (hardcoded)
- If condition: `i % 2 == 1` (hardcoded)
- Update logic: `sum + i` (hardcoded)

This is why `phase212_if_sum_min.hako` produces RC=0 instead of RC=2.
Pattern routing is complete; AST-based lowerer generalization is Phase 213.

## Documentation

- `docs/development/current/main/phase212-5-implementation-complete.md`
  - Complete implementation report with verification details

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

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
nyash-codex
2025-12-09 23:35:57 +09:00
parent 4c323b709e
commit aeb6282c2d
3 changed files with 253 additions and 8 deletions

View File

@ -240,18 +240,20 @@ pub(crate) fn extract_features(loop_form: &LoopForm, scope: Option<&LoopScopeSha
/// structure-based rules. It does NOT depend on function names or
/// variable names like "sum".
///
/// # Pattern Classification Rules
/// # Pattern Classification Rules (Phase 212.5: Structural if detection)
///
/// 1. **Pattern 4 (Continue)**: `has_continue == true`
/// - Priority: Check first (most specific)
///
/// 2. **Pattern 3 (If-Else PHI)**: `has_if_else_phi && !has_break && !has_continue`
/// - Multiple carriers indicate if-else with PHI
/// 2. **Pattern 3 (If-PHI)**: `has_if && carrier_count >= 1 && !has_break && !has_continue`
/// - Phase 212.5: Changed from carrier_count > 1 to structural if detection
/// - Includes single-carrier if-update patterns (e.g., if-sum with 1 carrier)
///
/// 3. **Pattern 2 (Break)**: `has_break && !has_continue`
/// - Has break but no continue
///
/// 4. **Pattern 1 (Simple While)**: `!has_break && !has_continue && !has_if_else_phi`
/// 4. **Pattern 1 (Simple While)**: `!has_break && !has_continue && !has_if`
/// - Phase 212.5: Exclude loops with if statements
/// - No control flow alterations
///
/// # Arguments
@ -271,8 +273,9 @@ pub fn classify(features: &LoopFeatures) -> LoopPatternKind {
return LoopPatternKind::Pattern4Continue;
}
// Pattern 3: If-Else PHI (check before Pattern 1)
if features.has_if_else_phi && !features.has_break && !features.has_continue {
// Pattern 3: If-PHI (check before Pattern 1)
// Phase 212.5: Structural if detection - route to P3 if has_if && carrier_count >= 1
if features.has_if && features.carrier_count >= 1 && !features.has_break && !features.has_continue {
return LoopPatternKind::Pattern3IfPhi;
}
@ -282,7 +285,8 @@ pub fn classify(features: &LoopFeatures) -> LoopPatternKind {
}
// Pattern 1: Simple While
if !features.has_break && !features.has_continue && !features.has_if_else_phi {
// Phase 212.5: Exclude loops with if statements (they go to P3)
if !features.has_break && !features.has_continue && !features.has_if {
return LoopPatternKind::Pattern1SimpleWhile;
}