Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 1 | // Copyright 2020 The SwiftShader Authors. All Rights Reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #include "LLVMReactor.hpp" |
| 16 | |
| 17 | #include "Debug.hpp" |
| 18 | #include "ExecutableMemory.hpp" |
| 19 | #include "Routine.hpp" |
| 20 | |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 21 | // TODO(b/143539525): Eliminate when warning has been fixed. |
| 22 | #ifdef _MSC_VER |
| 23 | __pragma(warning(push)) |
| 24 | __pragma(warning(disable : 4146)) // unary minus operator applied to unsigned type, result still unsigned |
| 25 | #endif |
| 26 | |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 27 | #include "llvm/ExecutionEngine/Orc/CompileUtils.h" |
| 28 | #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h" |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 29 | #include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h" |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 30 | #include "llvm/ExecutionEngine/SectionMemoryManager.h" |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 31 | #include "llvm/IR/LegacyPassManager.h" |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 32 | #include "llvm/Support/TargetSelect.h" |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 33 | #include "llvm/Transforms/InstCombine/InstCombine.h" |
| 34 | #include "llvm/Transforms/Scalar.h" |
| 35 | #include "llvm/Transforms/Scalar/GVN.h" |
| 36 | |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 37 | #ifdef _MSC_VER |
| 38 | __pragma(warning(pop)) |
| 39 | #endif |
| 40 | |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 41 | #if defined(_WIN64) |
| 42 | extern "C" void __chkstk(); |
| 43 | #elif defined(_WIN32) |
| 44 | extern "C" void _chkstk(); |
| 45 | #endif |
| 46 | |
Antonio Maiorano | dd48b7e | 2020-02-05 13:17:07 -0500 | [diff] [blame] | 47 | #if __has_feature(memory_sanitizer) |
| 48 | # include <sanitizer/msan_interface.h> |
| 49 | #endif |
| 50 | |
Anton D. Kachalov | ac4e1d2 | 2020-02-11 15:44:27 +0100 | [diff] [blame] | 51 | #ifdef __ARM_EABI__ |
| 52 | extern "C" signed __aeabi_idivmod(); |
| 53 | #endif |
| 54 | |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 55 | namespace { |
| 56 | |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 57 | // JITGlobals is a singleton that holds all the immutable machine specific |
| 58 | // information for the host device. |
| 59 | class JITGlobals |
| 60 | { |
| 61 | public: |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 62 | static JITGlobals *get(); |
| 63 | |
Ben Clayton | ee18f39 | 2020-10-19 16:54:21 -0400 | [diff] [blame^] | 64 | llvm::orc::JITTargetMachineBuilder getTargetMachineBuilder(rr::Optimization::Level optLevel) const; |
| 65 | const llvm::DataLayout &getDataLayout() const; |
| 66 | const llvm::Triple getTargetTriple() const; |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 67 | |
| 68 | private: |
Ben Clayton | ee18f39 | 2020-10-19 16:54:21 -0400 | [diff] [blame^] | 69 | JITGlobals(const llvm::orc::JITTargetMachineBuilder &jtmb, const llvm::DataLayout &dataLayout); |
| 70 | |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 71 | static llvm::CodeGenOpt::Level toLLVM(rr::Optimization::Level level); |
Ben Clayton | ee18f39 | 2020-10-19 16:54:21 -0400 | [diff] [blame^] | 72 | const llvm::orc::JITTargetMachineBuilder jtmb; |
| 73 | const llvm::DataLayout dataLayout; |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | JITGlobals *JITGlobals::get() |
| 77 | { |
Ben Clayton | ee18f39 | 2020-10-19 16:54:21 -0400 | [diff] [blame^] | 78 | static JITGlobals instance = [] { |
| 79 | llvm::InitializeNativeTarget(); |
| 80 | llvm::InitializeNativeTargetAsmPrinter(); |
| 81 | llvm::InitializeNativeTargetAsmParser(); |
| 82 | |
| 83 | auto jtmb = llvm::orc::JITTargetMachineBuilder::detectHost(); |
| 84 | ASSERT_MSG(jtmb, "JITTargetMachineBuilder::detectHost() failed"); |
| 85 | auto dataLayout = jtmb->getDefaultDataLayoutForTarget(); |
| 86 | ASSERT_MSG(dataLayout, "JITTargetMachineBuilder::getDefaultDataLayoutForTarget() failed"); |
| 87 | return JITGlobals(jtmb.get(), dataLayout.get()); |
| 88 | }(); |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 89 | return &instance; |
| 90 | } |
| 91 | |
Ben Clayton | ee18f39 | 2020-10-19 16:54:21 -0400 | [diff] [blame^] | 92 | llvm::orc::JITTargetMachineBuilder JITGlobals::getTargetMachineBuilder(rr::Optimization::Level optLevel) const |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 93 | { |
Ben Clayton | ee18f39 | 2020-10-19 16:54:21 -0400 | [diff] [blame^] | 94 | llvm::orc::JITTargetMachineBuilder out = jtmb; |
| 95 | out.setCodeGenOptLevel(toLLVM(optLevel)); |
| 96 | return out; |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 97 | } |
| 98 | |
Ben Clayton | ee18f39 | 2020-10-19 16:54:21 -0400 | [diff] [blame^] | 99 | const llvm::DataLayout &JITGlobals::getDataLayout() const |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 100 | { |
Ben Clayton | ee18f39 | 2020-10-19 16:54:21 -0400 | [diff] [blame^] | 101 | return dataLayout; |
| 102 | } |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 103 | |
Ben Clayton | ee18f39 | 2020-10-19 16:54:21 -0400 | [diff] [blame^] | 104 | const llvm::Triple JITGlobals::getTargetTriple() const |
| 105 | { |
| 106 | return jtmb.getTargetTriple(); |
| 107 | } |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 108 | |
Ben Clayton | ee18f39 | 2020-10-19 16:54:21 -0400 | [diff] [blame^] | 109 | JITGlobals::JITGlobals(const llvm::orc::JITTargetMachineBuilder &jtmb, const llvm::DataLayout &dataLayout) |
| 110 | : jtmb(jtmb) |
| 111 | , dataLayout(dataLayout) |
| 112 | { |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | llvm::CodeGenOpt::Level JITGlobals::toLLVM(rr::Optimization::Level level) |
| 116 | { |
| 117 | switch(level) |
| 118 | { |
| 119 | case rr::Optimization::Level::None: return ::llvm::CodeGenOpt::None; |
| 120 | case rr::Optimization::Level::Less: return ::llvm::CodeGenOpt::Less; |
| 121 | case rr::Optimization::Level::Default: return ::llvm::CodeGenOpt::Default; |
| 122 | case rr::Optimization::Level::Aggressive: return ::llvm::CodeGenOpt::Aggressive; |
| 123 | default: UNREACHABLE("Unknown Optimization Level %d", int(level)); |
| 124 | } |
| 125 | return ::llvm::CodeGenOpt::Default; |
| 126 | } |
| 127 | |
David 'Digit' Turner | 48f3f6c | 2020-03-23 14:23:10 +0100 | [diff] [blame] | 128 | class MemoryMapper final : public llvm::SectionMemoryManager::MemoryMapper |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 129 | { |
| 130 | public: |
| 131 | MemoryMapper() {} |
| 132 | ~MemoryMapper() final {} |
| 133 | |
| 134 | llvm::sys::MemoryBlock allocateMappedMemory( |
| 135 | llvm::SectionMemoryManager::AllocationPurpose purpose, |
| 136 | size_t numBytes, const llvm::sys::MemoryBlock *const nearBlock, |
| 137 | unsigned flags, std::error_code &errorCode) final |
| 138 | { |
| 139 | errorCode = std::error_code(); |
| 140 | |
| 141 | // Round up numBytes to page size. |
| 142 | size_t pageSize = rr::memoryPageSize(); |
| 143 | numBytes = (numBytes + pageSize - 1) & ~(pageSize - 1); |
| 144 | |
| 145 | bool need_exec = |
| 146 | purpose == llvm::SectionMemoryManager::AllocationPurpose::Code; |
| 147 | void *addr = rr::allocateMemoryPages( |
| 148 | numBytes, flagsToPermissions(flags), need_exec); |
| 149 | if(!addr) |
| 150 | return llvm::sys::MemoryBlock(); |
| 151 | return llvm::sys::MemoryBlock(addr, numBytes); |
| 152 | } |
| 153 | |
| 154 | std::error_code protectMappedMemory(const llvm::sys::MemoryBlock &block, |
| 155 | unsigned flags) |
| 156 | { |
| 157 | // Round down base address to align with a page boundary. This matches |
| 158 | // DefaultMMapper behavior. |
| 159 | void *addr = block.base(); |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 160 | size_t size = block.allocatedSize(); |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 161 | size_t pageSize = rr::memoryPageSize(); |
| 162 | addr = reinterpret_cast<void *>( |
| 163 | reinterpret_cast<uintptr_t>(addr) & ~(pageSize - 1)); |
| 164 | size += reinterpret_cast<uintptr_t>(block.base()) - |
| 165 | reinterpret_cast<uintptr_t>(addr); |
| 166 | |
| 167 | rr::protectMemoryPages(addr, size, flagsToPermissions(flags)); |
| 168 | return std::error_code(); |
| 169 | } |
| 170 | |
| 171 | std::error_code releaseMappedMemory(llvm::sys::MemoryBlock &block) |
| 172 | { |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 173 | size_t size = block.allocatedSize(); |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 174 | |
| 175 | rr::deallocateMemoryPages(block.base(), size); |
| 176 | return std::error_code(); |
| 177 | } |
| 178 | |
| 179 | private: |
| 180 | int flagsToPermissions(unsigned flags) |
| 181 | { |
| 182 | int result = 0; |
| 183 | if(flags & llvm::sys::Memory::MF_READ) |
| 184 | { |
| 185 | result |= rr::PERMISSION_READ; |
| 186 | } |
| 187 | if(flags & llvm::sys::Memory::MF_WRITE) |
| 188 | { |
| 189 | result |= rr::PERMISSION_WRITE; |
| 190 | } |
| 191 | if(flags & llvm::sys::Memory::MF_EXEC) |
| 192 | { |
| 193 | result |= rr::PERMISSION_EXECUTE; |
| 194 | } |
| 195 | return result; |
| 196 | } |
| 197 | }; |
| 198 | |
| 199 | template<typename T> |
| 200 | T alignUp(T val, T alignment) |
| 201 | { |
| 202 | return alignment * ((val + alignment - 1) / alignment); |
| 203 | } |
| 204 | |
| 205 | void *alignedAlloc(size_t size, size_t alignment) |
| 206 | { |
| 207 | ASSERT(alignment < 256); |
| 208 | auto allocation = new uint8_t[size + sizeof(uint8_t) + alignment]; |
| 209 | auto aligned = allocation; |
| 210 | aligned += sizeof(uint8_t); // Make space for the base-address offset. |
| 211 | aligned = reinterpret_cast<uint8_t *>(alignUp(reinterpret_cast<uintptr_t>(aligned), alignment)); // align |
| 212 | auto offset = static_cast<uint8_t>(aligned - allocation); |
| 213 | aligned[-1] = offset; |
| 214 | return aligned; |
| 215 | } |
| 216 | |
| 217 | void alignedFree(void *ptr) |
| 218 | { |
| 219 | auto aligned = reinterpret_cast<uint8_t *>(ptr); |
| 220 | auto offset = aligned[-1]; |
| 221 | auto allocation = aligned - offset; |
| 222 | delete[] allocation; |
| 223 | } |
| 224 | |
| 225 | template<typename T> |
| 226 | static void atomicLoad(void *ptr, void *ret, llvm::AtomicOrdering ordering) |
| 227 | { |
| 228 | *reinterpret_cast<T *>(ret) = std::atomic_load_explicit<T>(reinterpret_cast<std::atomic<T> *>(ptr), rr::atomicOrdering(ordering)); |
| 229 | } |
| 230 | |
| 231 | template<typename T> |
| 232 | static void atomicStore(void *ptr, void *val, llvm::AtomicOrdering ordering) |
| 233 | { |
| 234 | std::atomic_store_explicit<T>(reinterpret_cast<std::atomic<T> *>(ptr), *reinterpret_cast<T *>(val), rr::atomicOrdering(ordering)); |
| 235 | } |
| 236 | |
| 237 | #ifdef __ANDROID__ |
| 238 | template<typename F> |
| 239 | static uint32_t sync_fetch_and_op(uint32_t volatile *ptr, uint32_t val, F f) |
| 240 | { |
| 241 | // Build an arbitrary op out of looped CAS |
| 242 | for(;;) |
| 243 | { |
| 244 | uint32_t expected = *ptr; |
| 245 | uint32_t desired = f(expected, val); |
| 246 | |
| 247 | if(expected == __sync_val_compare_and_swap_4(ptr, expected, desired)) |
| 248 | { |
| 249 | return expected; |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | #endif |
| 254 | |
Ben Clayton | ee18f39 | 2020-10-19 16:54:21 -0400 | [diff] [blame^] | 255 | class ExternalSymbolGenerator : public llvm::orc::JITDylib::DefinitionGenerator |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 256 | { |
| 257 | struct Atomic |
| 258 | { |
| 259 | static void load(size_t size, void *ptr, void *ret, llvm::AtomicOrdering ordering) |
| 260 | { |
| 261 | switch(size) |
| 262 | { |
| 263 | case 1: atomicLoad<uint8_t>(ptr, ret, ordering); break; |
| 264 | case 2: atomicLoad<uint16_t>(ptr, ret, ordering); break; |
| 265 | case 4: atomicLoad<uint32_t>(ptr, ret, ordering); break; |
| 266 | case 8: atomicLoad<uint64_t>(ptr, ret, ordering); break; |
| 267 | default: |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 268 | UNIMPLEMENTED_NO_BUG("Atomic::load(size: %d)", int(size)); |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | static void store(size_t size, void *ptr, void *ret, llvm::AtomicOrdering ordering) |
| 272 | { |
| 273 | switch(size) |
| 274 | { |
| 275 | case 1: atomicStore<uint8_t>(ptr, ret, ordering); break; |
| 276 | case 2: atomicStore<uint16_t>(ptr, ret, ordering); break; |
| 277 | case 4: atomicStore<uint32_t>(ptr, ret, ordering); break; |
| 278 | case 8: atomicStore<uint64_t>(ptr, ret, ordering); break; |
| 279 | default: |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 280 | UNIMPLEMENTED_NO_BUG("Atomic::store(size: %d)", int(size)); |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | }; |
| 284 | |
Ben Clayton | ee18f39 | 2020-10-19 16:54:21 -0400 | [diff] [blame^] | 285 | static void nop() {} |
| 286 | static void neverCalled() { UNREACHABLE("Should never be called"); } |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 287 | |
Ben Clayton | ee18f39 | 2020-10-19 16:54:21 -0400 | [diff] [blame^] | 288 | static void *coroutine_alloc_frame(size_t size) { return alignedAlloc(size, 16); } |
| 289 | static void coroutine_free_frame(void *ptr) { alignedFree(ptr); } |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 290 | |
| 291 | #ifdef __ANDROID__ |
Ben Clayton | ee18f39 | 2020-10-19 16:54:21 -0400 | [diff] [blame^] | 292 | // forwarders since we can't take address of builtins |
| 293 | static void sync_synchronize() { __sync_synchronize(); } |
| 294 | static uint32_t sync_fetch_and_add_4(uint32_t *ptr, uint32_t val) { return __sync_fetch_and_add_4(ptr, val); } |
| 295 | static uint32_t sync_fetch_and_and_4(uint32_t *ptr, uint32_t val) { return __sync_fetch_and_and_4(ptr, val); } |
| 296 | static uint32_t sync_fetch_and_or_4(uint32_t *ptr, uint32_t val) { return __sync_fetch_and_or_4(ptr, val); } |
| 297 | static uint32_t sync_fetch_and_xor_4(uint32_t *ptr, uint32_t val) { return __sync_fetch_and_xor_4(ptr, val); } |
| 298 | static uint32_t sync_fetch_and_sub_4(uint32_t *ptr, uint32_t val) { return __sync_fetch_and_sub_4(ptr, val); } |
| 299 | static uint32_t sync_lock_test_and_set_4(uint32_t *ptr, uint32_t val) { return __sync_lock_test_and_set_4(ptr, val); } |
| 300 | static uint32_t sync_val_compare_and_swap_4(uint32_t *ptr, uint32_t expected, uint32_t desired) { return __sync_val_compare_and_swap_4(ptr, expected, desired); } |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 301 | |
Ben Clayton | ee18f39 | 2020-10-19 16:54:21 -0400 | [diff] [blame^] | 302 | static uint32_t sync_fetch_and_max_4(uint32_t *ptr, uint32_t val) |
| 303 | { |
| 304 | return sync_fetch_and_op(ptr, val, [](int32_t a, int32_t b) { return std::max(a, b); }); |
| 305 | } |
| 306 | static uint32_t sync_fetch_and_min_4(uint32_t *ptr, uint32_t val) |
| 307 | { |
| 308 | return sync_fetch_and_op(ptr, val, [](int32_t a, int32_t b) { return std::min(a, b); }); |
| 309 | } |
| 310 | static uint32_t sync_fetch_and_umax_4(uint32_t *ptr, uint32_t val) |
| 311 | { |
| 312 | return sync_fetch_and_op(ptr, val, [](uint32_t a, uint32_t b) { return std::max(a, b); }); |
| 313 | } |
| 314 | static uint32_t sync_fetch_and_umin_4(uint32_t *ptr, uint32_t val) |
| 315 | { |
| 316 | return sync_fetch_and_op(ptr, val, [](uint32_t a, uint32_t b) { return std::min(a, b); }); |
| 317 | } |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 318 | #endif |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 319 | class Resolver |
| 320 | { |
| 321 | public: |
Ben Clayton | ee18f39 | 2020-10-19 16:54:21 -0400 | [diff] [blame^] | 322 | using FunctionMap = llvm::StringMap<void *>; |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 323 | |
| 324 | FunctionMap functions; |
| 325 | |
| 326 | Resolver() |
| 327 | { |
Antonio Maiorano | 8cbee41 | 2020-06-10 15:59:20 -0400 | [diff] [blame] | 328 | #ifdef ENABLE_RR_PRINT |
Ben Clayton | ee18f39 | 2020-10-19 16:54:21 -0400 | [diff] [blame^] | 329 | functions.try_emplace("rr::DebugPrintf", reinterpret_cast<void *>(rr::DebugPrintf)); |
Antonio Maiorano | 8cbee41 | 2020-06-10 15:59:20 -0400 | [diff] [blame] | 330 | #endif |
Ben Clayton | ee18f39 | 2020-10-19 16:54:21 -0400 | [diff] [blame^] | 331 | functions.try_emplace("nop", reinterpret_cast<void *>(nop)); |
| 332 | functions.try_emplace("floorf", reinterpret_cast<void *>(floorf)); |
| 333 | functions.try_emplace("nearbyintf", reinterpret_cast<void *>(nearbyintf)); |
| 334 | functions.try_emplace("truncf", reinterpret_cast<void *>(truncf)); |
| 335 | functions.try_emplace("printf", reinterpret_cast<void *>(printf)); |
| 336 | functions.try_emplace("puts", reinterpret_cast<void *>(puts)); |
| 337 | functions.try_emplace("fmodf", reinterpret_cast<void *>(fmodf)); |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 338 | |
Ben Clayton | ee18f39 | 2020-10-19 16:54:21 -0400 | [diff] [blame^] | 339 | functions.try_emplace("sinf", reinterpret_cast<void *>(sinf)); |
| 340 | functions.try_emplace("cosf", reinterpret_cast<void *>(cosf)); |
| 341 | functions.try_emplace("asinf", reinterpret_cast<void *>(asinf)); |
| 342 | functions.try_emplace("acosf", reinterpret_cast<void *>(acosf)); |
| 343 | functions.try_emplace("atanf", reinterpret_cast<void *>(atanf)); |
| 344 | functions.try_emplace("sinhf", reinterpret_cast<void *>(sinhf)); |
| 345 | functions.try_emplace("coshf", reinterpret_cast<void *>(coshf)); |
| 346 | functions.try_emplace("tanhf", reinterpret_cast<void *>(tanhf)); |
| 347 | functions.try_emplace("asinhf", reinterpret_cast<void *>(asinhf)); |
| 348 | functions.try_emplace("acoshf", reinterpret_cast<void *>(acoshf)); |
| 349 | functions.try_emplace("atanhf", reinterpret_cast<void *>(atanhf)); |
| 350 | functions.try_emplace("atan2f", reinterpret_cast<void *>(atan2f)); |
| 351 | functions.try_emplace("powf", reinterpret_cast<void *>(powf)); |
| 352 | functions.try_emplace("expf", reinterpret_cast<void *>(expf)); |
| 353 | functions.try_emplace("logf", reinterpret_cast<void *>(logf)); |
| 354 | functions.try_emplace("exp2f", reinterpret_cast<void *>(exp2f)); |
| 355 | functions.try_emplace("log2f", reinterpret_cast<void *>(log2f)); |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 356 | |
Ben Clayton | ee18f39 | 2020-10-19 16:54:21 -0400 | [diff] [blame^] | 357 | functions.try_emplace("sin", reinterpret_cast<void *>(static_cast<double (*)(double)>(sin))); |
| 358 | functions.try_emplace("cos", reinterpret_cast<void *>(static_cast<double (*)(double)>(cos))); |
| 359 | functions.try_emplace("asin", reinterpret_cast<void *>(static_cast<double (*)(double)>(asin))); |
| 360 | functions.try_emplace("acos", reinterpret_cast<void *>(static_cast<double (*)(double)>(acos))); |
| 361 | functions.try_emplace("atan", reinterpret_cast<void *>(static_cast<double (*)(double)>(atan))); |
| 362 | functions.try_emplace("sinh", reinterpret_cast<void *>(static_cast<double (*)(double)>(sinh))); |
| 363 | functions.try_emplace("cosh", reinterpret_cast<void *>(static_cast<double (*)(double)>(cosh))); |
| 364 | functions.try_emplace("tanh", reinterpret_cast<void *>(static_cast<double (*)(double)>(tanh))); |
| 365 | functions.try_emplace("asinh", reinterpret_cast<void *>(static_cast<double (*)(double)>(asinh))); |
| 366 | functions.try_emplace("acosh", reinterpret_cast<void *>(static_cast<double (*)(double)>(acosh))); |
| 367 | functions.try_emplace("atanh", reinterpret_cast<void *>(static_cast<double (*)(double)>(atanh))); |
| 368 | functions.try_emplace("atan2", reinterpret_cast<void *>(static_cast<double (*)(double, double)>(atan2))); |
| 369 | functions.try_emplace("pow", reinterpret_cast<void *>(static_cast<double (*)(double, double)>(pow))); |
| 370 | functions.try_emplace("exp", reinterpret_cast<void *>(static_cast<double (*)(double)>(exp))); |
| 371 | functions.try_emplace("log", reinterpret_cast<void *>(static_cast<double (*)(double)>(log))); |
| 372 | functions.try_emplace("exp2", reinterpret_cast<void *>(static_cast<double (*)(double)>(exp2))); |
| 373 | functions.try_emplace("log2", reinterpret_cast<void *>(static_cast<double (*)(double)>(log2))); |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 374 | |
Ben Clayton | ee18f39 | 2020-10-19 16:54:21 -0400 | [diff] [blame^] | 375 | functions.try_emplace("atomic_load", reinterpret_cast<void *>(Atomic::load)); |
| 376 | functions.try_emplace("atomic_store", reinterpret_cast<void *>(Atomic::store)); |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 377 | |
| 378 | // FIXME(b/119409619): use an allocator here so we can control all memory allocations |
Ben Clayton | ee18f39 | 2020-10-19 16:54:21 -0400 | [diff] [blame^] | 379 | functions.try_emplace("coroutine_alloc_frame", reinterpret_cast<void *>(coroutine_alloc_frame)); |
| 380 | functions.try_emplace("coroutine_free_frame", reinterpret_cast<void *>(coroutine_free_frame)); |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 381 | |
| 382 | #ifdef __APPLE__ |
Ben Clayton | ee18f39 | 2020-10-19 16:54:21 -0400 | [diff] [blame^] | 383 | functions.try_emplace("sincosf_stret", reinterpret_cast<void *>(__sincosf_stret)); |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 384 | #elif defined(__linux__) |
Ben Clayton | ee18f39 | 2020-10-19 16:54:21 -0400 | [diff] [blame^] | 385 | functions.try_emplace("sincosf", reinterpret_cast<void *>(sincosf)); |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 386 | #elif defined(_WIN64) |
Ben Clayton | ee18f39 | 2020-10-19 16:54:21 -0400 | [diff] [blame^] | 387 | functions.try_emplace("chkstk", reinterpret_cast<void *>(__chkstk)); |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 388 | #elif defined(_WIN32) |
Ben Clayton | ee18f39 | 2020-10-19 16:54:21 -0400 | [diff] [blame^] | 389 | functions.try_emplace("chkstk", reinterpret_cast<void *>(_chkstk)); |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 390 | #endif |
| 391 | |
Anton D. Kachalov | ac4e1d2 | 2020-02-11 15:44:27 +0100 | [diff] [blame] | 392 | #ifdef __ARM_EABI__ |
Ben Clayton | ee18f39 | 2020-10-19 16:54:21 -0400 | [diff] [blame^] | 393 | functions.try_emplace("aeabi_idivmod", reinterpret_cast<void *>(__aeabi_idivmod)); |
Anton D. Kachalov | ac4e1d2 | 2020-02-11 15:44:27 +0100 | [diff] [blame] | 394 | #endif |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 395 | #ifdef __ANDROID__ |
Ben Clayton | ee18f39 | 2020-10-19 16:54:21 -0400 | [diff] [blame^] | 396 | functions.try_emplace("aeabi_unwind_cpp_pr0", reinterpret_cast<void *>(F::neverCalled)); |
| 397 | functions.try_emplace("sync_synchronize", reinterpret_cast<void *>(F::sync_synchronize)); |
| 398 | functions.try_emplace("sync_fetch_and_add_4", reinterpret_cast<void *>(F::sync_fetch_and_add_4)); |
| 399 | functions.try_emplace("sync_fetch_and_and_4", reinterpret_cast<void *>(F::sync_fetch_and_and_4)); |
| 400 | functions.try_emplace("sync_fetch_and_or_4", reinterpret_cast<void *>(F::sync_fetch_and_or_4)); |
| 401 | functions.try_emplace("sync_fetch_and_xor_4", reinterpret_cast<void *>(F::sync_fetch_and_xor_4)); |
| 402 | functions.try_emplace("sync_fetch_and_sub_4", reinterpret_cast<void *>(F::sync_fetch_and_sub_4)); |
| 403 | functions.try_emplace("sync_lock_test_and_set_4", reinterpret_cast<void *>(F::sync_lock_test_and_set_4)); |
| 404 | functions.try_emplace("sync_val_compare_and_swap_4", reinterpret_cast<void *>(F::sync_val_compare_and_swap_4)); |
| 405 | functions.try_emplace("sync_fetch_and_max_4", reinterpret_cast<void *>(F::sync_fetch_and_max_4)); |
| 406 | functions.try_emplace("sync_fetch_and_min_4", reinterpret_cast<void *>(F::sync_fetch_and_min_4)); |
| 407 | functions.try_emplace("sync_fetch_and_umax_4", reinterpret_cast<void *>(F::sync_fetch_and_umax_4)); |
| 408 | functions.try_emplace("sync_fetch_and_umin_4", reinterpret_cast<void *>(F::sync_fetch_and_umin_4)); |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 409 | #endif |
Antonio Maiorano | dd48b7e | 2020-02-05 13:17:07 -0500 | [diff] [blame] | 410 | #if __has_feature(memory_sanitizer) |
Ben Clayton | ee18f39 | 2020-10-19 16:54:21 -0400 | [diff] [blame^] | 411 | functions.try_emplace("msan_unpoison", reinterpret_cast<void *>(__msan_unpoison)); |
Antonio Maiorano | dd48b7e | 2020-02-05 13:17:07 -0500 | [diff] [blame] | 412 | #endif |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 413 | } |
| 414 | }; |
| 415 | |
Ben Clayton | ee18f39 | 2020-10-19 16:54:21 -0400 | [diff] [blame^] | 416 | llvm::Error tryToGenerate(llvm::orc::LookupKind kind, |
| 417 | llvm::orc::JITDylib &dylib, |
| 418 | llvm::orc::JITDylibLookupFlags flags, |
| 419 | const llvm::orc::SymbolLookupSet &set) override |
| 420 | { |
| 421 | static Resolver resolver; |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 422 | |
Ben Clayton | ee18f39 | 2020-10-19 16:54:21 -0400 | [diff] [blame^] | 423 | llvm::orc::SymbolMap symbols; |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 424 | |
Ben Clayton | ee18f39 | 2020-10-19 16:54:21 -0400 | [diff] [blame^] | 425 | #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) |
| 426 | std::string missing; |
| 427 | #endif // !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) |
| 428 | |
| 429 | for(auto symbol : set) |
| 430 | { |
| 431 | auto name = symbol.first; |
| 432 | |
| 433 | // Trim off any underscores from the start of the symbol. LLVM likes |
| 434 | // to append these on macOS. |
| 435 | auto trimmed = (*name).drop_while([](char c) { return c == '_'; }); |
| 436 | |
| 437 | auto it = resolver.functions.find(trimmed.str()); |
| 438 | if(it != resolver.functions.end()) |
| 439 | { |
| 440 | symbols[name] = llvm::JITEvaluatedSymbol( |
| 441 | static_cast<llvm::JITTargetAddress>(reinterpret_cast<uintptr_t>(it->second)), |
| 442 | llvm::JITSymbolFlags::Exported); |
| 443 | } |
| 444 | #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) |
| 445 | else |
| 446 | { |
| 447 | missing += (missing.empty() ? "'" : ", '") + (*name).str() + "'"; |
| 448 | } |
| 449 | #endif // !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) |
| 450 | } |
| 451 | |
| 452 | #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) |
| 453 | // Missing functions will likely make the module fail in exciting non-obvious ways. |
| 454 | if(!missing.empty()) |
| 455 | { |
| 456 | WARN("Missing external functions: %s", missing.c_str()); |
| 457 | } |
| 458 | #endif // !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) |
| 459 | |
| 460 | if(symbols.empty()) |
| 461 | { |
| 462 | return llvm::Error::success(); |
| 463 | } |
| 464 | |
| 465 | return dylib.define(llvm::orc::absoluteSymbols(std::move(symbols))); |
| 466 | } |
| 467 | }; |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 468 | |
| 469 | // JITRoutine is a rr::Routine that holds a LLVM JIT session, compiler and |
| 470 | // object layer as each routine may require different target machine |
| 471 | // settings and no Reactor routine directly links against another. |
| 472 | class JITRoutine : public rr::Routine |
| 473 | { |
Ben Clayton | ee18f39 | 2020-10-19 16:54:21 -0400 | [diff] [blame^] | 474 | using ObjLayer = llvm::orc::RTDyldObjectLinkingLayer; |
| 475 | using CompileLayer = llvm::orc::IRCompileLayer; |
| 476 | |
| 477 | llvm::orc::RTDyldObjectLinkingLayer objectLayer; |
| 478 | llvm::orc::IRCompileLayer compileLayer; |
| 479 | llvm::orc::MangleAndInterner mangle; |
| 480 | llvm::orc::ThreadSafeContext ctx; |
| 481 | llvm::orc::ExecutionSession session; |
| 482 | llvm::orc::JITDylib &dylib; |
| 483 | std::vector<const void *> addresses; |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 484 | |
| 485 | public: |
| 486 | JITRoutine( |
| 487 | std::unique_ptr<llvm::Module> module, |
| 488 | llvm::Function **funcs, |
| 489 | size_t count, |
| 490 | const rr::Config &config) |
Ben Clayton | ee18f39 | 2020-10-19 16:54:21 -0400 | [diff] [blame^] | 491 | : objectLayer(session, []() { return std::make_unique<llvm::SectionMemoryManager>(new MemoryMapper()); }) |
| 492 | , compileLayer(session, objectLayer, std::make_unique<llvm::orc::ConcurrentIRCompiler>(JITGlobals::get()->getTargetMachineBuilder(config.getOptimization().getLevel()))) |
| 493 | , mangle(session, JITGlobals::get()->getDataLayout()) |
| 494 | , ctx(std::make_unique<llvm::LLVMContext>()) |
| 495 | , dylib(session.createJITDylib("<routine>")) |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 496 | , addresses(count) |
| 497 | { |
Ben Clayton | a7bc2b9 | 2020-03-26 11:24:49 +0000 | [diff] [blame] | 498 | |
Ben Clayton | ee18f39 | 2020-10-19 16:54:21 -0400 | [diff] [blame^] | 499 | #ifdef ENABLE_RR_DEBUG_INFO |
| 500 | // TODO(b/165000222): Update this on next LLVM roll. |
| 501 | // https://github.com/llvm/llvm-project/commit/98f2bb4461072347dcca7d2b1b9571b3a6525801 |
| 502 | // introduces RTDyldObjectLinkingLayer::registerJITEventListener(). |
| 503 | // The current API does not appear to have any way to bind the |
| 504 | // rr::DebugInfo::NotifyFreeingObject event. |
| 505 | objectLayer.setNotifyLoaded([](llvm::orc::VModuleKey, |
| 506 | const llvm::object::ObjectFile &obj, |
| 507 | const llvm::RuntimeDyld::LoadedObjectInfo &l) { |
| 508 | static std::atomic<uint64_t> unique_key{ 0 }; |
| 509 | rr::DebugInfo::NotifyObjectEmitted(unique_key++, obj, l); |
| 510 | }); |
| 511 | #endif // ENABLE_RR_DEBUG_INFO |
Ben Clayton | a7bc2b9 | 2020-03-26 11:24:49 +0000 | [diff] [blame] | 512 | |
Ben Clayton | ee18f39 | 2020-10-19 16:54:21 -0400 | [diff] [blame^] | 513 | if(JITGlobals::get()->getTargetTriple().isOSBinFormatCOFF()) |
| 514 | { |
| 515 | // Hack to support symbol visibility in COFF. |
| 516 | // Matches hack in llvm::orc::LLJIT::createObjectLinkingLayer(). |
| 517 | // See documentation on these functions for more detail. |
| 518 | objectLayer.setOverrideObjectFlagsWithResponsibilityFlags(true); |
| 519 | objectLayer.setAutoClaimResponsibilityForObjectSymbols(true); |
| 520 | } |
| 521 | |
| 522 | dylib.addGenerator(std::make_unique<ExternalSymbolGenerator>()); |
| 523 | |
| 524 | llvm::SmallVector<llvm::orc::SymbolStringPtr, 8> names(count); |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 525 | for(size_t i = 0; i < count; i++) |
| 526 | { |
| 527 | auto func = funcs[i]; |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 528 | func->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 529 | func->setDoesNotThrow(); |
Ben Clayton | ee18f39 | 2020-10-19 16:54:21 -0400 | [diff] [blame^] | 530 | if(!func->hasName()) |
| 531 | { |
| 532 | func->setName("f" + llvm::Twine(i).str()); |
| 533 | } |
| 534 | names[i] = mangle(func->getName()); |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 535 | } |
| 536 | |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 537 | // Once the module is passed to the compileLayer, the |
| 538 | // llvm::Functions are freed. Make sure funcs are not referenced |
| 539 | // after this point. |
| 540 | funcs = nullptr; |
| 541 | |
Ben Clayton | ee18f39 | 2020-10-19 16:54:21 -0400 | [diff] [blame^] | 542 | llvm::cantFail(compileLayer.add(dylib, llvm::orc::ThreadSafeModule(std::move(module), ctx))); |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 543 | |
| 544 | // Resolve the function addresses. |
| 545 | for(size_t i = 0; i < count; i++) |
| 546 | { |
Ben Clayton | ee18f39 | 2020-10-19 16:54:21 -0400 | [diff] [blame^] | 547 | auto symbol = session.lookup({ &dylib }, names[i]); |
| 548 | ASSERT_MSG(symbol, "Failed to lookup address of routine function %d: %s", |
| 549 | (int)i, llvm::toString(symbol.takeError()).c_str()); |
| 550 | addresses[i] = reinterpret_cast<void *>(static_cast<intptr_t>(symbol->getAddress())); |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 551 | } |
| 552 | } |
| 553 | |
| 554 | const void *getEntry(int index) const override |
| 555 | { |
| 556 | return addresses[index]; |
| 557 | } |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 558 | }; |
| 559 | |
| 560 | } // anonymous namespace |
| 561 | |
| 562 | namespace rr { |
| 563 | |
| 564 | JITBuilder::JITBuilder(const rr::Config &config) |
| 565 | : config(config) |
| 566 | , module(new llvm::Module("", context)) |
| 567 | , builder(new llvm::IRBuilder<>(context)) |
| 568 | { |
Ben Clayton | ee18f39 | 2020-10-19 16:54:21 -0400 | [diff] [blame^] | 569 | module->setDataLayout(JITGlobals::get()->getDataLayout()); |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | void JITBuilder::optimize(const rr::Config &cfg) |
| 573 | { |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 574 | #ifdef ENABLE_RR_DEBUG_INFO |
| 575 | if(debugInfo != nullptr) |
| 576 | { |
| 577 | return; // Don't optimize if we're generating debug info. |
| 578 | } |
| 579 | #endif // ENABLE_RR_DEBUG_INFO |
| 580 | |
Ben Clayton | ee18f39 | 2020-10-19 16:54:21 -0400 | [diff] [blame^] | 581 | llvm::legacy::PassManager passManager; |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 582 | |
| 583 | for(auto pass : cfg.getOptimization().getPasses()) |
| 584 | { |
| 585 | switch(pass) |
| 586 | { |
| 587 | case rr::Optimization::Pass::Disabled: break; |
Ben Clayton | ee18f39 | 2020-10-19 16:54:21 -0400 | [diff] [blame^] | 588 | case rr::Optimization::Pass::CFGSimplification: passManager.add(llvm::createCFGSimplificationPass()); break; |
| 589 | case rr::Optimization::Pass::LICM: passManager.add(llvm::createLICMPass()); break; |
| 590 | case rr::Optimization::Pass::AggressiveDCE: passManager.add(llvm::createAggressiveDCEPass()); break; |
| 591 | case rr::Optimization::Pass::GVN: passManager.add(llvm::createGVNPass()); break; |
| 592 | case rr::Optimization::Pass::InstructionCombining: passManager.add(llvm::createInstructionCombiningPass()); break; |
| 593 | case rr::Optimization::Pass::Reassociate: passManager.add(llvm::createReassociatePass()); break; |
| 594 | case rr::Optimization::Pass::DeadStoreElimination: passManager.add(llvm::createDeadStoreEliminationPass()); break; |
| 595 | case rr::Optimization::Pass::SCCP: passManager.add(llvm::createSCCPPass()); break; |
| 596 | case rr::Optimization::Pass::ScalarReplAggregates: passManager.add(llvm::createSROAPass()); break; |
| 597 | case rr::Optimization::Pass::EarlyCSEPass: passManager.add(llvm::createEarlyCSEPass()); break; |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 598 | default: |
| 599 | UNREACHABLE("pass: %d", int(pass)); |
| 600 | } |
| 601 | } |
| 602 | |
Ben Clayton | ee18f39 | 2020-10-19 16:54:21 -0400 | [diff] [blame^] | 603 | passManager.run(*module); |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | std::shared_ptr<rr::Routine> JITBuilder::acquireRoutine(llvm::Function **funcs, size_t count, const rr::Config &cfg) |
| 607 | { |
| 608 | ASSERT(module); |
| 609 | return std::make_shared<JITRoutine>(std::move(module), funcs, count, cfg); |
| 610 | } |
| 611 | |
| 612 | } // namespace rr |