[clang] [HIP] Fix dynamic initialization with RDC and host profiling (PR #233655)

via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 19 19:20:25 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Yaxun (Sam) Liu (yxsamliu)

<details>
<summary>Changes</summary>

HIP compilation with `-fgpu-rdc` and `-fprofile-instr-generate` fails
when a global variable needs initialization before main(). Profiling
and C++ initialization create separate startup lists, but only one
list is allowed.

Collect profiling registration and C++ initialization in the same
startup list so both run before main().


---
Full diff: https://github.com/llvm/llvm-project/pull/233655.diff


2 Files Affected:

- (modified) clang/lib/CodeGen/CGCUDANV.cpp (+1-2) 
- (added) clang/test/CodeGenHIP/hip-rdc-profile-ctors.hip (+14) 


``````````diff
diff --git a/clang/lib/CodeGen/CGCUDANV.cpp b/clang/lib/CodeGen/CGCUDANV.cpp
index ac2ed9a109c4f..7c38bde6a78d8 100644
--- a/clang/lib/CodeGen/CGCUDANV.cpp
+++ b/clang/lib/CodeGen/CGCUDANV.cpp
@@ -31,7 +31,6 @@
 #include "llvm/Support/Format.h"
 #include "llvm/Support/MD5.h"
 #include "llvm/Support/VirtualFileSystem.h"
-#include "llvm/Transforms/Utils/ModuleUtils.h"
 
 using namespace clang;
 using namespace CodeGen;
@@ -1381,7 +1380,7 @@ void CGNVCUDARuntime::createOffloadingEntries() {
       B.CreateCall(RegisterSectionShadow, {Info.Shadow});
     }
     B.CreateRetVoid();
-    llvm::appendToGlobalCtors(M, CtorFn, /*Priority=*/65535);
+    CGM.AddGlobalCtor(CtorFn);
   }
 }
 
diff --git a/clang/test/CodeGenHIP/hip-rdc-profile-ctors.hip b/clang/test/CodeGenHIP/hip-rdc-profile-ctors.hip
new file mode 100644
index 0000000000000..53298c98ea583
--- /dev/null
+++ b/clang/test/CodeGenHIP/hip-rdc-profile-ctors.hip
@@ -0,0 +1,14 @@
+// NOTE: Do not autogenerate. Check the shared constructor list rather than unrelated function bodies.
+// REQUIRES: x86-registered-target
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -x hip -fgpu-rdc -cuid=abc -fprofile-instrument=clang -emit-llvm -o - %s | FileCheck %s --check-prefix=INIT --implicit-check-not=@llvm.global_ctors.
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -x hip -fgpu-rdc -cuid=abc -fprofile-instrument=clang -DNO_INIT -emit-llvm -o - %s | FileCheck %s --check-prefix=NO-INIT --implicit-check-not=@llvm.global_ctors.
+// Object emission must accept the combined constructor list.
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -x hip -fgpu-rdc -cuid=abc -fprofile-instrument=clang -emit-obj -o %t %s
+
+#ifndef NO_INIT
+int init();
+int g = init();
+#endif
+
+// INIT: @llvm.global_ctors = appending global [2 x { i32, ptr, ptr }] {{.*}}ptr @_GLOBAL__sub_I_{{[^,]+}}, ptr null{{.*}}ptr @__llvm_profile_register_shadow.{{[^,]+}}, ptr null
+// NO-INIT: @llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] {{.*}}ptr @__llvm_profile_register_shadow.{{[^,]+}}, ptr null

``````````

</details>


https://github.com/llvm/llvm-project/pull/233655


More information about the cfe-commits mailing list