[llvm] [Support] mmap when possible in getSTDIN. (PR #162013)

via llvm-commits llvm-commits at lists.llvm.org
Sun Oct 5 23:25:26 PDT 2025


================
@@ -573,6 +573,27 @@ ErrorOr<std::unique_ptr<MemoryBuffer>> MemoryBuffer::getSTDIN() {
   // FIXME: That isn't necessarily true, we should try to mmap stdin and
   // fallback if it fails.
   sys::ChangeStdinMode(sys::fs::OF_Text);
+  std::error_code EC;
+  sys::fs::file_type Type;
+  sys::fs::file_status Status;
+  EC = sys::fs::status(sys::fs::getStdinHandle(), Status);
+  if (EC)
+    return EC;
+
+  Type = Status.type();
+
+  /*
+   * If the FD is regular file or block file,
+   * we try to create a mmap buffer first.
+   * If failed, rollback to read and copy.
+   */
+  if (Type == sys::fs::file_type::regular_file ||
+      Type == sys::fs::file_type::block_file) {
+    std::unique_ptr<MemoryBuffer> Result(new MemoryBufferMMapFile<MemoryBuffer>(
+        false, sys::fs::getStdinHandle(), Status.getSize(), 0, EC));
----------------
aokblast wrote:

llvm memory buffer uses overload new operator to fill the first few bytes as its name. And some features inside memorybuffer mechanism actually use it to get the reference of the buffer. Can make_unique overload operator new?

https://github.com/llvm/llvm-project/pull/162013


More information about the llvm-commits mailing list