Skip to content

Commit 882082a

Browse files
authored
[HLSL] Buffer handle globals should not be constants (#130231)
If these are constants their initializers will be removed by InstCombine. Change them to not be constants and initialize them with poison.
1 parent dd3addf commit 882082a

File tree

8 files changed

+30
-31
lines changed

8 files changed

+30
-31
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: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
// CHECK: %__cblayout_CBScalars = type <{ float, double, half, i64, i32, i16, i32, i64 }>
55
// CHECK: %__cblayout_CBVectors = type <{ <3 x float>, <3 x double>, <2 x half>, <3 x i64>, <4 x i32>, <3 x i16>, <3 x i64> }>
66
// CHECK: %__cblayout_CBArrays = type <{ [3 x float], [2 x <3 x double>], [2 x [2 x half]], [3 x i64], [2 x [3 x [4 x <4 x i32>]]], [1 x i16], [2 x i64], [4 x i32] }>
7-
// CHECK: %__cblayout_CBStructs = type <{ target("dx.Layout", %A, 8, 0), target("dx.Layout", %B, 14, 0, 8),
8-
// CHECK-SAME: target("dx.Layout", %C, 24, 0, 16), [5 x target("dx.Layout", %A, 8, 0)],
7+
// CHECK: %__cblayout_CBStructs = type <{ target("dx.Layout", %A, 8, 0), target("dx.Layout", %B, 14, 0, 8),
8+
// CHECK-SAME: target("dx.Layout", %C, 24, 0, 16), [5 x target("dx.Layout", %A, 8, 0)],
99
// CHECK-SAME: target("dx.Layout", %__cblayout_D, 94, 0), half, <3 x i16> }>
1010

1111
// CHECK: %A = type <{ <2 x float> }>
@@ -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
@@ -161,9 +161,9 @@ cbuffer CBMix {
161161
float f7;
162162
vector<double,1> f8;
163163
uint16_t f9;
164-
};
164+
};
165165

166-
// CHECK: @CB_A.cb = external constant target("dx.CBuffer", target("dx.Layout", %__cblayout_CB_A, 188, 0, 32, 76, 80, 120, 128, 144, 160, 182))
166+
// CHECK: @CB_A.cb = global target("dx.CBuffer", target("dx.Layout", %__cblayout_CB_A, 188, 0, 32, 76, 80, 120, 128, 144, 160, 182))
167167

168168
cbuffer CB_A {
169169
double B0[2];
@@ -177,7 +177,7 @@ cbuffer CB_A {
177177
half3 B8;
178178
}
179179

180-
// CHECK: @CB_B.cb = external constant target("dx.CBuffer", target("dx.Layout", %__cblayout_CB_B, 94, 0, 88))
180+
// CHECK: @CB_B.cb = global target("dx.CBuffer", target("dx.Layout", %__cblayout_CB_B, 94, 0, 88))
181181
cbuffer CB_B {
182182
double3 B9[3];
183183
half3 B10;
@@ -212,7 +212,7 @@ struct G {
212212
half C3;
213213
};
214214

215-
// CHECK: @CB_C.cb = external constant target("dx.CBuffer", target("dx.Layout", %__cblayout_CB_C, 400, 0, 16, 112, 128, 392))
215+
// CHECK: @CB_C.cb = global target("dx.CBuffer", target("dx.Layout", %__cblayout_CB_C, 400, 0, 16, 112, 128, 392))
216216
cbuffer CB_C {
217217
int D0;
218218
F D1;
@@ -254,10 +254,10 @@ void main() {
254254
// CHECK: ![[CBVECTORS]] = !{ptr @CBVectors.cb, ptr addrspace(2) @b1, ptr addrspace(2) @b2, ptr addrspace(2) @b3, ptr addrspace(2) @b4,
255255
// CHECK-SAME: ptr addrspace(2) @b5, ptr addrspace(2) @b6, ptr addrspace(2) @b7}
256256

257-
// CHECK: ![[CBARRAYS]] = !{ptr @CBArrays.cb, ptr addrspace(2) @c1, ptr addrspace(2) @c2, ptr addrspace(2) @c3, ptr addrspace(2) @c4,
257+
// CHECK: ![[CBARRAYS]] = !{ptr @CBArrays.cb, ptr addrspace(2) @c1, ptr addrspace(2) @c2, ptr addrspace(2) @c3, ptr addrspace(2) @c4,
258258
// CHECK-SAME: ptr addrspace(2) @c5, ptr addrspace(2) @c6, ptr addrspace(2) @c7, ptr addrspace(2) @c8}
259259

260-
// CHECK: ![[CBSTRUCTS]] = !{ptr @CBStructs.cb, ptr addrspace(2) @a, ptr addrspace(2) @b, ptr addrspace(2) @c, ptr addrspace(2) @array_of_A,
260+
// CHECK: ![[CBSTRUCTS]] = !{ptr @CBStructs.cb, ptr addrspace(2) @a, ptr addrspace(2) @b, ptr addrspace(2) @c, ptr addrspace(2) @array_of_A,
261261
// CHECK-SAME: ptr addrspace(2) @d, ptr addrspace(2) @e, ptr addrspace(2) @f}
262262

263263
// CHECK: ![[CBMIX]] = !{ptr @CBMix.cb, ptr addrspace(2) @test, ptr addrspace(2) @f1, ptr addrspace(2) @f2, ptr addrspace(2) @f3,

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: 2 additions & 2 deletions
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
@@ -16,7 +16,7 @@ cbuffer CB : register(b1, space3) {
1616
int2 c : packoffset(c5.z);
1717
}
1818

19-
// CHECK: @CB.cb.1 = external constant target("dx.CBuffer", target("dx.Layout", %__cblayout_CB_1, 92, 88, 80))
19+
// CHECK: @CB.cb.1 = global target("dx.CBuffer", target("dx.Layout", %__cblayout_CB_1, 92, 88, 80))
2020
// CHECK: @x = external addrspace(2) global float, align 4
2121
// CHECK: @y = external addrspace(2) global <2 x float>, align 8
2222

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

clang/test/CodeGenHLSL/default_cbuffer_with_layout.hlsl

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

88
// CHECK-DAG: @b = external addrspace(2) global float, align 4
99
// CHECK-DAG: @d = external addrspace(2) global <4 x i32>, align 16
10-
// CHECK-DAG: @"$Globals.cb" = external constant target("dx.CBuffer",
10+
// CHECK-DAG: @"$Globals.cb" = global target("dx.CBuffer",
1111
// CHECK-DAG-SAME: target("dx.Layout", %"__cblayout_$Globals", 144, 120, 16, 32, 64, 128, 112))
1212
// CHECK-DAG: @a = external addrspace(2) global i32, align 4
1313
// CHECK-DAG: @c = external addrspace(2) global [4 x double], align 8

llvm/test/CodeGen/DirectX/Metadata/cbuffer_metadata.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ target datalayout = "e-m:e-p:32:32-i1:32-i8:8-i16:16-i32:32-i64:64-f16:16-f32:32
66
target triple = "dxil-pc-shadermodel6.6-compute"
77

88
%__cblayout_CB1 = type <{ float, i32, double, <2 x i32> }>
9-
@CB1.cb = external constant target("dx.CBuffer", target("dx.Layout", %__cblayout_CB1, 24, 0, 4, 8, 16))
9+
@CB1.cb = global target("dx.CBuffer", target("dx.Layout", %__cblayout_CB1, 24, 0, 4, 8, 16)) poison
1010

1111
%__cblayout_CB2 = type <{ float, double, float, half, i16, i64, i32 }>
12-
@CB2.cb = external constant target("dx.CBuffer", target("dx.Layout", %__cblayout_CB2, 36, 0, 8, 16, 20, 22, 24, 32))
12+
@CB2.cb = global target("dx.CBuffer", target("dx.Layout", %__cblayout_CB2, 36, 0, 8, 16, 20, 22, 24, 32)) poison
1313

1414
%__cblayout_CB3 = type <{ double, <3 x float>, float, <3 x double>, half, <2 x double>, float, <3 x half>, <3 x half> }>
15-
@CB3.cb = external constant target("dx.CBuffer", target("dx.Layout", %__cblayout_CB3, 96, 0, 16, 28, 32, 56, 64, 80, 84, 90))
15+
@CB3.cb = global target("dx.CBuffer", target("dx.Layout", %__cblayout_CB3, 96, 0, 16, 28, 32, 56, 64, 80, 84, 90)) poison
1616

1717
; PRINT:; Resource Bindings:
1818
; PRINT-NEXT:;

0 commit comments

Comments
 (0)