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

Yaxun Liu via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 20 07:02:29 PDT 2026


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

>From 9491bfa111a20ff830e056595f3cf44bb99c9786 Mon Sep 17 00:00:00 2001
From: "Yaxun (Sam) Liu" <yaxun.liu at amd.com>
Date: Tue, 20 Oct 2026 00:33:34 +0000
Subject: [PATCH] [HIP] Fix dynamic initialization with RDC and host profiling

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().
---
 clang/lib/CodeGen/CGCUDANV.cpp                  |  3 +--
 clang/test/CodeGenHIP/hip-rdc-profile-ctors.hip | 14 ++++++++++++++
 2 files changed, 15 insertions(+), 2 deletions(-)
 create mode 100644 clang/test/CodeGenHIP/hip-rdc-profile-ctors.hip

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



More information about the cfe-commits mailing list