Skip to content

Commit 13a33b0

Browse files
committed
[HLSL] Fix call convention mismatch for ctor/dtor
Before this patch, there was a calling-convention mismatch between the constructors and the actual call emitted for the entrypoint wrapper. Such mismatch causes the InstCombine pass to replace this call with an `unreachable`, breaking the whole function. Signed-off-by: Nathan Gauër <[email protected]>
1 parent 99b862e commit 13a33b0

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

clang/lib/CodeGen/CGHLSLRuntime.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -507,13 +507,17 @@ void CGHLSLRuntime::generateGlobalCtorDtorCalls() {
507507
IP = Token->getNextNode();
508508
}
509509
IRBuilder<> B(IP);
510-
for (auto *Fn : CtorFns)
511-
B.CreateCall(FunctionCallee(Fn), {}, OB);
510+
for (auto *Fn : CtorFns) {
511+
auto CI = B.CreateCall(FunctionCallee(Fn), {}, OB);
512+
CI->setCallingConv(Fn->getCallingConv());
513+
}
512514

513515
// Insert global dtors before the terminator of the last instruction
514516
B.SetInsertPoint(F.back().getTerminator());
515-
for (auto *Fn : DtorFns)
516-
B.CreateCall(FunctionCallee(Fn), {}, OB);
517+
for (auto *Fn : DtorFns) {
518+
auto CI = B.CreateCall(FunctionCallee(Fn), {}, OB);
519+
CI->setCallingConv(Fn->getCallingConv());
520+
}
517521
}
518522

519523
// No need to keep global ctors/dtors for non-lib profile after call to
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -x hlsl -emit-llvm -O3 -o - %s | FileCheck %s --check-prefixes=CHECK,CHECK-DXIL
2+
// RUN: %clang_cc1 -triple spirv-vulkan-compute -x hlsl -emit-llvm -O3 -o - %s | FileCheck %s --check-prefixes=CHECK,CHECK-SPIRV
3+
4+
// CHECK-SPIRV: %"class.hlsl::RWBuffer" = type { target("spirv.Image", float, 5, 2, 0, 0, 2, 0) }
5+
// CHECK-DXIL: %"class.hlsl::RWBuffer" = type { target("dx.TypedBuffer", float, 1, 0, 0) }
6+
RWBuffer<float> Buf : register(u5, space3);
7+
8+
[shader("compute")]
9+
[numthreads(1, 1, 1)]
10+
void main() {
11+
// CHECK: define void @main()
12+
// CHECK-NEXT: entry:
13+
14+
// CHECK-SPIRV-NEXT: %Buf_h.i = tail call target("spirv.Image", float, 5, 2, 0, 0, 2, 0) @llvm.spv.handle.fromBinding.tspirv.Image_f32_5_2_0_0_2_0t(i32 3, i32 5, i32 1, i32 0, i1 false)
15+
// CHECK-SPIRV-NEXT: store target("spirv.Image", float, 5, 2, 0, 0, 2, 0) %Buf_h.i, ptr @Buf, align 8
16+
17+
// CHECK-DXIL-NEXT: %Buf_h.i = tail call target("dx.TypedBuffer", float, 1, 0, 0) @llvm.dx.handle.fromBinding.tdx.TypedBuffer_f32_1_0_0t(i32 3, i32 5, i32 1, i32 0, i1 false)
18+
// CHECK-DXIL-NEXT: store target("dx.TypedBuffer", float, 1, 0, 0) %Buf_h.i, ptr @Buf, align 4
19+
20+
// CHECK-NEXT: ret void
21+
}

0 commit comments

Comments
 (0)