| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // Host GPU shim for sanitizer runtimes. HSA is resolved, never linked. |
| 10 | // Callers must not include a system hsa.h alongside this header. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef SANITIZER_OFFLOAD_H |
| 15 | #define SANITIZER_OFFLOAD_H |
| 16 | |
| 17 | #include "sanitizer_atomic.h" |
| 18 | #include "sanitizer_common.h" |
| 19 | #include "sanitizer_mutex.h" |
| 20 | #include "sanitizer_offload_hsa.h" |
| 21 | #include "sanitizer_symbolizer.h" |
| 22 | |
| 23 | namespace __sanitizer { |
| 24 | |
| 25 | struct OffloadRpc; |
| 26 | |
| 27 | class Offload { |
| 28 | public: |
| 29 | static Offload& Get(); |
| 30 | |
| 31 | bool Init(); |
| 32 | void Shutdown(); |
| 33 | bool Ready() const; |
| 34 | |
| 35 | const void* HostPointer(uptr DeviceAddr); |
| 36 | |
| 37 | using Handler = u32 (*)(void* Port, u32 Lanes); |
| 38 | void RegisterHandler(Handler Fn); |
| 39 | |
| 40 | void TrackExecutable(hsa_executable_t Exec); |
| 41 | void UntrackExecutable(hsa_executable_t Exec); |
| 42 | void UntrackImages(); |
| 43 | SymbolizedStack* Symbolize(uptr PC); |
| 44 | |
| 45 | private: |
| 46 | friend struct OffloadRpc; |
| 47 | |
| 48 | constexpr Offload() = default; |
| 49 | |
| 50 | static Offload Ctx; |
| 51 | |
| 52 | struct Device { |
| 53 | hsa_agent_t Agent; |
| 54 | hsa_amd_memory_pool_t Pool; |
| 55 | u32 Lanes; |
| 56 | u32 MaxWaves; |
| 57 | }; |
| 58 | |
| 59 | struct Api { |
| 60 | #define SANITIZER_HSA_DECLARE(Name) decltype(&::Name) Name; |
| 61 | SANITIZER_HSA_FUNCTIONS(SANITIZER_HSA_DECLARE) |
| 62 | #undef SANITIZER_HSA_DECLARE |
| 63 | } Api{}; |
| 64 | |
| 65 | bool Resolve(); |
| 66 | bool Discover(); |
| 67 | bool BindLoader(); |
| 68 | bool Release(); |
| 69 | void Teardown(); |
| 70 | |
| 71 | const Device& Host() const { return HostDevice; } |
| 72 | const InternalMmapVectorNoCtor<Device>& Devices() const { return DeviceList; } |
| 73 | |
| 74 | bool Alloc(const Device& D, uptr Bytes, void** Out); |
| 75 | void Free(void* P); |
| 76 | bool Copy(void* Dst, const void* Src, uptr N); |
| 77 | bool Lookup(hsa_executable_t Exec, const char* Name, hsa_agent_t Agent, |
| 78 | u64* Addr); |
| 79 | bool CreateSignal(hsa_signal_t* Out); |
| 80 | void DestroySignal(hsa_signal_t Sig); |
| 81 | void WaitSignal(hsa_signal_t Sig); |
| 82 | void StoreSignal(hsa_signal_t Sig); |
| 83 | |
| 84 | void TrackImage(uptr LoadBase, uptr LoadSize, const void* Storage, |
| 85 | uptr StorageSize); |
| 86 | void UntrackImage(uptr LoadBase); |
| 87 | bool ExecutableInfo(hsa_loaded_code_object_t Obj, |
| 88 | hsa_ven_amd_loader_loaded_code_object_info_t Attr, |
| 89 | u64* Out); |
| 90 | template <typename Cb> |
| 91 | void ForEachAgentObject(hsa_executable_t Exec, Cb F); |
| 92 | |
| 93 | Device HostDevice{}; |
| 94 | InternalMmapVectorNoCtor<Device> DeviceList{}; |
| 95 | |
| 96 | // Serializes initialization and shutdown of the core HSA interface. |
| 97 | Mutex LifetimeMtx{}; |
| 98 | |
| 99 | // Serializes HSA functions that affect the common compiler-rt state. |
| 100 | Mutex OffloadMtx{}; |
| 101 | uptr Refs{}; |
| 102 | atomic_uint8_t Active{}; |
| 103 | LoaderApi Loader{}; |
| 104 | }; |
| 105 | |
| 106 | } // namespace __sanitizer |
| 107 | |
| 108 | #endif // SANITIZER_OFFLOAD_H |
| 109 | |