feat(joinir): Phase 188.3 - Pattern6 (NestedLoopMinimal) 選択ロジック実装

## Phase 188.3 進捗: Phase 2 完了 (6/13 tasks)

### 実装完了 

**Phase 1: Fixture作成**
- apps/tests/phase1883_nested_minimal.hako 追加
  - Add/Compare のみ(乗算なし)
  - 期待 exit code: 9 (3×3 nested loops)
- 既存 lowering で fallback 動作確認

**Phase 2: 選択ロジック (SSOT)**
- LoopPatternContext に step_tree_max_loop_depth フィールド追加
- choose_pattern_kind() に Pattern6 選択ロジック実装:
  1. Cheap check (has_inner_loop)
  2. StepTree 構築 (max_loop_depth 取得)
  3. AST validation (is_pattern6_lowerable)
- pattern6_nested_minimal.rs モジュール作成 (stub)
- LOOP_PATTERNS に Pattern6 entry 追加
- **検証**: Pattern6 が正しく選択される 

### 設計原則 (確認済み)

1. **Fail-Fast**: Pattern6 選択後は Ok(None) で逃げない
2. **outer 変数 write-back 検出 → validation false** (Phase 188.4+)
3. **最小実装**: inner local だけ、Pattern1 モデル二重化
4. **cfg! 依存なし**: production で動作

### 検証結果

```
[choose_pattern_kind] has_inner_loop=true
[choose_pattern_kind] max_loop_depth=2
[choose_pattern_kind] is_pattern6_lowerable=true
 Pattern6 SELECTED!
```

Stub からの期待エラー:
```
[ERROR]  [Pattern6] Nested loop lowering not yet implemented
```

### 次: Phase 3 (Lowering 実装 - 推定4時間)

残りタスク:
- Phase 3-1: AST 抽出ヘルパー
- Phase 3-2: Validation ヘルパー
- Phase 3-3: Continuation 生成 (outer_step, inner_step, k_inner_exit)
- Phase 3-4: fixture が exit=9 を返すことを検証

### 変更ファイル

**新規**:
- apps/tests/phase1883_nested_minimal.hako
- src/mir/builder/control_flow/joinir/patterns/pattern6_nested_minimal.rs
- docs/development/current/main/phases/phase-188.{1,2,3}/README.md

**変更**:
- src/mir/builder/control_flow/joinir/routing.rs (Pattern6 選択)
- src/mir/builder/control_flow/joinir/patterns/router.rs (Context 拡張)
- src/mir/builder/control_flow/joinir/patterns/mod.rs (module 宣言)

🎯 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-27 05:45:12 +09:00
parent 229553f7a4
commit a2c5fd90fe
32 changed files with 1780 additions and 42 deletions

View File

@ -0,0 +1,133 @@
# Phase 188.2: StepTree nesting depth SSOT (Option A) — strict Fail-Fast
**Date**: 2025-12-27
**Status**: ✅ Option A implemented / ❌ Pattern 6 lowering deferred (Phase 188.3+)
**Goal**: `StepTreeFeatures.max_loop_depth` を SSOT として、strict mode で depth > 2 を明示エラーにする
---
## ✅ Decision: Option A Adopted (2025-12-27)
**Chosen Approach**: StepTreeFeatures Integration
**What Phase 188.2 Implements**:
- Use `StepTree.features.max_loop_depth` as SSOT for nesting depth
- Add explicit error for `max_loop_depth > 2` in strict mode (capability_guard.rs)
- Update NestedLoop capability hint to reflect current support
**Implementation Location**:
- `src/mir/builder/control_flow/joinir/control_tree_capability_guard.rs` (lines 32-76)
**Status**: Option A depth checking implemented, Option B deferred
---
## What Phase 188.2 delivered
- ✅ strict mode の depth 制約を SSOT 化(`max_loop_depth > 2` は明示エラー)
- ✅ NestedLoop capability hint を現実に合わせて更新1-levelは許可、2+ level は depth_exceeded
- ✅ unit tests 更新depth=2 PASS / depth=3 FAIL
- ✅ quick/integration は不変(回帰なし)
### Verification (reference)
- quick: `./tools/smokes/v2/run.sh --profile quick`
- integration selfhost: `./tools/smokes/v2/run.sh --profile integration --filter "selfhost_"`
---
## Background
Phase 188.1 delivered infrastructure (capability allowlist, enum, stub module), but **detection and lowering are NOT implemented** due to architectural limitations.
**See**: `docs/development/current/main/phases/phase-188.1/README.md` § Implementation Reality
---
## Design Decision Required (Choose A or B)
### Option A: StepTreeFeatures Integration
**Approach**:
- Pass `StepTreeFeatures` from StepTree to LoopForm creation
- Store `max_loop_depth` in LoopForm (add field) or pass via context
- Use AST-level nesting depth for Pattern 6 detection
**Pros**:
- Nesting info already exists in StepTree
- No new detection logic required
- Fast implementation
**Cons**:
- AST-level info may diverge from MIR structure (after optimizations)
- Adding fields to LoopForm breaks separation of concerns
**Estimated Effort**: 1-2 days
---
### Option B: LoopRegion Integration
**Approach**:
- Implement LoopRegion builder (instantiate parent/child structure)
- Build LoopRegion tree from MIR control flow graph
- Compute nesting depth from LoopRegion.parent traversal
- Replace LoopForm with LoopRegion in lowering pipeline
**Pros**:
- MIR-level truth (accurate after optimizations)
- Leverages Phase 32 infrastructure
- Clean separation (LoopRegion is designed for nesting)
**Cons**:
- Requires implementing LoopRegion builder (non-trivial)
- Larger architectural change
- May affect other loop analysis code
**Estimated Effort**: 1-2 weeks
---
## Planning Session Agenda
**Step 1: Docs-First Design Review**
- Review Option A vs B trade-offs
- Consider impact on selfhost_minimal requirements
- Decide on approach based on scope/timeline
**Step 2: Detailed Implementation Plan**
- Write step-by-step implementation guide
- Identify critical files
- Estimate effort per task
**Step 3: Implementation**
- Execute chosen approach
- Test with selfhost_minimal
- Verify 154/154 PASS maintained
---
## Out of Scope (Phase 188.2)
Still deferred to Phase 188.3+:
- Break/continue in nested loops (Pattern 2/4 combinations)
- Multiple inner loops (siblings)
- 2+ level nesting (depth > 2) の loweringPhase 188.2 は strict Fail-Fast のみ)
**Rationale**: Phase 188.2 focuses on 1-level nesting detection only (minimal scope).
---
## References
- **Phase 188.1 Reality**: `docs/development/current/main/phases/phase-188.1/README.md` § Implementation Reality
- **LoopRegion Definition**: `src/mir/control_form.rs` (lines 40-62)
- **StepTreeFeatures Definition**: `src/mir/control_tree/step_tree.rs` (lines 17-25)
- **LoopForm/LoopShape Definition**: `src/mir/loop_form.rs`, `src/mir/control_form.rs`
---
## End of Phase 188.2 Planning Stub
**Status**: Option A is complete; remaining work is Phase 188.3+
**Dependencies**: Phase 188.1 “Implementation Reality” remains SSOT