[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
Fri Apr 11 08:17:23 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 1/5] [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/5] 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/5] 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 4/5] 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 5/5] 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);
}
More information about the llvm-commits
mailing list