Repository navigation
perf(ast): memoize top-level constant function calls - #2
Merged
Merged
Conversation
A constant function called from a parameter initializer is evaluated once per instance of the enclosing module, even when every instance passes the same arguments. Designs that compute large tables in package functions (e.g. Hsiao ECC matrices) spend most of their elaboration time redoing identical evaluations. Top-level calls are now memoized on the compilation, keyed on the subroutine, the evaluation and AST flags, and the exact argument values. A result is only recorded when the evaluation took enough steps to be worth it, produced no diagnostics, and did not depend on its call site (a non-package parameter checked against the call's lookup location). Calls made from within a package are never memoized. On a hit the original step count is replayed, and a call that would exceed the step limit is evaluated for real so that it fails exactly as before. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…e arguments Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Author
|
Pushed 6b20f34: |
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.
A constant function called from a parameter initializer is evaluated once per instance of the enclosing module, even when every instance passes the same arguments. On the PULP astral SoC,
hsiao_ecc_pkg::hsiao_matrix(DataWidth, ProtWidth)is called 108 times with 6 distinct argument sets. It accounts for 145 s of a 148 s elaboration. The value is needed for each instance's instance-cache key, so the evaluation can't be skipped.This change memoizes top-level constant function calls on the
Compilation. The key is the subroutine, the eval flags, the AST flags and the exact argument values: integers compare width, sign and X/Z bits, and reals compare bit patterns, so+0.0and-0.0are different keys. A result is recorded only when the evaluation:EvalContext, andNamedValueExpression::checkConstantflags any non-package parameter checked against the outermost frame's lookup location.Calls made from inside a package, calls with a queue target or lvalues on the stack, class methods, tasks and subroutines with output arguments are never memoized. On a hit, the original step count is added to the context. If the hit would cross
maxConstexprSteps, the call is evaluated for real so that it fails exactly as it would have. The memo is mutex-guarded and holds at most 65,536 entries.Measured with the boson native-elab driver setup,
getAllDiagnostics, 1 thread:The rendered diagnostics are byte-identical on both designs.
New tests in
ParameterTests.cppcover values across instances and generate blocks,+0.0/-0.0arguments, the step limit with memo hits, and call-site-dependent diagnostics (ConstEvalIdUsedInCEBeforeDecl). The full unittests pass: 22082 assertions in 2204 test cases, with #3 applied too.Ships to boson as
scripts/patches/slang/0010-memoize-top-level-constant-function-calls.patch(partcleda/boson#13187).🤖 Generated with Claude Code
Need help on this PR? Tag
@codesmith-botwith what you need.