Skip to content

fix(sv): count intermediate writes of combinational and flip-flop units - #1726

Merged
mergify[bot] merged 9 commits into
masterfrom
feature/fix/sv-intermediate-write-events
Oct 22, 2026
Merged

mergify[bot] merged 9 commits into
masterfrom
feature/fix/sv-intermediate-write-events

Conversation

@tignear

@tignear tignear commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1706. Part of #1123 (dynamic primitive: occurrence counters at store time).

Problem

A process waiting on @(y) missed a value that one evaluation of an always_comb, a blocking edge-triggered always or an always_ff writes and then overwrites (y = 0; if (a) y = 1;, y <= 0; if (a) y <= 1;). Every store is an update event that triggers the waiter (IEEE 1800-2023 4.3, 4.5, 9.4.2), and nonblocking updates apply in the NBA region in issue order, each its own update (10.4.2). always @* already counted them.

Why only always @* worked:

  • always @* is lowered to a gated imperative unit, whose repeated stores get counter slots and are instrumented in assembly.
  • always_comb is lowered symbolically. y = 0; if (a) y = 1; becomes one expression, so the intermediate store no longer exists when assembly runs (O0 and O1 alike).
  • Flip-flop bodies write the next state in the working region and publish it with one commit. Nothing searched them for candidates or instrumented them, so only the final value was visible.

Change

Everything below applies only in a design with at least one wait. A design without one, such as pure RTL, lowers exactly as before.

  • always_comb: a process that may store a variable twice is lowered both ways: symbolically, as today, and as a gated imperative unit.
    • The imperative unit reruns on input value changes only, so the units that write its inputs stay unchanged.
    • Assembly keeps the unit for an instance in which such a variable is counted (a wait term or a flip-flop event), and the symbolic definitions otherwise.
    • If the imperative lowering finds no candidate, it is rolled back (ProcModule::checkpoint/rollback), so nothing of the attempt remains.
    • A process with runtime events ($display, assertions) keeps only its symbolic form.
  • Flip-flops (watch::PendingSlots): an evaluation that may write a signal twice holds the occurrences back until the commit that publishes them. So a waiter wakes when the update is applied (the active region for blocking writes, the NBA region for nonblocking ones), and each update counts in issue order.
    • Each write is also applied to a private copy of the stream, and a mask marks the bits written so far. The copy is independent of the next state, which seeds and other parts also write.
    • A write that changes already-covered bits is an occurrence, whatever value is published later. It marks its bits as changed, and an edge of the LSB is held as a rise or a fall.
    • A write of not-yet-covered bits keeps its value as the first one. The commit compares it with the value it publishes over: for a staged variable that is the final blocking value, not the next state at the time of the write.
    • Each commit counts and releases only the bits it publishes. Units of one evaluation (fused eval/apply, split active/NBA commits, parallel evaluate/apply parts in one lane) therefore share the stream without clearing each other's held writes.
    • A unit starts its own bits of a stream empty at entry: the bits it seeds into the next state, or all of a staging value, which only one process writes. The host can continue after a runtime error ($fatal, a non-progressing loop, a runtime limit) that ended an evaluation before its commit, and the next evaluation then starts clean. Other parts of the same evaluation keep what they hold. timing::runtime_error_before_a_commit_leaves_no_held_writes (a Rust test, since the vtest DSL cannot express a resumed runtime error) hits a $fatal between two writes of a watched y, runs on, and asserts no waiter wakes, on native, cranelift and both parallel backends. Without the entry clear it fails with n=1.
    • A waiter compares a counter only with the value it saw (counter != seen), so a commit adds one change for all the changes it releases. Rises and falls are counted exactly.
    • A counted signal that the flip-flop processes of one trigger write once each is counted at each commit; see "Writers of one trigger" below.
  • always_comb alternative paths: an instance that keeps the imperative unit removes only the symbolic paths that process produced: any path that overlaps one of its target ranges. Flattening may have split a definition at the boundaries of its readers (assign w = y[1:0]), so a piece of a range counts. No other driver can write those bits, since validate_variable_driver_ranges rejects overlapping drivers. A continuous assignment or another always_comb driving other bits of the same variable keeps its paths.
  • Marking: none was needed. Instrumentation is ordinary SIR emitted before any pass, and each counter bump is load/compare/store dataflow on the counter, so a pass that forwards or kills the intermediate store still keeps the count. The new cases pass at O0 and O1.

Every unit that writes no counted address is unchanged.

Semantics checked

The cases were verified with verify-sv-icarus and verify-sv-verilator --fresh, and Celox agrees with Icarus on all of them except the port case:

  • intermediate_combinational_writes_wake_waiters: always_comb and always @*, comb=3 star=3.
  • intermediate_edge_process_writes_wake_waiters: blocking edge always, always_ff NBA, a constant y = 0; y = 1;, plus @(posedge y) and @(negedge y) counts: blk=3 nba=3 const=3 rise=3 fall=2. The processes share one clock, so the parallel backends run them as separate evaluate/apply parts.
  • staged_nonblocking_writes_wake_waiters: mixed blocking + NBA staging, whole-variable and partial (z[0] <= …), y=3 z=3.
  • intermediate_combinational_writes_keep_other_drivers: the counted always_comb drives y[3:0], assign y[7:4] and another always_comb drive the rest: y=a1 z=f1 n=2 m=3.
  • parallel_flip_flop_parts_keep_pulses: one always_ff pulses y[0] back to its value, two others write y[2]/y[1] unchanged, plus independent work so the parallel build splits the processes into parts in one lane: y=001 n=2 f=2 on all four backends.
  • staged_nonblocking_writes_follow_later_blocking_writes: y[1] <= 0; y[0] <= 1; y = 2'b00;. The NBA update of y[0] rises from the final blocking value 0, which a @(posedge y) waiter sees: r=2.
  • intermediate_combinational_writes_replace_split_definitions: assign w = y[1:0] splits the counted variable's symbolic definition; every piece is replaced by the unit: y=0111 w=11 n=3 (before the fix: a multiple-driver build error).
  • intermediate_combinational_writes_reach_concatenated_ports: the counted variable is an output port connected to {p, q}: y=1001 n=3, p=10 q=01.
  • child_port_pulses_reach_waiters_settled: a pulse behind a child's output port. The port is a continuous assignment (23.3.3) evaluated after the child yields, so missing the pulse is legal (4.7). Celox and Verilator give 1; Icarus collapses the port and gives 3.

Exclusions, each with a standalone repro:

  • sv_verilator_intermediate_writes: the six cases on intermediate writes (all above except the split-definition and the two port cases) for Verilator 5.052, which sees only final values (comb=1 star=1, blk=1 nba=1 const=1 rise=1 fall=0, y=0 z=0, n=1 m=2, n=0 f=0, r=0).
  • sv_icarus_port_pulses: the port case for Icarus 13.0.

Writers of one trigger

Pending streams are chosen per process, before the processes of one trigger are merged. So two processes that each write disjoint bits of y once get no stream, and each process's commit is counted on its own (emit_watched around each Commit). No result can be wrong:

  • Overlapping bits are rejected ("multiple variable drivers"), so each bit has one writer, written once in the evaluation, and changes at most once.
  • Each commit publishes one process's bits and is counted against the value just before it. That is the sequence of updates in process order, which 4.7 allows: per-bit values, LSB edges and pulse-and-return all match the per-update view. A pulse needs two writes of the same bits by one process, and that process gets a stream.
  • When one process does have a stream for y, the single writes and commits of the other processes join it (see above).

Not in scope, as the issue says: a pulse that reaches a waiter only through comb logic evaluated after the writer yields (assign y = a with a = 1; a = 0;). The pulse/pulse2 probes are unchanged.

Found on the way and filed as #1721 (P1, separate PR): a kernel woken by an edge process's blocking write runs after that edge's NBA updates (master too). The staged case starts y/z at their blocking values so it does not depend on that.

Perf

Base and branch binaries are both built from e06cf24. The branch was later rebased onto 3430315 (#1716 docs and #1679).

SIR: for all 538 SV suite designs (437 untimed, 101 timed), a dump of the post-optimized SIR plus the runtime schema is byte-identical between base and branch. The base dumps were stable across two runs. Only the 4 new cases differ, and they are new.

Review fixes: a dump of the post-optimized SIR plus the runtime schema of every suite design, before (7300779) and after the review fixes, differs only in this PR's own cases: the two earlier ones whose flip-flops write a watched variable twice, and the three new ones. Every other design (576) is byte-identical.

State layout: designs with a wait and a flip-flop or always_comb stream that may be written twice can get hidden pending/counter objects even when nothing watches that variable, so their state layout is not byte-identical. Their SIR behavior is unchanged: no unit is instrumented unless the variable is counted.

Runtime, callgrind: local gate, not committed. It is 64 lanes of always_ff accumulators with an XOR-reduction always_comb, driven by add_clock. Method: whole program at 200,000 cycles minus at 20,000, five runs, median.

design base runs base median branch runs branch median Δ
(a) pure RTL 1178742829, 1178640045, 1178640045, 1178640211, 1178640045 1,178,640,045 1178639479, 1178639474, 1178624559, 1178639479, 1178639479 1,178,639,479 -0.00%
(b1) + initial forever @(posedge clk) stim = stim + 3 1563300034, 1563300034, 1563300034, 1563300034, 1563300034 1,563,300,034 1563160595, 1563300020, 1563300020, 1563300020, 1563300020 1,563,300,020 -0.00%
(b5) same stimulus with <= 2123977902, 2123977892, 2123977902, 2123977902, 2123977902 2,123,977,902 2123977888, 2123977888, 2123977888, 2123977888, 2123977893 2,123,977,888 -0.00%
(c) b1 + watched always_comb (w = 0; if (stim[0]) w = 1;, @(w)) 2179080003, 2179080003, 2179080003, 2179110112, 2179080003 2,179,080,003 3184739272, 3185413292, 3185413292, 3185413292, 3186098556 3,185,413,292 +46.18%
(cs) c written with always @* (existing path) 3186461038, 3186542218, 3186540034, 3187873900, 3187873900 3,186,542,218 3186564354, 3186564354, 3186540020, 3187287723, 3186540020 3,186,564,354 +0.00%
  • (a), (b1), (b5): equal (within 0.01%). (b1) and (b5) take the double lowering of the sum always_comb and keep its symbolic form; in an earlier pair of binaries (both from 144bbcc) their per-function JIT-code Ir was identical to base.
  • (c): the watched variable now runs as a gated imperative unit with counters. The branch costs the same as (cs), which is the same design written with always @* on master: the existing imperative path. The cost is not in the RTL or the new SIR. It is in the runtime's check of a counter-baseline wait, which reads the signal into a Value (BigUint) at every observation: check_waits, read_signal_plane and BigUint conversions. A follow-up perf PR can compare counter baselines on raw planes, as Baseline::Raw already does, for always @* too. That changes the public celox_runtime::timing::Baseline, so I left it out of this fix.

Compile time (callgrind Ir of building the gate design, 0 cycles):

design base branch Δ
(a) no wait 1,148,234,605 1,148,178,089 -0.0%
(b1) double-lowered, symbolic kept 1,203,443,937 1,392,074,072 +15.7%
(c) double-lowered, unit kept 1,217,026,592 1,432,638,790 +17.7%
(cs) always @* + the sum always_comb 1,242,025,449 1,430,691,221 +15.2%

The increase is the second lowering of the 64-iteration sum always_comb (Ff::lower_comb_process, 188M Ir); the candidate analysis is negligible. The whole SV suite (538 designs, built and dumped): user CPU 3.46–3.52 s on base vs 3.47–3.56 s on branch over three runs each, equal within noise (the branch also builds the 4 new cases).

Validation

  • Scoped tests on the final base (32f5b06), with CI's systemverilog feature: 5676 passed, 0 failed.
    • celox-frontend-core and celox-frontend-sv unit tests.
    • celox --lib, including the golden call traces: executor_call_traces_are_unchanged, …_of_veryl_clock_domains_… and sv_traces::…_of_sv_designs_… pass unchanged, so there is nothing to explain.
    • --test systemverilog, including every timing case on native, cranelift, native-parallel, cranelift-parallel and wasm.
    • The process, simulation, state-file, VCD, flip-flop and native MIR test files.
    • celox-test-suite --features external --test retained_reports.
  • sv-tests conformance example (run.py --state-mode both --expected conformance/sv-tests/expected.tsv): verdicts identical, so expected.tsv is unchanged.
  • cargo fmt --check and clippy (--all-targets on the touched crates with CI's features, -D warnings) are clean.
  • pnpm docs:build passes.

Docs

docs/guide/systemverilog-reference.md and its Japanese counterpart replace the #1706 bullet in "Event and level waits" with the behavior described above.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-10-22T04:42:49.646864Z
We couldn’t complete this review. Comment @codex review to try again.
d7a59a5 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, you can upgrade your account or add credits to your account and enable them for code reviews in your settings.

@mergify

mergify Bot commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Queued — the merge queue status continues in this comment ↓.

@tignear
tignear force-pushed the feature/fix/sv-intermediate-write-events branch from b750326 to 7300779 Compare October 22, 2026 13:47
@tignear
tignear force-pushed the feature/fix/sv-intermediate-write-events branch from 7300779 to e182d7e Compare October 22, 2026 15:15
@tignear tignear added the merge-ready Request merging through Mergify after required checks label Oct 22, 2026
@tignear
tignear force-pushed the feature/fix/sv-intermediate-write-events branch from a3d7365 to 37be913 Compare October 22, 2026 17:33
@mergify mergify Bot added the queued Mergify queue entry label Oct 22, 2026
@mergify

mergify Bot commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-10-22 18:46 UTC · Rule: changes · triggered by rule Queue explicitly requested merges
  • 🚫 Left the queue — 2026-10-22 20:24 UTC · at 37be9131c2421f312985f56073786c7af508b079

This pull request spent 1 hour 37 minutes 24 seconds in the queue, with no time running CI.

Reason

The pull request conflicts with the base branch

The pull request was held in the queue, conflicting with at least one of 2 pull requests ahead of it. The conflict became a base branch one after #1737, #1766 merged.

Hint

You should update or rebase your pull request.

If you want to requeue this pull request, you can post a @mergifyio queue comment.

Requeued — the merge queue status continues in this comment ↓.

@mergify mergify Bot added dequeued Mergify stopped this queue entry; investigate before retrying and removed queued Mergify queue entry labels Oct 22, 2026
A process waiting on a variable missed a value that one evaluation of an
always_comb, a blocking edge-triggered always or an always_ff writes and
then overwrites (`y = 0; if (a) y = 1;`). Each store is an update event
(IEEE 1800-2023 4.3, 9.4.2), and nonblocking updates apply in the NBA
region in issue order, each its own update (10.4.2).

In a design with a wait:
- An always_comb that may store a variable twice is lowered both
  symbolically and as a gated imperative unit. Assembly keeps the unit for
  an instance in which such a variable is counted, and the symbolic
  definitions otherwise.
- A flip-flop evaluation that may write a signal twice keeps its first
  written value and counts the occurrences between later writes as
  pending; the commit adds the occurrence from the published value to the
  first one, and the pending ones. Staged nonblocking writes of a variable
  also written by blocking assignments are counted on the value the NBA
  merge publishes.
- Commits of a counted signal written once are counted at the commit.

Units that write no counted signal are unchanged, and a design without a
wait lowers exactly as before.

Fixes #1706

Change-Id: I6096fe2d13f0205b53fd8342c9057801a71c2465
@tignear tignear removed the dequeued Mergify stopped this queue entry; investigate before retrying label Oct 22, 2026
Add timing cases for intermediate writes of always_comb, always @*,
blocking edge-triggered always and always_ff bodies, for staged
nonblocking writes, and for a pulse behind a child's output port.

Verilator 5.052 sees only settled values, so the first three are excluded
for it. Icarus 13.0 collapses the output port and counts the child's
pulse, which 4.7 and 23.3.3 leave open; the port case asserts the settled
count and is excluded for Icarus. Both exclusions keep standalone repros.

Change-Id: Id2eb6d5daaefaa8604216f8cc543269a5716e59f
Replace the #1706 limitation in 'Event and level waits' with the behavior: every write of one evaluation is an update a waiter sees, nonblocking updates apply in issue order, and a child's output port shows only the settled value.

Change-Id: Id7909b9972b7574eeabd8d5f1910df4ba47eb75b
Change-Id: Ie0a71ea46beca39a738c17cc203f752ea8173e2b
A flip-flop stream now applies each write to a private copy and marks
the bits written so far. A write that changes covered bits is an
occurrence; a write of uncovered bits compares at the commit with the
value the commit publishes over, so a staged nonblocking update is
judged against the final blocking value. Each commit releases only the
bits it publishes and no unit clears the stream on entry, so the parts
of a parallel build no longer wipe each other's held writes.

An instance that keeps the imperative alternative of an always_comb
removes only the symbolic paths of that process's target ranges, so
other drivers of the same variables keep theirs.

Change-Id: I93f3569cb5edb5ed981fda8bf33fdf3401e19d17
Three timing cases for the review findings on intermediate writes, verified with Icarus 13.0. Verilator 5.052 sees only final values and joins the existing sv_verilator_intermediate_writes exclusion, with standalone probes.

Change-Id: I795ddad81bf28d02100c45a6c7d25fe358160fe3
Flattening may split the symbolic definition of a counted always_comb
at the boundaries of its readers, so an instance that keeps the
imperative unit now removes every path overlapping one of the
process's target ranges, not only exact ones. Overlapping drivers are
rejected earlier, so no other driver loses a path.

A flip-flop unit starts the bits of a held stream that it seeds empty,
or a whole staging value, which one process writes. An evaluation that
a runtime error ended before its commit, and that the host continues,
no longer leaves held writes for the next one, while the other parts of
an evaluation keep theirs.

Change-Id: If3d08166804161c395482b6534e6aa8238c0d971
A reader of some bits of a counted always_comb variable, and a counted output port connected to a concatenation. Icarus 13.0 and Verilator 5.052 both pass them.

Change-Id: I5fb7bf6c32594eb3403a1a2eff64ec98257f1d35
A $fatal ends an always_ff evaluation after it wrote a watched variable and before its commit; the host continues, and later edges that rewrite the bit to its own value wake no waiter on every backend.

Change-Id: Ia88c8150faf478de68e42a7a270684b3e881d240
@mergify mergify Bot added the dequeued Mergify stopped this queue entry; investigate before retrying label Oct 22, 2026
@tignear
tignear force-pushed the feature/fix/sv-intermediate-write-events branch from 37be913 to 4f4d93a Compare October 22, 2026 21:06
@tignear tignear added merge-ready Request merging through Mergify after required checks and removed merge-ready Request merging through Mergify after required checks dequeued Mergify stopped this queue entry; investigate before retrying labels Oct 22, 2026
@mergify mergify Bot added the queued Mergify queue entry label Oct 22, 2026
@mergify

mergify Bot commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-10-22 22:15 UTC · Rule: changes · triggered by rule Queue explicitly requested merges
  • ✅ Checks passed · on draft merge queue: checking #1726 on master (22d68c3) #1774
  • ✅ Merged — 2026-10-22 23:28 UTC · at 73848072f56e4a54285654a4a496ccb4093da27d · merge

This pull request spent 1 hour 13 minutes 35 seconds in the queue, including 1 hour 11 minutes 32 seconds running CI.

Required conditions to merge
  • check-success = Conventional Commit title
  • check-success = Queue validation
  • any of [🛡 GitHub repository ruleset rule master]:
    • check-success = @github-actions/Lint & Format
    • check-neutral = @github-actions/Lint & Format
    • check-skipped = @github-actions/Lint & Format
  • any of [🛡 GitHub repository ruleset rule master]:
    • check-success = @github-actions/NAPI Build (Linux ARM64 GNU)
    • check-neutral = @github-actions/NAPI Build (Linux ARM64 GNU)
    • check-skipped = @github-actions/NAPI Build (Linux ARM64 GNU)
  • any of [🛡 GitHub repository ruleset rule master]:
    • check-success = @github-actions/NAPI Build (Windows)
    • check-neutral = @github-actions/NAPI Build (Windows)
    • check-skipped = @github-actions/NAPI Build (Windows)
  • any of [🛡 GitHub repository ruleset rule master]:
    • check-success = @github-actions/Rust Test & NAPI Build
    • check-neutral = @github-actions/Rust Test & NAPI Build
    • check-skipped = @github-actions/Rust Test & NAPI Build
  • any of [🛡 GitHub repository ruleset rule master]:
    • check-success = @github-actions/JS Test (Ubuntu)
    • check-neutral = @github-actions/JS Test (Ubuntu)
    • check-skipped = @github-actions/JS Test (Ubuntu)
  • any of [🛡 GitHub repository ruleset rule master]:
    • check-success = @github-actions/JS Test (Windows)
    • check-neutral = @github-actions/JS Test (Windows)
    • check-skipped = @github-actions/JS Test (Windows)
  • any of [🛡 GitHub repository ruleset rule master]:
    • check-success = @github-actions/Conventional Commit title
    • check-neutral = @github-actions/Conventional Commit title
    • check-skipped = @github-actions/Conventional Commit title

@mergify mergify Bot mentioned this pull request Oct 22, 2026
40 of 71 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merge-ready Request merging through Mergify after required checks

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SV: waiters miss intermediate writes inside one comb/FF evaluation

1 participant