fix(joinir): improve Pattern3 classification to exclude simple conditional assignment (Phase 264 P0)

Problem:
- Pattern3 heuristic was too conservative: detect_if_in_body() returned
  true for ANY if statement, causing simple conditional assignments to be
  misclassified as Pattern3IfPhi
- Example: `if i == 0 then seg = "first" else seg = "other"` was routed
  to Pattern3, but Pattern3 only handles if-sum patterns like
  `sum = sum + (if x then 1 else 0)`
- This caused loops with conditional assignment to fail Pattern3 check
  and exhaust all routing paths

Solution (Conservative, Phase 264 P0):
- ast_feature_extractor.rs:
  - detect_if_else_phi_in_body(): Always return false
  - has_if = has_if_else_phi (don't use detect_if_in_body())
- loop_pattern_detection/mod.rs:
  - Add has_if_sum_signature() (returns false for P0)
  - has_if_else_phi = carrier_count > 1 && has_if_sum_signature(scope)

Effect:
- Simple conditional assignment loops now fall through to Pattern1 
- Pattern3 misrouting prevented 

Results:
- Lib tests: 1368/1368 PASS (no regression)
- Minimal repro: phase264_p0_bundle_resolver_loop_min.hako PASS 
- Quick smoke: 45/46 (unchanged - complex BundleResolver loop needs P1)

Phase 264 P1 TODO:
- Implement accurate if-sum signature detection (AST/CFG analysis)
- Support complex nested loops in Pattern2 or new pattern

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

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
2025-12-21 11:49:03 +09:00
parent e17902a443
commit be4de67601
5 changed files with 416 additions and 27 deletions

View File

@ -200,6 +200,31 @@ impl LoopFeatures {
}
}
/// Phase 264 P0: Detect if-sum pattern signature
///
/// Returns true if loop has if-else with arithmetic accumulation pattern:
/// - Same variable updated in both if and else branches
/// - Update involves arithmetic operations (BinOp: Add, Sub, etc.)
///
/// Example: sum = sum + (if x then 1 else 0)
///
/// Simple conditional assignment (seg = if x then "A" else "B") returns false.
///
/// # Phase 264 P0: Conservative Implementation
///
/// For now, this function returns false to allow carrier_count > 1 loops
/// to fall through to Pattern1 instead of being misclassified as Pattern3IfPhi.
///
/// # Phase 264 P1: TODO
///
/// Implement accurate if-sum signature detection via AST/CFG analysis.
fn has_if_sum_signature(_scope: Option<&LoopScopeShape>) -> bool {
// Phase 264 P0: Conservative - always return false
// This makes carrier_count > 1 loops with simple conditional assignment
// fall through to Pattern1 instead of being classified as Pattern3IfPhi
false
}
/// Extract features from LoopForm for pattern classification.
///
/// This function is the entry point for structure-based pattern detection.
@ -224,10 +249,14 @@ pub(crate) fn extract_features(
// Phase 194+: Extract carrier_count from LoopScopeShape if available
let carrier_count = scope.map(|s| s.carriers.len()).unwrap_or(0);
// Pattern 3 heuristic: has_if_else_phi if carrier_count > 1
// This is a conservative heuristic - multiple carriers typically
// indicate if-else statements with PHI nodes.
let has_if_else_phi = carrier_count > 1;
// Phase 264 P0: Improved if-else PHI detection
// Pattern3 heuristic: has_if_else_phi if there's an if-sum pattern signature
// - Multiple carriers (carrier_count > 1)
// - AND at least one carrier updated in both if/else branches with arithmetic
//
// Simple conditional assignment (seg = if x then "A" else "B") should NOT
// be classified as Pattern3IfPhi - it should fall through to Pattern1.
let has_if_else_phi = carrier_count > 1 && has_if_sum_signature(scope);
// TODO: Implement has_if detection via CFG analysis
// For now, we infer it from carrier_count > 1 (Pattern 3 heuristic)