[llvm-mca][JSON] Further refactoring of the JSON printing logic.

This patch renames object "Resources" to "TargetInfo".

Moved the getJSONTargetInfo method from class InstructionView to the
PipelinePrinter.

Removed uses of std::stringstream.
Removed unused method View::printViewJSON().
This commit is contained in:
Andrea Di Biagio 2021-07-10 12:27:30 +01:00
parent 239fcda268
commit d919bca875
9 changed files with 35 additions and 41 deletions

View File

@ -15,7 +15,7 @@ add %edx, %edx
# LLVM-MCA-END foo
# CHECK: {
# CHECK-NEXT: "Resources": {
# CHECK-NEXT: "TargetInfo": {
# CHECK-NEXT: "CPUName": "haswell",
# CHECK-NEXT: "Resources": [
# CHECK-NEXT: "HWDivider",

View File

@ -18,7 +18,7 @@ add %edx, %edx
# LLVM-MCA-END foo
# CHECK: {
# CHECK-NEXT: "Resources": {
# CHECK-NEXT: "TargetInfo": {
# CHECK-NEXT: "CPUName": "haswell",
# CHECK-NEXT: "Resources": [
# CHECK-NEXT: "HWDivider",

View File

@ -14,7 +14,7 @@ add %ecx, %ecx
add %edx, %edx
# CHECK: {
# CHECK-NEXT: "Resources": {
# CHECK-NEXT: "TargetInfo": {
# CHECK-NEXT: "CPUName": "haswell",
# CHECK-NEXT: "Resources": [
# CHECK-NEXT: "HWDivider",

View File

@ -40,9 +40,34 @@ json::Object PipelinePrinter::getJSONReportRegion() const {
return JO;
}
json::Object PipelinePrinter::getJSONTargetInfo() const {
json::Array Resources;
const MCSchedModel &SM = STI.getSchedModel();
StringRef MCPU = STI.getCPU();
for (unsigned I = 1, E = SM.getNumProcResourceKinds(); I < E; ++I) {
const MCProcResourceDesc &ProcResource = *SM.getProcResource(I);
unsigned NumUnits = ProcResource.NumUnits;
if (ProcResource.SubUnitsIdxBegin || !NumUnits)
continue;
for (unsigned J = 0; J < NumUnits; ++J) {
std::string ResourceName = ProcResource.Name;
if (NumUnits > 1) {
ResourceName += ".";
ResourceName += J;
}
Resources.push_back(ResourceName);
}
}
return json::Object({{"CPUName", MCPU}, {"Resources", std::move(Resources)}});
}
void PipelinePrinter::printReport(json::Object &JO) const {
if (!RegionIdx)
JO.try_emplace("Resources", InstructionView::getJSONTargetInfo(STI));
JO.try_emplace("TargetInfo", getJSONTargetInfo());
StringRef RegionName;
if (Region.getDescription().empty())

View File

@ -45,6 +45,7 @@ class PipelinePrinter {
void printRegionHeader(llvm::raw_ostream &OS) const;
json::Object getJSONReportRegion() const;
json::Object getJSONTargetInfo() const;
public:
PipelinePrinter(Pipeline &Pipe, const CodeRegion &R, unsigned Idx,

View File

@ -11,9 +11,9 @@
///
//===----------------------------------------------------------------------===//
#include <sstream>
#include "Views/InstructionView.h"
#include "llvm/MC/MCInst.h"
#include "llvm/MC/MCInstPrinter.h"
#include "llvm/MC/MCSubtargetInfo.h"
namespace llvm {
@ -38,26 +38,5 @@ json::Value InstructionView::toJSON() const {
return SourceInfo;
}
json::Object InstructionView::getJSONTargetInfo(const MCSubtargetInfo &STI) {
json::Array Resources;
const MCSchedModel &SM = STI.getSchedModel();
StringRef MCPU = STI.getCPU();
for (unsigned I = 1, 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) {
std::stringstream ResNameStream;
ResNameStream << ProcResource.Name;
if (NumUnits > 1)
ResNameStream << "." << J;
Resources.push_back(ResNameStream.str());
}
}
return json::Object({{"CPUName", MCPU}, {"Resources", std::move(Resources)}});
}
} // namespace mca
} // namespace llvm

View File

@ -16,7 +16,6 @@
#define LLVM_TOOLS_LLVM_MCA_INSTRUCTIONVIEW_H
#include "Views/View.h"
#include "llvm/MC/MCInstPrinter.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/JSON.h"
@ -44,6 +43,7 @@ public:
StringRef getNameAsString() const override {
return "Instructions";
}
// Return a reference to a string representing a given machine instruction.
// The result should be used or copied before the next call to
// printInstructionString() as it will overwrite the previous result.
@ -52,14 +52,10 @@ public:
llvm::MCInstPrinter &getInstPrinter() const { return MCIP; }
llvm::ArrayRef<llvm::MCInst> getSource() const { return Source; }
json::Value toJSON() const override;
virtual void printViewJSON(llvm::raw_ostream &OS) override {
json::Value JV = toJSON();
OS << formatv("{0:2}", JV) << "\n";
}
static json::Object getJSONTargetInfo(const llvm::MCSubtargetInfo &STI);
json::Value toJSON() const override;
};
} // namespace mca
} // namespace llvm

View File

@ -20,12 +20,5 @@ namespace mca {
void View::anchor() {}
void View::printViewJSON(llvm::raw_ostream &OS) {
json::Object JO;
JO.try_emplace(getNameAsString().str(), toJSON());
OS << formatv("{0:2}", json::Value(std::move(JO))) << "\n";
}
} // namespace mca
} // namespace llvm

View File

@ -30,9 +30,9 @@ public:
virtual void printView(llvm::raw_ostream &OS) const = 0;
virtual StringRef getNameAsString() const = 0;
virtual void printViewJSON(llvm::raw_ostream &OS);
virtual json::Value toJSON() const { return "not implemented"; }
virtual bool isSerializable() const { return true; }
void anchor() override;
};
} // namespace mca