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
110 changes: 107 additions & 3 deletions crates/celox-frontend-sv/src/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7463,6 +7463,90 @@ struct DynamicPackedWrite {
window: Option<BitAccess>,
}

/// Recover a constant outer element for a checked dynamic bit select of a
/// one-bit packed element. Its only valid inner index is its declared bound.
/// An arbitrary dynamic unpacked element permits more than one index and must
/// retain its dynamic element addressing.
fn single_bit_select_window(
lsb: &sv::ir::ConstExpr,
width: usize,
constants: &HashMap<String, i128>,
types: &HashMap<String, (usize, bool)>,
) -> Option<BitAccess> {
use sv::ir::{BinaryOp, ConstExpr};
fn selected<'a>(
mut expr: &'a ConstExpr,
constants: &HashMap<String, i128>,
types: &HashMap<String, (usize, bool)>,
) -> &'a ConstExpr {
while let ConstExpr::Mux {
condition,
then_expr,
else_expr,
} = expr
{
match sv::typecheck::eval_const_expr_with_types(condition, constants, types) {
Some(0) => expr = else_expr,
Some(_) => expr = then_expr,
None => break,
}
}
expr
}
let (base, offset) = split_dynamic_array_offset(lsb, constants, types)?;
let ConstExpr::Mux {
condition,
then_expr,
else_expr,
} = selected(base, constants, types)
else {
return None;
};
let ConstExpr::Binary {
left,
op: BinaryOp::LogicAnd,
right,
} = selected(condition, constants, types)
else {
return None;
};
let bound = |expr, expected| {
let ConstExpr::Binary { left, op, right } = selected(expr, constants, types) else {
return None;
};
let ConstExpr::Ident(name) = left.as_ref() else {
return None;
};
if *op != expected {
return None;
}
Some((
name,
sv::typecheck::eval_const_expr_with_types(right, constants, types)?,
))
};
let lower = bound(left, BinaryOp::Ge)?;
let upper = bound(right, BinaryOp::Le)?;
if lower != upper {
return None;
}
let invalid = sv::typecheck::eval_const_expr_with_types(else_expr, constants, types)?
.checked_add(offset)?;
let width = i128::try_from(width).ok()?;
if (0..width).contains(&invalid) {
return None;
}
let mut checked_index = constants.clone();
checked_index.insert(lower.0.clone(), lower.1);
let position = sv::typecheck::eval_const_expr_with_types(then_expr, &checked_index, types)?
.checked_add(offset)?;
if !(0..width).contains(&position) {
return None;
}
let position = usize::try_from(position).ok()?;
Some(BitAccess::new(position, position))
}

/// For a run-time select inside one element of an unpacked array, such as
/// `mem[1][i]`, the bits of that element: the constant part of the flattened
/// position names the element, and the run-time part moves within it.
Expand All @@ -7473,9 +7557,13 @@ fn runtime_select_window(
constants: &HashMap<String, i128>,
parameter_types: &HashMap<String, (usize, bool)>,
) -> Option<BitAccess> {
// One-bit elements have no bits within an element, and their run-time
// index has no stride to recognize.
if element_width <= 1 || element_width == width {
// A checked bit select inside a one-bit element can only write its zero
// bit. Preserve the constant outer element in both driver analysis and
// lowering; an arbitrary run-time array index remains unconstrained.
if element_width == 1 {
return single_bit_select_window(lsb, width, constants, parameter_types);
}
if element_width == 0 || element_width == width {
return None;
}
let (base, offset) = split_dynamic_array_offset(lsb, constants, parameter_types)?;
Expand Down Expand Up @@ -9232,6 +9320,14 @@ fn dynamic_array_element_subselection(
if !is_dynamic {
return None;
}
// This is a dynamic bit inside one constant one-bit element, rather
// than a dynamic element of the whole array. Let the packed lowering
// retain the checked element window.
if packed_element_width == 1
&& single_bit_select_window(lsb, variable.width, constants, parameter_types).is_some()
{
return None;
}
let (msb_base, msb_offset) = split_dynamic_array_offset(msb, constants, parameter_types)?;
let (lsb_base, lsb_offset) = split_dynamic_array_offset(lsb, constants, parameter_types)?;
if msb_base != lsb_base {
Expand Down Expand Up @@ -9366,6 +9462,14 @@ fn dynamic_array_element_lvalue(
if !is_dynamic {
return None;
}
// This is a dynamic bit inside one constant one-bit element, rather
// than a dynamic element of the whole array. Let the packed lowering
// retain the checked element window.
if packed_element_width == 1
&& single_bit_select_window(lsb, variable.width, constants, parameter_types).is_some()
{
return None;
}
let (msb_base, msb_offset) = split_dynamic_array_offset(msb, constants, parameter_types)?;
let (lsb_base, lsb_offset) = split_dynamic_array_offset(lsb, constants, parameter_types)?;
if msb_base != lsb_base {
Expand Down
72 changes: 72 additions & 0 deletions crates/celox-test-suite/src/sv/cases/indexed_select.vtest
Original file line number Diff line number Diff line change
Expand Up @@ -2185,3 +2185,75 @@
(assert_eq b2 (? (> cycle 1) (* (- cycle 1) 17) 0))
(assert_eq a4 (? (> cycle 3) (* (- cycle 3) 17) 0))
(assert_eq b4 (? (> cycle 3) (* (- cycle 3) 17) 0))))

(case disjoint_single_bit_array_drivers
(source "single_bit_drivers.sv" #"
module Top #(parameter BASE=0)(input logic signed [31:0] index, input logic [2:0] value,
output wire [2:0] q);
logic [BASE:BASE] ready[3];
for (genvar n=0; n<3; n=n+1) begin : lanes
always_comb begin
ready[n] = '0;
ready[n][index] = value[n];
end
assign q[n] = ready[n][BASE];
end
endmodule
"#)
(top Top)
(modify (set index 0) (set value 5))
(assert_eq q 5)
(modify (set value 2))
(assert_eq q 2)
(modify (set value 0))
(assert_eq q 0)
(modify (set value 7))
(assert_eq q 7))

(case disjoint_shifted_single_bit_array_drivers
(source "single_bit_drivers.sv" #"
module Top #(parameter BASE=7)(input logic signed [31:0] index, input logic [2:0] value,
output wire [2:0] q);
logic [BASE:BASE] ready[3];
for (genvar n=0; n<3; n=n+1) begin : lanes
always_comb begin
ready[n] = '0;
ready[n][index] = value[n];
end
assign q[n] = ready[n][BASE];
end
endmodule
"#)
(top Top)
(modify (set index 7) (set value 5))
(assert_eq q 5)
(modify (set value 2))
(assert_eq q 2)
(modify (set value 0))
(assert_eq q 0)
(modify (set value 7))
(assert_eq q 7))

(case disjoint_negative_single_bit_array_drivers
(source "single_bit_drivers.sv" #"
module Top #(parameter BASE=-2)(input logic signed [31:0] index, input logic [2:0] value,
output wire [2:0] q);
logic [BASE:BASE] ready[3];
for (genvar n=0; n<3; n=n+1) begin : lanes
always_comb begin
ready[n] = '0;
ready[n][index] = value[n];
end
assign q[n] = ready[n][BASE];
end
endmodule
"#)
(top Top)
(modify (set index (trunc 32 -2)) (set value 5))
(assert_eq q 5)
(modify (set value 2))
(assert_eq q 2)
(modify (set value 0))
(assert_eq q 0)
(modify (set value 7))
(assert_eq q 7))
41 changes: 41 additions & 0 deletions crates/celox/tests/frontends/systemverilog/indexed_select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,3 +403,44 @@ sv_backends! {
@case "indexed_select::generated_pipeline_with_explicit_generate_branches";
}
}

sv_backends! {
fn disjoint_single_bit_array_drivers(sim) {
@case "indexed_select::disjoint_single_bit_array_drivers";
}
}

// Verilator 5.052 truncates the bit index of a one-bit packed element and
// writes its zero bit for index 2. Keep the out-of-range check local; the
// disjoint-driver behavior at valid indices is shared with both adapters.
sv_backends! {
fn single_bit_array_drivers_ignore_invalid_inner_indices(sim) {
@setup {
let source = "module Top(input logic [1:0] index, input logic [2:0] value,
output wire [2:0] q);
logic [0:0] ready[3];
for (genvar n=0; n<3; n=n+1) begin : lanes
always_comb begin ready[n]='0; ready[n][index]=value[n]; end
assign q[n]=ready[n][0];
end
endmodule";
}
@build Simulator::from_sv_sources(vec![(source, Path::new("single_bit_bounds.sv"))], "Top");
let index=sim.signal("index");
let value=sim.signal("value");
let q=sim.signal("q");
for (position, bits, expected) in [(0u8,5u8,5u8),(1,5,0),(2,5,0),(0,2,2)] {
sim.modify(|io| {io.set(index,position);io.set(value,bits);}).unwrap();
assert_eq!(sim.get(q),BigUint::from(expected));
}
}
}

sv_backends! {
fn disjoint_shifted_single_bit_array_drivers(sim) {
@case "indexed_select::disjoint_shifted_single_bit_array_drivers";
}
fn disjoint_negative_single_bit_array_drivers(sim) {
@case "indexed_select::disjoint_negative_single_bit_array_drivers";
}
}
15 changes: 15 additions & 0 deletions crates/celox/tests/frontends/systemverilog/review_regressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8974,3 +8974,18 @@ fn net_alias_coalescing_retains_distinct_and_variable_drivers() {
assert!(error.contains(diagnostic), "{error}");
}
}

#[test]
fn single_bit_array_driver_windows_still_reject_overlaps() {
for source in [
"module Top(input logic index, input logic a, b, output logic [0:0] q[2]);
always_comb begin q[0]='0; q[0][index]=a; end
always_comb begin q[0]='0; q[0][index]=b; end endmodule",
"module Top(input logic index, input logic a, b, output logic [0:0] q[2]);
always_comb q[index]=a;
always_comb q[0]=b; endmodule",
] {
let error = cranelift_build_error(source);
assert!(error.contains("multiple variable drivers"), "{error}");
}
}
Loading