View.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. #ifndef LLVM_TOOLS_LLVM_MCA_VIEW_H
  15. #define LLVM_TOOLS_LLVM_MCA_VIEW_H
  16. #include "llvm/MC/MCInstPrinter.h"
  17. #include "llvm/MCA/HWEventListener.h"
  18. #include "llvm/Support/raw_ostream.h"
  19. #include "llvm/Support/JSON.h"
  20. namespace llvm {
  21. namespace mca {
  22. class View : public HWEventListener {
  23. public:
  24. enum OutputKind { OK_READABLE, OK_JSON };
  25. void printView(OutputKind OutputKind, llvm::raw_ostream &OS) {
  26. if (OutputKind == OK_JSON)
  27. printViewJSON(OS);
  28. else
  29. printView(OS);
  30. }
  31. virtual void printView(llvm::raw_ostream &OS) const = 0;
  32. virtual void printViewJSON(llvm::raw_ostream &OS) {
  33. json::Object JO;
  34. JO.try_emplace(getNameAsString().str(), toJSON());
  35. OS << formatv("{0:2}", json::Value(std::move(JO))) << "\n";
  36. }
  37. virtual ~View() = default;
  38. virtual StringRef getNameAsString() const = 0;
  39. virtual json::Value toJSON() const { return "not implemented"; }
  40. void anchor() override;
  41. };
  42. } // namespace mca
  43. } // namespace llvm
  44. #endif