From 6db33adff2feab12482a768e923e4e864abbba09 Mon Sep 17 00:00:00 2001 From: tignear Date: Sun, 25 Oct 2026 22:30:50 +0900 Subject: [PATCH] perf(sv): publish only the written elements of an array another process writes A process's nonblocking writes to an unpacked array that another process also writes, such as a testbench loading a RAM through hierarchical references, are staged in a value and a mask plane so that the other process's writes in between survive (IEEE 1800-2023 10.4.2). Their merge looped over every element on every edge, and every evaluation reset the whole mask plane. On rtlmeter C906 that is 16 x 524288 element merges per clock edge, 4.6 s per edge on the interpreter. When each nonblocking write of such an array is a select that runs at most once an evaluation (none is in a loop, a concatenation, a whole-array or a slice write), each write now also records the bit range it wrote and that it ran. The merge publishes only the elements those ranges cover and clears their mask bits, so an edge costs its writes. Elements another write already merged find their mask clear, and an out-of-range write marked no mask bits. Other arrays merge every element as before. Closes #2020 Change-Id: Iaa55fbe4ea0ab9588872ac23384812d151dba56a --- crates/celox-frontend-sv/src/lowering/ff.rs | 325 ++++++++++++++++-- .../sv/cases/hierarchical_references.vtest | 56 +++ .../systemverilog/hierarchical_references.rs | 3 + 3 files changed, 360 insertions(+), 24 deletions(-) diff --git a/crates/celox-frontend-sv/src/lowering/ff.rs b/crates/celox-frontend-sv/src/lowering/ff.rs index d399b4a9628..7c9c153da7c 100644 --- a/crates/celox-frontend-sv/src/lowering/ff.rs +++ b/crates/celox-frontend-sv/src/lowering/ff.rs @@ -4362,13 +4362,43 @@ impl<'p, 'a> Ff<'p, 'a> { value_name, mask, mask_name, + sites: None, } }) .collect(); + // Publish only the elements an array's nonblocking writes wrote, so + // an edge costs its writes rather than the whole array. + let mut staged = staged; + if self.kernel.is_none() && !self.direct { + for stage in &mut staged { + if self.m.var(stage.id).array_dims.is_empty() { + continue; + } + let Some(count) = array_nba_site_count(body, stage.id, self.m) else { + continue; + }; + let sites = (0..count) + .map(|_| { + let (lsb, lsb_name) = self.m.temp("nba_lsb", 64, false, false); + let (msb, msb_name) = self.m.temp("nba_msb", 64, false, false); + let (hit, hit_name) = self.m.temp("nba_hit", 1, false, false); + NbaArraySite { + lsb, + lsb_name, + msb, + msb_name, + hit, + hit_name, + } + }) + .collect(); + stage.sites = Some(sites); + } + } let body = if staged.is_empty() { body.to_vec() } else { - stage_nonblocking(body, &staged, self.m) + stage_nonblocking(body, &staged, self.m, &mut HashMap::default()) }; // A variable staged for an exact publication has no next state of // its own: only its merge publishes it. A shared variable is written @@ -4390,6 +4420,8 @@ impl<'p, 'a> Ff<'p, 'a> { let zero = self.b.alloc_bit(width, false); self.b.emit(SIRInstruction::Imm(zero, SIRValue::new(0u8))); self.store(stage.mask, SIROffset::Static(0), width, zero); + } else if stage.sites.is_some() { + // The merge clears the mask bits it published. } else { // Nothing writes the mask's stable state, which stays zero; // a store may not span the elements of an array. @@ -4552,6 +4584,87 @@ struct NbaStage { value_name: String, mask: SourceVarId, mask_name: String, + /// For an unpacked array, the sites of its nonblocking writes, when each + /// runs at most once an evaluation: the merge then publishes only the + /// elements they wrote. Without them every element is merged. + sites: Option>, +} + +/// A nonblocking write of a staged unpacked array: the bit range it wrote, +/// in the array's flattened bits, and whether it ran. +struct NbaArraySite { + lsb: SourceVarId, + lsb_name: String, + msb: SourceVarId, + msb_name: String, + hit: SourceVarId, + hit_name: String, +} + +/// The number of nonblocking writes of the unpacked array `id` in `stmts`, +/// when each is a select [`stage_nonblocking`] stages and none is in a loop, +/// so that each runs at most once an evaluation. +fn array_nba_site_count( + stmts: &[sv::ir::Stmt], + id: SourceVarId, + pm: &ProcModule<'_>, +) -> Option { + let writes = |stmt: &sv::ir::Stmt| { + let mut found = false; + stmt.walk(&mut |stmt| match stmt { + sv::ir::Stmt::Assign { + lhs, + nonblocking: true, + .. + } => found |= pm.id(lhs.name()) == Some(id), + sv::ir::Stmt::AssignConcat { + parts, + nonblocking: true, + .. + } => found |= parts.iter().any(|part| pm.id(part.name()) == Some(id)), + _ => {} + }); + found + }; + let mut count = 0; + for stmt in stmts { + match stmt { + sv::ir::Stmt::Assign { + lhs, + nonblocking: true, + .. + } if pm.id(lhs.name()) == Some(id) => match lhs { + sv::ir::LValue::Select { + msb, + lsb, + array_slice_width: None, + .. + } if expr_from_const_expr(msb).is_some() && expr_from_const_expr(lsb).is_some() => { + count += 1; + } + _ => return None, + }, + sv::ir::Stmt::If { + then_body, + else_body, + .. + } => { + count += array_nba_site_count(then_body, id, pm)?; + count += array_nba_site_count(else_body, id, pm)?; + } + sv::ir::Stmt::Case { items, default, .. } => { + for item in items { + count += array_nba_site_count(&item.body, id, pm)?; + } + if let Some(default) = default { + count += array_nba_site_count(default, id, pm)?; + } + } + other if writes(other) => return None, + _ => {} + } + } + Some(count) } /// `stmts` with every nonblocking assignment to a staged variable writing @@ -4560,6 +4673,7 @@ fn stage_nonblocking( stmts: &[sv::ir::Stmt], staged: &[NbaStage], pm: &ProcModule<'_>, + next_site: &mut HashMap, ) -> Vec { let stage_of = |lvalue: &sv::ir::LValue| { staged @@ -4596,6 +4710,29 @@ fn stage_nonblocking( nonblocking: true, } if stage_of(lhs).is_some() => { let stage = stage_of(lhs).unwrap(); + if let (Some(sites), sv::ir::LValue::Select { msb, lsb, .. }) = (&stage.sites, lhs) + { + let index = next_site.entry(stage.id).or_default(); + let site = &sites[*index]; + *index += 1; + let record = |name: &str, rhs: sv::ir::Expr| sv::ir::Stmt::Assign { + lhs: sv::ir::LValue::Ident(name.to_string()), + rhs, + nonblocking: false, + }; + out.push(record( + &site.lsb_name, + expr_from_const_expr(lsb).expect("a staged site's bounds are expressions"), + )); + out.push(record( + &site.msb_name, + expr_from_const_expr(msb).expect("a staged site's bounds are expressions"), + )); + out.push(record( + &site.hit_name, + sv::ir::Expr::Literal("1'b1".to_string()), + )); + } out.push(sv::ir::Stmt::Assign { lhs: renamed(lhs, &stage.value_name), rhs: rhs.clone(), @@ -4631,8 +4768,8 @@ fn stage_nonblocking( else_body, } => out.push(sv::ir::Stmt::If { condition: condition.clone(), - then_body: stage_nonblocking(then_body, staged, pm), - else_body: stage_nonblocking(else_body, staged, pm), + then_body: stage_nonblocking(then_body, staged, pm, next_site), + else_body: stage_nonblocking(else_body, staged, pm, next_site), }), sv::ir::Stmt::Case { kind, @@ -4646,12 +4783,12 @@ fn stage_nonblocking( .iter() .map(|item| sv::ir::CaseItem { labels: item.labels.clone(), - body: stage_nonblocking(&item.body, staged, pm), + body: stage_nonblocking(&item.body, staged, pm, next_site), }) .collect(), default: default .as_ref() - .map(|body| stage_nonblocking(body, staged, pm)), + .map(|body| stage_nonblocking(body, staged, pm, next_site)), }), sv::ir::Stmt::Loop { kind, @@ -4664,7 +4801,7 @@ fn stage_nonblocking( init: init.clone(), condition: condition.clone(), step: step.clone(), - body: stage_nonblocking(body, staged, pm), + body: stage_nonblocking(body, staged, pm, next_site), }), other => out.push(other.clone()), } @@ -4682,7 +4819,14 @@ fn emit_nba_merges(builder: &mut SIRBuilder, staged: &[NbaStage], pm: &Pro if let Some(element_width) = unpacked_element_width(variable).filter(|_| !variable.array_dims.is_empty()) { - emit_array_nba_merge(builder, stage, variable, element_width); + match &stage.sites { + Some(sites) => { + for site in sites { + emit_array_site_merge(builder, stage, site, variable, element_width); + } + } + None => emit_array_nba_merge(builder, stage, variable, element_width), + } continue; } let width = variable.width; @@ -4746,41 +4890,81 @@ fn emit_nba_merges(builder: &mut SIRBuilder, staged: &[NbaStage], pm: &Pro } } -/// [`emit_nba_merges`] for an unpacked array: a loop merges each element, -/// since an access may not span elements. -fn emit_array_nba_merge( +/// [`emit_nba_merges`] for one write site of an unpacked array: when it ran, +/// a loop merges the elements its bit range covers, and clears their mask +/// bits and its hit for the next evaluation. Another site that wrote the +/// same element merged it already, and then finds its mask clear. +fn emit_array_site_merge( builder: &mut SIRBuilder, stage: &NbaStage, + site: &NbaArraySite, variable: &SvVariable, element_width: usize, ) { let count = variable.width / element_width; - let four_state = variable.is_4state; - let alloc = |builder: &mut SIRBuilder| { - if four_state { - builder.alloc_logic(element_width) - } else { - builder.alloc_bit(element_width, false) - } + let hit = builder.alloc_bit(1, false); + builder.emit(SIRInstruction::Load( + hit, + working(site.hit), + SIROffset::Static(0), + 1, + )); + let first = builder.new_block(); + let done = builder.new_block(); + let next = builder.new_block(); + builder.seal_block(SIRTerminator::Branch { + cond: hit, + true_block: (first, Vec::new()), + false_block: (next, Vec::new()), + }); + + builder.switch_to_block(first); + let load = |builder: &mut SIRBuilder, id| { + let value = builder.alloc_bit(64, false); + builder.emit(SIRInstruction::Load( + value, + working(id), + SIROffset::Static(0), + 64, + )); + value }; - let first = builder.alloc_bit(64, false); - builder.emit(SIRInstruction::Imm(first, SIRValue::new(0u8))); + let lsb = load(builder, site.lsb); + let msb = load(builder, site.msb); + let reversed = builder.alloc_bit(1, false); + builder.emit(SIRInstruction::Binary(reversed, msb, BinaryOp::LtU, lsb)); + let low = builder.alloc_bit(64, false); + builder.emit(SIRInstruction::Mux(low, reversed, msb, lsb)); + let high = builder.alloc_bit(64, false); + builder.emit(SIRInstruction::Mux(high, reversed, lsb, msb)); + let width = builder.alloc_bit(64, false); + builder.emit(SIRInstruction::Imm( + width, + SIRValue::new(element_width as u64), + )); + let start = builder.alloc_bit(64, false); + builder.emit(SIRInstruction::Binary(start, low, BinaryOp::DivU, width)); + let end = builder.alloc_bit(64, false); + builder.emit(SIRInstruction::Binary(end, high, BinaryOp::DivU, width)); let index = builder.alloc_bit(64, false); let header = builder.new_block_with(vec![index]); let body_index = builder.alloc_bit(64, false); let body = builder.new_block_with(vec![body_index]); - let exit = builder.new_block(); - builder.seal_block(SIRTerminator::Jump(header, vec![first])); + builder.seal_block(SIRTerminator::Jump(header, vec![start])); builder.switch_to_block(header); let limit = builder.alloc_bit(64, false); builder.emit(SIRInstruction::Imm(limit, SIRValue::new(count as u64))); + let inside = builder.alloc_bit(1, false); + builder.emit(SIRInstruction::Binary(inside, index, BinaryOp::LtU, limit)); + let covered = builder.alloc_bit(1, false); + builder.emit(SIRInstruction::Binary(covered, index, BinaryOp::LeU, end)); let more = builder.alloc_bit(1, false); - builder.emit(SIRInstruction::Binary(more, index, BinaryOp::LtU, limit)); + builder.emit(SIRInstruction::Binary(more, inside, BinaryOp::And, covered)); builder.seal_block(SIRTerminator::Branch { cond: more, true_block: (body, vec![index]), - false_block: (exit, Vec::new()), + false_block: (done, Vec::new()), }); builder.switch_to_block(body); @@ -4790,6 +4974,60 @@ fn emit_array_nba_merge( bit_offset: 0, dynamic_bit_offset: None, }; + emit_element_merge(builder, stage, variable, element_width, &offset); + let clear = builder.alloc_bit(element_width, false); + builder.emit(SIRInstruction::Imm(clear, SIRValue::new(0u8))); + builder.emit(SIRInstruction::Store( + working(stage.mask), + offset, + element_width, + clear, + Vec::new(), + Vec::new(), + )); + let one = builder.alloc_bit(64, false); + builder.emit(SIRInstruction::Imm(one, SIRValue::new(1u8))); + let following = builder.alloc_bit(64, false); + builder.emit(SIRInstruction::Binary( + following, + body_index, + BinaryOp::Add, + one, + )); + builder.seal_block(SIRTerminator::Jump(header, vec![following])); + + builder.switch_to_block(done); + let unset = builder.alloc_bit(1, false); + builder.emit(SIRInstruction::Imm(unset, SIRValue::new(0u8))); + builder.emit(SIRInstruction::Store( + working(site.hit), + SIROffset::Static(0), + 1, + unset, + Vec::new(), + Vec::new(), + )); + builder.seal_block(SIRTerminator::Jump(next, Vec::new())); + + builder.switch_to_block(next); +} + +/// Merge the staged nonblocking value of one element of an unpacked array +/// into its stable state, in the bits its mask marks. +fn emit_element_merge( + builder: &mut SIRBuilder, + stage: &NbaStage, + variable: &SvVariable, + element_width: usize, + offset: &SIROffset, +) { + let alloc = |builder: &mut SIRBuilder| { + if variable.is_4state { + builder.alloc_logic(element_width) + } else { + builder.alloc_bit(element_width, false) + } + }; let current = alloc(builder); builder.emit(SIRInstruction::Load( current, @@ -4821,12 +5059,51 @@ fn emit_array_nba_merge( builder.emit(SIRInstruction::Binary(merged, kept, BinaryOp::Or, taken)); builder.emit(SIRInstruction::Store( stable(stage.id), - offset, + offset.clone(), element_width, merged, Vec::new(), Vec::new(), )); +} + +/// [`emit_nba_merges`] for an unpacked array: a loop merges each element, +/// since an access may not span elements. +fn emit_array_nba_merge( + builder: &mut SIRBuilder, + stage: &NbaStage, + variable: &SvVariable, + element_width: usize, +) { + let count = variable.width / element_width; + let first = builder.alloc_bit(64, false); + builder.emit(SIRInstruction::Imm(first, SIRValue::new(0u8))); + let index = builder.alloc_bit(64, false); + let header = builder.new_block_with(vec![index]); + let body_index = builder.alloc_bit(64, false); + let body = builder.new_block_with(vec![body_index]); + let exit = builder.new_block(); + builder.seal_block(SIRTerminator::Jump(header, vec![first])); + + builder.switch_to_block(header); + let limit = builder.alloc_bit(64, false); + builder.emit(SIRInstruction::Imm(limit, SIRValue::new(count as u64))); + let more = builder.alloc_bit(1, false); + builder.emit(SIRInstruction::Binary(more, index, BinaryOp::LtU, limit)); + builder.seal_block(SIRTerminator::Branch { + cond: more, + true_block: (body, vec![index]), + false_block: (exit, Vec::new()), + }); + + builder.switch_to_block(body); + let offset = SIROffset::Element { + index: body_index, + element_width, + bit_offset: 0, + dynamic_bit_offset: None, + }; + emit_element_merge(builder, stage, variable, element_width, &offset); let one = builder.alloc_bit(64, false); builder.emit(SIRInstruction::Imm(one, SIRValue::new(1u8))); let next = builder.alloc_bit(64, false); diff --git a/crates/celox-test-suite/src/sv/cases/hierarchical_references.vtest b/crates/celox-test-suite/src/sv/cases/hierarchical_references.vtest index 6f9d257fce0..a37ebc973b3 100644 --- a/crates/celox-test-suite/src/sv/cases/hierarchical_references.vtest +++ b/crates/celox-test-suite/src/sv/cases/hierarchical_references.vtest @@ -83,6 +83,62 @@ (run_to_finish) (expect_output "q=8616\nlo2=ef hi2=be hi7=80\nq=be5a\n")) +; An array a process writes by nonblocking element writes and another +; process writes through hierarchical names publishes only the elements +; written: in the same step, the other process's writes stay in the bits a +; nonblocking write did not write, and a nonblocking write to the same bits +; lands after them; an out-of-range write writes nothing (IEEE 1800-2023 +; 7.4.5, 10.4.2). +(case nonblocking_element_writes_keep_hierarchical_writes_of_the_same_step + (source "site_merge.sv" #" + module Ram(input logic clk, input logic [1:0] op, input logic [4:0] addr, + input logic [7:0] d, output logic [7:0] q); + logic [7:0] mem [15:0]; + always @(posedge clk) begin + case (op) + 2'd1: mem[addr] <= d; + 2'd2: mem[addr][3:0] <= d[3:0]; + default: q <= mem[addr]; + endcase + end + endmodule + module Top; + logic clk = 0; + logic [1:0] op = 0; + logic [4:0] addr = 0; + logic [7:0] d = 0; + logic [7:0] q; + integer i; + Ram ram(.clk(clk), .op(op), .addr(addr), .d(d), .q(q)); + task automatic step; + #5 clk = 1; + #5 clk = 0; + endtask + initial begin + for (i = 0; i < 16; i = i + 1) ram.mem[i] = 8'hb0 + i; + op = 1; addr = 2; d = 8'h5a; + #5 clk = 1; ram.mem[3] = 8'hc3; #5 clk = 0; + op = 2; addr = 4; d = 8'h0e; + #5 clk = 1; ram.mem[4][7:4] = 4'h9; #5 clk = 0; + op = 1; addr = 7; d = 8'h77; + #5 clk = 1; ram.mem[7] = 8'h11; #5 clk = 0; + op = 1; addr = 20; d = 8'hee; + step(); + op = 0; + for (i = 0; i < 9; i = i + 1) begin + addr = i; + step(); + $display("%0d=%h", i, q); + end + $display("15=%h", ram.mem[15]); + $finish; + end + endmodule + "#) + (top Top) + (run_to_finish) + (expect_output "0=b0\n1=b1\n2=5a\n3=c3\n4=9e\n5=b5\n6=b6\n7=77\n8=b8\n15=bf\n")) + ; A component may name an element of an instance array or an instance of a ; generate block. (case names_instance_array_elements_and_generated_instances diff --git a/crates/celox/tests/frontends/systemverilog/hierarchical_references.rs b/crates/celox/tests/frontends/systemverilog/hierarchical_references.rs index 828aca9bfa2..aefd0000696 100644 --- a/crates/celox/tests/frontends/systemverilog/hierarchical_references.rs +++ b/crates/celox/tests/frontends/systemverilog/hierarchical_references.rs @@ -7,6 +7,9 @@ sv_backends! { fn writes_memory_elements_of_instances_below(sim) { @case "hierarchical_references::writes_memory_elements_of_instances_below"; } + fn nonblocking_element_writes_keep_hierarchical_writes_of_the_same_step(sim) { + @case "hierarchical_references::nonblocking_element_writes_keep_hierarchical_writes_of_the_same_step"; + } fn names_instance_array_elements_and_generated_instances(sim) { @case "hierarchical_references::names_instance_array_elements_and_generated_instances"; }