[llvm] [NVPTX] fix illegal name for .extern .shared global varaibles (PR #173018)

Kjetil Kjeka via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 19 07:27:14 PST 2025


https://github.com/kjetilkjeka created https://github.com/llvm/llvm-project/pull/173018

In ptx we can create a GV in AS(3) that will be compiled to a `.extern .shared` in ptx. Since the `.extern .shared` is not an "extern" in the traditional sense of the word it will not be linked based on name but rather refer to the shared memory allocated at kernel launch.

Since we don't care about the name it's tempting to make the GV unnamed.  Then the problem that the `nameUnamedGlobals` will use a name for the global that is invalid ptx occurs. For non-extern globals, this is later fixed by running the `NVPTXAssignValidGlobalNames` pass. However, It makes sure to not touch externs as changing the name of "traditional externs"  will cause linking issues down the road. 

This MR treats `.extern .shared` in the same manner as non-extern globals during `NVPTXAssignValidGlobalNames` to fix the invalid names given by `nameUnamedGlobals`.

>From aceecfd724b5df6d5cb3fdec368fec5f6bff1dbf Mon Sep 17 00:00:00 2001
From: Kjetil Kjeka <kjetil at muybridge.com>
Date: Fri, 19 Dec 2025 16:16:16 +0100
Subject: [PATCH] [NVPTX] fix illegal name for .extern .shared global varaibles

---
 llvm/lib/Target/NVPTX/NVPTXAssignValidGlobalNames.cpp | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Target/NVPTX/NVPTXAssignValidGlobalNames.cpp b/llvm/lib/Target/NVPTX/NVPTXAssignValidGlobalNames.cpp
index 15417a15f389b..19cc448843838 100644
--- a/llvm/lib/Target/NVPTX/NVPTXAssignValidGlobalNames.cpp
+++ b/llvm/lib/Target/NVPTX/NVPTXAssignValidGlobalNames.cpp
@@ -43,8 +43,12 @@ INITIALIZE_PASS(NVPTXAssignValidGlobalNames, "nvptx-assign-valid-global-names",
 
 bool NVPTXAssignValidGlobalNames::runOnModule(Module &M) {
   for (GlobalVariable &GV : M.globals()) {
-    // We are only allowed to rename local symbols.
-    if (GV.hasLocalLinkage()) {
+    // We are only allowed rename symbols that are not externally linked by name
+    // - local symbols, as all references will be renamed
+    // - .extern .shared symbols, as they're the same regardless of name
+    if (GV.hasLocalLinkage() ||
+        (GV.hasExternalLinkage() &&
+         GV.getAddressSpace() == NVPTX::AddressSpace::Shared)) {
       // setName doesn't do extra work if the name does not change.
       // Note: this does not create collisions - if setName is asked to set the
       // name to something that already exists, it adds a proper postfix to



More information about the llvm-commits mailing list