Skip to content
Closed
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: 65 additions & 0 deletions crates/celox-frontend-sv/src/lowering/ff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7244,6 +7244,11 @@ impl Ff<'_, '_> {
}) {
return Err(unsupported("wait condition reading a static task argument"));
}
// A write of a class property does not wake a waiting process yet, so
// a condition that reads one would never be checked again.
if let Some(op) = heap_read(self.m, condition) {
return Err(unsupported(format!("{op} in a wait condition")));
}
let mut ids: Vec<_> = names
.iter()
.filter_map(|name| self.m.id(name))
Expand Down Expand Up @@ -7471,6 +7476,66 @@ fn subroutine_reads(pm: &ProcModule<'_>, expr: &sv::ir::Expr, names: &mut HashSe
}
}

/// The name of an operation in `expr` that reads the class object heap,
/// directly or through the functions it calls: a class property, or the
/// random mode of one. Container contents are values of their handles. The
/// default of an argument counts only where a call omits the argument.
fn heap_read(pm: &ProcModule<'_>, expr: &sv::ir::Expr) -> Option<String> {
let mut pending = vec![expr.clone()];
let mut calls = Vec::new();
let mut visited = HashSet::default();
let mut defaults = HashSet::default();
loop {
while let Some(expr) = pending.pop() {
let found = std::cell::RefCell::new(None);
expr_calls(&expr, &|callee| {
if let sv::ir::Callee::Object(op) = callee
&& matches!(
**op,
sv::ir::ObjectOp::Load(_)
| sv::ir::ObjectOp::ModeGet(..)
| sv::ir::ObjectOp::ConstraintModeGet(_)
)
{
found.borrow_mut().get_or_insert(op.to_string());
}
false
});
if let Some(op) = found.into_inner() {
return Some(op);
}
collect_calls(&expr, &mut calls);
}
let (callee, args) = calls.pop()?;
let sv::ir::Callee::Subroutine(name) = callee else {
continue;
};
let Some(subroutine) = pm.subroutine(&name) else {
continue;
};
for (position, param) in subroutine.params.iter().enumerate() {
if matches!(args.get(position), None | Some(None))
&& let Some(default) = &param.default
&& defaults.insert((name.clone(), position))
{
pending.push(default.clone());
}
}
if !visited.insert(name) {
continue;
}
for stmt in &subroutine.body {
let mut stmt = stmt.clone();
stmt.walk(&mut |stmt| stmt_calls(stmt, &mut calls));
stmt.visit_mut(
&mut |expr| pending.push(expr.clone()),
&mut |_| {},
&mut |_| {},
);
}
}
}

/// The name of a call in `expr` that has an effect or reads the state of the
/// simulation, directly or through the functions it calls: an event
/// expression must be a value of the variables it reads.
Expand Down
23 changes: 18 additions & 5 deletions crates/celox-napi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4343,8 +4343,12 @@ mod tests {
}

#[test]
fn native_handle_edges_of_clocks_comb_logic_does_not_read_stay_settled() {
for (seen, comb_read) in [("1'b1", false), ("clk", true)] {
fn native_handle_edges_preserve_settledness_for_unused_clocks_and_wiring() {
for (seen, settled, value) in [
("1'b1", true, Some(1)),
("clk", true, Some(0)),
("~clk", false, None),
] {
let source = format!(
"module Top (clk: input clock, count: output logic<8>, seen: output logic) {{
always_ff (clk) {{ count += 1; }}
Expand All @@ -4356,13 +4360,22 @@ mod tests {
let events: HashMap<String, u32> = serde_json::from_str(&handle.events_json()).unwrap();
let clk = events["clk"];
assert!(handle.edges(vec![clk], vec![true], None).unwrap());
// Moving `clk` without running a domain leaves combinational
// logic stale only when it reads `clk`, or already was.
// Unused clocks and direct wiring remain settled after an
// inactive edge. Other clock-dependent logic still needs a settle.
assert_eq!(
handle.edges(vec![clk], vec![false], Some(false)).unwrap(),
!comb_read,
settled,
"{seen}"
);
if let Some(value) = value {
let layout: serde_json::Value =
serde_json::from_str(&handle.layout_json()).unwrap();
let offset = layout["seen"]["offset"].as_u64().unwrap() as usize;
let (memory, len) = handle.backend.as_mut().unwrap().memory_as_mut_ptr();
assert!(offset < len);
// Read the stored result without implicitly evaluating comb.
assert_eq!(unsafe { *memory.add(offset) }, value, "{seen}");
}
assert!(handle.edges(vec![clk], vec![true], Some(true)).unwrap());
assert!(!handle.edges(vec![clk], vec![false], Some(true)).unwrap());
handle.dispose().unwrap();
Expand Down
8 changes: 8 additions & 0 deletions crates/celox-runtime/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ pub trait SimBackend {
// ── evaluation ──────────────────────────────────────────────
fn eval_comb(&mut self) -> Result<(), SimulatorErrorCode>;

/// Publish all combinational effects of a single clock-level change when
/// its cone is proven to contain only wiring. Called only from a settled
/// state, immediately after the host writes the clock. `false` must leave
/// state unchanged and requests the normal full settle.
fn eval_clock_aliases(&mut self, _event: Self::Event) -> Result<bool, SimulatorErrorCode> {
Ok(false)
}

/// Evaluate and apply a flip-flop domain for the given event.
fn eval_apply_ff_at(&mut self, event: Self::Event) -> Result<(), SimulatorErrorCode>;

Expand Down
11 changes: 7 additions & 4 deletions crates/celox-runtime/src/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,9 @@ where
};
let new = edge.level();
let fires = info.fires(known(from), edge);
let settle = dirty || info.comb_read;
let comb_read =
info.comb_read && (dirty || !executor.backend_mut().eval_clock_aliases(event)?);
let settle = dirty || comb_read;
if let Some(plan) = plan {
// A gated clock is judged from the lever's real level before the
// edge, x and z included.
Expand All @@ -381,11 +383,11 @@ where
return Ok(if settled {
EdgeOutcome::Settled
} else {
moved_only(info.comb_read)
moved_only(comb_read)
});
}
if !fires {
return Ok(moved_only(info.comb_read));
return Ok(moved_only(comb_read));
}
if settle {
executor.eval_comb_apply_ff_at(event)?;
Expand Down Expand Up @@ -552,7 +554,8 @@ where
any_moved = true;
write.write(executor.backend_mut(), info.signal, new);
transitions.push((info.signal, (old, new)));
comb_read |= info.comb_read;
comb_read |=
info.comb_read && (dirty || !executor.backend_mut().eval_clock_aliases(event)?);
let fires = info.fires(known(old), edge);
if cached {
changed.push((event, fires));
Expand Down
1 change: 1 addition & 0 deletions crates/celox-test-suite/LIMITATIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,7 @@ four-state expectations; the assertions remain enabled on all Celox variants.
| `sv_verilator_program_exit` | execute | verilator | 1 | Verilator 5.052 continues after `$exit` and keeps the program's other `initial` procedures (24.7). Icarus does not run the case; xezim 0.11.3 agrees with the expected output. |
| `sv_verilator_program_items` | compile | verilator | 2 | Verilator 5.052 rejects an `always` procedure and a module instance in a program with syntax errors, which the run cannot count as language rejections. Both are illegal (IEEE 1800-2023 24.3); Icarus 13.0 rejects them with diagnostics that name the rule. |
| `sv_icarus_iff_event_controls` | compile | icarus | 15 | Icarus 13.0 rejects `iff` in an event control with "syntax error" and "Malformed event control expression." IEEE 1800-2023 9.4.2.3 and A.6.5 permit `@(posedge clk iff en)`; [Standalone probe](verification/repros/sv_iff_event_controls_icarus.json); Verilator 5.052 runs the cases and gives the asserted counts. |
| `sv_icarus_static_property_wait` | execute | icarus | 1 | Icarus 13.0 compiles a `wait` on a static class property in a class task but vvp cannot load the result ("unresolved vvp_net reference", "Program not runnable"); IEEE 1800-2023 8.9 and 9.4.3 make the property one variable that the wait watches. [Standalone probe](verification/repros/sv_static_property_wait_icarus.json); Verilator 5.052 and xezim 0.11.3 pass the case. |
| `sv_verilator_iff_late_conditions` | execute | verilator | 4 | Verilator 5.052 judges an `iff` condition after the process that writes the event signal suspends, not at the write: with `clk = 1; en = 0;` from en = 1 it counts no event, with nonblocking updates `clk <= 1; en <= 0;` from en = 1 it counts none either, and an always_comb that writes `en = a; clk = a; en = 0;` gives no event of its `clk` write while `en` is 1. IEEE 1800-2023 9.4.2.3 judges the condition when the event occurs, and 10.4.2 applies nonblocking updates in issue order, so all three are events. [Standalone probes](verification/repros/sv_iff_late_conditions_verilator.json) print n=0, n=1 and n=0 in Verilator where the spec gives 1, 2 and 1; Icarus 13.0 rejects `iff` in event controls. |

The [system-function edge-case evidence](verification/repros/system_function_edge_cases.json)
Expand Down
25 changes: 25 additions & 0 deletions crates/celox-test-suite/src/sv/cases/class.vtest
Original file line number Diff line number Diff line change
Expand Up @@ -2806,3 +2806,28 @@ module Top(output int a, output int b);
endmodule
"#)
(top Top) (run_until 0) (assert_eq a 8) (assert_eq b 8))

(case waits_on_static_properties
; A static property is a variable of the declaring scope, so a wait in a
; method wakes when another process writes it (IEEE 1800-2023 8.9, 9.4.3).
(source "class.sv" #"
class C;
static int s_state;
task wait_for(int s);
wait (s_state >= s);
$display("reached %0d at %0d", s, $time);
endtask
endclass
module Top;
C c;
initial begin
c = new;
fork
c.wait_for(2);
begin #1 c.s_state = 1; #1 c.s_state = 2; end
join
end
endmodule
"#)
(top Top) (run_until 5)
(expect_output "reached 2 at 2\n"))
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"tool": "icarus",
"version": "Icarus Verilog version 13.0 (stable) (v13_0)",
"source": "sv_static_property_wait_icarus.sv",
"standard": "IEEE 1800-2023 8.9, 9.4.3",
"command": "iverilog -g2012 -s Top -o sp.vvp sv_static_property_wait_icarus.sv && vvp sp.vvp",
"expected_output": "reached 2 at 2",
"observations": [
{
"build_exit_code": 0,
"exit_code": 2,
"output": "unresolved vvp_net reference: v0x5555558f2f70_0\nunresolved vvp_net reference: v0x5555558f3050_0\ncompile_cleanup: 2 unresolved items\nsp.vvp: Program not runnable, 2 errors.\n"
}
],
"note": "A task of a class waits on a static property of the class, which another process writes through a handle. Icarus 13.0 compiles the design but vvp cannot load it (unresolved nets). Verilator 5.052, xezim 0.11.3 and Celox print reached 2 at 2."
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class C;
static int s_state;
task wait_for(int s);
wait (s_state >= s);
$display("reached %0d at %0d", s, $time);
endtask
endclass
module Top;
C c;
initial begin
c = new;
fork
c.wait_for(2);
begin #1 c.s_state = 1; #1 c.s_state = 2; end
join
end
endmodule
23 changes: 23 additions & 0 deletions crates/celox-test-suite/verification/sv/icarus/class.json
Original file line number Diff line number Diff line change
Expand Up @@ -1692,6 +1692,29 @@
"stronger_than_sv": false,
"tag_reasons": {},
"tags": []
},
{
"category": "Types",
"detail": "Icarus 13.0 compiles a `wait` on a static class property in a class task but vvp cannot load the result (\"unresolved vvp_net reference\", \"Program not runnable\"). IEEE 1800-2023 8.9 and 9.4.3 make the property one variable that the wait watches. Verilator 5.052 and xezim 0.11.3 pass the case.",
"expectation": "Simulation",
"known_issue": {
"id": "sv_icarus_static_property_wait",
"category": "simulator_runtime_limitation",
"phase": "execute",
"observed_version": "Icarus Verilog version 13.0 (stable) (v13_0)",
"reason": "Icarus 13.0 compiles a `wait` on a static class property in a class task but vvp cannot load the result (\"unresolved vvp_net reference\", \"Program not runnable\"). IEEE 1800-2023 8.9 and 9.4.3 make the property one variable that the wait watches. Verilator 5.052 and xezim 0.11.3 pass the case.",
"upstream": [],
"evidence": [
"verification/repros/sv_static_property_wait_icarus.json",
"LIMITATIONS.md#systemverilog-suite"
]
},
"name": "class::waits_on_static_properties",
"phase": "skip",
"status": "ignored",
"stronger_than_sv": false,
"tag_reasons": {},
"tags": []
}
]
}
17 changes: 17 additions & 0 deletions crates/celox-test-suite/verification/sv/limitations.json
Original file line number Diff line number Diff line change
Expand Up @@ -2769,6 +2769,23 @@
]
}
},
{
"id": "sv_icarus_static_property_wait",
"category": "simulator_runtime_limitation",
"phase": "execute",
"observed_version": "Icarus Verilog version 13.0 (stable) (v13_0)",
"reason": "Icarus 13.0 compiles a `wait` on a static class property in a class task but vvp cannot load the result (\"unresolved vvp_net reference\", \"Program not runnable\"). IEEE 1800-2023 8.9 and 9.4.3 make the property one variable that the wait watches. Verilator 5.052 and xezim 0.11.3 pass the case.",
"upstream": [],
"evidence": [
"verification/repros/sv_static_property_wait_icarus.json",
"LIMITATIONS.md#systemverilog-suite"
],
"cases": {
"icarus": [
"class::waits_on_static_properties"
]
}
},
{
"id": "sv_verilator_iff_late_conditions",
"category": "simulator_disagreement",
Expand Down
11 changes: 11 additions & 0 deletions crates/celox-test-suite/verification/sv/verilator/class.json
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,17 @@
"stronger_than_sv": false,
"tag_reasons": {},
"tags": []
},
{
"category": "Types",
"detail": "",
"expectation": "Simulation",
"name": "class::waits_on_static_properties",
"phase": "execute",
"status": "passed",
"stronger_than_sv": false,
"tag_reasons": {},
"tags": []
}
]
}
11 changes: 11 additions & 0 deletions crates/celox-test-suite/verification/sv/xezim/class.json
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,17 @@
"stronger_than_sv": false,
"tag_reasons": {},
"tags": []
},
{
"category": "Types",
"detail": "",
"expectation": "Simulation",
"name": "class::waits_on_static_properties",
"phase": "execute",
"status": "passed",
"stronger_than_sv": false,
"tag_reasons": {},
"tags": []
}
]
}
Loading
Loading