Skip to content

Commit a54c2fe

Browse files
[AIE2P] Transform VST.FLUSH into VST.FLUSH.CONV in PostSelectOptimize
1 parent 1ff7995 commit a54c2fe

File tree

5 files changed

+318
-0
lines changed

5 files changed

+318
-0
lines changed

llvm/lib/Target/AIE/AIEBaseInstrInfo.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,15 @@ struct AIEBaseInstrInfo : public TargetInstrInfo {
259259
TypeSize Size) const {
260260
llvm_unreachable("Target didn't implement getCombinedPostIncOpcode");
261261
}
262+
263+
/// Check whether Opcode is a VST.PUSH.CONV
264+
virtual bool isFifoStoreConvOpcode(unsigned Opcode) const { return false; }
265+
/// \return Corresponding VST.FLUSH.CONV Opcode based on \a VST.FLUSH Opcode
266+
virtual std::optional<unsigned>
267+
getStoreFlushConvOpcode(unsigned StoreFlushOpcode) const {
268+
llvm_unreachable("Target didn't implement getStoreFlushConvOpcode!");
269+
}
270+
262271
/// \return AIE2p OpCode based on \a IntrinsicID
263272
virtual unsigned getOpCode(MachineInstr &MI) const {
264273
llvm_unreachable("Target didn't implement getOpCode");

llvm/lib/Target/AIE/AIEPostSelectOptimize.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,39 @@ bool fixLoadMemOpInfo(MachineFunction &MF, MachineBasicBlock &MBB,
584584
return Changed;
585585
}
586586

587+
// Replace the VST.FLUSH opcode with VST.FLUSH.CONV if it is chained with
588+
// VST.PUSH.CONV. The CONV variant behaves identically to the normal variant but
589+
// all actions are delayed by one pipeline stage.
590+
bool modifyStoreFlush(MachineBasicBlock &MBB, MachineRegisterInfo &MRI) {
591+
const TargetInstrInfo *TII = MBB.getParent()->getSubtarget().getInstrInfo();
592+
const AIEBaseInstrInfo *AIEII = static_cast<const AIEBaseInstrInfo *>(TII);
593+
bool Changed = false;
594+
595+
// Helper function to recursively update VST.FLUSH to VST.FLUSH.CONV
596+
std::function<void(const Register)> Impl = [&](const Register UseReg) {
597+
for (MachineInstr &UseMI : MRI.use_instructions(UseReg)) {
598+
std::optional<unsigned> StoreFlushConvOpcode =
599+
AIEII->getStoreFlushConvOpcode(UseMI.getOpcode());
600+
if (StoreFlushConvOpcode) {
601+
UseMI.setDesc(TII->get(*StoreFlushConvOpcode));
602+
Changed = true;
603+
// Update the opcode for the next dependent instruction in the chain
604+
const Register UseDstReg = UseMI.getOperand(0).getReg();
605+
Impl(UseDstReg);
606+
}
607+
}
608+
};
609+
610+
for (MachineInstr &MI : MBB) {
611+
if (AIEII->isFifoStoreConvOpcode(MI.getOpcode())) {
612+
const Register DstReg = MI.getOperand(0).getReg();
613+
Impl(DstReg);
614+
}
615+
}
616+
617+
return Changed;
618+
}
619+
587620
bool AIEPostSelectOptimize::runOnMachineFunction(MachineFunction &MF) {
588621
LLVM_DEBUG(dbgs() << "\n******* POST I-SEL OPTIMIZATION PASS *******\n"
589622
<< "********** Function: " << MF.getName() << '\n');
@@ -624,6 +657,14 @@ bool AIEPostSelectOptimize::runOnMachineFunction(MachineFunction &MF) {
624657
Changed |= fixLoadMemOpInfo(MF, MBB, MF.getRegInfo());
625658
}
626659

660+
// 5. Convert store flush instructions only on AIE2P targets: when VST.FLUSH
661+
// and VST.PUSH.CONV are chained, replace VST.FLUSH with VST.FLUSH.CONV
662+
if (MF.getTarget().getTargetTriple().isAIE2P()) {
663+
for (MachineBasicBlock &MBB : MF) {
664+
Changed |= modifyStoreFlush(MBB, MF.getRegInfo());
665+
}
666+
}
667+
627668
return Changed;
628669
}
629670

llvm/lib/Target/AIE/aie2p/AIE2PInstrInfo.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,27 @@ bool AIE2PInstrInfo::isGenericOffsetMemOpcode(unsigned Opcode) const {
270270
(Opcode == AIE2P::G_AIE_OFFSET_ZEXTLOAD));
271271
}
272272

273+
bool AIE2PInstrInfo::isFifoStoreConvOpcode(unsigned Opcode) const {
274+
return ((Opcode == AIE2P::VST_PUSH_544_CONV_bfp16ebs16_ebs8) ||
275+
(Opcode == AIE2P::VST_PUSH_544_CONV_bfp16ebs16_fp32) ||
276+
(Opcode == AIE2P::VST_PUSH_576_CONV_bfp16ebs8_fp32));
277+
}
278+
279+
std::optional<unsigned>
280+
AIE2PInstrInfo::getStoreFlushConvOpcode(unsigned StoreFlushOpcode) const {
281+
switch (StoreFlushOpcode) {
282+
case AIE2P::VST_FLUSH_512_normal_flush:
283+
return AIE2P::VST_FLUSH_512_CONV_normal_flush;
284+
case AIE2P::VST_FLUSH_512_fifo_1d_flush:
285+
return AIE2P::VST_FLUSH_512_CONV_fifo_1d_flush;
286+
case AIE2P::VST_FLUSH_512_2D:
287+
return AIE2P::VST_FLUSH_512_CONV_2D;
288+
case AIE2P::VST_FLUSH_512_3D:
289+
return AIE2P::VST_FLUSH_512_CONV_3D;
290+
}
291+
return std::nullopt;
292+
}
293+
273294
std::optional<unsigned> AIE2PInstrInfo::getCombinedPostIncOpcode(
274295
MachineInstr &BaseMemI, MachineInstr &PostIncI, TypeSize Size) const {
275296
switch (PostIncI.getOpcode()) {

llvm/lib/Target/AIE/aie2p/AIE2PInstrInfo.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ class AIE2PInstrInfo : public AIE2PGenInstrInfo {
8787
bool isBooleanNot(unsigned Opc) const override;
8888
bool isConstStep(const MachineInstr &MI, int64_t &Step) const override;
8989
bool isGenericOffsetMemOpcode(unsigned Opcode) const override;
90+
bool isFifoStoreConvOpcode(unsigned Opcode) const override;
9091

9192
bool verifyGenericInstruction(const MachineInstr &MI,
9293
StringRef &ErrInfo) const override;
@@ -97,6 +98,8 @@ class AIE2PInstrInfo : public AIE2PGenInstrInfo {
9798
std::optional<unsigned>
9899
getCombinedPostIncOpcode(MachineInstr &BaseMemI, MachineInstr &PtrAddI,
99100
TypeSize Size) const override;
101+
std::optional<unsigned>
102+
getStoreFlushConvOpcode(unsigned StoreFlushOpcode) const override;
100103
unsigned getOpCode(MachineInstr &MI) const override;
101104
Register getVaddSignControlRegister() const override;
102105

0 commit comments

Comments
 (0)