[Openmp-commits] [openmp] [OpenMP] Add memory diff dump for kernel record-replay (PR #70667)

via Openmp-commits openmp-commits at lists.llvm.org
Wed Jan 17 15:15:39 PST 2024


https://github.com/nmustakin updated https://github.com/llvm/llvm-project/pull/70667

>From 345710b3d16d6f379cd6609d891254346d0dea36 Mon Sep 17 00:00:00 2001
From: nmustakin <nmust004 at ucr.edu>
Date: Wed, 17 Jan 2024 14:34:55 -0800
Subject: [PATCH] Add memory diff dump for kernel record-replay

---
 .../common/src/PluginInterface.cpp            | 65 ++++++++++++++++++-
 1 file changed, 64 insertions(+), 1 deletion(-)

diff --git a/openmp/libomptarget/plugins-nextgen/common/src/PluginInterface.cpp b/openmp/libomptarget/plugins-nextgen/common/src/PluginInterface.cpp
index 1bd70b85da3414..95df98e91a842b 100644
--- a/openmp/libomptarget/plugins-nextgen/common/src/PluginInterface.cpp
+++ b/openmp/libomptarget/plugins-nextgen/common/src/PluginInterface.cpp
@@ -181,6 +181,69 @@ struct RecordReplayTy {
     OS.close();
   }
 
+  void dumpDeviceMemoryDiff(StringRef Filename) {
+    ErrorOr<std::unique_ptr<WritableMemoryBuffer>> DeviceMemoryMB =
+        WritableMemoryBuffer::getNewUninitMemBuffer(MemorySize);
+    if (!DeviceMemoryMB)
+      report_fatal_error("Error creating MemoryBuffer for device memory");
+
+    auto Err = Device->dataRetrieve(DeviceMemoryMB.get()->getBufferStart(),
+                                    MemoryStart, MemorySize, nullptr);
+    if (Err)
+      report_fatal_error("Error retrieving data for target pointer");
+
+    // Get the pre-record memory filename.
+    SmallString<128> InputFilename = {Filename.split('.').first, ".memory"};
+
+    // Read the pre-record memorydump.
+    auto InputFileBuffer = MemoryBuffer::getFileOrSTDIN(InputFilename);
+    if (std::error_code EC = InputFileBuffer.getError())
+      report_fatal_error("Error reading pre-record device memory");
+
+    StringRef InputBufferContents = (*InputFileBuffer)->getBuffer();
+    if (InputBufferContents.size() != MemorySize)
+      report_fatal_error("Error: Pre-record device memory size mismatch");
+
+    std::error_code EC;
+    raw_fd_ostream OS(Filename, EC);
+    if (EC)
+      report_fatal_error("Error dumping memory to file " + Filename + " :" +
+                         EC.message());
+
+    // Get current memory contents.
+    StringRef DeviceMemoryContents(DeviceMemoryMB.get()->getBuffer().data(),
+                                   DeviceMemoryMB.get()->getBuffer().size());
+
+    // Loop over all memory locations.
+    // If mismatch is found, create a new diff line.
+    // Diff format: location, size, differences.
+    for (size_t I = 0; I < MemorySize; ++I) {
+      // If buffers are same, continue
+      if (InputBufferContents[I] == DeviceMemoryContents[I])
+        continue;
+
+      // Mark the start offset
+      OS << I << " ";
+
+      SmallVector<uint8_t, 128> Modified;
+      Modified.push_back(DeviceMemoryContents[I]);
+
+      for (I += 1; I < MemorySize; ++I) {
+        // If no more mismatch is found, break out of the loop.
+        if (InputBufferContents[I] == DeviceMemoryContents[I])
+          break;
+        // If mismatch continues - push diff to Modified.
+        Modified.push_back(DeviceMemoryContents[I]);
+      }
+      // Mark the length of the mismatching sequence.
+      OS << Modified.size() << " ";
+      for (const auto &Value : Modified)
+        OS << Value << " ";
+      OS << "\n";
+    }
+    OS.close();
+  }
+
 public:
   bool isRecording() const { return Status == RRStatusTy::RRRecording; }
   bool isReplaying() const { return Status == RRStatusTy::RRReplaying; }
@@ -302,7 +365,7 @@ struct RecordReplayTy {
   void saveKernelOutputInfo(const char *Name) {
     SmallString<128> OutputFilename = {
         Name, (isRecording() ? ".original.output" : ".replay.output")};
-    dumpDeviceMemory(OutputFilename);
+    dumpDeviceMemoryDiff(OutputFilename);
   }
 
   void *alloc(uint64_t Size) {



More information about the Openmp-commits mailing list