Skip to content

Commit 669992b

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 e15545c commit 669992b

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

@@ -214,12 +213,12 @@ void CGHLSLRuntime::addBuffer(const HLSLBufferDecl *BufDecl) {
214213
llvm::TargetExtType *TargetTy =
215214
cast<llvm::TargetExtType>(convertHLSLSpecificType(
216215
ResHandleTy, BufDecl->hasValidPackoffset() ? &Layout : nullptr));
217-
llvm::GlobalVariable *BufGV =
218-
new GlobalVariable(TargetTy, /*isConstant*/ true,
219-
GlobalValue::LinkageTypes::ExternalLinkage, nullptr,
220-
llvm::formatv("{0}{1}", BufDecl->getName(),
221-
BufDecl->isCBuffer() ? ".cb" : ".tb"),
222-
GlobalValue::NotThreadLocal);
216+
llvm::GlobalVariable *BufGV = new GlobalVariable(
217+
TargetTy, /*isConstant*/ false,
218+
GlobalValue::LinkageTypes::ExternalLinkage, PoisonValue::get(TargetTy),
219+
llvm::formatv("{0}{1}", BufDecl->getName(),
220+
BufDecl->isCBuffer() ? ".cb" : ".tb"),
221+
GlobalValue::NotThreadLocal);
223222
CGM.getModule().insertGlobalVariable(BufGV);
224223

225224
// 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
@@ -31,7 +31,7 @@ cbuffer CBScalars : register(b1, space5) {
3131
int64_t a8;
3232
}
3333

34-
// CHECK: @CBScalars.cb = external constant target("dx.CBuffer", target("dx.Layout", %__cblayout_CBScalars,
34+
// CHECK: @CBScalars.cb = global target("dx.CBuffer", target("dx.Layout", %__cblayout_CBScalars,
3535
// CHECK-SAME: 56, 0, 8, 16, 24, 32, 36, 40, 48))
3636
// CHECK: @a1 = external addrspace(2) global float, align 4
3737
// CHECK: @a2 = external addrspace(2) global double, align 8
@@ -53,7 +53,7 @@ cbuffer CBVectors {
5353
// FIXME: add a bool vectors after llvm-project/llvm#91639 is added
5454
}
5555

56-
// CHECK: @CBVectors.cb = external constant target("dx.CBuffer", target("dx.Layout", %__cblayout_CBVectors,
56+
// CHECK: @CBVectors.cb = global target("dx.CBuffer", target("dx.Layout", %__cblayout_CBVectors,
5757
// CHECK-SAME: 136, 0, 16, 40, 48, 80, 96, 112))
5858
// CHECK: @b1 = external addrspace(2) global <3 x float>, align 16
5959
// CHECK: @b2 = external addrspace(2) global <3 x double>, align 32
@@ -74,7 +74,7 @@ cbuffer CBArrays : register(b2) {
7474
bool c8[4];
7575
}
7676

77-
// CHECK: @CBArrays.cb = external constant target("dx.CBuffer", target("dx.Layout", %__cblayout_CBArrays,
77+
// CHECK: @CBArrays.cb = global target("dx.CBuffer", target("dx.Layout", %__cblayout_CBArrays,
7878
// CHECK-SAME: 708, 0, 48, 112, 176, 224, 608, 624, 656))
7979
// CHECK: @c1 = external addrspace(2) global [3 x float], align 4
8080
// CHECK: @c2 = external addrspace(2) global [2 x <3 x double>], align 32
@@ -105,7 +105,7 @@ struct D {
105105
Empty es;
106106
};
107107

108-
// CHECK: @CBStructs.cb = external constant target("dx.CBuffer", target("dx.Layout", %__cblayout_CBStructs,
108+
// CHECK: @CBStructs.cb = global target("dx.CBuffer", target("dx.Layout", %__cblayout_CBStructs,
109109
// CHECK-SAME: 246, 0, 16, 32, 64, 144, 238, 240))
110110
// CHECK: @a = external addrspace(2) global target("dx.Layout", %A, 8, 0), align 8
111111
// CHECK: @b = external addrspace(2) global target("dx.Layout", %B, 14, 0, 8), align 8
@@ -129,7 +129,7 @@ struct Test {
129129
float a, b;
130130
};
131131

132-
// CHECK: @CBMix.cb = external constant target("dx.CBuffer", target("dx.Layout", %__cblayout_CBMix,
132+
// CHECK: @CBMix.cb = global target("dx.CBuffer", target("dx.Layout", %__cblayout_CBMix,
133133
// CHECK-SAME: 170, 0, 24, 32, 120, 128, 136, 144, 152, 160, 168))
134134
// CHECK: @test = external addrspace(2) global [2 x target("dx.Layout", %Test, 8, 0, 4)], align 4
135135
// 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
@@ -4,7 +4,7 @@
44

55
// CHECK: %__cblayout_CB = type <{ float, double, <2 x i32> }>
66

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