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
65 changes: 62 additions & 3 deletions crates/celox-frontend-sv/src/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, i128>,
parameter_types: &HashMap<String, (usize, bool)>,
) -> HashSet<String> {
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
Expand Down Expand Up @@ -1399,7 +1453,7 @@ fn validate_variable_driver_ranges(
module: &sv::ir::Module,
constants: &HashMap<String, i128>,
parameter_types: &HashMap<String, (usize, bool)>,
shared_memories: &HashSet<String>,
shared: &HashSet<String>,
) -> Result<(), sv::AnalyzerError> {
for port in module
.ports()
Expand Down Expand Up @@ -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}`"
)));
Expand Down Expand Up @@ -3303,7 +3357,9 @@ fn lower_module_with_overrides(
foreign_writes,
warnings,
);
validate_variable_driver_ranges(module, &constants, &parameter_types, &shared_memories)?;
let shared_variables = shared_edge_variables(module, &constants, &parameter_types);
let shared_all = shared_memories.union(&shared_variables).cloned().collect();
validate_variable_driver_ranges(module, &constants, &parameter_types, &shared_all)?;
validate_foreign_drivers(
module,
&constants,
Expand Down Expand Up @@ -3651,6 +3707,7 @@ fn lower_module_with_overrides(
&ff_event_roles,
&shared_memories,
foreign_writes,
&shared_variables,
&mut repeated_ff_writes,
)?;
(
Expand Down Expand Up @@ -9141,6 +9198,7 @@ fn lower_ff_processes(
ff_event_roles: &[FfEventRole],
shared_memories: &HashSet<String>,
foreign_writes: &HashMap<String, hierarchy::ForeignWrites>,
shared_variables: &HashSet<String>,
repeated_writes: &mut Vec<FfWriteStream>,
) -> Result<SvFfBlocks, sv::AnalyzerError> {
let mut parallel_ff_parts = HashMap::<_, Vec<_>>::default();
Expand Down Expand Up @@ -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())?;
Expand Down
37 changes: 31 additions & 6 deletions crates/celox-frontend-sv/src/lowering/ff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SourceVarId>,
/// Variables several edge processes write by blocking assignments (see
/// `with_shared_variables`).
shared: HashSet<SourceVarId>,
b: SIRBuilder<Addr>,
/// Variables read from the working region.
working: HashSet<SourceVarId>,
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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<String>) -> 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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
137 changes: 137 additions & 0 deletions crates/celox-test-suite/src/sv/cases/procedural.vtest
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 12 additions & 0 deletions crates/celox/tests/frontends/systemverilog/procedural.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Expand Down
Loading
Loading