blob: e8fab56156497cfa171757cfdadc293d64142511 [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:
57 std::unique_ptr<TargetMachine> TM;
Lang Hames130a7c42015-10-28 02:40:04 +000058private:
59 static bool NativeTargetInitialized;
60};
61
Lang Hames4a51e5d2015-10-27 17:45:48 +000062class ModuleBuilder {
63public:
64 ModuleBuilder(LLVMContext &Context, StringRef Triple,
65 StringRef Name);
Lang Hamesdc4260d2015-04-20 20:41:45 +000066
Lang Hames4a51e5d2015-10-27 17:45:48 +000067 template <typename FuncType>
Lang Hames130a7c42015-10-28 02:40:04 +000068 Function* createFunctionDecl(StringRef Name) {
Lang Hames4a51e5d2015-10-27 17:45:48 +000069 return Function::Create(
70 TypeBuilder<FuncType, false>::get(M->getContext()),
Lang Hames130a7c42015-10-28 02:40:04 +000071 GlobalValue::ExternalLinkage, Name, M.get());
Lang Hames4a51e5d2015-10-27 17:45:48 +000072 }
Lang Hamesdc4260d2015-04-20 20:41:45 +000073
Lang Hames4a51e5d2015-10-27 17:45:48 +000074 Module* getModule() { return M.get(); }
75 const Module* getModule() const { return M.get(); }
76 std::unique_ptr<Module> takeModule() { return std::move(M); }
Lang Hamesdc4260d2015-04-20 20:41:45 +000077
Lang Hames4a51e5d2015-10-27 17:45:48 +000078private:
79 std::unique_ptr<Module> M;
Lang Hames4a51e5d2015-10-27 17:45:48 +000080};
Lang Hamesdc4260d2015-04-20 20:41:45 +000081
Lang Hames4a51e5d2015-10-27 17:45:48 +000082// Dummy struct type.
83struct DummyStruct {
84 int X[256];
85};
Lang Hamesdc4260d2015-04-20 20:41:45 +000086
Lang Hames4a51e5d2015-10-27 17:45:48 +000087// TypeBuilder specialization for DummyStruct.
88template <bool XCompile>
89class TypeBuilder<DummyStruct, XCompile> {
90public:
91 static StructType *get(LLVMContext &Context) {
92 return StructType::get(
93 TypeBuilder<types::i<32>[256], XCompile>::get(Context), nullptr);
94 }
95};
Lang Hamesdc4260d2015-04-20 20:41:45 +000096
Lang Hamesc0056562015-10-20 04:35:02 +000097template <typename HandleT,
98 typename AddModuleSetFtor,
99 typename RemoveModuleSetFtor,
100 typename FindSymbolFtor,
101 typename FindSymbolInFtor>
102class MockBaseLayer {
103public:
104
105 typedef HandleT ModuleSetHandleT;
106
107 MockBaseLayer(AddModuleSetFtor &&AddModuleSet,
108 RemoveModuleSetFtor &&RemoveModuleSet,
109 FindSymbolFtor &&FindSymbol,
110 FindSymbolInFtor &&FindSymbolIn)
111 : AddModuleSet(AddModuleSet), RemoveModuleSet(RemoveModuleSet),
112 FindSymbol(FindSymbol), FindSymbolIn(FindSymbolIn)
113 {}
114
115 template <typename ModuleSetT, typename MemoryManagerPtrT,
116 typename SymbolResolverPtrT>
117 ModuleSetHandleT addModuleSet(ModuleSetT Ms, MemoryManagerPtrT MemMgr,
118 SymbolResolverPtrT Resolver) {
119 return AddModuleSet(std::move(Ms), std::move(MemMgr), std::move(Resolver));
120 }
121
122 void removeModuleSet(ModuleSetHandleT H) {
123 RemoveModuleSet(H);
124 }
125
126 orc::JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
127 return FindSymbol(Name, ExportedSymbolsOnly);
128 }
129
130 orc::JITSymbol findSymbolIn(ModuleSetHandleT H, const std::string &Name,
131 bool ExportedSymbolsOnly) {
132 return FindSymbolIn(H, Name, ExportedSymbolsOnly);
133 }
134
135private:
136 AddModuleSetFtor AddModuleSet;
137 RemoveModuleSetFtor RemoveModuleSet;
138 FindSymbolFtor FindSymbol;
139 FindSymbolInFtor FindSymbolIn;
140};
141
142template <typename ModuleSetHandleT,
143 typename AddModuleSetFtor,
144 typename RemoveModuleSetFtor,
145 typename FindSymbolFtor,
146 typename FindSymbolInFtor>
147MockBaseLayer<ModuleSetHandleT, AddModuleSetFtor, RemoveModuleSetFtor,
148 FindSymbolFtor, FindSymbolInFtor>
149createMockBaseLayer(AddModuleSetFtor &&AddModuleSet,
150 RemoveModuleSetFtor &&RemoveModuleSet,
151 FindSymbolFtor &&FindSymbol,
152 FindSymbolInFtor &&FindSymbolIn) {
153 return MockBaseLayer<ModuleSetHandleT, AddModuleSetFtor, RemoveModuleSetFtor,
154 FindSymbolFtor, FindSymbolInFtor>(
155 std::forward<AddModuleSetFtor>(AddModuleSet),
156 std::forward<RemoveModuleSetFtor>(RemoveModuleSet),
157 std::forward<FindSymbolFtor>(FindSymbol),
158 std::forward<FindSymbolInFtor>(FindSymbolIn));
159}
160
161template <typename ReturnT>
162class DoNothingAndReturn {
163public:
164 DoNothingAndReturn(ReturnT Val) : Val(Val) {}
165
166 template <typename... Args>
167 ReturnT operator()(Args...) const { return Val; }
168private:
169 ReturnT Val;
170};
171
172template <>
173class DoNothingAndReturn<void> {
174public:
175 template <typename... Args>
176 void operator()(Args...) const { }
177};
Lang Hamesdc4260d2015-04-20 20:41:45 +0000178
179} // namespace llvm
180
181#endif