Skip to content

perf(sv): fold constants without big-integer parsing or needless copies - #2066

Merged
mergify[bot] merged 1 commit into
masterfrom
perf/sv-const-fold-numeric
Oct 25, 2026
Merged

mergify[bot] merged 1 commit into
masterfrom
perf/sv-const-fold-numeric

Conversation

@tignear

@tignear tignear commented Oct 25, 2026

Copy link
Copy Markdown
Contributor

What

Constant folding passes values through literal strings (#1993): eval_const_expr_with_types clones the expression and substitutes each typed constant as a sized literal (32'sd5), and the evaluator parses every literal again with BigUint arithmetic (str::replace, radix conversion, shifts and masks).

  • parse_integral_literal first tries parse_small_literal: unsized decimals and sized 'b/'o/'d/'h literals (optionally signed) of plain digits, without _, x/z or whitespace, whose value fits in u128, parsed with machine integers. Everything else, and anything that does not fit, goes to the unchanged general parser.
  • eval_const_expr_with_types evaluates the expression in place unless it names a typed constant (has_typed_constant), the only nodes substitution replaces.

Results are identical; the generated SIR is unchanged (frontend-only).

Refs #1993 (first step; the evaluator still round-trips through literal strings for typed constants).

Why

On rtlmeter C906 the typecheck evaluator (eval_const_expr_with_types) was about 8-10% of the run up to the first testbench output, mostly literal parsing and copies. User-mode instructions to the first testbench output (C906, --backend interpreter --two-state, integration base + #2043): 327.4 G → 309.4 G (−5.5%) with the literal fast path alone; deterministic across repeats.

Validation

  • typecheck::literal_tests::small_literals_parse_like_any_literal: 20 000 generated literals (all bases and cases, signed and unsigned, sizes 0..200, leading zeros, values beyond 128 bits, and x/z/_ digits that must fall back) compared with the general parser; over 5 000 take the fast path. Dropping the 32-bit minimum width of unsized decimals makes it fail.
  • typecheck::literal_tests::typed_evaluation_substitutes_only_typed_constants: expressions without typed constants evaluate as their substituted copies; a typed 8-bit constant still wraps in its width.
  • cargo fmt --all -- --check, cargo clippy -p celox-sv-analyzer --all-targets -- -D warnings: clean.
  • cargo nextest run -p celox -p celox-sv-analyzer -p celox-frontend-sv -p celox-test-suite --features celox/systemverilog,celox/cranelift,celox/wasm-runtime,celox/wasm-component,celox/veryl-reference,celox-test-suite/runtime-cases: 15608 passed, 316 skipped.

Constant folding passes values through literal strings: typed constants
are substituted as sized literals and every literal is parsed again with
big-integer arithmetic. On rtlmeter C906, evaluating constant expressions
took about a tenth of the frontend.

Literals of plain digits whose value fits in 128 bits, the form constant
substitution writes, are now parsed with machine integers; other literals
keep the general parser. Evaluating with types copies and substitutes the
expression only when it names a typed constant, since substitution
changes nothing else.

Refs #1993

Change-Id: Ia0b236b14e15e4077bba958ce05f230504f70ab3
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 25, 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-25T19:19:16.483158Z 5b22578 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
@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 33 minutes 37 seconds in the queue, including 31 minutes 54 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 merged commit 72eb8cf into master Oct 25, 2026
23 checks passed
@mergify
mergify Bot deleted the perf/sv-const-fold-numeric branch October 25, 2026 20:17
@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.

1 participant