[clang] [OpenMP][clang][HIP][CUDA] fix weak alias emit on device compilation (PR #164326)

via cfe-commits cfe-commits at lists.llvm.org
Fri Oct 24 13:52:38 PDT 2025


================
@@ -4065,8 +4065,40 @@ void CodeGenModule::EmitGlobal(GlobalDecl GD) {
 
   // If this is an alias definition (which otherwise looks like a declaration)
   // emit it now.
-  if (Global->hasAttr<AliasAttr>())
+  if (Global->hasAttr<AliasAttr>()) {
+    if (LangOpts.OpenMPIsTargetDevice || LangOpts.CUDA) {
+      const auto *AA = Global->getAttr<AliasAttr>();
+      assert(AA && "Not an alias?");
+      GlobalDecl AliaseeGD;
+      if (!lookupRepresentativeDecl(AA->getAliasee(), AliaseeGD)) {
+        if (LangOpts.CUDA)
+          // Failed to find aliasee on device side, skip emitting
+          return;
+      } else {
+        const auto *AliaseeDecl = dyn_cast<ValueDecl>(AliaseeGD.getDecl());
+        if (LangOpts.OpenMPIsTargetDevice) {
+          if (!AliaseeDecl ||
+              !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(
+                  AliaseeDecl))
+            // Not a target declaration, skip emitting
+            return;
+        } else {
+          // HIP/CUDA
+          const bool HasDeviceAttr = Global->hasAttr<CUDADeviceAttr>();
+          const bool AliaseeHasDeviceAttr =
+              AliaseeDecl && AliaseeDecl->hasAttr<CUDADeviceAttr>();
+          if (LangOpts.CUDAIsDevice) {
+            if (!HasDeviceAttr || !AliaseeHasDeviceAttr)
+              return;
+          } else if (HasDeviceAttr && AliaseeHasDeviceAttr) {
+            // Alias is only on device side, skip emitting on host side
+            return;
----------------
Jason-VanBeusekom wrote:

It is needed as `LangOpts.CUDA` is true for both the host and device compilation, for the host compilation case (the else if) we need to skip if the alias / Aliasee is only on the device, and for the device side we need to make sure both the alias and aliasee are both on the device.


in [16c1a68](https://github.com/llvm/llvm-project/pull/164326/commits/16c1a6888b87644f24c07a75c067d9e25eb1ac3e) I refactored this and added comments to hopefully clear this up. I also added a comment below going into depth about the various states in the refactor and when they are triggered. 


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


More information about the cfe-commits mailing list