Skip to content

Commit 83e4cd4

Browse files
authored
Fix crash in constant initializer lowering for global variable. (microsoft#6820)
In microsoft#6814, we modified the compiler to avoid generating bad code in some cases for array initializers. However, this caused a crash in the case where the initializer does not use a GEP expression for addressing because the `GV` will be null. I considered setting `GV` to the value in the `store` pointer operand, but it looked like `GV` was also checked elsewhere for null and did not want to modify the behavior of the code in other places. The fix is to check if we found a global variable before validating the array case.
1 parent e0fbce7 commit 83e4cd4

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

tools/clang/lib/CodeGen/CGHLSLMSFinishCodeGen.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2979,11 +2979,13 @@ bool BuildImmInit(Function *Ctor) {
29792979
}
29802980
// If initializing an array, make sure init value type matches array
29812981
// element type
2982-
llvm::Type *GVElemTy = GV->getType()->getElementType();
2983-
if (llvm::ArrayType *AT = dyn_cast<llvm::ArrayType>(GVElemTy)) {
2984-
llvm::Type *ElTy = AT->getElementType();
2985-
if (V->getType() != ElTy)
2986-
return false;
2982+
if (GV) {
2983+
llvm::Type *GVElemTy = GV->getType()->getElementType();
2984+
if (llvm::ArrayType *AT = dyn_cast<llvm::ArrayType>(GVElemTy)) {
2985+
llvm::Type *ElTy = AT->getElementType();
2986+
if (V->getType() != ElTy)
2987+
return false;
2988+
}
29872989
}
29882990
ImmList.emplace_back(cast<Constant>(V));
29892991
} else {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// RUN: %dxc -T ps_6_0 %s | FileCheck %s
2+
3+
// Regression test to make sure we can compile a global constant with
4+
// initializer. Previously this shader would crash in the compiler.
5+
6+
// CHECK: define void @main
7+
// CHECK: call void @dx.op.storeOutput.f32(i32 5, i32 0, i32 0, i8 0, float 0x3FE14A2800000000)
8+
9+
static const float val = cos(1.0f);
10+
11+
float main() : SV_Target
12+
{
13+
return val;
14+
}

0 commit comments

Comments
 (0)