blob: 15d9f54a37fc59992394fad4098f016e49952314 [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 Hames130a7c42015-10-28 02:40:04 +000023#include "llvm/ExecutionEngine/ExecutionEngine.h"
Lang Hamesc0056562015-10-20 04:35:02 +000024#include "llvm/ExecutionEngine/Orc/JITSymbol.h"
Lang Hames130a7c42015-10-28 02:40:04 +000025#include "llvm/Support/TargetSelect.h"
Lang Hamesdc4260d2015-04-20 20:41:45 +000026#include <memory>
27
28namespace llvm {
29
Lang Hames130a7c42015-10-28 02:40:04 +000030// Base class for Orc tests that will execute code.
31class OrcExecutionTest {
32public:
33
34 OrcExecutionTest() {
35 if (!NativeTargetInitialized) {
36 InitializeNativeTarget();
37 InitializeNativeTargetAsmParser();
38 InitializeNativeTargetAsmPrinter();
39 NativeTargetInitialized = true;
40 }
Lang Hamesfd6e8dc2015-10-30 03:20:21 +000041
42 // Try to select a TargetMachine for the host.
43 TM.reset(EngineBuilder().selectTarget());
44
45 if (TM) {
46 // If we found a TargetMachine, check that it's one that Orc supports.
47 const Triple& TT = TM->getTargetTriple();
48 if (TT.getArch() != Triple::x86_64 || !TT.isOSDarwin())
49 TM = nullptr;
50 }
Lang Hames130a7c42015-10-28 02:40:04 +000051 };
52
Lang Hamesfd6e8dc2015-10-30 03:20:21 +000053protected:
54 std::unique_ptr<TargetMachine> TM;
Lang Hames130a7c42015-10-28 02:40:04 +000055private:
56 static bool NativeTargetInitialized;
57};
58
Lang Hames4a51e5d2015-10-27 17:45:48 +000059class ModuleBuilder {
60public:
61 ModuleBuilder(LLVMContext &Context, StringRef Triple,
62 StringRef Name);
Lang Hamesdc4260d2015-04-20 20:41:45 +000063
Lang Hames4a51e5d2015-10-27 17:45:48 +000064 template <typename FuncType>
Lang Hames130a7c42015-10-28 02:40:04 +000065 Function* createFunctionDecl(StringRef Name) {
Lang Hames4a51e5d2015-10-27 17:45:48 +000066 return Function::Create(
67 TypeBuilder<FuncType, false>::get(M->getContext()),
Lang Hames130a7c42015-10-28 02:40:04 +000068 GlobalValue::ExternalLinkage, Name, M.get());
Lang Hames4a51e5d2015-10-27 17:45:48 +000069 }
Lang Hamesdc4260d2015-04-20 20:41:45 +000070
Lang Hames4a51e5d2015-10-27 17:45:48 +000071 Module* getModule() { return M.get(); }
72 const Module* getModule() const { return M.get(); }
73 std::unique_ptr<Module> takeModule() { return std::move(M); }
Lang Hamesdc4260d2015-04-20 20:41:45 +000074
Lang Hames4a51e5d2015-10-27 17:45:48 +000075private:
76 std::unique_ptr<Module> M;
77 IRBuilder<> Builder;
78};
Lang Hamesdc4260d2015-04-20 20:41:45 +000079
Lang Hames4a51e5d2015-10-27 17:45:48 +000080// Dummy struct type.
81struct DummyStruct {
82 int X[256];
83};
Lang Hamesdc4260d2015-04-20 20:41:45 +000084
Lang Hames4a51e5d2015-10-27 17:45:48 +000085// TypeBuilder specialization for DummyStruct.
86template <bool XCompile>
87class TypeBuilder<DummyStruct, XCompile> {
88public:
89 static StructType *get(LLVMContext &Context) {
90 return StructType::get(
91 TypeBuilder<types::i<32>[256], XCompile>::get(Context), nullptr);
92 }
93};
Lang Hamesdc4260d2015-04-20 20:41:45 +000094
Lang Hamesc0056562015-10-20 04:35:02 +000095template <typename HandleT,
96 typename AddModuleSetFtor,
97 typename RemoveModuleSetFtor,
98 typename FindSymbolFtor,
99 typename FindSymbolInFtor>
100class MockBaseLayer {
101public:
102
103 typedef HandleT ModuleSetHandleT;
104
105 MockBaseLayer(AddModuleSetFtor &&AddModuleSet,
106 RemoveModuleSetFtor &&RemoveModuleSet,
107 FindSymbolFtor &&FindSymbol,
108 FindSymbolInFtor &&FindSymbolIn)
109 : AddModuleSet(AddModuleSet), RemoveModuleSet(RemoveModuleSet),
110 FindSymbol(FindSymbol), FindSymbolIn(FindSymbolIn)
111 {}
112
113 template <typename ModuleSetT, typename MemoryManagerPtrT,
114 typename SymbolResolverPtrT>
115 ModuleSetHandleT addModuleSet(ModuleSetT Ms, MemoryManagerPtrT MemMgr,
116 SymbolResolverPtrT Resolver) {
117 return AddModuleSet(std::move(Ms), std::move(MemMgr), std::move(Resolver));
118 }
119
120 void removeModuleSet(ModuleSetHandleT H) {
121 RemoveModuleSet(H);
122 }
123
124 orc::JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
125 return FindSymbol(Name, ExportedSymbolsOnly);
126 }
127
128 orc::JITSymbol findSymbolIn(ModuleSetHandleT H, const std::string &Name,
129 bool ExportedSymbolsOnly) {
130 return FindSymbolIn(H, Name, ExportedSymbolsOnly);
131 }
132
133private:
134 AddModuleSetFtor AddModuleSet;
135 RemoveModuleSetFtor RemoveModuleSet;
136 FindSymbolFtor FindSymbol;
137 FindSymbolInFtor FindSymbolIn;
138};
139
140template <typename ModuleSetHandleT,
141 typename AddModuleSetFtor,
142 typename RemoveModuleSetFtor,
143 typename FindSymbolFtor,
144 typename FindSymbolInFtor>
145MockBaseLayer<ModuleSetHandleT, AddModuleSetFtor, RemoveModuleSetFtor,
146 FindSymbolFtor, FindSymbolInFtor>
147createMockBaseLayer(AddModuleSetFtor &&AddModuleSet,
148 RemoveModuleSetFtor &&RemoveModuleSet,
149 FindSymbolFtor &&FindSymbol,
150 FindSymbolInFtor &&FindSymbolIn) {
151 return MockBaseLayer<ModuleSetHandleT, AddModuleSetFtor, RemoveModuleSetFtor,
152 FindSymbolFtor, FindSymbolInFtor>(
153 std::forward<AddModuleSetFtor>(AddModuleSet),
154 std::forward<RemoveModuleSetFtor>(RemoveModuleSet),
155 std::forward<FindSymbolFtor>(FindSymbol),
156 std::forward<FindSymbolInFtor>(FindSymbolIn));
157}
158
159template <typename ReturnT>
160class DoNothingAndReturn {
161public:
162 DoNothingAndReturn(ReturnT Val) : Val(Val) {}
163
164 template <typename... Args>
165 ReturnT operator()(Args...) const { return Val; }
166private:
167 ReturnT Val;
168};
169
170template <>
171class DoNothingAndReturn<void> {
172public:
173 template <typename... Args>
174 void operator()(Args...) const { }
175};
Lang Hamesdc4260d2015-04-20 20:41:45 +0000176
177} // namespace llvm
178
179#endif