xref: /llvm-project/llvm/unittests/tools/llvm-mca/MCATestBase.h (revision e03f427196ec67a8a5cfbdd658f9eabe9bce83ce)
1 //===---- MCATestBase.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 // Test fixture common to all MCA tests.
9 //===----------------------------------------------------------------------===//
10 
11 #ifndef LLVM_UNITTESTS_TOOLS_LLVMMCA_MCATESTBASE_H
12 #define LLVM_UNITTESTS_TOOLS_LLVMMCA_MCATESTBASE_H
13 
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/MC/MCAsmInfo.h"
16 #include "llvm/MC/MCContext.h"
17 #include "llvm/MC/MCInst.h"
18 #include "llvm/MC/MCInstPrinter.h"
19 #include "llvm/MC/MCInstrAnalysis.h"
20 #include "llvm/MC/MCInstrInfo.h"
21 #include "llvm/MC/MCObjectFileInfo.h"
22 #include "llvm/MC/MCRegisterInfo.h"
23 #include "llvm/MC/MCSubtargetInfo.h"
24 #include "llvm/MC/MCTargetOptions.h"
25 #include "llvm/MC/TargetRegistry.h"
26 #include "llvm/MCA/Context.h"
27 #include "llvm/TargetParser/SubtargetFeature.h"
28 #include "llvm/TargetParser/Triple.h"
29 
30 #include "gtest/gtest.h"
31 
32 namespace llvm {
33 namespace json {
34 class Object;
35 } // end namespace json
36 
37 namespace mca {
38 class View;
39 
40 class MCATestBase : public ::testing::Test {
41 protected:
42   // Note: Subclass ctors are expected to perform target-specific
43   // initializations.
44   MCATestBase(StringRef TripleStr, StringRef CPUName, StringRef MAttr = "")
45       : TheTriple(TripleStr), CPUName(CPUName), MAttr(MAttr) {}
46 
47   /// Factory function to create a Target.
48   virtual const Target *getLLVMTarget() const;
49 
50   /// Factory function to create a MCTargetOptions instance. Returns an
51   /// empty one by default.
52   virtual MCTargetOptions getMCTargetOptions() { return MCTargetOptions(); }
53 
54   const Target *TheTarget;
55   const Triple TheTriple;
56   StringRef CPUName;
57   StringRef MAttr;
58 
59   // MC components.
60   std::unique_ptr<MCSubtargetInfo> STI;
61   std::unique_ptr<MCRegisterInfo> MRI;
62   std::unique_ptr<MCAsmInfo> MAI;
63   std::unique_ptr<MCObjectFileInfo> MOFI;
64   std::unique_ptr<MCContext> Ctx;
65   std::unique_ptr<MCInstrInfo> MCII;
66   std::unique_ptr<MCInstrAnalysis> MCIA;
67   std::unique_ptr<MCInstPrinter> IP;
68 
69   static mca::PipelineOptions getDefaultPipelineOptions();
70 
71   void SetUp() override;
72 
73   /// Utility function to run MCA with (nearly) the same configuration as the
74   /// `llvm-mca` tool to verify result correctness.
75   /// This function only displays on SummaryView by default.
76   virtual Error runBaselineMCA(json::Object &Result, ArrayRef<MCInst> Insts,
77                                ArrayRef<mca::View *> Views = {},
78                                const mca::PipelineOptions *PO = nullptr);
79 };
80 
81 } // end namespace mca
82 } // end namespace llvm
83 #endif
84