Skip to content

Commit bf5e5b4

Browse files
committed
[HLSL] Buffer handle globals should not be constants
If these are constants their initializers will be removed by InstCombine. Change them to not be constants and initialize them with poison.
1 parent 73cd84a commit bf5e5b4

File tree

6 files changed

+17
-18
lines changed

6 files changed

+17
-18
lines changed

clang/lib/CodeGen/CGHLSLRuntime.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
#include "llvm/IR/Type.h"
3232
#include "llvm/IR/Value.h"
3333
#include "llvm/Support/Alignment.h"
34-
3534
#include "llvm/Support/ErrorHandling.h"
3635
#include "llvm/Support/FormatVariadic.h"
3736

@@ -245,12 +244,12 @@ void CGHLSLRuntime::addBuffer(const HLSLBufferDecl *BufDecl) {
245244
llvm::TargetExtType *TargetTy =
246245
cast<llvm::TargetExtType>(convertHLSLSpecificType(
247246
ResHandleTy, BufDecl->hasValidPackoffset() ? &Layout : nullptr));
248-
llvm::GlobalVariable *BufGV =
249-
new GlobalVariable(TargetTy, /*isConstant*/ true,
250-
GlobalValue::LinkageTypes::ExternalLinkage, nullptr,
251-
llvm::formatv("{0}{1}", BufDecl->getName(),
252-
BufDecl->isCBuffer() ? ".cb" : ".tb"),
253-
GlobalValue::NotThreadLocal);
247+
llvm::GlobalVariable *BufGV = new GlobalVariable(
248+
TargetTy, /*isConstant*/ false,
249+
GlobalValue::LinkageTypes::ExternalLinkage, PoisonValue::get(TargetTy),
250+
llvm::formatv("{0}{1}", BufDecl->getName(),
251+
BufDecl->isCBuffer() ? ".cb" : ".tb"),
252+
GlobalValue::NotThreadLocal);
254253
CGM.getModule().insertGlobalVariable(BufGV);
255254

256255
// Add globals for constant buffer elements and create metadata nodes

clang/test/CodeGenHLSL/cbuffer.hlsl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ cbuffer CBScalars : register(b1, space5) {
3939
int64_t a8;
4040
}
4141

42-
// CHECK: @CBScalars.cb = external constant target("dx.CBuffer", target("dx.Layout", %__cblayout_CBScalars,
42+
// CHECK: @CBScalars.cb = global target("dx.CBuffer", target("dx.Layout", %__cblayout_CBScalars,
4343
// CHECK-SAME: 56, 0, 8, 16, 24, 32, 36, 40, 48))
4444
// CHECK: @a1 = external addrspace(2) global float, align 4
4545
// CHECK: @a2 = external addrspace(2) global double, align 8
@@ -61,7 +61,7 @@ cbuffer CBVectors {
6161
// FIXME: add a bool vectors after llvm-project/llvm#91639 is added
6262
}
6363

64-
// CHECK: @CBVectors.cb = external constant target("dx.CBuffer", target("dx.Layout", %__cblayout_CBVectors,
64+
// CHECK: @CBVectors.cb = global target("dx.CBuffer", target("dx.Layout", %__cblayout_CBVectors,
6565
// CHECK-SAME: 136, 0, 16, 40, 48, 80, 96, 112))
6666
// CHECK: @b1 = external addrspace(2) global <3 x float>, align 16
6767
// CHECK: @b2 = external addrspace(2) global <3 x double>, align 32
@@ -82,7 +82,7 @@ cbuffer CBArrays : register(b2) {
8282
bool c8[4];
8383
}
8484

85-
// CHECK: @CBArrays.cb = external constant target("dx.CBuffer", target("dx.Layout", %__cblayout_CBArrays,
85+
// CHECK: @CBArrays.cb = global target("dx.CBuffer", target("dx.Layout", %__cblayout_CBArrays,
8686
// CHECK-SAME: 708, 0, 48, 112, 176, 224, 608, 624, 656))
8787
// CHECK: @c1 = external addrspace(2) global [3 x float], align 4
8888
// CHECK: @c2 = external addrspace(2) global [2 x <3 x double>], align 32
@@ -113,7 +113,7 @@ struct D {
113113
Empty es;
114114
};
115115

116-
// CHECK: @CBStructs.cb = external constant target("dx.CBuffer", target("dx.Layout", %__cblayout_CBStructs,
116+
// CHECK: @CBStructs.cb = global target("dx.CBuffer", target("dx.Layout", %__cblayout_CBStructs,
117117
// CHECK-SAME: 246, 0, 16, 32, 64, 144, 238, 240))
118118
// CHECK: @a = external addrspace(2) global target("dx.Layout", %A, 8, 0), align 8
119119
// CHECK: @b = external addrspace(2) global target("dx.Layout", %B, 14, 0, 8), align 8
@@ -137,7 +137,7 @@ struct Test {
137137
float a, b;
138138
};
139139

140-
// CHECK: @CBMix.cb = external constant target("dx.CBuffer", target("dx.Layout", %__cblayout_CBMix,
140+
// CHECK: @CBMix.cb = global target("dx.CBuffer", target("dx.Layout", %__cblayout_CBMix,
141141
// CHECK-SAME: 170, 0, 24, 32, 120, 128, 136, 144, 152, 160, 168))
142142
// CHECK: @test = external addrspace(2) global [2 x target("dx.Layout", %Test, 8, 0, 4)], align 4
143143
// CHECK: @f1 = external addrspace(2) global float, align 4

clang/test/CodeGenHLSL/cbuffer_and_namespaces.hlsl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
// CHECK: %"n0::n2::__cblayout_C" = type <{ float, target("dx.Layout", %"n0::Foo", 4, 0) }>
1010
// CHECK: %"n0::Foo" = type <{ float }>
1111

12-
// CHECK: @A.cb = external constant target("dx.CBuffer", target("dx.Layout", %"n0::n1::__cblayout_A", 4, 0))
12+
// CHECK: @A.cb = global target("dx.CBuffer", target("dx.Layout", %"n0::n1::__cblayout_A", 4, 0))
1313
// CHECK: @_ZN2n02n11aE = external addrspace(2) global float, align 4
1414

15-
// CHECK: @B.cb = external constant target("dx.CBuffer", target("dx.Layout", %"n0::__cblayout_B", 4, 0))
15+
// CHECK: @B.cb = global target("dx.CBuffer", target("dx.Layout", %"n0::__cblayout_B", 4, 0))
1616
// CHECK: @_ZN2n01aE = external addrspace(2) global float, align 4
1717

18-
// CHECK: @C.cb = external constant target("dx.CBuffer", target("dx.Layout", %"n0::n2::__cblayout_C", 20, 0, 16))
18+
// CHECK: @C.cb = global target("dx.CBuffer", target("dx.Layout", %"n0::n2::__cblayout_C", 20, 0, 16))
1919
// CHECK: @_ZN2n02n21aE = external addrspace(2) global float, align 4
2020
// CHECK: external addrspace(2) global target("dx.Layout", %"n0::Foo", 4, 0), align 4
2121

clang/test/CodeGenHLSL/cbuffer_with_packoffset.hlsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// CHECK: %__cblayout_CB = type <{ float, double, <2 x i32> }>
66
// CHECK: %__cblayout_CB_1 = type <{ float, <2 x float> }>
77

8-
// CHECK: @CB.cb = external constant target("dx.CBuffer", target("dx.Layout", %__cblayout_CB, 176, 16, 168, 88))
8+
// CHECK: @CB.cb = global target("dx.CBuffer", target("dx.Layout", %__cblayout_CB, 176, 16, 168, 88))
99
// CHECK: @a = external addrspace(2) global float, align 4
1010
// CHECK: @b = external addrspace(2) global double, align 8
1111
// CHECK: @c = external addrspace(2) global <2 x i32>, align 8

clang/test/CodeGenHLSL/cbuffer_with_static_global_and_function.hlsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
// CHECK: %__cblayout_A = type <{ float }>
55

6-
// CHECK: @A.cb = external constant target("dx.CBuffer", target("dx.Layout", %__cblayout_A, 4, 0))
6+
// CHECK: @A.cb = global target("dx.CBuffer", target("dx.Layout", %__cblayout_A, 4, 0))
77
// CHECK: @a = external addrspace(2) global float, align 4
88
// CHECK-DAG: @_ZL1b = internal global float 3.000000e+00, align 4
99
// CHECK-NOT: @B.cb

clang/test/CodeGenHLSL/default_cbuffer.hlsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// CHECK: %"__cblayout_$Globals" = type <{ float, float, target("dx.Layout", %__cblayout_S, 4, 0) }>
55
// CHECK: %__cblayout_S = type <{ float }>
66

7-
// CHECK-DAG: @"$Globals.cb" = external constant target("dx.CBuffer", target("dx.Layout", %"__cblayout_$Globals", 20, 0, 4, 16))
7+
// CHECK-DAG: @"$Globals.cb" = global target("dx.CBuffer", target("dx.Layout", %"__cblayout_$Globals", 20, 0, 4, 16))
88
// CHECK-DAG: @a = external addrspace(2) global float
99
// CHECK-DAG: @g = external addrspace(2) global float
1010
// CHECK-DAG: @h = external addrspace(2) global target("dx.Layout", %__cblayout_S, 4, 0), align 4

0 commit comments

Comments
 (0)