2018-03-08 14:05:02 +01:00
|
|
|
//===--------------------- ResourcePressureView.cpp -------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// \file
|
|
|
|
///
|
|
|
|
/// This file implements methods in the ResourcePressureView interface.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "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"
|
|
|
|
|
|
|
|
namespace mca {
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2018-03-10 17:55:07 +01:00
|
|
|
void ResourcePressureView::initialize() {
|
2018-03-08 14:05:02 +01:00
|
|
|
// Populate the map of resource descriptors.
|
|
|
|
unsigned R2VIndex = 0;
|
|
|
|
const MCSchedModel &SM = STI.getSchedModel();
|
|
|
|
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;
|
|
|
|
ResourceUsage.resize(NumResourceUnits * (Source.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-03-13 14:11:01 +01:00
|
|
|
// We're only interested in Issue events.
|
|
|
|
if (Event.Type != HWInstructionEvent::Issued)
|
|
|
|
return;
|
|
|
|
const auto &IssueEvent = static_cast<const HWInstructionIssuedEvent &>(Event);
|
2018-05-07 20:29:15 +02:00
|
|
|
const unsigned SourceIdx = Event.IR.getSourceIndex() % Source.size();
|
2018-03-23 18:53:02 +01:00
|
|
|
for (const std::pair<ResourceRef, double> &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-03-08 14:05:02 +01:00
|
|
|
void ResourcePressureView::printResourcePressurePerIteration(
|
|
|
|
raw_ostream &OS, unsigned Executions) const {
|
|
|
|
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";
|
2018-03-08 14:05:02 +01:00
|
|
|
const MCSchedModel &SM = STI.getSchedModel();
|
|
|
|
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
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourcePressureView::printResourcePressurePerInstruction(
|
|
|
|
raw_ostream &OS, unsigned Executions) const {
|
|
|
|
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";
|
|
|
|
printColumnNames(FOS, STI.getSchedModel());
|
|
|
|
FOS << "Instructions:\n";
|
2018-05-15 17:18:05 +02:00
|
|
|
|
|
|
|
std::string Instruction;
|
|
|
|
raw_string_ostream InstrStream(Instruction);
|
2018-03-08 14:05:02 +01:00
|
|
|
|
|
|
|
for (unsigned I = 0, E = Source.size(); I < E; ++I) {
|
|
|
|
for (unsigned J = 0; J < NumResourceUnits; ++J) {
|
2018-03-23 18:53:02 +01:00
|
|
|
double Usage = ResourceUsage[J + I * NumResourceUnits];
|
2018-05-15 20:11:45 +02:00
|
|
|
printResourcePressure(FOS, Usage / Executions, (J + 1) * 7);
|
2018-03-08 14:05:02 +01:00
|
|
|
}
|
|
|
|
|
2018-05-15 17:18:05 +02:00
|
|
|
MCIP.printInst(&Source.getMCInstFromIndex(I), InstrStream, "", STI);
|
|
|
|
InstrStream.flush();
|
|
|
|
StringRef Str(Instruction);
|
|
|
|
|
|
|
|
// Remove any tabs or spaces at the beginning of the instruction.
|
|
|
|
Str = Str.ltrim();
|
|
|
|
|
2018-05-15 20:11:45 +02:00
|
|
|
FOS << Str << '\n';
|
2018-05-15 17:18:05 +02:00
|
|
|
Instruction = "";
|
|
|
|
|
2018-05-15 20:11:45 +02:00
|
|
|
FOS.flush();
|
2018-03-08 14:05:02 +01:00
|
|
|
OS << Buffer;
|
|
|
|
Buffer = "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace mca
|