Skip to content

[llvm][transforms] Add a new algorithm to SplitModule #95941

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 3, 2024
Merged

[llvm][transforms] Add a new algorithm to SplitModule #95941

merged 4 commits into from
Jul 3, 2024

Conversation

sergachev
Copy link
Contributor

@sergachev sergachev commented Jun 18, 2024

The new round-robin algorithm overrides the hash-based distribution of functions to modules. It achieves a more even number of functions per module when the number of functions is close to the number of requested modules. It's not in use by default and is available under a new flag.

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be
notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write
permissions for the repository. In which case you can instead tag reviewers by
name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review
by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate
is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Member

llvmbot commented Jun 18, 2024

@llvm/pr-subscribers-llvm-transforms

Author: Ilia Sergachev (sergachev)

Changes

The new algorithm augments the hash-based distribution of functions to modules by pairing unmapped functions to empty modules before using the hash-based distribution. It allows getting less empty modules when the number of functions is close to the number of requested modules. It's not in use by default and is available under a new flag.


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

5 Files Affected:

  • (modified) llvm/include/llvm/Transforms/Utils/SplitModule.h (+1-1)
  • (modified) llvm/lib/Transforms/Utils/SplitModule.cpp (+33-1)
  • (added) llvm/test/tools/llvm-split/avoid-empty-modules.ll (+23)
  • (added) llvm/test/tools/llvm-split/name-hash-based-distribution.ll (+17)
  • (modified) llvm/tools/llvm-split/llvm-split.cpp (+11-1)
diff --git a/llvm/include/llvm/Transforms/Utils/SplitModule.h b/llvm/include/llvm/Transforms/Utils/SplitModule.h
index a5450738060a8..7b303172d1bb0 100644
--- a/llvm/include/llvm/Transforms/Utils/SplitModule.h
+++ b/llvm/include/llvm/Transforms/Utils/SplitModule.h
@@ -35,7 +35,7 @@ class Module;
 void SplitModule(
     Module &M, unsigned N,
     function_ref<void(std::unique_ptr<Module> MPart)> ModuleCallback,
-    bool PreserveLocals = false);
+    bool PreserveLocals = false, bool TryToAvoidEmptyModules = false);
 
 } // end namespace llvm
 
diff --git a/llvm/lib/Transforms/Utils/SplitModule.cpp b/llvm/lib/Transforms/Utils/SplitModule.cpp
index 9c39c26d8b7af..2f0ada5ef0019 100644
--- a/llvm/lib/Transforms/Utils/SplitModule.cpp
+++ b/llvm/lib/Transforms/Utils/SplitModule.cpp
@@ -251,7 +251,7 @@ static bool isInPartition(const GlobalValue *GV, unsigned I, unsigned N) {
 void llvm::SplitModule(
     Module &M, unsigned N,
     function_ref<void(std::unique_ptr<Module> MPart)> ModuleCallback,
-    bool PreserveLocals) {
+    bool PreserveLocals, bool TryToAvoidEmptyModules) {
   if (!PreserveLocals) {
     for (Function &F : M)
       externalize(&F);
@@ -268,6 +268,38 @@ void llvm::SplitModule(
   ClusterIDMapType ClusterIDMap;
   findPartitions(M, ClusterIDMap, N);
 
+  // Find empty modules and functions not mapped to modules in ClusterIDMap.
+  // Map these functions to the empty modules so that they skip being
+  // distributed by isInPartition() based on function name hashes below.
+  // This provides better uniformity of distribution of functions to modules
+  // in some cases - for example when the number of functions equals to N.
+  if (TryToAvoidEmptyModules) {
+    DenseSet<unsigned> NonEmptyModules;
+    SmallVector<const GlobalValue *> UnmappedFunctions;
+    for (const auto &F : M.functions()) {
+      if (F.isDeclaration() ||
+          F.getLinkage() != GlobalValue::LinkageTypes::ExternalLinkage)
+        continue;
+      auto It = ClusterIDMap.find(&F);
+      if (It == ClusterIDMap.end())
+        UnmappedFunctions.push_back(&F);
+      else
+        NonEmptyModules.insert(It->second);
+    }
+    SmallVector<unsigned> EmptyModules;
+    for (unsigned I = 0; I < N; ++I) {
+      if (!NonEmptyModules.contains(I))
+        EmptyModules.push_back(I);
+    }
+    auto NextEmptyModuleIt = EmptyModules.begin();
+    for (const auto F : UnmappedFunctions) {
+      if (NextEmptyModuleIt == EmptyModules.end())
+        break;
+      ClusterIDMap.insert({F, *NextEmptyModuleIt});
+      ++NextEmptyModuleIt;
+    }
+  }
+
   // FIXME: We should be able to reuse M as the last partition instead of
   // cloning it. Note that the callers at the moment expect the module to
   // be preserved, so will need some adjustments as well.
diff --git a/llvm/test/tools/llvm-split/avoid-empty-modules.ll b/llvm/test/tools/llvm-split/avoid-empty-modules.ll
new file mode 100644
index 0000000000000..654b8410a704f
--- /dev/null
+++ b/llvm/test/tools/llvm-split/avoid-empty-modules.ll
@@ -0,0 +1,23 @@
+; RUN: llvm-split -o %t %s -j 2 -avoid-empty-modules
+; RUN: llvm-dis -o - %t0 | FileCheck --check-prefix=CHECK0 %s
+; RUN: llvm-dis -o - %t1 | FileCheck --check-prefix=CHECK1 %s
+
+; CHECK0-NOT: define
+; CHECK0: declare extern_weak void @e
+; CHECK0: define void @A
+; CHECK0-NOT: define
+
+; CHECK1-NOT: define
+; CHECK1: declare extern_weak void @e
+; CHECK1: define void @C
+; CHECK1-NOT: define
+
+declare extern_weak void @e(...)
+
+define void @A() {
+  ret void
+}
+
+define void @C() {
+  ret void
+}
diff --git a/llvm/test/tools/llvm-split/name-hash-based-distribution.ll b/llvm/test/tools/llvm-split/name-hash-based-distribution.ll
new file mode 100644
index 0000000000000..d9c4fc1eae0ad
--- /dev/null
+++ b/llvm/test/tools/llvm-split/name-hash-based-distribution.ll
@@ -0,0 +1,17 @@
+; RUN: llvm-split -o %t %s -j 2
+; RUN: llvm-dis -o - %t0 | FileCheck --check-prefix=CHECK0 %s
+; RUN: llvm-dis -o - %t1 | FileCheck --check-prefix=CHECK1 %s
+
+; CHECK0-NOT: define
+
+; CHECK1-NOT: declare
+; CHECK1: define void @A
+; CHECK1: define void @C
+
+define void @A() {
+  ret void
+}
+
+define void @C() {
+  ret void
+}
diff --git a/llvm/tools/llvm-split/llvm-split.cpp b/llvm/tools/llvm-split/llvm-split.cpp
index 39a89cb1d2e75..786e92dd75084 100644
--- a/llvm/tools/llvm-split/llvm-split.cpp
+++ b/llvm/tools/llvm-split/llvm-split.cpp
@@ -53,6 +53,12 @@ static cl::opt<bool>
                    cl::desc("Split without externalizing locals"),
                    cl::cat(SplitCategory));
 
+static cl::opt<bool>
+    TryToAvoidEmptyModules("avoid-empty-modules", cl::Prefix, cl::init(false),
+                           cl::desc("Try to avoid generating empty modules by "
+                                    "modifying the distribution of functions"),
+                           cl::cat(SplitCategory));
+
 static cl::opt<std::string>
     MTriple("mtriple",
             cl::desc("Target triple. When present, a TargetMachine is created "
@@ -122,6 +128,9 @@ int main(int argc, char **argv) {
       errs() << "warning: -preserve-locals has no effect when using "
                 "TargetMachine::splitModule\n";
     }
+    if (TryToAvoidEmptyModules)
+      errs() << "warning: -avoid-empty-modules has no effect when using "
+                "TargetMachine::splitModule\n";
 
     if (TM->splitModule(*M, NumOutputs, HandleModulePart))
       return 0;
@@ -131,6 +140,7 @@ int main(int argc, char **argv) {
               "splitModule implementation\n";
   }
 
-  SplitModule(*M, NumOutputs, HandleModulePart, PreserveLocals);
+  SplitModule(*M, NumOutputs, HandleModulePart, PreserveLocals,
+              TryToAvoidEmptyModules);
   return 0;
 }

Copy link
Member

@grypp grypp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not expert here, but it looks good to me.
Nit: title should be [llvm][transforms]

@fhahn
Copy link
Contributor

fhahn commented Jun 25, 2024

Could you share some statistics on how this performs on larger inputs? (e.g. llvm-test-suite)

@sergachev sergachev changed the title [Transforms] Add a new algorithm to SplitModule [llvm][transforms] Add a new algorithm to SplitModule Jun 25, 2024
@sergachev
Copy link
Contributor Author

Could you share some statistics on how this performs on larger inputs? (e.g. llvm-test-suite)

Sure! https://gist.github.com/sergachev/f68e5f6dad591d1efbcfa3fcd23b140c#file-results-txt-L4-L41

@sergachev
Copy link
Contributor Author

ping

sergachev added 4 commits July 3, 2024 12:08
The new algorithm augments the hash-based distribution of functions to
modules by pairing unmapped functions to empty modules before using the
hash-based distribution. It allows getting less empty modules when the
number of functions is close to the number of requested modules.
It's not in use by default and is available under a new flag.
address review comment
completely override the hash-based distribution by mapping all unmapped
functions to modules using round-robin
Copy link
Collaborator

@joker-eph joker-eph left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LG, thanks!

@joker-eph joker-eph merged commit c02e8f7 into llvm:main Jul 3, 2024
7 checks passed
Copy link

github-actions bot commented Jul 3, 2024

@sergachev Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested
by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as
the builds can include changes from many authors. It is not uncommon for your
change to be included in a build that fails due to someone else's changes, or
infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself.
This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

kbluck pushed a commit to kbluck/llvm-project that referenced this pull request Jul 6, 2024
The new round-robin algorithm overrides the hash-based distribution of
functions to modules. It achieves a more even number of functions per
module when the number of functions is close to the number of requested
modules. It's not in use by default and is available under a new flag.
@sergachev sergachev deleted the users/sergachev/splitmodule_new_algo branch October 3, 2024 13:27
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.

5 participants