Skip to content

Commit a9ce752

Browse files
authoredJan 23, 2025
Fix incorrect resolve of specialization instance (shader-slang#6162)
* Fix incorrect resolve of specialization instance While checking the uninitialized variables, we were not resolving the specialized instance correctly. This commit repeats the resolve while the result is a specialization instance. A new test is added for this: tests/diagnostics/uninitialized-generic.slang After the problem is fixed, it revealed another problem in existing tests: tests/compute/nested-generics2.slang tests/diagnostics/uninitialized-local-variables.slang When a struct has a member variable whose type is a generic type, we cannot iterate over its member variables yet, because the type is unknown until the generic function/struct is specialized. We will have to give up checking for these cases.
1 parent 8000e0e commit a9ce752

File tree

3 files changed

+41
-13
lines changed

3 files changed

+41
-13
lines changed
 

‎source/slang/slang-ir-use-uninitialized-values.cpp

+6-9
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,6 @@ static bool isDifferentiableFunc(IRInst* func)
139139
return false;
140140
}
141141

142-
static IRInst* resolveSpecialization(IRSpecialize* spec)
143-
{
144-
IRInst* base = spec->getBase();
145-
IRGeneric* generic = as<IRGeneric>(base);
146-
return findInnerMostGenericReturnVal(generic);
147-
}
148-
149142
// The `upper` field contains the struct that the type is
150143
// is contained in. It is used to check for empty structs.
151144
static bool canIgnoreType(IRType* type, IRType* upper)
@@ -174,6 +167,10 @@ static bool canIgnoreType(IRType* type, IRType* upper)
174167
if (as<IRInterfaceType>(type))
175168
return true;
176169

170+
// We don't know what type it will be yet.
171+
if (as<IRParam>(type))
172+
return true;
173+
177174
// For pointers, check the value type (primarily for globals)
178175
if (auto ptr = as<IRPtrType>(type))
179176
{
@@ -188,7 +185,7 @@ static bool canIgnoreType(IRType* type, IRType* upper)
188185
// In the case of specializations, check returned type
189186
if (auto spec = as<IRSpecialize>(type))
190187
{
191-
IRInst* inner = resolveSpecialization(spec);
188+
IRInst* inner = getResolvedInstForDecorations(spec);
192189
IRType* innerType = as<IRType>(inner);
193190
return canIgnoreType(innerType, upper);
194191
}
@@ -231,7 +228,7 @@ static InstructionUsageType getCallUsageType(IRCall* call, IRInst* inst)
231228
IRFunc* ftn = nullptr;
232229
IRFuncType* ftype = nullptr;
233230
if (auto spec = as<IRSpecialize>(callee))
234-
ftn = as<IRFunc>(resolveSpecialization(spec));
231+
ftn = as<IRFunc>(getResolvedInstForDecorations(spec));
235232

236233
// Differentiable functions are mostly ignored, treated as having inout parameters
237234
else if (as<IRForwardDifferentiate>(callee))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//TEST:SIMPLE(filecheck=CHK): -target spirv -entry computeMain
2+
3+
//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer
4+
RWStructuredBuffer<float> outputBuffer;
5+
6+
groupshared float4 gsVar;
7+
8+
__generic<TYPE1 : __BuiltinArithmeticType>
9+
struct MyContainer
10+
{
11+
__generic<TYPE2 : __BuiltinArithmeticType>
12+
void store(__ref vector<TYPE2,4> v)
13+
{
14+
v[0] = TYPE2(0);
15+
v[1] = TYPE2(1);
16+
v[2] = TYPE2(2);
17+
v[3] = TYPE2(3);
18+
}
19+
};
20+
21+
[Shader("compute")]
22+
[NumThreads(1, 1, 1)]
23+
void computeMain(int3 dispatchThreadID : SV_DispatchThreadID)
24+
{
25+
MyContainer<float> obj;
26+
obj.store(gsVar);
27+
28+
// CHK-NOT:warning 41017:
29+
outputBuffer[0] = gsVar.x;
30+
}

‎tests/diagnostics/uninitialized-local-variables.slang

+5-4
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,14 @@ int use_undefined_value(int k)
3838
return x;
3939
}
4040

41-
// Returning uninitialized values
41+
// We don't know the exact type of T yet.
42+
// T may not have any members, and it may not need any initialization.
4243
__generic<T>
4344
T generic_undefined_return()
4445
{
45-
T x;
46-
//CHK-DAG: warning 41016: use of uninitialized variable 'x'
47-
return x;
46+
T y;
47+
//CHK-NOT: warning 41016: use of uninitialized variable 'y'
48+
return y;
4849
}
4950

5051
// Array variables

0 commit comments

Comments
 (0)
Please sign in to comment.