blob: c9a43058f7c94c61a5e6e80d554a3313529c74e0 [file] [log] [blame]
Lang Hamesdc4260d2015-04-20 20:41:45 +00001//===------ 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
Chandler Carruth9a67b072017-06-06 11:06:56 +000018#include "llvm/ExecutionEngine/ExecutionEngine.h"
19#include "llvm/ExecutionEngine/JITSymbol.h"
Lang Hamesa95b0df2018-03-28 03:41:45 +000020#include "llvm/ExecutionEngine/Orc/IndirectionUtils.h"
Lang Hamesdc4260d2015-04-20 20:41:45 +000021#include "llvm/IR/Function.h"
22#include "llvm/IR/IRBuilder.h"
23#include "llvm/IR/LLVMContext.h"
24#include "llvm/IR/Module.h"
25#include "llvm/IR/TypeBuilder.h"
Lang Hames859d73c2016-01-09 19:50:40 +000026#include "llvm/Object/ObjectFile.h"
Lang Hamesa95b0df2018-03-28 03:41:45 +000027#include "llvm/Support/TargetRegistry.h"
Lang Hamesfd0c1e712018-07-20 18:31:50 +000028#include "llvm/Support/TargetSelect.h"
29#include "gtest/gtest.h"
30
Lang Hamesdc4260d2015-04-20 20:41:45 +000031#include <memory>
32
33namespace llvm {
34
Lang Hamesfd0c1e712018-07-20 18:31:50 +000035namespace orc {
36// CoreAPIsStandardTest that saves a bunch of boilerplate by providing the
37// following:
38//
39// (1) ES -- An ExecutionSession
40// (2) Foo, Bar, Baz, Qux -- SymbolStringPtrs for strings "foo", "bar", "baz",
41// and "qux" respectively.
42// (3) FooAddr, BarAddr, BazAddr, QuxAddr -- Dummy addresses. Guaranteed
43// distinct and non-null.
44// (4) FooSym, BarSym, BazSym, QuxSym -- JITEvaluatedSymbols with FooAddr,
45// BarAddr, BazAddr, and QuxAddr respectively. All with default strong,
46// linkage and non-hidden visibility.
Lang Hamesd5f56c52018-08-17 21:18:18 +000047// (5) V -- A JITDylib associated with ES.
Lang Hamesfd0c1e712018-07-20 18:31:50 +000048class CoreAPIsBasedStandardTest : public testing::Test {
Lang Hamesfd0c1e712018-07-20 18:31:50 +000049protected:
Lang Hames13014d32018-09-12 21:48:59 +000050 std::shared_ptr<SymbolStringPool> SSP = std::make_shared<SymbolStringPool>();
51 ExecutionSession ES{SSP};
Lang Hamesd5f56c52018-08-17 21:18:18 +000052 JITDylib &JD = ES.createJITDylib("JD");
Lang Hamesfd0c1e712018-07-20 18:31:50 +000053 SymbolStringPtr Foo = ES.getSymbolStringPool().intern("foo");
54 SymbolStringPtr Bar = ES.getSymbolStringPool().intern("bar");
55 SymbolStringPtr Baz = ES.getSymbolStringPool().intern("baz");
56 SymbolStringPtr Qux = ES.getSymbolStringPool().intern("qux");
57 static const JITTargetAddress FooAddr = 1U;
58 static const JITTargetAddress BarAddr = 2U;
59 static const JITTargetAddress BazAddr = 3U;
60 static const JITTargetAddress QuxAddr = 4U;
61 JITEvaluatedSymbol FooSym =
62 JITEvaluatedSymbol(FooAddr, JITSymbolFlags::Exported);
63 JITEvaluatedSymbol BarSym =
64 JITEvaluatedSymbol(BarAddr, JITSymbolFlags::Exported);
65 JITEvaluatedSymbol BazSym =
66 JITEvaluatedSymbol(BazAddr, JITSymbolFlags::Exported);
67 JITEvaluatedSymbol QuxSym =
68 JITEvaluatedSymbol(QuxAddr, JITSymbolFlags::Exported);
69};
70
71} // end namespace orc
72
Lang Hamesd22bade2017-04-04 17:03:49 +000073class OrcNativeTarget {
Lang Hames130a7c42015-10-28 02:40:04 +000074public:
Lang Hamesd22bade2017-04-04 17:03:49 +000075 static void initialize() {
Lang Hames130a7c42015-10-28 02:40:04 +000076 if (!NativeTargetInitialized) {
77 InitializeNativeTarget();
78 InitializeNativeTargetAsmParser();
79 InitializeNativeTargetAsmPrinter();
80 NativeTargetInitialized = true;
81 }
Lang Hamesd22bade2017-04-04 17:03:49 +000082 }
83
84private:
85 static bool NativeTargetInitialized;
86};
87
Lang Hamesc1275e72018-09-26 04:18:30 +000088class SimpleMaterializationUnit : public orc::MaterializationUnit {
89public:
90 using MaterializeFunction =
91 std::function<void(orc::MaterializationResponsibility)>;
92 using DiscardFunction =
93 std::function<void(const orc::JITDylib &, orc::SymbolStringPtr)>;
94 using DestructorFunction = std::function<void()>;
95
96 SimpleMaterializationUnit(
97 orc::SymbolFlagsMap SymbolFlags, MaterializeFunction Materialize,
98 DiscardFunction Discard = DiscardFunction(),
99 DestructorFunction Destructor = DestructorFunction())
100 : MaterializationUnit(std::move(SymbolFlags)),
101 Materialize(std::move(Materialize)), Discard(std::move(Discard)),
102 Destructor(std::move(Destructor)) {}
103
104 ~SimpleMaterializationUnit() override {
105 if (Destructor)
106 Destructor();
107 }
108
Lang Hames53e0df12018-09-28 15:09:14 +0000109 StringRef getName() const override { return "<Simple>"; }
110
Lang Hamesc1275e72018-09-26 04:18:30 +0000111 void materialize(orc::MaterializationResponsibility R) override {
112 Materialize(std::move(R));
113 }
114
115 void discard(const orc::JITDylib &JD, orc::SymbolStringPtr Name) override {
116 if (Discard)
117 Discard(JD, std::move(Name));
118 else
119 llvm_unreachable("Discard not supported");
120 }
121
122private:
123 MaterializeFunction Materialize;
124 DiscardFunction Discard;
125 DestructorFunction Destructor;
126};
127
Lang Hamesd22bade2017-04-04 17:03:49 +0000128// Base class for Orc tests that will execute code.
129class OrcExecutionTest {
130public:
131
132 OrcExecutionTest() {
133
134 // Initialize the native target if it hasn't been done already.
135 OrcNativeTarget::initialize();
Lang Hamesfd6e8dc2015-10-30 03:20:21 +0000136
137 // Try to select a TargetMachine for the host.
138 TM.reset(EngineBuilder().selectTarget());
139
140 if (TM) {
141 // If we found a TargetMachine, check that it's one that Orc supports.
142 const Triple& TT = TM->getTargetTriple();
Lang Hames4f8194e2016-02-10 01:02:33 +0000143
Lang Hamesec978e22018-03-28 14:47:11 +0000144 // Bail out for windows platforms. We do not support these yet.
Lang Hamesda5c6ac2018-03-28 15:58:14 +0000145 if ((TT.getArch() != Triple::x86_64 && TT.getArch() != Triple::x86) ||
146 TT.isOSWindows())
Lang Hamesec978e22018-03-28 14:47:11 +0000147 return;
148
Lang Hamesa95b0df2018-03-28 03:41:45 +0000149 // Target can JIT?
150 SupportsJIT = TM->getTarget().hasJIT();
151 // Use ability to create callback manager to detect whether Orc
152 // has indirection support on this platform. This way the test
153 // and Orc code do not get out of sync.
Lang Hamesbd0cb782018-05-30 01:57:45 +0000154 SupportsIndirection = !!orc::createLocalCompileCallbackManager(TT, ES, 0);
Lang Hamesfd6e8dc2015-10-30 03:20:21 +0000155 }
Lang Hames130a7c42015-10-28 02:40:04 +0000156 };
157
Lang Hamesfd6e8dc2015-10-30 03:20:21 +0000158protected:
Lang Hamesbd0cb782018-05-30 01:57:45 +0000159 orc::ExecutionSession ES;
Mehdi Amini03b42e42016-04-14 21:59:01 +0000160 LLVMContext Context;
Lang Hamesfd6e8dc2015-10-30 03:20:21 +0000161 std::unique_ptr<TargetMachine> TM;
Lang Hamesa95b0df2018-03-28 03:41:45 +0000162 bool SupportsJIT = false;
163 bool SupportsIndirection = false;
Lang Hames130a7c42015-10-28 02:40:04 +0000164};
165
Lang Hames4a51e5d2015-10-27 17:45:48 +0000166class ModuleBuilder {
167public:
168 ModuleBuilder(LLVMContext &Context, StringRef Triple,
169 StringRef Name);
Lang Hamesdc4260d2015-04-20 20:41:45 +0000170
Lang Hames4a51e5d2015-10-27 17:45:48 +0000171 template <typename FuncType>
Lang Hames130a7c42015-10-28 02:40:04 +0000172 Function* createFunctionDecl(StringRef Name) {
Lang Hames4a51e5d2015-10-27 17:45:48 +0000173 return Function::Create(
174 TypeBuilder<FuncType, false>::get(M->getContext()),
Lang Hames130a7c42015-10-28 02:40:04 +0000175 GlobalValue::ExternalLinkage, Name, M.get());
Lang Hames4a51e5d2015-10-27 17:45:48 +0000176 }
Lang Hamesdc4260d2015-04-20 20:41:45 +0000177
Lang Hames4a51e5d2015-10-27 17:45:48 +0000178 Module* getModule() { return M.get(); }
179 const Module* getModule() const { return M.get(); }
180 std::unique_ptr<Module> takeModule() { return std::move(M); }
Lang Hamesdc4260d2015-04-20 20:41:45 +0000181
Lang Hames4a51e5d2015-10-27 17:45:48 +0000182private:
183 std::unique_ptr<Module> M;
Lang Hames4a51e5d2015-10-27 17:45:48 +0000184};
Lang Hamesdc4260d2015-04-20 20:41:45 +0000185
Lang Hames4a51e5d2015-10-27 17:45:48 +0000186// Dummy struct type.
187struct DummyStruct {
188 int X[256];
189};
Lang Hamesdc4260d2015-04-20 20:41:45 +0000190
Lang Hames4a51e5d2015-10-27 17:45:48 +0000191// TypeBuilder specialization for DummyStruct.
192template <bool XCompile>
193class TypeBuilder<DummyStruct, XCompile> {
194public:
195 static StructType *get(LLVMContext &Context) {
196 return StructType::get(
Serge Gueltone38003f2017-05-09 19:31:13 +0000197 TypeBuilder<types::i<32>[256], XCompile>::get(Context));
Lang Hames4a51e5d2015-10-27 17:45:48 +0000198 }
199};
Lang Hamesdc4260d2015-04-20 20:41:45 +0000200
Lang Hamescf771ad2017-09-28 02:17:35 +0000201template <typename HandleT, typename ModuleT>
Lang Hamesc0056562015-10-20 04:35:02 +0000202class MockBaseLayer {
203public:
204
Lang Hamescf771ad2017-09-28 02:17:35 +0000205 using ModuleHandleT = HandleT;
Lang Hamesc0056562015-10-20 04:35:02 +0000206
Lang Hamescf771ad2017-09-28 02:17:35 +0000207 using AddModuleSignature =
208 Expected<ModuleHandleT>(ModuleT M,
209 std::shared_ptr<JITSymbolResolver> R);
Lang Hamesc0056562015-10-20 04:35:02 +0000210
Lang Hamescf771ad2017-09-28 02:17:35 +0000211 using RemoveModuleSignature = Error(ModuleHandleT H);
212 using FindSymbolSignature = JITSymbol(const std::string &Name,
213 bool ExportedSymbolsOnly);
214 using FindSymbolInSignature = JITSymbol(ModuleHandleT H,
215 const std::string &Name,
216 bool ExportedSymbolsONly);
217 using EmitAndFinalizeSignature = Error(ModuleHandleT H);
218
219 std::function<AddModuleSignature> addModuleImpl;
220 std::function<RemoveModuleSignature> removeModuleImpl;
221 std::function<FindSymbolSignature> findSymbolImpl;
222 std::function<FindSymbolInSignature> findSymbolInImpl;
223 std::function<EmitAndFinalizeSignature> emitAndFinalizeImpl;
224
225 Expected<ModuleHandleT> addModule(ModuleT M,
226 std::shared_ptr<JITSymbolResolver> R) {
227 assert(addModuleImpl &&
228 "addModule called, but no mock implementation was provided");
229 return addModuleImpl(std::move(M), std::move(R));
Lang Hamesc0056562015-10-20 04:35:02 +0000230 }
231
Lang Hames4ce98662017-07-07 02:59:13 +0000232 Error removeModule(ModuleHandleT H) {
Lang Hamescf771ad2017-09-28 02:17:35 +0000233 assert(removeModuleImpl &&
234 "removeModule called, but no mock implementation was provided");
235 return removeModuleImpl(H);
Lang Hamesc0056562015-10-20 04:35:02 +0000236 }
237
Lang Hamesad4a9112016-08-01 20:49:11 +0000238 JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
Lang Hamescf771ad2017-09-28 02:17:35 +0000239 assert(findSymbolImpl &&
240 "findSymbol called, but no mock implementation was provided");
241 return findSymbolImpl(Name, ExportedSymbolsOnly);
Lang Hamesc0056562015-10-20 04:35:02 +0000242 }
243
Lang Hamescd9d49b2017-06-23 23:25:28 +0000244 JITSymbol findSymbolIn(ModuleHandleT H, const std::string &Name,
Lang Hamesc0056562015-10-20 04:35:02 +0000245 bool ExportedSymbolsOnly) {
Lang Hamescf771ad2017-09-28 02:17:35 +0000246 assert(findSymbolInImpl &&
247 "findSymbolIn called, but no mock implementation was provided");
248 return findSymbolInImpl(H, Name, ExportedSymbolsOnly);
Lang Hamesc0056562015-10-20 04:35:02 +0000249 }
250
Lang Hamescf771ad2017-09-28 02:17:35 +0000251 Error emitAndFinaliez(ModuleHandleT H) {
252 assert(emitAndFinalizeImpl &&
253 "emitAndFinalize called, but no mock implementation was provided");
254 return emitAndFinalizeImpl(H);
255 }
Lang Hamesc0056562015-10-20 04:35:02 +0000256};
257
Lang Hames4ce98662017-07-07 02:59:13 +0000258class ReturnNullJITSymbol {
259public:
260 template <typename... Args>
261 JITSymbol operator()(Args...) const {
262 return nullptr;
263 }
264};
265
Lang Hamesc0056562015-10-20 04:35:02 +0000266template <typename ReturnT>
267class DoNothingAndReturn {
268public:
Lang Hames4ce98662017-07-07 02:59:13 +0000269 DoNothingAndReturn(ReturnT Ret) : Ret(std::move(Ret)) {}
Lang Hamesc0056562015-10-20 04:35:02 +0000270
271 template <typename... Args>
Lang Hames4ce98662017-07-07 02:59:13 +0000272 void operator()(Args...) const { return Ret; }
Lang Hamesc0056562015-10-20 04:35:02 +0000273private:
Lang Hames4ce98662017-07-07 02:59:13 +0000274 ReturnT Ret;
Lang Hamesc0056562015-10-20 04:35:02 +0000275};
276
277template <>
278class DoNothingAndReturn<void> {
279public:
280 template <typename... Args>
281 void operator()(Args...) const { }
282};
Lang Hamesdc4260d2015-04-20 20:41:45 +0000283
284} // namespace llvm
285
286#endif