[llvm] [NFC][IR][GlobalValue] Move externalize helpers to GlobalValue (PR #223654)

via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 15 03:58:24 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-transforms

Author: MaoJiaping (mmjjpp)

<details>
<summary>Changes</summary>

This PR is a revised refactoring of #<!-- -->221181.
To improve generality, it makes the splitting helpers externalizeGlobal() and nameUnnamedGlobalValue() generic members of GlobalValue and removes SplitModuleCommon:

- GlobalValue::externalize() promotes a local-linkage global to external + hidden and names it if unnamed.
- GlobalValue::nameUnnamed() names unnamed globals __llvm_unnamed.

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


7 Files Affected:

- (modified) llvm/include/llvm/IR/GlobalValue.h (+17) 
- (removed) llvm/include/llvm/Transforms/Utils/SplitModuleCommon.h (-33) 
- (modified) llvm/lib/Target/AMDGPU/AMDGPUSplitModule.cpp (+3-4) 
- (modified) llvm/lib/Transforms/Utils/CMakeLists.txt (-1) 
- (modified) llvm/lib/Transforms/Utils/SplitModule.cpp (+5-6) 
- (removed) llvm/lib/Transforms/Utils/SplitModuleCommon.cpp (-33) 
- (modified) llvm/test/tools/llvm-split/unnamed.ll (+8-8) 


``````````diff
diff --git a/llvm/include/llvm/IR/GlobalValue.h b/llvm/include/llvm/IR/GlobalValue.h
index d7b0f1f25f929..79e45ef51367c 100644
--- a/llvm/include/llvm/IR/GlobalValue.h
+++ b/llvm/include/llvm/IR/GlobalValue.h
@@ -561,6 +561,23 @@ class GlobalValue : public Constant {
 
   bool isWeakForLinker() const { return isWeakForLinker(getLinkage()); }
 
+  /// Assign a stable name to this GlobalValue if it is unnamed. setName will
+  /// make the name unique (e.g. __llvm_unnamed.1) if it is already taken.
+  void nameUnnamed() {
+    if (!hasName())
+      setName("__llvm_unnamed");
+  }
+
+  /// Promote this GlobalValue to external linkage with hidden visibility if
+  /// it has local linkage, and name it if it is unnamed.
+  void externalize() {
+    if (hasLocalLinkage()) {
+      setLinkage(ExternalLinkage);
+      setVisibility(HiddenVisibility);
+    }
+    nameUnnamed();
+  }
+
 protected:
   /// Copy all additional attributes (those not needed to create a GlobalValue)
   /// from the GlobalValue Src to this one.
diff --git a/llvm/include/llvm/Transforms/Utils/SplitModuleCommon.h b/llvm/include/llvm/Transforms/Utils/SplitModuleCommon.h
deleted file mode 100644
index f611717bc5a97..0000000000000
--- a/llvm/include/llvm/Transforms/Utils/SplitModuleCommon.h
+++ /dev/null
@@ -1,33 +0,0 @@
-//===----- SplitModuleCommon.h - shared module splitting helpers ---------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-// Common utilities shared by the module splitting implementations
-// (SplitModule, AMDGPUSplitModule, ...).
-//===----------------------------------------------------------------------===//
-
-#ifndef LLVM_TRANSFORMS_UTILS_SPLITMODULECOMMON_H
-#define LLVM_TRANSFORMS_UTILS_SPLITMODULECOMMON_H
-
-#include "llvm/Support/Compiler.h"
-
-namespace llvm {
-
-class GlobalValue;
-
-/// Assign a stable name to \p GV if it is unnamed, so that it is named
-/// consistently across the split partitions. setName will give a distinct
-/// name (e.g. __llvmsplit_externalize_unnamed.1) to each such entity.
-LLVM_ABI void nameUnnamedGlobalValue(GlobalValue &GV);
-
-/// If \p GV has local linkage, promote it to external + hidden visibility so
-/// it can be referenced across module partitions, and assign a stable name
-/// to unnamed entities (see nameUnnamedGlobalValue).
-LLVM_ABI void externalizeGlobal(GlobalValue &GV);
-
-} // end namespace llvm
-
-#endif // LLVM_TRANSFORMS_UTILS_SPLITMODULECOMMON_H
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUSplitModule.cpp b/llvm/lib/Target/AMDGPU/AMDGPUSplitModule.cpp
index 5878d2c86eb6a..d119711edb18d 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUSplitModule.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUSplitModule.cpp
@@ -56,7 +56,6 @@
 #include "llvm/Support/Timer.h"
 #include "llvm/Support/raw_ostream.h"
 #include "llvm/Transforms/Utils/Cloning.h"
-#include "llvm/Transforms/Utils/SplitModuleCommon.h"
 #include <cassert>
 #include <cmath>
 #include <utility>
@@ -1385,7 +1384,7 @@ static void splitAMDGPUModule(
       if (Fn.hasLocalLinkage() && Fn.hasAddressTaken()) {
         LLVM_DEBUG(dbgs() << "[externalize] "; Fn.printAsOperand(dbgs());
                    dbgs() << " because its address is taken\n");
-        externalizeGlobal(Fn);
+        Fn.externalize();
       }
     }
   }
@@ -1396,14 +1395,14 @@ static void splitAMDGPUModule(
     for (auto &GV : M.globals()) {
       if (GV.hasLocalLinkage())
         LLVM_DEBUG(dbgs() << "[externalize] GV " << GV.getName() << '\n');
-      externalizeGlobal(GV);
+      GV.externalize();
     }
   }
 
   for (auto &GA : M.aliases()) {
     if (GA.hasLocalLinkage()) {
       LLVM_DEBUG(dbgs() << "[externalize] alias " << GA.getName() << '\n');
-      externalizeGlobal(GA);
+      GA.externalize();
     }
   }
 
diff --git a/llvm/lib/Transforms/Utils/CMakeLists.txt b/llvm/lib/Transforms/Utils/CMakeLists.txt
index 103221325f912..3b7a134b49e2e 100644
--- a/llvm/lib/Transforms/Utils/CMakeLists.txt
+++ b/llvm/lib/Transforms/Utils/CMakeLists.txt
@@ -91,7 +91,6 @@ add_llvm_component_library(LLVMTransformUtils
   SizeOpts.cpp
   SplitModule.cpp
   SplitModuleByCategory.cpp
-  SplitModuleCommon.cpp
   StripNonLineTableDebugInfo.cpp
   SymbolRewriter.cpp
   UnifyLoopExits.cpp
diff --git a/llvm/lib/Transforms/Utils/SplitModule.cpp b/llvm/lib/Transforms/Utils/SplitModule.cpp
index be577b14c4660..01507f82e0c52 100644
--- a/llvm/lib/Transforms/Utils/SplitModule.cpp
+++ b/llvm/lib/Transforms/Utils/SplitModule.cpp
@@ -36,7 +36,6 @@
 #include "llvm/Support/MD5.h"
 #include "llvm/Support/raw_ostream.h"
 #include "llvm/Transforms/Utils/Cloning.h"
-#include "llvm/Transforms/Utils/SplitModuleCommon.h"
 #include "llvm/Transforms/Utils/ValueMapper.h"
 #include <cassert>
 #include <iterator>
@@ -126,7 +125,7 @@ static void findPartitions(Module &M, ClusterIDMapType &ClusterIDMap,
     if (GV.isDeclaration())
       return;
 
-    nameUnnamedGlobalValue(GV);
+    GV.nameUnnamed();
 
     // Comdat groups must not be partitioned. For comdat groups that contain
     // locals, record all their members here so we can keep them together.
@@ -230,13 +229,13 @@ void llvm::SplitModule(
     bool PreserveLocals, bool RoundRobin) {
   if (!PreserveLocals) {
     for (Function &F : M)
-      externalizeGlobal(F);
+      F.externalize();
     for (GlobalVariable &GV : M.globals())
-      externalizeGlobal(GV);
+      GV.externalize();
     for (GlobalAlias &GA : M.aliases())
-      externalizeGlobal(GA);
+      GA.externalize();
     for (GlobalIFunc &GIF : M.ifuncs())
-      externalizeGlobal(GIF);
+      GIF.externalize();
   }
 
   // This performs splitting without a need for externalization, which might not
diff --git a/llvm/lib/Transforms/Utils/SplitModuleCommon.cpp b/llvm/lib/Transforms/Utils/SplitModuleCommon.cpp
deleted file mode 100644
index a1a10a7755d87..0000000000000
--- a/llvm/lib/Transforms/Utils/SplitModuleCommon.cpp
+++ /dev/null
@@ -1,33 +0,0 @@
-//===----- SplitModuleCommon.cpp - shared module splitting helpers --------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-//
-// This file implements the utilities shared by the module splitting
-// implementations (SplitModule, AMDGPUSplitModule, ...).
-//
-//===----------------------------------------------------------------------===//
-
-#include "llvm/Transforms/Utils/SplitModuleCommon.h"
-#include "llvm/IR/GlobalValue.h"
-
-using namespace llvm;
-
-void llvm::nameUnnamedGlobalValue(GlobalValue &GV) {
-  // Unnamed entities must be named consistently between modules. setName will
-  // give a distinct name to each such entity.
-  if (!GV.hasName())
-    GV.setName("__llvmsplit_externalize_unnamed");
-}
-
-void llvm::externalizeGlobal(GlobalValue &GV) {
-  if (GV.hasLocalLinkage()) {
-    GV.setLinkage(GlobalValue::ExternalLinkage);
-    GV.setVisibility(GlobalValue::HiddenVisibility);
-  }
-
-  nameUnnamedGlobalValue(GV);
-}
diff --git a/llvm/test/tools/llvm-split/unnamed.ll b/llvm/test/tools/llvm-split/unnamed.ll
index 35a3a4e958433..eca973bc27070 100644
--- a/llvm/test/tools/llvm-split/unnamed.ll
+++ b/llvm/test/tools/llvm-split/unnamed.ll
@@ -2,19 +2,19 @@
 ; RUN: llvm-dis -o - %t0 | FileCheck --check-prefix=CHECK0 %s
 ; RUN: llvm-dis -o - %t1 | FileCheck --check-prefix=CHECK1 %s
 
-; CHECK0: define hidden void @__llvmsplit_externalize_unnamed()
-; CHECK1: declare hidden void @__llvmsplit_externalize_unnamed()
+; CHECK0: define hidden void @__llvm_unnamed()
+; CHECK1: declare hidden void @__llvm_unnamed()
 define internal void @0() {
   ; CHECK0: call void @foo()
   call void @foo()
   ret void
 }
 
-; CHECK0: define hidden void @__llvmsplit_externalize_unnamed.1()
-; CHECK1: declare hidden void @__llvmsplit_externalize_unnamed.1()
+; CHECK0: declare hidden void @__llvm_unnamed.1()
+; CHECK1: define hidden void @__llvm_unnamed.1()
 define internal void @1() {
-  ; CHECK0: call void @foo()
-  ; CHECK0: call void @foo()
+  ; CHECK1: call void @foo()
+  ; CHECK1: call void @foo()
   call void @foo()
   call void @foo()
   ret void
@@ -23,8 +23,8 @@ define internal void @1() {
 ; CHECK0: define void @foo()
 ; CHECK1: declare void @foo()
 define void @foo() {
-  ; CHECK0: call void @__llvmsplit_externalize_unnamed.1()
-  ; CHECK0: call void @__llvmsplit_externalize_unnamed()
+  ; CHECK0: call void @__llvm_unnamed.1()
+  ; CHECK0: call void @__llvm_unnamed()
   call void @1()
   call void @0()
   ret void

``````````

</details>


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


More information about the llvm-commits mailing list