[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
Mon Apr 21 06:10:12 PDT 2025


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

>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 01/13] [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 02/13] 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 03/13] 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"

>From 9b959746c3125f3f8727158cb26151b666ff6772 Mon Sep 17 00:00:00 2001
From: "Mikhail R. Gadelha" <mikhail at igalia.com>
Date: Fri, 11 Apr 2025 12:05:16 -0300
Subject: [PATCH 04/13] Pass global flag via argument

Signed-off-by: Mikhail R. Gadelha <mikhail at igalia.com>
---
 llvm/lib/CodeGen/BranchFolding.cpp    | 5 ++---
 llvm/lib/CodeGen/TargetPassConfig.cpp | 2 +-
 2 files changed, 3 insertions(+), 4 deletions(-)

diff --git a/llvm/lib/CodeGen/BranchFolding.cpp b/llvm/lib/CodeGen/BranchFolding.cpp
index 42f54c2526487..38a4ff604f55a 100644
--- a/llvm/lib/CodeGen/BranchFolding.cpp
+++ b/llvm/lib/CodeGen/BranchFolding.cpp
@@ -161,9 +161,8 @@ bool BranchFolderLegacy::runOnMachineFunction(MachineFunction &MF) {
   TargetPassConfig *PassConfig = &getAnalysis<TargetPassConfig>();
   // TailMerge can create jump into if branches that make CFG irreducible for
   // HW that requires structurized CFG.
-  bool EnableTailMerge = !MF.getTarget().requiresStructuredCFG() &&
-                         PassConfig->getEnableTailMerge() &&
-                         this->EnableTailMerge;
+  bool EnableTailMerge =
+      !MF.getTarget().requiresStructuredCFG() && this->EnableTailMerge;
   MBFIWrapper MBBFreqInfo(
       getAnalysis<MachineBlockFrequencyInfoWrapperPass>().getMBFI());
   BranchFolder Folder(
diff --git a/llvm/lib/CodeGen/TargetPassConfig.cpp b/llvm/lib/CodeGen/TargetPassConfig.cpp
index fa1bb84ec5319..ec9012e99107b 100644
--- a/llvm/lib/CodeGen/TargetPassConfig.cpp
+++ b/llvm/lib/CodeGen/TargetPassConfig.cpp
@@ -1514,7 +1514,7 @@ void TargetPassConfig::addMachineLateOptimization() {
   addPass(&MachineLateInstrsCleanupID);
 
   // Branch folding must be run after regalloc and prolog/epilog insertion.
-  addPass(&BranchFolderPassID);
+  addPass(createBranchFolderPass(getEnableTailMerge()));
 
   // Tail duplication.
   // Note that duplicating tail just increases code size and degrades

>From 17fcc45152e17ac5f95bc8d6bd2ba63f31787484 Mon Sep 17 00:00:00 2001
From: "Mikhail R. Gadelha" <mikhail at igalia.com>
Date: Fri, 11 Apr 2025 12:15:56 -0300
Subject: [PATCH 05/13] Removed the default arg

Signed-off-by: Mikhail R. Gadelha <mikhail at igalia.com>
---
 llvm/lib/CodeGen/BranchFolding.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/CodeGen/BranchFolding.cpp b/llvm/lib/CodeGen/BranchFolding.cpp
index 38a4ff604f55a..97c5a5b017883 100644
--- a/llvm/lib/CodeGen/BranchFolding.cpp
+++ b/llvm/lib/CodeGen/BranchFolding.cpp
@@ -95,7 +95,7 @@ class BranchFolderLegacy : public MachineFunctionPass {
 public:
   static char ID;
 
-  explicit BranchFolderLegacy(bool EnableTailMerge = true)
+  explicit BranchFolderLegacy(bool EnableTailMerge)
       : MachineFunctionPass(ID), EnableTailMerge(EnableTailMerge) {}
 
   bool runOnMachineFunction(MachineFunction &MF) override;
@@ -2091,6 +2091,6 @@ bool BranchFolder::HoistCommonCodeInSuccs(MachineBasicBlock *MBB) {
   return true;
 }
 
-MachineFunctionPass *llvm::createBranchFolderPass(bool EnableTailMerge = true) {
+MachineFunctionPass *llvm::createBranchFolderPass(bool EnableTailMerge) {
   return new BranchFolderLegacy(EnableTailMerge);
 }

>From 73b228a47dd23fe7ea42e3d83926814cf3e4253a Mon Sep 17 00:00:00 2001
From: "Mikhail R. Gadelha" <mikhail at igalia.com>
Date: Fri, 11 Apr 2025 13:05:50 -0300
Subject: [PATCH 06/13] PassConfig is no longer needed

Signed-off-by: Mikhail R. Gadelha <mikhail at igalia.com>
---
 llvm/lib/CodeGen/BranchFolding.cpp | 3 ---
 1 file changed, 3 deletions(-)

diff --git a/llvm/lib/CodeGen/BranchFolding.cpp b/llvm/lib/CodeGen/BranchFolding.cpp
index 97c5a5b017883..90385a8d6a7a5 100644
--- a/llvm/lib/CodeGen/BranchFolding.cpp
+++ b/llvm/lib/CodeGen/BranchFolding.cpp
@@ -39,7 +39,6 @@
 #include "llvm/CodeGen/MachineSizeOpts.h"
 #include "llvm/CodeGen/TargetInstrInfo.h"
 #include "llvm/CodeGen/TargetOpcodes.h"
-#include "llvm/CodeGen/TargetPassConfig.h"
 #include "llvm/CodeGen/TargetRegisterInfo.h"
 #include "llvm/CodeGen/TargetSubtargetInfo.h"
 #include "llvm/IR/DebugInfoMetadata.h"
@@ -104,7 +103,6 @@ class BranchFolderLegacy : public MachineFunctionPass {
     AU.addRequired<MachineBlockFrequencyInfoWrapperPass>();
     AU.addRequired<MachineBranchProbabilityInfoWrapperPass>();
     AU.addRequired<ProfileSummaryInfoWrapperPass>();
-    AU.addRequired<TargetPassConfig>();
     MachineFunctionPass::getAnalysisUsage(AU);
   }
 
@@ -158,7 +156,6 @@ bool BranchFolderLegacy::runOnMachineFunction(MachineFunction &MF) {
   if (skipFunction(MF.getFunction()))
     return false;
 
-  TargetPassConfig *PassConfig = &getAnalysis<TargetPassConfig>();
   // TailMerge can create jump into if branches that make CFG irreducible for
   // HW that requires structurized CFG.
   bool EnableTailMerge =

>From f2dd02c4c56d11d6d166f149594c38ddbc68c49b Mon Sep 17 00:00:00 2001
From: "Mikhail R. Gadelha" <mikhail at igalia.com>
Date: Mon, 14 Apr 2025 19:20:52 -0300
Subject: [PATCH 07/13] Implemented suggested changes

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

diff --git a/llvm/include/llvm/CodeGen/TargetPassConfig.h b/llvm/include/llvm/CodeGen/TargetPassConfig.h
index 1af7267fa9ecf..c3fc31243c51a 100644
--- a/llvm/include/llvm/CodeGen/TargetPassConfig.h
+++ b/llvm/include/llvm/CodeGen/TargetPassConfig.h
@@ -457,7 +457,7 @@ class TargetPassConfig : public ImmutablePass {
 
   /// Add a CodeGen pass at this point in the pipeline after checking overrides.
   /// Return the pass that was added, or zero if no pass was added.
-  AnalysisID addPass(AnalysisID PassID);
+  AnalysisID addPass(IdentifyingPassPtr PassID);
 
   /// Add a pass to the PassManager if that pass is supposed to be run, as
   /// determined by the StartAfter and StopAfter options. Takes ownership of the
diff --git a/llvm/lib/CodeGen/TargetPassConfig.cpp b/llvm/lib/CodeGen/TargetPassConfig.cpp
index ec9012e99107b..649e76bb84828 100644
--- a/llvm/lib/CodeGen/TargetPassConfig.cpp
+++ b/llvm/lib/CodeGen/TargetPassConfig.cpp
@@ -745,7 +745,9 @@ void TargetPassConfig::addPass(Pass *P) {
 ///
 /// addPass cannot return a pointer to the pass instance because is internal the
 /// PassManager and the instance we create here may already be freed.
-AnalysisID TargetPassConfig::addPass(AnalysisID PassID) {
+AnalysisID TargetPassConfig::addPass(IdentifyingPassPtr PassPtr) {
+  AnalysisID PassID = PassPtr.isInstance() ? PassPtr.getInstance()->getPassID()
+                                           : PassPtr.getID();
   IdentifyingPassPtr TargetID = getPassSubstitution(PassID);
   IdentifyingPassPtr FinalPtr = overridePass(PassID, TargetID);
   if (!FinalPtr.isValid())
@@ -1513,8 +1515,7 @@ void TargetPassConfig::addMachineLateOptimization() {
   // Cleanup of redundant immediate/address loads.
   addPass(&MachineLateInstrsCleanupID);
 
-  // Branch folding must be run after regalloc and prolog/epilog insertion.
-  addPass(createBranchFolderPass(getEnableTailMerge()));
+  addPass(IdentifyingPassPtr(createBranchFolderPass(getEnableTailMerge())));
 
   // Tail duplication.
   // Note that duplicating tail just increases code size and degrades

>From fc156b55783ea05e0285ef9348ab00f4e300fad5 Mon Sep 17 00:00:00 2001
From: "Mikhail R. Gadelha" <mikhail at igalia.com>
Date: Tue, 15 Apr 2025 09:51:06 -0300
Subject: [PATCH 08/13] Default to true

Signed-off-by: Mikhail R. Gadelha <mikhail at igalia.com>
---
 llvm/lib/CodeGen/BranchFolding.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/CodeGen/BranchFolding.cpp b/llvm/lib/CodeGen/BranchFolding.cpp
index 90385a8d6a7a5..b54881dd95f6b 100644
--- a/llvm/lib/CodeGen/BranchFolding.cpp
+++ b/llvm/lib/CodeGen/BranchFolding.cpp
@@ -94,7 +94,7 @@ class BranchFolderLegacy : public MachineFunctionPass {
 public:
   static char ID;
 
-  explicit BranchFolderLegacy(bool EnableTailMerge)
+  explicit BranchFolderLegacy(bool EnableTailMerge = true)
       : MachineFunctionPass(ID), EnableTailMerge(EnableTailMerge) {}
 
   bool runOnMachineFunction(MachineFunction &MF) override;
@@ -2088,6 +2088,6 @@ bool BranchFolder::HoistCommonCodeInSuccs(MachineBasicBlock *MBB) {
   return true;
 }
 
-MachineFunctionPass *llvm::createBranchFolderPass(bool EnableTailMerge) {
+MachineFunctionPass *llvm::createBranchFolderPass(bool EnableTailMerge = true) {
   return new BranchFolderLegacy(EnableTailMerge);
 }

>From b49d6d36cd84498112001dccc74ff122bf503532 Mon Sep 17 00:00:00 2001
From: "Mikhail R. Gadelha" <mikhail at igalia.com>
Date: Tue, 15 Apr 2025 15:12:24 -0300
Subject: [PATCH 09/13] Revert "Implemented suggested changes"

This reverts commit f2dd02c4c56d11d6d166f149594c38ddbc68c49b.
---
 llvm/include/llvm/CodeGen/TargetPassConfig.h | 2 +-
 llvm/lib/CodeGen/TargetPassConfig.cpp        | 7 +++----
 2 files changed, 4 insertions(+), 5 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/TargetPassConfig.h b/llvm/include/llvm/CodeGen/TargetPassConfig.h
index c3fc31243c51a..1af7267fa9ecf 100644
--- a/llvm/include/llvm/CodeGen/TargetPassConfig.h
+++ b/llvm/include/llvm/CodeGen/TargetPassConfig.h
@@ -457,7 +457,7 @@ class TargetPassConfig : public ImmutablePass {
 
   /// Add a CodeGen pass at this point in the pipeline after checking overrides.
   /// Return the pass that was added, or zero if no pass was added.
-  AnalysisID addPass(IdentifyingPassPtr PassID);
+  AnalysisID addPass(AnalysisID PassID);
 
   /// Add a pass to the PassManager if that pass is supposed to be run, as
   /// determined by the StartAfter and StopAfter options. Takes ownership of the
diff --git a/llvm/lib/CodeGen/TargetPassConfig.cpp b/llvm/lib/CodeGen/TargetPassConfig.cpp
index 649e76bb84828..ec9012e99107b 100644
--- a/llvm/lib/CodeGen/TargetPassConfig.cpp
+++ b/llvm/lib/CodeGen/TargetPassConfig.cpp
@@ -745,9 +745,7 @@ void TargetPassConfig::addPass(Pass *P) {
 ///
 /// addPass cannot return a pointer to the pass instance because is internal the
 /// PassManager and the instance we create here may already be freed.
-AnalysisID TargetPassConfig::addPass(IdentifyingPassPtr PassPtr) {
-  AnalysisID PassID = PassPtr.isInstance() ? PassPtr.getInstance()->getPassID()
-                                           : PassPtr.getID();
+AnalysisID TargetPassConfig::addPass(AnalysisID PassID) {
   IdentifyingPassPtr TargetID = getPassSubstitution(PassID);
   IdentifyingPassPtr FinalPtr = overridePass(PassID, TargetID);
   if (!FinalPtr.isValid())
@@ -1515,7 +1513,8 @@ void TargetPassConfig::addMachineLateOptimization() {
   // Cleanup of redundant immediate/address loads.
   addPass(&MachineLateInstrsCleanupID);
 
-  addPass(IdentifyingPassPtr(createBranchFolderPass(getEnableTailMerge())));
+  // Branch folding must be run after regalloc and prolog/epilog insertion.
+  addPass(createBranchFolderPass(getEnableTailMerge()));
 
   // Tail duplication.
   // Note that duplicating tail just increases code size and degrades

>From 129347733db518861cd36aa0a293334f4185ddcb Mon Sep 17 00:00:00 2001
From: "Mikhail R. Gadelha" <mikhail at igalia.com>
Date: Tue, 15 Apr 2025 15:29:07 -0300
Subject: [PATCH 10/13] Check if the pass was disabled when adding the pass via
 the pass' pointer

Signed-off-by: Mikhail R. Gadelha <mikhail at igalia.com>
---
 llvm/lib/CodeGen/TargetPassConfig.cpp | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/llvm/lib/CodeGen/TargetPassConfig.cpp b/llvm/lib/CodeGen/TargetPassConfig.cpp
index ec9012e99107b..8de3ff808f083 100644
--- a/llvm/lib/CodeGen/TargetPassConfig.cpp
+++ b/llvm/lib/CodeGen/TargetPassConfig.cpp
@@ -707,6 +707,10 @@ void TargetPassConfig::addPass(Pass *P) {
   // and shouldn't reference it.
   AnalysisID PassID = P->getPassID();
 
+  IdentifyingPassPtr TargetID = getPassSubstitution(PassID);
+  if (!overridePass(PassID, TargetID).isValid())
+    return;
+
   if (StartBefore == PassID && StartBeforeCount++ == StartBeforeInstanceNum)
     Started = true;
   if (StopBefore == PassID && StopBeforeCount++ == StopBeforeInstanceNum)

>From 756e1a5ce9035d3637d8f3081202fada172e5e37 Mon Sep 17 00:00:00 2001
From: "Mikhail R. Gadelha" <mikhail at igalia.com>
Date: Fri, 18 Apr 2025 14:27:54 -0300
Subject: [PATCH 11/13] Moved requiresStructuredCFG check to pass creation

Signed-off-by: Mikhail R. Gadelha <mikhail at igalia.com>
---
 llvm/lib/CodeGen/BranchFolding.cpp    | 3 ---
 llvm/lib/CodeGen/TargetPassConfig.cpp | 3 ++-
 2 files changed, 2 insertions(+), 4 deletions(-)

diff --git a/llvm/lib/CodeGen/BranchFolding.cpp b/llvm/lib/CodeGen/BranchFolding.cpp
index b54881dd95f6b..43b8a6a583a29 100644
--- a/llvm/lib/CodeGen/BranchFolding.cpp
+++ b/llvm/lib/CodeGen/BranchFolding.cpp
@@ -124,9 +124,6 @@ INITIALIZE_PASS(BranchFolderLegacy, DEBUG_TYPE, "Control Flow Optimizer", false,
 PreservedAnalyses BranchFolderPass::run(MachineFunction &MF,
                                         MachineFunctionAnalysisManager &MFAM) {
   MFPropsModifier _(*this, MF);
-  bool EnableTailMerge =
-      !MF.getTarget().requiresStructuredCFG() && this->EnableTailMerge;
-
   auto &MBPI = MFAM.getResult<MachineBranchProbabilityAnalysis>(MF);
   auto *PSI = MFAM.getResult<ModuleAnalysisManagerMachineFunctionProxy>(MF)
                   .getCachedResult<ProfileSummaryAnalysis>(
diff --git a/llvm/lib/CodeGen/TargetPassConfig.cpp b/llvm/lib/CodeGen/TargetPassConfig.cpp
index 8de3ff808f083..a8e676ad54b22 100644
--- a/llvm/lib/CodeGen/TargetPassConfig.cpp
+++ b/llvm/lib/CodeGen/TargetPassConfig.cpp
@@ -1518,7 +1518,8 @@ void TargetPassConfig::addMachineLateOptimization() {
   addPass(&MachineLateInstrsCleanupID);
 
   // Branch folding must be run after regalloc and prolog/epilog insertion.
-  addPass(createBranchFolderPass(getEnableTailMerge()));
+  addPass(createBranchFolderPass(!TM->requiresStructuredCFG() &&
+                                 getEnableTailMerge()));
 
   // Tail duplication.
   // Note that duplicating tail just increases code size and degrades

>From 501a1cf70188cb6b55586a505b5b926812cecd5c Mon Sep 17 00:00:00 2001
From: "Mikhail R. Gadelha" <mikhail at igalia.com>
Date: Mon, 21 Apr 2025 09:53:11 -0300
Subject: [PATCH 12/13] Use isPassSubstitutedOrOverridden to check if the pass
 was disabled

Signed-off-by: Mikhail R. Gadelha <mikhail at igalia.com>
---
 llvm/lib/CodeGen/TargetPassConfig.cpp | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/llvm/lib/CodeGen/TargetPassConfig.cpp b/llvm/lib/CodeGen/TargetPassConfig.cpp
index a8e676ad54b22..4a5469b6d47ad 100644
--- a/llvm/lib/CodeGen/TargetPassConfig.cpp
+++ b/llvm/lib/CodeGen/TargetPassConfig.cpp
@@ -707,10 +707,6 @@ void TargetPassConfig::addPass(Pass *P) {
   // and shouldn't reference it.
   AnalysisID PassID = P->getPassID();
 
-  IdentifyingPassPtr TargetID = getPassSubstitution(PassID);
-  if (!overridePass(PassID, TargetID).isValid())
-    return;
-
   if (StartBefore == PassID && StartBeforeCount++ == StartBeforeInstanceNum)
     Started = true;
   if (StopBefore == PassID && StopBeforeCount++ == StopBeforeInstanceNum)
@@ -1518,8 +1514,9 @@ void TargetPassConfig::addMachineLateOptimization() {
   addPass(&MachineLateInstrsCleanupID);
 
   // Branch folding must be run after regalloc and prolog/epilog insertion.
-  addPass(createBranchFolderPass(!TM->requiresStructuredCFG() &&
-                                 getEnableTailMerge()));
+  if(!isPassSubstitutedOrOverridden(&BranchFolderPassID))
+    addPass(createBranchFolderPass(!TM->requiresStructuredCFG() &&
+                                  getEnableTailMerge()));
 
   // Tail duplication.
   // Note that duplicating tail just increases code size and degrades

>From 61251d309508ad9de53cd734c50e886718c83c10 Mon Sep 17 00:00:00 2001
From: "Mikhail R. Gadelha" <mikhail at igalia.com>
Date: Mon, 21 Apr 2025 09:57:17 -0300
Subject: [PATCH 13/13] Removed unnecessary local variable

Signed-off-by: Mikhail R. Gadelha <mikhail at igalia.com>
---
 llvm/lib/CodeGen/BranchFolding.cpp | 4 ----
 1 file changed, 4 deletions(-)

diff --git a/llvm/lib/CodeGen/BranchFolding.cpp b/llvm/lib/CodeGen/BranchFolding.cpp
index 43b8a6a583a29..ad4ab3142d542 100644
--- a/llvm/lib/CodeGen/BranchFolding.cpp
+++ b/llvm/lib/CodeGen/BranchFolding.cpp
@@ -153,10 +153,6 @@ bool BranchFolderLegacy::runOnMachineFunction(MachineFunction &MF) {
   if (skipFunction(MF.getFunction()))
     return false;
 
-  // TailMerge can create jump into if branches that make CFG irreducible for
-  // HW that requires structurized CFG.
-  bool EnableTailMerge =
-      !MF.getTarget().requiresStructuredCFG() && this->EnableTailMerge;
   MBFIWrapper MBBFreqInfo(
       getAnalysis<MachineBlockFrequencyInfoWrapperPass>().getMBFI());
   BranchFolder Folder(



More information about the llvm-commits mailing list