From 0f4b4e8e4d8b3aafba829143e2fd4237620ea4d7 Mon Sep 17 00:00:00 2001 From: Giorgis Georgakoudis Date: Tue, 13 Dec 2022 22:57:02 -0800 Subject: [PATCH] [OpenMP] RecordReplay saves bitcode when JIT-ing This patch enables to store bitcode images when JIT is enabled for the record-and-replay functionality (see https://reviews.llvm.org/D138931). Credits to @jdoerfert for refactoring the code. Reviewed By: jdoerfert Differential Revision: https://reviews.llvm.org/D141986 --- .../common/PluginInterface/PluginInterface.cpp | 12 +++++++++++- .../plugins-nextgen/common/PluginInterface/PluginInterface.h | 11 ++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/openmp/libomptarget/plugins-nextgen/common/PluginInterface/PluginInterface.cpp b/openmp/libomptarget/plugins-nextgen/common/PluginInterface/PluginInterface.cpp index d20fbf6..7cc1aa2 100644 --- a/openmp/libomptarget/plugins-nextgen/common/PluginInterface/PluginInterface.cpp +++ b/openmp/libomptarget/plugins-nextgen/common/PluginInterface/PluginInterface.cpp @@ -122,7 +122,15 @@ public: raw_fd_ostream OS(ImageName.str(), EC); if (EC) report_fatal_error("Error saving image : " + StringRef(EC.message())); - OS << Image.getMemoryBuffer().getBuffer(); + if (auto TgtImageBitcode = Image.getTgtImageBitcode()) { + size_t Size = + getPtrDiff(TgtImageBitcode->ImageEnd, TgtImageBitcode->ImageStart); + MemoryBufferRef MBR = MemoryBufferRef( + StringRef((const char *)TgtImageBitcode->ImageStart, Size), ""); + OS << MBR.getBuffer(); + } else { + OS << Image.getMemoryBuffer().getBuffer(); + } OS.close(); } @@ -407,6 +415,8 @@ GenericDeviceTy::loadBinary(GenericPluginTy &Plugin, DeviceImageTy *Image = *ImageOrErr; assert(Image != nullptr && "Invalid image"); + if (InputTgtImage != PostJITImageOrErr.get()) + Image->setTgtImageBitcode(InputTgtImage); // Add the image to list. LoadedImages.push_back(Image); diff --git a/openmp/libomptarget/plugins-nextgen/common/PluginInterface/PluginInterface.h b/openmp/libomptarget/plugins-nextgen/common/PluginInterface/PluginInterface.h index d65209d..e53c666 100644 --- a/openmp/libomptarget/plugins-nextgen/common/PluginInterface/PluginInterface.h +++ b/openmp/libomptarget/plugins-nextgen/common/PluginInterface/PluginInterface.h @@ -113,13 +113,14 @@ class DeviceImageTy { /// The pointer to the raw __tgt_device_image. const __tgt_device_image *TgtImage; + const __tgt_device_image *TgtImageBitcode; /// Table of offload entries. OffloadEntryTableTy OffloadEntryTable; public: DeviceImageTy(int32_t Id, const __tgt_device_image *Image) - : ImageId(Id), TgtImage(Image) { + : ImageId(Id), TgtImage(Image), TgtImageBitcode(nullptr) { assert(TgtImage && "Invalid target image"); } @@ -129,6 +130,14 @@ public: /// Get the pointer to the raw __tgt_device_image. const __tgt_device_image *getTgtImage() const { return TgtImage; } + void setTgtImageBitcode(const __tgt_device_image *TgtImageBitcode) { + this->TgtImageBitcode = TgtImageBitcode; + } + + const __tgt_device_image *getTgtImageBitcode() const { + return TgtImageBitcode; + } + /// Get the image starting address. void *getStart() const { return TgtImage->ImageStart; } -- 2.7.4