Skip to content

Conversation

@kjetilkjeka
Copy link
Contributor

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.

@llvmbot
Copy link
Member

llvmbot commented Dec 19, 2025

@llvm/pr-subscribers-backend-nvptx

Author: Kjetil Kjeka (kjetilkjeka)

Changes

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.


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

1 Files Affected:

  • (modified) llvm/lib/Target/NVPTX/NVPTXAssignValidGlobalNames.cpp (+6-2)
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

@kjetilkjeka kjetilkjeka force-pushed the nvptx_extern_shared_valid_name branch from ffca621 to f7548a7 Compare December 19, 2025 19:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants