Skip to content

Commit 4b5f425

Browse files
authoredFeb 27, 2025
Merge branch 'master' into cheneym2/precompiledtestcrash
2 parents 2c838ff + 6e862bb commit 4b5f425

10 files changed

+135
-23
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-glslang/slang-glslang.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ extern "C"
176176

177177
spvtools::ValidatorOptions options;
178178
options.SetScalarBlockLayout(true);
179+
options.SetFriendlyNames(true);
179180

180181
spvtools::SpirvTools tools(target_env);
181182
tools.SetMessageConsumer(validationMessageConsumer);
@@ -198,6 +199,7 @@ extern "C"
198199
options |= SPV_BINARY_TO_TEXT_OPTION_COMMENT;
199200
options |= SPV_BINARY_TO_TEXT_OPTION_PRINT;
200201
options |= SPV_BINARY_TO_TEXT_OPTION_COLOR;
202+
options |= SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES;
201203

202204
spv_diagnostic diagnostic = nullptr;
203205
spv_context context = spvContextCreate(kDefaultEnvironment);

‎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-emit-spirv.cpp

+54-17
Original file line numberDiff line numberDiff line change
@@ -3118,31 +3118,56 @@ struct SPIRVEmitContext : public SourceEmitterBase, public SPIRVEmitSharedContex
31183118
return (isSpirv16OrLater() || m_useDemoteToHelperInvocationExtension);
31193119
}
31203120

3121-
SpvInst* emitMemorySemanticMask(IRInst* inst)
3121+
SpvInst* emitMemorySemanticMask(IRInst* memoryOrderInst, IRInst* ptrInst)
31223122
{
3123-
IRBuilder builder(inst);
3124-
auto memoryOrder = (IRMemoryOrder)getIntVal(inst);
3125-
switch (memoryOrder)
3123+
IRBuilder builder(memoryOrderInst);
3124+
auto memoryOrder = (IRMemoryOrder)getIntVal(memoryOrderInst);
3125+
if (memoryOrder == kIRMemoryOrder_Relaxed)
31263126
{
3127-
case kIRMemoryOrder_Relaxed:
31283127
return emitIntConstant(
31293128
IRIntegerValue{SpvMemorySemanticsMaskNone},
31303129
builder.getUIntType());
3130+
}
3131+
uint32_t memoryClass = 0;
3132+
if (auto ptrType = as<IRPtrTypeBase>(ptrInst->getDataType()))
3133+
{
3134+
if (ptrType->hasAddressSpace())
3135+
{
3136+
switch (ptrType->getAddressSpace())
3137+
{
3138+
case AddressSpace::StorageBuffer:
3139+
case AddressSpace::UserPointer:
3140+
memoryClass = SpvMemorySemanticsUniformMemoryMask;
3141+
break;
3142+
case AddressSpace::Image:
3143+
memoryClass = SpvMemorySemanticsImageMemoryMask;
3144+
break;
3145+
case AddressSpace::Output:
3146+
memoryClass = SpvMemorySemanticsOutputMemoryKHRMask;
3147+
break;
3148+
case AddressSpace::GroupShared:
3149+
memoryClass = SpvMemorySemanticsWorkgroupMemoryMask;
3150+
break;
3151+
}
3152+
}
3153+
}
3154+
switch (memoryOrder)
3155+
{
31313156
case kIRMemoryOrder_Acquire:
31323157
return emitIntConstant(
3133-
IRIntegerValue{SpvMemorySemanticsAcquireMask},
3158+
IRIntegerValue{SpvMemorySemanticsAcquireMask | memoryClass},
31343159
builder.getUIntType());
31353160
case kIRMemoryOrder_Release:
31363161
return emitIntConstant(
3137-
IRIntegerValue{SpvMemorySemanticsReleaseMask},
3162+
IRIntegerValue{SpvMemorySemanticsReleaseMask | memoryClass},
31383163
builder.getUIntType());
31393164
case kIRMemoryOrder_AcquireRelease:
31403165
return emitIntConstant(
3141-
IRIntegerValue{SpvMemorySemanticsAcquireReleaseMask},
3166+
IRIntegerValue{SpvMemorySemanticsAcquireReleaseMask | memoryClass},
31423167
builder.getUIntType());
31433168
case kIRMemoryOrder_SeqCst:
31443169
return emitIntConstant(
3145-
IRIntegerValue{SpvMemorySemanticsSequentiallyConsistentMask},
3170+
IRIntegerValue{SpvMemorySemanticsSequentiallyConsistentMask | memoryClass},
31463171
builder.getUIntType());
31473172
default:
31483173
SLANG_UNEXPECTED("unhandled memory order");
@@ -3805,7 +3830,8 @@ struct SPIRVEmitContext : public SourceEmitterBase, public SPIRVEmitSharedContex
38053830
IRBuilder builder{inst};
38063831
const auto memoryScope =
38073832
emitIntConstant(IRIntegerValue{SpvScopeDevice}, builder.getUIntType());
3808-
const auto memorySemantics = emitMemorySemanticMask(inst->getOperand(1));
3833+
const auto memorySemantics =
3834+
emitMemorySemanticMask(inst->getOperand(1), inst->getOperand(0));
38093835
result = emitOpAtomicIIncrement(
38103836
parent,
38113837
inst,
@@ -3821,7 +3847,8 @@ struct SPIRVEmitContext : public SourceEmitterBase, public SPIRVEmitSharedContex
38213847
IRBuilder builder{inst};
38223848
const auto memoryScope =
38233849
emitIntConstant(IRIntegerValue{SpvScopeDevice}, builder.getUIntType());
3824-
const auto memorySemantics = emitMemorySemanticMask(inst->getOperand(1));
3850+
const auto memorySemantics =
3851+
emitMemorySemanticMask(inst->getOperand(1), inst->getOperand(0));
38253852
result = emitOpAtomicIDecrement(
38263853
parent,
38273854
inst,
@@ -3837,7 +3864,8 @@ struct SPIRVEmitContext : public SourceEmitterBase, public SPIRVEmitSharedContex
38373864
IRBuilder builder{inst};
38383865
const auto memoryScope =
38393866
emitIntConstant(IRIntegerValue{SpvScopeDevice}, builder.getUIntType());
3840-
const auto memorySemantics = emitMemorySemanticMask(inst->getOperand(1));
3867+
const auto memorySemantics =
3868+
emitMemorySemanticMask(inst->getOperand(1), inst->getOperand(0));
38413869
result = emitOpAtomicLoad(
38423870
parent,
38433871
inst,
@@ -3853,7 +3881,8 @@ struct SPIRVEmitContext : public SourceEmitterBase, public SPIRVEmitSharedContex
38533881
IRBuilder builder{inst};
38543882
const auto memoryScope =
38553883
emitIntConstant(IRIntegerValue{SpvScopeDevice}, builder.getUIntType());
3856-
const auto memorySemantics = emitMemorySemanticMask(inst->getOperand(2));
3884+
const auto memorySemantics =
3885+
emitMemorySemanticMask(inst->getOperand(2), inst->getOperand(0));
38573886
result = emitOpAtomicStore(
38583887
parent,
38593888
inst,
@@ -3869,7 +3898,8 @@ struct SPIRVEmitContext : public SourceEmitterBase, public SPIRVEmitSharedContex
38693898
IRBuilder builder{inst};
38703899
const auto memoryScope =
38713900
emitIntConstant(IRIntegerValue{SpvScopeDevice}, builder.getUIntType());
3872-
const auto memorySemantics = emitMemorySemanticMask(inst->getOperand(2));
3901+
const auto memorySemantics =
3902+
emitMemorySemanticMask(inst->getOperand(2), inst->getOperand(0));
38733903
result = emitOpAtomicExchange(
38743904
parent,
38753905
inst,
@@ -3886,8 +3916,10 @@ struct SPIRVEmitContext : public SourceEmitterBase, public SPIRVEmitSharedContex
38863916
IRBuilder builder{inst};
38873917
const auto memoryScope =
38883918
emitIntConstant(IRIntegerValue{SpvScopeDevice}, builder.getUIntType());
3889-
const auto memorySemanticsEqual = emitMemorySemanticMask(inst->getOperand(3));
3890-
const auto memorySemanticsUnequal = emitMemorySemanticMask(inst->getOperand(4));
3919+
const auto memorySemanticsEqual =
3920+
emitMemorySemanticMask(inst->getOperand(3), inst->getOperand(0));
3921+
const auto memorySemanticsUnequal =
3922+
emitMemorySemanticMask(inst->getOperand(4), inst->getOperand(0));
38913923
result = emitOpAtomicCompareExchange(
38923924
parent,
38933925
inst,
@@ -3912,7 +3944,8 @@ struct SPIRVEmitContext : public SourceEmitterBase, public SPIRVEmitSharedContex
39123944
IRBuilder builder{inst};
39133945
const auto memoryScope =
39143946
emitIntConstant(IRIntegerValue{SpvScopeDevice}, builder.getUIntType());
3915-
const auto memorySemantics = emitMemorySemanticMask(inst->getOperand(2));
3947+
const auto memorySemantics =
3948+
emitMemorySemanticMask(inst->getOperand(2), inst->getOperand(0));
39163949
bool negateOperand = false;
39173950
auto spvOp = getSpvAtomicOp(inst, negateOperand);
39183951
auto operand = inst->getOperand(1);
@@ -5309,6 +5342,10 @@ struct SPIRVEmitContext : public SourceEmitterBase, public SPIRVEmitSharedContex
53095342
{
53105343
return getBuiltinGlobalVar(inst->getFullType(), SpvBuiltInInstanceIndex, inst);
53115344
}
5345+
else if (semanticName == "sv_baseinstanceid")
5346+
{
5347+
return getBuiltinGlobalVar(inst->getFullType(), SpvBuiltInBaseInstance, inst);
5348+
}
53125349
else if (semanticName == "sv_isfrontface")
53135350
{
53145351
return getBuiltinGlobalVar(inst->getFullType(), SpvBuiltInFrontFacing, inst);

‎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
{

‎tests/spirv/atomic-memory-class.slang

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//TEST:SIMPLE(filecheck=CHECK): -target spirv
2+
3+
uniform Atomic<int>* pInt;
4+
RWStructuredBuffer<Atomic<int>> bInt;
5+
static groupshared Atomic<int> sInt;
6+
7+
[numthreads(4,1,1)]
8+
void computeMain()
9+
{
10+
// CHECK: OpAtomicLoad {{.*}} %uint_258
11+
// CHECK: OpAtomicStore {{.*}} %uint_68
12+
// CHECK: OpAtomicStore {{.*}} %uint_260
13+
let v = sInt.load(MemoryOrder.Acquire);
14+
pInt->store(v, MemoryOrder.Release);
15+
sInt.store(v, MemoryOrder.Release);
16+
}

0 commit comments

Comments
 (0)