Repository navigation
perf(sv)!: run initial loops without a static trip count as a process - #2014
Merged
Merged
Conversation
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
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Contributor
Author
|
b2_vm_dispatch_bit callgrind gate (release builds,
|
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
|
This was referenced Oct 25, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
An
initialblock 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 untilMAX_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
initialblock, 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:repeatloops, and canonicalforloops whose body does not write the loop variable (their trip count was already checked byloop_needs_fold);whileanddo-whileloops shaped like one (counted_while_loop): the condition compares a variable with a bound, the last statement steps that variable as a canonicalforstep 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 awhilewhose 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
initialblock with such a loop is no longer folded even when it would finish within the unroll limit. It runs as a process, which aSimulatorrejects ("initial... is a process, which Simulator does not run"); build aSimulationinstead.Closes #1992.
procedural::initial_loops_without_a_static_trip_count_run_as_a_process(the testbench's load-loop shape with hierarchical memory writes, pluswhileanddo-while, matching Icarus and Verilator) andprocedural::initial_while_loops_define_the_state(a countedwhileloop still defines the state aSimulatorsees).f_spsram_524288x128andrammodules 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.--features celox/systemverilog --all-targets -D warnings: clean.verify-sv-icarus/verify-sv-verilator --filter procedural::initial_: 4/4 passed each.conformance/sv-tests/expected.tsv.step_untilidentical (51,200,204), PROGRAM TOTALS +0.0001% (see comment).