From 02e37d48ef13c7ef03da64d23593f3ce1caff0c5 Mon Sep 17 00:00:00 2001 From: tignear Date: Thu, 22 Oct 2026 22:23:13 +0900 Subject: [PATCH] fix(sv): lower loop-indexed packed word part-selects Procedural loops selecting bytes from multidimensional packed words or implicitly typed packed parameters failed with "indexed part-select start". Accept deferred starts for the selected packed word and constant parameter metadata, preserving preceding indices and the index expression width/sign. This lets normal loop unrolling and optimization handle expressions such as words[n][i*8 +: 8] and ID[8*(last-i) +: 8]. Add three portable regressions covering ascending/descending packed ranges, masked writes, generated memory banks, and string parameter ROM bytes. Validation: - Full SV backend harness: 3873 passed, 20 existing ignored; analyzer unit tests: 784 passed, 3 existing ignored, on master a061e1396. - Affected library/harness Clippy, cargo fmt, and diff checks pass. - All four related procedural-loop cases pass Verilator and Icarus. - Shared suite unit tests: 46 passed, 2 existing ignored (base 446753453). - Unchanged upstream Taxi sources pass all 88 cocotb tests across seven representative configurations (DMA RAM x2, XFCP APB/AXI/AXI-Lite/I2C/switch), using O1 and two-state simulation with no skipped tests. This runtime replay used base 446753453; the latest-base checks above cover the combined tree. Change-Id: I6d1f2581123bd6805227a49d01c9edb22f4517a8 --- crates/celox-sv-analyzer/src/ast/selects.rs | 25 ++--- .../src/sv/cases/indexed_select.vtest | 97 +++++++++++++++++++ .../frontends/systemverilog/indexed_select.rs | 11 +++ 3 files changed, 122 insertions(+), 11 deletions(-) diff --git a/crates/celox-sv-analyzer/src/ast/selects.rs b/crates/celox-sv-analyzer/src/ast/selects.rs index 941ef781958..3945c4f45eb 100644 --- a/crates/celox-sv-analyzer/src/ast/selects.rs +++ b/crates/celox-sv-analyzer/src/ast/selects.rs @@ -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 { diff --git a/crates/celox-test-suite/src/sv/cases/indexed_select.vtest b/crates/celox-test-suite/src/sv/cases/indexed_select.vtest index da1e5612b22..7a3c79395be 100644 --- a/crates/celox-test-suite/src/sv/cases/indexed_select.vtest +++ b/crates/celox-test-suite/src/sv/cases/indexed_select.vtest @@ -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)) diff --git a/crates/celox/tests/frontends/systemverilog/indexed_select.rs b/crates/celox/tests/frontends/systemverilog/indexed_select.rs index 94701d08590..7f38381d410 100644 --- a/crates/celox/tests/frontends/systemverilog/indexed_select.rs +++ b/crates/celox/tests/frontends/systemverilog/indexed_select.rs @@ -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"; }