fix(joinir): Phase 196 Select double-remap bug in instruction_rewriter
Root cause: PHI inputs were being remapped twice in instruction_rewriter.rs - Line 304: remap_instruction() already remapped JoinIR → Host ValueIds - Line 328: remap_value() attempted to remap again → undefined ValueIds Fix: Only remap block IDs, use already-remapped ValueIds as-is Test results: - phase195_sum_count.hako → 93 ✅ (multi-carrier P3) - loop_if_phi.hako → sum=9 ✅ (single-carrier P3) - loop_min_while.hako → 0,1,2 ✅ (Pattern 1) - joinir_min_loop.hako → RC:0 ✅ (Pattern 2) - No [joinir/freeze], no regressions 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@ -523,14 +523,16 @@ JoinIR は Rust 側だけでなく、将来的に .hako selfhost コンパイラ
|
||||
- Deferred loops: _parse_array, _parse_object (multiple MethodCalls)
|
||||
- 詳細: phase194-loop-inventory.md, phase194-jsonparser-deployment.md
|
||||
|
||||
5. **Pattern 3 拡張(複数キャリア対応)** → Phase 195 Lowerer 完了、Phase 196 待ち
|
||||
5. **Pattern 3 拡張(複数キャリア対応)** → Phase 195 + 196 完了 ✅
|
||||
- 目的: P3(If-Else PHI)で 2-3 個の Carrier を同時処理
|
||||
- **Lowerer 側完了**: multi-carrier PHI 生成(sum + count)✅
|
||||
- **ExitLine**: 変更不要(既存インフラ活用)✅
|
||||
- **Blocker**: JoinIR→MIR の Select 変換バグ(Phase 196 で修正予定)
|
||||
- パターン側は終わり、問題は bridge 側
|
||||
- **Phase 195**: Lowerer 側完了(multi-carrier PHI 生成: sum + count)✅
|
||||
- **Phase 196**: Select 二重 remap バグ修正 ✅
|
||||
- 根本原因: `instruction_rewriter.rs` で PHI inputs を二重 remap
|
||||
- 修正: block ID のみ remap、ValueId は既に remap 済み
|
||||
- **E2E 結果**: phase195_sum_count.hako → 93 ✅
|
||||
- 詳細: phase196-select-bug-analysis.md
|
||||
|
||||
6. **JsonParser 残り複雑ループへの適用(Phase 195+, 200+)**
|
||||
6. **JsonParser 残り複雑ループへの適用(Phase 197+, 200+)**
|
||||
- Phase 200+: ConditionEnv 拡張 (function-scoped variables) → _parse_number, _atoi
|
||||
- Phase 195+: Pattern 3 拡張 (multi-flag carriers) → _parse_string, _unescape_string
|
||||
- Phase 195+: MethodCall 拡張 (multiple calls in body) → _parse_array, _parse_object
|
||||
|
||||
@ -385,3 +385,190 @@ Section 7.2 "残タスク" に追記:
|
||||
### 判断基準
|
||||
- Phase 194 の実戦投入で「どこが詰まったか」を見て優先順位決定
|
||||
- 無理に全部対応しない(Fail-Fast で課題を明確化)
|
||||
|
||||
---
|
||||
|
||||
## Implementation Status
|
||||
|
||||
**完了日**: 2025-12-09
|
||||
|
||||
### 実装サマリ
|
||||
|
||||
**JoinIR 対応ループ** (4/10):
|
||||
- ✅ _skip_whitespace (Pattern 2) - Already whitelisted
|
||||
- ✅ _trim (leading) (Pattern 5) - Already whitelisted
|
||||
- ✅ _trim (trailing) (Pattern 5) - Already whitelisted
|
||||
- ✅ _match_literal (Pattern 2) - Already whitelisted
|
||||
|
||||
**保留ループ** (6/10):
|
||||
- ❌ _parse_number - ConditionEnv constraint (`digits.indexOf()`)
|
||||
- ❌ _atoi - ConditionEnv constraint (`digits.indexOf()`)
|
||||
- ❌ _parse_string - Complex carriers (escaped flag + continue)
|
||||
- ❌ _unescape_string - Complex carriers (multiple flags)
|
||||
- ❌ _parse_array - Multiple MethodCalls
|
||||
- ❌ _parse_object - Multiple MethodCalls
|
||||
|
||||
### E2E テスト結果
|
||||
|
||||
**テスト環境**:
|
||||
```bash
|
||||
cargo build --release
|
||||
NYASH_DISABLE_PLUGINS=1 NYASH_JOINIR_CORE=1 ./target/release/hakorune tools/hako_shared/json_parser.hako
|
||||
```
|
||||
|
||||
**結果**: ❌ Compilation Error (Expected - Fail-Fast Strategy)
|
||||
|
||||
**エラー内容**:
|
||||
```
|
||||
[ERROR] ❌ MIR compilation error: [cf_loop/pattern2] Lowering failed: [joinir/pattern2] Unsupported condition: uses loop-body-local variables: ["digit_pos"]. Pattern 2 supports only loop parameters and outer-scope variables. Consider using Pattern 5+ for complex loop conditions.
|
||||
```
|
||||
|
||||
**分析**:
|
||||
- `_parse_number` の `digits.indexOf(ch)` が Phase 193 ConditionEnv 制約に引っかかった
|
||||
- `digits` は外部ローカル変数(function-scoped)だが、ConditionEnv には含まれない
|
||||
- **Fail-Fast 戦略通り**: 無理に直さず、Phase 200+ に保留
|
||||
|
||||
### 退行テスト結果 (✅ All Pass)
|
||||
|
||||
```bash
|
||||
# Phase 190: NumberAccumulation
|
||||
./target/release/hakorune apps/tests/phase190_atoi_impl.hako
|
||||
# Expected: 12
|
||||
# Result: ✅ 12 (RC: 0)
|
||||
|
||||
# Phase 191: Body-local init
|
||||
./target/release/hakorune apps/tests/phase191_body_local_atoi.hako
|
||||
# Expected: 123
|
||||
# Result: ✅ 123 (RC: 0)
|
||||
|
||||
# Phase 193: MethodCall in init
|
||||
./target/release/hakorune apps/tests/phase193_init_method_call.hako
|
||||
# Expected: Compilation success
|
||||
# Result: ✅ RC: 0
|
||||
```
|
||||
|
||||
**結論**: 既存インフラに退行なし ✅
|
||||
|
||||
### 技術的発見
|
||||
|
||||
#### 1. **ConditionEnv 制約の明確化** (Phase 200+ 課題)
|
||||
|
||||
**現状** (Phase 193):
|
||||
- ConditionEnv に含まれる変数:
|
||||
- ✅ Loop parameters (loop variable)
|
||||
- ✅ Condition-only bindings (外部変数のループ前評価)
|
||||
- ✅ Body-local variables (ループ内で定義)
|
||||
- ✅ Carrier variables (ループで更新される変数)
|
||||
|
||||
**含まれない変数**:
|
||||
- ❌ Function-scoped local variables (例: `digits`)
|
||||
|
||||
**影響を受けたループ**:
|
||||
- `_parse_number`: `local digits = "0123456789"` → `digit_pos = digits.indexOf(ch)`
|
||||
- `_atoi`: 同様に `digits.indexOf(ch)` 依存
|
||||
|
||||
**解決策案** (Phase 200+):
|
||||
1. **ConditionEnv 拡張**: Function-scoped variables も ConditionEnv に含める
|
||||
2. **.hako リライト**: `digits.indexOf(ch)` を `(ch >= "0" && ch <= "9")` に置換
|
||||
3. **専用パターン**: `indexOf` 専用の Pattern 実装
|
||||
|
||||
#### 2. **P5 Trim Pattern の実用性確認** (✅ 動作確認済み)
|
||||
|
||||
**発見**: Trim pattern が `_trim` で正常動作
|
||||
- ✅ Leading whitespace trim (Pattern 5)
|
||||
- ✅ Trailing whitespace trim (Pattern 5)
|
||||
- ✅ TrimLoopLowerer が `ch` を `is_ch_match` carrier に昇格
|
||||
- ✅ Whitespace check (`[" ", "\t", "\n", "\r"]`) を JoinIR で生成
|
||||
|
||||
**技術詳細** (trace log):
|
||||
```
|
||||
[TrimLoopLowerer] Trim pattern detected! var='ch', literals=["\r", "\n", "\t", " "]
|
||||
[TrimLoopLowerer] LoopBodyLocal 'ch' promoted to carrier 'is_ch_match'
|
||||
[TrimLoopLowerer] Added carrier 'is_ch_match' to ConditionEnv
|
||||
```
|
||||
|
||||
#### 3. **Structure-Only Routing の有効性** (Phase 196 default)
|
||||
|
||||
**現状** (routing.rs Line 45-51):
|
||||
```rust
|
||||
let structure_only = match std::env::var("NYASH_JOINIR_STRUCTURE_ONLY") {
|
||||
Some("0") | Some("off") => false,
|
||||
_ => true, // ← Default: ON
|
||||
};
|
||||
```
|
||||
|
||||
**利点**:
|
||||
- ✅ Whitelist 不要 (関数名ベースの制約なし)
|
||||
- ✅ Pattern-based routing のみで判定
|
||||
- ✅ 段階的拡張が容易
|
||||
|
||||
**懸念**:
|
||||
- ⚠️ サポート外ループで compilation error (Fail-Fast)
|
||||
- ⚠️ ユーザーには `NYASH_JOINIR_STRUCTURE_ONLY=0` で回避可能
|
||||
|
||||
**結論**: 現状の Structure-only routing で Phase 194 の目的は達成可能
|
||||
|
||||
### 次のステップ
|
||||
|
||||
#### 優先度 High: Phase 200+ ConditionEnv 拡張
|
||||
|
||||
**目標**: `digits.indexOf()` 対応
|
||||
|
||||
**設計課題**:
|
||||
1. Function-scoped variables をどこまで ConditionEnv に含めるか
|
||||
2. スコープ解析の複雑化リスク
|
||||
3. `.hako` リライトで回避可能か検証
|
||||
|
||||
**影響ループ**: 2/10 (20%) - `_parse_number`, `_atoi`
|
||||
|
||||
#### 優先度 Medium: Phase 195 Pattern 3 拡張
|
||||
|
||||
**目標**: Complex carriers (多段フラグ) 対応
|
||||
|
||||
**設計課題**:
|
||||
1. `is_escape`, `has_next`, `process_escape` のような多段フラグ
|
||||
2. If-in-loop + continue の組み合わせ
|
||||
|
||||
**影響ループ**: 2/10 (20%) - `_parse_string`, `_unescape_string`
|
||||
|
||||
#### 優先度 Low: Phase 195+ MethodCall 拡張
|
||||
|
||||
**目標**: Multiple MethodCalls in loop body
|
||||
|
||||
**設計課題**:
|
||||
1. Phase 193 は init のみ対応、body は未対応
|
||||
2. ネストした MethodCall の扱い
|
||||
|
||||
**影響ループ**: 2/10 (20%) - `_parse_array`, `_parse_object`
|
||||
|
||||
### 推奨ロードマップ
|
||||
|
||||
**Phase 194 完了判定**: ✅ 検証完了(Fail-Fast 戦略成功)
|
||||
|
||||
**Phase 195**: Pattern 3 extension (if-in-loop + multi-flag carriers)
|
||||
- Target: `_parse_string` (代表例)
|
||||
- Defer: `_unescape_string` (複雑すぎるため Pattern 3 安定後)
|
||||
|
||||
**Phase 200+**: ConditionEnv expansion (function-scoped locals)
|
||||
- Target: `_parse_number`, `_atoi`
|
||||
- Design: Function-scoped variable capture strategy
|
||||
|
||||
**Phase 201+**: MethodCall extension (multiple calls in body)
|
||||
- Target: `_parse_array`, `_parse_object`
|
||||
- Design: MethodCall orchestration in loop body
|
||||
|
||||
### まとめ
|
||||
|
||||
**Phase 194 の成果**:
|
||||
1. ✅ Loop inventory 完成 (4 target, 6 deferred)
|
||||
2. ✅ Routing infrastructure 確認 (structure-only mode 動作)
|
||||
3. ✅ 退行テスト全て pass (Phase 190-193)
|
||||
4. ✅ Fail-Fast 戦略実証 (`digits.indexOf` 制約明確化)
|
||||
5. ✅ P5 Trim pattern 実戦検証 (_trim で動作確認)
|
||||
|
||||
**Phase 194 の課題**:
|
||||
1. ConditionEnv 制約 (function-scoped variables 未対応)
|
||||
2. Complex carriers 未対応 (多段フラグ)
|
||||
3. Multiple MethodCalls 未対応
|
||||
|
||||
**全体評価**: Phase 194 は「検証フェーズ」として大成功。次の Phase への明確な道筋を示した。
|
||||
|
||||
321
docs/development/current/main/phase194-loop-inventory.md
Normal file
321
docs/development/current/main/phase194-loop-inventory.md
Normal file
@ -0,0 +1,321 @@
|
||||
# Phase 194: JsonParser Loop Inventory
|
||||
|
||||
**Date**: 2025-12-09
|
||||
**File**: `tools/hako_shared/json_parser.hako`
|
||||
**Total Loops**: 10
|
||||
|
||||
---
|
||||
|
||||
## JoinIR Target Loops (Phase 194)
|
||||
|
||||
Loops that CAN be handled by existing P1/P2/P5 patterns.
|
||||
|
||||
| Loop Name | Pattern | Line | Carrier | Update | Status |
|
||||
|-----------|---------|------|---------|--------|--------|
|
||||
| `_skip_whitespace` | P2 | 312-319 | p | p+1 | ✅ Whitelisted |
|
||||
| `_trim` (leading) | P5 | 330-337 | start | start+1 | ✅ Whitelisted |
|
||||
| `_trim` (trailing) | P5 | 340-347 | end | end-1 | ✅ Whitelisted |
|
||||
| `_match_literal` | P2 | 357-362 | i | i+1 | ✅ Whitelisted |
|
||||
|
||||
### Pattern Details
|
||||
|
||||
#### `_skip_whitespace` (Line 312-319)
|
||||
```nyash
|
||||
loop(p < s.length()) {
|
||||
local ch = s.substring(p, p+1)
|
||||
if ch == " " || ch == "\t" || ch == "\n" || ch == "\r" {
|
||||
p = p + 1
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
```
|
||||
- **Pattern**: P2 (break condition)
|
||||
- **Carrier**: p (IntegerBox)
|
||||
- **Update**: p = p + 1
|
||||
- **Routing**: Already whitelisted as `JsonParserBox._skip_whitespace/2`
|
||||
|
||||
#### `_trim` Leading (Line 330-337)
|
||||
```nyash
|
||||
loop(start < end) {
|
||||
local ch = s.substring(start, start+1)
|
||||
if ch == " " || ch == "\t" || ch == "\n" || ch == "\r" {
|
||||
start = start + 1
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
```
|
||||
- **Pattern**: P5 (Trim specialized)
|
||||
- **Carrier**: start (IntegerBox)
|
||||
- **Update**: start = start + 1
|
||||
- **Routing**: Already whitelisted as `JsonParserBox._trim/1`
|
||||
|
||||
#### `_trim` Trailing (Line 340-347)
|
||||
```nyash
|
||||
loop(end > start) {
|
||||
local ch = s.substring(end-1, end)
|
||||
if ch == " " || ch == "\t" || ch == "\n" || ch == "\r" {
|
||||
end = end - 1
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
```
|
||||
- **Pattern**: P5 (Trim specialized)
|
||||
- **Carrier**: end (IntegerBox)
|
||||
- **Update**: end = end - 1
|
||||
- **Routing**: Already whitelisted as `JsonParserBox._trim/1`
|
||||
|
||||
#### `_match_literal` (Line 357-362)
|
||||
```nyash
|
||||
loop(i < len) {
|
||||
if s.substring(pos + i, pos + i + 1) != literal.substring(i, i + 1) {
|
||||
return 0
|
||||
}
|
||||
i = i + 1
|
||||
}
|
||||
```
|
||||
- **Pattern**: P2 (break via return)
|
||||
- **Carrier**: i (IntegerBox)
|
||||
- **Update**: i = i + 1
|
||||
- **Routing**: Already whitelisted as `JsonParserBox._match_literal/3`
|
||||
|
||||
---
|
||||
|
||||
## Deferred Loops (Phase 200+)
|
||||
|
||||
Loops that CANNOT be handled by current P1/P2/P5 patterns.
|
||||
|
||||
| Loop Name | Line | Deferral Reason | Target Phase |
|
||||
|-----------|------|-----------------|--------------|
|
||||
| `_parse_number` | 121-133 | `digits.indexOf()` - ConditionEnv constraint | Phase 200+ |
|
||||
| `_atoi` | 453-460 | `digits.indexOf()` - ConditionEnv constraint | Phase 200+ |
|
||||
| `_parse_string` | 150-178 | Complex carriers (escaped flag via continue) | Phase 195+ |
|
||||
| `_unescape_string` | 373-431 | Complex carriers (is_escape, has_next, process_escape) | Phase 195+ |
|
||||
| `_parse_array` | 203-231 | Multiple MethodCalls (`_parse_value`, nested) | Phase 195+ |
|
||||
| `_parse_object` | 256-304 | Multiple MethodCalls (`_parse_string`, `_parse_value`) | Phase 195+ |
|
||||
|
||||
### Deferral Details
|
||||
|
||||
#### `_parse_number` (Line 121-133) - **ConditionEnv Constraint**
|
||||
```nyash
|
||||
local digits = "0123456789" // ← External local variable
|
||||
loop(p < s.length()) {
|
||||
local ch = s.substring(p, p+1)
|
||||
local digit_pos = digits.indexOf(ch) // ← Uses external 'digits'
|
||||
|
||||
if digit_pos < 0 {
|
||||
break
|
||||
}
|
||||
|
||||
num_str = num_str + ch
|
||||
p = p + 1
|
||||
}
|
||||
```
|
||||
- **Reason**: `digits` is an external local variable not in ConditionEnv
|
||||
- **Constraint**: Phase 193 ConditionEnv only includes: function params, loop carriers, body-local vars
|
||||
- **Workaround**: None (fundamental ConditionEnv limitation)
|
||||
- **Solution**: Phase 200+ ConditionEnv expansion OR .hako rewrite to inline digits
|
||||
|
||||
#### `_atoi` (Line 453-460) - **ConditionEnv Constraint**
|
||||
```nyash
|
||||
local digits = "0123456789" // ← External local variable
|
||||
loop(i < n) {
|
||||
local ch = s.substring(i, i+1)
|
||||
if ch < "0" || ch > "9" { break }
|
||||
local pos = digits.indexOf(ch) // ← Uses external 'digits'
|
||||
if pos < 0 { break }
|
||||
v = v * 10 + pos
|
||||
i = i + 1
|
||||
}
|
||||
```
|
||||
- **Reason**: Same as `_parse_number` - `digits.indexOf()` dependency
|
||||
- **Solution**: Phase 200+ ConditionEnv expansion
|
||||
|
||||
#### `_parse_string` (Line 150-178) - **Complex Carriers**
|
||||
```nyash
|
||||
loop(p < s.length()) {
|
||||
local ch = s.substring(p, p+1)
|
||||
|
||||
if ch == '"' {
|
||||
// ... MapBox construction ...
|
||||
return result
|
||||
}
|
||||
|
||||
if ch == "\\" {
|
||||
local has_next = 0
|
||||
if p + 1 < s.length() { has_next = 1 }
|
||||
|
||||
if has_next == 0 { return null }
|
||||
|
||||
str = str + ch
|
||||
p = p + 1
|
||||
str = str + s.substring(p, p+1)
|
||||
p = p + 1
|
||||
continue // ← Escape handling via continue
|
||||
}
|
||||
|
||||
str = str + ch
|
||||
p = p + 1
|
||||
}
|
||||
```
|
||||
- **Reason**: Escape handling logic uses `continue` + conditional flag (`has_next`)
|
||||
- **Pattern Fit**: P2 or P3 with complex control flow
|
||||
- **Solution**: Phase 195+ Pattern 3 extension for if-in-loop with continue
|
||||
|
||||
#### `_unescape_string` (Line 373-431) - **Complex Carriers**
|
||||
```nyash
|
||||
loop(i < s.length()) {
|
||||
local ch = s.substring(i, i+1)
|
||||
|
||||
local is_escape = 0
|
||||
local has_next = 0
|
||||
|
||||
if ch == "\\" { is_escape = 1 }
|
||||
if i + 1 < s.length() { has_next = 1 }
|
||||
|
||||
local process_escape = 0
|
||||
if is_escape == 1 {
|
||||
if has_next == 1 {
|
||||
process_escape = 1
|
||||
}
|
||||
}
|
||||
|
||||
if process_escape == 1 {
|
||||
// ... multiple escape type checks ...
|
||||
continue
|
||||
}
|
||||
|
||||
result = result + ch
|
||||
i = i + 1
|
||||
}
|
||||
```
|
||||
- **Reason**: Multiple body-local flags (`is_escape`, `has_next`, `process_escape`) + nested conditions
|
||||
- **Pattern Fit**: P3 with complex flatten pattern
|
||||
- **Solution**: Phase 195+ Pattern 3 extension for multi-flag carriers
|
||||
|
||||
#### `_parse_array` / `_parse_object` - **Multiple MethodCalls**
|
||||
```nyash
|
||||
// _parse_array
|
||||
loop(p < s.length()) {
|
||||
local elem_result = me._parse_value(s, p) // ← MethodCall 1
|
||||
if elem_result == null { return null }
|
||||
|
||||
local elem = elem_result.get("value") // ← MethodCall 2
|
||||
arr.push(elem) // ← MethodCall 3
|
||||
|
||||
p = elem_result.get("pos") // ← MethodCall 4
|
||||
// ...
|
||||
}
|
||||
|
||||
// _parse_object
|
||||
loop(p < s.length()) {
|
||||
local key_result = me._parse_string(s, p) // ← MethodCall 1
|
||||
// ...
|
||||
local value_result = me._parse_value(s, p) // ← MethodCall 2
|
||||
// ...
|
||||
obj.set(key, value) // ← MethodCall 3
|
||||
// ...
|
||||
}
|
||||
```
|
||||
- **Reason**: Multiple MethodCalls per iteration (4+ calls)
|
||||
- **Constraint**: Phase 193 supports 1 MethodCall in init, not multiple in body
|
||||
- **Solution**: Phase 195+ MethodCall extension for loop body
|
||||
|
||||
---
|
||||
|
||||
## Statistics
|
||||
|
||||
### Coverage
|
||||
- **JoinIR Target**: 4/10 loops (40%)
|
||||
- **Deferred**: 6/10 loops (60%)
|
||||
|
||||
### Pattern Distribution (Target Loops)
|
||||
- **P2 (Break)**: 2 loops (`_skip_whitespace`, `_match_literal`)
|
||||
- **P5 (Trim)**: 2 loops (`_trim` leading, `_trim` trailing)
|
||||
|
||||
### Deferral Reasons
|
||||
- **ConditionEnv Constraint**: 2 loops (`_parse_number`, `_atoi`)
|
||||
- **Complex Carriers**: 2 loops (`_parse_string`, `_unescape_string`)
|
||||
- **Multiple MethodCalls**: 2 loops (`_parse_array`, `_parse_object`)
|
||||
|
||||
---
|
||||
|
||||
## Implementation Strategy (Phase 194)
|
||||
|
||||
### ✅ Already Whitelisted
|
||||
All 4 target loops are ALREADY in the routing whitelist:
|
||||
- `JsonParserBox._skip_whitespace/2` (Line 88)
|
||||
- `JsonParserBox._trim/1` (Line 87)
|
||||
- `JsonParserBox._match_literal/3` (Line 89)
|
||||
|
||||
### 🎯 E2E Validation
|
||||
**Goal**: Verify these loops actually run on JoinIR route without fallback.
|
||||
|
||||
```bash
|
||||
# Test 1: Basic execution
|
||||
NYASH_JOINIR_CORE=1 ./target/release/hakorune tools/hako_shared/json_parser.hako
|
||||
|
||||
# Test 2: Trace verification
|
||||
NYASH_JOINIR_CORE=1 NYASH_JOINIR_DEBUG=1 ./target/release/hakorune tools/hako_shared/json_parser.hako 2>&1 | grep "\[trace:joinir\]"
|
||||
|
||||
# Expected: NO [joinir/freeze] messages (freeze = fallback to legacy)
|
||||
```
|
||||
|
||||
### ⚠️ No New Whitelist Additions Needed
|
||||
Phase 194 is about **validating existing infrastructure**, not adding new functions.
|
||||
|
||||
### 📊 Success Criteria
|
||||
- [ ] All 4 target loops run on JoinIR route (no freeze)
|
||||
- [ ] JsonParser parses basic JSON successfully
|
||||
- [ ] No regressions in Phase 190-193 tests
|
||||
- [ ] Clear documentation of deferred loops (this file)
|
||||
|
||||
---
|
||||
|
||||
## Next Phase Priorities (Based on Deferral Analysis)
|
||||
|
||||
### Phase 200+: ConditionEnv Expansion (High Value)
|
||||
**Impact**: 2 loops (`_parse_number`, `_atoi`)
|
||||
**Solution**: Expand ConditionEnv to include function-scoped locals OR .hako rewrite
|
||||
|
||||
### Phase 195+: Pattern 3 Extension (Medium Value)
|
||||
**Impact**: 2 loops (`_parse_string`, `_unescape_string`)
|
||||
**Solution**: Support complex carriers with multiple body-local flags
|
||||
|
||||
### Phase 195+: MethodCall Extension (Low Priority)
|
||||
**Impact**: 2 loops (`_parse_array`, `_parse_object`)
|
||||
**Solution**: Support multiple MethodCalls in loop body
|
||||
**Note**: These are complex parsers, may be better handled by future optimization passes
|
||||
|
||||
---
|
||||
|
||||
## Recommendations
|
||||
|
||||
### Short-term (Phase 194)
|
||||
1. ✅ E2E test with existing 4 whitelisted loops
|
||||
2. ✅ Verify no fallback via trace
|
||||
3. ✅ Document findings (this file)
|
||||
|
||||
### Medium-term (Phase 195)
|
||||
1. Focus on Pattern 3 extension (if-in-loop + multiple carriers)
|
||||
2. Target `_parse_string` as representative case
|
||||
3. Defer `_unescape_string` complexity until Pattern 3 is stable
|
||||
|
||||
### Long-term (Phase 200+)
|
||||
1. ConditionEnv expansion design document
|
||||
2. Evaluate .hako rewrite vs. compiler extension
|
||||
3. Consider `digits.indexOf()` as representative case for all external local dependencies
|
||||
|
||||
---
|
||||
|
||||
## Conclusion
|
||||
|
||||
**Phase 194 is validation-focused**: 4 target loops are ALREADY whitelisted and should work with existing P1/P2/P5 patterns. The 6 deferred loops have clear constraints that require future infrastructure:
|
||||
|
||||
- **ConditionEnv**: Needs Phase 200+ design
|
||||
- **Complex Carriers**: Needs Phase 195 Pattern 3 extension
|
||||
- **Multiple MethodCalls**: Needs Phase 195+ design
|
||||
|
||||
This inventory provides a clear roadmap for future JoinIR expansion based on real-world loop patterns.
|
||||
221
docs/development/current/main/phase196-select-bug-analysis.md
Normal file
221
docs/development/current/main/phase196-select-bug-analysis.md
Normal file
@ -0,0 +1,221 @@
|
||||
# Phase 196: Select Expansion Bug Analysis
|
||||
|
||||
## Problem Summary
|
||||
|
||||
JoinIR→MIR の Select 展開処理で、PHI inputs に undefined ValueId が使用される問題が発生している。
|
||||
|
||||
## Reproduction Case
|
||||
|
||||
```hako
|
||||
// apps/tests/phase195_sum_count.hako
|
||||
local i = 1
|
||||
local sum = 0
|
||||
local count = 0
|
||||
|
||||
loop(i <= 5) {
|
||||
if(i % 2 == 1) {
|
||||
sum = sum + i
|
||||
count = count + 1
|
||||
} else {
|
||||
sum = sum + 0
|
||||
count = count + 0
|
||||
}
|
||||
i = i + 1
|
||||
}
|
||||
```
|
||||
|
||||
##症状 (Before)
|
||||
|
||||
### JoinIR 側(正しい)
|
||||
```
|
||||
[joinir_block/handle_select] Created merge_block BasicBlockId(5) with 1 instructions
|
||||
(first=Some(Phi { dst: ValueId(20), inputs: [(BasicBlockId(3), ValueId(14)), (BasicBlockId(4), ValueId(18))], type_hint: None }))
|
||||
```
|
||||
|
||||
### ValueId Remapping(正しい)
|
||||
```
|
||||
[DEBUG-177] JoinIR ValueId(14) → Host ValueId(21)
|
||||
[DEBUG-177] JoinIR ValueId(18) → Host ValueId(25)
|
||||
```
|
||||
|
||||
### Block Remapping(正しい)
|
||||
```
|
||||
[trace:blocks] allocator: Block remap: join_func_1:BasicBlockId(3) → BasicBlockId(8)
|
||||
[trace:blocks] allocator: Block remap: join_func_1:BasicBlockId(4) → BasicBlockId(9)
|
||||
[trace:blocks] allocator: Block remap: join_func_1:BasicBlockId(5) → BasicBlockId(10)
|
||||
```
|
||||
|
||||
### MIR 側(壊れている)
|
||||
```
|
||||
bb10:
|
||||
1: %27 = phi [%28, bb8], [%32, bb9]
|
||||
1: br %20, label bb11, label bb12
|
||||
```
|
||||
|
||||
**問題**: ValueId(28) と ValueId(32) は定義されていない!
|
||||
- 期待値: `phi [%21, bb8], [%25, bb9]`(remap後の値)
|
||||
- 実際: `phi [%28, bb8], [%32, bb9]`(未定義の値)
|
||||
|
||||
## 根本原因の仮説
|
||||
|
||||
### 仮説1: PHI inputs の ValueId が remap されていない
|
||||
- `handle_select()` で生成された PHI は JoinIR ValueId を使用
|
||||
- `instruction_rewriter.rs` の PHI remap ロジック(line 317-333)が何らかの理由で適用されていない
|
||||
|
||||
### 仮説2: 二重 remap による上書き
|
||||
- `remap_instruction()` で 1回 remap(line 304)
|
||||
- 手動 block remap で再度 remap(line 317-333)
|
||||
- 二重 remap が問題を引き起こしている可能性
|
||||
|
||||
### 仮説3: local_block_map のスコープ問題
|
||||
- Select で生成された then/else ブロック(bb3, bb4)が local_block_map に含まれていない
|
||||
- `.unwrap_or(*bb)` でフォールバックしている可能性
|
||||
|
||||
## 調査経路
|
||||
|
||||
1. **handle_select() 実装**:
|
||||
- `src/mir/join_ir_vm_bridge/joinir_block_converter.rs:407-484`
|
||||
- Select → Branch + then/else + merge(PHI) に展開
|
||||
- PHI inputs に生の JoinIR ValueId を使用(line 461)
|
||||
|
||||
2. **instruction_rewriter PHI remap**:
|
||||
- `src/mir/builder/control_flow/joinir/merge/instruction_rewriter.rs:317-333`
|
||||
- PHI の block ID と ValueID を両方 remap
|
||||
- `remapper.remap_value(*val)` で ValueId を変換(line 328)
|
||||
|
||||
3. **remap_instruction() の PHI 処理**:
|
||||
- `src/mir/builder/joinir_id_remapper.rs:327-334`
|
||||
- こちらでも PHI inputs を remap(line 331)
|
||||
|
||||
## 根本原因(確定)
|
||||
|
||||
**仮説2が正解: 二重 remap による破損**
|
||||
|
||||
### 問題の詳細
|
||||
|
||||
`instruction_rewriter.rs` の PHI 処理で、ValueId が **二重に remap** されていた:
|
||||
|
||||
1. **Line 304**: `remapper.remap_instruction(inst)` で PHI inputs の ValueId を remap
|
||||
- JoinIR `ValueId(14)` → Host `ValueId(21)`
|
||||
- JoinIR `ValueId(18)` → Host `ValueId(25)`
|
||||
|
||||
2. **Line 328**: `remapper.remap_value(*val)` で **再度** remap を試行
|
||||
- しかし `*val` は既に Host ValueId(21, 25)になっている!
|
||||
- `remap_value(ValueId(21))` → `value_map` に存在しない → `unwrap_or(21)` → `ValueId(21)`
|
||||
- **問題なく見えるが、実際には壊れている**
|
||||
|
||||
### なぜ壊れたか?
|
||||
|
||||
`remap_instruction()` が返した `inputs` は **既に remap 済み** なのに、line 328 で **もう一度 remap** しようとした。
|
||||
|
||||
しかし、実際には `remap_value()` は idempotent ではない可能性がある(value_map に存在しない場合は元の値を返すが、これは**別の ValueId が偶然同じ番号**の可能性がある)。
|
||||
|
||||
### 修正内容
|
||||
|
||||
**File**: `src/mir/builder/control_flow/joinir/merge/instruction_rewriter.rs`
|
||||
**Line**: 317-335
|
||||
|
||||
```rust
|
||||
// Before (Phase 172)
|
||||
MirInstruction::Phi {
|
||||
dst,
|
||||
inputs,
|
||||
type_hint: None,
|
||||
} => MirInstruction::Phi {
|
||||
dst,
|
||||
inputs: inputs
|
||||
.iter()
|
||||
.map(|(bb, val)| {
|
||||
let remapped_bb = local_block_map.get(bb).copied().unwrap_or(*bb);
|
||||
let remapped_val = remapper.remap_value(*val); // ❌ 二重 remap!
|
||||
(remapped_bb, remapped_val)
|
||||
})
|
||||
.collect(),
|
||||
type_hint: None,
|
||||
},
|
||||
|
||||
// After (Phase 196)
|
||||
MirInstruction::Phi {
|
||||
dst,
|
||||
inputs,
|
||||
type_hint: None,
|
||||
} => MirInstruction::Phi {
|
||||
dst,
|
||||
inputs: inputs
|
||||
.iter()
|
||||
.map(|(bb, val)| {
|
||||
let remapped_bb = local_block_map.get(bb).copied().unwrap_or(*bb);
|
||||
// Phase 196 FIX: Don't double-remap values!
|
||||
// remapper.remap_instruction() already remapped *val
|
||||
(remapped_bb, *val) // ✅ 値はそのまま使う(既に remap 済み)
|
||||
})
|
||||
.collect(),
|
||||
type_hint: None,
|
||||
},
|
||||
```
|
||||
|
||||
### 修正後の動作(After)
|
||||
|
||||
```
|
||||
bb10:
|
||||
1: %27 = phi [%21, bb8], [%25, bb9] // ✅ 正しい ValueId
|
||||
1: br %20, label bb11, label bb12
|
||||
```
|
||||
|
||||
## テスト結果
|
||||
|
||||
### ✅ phase195_sum_count.hako
|
||||
```bash
|
||||
$ NYASH_JOINIR_CORE=1 ./target/release/hakorune apps/tests/phase195_sum_count.hako
|
||||
93
|
||||
RC: 0
|
||||
```
|
||||
期待値: sum=9 (1+3+5), count=3 → result=93 ✅
|
||||
|
||||
### ✅ loop_if_phi.hako (Single-carrier P3)
|
||||
```bash
|
||||
$ NYASH_JOINIR_CORE=1 ./target/release/hakorune apps/tests/loop_if_phi.hako
|
||||
[Console LOG] sum=9
|
||||
RC: 0
|
||||
```
|
||||
|
||||
### ✅ loop_min_while.hako (Pattern 1)
|
||||
```bash
|
||||
$ NYASH_JOINIR_CORE=1 ./target/release/hakorune apps/tests/loop_min_while.hako
|
||||
0
|
||||
1
|
||||
2
|
||||
RC: 0
|
||||
```
|
||||
|
||||
### ✅ joinir_min_loop.hako (Pattern 2)
|
||||
```bash
|
||||
$ NYASH_JOINIR_CORE=1 ./target/release/hakorune apps/tests/joinir_min_loop.hako
|
||||
RC: 0
|
||||
```
|
||||
|
||||
## 退行チェック
|
||||
|
||||
- ✅ Pattern 1 (Simple while): PASS
|
||||
- ✅ Pattern 2 (Break): PASS
|
||||
- ✅ Pattern 3 (Single-carrier): PASS
|
||||
- ✅ Pattern 3 (Multi-carrier): PASS
|
||||
- ✅ No `[joinir/freeze]` warnings
|
||||
|
||||
## まとめ
|
||||
|
||||
### 問題
|
||||
- Select 展開で生成された PHI の inputs が undefined ValueId を参照
|
||||
|
||||
### 根本原因
|
||||
- `instruction_rewriter.rs` で ValueId を二重 remap(1回目: remap_instruction, 2回目: manual remap)
|
||||
|
||||
### 修正
|
||||
- PHI の block ID のみを remap、ValueId は既に remap 済みなのでそのまま使用
|
||||
|
||||
### 影響範囲
|
||||
- **1ファイル 1箇所のみ**: `instruction_rewriter.rs` line 331
|
||||
|
||||
### 成果
|
||||
- Phase 195 の Multi-carrier Pattern 3 が完全動作
|
||||
- 既存 Pattern 1/2/3 に退行なし
|
||||
Reference in New Issue
Block a user