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 | a95b0df | 2018-03-28 03:41:45 +0000 | [diff] [blame] | 27 | #include "llvm/Support/TargetRegistry.h" |
Lang Hames | fd0c1e71 | 2018-07-20 18:31:50 +0000 | [diff] [blame] | 28 | #include "llvm/Support/TargetSelect.h" |
| 29 | #include "gtest/gtest.h" |
| 30 | |
Lang Hames | dc4260d | 2015-04-20 20:41:45 +0000 | [diff] [blame] | 31 | #include <memory> |
| 32 | |
| 33 | namespace llvm { |
| 34 | |
Lang Hames | fd0c1e71 | 2018-07-20 18:31:50 +0000 | [diff] [blame] | 35 | namespace orc { |
| 36 | // CoreAPIsStandardTest that saves a bunch of boilerplate by providing the |
| 37 | // following: |
| 38 | // |
| 39 | // (1) ES -- An ExecutionSession |
| 40 | // (2) Foo, Bar, Baz, Qux -- SymbolStringPtrs for strings "foo", "bar", "baz", |
| 41 | // and "qux" respectively. |
| 42 | // (3) FooAddr, BarAddr, BazAddr, QuxAddr -- Dummy addresses. Guaranteed |
| 43 | // distinct and non-null. |
| 44 | // (4) FooSym, BarSym, BazSym, QuxSym -- JITEvaluatedSymbols with FooAddr, |
| 45 | // BarAddr, BazAddr, and QuxAddr respectively. All with default strong, |
| 46 | // linkage and non-hidden visibility. |
| 47 | // (5) V -- A VSO associated with ES. |
| 48 | class CoreAPIsBasedStandardTest : public testing::Test { |
| 49 | public: |
| 50 | protected: |
| 51 | ExecutionSession ES; |
| 52 | VSO &V = ES.createVSO("V"); |
| 53 | SymbolStringPtr Foo = ES.getSymbolStringPool().intern("foo"); |
| 54 | SymbolStringPtr Bar = ES.getSymbolStringPool().intern("bar"); |
| 55 | SymbolStringPtr Baz = ES.getSymbolStringPool().intern("baz"); |
| 56 | SymbolStringPtr Qux = ES.getSymbolStringPool().intern("qux"); |
| 57 | static const JITTargetAddress FooAddr = 1U; |
| 58 | static const JITTargetAddress BarAddr = 2U; |
| 59 | static const JITTargetAddress BazAddr = 3U; |
| 60 | static const JITTargetAddress QuxAddr = 4U; |
| 61 | JITEvaluatedSymbol FooSym = |
| 62 | JITEvaluatedSymbol(FooAddr, JITSymbolFlags::Exported); |
| 63 | JITEvaluatedSymbol BarSym = |
| 64 | JITEvaluatedSymbol(BarAddr, JITSymbolFlags::Exported); |
| 65 | JITEvaluatedSymbol BazSym = |
| 66 | JITEvaluatedSymbol(BazAddr, JITSymbolFlags::Exported); |
| 67 | JITEvaluatedSymbol QuxSym = |
| 68 | JITEvaluatedSymbol(QuxAddr, JITSymbolFlags::Exported); |
| 69 | }; |
| 70 | |
| 71 | } // end namespace orc |
| 72 | |
Lang Hames | d22bade | 2017-04-04 17:03:49 +0000 | [diff] [blame] | 73 | class OrcNativeTarget { |
Lang Hames | 130a7c4 | 2015-10-28 02:40:04 +0000 | [diff] [blame] | 74 | public: |
Lang Hames | d22bade | 2017-04-04 17:03:49 +0000 | [diff] [blame] | 75 | static void initialize() { |
Lang Hames | 130a7c4 | 2015-10-28 02:40:04 +0000 | [diff] [blame] | 76 | if (!NativeTargetInitialized) { |
| 77 | InitializeNativeTarget(); |
| 78 | InitializeNativeTargetAsmParser(); |
| 79 | InitializeNativeTargetAsmPrinter(); |
| 80 | NativeTargetInitialized = true; |
| 81 | } |
Lang Hames | d22bade | 2017-04-04 17:03:49 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | private: |
| 85 | static bool NativeTargetInitialized; |
| 86 | }; |
| 87 | |
| 88 | // Base class for Orc tests that will execute code. |
| 89 | class OrcExecutionTest { |
| 90 | public: |
| 91 | |
| 92 | OrcExecutionTest() { |
| 93 | |
| 94 | // Initialize the native target if it hasn't been done already. |
| 95 | OrcNativeTarget::initialize(); |
Lang Hames | fd6e8dc | 2015-10-30 03:20:21 +0000 | [diff] [blame] | 96 | |
| 97 | // Try to select a TargetMachine for the host. |
| 98 | TM.reset(EngineBuilder().selectTarget()); |
| 99 | |
| 100 | if (TM) { |
| 101 | // If we found a TargetMachine, check that it's one that Orc supports. |
| 102 | const Triple& TT = TM->getTargetTriple(); |
Lang Hames | 4f8194e | 2016-02-10 01:02:33 +0000 | [diff] [blame] | 103 | |
Lang Hames | ec978e2 | 2018-03-28 14:47:11 +0000 | [diff] [blame] | 104 | // Bail out for windows platforms. We do not support these yet. |
Lang Hames | da5c6ac | 2018-03-28 15:58:14 +0000 | [diff] [blame] | 105 | if ((TT.getArch() != Triple::x86_64 && TT.getArch() != Triple::x86) || |
| 106 | TT.isOSWindows()) |
Lang Hames | ec978e2 | 2018-03-28 14:47:11 +0000 | [diff] [blame] | 107 | return; |
| 108 | |
Lang Hames | a95b0df | 2018-03-28 03:41:45 +0000 | [diff] [blame] | 109 | // Target can JIT? |
| 110 | SupportsJIT = TM->getTarget().hasJIT(); |
| 111 | // Use ability to create callback manager to detect whether Orc |
| 112 | // has indirection support on this platform. This way the test |
| 113 | // and Orc code do not get out of sync. |
Lang Hames | bd0cb78 | 2018-05-30 01:57:45 +0000 | [diff] [blame] | 114 | SupportsIndirection = !!orc::createLocalCompileCallbackManager(TT, ES, 0); |
Lang Hames | fd6e8dc | 2015-10-30 03:20:21 +0000 | [diff] [blame] | 115 | } |
Lang Hames | 130a7c4 | 2015-10-28 02:40:04 +0000 | [diff] [blame] | 116 | }; |
| 117 | |
Lang Hames | fd6e8dc | 2015-10-30 03:20:21 +0000 | [diff] [blame] | 118 | protected: |
Lang Hames | bd0cb78 | 2018-05-30 01:57:45 +0000 | [diff] [blame] | 119 | orc::ExecutionSession ES; |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 120 | LLVMContext Context; |
Lang Hames | fd6e8dc | 2015-10-30 03:20:21 +0000 | [diff] [blame] | 121 | std::unique_ptr<TargetMachine> TM; |
Lang Hames | a95b0df | 2018-03-28 03:41:45 +0000 | [diff] [blame] | 122 | bool SupportsJIT = false; |
| 123 | bool SupportsIndirection = false; |
Lang Hames | 130a7c4 | 2015-10-28 02:40:04 +0000 | [diff] [blame] | 124 | }; |
| 125 | |
Lang Hames | 4a51e5d | 2015-10-27 17:45:48 +0000 | [diff] [blame] | 126 | class ModuleBuilder { |
| 127 | public: |
| 128 | ModuleBuilder(LLVMContext &Context, StringRef Triple, |
| 129 | StringRef Name); |
Lang Hames | dc4260d | 2015-04-20 20:41:45 +0000 | [diff] [blame] | 130 | |
Lang Hames | 4a51e5d | 2015-10-27 17:45:48 +0000 | [diff] [blame] | 131 | template <typename FuncType> |
Lang Hames | 130a7c4 | 2015-10-28 02:40:04 +0000 | [diff] [blame] | 132 | Function* createFunctionDecl(StringRef Name) { |
Lang Hames | 4a51e5d | 2015-10-27 17:45:48 +0000 | [diff] [blame] | 133 | return Function::Create( |
| 134 | TypeBuilder<FuncType, false>::get(M->getContext()), |
Lang Hames | 130a7c4 | 2015-10-28 02:40:04 +0000 | [diff] [blame] | 135 | GlobalValue::ExternalLinkage, Name, M.get()); |
Lang Hames | 4a51e5d | 2015-10-27 17:45:48 +0000 | [diff] [blame] | 136 | } |
Lang Hames | dc4260d | 2015-04-20 20:41:45 +0000 | [diff] [blame] | 137 | |
Lang Hames | 4a51e5d | 2015-10-27 17:45:48 +0000 | [diff] [blame] | 138 | Module* getModule() { return M.get(); } |
| 139 | const Module* getModule() const { return M.get(); } |
| 140 | std::unique_ptr<Module> takeModule() { return std::move(M); } |
Lang Hames | dc4260d | 2015-04-20 20:41:45 +0000 | [diff] [blame] | 141 | |
Lang Hames | 4a51e5d | 2015-10-27 17:45:48 +0000 | [diff] [blame] | 142 | private: |
| 143 | std::unique_ptr<Module> M; |
Lang Hames | 4a51e5d | 2015-10-27 17:45:48 +0000 | [diff] [blame] | 144 | }; |
Lang Hames | dc4260d | 2015-04-20 20:41:45 +0000 | [diff] [blame] | 145 | |
Lang Hames | 4a51e5d | 2015-10-27 17:45:48 +0000 | [diff] [blame] | 146 | // Dummy struct type. |
| 147 | struct DummyStruct { |
| 148 | int X[256]; |
| 149 | }; |
Lang Hames | dc4260d | 2015-04-20 20:41:45 +0000 | [diff] [blame] | 150 | |
Lang Hames | 4a51e5d | 2015-10-27 17:45:48 +0000 | [diff] [blame] | 151 | // TypeBuilder specialization for DummyStruct. |
| 152 | template <bool XCompile> |
| 153 | class TypeBuilder<DummyStruct, XCompile> { |
| 154 | public: |
| 155 | static StructType *get(LLVMContext &Context) { |
| 156 | return StructType::get( |
Serge Guelton | e38003f | 2017-05-09 19:31:13 +0000 | [diff] [blame] | 157 | TypeBuilder<types::i<32>[256], XCompile>::get(Context)); |
Lang Hames | 4a51e5d | 2015-10-27 17:45:48 +0000 | [diff] [blame] | 158 | } |
| 159 | }; |
Lang Hames | dc4260d | 2015-04-20 20:41:45 +0000 | [diff] [blame] | 160 | |
Lang Hames | cf771ad | 2017-09-28 02:17:35 +0000 | [diff] [blame] | 161 | template <typename HandleT, typename ModuleT> |
Lang Hames | c005656 | 2015-10-20 04:35:02 +0000 | [diff] [blame] | 162 | class MockBaseLayer { |
| 163 | public: |
| 164 | |
Lang Hames | cf771ad | 2017-09-28 02:17:35 +0000 | [diff] [blame] | 165 | using ModuleHandleT = HandleT; |
Lang Hames | c005656 | 2015-10-20 04:35:02 +0000 | [diff] [blame] | 166 | |
Lang Hames | cf771ad | 2017-09-28 02:17:35 +0000 | [diff] [blame] | 167 | using AddModuleSignature = |
| 168 | Expected<ModuleHandleT>(ModuleT M, |
| 169 | std::shared_ptr<JITSymbolResolver> R); |
Lang Hames | c005656 | 2015-10-20 04:35:02 +0000 | [diff] [blame] | 170 | |
Lang Hames | cf771ad | 2017-09-28 02:17:35 +0000 | [diff] [blame] | 171 | using RemoveModuleSignature = Error(ModuleHandleT H); |
| 172 | using FindSymbolSignature = JITSymbol(const std::string &Name, |
| 173 | bool ExportedSymbolsOnly); |
| 174 | using FindSymbolInSignature = JITSymbol(ModuleHandleT H, |
| 175 | const std::string &Name, |
| 176 | bool ExportedSymbolsONly); |
| 177 | using EmitAndFinalizeSignature = Error(ModuleHandleT H); |
| 178 | |
| 179 | std::function<AddModuleSignature> addModuleImpl; |
| 180 | std::function<RemoveModuleSignature> removeModuleImpl; |
| 181 | std::function<FindSymbolSignature> findSymbolImpl; |
| 182 | std::function<FindSymbolInSignature> findSymbolInImpl; |
| 183 | std::function<EmitAndFinalizeSignature> emitAndFinalizeImpl; |
| 184 | |
| 185 | Expected<ModuleHandleT> addModule(ModuleT M, |
| 186 | std::shared_ptr<JITSymbolResolver> R) { |
| 187 | assert(addModuleImpl && |
| 188 | "addModule called, but no mock implementation was provided"); |
| 189 | return addModuleImpl(std::move(M), std::move(R)); |
Lang Hames | c005656 | 2015-10-20 04:35:02 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Lang Hames | 4ce9866 | 2017-07-07 02:59:13 +0000 | [diff] [blame] | 192 | Error removeModule(ModuleHandleT H) { |
Lang Hames | cf771ad | 2017-09-28 02:17:35 +0000 | [diff] [blame] | 193 | assert(removeModuleImpl && |
| 194 | "removeModule called, but no mock implementation was provided"); |
| 195 | return removeModuleImpl(H); |
Lang Hames | c005656 | 2015-10-20 04:35:02 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Lang Hames | ad4a911 | 2016-08-01 20:49:11 +0000 | [diff] [blame] | 198 | JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) { |
Lang Hames | cf771ad | 2017-09-28 02:17:35 +0000 | [diff] [blame] | 199 | assert(findSymbolImpl && |
| 200 | "findSymbol called, but no mock implementation was provided"); |
| 201 | return findSymbolImpl(Name, ExportedSymbolsOnly); |
Lang Hames | c005656 | 2015-10-20 04:35:02 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Lang Hames | cd9d49b | 2017-06-23 23:25:28 +0000 | [diff] [blame] | 204 | JITSymbol findSymbolIn(ModuleHandleT H, const std::string &Name, |
Lang Hames | c005656 | 2015-10-20 04:35:02 +0000 | [diff] [blame] | 205 | bool ExportedSymbolsOnly) { |
Lang Hames | cf771ad | 2017-09-28 02:17:35 +0000 | [diff] [blame] | 206 | assert(findSymbolInImpl && |
| 207 | "findSymbolIn called, but no mock implementation was provided"); |
| 208 | return findSymbolInImpl(H, Name, ExportedSymbolsOnly); |
Lang Hames | c005656 | 2015-10-20 04:35:02 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Lang Hames | cf771ad | 2017-09-28 02:17:35 +0000 | [diff] [blame] | 211 | Error emitAndFinaliez(ModuleHandleT H) { |
| 212 | assert(emitAndFinalizeImpl && |
| 213 | "emitAndFinalize called, but no mock implementation was provided"); |
| 214 | return emitAndFinalizeImpl(H); |
| 215 | } |
Lang Hames | c005656 | 2015-10-20 04:35:02 +0000 | [diff] [blame] | 216 | }; |
| 217 | |
Lang Hames | 4ce9866 | 2017-07-07 02:59:13 +0000 | [diff] [blame] | 218 | class ReturnNullJITSymbol { |
| 219 | public: |
| 220 | template <typename... Args> |
| 221 | JITSymbol operator()(Args...) const { |
| 222 | return nullptr; |
| 223 | } |
| 224 | }; |
| 225 | |
Lang Hames | c005656 | 2015-10-20 04:35:02 +0000 | [diff] [blame] | 226 | template <typename ReturnT> |
| 227 | class DoNothingAndReturn { |
| 228 | public: |
Lang Hames | 4ce9866 | 2017-07-07 02:59:13 +0000 | [diff] [blame] | 229 | DoNothingAndReturn(ReturnT Ret) : Ret(std::move(Ret)) {} |
Lang Hames | c005656 | 2015-10-20 04:35:02 +0000 | [diff] [blame] | 230 | |
| 231 | template <typename... Args> |
Lang Hames | 4ce9866 | 2017-07-07 02:59:13 +0000 | [diff] [blame] | 232 | void operator()(Args...) const { return Ret; } |
Lang Hames | c005656 | 2015-10-20 04:35:02 +0000 | [diff] [blame] | 233 | private: |
Lang Hames | 4ce9866 | 2017-07-07 02:59:13 +0000 | [diff] [blame] | 234 | ReturnT Ret; |
Lang Hames | c005656 | 2015-10-20 04:35:02 +0000 | [diff] [blame] | 235 | }; |
| 236 | |
| 237 | template <> |
| 238 | class DoNothingAndReturn<void> { |
| 239 | public: |
| 240 | template <typename... Args> |
| 241 | void operator()(Args...) const { } |
| 242 | }; |
Lang Hames | dc4260d | 2015-04-20 20:41:45 +0000 | [diff] [blame] | 243 | |
| 244 | } // namespace llvm |
| 245 | |
| 246 | #endif |