[Orc] Require target support for host before running execution unit tests.
[oota-llvm.git] / unittests / ExecutionEngine / Orc / OrcTestCommon.h
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/ExecutionEngine/ExecutionEngine.h"
24 #include "llvm/ExecutionEngine/Orc/JITSymbol.h"
25 #include "llvm/Support/TargetSelect.h"
26 #include <memory>
27
28 namespace llvm {
29
30 // Base class for Orc tests that will execute code.
31 class OrcExecutionTest {
32 public:
33
34   OrcExecutionTest() {
35     if (!NativeTargetInitialized) {
36       InitializeNativeTarget();
37       InitializeNativeTargetAsmParser();
38       InitializeNativeTargetAsmPrinter();
39       NativeTargetInitialized = true;
40     }
41   };
42
43   // Get a target machine for the host if it supports JIT execution.
44   std::unique_ptr<TargetMachine> getHostTargetMachineIfSupported() {
45     std::unique_ptr<TargetMachine> TM(EngineBuilder().selectTarget());
46
47     if (!TM)
48       return nullptr;
49
50     const Triple& TT = TM->getTargetTriple();
51
52     if (TT.getArch() != Triple::x86_64 || !TT.isOSDarwin())
53       return nullptr;
54
55     return TM;
56   }
57
58 private:
59   static bool NativeTargetInitialized;
60 };
61
62 class ModuleBuilder {
63 public:
64   ModuleBuilder(LLVMContext &Context, StringRef Triple,
65                 StringRef Name);
66
67   template <typename FuncType>
68   Function* createFunctionDecl(StringRef Name) {
69     return Function::Create(
70              TypeBuilder<FuncType, false>::get(M->getContext()),
71              GlobalValue::ExternalLinkage, Name, M.get());
72   }
73
74   Module* getModule() { return M.get(); }
75   const Module* getModule() const { return M.get(); }
76   std::unique_ptr<Module> takeModule() { return std::move(M); }
77
78 private:
79   std::unique_ptr<Module> M;
80   IRBuilder<> Builder;
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:
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
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>
118   ModuleSetHandleT addModuleSet(ModuleSetT Ms, MemoryManagerPtrT MemMgr,
119                                 SymbolResolverPtrT Resolver) {
120     return AddModuleSet(std::move(Ms), std::move(MemMgr), std::move(Resolver));
121   }
122
123   void removeModuleSet(ModuleSetHandleT H) {
124     RemoveModuleSet(H);
125   }
126
127   orc::JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
128     return FindSymbol(Name, ExportedSymbolsOnly);
129   }
130
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>
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:
165   DoNothingAndReturn(ReturnT Val) : Val(Val) {}
166
167   template <typename... Args>
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>
177   void operator()(Args...) const { }
178 };
179
180 } // namespace llvm
181
182 #endif