blob: c6caaf07db0e765f289d90a81f5a79ec548f4bc8 [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.
47// (5) V -- A VSO associated with ES.
48class CoreAPIsBasedStandardTest : public testing::Test {
49public:
50protected:
51 ExecutionSession ES;
52 VSO &V = ES.createVSO("V");
53 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
88// Base class for Orc tests that will execute code.
89class OrcExecutionTest {
90public:
91
92 OrcExecutionTest() {
93
94 // Initialize the native target if it hasn't been done already.
95 OrcNativeTarget::initialize();
Lang Hamesfd6e8dc2015-10-30 03:20:21 +000096
97 // Try to select a TargetMachine for the host.
98 TM.reset(EngineBuilder().selectTarget());
99
100 if (TM) {
101 // If we found a TargetMachine, check that it's one that Orc supports.
102 const Triple& TT = TM->getTargetTriple();
Lang Hames4f8194e2016-02-10 01:02:33 +0000103
Lang Hamesec978e22018-03-28 14:47:11 +0000104 // Bail out for windows platforms. We do not support these yet.
Lang Hamesda5c6ac2018-03-28 15:58:14 +0000105 if ((TT.getArch() != Triple::x86_64 && TT.getArch() != Triple::x86) ||
106 TT.isOSWindows())
Lang Hamesec978e22018-03-28 14:47:11 +0000107 return;
108
Lang Hamesa95b0df2018-03-28 03:41:45 +0000109 // Target can JIT?
110 SupportsJIT = TM->getTarget().hasJIT();
111 // Use ability to create callback manager to detect whether Orc
112 // has indirection support on this platform. This way the test
113 // and Orc code do not get out of sync.
Lang Hamesbd0cb782018-05-30 01:57:45 +0000114 SupportsIndirection = !!orc::createLocalCompileCallbackManager(TT, ES, 0);
Lang Hamesfd6e8dc2015-10-30 03:20:21 +0000115 }
Lang Hames130a7c42015-10-28 02:40:04 +0000116 };
117
Lang Hamesfd6e8dc2015-10-30 03:20:21 +0000118protected:
Lang Hamesbd0cb782018-05-30 01:57:45 +0000119 orc::ExecutionSession ES;
Mehdi Amini03b42e42016-04-14 21:59:01 +0000120 LLVMContext Context;
Lang Hamesfd6e8dc2015-10-30 03:20:21 +0000121 std::unique_ptr<TargetMachine> TM;
Lang Hamesa95b0df2018-03-28 03:41:45 +0000122 bool SupportsJIT = false;
123 bool SupportsIndirection = false;
Lang Hames130a7c42015-10-28 02:40:04 +0000124};
125
Lang Hames4a51e5d2015-10-27 17:45:48 +0000126class ModuleBuilder {
127public:
128 ModuleBuilder(LLVMContext &Context, StringRef Triple,
129 StringRef Name);
Lang Hamesdc4260d2015-04-20 20:41:45 +0000130
Lang Hames4a51e5d2015-10-27 17:45:48 +0000131 template <typename FuncType>
Lang Hames130a7c42015-10-28 02:40:04 +0000132 Function* createFunctionDecl(StringRef Name) {
Lang Hames4a51e5d2015-10-27 17:45:48 +0000133 return Function::Create(
134 TypeBuilder<FuncType, false>::get(M->getContext()),
Lang Hames130a7c42015-10-28 02:40:04 +0000135 GlobalValue::ExternalLinkage, Name, M.get());
Lang Hames4a51e5d2015-10-27 17:45:48 +0000136 }
Lang Hamesdc4260d2015-04-20 20:41:45 +0000137
Lang Hames4a51e5d2015-10-27 17:45:48 +0000138 Module* getModule() { return M.get(); }
139 const Module* getModule() const { return M.get(); }
140 std::unique_ptr<Module> takeModule() { return std::move(M); }
Lang Hamesdc4260d2015-04-20 20:41:45 +0000141
Lang Hames4a51e5d2015-10-27 17:45:48 +0000142private:
143 std::unique_ptr<Module> M;
Lang Hames4a51e5d2015-10-27 17:45:48 +0000144};
Lang Hamesdc4260d2015-04-20 20:41:45 +0000145
Lang Hames4a51e5d2015-10-27 17:45:48 +0000146// Dummy struct type.
147struct DummyStruct {
148 int X[256];
149};
Lang Hamesdc4260d2015-04-20 20:41:45 +0000150
Lang Hames4a51e5d2015-10-27 17:45:48 +0000151// TypeBuilder specialization for DummyStruct.
152template <bool XCompile>
153class TypeBuilder<DummyStruct, XCompile> {
154public:
155 static StructType *get(LLVMContext &Context) {
156 return StructType::get(
Serge Gueltone38003f2017-05-09 19:31:13 +0000157 TypeBuilder<types::i<32>[256], XCompile>::get(Context));
Lang Hames4a51e5d2015-10-27 17:45:48 +0000158 }
159};
Lang Hamesdc4260d2015-04-20 20:41:45 +0000160
Lang Hamescf771ad2017-09-28 02:17:35 +0000161template <typename HandleT, typename ModuleT>
Lang Hamesc0056562015-10-20 04:35:02 +0000162class MockBaseLayer {
163public:
164
Lang Hamescf771ad2017-09-28 02:17:35 +0000165 using ModuleHandleT = HandleT;
Lang Hamesc0056562015-10-20 04:35:02 +0000166
Lang Hamescf771ad2017-09-28 02:17:35 +0000167 using AddModuleSignature =
168 Expected<ModuleHandleT>(ModuleT M,
169 std::shared_ptr<JITSymbolResolver> R);
Lang Hamesc0056562015-10-20 04:35:02 +0000170
Lang Hamescf771ad2017-09-28 02:17:35 +0000171 using RemoveModuleSignature = Error(ModuleHandleT H);
172 using FindSymbolSignature = JITSymbol(const std::string &Name,
173 bool ExportedSymbolsOnly);
174 using FindSymbolInSignature = JITSymbol(ModuleHandleT H,
175 const std::string &Name,
176 bool ExportedSymbolsONly);
177 using EmitAndFinalizeSignature = Error(ModuleHandleT H);
178
179 std::function<AddModuleSignature> addModuleImpl;
180 std::function<RemoveModuleSignature> removeModuleImpl;
181 std::function<FindSymbolSignature> findSymbolImpl;
182 std::function<FindSymbolInSignature> findSymbolInImpl;
183 std::function<EmitAndFinalizeSignature> emitAndFinalizeImpl;
184
185 Expected<ModuleHandleT> addModule(ModuleT M,
186 std::shared_ptr<JITSymbolResolver> R) {
187 assert(addModuleImpl &&
188 "addModule called, but no mock implementation was provided");
189 return addModuleImpl(std::move(M), std::move(R));
Lang Hamesc0056562015-10-20 04:35:02 +0000190 }
191
Lang Hames4ce98662017-07-07 02:59:13 +0000192 Error removeModule(ModuleHandleT H) {
Lang Hamescf771ad2017-09-28 02:17:35 +0000193 assert(removeModuleImpl &&
194 "removeModule called, but no mock implementation was provided");
195 return removeModuleImpl(H);
Lang Hamesc0056562015-10-20 04:35:02 +0000196 }
197
Lang Hamesad4a9112016-08-01 20:49:11 +0000198 JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
Lang Hamescf771ad2017-09-28 02:17:35 +0000199 assert(findSymbolImpl &&
200 "findSymbol called, but no mock implementation was provided");
201 return findSymbolImpl(Name, ExportedSymbolsOnly);
Lang Hamesc0056562015-10-20 04:35:02 +0000202 }
203
Lang Hamescd9d49b2017-06-23 23:25:28 +0000204 JITSymbol findSymbolIn(ModuleHandleT H, const std::string &Name,
Lang Hamesc0056562015-10-20 04:35:02 +0000205 bool ExportedSymbolsOnly) {
Lang Hamescf771ad2017-09-28 02:17:35 +0000206 assert(findSymbolInImpl &&
207 "findSymbolIn called, but no mock implementation was provided");
208 return findSymbolInImpl(H, Name, ExportedSymbolsOnly);
Lang Hamesc0056562015-10-20 04:35:02 +0000209 }
210
Lang Hamescf771ad2017-09-28 02:17:35 +0000211 Error emitAndFinaliez(ModuleHandleT H) {
212 assert(emitAndFinalizeImpl &&
213 "emitAndFinalize called, but no mock implementation was provided");
214 return emitAndFinalizeImpl(H);
215 }
Lang Hamesc0056562015-10-20 04:35:02 +0000216};
217
Lang Hames4ce98662017-07-07 02:59:13 +0000218class ReturnNullJITSymbol {
219public:
220 template <typename... Args>
221 JITSymbol operator()(Args...) const {
222 return nullptr;
223 }
224};
225
Lang Hamesc0056562015-10-20 04:35:02 +0000226template <typename ReturnT>
227class DoNothingAndReturn {
228public:
Lang Hames4ce98662017-07-07 02:59:13 +0000229 DoNothingAndReturn(ReturnT Ret) : Ret(std::move(Ret)) {}
Lang Hamesc0056562015-10-20 04:35:02 +0000230
231 template <typename... Args>
Lang Hames4ce98662017-07-07 02:59:13 +0000232 void operator()(Args...) const { return Ret; }
Lang Hamesc0056562015-10-20 04:35:02 +0000233private:
Lang Hames4ce98662017-07-07 02:59:13 +0000234 ReturnT Ret;
Lang Hamesc0056562015-10-20 04:35:02 +0000235};
236
237template <>
238class DoNothingAndReturn<void> {
239public:
240 template <typename... Args>
241 void operator()(Args...) const { }
242};
Lang Hamesdc4260d2015-04-20 20:41:45 +0000243
244} // namespace llvm
245
246#endif