Skip to content

Commit 8bc614a

Browse files
committed
Fix bug - initialize resource arrays to poison, add test
1 parent 227f6ae commit 8bc614a

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5752,11 +5752,16 @@ void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D,
57525752
(D->getType()->isCUDADeviceBuiltinSurfaceType() ||
57535753
D->getType()->isCUDADeviceBuiltinTextureType());
57545754
if (getLangOpts().CUDA &&
5755-
(IsCUDASharedVar || IsCUDAShadowVar || IsCUDADeviceShadowVar))
5755+
(IsCUDASharedVar || IsCUDAShadowVar || IsCUDADeviceShadowVar)) {
57565756
Init = llvm::UndefValue::get(getTypes().ConvertTypeForMem(ASTTy));
5757-
else if (D->hasAttr<LoaderUninitializedAttr>())
5757+
} else if (getLangOpts().HLSL &&
5758+
(D->getType()->isHLSLResourceRecord() ||
5759+
D->getType()->isHLSLResourceRecordArray())) {
5760+
Init = llvm::PoisonValue::get(getTypes().ConvertType(ASTTy));
5761+
NeedsGlobalCtor = D->getType()->isHLSLResourceRecord();
5762+
} else if (D->hasAttr<LoaderUninitializedAttr>()) {
57585763
Init = llvm::UndefValue::get(getTypes().ConvertTypeForMem(ASTTy));
5759-
else if (!InitExpr) {
5764+
} else if (!InitExpr) {
57605765
// This is a tentative definition; tentative definitions are
57615766
// implicitly initialized with { 0 }.
57625767
//
@@ -5777,11 +5782,7 @@ void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D,
57775782
if (D->getType()->isReferenceType())
57785783
T = D->getType();
57795784

5780-
if (getLangOpts().HLSL && (D->getType()->isHLSLResourceRecord() ||
5781-
D->getType()->isHLSLResourceRecordArray())) {
5782-
Init = llvm::PoisonValue::get(getTypes().ConvertType(ASTTy));
5783-
NeedsGlobalCtor = true;
5784-
} else if (getLangOpts().CPlusPlus) {
5785+
if (getLangOpts().CPlusPlus) {
57855786
Init = EmitNullConstant(T);
57865787
if (!IsDefinitionAvailableExternally)
57875788
NeedsGlobalCtor = true;

clang/test/CodeGenHLSL/resources/resource-bindings.hlsl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
// CHECK: %"class.hlsl::RWBuffer.0" = type { target("dx.TypedBuffer", float, 1, 0, 0) }
55
// CHECK: %"class.hlsl::StructuredBuffer" = type { target("dx.RawBuffer", i32, 0, 0) }
66
// CHECK: %"class.hlsl::RWStructuredBuffer" = type { target("dx.RawBuffer", %struct.S, 1, 0) }
7+
// CHECK: %"class.hlsl::RWBuffer.1" = type { target("dx.TypedBuffer", double, 1, 0, 0) }
78

89
// CHECK: @_ZL4U0S0 = internal global %"class.hlsl::RWBuffer" poison, align 4
910
// CHECK: @_ZL4U5S3 = internal global %"class.hlsl::RWBuffer.0" poison, align 4
1011
// CHECK: @_ZL4T2S2 = internal global %"class.hlsl::StructuredBuffer" poison, align 4
1112
// CHECK: @_ZL4T3S0 = internal global %"class.hlsl::RWStructuredBuffer" poison, align 4
13+
// CHECK: @_ZL5Array = internal global [10 x %"class.hlsl::RWBuffer.1"] poison, align 4
1214

1315
// CHECK: %[[HANDLE:.*]] = call target("dx.TypedBuffer", <4 x float>, 1, 0, 0)
1416
// CHECK-SAME: @llvm.dx.resource.handlefrombinding.tdx.TypedBuffer_v4f32_1_0_0t(
@@ -42,5 +44,15 @@ struct S {
4244
};
4345
RWStructuredBuffer<S> T3S0 : register(u3);
4446

47+
// Resource array elements are initialized on access; make sure there is not call
48+
// to initialize RWBuffer<double>.
49+
// CHECK-NOT: call target("dx.TypedBuffer", double, 1, 0, 0) @llvm.dx.resource.handlefrombinding.tdx.TypedBuffer_f64_1_0_0t(
50+
RWBuffer<double> Array[10] : register(u4, space0);
51+
4552
[numthreads(4,1,1)]
46-
void main() {}
53+
void main() {
54+
// Reference Array to ensure it is emitted and we can test that it is initialized
55+
// to poison, but do not index it.
56+
// Non-array resources are always emitted because they have a constructor initializer.
57+
(void)Array;
58+
}

0 commit comments

Comments
 (0)