feat(phase131): LLVM backend re-enable & PHI ordering bug discovery

Phase 131: JoinIR → LLVM individual fixes (minimal scope)

## Modifications

### 1. LLVM Backend Re-enable 
- Built with --features llvm to enable real LLVM execution
- Verified Python/llvmlite environment (llvmlite 0.45.1)
- Fixed llvmlite deprecated API: removed llvm.initialize() call
- Successfully moved from Mock backend to real LLVM harness execution

### 2. PHI Instruction Ordering Bug Discovery ⚠️
- Discovered critical bug: PHI nodes placed AFTER terminator instructions
- LLVM IR constraint: PHI must be at block start, before any non-PHI
- Root cause: finalize_phis() in llvm_builder.py emits PHIs after block termination
- Affects all 6 tests with control flow merges

### 3. ConsoleBox LLVM Integration ⚠️
- Confirmed ConsoleBox not registered in Rust VM environment
- Deferred to Phase 132 (prerequisite issue)

## Results

Modified files:
- src/llvm_py/llvm_builder.py: Removed deprecated llvm.initialize()
- docs/development/current/main/phase130_joinir_llvm_baseline.md: Added Phase 131 results
- CURRENT_TASK.md: Added Phase 131 completion report

Test results:
-  peek_expr_block.hako: LLVM execution success (Result: 1)
-  6/7 tests: PHI ordering bug (requires Phase 132 refactoring)

## Success Criteria

-  LLVM backend minimal re-enable (1/7 test passing)
- ⚠️ PHI ordering bug discovered and documented
- ⚠️ ConsoleBox integration deferred to Phase 132

## Phase 132 Handoff

Priority 1: Fix PHI instruction ordering bug (finalize_phis() refactoring)
Priority 2: Resolve ConsoleBox registration issue
Priority 3: Enable remaining test cases for LLVM

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
nyash-codex
2025-12-04 11:04:29 +09:00
parent 67bb7e2834
commit d7b8f79a75
3 changed files with 212 additions and 20 deletions

View File

@ -389,3 +389,101 @@ invalid operation: Unknown Box type: ConsoleBox. Available: Main
2. ConsoleBoxのRust VM登録問題が再発
3. JoinIR → MIR変換は全て正常動作
4. Phase 131での優先課題が明確化
---
## Phase 131 修正内容2025-12-04実施
### 修正1: LLVM Backend Re-enable ✅
**実施内容**:
1. `cargo build --release --features llvm` でLLVM機能有効化ビルド実行
2. Python/llvmlite環境確認llvmlite 0.45.1インストール済み)
3. llvmlite非推奨API対応: `llvm.initialize()` 削除(自動初期化に移行)
**修正ファイル**:
- `src/llvm_py/llvm_builder.py`: `llvm.initialize()` 呼び出しをコメントアウト
**結果**:
- ✅ `peek_expr_block.hako`: LLVM実行成功Result: 1、Rust VM: RC: 1
- ✅ Mock backendから実LLVM実行への移行成功
- ✅ LLVM harness経路が正常動作
### 修正2: PHI命令順序バグ発見 ⚠️
**検出された問題**:
LLVM IR生成時、PHI命令がreturn命令の**後**に配置されるバグを発見。
**問題例**生成されたLLVM IR:
```llvm
bb5:
ret i64 %"ret_phi_16"
%"ret_phi_16" = phi i64 [0, %"bb3"], [0, %"bb4"] ; ← エラーPHIはretの前に必要
}
```
**LLVM IRの制約**:
- PHI命令はBasic Blockの**先頭**に配置必須
- terminator命令ret/br/switch等の後に命令を配置不可
**影響範囲**:
- ❌ phase123_simple_if.hako: LLVM IR parsing error
- ❌ loop_min_while.hako: LLVM IR parsing error
- ❌ 制御フロー合流を含む全テストが影響
**根本原因**:
- `src/llvm_py/llvm_builder.py`の`finalize_phis()`関数
- PHI nodesがblock終端処理後に追加されている
- LLVM IRbuilderのblock構築順序の設計問題
**Phase 132への引き継ぎ**:
この問題は`finalize_phis()`の大規模リファクタリングが必要100行以上の関数
Phase 131の最小スコープを超えるため、Phase 132で対応。
### 修正3: ConsoleBox LLVM統合 ⚠️
**現状確認**:
- ❌ Rust VM環境でもConsoleBox未登録`apps/tests/esc_dirname_smoke.hako`実行不可)
- ❌ LLVM環境でもConsoleBox未対応
**Phase 132への引き継ぎ**:
ConsoleBoxの登録・実装はRust VM側の問題。LLVM統合の前提条件未達のため、Phase 132で対応。
### Phase 131 実行結果サマリー
**修正前Phase 130**:
| 経路 | PASS | PARTIAL | FAIL | 成功パス |
|--------------|------|---------|------|---------|
| Rust VM | 6 | 0 | 1 | 6/7 |
| LLVM harness | 0 | 7 (Mock)| 0 | 0/7 |
**修正後Phase 131**:
| 経路 | PASS | PARTIAL | FAIL | 成功パス | メモ |
|--------------|------|---------|------|---------|------|
| Rust VM | 6 | 0 | 1 | 6/7 | 変更なし |
| LLVM harness | 1 | 0 | 6 | 1/7 | peek_expr_block.hako成功 |
**成功ケース詳細**:
- ✅ `peek_expr_block.hako`: Rust VM ✅ → LLVM ✅Result: 1
**失敗ケース詳細**:
- ❌ `loop_min_while.hako`: PHI ordering bug
- ❌ `phase123_simple_if.hako`: PHI ordering bug
- ❌ `phase123_while_loop.hako`: PHI ordering bug
- ❌ `joinir_if_select_simple.hako`: PHI ordering bug
- ❌ `joinir_min_loop.hako`: PHI ordering bug
- ❌ `esc_dirname_smoke.hako`: ConsoleBox未登録
### Phase 131 完了判定
**達成内容**:
1. ✅ LLVM backend最小re-enable成功peek_expr_block.hako ✅)
2. ⚠️ PHI ordering bug発見・記録Phase 132対応
3. ⚠️ ConsoleBox問題確認Phase 132対応
**最小成功パス確立**: ✅ 1/7テストで成功目標2-3本だが、根本的なPHI問題により1本に制限
**Phase 132への優先課題**:
1. **最優先**: PHI命令順序バグ修正`finalize_phis()`リファクタリング)
2. **優先**: ConsoleBox登録問題解決
3. **通常**: 残りテストケースのLLVM対応