Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 40 additions & 24 deletions crates/celox-backend-x86/src/backend/native/isel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1758,6 +1760,11 @@ type WideMaskMap = crate::HashMap<RegisterId, Vec<(VReg, usize)>>;
/// Tracks known constant values for SIR registers (for constant folding in ISel).
type ConstMap = crate::HashMap<RegisterId, u64>;

#[cfg(test)]
thread_local! {
static WIDE_VALUE_VERIFICATIONS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
}

struct ISelContext<'a> {
vregs: &'a mut VRegAllocator,
spill_descs: &'a mut Vec<SpillDesc>,
Expand Down Expand Up @@ -2193,6 +2200,9 @@ impl<'a> ISelContext<'a> {
if width <= 64 {
self.wide_regs.remove(&reg);
} else {
if cfg!(debug_assertions) {
self.verify_wide_value(&reg, &chunks);
}
self.wide_regs.replace(reg, chunks);
}
}
Expand Down Expand Up @@ -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
);
}
}
}

Expand Down
41 changes: 41 additions & 0 deletions crates/celox-backend-x86/src/backend/native/isel/tests/wide.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
);
}
Loading