fix(mir): PHI検証panic修正 - update_cfg()を検証前に呼び出し

A案実装: debug_verify_phi_inputs呼び出し前にCFG predecessorを更新

修正箇所(7箇所):
- src/mir/builder/phi.rs:50, 73, 132, 143
- src/mir/builder/ops.rs:273, 328, 351

根本原因:
- Branch/Jump命令でsuccessorは即座に更新
- predecessorはupdate_cfg()で遅延再構築
- PHI検証が先に実行されてpredecessor未更新でpanic

解決策:
- 各debug_verify_phi_inputs呼び出し前に
  if let Some(func) = self.current_function.as_mut() {
      func.update_cfg();
  }
  を挿入してCFGを同期

影響: if/else文、論理演算子(&&/||)のPHI生成が正常動作
This commit is contained in:
nyash-codex
2025-11-01 13:28:56 +09:00
parent 149ec61d4d
commit 6a452b2dca
174 changed files with 2432 additions and 1014 deletions

View File

@ -839,8 +839,8 @@ impl super::MirBuilder {
self.variable_map.insert(p.clone(), pid);
}
}
let program_ast = function_lowering::wrap_in_program(body);
let _last = self.build_expression(program_ast)?;
// Lower statements in sequence to preserve def→use order
let _last = self.cf_block(body)?;
if !returns_value && !self.is_current_block_terminated() {
let void_val = crate::mir::builder::emission::constant::emit_void(self);
self.emit_instruction(MirInstruction::Return {

View File

@ -54,6 +54,15 @@ impl super::MirBuilder {
self
.value_types
.insert(pid, super::MirType::Box("ArrayBox".to_string()));
// Explicitly call birth() to initialize internal state
self.emit_instruction(MirInstruction::BoxCall {
dst: None,
box_val: pid,
method: "birth".to_string(),
method_id: None,
args: vec![],
effects: super::EffectMask::MUT,
})?;
if let Some(args) = script_args.as_ref() {
for arg in args {
let val = crate::mir::builder::emission::constant::emit_string(self, arg.clone());
@ -75,7 +84,8 @@ impl super::MirBuilder {
}
self.variable_map.insert(p.clone(), pid);
}
let lowered = self.build_expression(program_ast);
// Lower statements in order to preserve def→use
let lowered = self.cf_block(body.clone());
self.variable_map = saved_var_map;
lowered
} else {

View File

@ -219,6 +219,15 @@ impl super::MirBuilder {
box_type: "ArrayBox".to_string(),
args: vec![],
})?;
// Explicit birth() to satisfy runtime invariant (NewBox→birth)
self.emit_instruction(MirInstruction::BoxCall {
dst: None,
box_val: arr_id,
method: "birth".to_string(),
method_id: None,
args: vec![],
effects: super::EffectMask::MUT,
})?;
self.value_origin_newbox
.insert(arr_id, "ArrayBox".to_string());
self
@ -244,6 +253,15 @@ impl super::MirBuilder {
box_type: "MapBox".to_string(),
args: vec![],
})?;
// Explicit birth() to satisfy runtime invariant (NewBox→birth)
self.emit_instruction(MirInstruction::BoxCall {
dst: None,
box_val: map_id,
method: "birth".to_string(),
method_id: None,
args: vec![],
effects: super::EffectMask::MUT,
})?;
self
.value_origin_newbox
.insert(map_id, "MapBox".to_string());

View File

@ -269,6 +269,9 @@ impl super::MirBuilder {
self.start_new_block(rhs_join)?;
let rhs_bool = self.value_gen.next();
let inputs = vec![(rhs_true_exit, t_id), (rhs_false_exit, f_id)];
if let Some(func) = self.current_function.as_mut() {
func.update_cfg();
}
if let (Some(func), Some(cur_bb)) = (&self.current_function, self.current_block) {
crate::mir::phi_core::common::debug_verify_phi_inputs(func, cur_bb, &inputs);
}
@ -324,6 +327,9 @@ impl super::MirBuilder {
self.start_new_block(rhs_join)?;
let rhs_bool = self.value_gen.next();
let inputs = vec![(rhs_true_exit, t_id), (rhs_false_exit, f_id)];
if let Some(func) = self.current_function.as_mut() {
func.update_cfg();
}
if let (Some(func), Some(cur_bb)) = (&self.current_function, self.current_block) {
crate::mir::phi_core::common::debug_verify_phi_inputs(func, cur_bb, &inputs);
}
@ -347,6 +353,9 @@ impl super::MirBuilder {
// Result PHI (bool)
let result_val = self.value_gen.next();
let inputs = vec![(then_exit_block, then_value_raw), (else_exit_block, else_value_raw)];
if let Some(func) = self.current_function.as_mut() {
func.update_cfg();
}
if let (Some(func), Some(cur_bb)) = (&self.current_function, self.current_block) {
crate::mir::phi_core::common::debug_verify_phi_inputs(func, cur_bb, &inputs);
}

View File

@ -46,6 +46,9 @@ impl MirBuilder {
let else_pred = else_exit_block_opt.unwrap_or(else_block);
let merged = self.value_gen.next();
let inputs = vec![(then_pred, then_v), (else_pred, else_v)];
if let Some(func) = self.current_function.as_mut() {
func.update_cfg();
}
if let (Some(func), Some(cur_bb)) = (&self.current_function, self.current_block) {
crate::mir::phi_core::common::debug_verify_phi_inputs(func, cur_bb, &inputs);
}
@ -69,6 +72,9 @@ impl MirBuilder {
let else_pred = else_exit_block_opt.unwrap_or(else_block);
let merged = self.value_gen.next();
let inputs = vec![(then_pred, then_v), (else_pred, else_v)];
if let Some(func) = self.current_function.as_mut() {
func.update_cfg();
}
if let (Some(func), Some(cur_bb)) = (&self.current_function, self.current_block) {
crate::mir::phi_core::common::debug_verify_phi_inputs(func, cur_bb, &inputs);
}
@ -128,6 +134,9 @@ impl MirBuilder {
let else_pred = else_exit_block_opt.unwrap_or(else_block);
// Emit Phi for the assigned variable and bind it
let inputs = vec![(then_pred, then_value_for_var), (else_pred, else_value_for_var)];
if let Some(func) = self.current_function.as_mut() {
func.update_cfg();
}
if let (Some(func), Some(cur_bb)) = (&self.current_function, self.current_block) {
crate::mir::phi_core::common::debug_verify_phi_inputs(func, cur_bb, &inputs);
}
@ -139,6 +148,9 @@ impl MirBuilder {
let then_pred = then_exit_block_opt.unwrap_or(then_block);
let else_pred = else_exit_block_opt.unwrap_or(else_block);
let inputs = vec![(then_pred, then_value_raw), (else_pred, else_value_raw)];
if let Some(func) = self.current_function.as_mut() {
func.update_cfg();
}
if let (Some(func), Some(cur_bb)) = (&self.current_function, self.current_block) {
crate::mir::phi_core::common::debug_verify_phi_inputs(func, cur_bb, &inputs);
}