diff options
| author | Alexandre Bléron <alex.bleron@gmail.com> | 2025-02-26 22:43:57 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-02-26 13:43:57 -0800 |
| commit | 86669cbb781840f8de180b1f793275f4511d3b65 (patch) | |
| tree | c28f261a15e3ea69f8a0ab533d484140fa780ec2 | |
| parent | 519e866ff44ec728f95c191fff8a200b66a5377e (diff) | |
expose value of constant integers in module reflection (#6367)
* Expose value of constant integers in module reflection
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.
* Update VarDecl folded constant value during DeclBodyVisitor
Constant folding for integer values is already done internally by _validateCircularVarDefinition, this just reuses the result.
* Address review comments & formatting
* Formatting
---------
Co-authored-by: Yong He <yonghe@outlook.com>
| -rw-r--r-- | examples/reflection-api/compute-simple.slang | 5 | ||||
| -rw-r--r-- | examples/reflection-api/main.cpp | 22 | ||||
| -rw-r--r-- | include/slang-deprecated.h | 2 | ||||
| -rw-r--r-- | include/slang.h | 5 | ||||
| -rw-r--r-- | source/slang/slang-check-decl.cpp | 17 | ||||
| -rw-r--r-- | source/slang/slang-check-impl.h | 2 | ||||
| -rw-r--r-- | source/slang/slang-reflection-api.cpp | 16 |
7 files changed, 63 insertions, 6 deletions
diff --git a/examples/reflection-api/compute-simple.slang b/examples/reflection-api/compute-simple.slang index 3d8872180..00d45d857 100644 --- a/examples/reflection-api/compute-simple.slang +++ b/examples/reflection-api/compute-simple.slang @@ -1,5 +1,8 @@ // compute-simple.slang +static const uint THREADGROUP_SIZE_X = 8; +static const uint THREADGROUP_SIZE_Y = THREADGROUP_SIZE_X; + struct ImageProcessingOptions { float3 tintColor; @@ -10,7 +13,7 @@ struct ImageProcessingOptions } [shader("compute")] -[numthreads(8, 8)] +[numthreads(THREADGROUP_SIZE_X, THREADGROUP_SIZE_Y)] void processImage( uint3 threadID : SV_DispatchThreadID, uniform Texture2D inputImage, diff --git a/examples/reflection-api/main.cpp b/examples/reflection-api/main.cpp index c072c641b..2ff4475bf 100644 --- a/examples/reflection-api/main.cpp +++ b/examples/reflection-api/main.cpp @@ -114,6 +114,21 @@ struct ReflectingPrinting List<ComPtr<slang::IComponentType>> componentsToLink; + // ### Variable decls + // + key("global constants"); + WITH_ARRAY() + for (auto decl : module->getModuleReflection()->getChildren()) + { + if (auto varDecl = decl->asVariable(); varDecl && + varDecl->findModifier(slang::Modifier::Const) && + varDecl->findModifier(slang::Modifier::Static)) + { + element(); + printVariable(varDecl); + } + } + // ### Finding Entry Points // @@ -213,6 +228,13 @@ struct ReflectingPrinting printQuotedString(name); key("type"); printType(type); + + int64_t value; + if (SLANG_SUCCEEDED(variable->getDefaultValueInt(&value))) + { + key("value"); + printf("%" PRId64, value); + } } // ### Types diff --git a/include/slang-deprecated.h b/include/slang-deprecated.h index 82d81af75..df6e41488 100644 --- a/include/slang-deprecated.h +++ b/include/slang-deprecated.h @@ -659,6 +659,8 @@ extern "C" SlangSession* globalSession, char const* name); SLANG_API bool spReflectionVariable_HasDefaultValue(SlangReflectionVariable* inVar); + SLANG_API SlangResult + spReflectionVariable_GetDefaultValueInt(SlangReflectionVariable* inVar, int64_t* rs); SLANG_API SlangReflectionGeneric* spReflectionVariable_GetGenericContainer( SlangReflectionVariable* var); SLANG_API SlangReflectionVariable* spReflectionVariable_applySpecializations( diff --git a/include/slang.h b/include/slang.h index af635e3c4..54647b830 100644 --- a/include/slang.h +++ b/include/slang.h @@ -2832,6 +2832,11 @@ struct VariableReflection return spReflectionVariable_HasDefaultValue((SlangReflectionVariable*)this); } + SlangResult getDefaultValueInt(int64_t* value) + { + return spReflectionVariable_GetDefaultValueInt((SlangReflectionVariable*)this, value); + } + GenericReflection* getGenericContainer() { return (GenericReflection*)spReflectionVariable_GetGenericContainer( diff --git a/source/slang/slang-check-decl.cpp b/source/slang/slang-check-decl.cpp index 0c42817c8..1659a161b 100644 --- a/source/slang/slang-check-decl.cpp +++ b/source/slang/slang-check-decl.cpp @@ -1430,7 +1430,7 @@ bool SemanticsVisitor::shouldSkipChecking(Decl* decl, DeclCheckState state) return false; } -void SemanticsVisitor::_validateCircularVarDefinition(VarDeclBase* varDecl) +IntVal* SemanticsVisitor::_validateCircularVarDefinition(VarDeclBase* varDecl) { // The easiest way to test if the declaration is circular is to // validate it as a constant. @@ -1444,8 +1444,11 @@ void SemanticsVisitor::_validateCircularVarDefinition(VarDeclBase* varDecl) // // if (!isScalarIntegerType(varDecl->type)) - return; - tryConstantFoldDeclRef(DeclRef<VarDeclBase>(varDecl), ConstantFoldingKind::LinkTime, nullptr); + return nullptr; + return tryConstantFoldDeclRef( + DeclRef<VarDeclBase>(varDecl), + ConstantFoldingKind::LinkTime, + nullptr); } void SemanticsDeclModifiersVisitor::visitStructDecl(StructDecl* structDecl) @@ -2350,7 +2353,13 @@ void SemanticsDeclBodyVisitor::checkVarDeclCommon(VarDeclBase* varDecl) // a constant with a circular definition. // varDecl->setCheckState(DeclCheckState::DefinitionChecked); - _validateCircularVarDefinition(varDecl); + + // Update constant value + // + if (!varDecl->val) + { + varDecl->val = _validateCircularVarDefinition(varDecl); + } } else { diff --git a/source/slang/slang-check-impl.h b/source/slang/slang-check-impl.h index edb199299..ba3792af7 100644 --- a/source/slang/slang-check-impl.h +++ b/source/slang/slang-check-impl.h @@ -1496,7 +1496,7 @@ public: /// by calling a function that indirectly reads the variable) will be allowed and then /// exhibit undefined behavior at runtime. /// - void _validateCircularVarDefinition(VarDeclBase* varDecl); + IntVal* _validateCircularVarDefinition(VarDeclBase* varDecl); bool shouldSkipChecking(Decl* decl, DeclCheckState state); diff --git a/source/slang/slang-reflection-api.cpp b/source/slang/slang-reflection-api.cpp index 52047a751..9295bfdf6 100644 --- a/source/slang/slang-reflection-api.cpp +++ b/source/slang/slang-reflection-api.cpp @@ -3164,6 +3164,22 @@ SLANG_API bool spReflectionVariable_HasDefaultValue(SlangReflectionVariable* inV return false; } +SLANG_API SlangResult +spReflectionVariable_GetDefaultValueInt(SlangReflectionVariable* inVar, int64_t* rs) +{ + auto decl = convert(inVar).getDecl(); + if (auto varDecl = as<VarDeclBase>(decl)) + { + if (auto constantVal = as<ConstantIntVal>(varDecl->val)) + { + *rs = constantVal->getValue(); + return 0; + } + } + + return SLANG_E_INVALID_ARG; +} + SLANG_API SlangReflectionGeneric* spReflectionVariable_GetGenericContainer( SlangReflectionVariable* var) { |
