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

@ -83,6 +83,9 @@ pub fn extract_features(
has_continue: bool,
has_break: bool,
) -> LoopFeatures {
// Phase 212.5: Detect ANY if statement in loop body (structural detection)
let has_if = detect_if_in_body(body);
// Detect if-else statements with PHI pattern
let has_if_else_phi = detect_if_else_phi_in_body(body);
@ -92,7 +95,7 @@ pub fn extract_features(
LoopFeatures {
has_break,
has_continue,
has_if: has_if_else_phi,
has_if,
has_if_else_phi,
carrier_count,
break_count: if has_break { 1 } else { 0 },
@ -102,6 +105,27 @@ pub fn extract_features(
}
}
/// Phase 212.5: Detect ANY if statement in loop body (structural detection)
///
/// This function detects any if statement, regardless of whether it has an else branch.
/// Used for routing single-carrier if-update patterns to Pattern 3.
///
/// # Arguments
///
/// * `body` - Loop body statements to analyze
///
/// # Returns
///
/// `true` if at least one if statement is found (with or without else)
fn detect_if_in_body(body: &[ASTNode]) -> bool {
for node in body {
if let ASTNode::If { .. } = node {
return true;
}
}
false
}
/// Detect if-else statements with potential PHI pattern
///
/// Looks for if-else statements where both branches contain assignments.