Peter Collingbourne | c269ed5 | 2015-08-27 23:37:36 +0000 | [diff] [blame] | 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 | |
| 14 | #include "llvm/CodeGen/ParallelCG.h" |
Teresa Johnson | ad17679 | 2016-11-11 05:34:58 +0000 | [diff] [blame] | 15 | #include "llvm/Bitcode/BitcodeReader.h" |
| 16 | #include "llvm/Bitcode/BitcodeWriter.h" |
Peter Collingbourne | c269ed5 | 2015-08-27 23:37:36 +0000 | [diff] [blame] | 17 | #include "llvm/IR/LLVMContext.h" |
| 18 | #include "llvm/IR/LegacyPassManager.h" |
| 19 | #include "llvm/IR/Module.h" |
| 20 | #include "llvm/Support/ErrorOr.h" |
| 21 | #include "llvm/Support/MemoryBuffer.h" |
| 22 | #include "llvm/Support/TargetRegistry.h" |
Teresa Johnson | d84c7de | 2016-03-04 15:39:13 +0000 | [diff] [blame] | 23 | #include "llvm/Support/ThreadPool.h" |
Peter Collingbourne | c269ed5 | 2015-08-27 23:37:36 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetMachine.h" |
| 25 | #include "llvm/Transforms/Utils/SplitModule.h" |
| 26 | |
| 27 | using namespace llvm; |
| 28 | |
Benjamin Kramer | 1afc1de | 2016-06-17 20:41:14 +0000 | [diff] [blame] | 29 | static void codegen(Module *M, llvm::raw_pwrite_stream &OS, |
| 30 | function_ref<std::unique_ptr<TargetMachine>()> TMFactory, |
| 31 | TargetMachine::CodeGenFileType FileType) { |
Davide Italiano | 7950b12 | 2016-04-15 17:34:32 +0000 | [diff] [blame] | 32 | std::unique_ptr<TargetMachine> TM = TMFactory(); |
Peter Collingbourne | c269ed5 | 2015-08-27 23:37:36 +0000 | [diff] [blame] | 33 | legacy::PassManager CodeGenPasses; |
Tobias Edler von Koch | 4d45090 | 2015-11-19 23:59:24 +0000 | [diff] [blame] | 34 | if (TM->addPassesToEmitFile(CodeGenPasses, OS, FileType)) |
Peter Collingbourne | c269ed5 | 2015-08-27 23:37:36 +0000 | [diff] [blame] | 35 | report_fatal_error("Failed to setup codegen"); |
| 36 | CodeGenPasses.run(*M); |
| 37 | } |
| 38 | |
Davide Italiano | 7950b12 | 2016-04-15 17:34:32 +0000 | [diff] [blame] | 39 | std::unique_ptr<Module> llvm::splitCodeGen( |
| 40 | std::unique_ptr<Module> M, ArrayRef<llvm::raw_pwrite_stream *> OSs, |
| 41 | ArrayRef<llvm::raw_pwrite_stream *> BCOSs, |
| 42 | const std::function<std::unique_ptr<TargetMachine>()> &TMFactory, |
| 43 | TargetMachine::CodeGenFileType FileType, bool PreserveLocals) { |
Evgeniy Stepanov | 268826a | 2016-04-06 18:32:13 +0000 | [diff] [blame] | 44 | assert(BCOSs.empty() || BCOSs.size() == OSs.size()); |
| 45 | |
Peter Collingbourne | c269ed5 | 2015-08-27 23:37:36 +0000 | [diff] [blame] | 46 | if (OSs.size() == 1) { |
Evgeniy Stepanov | 268826a | 2016-04-06 18:32:13 +0000 | [diff] [blame] | 47 | if (!BCOSs.empty()) |
| 48 | WriteBitcodeToFile(M.get(), *BCOSs[0]); |
Davide Italiano | 7950b12 | 2016-04-15 17:34:32 +0000 | [diff] [blame] | 49 | codegen(M.get(), *OSs[0], TMFactory, FileType); |
Peter Collingbourne | c269ed5 | 2015-08-27 23:37:36 +0000 | [diff] [blame] | 50 | return M; |
| 51 | } |
| 52 | |
Teresa Johnson | d84c7de | 2016-03-04 15:39:13 +0000 | [diff] [blame] | 53 | // Create ThreadPool in nested scope so that threads will be joined |
| 54 | // on destruction. |
| 55 | { |
| 56 | ThreadPool CodegenThreadPool(OSs.size()); |
| 57 | int ThreadCount = 0; |
Peter Collingbourne | c269ed5 | 2015-08-27 23:37:36 +0000 | [diff] [blame] | 58 | |
Teresa Johnson | d84c7de | 2016-03-04 15:39:13 +0000 | [diff] [blame] | 59 | SplitModule( |
| 60 | std::move(M), OSs.size(), |
| 61 | [&](std::unique_ptr<Module> MPart) { |
| 62 | // We want to clone the module in a new context to multi-thread the |
| 63 | // codegen. We do it by serializing partition modules to bitcode |
| 64 | // (while still on the main thread, in order to avoid data races) and |
| 65 | // spinning up new threads which deserialize the partitions into |
| 66 | // separate contexts. |
| 67 | // FIXME: Provide a more direct way to do this in LLVM. |
Davide Italiano | caa1169 | 2016-04-17 19:38:57 +0000 | [diff] [blame] | 68 | SmallString<0> BC; |
Teresa Johnson | d84c7de | 2016-03-04 15:39:13 +0000 | [diff] [blame] | 69 | raw_svector_ostream BCOS(BC); |
| 70 | WriteBitcodeToFile(MPart.get(), BCOS); |
Peter Collingbourne | c269ed5 | 2015-08-27 23:37:36 +0000 | [diff] [blame] | 71 | |
Evgeniy Stepanov | 268826a | 2016-04-06 18:32:13 +0000 | [diff] [blame] | 72 | if (!BCOSs.empty()) { |
| 73 | BCOSs[ThreadCount]->write(BC.begin(), BC.size()); |
| 74 | BCOSs[ThreadCount]->flush(); |
| 75 | } |
| 76 | |
Teresa Johnson | d84c7de | 2016-03-04 15:39:13 +0000 | [diff] [blame] | 77 | llvm::raw_pwrite_stream *ThreadOS = OSs[ThreadCount++]; |
| 78 | // Enqueue the task |
| 79 | CodegenThreadPool.async( |
Davide Italiano | caa1169 | 2016-04-17 19:38:57 +0000 | [diff] [blame] | 80 | [TMFactory, FileType, ThreadOS](const SmallString<0> &BC) { |
Teresa Johnson | d84c7de | 2016-03-04 15:39:13 +0000 | [diff] [blame] | 81 | LLVMContext Ctx; |
Peter Collingbourne | d9445c4 | 2016-11-13 07:00:17 +0000 | [diff] [blame] | 82 | Expected<std::unique_ptr<Module>> MOrErr = parseBitcodeFile( |
Teresa Johnson | d84c7de | 2016-03-04 15:39:13 +0000 | [diff] [blame] | 83 | MemoryBufferRef(StringRef(BC.data(), BC.size()), |
| 84 | "<split-module>"), |
| 85 | Ctx); |
| 86 | if (!MOrErr) |
| 87 | report_fatal_error("Failed to read bitcode"); |
| 88 | std::unique_ptr<Module> MPartInCtx = std::move(MOrErr.get()); |
| 89 | |
Davide Italiano | 7950b12 | 2016-04-15 17:34:32 +0000 | [diff] [blame] | 90 | codegen(MPartInCtx.get(), *ThreadOS, TMFactory, FileType); |
Teresa Johnson | d84c7de | 2016-03-04 15:39:13 +0000 | [diff] [blame] | 91 | }, |
| 92 | // Pass BC using std::move to ensure that it get moved rather than |
| 93 | // copied into the thread's context. |
| 94 | std::move(BC)); |
Peter Collingbourne | c269ed5 | 2015-08-27 23:37:36 +0000 | [diff] [blame] | 95 | }, |
Teresa Johnson | d84c7de | 2016-03-04 15:39:13 +0000 | [diff] [blame] | 96 | PreserveLocals); |
| 97 | } |
Peter Collingbourne | c269ed5 | 2015-08-27 23:37:36 +0000 | [diff] [blame] | 98 | |
| 99 | return {}; |
| 100 | } |