1 //===------ OrcTestCommon.h - Utilities for Orc Unit Tests ------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Common utilities for the Orc unit tests.
12 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_UNITTESTS_EXECUTIONENGINE_ORC_ORCTESTCOMMON_H
16 #define LLVM_UNITTESTS_EXECUTIONENGINE_ORC_ORCTESTCOMMON_H
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"
23 #include "llvm/ExecutionEngine/ExecutionEngine.h"
24 #include "llvm/ExecutionEngine/Orc/JITSymbol.h"
25 #include "llvm/Support/TargetSelect.h"
30 // Base class for Orc tests that will execute code.
31 class OrcExecutionTest {
35 if (!NativeTargetInitialized) {
36 InitializeNativeTarget();
37 InitializeNativeTargetAsmParser();
38 InitializeNativeTargetAsmPrinter();
39 NativeTargetInitialized = true;
43 // Get a target machine for the host if it supports JIT execution.
44 std::unique_ptr<TargetMachine> getHostTargetMachineIfSupported() {
45 std::unique_ptr<TargetMachine> TM(EngineBuilder().selectTarget());
50 const Triple& TT = TM->getTargetTriple();
52 if (TT.getArch() != Triple::x86_64 || !TT.isOSDarwin())
59 static bool NativeTargetInitialized;
64 ModuleBuilder(LLVMContext &Context, StringRef Triple,
67 template <typename FuncType>
68 Function* createFunctionDecl(StringRef Name) {
69 return Function::Create(
70 TypeBuilder<FuncType, false>::get(M->getContext()),
71 GlobalValue::ExternalLinkage, Name, M.get());
74 Module* getModule() { return M.get(); }
75 const Module* getModule() const { return M.get(); }
76 std::unique_ptr<Module> takeModule() { return std::move(M); }
79 std::unique_ptr<Module> M;
88 // TypeBuilder specialization for DummyStruct.
89 template <bool XCompile>
90 class TypeBuilder<DummyStruct, XCompile> {
92 static StructType *get(LLVMContext &Context) {
93 return StructType::get(
94 TypeBuilder<types::i<32>[256], XCompile>::get(Context), nullptr);
98 template <typename HandleT,
99 typename AddModuleSetFtor,
100 typename RemoveModuleSetFtor,
101 typename FindSymbolFtor,
102 typename FindSymbolInFtor>
103 class MockBaseLayer {
106 typedef HandleT ModuleSetHandleT;
108 MockBaseLayer(AddModuleSetFtor &&AddModuleSet,
109 RemoveModuleSetFtor &&RemoveModuleSet,
110 FindSymbolFtor &&FindSymbol,
111 FindSymbolInFtor &&FindSymbolIn)
112 : AddModuleSet(AddModuleSet), RemoveModuleSet(RemoveModuleSet),
113 FindSymbol(FindSymbol), FindSymbolIn(FindSymbolIn)
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));
123 void removeModuleSet(ModuleSetHandleT H) {
127 orc::JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
128 return FindSymbol(Name, ExportedSymbolsOnly);
131 orc::JITSymbol findSymbolIn(ModuleSetHandleT H, const std::string &Name,
132 bool ExportedSymbolsOnly) {
133 return FindSymbolIn(H, Name, ExportedSymbolsOnly);
137 AddModuleSetFtor AddModuleSet;
138 RemoveModuleSetFtor RemoveModuleSet;
139 FindSymbolFtor FindSymbol;
140 FindSymbolInFtor FindSymbolIn;
143 template <typename ModuleSetHandleT,
144 typename AddModuleSetFtor,
145 typename RemoveModuleSetFtor,
146 typename FindSymbolFtor,
147 typename FindSymbolInFtor>
148 MockBaseLayer<ModuleSetHandleT, AddModuleSetFtor, RemoveModuleSetFtor,
149 FindSymbolFtor, FindSymbolInFtor>
150 createMockBaseLayer(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));
162 template <typename ReturnT>
163 class DoNothingAndReturn {
165 DoNothingAndReturn(ReturnT Val) : Val(Val) {}
167 template <typename... Args>
168 ReturnT operator()(Args...) const { return Val; }
174 class DoNothingAndReturn<void> {
176 template <typename... Args>
177 void operator()(Args...) const { }