1 //===----------------------- View.h -----------------------------*- 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 defines the main interface for Views. Each view contributes a 11 /// portion of the final report generated by the tool. 12 /// 13 //===----------------------------------------------------------------------===// 14 15 #ifndef LLVM_TOOLS_LLVM_MCA_VIEW_H 16 #define LLVM_TOOLS_LLVM_MCA_VIEW_H 17 18 #include "llvm/MC/MCInstPrinter.h" 19 #include "llvm/MCA/HWEventListener.h" 20 #include "llvm/Support/raw_ostream.h" 21 #include "llvm/Support/JSON.h" 22 23 namespace llvm { 24 namespace mca { 25 26 class View : public HWEventListener { 27 public: 28 enum OutputKind { OK_READABLE, OK_JSON }; 29 printView(OutputKind OutputKind,llvm::raw_ostream & OS)30 void printView(OutputKind OutputKind, llvm::raw_ostream &OS) { 31 if (OutputKind == OK_JSON) 32 printViewJSON(OS); 33 else 34 printView(OS); 35 } 36 37 virtual void printView(llvm::raw_ostream &OS) const = 0; printViewJSON(llvm::raw_ostream & OS)38 virtual void printViewJSON(llvm::raw_ostream &OS) { 39 json::Object JO; 40 JO.try_emplace(getNameAsString().str(), toJSON()); 41 OS << formatv("{0:2}", json::Value(std::move(JO))) << "\n"; 42 } 43 virtual ~View() = default; 44 virtual StringRef getNameAsString() const = 0; toJSON()45 virtual json::Value toJSON() const { return "not implemented"; } 46 void anchor() override; 47 }; 48 } // namespace mca 49 } // namespace llvm 50 51 #endif 52