Skip to content

Commit 97fff7e

Browse files
Abnikant Singhabnikant
authored andcommitted
[AIE] Refactor AIEMachineAlignment
1 parent b1ac10e commit 97fff7e

File tree

4 files changed

+51
-38
lines changed

4 files changed

+51
-38
lines changed

llvm/lib/Target/AIE/AIEBaseInstrInfo.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,39 @@ bool AIEBaseInstrInfo::isLastZOLSetupBundleInMBB(
397397
return true;
398398
}
399399

400+
unsigned AIEBaseInstrInfo::getRegionSize(
401+
llvm::iterator_range<MachineBasicBlock::iterator> Region) const {
402+
unsigned Size = 0;
403+
LLVM_DEBUG(dbgs() << "---Region Begin---\n");
404+
for (auto it = Region.begin(), end = Region.end(); it != end; ++it) {
405+
if (it->isBundle()) {
406+
AIE::MachineBundle Bundle = getAIEMachineBundle(it);
407+
const VLIWFormat *Format = Bundle.getFormatOrNull();
408+
assert(Format);
409+
Size += Format->getSize();
410+
LLVM_DEBUG(dbgs() << Format->Name << "\n");
411+
}
412+
}
413+
LLVM_DEBUG(dbgs() << "---Region End---\n");
414+
LLVM_DEBUG(dbgs() << "Region Size"
415+
<< " " << Size << "\n");
416+
return Size;
417+
}
418+
419+
const AIE::MachineBundle
420+
AIEBaseInstrInfo::getAIEMachineBundle(MachineBasicBlock::iterator MII) const {
421+
AIE::MachineBundle Bundle(getFormatInterface());
422+
// Iterate over the instructions in the bundle.
423+
MachineBasicBlock::const_instr_iterator I = ++MII->getIterator();
424+
MachineBasicBlock::instr_iterator E = MII->getParent()->instr_end();
425+
while (I != E && I->isInsideBundle()) {
426+
MachineInstr *MI = const_cast<MachineInstr *>(&(*I));
427+
Bundle.add(MI);
428+
I++;
429+
}
430+
return Bundle;
431+
}
432+
400433
unsigned computeRegStateFlags(const MachineOperand &RegOp) {
401434
assert(RegOp.isReg() && "Not a register operand");
402435
assert(!RegOp.getSubReg() && "RegOp has SubReg flags set");

llvm/lib/Target/AIE/AIEBaseInstrInfo.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929

3030
namespace llvm {
3131

32+
namespace AIE {
33+
template <class I> class Bundle;
34+
}
35+
3236
struct AIEBaseInstrInfo : public TargetInstrInfo {
3337
using TargetInstrInfo::TargetInstrInfo;
3438

@@ -435,6 +439,10 @@ struct AIEBaseInstrInfo : public TargetInstrInfo {
435439
// setting LE Reg
436440
bool isZOLSetupBundle(MachineBasicBlock::iterator MII) const;
437441
bool isLastZOLSetupBundleInMBB(MachineBasicBlock::iterator MII) const;
442+
virtual const AIE::Bundle<MachineInstr>
443+
getAIEMachineBundle(MachineBasicBlock::iterator MII) const;
444+
virtual unsigned
445+
getRegionSize(llvm::iterator_range<MachineBasicBlock::iterator> Region) const;
438446

439447
/// Central place to compute RAW/WAR/WAW operand latencies.
440448
/// This uses itineraries when they exist. It returns std::nullopt for

llvm/lib/Target/AIE/AIEMachineAlignment.cpp

Lines changed: 8 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,6 @@ const AIEBaseInstrInfo *getTII(MachineBasicBlock::iterator MII) {
3939
return static_cast<const AIEBaseInstrInfo *>(Subtarget.getInstrInfo());
4040
}
4141

42-
const AIE::MachineBundle getAIEMachineBundle(MachineBasicBlock::iterator MII) {
43-
const AIEBaseInstrInfo *TII = getTII(MII);
44-
AIE::MachineBundle Bundle(TII->getFormatInterface());
45-
// Iterate over the instructions in the bundle.
46-
MachineBasicBlock::const_instr_iterator I = ++MII->getIterator();
47-
MachineBasicBlock::instr_iterator E = MII->getParent()->instr_end();
48-
while (I != E && I->isInsideBundle()) {
49-
MachineInstr *MI = const_cast<MachineInstr *>(&(*I));
50-
Bundle.add(MI);
51-
I++;
52-
}
53-
return Bundle;
54-
}
55-
5642
const VLIWFormat *getElongatedFormat(AIE::MachineBundle &Bundle,
5743
unsigned Size) {
5844
if (Bundle.isNOPBundle()) {
@@ -108,7 +94,8 @@ unsigned applyRegionAlignment(MachineBasicBlock::iterator MI,
10894
unsigned f = 0;
10995
while (MI != EndMI) {
11096
if (MI->isBundle()) {
111-
AIE::MachineBundle Bundle = getAIEMachineBundle(MI);
97+
const AIEBaseInstrInfo *TII = getTII(MI);
98+
AIE::MachineBundle Bundle = TII->getAIEMachineBundle(MI);
11299
const VLIWFormat *Format = Bundle.getFormatOrNull();
113100
assert(Format);
114101
Size = Format->getSize();
@@ -154,34 +141,16 @@ unsigned applyRegionAlignment(MachineBasicBlock::iterator MI,
154141
return PadBytes;
155142
}
156143

157-
unsigned
158-
getRegionSize(llvm::iterator_range<MachineBasicBlock::iterator> Region) {
159-
unsigned Size = 0;
160-
LLVM_DEBUG(dbgs() << "---Region Begin---\n");
161-
for (auto it = Region.begin(), end = Region.end(); it != end; ++it) {
162-
if (it->isBundle()) {
163-
AIE::MachineBundle Bundle = getAIEMachineBundle(it);
164-
const VLIWFormat *Format = Bundle.getFormatOrNull();
165-
assert(Format);
166-
Size += Format->getSize();
167-
LLVM_DEBUG(dbgs() << Format->Name << "\n");
168-
}
169-
}
170-
LLVM_DEBUG(dbgs() << "---Region End---\n");
171-
LLVM_DEBUG(dbgs() << "Region Size"
172-
<< " " << Size << "\n");
173-
return Size;
174-
}
175-
176144
} // namespace
177145

178146
void AIEMachineAlignment::applyBundlesAlignment(
179147
const std::vector<llvm::iterator_range<MachineBasicBlock::iterator>>
180-
&Regions) {
148+
&Regions,
149+
const AIEBaseInstrInfo *TII) {
181150
for (auto Region : Regions) {
182151
unsigned Size = 0;
183152
unsigned PadBytes = 0;
184-
Size = getRegionSize(Region);
153+
Size = TII->getRegionSize(Region);
185154
if ((Size % 16) == 0)
186155
continue;
187156
PadBytes = 16 - (Size % 16);
@@ -226,7 +195,9 @@ bool AIEMachineAlignment::runOnMachineFunction(MachineFunction &MF) {
226195
for (auto &MBB : MF) {
227196
std::vector<llvm::iterator_range<MachineBasicBlock::iterator>> Regions =
228197
findRegions(MBB);
229-
applyBundlesAlignment(Regions);
198+
auto *TII = static_cast<const AIEBaseInstrInfo *>(
199+
MBB.getParent()->getSubtarget().getInstrInfo());
200+
applyBundlesAlignment(Regions, TII);
230201
// Clean up BB local Regions
231202
Regions.clear();
232203
}

llvm/lib/Target/AIE/AIEMachineAlignment.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ class AIEMachineAlignment : public llvm::MachineFunctionPass {
2828
bool runOnMachineFunction(llvm::MachineFunction &MF) override;
2929
void applyBundlesAlignment(
3030
const std::vector<llvm::iterator_range<MachineBasicBlock::iterator>>
31-
&Regions);
31+
&Regions,
32+
const AIEBaseInstrInfo *TII);
3233
};
3334

3435
} // end namespace llvm

0 commit comments

Comments
 (0)