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/Object/ObjectFile.h"
24 #include "llvm/ExecutionEngine/ExecutionEngine.h"
25 #include "llvm/ExecutionEngine/Orc/JITSymbol.h"
26 #include "llvm/Support/TargetSelect.h"
31 // Base class for Orc tests that will execute code.
32 class OrcExecutionTest {
36 if (!NativeTargetInitialized) {
37 InitializeNativeTarget();
38 InitializeNativeTargetAsmParser();
39 InitializeNativeTargetAsmPrinter();
40 NativeTargetInitialized = true;
43 // Try to select a TargetMachine for the host.
44 TM.reset(EngineBuilder().selectTarget());
47 // If we found a TargetMachine, check that it's one that Orc supports.
48 const Triple& TT = TM->getTargetTriple();
49 if (TT.getArch() != Triple::x86_64 || !TT.isOSDarwin())
55 std::unique_ptr<TargetMachine> TM;
57 static bool NativeTargetInitialized;
62 ModuleBuilder(LLVMContext &Context, StringRef Triple,
65 template <typename FuncType>
66 Function* createFunctionDecl(StringRef Name) {
67 return Function::Create(
68 TypeBuilder<FuncType, false>::get(M->getContext()),
69 GlobalValue::ExternalLinkage, Name, M.get());
72 Module* getModule() { return M.get(); }
73 const Module* getModule() const { return M.get(); }
74 std::unique_ptr<Module> takeModule() { return std::move(M); }
77 std::unique_ptr<Module> M;
85 // TypeBuilder specialization for DummyStruct.
86 template <bool XCompile>
87 class TypeBuilder<DummyStruct, XCompile> {
89 static StructType *get(LLVMContext &Context) {
90 return StructType::get(
91 TypeBuilder<types::i<32>[256], XCompile>::get(Context), nullptr);
95 template <typename HandleT,
96 typename AddModuleSetFtor,
97 typename RemoveModuleSetFtor,
98 typename FindSymbolFtor,
99 typename FindSymbolInFtor>
100 class MockBaseLayer {
103 typedef HandleT ModuleSetHandleT;
105 MockBaseLayer(AddModuleSetFtor &&AddModuleSet,
106 RemoveModuleSetFtor &&RemoveModuleSet,
107 FindSymbolFtor &&FindSymbol,
108 FindSymbolInFtor &&FindSymbolIn)
109 : AddModuleSet(AddModuleSet), RemoveModuleSet(RemoveModuleSet),
110 FindSymbol(FindSymbol), FindSymbolIn(FindSymbolIn)
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));
120 void removeModuleSet(ModuleSetHandleT H) {
124 orc::JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
125 return FindSymbol(Name, ExportedSymbolsOnly);
128 orc::JITSymbol findSymbolIn(ModuleSetHandleT H, const std::string &Name,
129 bool ExportedSymbolsOnly) {
130 return FindSymbolIn(H, Name, ExportedSymbolsOnly);
134 AddModuleSetFtor AddModuleSet;
135 RemoveModuleSetFtor RemoveModuleSet;
136 FindSymbolFtor FindSymbol;
137 FindSymbolInFtor FindSymbolIn;
140 template <typename ModuleSetHandleT,
141 typename AddModuleSetFtor,
142 typename RemoveModuleSetFtor,
143 typename FindSymbolFtor,
144 typename FindSymbolInFtor>
145 MockBaseLayer<ModuleSetHandleT, AddModuleSetFtor, RemoveModuleSetFtor,
146 FindSymbolFtor, FindSymbolInFtor>
147 createMockBaseLayer(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));
159 template <typename ReturnT>
160 class DoNothingAndReturn {
162 DoNothingAndReturn(ReturnT Val) : Val(Val) {}
164 template <typename... Args>
165 ReturnT operator()(Args...) const { return Val; }
171 class DoNothingAndReturn<void> {
173 template <typename... Args>
174 void operator()(Args...) const { }