Mneme
Mneme a tool allowing recording the execution of a GPU (CUDA/HIP) kernel and replaying that kernel as an independent executable.
Loading...
Searching...
No Matches
MnemeSnapshot.hpp
Go to the documentation of this file.
1#pragma once
2#include <algorithm>
3#include <cstdint>
4#include <cstring>
5#include <filesystem>
6#include <functional>
7#include <iostream>
8#include <llvm/ADT/ArrayRef.h>
9#include <llvm/ADT/DenseSet.h>
10#include <llvm/ADT/SmallVector.h>
11#include <llvm/Support/JSON.h>
12#include <llvm/Support/MD5.h>
13#include <llvm/Support/MemoryBuffer.h>
14#include <llvm/Support/raw_ostream.h>
15#include <memory>
16#include <mutex>
17#include <optional>
18#include <unordered_map>
19#include <vector>
20
21#include <llvm/ADT/StableHashing.h>
22#include <llvm/ADT/StringRef.h>
23#include <llvm/Bitcode/BitcodeReader.h>
24#include <llvm/IR/DebugInfo.h>
25#include <llvm/IR/DebugInfoMetadata.h>
26#include <llvm/IR/LLVMContext.h>
27#include <llvm/IR/Module.h>
28#include <llvm/Support/FileSystem.h>
29#include <llvm/Support/Path.h>
30#include <string>
31#include <sys/types.h>
32#include <type_traits>
33
35#include "mneme/MnemeConfig.hpp"
38#include "mneme/MnemeLogger.hpp"
39#include "mneme/MnemeMemory.hpp"
43#include "mneme/MnemeUtils.hpp"
44#include <proteus/KernelMetadata.h>
45
46namespace mneme {
47
48// Identifies a counted launch by its kernel, its launch configuration when that
49// has an instance entry, and its position among the kernel's launches.
51 uint64_t StaticHash;
52 std::optional<uint64_t> DynamicHash;
53 uint64_t LaunchIndex;
54};
55
56// Durations of an instance's timed launches, written as its TimeNs.
58 uint64_t Sum;
59 uint64_t Min;
60 uint64_t Max;
61
62 void add(uint64_t Ns) {
63 Sum += Ns;
64 Min = std::min(Min, Ns);
65 Max = std::max(Max, Ns);
66 }
67 llvm::json::Object toJSON() const {
68 return llvm::json::Object{{"Sum", Sum}, {"Min", Min}, {"Max", Max}};
69 }
70};
71
73 std::string PrologueFn;
74 std::string EpilogueFn;
76 dim3 GridDim;
77 llvm::SmallVector<double> ArgValues;
78 uint64_t NumOccurrences = 0;
79 uint64_t SharedMem;
80 uint64_t TimedLaunches = 0;
81 std::optional<LaunchTimes> TimeNs;
82 std::optional<uint64_t> RecordedLaunchIndex;
83 std::optional<uint64_t> RecordedLaunchTimeNs;
84 static llvm::json::Object toJSON(const dim3 &Dim) {
85 llvm::json::Object JSONDim;
86 JSONDim["x"] = Dim.x;
87 JSONDim["y"] = Dim.y;
88 JSONDim["z"] = Dim.z;
89 return JSONDim;
90 }
91 bool isRecorded() const { return RecordedLaunchIndex.has_value(); }
92 void addLaunchTime(uint64_t Ns, uint64_t LaunchIndex) {
93 if (TimeNs)
94 TimeNs->add(Ns);
95 else
96 TimeNs = LaunchTimes{Ns, Ns, Ns};
98 if (RecordedLaunchIndex == LaunchIndex)
100 }
101 llvm::json::Object toJSON(bool KernelTiming) const {
102 llvm::json::Object instance;
103 instance["Prologue"] = PrologueFn;
104 instance["Epilogue"] = EpilogueFn;
107 instance["SharedMem"] = SharedMem;
108 instance["Args"] = llvm::json::Array(ArgValues);
109 instance["Occurrences"] = NumOccurrences;
110 if (KernelTiming) {
111 instance["TimedLaunches"] = TimedLaunches;
112 if (TimeNs)
113 instance["TimeNs"] = TimeNs->toJSON();
115 instance["RecordedLaunchTimeNs"] = *RecordedLaunchTimeNs;
116 }
117 return instance;
118 }
119 KernelInstance(const dim3 &GridDim, const dim3 &BlockDim, uint64_t SharedMem)
121 KernelInstance() = default;
122};
123
124// Locates a recorded kernel's source from its line-table debug info, which
125// gives the file, the line range that generated code, and the MD5 checksum the
126// file had at compile time.
128 std::string Path;
129 std::string CopyName;
130 std::string MD5;
131 unsigned Line = 0;
132 unsigned EndLine = 0;
133 std::string CompileMD5;
134
135public:
136 SourceFileInfo() = default;
137
138 explicit SourceFileInfo(const llvm::Module &Mod, llvm::StringRef KernelName) {
139 const llvm::Function *F = Mod.getFunction(KernelName);
140 const llvm::DISubprogram *SP = F ? F->getSubprogram() : nullptr;
141 if (!SP)
142 return;
143
144 initFromSubprogram(*F, *SP);
145 }
146
147 bool isKnown() const { return !Path.empty(); }
148
149 // The copy is named by content hash so kernels from the same file share one.
150 void copyTo(const std::string &Dir) {
151 auto BufOrErr = llvm::MemoryBuffer::getFile(Path);
152 if (!BufOrErr) {
153 LOG_WARN("Cannot read source file {}: {}", Path,
154 BufOrErr.getError().message());
155 return;
156 }
157 llvm::StringRef Contents = (*BufOrErr)->getBuffer();
158
159 llvm::MD5 Hash;
160 Hash.update(Contents);
161 llvm::MD5::MD5Result Result;
162 Hash.final(Result);
163 std::string Digest = Result.digest().str().str();
164
165 // A copy that no longer matches what was compiled would misattribute the
166 // recorded line range, so keep the compile-time checksum instead.
167 if (!CompileMD5.empty() && CompileMD5 != Digest) {
168 LOG_WARN("Source file {} changed since it was compiled (compiled MD5 {}, "
169 "current MD5 {}); not copying it",
170 Path, CompileMD5, Digest);
171 return;
172 }
173
174 std::string Basename = std::filesystem::path(Path).filename().string();
175 std::string Filename = "RecordedSource_" + Digest + "_" + Basename;
176 std::filesystem::path Dest = std::filesystem::path(Dir) / Filename;
177 if (!std::filesystem::exists(Dest)) {
178 std::error_code EC;
179 llvm::raw_fd_ostream Out(Dest.string(), EC);
180 if (EC) {
181 LOG_WARN("Cannot write source copy {}: {}", Dest.string(),
182 EC.message());
183 return;
184 }
185 Out << Contents;
186 }
187
188 CopyName = Filename;
189 MD5 = Digest;
190 }
191
192 void addToJSON(llvm::json::Object &Obj) const {
193 if (!isKnown())
194 return;
195
196 Obj["SourceFile"] = Path;
197 Obj["SourceLine"] = Line;
198 Obj["SourceEndLine"] = EndLine;
199 if (!CopyName.empty())
200 Obj["SourceCopy"] = CopyName;
201 const std::string &Checksum = CompileMD5.empty() ? MD5 : CompileMD5;
202 if (!Checksum.empty())
203 Obj["SourceMD5"] = Checksum;
204 }
205
206private:
207 void initFromSubprogram(const llvm::Function &F,
208 const llvm::DISubprogram &SP) {
209 const llvm::DIFile *File = SP.getFile();
210 llvm::SmallString<256> Joined{File->getFilename()};
211 if (!llvm::sys::path::is_absolute(Joined)) {
212 Joined = File->getDirectory();
213 llvm::sys::path::append(Joined, File->getFilename());
214 }
215
216 llvm::SmallString<256> Real;
217 if (llvm::sys::fs::real_path(Joined, Real))
218 Path = Joined.str().str();
219 else
220 Path = Real.str().str();
221
222 Line = SP.getLine();
223 EndLine = findLastCodeLine(F, SP);
224
225 if (auto Checksum = File->getChecksum())
226 if (Checksum->Kind == llvm::DIFile::CSK_MD5)
227 CompileMD5 = Checksum->Value.str();
228 }
229
230 // The last line of the kernel that generated code, which for Clang is
231 // normally its closing brace. Proteus captures the bitcode before inlining,
232 // so every location in the function belongs to the kernel itself.
233 static unsigned findLastCodeLine(const llvm::Function &F,
234 const llvm::DISubprogram &SP) {
235 unsigned Last = SP.getScopeLine();
236 for (const llvm::BasicBlock &BB : F)
237 for (const llvm::Instruction &I : BB)
238 if (const llvm::DILocation *DL = I.getDebugLoc().get())
239 Last = std::max(Last, DL->getLine());
240 return Last;
241 }
242};
243
245 llvm::DenseMap<uint64_t, KernelInstance> Instances;
246 uint64_t NumRecords;
247 uint64_t TotalLaunches = 0;
248 uint64_t TotalTimeNs = 0;
250 llvm::SmallVector<size_t> KernelArgSizes;
251 llvm::SmallVector<std::string> KernelArgNames;
252 llvm::SmallVector<bool> KernelSpecializations;
253 llvm::SmallVector<std::function<double(void *)>> ConvertArgToDouble;
254 llvm::SmallVector<std::string> ModuleFiles;
255 const uint64_t StaticHash;
256 const std::string KName;
257 SourceFileInfo Source;
258 bool CopySource;
259
260private:
261 // Parse Proteus's serialized bitcode in a Mneme-owned LLVMContext and
262 // extract per-argument metadata. Operating on a Mneme-owned Module
263 // keeps Mneme's LLVM runtime from touching any
264 // Proteus-owned LLVM C++ object across the DSO boundary.
265 void extractArgInfoFromBitcode(llvm::StringRef Bitcode) {
266 auto Ctx = std::make_unique<llvm::LLVMContext>();
267 llvm::MemoryBufferRef BufRef(Bitcode, KName);
268 auto ModOrErr = llvm::parseBitcodeFile(BufRef, *Ctx);
269 if (!ModOrErr)
270 LOG_FATAL("parseBitcodeFile failed for kernel " + KName + ": " +
271 llvm::toString(ModOrErr.takeError()));
272 std::unique_ptr<llvm::Module> Mod = std::move(*ModOrErr);
273
274 llvm::Function *F = Mod->getFunction(KName);
275 if (!F)
276 LOG_FATAL("Function " + KName + " not found in parsed bitcode");
277
278 KernelArgSizes = mneme::getFuncDescr(*F);
279 KernelArgNames = mneme::getArgNames(*F);
280 KernelSpecializations = mneme::canSpecialize(*F);
281 ConvertArgToDouble = mneme::convertToDouble(*F);
282 Source = SourceFileInfo(*Mod, KName);
283 }
284
285 std::string StoreModuleBytes(llvm::StringRef Bytes,
286 const std::string &RecordReplayDir) {
287 std::string Filename(
288 std::filesystem::path(llvm::Twine(RecordReplayDir + "/RecordedIR_" +
289 std::to_string(StaticHash) + ".bc")
290 .str())
291 .string());
292
293 std::error_code EC;
294 llvm::raw_fd_ostream OutBC(Filename, EC);
295 if (EC)
296 LOG_FATAL("Cannot write module ir file");
297 OutBC << Bytes;
298 OutBC.close();
299
300 LOG_DEBUG("Stored Blob with StaticHash:{} to file {}", StaticHash,
301 std::filesystem::canonical(Filename).string());
302 return std::filesystem::path(Filename).filename().string();
303 }
304
305public:
306 llvm::json::Object toJSON(KernelTimingType Timing) const {
307 llvm::json::Object Collection;
308 Collection["StaticHash"] = StaticHash;
309 Collection["KernelName"] = KName;
310 Collection["DemangledName"] = util::demangleKernelName(KName);
311 Collection["Modules"] = llvm::json::Array(ModuleFiles);
312 Collection["BinaryBlobs"] = llvm::json::Array();
313 Collection["ArgNames"] = llvm::json::Array(KernelArgNames);
314 Collection["Specializations"] = llvm::json::Array(KernelSpecializations);
315 Source.addToJSON(Collection);
316 Collection["TotalLaunches"] = TotalLaunches;
317 bool KernelTiming = Timing != KernelTimingType::Off;
318 if (KernelTiming) {
319 Collection["TimingSource"] = getKernelTimingName(Timing);
320 Collection["TotalTimeNs"] = TotalTimeNs;
321 }
322 llvm::json::Object JSONInstances;
323 for (auto &[hash, KI] : Instances)
324 if (KI.isRecorded())
325 JSONInstances[std::to_string(hash)] = KI.toJSON(KernelTiming);
326 Collection["instances"] = std::move(JSONInstances);
327 return Collection;
328 }
329
330 KernelInstancesCollection(const proteus::runtime::KernelMetadata &KInfo,
331 const KernelRecordingPolicy &Policy,
332 bool CopySource)
333 : NumRecords(0), Policy(Policy), StaticHash(KInfo.getStaticHash()),
334 KName(KInfo.getName()), CopySource(CopySource) {}
335
336 bool hasRecords() const { return NumRecords > 0; }
337 const std::string &getName() const { return KName; }
338
339 void addLaunchTime(const LaunchHandle &Launch, uint64_t Ns) {
340 TotalTimeNs += Ns;
341 if (!Launch.DynamicHash)
342 return;
343 Instances.find(*Launch.DynamicHash)
344 ->second.addLaunchTime(Ns, Launch.LaunchIndex);
345 }
346
347 // Deferred to the first recording so unrecorded kernels write nothing.
348 void storeModule(const std::string &MnemeDirectory,
349 const proteus::runtime::KernelMetadata &KInfo) {
350 const auto &BitcodeBytes = KInfo.getBitcode();
351 llvm::StringRef Bitcode(BitcodeBytes.data(), BitcodeBytes.size());
352 if (Bitcode.empty())
353 LOG_FATAL("Empty bitcode for kernel " + KName);
354
355 extractArgInfoFromBitcode(Bitcode);
356 if (CopySource) {
357 if (Source.isKnown()) {
358 Source.copyTo(MnemeDirectory);
359 } else {
360 // Printed without the logger so that default builds still tell the
361 // user why the record has no source fields. One notice suffices since
362 // the fix is the same for every kernel.
363 static std::once_flag Warned;
364 std::call_once(Warned, [this] {
365 std::cerr << "[mneme] Kernel " << KName
366 << " has no line-table debug info; compile with "
367 "-gline-tables-only to record source locations "
368 "(reported once)\n";
369 });
370 }
371 }
372 ModuleFiles.emplace_back(StoreModuleBytes(Bitcode, MnemeDirectory));
373 }
374
375 LaunchHandle registerLaunch(const dim3 &GridDim, const dim3 &BlockDim,
376 uint64_t SharedMem) {
377 TotalLaunches++;
378 LaunchHandle Launch{StaticHash, std::nullopt, TotalLaunches};
379
380 auto DynamicHash = computeHash(GridDim, BlockDim, SharedMem);
381 auto It = Instances.find(DynamicHash);
382 if (It == Instances.end()) {
383 // A new launch configuration past the limit can never be recorded, so
384 // it is only counted in TotalLaunches and the map stays bounded.
385 if (NumRecords >= Policy.MaxRecords)
386 return Launch;
387 It = Instances.try_emplace(DynamicHash, GridDim, BlockDim, SharedMem)
388 .first;
389 }
390 It->second.NumOccurrences++;
391 Launch.DynamicHash = DynamicHash;
392 return Launch;
393 }
394
395 static llvm::stable_hash
396 computeHash(const dim3 &GridDim, const dim3 &BlockDim, uint64_t SharedMem) {
397 auto BlockHash = llvm::stable_hash_combine((llvm::stable_hash)BlockDim.x,
398 (llvm::stable_hash)BlockDim.y,
399 (llvm::stable_hash)BlockDim.z);
400 auto GridHash = llvm::stable_hash_combine((llvm::stable_hash)GridDim.x,
401 (llvm::stable_hash)GridDim.y,
402 (llvm::stable_hash)GridDim.z);
403 auto DHash = llvm::stable_hash_combine(GridHash, BlockHash, SharedMem);
404 return DHash;
405 }
406
407 // Returns the epilogue capture to run after the launch completes.
408 template <DeviceVendors VendorTypes>
409 std::optional<std::function<void()>>
410 takeSnapshot(std::filesystem::path &MnemeDir, const void *Kernel,
411 const LaunchHandle &Launch,
412 BlobsByAddress<VendorTypes> &DeviceMemory, void **Args,
414 EpilogueSnapshotType EpilogueType) {
415 if (!Launch.DynamicHash)
416 return std::nullopt;
417 uint64_t DynamicHash = *Launch.DynamicHash;
418
419 if (!Policy.selects(Launch.LaunchIndex)) {
420 LOG_DEBUG("Launch {} of kernel {} is not selected for recording",
421 Launch.LaunchIndex, KName);
422 return std::nullopt;
423 }
424
425 KernelInstance *Instance = &Instances.find(DynamicHash)->second;
426 if (Instance->isRecorded()) {
427 LOG_DEBUG(
428 "Kernel {} with DynamicHash {} is already recorded, skipping ...",
429 StaticHash, DynamicHash);
430 return std::nullopt;
431 }
432
433 if (NumRecords >= Policy.MaxRecords)
434 return std::nullopt;
435
436 // Captured only for recorded launches, because it copies the bitcode.
437 auto KInfo = proteus::runtime::captureKernelMetadata(Kernel);
438 if (!KInfo)
439 LOG_FATAL("Kernel metadata disappeared for counted kernel " + KName);
440 const auto &GlobalVars = KInfo->getGlobals();
441
442 if (NumRecords == 0)
443 storeModule(MnemeDir.string(), *KInfo);
444 NumRecords++;
445 Instance->RecordedLaunchIndex = Launch.LaunchIndex;
446
447 LOG_DEBUG("First Instance of Kernel {} with DynamicHash {}, recording ...",
448 StaticHash, DynamicHash);
449
450 std::filesystem::path Filename(MnemeDir /
451 (std::string("DeviceState.prologue.") +
452 std::to_string(StaticHash) + "." +
453 std::to_string(DynamicHash) + ".mneme"));
454
455 auto PrologueGlobals = std::make_shared<GlobalSnapshotData>();
456 SnapshotInput<VendorTypes> In{GlobalVars, DeviceMemory, KernelArgSizes,
457 Args, Stream};
458 Instance->PrologueFn =
459 BytesWriter<VendorTypes>(PrologueGlobals).write(Filename, In).string();
460
461 // std::function requires a copyable callable, so the writer is shared.
462 std::shared_ptr<SnapshotWriter<VendorTypes>> Writer =
463 makeEpilogueWriter<VendorTypes>(EpilogueType, PrologueGlobals);
464
465 return [this, DynamicHash, MnemeDir, GlobalVars, Writer, &DeviceMemory,
466 Args, Stream] {
467 std::filesystem::path Filename(MnemeDir /
468 (std::string("DeviceState.epilogue.") +
469 std::to_string(StaticHash) + "." +
470 std::to_string(DynamicHash) + ".mneme"));
471
472 SnapshotInput<VendorTypes> In{GlobalVars, DeviceMemory, KernelArgSizes,
473 Args, Stream};
474 Instances[DynamicHash].EpilogueFn = Writer->write(Filename, In).string();
475 };
476 }
477};
478
480 std::filesystem::path MnemeDirectory;
481 llvm::DenseMap<uint64_t, KernelInstancesCollection> KernelRecords;
482 llvm::DenseSet<uint64_t> FilteredKernels;
483 // Launches are counted by kernel address, so only a kernel's first launch
484 // pays for capturing its metadata.
485 llvm::DenseMap<const void *, std::optional<uint64_t>> CountedKernels;
486 EpilogueSnapshotType EpilogueType;
487 bool CopySource;
488 KernelTimingType Timing;
489
490public:
491 RecordDatabase(std::filesystem::path MnemeDirectory,
492 EpilogueSnapshotType EpilogueType, bool CopySource,
493 KernelTimingType Timing)
494 : MnemeDirectory(std::move(MnemeDirectory)), EpilogueType(EpilogueType),
495 CopySource(CopySource), Timing(Timing) {
496 // Logging here constructs the logger first so it outlives flush() at exit.
497 LOG_DEBUG("Recording into {}", MnemeDirectory.string());
498 }
499
500 void writeKernelJSON(uint64_t StaticHash) {
501 auto It = KernelRecords.find(StaticHash);
502 if (It == KernelRecords.end()) {
503 LOG_WARN("Attempted to write JSON for unrecorded kernel hash {}",
504 StaticHash);
505 return;
506 }
507 auto *RecordPtr = &It->second;
508
509 auto JsonFilename = MnemeDirectory / (std::to_string(StaticHash) + ".json");
510 auto JSONRecord = RecordPtr->toJSON(Timing);
511
512 std::error_code EC;
513 llvm::raw_fd_ostream JsonOS(JsonFilename.string(), EC);
514 if (EC) {
515 LOG_WARN("Failed to open JSON file for kernel {}: {}", StaticHash,
516 EC.message());
517 return;
518 }
519
520 JsonOS << llvm::json::Value(std::move(JSONRecord));
521 JsonOS.close();
522 if (JsonOS.has_error()) {
523 LOG_WARN("Failed to write JSON for kernel {}: {}", StaticHash,
524 JsonOS.error().message());
525 return;
526 }
527 }
528
529 void flush() {
530 for (const auto &Entry : KernelRecords)
531 if (Entry.second.hasRecords())
532 writeKernelJSON(Entry.first);
533 }
534
535 // Only a kernel's first launch resolves its policy, because kernels that can
536 // record get a record and kernels that cannot are remembered here.
537 std::optional<KernelRecordingPolicy>
538 filterLaunch(uint64_t StaticHash, const std::string &KernelName) {
539 if (FilteredKernels.contains(StaticHash))
540 return std::nullopt;
541
542 // Match the same original name displayed in the recording database.
543 auto Policy = RecordingPolicy::get().resolve(
544 util::demangleKernelName(KernelName), KernelName);
545 if (Policy.MaxRecords == 0) {
546 LOG_INFO("Skip record of Kernel {}", KernelName);
547 FilteredKernels.insert(StaticHash);
548 return std::nullopt;
549 }
550 return Policy;
551 }
552
554 findOrCreateRecord(const proteus::runtime::KernelMetadata &KInfo) {
555 auto StaticHash = KInfo.getStaticHash();
556 auto It = KernelRecords.find(StaticHash);
557 if (It == KernelRecords.end()) {
558 auto Policy = filterLaunch(StaticHash, KInfo.getName());
559 if (!Policy)
560 return nullptr;
561 It = KernelRecords.try_emplace(StaticHash, KInfo, *Policy, CopySource)
562 .first;
563 LOG_INFO("Created instance");
564 }
565 return &It->second;
566 }
567
568 // Returns nothing when Kernel has no record, because the recording filter
569 // rejects it or Proteus has no metadata for it.
570 std::optional<LaunchHandle> registerLaunch(const void *Kernel,
571 const dim3 &GridDim,
572 const dim3 &BlockDim,
573 size_t SharedMem) {
574 auto [It, Inserted] = CountedKernels.try_emplace(Kernel);
575 if (Inserted) {
576 auto KInfo = proteus::runtime::captureKernelMetadata(Kernel);
577 if (KInfo && findOrCreateRecord(*KInfo))
578 It->second = KInfo->getStaticHash();
579 }
580 if (!It->second)
581 return std::nullopt;
582 return KernelRecords.find(*It->second)
583 ->second.registerLaunch(GridDim, BlockDim, SharedMem);
584 }
585
586 template <DeviceVendors VendorTypes>
587 std::optional<std::function<void()>>
588 takeSnapshot(const void *Kernel, const LaunchHandle &Launch,
589 BlobsByAddress<VendorTypes> &DeviceMemory, void **Args,
591 return KernelRecords.find(Launch.StaticHash)
592 ->second.takeSnapshot<VendorTypes>(MnemeDirectory, Kernel, Launch,
593 DeviceMemory, Args, Stream,
594 EpilogueType);
595 }
596
597 const std::string &kernelName(uint64_t StaticHash) const {
598 return KernelRecords.find(StaticHash)->second.getName();
599 }
600
601 void addLaunchTime(const LaunchHandle &Launch, uint64_t Ns) {
602 KernelRecords.find(Launch.StaticHash)->second.addLaunchTime(Launch, Ns);
603 }
604};
605
606} // namespace mneme
#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:599
std::filesystem::path write(const std::filesystem::path &Filename, const SnapshotInput< VendorTypes > &In) const final
Definition MnemeSnapshotFormat.hpp:544
Definition MnemeSnapshot.hpp:244
llvm::json::Object toJSON(KernelTimingType Timing) const
Definition MnemeSnapshot.hpp:306
KernelInstancesCollection(const proteus::runtime::KernelMetadata &KInfo, const KernelRecordingPolicy &Policy, bool CopySource)
Definition MnemeSnapshot.hpp:330
void addLaunchTime(const LaunchHandle &Launch, uint64_t Ns)
Definition MnemeSnapshot.hpp:339
const std::string & getName() const
Definition MnemeSnapshot.hpp:337
std::optional< std::function< void()> > takeSnapshot(std::filesystem::path &MnemeDir, const void *Kernel, const LaunchHandle &Launch, BlobsByAddress< VendorTypes > &DeviceMemory, void **Args, typename DeviceTraits< VendorTypes >::DeviceStream_t Stream, EpilogueSnapshotType EpilogueType)
Definition MnemeSnapshot.hpp:410
static llvm::stable_hash computeHash(const dim3 &GridDim, const dim3 &BlockDim, uint64_t SharedMem)
Definition MnemeSnapshot.hpp:396
LaunchHandle registerLaunch(const dim3 &GridDim, const dim3 &BlockDim, uint64_t SharedMem)
Definition MnemeSnapshot.hpp:375
void storeModule(const std::string &MnemeDirectory, const proteus::runtime::KernelMetadata &KInfo)
Definition MnemeSnapshot.hpp:348
bool hasRecords() const
Definition MnemeSnapshot.hpp:336
RecordDatabase(std::filesystem::path MnemeDirectory, EpilogueSnapshotType EpilogueType, bool CopySource, KernelTimingType Timing)
Definition MnemeSnapshot.hpp:491
void writeKernelJSON(uint64_t StaticHash)
Definition MnemeSnapshot.hpp:500
std::optional< std::function< void()> > takeSnapshot(const void *Kernel, const LaunchHandle &Launch, BlobsByAddress< VendorTypes > &DeviceMemory, void **Args, typename DeviceTraits< VendorTypes >::DeviceStream_t Stream)
Definition MnemeSnapshot.hpp:588
std::optional< KernelRecordingPolicy > filterLaunch(uint64_t StaticHash, const std::string &KernelName)
Definition MnemeSnapshot.hpp:538
std::optional< LaunchHandle > registerLaunch(const void *Kernel, const dim3 &GridDim, const dim3 &BlockDim, size_t SharedMem)
Definition MnemeSnapshot.hpp:570
void flush()
Definition MnemeSnapshot.hpp:529
const std::string & kernelName(uint64_t StaticHash) const
Definition MnemeSnapshot.hpp:597
void addLaunchTime(const LaunchHandle &Launch, uint64_t Ns)
Definition MnemeSnapshot.hpp:601
KernelInstancesCollection * findOrCreateRecord(const proteus::runtime::KernelMetadata &KInfo)
Definition MnemeSnapshot.hpp:554
KernelRecordingPolicy resolve(const std::string &DemangledName, const std::string &MangledName="") const
Definition MnemeRecordingPolicy.cpp:230
static const RecordingPolicy & get()
Definition MnemeRecordingPolicy.cpp:220
Definition MnemeSnapshot.hpp:127
bool isKnown() const
Definition MnemeSnapshot.hpp:147
SourceFileInfo(const llvm::Module &Mod, llvm::StringRef KernelName)
Definition MnemeSnapshot.hpp:138
void copyTo(const std::string &Dir)
Definition MnemeSnapshot.hpp:150
void addToJSON(llvm::json::Object &Obj) const
Definition MnemeSnapshot.hpp:192
std::string demangleKernelName(const std::string &Name)
Definition MnemeLLVMUtils.hpp:35
Definition MnemeAnnotation.cpp:3
std::unique_ptr< SnapshotWriter< VendorTypes > > makeEpilogueWriter(EpilogueSnapshotType Type, std::shared_ptr< const GlobalSnapshotData > PrologueGlobals)
Definition MnemeSnapshotFormat.hpp:867
KernelTimingType
Definition MnemeConfig.hpp:26
@ Off
Definition MnemeConfig.hpp:26
const char * getKernelTimingName(KernelTimingType Type)
Definition MnemeConfig.hpp:38
EpilogueSnapshotType
Definition MnemeConfig.hpp:24
@ Bytes
Definition MnemeConfig.hpp:24
std::map< void *, MnemeMemoryBlob< VendorTypes > > BlobsByAddress
Definition MnemeMemory.hpp:605
auto & instance
Definition rocr_profile.cpp:205
Definition DeviceTraits.hpp:73
Definition MnemeSnapshot.hpp:72
static llvm::json::Object toJSON(const dim3 &Dim)
Definition MnemeSnapshot.hpp:84
dim3 BlockDim
Definition MnemeSnapshot.hpp:75
KernelInstance(const dim3 &GridDim, const dim3 &BlockDim, uint64_t SharedMem)
Definition MnemeSnapshot.hpp:119
std::string EpilogueFn
Definition MnemeSnapshot.hpp:74
uint64_t NumOccurrences
Definition MnemeSnapshot.hpp:78
uint64_t SharedMem
Definition MnemeSnapshot.hpp:79
std::optional< uint64_t > RecordedLaunchTimeNs
Definition MnemeSnapshot.hpp:83
llvm::json::Object toJSON(bool KernelTiming) const
Definition MnemeSnapshot.hpp:101
dim3 GridDim
Definition MnemeSnapshot.hpp:76
uint64_t TimedLaunches
Definition MnemeSnapshot.hpp:80
std::optional< uint64_t > RecordedLaunchIndex
Definition MnemeSnapshot.hpp:82
void addLaunchTime(uint64_t Ns, uint64_t LaunchIndex)
Definition MnemeSnapshot.hpp:92
llvm::SmallVector< double > ArgValues
Definition MnemeSnapshot.hpp:77
std::optional< LaunchTimes > TimeNs
Definition MnemeSnapshot.hpp:81
bool isRecorded() const
Definition MnemeSnapshot.hpp:91
std::string PrologueFn
Definition MnemeSnapshot.hpp:73
Definition MnemeRecordingPolicy.hpp:19
Definition MnemeSnapshot.hpp:50
uint64_t StaticHash
Definition MnemeSnapshot.hpp:51
uint64_t LaunchIndex
Definition MnemeSnapshot.hpp:53
std::optional< uint64_t > DynamicHash
Definition MnemeSnapshot.hpp:52
Definition MnemeSnapshot.hpp:57
llvm::json::Object toJSON() const
Definition MnemeSnapshot.hpp:67
uint64_t Max
Definition MnemeSnapshot.hpp:60
uint64_t Min
Definition MnemeSnapshot.hpp:59
uint64_t Sum
Definition MnemeSnapshot.hpp:58
void add(uint64_t Ns)
Definition MnemeSnapshot.hpp:62
Definition MnemeSnapshotFormat.hpp:495