Skip to content

Commit f7f5824

Browse files
committed
[AIEX] Improve pre-sched decisions for Post-SWP candidates
1 parent d72b780 commit f7f5824

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
@@ -57,6 +57,11 @@ static cl::opt<bool> ForcePostPipeliner(
5757
cl::desc(
5858
"Force using AIE's post-pipeliner instead of the MachinePipeliner"),
5959
cl::init(false), cl::Hidden);
60+
61+
static cl::opt<bool> PreSchedPostSWPCandidates(
62+
"aie-presched-postpipeliner-candidates",
63+
cl::desc("Run pre-scheduler over potential postpipeliner candidates"),
64+
cl::init(true), cl::Hidden);
6065
// These are debugging/testing options.
6166

6267
// aie-latency-margin defines the latency that will be given to ExitSU edges.
@@ -927,3 +932,8 @@ bool AIEBaseSubtarget::enableWindowScheduler() const {
927932
unsigned AIEBaseSubtarget::getCriticalPathLimitImpl() const {
928933
return IfConversionCritPathLimit;
929934
}
935+
936+
/// Whether to enable the pre-RA MachineScheduler for Post SWP candidates.
937+
bool AIEBaseSubtarget::shouldPreSchedPostSWPCandidates() const {
938+
return PreSchedPostSWPCandidates;
939+
}

llvm/lib/Target/AIE/AIEBaseSubtarget.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ class AIEBaseSubtarget {
9696
/// Returns the critical path limit that EarlyIfConversion should use
9797
/// when deciding about a specific conversion - common implementation.
9898
unsigned getCriticalPathLimitImpl() const;
99+
100+
/// Whether to enable the pre-RA MachineScheduler for Post SWP candidates.
101+
virtual bool shouldPreSchedPostSWPCandidates() const;
99102
};
100103
} // namespace llvm
101104

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;
@@ -1086,11 +1082,19 @@ MachineBasicBlock *AIEPreRASchedStrategy::nextBlock() {
10861082
auto Skip = [](MachineBasicBlock *Block) {
10871083
if (!Block)
10881084
return false;
1089-
bool PrePipelinerDisabled =
1090-
AIELoopUtils::getPipelinerDisabled(*Block) ||
1091-
!Block->getParent()->getSubtarget().enableMachinePipeliner();
1092-
return PreSchedFollowsSkipPipeliner &&
1093-
AIELoopUtils::isSingleMBBLoop(Block) && PrePipelinerDisabled;
1085+
1086+
auto *TII = static_cast<const AIEBaseInstrInfo *>(
1087+
Block->getParent()->getSubtarget().getInstrInfo());
1088+
const AIEBaseSubtarget &STI = AIEBaseSubtarget::get(*Block->getParent());
1089+
1090+
if (AIELoopUtils::isPostSWPCandidate(*TII, Block)) {
1091+
bool ShouldSkip = !STI.shouldPreSchedPostSWPCandidates();
1092+
LLVM_DEBUG(dbgs() << "Skip pre-sched of post-SWP candidate "
1093+
<< Block->getName() << ":" << ShouldSkip << "\n");
1094+
return ShouldSkip;
1095+
}
1096+
1097+
return false;
10941098
};
10951099

10961100
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)