ParallelCG.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //===-- ParallelCG.cpp ----------------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file defines functions that can be used for parallel code generation.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/CodeGen/ParallelCG.h"
  14. #include "llvm/Bitcode/ReaderWriter.h"
  15. #include "llvm/IR/LLVMContext.h"
  16. #include "llvm/IR/LegacyPassManager.h"
  17. #include "llvm/IR/Module.h"
  18. #include "llvm/Support/ErrorOr.h"
  19. #include "llvm/Support/MemoryBuffer.h"
  20. #include "llvm/Support/TargetRegistry.h"
  21. #include "llvm/Support/ThreadPool.h"
  22. #include "llvm/Target/TargetMachine.h"
  23. #include "llvm/Transforms/Utils/SplitModule.h"
  24. using namespace llvm;
  25. static void codegen(Module *M, llvm::raw_pwrite_stream &OS,
  26. const Target *TheTarget, StringRef CPU, StringRef Features,
  27. const TargetOptions &Options, Reloc::Model RM,
  28. CodeModel::Model CM, CodeGenOpt::Level OL,
  29. TargetMachine::CodeGenFileType FileType) {
  30. std::unique_ptr<TargetMachine> TM(TheTarget->createTargetMachine(
  31. M->getTargetTriple(), CPU, Features, Options, RM, CM, OL));
  32. legacy::PassManager CodeGenPasses;
  33. if (TM->addPassesToEmitFile(CodeGenPasses, OS, FileType))
  34. report_fatal_error("Failed to setup codegen");
  35. CodeGenPasses.run(*M);
  36. }
  37. std::unique_ptr<Module> llvm::splitCodeGen(
  38. std::unique_ptr<Module> M, ArrayRef<llvm::raw_pwrite_stream *> OSs,
  39. ArrayRef<llvm::raw_pwrite_stream *> BCOSs, StringRef CPU,
  40. StringRef Features, const TargetOptions &Options, Reloc::Model RM,
  41. CodeModel::Model CM, CodeGenOpt::Level OL,
  42. TargetMachine::CodeGenFileType FileType, bool PreserveLocals) {
  43. StringRef TripleStr = M->getTargetTriple();
  44. std::string ErrMsg;
  45. const Target *TheTarget = TargetRegistry::lookupTarget(TripleStr, ErrMsg);
  46. if (!TheTarget)
  47. report_fatal_error(Twine("Target not found: ") + ErrMsg);
  48. assert(BCOSs.empty() || BCOSs.size() == OSs.size());
  49. if (OSs.size() == 1) {
  50. if (!BCOSs.empty())
  51. WriteBitcodeToFile(M.get(), *BCOSs[0]);
  52. codegen(M.get(), *OSs[0], TheTarget, CPU, Features, Options, RM, CM, OL,
  53. FileType);
  54. return M;
  55. }
  56. // Create ThreadPool in nested scope so that threads will be joined
  57. // on destruction.
  58. {
  59. ThreadPool CodegenThreadPool(OSs.size());
  60. int ThreadCount = 0;
  61. SplitModule(
  62. std::move(M), OSs.size(),
  63. [&](std::unique_ptr<Module> MPart) {
  64. // We want to clone the module in a new context to multi-thread the
  65. // codegen. We do it by serializing partition modules to bitcode
  66. // (while still on the main thread, in order to avoid data races) and
  67. // spinning up new threads which deserialize the partitions into
  68. // separate contexts.
  69. // FIXME: Provide a more direct way to do this in LLVM.
  70. SmallVector<char, 0> BC;
  71. raw_svector_ostream BCOS(BC);
  72. WriteBitcodeToFile(MPart.get(), BCOS);
  73. if (!BCOSs.empty()) {
  74. BCOSs[ThreadCount]->write(BC.begin(), BC.size());
  75. BCOSs[ThreadCount]->flush();
  76. }
  77. llvm::raw_pwrite_stream *ThreadOS = OSs[ThreadCount++];
  78. // Enqueue the task
  79. CodegenThreadPool.async(
  80. [TheTarget, CPU, Features, Options, RM, CM, OL, FileType,
  81. ThreadOS](const SmallVector<char, 0> &BC) {
  82. LLVMContext Ctx;
  83. ErrorOr<std::unique_ptr<Module>> MOrErr = parseBitcodeFile(
  84. MemoryBufferRef(StringRef(BC.data(), BC.size()),
  85. "<split-module>"),
  86. Ctx);
  87. if (!MOrErr)
  88. report_fatal_error("Failed to read bitcode");
  89. std::unique_ptr<Module> MPartInCtx = std::move(MOrErr.get());
  90. codegen(MPartInCtx.get(), *ThreadOS, TheTarget, CPU, Features,
  91. Options, RM, CM, OL, FileType);
  92. },
  93. // Pass BC using std::move to ensure that it get moved rather than
  94. // copied into the thread's context.
  95. std::move(BC));
  96. },
  97. PreserveLocals);
  98. }
  99. return {};
  100. }