feat(joinir): Phase 204 PHI Contract Verifier complete

Phase 204-3/5/6/7: PHI inputs verification, integration, testing, docs

Implementation:
- verify_phi_inputs_defined(): Conservative sanity checks (ValueId < 100000)
- Integration: All verifiers in verify_joinir_contracts()
- Testing: 821 tests PASS, no regressions

Task Status:
-  204-1: Design document (phase204-phi-contract-verifier.md)
-  204-2: PHI dst overwrite detection
-  204-3: PHI inputs sanity checks
- ⚠️ 204-4: JoinValueSpace region verification (deferred to Phase 205+)
  - Rationale: Requires LoopHeaderPhiInfo extension (4+ files)
  - Alternative: Conservative threshold checks in verify_phi_inputs_defined()
-  204-5: Integration (verify_joinir_contracts)
-  204-6: Tests (821 PASS)
-  204-7: Documentation (phase204 doc + CURRENT_TASK.md)

Verification Coverage:
-  PHI exists (Phase 200-3)
-  PHI dst match (Phase 200-3)
-  PHI dst not overwritten (Phase 204-2) 
-  PHI inputs sanity (Phase 204-3) 
- ⚠️ PHI inputs DFA (Phase 205+)
- ⚠️ ValueId regions (Phase 205+)

Design Principles:
- Debug-only (#[cfg(debug_assertions)])
- Fail-Fast (panic on violation)
- Zero cost in release builds

Files Modified:
- src/mir/builder/control_flow/joinir/merge/mod.rs (+115 lines)
- src/mir/builder/control_flow/joinir/merge/exit_line/reconnector.rs (1 line)
- docs/development/current/main/phase204-phi-contract-verifier.md (updated)
- CURRENT_TASK.md (Phase 204 complete)

Success Criteria: 4/5 met (1 deferred with rationale)
This commit is contained in:
nyash-codex
2025-12-09 19:57:32 +09:00
parent 0175e62d9e
commit 76a36333c2
3 changed files with 171 additions and 1 deletions

View File

@ -889,6 +889,54 @@ fn get_instruction_dst(instr: &crate::mir::MirInstruction) -> Option<crate::mir:
}
}
/// Verify PHI inputs are defined (Phase 204-3 - Conservative sanity checks)
///
/// # Checks
///
/// 1. PHI inputs have reasonable ValueId values (< threshold)
/// 2. No obviously undefined values (e.g., suspiciously large IDs)
///
/// # Note
///
/// Full data-flow analysis (DFA) verification is deferred to Phase 205+.
/// This function only performs conservative sanity checks.
///
/// # Panics
///
/// Panics in debug mode if suspicious PHI inputs are detected.
#[cfg(debug_assertions)]
fn verify_phi_inputs_defined(
func: &crate::mir::MirFunction,
header_block: crate::mir::BasicBlockId,
) {
let header_block_data = func.blocks.get(&header_block).unwrap_or_else(|| {
panic!(
"[JoinIRVerifier] Header block {} not found ({} blocks in func)",
header_block,
func.blocks.len()
)
});
for instr in &header_block_data.instructions {
if let crate::mir::MirInstruction::Phi { dst, inputs, type_hint: _ } = instr {
for (value_id, pred_block) in inputs {
// Conservative sanity check: ValueId should not be suspiciously large
// Phase 201 JoinValueSpace uses regions:
// - PHI Reserved: 0-99
// - Param: 100-999
// - Local: 1000+
// - Reasonable max: 100000 (arbitrary but catches obvious bugs)
if value_id.0 >= 100000 {
panic!(
"[JoinIRVerifier/Phase204-3] PHI {:?} has suspiciously large input {:?} from predecessor block {:?}",
dst, value_id, pred_block
);
}
}
}
}
}
/// Verify all loop contracts for a merged JoinIR function
///
/// This is the main entry point for verification. It runs all checks
@ -907,5 +955,6 @@ fn verify_joinir_contracts(
) {
verify_loop_header_phis(func, header_block, loop_info, boundary);
verify_no_phi_dst_overwrite(func, header_block, loop_info); // Phase 204-2
verify_phi_inputs_defined(func, header_block); // Phase 204-3
verify_exit_line(func, exit_block, boundary);
}