Skip to content

Commit b1428ca

Browse files
committed
[AIEX] Improve pre-sched decisions for Post-SWP candidates
1 parent 8933b34 commit b1428ca

File tree

6 files changed

+80
-11
lines changed

6 files changed

+80
-11
lines changed

llvm/lib/Target/AIE/AIEBaseSubtarget.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ static cl::opt<bool> ForcePostPipeliner(
5959
cl::desc(
6060
"Force using AIE's post-pipeliner instead of the MachinePipeliner"),
6161
cl::init(false), cl::Hidden);
62+
63+
static cl::opt<bool> PreSchedPostSWPCandidates(
64+
"aie-presched-postpipeliner-candidates",
65+
cl::desc("Run pre-scheduler over potential postpipeliner candidates"),
66+
cl::init(true), cl::Hidden);
6267
// These are debugging/testing options.
6368

6469
// aie-latency-margin defines the latency that will be given to ExitSU edges.
@@ -864,3 +869,8 @@ AIEBaseSubtarget::getSMSMutationsImpl(const Triple &TT) {
864869
bool AIEBaseSubtarget::enableMachinePipeliner() const {
865870
return !ForcePostPipeliner;
866871
}
872+
873+
/// Whether to enable the pre-RA MachineScheduler for Post SWP candidates.
874+
bool AIEBaseSubtarget::shouldPreSchedPostSWPCandidates() const {
875+
return PreSchedPostSWPCandidates;
876+
}

llvm/lib/Target/AIE/AIEBaseSubtarget.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ class AIEBaseSubtarget {
8989
/// Whether to enable the pre-RA MachinePipeliner. This can be disabled to let
9090
/// the post-RA pipeliner handle the scheduling.
9191
bool enableMachinePipeliner() const;
92+
93+
/// Whether to enable the pre-RA MachineScheduler for Post SWP candidates.
94+
virtual bool shouldPreSchedPostSWPCandidates() const;
9295
};
9396
} // namespace llvm
9497

llvm/lib/Target/AIE/AIEMachineScheduler.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,6 @@ static cl::opt<bool> UseLoopHeuristics(
9191
"aie-loop-sched-heuristics", cl::init(true),
9292
cl::desc("Use special picking heuristics when scheduling a loop region"));
9393

94-
static cl::opt<bool> PreSchedFollowsSkipPipeliner(
95-
"aie-presched-follows-skip-pipeliner", cl::init(true),
96-
cl::desc("Don't run the prescheduler if the pipeliner is skipped"));
97-
9894
namespace {
9995
// A sentinel value to represent an unknown SUnit.
10096
const constexpr unsigned UnknownSUNum = ~0;
@@ -1082,11 +1078,19 @@ MachineBasicBlock *AIEPreRASchedStrategy::nextBlock() {
10821078
auto Skip = [](MachineBasicBlock *Block) {
10831079
if (!Block)
10841080
return false;
1085-
bool PrePipelinerDisabled =
1086-
AIELoopUtils::getPipelinerDisabled(*Block) ||
1087-
!Block->getParent()->getSubtarget().enableMachinePipeliner();
1088-
return PreSchedFollowsSkipPipeliner &&
1089-
AIELoopUtils::isSingleMBBLoop(Block) && PrePipelinerDisabled;
1081+
1082+
auto *TII = static_cast<const AIEBaseInstrInfo *>(
1083+
Block->getParent()->getSubtarget().getInstrInfo());
1084+
const AIEBaseSubtarget &STI = AIEBaseSubtarget::get(*Block->getParent());
1085+
1086+
if (AIELoopUtils::isPostSWPCandidate(*TII, Block)) {
1087+
bool ShouldSkip = !STI.shouldPreSchedPostSWPCandidates();
1088+
LLVM_DEBUG(dbgs() << "Skip pre-sched of post-SWP candidate "
1089+
<< Block->getName() << ":" << ShouldSkip << "\n");
1090+
return ShouldSkip;
1091+
}
1092+
1093+
return false;
10901094
};
10911095

10921096
do {

llvm/lib/Target/AIE/Utils/AIELoopUtils.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,4 +190,51 @@ bool hasUnrollPragma(const Loop *L) {
190190
return false;
191191
}
192192

193+
/// Check if this block is a post-SWP candidate.
194+
bool isPostSWPCandidate(const AIEBaseInstrInfo &TII,
195+
const MachineBasicBlock *MBB) {
196+
197+
if (!isSingleMBBLoop(MBB))
198+
return false;
199+
200+
const MachineInstr &Terminator = *MBB->getFirstTerminator();
201+
if (!TII.isHardwareLoopEnd(Terminator.getOpcode()))
202+
return false;
203+
204+
if (Terminator.getOperand(1).getMBB() != MBB)
205+
return false;
206+
207+
auto GetLoopStartBlock =
208+
[&](const MachineBasicBlock *LoopBlock) -> const MachineBasicBlock * {
209+
const MachineBasicBlock *LoopStartBlock = nullptr;
210+
for (auto *Pred : LoopBlock->predecessors()) {
211+
if (Pred == LoopBlock)
212+
continue;
213+
if (LoopStartBlock)
214+
return nullptr;
215+
LoopStartBlock = Pred;
216+
}
217+
return LoopStartBlock;
218+
};
219+
220+
auto LoopStartBlock = GetLoopStartBlock(MBB);
221+
if (!LoopStartBlock)
222+
return false;
223+
224+
auto FindLoopStart =
225+
[&](const MachineBasicBlock &Block) -> const MachineInstr * {
226+
for (auto &MI : reverse(Block)) {
227+
if (TII.isHardwareLoopStart(MI.getOpcode()))
228+
return &MI;
229+
}
230+
return nullptr;
231+
};
232+
233+
auto Init = FindLoopStart(*LoopStartBlock);
234+
if (!Init)
235+
return false;
236+
237+
return Init->getOperand(1).getImm() == 0;
238+
}
239+
193240
} // namespace llvm::AIELoopUtils

llvm/lib/Target/AIE/Utils/AIELoopUtils.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#ifndef LLVM_LIB_TARGET_AIE_UTILS_AIELOOPUTILS_H
1515
#define LLVM_LIB_TARGET_AIE_UTILS_AIELOOPUTILS_H
1616

17+
#include "AIEBaseInstrInfo.h"
1718
#include "llvm/Analysis/LoopInfo.h"
1819
#include "llvm/CodeGen/MachineLoopInfo.h"
1920

@@ -70,6 +71,10 @@ bool hasUnrollCountPragma(const MDNode *LoopID);
7071

7172
bool hasUnrollPragma(const Loop *L);
7273

74+
/// Check if this block is a post-SWP candidate.
75+
bool isPostSWPCandidate(const AIEBaseInstrInfo &TII,
76+
const MachineBasicBlock *MBB);
77+
7378
} // namespace llvm::AIELoopUtils
7479

7580
#endif

llvm/test/CodeGen/AIE/aie2/schedule/swp/disable.mir

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,10 @@ body: |
158158
; CHECK-NEXT: bb.1.for.body:
159159
; CHECK-NEXT: successors: %bb.2(0x04000000), %bb.1(0x7c000000)
160160
; CHECK-NEXT: {{ $}}
161-
; CHECK-NEXT: [[XOR:%[0-9]+]]:er = XOR [[MOV_RLC_imm10_pseudo]], [[MOV_RLC_imm10_pseudo1]]
162161
; CHECK-NEXT: [[LDA_dms_lda_pstm_nrm_imm:%[0-9]+]]:er, [[COPY:%[0-9]+]]:ep_as_32bit = LDA_dms_lda_pstm_nrm_imm [[COPY]], 4 :: (load (s32) from %ir.p.addr.04)
163-
; CHECK-NEXT: [[XOR1:%[0-9]+]]:er = XOR [[LDA_dms_lda_pstm_nrm_imm]], [[XOR]]
162+
; CHECK-NEXT: [[XOR:%[0-9]+]]:er = XOR [[MOV_RLC_imm10_pseudo]], [[MOV_RLC_imm10_pseudo1]]
164163
; CHECK-NEXT: [[MOV_RLC_imm10_pseudo:%[0-9]+]]:er = nuw nsw ADD_add_r_ri [[MOV_RLC_imm10_pseudo]], -1, implicit-def dead $srcarry
164+
; CHECK-NEXT: [[XOR1:%[0-9]+]]:er = XOR [[LDA_dms_lda_pstm_nrm_imm]], [[XOR]]
165165
; CHECK-NEXT: PseudoJNZ [[MOV_RLC_imm10_pseudo]], %bb.1
166166
; CHECK-NEXT: {{ $}}
167167
; CHECK-NEXT: bb.2.for.cond.cleanup:

0 commit comments

Comments
 (0)