-
Notifications
You must be signed in to change notification settings - Fork 272
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
expose value of constant integers in module reflection #6367
Merged
csyonghe
merged 5 commits into
shader-slang:master
from
ennis:feature/static-const-reflection
Feb 26, 2025
Merged
expose value of constant integers in module reflection #6367
csyonghe
merged 5 commits into
shader-slang:master
from
ennis:feature/static-const-reflection
Feb 26, 2025
Conversation
This file contains 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
Following a suggestion on discord I've added code to update VarDecl's constant value at a later stage during DeclBodyVisitor. Since constant folding is already done inside _validateCircularVarDefinition, this commit reuses the constant that is evaluated there. |
csyonghe
previously approved these changes
Feb 19, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me!
csyonghe
approved these changes
Feb 25, 2025
This commit adds `VariableReflection::getDefaultValueInt` to get the value of a variable if it is a compile-time constant integer. TODO: currently it works only if the initializer expression is an integer literal, references to other constant values are not handled.
Constant folding for integer values is already done internally by _validateCircularVarDefinition, this just reuses the result.
c17ad2c
to
0047cef
Compare
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.
One of the pain points of working with shaders is needing to keep the definitions of interface types in sync between shader and device code. With the reflection API of slang, it is possible to automate this process by generating a header file for the host language from the reflection data, that contains all interface types used in the shader.
However, one thing missing is the ability to get the value of compile-time constants defined in the shaders. This is useful if you have some constant that an algorithm or a data structure depends on, like a static block or tile size, and you don't want to have to keep them in sync manually between host and shader.
To solve this (at least for integers) this PR adds
VariableReflection::getDefaultValueInt
to get the default value of a variable if it is a compile-time constant integer. The reflection-api example has also been updated to print all the static const variables of a module and their values.Internally, this uses the
VarDeclBase::val
field that should contain the constant-folded initializer value. However, currently this field is null if, for instance, the initializer is a variable reference (as is the case for THREADGROUP_SIZE_Y in thecompute-simple.slang
of the reflection-api example). It seems to be initialized inSemanticsDeclHeaderVisitor::checkVarDeclCommon
, which then goes throughSemanticsVisitor::tryConstantFoldExpr
, which does try to follow variable references. However it seems that at this point the reference cannot be resolved. I'd like to make this work before trying to merge this.