<p dir="ltr">We should probably convert the existing users too, no?</p>
<div class="gmail_quote">On Apr 14, 2016 8:12 PM, "Davide Italiano via llvm-commits" <<a href="mailto:llvm-commits@lists.llvm.org">llvm-commits@lists.llvm.org</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: davide<br>
Date: Thu Apr 14 19:07:28 2016<br>
New Revision: 266390<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=266390&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=266390&view=rev</a><br>
Log:<br>
[LTO] Add a new splitCodeGen() API which takes a TargetMachineFactory.<br>
<br>
This will be used in lld to avoid creating TargetMachine in two<br>
different places. See D18999 for a more detailed discussion.<br>
<br>
Differential Revision:  <a href="http://reviews.llvm.org/D19139" rel="noreferrer" target="_blank">http://reviews.llvm.org/D19139</a><br>
<br>
Modified:<br>
    llvm/trunk/include/llvm/CodeGen/ParallelCG.h<br>
    llvm/trunk/lib/CodeGen/ParallelCG.cpp<br>
<br>
Modified: llvm/trunk/include/llvm/CodeGen/ParallelCG.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/CodeGen/ParallelCG.h?rev=266390&r1=266389&r2=266390&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/CodeGen/ParallelCG.h?rev=266390&r1=266389&r2=266390&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/CodeGen/ParallelCG.h (original)<br>
+++ llvm/trunk/include/llvm/CodeGen/ParallelCG.h Thu Apr 14 19:07:28 2016<br>
@@ -43,6 +43,19 @@ splitCodeGen(std::unique_ptr<Module> M,<br>
              TargetMachine::CodeGenFileType FT = TargetMachine::CGFT_ObjectFile,<br>
              bool PreserveLocals = false);<br>
<br>
+/// Split M into OSs.size() partitions, and generate code for each.<br>
+/// It is a variant that takes a factory function for the TargetMachine<br>
+/// TMFactory. TMFactory needs to be thread safe on the client side.<br>
+/// See the other splitCodeGen() for a more detailed description.<br>
+///<br>
+/// \returns M if OSs.size() == 1, otherwise returns std::unique_ptr<Module>().<br>
+std::unique_ptr<Module><br>
+splitCodeGen(std::unique_ptr<Module> M, ArrayRef<raw_pwrite_stream *> OSs,<br>
+             ArrayRef<llvm::raw_pwrite_stream *> BCOSs,<br>
+             const std::function<std::unique_ptr<TargetMachine>()> &TMFactory,<br>
+             TargetMachine::CodeGenFileType FT = TargetMachine::CGFT_ObjectFile,<br>
+             bool PreserveLocals = false);<br>
+<br>
 } // namespace llvm<br>
<br>
 #endif<br>
<br>
Modified: llvm/trunk/lib/CodeGen/ParallelCG.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/ParallelCG.cpp?rev=266390&r1=266389&r2=266390&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/ParallelCG.cpp?rev=266390&r1=266389&r2=266390&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/CodeGen/ParallelCG.cpp (original)<br>
+++ llvm/trunk/lib/CodeGen/ParallelCG.cpp Thu Apr 14 19:07:28 2016<br>
@@ -25,39 +25,47 @@<br>
<br>
 using namespace llvm;<br>
<br>
-static void codegen(Module *M, llvm::raw_pwrite_stream &OS,<br>
-                    const Target *TheTarget, StringRef CPU, StringRef Features,<br>
-                    const TargetOptions &Options, Reloc::Model RM,<br>
-                    CodeModel::Model CM, CodeGenOpt::Level OL,<br>
-                    TargetMachine::CodeGenFileType FileType) {<br>
-  std::unique_ptr<TargetMachine> TM(TheTarget->createTargetMachine(<br>
-      M->getTargetTriple(), CPU, Features, Options, RM, CM, OL));<br>
-<br>
+static void<br>
+codegen(Module *M, llvm::raw_pwrite_stream &OS,<br>
+        const std::function<std::unique_ptr<TargetMachine>()> &TMFactory,<br>
+        TargetMachine::CodeGenFileType FileType) {<br>
+  std::unique_ptr<TargetMachine> TM = TMFactory();<br>
   legacy::PassManager CodeGenPasses;<br>
   if (TM->addPassesToEmitFile(CodeGenPasses, OS, FileType))<br>
     report_fatal_error("Failed to setup codegen");<br>
   CodeGenPasses.run(*M);<br>
 }<br>
<br>
-std::unique_ptr<Module> llvm::splitCodeGen(<br>
-    std::unique_ptr<Module> M, ArrayRef<llvm::raw_pwrite_stream *> OSs,<br>
-    ArrayRef<llvm::raw_pwrite_stream *> BCOSs, StringRef CPU,<br>
-    StringRef Features, const TargetOptions &Options, Reloc::Model RM,<br>
-    CodeModel::Model CM, CodeGenOpt::Level OL,<br>
-    TargetMachine::CodeGenFileType FileType, bool PreserveLocals) {<br>
+std::unique_ptr<Module><br>
+llvm::splitCodeGen(std::unique_ptr<Module> M, ArrayRef<raw_pwrite_stream *> OSs,<br>
+                   ArrayRef<llvm::raw_pwrite_stream *> BCOSs, StringRef CPU,<br>
+                   StringRef Features, const TargetOptions &Options,<br>
+                   Reloc::Model RM, CodeModel::Model CM, CodeGenOpt::Level OL,<br>
+                   TargetMachine::CodeGenFileType FileType,<br>
+                   bool PreserveLocals) {<br>
   StringRef TripleStr = M->getTargetTriple();<br>
   std::string ErrMsg;<br>
+<br>
   const Target *TheTarget = TargetRegistry::lookupTarget(TripleStr, ErrMsg);<br>
   if (!TheTarget)<br>
     report_fatal_error(Twine("Target not found: ") + ErrMsg);<br>
+  return splitCodeGen(std::move(M), OSs, BCOSs, [&]() {<br>
+    return std::unique_ptr<TargetMachine>(TheTarget->createTargetMachine(<br>
+        TripleStr, CPU, Features, Options, RM, CM, OL));<br>
+  }, FileType, PreserveLocals);<br>
+}<br>
<br>
+std::unique_ptr<Module> llvm::splitCodeGen(<br>
+    std::unique_ptr<Module> M, ArrayRef<llvm::raw_pwrite_stream *> OSs,<br>
+    ArrayRef<llvm::raw_pwrite_stream *> BCOSs,<br>
+    const std::function<std::unique_ptr<TargetMachine>()> &TMFactory,<br>
+    TargetMachine::CodeGenFileType FileType, bool PreserveLocals) {<br>
   assert(BCOSs.empty() || BCOSs.size() == OSs.size());<br>
<br>
   if (OSs.size() == 1) {<br>
     if (!BCOSs.empty())<br>
       WriteBitcodeToFile(M.get(), *BCOSs[0]);<br>
-    codegen(M.get(), *OSs[0], TheTarget, CPU, Features, Options, RM, CM, OL,<br>
-            FileType);<br>
+    codegen(M.get(), *OSs[0], TMFactory, FileType);<br>
     return M;<br>
   }<br>
<br>
@@ -88,8 +96,7 @@ std::unique_ptr<Module> llvm::splitCodeG<br>
           llvm::raw_pwrite_stream *ThreadOS = OSs[ThreadCount++];<br>
           // Enqueue the task<br>
           CodegenThreadPool.async(<br>
-              [TheTarget, CPU, Features, Options, RM, CM, OL, FileType,<br>
-               ThreadOS](const SmallVector<char, 0> &BC) {<br>
+              [&TMFactory, FileType, ThreadOS](const SmallVector<char, 0> &BC) {<br>
                 LLVMContext Ctx;<br>
                 ErrorOr<std::unique_ptr<Module>> MOrErr = parseBitcodeFile(<br>
                     MemoryBufferRef(StringRef(BC.data(), BC.size()),<br>
@@ -99,8 +106,7 @@ std::unique_ptr<Module> llvm::splitCodeG<br>
                   report_fatal_error("Failed to read bitcode");<br>
                 std::unique_ptr<Module> MPartInCtx = std::move(MOrErr.get());<br>
<br>
-                codegen(MPartInCtx.get(), *ThreadOS, TheTarget, CPU, Features,<br>
-                        Options, RM, CM, OL, FileType);<br>
+                codegen(MPartInCtx.get(), *ThreadOS, TMFactory, FileType);<br>
               },<br>
               // Pass BC using std::move to ensure that it get moved rather than<br>
               // copied into the thread's context.<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org">llvm-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits</a><br>
</blockquote></div>