https://github.com/jansvoboda11 updated 
https://github.com/llvm/llvm-project/pull/242384

>From 583379443e13fce59acb47067c8e393e213ae60b Mon Sep 17 00:00:00 2001
From: Jan Svoboda <[email protected]>
Date: Thu, 12 Nov 2026 13:08:03 -0800
Subject: [PATCH] [clang] Extract built PCMs from their container without
 copying

When a module is built implicitly, the importing instance extracts the AST
file from the module cache's view of the written PCM and stores a copy of it
in its `InMemoryModuleCache`. That's an extra heap copy of every built PCM,
kept alive for the lifetime of the translation unit.

This PR resolves the FIXME by storing a buffer that keeps the written PCM
alive and exposes just the AST file: the `__clangast` section for PCMs
wrapped in an object file (`-gmodules`), or the whole PCM otherwise. The
in-process module cache now shares the memory of its entry with the
importing instance, and the cross-process module cache gives it the
memory-mapped file.

For a regular implicit modules build importing Foundation and friends, this
reduces peak memory footprint from ~150 MB to ~100 MB. For a TU importing
Foundation with `-gmodules` (31 PCMs), the copy increased the peak memory
footprint by 12-17%.
---
 clang/lib/Frontend/CompilerInstance.cpp | 34 ++++++++++++++++++++-----
 1 file changed, 27 insertions(+), 7 deletions(-)

diff --git a/clang/lib/Frontend/CompilerInstance.cpp 
b/clang/lib/Frontend/CompilerInstance.cpp
index 7d23b43a2fbed..0b42e03d1320e 100644
--- a/clang/lib/Frontend/CompilerInstance.cpp
+++ b/clang/lib/Frontend/CompilerInstance.cpp
@@ -1533,6 +1533,28 @@ static bool readASTAfterCompileModule(CompilerInstance 
&ImportingInstance,
   return false;
 }
 
+namespace {
+/// A buffer exposing a sub-range of an owned buffer.
+class SubMemoryBuffer : public llvm::MemoryBuffer {
+  std::unique_ptr<llvm::MemoryBuffer> Underlying;
+
+public:
+  SubMemoryBuffer(std::unique_ptr<llvm::MemoryBuffer> Underlying,
+                  StringRef Range)
+      : Underlying(std::move(Underlying)) {
+    init(Range.begin(), Range.end(), /*RequiresNullTerminator=*/false);
+  }
+
+  StringRef getBufferIdentifier() const override {
+    return Underlying->getBufferIdentifier();
+  }
+
+  BufferKind getBufferKind() const override {
+    return Underlying->getBufferKind();
+  }
+};
+} // namespace
+
 /// Compile a module in a separate compiler instance.
 /// Returns true on success, false on failure.
 static bool compileModuleImpl(CompilerInstance &ImportingInstance,
@@ -1579,15 +1601,13 @@ static bool compileModuleImpl(CompilerInstance 
&ImportingInstance,
     ImportingInstance.getModuleCache().updateModuleTimestamp(ModuleFileName);
   }
 
-  // This isn't strictly necessary, but it's more efficient to extract the AST
-  // file (which may be wrapped in an object file) now rather than doing so
-  // repeatedly in the readers.
+  // Readers use the module cache's view of the written PCM. Extracting the AST
+  // file (which may be wrapped in an object file) isn't strictly necessary, 
but
+  // it's more efficient to do so now rather than repeatedly in the readers.
   const PCHContainerReader &Rdr = ImportingInstance.getPCHContainerReader();
   StringRef ExtractedBuffer = Rdr.ExtractPCH(*Buffer);
-  // FIXME: Avoid the copy here by having InMemoryModuleCache accept both the
-  // owning buffer and the StringRef.
-  Buffer = llvm::MemoryBuffer::getMemBufferCopy(ExtractedBuffer);
-
+  Buffer =
+      std::make_unique<SubMemoryBuffer>(std::move(Buffer), ExtractedBuffer);
   ImportingInstance.getModuleCache().getInMemoryModuleCache().addBuiltPCM(
       ModuleFileName, std::move(Buffer), Size, ModTime);
 

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to