[llvm] 240670d - [NFC][ModuleUtils] Move externalize() from module splitting utilities (#221181)

via llvm-commits llvm-commits at lists.llvm.org
Sun Sep 13 07:40:11 PDT 2026


Author: MaoJiaping
Date: 2026-09-13T16:40:06+02:00
New Revision: 240670d0d5dcd649bd3728871087c938b5e097f2

URL: https://github.com/llvm/llvm-project/commit/240670d0d5dcd649bd3728871087c938b5e097f2
DIFF: https://github.com/llvm/llvm-project/commit/240670d0d5dcd649bd3728871087c938b5e097f2.diff

LOG: [NFC][ModuleUtils] Move externalize() from module splitting utilities (#221181)

`SplitModule.cpp` and `AMDGPUSplitModule.cpp` each carry an identical
static `externalize()` helper that promotes local-linkage globals to
external + hidden visibility and assigns a stable name to unnamed
entities, so they are named consistently across module partitions.

This patch removes the duplication by moving the helper into
`ModuleUtils` as `llvm::externalize`, and updates both users to call the
shared implementation. No functional changes are intended.

This also prepares for upcoming module splitting utilities to reuse a
single, canonical implementation.

Development of this patch was assisted by AI and reviewed by me.

Added: 
    llvm/include/llvm/Transforms/Utils/SplitModuleCommon.h
    llvm/lib/Transforms/Utils/SplitModuleCommon.cpp

Modified: 
    llvm/lib/Target/AMDGPU/AMDGPUSplitModule.cpp
    llvm/lib/Transforms/Utils/CMakeLists.txt
    llvm/lib/Transforms/Utils/SplitModule.cpp
    llvm/test/tools/llvm-split/unnamed.ll

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Transforms/Utils/SplitModuleCommon.h b/llvm/include/llvm/Transforms/Utils/SplitModuleCommon.h
new file mode 100644
index 0000000000000..f611717bc5a97
--- /dev/null
+++ b/llvm/include/llvm/Transforms/Utils/SplitModuleCommon.h
@@ -0,0 +1,33 @@
+//===----- 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 05857d88a597c..5878d2c86eb6a 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUSplitModule.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUSplitModule.cpp
@@ -56,6 +56,7 @@
 #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>
@@ -164,19 +165,6 @@ static bool isNonCopyable(const Function &F) {
          AMDGPU::isEntryFunctionCC(F.getCallingConv());
 }
 
-/// If \p GV has local linkage, make it external + hidden.
-static void externalize(GlobalValue &GV) {
-  if (GV.hasLocalLinkage()) {
-    GV.setLinkage(GlobalValue::ExternalLinkage);
-    GV.setVisibility(GlobalValue::HiddenVisibility);
-  }
-
-  // Unnamed entities must be named consistently between modules. setName will
-  // give a distinct name to each such entity.
-  if (!GV.hasName())
-    GV.setName("__llvmsplit_unnamed");
-}
-
 /// Cost analysis function. Calculates the cost of each function in \p M
 ///
 /// \param GetTTI Abstract getter for TargetTransformInfo.
@@ -1397,7 +1385,7 @@ static void splitAMDGPUModule(
       if (Fn.hasLocalLinkage() && Fn.hasAddressTaken()) {
         LLVM_DEBUG(dbgs() << "[externalize] "; Fn.printAsOperand(dbgs());
                    dbgs() << " because its address is taken\n");
-        externalize(Fn);
+        externalizeGlobal(Fn);
       }
     }
   }
@@ -1408,14 +1396,14 @@ static void splitAMDGPUModule(
     for (auto &GV : M.globals()) {
       if (GV.hasLocalLinkage())
         LLVM_DEBUG(dbgs() << "[externalize] GV " << GV.getName() << '\n');
-      externalize(GV);
+      externalizeGlobal(GV);
     }
   }
 
   for (auto &GA : M.aliases()) {
     if (GA.hasLocalLinkage()) {
       LLVM_DEBUG(dbgs() << "[externalize] alias " << GA.getName() << '\n');
-      externalize(GA);
+      externalizeGlobal(GA);
     }
   }
 

diff  --git a/llvm/lib/Transforms/Utils/CMakeLists.txt b/llvm/lib/Transforms/Utils/CMakeLists.txt
index 3b7a134b49e2e..103221325f912 100644
--- a/llvm/lib/Transforms/Utils/CMakeLists.txt
+++ b/llvm/lib/Transforms/Utils/CMakeLists.txt
@@ -91,6 +91,7 @@ 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 8e70902c4dac2..be577b14c4660 100644
--- a/llvm/lib/Transforms/Utils/SplitModule.cpp
+++ b/llvm/lib/Transforms/Utils/SplitModule.cpp
@@ -36,6 +36,7 @@
 #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>
@@ -125,8 +126,7 @@ static void findPartitions(Module &M, ClusterIDMapType &ClusterIDMap,
     if (GV.isDeclaration())
       return;
 
-    if (!GV.hasName())
-      GV.setName("__llvmsplit_unnamed");
+    nameUnnamedGlobalValue(GV);
 
     // Comdat groups must not be partitioned. For comdat groups that contain
     // locals, record all their members here so we can keep them together.
@@ -203,18 +203,6 @@ static void findPartitions(Module &M, ClusterIDMapType &ClusterIDMap,
   }
 }
 
-static void externalize(GlobalValue *GV) {
-  if (GV->hasLocalLinkage()) {
-    GV->setLinkage(GlobalValue::ExternalLinkage);
-    GV->setVisibility(GlobalValue::HiddenVisibility);
-  }
-
-  // Unnamed entities must be named consistently between modules. setName will
-  // give a distinct name to each such entity.
-  if (!GV->hasName())
-    GV->setName("__llvmsplit_unnamed");
-}
-
 // Returns whether GV should be in partition (0-based) I of N.
 static bool isInPartition(const GlobalValue *GV, unsigned I, unsigned N) {
   if (const GlobalObject *Root = getGVPartitioningRoot(GV))
@@ -242,13 +230,13 @@ void llvm::SplitModule(
     bool PreserveLocals, bool RoundRobin) {
   if (!PreserveLocals) {
     for (Function &F : M)
-      externalize(&F);
+      externalizeGlobal(F);
     for (GlobalVariable &GV : M.globals())
-      externalize(&GV);
+      externalizeGlobal(GV);
     for (GlobalAlias &GA : M.aliases())
-      externalize(&GA);
+      externalizeGlobal(GA);
     for (GlobalIFunc &GIF : M.ifuncs())
-      externalize(&GIF);
+      externalizeGlobal(GIF);
   }
 
   // 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
new file mode 100644
index 0000000000000..a1a10a7755d87
--- /dev/null
+++ b/llvm/lib/Transforms/Utils/SplitModuleCommon.cpp
@@ -0,0 +1,33 @@
+//===----- 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 fd24b4ca92bb3..35a3a4e958433 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: declare hidden void @__llvmsplit_unnamed()
-; CHECK1: define hidden void @__llvmsplit_unnamed()
+; CHECK0: define hidden void @__llvmsplit_externalize_unnamed()
+; CHECK1: declare hidden void @__llvmsplit_externalize_unnamed()
 define internal void @0() {
-  ; CHECK1: call void @foo()
+  ; CHECK0: call void @foo()
   call void @foo()
   ret void
 }
 
-; CHECK0: declare hidden void @__llvmsplit_unnamed.1()
-; CHECK1: define hidden void @__llvmsplit_unnamed.1()
+; CHECK0: define hidden void @__llvmsplit_externalize_unnamed.1()
+; CHECK1: declare hidden void @__llvmsplit_externalize_unnamed.1()
 define internal void @1() {
-  ; CHECK1: call void @foo()
-  ; CHECK1: call void @foo()
+  ; CHECK0: call void @foo()
+  ; CHECK0: 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_unnamed.1()
-  ; CHECK0: call void @__llvmsplit_unnamed()
+  ; CHECK0: call void @__llvmsplit_externalize_unnamed.1()
+  ; CHECK0: call void @__llvmsplit_externalize_unnamed()
   call void @1()
   call void @0()
   ret void


        


More information about the llvm-commits mailing list