Skip to content

perf(sim): keep the inactive edge of a tick cheap when nothing can fire on it - #1789

Merged
mergify[bot] merged 2 commits into
masterfrom
feature/tick-inactive-edge-fast-path
Oct 23, 2026
Merged

mergify[bot] merged 2 commits into
masterfrom
feature/tick-inactive-edge-fast-path

Conversation

@tignear

@tignear tignear commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Stacked on #1776.

A tick of an event with derived clocks ran its inactive edge through the derived plan, which settles combinational logic although no domain fires on that edge for a gated (clk & en) or divided clock.

Change

  • EventEdge::inactive_runs (celox-runtime build_event_edges): decided per event at build time. The inactive edge can run a domain when it fires the event's own domain, or a clock computed combinationally from the signal that the edge can move to its active edge. A gate program's dependence on its source is classified as positive, negative, independent or mixed. Gates without a program count as able to fire, and so do clocks of unknown kind. With nothing able to fire, nothing commits, so state-derived domains (dividers) can't run either.

  • tick, tick_events: when the inactive edge runs nothing, take the active edge and then only store the inactive level. Combinational logic is marked dirty when it reads the signal, as the plain path already did. If the active edge left combinational logic unsettled, the inactive edge still runs in full.

  • Plain tick fast path (second commit): a plain tick stores the active level, runs the domain and stores the inactive level. When the signal is already at its known inactive level, both stores are unobservable, so the tick is one run of the domain, as tick was before feat(sim)!: make a tick one clock period and run every clock generator #1776. The conditions are:

    • the signal is at its known inactive level (two-state, or four-state with no unknown bits);
    • no combinational logic reads it;
    • no flip-flop of the event's own domain reads it;
    • no comb observer or host watch is active.

    The frontend records EventTopology::level_unread_by_domain at build time: a load or commit source of the signal, or an external call, counts as a read, and an absent entry means the level may be read. EventEdge::level_read combines it with comb_read. A first tick from an unknown or active level still moves the signal. VCD and checkpoints only see state at the end of a host call, where the level is the same either way. tick_events is unchanged.

  • period_runs_only_own_domain → period_runs_only_active_edge: native testbench clk.next(n) keeps its batched path for gated and divided designs.

Observable behaviour is unchanged (see docs/guide/execution-semantics.md).

Tests

  • New tests/tick_inactive_edge.rs compares against the timed Simulation running the equivalent SV statements, in two-state and four-state, unknown bits included. It covers:

    • a gated rising-edge domain plus combinational logic reading clk/gclk (must be low after a tick), including coincident tick_events;
    • a negedge gclk domain (runs on every tick);
    • a ~clk domain;
    • a register divider whose divided domain holds while en is low.

    It also asserts the computed inactive_runs for each design.

  • Plain fast path:

    • a_plain_tick_skips_only_unobservable_level_stores: a differential test against Simulation, with one clock nothing reads (fast path) and one a flip-flop samples as data (stores kept). Mutation-checked: dropping the flip-flop read from level_read makes it fail.
    • vcd::test_vcd_of_ticks_matches_stored_periods: the VCD of ticks equals the VCD of explicit active_edge + inactive_edge periods, in two-state and four-state.
  • New native testbench test: clk.next(n) on gated and divided clocks.

  • Unit tests for the gate dependence analysis.

  • Passed: celox lib plus 25 tick/edge/testbench/VCD/checkpoint/state-file test binaries and the systemverilog suite with --features systemverilog, celox-runtime, celox-design and celox-frontend-core tests, workspace clippy with the CI features, cargo fmt --check.

Bench

overhead, best median per 10000 periods over 3 interleaved rounds on one pinned core. Each run started with the 1-minute load below 4.

The parent session's quiet-machine run of the first commit:

design master tick master cycle #1776 tick e673910 tick
plain 1.68 ms 1.78 2.97 2.63
gated 3.39 4.95 5.11 3.74
divided 3.12 3.63 3.73 3.25
gated 4-state 10.08 9.43 9.54 8.21
divider 4-state 7.20 7.88 7.78 7.48

With the plain fast path (b49c5a4), same procedure:

design master tick master cycle e673910 tick b49c5a4 tick
plain 1.72 ms 1.81 2.62 1.66
gated 3.35 4.87 3.74 3.64
divided 3.13 3.54 3.22 2.18
gated 4-state 9.73 9.37 8.09 8.10
divider 4-state 7.18 7.76 7.62 7.33

Plain now matches master's tick, and the other variants are equal or better than e673910. Divider 4-state is bimodal across runs in both builds (7.3 / 9.6 ms for b49c5a4, 7.6 / 10.3 ms for e673910), so it is layout-sensitive; the table gives the best median.

…re on it

A tick of an event with derived clocks ran its inactive edge through the
derived plan, which settles combinational logic although no domain fires
on that edge for a gated (`clk & en`) or divided clock.

Decide per event at build time whether the inactive edge can run a
domain: the event's own, or one of a clock computed combinationally from
the signal that the edge can move to its active edge, judged from how the
gate program depends on the signal (positive, negative, independent or
mixed). With nothing able to fire on that edge nothing commits, so no
state-derived domain runs either. `EventEdge::inactive_runs` records it.

`tick`, `tick_events` and the native testbench's `clk.next(n)` use it:
when the inactive edge runs nothing they take the active edge and then
only store the inactive level, marking combinational logic dirty when it
reads the signal. If the active edge left combinational logic unsettled,
the inactive edge still runs in full. Gated and divided designs keep the
batched `clk.next(n)` path.

Change-Id: Id9d21afbe418767912aa2adf08d018ec5899162e
Base automatically changed from feature/simulator-tick-andveryl-tb-clock-next-semantics to master October 23, 2026 03:40
…re unobservable

A plain tick stores the active level, runs the domain and stores the
inactive level. When the signal already is at its known inactive level,
no combinational logic and no flip-flop of the event's own domain reads
it, and no comb observer or host watch is active, both stores are
unobservable: the level is the inactive one before and after the call
and nothing but the domain runs in between. `tick` then is exactly the
domain run, as before ticks became clock periods.

The frontend records at build time which event domains never read their
own signal (`EventTopology::level_unread_by_domain`; a load or commit
source of the signal or an external call counts as a read, and an absent
entry means the level may be read). `EventEdge::level_read` combines it
with `comb_read`. A first tick from an unknown or active level still
moves the signal.

Change-Id: I0ec005eafbfc07f8f8ff246be3e7f381436178a3
@tignear
tignear marked this pull request as ready for review October 23, 2026 05:38
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 23, 2026 •

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 ✅ Completed 2026-10-23T05:43:20.404016Z b49c5a4 Draft marked ready
ℹ️ 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.

@tignear

tignear commented Oct 23, 2026

Copy link
Copy Markdown
Contributor Author

@Mergifyio queue

@mergify

mergify Bot commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

This pull request spent 2 hours 43 minutes 57 seconds in the queue, including 1 hour 55 minutes 27 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

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b49c5a495e

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +2243 to +2244
pub(crate) fn period_runs_only_active_edge(&self, event: B::Event) -> bool {
!self.event_edges[event.id()].inactive_runs && self.host_watch.is_empty()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Preserve the clock level in derived clk.next periods

When a native-testbench clock has an active-edge-only derived plan (for example, gclk = clk & en) and a flip-flop in the source clock's own domain samples clk as data, this predicate returns true even though EventEdge::level_read is true. clock_periods then uses tick_deferred_comb_many; its derived-plan path reaches fire/tick_derived, which models the derived transitions without moving the source signal, so the source-domain flip-flop sees the inactive level instead of the active level. Before this change, the presence of a derived plan forced the explicit rising/falling edge path. Either exclude level-reading events here or make the derived deferred path expose the active source level while evaluating domains.

Useful? React with 👍 / 👎.

Comment on lines +921 to +926
/// Canonical events whose own domain reads no level of their signal: its
/// flip-flops do not sample the signal as data. With
/// [`Self::combinationally_read`] it says whether a run of the domain
/// observes the signal's level. Absent means it may.
#[serde(default)]
pub level_unread_by_domain: BTreeSet<A>,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Declare the public struct expansion as breaking

Adding this required public field breaks downstream exhaustive struct literals and destructuring for the published celox-design crate; the same commit also adds required fields to the exported celox_runtime::EventEdge. Because the perf(sim) subject drives release automation without a breaking marker, these source-incompatible changes can be shipped as a patch release rather than receiving the required breaking-version treatment. Avoid changing the public struct shape or mark the change with !.

AGENTS.md reference: AGENTS.md:L116-L118

Useful? React with 👍 / 👎.

@mergify mergify Bot added the queued Mergify queue entry label Oct 23, 2026
@mergify
mergify Bot merged commit 1ca40a2 into master Oct 23, 2026
31 checks passed
@mergify
mergify Bot deleted the feature/tick-inactive-edge-fast-path branch October 23, 2026 09:19
@mergify mergify Bot removed the queued Mergify queue entry label Oct 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant