feat: フェーズM.2完了 - JSON v0 Bridge層PHI統一でno_phi完全撤廃

- strip_phi_functions()削除: 40行の複雑なPHI→edge-copy後処理撤廃
- JSON v0 Bridge 8箇所のno_phi分岐完全削除:
  - try_catch.rs: 3箇所統一
  - ternary.rs, peek.rs, expr.rs, loop_.rs: 各1-2箇所統一
- config::env::mir_no_phi()大幅簡略化: 40行→8行、phi-legacy依存除去
- 未使用コード削除: PHI_ON_GATED_WARNED static、mir_no_phiフィールド
- 未使用import削除: HashSet、collect_phi_incoming_if_reachable

効果: フェーズM+M.2で推定500行超削減、MIR層PHI完全統一達成
Phase 15セルフホスティング80k→20k行圧縮の主要基盤完成

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Selfhosting Dev
2025-09-23 07:41:08 +09:00
parent 96abbf1634
commit 09149be41a
9 changed files with 41 additions and 160 deletions

View File

@ -26,7 +26,7 @@ pub(super) struct LoopContext {
#[derive(Clone)]
pub(super) struct BridgeEnv {
pub(super) throw_enabled: bool,
pub(super) mir_no_phi: bool,
// フェーズM.2: mir_no_phiフィールド削除PHI統一で不要
pub(super) allow_me_dummy: bool,
pub(super) me_class: String,
pub(super) try_result_mode: bool,
@ -35,13 +35,13 @@ pub(super) struct BridgeEnv {
impl BridgeEnv {
pub(super) fn load() -> Self {
let trm = crate::config::env::try_result_mode();
let no_phi = crate::config::env::mir_no_phi();
// フェーズM.2: no_phi変数削除
if crate::config::env::cli_verbose() {
eprintln!("[Bridge] load: try_result_mode={} mir_no_phi={}", trm, no_phi);
eprintln!("[Bridge] load: try_result_mode={}", trm);
}
Self {
throw_enabled: std::env::var("NYASH_BRIDGE_THROW_ENABLE").ok().as_deref() == Some("1"),
mir_no_phi: no_phi,
// フェーズM.2: mir_no_phiフィールド削除
allow_me_dummy: std::env::var("NYASH_BRIDGE_ME_DUMMY").ok().as_deref() == Some("1"),
me_class: std::env::var("NYASH_BRIDGE_ME_CLASS").unwrap_or_else(|_| "Main".to_string()),
try_result_mode: trm,
@ -61,45 +61,7 @@ fn jump_with_pred(f: &mut MirFunction, cur_bb: BasicBlockId, target: BasicBlockI
/// Strip Phi instructions by inserting edge copies on each predecessor.
/// This normalizes MIR to PHI-off form for downstream harnesses that synthesize PHIs.
fn strip_phi_functions(f: &mut MirFunction) {
// Collect block ids to avoid borrow issues while mutating
let block_ids: Vec<BasicBlockId> = f.blocks.keys().copied().collect();
for bbid in block_ids {
// Snapshot phi instructions at the head
let mut phi_entries: Vec<(ValueId, Vec<(BasicBlockId, ValueId)>)> = Vec::new();
if let Some(bb) = f.blocks.get(&bbid) {
for inst in &bb.instructions {
if let MirInstruction::Phi { dst, inputs } = inst {
phi_entries.push((*dst, inputs.clone()));
} else {
// PHIs must be at the beginning; once we see non-Phi, stop
break;
}
}
}
if phi_entries.is_empty() {
continue;
}
// Insert copies on predecessors
for (dst, inputs) in &phi_entries {
for (pred, val) in inputs {
if let Some(pbb) = f.blocks.get_mut(pred) {
pbb.add_instruction(MirInstruction::Copy { dst: *dst, src: *val });
}
}
}
// Remove Phi instructions from the merge block
if let Some(bb) = f.blocks.get_mut(&bbid) {
let non_phi: Vec<MirInstruction> = bb
.instructions
.iter()
.cloned()
.skip_while(|inst| matches!(inst, MirInstruction::Phi { .. }))
.collect();
bb.instructions = non_phi;
}
}
}
// フェーズM.2: strip_phi_functions()削除 - PHI統一により不要
fn lower_break_stmt(f: &mut MirFunction, cur_bb: BasicBlockId, exit_bb: BasicBlockId) {
jump_with_pred(f, cur_bb, exit_bb);
@ -256,10 +218,7 @@ pub(super) fn lower_program(prog: ProgramV0) -> Result<MirModule, String> {
}
}
f.signature.return_type = MirType::Unknown;
// PHI-off normalization for Bridge output
if env.mir_no_phi {
strip_phi_functions(&mut f);
}
// フェーズM.2: PHI後処理削除 - MirBuilder/LoopBuilderでPHI統一済み
module.add_function(f);
Ok(module)
}

View File

@ -255,14 +255,8 @@ pub(super) fn lower_expr_with_scope<S: VarScope>(
}
}
let out = f.next_value_id();
if env.mir_no_phi {
if let Some(bb) = f.get_block_mut(fall_bb) {
bb.add_instruction(MirInstruction::Copy { dst: out, src: cdst });
}
if let Some(bb) = f.get_block_mut(rhs_end) {
bb.add_instruction(MirInstruction::Copy { dst: out, src: rval });
}
} else if let Some(bb) = f.get_block_mut(merge_bb) {
// フェーズM.2: PHI統一処理no_phi分岐削除
if let Some(bb) = f.get_block_mut(merge_bb) {
let mut inputs: Vec<(BasicBlockId, ValueId)> = vec![(fall_bb, cdst)];
if rhs_end != fall_bb {
inputs.push((rhs_end, rval));

View File

@ -21,18 +21,15 @@ pub(super) fn lower_loop_stmt(
bb.add_instruction(MirInstruction::Jump { target: cond_bb });
}
}
let no_phi = env.mir_no_phi;
// フェーズM.2: no_phi変数削除
let base_vars = vars.clone();
let orig_names: Vec<String> = base_vars.keys().cloned().collect();
let mut phi_map: HashMap<String, ValueId> = HashMap::new();
for name in &orig_names {
if let Some(&bval) = base_vars.get(name) {
let dst = f.next_value_id();
if no_phi {
if let Some(bb) = f.get_block_mut(cur_bb) {
bb.add_instruction(MirInstruction::Copy { dst, src: bval });
}
} else if let Some(bb) = f.get_block_mut(cond_bb) {
// フェーズM.2: PHI統一処理no_phi分岐削除
if let Some(bb) = f.get_block_mut(cond_bb) {
bb.insert_instruction_after_phis(MirInstruction::Phi {
dst,
inputs: vec![(cur_bb, bval)],
@ -69,18 +66,8 @@ pub(super) fn lower_loop_stmt(
Some(MirInstruction::Jump { target, .. }) if *target == cond_bb
);
if backedge_to_cond {
if no_phi {
for (name, &phi_dst) in &phi_map {
if let Some(&latch_val) = body_vars.get(name) {
if let Some(bb) = f.get_block_mut(bend) {
bb.add_instruction(MirInstruction::Copy {
dst: phi_dst,
src: latch_val,
});
}
}
}
} else if let Some(bb) = f.get_block_mut(cond_bb) {
// フェーズM.2: PHI統一処理no_phi分岐削除
if let Some(bb) = f.get_block_mut(cond_bb) {
for (name, &phi_dst) in &phi_map {
if let Some(&latch_val) = body_vars.get(name) {
for inst in &mut bb.instructions {

View File

@ -69,13 +69,8 @@ pub(super) fn lower_peek_expr_with_scope<S: VarScope>(
// Merge result
let out = f.next_value_id();
if env.mir_no_phi {
for (pred, val) in phi_inputs {
if let Some(bb) = f.get_block_mut(pred) {
bb.add_instruction(MirInstruction::Copy { dst: out, src: val });
}
}
} else if let Some(bb) = f.get_block_mut(merge_bb) {
// フェーズM.2: PHI統一処理no_phi分岐削除
if let Some(bb) = f.get_block_mut(merge_bb) {
let mut inputs = phi_inputs;
inputs.sort_by_key(|(bbid, _)| bbid.0);
bb.insert_instruction_after_phis(MirInstruction::Phi { dst: out, inputs });

View File

@ -44,14 +44,8 @@ pub(super) fn lower_ternary_expr_with_scope<S: VarScope>(
}
}
let out = f.next_value_id();
if env.mir_no_phi {
if let Some(bb) = f.get_block_mut(tend) {
bb.add_instruction(MirInstruction::Copy { dst: out, src: tval });
}
if let Some(bb) = f.get_block_mut(eend) {
bb.add_instruction(MirInstruction::Copy { dst: out, src: eval });
}
} else if let Some(bb) = f.get_block_mut(merge_bb) {
// フェーズM.2: PHI統一処理no_phi分岐削除
if let Some(bb) = f.get_block_mut(merge_bb) {
let mut inputs = vec![(tend, tval), (eend, eval)];
inputs.sort_by_key(|(bbid, _)| bbid.0);
bb.insert_instruction_after_phis(MirInstruction::Phi { dst: out, inputs });

View File

@ -70,7 +70,8 @@ pub(super) fn lower_try_stmt(
let catch_clause = &catches[0];
let mut catch_vars = base_vars.clone();
if let Some(param) = &catch_clause.param {
if !env.mir_no_phi && !incoming_exc.is_empty() {
// フェーズM.2: PHI統一処理no_phi条件削除
if !incoming_exc.is_empty() {
let phi_dst = f.next_value_id();
if let Some(bb) = f.get_block_mut(catch_bb) {
let mut inputs = incoming_exc.clone();
@ -281,16 +282,8 @@ pub(super) fn lower_try_stmt(
phi_entries.push((dst, inputs));
merged_vars.insert(name.clone(), dst);
}
if env.mir_no_phi {
// Emit edge copies on predecessors instead of Phi at merge
for (dst, inputs) in phi_entries {
for (pred, val) in inputs {
if let Some(pbb) = f.get_block_mut(pred) {
pbb.add_instruction(MirInstruction::Copy { dst, src: val });
}
}
}
} else if let Some(bb) = f.get_block_mut(finally_block) {
// フェーズM.2: PHI統一処理no_phi分岐削除
if let Some(bb) = f.get_block_mut(finally_block) {
for (dst, inputs) in phi_entries {
bb.insert_instruction_after_phis(MirInstruction::Phi { dst, inputs });
}
@ -344,15 +337,8 @@ pub(super) fn lower_try_stmt(
phi_entries.push((dst, inputs));
merged_vars.insert(name.clone(), dst);
}
if env.mir_no_phi {
for (dst, inputs) in phi_entries {
for (pred, val) in inputs {
if let Some(pbb) = f.get_block_mut(pred) {
pbb.add_instruction(MirInstruction::Copy { dst, src: val });
}
}
}
} else if let Some(bb) = f.get_block_mut(exit_bb) {
// フェーズM.2: PHI統一処理no_phi分岐削除
if let Some(bb) = f.get_block_mut(exit_bb) {
for (dst, inputs) in phi_entries {
bb.insert_instruction_after_phis(MirInstruction::Phi { dst, inputs });
}