Skip to content

Commit 86669cb

Browse files
enniscsyonghe
andauthored
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>
1 parent 519e866 commit 86669cb

File tree

7 files changed

+63
-6
lines changed

7 files changed

+63
-6
lines changed

examples/reflection-api/compute-simple.slang

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
// compute-simple.slang
22

3+
static const uint THREADGROUP_SIZE_X = 8;
4+
static const uint THREADGROUP_SIZE_Y = THREADGROUP_SIZE_X;
5+
36
struct ImageProcessingOptions
47
{
58
float3 tintColor;
@@ -10,7 +13,7 @@ struct ImageProcessingOptions
1013
}
1114

1215
[shader("compute")]
13-
[numthreads(8, 8)]
16+
[numthreads(THREADGROUP_SIZE_X, THREADGROUP_SIZE_Y)]
1417
void processImage(
1518
uint3 threadID : SV_DispatchThreadID,
1619
uniform Texture2D inputImage,

examples/reflection-api/main.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,21 @@ struct ReflectingPrinting
114114

115115
List<ComPtr<slang::IComponentType>> componentsToLink;
116116

117+
// ### Variable decls
118+
//
119+
key("global constants");
120+
WITH_ARRAY()
121+
for (auto decl : module->getModuleReflection()->getChildren())
122+
{
123+
if (auto varDecl = decl->asVariable(); varDecl &&
124+
varDecl->findModifier(slang::Modifier::Const) &&
125+
varDecl->findModifier(slang::Modifier::Static))
126+
{
127+
element();
128+
printVariable(varDecl);
129+
}
130+
}
131+
117132
// ### Finding Entry Points
118133
//
119134

@@ -213,6 +228,13 @@ struct ReflectingPrinting
213228
printQuotedString(name);
214229
key("type");
215230
printType(type);
231+
232+
int64_t value;
233+
if (SLANG_SUCCEEDED(variable->getDefaultValueInt(&value)))
234+
{
235+
key("value");
236+
printf("%" PRId64, value);
237+
}
216238
}
217239

218240
// ### Types

include/slang-deprecated.h

+2
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,8 @@ extern "C"
659659
SlangSession* globalSession,
660660
char const* name);
661661
SLANG_API bool spReflectionVariable_HasDefaultValue(SlangReflectionVariable* inVar);
662+
SLANG_API SlangResult
663+
spReflectionVariable_GetDefaultValueInt(SlangReflectionVariable* inVar, int64_t* rs);
662664
SLANG_API SlangReflectionGeneric* spReflectionVariable_GetGenericContainer(
663665
SlangReflectionVariable* var);
664666
SLANG_API SlangReflectionVariable* spReflectionVariable_applySpecializations(

include/slang.h

+5
Original file line numberDiff line numberDiff line change
@@ -2832,6 +2832,11 @@ struct VariableReflection
28322832
return spReflectionVariable_HasDefaultValue((SlangReflectionVariable*)this);
28332833
}
28342834

2835+
SlangResult getDefaultValueInt(int64_t* value)
2836+
{
2837+
return spReflectionVariable_GetDefaultValueInt((SlangReflectionVariable*)this, value);
2838+
}
2839+
28352840
GenericReflection* getGenericContainer()
28362841
{
28372842
return (GenericReflection*)spReflectionVariable_GetGenericContainer(

source/slang/slang-check-decl.cpp

+13-4
Original file line numberDiff line numberDiff line change
@@ -1430,7 +1430,7 @@ bool SemanticsVisitor::shouldSkipChecking(Decl* decl, DeclCheckState state)
14301430
return false;
14311431
}
14321432

1433-
void SemanticsVisitor::_validateCircularVarDefinition(VarDeclBase* varDecl)
1433+
IntVal* SemanticsVisitor::_validateCircularVarDefinition(VarDeclBase* varDecl)
14341434
{
14351435
// The easiest way to test if the declaration is circular is to
14361436
// validate it as a constant.
@@ -1444,8 +1444,11 @@ void SemanticsVisitor::_validateCircularVarDefinition(VarDeclBase* varDecl)
14441444
//
14451445
//
14461446
if (!isScalarIntegerType(varDecl->type))
1447-
return;
1448-
tryConstantFoldDeclRef(DeclRef<VarDeclBase>(varDecl), ConstantFoldingKind::LinkTime, nullptr);
1447+
return nullptr;
1448+
return tryConstantFoldDeclRef(
1449+
DeclRef<VarDeclBase>(varDecl),
1450+
ConstantFoldingKind::LinkTime,
1451+
nullptr);
14491452
}
14501453

14511454
void SemanticsDeclModifiersVisitor::visitStructDecl(StructDecl* structDecl)
@@ -2350,7 +2353,13 @@ void SemanticsDeclBodyVisitor::checkVarDeclCommon(VarDeclBase* varDecl)
23502353
// a constant with a circular definition.
23512354
//
23522355
varDecl->setCheckState(DeclCheckState::DefinitionChecked);
2353-
_validateCircularVarDefinition(varDecl);
2356+
2357+
// Update constant value
2358+
//
2359+
if (!varDecl->val)
2360+
{
2361+
varDecl->val = _validateCircularVarDefinition(varDecl);
2362+
}
23542363
}
23552364
else
23562365
{

source/slang/slang-check-impl.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1496,7 +1496,7 @@ struct SemanticsVisitor : public SemanticsContext
14961496
/// by calling a function that indirectly reads the variable) will be allowed and then
14971497
/// exhibit undefined behavior at runtime.
14981498
///
1499-
void _validateCircularVarDefinition(VarDeclBase* varDecl);
1499+
IntVal* _validateCircularVarDefinition(VarDeclBase* varDecl);
15001500

15011501
bool shouldSkipChecking(Decl* decl, DeclCheckState state);
15021502

source/slang/slang-reflection-api.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -3164,6 +3164,22 @@ SLANG_API bool spReflectionVariable_HasDefaultValue(SlangReflectionVariable* inV
31643164
return false;
31653165
}
31663166

3167+
SLANG_API SlangResult
3168+
spReflectionVariable_GetDefaultValueInt(SlangReflectionVariable* inVar, int64_t* rs)
3169+
{
3170+
auto decl = convert(inVar).getDecl();
3171+
if (auto varDecl = as<VarDeclBase>(decl))
3172+
{
3173+
if (auto constantVal = as<ConstantIntVal>(varDecl->val))
3174+
{
3175+
*rs = constantVal->getValue();
3176+
return 0;
3177+
}
3178+
}
3179+
3180+
return SLANG_E_INVALID_ARG;
3181+
}
3182+
31673183
SLANG_API SlangReflectionGeneric* spReflectionVariable_GetGenericContainer(
31683184
SlangReflectionVariable* var)
31693185
{

0 commit comments

Comments
 (0)