feat(joinir): Phase 170-C-2b LoopUpdateSummary wiring into shape detection

Wire LoopUpdateSummary box into real code paths:

- Add `update_summary: Option<LoopUpdateSummary>` field to LoopFeatures
- Add `detect_with_updates()` method to CaseALoweringShape
  - Uses UpdateKind (CounterLike/AccumulationLike) for classification
  - Single CounterLike carrier → StringExamination
  - AccumulationLike present → ArrayAccumulation or IterationWithAccumulation
- Update loop_to_join.rs to use detect_with_updates()
- Update deprecated detect() to use detect_with_updates() internally
- Set update_summary: None in AST-based feature extraction

Carrier name heuristics now encapsulated in LoopUpdateSummary box.
No regression: 15/16 loop smoke tests pass (1 failure is pre-existing).

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
nyash-codex
2025-12-07 14:33:54 +09:00
parent c9458ef11e
commit 4170500c51
4 changed files with 98 additions and 10 deletions

View File

@ -90,6 +90,8 @@ pub fn extract_features(
carrier_count,
break_count: if has_break { 1 } else { 0 },
continue_count: if has_continue { 1 } else { 0 },
// Phase 170-C-2b: AST-based extraction doesn't have carrier names yet
update_summary: None,
}
}

View File

@ -202,6 +202,68 @@ impl CaseALoweringShape {
}
}
/// Phase 170-C-2b: LoopUpdateSummary を使った精度向上版
///
/// `features.update_summary` から UpdateKind を取得して判定。
/// これにより、carrier 名を直接見るコードを CaseALoweringShape から排除できる。
///
/// # Arguments
/// * `features` - LoopFeatures (update_summary 含む)
/// * `carrier_count` - Number of carrier variables
/// * `has_progress_carrier` - Whether progress carrier exists
///
/// # Returns
/// CaseALoweringShape classification based on UpdateSummary
pub fn detect_with_updates(
features: &crate::mir::loop_pattern_detection::LoopFeatures,
carrier_count: usize,
has_progress_carrier: bool,
) -> Self {
use crate::mir::join_ir::lowering::loop_update_summary::UpdateKind;
// Case-A requirement: must have a progress carrier
if !has_progress_carrier {
return CaseALoweringShape::NotCaseA;
}
// Case-A requirement: no complex control flow (continue)
if features.has_continue {
return CaseALoweringShape::NotCaseA;
}
// Phase 170-C-2b: Use UpdateSummary if available
if let Some(ref summary) = features.update_summary {
// Single carrier with CounterLike update → StringExamination
if summary.carriers.len() == 1
&& summary.carriers[0].kind == UpdateKind::CounterLike
{
return CaseALoweringShape::StringExamination;
}
// Any AccumulationLike carrier → ArrayAccumulation (for single carrier)
// or IterationWithAccumulation (for multiple carriers)
if summary.carriers.iter().any(|c| c.kind == UpdateKind::AccumulationLike) {
if carrier_count == 1 {
return CaseALoweringShape::ArrayAccumulation;
} else {
return CaseALoweringShape::IterationWithAccumulation;
}
}
// Multiple carriers without clear accumulation → IterationWithAccumulation
if carrier_count >= 2 {
return CaseALoweringShape::IterationWithAccumulation;
}
}
// Fallback to carrier count heuristic
match carrier_count {
0 => CaseALoweringShape::NotCaseA,
1 => CaseALoweringShape::Generic,
2.. => CaseALoweringShape::IterationWithAccumulation,
}
}
/// Typical index variable name detection
///
/// StringExamination パターンskip/trimで使われる典型的なインデックス名
@ -229,6 +291,12 @@ impl CaseALoweringShape {
let has_progress_carrier = scope.progress_carrier.is_some();
let carrier_count = scope.carriers.len();
// Phase 170-C-2b: Build update_summary from carrier names
// Note: carriers is BTreeSet<String>, so each item is already a String
let carrier_names: Vec<String> = scope.carriers.iter().cloned().collect();
let update_summary =
crate::mir::join_ir::lowering::loop_update_summary::analyze_loop_updates(&carrier_names);
// Create stub features (Phase 170-B will use real LoopFeatures)
let stub_features = crate::mir::loop_pattern_detection::LoopFeatures {
has_break: false, // Unknown from LoopScopeShape alone
@ -238,9 +306,10 @@ impl CaseALoweringShape {
carrier_count,
break_count: 0,
continue_count: 0,
update_summary: Some(update_summary),
};
Self::detect_from_features(&stub_features, carrier_count, has_progress_carrier)
Self::detect_with_updates(&stub_features, carrier_count, has_progress_carrier)
}
/// Is this a recognized lowering shape?

View File

@ -29,6 +29,7 @@ use crate::mir::control_form::{analyze_exits, ExitEdge, LoopControlShape, LoopId
use crate::mir::join_ir::lowering::generic_case_a;
use crate::mir::join_ir::lowering::loop_form_intake::intake_loop_form;
use crate::mir::join_ir::lowering::loop_scope_shape::{CaseALoweringShape, LoopScopeShape};
use crate::mir::join_ir::lowering::loop_update_summary; // Phase 170-C-2b
use crate::mir::join_ir::JoinModule;
use crate::mir::loop_form::LoopForm;
// Phase 48-2: Trio imports removed - now internal to LoopScopeShape::from_loop_form()
@ -383,11 +384,12 @@ impl LoopToJoinLowerer {
// This enables gradual migration: existing name-based code continues to work,
// while new patterns can be added purely by structure detection.
// Phase 170-C-1: Use carrier name-based detection for improved accuracy
// Extract progress carrier name from scope
let progress_carrier_name = scope.progress_carrier.as_deref();
// Phase 170-C-2b: Build update_summary from carrier names
// Note: carriers is BTreeSet<String>, so each item is already a String
let carrier_names: Vec<String> = scope.carriers.iter().cloned().collect();
let update_summary = loop_update_summary::analyze_loop_updates(&carrier_names);
// Construct minimal LoopFeatures from scope (for now)
// Construct LoopFeatures with update_summary
// TODO: Pass real LoopFeatures from LoopPatternDetection when available
let stub_features = crate::mir::loop_pattern_detection::LoopFeatures {
has_break: false, // Unknown at this level
@ -397,25 +399,25 @@ impl LoopToJoinLowerer {
carrier_count: scope.carriers.len(),
break_count: 0,
continue_count: 0,
update_summary: Some(update_summary),
};
let has_progress_carrier = scope.progress_carrier.is_some();
let carrier_count = scope.carriers.len();
// Phase 170-C-1: Use new carrier name-based detection
let shape = CaseALoweringShape::detect_with_carrier_name(
// Phase 170-C-2b: Use new UpdateSummary-based detection
let shape = CaseALoweringShape::detect_with_updates(
&stub_features,
carrier_count,
has_progress_carrier,
progress_carrier_name,
);
if self.debug {
eprintln!(
"[LoopToJoinLowerer] Phase 170-C-1: shape={:?}, name={:?}, carrier={:?}",
"[LoopToJoinLowerer] Phase 170-C-2b: shape={:?}, name={:?}, carriers={:?}",
shape.name(),
name,
progress_carrier_name
carrier_names
);
}

View File

@ -136,6 +136,13 @@ pub struct LoopFeatures {
/// Number of continue targets
pub continue_count: usize,
/// Phase 170-C-2b: Carrier update pattern summary
///
/// Contains UpdateKind (CounterLike/AccumulationLike/Other) for each carrier.
/// Used by CaseALoweringShape for more precise shape detection.
/// None if carrier names are not available.
pub update_summary: Option<crate::mir::join_ir::lowering::loop_update_summary::LoopUpdateSummary>,
}
impl LoopFeatures {
@ -208,6 +215,13 @@ pub fn extract_features(loop_form: &LoopForm, scope: Option<&LoopScopeShape>) ->
// For now, we infer it from carrier_count > 1 (Pattern 3 heuristic)
let has_if = has_if_else_phi;
// Phase 170-C-2b: Build update_summary from carrier names
// Note: carriers is BTreeSet<String>, so each item is already a String
let update_summary = scope.map(|s| {
let carrier_names: Vec<String> = s.carriers.iter().cloned().collect();
crate::mir::join_ir::lowering::loop_update_summary::analyze_loop_updates(&carrier_names)
});
LoopFeatures {
has_break,
has_continue,
@ -216,6 +230,7 @@ pub fn extract_features(loop_form: &LoopForm, scope: Option<&LoopScopeShape>) ->
carrier_count,
break_count,
continue_count,
update_summary,
}
}