|
25 | 25 |
|
26 | 26 | using namespace llvm;
|
27 | 27 |
|
28 |
| -static void codegen(Module *M, llvm::raw_pwrite_stream &OS, |
29 |
| - const Target *TheTarget, StringRef CPU, StringRef Features, |
30 |
| - const TargetOptions &Options, Reloc::Model RM, |
31 |
| - CodeModel::Model CM, CodeGenOpt::Level OL, |
32 |
| - TargetMachine::CodeGenFileType FileType) { |
33 |
| - std::unique_ptr<TargetMachine> TM(TheTarget->createTargetMachine( |
34 |
| - M->getTargetTriple(), CPU, Features, Options, RM, CM, OL)); |
35 |
| - |
36 |
| - legacy::PassManager CodeGenPasses; |
37 |
| - if (TM->addPassesToEmitFile(CodeGenPasses, OS, FileType)) |
38 |
| - report_fatal_error("Failed to setup codegen"); |
39 |
| - CodeGenPasses.run(*M); |
40 |
| -} |
41 |
| - |
42 | 28 | std::unique_ptr<Module>
|
43 | 29 | llvm::splitCodeGen(std::unique_ptr<Module> M,
|
44 | 30 | ArrayRef<llvm::raw_pwrite_stream *> OSs, StringRef CPU,
|
45 | 31 | StringRef Features, const TargetOptions &Options,
|
46 | 32 | Reloc::Model RM, CodeModel::Model CM, CodeGenOpt::Level OL,
|
47 | 33 | TargetMachine::CodeGenFileType FileType) {
|
48 |
| - StringRef TripleStr = M->getTargetTriple(); |
49 |
| - std::string ErrMsg; |
50 |
| - const Target *TheTarget = TargetRegistry::lookupTarget(TripleStr, ErrMsg); |
51 |
| - if (!TheTarget) |
52 |
| - report_fatal_error(Twine("Target not found: ") + ErrMsg); |
53 |
| - |
54 |
| - if (OSs.size() == 1) { |
55 |
| - codegen(M.get(), *OSs[0], TheTarget, CPU, Features, Options, RM, CM, |
56 |
| - OL, FileType); |
57 |
| - return M; |
58 |
| - } |
59 |
| - |
60 |
| - std::vector<thread> Threads; |
61 |
| - SplitModule(std::move(M), OSs.size(), [&](std::unique_ptr<Module> MPart) { |
62 |
| - // We want to clone the module in a new context to multi-thread the codegen. |
63 |
| - // We do it by serializing partition modules to bitcode (while still on the |
64 |
| - // main thread, in order to avoid data races) and spinning up new threads |
65 |
| - // which deserialize the partitions into separate contexts. |
66 |
| - // FIXME: Provide a more direct way to do this in LLVM. |
67 |
| - SmallVector<char, 0> BC; |
68 |
| - raw_svector_ostream BCOS(BC); |
69 |
| - WriteBitcodeToFile(MPart.get(), BCOS); |
70 |
| - |
71 |
| - llvm::raw_pwrite_stream *ThreadOS = OSs[Threads.size()]; |
72 |
| - Threads.emplace_back( |
73 |
| - [TheTarget, CPU, Features, Options, RM, CM, OL, FileType, |
74 |
| - ThreadOS](const SmallVector<char, 0> &BC) { |
75 |
| - LLVMContext Ctx; |
76 |
| - ErrorOr<std::unique_ptr<Module>> MOrErr = |
77 |
| - parseBitcodeFile(MemoryBufferRef(StringRef(BC.data(), BC.size()), |
78 |
| - "<split-module>"), |
79 |
| - Ctx); |
80 |
| - if (!MOrErr) |
81 |
| - report_fatal_error("Failed to read bitcode"); |
82 |
| - std::unique_ptr<Module> MPartInCtx = std::move(MOrErr.get()); |
83 |
| - |
84 |
| - codegen(MPartInCtx.get(), *ThreadOS, TheTarget, CPU, Features, |
85 |
| - Options, RM, CM, OL, FileType); |
86 |
| - }, |
87 |
| - // Pass BC using std::move to ensure that it get moved rather than |
88 |
| - // copied into the thread's context. |
89 |
| - std::move(BC)); |
90 |
| - }); |
91 |
| - |
92 |
| - for (thread &T : Threads) |
93 |
| - T.join(); |
94 |
| - |
95 |
| - return {}; |
| 34 | + return M; |
96 | 35 | }
|
0 commit comments