Skip to content
Merged
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
73 changes: 55 additions & 18 deletions llvm/tools/llvm-mca/Views/ResourcePressureView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,16 @@ ResourcePressureView::ResourcePressureView(const llvm::MCSubtargetInfo &sti,
}

NumResourceUnits = R2VIndex;
ResourceUsage.resize(NumResourceUnits * (getSource().size() + 1));
std::fill(ResourceUsage.begin(), ResourceUsage.end(), 0.0);
ResourceUsage.resize(getSource().size());

ResourceReleaseAtCycles InitValue{0, 0};
auto Generator = [&InitValue]() {
ResourceReleaseAtCycles Old = InitValue;
++InitValue.ResourceIdx;
return Old;
};
std::generate_n(std::back_inserter(CommonResourceUsage), NumResourceUnits,
Generator);
}

void ResourcePressureView::onEvent(const HWInstructionEvent &Event) {
Expand All @@ -60,8 +68,19 @@ void ResourcePressureView::onEvent(const HWInstructionEvent &Event) {
assert(Resource2VecIndex.contains(RR.first));
unsigned R2VIndex = Resource2VecIndex[RR.first];
R2VIndex += llvm::countr_zero(RR.second);
ResourceUsage[R2VIndex + NumResourceUnits * SourceIdx] += Use.second;
ResourceUsage[R2VIndex + NumResourceUnits * Source.size()] += Use.second;

InstResourceUsage &RU = ResourceUsage[SourceIdx];
ResourceReleaseAtCycles NewUsage{R2VIndex, Use.second};
auto ResCyclesIt =
lower_bound(RU, NewUsage, [](const auto &L, const auto &R) {
return L.ResourceIdx < R.ResourceIdx;
});
if (ResCyclesIt != RU.end() && ResCyclesIt->ResourceIdx == R2VIndex)
ResCyclesIt->Cycles += NewUsage.Cycles;
else
RU.insert(ResCyclesIt, std::move(NewUsage));

CommonResourceUsage[R2VIndex].Cycles += NewUsage.Cycles;
}
}

Expand Down Expand Up @@ -135,10 +154,17 @@ void ResourcePressureView::printResourcePressurePerIter(raw_ostream &OS) const {

ArrayRef<llvm::MCInst> Source = getSource();
const unsigned Executions = LastInstructionIdx / Source.size() + 1;
auto UsageEntryEnd = CommonResourceUsage.end();
auto UsageEntryIt = CommonResourceUsage.begin();
for (unsigned I = 0, E = NumResourceUnits; I < E; ++I) {
double Usage = ResourceUsage[I + Source.size() * E];
printResourcePressure(FOS, Usage / Executions, (I + 1) * 7);
double Pressure = 0.0;
if (UsageEntryIt != UsageEntryEnd && UsageEntryIt->ResourceIdx == I) {
Pressure = UsageEntryIt->Cycles / Executions;
++UsageEntryIt;
}
printResourcePressure(FOS, Pressure, (I + 1) * 7);
}
assert(UsageEntryIt == UsageEntryEnd);

FOS.flush();
OS << Buffer;
Expand All @@ -157,11 +183,17 @@ void ResourcePressureView::printResourcePressurePerInst(raw_ostream &OS) const {
ArrayRef<llvm::MCInst> Source = getSource();
const unsigned Executions = LastInstructionIdx / Source.size() + 1;
for (const MCInst &MCI : Source) {
unsigned BaseEltIdx = InstrIndex * NumResourceUnits;
auto UsageEntryEnd = ResourceUsage[InstrIndex].end();
auto UsageEntryIt = ResourceUsage[InstrIndex].begin();
for (unsigned J = 0; J < NumResourceUnits; ++J) {
double Usage = ResourceUsage[J + BaseEltIdx];
printResourcePressure(FOS, Usage / Executions, (J + 1) * 7);
double Pressure = 0.0;
if (UsageEntryIt != UsageEntryEnd && UsageEntryIt->ResourceIdx == J) {
Pressure = UsageEntryIt->Cycles / Executions;
++UsageEntryIt;
}
printResourcePressure(FOS, Pressure, (J + 1) * 7);
}
assert(UsageEntryIt == UsageEntryEnd);

FOS << printInstructionString(MCI) << '\n';
FOS.flush();
Expand All @@ -180,17 +212,22 @@ json::Value ResourcePressureView::toJSON() const {
// non-zero values.
ArrayRef<llvm::MCInst> Source = getSource();
const unsigned Executions = LastInstructionIdx / Source.size() + 1;
for (const auto &R : enumerate(ResourceUsage)) {
const ReleaseAtCycles &RU = R.value();
if (RU.getNumerator() == 0)
continue;
unsigned InstructionIndex = R.index() / NumResourceUnits;
unsigned ResourceIndex = R.index() % NumResourceUnits;
double Usage = RU / Executions;

auto AddToJSON = [&ResourcePressureInfo, Executions](
const ResourceReleaseAtCycles &RU, unsigned InstIndex) {
assert(RU.Cycles.getNumerator() != 0);
double Usage = RU.Cycles / Executions;
ResourcePressureInfo.push_back(
json::Object({{"InstructionIndex", InstructionIndex},
{"ResourceIndex", ResourceIndex},
json::Object({{"InstructionIndex", InstIndex},
{"ResourceIndex", RU.ResourceIdx},
{"ResourceUsage", Usage}}));
};
for (const auto &[InstIndex, Usages] : enumerate(ResourceUsage))
for (const auto &RU : Usages)
AddToJSON(RU, InstIndex);
for (const auto &RU : CommonResourceUsage) {
if (RU.Cycles.getNumerator() != 0)
AddToJSON(RU, Source.size());
}

json::Object JO({{"ResourcePressureInfo", std::move(ResourcePressureInfo)}});
Expand Down
9 changes: 7 additions & 2 deletions llvm/tools/llvm-mca/Views/ResourcePressureView.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,13 @@ class ResourcePressureView : public InstructionView {
// resource ID.
llvm::DenseMap<unsigned, unsigned> Resource2VecIndex;

// Table of resources used by instructions.
std::vector<ReleaseAtCycles> ResourceUsage;
struct ResourceReleaseAtCycles {
unsigned ResourceIdx;
ReleaseAtCycles Cycles;
};
using InstResourceUsage = std::vector<ResourceReleaseAtCycles>;
std::vector<InstResourceUsage> ResourceUsage;
InstResourceUsage CommonResourceUsage;
unsigned NumResourceUnits;

void printResourcePressurePerIter(llvm::raw_ostream &OS) const;
Expand Down