diff --git a/crates/celox-sir-opt/src/lib.rs b/crates/celox-sir-opt/src/lib.rs index 57d173ca032..5995c8250ff 100644 --- a/crates/celox-sir-opt/src/lib.rs +++ b/crates/celox-sir-opt/src/lib.rs @@ -1,3 +1,10 @@ +#![cfg_attr( + test, + allow( + clippy::disallowed_macros, + reason = "thread-local verification counters exist only in unit tests; non-test builds enforce the ban" + ) +)] //! Backend-independent SIR optimization policy and pass pipeline. pub type HashMap = fxhash::FxHashMap; diff --git a/crates/celox-sir-opt/src/optimizer/passes/control_flow/pass_branchify_mux/coupled.rs b/crates/celox-sir-opt/src/optimizer/passes/control_flow/pass_branchify_mux/coupled.rs index 46fc7ca20c9..332cdc4d302 100644 --- a/crates/celox-sir-opt/src/optimizer/passes/control_flow/pass_branchify_mux/coupled.rs +++ b/crates/celox-sir-opt/src/optimizer/passes/control_flow/pass_branchify_mux/coupled.rs @@ -20,7 +20,9 @@ pub(super) fn branchify_coupled_state_updates( applied += plans.len(); apply_coupled_state_update_batch(eu, plans, next_block_id, reg_counter); } - + if applied != 0 { + verify_coupled_rewrites(eu); + } applied } @@ -43,9 +45,25 @@ pub(super) fn branchify_coupled_priority_chains( applied += 1; worklist.push_front(merge); } + if applied != 0 { + verify_coupled_rewrites(eu); + } applied } +/// Verify the complete rewrite once, rather than revisiting every unrelated +/// block and definition after each local state update or priority chain. +fn verify_coupled_rewrites(eu: &ExecutionUnit) { + debug_assert_eq!( + { + #[cfg(test)] + super::tests::COUPLED_BATCH_VERIFICATIONS.with(|count| count.set(count.get() + 1)); + eu.verify_result() + }, + Ok(()) + ); +} + fn find_coupled_priority_chain_in_block( eu: &ExecutionUnit, block_id: BlockId, @@ -286,7 +304,6 @@ fn apply_coupled_priority_chain( terminator: original.terminator, }, ); - debug_assert_eq!(eu.verify_result(), Ok(())); merge_id } @@ -758,5 +775,4 @@ fn apply_coupled_state_update_batch( terminator: original.terminator, }, ); - debug_assert_eq!(eu.verify_result(), Ok(())); } diff --git a/crates/celox-sir-opt/src/optimizer/passes/control_flow/pass_branchify_mux/tests.rs b/crates/celox-sir-opt/src/optimizer/passes/control_flow/pass_branchify_mux/tests.rs index 51612ab2aad..cf5a8bd9cce 100644 --- a/crates/celox-sir-opt/src/optimizer/passes/control_flow/pass_branchify_mux/tests.rs +++ b/crates/celox-sir-opt/src/optimizer/passes/control_flow/pass_branchify_mux/tests.rs @@ -16,6 +16,10 @@ use crate::ir::{InstanceId, RegisterType, SIRValue}; use celox_design::StateObjectId as VarId; use num_bigint::BigUint; +thread_local! { + pub(super) static COUPLED_BATCH_VERIFICATIONS: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + fn addr(id: usize) -> RegionedAbsoluteAddr { RegionedAbsoluteAddr { region: 0, diff --git a/crates/celox-sir-opt/src/optimizer/passes/control_flow/pass_branchify_mux/tests/coupled.rs b/crates/celox-sir-opt/src/optimizer/passes/control_flow/pass_branchify_mux/tests/coupled.rs index 2c7665abefa..69a38bbc20a 100644 --- a/crates/celox-sir-opt/src/optimizer/passes/control_flow/pass_branchify_mux/tests/coupled.rs +++ b/crates/celox-sir-opt/src/optimizer/passes/control_flow/pass_branchify_mux/tests/coupled.rs @@ -1,5 +1,97 @@ use super::*; +#[cfg(debug_assertions)] +#[test] +fn coupled_rewrites_verify_once_across_many_source_blocks() { + const COUNT: usize = 16; + for priority_chain in [false, true] { + let blocks = (0..COUNT) + .map(|lane| { + let base = 6 + lane * 4; + BasicBlock { + id: BlockId(lane), + params: if lane == 0 { + (0..6).map(RegisterId).collect() + } else { + Vec::new() + }, + instructions: vec![ + SIRInstruction::Mux( + RegisterId(base), + RegisterId(0), + RegisterId(4), + RegisterId(2), + ), + SIRInstruction::Mux( + RegisterId(base + 1), + RegisterId(0), + RegisterId(5), + RegisterId(3), + ), + SIRInstruction::Mux( + RegisterId(base + 2), + RegisterId(1), + RegisterId(4), + RegisterId(base), + ), + SIRInstruction::Mux( + RegisterId(base + 3), + RegisterId(1), + RegisterId(5), + RegisterId(base + 1), + ), + store(lane * 2, base + 2), + store(lane * 2 + 1, base + 3), + ], + terminator: if lane + 1 == COUNT { + SIRTerminator::Return + } else { + SIRTerminator::Jump(BlockId(lane + 1), Vec::new()) + }, + } + }) + .collect(); + let mut eu = cfg_unit(6 + COUNT * 4, &[0, 1], blocks); + eu.verify_result().unwrap(); + let counts = count_uses(&eu); + let mut next_block = COUNT; + let mut next_register = 5 + COUNT * 4; + COUPLED_BATCH_VERIFICATIONS.with(|count| count.set(0)); + let applied = if priority_chain { + super::super::coupled::branchify_coupled_priority_chains( + &mut eu, + &counts, + &mut next_block, + &mut next_register, + ) + } else { + super::super::coupled::branchify_coupled_state_updates( + &mut eu, + &counts, + &mut next_block, + &mut next_register, + ) + }; + assert_eq!(applied, COUNT * if priority_chain { 1 } else { 2 }); + assert_eq!(COUPLED_BATCH_VERIFICATIONS.with(|count| count.get()), 1); + eu.verify_result().unwrap(); + assert!(!eu.blocks.values().any(|block| { + block + .instructions + .iter() + .any(|inst| matches!(inst, SIRInstruction::Mux(..))) + })); + assert_eq!( + eu.blocks + .values() + .flat_map(|block| &block.instructions) + .filter(|inst| matches!(inst, SIRInstruction::Store(..))) + .count(), + COUNT * 2 + ); + } +} + #[test] fn branchifies_coupled_state_updates_with_interleaved_conditions() { let mut eu = cfg_unit( diff --git a/crates/celox-sir-opt/src/optimizer/passes/control_flow/pass_guarded_region_sinking.rs b/crates/celox-sir-opt/src/optimizer/passes/control_flow/pass_guarded_region_sinking.rs index 06e7d6007f1..6fa73fdf2ae 100644 --- a/crates/celox-sir-opt/src/optimizer/passes/control_flow/pass_guarded_region_sinking.rs +++ b/crates/celox-sir-opt/src/optimizer/passes/control_flow/pass_guarded_region_sinking.rs @@ -2129,6 +2129,21 @@ fn form_coupled_store_regions(eu: &mut ExecutionUnit) { apply_coupled_store_plan(eu, plan, next_block, &mut reg_counter); next_block += consumed; } + // Plans belong to distinct original blocks and were collected before any + // mutation. Check the completed batch once rather than rescanning every + // unrelated definition after each local rewrite. + verify_coupled_store_batch(eu); +} + +fn verify_coupled_store_batch(eu: &ExecutionUnit) { + debug_assert_eq!( + { + #[cfg(test)] + tests::COUPLED_BATCH_VERIFICATIONS.with(|count| count.set(count.get() + 1)); + eu.verify_result() + }, + Ok(()) + ); } fn best_coupled_store_plan( @@ -2714,7 +2729,6 @@ fn apply_coupled_store_plan( for value in dead_mux_values { eu.register_map.remove(&value); } - debug_assert_eq!(eu.verify_result(), Ok(())); } fn form_same_predicate_regions(eu: &mut ExecutionUnit) { @@ -4097,6 +4111,10 @@ fn instruction_has_effect(inst: &SIRInstruction) -> bool { #[cfg(test)] mod tests { + thread_local! { + pub(super) static COUPLED_BATCH_VERIFICATIONS: std::cell::Cell = const { std::cell::Cell::new(0) }; + } + use super::*; use crate::ir::{DomainKind, InstanceId, SIRValue, TriggerIdWithKind}; use celox_design::StateObjectId as VarId; @@ -4561,6 +4579,99 @@ mod tests { assert_eq!(after.blocks, before.blocks); } + #[cfg(debug_assertions)] + #[test] + fn coupled_store_batches_verify_once_and_preserve_all_stores() { + const COUNT: usize = 16; + let mut registers = HashMap::from_iter([ + (RegisterId(0), bit(1)), + (RegisterId(1), bit(8)), + (RegisterId(2), bit(8)), + ]); + let mut blocks = HashMap::default(); + for lane in 0..COUNT { + let base = 3 + lane * 11; + let reg = |offset| RegisterId(base + offset); + for offset in 0..11 { + registers.insert(reg(offset), bit(8)); + } + let mut instructions = vec![SIRInstruction::Binary( + reg(0), + RegisterId(1), + BinaryOp::Mul, + RegisterId(1), + )]; + for offset in 1..7 { + instructions.push(SIRInstruction::Binary( + reg(offset), + reg(offset - 1), + BinaryOp::Mul, + RegisterId(1), + )); + } + instructions.extend([ + SIRInstruction::Imm(reg(7), SIRValue::new(85u8)), + SIRInstruction::Binary(reg(8), reg(6), BinaryOp::Add, RegisterId(2)), + SIRInstruction::Mux(reg(9), RegisterId(0), reg(6), reg(7)), + SIRInstruction::Mux(reg(10), RegisterId(0), reg(8), reg(7)), + SIRInstruction::Store( + address(100 + lane * 2), + SIROffset::Static(0), + 8, + reg(9), + vec![], + vec![], + ), + SIRInstruction::Store( + address(101 + lane * 2), + SIROffset::Static(0), + 8, + reg(10), + vec![], + vec![], + ), + ]); + insert_block( + &mut blocks, + lane, + if lane == 0 { + vec![RegisterId(0), RegisterId(1), RegisterId(2)] + } else { + vec![] + }, + instructions, + if lane + 1 == COUNT { + SIRTerminator::Return + } else { + SIRTerminator::Jump(BlockId(lane + 1), vec![]) + }, + ); + } + let mut eu = ExecutionUnit { + entry_block_id: BlockId(0), + blocks, + register_map: registers, + }; + eu.verify_result().unwrap(); + let before = eu.clone(); + COUPLED_BATCH_VERIFICATIONS.with(|count| count.set(0)); + form_coupled_store_regions(&mut eu); + assert_eq!(COUPLED_BATCH_VERIFICATIONS.with(|count| count.get()), 1); + assert_eq!( + eu.blocks.len(), + COUNT * 4, + "every independent store group should be transformed" + ); + eu.verify_result().unwrap(); + for condition in [0, 1] { + for input in [0, 1] { + let expected = execute_with_inputs(&before, condition, input, 7); + let actual = execute_with_inputs(&eu, condition, input, 7); + assert_eq!(actual, expected); + } + } + } + #[test] fn coupled_stores_recover_the_complete_shared_priority_spine() { let mut register_map = HashMap::default();