[clang] [HLSL] Fix call convention mismatch for ctor/dtor (PR #118651)

Nathan Gauër via cfe-commits cfe-commits at lists.llvm.org
Wed Dec 4 06:54:29 PST 2024


https://github.com/Keenuts created https://github.com/llvm/llvm-project/pull/118651

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.

>From 13a33b05f1e708210a4f4b3af9b02e6e3e290130 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Nathan=20Gau=C3=ABr?= <brioche at google.com>
Date: Wed, 4 Dec 2024 15:11:24 +0100
Subject: [PATCH] [HLSL] Fix call convention mismatch for ctor/dtor
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

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 <brioche at google.com>
---
 clang/lib/CodeGen/CGHLSLRuntime.cpp           | 12 +++++++----
 .../builtins/RWBuffer-constructor-opt.hlsl    | 21 +++++++++++++++++++
 2 files changed, 29 insertions(+), 4 deletions(-)
 create mode 100644 clang/test/CodeGenHLSL/builtins/RWBuffer-constructor-opt.hlsl

diff --git a/clang/lib/CodeGen/CGHLSLRuntime.cpp b/clang/lib/CodeGen/CGHLSLRuntime.cpp
index 2c293523fca8ca..9b6db8f0c5d660 100644
--- a/clang/lib/CodeGen/CGHLSLRuntime.cpp
+++ b/clang/lib/CodeGen/CGHLSLRuntime.cpp
@@ -507,13 +507,17 @@ void CGHLSLRuntime::generateGlobalCtorDtorCalls() {
       IP = Token->getNextNode();
     }
     IRBuilder<> B(IP);
-    for (auto *Fn : CtorFns)
-      B.CreateCall(FunctionCallee(Fn), {}, OB);
+    for (auto *Fn : CtorFns) {
+      auto CI = B.CreateCall(FunctionCallee(Fn), {}, OB);
+      CI->setCallingConv(Fn->getCallingConv());
+    }
 
     // Insert global dtors before the terminator of the last instruction
     B.SetInsertPoint(F.back().getTerminator());
-    for (auto *Fn : DtorFns)
-      B.CreateCall(FunctionCallee(Fn), {}, OB);
+    for (auto *Fn : DtorFns) {
+      auto CI = B.CreateCall(FunctionCallee(Fn), {}, OB);
+      CI->setCallingConv(Fn->getCallingConv());
+    }
   }
 
   // No need to keep global ctors/dtors for non-lib profile after call to
diff --git a/clang/test/CodeGenHLSL/builtins/RWBuffer-constructor-opt.hlsl b/clang/test/CodeGenHLSL/builtins/RWBuffer-constructor-opt.hlsl
new file mode 100644
index 00000000000000..d834a22917c1f6
--- /dev/null
+++ b/clang/test/CodeGenHLSL/builtins/RWBuffer-constructor-opt.hlsl
@@ -0,0 +1,21 @@
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -x hlsl -emit-llvm -O3 -o - %s | FileCheck %s --check-prefixes=CHECK,CHECK-DXIL
+// RUN: %clang_cc1 -triple spirv-vulkan-compute -x hlsl -emit-llvm -O3 -o - %s | FileCheck %s --check-prefixes=CHECK,CHECK-SPIRV
+
+// CHECK-SPIRV: %"class.hlsl::RWBuffer" = type { target("spirv.Image", float, 5, 2, 0, 0, 2, 0) }
+// CHECK-DXIL:  %"class.hlsl::RWBuffer" = type { target("dx.TypedBuffer", float, 1, 0, 0) }
+RWBuffer<float> Buf : register(u5, space3);
+
+[shader("compute")]
+[numthreads(1, 1, 1)]
+void main() {
+// CHECK: define void @main()
+// CHECK-NEXT: entry:
+
+// 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)
+// CHECK-SPIRV-NEXT: store target("spirv.Image", float, 5, 2, 0, 0, 2, 0) %Buf_h.i, ptr @Buf, align 8
+
+// 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)
+// CHECK-DXIL-NEXT: store target("dx.TypedBuffer", float, 1, 0, 0) %Buf_h.i, ptr @Buf, align 4
+
+// CHECK-NEXT: ret void
+}



More information about the cfe-commits mailing list