summaryrefslogtreecommitdiffstats
path: root/llvm/lib/CodeGen/ParallelCG.cpp
blob: 1486af14d4bce9ef0ab97c9c4c8f4adb5d136b32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//===-- ParallelCG.cpp ----------------------------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file defines functions that can be used for parallel code generation.
//
//===----------------------------------------------------------------------===//

#include "llvm/CodeGen/ParallelCG.h"
#include "llvm/Bitcode/ReaderWriter.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/TargetRegistry.h"
#include "llvm/Support/ThreadPool.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Transforms/Utils/SplitModule.h"

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,
                    TargetMachine::CodeGenFileType FileType) {
  std::unique_ptr<TargetMachine> TM(TheTarget->createTargetMachine(
      M->getTargetTriple(), CPU, Features, Options, RM, CM, OL));

  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,
    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);

  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);
    return M;
  }

  // Create ThreadPool in nested scope so that threads will be joined
  // on destruction.
  {
    ThreadPool CodegenThreadPool(OSs.size());
    int ThreadCount = 0;

    SplitModule(
        std::move(M), OSs.size(),
        [&](std::unique_ptr<Module> MPart) {
          // We want to clone the module in a new context to multi-thread the
          // codegen. We do it by serializing partition modules to bitcode
          // (while still on the main thread, in order to avoid data races) and
          // spinning up new threads which deserialize the partitions into
          // separate contexts.
          // FIXME: Provide a more direct way to do this in LLVM.
          SmallVector<char, 0> BC;
          raw_svector_ostream BCOS(BC);
          WriteBitcodeToFile(MPart.get(), BCOS);

          if (!BCOSs.empty()) {
            BCOSs[ThreadCount]->write(BC.begin(), BC.size());
            BCOSs[ThreadCount]->flush();
          }

          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) {
                LLVMContext Ctx;
                ErrorOr<std::unique_ptr<Module>> MOrErr = parseBitcodeFile(
                    MemoryBufferRef(StringRef(BC.data(), BC.size()),
                                    "<split-module>"),
                    Ctx);
                if (!MOrErr)
                  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);
              },
              // Pass BC using std::move to ensure that it get moved rather than
              // copied into the thread's context.
              std::move(BC));
        },
        PreserveLocals);
  }

  return {};
}
OpenPOWER on IntegriCloud