3#include <llvm/ADT/STLExtras.h>
4#include <llvm/Bitcode/BitcodeReader.h>
5#include <llvm/IR/LLVMContext.h>
6#include <llvm/IR/Module.h>
7#include <llvm/Support/FileSystem.h>
8#include <llvm/Support/JSON.h>
9#include <llvm/Support/MemoryBuffer.h>
39 std::shared_ptr<KernelInfo>
KInfo;
41 std::unordered_map<std::string, ReplayGlobalVar>
GlobalVars;
42 std::unique_ptr<void *[]>
Args;
48 LOG_DEBUG(
"Initialized replay memory state for kernel {}",
55 LOG_DEBUG(
"Copying data of variable {} to device addr {} and of size {}",
56 GVName, GVI.DevAddr, GVI.VarSize);
57 auto CEC = MnemeDeviceRT::DeviceErrorCheck(
58 MnemeDeviceRT::DeviceCopy(GVI.DevAddr, GVI.HostAddr, GVI.VarSize,
59 MnemeDeviceRT::MemcpyHostToDeviceKind()));
61 LOG_FATAL(
"Could not copy global " + GVName +
62 " to device EC: " + CEC.value() +
"\n");
67 std::unique_ptr<void *[]> copyOutArgs()
const {
68 void **
Args =
new void *[
KInfo->getNumArgs()];
69 auto ArgData =
KInfo->getArgData();
71 Args[I] = ArgData[I].get();
73 std::unique_ptr<void *[]> ArgUniquePtr{
Args};
91 LOG_WARN(
"Could not release replay memory blob: {}", EC.value());
100 const std::unordered_map<std::string, ReplayGlobalVar> &
getGlobalVars()
const {
108 void **
getArgs()
const {
return reinterpret_cast<void **
>(
Args.get()); }
115 auto [LoadedAddr, LoadedSize] =
118 if (KV.second.DevAddr != LoadedAddr) {
119 LOG_WARN(
"Global : {} was loaded on different addresses Record:{} vs "
121 KV.first, KV.second.DevAddr, LoadedAddr);
122 KV.second.DevAddr = LoadedAddr;
125 if (KV.second.VarSize != LoadedSize)
127 "has a different size between record and replay\n" +
128 "Record Size:" + std::to_string(KV.second.VarSize) +
129 "\nReplay Size:" + std::to_string(LoadedSize));
133 KV.second.DevAddr, KV.second.HostAddr, KV.second.VarSize,
136 LOG_FATAL(
"Copying Global :" + KV.first +
137 " from host to device raised error\nEC: " + EC.value());
138 LOG_INFO(
"Successfully loaded global variable: {}", KV.first);
144template <DeviceVendors VendorTypes>
150 PrologueState(
const std::string &KernelName,
const std::string &SnapshotFile)
158 std::vector<std::pair<uintptr_t, Blob *>> Blobs;
159 for (
auto &[DevAddr, MemBlob] : this->DeviceMemoryState)
160 Blobs.emplace_back(
reinterpret_cast<uintptr_t
>(DevAddr), &MemBlob);
161 llvm::sort(Blobs, llvm::less_first());
164 MnemeDeviceRT::getDevice(DeviceID);
165 uint64_t PageSize = MnemeDeviceRT::getMinPageSize(DeviceID);
168 auto StartOf = [&](
size_t I, uint64_t Unit) {
169 return Blobs[I].first & ~(Unit - 1);
171 auto EndOf = [&](
size_t I, uint64_t Unit) {
172 return util::roundUp(Blobs[I].first + Blobs[I].second->getActualSize(),
177 auto Group = [&](
size_t I,
size_t Limit, uint64_t Slack, uint64_t Unit) {
178 uintptr_t End = EndOf(I, Unit);
180 for (; J < Limit && StartOf(J, Unit) <= End + Slack; ++J)
181 End = std::max(End, EndOf(J, Unit));
182 return std::make_pair(J, End);
184 auto MapGroup = [&](
size_t I,
size_t J, uintptr_t End, uint64_t Unit) {
185 uintptr_t Start = StartOf(I, Unit);
187 uint64_t
Size = End - Start;
188 auto Status = Blobs[I].second->mapFixed(
189 reinterpret_cast<void *
>(Blobs[I].first),
190 reinterpret_cast<void *
>(Start),
Size,
196 for (
size_t K = I; K < J; ++K)
197 Blobs[K].second->mapInto(
reinterpret_cast<void *
>(Blobs[K].first));
201 for (
size_t I = 0, N = Blobs.size(); I < N;) {
208 uint64_t Unit = BlockSize;
209 for (
size_t K = I; K < J;) {
210 auto [L, E] = Group(K, J, 0, Unit);
211 auto Status = MapGroup(K, L, E, Unit);
217 LOG_FATAL(
"Cannot map recorded range {}-{}: {}\n{}",
218 reinterpret_cast<void *
>(StartOf(K, Unit)),
219 reinterpret_cast<void *
>(E),
230 for (
auto &[DevAddr, MemBlob] : this->DeviceMemoryState) {
231 MemBlob.moveDataToDevice();
232 restore(MemBlob,
nullptr);
234 auto EC = MnemeDeviceRT::DeviceErrorCheck(
235 MnemeDeviceRT::DeviceStreamSynchronize(
nullptr));
237 LOG_FATAL(
"Could not finish loading the prologue: {}", EC.value());
241 void reset(
typename MnemeDeviceRT::DeviceStream_t Stream)
override {
242 for (
const auto &[DevAddr, MemBlob] : this->DeviceMemoryState)
243 restore(MemBlob, Stream);
251 void restore(
const Blob &MemBlob,
252 typename MnemeDeviceRT::DeviceStream_t Stream) {
253 if (MemBlob.getSize() == 0)
255 auto EC = MnemeDeviceRT::DeviceErrorCheck(MnemeDeviceRT::DeviceCopyAsync(
256 MemBlob.getBlobAddr(), MemBlob.getDeviceData(), MemBlob.getSize(),
257 MnemeDeviceRT::MemcpyDeviceToDeviceKind(), Stream));
259 LOG_FATAL(
"Could not restore memory blob at {}: {}",
260 MemBlob.getBlobAddr(), EC.value());
267template <DeviceVendors VendorTypes>
274 for (
auto &[DevAddr, MemBlob] : this->DeviceMemoryState)
275 MemBlob.moveDataToDevice();
288 return compare(Prologue,
true);
295 return compare(Prologue,
false);
302 bool GlobalsOnDevice)
const {
309 auto It = this->DeviceMemoryState.find(DevAddr);
310 if (It == this->DeviceMemoryState.end()) {
311 LOG_WARN(
"Cannot find {} in comparators", DevAddr);
314 auto &EpiBlob = It->second;
315 if (EpiBlob.getSize() != ProBlob.getSize()) {
316 LOG_WARN(
"Sizes Differ {} vs {}", ProBlob.getSize(), EpiBlob.getSize());
319 if (!EpiBlob.recordedDataMatches(ProBlob))
328 LOG_WARN(
"comparing with global var {} that exists only on one of the "
335 auto &EpiGV = It->second;
336 if (!GlobalsOnDevice) {
337 if (memcmp(EpiGV.HostAddr, ProGV.HostAddr, ProGV.VarSize) != 0)
342 std::unique_ptr<uint8_t[]> ProData(
new uint8_t[ProGV.VarSize]);
343 auto CEC = DeviceTraits<VendorTypes>::DeviceErrorCheck(
344 DeviceTraits<VendorTypes>::DeviceCopy(
345 ProData.get(), ProGV.DevAddr, ProGV.VarSize,
346 DeviceTraits<VendorTypes>::MemcpyDeviceToHostKind()));
348 LOG_FATAL(
"Could not copy global from device EC: " + CEC.value() +
351 if (memcmp(EpiGV.HostAddr, ProData.get(), ProGV.VarSize) != 0)
355 LOG_DEBUG(
"Memory States {}", Correct ?
"are the same" :
"differ");
360template <DeviceVendors VendorTypes>
361std::unique_ptr<ReplayMemState<VendorTypes>>
363 const std::string &SnapshotFile) {
364 return std::make_unique<PrologueState<VendorTypes>>(KernelName, SnapshotFile);
367template <DeviceVendors VendorTypes>
368std::unique_ptr<ReplayMemState<VendorTypes>>
370 const std::string &SnapshotFile,
371 const std::string &BasePrologueFile) {
375 return std::make_unique<EpilogueState<VendorTypes>>(std::move(Snap));
#define LOG_INFO(...)
Definition MnemeLogger.hpp:110
#define LOG_DEBUG(...)
Definition MnemeLogger.hpp:109
#define LOG_FATAL(...)
Definition MnemeLogger.hpp:113
#define LOG_WARN(...)
Definition MnemeLogger.hpp:111
Definition MnemeSnapshotFormat.hpp:162
Definition MnemeReplay.hpp:268
bool matchesUnlaunched(const PrologueState< VendorTypes > &Prologue) const
Definition MnemeReplay.hpp:294
EpilogueState< VendorTypes > * asEpilogue() override
Definition MnemeReplay.hpp:283
EpilogueState(Snapshot< VendorTypes > SnapshotState)
Definition MnemeReplay.hpp:270
void reset(typename DeviceTraits< VendorTypes >::DeviceStream_t) override
Definition MnemeReplay.hpp:279
void load() override
Definition MnemeReplay.hpp:273
virtual bool matches(const PrologueState< VendorTypes > &Prologue) const
Definition MnemeReplay.hpp:287
Definition MnemeMemory.hpp:227
Definition MnemeReplay.hpp:145
void load() override
Definition MnemeReplay.hpp:157
PrologueState(const std::string &KernelName, const std::string &SnapshotFile)
Definition MnemeReplay.hpp:150
PrologueState< VendorTypes > * asPrologue() override
Definition MnemeReplay.hpp:247
void reset(typename MnemeDeviceRT::DeviceStream_t Stream) override
Definition MnemeReplay.hpp:241
typename MnemeDeviceRT::KernelFunction_t KernelFunction_t
Definition MnemeReplay.hpp:35
DeviceTraits< VendorTypes > MnemeDeviceRT
Definition MnemeReplay.hpp:32
void release()
Definition MnemeReplay.hpp:87
const std::unordered_map< std::string, ReplayGlobalVar > & getGlobalVars() const
Definition MnemeReplay.hpp:100
typename MnemeDeviceRT::DeviceError_t DeviceError_t
Definition MnemeReplay.hpp:33
std::shared_ptr< KernelInfo > KInfo
Definition MnemeReplay.hpp:39
uint64_t getNumArgs() const
Definition MnemeReplay.hpp:110
void ** getArgs() const
Definition MnemeReplay.hpp:108
ReplayMemState(Snapshot< VendorTypes > SnapshotState)
Definition MnemeReplay.hpp:44
typename MnemeDeviceRT::DeviceStream_t DeviceStream_t
Definition MnemeReplay.hpp:34
std::unique_ptr< void *[]> Args
Definition MnemeReplay.hpp:42
typename DeviceTraits< VendorTypes >::DeviceModule_t DeviceModule_t
Definition MnemeReplay.hpp:36
std::unordered_map< std::string, ReplayGlobalVar > GlobalVars
Definition MnemeReplay.hpp:41
const BlobsByAddress< VendorTypes > & getDeviceMemory() const
Definition MnemeReplay.hpp:96
virtual ~ReplayMemState()
Definition MnemeReplay.hpp:79
BlobsByAddress< VendorTypes > DeviceMemoryState
Definition MnemeReplay.hpp:40
void copyGlobals()
Definition MnemeReplay.hpp:53
virtual EpilogueState< VendorTypes > * asEpilogue()
Definition MnemeReplay.hpp:106
virtual PrologueState< VendorTypes > * asPrologue()
Definition MnemeReplay.hpp:105
void initializeGlobals(DeviceModule_t VendorMod)
Definition MnemeReplay.hpp:112
virtual void reset(DeviceStream_t Stream)=0
int64_t u64 Size
Definition cupti_profile.cpp:179
std::string getMappingsIn(uintptr_t Lo, uintptr_t Hi)
Definition MnemeVASpace.hpp:43
constexpr uint64_t LargePageSize
Definition MnemeVASpace.hpp:18
Ty roundUp(Ty Size, Ty Divider)
Definition MnemeUtils.hpp:16
uint64_t mapAlignment(uintptr_t Addr, uint64_t Size, uint64_t PageSize)
Definition MnemeVASpace.hpp:21
Definition MnemeAnnotation.cpp:3
std::unique_ptr< ReplayMemState< VendorTypes > > makeReplayPrologueState(const std::string &KernelName, const std::string &SnapshotFile)
Definition MnemeReplay.hpp:362
@ Mapped
Definition DeviceTraits.hpp:71
@ Occupied
Definition DeviceTraits.hpp:71
std::unique_ptr< ReplayMemState< VendorTypes > > makeReplayEpilogueState(const std::string &KernelName, const std::string &SnapshotFile, const std::string &BasePrologueFile)
Definition MnemeReplay.hpp:369
std::map< void *, MnemeMemoryBlob< VendorTypes > > BlobsByAddress
Definition MnemeMemory.hpp:605
Definition DeviceTraits.hpp:73
Definition MnemeSnapshotFormat.hpp:65