Use temporary files in lto

Rafael EspĂ­ndola via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 28 16:23:11 PDT 2016


This is the idea we chatted today to try to avoid mallocs os windows.
I was surprised to find this a small speedup up on linux (with /tmp in
tmpfs), so I wonder if we are doing something funny in SmallVector.

Linking llvm-as I got

master: 29.774786873 seconds
patch 29.752283093 seconds

Could one of you benchmark this on windows? If it is a performance win
there too I will clean the patch up and send for proper review.

Cheers,
Rafael
-------------- next part --------------
diff --git a/ELF/LTO.cpp b/ELF/LTO.cpp
index 6980993..a9fa743 100644
--- a/ELF/LTO.cpp
+++ b/ELF/LTO.cpp
@@ -90,6 +90,11 @@ BitcodeCompiler::BitcodeCompiler()
     : Combined(new llvm::Module("ld-temp.o", Driver->Context)),
       Mover(*Combined) {}
 
+BitcodeCompiler::~BitcodeCompiler() {
+  for (SmallString<0> &Name : TempFiles)
+    check(sys::fs::remove(Name));
+}
+
 void BitcodeCompiler::add(BitcodeFile &F) {
   std::unique_ptr<IRObjectFile> Obj = std::move(F.Obj);
   std::vector<GlobalValue *> Keep;
@@ -155,25 +160,37 @@ static void internalize(GlobalValue &GV) {
 std::vector<std::unique_ptr<InputFile>> BitcodeCompiler::runSplitCodegen(
     const std::function<std::unique_ptr<TargetMachine>()> &TMFactory) {
   unsigned NumThreads = Config->LtoJobs;
-  OwningData.resize(NumThreads);
+  std::vector<int> FDs;
 
-  std::list<raw_svector_ostream> OSs;
+  TempFiles.resize(NumThreads);
+  std::list<llvm::raw_fd_ostream> FOSs;
   std::vector<raw_pwrite_stream *> OSPtrs;
-  for (SmallString<0> &Obj : OwningData) {
-    OSs.emplace_back(Obj);
-    OSPtrs.push_back(&OSs.back());
+  for (SmallString<0> &Name : TempFiles) {
+    int FD;
+    std::error_code EC = sys::fs::createTemporaryFile("foo", "bar", FD, Name);
+    check(EC);
+    FOSs.emplace_back(FD, true);
+    OSPtrs.push_back(&FOSs.back());
+    FDs.push_back(FD);
   }
 
   splitCodeGen(std::move(Combined), OSPtrs, {}, TMFactory);
 
   std::vector<std::unique_ptr<InputFile>> ObjFiles;
-  for (SmallString<0> &Obj : OwningData)
-    ObjFiles.push_back(createObjectFile(
-        MemoryBufferRef(Obj, "LLD-INTERNAL-combined-lto-object")));
+  for (int FD : FDs) {
+    std::unique_ptr<MemoryBuffer> Buf =
+        check(MemoryBuffer::getOpenFile(FD, "LLD-INTERNAL-combined-lto-object",
+                                        -1, /*RequiresNullTerminator*/ false));
+    TempFileBuffers.push_back(std::move(Buf));
+    ObjFiles.push_back(
+        createObjectFile(TempFileBuffers.back()->getMemBufferRef()));
+  }
 
+  // FIXME: Change above to give good nemes for the temp files and just not
+  // delete them instead of copying in here.
   if (Config->SaveTemps)
     for (unsigned I = 0; I < NumThreads; ++I)
-      saveLtoObjectFile(OwningData[I], I, NumThreads > 1);
+      saveLtoObjectFile(TempFileBuffers[I]->getBuffer(), I, NumThreads > 1);
 
   return ObjFiles;
 }
diff --git a/ELF/LTO.h b/ELF/LTO.h
index 326ba24..93885af 100644
--- a/ELF/LTO.h
+++ b/ELF/LTO.h
@@ -36,6 +36,7 @@ class InputFile;
 class BitcodeCompiler {
 public:
   BitcodeCompiler();
+  ~BitcodeCompiler();
   void add(BitcodeFile &F);
   std::vector<std::unique_ptr<InputFile>> compile();
 
@@ -45,7 +46,8 @@ private:
 
   std::unique_ptr<llvm::Module> Combined;
   llvm::IRMover Mover;
-  std::vector<SmallString<0>> OwningData;
+  std::vector<SmallString<0>> TempFiles;
+  std::vector<std::unique_ptr<MemoryBuffer>> TempFileBuffers;
   llvm::StringSet<> InternalizedSyms;
   std::string TheTriple;
 };


More information about the llvm-commits mailing list