[PATCH] D69293: [Support] Add in memory flag for FileOutputBuffer

Nick Terrell via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 21 20:41:12 PDT 2019


terrelln created this revision.
terrelln added reviewers: rui314, MaskRay.
Herald added a subscriber: hiraditya.
Herald added a project: LLVM.
terrelln edited reviewers, added: ruiu; removed: rui314.

Adds a flag F_in_memory to prefer in-memory output buffers over on-disk
output buffers.

We need this to support -no-mmap-output-file in LLD. LLD currently
explicitly ignores this flag for compatibility with the GNU linker.

Test Plan:

  ninja check-llvm


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D69293

Files:
  llvm/include/llvm/Support/FileOutputBuffer.h
  llvm/lib/Support/FileOutputBuffer.cpp
  llvm/unittests/Support/FileOutputBufferTest.cpp


Index: llvm/unittests/Support/FileOutputBufferTest.cpp
===================================================================
--- llvm/unittests/Support/FileOutputBufferTest.cpp
+++ llvm/unittests/Support/FileOutputBufferTest.cpp
@@ -118,6 +118,28 @@
   EXPECT_TRUE(IsExecutable);
   ASSERT_NO_ERROR(fs::remove(File4.str()));
 
+  // TEST 5: In-memory buffer works as expected.
+  SmallString<128> File5(TestDirectory);
+  File5.append("/file5");
+  {
+    Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
+        FileOutputBuffer::create(File5, 8000, FileOutputBuffer::F_in_memory);
+    ASSERT_NO_ERROR(errorToErrorCode(BufferOrErr.takeError()));
+    std::unique_ptr<FileOutputBuffer> &Buffer = *BufferOrErr;
+    // Start buffer with special header.
+    memcpy(Buffer->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20);
+    ASSERT_NO_ERROR(errorToErrorCode(Buffer->commit()));
+    // Write to end of buffer to verify it is writable.
+    memcpy(Buffer->getBufferEnd() - 20, "AABBCCDDEEFFGGHHIIJJ", 20);
+    // Commit buffer.
+    ASSERT_NO_ERROR(errorToErrorCode(Buffer->commit()));
+  }
+
+  // Verify file is correct size.
+  uint64_t File5Size;
+  ASSERT_NO_ERROR(fs::file_size(Twine(File5), File5Size));
+  ASSERT_EQ(File5Size, 8000ULL);
+  ASSERT_NO_ERROR(fs::remove(File5.str()));
   // Clean up.
   ASSERT_NO_ERROR(fs::remove(TestDirectory.str()));
 }
Index: llvm/lib/Support/FileOutputBuffer.cpp
===================================================================
--- llvm/lib/Support/FileOutputBuffer.cpp
+++ llvm/lib/Support/FileOutputBuffer.cpp
@@ -189,7 +189,10 @@
   case fs::file_type::regular_file:
   case fs::file_type::file_not_found:
   case fs::file_type::status_error:
-    return createOnDiskBuffer(Path, Size, Mode);
+    if (Flags & F_in_memory)
+      return createInMemoryBuffer(Path, Size, Mode);
+    else
+      return createOnDiskBuffer(Path, Size, Mode);
   default:
     return createInMemoryBuffer(Path, Size, Mode);
   }
Index: llvm/include/llvm/Support/FileOutputBuffer.h
===================================================================
--- llvm/include/llvm/Support/FileOutputBuffer.h
+++ llvm/include/llvm/Support/FileOutputBuffer.h
@@ -32,6 +32,9 @@
   enum {
     /// set the 'x' bit on the resulting file
     F_executable = 1,
+
+    /// Always use an in-memory buffer instead of an on-disk buffer.
+    F_in_memory = 2,
   };
 
   /// Factory method to create an OutputBuffer object which manages a read/write


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D69293.225989.patch
Type: text/x-patch
Size: 2466 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20191022/3d839f0a/attachment.bin>


More information about the llvm-commits mailing list