1 //===------ 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" 23 #include "llvm/ExecutionEngine/ExecutionEngine.h" 24 #include "llvm/ExecutionEngine/Orc/JITSymbol.h" 25 #include "llvm/Support/TargetSelect.h" 26 #include <memory> 27 28 namespace llvm { 29 30 // Base class for Orc tests that will execute code. 31 class OrcExecutionTest { 32 public: 33 34 OrcExecutionTest() { 35 if (!NativeTargetInitialized) { 36 InitializeNativeTarget(); 37 InitializeNativeTargetAsmParser(); 38 InitializeNativeTargetAsmPrinter(); 39 NativeTargetInitialized = true; 40 } 41 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 } 51 }; 52 53 protected: 54 std::unique_ptr<TargetMachine> TM; 55 private: 56 static bool NativeTargetInitialized; 57 }; 58 59 class ModuleBuilder { 60 public: 61 ModuleBuilder(LLVMContext &Context, StringRef Triple, 62 StringRef Name); 63 64 template <typename FuncType> 65 Function* createFunctionDecl(StringRef Name) { 66 return Function::Create( 67 TypeBuilder<FuncType, false>::get(M->getContext()), 68 GlobalValue::ExternalLinkage, Name, M.get()); 69 } 70 71 Module* getModule() { return M.get(); } 72 const Module* getModule() const { return M.get(); } 73 std::unique_ptr<Module> takeModule() { return std::move(M); } 74 75 private: 76 std::unique_ptr<Module> M; 77 IRBuilder<> Builder; 78 }; 79 80 // Dummy struct type. 81 struct DummyStruct { 82 int X[256]; 83 }; 84 85 // TypeBuilder specialization for DummyStruct. 86 template <bool XCompile> 87 class TypeBuilder<DummyStruct, XCompile> { 88 public: 89 static StructType *get(LLVMContext &Context) { 90 return StructType::get( 91 TypeBuilder<types::i<32>[256], XCompile>::get(Context), nullptr); 92 } 93 }; 94 95 template <typename HandleT, 96 typename AddModuleSetFtor, 97 typename RemoveModuleSetFtor, 98 typename FindSymbolFtor, 99 typename FindSymbolInFtor> 100 class MockBaseLayer { 101 public: 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 133 private: 134 AddModuleSetFtor AddModuleSet; 135 RemoveModuleSetFtor RemoveModuleSet; 136 FindSymbolFtor FindSymbol; 137 FindSymbolInFtor FindSymbolIn; 138 }; 139 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)); 157 } 158 159 template <typename ReturnT> 160 class DoNothingAndReturn { 161 public: 162 DoNothingAndReturn(ReturnT Val) : Val(Val) {} 163 164 template <typename... Args> 165 ReturnT operator()(Args...) const { return Val; } 166 private: 167 ReturnT Val; 168 }; 169 170 template <> 171 class DoNothingAndReturn<void> { 172 public: 173 template <typename... Args> 174 void operator()(Args...) const { } 175 }; 176 177 } // namespace llvm 178 179 #endif 180