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
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion examples/reflection-api/compute-simple.slang
Original file line number Diff line number Diff line change
@@ -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,
22 changes: 22 additions & 0 deletions examples/reflection-api/main.cpp
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions include/slang-deprecated.h
Original file line number Diff line number Diff line change
@@ -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(
5 changes: 5 additions & 0 deletions include/slang.h
Original file line number Diff line number Diff line change
@@ -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(
17 changes: 13 additions & 4 deletions source/slang/slang-check-decl.cpp
Original file line number Diff line number Diff line change
@@ -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
{
2 changes: 1 addition & 1 deletion source/slang/slang-check-impl.h
Original file line number Diff line number Diff line change
@@ -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);

16 changes: 16 additions & 0 deletions source/slang/slang-reflection-api.cpp
Original file line number Diff line number Diff line change
@@ -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)
{