Skip to content
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
merged 5 commits into from
Feb 26, 2025

Conversation

ennis
Copy link
Contributor

@ennis ennis commented Feb 15, 2025

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 the compute-simple.slang of the reflection-api example). It seems to be initialized in SemanticsDeclHeaderVisitor::checkVarDeclCommon, which then goes through SemanticsVisitor::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.

@CLAassistant
Copy link

CLAassistant commented Feb 15, 2025

CLA assistant check
All committers have signed the CLA.

@ennis
Copy link
Contributor Author

ennis commented Feb 15, 2025

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.

@ennis ennis marked this pull request as ready for review February 15, 2025 21:21
@ennis ennis requested a review from a team as a code owner February 15, 2025 21:21
@csyonghe csyonghe added the pr: non-breaking PRs without breaking changes label Feb 19, 2025
csyonghe
csyonghe previously approved these changes Feb 19, 2025
Copy link
Collaborator

@csyonghe csyonghe left a 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!

@ennis ennis changed the title WIP: expose value of constant integers in module reflection expose value of constant integers in module reflection Feb 22, 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.
@ennis ennis force-pushed the feature/static-const-reflection branch from c17ad2c to 0047cef Compare February 25, 2025 09:16
@csyonghe csyonghe merged commit 86669cb into shader-slang:master Feb 26, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr: non-breaking PRs without breaking changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants