• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===------ OrcTestCommon.h - Utilities for Orc Unit Tests ------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Common utilities for the Orc unit tests.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 
15 #ifndef LLVM_UNITTESTS_EXECUTIONENGINE_ORC_ORCTESTCOMMON_H
16 #define LLVM_UNITTESTS_EXECUTIONENGINE_ORC_ORCTESTCOMMON_H
17 
18 #include "llvm/IR/Function.h"
19 #include "llvm/IR/IRBuilder.h"
20 #include "llvm/IR/LLVMContext.h"
21 #include "llvm/IR/Module.h"
22 #include "llvm/IR/TypeBuilder.h"
23 #include "llvm/Object/ObjectFile.h"
24 #include "llvm/ExecutionEngine/ExecutionEngine.h"
25 #include "llvm/ExecutionEngine/Orc/JITSymbol.h"
26 #include "llvm/Support/TargetSelect.h"
27 #include <memory>
28 
29 namespace llvm {
30 
31 // Base class for Orc tests that will execute code.
32 class OrcExecutionTest {
33 public:
34 
OrcExecutionTest()35   OrcExecutionTest() {
36     if (!NativeTargetInitialized) {
37       InitializeNativeTarget();
38       InitializeNativeTargetAsmParser();
39       InitializeNativeTargetAsmPrinter();
40       NativeTargetInitialized = true;
41     }
42 
43     // Try to select a TargetMachine for the host.
44     TM.reset(EngineBuilder().selectTarget());
45 
46     if (TM) {
47       // If we found a TargetMachine, check that it's one that Orc supports.
48       const Triple& TT = TM->getTargetTriple();
49 
50       if ((TT.getArch() != Triple::x86_64 && TT.getArch() != Triple::x86) ||
51           TT.isOSWindows())
52         TM = nullptr;
53     }
54   };
55 
56 protected:
57   LLVMContext Context;
58   std::unique_ptr<TargetMachine> TM;
59 private:
60   static bool NativeTargetInitialized;
61 };
62 
63 class ModuleBuilder {
64 public:
65   ModuleBuilder(LLVMContext &Context, StringRef Triple,
66                 StringRef Name);
67 
68   template <typename FuncType>
createFunctionDecl(StringRef Name)69   Function* createFunctionDecl(StringRef Name) {
70     return Function::Create(
71              TypeBuilder<FuncType, false>::get(M->getContext()),
72              GlobalValue::ExternalLinkage, Name, M.get());
73   }
74 
getModule()75   Module* getModule() { return M.get(); }
getModule()76   const Module* getModule() const { return M.get(); }
takeModule()77   std::unique_ptr<Module> takeModule() { return std::move(M); }
78 
79 private:
80   std::unique_ptr<Module> M;
81 };
82 
83 // Dummy struct type.
84 struct DummyStruct {
85   int X[256];
86 };
87 
88 // TypeBuilder specialization for DummyStruct.
89 template <bool XCompile>
90 class TypeBuilder<DummyStruct, XCompile> {
91 public:
get(LLVMContext & Context)92   static StructType *get(LLVMContext &Context) {
93     return StructType::get(
94       TypeBuilder<types::i<32>[256], XCompile>::get(Context), nullptr);
95   }
96 };
97 
98 template <typename HandleT,
99           typename AddModuleSetFtor,
100           typename RemoveModuleSetFtor,
101           typename FindSymbolFtor,
102           typename FindSymbolInFtor>
103 class MockBaseLayer {
104 public:
105 
106   typedef HandleT ModuleSetHandleT;
107 
MockBaseLayer(AddModuleSetFtor && AddModuleSet,RemoveModuleSetFtor && RemoveModuleSet,FindSymbolFtor && FindSymbol,FindSymbolInFtor && FindSymbolIn)108   MockBaseLayer(AddModuleSetFtor &&AddModuleSet,
109                 RemoveModuleSetFtor &&RemoveModuleSet,
110                 FindSymbolFtor &&FindSymbol,
111                 FindSymbolInFtor &&FindSymbolIn)
112       : AddModuleSet(AddModuleSet), RemoveModuleSet(RemoveModuleSet),
113         FindSymbol(FindSymbol), FindSymbolIn(FindSymbolIn)
114   {}
115 
116   template <typename ModuleSetT, typename MemoryManagerPtrT,
117             typename SymbolResolverPtrT>
addModuleSet(ModuleSetT Ms,MemoryManagerPtrT MemMgr,SymbolResolverPtrT Resolver)118   ModuleSetHandleT addModuleSet(ModuleSetT Ms, MemoryManagerPtrT MemMgr,
119                                 SymbolResolverPtrT Resolver) {
120     return AddModuleSet(std::move(Ms), std::move(MemMgr), std::move(Resolver));
121   }
122 
removeModuleSet(ModuleSetHandleT H)123   void removeModuleSet(ModuleSetHandleT H) {
124     RemoveModuleSet(H);
125   }
126 
findSymbol(const std::string & Name,bool ExportedSymbolsOnly)127   orc::JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
128     return FindSymbol(Name, ExportedSymbolsOnly);
129   }
130 
findSymbolIn(ModuleSetHandleT H,const std::string & Name,bool ExportedSymbolsOnly)131   orc::JITSymbol findSymbolIn(ModuleSetHandleT H, const std::string &Name,
132                          bool ExportedSymbolsOnly) {
133     return FindSymbolIn(H, Name, ExportedSymbolsOnly);
134   }
135 
136 private:
137   AddModuleSetFtor AddModuleSet;
138   RemoveModuleSetFtor RemoveModuleSet;
139   FindSymbolFtor FindSymbol;
140   FindSymbolInFtor FindSymbolIn;
141 };
142 
143 template <typename ModuleSetHandleT,
144           typename AddModuleSetFtor,
145           typename RemoveModuleSetFtor,
146           typename FindSymbolFtor,
147           typename FindSymbolInFtor>
148 MockBaseLayer<ModuleSetHandleT, AddModuleSetFtor, RemoveModuleSetFtor,
149               FindSymbolFtor, FindSymbolInFtor>
createMockBaseLayer(AddModuleSetFtor && AddModuleSet,RemoveModuleSetFtor && RemoveModuleSet,FindSymbolFtor && FindSymbol,FindSymbolInFtor && FindSymbolIn)150 createMockBaseLayer(AddModuleSetFtor &&AddModuleSet,
151                     RemoveModuleSetFtor &&RemoveModuleSet,
152                     FindSymbolFtor &&FindSymbol,
153                     FindSymbolInFtor &&FindSymbolIn) {
154   return MockBaseLayer<ModuleSetHandleT, AddModuleSetFtor, RemoveModuleSetFtor,
155                        FindSymbolFtor, FindSymbolInFtor>(
156                          std::forward<AddModuleSetFtor>(AddModuleSet),
157                          std::forward<RemoveModuleSetFtor>(RemoveModuleSet),
158                          std::forward<FindSymbolFtor>(FindSymbol),
159                          std::forward<FindSymbolInFtor>(FindSymbolIn));
160 }
161 
162 template <typename ReturnT>
163 class DoNothingAndReturn {
164 public:
DoNothingAndReturn(ReturnT Val)165   DoNothingAndReturn(ReturnT Val) : Val(Val) {}
166 
167   template <typename... Args>
operator()168   ReturnT operator()(Args...) const { return Val; }
169 private:
170   ReturnT Val;
171 };
172 
173 template <>
174 class DoNothingAndReturn<void> {
175 public:
176   template <typename... Args>
operator()177   void operator()(Args...) const { }
178 };
179 
180 } // namespace llvm
181 
182 #endif
183