Skip to content

Commit 498061a

Browse files
committed
Merge branch 'resource-array-subscript-one-elem-codegen' of https://github.com/hekota/llvm-project into pr152454-hekota+pr152636-farzon
2 parents 5477d16 + f0d05cd commit 498061a

File tree

3 files changed

+30
-11
lines changed

3 files changed

+30
-11
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/convergence/global_array.hlsl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,15 @@
66

77
// CHECK: [[loop_entry]]:
88
// CHECK: [[loop_token:%.*]] = call token @llvm.experimental.convergence.loop() [ "convergencectrl"(token [[entry_token]]) ]
9-
// CHECK: call void {{.*}} [ "convergencectrl"(token [[loop_token]]) ]
9+
// CHECK: call spir_func void {{.*}} [ "convergencectrl"(token [[loop_token]]) ]
1010
// CHECK: br i1 {{%.*}} label {{%.*}} label %[[loop_entry]]
11-
RWBuffer<float> e[2];
11+
12+
struct S {
13+
int i;
14+
S() { i = 10; }
15+
};
16+
17+
static S s[2];
1218

1319
[numthreads(4,1,1)]
1420
void main() {

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)