Files
hakorune/src/mir/phi_core/common.rs

61 lines
2.1 KiB
Rust
Raw Normal View History

/*!
* phi_core::common shared types and invariants (scaffold)
*
* Phase 1 keeps this minimal; future phases may move debug asserts and
* predicate set checks here for both if/loop PHI normalization.
*/
/// Placeholder for future shared PHI input type alias.
/// Using the same tuple form as MIR Phi instruction inputs.
pub type PhiInput = (crate::mir::BasicBlockId, crate::mir::ValueId);
#[cfg(debug_assertions)]
pub fn debug_verify_phi_inputs(
function: &crate::mir::MirFunction,
merge_bb: crate::mir::BasicBlockId,
inputs: &[(crate::mir::BasicBlockId, crate::mir::ValueId)],
) {
use std::collections::HashSet;
refactor(mir): Phase 6-2 - Apply helper methods to reduce ~28 lines in JSON v0 Bridge **Phase 6-2完了:ヘルパーメソッド適用で28行削減達成!** ## 📊 削減実績 - **loop_.rs**: 8行→1行(7行削減) - PHI更新ループ統一 - **if_else.rs**: 6行→1行(5行削減) - Branch終端設定統一 - **try_catch.rs**: 8箇所×2-3行(16行削減) - Jump終端設定統一 - **合計**: ~28行削減 ## 🔧 適用内容 ### 1. loop_.rs - PHI更新の統一化 - **Before**: 手動でPHI命令を検索してinputs.push() - **After**: `bb.update_phi_input(phi_dst, (bend, latch_val))?` ### 2. if_else.rs - Branch終端設定の統一化 - **Before**: if-let-Some + bb.set_terminator(Branch {...}) - **After**: `f.set_branch_terminator(cur, cval, then_bb, else_bb)?` ### 3. try_catch.rs - Jump終端設定の統一化(8箇所) - **Before**: if-let-Some + bb.set_terminator(Jump {...}) - **After**: `f.set_jump_terminator(bb_id, target)?` ## ✅ テスト結果 - `loop_min_while.nyash`: ✅ PASS(0,1,2出力) - `loop_phi_one_sided.nyash`: ✅ PASS(ArrayBox警告のみ) - ビルド: ✅ 88 warnings(既存レベル) ## 🎯 Phase 6進捗 - **Phase 6-1**: ✅ ヘルパーメソッド追加(+46行) - **Phase 6-2**: ✅ ヘルパー適用(-28行) - **実質削減**: -28行(基盤整備込み) ## 📋 次のステップ - **Phase 6-3**: BranchMergeBuilder pattern(~100行削減見込み) - **Phase 6-4**: 全体統合・最終テスト Related: Phase 1-5(3,824行削減)に続く段階的リファクタリング 🐱 にゃーん!実用化成功!
2025-11-01 15:23:28 +09:00
// Make a local, up-to-date view of CFG predecessors by rebuilding from successors.
// This avoids false positives when callers verify immediately after emitting terminators.
let mut func = function.clone();
func.update_cfg();
let mut seen = HashSet::new();
for (pred, _v) in inputs.iter() {
debug_assert_ne!(
*pred, merge_bb,
"PHI incoming predecessor must not be the merge block itself"
);
debug_assert!(
seen.insert(*pred),
"Duplicate PHI incoming predecessor detected: {:?}",
pred
);
}
refactor(mir): Phase 6-2 - Apply helper methods to reduce ~28 lines in JSON v0 Bridge **Phase 6-2完了:ヘルパーメソッド適用で28行削減達成!** ## 📊 削減実績 - **loop_.rs**: 8行→1行(7行削減) - PHI更新ループ統一 - **if_else.rs**: 6行→1行(5行削減) - Branch終端設定統一 - **try_catch.rs**: 8箇所×2-3行(16行削減) - Jump終端設定統一 - **合計**: ~28行削減 ## 🔧 適用内容 ### 1. loop_.rs - PHI更新の統一化 - **Before**: 手動でPHI命令を検索してinputs.push() - **After**: `bb.update_phi_input(phi_dst, (bend, latch_val))?` ### 2. if_else.rs - Branch終端設定の統一化 - **Before**: if-let-Some + bb.set_terminator(Branch {...}) - **After**: `f.set_branch_terminator(cur, cval, then_bb, else_bb)?` ### 3. try_catch.rs - Jump終端設定の統一化(8箇所) - **Before**: if-let-Some + bb.set_terminator(Jump {...}) - **After**: `f.set_jump_terminator(bb_id, target)?` ## ✅ テスト結果 - `loop_min_while.nyash`: ✅ PASS(0,1,2出力) - `loop_phi_one_sided.nyash`: ✅ PASS(ArrayBox警告のみ) - ビルド: ✅ 88 warnings(既存レベル) ## 🎯 Phase 6進捗 - **Phase 6-1**: ✅ ヘルパーメソッド追加(+46行) - **Phase 6-2**: ✅ ヘルパー適用(-28行) - **実質削減**: -28行(基盤整備込み) ## 📋 次のステップ - **Phase 6-3**: BranchMergeBuilder pattern(~100行削減見込み) - **Phase 6-4**: 全体統合・最終テスト Related: Phase 1-5(3,824行削減)に続く段階的リファクタリング 🐱 にゃーん!実用化成功!
2025-11-01 15:23:28 +09:00
if let Some(block) = func.blocks.get(&merge_bb) {
for (pred, _v) in inputs.iter() {
refactor(mir): Phase 6-2 - Apply helper methods to reduce ~28 lines in JSON v0 Bridge **Phase 6-2完了:ヘルパーメソッド適用で28行削減達成!** ## 📊 削減実績 - **loop_.rs**: 8行→1行(7行削減) - PHI更新ループ統一 - **if_else.rs**: 6行→1行(5行削減) - Branch終端設定統一 - **try_catch.rs**: 8箇所×2-3行(16行削減) - Jump終端設定統一 - **合計**: ~28行削減 ## 🔧 適用内容 ### 1. loop_.rs - PHI更新の統一化 - **Before**: 手動でPHI命令を検索してinputs.push() - **After**: `bb.update_phi_input(phi_dst, (bend, latch_val))?` ### 2. if_else.rs - Branch終端設定の統一化 - **Before**: if-let-Some + bb.set_terminator(Branch {...}) - **After**: `f.set_branch_terminator(cur, cval, then_bb, else_bb)?` ### 3. try_catch.rs - Jump終端設定の統一化(8箇所) - **Before**: if-let-Some + bb.set_terminator(Jump {...}) - **After**: `f.set_jump_terminator(bb_id, target)?` ## ✅ テスト結果 - `loop_min_while.nyash`: ✅ PASS(0,1,2出力) - `loop_phi_one_sided.nyash`: ✅ PASS(ArrayBox警告のみ) - ビルド: ✅ 88 warnings(既存レベル) ## 🎯 Phase 6進捗 - **Phase 6-1**: ✅ ヘルパーメソッド追加(+46行) - **Phase 6-2**: ✅ ヘルパー適用(-28行) - **実質削減**: -28行(基盤整備込み) ## 📋 次のステップ - **Phase 6-3**: BranchMergeBuilder pattern(~100行削減見込み) - **Phase 6-4**: 全体統合・最終テスト Related: Phase 1-5(3,824行削減)に続く段階的リファクタリング 🐱 にゃーん!実用化成功!
2025-11-01 15:23:28 +09:00
// Accept either declared predecessor or a direct successor edge pred -> merge_bb
let ok_pred = block.predecessors.contains(pred)
|| func
.blocks
.get(pred)
.map(|p| p.successors.contains(&merge_bb))
.unwrap_or(false);
if !ok_pred {
eprintln!(
"[phi-verify][warn] incoming pred {:?} is not a predecessor of merge bb {:?}",
pred, merge_bb
);
}
}
}
}
#[cfg(not(debug_assertions))]
pub fn debug_verify_phi_inputs(
_function: &crate::mir::MirFunction,
_merge_bb: crate::mir::BasicBlockId,
_inputs: &[(crate::mir::BasicBlockId, crate::mir::ValueId)],
) {
}