Skip to content

perf(sv)!: run initial loops without a static trip count as a process - #2014

Merged
mergify[bot] merged 1 commit into
masterfrom
feature/perf/1992-sv-runtime-loop-fallback
Oct 25, 2026
Merged

mergify[bot] merged 1 commit into
masterfrom
feature/perf/1992-sv-runtime-loop-fallback

Conversation

@tignear

@tignear tignear commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

An initial block is first folded into initial state by symbolic execution, and runs as a process only when that fails. A loop whose trip count the folding could not determine beforehand was unrolled one iteration at a time until MAX_UNROLLED_ITERATIONS (10000) failed the fold, so the block cost up to 10000 iterations of its body before running as the process it becomes anyway. The C906 testbench loads its program with two such loops (for (j = 0; i < 32'h4000; i = j/4) with 64 hierarchical memory writes per iteration), about 450 s each.

While folding an initial block, a loop now proceeds only when its trip count is known before it runs; otherwise the fold fails at once and the block runs as a process:

  • repeat loops, and canonical for loops whose body does not write the loop variable (their trip count was already checked by loop_needs_fold);
  • while and do-while loops shaped like one (counted_while_loop): the condition compares a variable with a bound, the last statement steps that variable as a canonical for step would, and no other statement writes the variable or the bound. They start from the variable's current value and keep folding when their trip count fits the unroll limit.

Other loops, such as a condition on a variable that another one derives, forever, or a while whose counter is stepped elsewhere, are not folded. The check costs one pass over the body, not iterations of it. The SystemVerilog reference lists the loops that fold.

BREAKING CHANGE: an initial block with such a loop is no longer folded even when it would finish within the unroll limit. It runs as a process, which a Simulator rejects ("initial ... is a process, which Simulator does not run"); build a Simulation instead.

Closes #1992.

  • Shared cases procedural::initial_loops_without_a_static_trip_count_run_as_a_process (the testbench's load-loop shape with hierarchical memory writes, plus while and do-while, matching Icarus and Verilator) and procedural::initial_while_loops_define_the_state (a counted while loop still defines the state a Simulator sees).
  • A reduction of the C906 memory load (the C906 f_spsram_524288x128 and ram modules with the testbench's wipe and load loops) now takes 3.3 s instead of 223 s, with identical output; the load loop alone (memC2.v) takes 1.9 s instead of not finishing in 300 s.

Verification

  • cargo nextest run -p celox -p celox-test-suite -p celox-frontend-sv --features celox/systemverilog: 14669 passed.
  • clippy on the workspace (minus lydite) with --features celox/systemverilog --all-targets -D warnings: clean.
  • verify-sv-icarus / verify-sv-verilator --filter procedural::initial_: 4/4 passed each.
  • sv-tests (2- and 4-state): identical to conformance/sv-tests/expected.tsv.
  • b2_vm_dispatch_bit callgrind gate: step_until identical (51,200,204), PROGRAM TOTALS +0.0001% (see comment).

An `initial` block is first folded into initial state by symbolic execution, and runs as a process only when that fails. A loop whose trip count the folding could not determine beforehand was unrolled one iteration at a time until `MAX_UNROLLED_ITERATIONS` (10000) failed the fold, so the block cost up to 10000 iterations of its body before running as the process it becomes anyway. The C906 testbench loads its program with two such loops (`for (j = 0; i < 32'h4000; i = j/4)` with 64 hierarchical memory writes per iteration), about 450 s each.

While folding an `initial` block, a loop now proceeds only when its trip count is known before it runs; otherwise the fold fails at once and the block runs as a process:
- `repeat` loops, and canonical `for` loops whose body does not write the loop variable (their trip count was already checked by `loop_needs_fold`);
- `while` and `do`-`while` loops shaped like one (`counted_while_loop`): the condition compares a variable with a bound, the last statement steps that variable as a canonical `for` step would, and no other statement writes the variable or the bound. They start from the variable's current value and keep folding when their trip count fits the unroll limit.

Other loops, such as a condition on a variable that another one derives, `forever`, or a `while` whose counter is stepped elsewhere, are not folded. The check costs one pass over the body, not iterations of it. The SystemVerilog reference lists the loops that fold.

BREAKING CHANGE: an `initial` block with such a loop is no longer folded even when it would finish within the unroll limit. It runs as a process, which a `Simulator` rejects ("`initial` ... is a process, which Simulator does not run"); build a `Simulation` instead.

Closes #1992.

- Shared cases `procedural::initial_loops_without_a_static_trip_count_run_as_a_process` (the testbench's load-loop shape with hierarchical memory writes, plus `while` and `do`-`while`, matching Icarus and Verilator) and `procedural::initial_while_loops_define_the_state` (a counted `while` loop still defines the state a `Simulator` sees).
- A reduction of the C906 memory load (the C906 `f_spsram_524288x128` and `ram` modules with the testbench's wipe and load loops) now takes 3.3 s instead of 223 s, with identical output; the load loop alone (`memC2.v`) takes 1.9 s instead of not finishing in 300 s.

Change-Id: I596c31f5ce2d71957b3e39e8c7afa7ac159dccee
@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 🔄 Running since 2026-10-25T12:30:12.801593Z 10dc5a2 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.

@tignear tignear added the merge-ready Request merging through Mergify after required checks label Oct 25, 2026
@tignear

tignear commented Oct 25, 2026

Copy link
Copy Markdown
Contributor Author

b2_vm_dispatch_bit callgrind gate (release builds, taskset -c 8-11, run --top bench_vm_dispatch --two-state --unlimited):

base fc0b162 this PR
output BENCH_DONE cycles=200000 checksum=136 same
step_until 51,200,204 51,200,204
PROGRAM TOTALS 40,643,992,992 40,644,031,800 (+0.0001%)

@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

This pull request spent 1 hour 33 minutes in the queue, including 33 minutes 1 second 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 merged commit 23411b0 into master Oct 25, 2026
24 checks passed
@mergify
mergify Bot deleted the feature/perf/1992-sv-runtime-loop-fallback branch October 25, 2026 14:33
@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.

SV procedural loops with a non-static trip count are unrolled up to 10000 iterations before falling back

1 participant