2018-03-08 14:05:02 +01:00
|
|
|
//===--------------------- ResourcePressureView.cpp -------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-03-08 14:05:02 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// \file
|
|
|
|
///
|
|
|
|
/// This file implements methods in the ResourcePressureView interface.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-08-24 22:24:53 +02:00
|
|
|
#include "Views/ResourcePressureView.h"
|
2018-05-15 20:11:45 +02:00
|
|
|
#include "llvm/Support/FormattedStream.h"
|
2018-03-08 14:05:02 +01:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
2018-10-30 16:56:08 +01:00
|
|
|
namespace llvm {
|
2018-03-08 14:05:02 +01:00
|
|
|
namespace mca {
|
|
|
|
|
2018-10-25 13:51:34 +02:00
|
|
|
ResourcePressureView::ResourcePressureView(const llvm::MCSubtargetInfo &sti,
|
2018-10-26 12:48:04 +02:00
|
|
|
MCInstPrinter &Printer,
|
|
|
|
ArrayRef<MCInst> S)
|
2020-08-22 00:52:02 +02:00
|
|
|
: InstructionView(sti, Printer, S), LastInstructionIdx(0) {
|
2018-03-08 14:05:02 +01:00
|
|
|
// Populate the map of resource descriptors.
|
|
|
|
unsigned R2VIndex = 0;
|
2020-08-22 00:52:02 +02:00
|
|
|
const MCSchedModel &SM = getSubTargetInfo().getSchedModel();
|
2018-03-08 14:05:02 +01:00
|
|
|
for (unsigned I = 0, E = SM.getNumProcResourceKinds(); I < E; ++I) {
|
|
|
|
const MCProcResourceDesc &ProcResource = *SM.getProcResource(I);
|
|
|
|
unsigned NumUnits = ProcResource.NumUnits;
|
|
|
|
// Skip groups and invalid resources with zero units.
|
|
|
|
if (ProcResource.SubUnitsIdxBegin || !NumUnits)
|
|
|
|
continue;
|
|
|
|
|
2018-03-22 11:19:20 +01:00
|
|
|
Resource2VecIndex.insert(std::pair<unsigned, unsigned>(I, R2VIndex));
|
2018-03-08 14:05:02 +01:00
|
|
|
R2VIndex += ProcResource.NumUnits;
|
|
|
|
}
|
|
|
|
|
|
|
|
NumResourceUnits = R2VIndex;
|
2020-08-22 00:52:02 +02:00
|
|
|
ResourceUsage.resize(NumResourceUnits * (getSource().size() + 1));
|
2018-03-23 18:53:02 +01:00
|
|
|
std::fill(ResourceUsage.begin(), ResourceUsage.end(), 0.0);
|
2018-03-08 14:05:02 +01:00
|
|
|
}
|
|
|
|
|
2018-07-12 18:56:17 +02:00
|
|
|
void ResourcePressureView::onEvent(const HWInstructionEvent &Event) {
|
2018-10-26 12:48:04 +02:00
|
|
|
if (Event.Type == HWInstructionEvent::Dispatched) {
|
|
|
|
LastInstructionIdx = Event.IR.getSourceIndex();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-13 14:11:01 +01:00
|
|
|
// We're only interested in Issue events.
|
|
|
|
if (Event.Type != HWInstructionEvent::Issued)
|
|
|
|
return;
|
2018-10-26 12:48:04 +02:00
|
|
|
|
2018-03-13 14:11:01 +01:00
|
|
|
const auto &IssueEvent = static_cast<const HWInstructionIssuedEvent &>(Event);
|
2020-08-22 00:52:02 +02:00
|
|
|
ArrayRef<llvm::MCInst> Source = getSource();
|
2018-05-07 20:29:15 +02:00
|
|
|
const unsigned SourceIdx = Event.IR.getSourceIndex() % Source.size();
|
[llvm-mca] Delay calculation of Cycles per Resources, separate the cycles and resource quantities.
Summary:
This patch removes the storing of accumulated floating point data
within the llvm-mca library.
This patch splits-up the two quantities: cycles and number of resource units.
By splitting-up these two quantities, we delay the calculation of "cycles per resource unit"
until that value is read, reducing the chance of accumulating floating point error.
I considered using the APFloat, but after measuring performance, for a large (many iteration)
sample, I decided to go with this faster solution.
Reviewers: andreadb, courbet, RKSimon
Reviewed By: andreadb
Subscribers: llvm-commits, javed.absar, tschuett, gbedwell
Differential Revision: https://reviews.llvm.org/D51903
llvm-svn: 341980
2018-09-11 20:47:48 +02:00
|
|
|
for (const std::pair<ResourceRef, ResourceCycles> &Use :
|
|
|
|
IssueEvent.UsedResources) {
|
2018-03-08 14:05:02 +01:00
|
|
|
const ResourceRef &RR = Use.first;
|
|
|
|
assert(Resource2VecIndex.find(RR.first) != Resource2VecIndex.end());
|
|
|
|
unsigned R2VIndex = Resource2VecIndex[RR.first];
|
|
|
|
R2VIndex += countTrailingZeros(RR.second);
|
|
|
|
ResourceUsage[R2VIndex + NumResourceUnits * SourceIdx] += Use.second;
|
|
|
|
ResourceUsage[R2VIndex + NumResourceUnits * Source.size()] += Use.second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-15 20:11:45 +02:00
|
|
|
static void printColumnNames(formatted_raw_ostream &OS,
|
|
|
|
const MCSchedModel &SM) {
|
|
|
|
unsigned Column = OS.getColumn();
|
2018-03-08 14:05:02 +01:00
|
|
|
for (unsigned I = 1, ResourceIndex = 0, E = SM.getNumProcResourceKinds();
|
|
|
|
I < E; ++I) {
|
|
|
|
const MCProcResourceDesc &ProcResource = *SM.getProcResource(I);
|
|
|
|
unsigned NumUnits = ProcResource.NumUnits;
|
|
|
|
// Skip groups and invalid resources with zero units.
|
|
|
|
if (ProcResource.SubUnitsIdxBegin || !NumUnits)
|
|
|
|
continue;
|
|
|
|
|
2018-05-15 20:11:45 +02:00
|
|
|
for (unsigned J = 0; J < NumUnits; ++J) {
|
|
|
|
Column += 7;
|
|
|
|
OS << "[" << ResourceIndex;
|
|
|
|
if (NumUnits > 1)
|
|
|
|
OS << '.' << J;
|
|
|
|
OS << ']';
|
|
|
|
OS.PadToColumn(Column);
|
2018-03-08 14:05:02 +01:00
|
|
|
}
|
2018-05-15 20:11:45 +02:00
|
|
|
|
2018-03-08 14:05:02 +01:00
|
|
|
ResourceIndex++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-15 20:11:45 +02:00
|
|
|
static void printResourcePressure(formatted_raw_ostream &OS, double Pressure,
|
|
|
|
unsigned Col) {
|
2018-05-05 14:21:54 +02:00
|
|
|
if (!Pressure || Pressure < 0.005) {
|
2018-05-15 20:11:45 +02:00
|
|
|
OS << " - ";
|
|
|
|
} else {
|
|
|
|
// Round to the value to the nearest hundredth and then print it.
|
|
|
|
OS << format("%.2f", floor((Pressure * 100) + 0.5) / 100);
|
2018-04-10 17:14:15 +02:00
|
|
|
}
|
2018-05-15 20:11:45 +02:00
|
|
|
OS.PadToColumn(Col);
|
2018-04-10 17:14:15 +02:00
|
|
|
}
|
|
|
|
|
2018-10-25 13:51:34 +02:00
|
|
|
void ResourcePressureView::printResourcePressurePerIter(raw_ostream &OS) const {
|
2018-03-08 14:05:02 +01:00
|
|
|
std::string Buffer;
|
|
|
|
raw_string_ostream TempStream(Buffer);
|
2018-05-15 20:11:45 +02:00
|
|
|
formatted_raw_ostream FOS(TempStream);
|
2018-03-08 14:05:02 +01:00
|
|
|
|
2018-05-15 20:11:45 +02:00
|
|
|
FOS << "\n\nResources:\n";
|
2020-08-22 00:52:02 +02:00
|
|
|
const MCSchedModel &SM = getSubTargetInfo().getSchedModel();
|
2018-03-08 14:05:02 +01:00
|
|
|
for (unsigned I = 1, ResourceIndex = 0, E = SM.getNumProcResourceKinds();
|
|
|
|
I < E; ++I) {
|
|
|
|
const MCProcResourceDesc &ProcResource = *SM.getProcResource(I);
|
|
|
|
unsigned NumUnits = ProcResource.NumUnits;
|
|
|
|
// Skip groups and invalid resources with zero units.
|
|
|
|
if (ProcResource.SubUnitsIdxBegin || !NumUnits)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (unsigned J = 0; J < NumUnits; ++J) {
|
2018-05-15 20:11:45 +02:00
|
|
|
FOS << '[' << ResourceIndex;
|
2018-03-08 14:05:02 +01:00
|
|
|
if (NumUnits > 1)
|
2018-05-15 20:11:45 +02:00
|
|
|
FOS << '.' << J;
|
|
|
|
FOS << ']';
|
|
|
|
FOS.PadToColumn(6);
|
|
|
|
FOS << "- " << ProcResource.Name << '\n';
|
2018-03-08 14:05:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ResourceIndex++;
|
|
|
|
}
|
|
|
|
|
2018-05-15 20:11:45 +02:00
|
|
|
FOS << "\n\nResource pressure per iteration:\n";
|
|
|
|
FOS.flush();
|
|
|
|
printColumnNames(FOS, SM);
|
|
|
|
FOS << '\n';
|
|
|
|
FOS.flush();
|
2018-03-08 14:05:02 +01:00
|
|
|
|
2020-08-22 00:52:02 +02:00
|
|
|
ArrayRef<llvm::MCInst> Source = getSource();
|
2018-10-26 12:48:04 +02:00
|
|
|
const unsigned Executions = LastInstructionIdx / Source.size() + 1;
|
2018-03-22 11:19:20 +01:00
|
|
|
for (unsigned I = 0, E = NumResourceUnits; I < E; ++I) {
|
2018-03-23 18:53:02 +01:00
|
|
|
double Usage = ResourceUsage[I + Source.size() * E];
|
2018-05-15 20:11:45 +02:00
|
|
|
printResourcePressure(FOS, Usage / Executions, (I + 1) * 7);
|
2018-03-08 14:05:02 +01:00
|
|
|
}
|
|
|
|
|
2018-05-15 20:11:45 +02:00
|
|
|
FOS.flush();
|
2018-03-08 14:05:02 +01:00
|
|
|
OS << Buffer;
|
|
|
|
}
|
|
|
|
|
2018-10-25 13:51:34 +02:00
|
|
|
void ResourcePressureView::printResourcePressurePerInst(raw_ostream &OS) const {
|
2018-03-08 14:05:02 +01:00
|
|
|
std::string Buffer;
|
|
|
|
raw_string_ostream TempStream(Buffer);
|
2018-05-15 20:11:45 +02:00
|
|
|
formatted_raw_ostream FOS(TempStream);
|
2018-03-08 14:05:02 +01:00
|
|
|
|
2018-05-15 20:11:45 +02:00
|
|
|
FOS << "\n\nResource pressure by instruction:\n";
|
2020-08-22 00:52:02 +02:00
|
|
|
printColumnNames(FOS, getSubTargetInfo().getSchedModel());
|
2018-05-15 20:11:45 +02:00
|
|
|
FOS << "Instructions:\n";
|
2018-05-15 17:18:05 +02:00
|
|
|
|
2018-10-24 17:06:27 +02:00
|
|
|
unsigned InstrIndex = 0;
|
2020-08-22 00:52:02 +02:00
|
|
|
ArrayRef<llvm::MCInst> Source = getSource();
|
2018-10-26 12:48:04 +02:00
|
|
|
const unsigned Executions = LastInstructionIdx / Source.size() + 1;
|
2018-10-24 17:06:27 +02:00
|
|
|
for (const MCInst &MCI : Source) {
|
|
|
|
unsigned BaseEltIdx = InstrIndex * NumResourceUnits;
|
2018-03-08 14:05:02 +01:00
|
|
|
for (unsigned J = 0; J < NumResourceUnits; ++J) {
|
2018-10-24 17:06:27 +02:00
|
|
|
double Usage = ResourceUsage[J + BaseEltIdx];
|
2018-05-15 20:11:45 +02:00
|
|
|
printResourcePressure(FOS, Usage / Executions, (J + 1) * 7);
|
2018-03-08 14:05:02 +01:00
|
|
|
}
|
|
|
|
|
2020-08-22 00:52:02 +02:00
|
|
|
FOS << printInstructionString(MCI) << '\n';
|
2018-05-15 20:11:45 +02:00
|
|
|
FOS.flush();
|
2018-03-08 14:05:02 +01:00
|
|
|
OS << Buffer;
|
|
|
|
Buffer = "";
|
2018-10-24 17:06:27 +02:00
|
|
|
|
|
|
|
++InstrIndex;
|
2018-03-08 14:05:02 +01:00
|
|
|
}
|
|
|
|
}
|
2021-01-21 23:04:13 +01:00
|
|
|
|
|
|
|
json::Value ResourcePressureView::toJSON() const {
|
|
|
|
// We're dumping the instructions and the ResourceUsage array.
|
|
|
|
json::Array ResourcePressureInfo;
|
|
|
|
|
|
|
|
// The ResourceUsage matrix is sparse, so we only consider
|
|
|
|
// non-zero values.
|
|
|
|
ArrayRef<llvm::MCInst> Source = getSource();
|
|
|
|
const unsigned Executions = LastInstructionIdx / Source.size() + 1;
|
|
|
|
for (const auto &R : enumerate(ResourceUsage)) {
|
|
|
|
const ResourceCycles &RU = R.value();
|
|
|
|
if (RU.getNumerator() == 0)
|
|
|
|
continue;
|
|
|
|
unsigned InstructionIndex = R.index() / NumResourceUnits;
|
|
|
|
unsigned ResourceIndex = R.index() % NumResourceUnits;
|
|
|
|
double Usage = RU / Executions;
|
|
|
|
ResourcePressureInfo.push_back(
|
|
|
|
json::Object({{"InstructionIndex", InstructionIndex},
|
|
|
|
{"ResourceIndex", ResourceIndex},
|
|
|
|
{"ResourceUsage", Usage}}));
|
|
|
|
}
|
|
|
|
|
|
|
|
json::Object JO({{"ResourcePressureInfo", std::move(ResourcePressureInfo)}});
|
|
|
|
return JO;
|
|
|
|
}
|
2018-03-08 14:05:02 +01:00
|
|
|
} // namespace mca
|
2018-10-30 16:56:08 +01:00
|
|
|
} // namespace llvm
|