Lang Hames | dc4260d | 2015-04-20 20:41:45 +0000 | [diff] [blame] | 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 | |
Chandler Carruth | 9a67b07 | 2017-06-06 11:06:56 +0000 | [diff] [blame] | 18 | #include "llvm/ExecutionEngine/ExecutionEngine.h" |
| 19 | #include "llvm/ExecutionEngine/JITSymbol.h" |
Lang Hames | a95b0df | 2018-03-28 03:41:45 +0000 | [diff] [blame] | 20 | #include "llvm/ExecutionEngine/Orc/IndirectionUtils.h" |
Lang Hames | dc4260d | 2015-04-20 20:41:45 +0000 | [diff] [blame] | 21 | #include "llvm/IR/Function.h" |
| 22 | #include "llvm/IR/IRBuilder.h" |
| 23 | #include "llvm/IR/LLVMContext.h" |
| 24 | #include "llvm/IR/Module.h" |
| 25 | #include "llvm/IR/TypeBuilder.h" |
Lang Hames | 859d73c | 2016-01-09 19:50:40 +0000 | [diff] [blame] | 26 | #include "llvm/Object/ObjectFile.h" |
Lang Hames | 130a7c4 | 2015-10-28 02:40:04 +0000 | [diff] [blame] | 27 | #include "llvm/Support/TargetSelect.h" |
Lang Hames | a95b0df | 2018-03-28 03:41:45 +0000 | [diff] [blame] | 28 | #include "llvm/Support/TargetRegistry.h" |
Lang Hames | dc4260d | 2015-04-20 20:41:45 +0000 | [diff] [blame] | 29 | #include <memory> |
| 30 | |
| 31 | namespace llvm { |
| 32 | |
Lang Hames | d22bade | 2017-04-04 17:03:49 +0000 | [diff] [blame] | 33 | class OrcNativeTarget { |
Lang Hames | 130a7c4 | 2015-10-28 02:40:04 +0000 | [diff] [blame] | 34 | public: |
Lang Hames | d22bade | 2017-04-04 17:03:49 +0000 | [diff] [blame] | 35 | static void initialize() { |
Lang Hames | 130a7c4 | 2015-10-28 02:40:04 +0000 | [diff] [blame] | 36 | if (!NativeTargetInitialized) { |
| 37 | InitializeNativeTarget(); |
| 38 | InitializeNativeTargetAsmParser(); |
| 39 | InitializeNativeTargetAsmPrinter(); |
| 40 | NativeTargetInitialized = true; |
| 41 | } |
Lang Hames | d22bade | 2017-04-04 17:03:49 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | private: |
| 45 | static bool NativeTargetInitialized; |
| 46 | }; |
| 47 | |
| 48 | // Base class for Orc tests that will execute code. |
| 49 | class OrcExecutionTest { |
| 50 | public: |
| 51 | |
| 52 | OrcExecutionTest() { |
| 53 | |
| 54 | // Initialize the native target if it hasn't been done already. |
| 55 | OrcNativeTarget::initialize(); |
Lang Hames | fd6e8dc | 2015-10-30 03:20:21 +0000 | [diff] [blame] | 56 | |
| 57 | // Try to select a TargetMachine for the host. |
| 58 | TM.reset(EngineBuilder().selectTarget()); |
| 59 | |
| 60 | if (TM) { |
| 61 | // If we found a TargetMachine, check that it's one that Orc supports. |
| 62 | const Triple& TT = TM->getTargetTriple(); |
Lang Hames | 4f8194e | 2016-02-10 01:02:33 +0000 | [diff] [blame] | 63 | |
Lang Hames | a95b0df | 2018-03-28 03:41:45 +0000 | [diff] [blame] | 64 | // Target can JIT? |
| 65 | SupportsJIT = TM->getTarget().hasJIT(); |
| 66 | // Use ability to create callback manager to detect whether Orc |
| 67 | // has indirection support on this platform. This way the test |
| 68 | // and Orc code do not get out of sync. |
| 69 | SupportsIndirection = !!orc::createLocalCompileCallbackManager(TT, 0); |
Lang Hames | fd6e8dc | 2015-10-30 03:20:21 +0000 | [diff] [blame] | 70 | } |
Lang Hames | 130a7c4 | 2015-10-28 02:40:04 +0000 | [diff] [blame] | 71 | }; |
| 72 | |
Lang Hames | fd6e8dc | 2015-10-30 03:20:21 +0000 | [diff] [blame] | 73 | protected: |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 74 | LLVMContext Context; |
Lang Hames | fd6e8dc | 2015-10-30 03:20:21 +0000 | [diff] [blame] | 75 | std::unique_ptr<TargetMachine> TM; |
Lang Hames | a95b0df | 2018-03-28 03:41:45 +0000 | [diff] [blame] | 76 | bool SupportsJIT = false; |
| 77 | bool SupportsIndirection = false; |
Lang Hames | 130a7c4 | 2015-10-28 02:40:04 +0000 | [diff] [blame] | 78 | }; |
| 79 | |
Lang Hames | 4a51e5d | 2015-10-27 17:45:48 +0000 | [diff] [blame] | 80 | class ModuleBuilder { |
| 81 | public: |
| 82 | ModuleBuilder(LLVMContext &Context, StringRef Triple, |
| 83 | StringRef Name); |
Lang Hames | dc4260d | 2015-04-20 20:41:45 +0000 | [diff] [blame] | 84 | |
Lang Hames | 4a51e5d | 2015-10-27 17:45:48 +0000 | [diff] [blame] | 85 | template <typename FuncType> |
Lang Hames | 130a7c4 | 2015-10-28 02:40:04 +0000 | [diff] [blame] | 86 | Function* createFunctionDecl(StringRef Name) { |
Lang Hames | 4a51e5d | 2015-10-27 17:45:48 +0000 | [diff] [blame] | 87 | return Function::Create( |
| 88 | TypeBuilder<FuncType, false>::get(M->getContext()), |
Lang Hames | 130a7c4 | 2015-10-28 02:40:04 +0000 | [diff] [blame] | 89 | GlobalValue::ExternalLinkage, Name, M.get()); |
Lang Hames | 4a51e5d | 2015-10-27 17:45:48 +0000 | [diff] [blame] | 90 | } |
Lang Hames | dc4260d | 2015-04-20 20:41:45 +0000 | [diff] [blame] | 91 | |
Lang Hames | 4a51e5d | 2015-10-27 17:45:48 +0000 | [diff] [blame] | 92 | Module* getModule() { return M.get(); } |
| 93 | const Module* getModule() const { return M.get(); } |
| 94 | std::unique_ptr<Module> takeModule() { return std::move(M); } |
Lang Hames | dc4260d | 2015-04-20 20:41:45 +0000 | [diff] [blame] | 95 | |
Lang Hames | 4a51e5d | 2015-10-27 17:45:48 +0000 | [diff] [blame] | 96 | private: |
| 97 | std::unique_ptr<Module> M; |
Lang Hames | 4a51e5d | 2015-10-27 17:45:48 +0000 | [diff] [blame] | 98 | }; |
Lang Hames | dc4260d | 2015-04-20 20:41:45 +0000 | [diff] [blame] | 99 | |
Lang Hames | 4a51e5d | 2015-10-27 17:45:48 +0000 | [diff] [blame] | 100 | // Dummy struct type. |
| 101 | struct DummyStruct { |
| 102 | int X[256]; |
| 103 | }; |
Lang Hames | dc4260d | 2015-04-20 20:41:45 +0000 | [diff] [blame] | 104 | |
Lang Hames | 4a51e5d | 2015-10-27 17:45:48 +0000 | [diff] [blame] | 105 | // TypeBuilder specialization for DummyStruct. |
| 106 | template <bool XCompile> |
| 107 | class TypeBuilder<DummyStruct, XCompile> { |
| 108 | public: |
| 109 | static StructType *get(LLVMContext &Context) { |
| 110 | return StructType::get( |
Serge Guelton | e38003f | 2017-05-09 19:31:13 +0000 | [diff] [blame] | 111 | TypeBuilder<types::i<32>[256], XCompile>::get(Context)); |
Lang Hames | 4a51e5d | 2015-10-27 17:45:48 +0000 | [diff] [blame] | 112 | } |
| 113 | }; |
Lang Hames | dc4260d | 2015-04-20 20:41:45 +0000 | [diff] [blame] | 114 | |
Lang Hames | cf771ad | 2017-09-28 02:17:35 +0000 | [diff] [blame] | 115 | template <typename HandleT, typename ModuleT> |
Lang Hames | c005656 | 2015-10-20 04:35:02 +0000 | [diff] [blame] | 116 | class MockBaseLayer { |
| 117 | public: |
| 118 | |
Lang Hames | cf771ad | 2017-09-28 02:17:35 +0000 | [diff] [blame] | 119 | using ModuleHandleT = HandleT; |
Lang Hames | c005656 | 2015-10-20 04:35:02 +0000 | [diff] [blame] | 120 | |
Lang Hames | cf771ad | 2017-09-28 02:17:35 +0000 | [diff] [blame] | 121 | using AddModuleSignature = |
| 122 | Expected<ModuleHandleT>(ModuleT M, |
| 123 | std::shared_ptr<JITSymbolResolver> R); |
Lang Hames | c005656 | 2015-10-20 04:35:02 +0000 | [diff] [blame] | 124 | |
Lang Hames | cf771ad | 2017-09-28 02:17:35 +0000 | [diff] [blame] | 125 | using RemoveModuleSignature = Error(ModuleHandleT H); |
| 126 | using FindSymbolSignature = JITSymbol(const std::string &Name, |
| 127 | bool ExportedSymbolsOnly); |
| 128 | using FindSymbolInSignature = JITSymbol(ModuleHandleT H, |
| 129 | const std::string &Name, |
| 130 | bool ExportedSymbolsONly); |
| 131 | using EmitAndFinalizeSignature = Error(ModuleHandleT H); |
| 132 | |
| 133 | std::function<AddModuleSignature> addModuleImpl; |
| 134 | std::function<RemoveModuleSignature> removeModuleImpl; |
| 135 | std::function<FindSymbolSignature> findSymbolImpl; |
| 136 | std::function<FindSymbolInSignature> findSymbolInImpl; |
| 137 | std::function<EmitAndFinalizeSignature> emitAndFinalizeImpl; |
| 138 | |
| 139 | Expected<ModuleHandleT> addModule(ModuleT M, |
| 140 | std::shared_ptr<JITSymbolResolver> R) { |
| 141 | assert(addModuleImpl && |
| 142 | "addModule called, but no mock implementation was provided"); |
| 143 | return addModuleImpl(std::move(M), std::move(R)); |
Lang Hames | c005656 | 2015-10-20 04:35:02 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Lang Hames | 4ce9866 | 2017-07-07 02:59:13 +0000 | [diff] [blame] | 146 | Error removeModule(ModuleHandleT H) { |
Lang Hames | cf771ad | 2017-09-28 02:17:35 +0000 | [diff] [blame] | 147 | assert(removeModuleImpl && |
| 148 | "removeModule called, but no mock implementation was provided"); |
| 149 | return removeModuleImpl(H); |
Lang Hames | c005656 | 2015-10-20 04:35:02 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Lang Hames | ad4a911 | 2016-08-01 20:49:11 +0000 | [diff] [blame] | 152 | JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) { |
Lang Hames | cf771ad | 2017-09-28 02:17:35 +0000 | [diff] [blame] | 153 | assert(findSymbolImpl && |
| 154 | "findSymbol called, but no mock implementation was provided"); |
| 155 | return findSymbolImpl(Name, ExportedSymbolsOnly); |
Lang Hames | c005656 | 2015-10-20 04:35:02 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Lang Hames | cd9d49b | 2017-06-23 23:25:28 +0000 | [diff] [blame] | 158 | JITSymbol findSymbolIn(ModuleHandleT H, const std::string &Name, |
Lang Hames | c005656 | 2015-10-20 04:35:02 +0000 | [diff] [blame] | 159 | bool ExportedSymbolsOnly) { |
Lang Hames | cf771ad | 2017-09-28 02:17:35 +0000 | [diff] [blame] | 160 | assert(findSymbolInImpl && |
| 161 | "findSymbolIn called, but no mock implementation was provided"); |
| 162 | return findSymbolInImpl(H, Name, ExportedSymbolsOnly); |
Lang Hames | c005656 | 2015-10-20 04:35:02 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Lang Hames | cf771ad | 2017-09-28 02:17:35 +0000 | [diff] [blame] | 165 | Error emitAndFinaliez(ModuleHandleT H) { |
| 166 | assert(emitAndFinalizeImpl && |
| 167 | "emitAndFinalize called, but no mock implementation was provided"); |
| 168 | return emitAndFinalizeImpl(H); |
| 169 | } |
Lang Hames | c005656 | 2015-10-20 04:35:02 +0000 | [diff] [blame] | 170 | }; |
| 171 | |
Lang Hames | 4ce9866 | 2017-07-07 02:59:13 +0000 | [diff] [blame] | 172 | class ReturnNullJITSymbol { |
| 173 | public: |
| 174 | template <typename... Args> |
| 175 | JITSymbol operator()(Args...) const { |
| 176 | return nullptr; |
| 177 | } |
| 178 | }; |
| 179 | |
Lang Hames | c005656 | 2015-10-20 04:35:02 +0000 | [diff] [blame] | 180 | template <typename ReturnT> |
| 181 | class DoNothingAndReturn { |
| 182 | public: |
Lang Hames | 4ce9866 | 2017-07-07 02:59:13 +0000 | [diff] [blame] | 183 | DoNothingAndReturn(ReturnT Ret) : Ret(std::move(Ret)) {} |
Lang Hames | c005656 | 2015-10-20 04:35:02 +0000 | [diff] [blame] | 184 | |
| 185 | template <typename... Args> |
Lang Hames | 4ce9866 | 2017-07-07 02:59:13 +0000 | [diff] [blame] | 186 | void operator()(Args...) const { return Ret; } |
Lang Hames | c005656 | 2015-10-20 04:35:02 +0000 | [diff] [blame] | 187 | private: |
Lang Hames | 4ce9866 | 2017-07-07 02:59:13 +0000 | [diff] [blame] | 188 | ReturnT Ret; |
Lang Hames | c005656 | 2015-10-20 04:35:02 +0000 | [diff] [blame] | 189 | }; |
| 190 | |
| 191 | template <> |
| 192 | class DoNothingAndReturn<void> { |
| 193 | public: |
| 194 | template <typename... Args> |
| 195 | void operator()(Args...) const { } |
| 196 | }; |
Lang Hames | dc4260d | 2015-04-20 20:41:45 +0000 | [diff] [blame] | 197 | |
| 198 | } // namespace llvm |
| 199 | |
| 200 | #endif |