Skip to content

Commit e910bae

Browse files
committed
[MacroFusion] Support multiple predicators
The user can provide multiple predicators to MacroFusion and the DAG mutation will be applied if one of them is evalated to true. `ShouldSchedulePredTy` is renamed to `MacroFusionPredTy`.
1 parent cac82e2 commit e910bae

File tree

9 files changed

+43
-29
lines changed

9 files changed

+43
-29
lines changed

llvm/include/llvm/CodeGen/MacroFusion.h

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
#ifndef LLVM_CODEGEN_MACROFUSION_H
1515
#define LLVM_CODEGEN_MACROFUSION_H
1616

17-
#include <functional>
1817
#include <memory>
18+
#include <vector>
1919

2020
namespace llvm {
2121

@@ -29,10 +29,10 @@ class SUnit;
2929
/// Check if the instr pair, FirstMI and SecondMI, should be fused
3030
/// together. Given SecondMI, when FirstMI is unspecified, then check if
3131
/// SecondMI may be part of a fused pair at all.
32-
using ShouldSchedulePredTy = std::function<bool(const TargetInstrInfo &TII,
33-
const TargetSubtargetInfo &TSI,
34-
const MachineInstr *FirstMI,
35-
const MachineInstr &SecondMI)>;
32+
using MacroFusionPredTy = bool (*)(const TargetInstrInfo &TII,
33+
const TargetSubtargetInfo &STI,
34+
const MachineInstr *FirstMI,
35+
const MachineInstr &SecondMI);
3636

3737
/// Checks if the number of cluster edges between SU and its predecessors is
3838
/// less than FuseLimit
@@ -48,15 +48,17 @@ bool fuseInstructionPair(ScheduleDAGInstrs &DAG, SUnit &FirstSU,
4848

4949
/// Create a DAG scheduling mutation to pair instructions back to back
5050
/// for instructions that benefit according to the target-specific
51-
/// shouldScheduleAdjacent predicate function.
51+
/// predicate functions. shouldScheduleAdjacent will be true if any of the
52+
/// provided predicates are true.
5253
std::unique_ptr<ScheduleDAGMutation>
53-
createMacroFusionDAGMutation(ShouldSchedulePredTy shouldScheduleAdjacent);
54+
createMacroFusionDAGMutation(std::vector<MacroFusionPredTy> Predicates);
5455

5556
/// Create a DAG scheduling mutation to pair branch instructions with one
5657
/// of their predecessors back to back for instructions that benefit according
57-
/// to the target-specific shouldScheduleAdjacent predicate function.
58+
/// to the target-specific predicate functions. shouldScheduleAdjacent will be
59+
/// true if any of the provided predicates are true.
5860
std::unique_ptr<ScheduleDAGMutation>
59-
createBranchMacroFusionDAGMutation(ShouldSchedulePredTy shouldScheduleAdjacent);
61+
createBranchMacroFusionDAGMutation(std::vector<MacroFusionPredTy> Predicates);
6062

6163
} // end namespace llvm
6264

llvm/lib/CodeGen/MacroFusion.cpp

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -137,19 +137,33 @@ namespace {
137137
/// Post-process the DAG to create cluster edges between instrs that may
138138
/// be fused by the processor into a single operation.
139139
class MacroFusion : public ScheduleDAGMutation {
140-
ShouldSchedulePredTy shouldScheduleAdjacent;
140+
std::vector<MacroFusionPredTy> Predicates;
141141
bool FuseBlock;
142142
bool scheduleAdjacentImpl(ScheduleDAGInstrs &DAG, SUnit &AnchorSU);
143143

144144
public:
145-
MacroFusion(ShouldSchedulePredTy shouldScheduleAdjacent, bool FuseBlock)
146-
: shouldScheduleAdjacent(shouldScheduleAdjacent), FuseBlock(FuseBlock) {}
145+
MacroFusion(std::vector<MacroFusionPredTy> Predicates, bool FuseBlock)
146+
: Predicates(std::move(Predicates)), FuseBlock(FuseBlock) {}
147147

148148
void apply(ScheduleDAGInstrs *DAGInstrs) override;
149+
150+
bool shouldScheduleAdjacent(const TargetInstrInfo &TII,
151+
const TargetSubtargetInfo &STI,
152+
const MachineInstr *FirstMI,
153+
const MachineInstr &SecondMI);
149154
};
150155

151156
} // end anonymous namespace
152157

158+
bool MacroFusion::shouldScheduleAdjacent(const TargetInstrInfo &TII,
159+
const TargetSubtargetInfo &STI,
160+
const MachineInstr *FirstMI,
161+
const MachineInstr &SecondMI) {
162+
return llvm::any_of(Predicates, [&](MacroFusionPredTy Predicate) {
163+
return Predicate(TII, STI, FirstMI, SecondMI);
164+
});
165+
}
166+
153167
void MacroFusion::apply(ScheduleDAGInstrs *DAG) {
154168
if (FuseBlock)
155169
// For each of the SUnits in the scheduling block, try to fuse the instr in
@@ -197,17 +211,15 @@ bool MacroFusion::scheduleAdjacentImpl(ScheduleDAGInstrs &DAG, SUnit &AnchorSU)
197211
}
198212

199213
std::unique_ptr<ScheduleDAGMutation>
200-
llvm::createMacroFusionDAGMutation(
201-
ShouldSchedulePredTy shouldScheduleAdjacent) {
202-
if(EnableMacroFusion)
203-
return std::make_unique<MacroFusion>(shouldScheduleAdjacent, true);
214+
llvm::createMacroFusionDAGMutation(std::vector<MacroFusionPredTy> Predicates) {
215+
if (EnableMacroFusion)
216+
return std::make_unique<MacroFusion>(std::move(Predicates), true);
204217
return nullptr;
205218
}
206219

207-
std::unique_ptr<ScheduleDAGMutation>
208-
llvm::createBranchMacroFusionDAGMutation(
209-
ShouldSchedulePredTy shouldScheduleAdjacent) {
210-
if(EnableMacroFusion)
211-
return std::make_unique<MacroFusion>(shouldScheduleAdjacent, false);
220+
std::unique_ptr<ScheduleDAGMutation> llvm::createBranchMacroFusionDAGMutation(
221+
std::vector<MacroFusionPredTy> Predicates) {
222+
if (EnableMacroFusion)
223+
return std::make_unique<MacroFusion>(std::move(Predicates), false);
212224
return nullptr;
213225
}

llvm/lib/Target/AArch64/AArch64MacroFusion.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,5 +478,5 @@ static bool shouldScheduleAdjacent(const TargetInstrInfo &TII,
478478

479479
std::unique_ptr<ScheduleDAGMutation>
480480
llvm::createAArch64MacroFusionDAGMutation() {
481-
return createMacroFusionDAGMutation(shouldScheduleAdjacent);
481+
return createMacroFusionDAGMutation({shouldScheduleAdjacent});
482482
}

llvm/lib/Target/AMDGPU/AMDGPUMacroFusion.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ static bool shouldScheduleAdjacent(const TargetInstrInfo &TII_,
6060
namespace llvm {
6161

6262
std::unique_ptr<ScheduleDAGMutation> createAMDGPUMacroFusionDAGMutation() {
63-
return createMacroFusionDAGMutation(shouldScheduleAdjacent);
63+
return createMacroFusionDAGMutation({shouldScheduleAdjacent});
6464
}
6565

6666
} // end namespace llvm

llvm/lib/Target/AMDGPU/GCNVOPDUtils.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,10 @@ namespace {
142142
/// be turned into VOPD instructions
143143
/// Greedily pairs instruction candidates. O(n^2) algorithm.
144144
struct VOPDPairingMutation : ScheduleDAGMutation {
145-
ShouldSchedulePredTy shouldScheduleAdjacent; // NOLINT: function pointer
145+
MacroFusionPredTy shouldScheduleAdjacent; // NOLINT: function pointer
146146

147147
VOPDPairingMutation(
148-
ShouldSchedulePredTy shouldScheduleAdjacent) // NOLINT: function pointer
148+
MacroFusionPredTy shouldScheduleAdjacent) // NOLINT: function pointer
149149
: shouldScheduleAdjacent(shouldScheduleAdjacent) {}
150150

151151
void apply(ScheduleDAGInstrs *DAG) override {

llvm/lib/Target/ARM/ARMMacroFusion.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ static bool shouldScheduleAdjacent(const TargetInstrInfo &TII,
6363
}
6464

6565
std::unique_ptr<ScheduleDAGMutation> createARMMacroFusionDAGMutation() {
66-
return createMacroFusionDAGMutation(shouldScheduleAdjacent);
66+
return createMacroFusionDAGMutation({shouldScheduleAdjacent});
6767
}
6868

6969
} // end namespace llvm

llvm/lib/Target/PowerPC/PPCMacroFusion.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ static bool shouldScheduleAdjacent(const TargetInstrInfo &TII,
287287
namespace llvm {
288288

289289
std::unique_ptr<ScheduleDAGMutation> createPowerPCMacroFusionDAGMutation() {
290-
return createMacroFusionDAGMutation(shouldScheduleAdjacent);
290+
return createMacroFusionDAGMutation({shouldScheduleAdjacent});
291291
}
292292

293293
} // end namespace llvm

llvm/lib/Target/RISCV/RISCVMacroFusion.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,5 @@ static bool shouldScheduleAdjacent(const TargetInstrInfo &TII,
6565
}
6666

6767
std::unique_ptr<ScheduleDAGMutation> llvm::createRISCVMacroFusionDAGMutation() {
68-
return createMacroFusionDAGMutation(shouldScheduleAdjacent);
68+
return createMacroFusionDAGMutation({shouldScheduleAdjacent});
6969
}

llvm/lib/Target/X86/X86MacroFusion.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ static bool shouldScheduleAdjacent(const TargetInstrInfo &TII,
6868
namespace llvm {
6969

7070
std::unique_ptr<ScheduleDAGMutation> createX86MacroFusionDAGMutation() {
71-
return createBranchMacroFusionDAGMutation(shouldScheduleAdjacent);
71+
return createBranchMacroFusionDAGMutation({shouldScheduleAdjacent});
7272
}
7373

7474
} // end namespace llvm

0 commit comments

Comments
 (0)