Skip to content

perf(sv): analyze each scope in time proportional to its own size - #1957

Merged
mergify[bot] merged 1 commit into
masterfrom
feature/perf/1956-scope-proportional-analysis
Oct 25, 2026
Merged

mergify[bot] merged 1 commit into
masterfrom
feature/perf/1956-scope-proportional-analysis

Conversation

@tignear

@tignear tignear commented Oct 25, 2026

Copy link
Copy Markdown
Contributor

What

The SV analyzer's per-scope checks visited every entry of every arena in the source file for each scope they validated, and kept only the entries inside the scope's token ranges. A design shipped as one file therefore cost roughly (number of scope analyses × file size). For rtlmeter's C906 (c906_all.v: 150k lines, 289 modules), that came to most of the 11 minutes reported in #1956.

Each scope now costs what its own token ranges contain:

  • source_tree::spans builds a per-file index once, on first use: the arenas (expressions, types, body items, nets, instantiations, subroutines) ordered by first token. Starts::within(ranges) returns the entries that start inside a scope's ranges, in arena order. Callers apply the same predicate as before. Every entry a range contains starts inside it, so the selected set and the visit order (which decides the first diagnostic) are unchanged. Ranges answers the "is this range or token inside any of these ranges" checks in O(log n), replacing linear any scans. The index lives next to the shared Declarations and is dropped whenever they are mutated through DerefMut.
  • Facts that depend only on the file are computed once per file instead of once per scope:
    • validation::Facts: the constant-value node closure (the skipped::children walk), callees, generate controls, parameter defaults, procedural ranges, cast exclusions, type-argument override dimensions, header ports and packed dimensions.
    • imports::Facts: qualified-name bases and pattern keys.
    • The timescale source key: the file's token text is built once, and its hash is cached, so the key is no longer rebuilt as a fresh ~6 MB string and rehashed on every install and query.
  • Parts that depend on the scope stay per scope, but are evaluated only for the candidates they can affect: data-query callee names and needs_selected for parameter defaults.
  • Also indexed: validate_scope/reject_constant_calls, timescales::validate_queries, generate::parameter_literal_references, ScopeImports::from_scope, imports::unqualified_names (also quadratic within a scope through qualified.iter().any), type_binding::packed_ranges and the function warnings.

Measurements

The C906 numbers come from a local stack that is never pushed: master + #1893, #1952, #1954 and #1950. On master the design is rejected before analysis (hierarchical references, non-ANSI ports). The binaries use the heliodor-dev profile, run under celox run --top tb --two-state --unlimited survey.v (c906_all.v with the local edits from the issue), pinned to taskset -c 8-11.

workload before after
C906 compile, through to the $readmemh stop (run outside the testbench directory) 394.7 s 8.6 s
C906 full run from hello/, through to the #1955 false loop (same stop as the issue) 1059 s¹ 137 s

¹ This baseline run shared the machine with a build part of the time, so it overstates the baseline somewhat. The analyzer-only row above is the clean comparison.

The diagnostics are byte-identical before and after in both workloads. In the full run, about 60% of what remains is celox_frontend_core::watch::instrument_unit, which the issue leaves for a separate look. The SV analyzer is now about 5%.

Small designs are not slower (perf stat -r 20/30, task-clock, master de79c93c6f vs this branch):

design master this PR
verification/repros/integer_edges.sv 79.5–89.8 ms 68.8–76.3 ms
benches/verilator/Fifo.sv 211–225 ms 186–195 ms
b2_vm_dispatch_bit_short.sv (native) 3301–3443 ms 2656–2955 ms

Generated code is unchanged. On b2_vm_dispatch_bit, native backend, callgrind scoped to SimulationState::step_until:

master this PR
JIT code 2,761,200,000 Ir 2,761,200,000 Ir
total 4,342,204,483 Ir 4,342,204,483 Ir

Validation

  • cargo nextest run --workspace --exclude 'lydite*' --profile ci --features celox/systemverilog,celox-test-suite/verilator,celox-test-suite/icarus,celox-test-suite/xezim,celox-test-suite/runtime-cases: 17257 passed, 340 skipped.
  • New unit tests in source_tree::spans compare within, containing, covering, contains and covers with brute-force scans over every range of a short token sequence.
  • sv-tests (pinned c4229f3bd522, --state-mode both --expected conformance/sv-tests/expected.tsv): matches the expectations.
  • cargo fmt --check; cargo clippy -p celox-sv-analyzer -p celox-frontend-sv --all-targets: clean.

Closes #1956

Per-scope analysis selected arena entries by token range by scanning the
whole source file's arenas, so a design shipped as one file cost
(analyses x file size). A per-file span index now orders each arena by
first token; a scope visits only the entries starting inside its own
ranges, in arena order, and applies the same predicates. Facts that are
pure functions of the file (constant-value nodes, callees, exclusion
ranges, override dimensions, qualified bases, pattern keys and the
timescale source key with its hash) are computed once per file.

Diagnostics and lowering are unchanged.

Closes #1956

Change-Id: I283e94ab1244d6aa8c08bfb4af44b1bca00a8e4d
@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-25T03:55:16.619155Z
We couldn’t complete this review. Comment @codex review to try again.
e221929 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 25, 2026 •

Copy link
Copy Markdown
Contributor

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

@tignear tignear added the merge-ready Request merging through Mergify after required checks label Oct 25, 2026
@mergify mergify Bot added the queued Mergify queue entry label Oct 25, 2026
@mergify

mergify Bot commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-10-25 04:29 UTC · Rule: changes · triggered by rule Queue explicitly requested merges
  • ✅ Checks passed · on draft merge queue: checking #1957 on master (726ab26) #1961
  • ✅ Merged — 2026-10-25 05:28 UTC · at 3533980e3c9712507f068a231d627fe5279aba72 · merge

This pull request spent 58 minutes 37 seconds in the queue, including 56 minutes 51 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 25, 2026
40 of 71 tasks
@mergify
mergify Bot merged commit 3533980 into master Oct 25, 2026
23 checks passed
@mergify
mergify Bot deleted the feature/perf/1956-scope-proportional-analysis branch October 25, 2026 05:28
@mergify mergify Bot removed the queued Mergify queue entry label Oct 25, 2026
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.

perf(sv): analysis scans the whole source file for every scope

1 participant