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
25 changes: 14 additions & 11 deletions crates/celox-sv-analyzer/src/ast/selects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -981,18 +981,21 @@ pub(super) fn indexed_select_bounds_values(
// unsigned type or truncate a carry across the base expression's width.
let Some(base_value) = eval_ast_const_expr(state, &base, &dimensions.const_env) else {
// A procedural loop index is not constant while its body is bound.
// Keep the endpoints in declared coordinates so normal memory range
// flattening retains the element offset. Lowering can then unroll the
// loop before requiring a constant selection inside each memory word.
let memory_word = name
.and_then(|name| dimensions.get(name))
.is_some_and(|shape| {
!shape.unpacked.is_empty()
&& index_count == shape.unpacked.len()
&& shape.packed.len() == 1
});
// Keep the endpoints in declared coordinates so normal range flattening
// retains preceding unpacked and packed indices. Lowering can then unroll
// the loop before requiring a constant selection inside the selected word.
let selected_word = name.is_some_and(|name| {
if let Some(shape) = dimensions.get(name) {
index_count >= shape.unpacked.len()
&& index_count + 1 == shape.unpacked.len() + shape.packed.len()
} else {
index_count == 0
&& dimensions::parameter_type_from_const_env(&dimensions.const_env, name)
.is_some_and(|ty| ty.width > 1)
}
});
let ty = self_determined_type(state, &base, dimensions).filter(|ty| ty.0 <= 32);
let Some(ty) = ty.filter(|_| memory_word) else {
let Some(ty) = ty.filter(|_| selected_word) else {
return Err(unsupported("indexed part-select start"));
};
let base = ConstExpr::Resize {
Expand Down
97 changes: 97 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 @@ -1763,3 +1763,100 @@
(modify (set index index_value))
(assert_eq narrowed (& (>> 0x0123456789abcdef (& index_value 3)) 15))
(assert_eq typed (& (>> 0x0123456789abcdef (& index_value 7)) 15))))

; IEEE 1800-2023 11.5.1: an indexed select remains valid after selecting
; a packed array element; procedural loop indices keep their own width/sign.
(case procedural_loop_selects_packed_words
(source "loop_packed_words.sv" #"
module Top(input logic clk, input logic [3:0] be,
input logic [31:0] data,
output logic [31:0] down_plus, down_minus, up_plus, up_minus);
logic [1:0][31:0] down = '0;
logic [2:3][0:31] up = '0;
always_ff @(posedge clk) begin
for (integer i = 0; i < 4; i = i + 1) begin
if (be[i]) begin
down[1][i*8 +: 8] <= data[i*8 +: 8];
down[0][31-i*8 -: 8] <= data[31-i*8 -: 8];
up[2][i*8 +: 8] <= data[i*8 +: 8];
up[3][31-i*8 -: 8] <= data[31-i*8 -: 8];
end
end
end
assign down_plus = down[1];
assign down_minus = down[0];
assign up_plus = up[2];
assign up_minus = up[3];
endmodule
"#)
(top Top)
(modify (set be 15) (set data 0x12345678))
(tick clk)
(assert_eq down_plus 0x12345678)
(assert_eq down_minus 0x12345678)
(assert_eq up_plus 0x78563412)
(assert_eq up_minus 0x78563412)
(modify (set be 5) (set data 0xaabbccdd))
(tick clk)
(assert_eq down_plus 0x12bb56dd)
(assert_eq down_minus 0xaa34cc78)
(assert_eq up_plus 0xdd56bb12)
(assert_eq up_minus 0x78cc34aa))

(case procedural_loop_reads_packed_words_into_memory
(source "loop_packed_memory.sv" #"
module Top(input logic clk, en, input logic [1:0] addr,
input logic [63:0] data, output logic [31:0] low_word, high_word);
wire [1:0][31:0] words = data;
wire [1:0][31:0] read_words;
for (genvar n = 0; n < 2; n = n + 1) begin : bank
logic [31:0] mem[4];
initial for (integer k = 0; k < 4; k = k + 1) mem[k] = 0;
always_ff @(posedge clk) begin
for (integer i = 0; i < 4; i = i + 1) begin
if (en) mem[addr][i*8 +: 8] <= words[n][i*8 +: 8];
end
end
assign read_words[n] = mem[addr];
end
assign low_word = read_words[0];
assign high_word = read_words[1];
endmodule
"#)
(top Top)
(modify (set en 1) (set addr 1) (set data 0x1234567889abcdef))
(tick clk)
(assert_eq low_word 0x89abcdef)
(assert_eq high_word 0x12345678)
(modify (set addr 2) (set data 0xfedcba9876543210))
(tick clk)
(assert_eq low_word 0x76543210)
(assert_eq high_word 0xfedcba98)
(modify (set en 0) (set addr 1))
(assert_eq low_word 0x89abcdef)
(assert_eq high_word 0x12345678))


(case procedural_loop_selects_constant_packed_bytes
(source "loop_constant_packed_bytes.sv" #"
module Top(input logic [1:0] addr, output logic [7:0] q);
localparam ID = "ABCD";
logic [7:0] rom[4];
initial begin
integer last;
last = 3;
for (integer i = 0; i < 4; i = i + 1) begin
rom[i] = ID[8*(last-i) +: 8];
end
end
assign q = rom[addr];
endmodule
"#)
(top Top)
(assert_eq q 0x41)
(modify (set addr 1))
(assert_eq q 0x42)
(modify (set addr 2))
(assert_eq q 0x43)
(modify (set addr 3))
(assert_eq q 0x44))
11 changes: 11 additions & 0 deletions crates/celox/tests/frontends/systemverilog/indexed_select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@ use super::*;
// IEEE 1800-2023 11.5.1: the index operator and declaration direction
// independently determine the endpoints of the selected range.
sv_backends! {
fn procedural_loop_selects_constant_packed_bytes(sim) {
@case "indexed_select::procedural_loop_selects_constant_packed_bytes";
}

fn procedural_loop_selects_packed_words(sim) {
@case "indexed_select::procedural_loop_selects_packed_words";
}
fn procedural_loop_reads_packed_words_into_memory(sim) {
@case "indexed_select::procedural_loop_reads_packed_words_into_memory";
}

fn procedural_loop_writes_memory_word_part_selects(sim) {
@case "indexed_select::procedural_loop_writes_memory_word_part_selects";
}
Expand Down
Loading