Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AIEX] Reschedule multi-slot instruction for better packing/schedule #217

Open
wants to merge 3 commits into
base: aie-public
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion llvm/lib/Target/AIE/AIEAlternateDescriptors.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@

namespace llvm {

using MutateInstructionMap =
std::unordered_map<MachineInstr *,
std::pair<MachineInstr *, const MCInstrDesc *>>;
using MIAltDescsMap = std::unordered_map<MachineInstr *, const MCInstrDesc *>;

class AIEAlternateDescriptors {
Expand All @@ -40,7 +43,11 @@ class AIEAlternateDescriptors {
const AIEBaseSubtarget &STI = AIEBaseSubtarget::get(*MI->getMF());
const AIEBaseInstrInfo *TII = STI.getInstrInfo();

AlternateDescs[MI] = &TII->get(AltInstOpcode);
setAlternateDescriptor(MI, &TII->get(AltInstOpcode));
}

void setAlternateDescriptor(MachineInstr *MI, const MCInstrDesc *AltDesc) {
AlternateDescs[MI] = AltDesc;
}

// Return the alternate descriptor for the given multi-opcode instruction.
Expand Down
153 changes: 137 additions & 16 deletions llvm/lib/Target/AIE/AIEHazardRecognizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,15 @@ FuncUnitWrapper &FuncUnitWrapper::operator|=(const FuncUnitWrapper &Other) {
return *this;
}

FuncUnitWrapper &FuncUnitWrapper::operator^=(const FuncUnitWrapper &Other) {
// XOR operation with the same FuncUnitWrapper will release resources.
Required ^= Other.Required;
Reserved ^= Other.Reserved;
Slots ^= Other.Slots;
MemoryBanks ^= Other.MemoryBanks;
return *this;
}

bool FuncUnitWrapper::conflict(const FuncUnitWrapper &Other) const {
if ((Required & Other.Required) != 0 || (Slots & Other.Slots) != 0 ||
(MemoryBanks & Other.MemoryBanks) != 0 ||
Expand Down Expand Up @@ -431,6 +440,14 @@ auto toHazardType(bool Conflict) {
}
} // namespace

ScheduleHazardRecognizer::HazardType AIEHazardRecognizer::getHazardType(
const MCInstrDesc &Desc, MemoryBankBits MemoryBanks,
iterator_range<const MachineOperand *> MIOperands,
const MachineRegisterInfo &MRI, int DeltaCycles) {
return getHazardType(Scoreboard, Desc, MemoryBanks, MIOperands, MRI,
DeltaCycles);
}

// These functions interpret the itinerary, translating InstrStages
// to ResourceCycles to apply.
// We deviate from the standard ScoreboardHazardRecognizer by not
Expand All @@ -448,10 +465,28 @@ ScheduleHazardRecognizer::HazardType AIEHazardRecognizer::getHazardType(
FUDepthLimit));
}

bool AIEHazardRecognizer::checkConflict(
ConflictTypeBits AIEHazardRecognizer::checkConflict(MachineInstr &MI,
int DeltaCycles) {
assert(!TII->getFormatInterface()->getAlternateInstsOpcode(MI.getOpcode()));
return checkConflict(Scoreboard, MI, DeltaCycles);
}

ConflictTypeBits AIEHazardRecognizer::checkConflict(
const ResourceScoreboard<FuncUnitWrapper> &Scoreboard, MachineInstr &MI,
int DeltaCycles) const {
const MCInstrDesc &Desc = MI.getDesc();
assert(!TII->getFormatInterface()->getAlternateInstsOpcode(MI.getOpcode()));
return checkConflict(Scoreboard, MI, MI.getDesc(), DeltaCycles);
}

ConflictTypeBits AIEHazardRecognizer::checkConflict(MachineInstr &MI,
const MCInstrDesc &Desc,
int DeltaCycles) {
return checkConflict(Scoreboard, MI, Desc, DeltaCycles);
}

ConflictTypeBits AIEHazardRecognizer::checkConflict(
const ResourceScoreboard<FuncUnitWrapper> &Scoreboard, MachineInstr &MI,
const MCInstrDesc &Desc, int DeltaCycles) const {
const unsigned SchedClass =
TII->getSchedClass(Desc, MI.operands(), MI.getMF()->getRegInfo());
const MemoryBankBits MemoryBanks = getMemoryBanks(&MI);
Expand All @@ -461,18 +496,41 @@ bool AIEHazardRecognizer::checkConflict(
MemoryBanks, TII->getMemoryCycles(SchedClass), DeltaCycles, std::nullopt);
}

bool AIEHazardRecognizer::checkConflict(
ConflictTypeBits AIEHazardRecognizer::checkConflict(
const ResourceScoreboard<FuncUnitWrapper> &Scoreboard,
const InstrItineraryData *ItinData, unsigned SchedClass, SlotBits SlotSet,
MemoryBankBits MemoryBanks, SmallVector<int, 2> MemoryAccessCycles,
int DeltaCycles, std::optional<int> FUDepthLimit) {
assert(Scoreboard.isValidDelta(DeltaCycles));
ConflictTypeBits Conflict = static_cast<uint32_t>(ConflictType::NoConflict);

Conflict |= checkFormatConflict(Scoreboard, DeltaCycles, SlotSet);

Conflict |= checkMemoryBankConflict(MemoryAccessCycles, Scoreboard,
DeltaCycles, MemoryBanks);

Conflict |= checkFUConflict(ItinData, SchedClass, DeltaCycles, Scoreboard,
FUDepthLimit);

return Conflict;
}

// Return true if there is a conflict due to format.
ConflictTypeBits AIEHazardRecognizer::checkFormatConflict(
const ResourceScoreboard<FuncUnitWrapper> &Scoreboard, int DeltaCycles,
unsigned SlotSet) {
// Verify format hazards
FuncUnitWrapper EmissionCycle(/*Req=*/0, /*Res=*/0, SlotSet);
if (EmissionCycle.conflict(Scoreboard[DeltaCycles]))
return true;
return static_cast<uint32_t>(EmissionCycle.conflict(Scoreboard[DeltaCycles])
? ConflictType::Format
: ConflictType::NoConflict);
}

// Return true if there is a conflict due to memory banks.
ConflictTypeBits AIEHazardRecognizer::checkMemoryBankConflict(
const SmallVector<int, 2> &MemoryAccessCycles,
const ResourceScoreboard<FuncUnitWrapper> &Scoreboard, int DeltaCycles,
unsigned MemoryBanks) {
// Verify memory bank hazards
if (!MemoryAccessCycles.empty()) {
FuncUnitWrapper MemoryBankAccessCycle(/*Req=*/0, /*Res=*/0, /*SlotSet=*/0,
Expand All @@ -484,39 +542,41 @@ bool AIEHazardRecognizer::checkConflict(
LLVM_DEBUG(dbgs() << "*** Memory bank conflict in cycle=" << AccessCycle
<< ":\n";
MemoryBankAccessCycle.dump(); dbgs() << "\n");
return true;
return static_cast<uint32_t>(ConflictType::MemoryBank);
}
}
}
return static_cast<uint32_t>(ConflictType::NoConflict);
}

// Return true if there is a conflict in the functional units.
ConflictTypeBits AIEHazardRecognizer::checkFUConflict(
const InstrItineraryData *ItinData, unsigned SchedClass, int DeltaCycles,
const ResourceScoreboard<FuncUnitWrapper> &Scoreboard,
const std::optional<int> &FUDepthLimit) {

// Note that Delta will be negative for bottom-up scheduling.
// Cycle is 'our' cycle at which each stage of the itinerary starts.
// It gets updated by the increment from the InstrStage.
int Cycle = DeltaCycles;
for (const InstrStage &IS : ItinData->getStages(SchedClass)) {
if (FUDepthLimit && (Cycle - DeltaCycles) >= *FUDepthLimit) {
if (FUDepthLimit && (Cycle - DeltaCycles) >= *FUDepthLimit)
break;
}
// Check availability of this stage's resources for the specified number
// of cycles
const FuncUnitWrapper ThisCycle(IS);
for (unsigned int C = 0; C < IS.getCycles(); ++C) {
int StageCycle = Cycle + (int)C;
assert(StageCycle < Scoreboard.getDepth());

if (ThisCycle.conflict(Scoreboard[StageCycle])) {
LLVM_DEBUG(dbgs() << "*** Hazard in cycle=" << StageCycle
<< " EC=" << StageCycle - DeltaCycles << ":\n";
ThisCycle.dump(); dbgs() << "\n");
return true;
}
if (ThisCycle.conflict(Scoreboard[StageCycle]))
return static_cast<uint32_t>(ConflictType::FuncUnit);
}

// Advance the cycle to the next stage.
Cycle += IS.getNextCycles();
}

return false;
return static_cast<uint32_t>(ConflictType::NoConflict);
}

void AIEHazardRecognizer::emitInScoreboard(
Expand All @@ -538,6 +598,26 @@ void AIEHazardRecognizer::emitInScoreboard(
TII->getMemoryCycles(SchedClass), DeltaCycles, FUDepthLimit);
}

void AIEHazardRecognizer::releaseFromScoreboard(
const MCInstrDesc &Desc, MemoryBankBits MemoryBanks,
iterator_range<const MachineOperand *> MIOperands,
const MachineRegisterInfo &MRI, int DeltaCycles) {
releaseFromScoreboard(Scoreboard, Desc, MemoryBanks, MIOperands, MRI,
DeltaCycles);
}

void AIEHazardRecognizer::releaseFromScoreboard(
ResourceScoreboard<FuncUnitWrapper> &TheScoreboard, const MCInstrDesc &Desc,
MemoryBankBits MemoryBanks,
iterator_range<const MachineOperand *> MIOperands,
const MachineRegisterInfo &MRI, int DeltaCycles) const {
const unsigned SchedClass = TII->getSchedClass(Desc, MIOperands, MRI);
const SlotBits SlotSet =
getSlotSet(Desc, *TII->getFormatInterface(), IgnoreUnknownSlotSets);
releaseResources(TheScoreboard, ItinData, SchedClass, SlotSet, MemoryBanks,
TII->getMemoryCycles(SchedClass), DeltaCycles, FUDepthLimit);
}

void AIEHazardRecognizer::enterResources(
ResourceScoreboard<FuncUnitWrapper> &Scoreboard,
const InstrItineraryData *ItinData, unsigned SchedClass, SlotBits SlotSet,
Expand Down Expand Up @@ -579,6 +659,47 @@ void AIEHazardRecognizer::enterResources(
});
}

void AIEHazardRecognizer::releaseResources(
ResourceScoreboard<FuncUnitWrapper> &Scoreboard,
const InstrItineraryData *ItinData, unsigned SchedClass, SlotBits SlotSet,
MemoryBankBits MemoryBanks, SmallVector<int, 2> MemoryAccessCycles,
int DeltaCycles, std::optional<int> FUDepthLimit) {
assert(Scoreboard.isValidDelta(DeltaCycles));

// Remove slot usage
FuncUnitWrapper EmissionCycle(/*Req=*/0, /*Res=*/0, SlotSet);
Scoreboard[DeltaCycles] ^= EmissionCycle;

// Remove memory bank usage
if (!MemoryAccessCycles.empty()) {
FuncUnitWrapper MemoryBankAccessCycle(/*Req=*/0, /*Res=*/0, /*SlotSet=*/0,
MemoryBanks);
for (int Cycles : MemoryAccessCycles) {
Scoreboard[DeltaCycles + Cycles - 1] ^= MemoryBankAccessCycle;
}
}

int Cycle = DeltaCycles;
Scoreboard[Cycle].IssueCount--;
for (const InstrStage &IS : ItinData->getStages(SchedClass)) {
if (FUDepthLimit && (Cycle - DeltaCycles) >= *FUDepthLimit) {
break;
}
const FuncUnitWrapper ResourceToRelease(IS);
for (unsigned int C = 0; C < IS.getCycles(); ++C) {
Scoreboard[Cycle + C] ^= ResourceToRelease;
}

// Advance the cycle to the next stage.
Cycle += IS.getNextCycles();
}

LLVM_DEBUG({
dbgs() << "Scoreboard after release resources:\n";
Scoreboard.dump();
});
}

unsigned AIEHazardRecognizer::getPipelineDepth() const { return PipelineDepth; }

unsigned AIEHazardRecognizer::getMaxLatency() const { return MaxLatency; }
Expand Down
64 changes: 61 additions & 3 deletions llvm/lib/Target/AIE/AIEHazardRecognizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
namespace llvm {

class MachineInstr;
using ConflictTypeBits = std::uint32_t;

void applyFormatOrdering(AIE::MachineBundle &Bundle, const VLIWFormat &Format,
MachineInstr *BundleRoot,
Expand Down Expand Up @@ -85,6 +86,7 @@ class FuncUnitWrapper {
void dump() const;

FuncUnitWrapper &operator|=(const FuncUnitWrapper &Other);
FuncUnitWrapper &operator^=(const FuncUnitWrapper &Other);
bool conflict(const FuncUnitWrapper &Other) const;
};

Expand All @@ -99,6 +101,13 @@ class AIEHazardRecognizer : public ScheduleHazardRecognizer {
void computeMaxLatency();

public:
enum class ConflictType : std::uint32_t {
NoConflict = 0b000,
Format = 0b001,
MemoryBank = 0b010,
FuncUnit = 0b100,
};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: To avoid the constant static_cast, I think we could create e.g.

class ConflictTypeSet {
uint32_t BitSet = 0b0;
static uint32_t  asBit(ConflictType CT) { return uint32_t(CT); }
public:
  bool has(ConflictType CT) const { return BitSet & asBit(CT); }
  bool any() const { return BitSet != 0b0; }
  void set(ConflictType CT) { BitSet |= asBit(CT); }
};


/// ScoreboardDepth can be used to speficy a fixed depth without querying the
/// scheduling model. This is mostly used for testing, for other cases we
/// should trust the instruction itineraries.
Expand Down Expand Up @@ -149,6 +158,18 @@ class AIEHazardRecognizer : public ScheduleHazardRecognizer {
iterator_range<const MachineOperand *> MIOperands,
const MachineRegisterInfo &MRI, int DeltaCycles);

void releaseFromScoreboard(ResourceScoreboard<FuncUnitWrapper> &Scoreboard,
const MCInstrDesc &Desc,
MemoryBankBits MemoryBanks,
iterator_range<const MachineOperand *> MIOperands,
const MachineRegisterInfo &MRI,
int DeltaCycles) const;
// Apply the above function to the local scoreboard.
void releaseFromScoreboard(const MCInstrDesc &Desc,
MemoryBankBits MemoryBanks,
iterator_range<const MachineOperand *> MIOperands,
const MachineRegisterInfo &MRI, int DeltaCycles);

/// Block all scoreboard resources at DeltaCycles
void blockCycleInScoreboard(int DeltaCycle);

Expand Down Expand Up @@ -189,26 +210,63 @@ class AIEHazardRecognizer : public ScheduleHazardRecognizer {
const MCInstrDesc &Desc, MemoryBankBits MemoryBanks,
iterator_range<const MachineOperand *> MIOperands,
const MachineRegisterInfo &MRI, int DeltaCycles) const;
bool checkConflict(const ResourceScoreboard<FuncUnitWrapper> &Scoreboard,
MachineInstr &MI, int DeltaCycles) const;
ScheduleHazardRecognizer::HazardType
getHazardType(const MCInstrDesc &Desc, MemoryBankBits MemoryBanks,
iterator_range<const MachineOperand *> MIOperands,
const MachineRegisterInfo &MRI, int DeltaCycles);

ConflictTypeBits
checkConflict(const ResourceScoreboard<FuncUnitWrapper> &Scoreboard,
MachineInstr &MI, const MCInstrDesc &Desc,
int DeltaCycles) const;
ConflictTypeBits checkConflict(MachineInstr &MI, const MCInstrDesc &Desc,
int DeltaCycles);

ConflictTypeBits
checkConflict(const ResourceScoreboard<FuncUnitWrapper> &Scoreboard,
MachineInstr &MI, int DeltaCycles) const;
ConflictTypeBits checkConflict(MachineInstr &MI, int DeltaCycles);

protected:
ScheduleHazardRecognizer::HazardType getHazardType(const MCInstrDesc &Desc,
int DeltaCycles);
static bool
static ConflictTypeBits
checkConflict(const ResourceScoreboard<FuncUnitWrapper> &Scoreboard,
const InstrItineraryData *ItinData, unsigned SchedClass,
SlotBits SlotSet, MemoryBankBits MemoryBanks,
SmallVector<int, 2> MemoryAccessCycles, int DeltaCycles,
std::optional<int> FUDepthLimit);

static ConflictTypeBits
checkFormatConflict(const ResourceScoreboard<FuncUnitWrapper> &Scoreboard,
int DeltaCycles, unsigned SlotSet);

static ConflictTypeBits
checkMemoryBankConflict(const SmallVector<int, 2> &MemoryAccessCycles,
const ResourceScoreboard<FuncUnitWrapper> &Scoreboard,
int DeltaCycles, unsigned MemoryBanks);

static ConflictTypeBits
checkFUConflict(const InstrItineraryData *ItinData, unsigned SchedClass,
int DeltaCycles,
const ResourceScoreboard<FuncUnitWrapper> &Scoreboard,
const std::optional<int> &FUDepthLimit);

static void enterResources(ResourceScoreboard<FuncUnitWrapper> &Scoreboard,
const InstrItineraryData *ItinData,
unsigned SchedClass, SlotBits SlotSet,
MemoryBankBits MemoryBanks,
SmallVector<int, 2> MemoryAccessCycles,
int DeltaCycles, std::optional<int> FUDepthLimit);

static void releaseResources(ResourceScoreboard<FuncUnitWrapper> &Scoreboard,
const InstrItineraryData *ItinData,
unsigned SchedClass, SlotBits SlotSet,
MemoryBankBits MemoryBanks,
SmallVector<int, 2> MemoryAccessCycles,
int DeltaCycles,
std::optional<int> FUDepthLimit);

private:
ResourceScoreboard<FuncUnitWrapper> Scoreboard;
const AIEBaseInstrInfo *TII;
Expand Down
Loading
Loading