Skip to content

Commit 7a4cdea

Browse files
authored
Add nullptr checks to handle undefined arrays (microsoft#5872)
When a variable declaration is not found, an error is emitted, and a null value returned. Not all function know what to do with that null value. I fix this by adding a few extra nullptr checks so that the compiler can unwind the stack and return gracefully. Fixes [SPIR-V] `-Wundefined-internal` SIGSEGVs the compiler when `-spirv` output microsoft#5821
1 parent 956fa34 commit 7a4cdea

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

tools/clang/lib/SPIRV/InitListHandler.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,12 @@ void InitListHandler::flatten(const InitListExpr *expr) {
8080
init->IgnoreParenNoopCasts(theEmitter.getASTContext()))) {
8181
flatten(subInitList);
8282
} else {
83-
initializers.push_back(theEmitter.loadIfGLValue(init));
83+
auto *initializer = theEmitter.loadIfGLValue(init);
84+
if (!initializer) {
85+
initializers.clear();
86+
return;
87+
}
88+
initializers.push_back(initializer);
8489
}
8590
}
8691
}
@@ -253,6 +258,10 @@ InitListHandler::createInitForBuiltinType(QualType type,
253258
while (tryToSplitStruct() || tryToSplitConstantArray())
254259
;
255260

261+
if (initializers.empty()) {
262+
return nullptr;
263+
}
264+
256265
auto init = initializers.back();
257266
initializers.pop_back();
258267

tools/clang/lib/SPIRV/SpirvEmitter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2688,7 +2688,7 @@ SpirvEmitter::doArraySubscriptExpr(const ArraySubscriptExpr *expr,
26882688
SourceRange range =
26892689
(rangeOverride != SourceRange()) ? rangeOverride : expr->getSourceRange();
26902690

2691-
if (indices.empty()) {
2691+
if (!info || indices.empty()) {
26922692
return info;
26932693
}
26942694

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// RUN: not %dxc -T ps_6_7 -E main -fcgl %s -spirv 2>&1 | FileCheck %s
2+
3+
template<uint32_t N, typename T=double>
4+
struct GaussLegendreValues
5+
{
6+
const static T wi[N];
7+
};
8+
9+
static int test_value = 1567;
10+
11+
float main() : SV_Target
12+
{
13+
return float(GaussLegendreValues<2>::wi[test_value]);
14+
}
15+
16+
// CHECK: :6:20: warning: variable 'GaussLegendreValues<2, double>::wi' has internal linkage but is not defined [-Wundefined-internal]
17+
// CHECK-NEXT: const static T wi[N];
18+
// CHECK: :13:42: note: used here
19+
// CHECK-NEXT: return float(GaussLegendreValues<2>::wi[test_value]);
20+
// CHECK: :6:20: fatal error: found unregistered decl
21+
// CHECK-NEXT: const static T wi[N];

0 commit comments

Comments
 (0)