1 //===--------------------- ResourcePressureView.cpp -------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// This file implements methods in the ResourcePressureView interface.
13 //===----------------------------------------------------------------------===//
15 #include "Views/ResourcePressureView.h"
16 #include "llvm/Support/FormattedStream.h"
17 #include "llvm/Support/raw_ostream.h"
22 ResourcePressureView::ResourcePressureView(const llvm::MCSubtargetInfo &sti,
23 MCInstPrinter &Printer,
25 : STI(sti), MCIP(Printer), Source(S), LastInstructionIdx(0) {
26 // Populate the map of resource descriptors.
27 unsigned R2VIndex = 0;
28 const MCSchedModel &SM = STI.getSchedModel();
29 for (unsigned I = 0, E = SM.getNumProcResourceKinds(); I < E; ++I) {
30 const MCProcResourceDesc &ProcResource = *SM.getProcResource(I);
31 unsigned NumUnits = ProcResource.NumUnits;
32 // Skip groups and invalid resources with zero units.
33 if (ProcResource.SubUnitsIdxBegin || !NumUnits)
36 Resource2VecIndex.insert(std::pair<unsigned, unsigned>(I, R2VIndex));
37 R2VIndex += ProcResource.NumUnits;
40 NumResourceUnits = R2VIndex;
41 ResourceUsage.resize(NumResourceUnits * (Source.size() + 1));
42 std::fill(ResourceUsage.begin(), ResourceUsage.end(), 0.0);
45 void ResourcePressureView::onEvent(const HWInstructionEvent &Event) {
46 if (Event.Type == HWInstructionEvent::Dispatched) {
47 LastInstructionIdx = Event.IR.getSourceIndex();
51 // We're only interested in Issue events.
52 if (Event.Type != HWInstructionEvent::Issued)
55 const auto &IssueEvent = static_cast<const HWInstructionIssuedEvent &>(Event);
56 const unsigned SourceIdx = Event.IR.getSourceIndex() % Source.size();
57 for (const std::pair<ResourceRef, ResourceCycles> &Use :
58 IssueEvent.UsedResources) {
59 const ResourceRef &RR = Use.first;
60 assert(Resource2VecIndex.find(RR.first) != Resource2VecIndex.end());
61 unsigned R2VIndex = Resource2VecIndex[RR.first];
62 R2VIndex += countTrailingZeros(RR.second);
63 ResourceUsage[R2VIndex + NumResourceUnits * SourceIdx] += Use.second;
64 ResourceUsage[R2VIndex + NumResourceUnits * Source.size()] += Use.second;
68 static void printColumnNames(formatted_raw_ostream &OS,
69 const MCSchedModel &SM) {
70 unsigned Column = OS.getColumn();
71 for (unsigned I = 1, ResourceIndex = 0, E = SM.getNumProcResourceKinds();
73 const MCProcResourceDesc &ProcResource = *SM.getProcResource(I);
74 unsigned NumUnits = ProcResource.NumUnits;
75 // Skip groups and invalid resources with zero units.
76 if (ProcResource.SubUnitsIdxBegin || !NumUnits)
79 for (unsigned J = 0; J < NumUnits; ++J) {
81 OS << "[" << ResourceIndex;
85 OS.PadToColumn(Column);
92 static void printResourcePressure(formatted_raw_ostream &OS, double Pressure,
94 if (!Pressure || Pressure < 0.005) {
97 // Round to the value to the nearest hundredth and then print it.
98 OS << format("%.2f", floor((Pressure * 100) + 0.5) / 100);
103 void ResourcePressureView::printResourcePressurePerIter(raw_ostream &OS) const {
105 raw_string_ostream TempStream(Buffer);
106 formatted_raw_ostream FOS(TempStream);
108 FOS << "\n\nResources:\n";
109 const MCSchedModel &SM = STI.getSchedModel();
110 for (unsigned I = 1, ResourceIndex = 0, E = SM.getNumProcResourceKinds();
112 const MCProcResourceDesc &ProcResource = *SM.getProcResource(I);
113 unsigned NumUnits = ProcResource.NumUnits;
114 // Skip groups and invalid resources with zero units.
115 if (ProcResource.SubUnitsIdxBegin || !NumUnits)
118 for (unsigned J = 0; J < NumUnits; ++J) {
119 FOS << '[' << ResourceIndex;
124 FOS << "- " << ProcResource.Name << '\n';
130 FOS << "\n\nResource pressure per iteration:\n";
132 printColumnNames(FOS, SM);
136 const unsigned Executions = LastInstructionIdx / Source.size() + 1;
137 for (unsigned I = 0, E = NumResourceUnits; I < E; ++I) {
138 double Usage = ResourceUsage[I + Source.size() * E];
139 printResourcePressure(FOS, Usage / Executions, (I + 1) * 7);
146 void ResourcePressureView::printResourcePressurePerInst(raw_ostream &OS) const {
148 raw_string_ostream TempStream(Buffer);
149 formatted_raw_ostream FOS(TempStream);
151 FOS << "\n\nResource pressure by instruction:\n";
152 printColumnNames(FOS, STI.getSchedModel());
153 FOS << "Instructions:\n";
155 std::string Instruction;
156 raw_string_ostream InstrStream(Instruction);
158 unsigned InstrIndex = 0;
159 const unsigned Executions = LastInstructionIdx / Source.size() + 1;
160 for (const MCInst &MCI : Source) {
161 unsigned BaseEltIdx = InstrIndex * NumResourceUnits;
162 for (unsigned J = 0; J < NumResourceUnits; ++J) {
163 double Usage = ResourceUsage[J + BaseEltIdx];
164 printResourcePressure(FOS, Usage / Executions, (J + 1) * 7);
167 MCIP.printInst(&MCI, InstrStream, "", STI);
169 StringRef Str(Instruction);
171 // Remove any tabs or spaces at the beginning of the instruction.