Skip to content

Commit

Permalink
[llvm-mca] Account for AcquireAtCycles in llvm-mca
Browse files Browse the repository at this point in the history
In the past, there was a variable named ResourceCycles which modeled how
long a resource was consumed. Then a variable named AcquireAtCycles was
added and allowed the scheduler to specify how many cycles after "cycle
0" the resource was acquired. It was consumed from that AcquireAtCycle
until the cycle ResourceCycles - AcquireAtCycle.

We decided ResourceCycles should be renamed ReleaseAtCycle, and the
number of cycles a resource was consumed is ReleaseAtCycle -
AcquireAtCycle. This renaming happened globally, but only the scheduler
was updated to account for AcquireAtCycle.

This patch accounts for the total number of cycles to be calculated as
ReleaseAtCycle - AcquireAtCycle in llvm-mca. This patch renames the
class ReleaseAtCycles to NumCyclesUsed since that better describes what that
class now models.
  • Loading branch information
michaelmaitland committed Feb 5, 2024
1 parent dea855d commit c7a33f6
Show file tree
Hide file tree
Showing 37 changed files with 944 additions and 944 deletions.
2 changes: 1 addition & 1 deletion llvm/include/llvm/MCA/HWEventListener.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class HWInstructionEvent {
// ResourceRef::second is a bitmask of the referenced sub-unit of the resource.
using ResourceRef = std::pair<uint64_t, uint64_t>;

using ResourceUse = std::pair<ResourceRef, ReleaseAtCycles>;
using ResourceUse = std::pair<ResourceRef, NumCyclesUsed>;

class HWInstructionIssuedEvent : public HWInstructionEvent {
public:
Expand Down
2 changes: 1 addition & 1 deletion llvm/include/llvm/MCA/HardwareUnits/ResourceManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ class ResourceManager {

void issueInstruction(
const InstrDesc &Desc,
SmallVectorImpl<std::pair<ResourceRef, ReleaseAtCycles>> &Pipes);
SmallVectorImpl<std::pair<ResourceRef, NumCyclesUsed>> &Pipes);

void cycleEvent(SmallVectorImpl<ResourceRef> &ResourcesFreed);

Expand Down
4 changes: 2 additions & 2 deletions llvm/include/llvm/MCA/HardwareUnits/Scheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class Scheduler : public HardwareUnit {
/// Issue an instruction without updating the ready queue.
void issueInstructionImpl(
InstRef &IR,
SmallVectorImpl<std::pair<ResourceRef, ReleaseAtCycles>> &Pipes);
SmallVectorImpl<std::pair<ResourceRef, NumCyclesUsed>> &Pipes);

// Identify instructions that have finished executing, and remove them from
// the IssuedSet. References to executed instructions are added to input
Expand Down Expand Up @@ -202,7 +202,7 @@ class Scheduler : public HardwareUnit {
/// result of this event.
void issueInstruction(
InstRef &IR,
SmallVectorImpl<std::pair<ResourceRef, ReleaseAtCycles>> &Used,
SmallVectorImpl<std::pair<ResourceRef, NumCyclesUsed>> &Used,
SmallVectorImpl<InstRef> &Pending,
SmallVectorImpl<InstRef> &Ready);

Expand Down
10 changes: 5 additions & 5 deletions llvm/include/llvm/MCA/Support.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ template <typename T> char InstructionError<T>::ID;
/// number of resources, are kept separate. This is used by the
/// ResourcePressureView to calculate the average resource cycles
/// per instruction/iteration.
class ReleaseAtCycles {
class NumCyclesUsed {
unsigned Numerator, Denominator;

public:
ReleaseAtCycles() : Numerator(0), Denominator(1) {}
ReleaseAtCycles(unsigned Cycles, unsigned ResourceUnits = 1)
NumCyclesUsed() : Numerator(0), Denominator(1) {}
NumCyclesUsed(unsigned Cycles, unsigned ResourceUnits = 1)
: Numerator(Cycles), Denominator(ResourceUnits) {}

operator double() const {
Expand All @@ -67,7 +67,7 @@ class ReleaseAtCycles {
// Add the components of RHS to this instance. Instead of calculating
// the final value here, we keep track of the numerator and denominator
// separately, to reduce floating point error.
ReleaseAtCycles &operator+=(const ReleaseAtCycles &RHS);
NumCyclesUsed &operator+=(const NumCyclesUsed &RHS);
};

/// Populates vector Masks with processor resource masks.
Expand Down Expand Up @@ -105,7 +105,7 @@ inline unsigned getResourceStateIndex(uint64_t Mask) {
/// Compute the reciprocal block throughput from a set of processor resource
/// cycles. The reciprocal block throughput is computed as the MAX between:
/// - NumMicroOps / DispatchWidth
/// - ProcReleaseAtCycles / #ProcResourceUnits (for every consumed resource).
/// - ProcNumCyclesUsed / #ProcResourceUnits (for every consumed resource).
double computeBlockRThroughput(const MCSchedModel &SM, unsigned DispatchWidth,
unsigned NumMicroOps,
ArrayRef<unsigned> ProcResourceUsage);
Expand Down
8 changes: 4 additions & 4 deletions llvm/lib/MCA/HardwareUnits/ResourceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,21 +346,21 @@ uint64_t ResourceManager::checkAvailability(const InstrDesc &Desc) const {

void ResourceManager::issueInstruction(
const InstrDesc &Desc,
SmallVectorImpl<std::pair<ResourceRef, ReleaseAtCycles>> &Pipes) {
SmallVectorImpl<std::pair<ResourceRef, NumCyclesUsed>> &Pipes) {
for (const std::pair<uint64_t, ResourceUsage> &R : Desc.Resources) {
const CycleSegment &CS = R.second.CS;
if (!CS.size()) {
releaseResource(R.first);
continue;
}

assert(CS.begin() == 0 && "Invalid {Start, End} cycles!");
assert(CS.isValid() && "Invalid {Start, End} cycles!");
if (!R.second.isReserved()) {
ResourceRef Pipe = selectPipe(R.first);
use(Pipe);
BusyResources[Pipe] += CS.size();
Pipes.emplace_back(std::pair<ResourceRef, ReleaseAtCycles>(
Pipe, ReleaseAtCycles(CS.size())));
Pipes.emplace_back(std::pair<ResourceRef, NumCyclesUsed>(
Pipe, NumCyclesUsed(CS.size())));
} else {
assert((llvm::popcount(R.first) > 1) && "Expected a group!");
// Mark this group as reserved.
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/MCA/HardwareUnits/Scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ Scheduler::Status Scheduler::isAvailable(const InstRef &IR) {

void Scheduler::issueInstructionImpl(
InstRef &IR,
SmallVectorImpl<std::pair<ResourceRef, ReleaseAtCycles>> &UsedResources) {
SmallVectorImpl<std::pair<ResourceRef, NumCyclesUsed>> &UsedResources) {
Instruction *IS = IR.getInstruction();
const InstrDesc &D = IS->getDesc();

Expand Down Expand Up @@ -98,7 +98,7 @@ void Scheduler::issueInstructionImpl(
// Release the buffered resources and issue the instruction.
void Scheduler::issueInstruction(
InstRef &IR,
SmallVectorImpl<std::pair<ResourceRef, ReleaseAtCycles>> &UsedResources,
SmallVectorImpl<std::pair<ResourceRef, NumCyclesUsed>> &UsedResources,
SmallVectorImpl<InstRef> &PendingInstructions,
SmallVectorImpl<InstRef> &ReadyInstructions) {
const Instruction &Inst = *IR.getInstruction();
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/MCA/InstrBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ static void initializeUsedResources(InstrDesc &ID,
AllInOrderResources &= (PR.BufferSize <= 1);
}

CycleSegment RCy(0, PRE->ReleaseAtCycle, false);
CycleSegment RCy(PRE->AcquireAtCycle, PRE->ReleaseAtCycle, false);
Worklist.emplace_back(ResourcePlusCycles(Mask, ResourceUsage(RCy)));
if (PR.SuperIdx) {
uint64_t Super = ProcResourceMasks[PR.SuperIdx];
SuperResources[Super] += PRE->ReleaseAtCycle;
SuperResources[Super] += PRE->ReleaseAtCycle - PRE->AcquireAtCycle;
}
}

Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/MCA/Stages/InstructionTables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Error InstructionTables::execute(InstRef &IR) {
for (unsigned I = 0, E = NumUnits; I < E; ++I) {
ResourceRef ResourceUnit = std::make_pair(Index, 1U << I);
UsedResources.emplace_back(
std::make_pair(ResourceUnit, ReleaseAtCycles(Cycles, NumUnits)));
std::make_pair(ResourceUnit, NumCyclesUsed(Cycles, NumUnits)));
}
continue;
}
Expand All @@ -54,7 +54,7 @@ Error InstructionTables::execute(InstRef &IR) {
ResourceRef ResourceUnit = std::make_pair(SubUnitIdx, 1U << I2);
UsedResources.emplace_back(std::make_pair(
ResourceUnit,
ReleaseAtCycles(Cycles, NumUnits * SubUnit.NumUnits)));
NumCyclesUsed(Cycles, NumUnits * SubUnit.NumUnits)));
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions llvm/lib/MCA/Support.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace mca {

#define DEBUG_TYPE "llvm-mca"

ReleaseAtCycles &ReleaseAtCycles::operator+=(const ReleaseAtCycles &RHS) {
NumCyclesUsed &NumCyclesUsed::operator+=(const NumCyclesUsed &RHS) {
if (Denominator == RHS.Denominator)
Numerator += RHS.Numerator;
else {
Expand Down Expand Up @@ -92,18 +92,18 @@ double computeBlockRThroughput(const MCSchedModel &SM, unsigned DispatchWidth,
// The number of available resource units affects the resource pressure
// distribution, as well as how many blocks can be executed every cycle.
for (unsigned I = 0, E = SM.getNumProcResourceKinds(); I < E; ++I) {
unsigned ReleaseAtCycles = ProcResourceUsage[I];
if (!ReleaseAtCycles)
unsigned NumCyclesUsed = ProcResourceUsage[I];
if (!NumCyclesUsed)
continue;

const MCProcResourceDesc &MCDesc = *SM.getProcResource(I);
double Throughput = static_cast<double>(ReleaseAtCycles) / MCDesc.NumUnits;
double Throughput = static_cast<double>(NumCyclesUsed) / MCDesc.NumUnits;
Max = std::max(Max, Throughput);
}

// The block reciprocal throughput is computed as the MAX of:
// - (NumMicroOps / DispatchWidth)
// - (NumUnits / ReleaseAtCycles) for every consumed processor resource.
// - (NumUnits / NumCyclesUsed) for every consumed processor resource.
return Max;
}

Expand Down
Loading

0 comments on commit c7a33f6

Please sign in to comment.