[PATCH] D154826: [BOLT] Make sure temp object file is always written

Job Noorman via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 11 00:35:58 PDT 2023


This revision was automatically updated to reflect the committed changes.
Closed by commit rGf2f1e670b097: [BOLT] Make sure temp object file is always written (authored by jobnoorman).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D154826/new/

https://reviews.llvm.org/D154826

Files:
  bolt/lib/Rewrite/RewriteInstance.cpp


Index: bolt/lib/Rewrite/RewriteInstance.cpp
===================================================================
--- bolt/lib/Rewrite/RewriteInstance.cpp
+++ bolt/lib/Rewrite/RewriteInstance.cpp
@@ -3167,24 +3167,14 @@
 void RewriteInstance::emitAndLink() {
   NamedRegionTimer T("emitAndLink", "emit and link", TimerGroupName,
                      TimerGroupDesc, opts::TimeRewrite);
-  std::error_code EC;
-
-  // This is an object file, which we keep for debugging purposes.
-  // Once we decide it's useless, we should create it in memory.
-  SmallString<128> OutObjectPath;
-  sys::fs::getPotentiallyUniqueTempFileName("output", "o", OutObjectPath);
-  std::unique_ptr<ToolOutputFile> TempOut =
-      std::make_unique<ToolOutputFile>(OutObjectPath, EC, sys::fs::OF_None);
-  check_error(EC, "cannot create output object file");
 
-  std::unique_ptr<buffer_ostream> BOS =
-      std::make_unique<buffer_ostream>(TempOut->os());
-  raw_pwrite_stream *OS = BOS.get();
+  SmallString<0> ObjectBuffer;
+  raw_svector_ostream OS(ObjectBuffer);
 
   // Implicitly MCObjectStreamer takes ownership of MCAsmBackend (MAB)
   // and MCCodeEmitter (MCE). ~MCObjectStreamer() will delete these
   // two instances.
-  std::unique_ptr<MCStreamer> Streamer = BC->createStreamer(*OS);
+  std::unique_ptr<MCStreamer> Streamer = BC->createStreamer(OS);
 
   if (EHFrameSection) {
     if (opts::UseOldText || opts::StrictMode) {
@@ -3205,6 +3195,18 @@
     exit(1);
   }
 
+  if (opts::KeepTmp) {
+    SmallString<128> OutObjectPath;
+    sys::fs::getPotentiallyUniqueTempFileName("output", "o", OutObjectPath);
+    std::error_code EC;
+    raw_fd_ostream FOS(OutObjectPath, EC);
+    check_error(EC, "cannot create output object file");
+    FOS << ObjectBuffer;
+    outs() << "BOLT-INFO: intermediary output object file saved for debugging "
+              "purposes: "
+           << OutObjectPath << "\n";
+  }
+
   ErrorOr<BinarySection &> TextSection =
       BC->getUniqueSectionByName(BC->getMainCodeSectionName());
   if (BC->HasRelocations && TextSection)
@@ -3216,7 +3218,7 @@
 
   // Get output object as ObjectFile.
   std::unique_ptr<MemoryBuffer> ObjectMemBuffer =
-      MemoryBuffer::getMemBuffer(BOS->str(), "in-memory object file", false);
+      MemoryBuffer::getMemBuffer(ObjectBuffer, "in-memory object file", false);
 
   auto EFMM = std::make_unique<ExecutableFileMemoryManager>(*BC);
   EFMM->setNewSecPrefix(getNewSecPrefix());
@@ -3270,13 +3272,6 @@
     outs() << "BOLT-INFO: cache metrics after emitting functions:\n";
     CacheMetrics::printAll(BC->getSortedFunctions());
   }
-
-  if (opts::KeepTmp) {
-    TempOut->keep();
-    outs() << "BOLT-INFO: intermediary output object file saved for debugging "
-              "purposes: "
-           << OutObjectPath << "\n";
-  }
 }
 
 void RewriteInstance::updateMetadata() {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D154826.538934.patch
Type: text/x-patch
Size: 2845 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230711/efc9821c/attachment.bin>


More information about the llvm-commits mailing list