refactor(phi): Phase 26-B-3 - loop_builder.rs and json_v0_bridge PhiInputCollector integration

- loop_builder.rs: Replace LoopSnapshotMergeBox calls with PhiInputCollector for continue-merge PHI generation
- json_v0_bridge/lowering/loop_.rs: Replace LoopSnapshotMergeBox calls with PhiInputCollector for continue_merge_bb PHI generation
- Unified PHI input handling across all loop builders (loopform_builder, loop_builder, json_v0_bridge)
- Tests: mir_loopform_exit_phi all passed (4/4)

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
nyash-codex
2025-11-20 18:07:11 +09:00
parent 059533876e
commit 26288b5451
2 changed files with 21 additions and 12 deletions

View File

@ -9,6 +9,7 @@ use super::{BasicBlockId, ConstValue, MirInstruction, ValueId};
use crate::mir::control_form::{ControlForm, IfShape, LoopShape, is_control_form_trace_on}; use crate::mir::control_form::{ControlForm, IfShape, LoopShape, is_control_form_trace_on};
use crate::mir::phi_core::loopform_builder::{LoopFormBuilder, LoopFormOps}; use crate::mir::phi_core::loopform_builder::{LoopFormBuilder, LoopFormOps};
use crate::mir::phi_core::loop_snapshot_merge::LoopSnapshotMergeBox; use crate::mir::phi_core::loop_snapshot_merge::LoopSnapshotMergeBox;
use crate::mir::phi_core::phi_input_collector::PhiInputCollector;
use crate::ast::ASTNode; use crate::ast::ASTNode;
use std::collections::HashMap; use std::collections::HashMap;
@ -431,17 +432,21 @@ impl<'a> LoopBuilder<'a> {
} }
} }
// 各変数について PHI ノードを生成(LoopSnapshotMergeBox の最適化を使用) // 各変数について PHI ノードを生成(Phase 26-B-3: PhiInputCollector使用)
let mut merged = HashMap::new(); let mut merged = HashMap::new();
for (var_name, mut inputs) in all_vars { for (var_name, inputs) in all_vars {
// Phase 26-B-3: Use PhiInputCollector
let mut collector = PhiInputCollector::new();
collector.add_snapshot(&inputs);
collector.sanitize();
// Phase 25.2: optimize_same_value() で最適化判定 // Phase 25.2: optimize_same_value() で最適化判定
let result_value = if let Some(same_val) = LoopSnapshotMergeBox::optimize_same_value(&inputs) { let result_value = if let Some(same_val) = collector.optimize_same_value() {
// 全て同じ値 or 単一入力 → PHI 不要 // 全て同じ値 or 単一入力 → PHI 不要
same_val same_val
} else { } else {
// 異なる値を持つ場合は PHI ノードを生成 // 異なる値を持つ場合は PHI ノードを生成
// Phase 25.2: sanitize_inputs() で入力を正規化 let final_inputs = collector.finalize();
LoopSnapshotMergeBox::sanitize_inputs(&mut inputs);
let phi_id = self.new_value(); let phi_id = self.new_value();
@ -449,7 +454,7 @@ impl<'a> LoopBuilder<'a> {
if let Some(merge_block) = func.blocks.get_mut(&continue_merge_id) { if let Some(merge_block) = func.blocks.get_mut(&continue_merge_id) {
merge_block.add_instruction(MirInstruction::Phi { merge_block.add_instruction(MirInstruction::Phi {
dst: phi_id, dst: phi_id,
inputs, inputs: final_inputs,
}); });
} }
} }

View File

@ -24,6 +24,7 @@ use super::{lower_stmt_list_with_vars, new_block, BridgeEnv, LoopContext};
use crate::mir::{BasicBlockId, MirFunction, MirInstruction, ValueId}; use crate::mir::{BasicBlockId, MirFunction, MirInstruction, ValueId};
use crate::mir::phi_core::loopform_builder::{LoopFormBuilder, LoopFormOps}; use crate::mir::phi_core::loopform_builder::{LoopFormBuilder, LoopFormOps};
use crate::mir::phi_core::loop_snapshot_merge::LoopSnapshotMergeBox; use crate::mir::phi_core::loop_snapshot_merge::LoopSnapshotMergeBox;
use crate::mir::phi_core::phi_input_collector::PhiInputCollector;
use std::collections::HashMap; use std::collections::HashMap;
use super::super::ast::StmtV0; use super::super::ast::StmtV0;
use super::super::ast::ExprV0; use super::super::ast::ExprV0;
@ -337,22 +338,25 @@ pub(super) fn lower_loop_stmt(
} }
// 6-2) continue_merge_bb に必要な PHI を生成しつつ、merged_snapshot を作る // 6-2) continue_merge_bb に必要な PHI を生成しつつ、merged_snapshot を作る
// Phase 26-B-3: Use PhiInputCollector
let mut merged_snapshot: HashMap<String, ValueId> = HashMap::new(); let mut merged_snapshot: HashMap<String, ValueId> = HashMap::new();
for (name, mut inputs) in all_inputs { for (name, inputs) in all_inputs {
let value = if let Some(same_val) = let mut collector = PhiInputCollector::new();
LoopSnapshotMergeBox::optimize_same_value(&inputs) collector.add_snapshot(&inputs);
{ collector.sanitize();
let value = if let Some(same_val) = collector.optimize_same_value() {
// 全て同じ値 or 単一入力 → PHI 不要 // 全て同じ値 or 単一入力 → PHI 不要
same_val same_val
} else { } else {
// 異なる値を持つ場合は PHI ノードを continue_merge_bb に生成 // 異なる値を持つ場合は PHI ノードを continue_merge_bb に生成
LoopSnapshotMergeBox::sanitize_inputs(&mut inputs); let final_inputs = collector.finalize();
let phi_id = ops.f.next_value_id(); let phi_id = ops.f.next_value_id();
crate::mir::ssot::cf_common::insert_phi_at_head( crate::mir::ssot::cf_common::insert_phi_at_head(
ops.f, ops.f,
continue_merge_bb, continue_merge_bb,
phi_id, phi_id,
inputs, final_inputs,
); );
phi_id phi_id
}; };