llvm.org GIT mirror llvm / 51f5548
Add a libLTO API to stop/restart ThinLTO between optimizations and CodeGen This allows the linker to instruct ThinLTO to perform only the optimization part or only the codegen part of the process. From: Mehdi Amini <mehdi.amini@apple.com> git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@265113 91177308-0d34-0410-b5e6-96231b3b80d8 Mehdi Amini 3 years ago
5 changed file(s) with 83 addition(s) and 5 deletion(s). Raw diff Collapse all Expand all
168168 TMBuilder.CGOptLevel = CGOptLevel;
169169 }
170170
171 /// Disable CodeGen, only run the stages till codegen and stop. The output
172 /// will be bitcode.
173 void disableCodeGen(bool Disable) { DisableCodeGen = Disable; }
174
175 /// Perform CodeGen only: disable all other stages.
176 void setCodeGenOnly(bool CGOnly) { CodeGenOnly = CGOnly; }
177
171178 /**@}*/
172179
173180 /**
227234
228235 /// Path to a directory to save the temporary bitcode files.
229236 std::string SaveTempsDir;
237
238 /// Flag to enable/disable CodeGen. When set to true, the process stops after
239 /// optimizations and a bitcode is produced.
240 bool DisableCodeGen;
241
242 /// Flag to indicate that only the CodeGen will be performed, no cross-module
243 /// importing or optimization.
244 bool CodeGenOnly;
230245 };
231246 }
232247 #endif
4343 * @{
4444 */
4545
46 #define LTO_API_VERSION 18
46 #define LTO_API_VERSION 19
47
4748 /**
4849 * \since prior to LTO_API_VERSION=3
4950 */
717718 extern void thinlto_codegen_set_cpu(thinlto_code_gen_t cg, const char *cpu);
718719
719720 /**
721 * Disable CodeGen, only run the stages till codegen and stop. The output will
722 * be bitcode.
723 *
724 * \since LTO_API_VERSION=19
725 */
726 extern void thinlto_codegen_disable_codegen(thinlto_code_gen_t cg,
727 lto_bool_t disable);
728
729 /**
730 * Perform CodeGen only: disable all other stages.
731 *
732 * \since LTO_API_VERSION=19
733 */
734 extern void thinlto_codegen_set_codegen_only(thinlto_code_gen_t cg,
735 lto_bool_t codegen_only);
736
737 /**
720738 * Parse -mllvm style debug options.
721739 *
722740 * \since LTO_API_VERSION=18
187187 StringMap &ModuleMap, TargetMachine &TM,
188188 const FunctionImporter::ImportMapTy &ImportList,
189189 ThinLTOCodeGenerator::CachingOptions CacheOptions,
190 StringRef SaveTempsDir, unsigned count) {
190 bool DisableCodeGen, StringRef SaveTempsDir,
191 unsigned count) {
191192
192193 // Save temps: after IPO.
193194 saveTempBitcode(TheModule, SaveTempsDir, count, ".1.IPO.bc");
210211 optimizeModule(TheModule, TM);
211212
212213 saveTempBitcode(TheModule, SaveTempsDir, count, ".3.opt.bc");
214
215 if (DisableCodeGen) {
216 // Configured to stop before CodeGen, serialize the bitcode and return.
217 SmallVector OutputBuffer;
218 {
219 raw_svector_ostream OS(OutputBuffer);
220 WriteBitcodeToFile(&TheModule, OS, true, true);
221 }
222 return make_unique(std::move(OutputBuffer));
223 }
213224
214225 return codegenModule(TheModule, TM);
215226 }
347358
348359 // Main entry point for the ThinLTO processing
349360 void ThinLTOCodeGenerator::run() {
361 if (CodeGenOnly) {
362 // Perform only parallel codegen and return.
363 ThreadPool Pool;
364 assert(ProducedBinaries.empty() && "The generator should not be reused");
365 ProducedBinaries.resize(Modules.size());
366 int count = 0;
367 for (auto &ModuleBuffer : Modules) {
368 Pool.async([&](int count) {
369 LLVMContext Context;
370 Context.setDiscardValueNames(LTODiscardValueNames);
371
372 // Parse module now
373 auto TheModule = loadModuleFromBuffer(ModuleBuffer, Context, false);
374
375 // CodeGen
376 ProducedBinaries[count] = codegen(*TheModule);
377 }, count++);
378 }
379
380 return;
381 }
382
350383 // Sequential linking phase
351384 auto Index = linkCombinedIndex();
352385
395428 auto &ImportList = ImportLists[TheModule->getModuleIdentifier()];
396429 ProducedBinaries[count] = ProcessThinLTOModule(
397430 *TheModule, *Index, ModuleMap, *TMBuilder.create(), ImportList,
398 CacheOptions, SaveTempsDir, count);
431 CacheOptions, DisableCodeGen, SaveTempsDir, count);
399432 }, count);
400433 count++;
401434 }
472472 MemBuffer->getBufferSize()};
473473 }
474474
475 void thinlto_codegen_disable_codegen(thinlto_code_gen_t cg,
476 lto_bool_t disable) {
477 unwrap(cg)->disableCodeGen(disable);
478 }
479
480 void thinlto_codegen_set_codegen_only(thinlto_code_gen_t cg,
481 lto_bool_t CodeGenOnly) {
482 unwrap(cg)->setCodeGenOnly(CodeGenOnly);
483 }
484
475485 void thinlto_debug_options(const char *const *options, int number) {
476486 // if options were requested, set them
477487 if (number && options) {
482492 }
483493 }
484494
485 bool lto_module_is_thinlto(lto_module_t mod) {
495 lto_bool_t lto_module_is_thinlto(lto_module_t mod) {
486496 return unwrap(mod)->isThinLTO();
487497 }
488498
6060 lto_module_is_thinlto
6161 thinlto_codegen_add_must_preserve_symbol
6262 thinlto_codegen_add_cross_referenced_symbol
63 thinlto_codegen_set_final_cache_size_relative_to_available_space
63 thinlto_codegen_set_final_cache_size_relative_to_available_space
64 thinlto_codegen_set_codegen_only
65 thinlto_codegen_disable_codegen