[PATCH] D19139: [LTO] SplitCodeGen new API

Davide Italiano via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 14 16:21:45 PDT 2016


davide created this revision.
davide added reviewers: Bigcheese, rafael, pcc.
davide added subscribers: llvm-commits, joker.eph.
davide set the repository for this revision to rL LLVM.

.. so that we don't have to create TM in two places in lld. See http://reviews.llvm.org/D18999 for reference.
Hopefully should work, assuming TheTarget is thread-safe. Thanks to Michael Spencer for help with this patch.

Repository:
  rL LLVM

http://reviews.llvm.org/D19139

Files:
  include/llvm/CodeGen/ParallelCG.h
  lib/CodeGen/ParallelCG.cpp

Index: lib/CodeGen/ParallelCG.cpp
===================================================================
--- lib/CodeGen/ParallelCG.cpp
+++ lib/CodeGen/ParallelCG.cpp
@@ -26,38 +26,45 @@
 using namespace llvm;
 
 static void codegen(Module *M, llvm::raw_pwrite_stream &OS,
-                    const Target *TheTarget, StringRef CPU, StringRef Features,
-                    const TargetOptions &Options, Reloc::Model RM,
-                    CodeModel::Model CM, CodeGenOpt::Level OL,
+                    std::function<std::unique_ptr<TargetMachine>()> F,
                     TargetMachine::CodeGenFileType FileType) {
-  std::unique_ptr<TargetMachine> TM(TheTarget->createTargetMachine(
-      M->getTargetTriple(), CPU, Features, Options, RM, CM, OL));
-
+  std::unique_ptr<TargetMachine> TM = F();
   legacy::PassManager CodeGenPasses;
   if (TM->addPassesToEmitFile(CodeGenPasses, OS, FileType))
     report_fatal_error("Failed to setup codegen");
   CodeGenPasses.run(*M);
 }
 
-std::unique_ptr<Module> llvm::splitCodeGen(
-    std::unique_ptr<Module> M, ArrayRef<llvm::raw_pwrite_stream *> OSs,
+std::unique_ptr<Module>
+llvm::splitCodeGen(std::unique_ptr<Module> M, ArrayRef<raw_pwrite_stream *> OSs,
     ArrayRef<llvm::raw_pwrite_stream *> BCOSs, StringRef CPU,
     StringRef Features, const TargetOptions &Options, Reloc::Model RM,
     CodeModel::Model CM, CodeGenOpt::Level OL,
     TargetMachine::CodeGenFileType FileType, bool PreserveLocals) {
   StringRef TripleStr = M->getTargetTriple();
   std::string ErrMsg;
+
   const Target *TheTarget = TargetRegistry::lookupTarget(TripleStr, ErrMsg);
   if (!TheTarget)
     report_fatal_error(Twine("Target not found: ") + ErrMsg);
+  return splitCodeGen(std::move(M), OSs, BCOSs,
+    [=]() {
+      return std::unique_ptr<TargetMachine>(TheTarget->createTargetMachine(
+      TripleStr, CPU, Features, Options, RM, CM, OL));
+    }, FileType, PreserveLocals);
+}
 
+std::unique_ptr<Module> llvm::splitCodeGen(
+    std::unique_ptr<Module> M, ArrayRef<llvm::raw_pwrite_stream *> OSs,
+    ArrayRef<llvm::raw_pwrite_stream *> BCOSs,
+    std::function<std::unique_ptr<TargetMachine>()> F,
+    TargetMachine::CodeGenFileType FileType, bool PreserveLocals) {
   assert(BCOSs.empty() || BCOSs.size() == OSs.size());
 
   if (OSs.size() == 1) {
     if (!BCOSs.empty())
       WriteBitcodeToFile(M.get(), *BCOSs[0]);
-    codegen(M.get(), *OSs[0], TheTarget, CPU, Features, Options, RM, CM, OL,
-            FileType);
+    codegen(M.get(), *OSs[0], F, FileType);
     return M;
   }
 
@@ -88,8 +95,7 @@
           llvm::raw_pwrite_stream *ThreadOS = OSs[ThreadCount++];
           // Enqueue the task
           CodegenThreadPool.async(
-              [TheTarget, CPU, Features, Options, RM, CM, OL, FileType,
-               ThreadOS](const SmallVector<char, 0> &BC) {
+              [F, FileType, ThreadOS](const SmallVector<char, 0> &BC) {
                 LLVMContext Ctx;
                 ErrorOr<std::unique_ptr<Module>> MOrErr = parseBitcodeFile(
                     MemoryBufferRef(StringRef(BC.data(), BC.size()),
@@ -99,8 +105,7 @@
                   report_fatal_error("Failed to read bitcode");
                 std::unique_ptr<Module> MPartInCtx = std::move(MOrErr.get());
 
-                codegen(MPartInCtx.get(), *ThreadOS, TheTarget, CPU, Features,
-                        Options, RM, CM, OL, FileType);
+                codegen(MPartInCtx.get(), *ThreadOS, F, FileType);
               },
               // Pass BC using std::move to ensure that it get moved rather than
               // copied into the thread's context.
Index: include/llvm/CodeGen/ParallelCG.h
===================================================================
--- include/llvm/CodeGen/ParallelCG.h
+++ include/llvm/CodeGen/ParallelCG.h
@@ -43,6 +43,13 @@
              TargetMachine::CodeGenFileType FT = TargetMachine::CGFT_ObjectFile,
              bool PreserveLocals = false);
 
+std::unique_ptr<Module>
+splitCodeGen(std::unique_ptr<Module> M, ArrayRef<raw_pwrite_stream *> OSs,
+             ArrayRef<llvm::raw_pwrite_stream *> BCOSs,
+             std::function<std::unique_ptr<TargetMachine>()> F,
+             TargetMachine::CodeGenFileType FT = TargetMachine::CGFT_ObjectFile,
+             bool PreserveLocals = false);
+
 } // namespace llvm
 
 #endif


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D19139.53804.patch
Type: text/x-patch
Size: 4318 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160414/4a34effb/attachment.bin>


More information about the llvm-commits mailing list