From 2042895ab873f89be4d7059232f791c3d76e7300 Mon Sep 17 00:00:00 2001 From: tignear Date: Tue, 27 Oct 2026 04:11:46 +0900 Subject: [PATCH] perf(x86): validate wide values at mutation boundaries Debug ISel scanned the entire growing wide-value map after every instruction. Large process functions consequently repeated the same invariant checks quadratically. Validate the changed entry in set_wide_chunks, the only insertion path, and scan the complete map once before extracting it for phi construction. Retain all width and chunk-count invariants. Release behavior, HDL optimization levels and generated instruction selection are unchanged. Add a debug-only 128-value regression that counts invariant checks rather than asserting wall-clock time. Validation covers 620 x86 crate tests and 5,700 SystemVerilog/diagnostic tests (20 existing skips), affected clippy with warnings denied, formatting and diff checks. Full VCU108 and HTG9200 compiles still exceeded 300 seconds in the implementation snapshot: this removes a quadratic debug check but does not claim to resolve the board-level timeout or demonstrate an end-to-end speedup. Change-Id: Ief6ae9750c629821b5b52b0e8e7a30ae89b7e545 --- .../src/backend/native/isel.rs | 64 ++++++++++++------- .../src/backend/native/isel/tests/wide.rs | 41 ++++++++++++ 2 files changed, 81 insertions(+), 24 deletions(-) diff --git a/crates/celox-backend-x86/src/backend/native/isel.rs b/crates/celox-backend-x86/src/backend/native/isel.rs index e4da6840d2a..798f1615f56 100644 --- a/crates/celox-backend-x86/src/backend/native/isel.rs +++ b/crates/celox-backend-x86/src/backend/native/isel.rs @@ -1480,10 +1480,6 @@ pub fn lower_execution_unit_with_diagnostics( sparse_write_states.metadata_action(sir_block_id, inst_idx), ); } - if cfg!(debug_assertions) { - ctx.verify_wide_values(); - } - // Track known bit width for redundant mask elimination. let dst_reg = inst.defined_register(); if let Some(dr) = dst_reg { @@ -1563,6 +1559,12 @@ pub fn lower_execution_unit_with_diagnostics( // Extract mask_map for phi node construction (ctx borrows func fields) let saved_mask_map = std::mem::replace(&mut ctx.mask_map, RegMap::new(0)); + // Each mutation validates its own entry. Scan the whole map once at + // the boundary rather than once per instruction, which is quadratic in + // large functions with many wide values. + if cfg!(debug_assertions) { + ctx.verify_wide_values(); + } let saved_wide_regs = std::mem::take(&mut ctx.wide_regs); let saved_wide_masks = std::mem::take(&mut ctx.wide_masks); drop(ctx); // Release borrows on func @@ -1758,6 +1760,11 @@ type WideMaskMap = crate::HashMap>; /// Tracks known constant values for SIR registers (for constant folding in ISel). type ConstMap = crate::HashMap; +#[cfg(test)] +thread_local! { + static WIDE_VALUE_VERIFICATIONS: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + struct ISelContext<'a> { vregs: &'a mut VRegAllocator, spill_descs: &'a mut Vec, @@ -2193,6 +2200,9 @@ impl<'a> ISelContext<'a> { if width <= 64 { self.wide_regs.remove(®); } else { + if cfg!(debug_assertions) { + self.verify_wide_value(®, &chunks); + } self.wide_regs.replace(reg, chunks); } } @@ -2244,28 +2254,34 @@ impl<'a> ISelContext<'a> { /// this map through a direct insertion. fn verify_wide_values(&self) { for (reg, chunks) in self.wide_regs.iter() { - let width = self.sir_width(reg); - assert!( - width > 64, - "narrow SIR register r{} has a wide native representation (width={width})", - reg.0 - ); - let expected_chunks = Self::num_chunks(width); + self.verify_wide_value(reg, chunks); + } + } + + fn verify_wide_value(&self, reg: &RegisterId, chunks: &[(VReg, usize)]) { + #[cfg(test)] + WIDE_VALUE_VERIFICATIONS.with(|count| count.set(count.get() + 1)); + let width = self.sir_width(reg); + assert!( + width > 64, + "narrow SIR register r{} has a wide native representation (width={width})", + reg.0 + ); + let expected_chunks = Self::num_chunks(width); + assert_eq!( + chunks.len(), + expected_chunks, + "wide SIR register r{} has {} chunks, expected {expected_chunks}", + reg.0, + chunks.len() + ); + for (index, (_, chunk_width)) in chunks.iter().enumerate() { + let expected_width = width.saturating_sub(index * 64).min(64); assert_eq!( - chunks.len(), - expected_chunks, - "wide SIR register r{} has {} chunks, expected {expected_chunks}", - reg.0, - chunks.len() + *chunk_width, expected_width, + "wide SIR register r{} chunk {index} has width {chunk_width}, expected {expected_width}", + reg.0 ); - for (index, (_, chunk_width)) in chunks.iter().enumerate() { - let expected_width = width.saturating_sub(index * 64).min(64); - assert_eq!( - *chunk_width, expected_width, - "wide SIR register r{} chunk {index} has width {chunk_width}, expected {expected_width}", - reg.0 - ); - } } } diff --git a/crates/celox-backend-x86/src/backend/native/isel/tests/wide.rs b/crates/celox-backend-x86/src/backend/native/isel/tests/wide.rs index 3f94d1bda99..e1b98ebefce 100644 --- a/crates/celox-backend-x86/src/backend/native/isel/tests/wide.rs +++ b/crates/celox-backend-x86/src/backend/native/isel/tests/wide.rs @@ -1463,3 +1463,44 @@ fn widened_zero_compares_equal_to_a_zero_wide_load() { assert_eq!(state[output_offset] & 1, 1, "four_state={four_state}"); } } + +#[cfg(debug_assertions)] +#[test] +fn wide_value_invariant_validation_scales_with_values() { + const COUNT: usize = 128; + let unit = ExecutionUnit { + entry_block_id: SirBlockId(0), + blocks: [( + SirBlockId(0), + BasicBlock { + id: SirBlockId(0), + params: vec![], + instructions: (0..COUNT) + .map(|i| { + SIRInstruction::Imm( + RegisterId(i), + SIRValue::new_four_state(BigUint::from(i), BigUint::default()), + ) + }) + .collect(), + terminator: SIRTerminator::Return, + }, + )] + .into_iter() + .collect(), + register_map: (0..COUNT) + .map(|i| (RegisterId(i), RegisterType::Logic { width: 65 })) + .collect(), + }; + WIDE_VALUE_VERIFICATIONS.with(|count| count.set(0)); + let _ = lower_execution_unit(&unit, &empty_layout(), false); + let checks = WIDE_VALUE_VERIFICATIONS.with(|count| count.get()); + assert!( + checks >= COUNT, + "the invariant must still be checked: {checks}" + ); + assert!( + checks <= 2 * COUNT, + "repeated whole-map validation: {checks}" + ); +}