[llvm] d919bca - [llvm-mca][JSON] Further refactoring of the JSON printing logic.
Andrea Di Biagio via llvm-commits
llvm-commits at lists.llvm.org
Sat Jul 10 04:40:17 PDT 2021
Author: Andrea Di Biagio
Date: 2021-07-10T12:38:19+01:00
New Revision: d919bca87556548555af0a7aa1239ea64ba4f3e8
URL: https://github.com/llvm/llvm-project/commit/d919bca87556548555af0a7aa1239ea64ba4f3e8
DIFF: https://github.com/llvm/llvm-project/commit/d919bca87556548555af0a7aa1239ea64ba4f3e8.diff
LOG: [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().
Added:
Modified:
llvm/test/tools/llvm-mca/JSON/X86/instruction-tables-multiple-regions.s
llvm/test/tools/llvm-mca/JSON/X86/views-multiple-region.s
llvm/test/tools/llvm-mca/JSON/X86/views.s
llvm/tools/llvm-mca/PipelinePrinter.cpp
llvm/tools/llvm-mca/PipelinePrinter.h
llvm/tools/llvm-mca/Views/InstructionView.cpp
llvm/tools/llvm-mca/Views/InstructionView.h
llvm/tools/llvm-mca/Views/View.cpp
llvm/tools/llvm-mca/Views/View.h
Removed:
################################################################################
diff --git a/llvm/test/tools/llvm-mca/JSON/X86/instruction-tables-multiple-regions.s b/llvm/test/tools/llvm-mca/JSON/X86/instruction-tables-multiple-regions.s
index 3fb320af06cd..758b57d0479b 100644
--- a/llvm/test/tools/llvm-mca/JSON/X86/instruction-tables-multiple-regions.s
+++ b/llvm/test/tools/llvm-mca/JSON/X86/instruction-tables-multiple-regions.s
@@ -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",
diff --git a/llvm/test/tools/llvm-mca/JSON/X86/views-multiple-region.s b/llvm/test/tools/llvm-mca/JSON/X86/views-multiple-region.s
index 51521b112851..55768b53a2b9 100644
--- a/llvm/test/tools/llvm-mca/JSON/X86/views-multiple-region.s
+++ b/llvm/test/tools/llvm-mca/JSON/X86/views-multiple-region.s
@@ -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",
diff --git a/llvm/test/tools/llvm-mca/JSON/X86/views.s b/llvm/test/tools/llvm-mca/JSON/X86/views.s
index ff82e342e354..c718a0e86ffd 100644
--- a/llvm/test/tools/llvm-mca/JSON/X86/views.s
+++ b/llvm/test/tools/llvm-mca/JSON/X86/views.s
@@ -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",
diff --git a/llvm/tools/llvm-mca/PipelinePrinter.cpp b/llvm/tools/llvm-mca/PipelinePrinter.cpp
index ba491c380713..def9f350a581 100644
--- a/llvm/tools/llvm-mca/PipelinePrinter.cpp
+++ b/llvm/tools/llvm-mca/PipelinePrinter.cpp
@@ -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())
diff --git a/llvm/tools/llvm-mca/PipelinePrinter.h b/llvm/tools/llvm-mca/PipelinePrinter.h
index 1451813d5823..e0b54dec5af1 100644
--- a/llvm/tools/llvm-mca/PipelinePrinter.h
+++ b/llvm/tools/llvm-mca/PipelinePrinter.h
@@ -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,
diff --git a/llvm/tools/llvm-mca/Views/InstructionView.cpp b/llvm/tools/llvm-mca/Views/InstructionView.cpp
index 2e3c08bf7cca..2abe9073cd9f 100644
--- a/llvm/tools/llvm-mca/Views/InstructionView.cpp
+++ b/llvm/tools/llvm-mca/Views/InstructionView.cpp
@@ -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
diff --git a/llvm/tools/llvm-mca/Views/InstructionView.h b/llvm/tools/llvm-mca/Views/InstructionView.h
index c396fb37c5a1..41e943b3eb71 100644
--- a/llvm/tools/llvm-mca/Views/InstructionView.h
+++ b/llvm/tools/llvm-mca/Views/InstructionView.h
@@ -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 @@ class InstructionView : public View {
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 @@ class InstructionView : public View {
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
diff --git a/llvm/tools/llvm-mca/Views/View.cpp b/llvm/tools/llvm-mca/Views/View.cpp
index 79abd3b50649..09d08d3ae007 100644
--- a/llvm/tools/llvm-mca/Views/View.cpp
+++ b/llvm/tools/llvm-mca/Views/View.cpp
@@ -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
diff --git a/llvm/tools/llvm-mca/Views/View.h b/llvm/tools/llvm-mca/Views/View.h
index 2b578c11d4f2..09d8f1b561d7 100644
--- a/llvm/tools/llvm-mca/Views/View.h
+++ b/llvm/tools/llvm-mca/Views/View.h
@@ -30,9 +30,9 @@ class View : public HWEventListener {
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
More information about the llvm-commits
mailing list