refactor(joinir): Phase 286C-2.1 Step 4-5 (Partial) - Document 3-stage pipeline structure

Steps 3-5 Summary:
-  Step 3: scan_blocks() extraction complete (94 lines, functional)
-  Step 4-5: Documented plan/apply separation (requires future work)

What was achieved:
1. scan_blocks() now identifies tail calls and returns (read-only)
2. Clear orchestrator structure documented in merge_and_rewrite()
3. Strategic markers show what belongs to each stage
4. Foundation laid for future extraction of plan/apply logic

Current state:
- scan_blocks(): 94 lines (EXTRACTED )
- plan_rewrites(): Stub (main loop lines 380-1469 contains logic)
- apply_rewrites(): Stub (builder mutations intertwined with plan)
- merge_and_rewrite(): ~1300 lines (needs further refactoring)

Target state (future work):
- scan_blocks(): ~100 lines (identification only)
- plan_rewrites(): ~400 lines (block generation, no builder mutation)
- apply_rewrites(): ~200 lines (builder mutation only)
- merge_and_rewrite(): ~100-200 lines (orchestrator)

Why partial completion:
- Main loop (1400 lines) has highly intertwined plan + apply logic
- Safe extraction requires careful separation (multi-session work)
- Current commit achieves scan extraction + clear architecture docs
- Follows 80/20 rule: Working scan stage + clear path forward

Next steps (future):
- Extract parameter binding logic → plan_rewrites()
- Extract block generation logic → plan_rewrites()
- Extract builder.add_block() calls → apply_rewrites()
- Move PHI/carrier input collection → plan_rewrites()

Build: cargo build --release 
Tests: Existing tests pass (no behavior changes)

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2025-12-25 03:31:43 +09:00
parent 7e259ad334
commit bfee1fd451

View File

@ -365,9 +365,17 @@ pub(super) fn merge_and_rewrite(
// The issue was that pre-pass used stale remapper values before Phase 33-21 updates.
// Instead, we now generate Copies for non-carrier params in the main block processing loop.
// Phase 286C-2.1 Step 2: Call scan_blocks() to establish 3-stage pipeline
// Currently returns empty plan - future steps will extract instruction scanning logic
let _scan_plan = scan_blocks(mir_module, remapper, value_to_func_name, &ctx, debug)?;
// Phase 286C-2.1 Step 3: Call scan_blocks() to identify rewrites
let scan_plan = scan_blocks(mir_module, remapper, value_to_func_name, &ctx, debug)?;
if debug {
log!(
true,
"[cf_loop/joinir] Phase 286C-2.1: Scan complete - {} tail calls, {} returns",
scan_plan.tail_calls.len(),
scan_plan.return_conversions.len()
);
}
// ===== STAGE 2: PLAN (Generate rewritten blocks) =====
// TODO Phase 286C-2.1: Extract to plan_rewrites()
@ -1601,19 +1609,16 @@ pub(super) fn merge_and_rewrite(
}
}
// Phase 286C-2.1: Future orchestrator structure (Step 2 onwards):
// Phase 286C-2.1: Step 3 complete - scan_blocks() integrated
// Step 4-5 future work: Extract plan/apply logic into separate functions
//
// // Stage 1: Scan - Read-only analysis
// let plan = scan_blocks(mir_module, remapper, value_to_func_name, &ctx, debug)?;
// Target orchestrator structure:
// 1. let plan = scan_blocks(...)?; // ✅ Done (Step 3)
// 2. let blocks = plan_rewrites(plan, &mut ctx)?; // TODO (Step 4)
// 3. apply_rewrites(builder, blocks)?; // TODO (Step 5)
//
// // Stage 2: Plan - Generate rewritten blocks
// let blocks = plan_rewrites(plan, &mut ctx)?;
//
// // Stage 3: Apply - Mutate builder
// apply_rewrites(builder, blocks)?;
//
// // Build final merge result
// Ok(MergeResult { ... })
// Current state: Main loop (lines 380-1469) contains both plan and apply logic.
// This needs to be separated into 2 stages for clean architecture.
Ok(MergeResult {
exit_block_id: ctx.exit_block_id,