Skip to content

Commit d11e54f

Browse files
committed
[MacroFusion] Support multiple predicators (#72219)
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 ce08c7e commit d11e54f

File tree

3 files changed

+38
-23
lines changed

3 files changed

+38
-23
lines changed

llvm/include/llvm/CodeGen/MacroFusion.h

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

17-
#include <functional>
17+
#include "llvm/ADT/ArrayRef.h"
1818
#include <memory>
1919

2020
namespace llvm {
@@ -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(ArrayRef<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(ArrayRef<MacroFusionPredTy> Predicates);
6062

6163
} // end namespace llvm
6264

llvm/lib/CodeGen/MacroFusion.cpp

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -137,19 +137,34 @@ 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(ArrayRef<MacroFusionPredTy> Predicates, bool FuseBlock)
146+
: Predicates(Predicates.begin(), Predicates.end()), FuseBlock(FuseBlock) {
147+
}
147148

148149
void apply(ScheduleDAGInstrs *DAGInstrs) override;
150+
151+
bool shouldScheduleAdjacent(const TargetInstrInfo &TII,
152+
const TargetSubtargetInfo &STI,
153+
const MachineInstr *FirstMI,
154+
const MachineInstr &SecondMI);
149155
};
150156

151157
} // end anonymous namespace
152158

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

199214
std::unique_ptr<ScheduleDAGMutation>
200-
llvm::createMacroFusionDAGMutation(
201-
ShouldSchedulePredTy shouldScheduleAdjacent) {
202-
if(EnableMacroFusion)
203-
return std::make_unique<MacroFusion>(shouldScheduleAdjacent, true);
215+
llvm::createMacroFusionDAGMutation(ArrayRef<MacroFusionPredTy> Predicates) {
216+
if (EnableMacroFusion)
217+
return std::make_unique<MacroFusion>(Predicates, true);
204218
return nullptr;
205219
}
206220

207-
std::unique_ptr<ScheduleDAGMutation>
208-
llvm::createBranchMacroFusionDAGMutation(
209-
ShouldSchedulePredTy shouldScheduleAdjacent) {
210-
if(EnableMacroFusion)
211-
return std::make_unique<MacroFusion>(shouldScheduleAdjacent, false);
221+
std::unique_ptr<ScheduleDAGMutation> llvm::createBranchMacroFusionDAGMutation(
222+
ArrayRef<MacroFusionPredTy> Predicates) {
223+
if (EnableMacroFusion)
224+
return std::make_unique<MacroFusion>(Predicates, false);
212225
return nullptr;
213226
}

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 {

0 commit comments

Comments
 (0)