[llvm] [CodeGen][NPM] Update BranchFolderLegacy make tail merge configurable via flag (PR #135277)

Mikhail R. Gadelha via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 10 16:07:29 PDT 2025


https://github.com/mikhailramalho created https://github.com/llvm/llvm-project/pull/135277

This patch adds support for configuring the BranchFolderPass with or without tail merging.



>From 143976480286b42c20195ff4324c1703e904a149 Mon Sep 17 00:00:00 2001
From: "Mikhail R. Gadelha" <mikhail at igalia.com>
Date: Thu, 10 Apr 2025 14:45:14 -0300
Subject: [PATCH 1/3] [CodeGen][NPM] Create method to add the branch folding
 pass with the option to enable tail merge

Signed-off-by: Mikhail R. Gadelha <mikhail at igalia.com>
---
 llvm/include/llvm/CodeGen/Passes.h           |  2 ++
 llvm/lib/CodeGen/BranchFolding.cpp           | 12 ++++++++++--
 llvm/lib/Target/RISCV/RISCVTargetMachine.cpp |  1 +
 3 files changed, 13 insertions(+), 2 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/Passes.h b/llvm/include/llvm/CodeGen/Passes.h
index d214ab9306c2f..426e430b947f9 100644
--- a/llvm/include/llvm/CodeGen/Passes.h
+++ b/llvm/include/llvm/CodeGen/Passes.h
@@ -257,6 +257,8 @@ namespace llvm {
   /// branches.
   extern char &BranchFolderPassID;
 
+  MachineFunctionPass *createBranchFolderPass(bool EnableTailMerge);
+
   /// BranchRelaxation - This pass replaces branches that need to jump further
   /// than is supported by a branch instruction.
   extern char &BranchRelaxationPassID;
diff --git a/llvm/lib/CodeGen/BranchFolding.cpp b/llvm/lib/CodeGen/BranchFolding.cpp
index 6f5afbd2a996a..5d015d7313b5c 100644
--- a/llvm/lib/CodeGen/BranchFolding.cpp
+++ b/llvm/lib/CodeGen/BranchFolding.cpp
@@ -90,10 +90,13 @@ namespace {
 
   /// BranchFolderPass - Wrap branch folder in a machine function pass.
 class BranchFolderLegacy : public MachineFunctionPass {
+  bool EnableTailMerge;
+
 public:
   static char ID;
 
-  explicit BranchFolderLegacy() : MachineFunctionPass(ID) {}
+  explicit BranchFolderLegacy(bool EnableTailMerge = true)
+      : MachineFunctionPass(ID), EnableTailMerge(EnableTailMerge) {}
 
   bool runOnMachineFunction(MachineFunction &MF) override;
 
@@ -152,7 +155,8 @@ bool BranchFolderLegacy::runOnMachineFunction(MachineFunction &MF) {
   // TailMerge can create jump into if branches that make CFG irreducible for
   // HW that requires structurized CFG.
   bool EnableTailMerge = !MF.getTarget().requiresStructuredCFG() &&
-                         PassConfig->getEnableTailMerge();
+                         PassConfig->getEnableTailMerge() &&
+                         this->EnableTailMerge;
   MBFIWrapper MBBFreqInfo(
       getAnalysis<MachineBlockFrequencyInfoWrapperPass>().getMBFI());
   BranchFolder Folder(
@@ -2080,3 +2084,7 @@ bool BranchFolder::HoistCommonCodeInSuccs(MachineBasicBlock *MBB) {
   ++NumHoist;
   return true;
 }
+
+MachineFunctionPass *llvm::createBranchFolderPass(bool EnableTailMerge = true) {
+  return new BranchFolderLegacy(EnableTailMerge);
+}
diff --git a/llvm/lib/Target/RISCV/RISCVTargetMachine.cpp b/llvm/lib/Target/RISCV/RISCVTargetMachine.cpp
index 7fb64be3975d5..8ff5e02f1d767 100644
--- a/llvm/lib/Target/RISCV/RISCVTargetMachine.cpp
+++ b/llvm/lib/Target/RISCV/RISCVTargetMachine.cpp
@@ -18,6 +18,7 @@
 #include "RISCVTargetTransformInfo.h"
 #include "TargetInfo/RISCVTargetInfo.h"
 #include "llvm/Analysis/TargetTransformInfo.h"
+#include "llvm/CodeGen/BranchFoldingPass.h"
 #include "llvm/CodeGen/GlobalISel/CSEInfo.h"
 #include "llvm/CodeGen/GlobalISel/IRTranslator.h"
 #include "llvm/CodeGen/GlobalISel/InstructionSelect.h"

>From 1129f40a6120a978cdd5161a410e27ecbe536367 Mon Sep 17 00:00:00 2001
From: "Mikhail R. Gadelha" <mikhail at igalia.com>
Date: Thu, 10 Apr 2025 17:16:50 -0300
Subject: [PATCH 2/3] Add printPipeline

Signed-off-by: Mikhail R. Gadelha <mikhail at igalia.com>
---
 llvm/include/llvm/CodeGen/BranchFoldingPass.h | 3 +++
 llvm/lib/CodeGen/BranchFolding.cpp            | 7 +++++++
 2 files changed, 10 insertions(+)

diff --git a/llvm/include/llvm/CodeGen/BranchFoldingPass.h b/llvm/include/llvm/CodeGen/BranchFoldingPass.h
index 6ebef47252d25..b4380b5a4a6c8 100644
--- a/llvm/include/llvm/CodeGen/BranchFoldingPass.h
+++ b/llvm/include/llvm/CodeGen/BranchFoldingPass.h
@@ -24,6 +24,9 @@ class BranchFolderPass : public PassInfoMixin<BranchFolderPass> {
     return MachineFunctionProperties().set(
         MachineFunctionProperties::Property::NoPHIs);
   }
+
+  void printPipeline(raw_ostream &OS,
+                     function_ref<StringRef(StringRef)> MapClassName2PassName);
 };
 
 } // namespace llvm
diff --git a/llvm/lib/CodeGen/BranchFolding.cpp b/llvm/lib/CodeGen/BranchFolding.cpp
index 5d015d7313b5c..42f54c2526487 100644
--- a/llvm/lib/CodeGen/BranchFolding.cpp
+++ b/llvm/lib/CodeGen/BranchFolding.cpp
@@ -147,6 +147,13 @@ PreservedAnalyses BranchFolderPass::run(MachineFunction &MF,
   return getMachineFunctionPassPreservedAnalyses();
 }
 
+void BranchFolderPass::printPipeline(
+    raw_ostream &OS, function_ref<StringRef(StringRef)> MapClassName2PassName) {
+  OS << MapClassName2PassName(name());
+  if (EnableTailMerge)
+    OS << "<enable-tail-merge>";
+}
+
 bool BranchFolderLegacy::runOnMachineFunction(MachineFunction &MF) {
   if (skipFunction(MF.getFunction()))
     return false;

>From 7d8d9df2dd494cfe0886f5c9962972ea92cb7006 Mon Sep 17 00:00:00 2001
From: "Mikhail R. Gadelha" <mikhail at igalia.com>
Date: Thu, 10 Apr 2025 17:48:08 -0300
Subject: [PATCH 3/3] Remove unused include

Signed-off-by: Mikhail R. Gadelha <mikhail at igalia.com>
---
 llvm/lib/Target/RISCV/RISCVTargetMachine.cpp | 1 -
 1 file changed, 1 deletion(-)

diff --git a/llvm/lib/Target/RISCV/RISCVTargetMachine.cpp b/llvm/lib/Target/RISCV/RISCVTargetMachine.cpp
index 8ff5e02f1d767..7fb64be3975d5 100644
--- a/llvm/lib/Target/RISCV/RISCVTargetMachine.cpp
+++ b/llvm/lib/Target/RISCV/RISCVTargetMachine.cpp
@@ -18,7 +18,6 @@
 #include "RISCVTargetTransformInfo.h"
 #include "TargetInfo/RISCVTargetInfo.h"
 #include "llvm/Analysis/TargetTransformInfo.h"
-#include "llvm/CodeGen/BranchFoldingPass.h"
 #include "llvm/CodeGen/GlobalISel/CSEInfo.h"
 #include "llvm/CodeGen/GlobalISel/IRTranslator.h"
 #include "llvm/CodeGen/GlobalISel/InstructionSelect.h"



More information about the llvm-commits mailing list