From 74f3b752acb46840452257656672b61c01aff4c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandre=20Bl=C3=A9ron?= Date: Sat, 15 Feb 2025 15:06:43 +0100 Subject: [PATCH 1/4] 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. --- examples/reflection-api/compute-simple.slang | 5 ++++- examples/reflection-api/main.cpp | 23 ++++++++++++++++++++ include/slang-deprecated.h | 3 +++ include/slang.h | 5 +++++ source/slang/slang-reflection-api.cpp | 16 ++++++++++++++ 5 files changed, 51 insertions(+), 1 deletion(-) diff --git a/examples/reflection-api/compute-simple.slang b/examples/reflection-api/compute-simple.slang index 3d88721805..00d45d857b 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 c072c641b9..38902f0613 100644 --- a/examples/reflection-api/main.cpp +++ b/examples/reflection-api/main.cpp @@ -114,6 +114,22 @@ struct ReflectingPrinting List> 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 +229,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 82d81af75c..55b018734d 100644 --- a/include/slang-deprecated.h +++ b/include/slang-deprecated.h @@ -659,6 +659,9 @@ 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 af635e3c42..54647b830b 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-reflection-api.cpp b/source/slang/slang-reflection-api.cpp index 52047a7518..9295bfdf6d 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(decl)) + { + if (auto constantVal = as(varDecl->val)) + { + *rs = constantVal->getValue(); + return 0; + } + } + + return SLANG_E_INVALID_ARG; +} + SLANG_API SlangReflectionGeneric* spReflectionVariable_GetGenericContainer( SlangReflectionVariable* var) { From 76707c05b1bce0d32f1d35acbcb18c42829582d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandre=20Bl=C3=A9ron?= Date: Sat, 15 Feb 2025 21:49:44 +0100 Subject: [PATCH 2/4] Update VarDecl folded constant value during DeclBodyVisitor Constant folding for integer values is already done internally by _validateCircularVarDefinition, this just reuses the result. --- source/slang/slang-check-decl.cpp | 15 +++++++++++---- source/slang/slang-check-impl.h | 2 +- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/source/slang/slang-check-decl.cpp b/source/slang/slang-check-decl.cpp index 0c42817c81..ddd80f7140 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,8 @@ void SemanticsVisitor::_validateCircularVarDefinition(VarDeclBase* varDecl) // // if (!isScalarIntegerType(varDecl->type)) - return; - tryConstantFoldDeclRef(DeclRef(varDecl), ConstantFoldingKind::LinkTime, nullptr); + return nullptr; + return tryConstantFoldDeclRef(DeclRef(varDecl), ConstantFoldingKind::LinkTime, nullptr); } void SemanticsDeclModifiersVisitor::visitStructDecl(StructDecl* structDecl) @@ -2350,7 +2350,14 @@ void SemanticsDeclBodyVisitor::checkVarDeclCommon(VarDeclBase* varDecl) // a constant with a circular definition. // varDecl->setCheckState(DeclCheckState::DefinitionChecked); - _validateCircularVarDefinition(varDecl); + auto intVal = _validateCircularVarDefinition(varDecl); + + // Update constant value + // + if (!varDecl->val) + { + varDecl->val = intVal; + } } else { diff --git a/source/slang/slang-check-impl.h b/source/slang/slang-check-impl.h index edb199299f..ba3792af7f 100644 --- a/source/slang/slang-check-impl.h +++ b/source/slang/slang-check-impl.h @@ -1496,7 +1496,7 @@ struct SemanticsVisitor : public SemanticsContext /// 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); From 5804ff12864546ff81c380378b6b2a1100b3119c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandre=20Bl=C3=A9ron?= Date: Wed, 19 Feb 2025 22:55:56 +0100 Subject: [PATCH 3/4] Address review comments & formatting --- examples/reflection-api/main.cpp | 13 ++++++------- source/slang/slang-check-decl.cpp | 3 +-- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/examples/reflection-api/main.cpp b/examples/reflection-api/main.cpp index 38902f0613..2ff4475bfb 100644 --- a/examples/reflection-api/main.cpp +++ b/examples/reflection-api/main.cpp @@ -115,15 +115,14 @@ struct ReflectingPrinting List> 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) - ) + 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); diff --git a/source/slang/slang-check-decl.cpp b/source/slang/slang-check-decl.cpp index ddd80f7140..cc91de173b 100644 --- a/source/slang/slang-check-decl.cpp +++ b/source/slang/slang-check-decl.cpp @@ -2350,13 +2350,12 @@ void SemanticsDeclBodyVisitor::checkVarDeclCommon(VarDeclBase* varDecl) // a constant with a circular definition. // varDecl->setCheckState(DeclCheckState::DefinitionChecked); - auto intVal = _validateCircularVarDefinition(varDecl); // Update constant value // if (!varDecl->val) { - varDecl->val = intVal; + varDecl->val = _validateCircularVarDefinition(varDecl); } } else From 0047cefd7b268a2430bb13e007ddd9cfc07e1918 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandre=20Bl=C3=A9ron?= Date: Fri, 21 Feb 2025 21:10:31 +0100 Subject: [PATCH 4/4] Formatting --- include/slang-deprecated.h | 3 +-- source/slang/slang-check-decl.cpp | 5 ++++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/include/slang-deprecated.h b/include/slang-deprecated.h index 55b018734d..df6e41488d 100644 --- a/include/slang-deprecated.h +++ b/include/slang-deprecated.h @@ -660,8 +660,7 @@ extern "C" char const* name); SLANG_API bool spReflectionVariable_HasDefaultValue(SlangReflectionVariable* inVar); SLANG_API SlangResult - spReflectionVariable_GetDefaultValueInt(SlangReflectionVariable* inVar, - int64_t* rs); + spReflectionVariable_GetDefaultValueInt(SlangReflectionVariable* inVar, int64_t* rs); SLANG_API SlangReflectionGeneric* spReflectionVariable_GetGenericContainer( SlangReflectionVariable* var); SLANG_API SlangReflectionVariable* spReflectionVariable_applySpecializations( diff --git a/source/slang/slang-check-decl.cpp b/source/slang/slang-check-decl.cpp index cc91de173b..1659a161bc 100644 --- a/source/slang/slang-check-decl.cpp +++ b/source/slang/slang-check-decl.cpp @@ -1445,7 +1445,10 @@ IntVal* SemanticsVisitor::_validateCircularVarDefinition(VarDeclBase* varDecl) // if (!isScalarIntegerType(varDecl->type)) return nullptr; - return tryConstantFoldDeclRef(DeclRef(varDecl), ConstantFoldingKind::LinkTime, nullptr); + return tryConstantFoldDeclRef( + DeclRef(varDecl), + ConstantFoldingKind::LinkTime, + nullptr); } void SemanticsDeclModifiersVisitor::visitStructDecl(StructDecl* structDecl)