refactor(phase284): P1 SSOT Consolidation - Block Remapper & Return Emitter

## Summary

Refactored Phase 284 P1 codebase to consolidate scattered logic into SSOT
(Single Source of Truth) modules, improving maintainability and enabling
Pattern4/5 code reuse.

## New Modules

### 1. Block Remapper SSOT
**File**: `src/mir/builder/control_flow/joinir/merge/block_remapper.rs` (152 lines)

- **Purpose**: Consolidate block ID remapping logic (Phase 284 P1 fix)
- **Function**: `remap_block_id(block_id, local_block_map, skipped_entry_redirects)`
- **Rule**: local_block_map priority > skipped_entry_redirects (prevents ID collision)
- **Tests**: 4 unit tests (priority cascade, collision handling, etc.)

### 2. Return Jump Emitter
**File**: `src/mir/join_ir/lowering/return_jump_emitter.rs` (354 lines)

- **Purpose**: Reusable return handling helper for Pattern4/5
- **Function**: `emit_return_conditional_jump(loop_step_func, return_info, k_return_id, ...)`
- **Scope**: P1 - unconditional return + conditional (loop_var == N) only
- **Tests**: 3 unit tests (unconditional, no return, conditional)

## Modified Files

**merge/instruction_rewriter.rs** (-15 lines):
- Replaced inline block remapping with `remap_block_id()` call
- Cleaner Branch remap logic

**merge/rewriter/terminator.rs** (-43 lines):
- Delegates remap_jump/remap_branch to block_remapper SSOT
- Simplified duplicate logic

**lowering/loop_with_continue_minimal.rs** (-108 lines):
- Replaced ~100 line return handling with `emit_return_conditional_jump()` call
- Extracted helper functions to return_jump_emitter.rs
- Line reduction: 57% decrease in function complexity

**merge/mod.rs, lowering/mod.rs**:
- Added new module exports (block_remapper, return_jump_emitter)

**phase-284/README.md**:
- Updated completion status (P1 Complete + Refactored)
- Added SSOT consolidation notes
- Documented module architecture

## Code Quality Improvements

| Metric | Before | After | Change |
|--------|--------|-------|--------|
| Duplicate block remap logic | 2 places | SSOT | -15 lines |
| Return handling code | inline (100L) | helper call | -99 lines |
| Testability | Limited | Unit tests (7) | +7 tests |
| Module cohesion | Low (scattered) | High (consolidated) | Better |

## Testing

 Build: Success (cargo build --release)
 Smoke tests: All pass (46 PASS, 1 pre-existing FAIL)
 Regression: Zero
 Unit tests: 7 new tests added

## Future Benefits

1. **Pattern5 Reuse**: Direct use of `emit_return_conditional_jump()` helper
2. **Phase 285 (P2)**: Nested if/loop returns via same infrastructure
3. **Maintainability**: SSOT reduces debugging surface area
4. **Clarity**: Each module has single responsibility

## Architectural Notes

**Block Remapper SSOT Rule**:
```
remap_block_id(id, local_block_map, skipped_entry_redirects):
  1. Check local_block_map (function-local priority)
  2. Fall back to skipped_entry_redirects (global redirects)
  3. Return original if not found
```

Prevents function-local block ID collisions with global remap entries.

**Return Emitter Pattern**:
```
emit_return_conditional_jump(func, return_info, k_return_id, alloc_value):
  - Pattern1-5: All use same infrastructure
  - P1 scope: top-level return only (nested if/loop → P2)
  - Returns: JoinInst::Jump(cont=k_return, cond=Some(return_cond))
```

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

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
2025-12-23 14:37:01 +09:00
parent 661bbe1ab7
commit b0eeb14c54
8 changed files with 555 additions and 145 deletions

View File

@ -1,6 +1,6 @@
# Phase 284: Return as ExitKind SSOTpatternに散らさない
Status: Planned (design-first)
Status: P1 Complete (2025-12-23)
## Goal
@ -75,14 +75,28 @@ Phase 284 の完了条件は「`return` を含むケースが close-but-unsuppor
## Scope
### P0docs-only
### P0docs-only ✅ COMPLETE
- `return` を ExitKind として扱う SSOT を文章で固定する(本ファイル + 参照先リンク)。
- 移行期間のルールOk(None)/Err の境界、黙殺禁止)を Phase 282 と整合させる。
### P1+code
### P1code ✅ COMPLETE (2025-12-23)
- `return` を含む loop body を、JoinIR/Plan のどちらの経路でも **同じ ExitKind::Return** に落とす。
**実装完了内容**:
1. **return_collector.rs** - Return statement detection SSOT (既存)
2. **return_jump_emitter.rs** - Return jump emission helper (Pattern4/5 reuse) ⭐NEW
3. **block_remapper.rs** - Block ID remap SSOT (Phase 284 P1 Fix) ⭐NEW
4. **Loop refactoring** - loop_with_continue_minimal.rs simplified (~100 lines removed)
5. **Instruction/terminator updates** - Use block_remapper SSOT
**コード品質向上**:
- Return handling: ~100 lines inline code → 1 function call
- Block remapping: Duplicate logic → SSOT function
- Future reusability: Pattern5 can now reuse return_jump_emitter
### P2+(未実装)
- `return` を含む loop body を、Plan line でも ExitKind::Return に落とす。
- VM/LLVM の両方で、`return` を含む fixture を smoke 化して SSOT を固定する。
## Acceptance