From 7c97e183df5f27d6f8e56002912ab4f598d0e6f3 Mon Sep 17 00:00:00 2001 From: tignear Date: Sun, 25 Oct 2026 09:04:31 +0900 Subject: [PATCH] feat(sv): accept loop temporaries shared by plain edge always blocks Several plain edge-triggered `always` processes may write one variable, such as the `integer i` that C906's aq_biu_req_arbiter uses as the index of two `always @(posedge clk)` loops. Only always_comb, always_latch and always_ff restrict a variable to one process (IEEE 1800-2023 9.2.2.2, 9.2.2.4); otherwise the last write determines the value (6.5). Celox rejected the design with "multiple variable drivers for `i`". A variable other than an unpacked array that only plain edge processes write, and only by blocking assignments, is now accepted. Those processes read and write its stable state directly, as a blocking assignment updates the variable at once (10.4.1): a process reads its own preceding writes, a later edge sees the other process's writes, and the last write stays even when a coincident process does not write the variable. The usual per-process working seed and commit would let such a process commit the pre-edge value over another process's write. The handling is per variable; designs without such variables lower as before. always_ff overlap and nonblocking writes from several processes stay rejected. Closes #1945 Change-Id: I2bcf163f2ca3ebc3b62c1e6c8fad0c4720f54958 --- crates/celox-frontend-sv/src/lowering.rs | 65 ++++++++- crates/celox-frontend-sv/src/lowering/ff.rs | 37 ++++- .../src/sv/cases/procedural.vtest | 137 ++++++++++++++++++ .../frontends/systemverilog/procedural.rs | 12 ++ .../systemverilog/review_regressions.rs | 42 ++++++ 5 files changed, 284 insertions(+), 9 deletions(-) diff --git a/crates/celox-frontend-sv/src/lowering.rs b/crates/celox-frontend-sv/src/lowering.rs index 0202c7f5c14..a7e271a6439 100644 --- a/crates/celox-frontend-sv/src/lowering.rs +++ b/crates/celox-frontend-sv/src/lowering.rs @@ -1117,6 +1117,60 @@ fn shared_edge_memories( memories } +/// Variables, other than unpacked arrays, that several plain edge-triggered +/// `always` processes write only by blocking assignments, such as a loop +/// index they share. A variable may have several procedural writers, and the +/// last write determines its value (IEEE 1800-2023 6.5); only always_comb, +/// always_latch and always_ff restrict it to one process (9.2.2.2, 9.2.2.4). +/// The processes read and write the variable's stable state directly (see +/// `Ff::with_shared_variables`). +fn shared_edge_variables( + module: &sv::ir::Module, + constants: &HashMap, + parameter_types: &HashMap, +) -> HashSet { + let first_ff = module.comb_processes().len(); + let end_ff = first_ff + module.ff_processes().len(); + let mut nonblocking = HashSet::default(); + for process in module.ff_processes() { + for stmt in process.body() { + stmt.walk(&mut |stmt| match stmt { + sv::ir::Stmt::Assign { + lhs, + nonblocking: true, + .. + } => { + nonblocking.insert(lhs.name()); + } + sv::ir::Stmt::AssignConcat { + parts, + nonblocking: true, + .. + } => nonblocking.extend(parts.iter().map(sv::ir::LValue::name)), + _ => {} + }); + } + } + module + .signals() + .iter() + .filter(|signal| { + !signal.is_net() + && signal.r#type().unpacked_ranges().is_empty() + && !nonblocking.contains(signal.name()) + }) + .filter(|signal| { + let drivers = local_driver_ranges(module, signal.name(), constants, parameter_types); + drivers.iter().any(|(id, _)| drivers[0].0 != *id) + && drivers.iter().all(|(id, _)| { + (first_ff..end_ff).contains(id) + && !module.ff_processes()[id - first_ff].is_always_ff() + }) + }) + .map(|signal| signal.name().to_string()) + .collect() +} + /// The unpacked arrays that only edge-triggered processes of `module` drive. /// A process that writes one of them only by nonblocking assignments, at /// least one of them at a run-time element, publishes just the elements it @@ -1399,7 +1453,7 @@ fn validate_variable_driver_ranges( module: &sv::ir::Module, constants: &HashMap, parameter_types: &HashMap, - shared_memories: &HashSet, + shared: &HashSet, ) -> Result<(), sv::AnalyzerError> { for port in module .ports() @@ -1448,7 +1502,7 @@ fn validate_variable_driver_ranges( && net_driver_ranges_overlap(left.1, right.1) }) }); - if overlapping_continuous || (has_overlap && !shared_memories.contains(signal_name)) { + if overlapping_continuous || (has_overlap && !shared.contains(signal_name)) { return Err(sv::AnalyzerError::Unsupported(format!( "multiple variable drivers for `{signal_name}`" ))); @@ -3303,7 +3357,9 @@ fn lower_module_with_overrides( foreign_writes, warnings, ); - validate_variable_driver_ranges(module, &constants, ¶meter_types, &shared_memories)?; + let shared_variables = shared_edge_variables(module, &constants, ¶meter_types); + let shared_all = shared_memories.union(&shared_variables).cloned().collect(); + validate_variable_driver_ranges(module, &constants, ¶meter_types, &shared_all)?; validate_foreign_drivers( module, &constants, @@ -3651,6 +3707,7 @@ fn lower_module_with_overrides( &ff_event_roles, &shared_memories, foreign_writes, + &shared_variables, &mut repeated_ff_writes, )?; ( @@ -9141,6 +9198,7 @@ fn lower_ff_processes( ff_event_roles: &[FfEventRole], shared_memories: &HashSet, foreign_writes: &HashMap, + shared_variables: &HashSet, repeated_writes: &mut Vec, ) -> Result { let mut parallel_ff_parts = HashMap::<_, Vec<_>>::default(); @@ -9269,6 +9327,7 @@ fn lower_ff_processes( staged, } = ff::Ff::new(pm, random_seeds[&(0, index)]) .with_sparse_memories(shared_memories) + .with_shared_variables(shared_variables) .with_sparse_candidates(&edge_memories) .with_window_writes(&window_writes) .lower_process(process.body())?; diff --git a/crates/celox-frontend-sv/src/lowering/ff.rs b/crates/celox-frontend-sv/src/lowering/ff.rs index 6058fabcc4a..3220791f75c 100644 --- a/crates/celox-frontend-sv/src/lowering/ff.rs +++ b/crates/celox-frontend-sv/src/lowering/ff.rs @@ -53,6 +53,9 @@ pub(super) struct Ff<'p, 'a> { /// Variables a process kernel may write between the evaluation of this /// process and the NBA region (see `kernel_window_writes`). window_writes: HashSet, + /// Variables several edge processes write by blocking assignments (see + /// `with_shared_variables`). + shared: HashSet, b: SIRBuilder, /// Variables read from the working region. working: HashSet, @@ -129,6 +132,7 @@ impl<'p, 'a> Ff<'p, 'a> { element_writes: HashSet::default(), other_writes: HashSet::default(), window_writes: HashSet::default(), + shared: HashSet::default(), b: SIRBuilder::new(), working: HashSet::default(), nonblocking: HashSet::default(), @@ -255,10 +259,23 @@ impl<'p, 'a> Ff<'p, 'a> { self } + /// Variables other edge processes also write by blocking assignments. + /// The process reads and writes their stable state directly, as a + /// blocking assignment updates the variable at once (IEEE 1800-2023 + /// 10.4.1): its reads see its own preceding writes, and the last write of + /// any process stays, even when a later process does not write it. A + /// working value seeded by each process would instead be committed by + /// every one of them. + pub fn with_shared_variables(mut self, names: &HashSet) -> Self { + self.shared = names.iter().filter_map(|name| self.m.id(name)).collect(); + self + } + /// Where a write of `id`, and a read of a value the process wrote, go: - /// the working region, or stable state in a process kernel. + /// the working region, or stable state in a process kernel and for a + /// shared variable. fn target(&self, id: SourceVarId) -> Addr { - if self.kernel.is_some() || self.direct { + if self.kernel.is_some() || self.direct || self.shared.contains(&id) { stable(id) } else { working(id) @@ -475,9 +492,11 @@ impl<'p, 'a> Ff<'p, 'a> { let working_set = &self.working; let variables = &*self.m.variables; let direct = self.kernel.is_some() || self.direct; + let shared = &self.shared; let map = |id: &SourceVarId| -> Addr { - if direct { - stable(*aliases.get(id).unwrap_or(id)) + let var = aliases.get(id).unwrap_or(id); + if direct || shared.contains(var) { + stable(*var) } else if let Some(var) = aliases.get(id) { working(*var) } else if working_set.contains(id) @@ -4268,6 +4287,11 @@ impl<'p, 'a> Ff<'p, 'a> { }) .collect(); self.sparse_memories.extend(sparse); + if self.nonblocking.iter().any(|id| self.shared.contains(id)) { + return Err(unsupported( + "nonblocking writes to a variable several edge processes write", + )); + } if self .working .iter() @@ -4333,11 +4357,12 @@ impl<'p, 'a> Ff<'p, 'a> { stage_nonblocking(body, &staged, self.m) }; // A variable staged for an exact publication has no next state of - // its own: only its merge publishes it. + // its own: only its merge publishes it. A shared variable is written + // in stable state. let published: Vec<_> = targets .iter() .copied() - .filter(|target| !exact.contains(&target.id)) + .filter(|target| !exact.contains(&target.id) && !self.shared.contains(&target.id)) .collect(); let ordinary_targets: Vec<_> = published .iter() diff --git a/crates/celox-test-suite/src/sv/cases/procedural.vtest b/crates/celox-test-suite/src/sv/cases/procedural.vtest index 26f8a919e5c..867a5ff44e3 100644 --- a/crates/celox-test-suite/src/sv/cases/procedural.vtest +++ b/crates/celox-test-suite/src/sv/cases/procedural.vtest @@ -70,6 +70,143 @@ (assert_eq x 7) (assert_eq r 2)) +; Plain always processes may share a variable, such as a loop index; the last +; write determines its value (IEEE 1800-2023 6.5, 9.2.2.4). A process reads its +; own preceding blocking writes. Which process of one edge runs first is not +; defined, so the assertions avoid reads that depend on it. +(case edge_processes_share_a_blocking_loop_variable + (source "shared_index.sv" #" + module Top(input logic clk, input logic en, input logic [7:0] x, input logic [7:0] y, + output logic [7:0] a, output logic [7:0] b, + output integer last_a, output integer last_b, output integer prev, + output integer i_q); + integer i; + logic [7:0] ra = 8'd0; + logic [7:0] rb = 8'd0; + always @(posedge clk) begin + for (i = 0; i < 8; i = i + 1) ra[i] <= x[7 - i]; + last_a <= i; + end + always @(posedge clk) begin + prev <= i; + if (en) begin + for (i = 0; i < 4; i = i + 1) rb[i] <= y[i]; + last_b <= i; + end + end + assign a = ra; + assign b = rb; + assign i_q = i; + endmodule + "#) + (top Top) + (modify (set en 0) (set x 0x01) (set y 0xff)) + (tick clk) + (assert_eq a 0x80) + (assert_eq b 0) + (assert_eq last_a 8) + (assert_eq i_q 8) + (tick clk) + (assert_eq prev 8 "a read before the process writes sees the earlier write") + (modify (set en 1) (set x 0x80)) + (tick clk) + (assert_eq a 0x01) + (assert_eq b 0x0f) + (assert_eq last_a 8) + (assert_eq last_b 4 "a read after the loop sees the process's own write") + (assert_eq prev 8) + (modify (set en 0) (set y 0x05)) + (tick clk) + (assert_eq i_q 8 "the write stays when the other process does not write") + (assert_eq b 0x0f) + (tick clk) + (assert_eq prev 8) + (assert_eq i_q 8)) + +; Processes of different clocks share a variable. A write at one edge is +; visible to a process of a later edge, and survives a coincident edge whose +; process does not write the variable. +(case edge_processes_of_two_clocks_share_a_blocking_variable + (source "shared_clocks.sv" #" + module Top(input logic ca, input logic cb, input logic en, + output integer qa, output integer qb, output integer i_q); + integer i; + integer sa = 0; + integer sb = 0; + always @(posedge ca) begin + for (i = 0; i < 3; i = i + 1) sa = sa + i; + qa <= i + sa; + end + always @(posedge cb) begin + qb <= i; + if (en) for (i = 0; i < 5; i = i + 1) sb = sb + i; + end + assign i_q = i; + endmodule + "#) + (top Top) + (run_until 0) + (modify (set ca 0) (set cb 0) (set en 1)) + (modify (set ca 1)) + (run_until 1) + (assert_eq qa 6) + (assert_eq i_q 3) + (modify (set ca 0) (set cb 1)) + (run_until 2) + (assert_eq qb 3 "a later edge sees the other process's write") + (assert_eq i_q 5) + (modify (set cb 0) (set en 0)) + (modify (set ca 1) (set cb 1)) + (run_until 3) + (assert_eq qa 9) + (assert_eq i_q 3 "a coincident process that does not write keeps the write") + (modify (set ca 0) (set cb 0)) + (modify (set cb 1)) + (run_until 4) + (assert_eq qb 3) + (assert_eq i_q 3)) + +; The loop index of two asynchronously reset processes (from the C906 +; aq_biu_req_arbiter), which select bits of separate registers. +(case reset_processes_share_a_loop_index + (source "shared_reset_index.sv" #" + module Top(input logic clk, input logic rst_b, input logic we, + input logic [2:0] ptr, input logic sel, input logic hit, + output logic [7:0] fifo, output logic [7:0] apb); + reg [7:0] wsel_fifo; + reg [7:0] apbif_sel; + integer i; + always @(posedge clk or negedge rst_b) + if (!rst_b) + for (i = 0; i < 8; i = i + 1) wsel_fifo[i] <= 1'b0; + else + for (i = 0; i < 8; i = i + 1) + if (we & ptr == i) wsel_fifo[i] <= sel; + else wsel_fifo[i] <= wsel_fifo[i]; + always @(posedge clk or negedge rst_b) + if (!rst_b) + for (i = 0; i < 8; i = i + 1) apbif_sel[i] <= 1'b0; + else + for (i = 0; i < 8; i = i + 1) + if (we & ptr == i) apbif_sel[i] <= hit; + else apbif_sel[i] <= apbif_sel[i]; + assign fifo = wsel_fifo; + assign apb = apbif_sel; + endmodule + "#) + (top Top) + (modify (set rst_b 1) (set we 0) (set ptr 0) (set sel 0) (set hit 0)) + (modify (set rst_b 0)) + (modify (set rst_b 1)) + (assert_eq fifo 0) + (assert_eq apb 0) + (for (write position s h f a) + (list (1 3 1 0 0x08 0x00) (1 5 0 1 0x08 0x20) (0 1 1 1 0x08 0x20) (1 3 0 1 0x00 0x28)) + (modify (set we write) (set ptr position) (set sel s) (set hit h)) + (tick clk) + (assert_eq fifo f) + (assert_eq apb a))) + (case constant_functions_size_parameters_and_ports (source "constant_functions.sv" #" module Child #(parameter int unsigned ENTRIES = 2, diff --git a/crates/celox/tests/frontends/systemverilog/procedural.rs b/crates/celox/tests/frontends/systemverilog/procedural.rs index a9b5632084c..11835e6e428 100644 --- a/crates/celox/tests/frontends/systemverilog/procedural.rs +++ b/crates/celox/tests/frontends/systemverilog/procedural.rs @@ -98,6 +98,18 @@ sv_backends! { @case "procedural::mixed_blocking_and_nonblocking_writes_keep_their_regions"; } + fn edge_processes_share_a_blocking_loop_variable(sim) { + @case "procedural::edge_processes_share_a_blocking_loop_variable"; + } + + fn edge_processes_of_two_clocks_share_a_blocking_variable(sim) { + @case "procedural::edge_processes_of_two_clocks_share_a_blocking_variable"; + } + + fn reset_processes_share_a_loop_index(sim) { + @case "procedural::reset_processes_share_a_loop_index"; + } + fn constant_functions_size_parameters_and_ports(sim) { @case "procedural::constant_functions_size_parameters_and_ports"; } diff --git a/crates/celox/tests/frontends/systemverilog/review_regressions.rs b/crates/celox/tests/frontends/systemverilog/review_regressions.rs index 7c6b4f10ca6..80586dfa2ed 100644 --- a/crates/celox/tests/frontends/systemverilog/review_regressions.rs +++ b/crates/celox/tests/frontends/systemverilog/review_regressions.rs @@ -320,6 +320,48 @@ fn rejects_overlapping_always_ff_variable_drivers() { ); } +#[test] +fn rejects_loop_indices_shared_with_always_ff() { + // Only plain always processes may share a variable (IEEE 1800-2023 + // 9.2.2.4); nonblocking writes from several processes stay rejected. + for (first, second) in [ + ("always_ff", "always_ff"), + ("always_ff", "always"), + ("always", "always_ff"), + ] { + let error = cranelift_build_error(&format!( + r#" + module Top(input logic clk, input logic [3:0] a, b, output wire [3:0] y, z); + integer i; + logic [3:0] p, q; + {first} @(posedge clk) for (i = 0; i < 4; i = i + 1) p[i] <= a[i]; + {second} @(posedge clk) for (i = 0; i < 4; i = i + 1) q[i] <= b[i]; + assign y = p; + assign z = q; + endmodule + "# + )); + assert!( + error.contains("multiple variable drivers for `i`"), + "{first}/{second}: unexpected error: {error}" + ); + } + let error = cranelift_build_error( + r#" + module Top(input logic clk, a, b, output wire y); + logic q; + always @(posedge clk) q <= a; + always @(posedge clk) q = b; + assign y = q; + endmodule + "#, + ); + assert!( + error.contains("multiple variable drivers for `q`"), + "unexpected error: {error}" + ); +} + #[test] fn rejects_variables_also_written_through_output_arguments() { // A called subroutine writes its output actual for the calling process