1//===--------------------- PipelinePrinter.cpp ------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8/// \file
9///
10/// This file implements the PipelinePrinter interface.
11///
12//===----------------------------------------------------------------------===//
13
14#include "PipelinePrinter.h"
15#include "CodeRegion.h"
16
17namespace llvm {
18namespace mca {
19
20void PipelinePrinter::printRegionHeader(llvm::raw_ostream &OS) const {
21 StringRef RegionName;
22 if (!Region.getDescription().empty())
23 RegionName = Region.getDescription();
24
25 OS << "\n[" << RegionIdx << "] Code Region";
26 if (!RegionName.empty())
27 OS << " - " << RegionName;
28 OS << "\n\n";
29}
30
31json::Object PipelinePrinter::getJSONReportRegion() const {
32 json::Object JO;
33
34 StringRef RegionName = "";
35 if (!Region.getDescription().empty())
36 RegionName = Region.getDescription();
37
38 JO.try_emplace(K: "Name", Args&: RegionName);
39 for (const auto &V : Views)
40 if (V->isSerializable())
41 JO.try_emplace(K: V->getNameAsString().str(), Args: V->toJSON());
42
43 return JO;
44}
45
46json::Object PipelinePrinter::getJSONSimulationParameters() const {
47 json::Object SimParameters({{.K: "-mcpu", .V: STI.getCPU()},
48 {.K: "-mtriple", .V: STI.getTargetTriple().getTriple()},
49 {.K: "-march", .V: STI.getTargetTriple().getArchName()}});
50
51 const MCSchedModel &SM = STI.getSchedModel();
52 if (!SM.isOutOfOrder())
53 return SimParameters;
54
55 if (PO.RegisterFileSize)
56 SimParameters.try_emplace(K: "-register-file-size", Args: PO.RegisterFileSize);
57
58 if (!PO.AssumeNoAlias)
59 SimParameters.try_emplace(K: "-noalias", Args: PO.AssumeNoAlias);
60
61 if (PO.DecodersThroughput)
62 SimParameters.try_emplace(K: "-decoder-throughput", Args: PO.DecodersThroughput);
63
64 if (PO.MicroOpQueueSize)
65 SimParameters.try_emplace(K: "-micro-op-queue-size", Args: PO.MicroOpQueueSize);
66
67 if (PO.DispatchWidth)
68 SimParameters.try_emplace(K: "-dispatch", Args: PO.DispatchWidth);
69
70 if (PO.LoadQueueSize)
71 SimParameters.try_emplace(K: "-lqueue", Args: PO.LoadQueueSize);
72
73 if (PO.StoreQueueSize)
74 SimParameters.try_emplace(K: "-squeue", Args: PO.StoreQueueSize);
75
76 return SimParameters;
77}
78
79json::Object PipelinePrinter::getJSONTargetInfo() const {
80 json::Array Resources;
81 const MCSchedModel &SM = STI.getSchedModel();
82 StringRef MCPU = STI.getCPU();
83
84 for (unsigned I = 1, E = SM.getNumProcResourceKinds(); I < E; ++I) {
85 const MCProcResourceDesc &ProcResource = *SM.getProcResource(ProcResourceIdx: I);
86 unsigned NumUnits = ProcResource.NumUnits;
87 if (ProcResource.SubUnitsIdxBegin || !NumUnits)
88 continue;
89
90 for (unsigned J = 0; J < NumUnits; ++J) {
91 std::string ResourceName = ProcResource.Name;
92 if (NumUnits > 1) {
93 ResourceName += ".";
94 ResourceName += J;
95 }
96
97 Resources.push_back(E: ResourceName);
98 }
99 }
100
101 return json::Object({{.K: "CPUName", .V: MCPU}, {.K: "Resources", .V: std::move(Resources)}});
102}
103
104void PipelinePrinter::printReport(json::Object &JO) const {
105 if (!RegionIdx) {
106 JO.try_emplace(K: "TargetInfo", Args: getJSONTargetInfo());
107 JO.try_emplace(K: "SimulationParameters", Args: getJSONSimulationParameters());
108 // Construct an array of regions.
109 JO.try_emplace(K: "CodeRegions", Args: json::Array());
110 }
111
112 json::Array *Regions = JO.getArray(K: "CodeRegions");
113 assert(Regions && "This array must exist!");
114 Regions->push_back(E: getJSONReportRegion());
115}
116
117void PipelinePrinter::printReport(llvm::raw_ostream &OS) const {
118 // Don't print the header of this region if it is the default region, and if
119 // it doesn't have an end location.
120 if (Region.startLoc().isValid() || Region.endLoc().isValid())
121 printRegionHeader(OS);
122
123 for (const auto &V : Views)
124 V->printView(OS);
125}
126
127} // namespace mca
128} // namespace llvm
129