Commit Graph

14 Commits

Author SHA1 Message Date
440f8646b1 feat(joinir): Phase 183 LoopBodyLocal role separation + test fixes
Phase 183 Implementation:
- Added is_var_used_in_condition() helper for AST variable detection
- Implemented LoopBodyLocal filtering in TrimLoopLowerer
- Created 4 test files for P1/P2 patterns
- Added 5 unit tests for variable detection

Test Fixes:
- Fixed test_is_outer_scope_variable_pinned (BasicBlockId import)
- Fixed test_pattern2_accepts_loop_param_only (literal node usage)

Refactoring:
- Unified pattern detection documentation
- Consolidated CarrierInfo initialization
- Documented LoopScopeShape construction paths

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-08 23:43:26 +09:00
b028aa791e feat(phase72): HAKO_JOINIR_IF_SELECT SSOT consolidation - Test helpers
Phase 72-B: Created SSOT helper functions for HAKO_JOINIR_IF_SELECT ENV control.

Changes:
- Added set_if_select_on() and set_if_select_off() helpers to joinir_env.rs
- Replaced 13 direct std::env calls with helper functions in src/tests/mir_joinir_if_select.rs
  - std::env::set_var("HAKO_JOINIR_IF_SELECT", "1") → joinir_env::set_if_select_on()
  - std::env::remove_var("HAKO_JOINIR_IF_SELECT") → joinir_env::set_if_select_off()

Benefits:
- Centralized IfSelect mode control at single point (joinir_env.rs)
- Consistent naming convention (set_X_on/off pattern)
- Easier to extend with validation or trace logging if needed

Tests verified: cargo check --tests passed with no errors

Completed Phase 72-A+B progress:
- Phase 72-A: NYASH_JOINIR_EXPERIMENT (15 occurrences) 
- Phase 72-B: HAKO_JOINIR_IF_SELECT (13 occurrences) 
- Phase 72-C: Dev/experimental flags (pending)

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-02 12:42:22 +09:00
8633224061 JoinIR/SSA/Stage-3: sync CURRENT_TASK and dev env 2025-12-01 11:10:46 +09:00
a3d5bacc55 Phase 30.1 & 73: Stage-3 features env and JoinIR flag cleanup 2025-11-30 14:30:28 +09:00
7de192aa6b feat(mir): Phase 69-3 Fix MIR non-determinism with BTreeSet
Replace HashSet with BTreeSet for CFG predecessors/successors:
- BasicBlock.predecessors: HashSet → BTreeSet
- BasicBlock.successors: HashSet → BTreeSet
- LoopFormOps.get_block_predecessors(): returns BTreeSet
- BasicBlock.dominates(): takes &[BTreeSet<BasicBlockId>]

This ensures deterministic PHI generation and test stability.

Test results:
- loop_with_continue_and_break tests: now deterministic (3/3 same output)
- loopform tests: 14/14 PASS (no regressions)
- merge_exit_with_classification tests: 3/3 PASS

Technical changes (6 files):
- basic_block.rs: BTreeSet types + new() initialization
- loopform_builder.rs: trait signature + 2 mock implementations
- phi_ops.rs: return type
- json_v0_bridge/loop_.rs: return type

Same pattern as Phase 25.1 (MirFunction.blocks HashMap → BTreeMap).

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-30 09:38:28 +09:00
e669f124d2 feat(joinir): Phase 64-1/2 P2型ヒント実装 & 削減条件準備
Phase 64-1: P2/P3 対象関数分類完了
- P2 対象リスト作成: read_quoted_from, IfMerge Simple/Multiple
- P3 将来拡張: MethodCall戻り値型, Box コンストラクタ
- 実装戦略確立: P2から1関数ずつ段階的拡大

Phase 64-2: P2 型ヒント供給実装完了
- read_quoted.rs: ループカウンタ i (Integer), 文字列 ch (String) 型確定
- if_merge.rs: infer_type_from_mir_pattern() 追加 (Const命令から型推論)
- A/B テスト追加: test_p2_if_merge_type_hint() で型ヒント伝播検証 

技術的成果:
- JoinIR MergePair の type_hint 自動推論システム完成
- Phase 63 P1実装パターンを P2 に拡大適用
- 次ステップ: Phase 64-3 lifecycle.rs で P2 hint経路統合

修正ファイル:
- phase-63-joinir-type-info/README.md: Phase 64-1/2 セクション追加
- read_quoted.rs: MergePair 型ヒント追加 (Integer/String)
- if_merge.rs: infer_type_from_mir_pattern() + 型ヒント推論
- mir_joinir_if_select.rs: test_p2_if_merge_type_hint() 追加

🎯 削除条件 4/5 維持 (P1完了), Phase 64-3で P2拡大へ

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-30 05:12:59 +09:00
dc70d0de1b feat(mir): Phase 63-6-3/4/5 P1 type hint完全実装 & 削除条件4/5達成
Phase 63-6-3: lifecycle.rs で型ヒント取得・使用
- `get_phi_type_hint()` ヘルパー関数追加(lifecycle.rs:44-60)
- P1 ケース(IfSelectTest.*)限定で PHI の type_hint を取得
- lifecycle.rs:313-316, 335-338 で型ヒント使用
- 関数名フィルタでガード、他は None(既存挙動維持)

Phase 63-6-4: P1 ケーステスト追加(A/B 検証)
- `test_p1_ab_type_inference()` 追加(mir_joinir_if_select.rs:684-721)
- Route B(JoinIR 型ヒント経由)の動作確認
- Select type_hint = Some(Integer) 検証
- P1 function name filter 検証

Phase 63-6-5: ドキュメント更新(削除条件 4/5 達成)
- Phase 63 README.md 更新:削除条件 4/5 を  完了に
- 達成率 3/5(60%)→ 4/5(80%)に更新
- Phase 63-6 完了セクション追加(実装内容・成果・ファイル一覧)
- CURRENT_TASK.md に Phase 63-6 完了記録追加

削減実績: 0行(段階的拡大のため削除なし)

**削除条件達成率: 4/5(80%)← Phase 63-6 完了で +20%**

技術的成果:
- **P1 ケースで JoinIR 型ヒントのみで型決定(削除条件 4/5 達成)**
- JoinIR が If 系 PHI の型情報 SSOT として機能確立
- lifecycle.rs が型ヒント優先で推論する基盤完成
- Select → PHI → lifecycle.rs の全経路が動作

Modified files:
- src/mir/builder/lifecycle.rs: get_phi_type_hint() 追加、P1 型ヒント使用
- src/tests/mir_joinir_if_select.rs: A/B テスト追加
- CURRENT_TASK.md: Phase 63-6 完了記録
- docs/private/roadmap2/phases/phase-63-joinir-type-info/README.md: 削除条件更新

Test results:
-  test_p1_ab_type_inference: PASS
-  test_if_select_pattern_matching: PASS
-  All if_select tests: 8/8 PASS

次のステップ: Phase 64 で P2/P3 ケースへ拡大、全関数で型ヒント化完了(削除条件 5/5)

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-30 04:45:11 +09:00
8f736358c4 feat(joinir): Phase 63-4 infer_type_from_phi degradation design
Phase 63-4: infer_type_from_phi を『JoinIR 型ヒント優先+従来ロジックフォールバック』に縮退する仕様を設計(実装は Phase 63-5+)

## Changes

### Documentation Updates
- **README.md**: Added complete Phase 63-4 design (63-4.1 through 63-4.5)
  - 63-4.1: Current state analysis (definition location, callsites, role, JoinIR preparation)
  - 63-4.2: Degradation spec (type_hint priority + fallback pattern)
  - 63-4.3: Representative cases and A/B testing strategy (P1/P2/P3)
  - 63-4.4: Deletion conditions (5 conditions, current: 2/5 = 40%)
  - 63-4.5: Phase 63-5 handoff (infer_type_from_phi_with_hint() implementation tasks)

- **PHI_BOX_INVENTORY.md**: Updated if_phi.rs entry with Phase 63-4 deletion plan
  - Added: "Phase 63-4完了: infer_type_from_phi の JoinIR type_hint 優先への縮退案を設計(実装は Phase 63-5+)"

- **CURRENT_TASK.md**: Added Phase 63-4 section with summary of design work

## Design Highlights

### Degradation Pattern
```rust
pub fn infer_type_from_phi_with_hint(
    function: &MirFunction,
    ret_val: ValueId,
    types: &BTreeMap<ValueId, MirType>,
    type_hint: Option<MirType>,
) -> Option<MirType> {
    if let Some(hint) = type_hint {
        return Some(hint);  // Route B: JoinIR priority (SSOT)
    }
    infer_type_from_phi(function, ret_val, types)  // Route A: Fallback
}
```

### Representative Cases
- **P1**: IfSelectTest.simple/local (Phase 63-5 target)
- **P2**: read_quoted_from (Phase 63-6+ target)
- **P3**: MethodCall/Box constructors (Phase 64+ expansion)

### Deletion Conditions (2/5 achieved)
1.  JoinIR has type_hint field (Phase 63-3)
2.  Type hints populated for representative cases (Phase 63-2)
3.  Degraded to type_hint priority (Phase 63-5)
4.  P1 cases determined by type_hint only (Phase 63-5)
5.  All functions use type hints (Phase 64+)

## Files Changed
- docs/private/roadmap2/phases/phase-63-joinir-type-info/README.md
- docs/private/roadmap2/phases/phase-30-final-joinir-world/PHI_BOX_INVENTORY.md
- CURRENT_TASK.md

## Next Steps
Phase 63-5: Implement degradation for P1 cases (IfSelectTest.simple/local)

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-29 17:58:06 +09:00
3ea397fd3e feat(joinir): Phase 61-1 If-in-loop JoinIR化インフラ整備完了
## 実装内容

### 新規ファイル
- `if_phi_context.rs`: If-in-loop用PHIコンテキスト構造体 (135行)
  - `IfPhiContext::for_loop_body()`: ループ内if用コンストラクタ
  - `is_carrier()`: ループキャリア変数判定
  - 単体テスト2個完全動作

### 既存ファイル拡張
- `if_select.rs`, `if_merge.rs`: context パラメータ追加 (+68行)
  - `with_context()` コンストラクタ実装
  - Pure If との完全互換性維持
- `mod.rs`: `try_lower_if_to_joinir()` シグネチャ拡張 (+25行)
  - `context: Option<&IfPhiContext>` パラメータ追加
  - 既存呼び出し箇所6箇所修正完了
- `loop_builder.rs`: JoinIR経路実装 (+43行)
  - `NYASH_JOINIR_IF_SELECT=1` で試行
  - フォールバック設計(PhiBuilderBox経路保持)
  - デバッグログ完備

## テスト結果
-  loopform テスト 14/14 PASS(退行なし)
-  ビルド成功(エラー0件)
-  Borrow Checker 問題解決

## コード変更量
- 新規: +135行
- 拡張: +136行
- 削除: -18行
- 純増: +253行(インフラ投資、Phase 61-3で-226行削減予定)

## 次のステップ
Phase 61-2: join_inst dry-run実装で実際のPHI生成を行う

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-29 11:53:57 +09:00
447bbec998 refactor(joinir): Split ast_lowerer and join_ir_vm_bridge into modules
ast_lowerer.rs → ast_lowerer/ (10 files):
- mod.rs: public surface + entry dispatch
- context.rs: ExtractCtx helpers
- expr.rs: expression-to-JoinIR extraction
- if_return.rs: simple if→Select lowering
- loop_patterns.rs: loop variants (simple/break/continue)
- read_quoted.rs: read_quoted_from lowering (Phase 45-46)
- nested_if.rs: NestedIfMerge lowering
- analysis.rs: loop if-var analysis + metadata helpers
- tests.rs: frontend lowering tests
- README.md: module documentation

join_ir_vm_bridge.rs → join_ir_vm_bridge/ (5 files):
- mod.rs: public surface + shared helpers
- convert.rs: JoinIR→MIR lowering
- runner.rs: VM execution entry (run_joinir_via_vm)
- meta.rs: experimental metadata-aware hooks
- tests.rs: bridge-specific unit tests
- README.md: module documentation

Benefits:
- Clear separation of concerns per pattern
- Easier navigation and maintenance
- Each file has single responsibility
- README documents module boundaries

Co-authored-by: ChatGPT <noreply@openai.com>

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-28 17:42:19 +09:00
5b7818f5c9 feat(joinir): Phase 33-7 IfMerge lowering for multiple-variable PHI
Implements IfMerge instruction lowering to support multiple variables
merging from if/else branches (Phase 33-7: return pattern only).

## Changes

- Add src/mir/join_ir/lowering/if_merge.rs (232 lines)
  - IfMergeLowerer with pattern matching for common variables
  - extract_written_vars() / find_written_value() helpers
  - Phase 33-7 constraint: return pattern only (k_next=None)

- Update src/mir/join_ir/lowering/mod.rs
  - Unified entry point: try_lower_if_to_joinir()
  - Priority: IfMerge → Select → if_phi fallback
  - Add IfMergeTest.* to whitelist

- Add unit tests in src/tests/mir_joinir_if_select.rs
  - test_if_merge_simple_pattern (2 variables)
  - test_if_merge_multiple_pattern (3 variables)
  - All 7/7 tests PASS 

- Add reference test cases
  - apps/tests/joinir_if_merge_simple.hako (2-var pattern)
  - apps/tests/joinir_if_merge_multiple.hako (3-var pattern)

## Test Results

 Simple pattern (2 vars): merges=2, k_next=None
 Multiple pattern (3 vars): merges=3, k_next=None
 test result: ok. 7 passed; 0 failed

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-27 08:18:09 +09:00
bb4e3044a8 test(joinir): fix Phase 33-3.2 test flakiness by unifying env-dependent tests
Phase 33-3.2 test stabilization:
- Unified 4 env-dependent tests into 1 (test_if_select_pattern_matching)
- Eliminated race condition from parallel test execution
- All 5 tests now pass consistently (5/5 stable PASS)

Before: 8 tests (flaky 7-8/8 due to env var race)
After: 5 tests (stable 5/5, no flakiness)

Test coverage maintained:
- Simple pattern lowering (env ON)
- Local pattern lowering (env ON)
- Disabled by default (env OFF)
- Wrong function name filter (env ON)
- 4 verify tests (env-independent, kept separate)

Documentation:
- Updated TASKS.md with test flake resolution notes
- Updated docs/private submodule to 81ec67f

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-27 04:27:57 +09:00
4ba3fcd615 Phase 33-3.2: phi_invariants/conservative の JoinIR 側への移譲
実装内容:
- verify_select_minimal() 実装(Select 命令の最小 invariant チェック)
- phi_invariants.rs / conservative.rs のエッセンス抽出・移譲
- 4 テスト追加(simple/local with verify, reject multiple selects, check invariants)

移譲した責務:
- phi_invariants.rs::ensure_if_values_exist() → 型一貫性・完全性チェック
- conservative.rs::ConservativeMerge → 単一 PHI チェック

テスト結果:
-  8/8 tests PASS
-  simple/local パターン: Verifier PASS
-  不正パターン(複数 Select): Verifier REJECT
-  invariant ログ: conservative.rs / phi_invariants.rs からの移譲を明記

制限事項:
- IfSelectTest.* のみ対象、本線 if_phi は保持
- 削除は Stage-1/Stage-B/selfhost への適用後に行う

Phase 33-3.2 完了(2025-11-27)
2025-11-27 03:55:45 +09:00
5cfb0e1d5b Phase 33-3: If/PHI MIR pattern matching + Select lowering (minimal patterns)
Implementation:
- Implement MIR pattern matching in if_select.rs (simple/local patterns)
- Add try_lower_if_to_joinir() entry point in lowering/mod.rs
- Create comprehensive integration tests (4/4 passing)

Pattern Support:
- Simple pattern: if cond { return 1 } else { return 2 }
  - Both blocks must have Return only (no instructions)
- Local pattern: if cond { x = a } else { x = b }; return x
  - Each branch has exactly 1 Copy instruction
  - Both branches jump to same merge block
  - Merge block Returns the assigned variable

Safety Mechanisms:
- Dev toggle: NYASH_JOINIR_IF_SELECT=1 required
- Function name filter: Only IfSelectTest.* functions
- Fallback: Returns None on pattern mismatch
- Zero breaking changes: Existing if_phi path untouched

Tests (4/4 PASS):
- test_if_select_simple_pattern
- test_if_select_local_pattern
- test_if_select_disabled_by_default
- test_if_select_wrong_function_name

Files:
- Modified: src/mir/join_ir/lowering/if_select.rs (pattern matching)
- Modified: src/mir/join_ir/lowering/mod.rs (entry point)
- New: src/tests/mir_joinir_if_select.rs (integration tests)
- Modified: src/tests/mod.rs (module registration)

Phase 33-3.2/3.3 (legacy removal) pending

🤖 Generated with Claude Code
Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-27 03:28:32 +09:00