refactor(joinir): Unify Pattern 4 with PatternPipelineContext
Phase 179-B Task 6: Refactor Pattern 4 to use PatternPipelineContext for unified preprocessing. Changes: - Use build_pattern_context() for initial loop variable extraction and scope construction - Extract loop_var_name, loop_var_id, carrier_info_prelim, and scope from context - Keep Pattern4CarrierAnalyzer logic inline (Select-based continue semantics) - Reduce line count: 422 → 414 lines (1.9% reduction) Note: Pattern 4 has complex carrier analysis (Select-based continue, carrier filtering, normalization) that requires specialized Pattern4CarrierAnalyzer. The minimal refactoring maintains this complexity while establishing the pipeline pattern. Benefits: - Consistent entry point with Patterns 1-3 - Unified preprocessing flow - Maintains all existing functionality and test compatibility 🤖 Generated with Claude Code (https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@ -133,9 +133,16 @@ pub fn lower(
|
||||
}
|
||||
|
||||
impl MirBuilder {
|
||||
/// Phase 33-21: Pattern 4 (Loop with Continue) lowerer
|
||||
/// Phase 179-B: Pattern 4 (Loop with Continue) lowerer
|
||||
///
|
||||
/// Handles loops with continue statements using Select-based carrier updates.
|
||||
/// **Refactored**: Now uses PatternPipelineContext for unified preprocessing
|
||||
///
|
||||
/// # Pipeline (Phase 179-B)
|
||||
/// 1. Build preprocessing context → PatternPipelineContext
|
||||
/// 2. Pattern 4-specific processing (continue normalization, carrier analysis)
|
||||
/// 3. Call JoinIR lowerer → JoinModule
|
||||
/// 4. Create boundary from context → JoinInlineBoundary
|
||||
/// 5. Merge MIR blocks → JoinIRConversionPipeline
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
@ -152,15 +159,8 @@ impl MirBuilder {
|
||||
/// // sum = 25 (1+3+5+7+9)
|
||||
/// ```
|
||||
///
|
||||
/// # Implementation
|
||||
///
|
||||
/// 1. Extract loop variable from condition
|
||||
/// 2. Build CarrierInfo from variable_map
|
||||
/// 3. Analyze carrier update expressions from loop body
|
||||
/// 4. Generate JoinIR with Select-based continue semantics
|
||||
/// 5. Convert JoinModule → MirModule
|
||||
/// 6. Merge MIR blocks with header PHI and exit bindings
|
||||
/// 7. Return Void (loop result accessed via variable_map)
|
||||
/// Note: Pattern 4 has complex carrier analysis logic that remains inline
|
||||
/// for now, using Pattern4CarrierAnalyzer for Select-based continue semantics.
|
||||
pub(in crate::mir::builder) fn cf_loop_pattern4_with_continue(
|
||||
&mut self,
|
||||
condition: &ASTNode,
|
||||
@ -169,8 +169,6 @@ impl MirBuilder {
|
||||
debug: bool,
|
||||
) -> Result<Option<ValueId>, String> {
|
||||
use crate::mir::join_ir::lowering::loop_with_continue_minimal::lower_loop_with_continue_minimal;
|
||||
use crate::mir::BasicBlockId;
|
||||
use std::collections::BTreeSet;
|
||||
use super::pattern4_carrier_analyzer::Pattern4CarrierAnalyzer;
|
||||
|
||||
// Phase 195: Use unified trace
|
||||
@ -182,15 +180,21 @@ impl MirBuilder {
|
||||
let normalized_body = Pattern4CarrierAnalyzer::normalize_continue_branches(_body);
|
||||
let body_to_analyze = &normalized_body;
|
||||
|
||||
// Phase 33-22: Use CommonPatternInitializer for loop variable extraction
|
||||
use super::common_init::CommonPatternInitializer;
|
||||
let (loop_var_name, loop_var_id, carrier_info_prelim) =
|
||||
CommonPatternInitializer::initialize_pattern(
|
||||
self,
|
||||
condition,
|
||||
&self.variable_map,
|
||||
None, // Pattern 4 will filter carriers via LoopUpdateAnalyzer
|
||||
)?;
|
||||
// Phase 179-B: Use PatternPipelineContext for unified preprocessing
|
||||
// Note: Pattern 4 still needs inline processing for carrier analysis
|
||||
use super::pattern_pipeline::{build_pattern_context, PatternVariant};
|
||||
let ctx = build_pattern_context(
|
||||
self,
|
||||
condition,
|
||||
body_to_analyze,
|
||||
PatternVariant::Pattern4,
|
||||
)?;
|
||||
|
||||
// Extract from context
|
||||
let loop_var_name = ctx.loop_var_name.clone();
|
||||
let loop_var_id = ctx.loop_var_id;
|
||||
let carrier_info_prelim = ctx.carrier_info.clone();
|
||||
let scope = ctx.loop_scope.clone();
|
||||
|
||||
// Phase 33-23: Use Pattern4CarrierAnalyzer for carrier filtering
|
||||
// Analyze carrier update expressions FIRST to identify actual carriers
|
||||
@ -230,18 +234,6 @@ impl MirBuilder {
|
||||
// Phase 195: Use unified trace
|
||||
trace::trace().varmap("pattern4_start", &self.variable_map);
|
||||
|
||||
// Phase 171-172: Use LoopScopeShapeBuilder for unified initialization (Issue 4)
|
||||
// Extract body_locals from loop body AST for proper variable classification
|
||||
use super::loop_scope_shape_builder::LoopScopeShapeBuilder;
|
||||
let scope = LoopScopeShapeBuilder::with_body_locals(
|
||||
BasicBlockId(0),
|
||||
BasicBlockId(0),
|
||||
BasicBlockId(0),
|
||||
BasicBlockId(0),
|
||||
BTreeSet::new(),
|
||||
body_to_analyze,
|
||||
);
|
||||
|
||||
// Phase 171-C-3: LoopBodyCarrierPromoter integration
|
||||
// Check if LoopConditionScopeBox detects LoopBodyLocal variables, and attempt promotion
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user