[llvm-branch-commits] [llvm] [NFC][LowerTypeTests] Support ordering in GlobalLayoutBuilder with Less comparator (PR #221044)

Vitaly Buka via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Fri Sep 4 00:54:53 PDT 2026


https://github.com/vitalybuka updated https://github.com/llvm/llvm-project/pull/221044

>From cff5b69c022d0e154498d352494f402e16d9010f Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at google.com>
Date: Thu, 3 Sep 2026 14:47:38 -0700
Subject: [PATCH 1/2] comments

Created using spr 1.3.7
---
 llvm/include/llvm/Transforms/IPO/LowerTypeTests.h | 10 +++++++---
 llvm/unittests/Transforms/IPO/LowerTypeTests.cpp  |  6 ++++--
 2 files changed, 11 insertions(+), 5 deletions(-)

diff --git a/llvm/include/llvm/Transforms/IPO/LowerTypeTests.h b/llvm/include/llvm/Transforms/IPO/LowerTypeTests.h
index b35e5d6699227..b3c275001a9a7 100644
--- a/llvm/include/llvm/Transforms/IPO/LowerTypeTests.h
+++ b/llvm/include/llvm/Transforms/IPO/LowerTypeTests.h
@@ -14,6 +14,7 @@
 #ifndef LLVM_TRANSFORMS_IPO_LOWERTYPETESTS_H
 #define LLVM_TRANSFORMS_IPO_LOWERTYPETESTS_H
 
+#include "llvm/ADT/FunctionExtras.h"
 #include "llvm/ADT/STLFunctionalExtras.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/IR/PassManager.h"
@@ -131,12 +132,15 @@ class GlobalLayoutBuilder {
   std::vector<uint64_t> FragmentMap;
 
   /// Optional comparator for object hotness/ordering.
-  function_ref<bool(uint64_t, uint64_t)> Less;
+  unique_function<bool(uint64_t, uint64_t)> Less;
 
 public:
+  /// Construct a layout builder for \p NumObjects objects.
+  /// If \p Less is provided, it is used to sort sub-fragments and root
+  /// fragments by maximum element.
   GlobalLayoutBuilder(uint64_t NumObjects,
-                      function_ref<bool(uint64_t, uint64_t)> Less = nullptr)
-      : Fragments(1), FragmentMap(NumObjects), Less(Less) {}
+                      unique_function<bool(uint64_t, uint64_t)> Less = nullptr)
+      : Fragments(1), FragmentMap(NumObjects), Less(std::move(Less)) {}
 
   /// Add F to the layout while trying to keep its indices contiguous.
   /// If a previously seen fragment uses any of F's indices, that
diff --git a/llvm/unittests/Transforms/IPO/LowerTypeTests.cpp b/llvm/unittests/Transforms/IPO/LowerTypeTests.cpp
index ad1d343fd3cb9..e3c058958a3ac 100644
--- a/llvm/unittests/Transforms/IPO/LowerTypeTests.cpp
+++ b/llvm/unittests/Transforms/IPO/LowerTypeTests.cpp
@@ -149,8 +149,10 @@ TEST(LowerTypeTests, GlobalLayoutBuilderHotness) {
   };
 
   for (auto &&T : GLBTests) {
-    auto Less = [&](uint64_t A, uint64_t B) { return T.Ranks[A] < T.Ranks[B]; };
-    GlobalLayoutBuilder GLB(T.NumObjects, Less);
+    // Pass a temporary lambda directly to verify it does not dangle.
+    GlobalLayoutBuilder GLB(T.NumObjects, [&](uint64_t A, uint64_t B) {
+      return T.Ranks[A] < T.Ranks[B];
+    });
     for (auto &&F : T.Fragments)
       GLB.addFragment(F);
 

>From bbcfb520134227b18836c093af1bb92fd1cda474 Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at google.com>
Date: Fri, 4 Sep 2026 00:36:02 -0700
Subject: [PATCH 2/2] format

Created using spr 1.3.7
---
 llvm/lib/Analysis/RegionPass.cpp                | 2 +-
 llvm/lib/CodeGen/MachineFunctionPrinterPass.cpp | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Analysis/RegionPass.cpp b/llvm/lib/Analysis/RegionPass.cpp
index ae1d84659de86..f6f79e5c9bb33 100644
--- a/llvm/lib/Analysis/RegionPass.cpp
+++ b/llvm/lib/Analysis/RegionPass.cpp
@@ -206,7 +206,7 @@ class PrintRegionPass : public RegionPass {
 };
 
 char PrintRegionPass::ID = 0;
-}  //end anonymous namespace
+} // end anonymous namespace
 
 //===----------------------------------------------------------------------===//
 // RegionPass
diff --git a/llvm/lib/CodeGen/MachineFunctionPrinterPass.cpp b/llvm/lib/CodeGen/MachineFunctionPrinterPass.cpp
index 5111322023d04..9353a2e851b76 100644
--- a/llvm/lib/CodeGen/MachineFunctionPrinterPass.cpp
+++ b/llvm/lib/CodeGen/MachineFunctionPrinterPass.cpp
@@ -54,7 +54,7 @@ struct MachineFunctionPrinterPass : public MachineFunctionPass {
 };
 
 char MachineFunctionPrinterPass::ID = 0;
-}
+} // namespace
 
 char &llvm::MachineFunctionPrinterPassID = MachineFunctionPrinterPass::ID;
 INITIALIZE_PASS(MachineFunctionPrinterPass, "machineinstr-printer",



More information about the llvm-branch-commits mailing list