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>
7.4 KiB
Phase 212.5: 実装完了レポート
Date: 2025-12-09 Status: ✅ 完了 (Structural if detection + Pattern 3 routing) Commit: [To be filled after commit]
🎯 達成した目標
✅ Task 1: 構造ベース if 検出実装
修正ファイル: src/mir/builder/control_flow/joinir/patterns/ast_feature_extractor.rs
変更内容:
-
新規関数
detect_if_in_body()追加 (line 120-127)- ループ本体に ANY if 文が存在するかを検出
- if-else だけでなく、単一 if 文も検出
-
extract_features()を修正 (line 81-106)has_if = detect_if_in_body(body)で構造ベース検出- 従来の carrier_count ヒューリスティックから脱却
✅ Task 2: Pattern 分類ロジック更新
修正ファイル: src/mir/loop_pattern_detection/mod.rs
変更内容: classify() 関数を更新 (line 270-295)
Before (carrier-count heuristic):
// Pattern 3: If-Else PHI (check before Pattern 1)
if features.has_if_else_phi && !features.has_break && !features.has_continue {
return LoopPatternKind::Pattern3IfPhi;
}
// Pattern 1: Simple While
if !features.has_break && !features.has_continue && !features.has_if_else_phi {
return LoopPatternKind::Pattern1SimpleWhile;
}
After (structural if detection):
// Pattern 3: If-PHI
// 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;
}
// Pattern 1: Simple While
// 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;
}
重要な変更点:
- Pattern 3 条件:
carrier_count > 1→has_if && carrier_count >= 1 - Pattern 1 条件:
!has_if_else_phi→!has_if - 単一キャリアの if-update パターンも Pattern 3 にルーティング可能に!
📊 検証結果
Test Case: apps/tests/phase212_if_sum_min.hako
Before (Phase 212):
- Pattern: Pattern 1 (Simple While) ← 誤ルーティング
- MIR: if 文が消失
- Carriers:
iのみ (sum消失) - Output: RC=0 (期待: RC=2)
After (Phase 212.5):
- ✅ Pattern: Pattern 3 (If-Else PHI) ← 正しくルーティング!
- ✅ MIR: PHI ノード生成成功
- ✅ Carriers:
i,sum,countの 3 つ検出 - ✅ if-else merge:
%31 = phi [%25, bb9], [%29, bb10]生成
パターンルーティングログ:
[joinir/pattern3] Generated JoinIR for Loop with If-Else PHI (Phase 195: multi-carrier)
[joinir/pattern3] Functions: main, loop_step, k_exit
[joinir/pattern3] Carriers: i (counter), sum (accumulator), count (counter) [Phase 195]
[joinir/pattern3] If-Else PHI in loop body:
[joinir/pattern3] sum_new = (i % 2 == 1) ? sum+i : sum+0
生成された MIR (抜粋):
bb8:
1: %24 = icmp Eq %22, %23 ; if condition
1: %25 = %9 Add %8 ; then branch
1: %29 = %9 Add %28 ; else branch
1: br %24, label bb9, label bb10
bb11:
1: %31 = phi [%25, bb9], [%29, bb10] ; ← PHI merge!
🔍 発見した制約 (Pattern 3 Lowerer)
現状の Pattern 3 実装について
Pattern 3 lowerer (lower_loop_with_if_phi_pattern) はテスト専用のハードコード実装:
Hardcoded elements:
- Loop condition:
i <= 5(固定値) - If condition:
i % 2 == 1(modulo 演算固定) - Update logic:
sum = sum + i/count = count + 1
Why RC=0 instead of RC=2?
phase212_if_sum_min.hako の実際の条件:
loop(i < 3) { // Expected: i < 3
if i > 0 { // Expected: i > 0
sum = sum + 1 // Expected: sum + 1
}
}
Pattern 3 lowerer が生成した MIR:
loop(i <= 5) { // Actual: i <= 5 (hardcoded)
if i % 2 == 1 { // Actual: i % 2 == 1 (hardcoded)
sum = sum + i // Actual: sum + i (hardcoded)
}
}
Conclusion: Pattern 3 lowerer is not AST-based, it's a hardcoded test pattern generator.
🎉 Phase 212.5 の価値
✅ 達成したこと
-
構造ベース if 検出の実装
- Carrier count ヒューリスティック脱却
- 単一キャリアの if-update パターンサポート
-
Pattern routing の修正
- if 文を含むループが Pattern 3 に正しくルーティング
- Pattern 1 からの誤った分類を解消
-
MIR PHI 生成の確認
- Pattern 3 lowerer が if-else PHI を正常生成
- JoinIR → MIR 変換パイプライン正常動作
-
アーキテクチャの理解深化
- AST feature extraction → LoopFeatures → classify → routing の流れ確認
- Pattern 3 lowerer の現状制約を文書化
📝 残タスク (Phase 212 継続)
To enable full if-sum support:
-
Pattern 3 lowerer を AST-based に書き換え
lower_loop_with_if_phi_pattern()の汎用化- AST if condition の動的読み込み
- AST update expression の動的読み込み
-
Or: Pattern 5/6 で汎用 if-sum lowerer を実装
- Phase 212 の本来の目標
- Pattern 3 は test-only のまま残す選択肢
🚀 Next Steps
Option A: Pattern 3 を汎用化
- Pros: 既存 Pattern を完全に
- Cons: リファクタ規模大
Option B: Pattern 5/6 で新規実装
- Pros: クリーンな設計
- Cons: 新規 Pattern 追加コスト
Option C: 段階的移行 (推奨)
- Phase 212.5 完了として記録 ✅
- Phase 212 で汎用 if-sum pattern (P5?) を設計
- Pattern 3 は test-only として保持
📦 Modified Files Summary
-
src/mir/builder/control_flow/joinir/patterns/ast_feature_extractor.rs- Added:
detect_if_in_body()function - Modified:
extract_features()to use structural if detection
- Added:
-
src/mir/loop_pattern_detection/mod.rs- Modified:
classify()to usehas_if && carrier_count >= 1for Pattern 3 - Modified: Pattern 1 to exclude loops with if statements
- Modified:
-
docs/development/current/main/phase212-5-implementation-complete.md(this file)- Complete implementation report
✅ Checklist
- Structural if detection implemented (
detect_if_in_body) - AST feature extraction updated (
extract_features) - Pattern classification logic updated (
classify) - Build successful (cargo build --release)
- Test execution successful (phase212_if_sum_min.hako)
- Pattern routing verified (Pattern 3 selected)
- MIR PHI generation verified (bb11: %31 = phi)
- Pattern 3 lowerer constraints documented
- Implementation report created
🔖 Status Update
Phase 212.5: COMPLETE ✅ (Commit: aeb6282c)
Scope Completed:
- ✅ Structural if detection (
detect_if_in_body) - ✅ Pattern routing logic (classify with
has_if && carrier_count >= 1) - ✅ Test verification (Pattern 3 routing confirmed)
- ✅ MIR PHI generation verified
Out of Scope (Phase 213):
Pattern 3 lowerer (lower_loop_with_if_phi_pattern) is currently a test-only PoC implementation with hardcoded conditions and updates. AST-based generalization for if-sum patterns is handled in Phase 213.
Next: Phase 213 will generalize Pattern 3 lowerer to read actual AST conditions and update expressions dynamically.
Phase 212.5: COMPLETE ✅
Status: Active
Scope: If-sum 実装完了メモ(JoinIR v2)