blob: fe3da88dc9d1f8f4aa2b2630cfe431472bd2d083 [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
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"
Lang Hames859d73c2016-01-09 19:50:40 +000023#include "llvm/Object/ObjectFile.h"
Lang Hames130a7c42015-10-28 02:40:04 +000024#include "llvm/ExecutionEngine/ExecutionEngine.h"
Lang Hamesc0056562015-10-20 04:35:02 +000025#include "llvm/ExecutionEngine/Orc/JITSymbol.h"
Lang Hames130a7c42015-10-28 02:40:04 +000026#include "llvm/Support/TargetSelect.h"
Lang Hamesdc4260d2015-04-20 20:41:45 +000027#include <memory>
28
29namespace llvm {
30
Lang Hames130a7c42015-10-28 02:40:04 +000031// Base class for Orc tests that will execute code.
32class OrcExecutionTest {
33public:
34
35 OrcExecutionTest() {
36 if (!NativeTargetInitialized) {
37 InitializeNativeTarget();
38 InitializeNativeTargetAsmParser();
39 InitializeNativeTargetAsmPrinter();
40 NativeTargetInitialized = true;
41 }
Lang Hamesfd6e8dc2015-10-30 03:20:21 +000042
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();
Lang Hames4f8194e2016-02-10 01:02:33 +000049
50 if ((TT.getArch() != Triple::x86_64 && TT.getArch() != Triple::x86) ||
51 TT.isOSWindows())
Lang Hamesfd6e8dc2015-10-30 03:20:21 +000052 TM = nullptr;
53 }
Lang Hames130a7c42015-10-28 02:40:04 +000054 };
55
Lang Hamesfd6e8dc2015-10-30 03:20:21 +000056protected:
Mehdi Amini03b42e42016-04-14 21:59:01 +000057 LLVMContext Context;
Lang Hamesfd6e8dc2015-10-30 03:20:21 +000058 std::unique_ptr<TargetMachine> TM;
Lang Hames130a7c42015-10-28 02:40:04 +000059private:
60 static bool NativeTargetInitialized;
61};
62
Lang Hames4a51e5d2015-10-27 17:45:48 +000063class ModuleBuilder {
64public:
65 ModuleBuilder(LLVMContext &Context, StringRef Triple,
66 StringRef Name);
Lang Hamesdc4260d2015-04-20 20:41:45 +000067
Lang Hames4a51e5d2015-10-27 17:45:48 +000068 template <typename FuncType>
Lang Hames130a7c42015-10-28 02:40:04 +000069 Function* createFunctionDecl(StringRef Name) {
Lang Hames4a51e5d2015-10-27 17:45:48 +000070 return Function::Create(
71 TypeBuilder<FuncType, false>::get(M->getContext()),
Lang Hames130a7c42015-10-28 02:40:04 +000072 GlobalValue::ExternalLinkage, Name, M.get());
Lang Hames4a51e5d2015-10-27 17:45:48 +000073 }
Lang Hamesdc4260d2015-04-20 20:41:45 +000074
Lang Hames4a51e5d2015-10-27 17:45:48 +000075 Module* getModule() { return M.get(); }
76 const Module* getModule() const { return M.get(); }
77 std::unique_ptr<Module> takeModule() { return std::move(M); }
Lang Hamesdc4260d2015-04-20 20:41:45 +000078
Lang Hames4a51e5d2015-10-27 17:45:48 +000079private:
80 std::unique_ptr<Module> M;
Lang Hames4a51e5d2015-10-27 17:45:48 +000081};
Lang Hamesdc4260d2015-04-20 20:41:45 +000082
Lang Hames4a51e5d2015-10-27 17:45:48 +000083// Dummy struct type.
84struct DummyStruct {
85 int X[256];
86};
Lang Hamesdc4260d2015-04-20 20:41:45 +000087
Lang Hames4a51e5d2015-10-27 17:45:48 +000088// TypeBuilder specialization for DummyStruct.
89template <bool XCompile>
90class TypeBuilder<DummyStruct, XCompile> {
91public:
92 static StructType *get(LLVMContext &Context) {
93 return StructType::get(
94 TypeBuilder<types::i<32>[256], XCompile>::get(Context), nullptr);
95 }
96};
Lang Hamesdc4260d2015-04-20 20:41:45 +000097
Lang Hamesc0056562015-10-20 04:35:02 +000098template <typename HandleT,
99 typename AddModuleSetFtor,
100 typename RemoveModuleSetFtor,
101 typename FindSymbolFtor,
102 typename FindSymbolInFtor>
103class MockBaseLayer {
104public:
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
136private:
137 AddModuleSetFtor AddModuleSet;
138 RemoveModuleSetFtor RemoveModuleSet;
139 FindSymbolFtor FindSymbol;
140 FindSymbolInFtor FindSymbolIn;
141};
142
143template <typename ModuleSetHandleT,
144 typename AddModuleSetFtor,
145 typename RemoveModuleSetFtor,
146 typename FindSymbolFtor,
147 typename FindSymbolInFtor>
148MockBaseLayer<ModuleSetHandleT, AddModuleSetFtor, RemoveModuleSetFtor,
149 FindSymbolFtor, FindSymbolInFtor>
150createMockBaseLayer(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
162template <typename ReturnT>
163class DoNothingAndReturn {
164public:
165 DoNothingAndReturn(ReturnT Val) : Val(Val) {}
166
167 template <typename... Args>
168 ReturnT operator()(Args...) const { return Val; }
169private:
170 ReturnT Val;
171};
172
173template <>
174class DoNothingAndReturn<void> {
175public:
176 template <typename... Args>
177 void operator()(Args...) const { }
178};
Lang Hamesdc4260d2015-04-20 20:41:45 +0000179
180} // namespace llvm
181
182#endif