1 //===-- gold-plugin.cpp - Plugin to gold for Link Time Optimization ------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This is a gold plugin for LLVM. It provides an LLVM implementation of the
11 // interface described in http://gcc.gnu.org/wiki/whopr/driver .
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Config/config.h" // plugin-api.h requires HAVE_STDINT_H
16 #include "llvm/ADT/DenseSet.h"
17 #include "llvm/ADT/StringSet.h"
18 #include "llvm/Analysis/TargetLibraryInfo.h"
19 #include "llvm/Analysis/TargetTransformInfo.h"
20 #include "llvm/Bitcode/ReaderWriter.h"
21 #include "llvm/CodeGen/Analysis.h"
22 #include "llvm/CodeGen/CommandFlags.h"
23 #include "llvm/CodeGen/ParallelCG.h"
24 #include "llvm/IR/AutoUpgrade.h"
25 #include "llvm/IR/Constants.h"
26 #include "llvm/IR/DiagnosticInfo.h"
27 #include "llvm/IR/DiagnosticPrinter.h"
28 #include "llvm/IR/LLVMContext.h"
29 #include "llvm/IR/LegacyPassManager.h"
30 #include "llvm/IR/Module.h"
31 #include "llvm/IR/Verifier.h"
32 #include "llvm/Linker/IRMover.h"
33 #include "llvm/MC/SubtargetFeature.h"
34 #include "llvm/Object/FunctionIndexObjectFile.h"
35 #include "llvm/Object/IRObjectFile.h"
36 #include "llvm/Support/Host.h"
37 #include "llvm/Support/ManagedStatic.h"
38 #include "llvm/Support/MemoryBuffer.h"
39 #include "llvm/Support/TargetRegistry.h"
40 #include "llvm/Support/TargetSelect.h"
41 #include "llvm/Support/raw_ostream.h"
42 #include "llvm/Transforms/IPO.h"
43 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
44 #include "llvm/Transforms/Utils/GlobalStatus.h"
45 #include "llvm/Transforms/Utils/ModuleUtils.h"
46 #include "llvm/Transforms/Utils/ValueMapper.h"
48 #include <plugin-api.h>
49 #include <system_error>
53 // FIXME: remove this declaration when we stop maintaining Ubuntu Quantal and
54 // Precise and Debian Wheezy (binutils 2.23 is required)
60 static ld_plugin_status discard_message(int level, const char *format, ...) {
61 // Die loudly. Recent versions of Gold pass ld_plugin_message as the first
62 // callback in the transfer vector. This should never be called.
66 static ld_plugin_release_input_file release_input_file = nullptr;
67 static ld_plugin_get_input_file get_input_file = nullptr;
68 static ld_plugin_message message = discard_message;
73 std::vector<ld_plugin_symbol> syms;
76 /// RAII wrapper to manage opening and releasing of a ld_plugin_input_file.
77 struct PluginInputFile {
79 ld_plugin_input_file File;
81 PluginInputFile(void *Handle) : Handle(Handle) {
82 if (get_input_file(Handle, &File) != LDPS_OK)
83 message(LDPL_FATAL, "Failed to get file information");
86 if (release_input_file(Handle) != LDPS_OK)
87 message(LDPL_FATAL, "Failed to release file information");
89 ld_plugin_input_file &file() { return File; }
92 struct ResolutionInfo {
93 bool IsLinkonceOdr = true;
94 bool UnnamedAddr = true;
95 GlobalValue::VisibilityTypes Visibility = GlobalValue::DefaultVisibility;
96 bool CommonInternal = false;
97 bool UseCommon = false;
98 unsigned CommonSize = 0;
99 unsigned CommonAlign = 0;
100 claimed_file *CommonFile = nullptr;
104 static ld_plugin_add_symbols add_symbols = nullptr;
105 static ld_plugin_get_symbols get_symbols = nullptr;
106 static ld_plugin_add_input_file add_input_file = nullptr;
107 static ld_plugin_set_extra_library_path set_extra_library_path = nullptr;
108 static ld_plugin_get_view get_view = nullptr;
109 static Reloc::Model RelocationModel = Reloc::Default;
110 static std::string output_name = "";
111 static std::list<claimed_file> Modules;
112 static StringMap<ResolutionInfo> ResInfo;
113 static std::vector<std::string> Cleanup;
114 static llvm::TargetOptions TargetOpts;
123 static bool generate_api_file = false;
124 static OutputType TheOutputType = OT_NORMAL;
125 static unsigned OptLevel = 2;
126 static unsigned Parallelism = 1;
128 static bool DisableVerify = true;
130 static bool DisableVerify = false;
132 static std::string obj_path;
133 static std::string extra_library_path;
134 static std::string triple;
135 static std::string mcpu;
136 // When the thinlto plugin option is specified, only read the function
137 // the information from intermediate files and write a combined
138 // global index for the ThinLTO backends.
139 static bool thinlto = false;
140 // Additional options to pass into the code generator.
141 // Note: This array will contain all plugin options which are not claimed
142 // as plugin exclusive to pass to the code generator.
143 // For example, "generate-api-file" and "as"options are for the plugin
144 // use only and will not be passed.
145 static std::vector<const char *> extra;
147 static void process_plugin_option(const char *opt_)
151 llvm::StringRef opt = opt_;
153 if (opt == "generate-api-file") {
154 generate_api_file = true;
155 } else if (opt.startswith("mcpu=")) {
156 mcpu = opt.substr(strlen("mcpu="));
157 } else if (opt.startswith("extra-library-path=")) {
158 extra_library_path = opt.substr(strlen("extra_library_path="));
159 } else if (opt.startswith("mtriple=")) {
160 triple = opt.substr(strlen("mtriple="));
161 } else if (opt.startswith("obj-path=")) {
162 obj_path = opt.substr(strlen("obj-path="));
163 } else if (opt == "emit-llvm") {
164 TheOutputType = OT_BC_ONLY;
165 } else if (opt == "save-temps") {
166 TheOutputType = OT_SAVE_TEMPS;
167 } else if (opt == "disable-output") {
168 TheOutputType = OT_DISABLE;
169 } else if (opt == "thinlto") {
171 } else if (opt.size() == 2 && opt[0] == 'O') {
172 if (opt[1] < '0' || opt[1] > '3')
173 message(LDPL_FATAL, "Optimization level must be between 0 and 3");
174 OptLevel = opt[1] - '0';
175 } else if (opt.startswith("jobs=")) {
176 if (StringRef(opt_ + 5).getAsInteger(10, Parallelism))
177 message(LDPL_FATAL, "Invalid parallelism level: %s", opt_ + 5);
178 } else if (opt == "disable-verify") {
179 DisableVerify = true;
181 // Save this option to pass to the code generator.
182 // ParseCommandLineOptions() expects argv[0] to be program name. Lazily
185 extra.push_back("LLVMgold");
187 extra.push_back(opt_);
192 static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
194 static ld_plugin_status all_symbols_read_hook(void);
195 static ld_plugin_status cleanup_hook(void);
197 extern "C" ld_plugin_status onload(ld_plugin_tv *tv);
198 ld_plugin_status onload(ld_plugin_tv *tv) {
199 InitializeAllTargetInfos();
200 InitializeAllTargets();
201 InitializeAllTargetMCs();
202 InitializeAllAsmParsers();
203 InitializeAllAsmPrinters();
205 // We're given a pointer to the first transfer vector. We read through them
206 // until we find one where tv_tag == LDPT_NULL. The REGISTER_* tagged values
207 // contain pointers to functions that we need to call to register our own
208 // hooks. The others are addresses of functions we can use to call into gold
211 bool registeredClaimFile = false;
212 bool RegisteredAllSymbolsRead = false;
214 for (; tv->tv_tag != LDPT_NULL; ++tv) {
215 switch (tv->tv_tag) {
216 case LDPT_OUTPUT_NAME:
217 output_name = tv->tv_u.tv_string;
219 case LDPT_LINKER_OUTPUT:
220 switch (tv->tv_u.tv_val) {
222 case LDPO_DYN: // .so
223 case LDPO_PIE: // position independent executable
224 RelocationModel = Reloc::PIC_;
226 case LDPO_EXEC: // .exe
227 RelocationModel = Reloc::Static;
230 message(LDPL_ERROR, "Unknown output file type %d", tv->tv_u.tv_val);
235 options::process_plugin_option(tv->tv_u.tv_string);
237 case LDPT_REGISTER_CLAIM_FILE_HOOK: {
238 ld_plugin_register_claim_file callback;
239 callback = tv->tv_u.tv_register_claim_file;
241 if (callback(claim_file_hook) != LDPS_OK)
244 registeredClaimFile = true;
246 case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK: {
247 ld_plugin_register_all_symbols_read callback;
248 callback = tv->tv_u.tv_register_all_symbols_read;
250 if (callback(all_symbols_read_hook) != LDPS_OK)
253 RegisteredAllSymbolsRead = true;
255 case LDPT_REGISTER_CLEANUP_HOOK: {
256 ld_plugin_register_cleanup callback;
257 callback = tv->tv_u.tv_register_cleanup;
259 if (callback(cleanup_hook) != LDPS_OK)
262 case LDPT_GET_INPUT_FILE:
263 get_input_file = tv->tv_u.tv_get_input_file;
265 case LDPT_RELEASE_INPUT_FILE:
266 release_input_file = tv->tv_u.tv_release_input_file;
268 case LDPT_ADD_SYMBOLS:
269 add_symbols = tv->tv_u.tv_add_symbols;
271 case LDPT_GET_SYMBOLS_V2:
272 get_symbols = tv->tv_u.tv_get_symbols;
274 case LDPT_ADD_INPUT_FILE:
275 add_input_file = tv->tv_u.tv_add_input_file;
277 case LDPT_SET_EXTRA_LIBRARY_PATH:
278 set_extra_library_path = tv->tv_u.tv_set_extra_library_path;
281 get_view = tv->tv_u.tv_get_view;
284 message = tv->tv_u.tv_message;
291 if (!registeredClaimFile) {
292 message(LDPL_ERROR, "register_claim_file not passed to LLVMgold.");
296 message(LDPL_ERROR, "add_symbols not passed to LLVMgold.");
300 if (!RegisteredAllSymbolsRead)
303 if (!get_input_file) {
304 message(LDPL_ERROR, "get_input_file not passed to LLVMgold.");
307 if (!release_input_file) {
308 message(LDPL_ERROR, "relesase_input_file not passed to LLVMgold.");
315 static const GlobalObject *getBaseObject(const GlobalValue &GV) {
316 if (auto *GA = dyn_cast<GlobalAlias>(&GV))
317 return GA->getBaseObject();
318 return cast<GlobalObject>(&GV);
321 static bool shouldSkip(uint32_t Symflags) {
322 if (!(Symflags & object::BasicSymbolRef::SF_Global))
324 if (Symflags & object::BasicSymbolRef::SF_FormatSpecific)
329 static void diagnosticHandler(const DiagnosticInfo &DI) {
330 if (const auto *BDI = dyn_cast<BitcodeDiagnosticInfo>(&DI)) {
331 std::error_code EC = BDI->getError();
332 if (EC == BitcodeError::InvalidBitcodeSignature)
336 std::string ErrStorage;
338 raw_string_ostream OS(ErrStorage);
339 DiagnosticPrinterRawOStream DP(OS);
342 ld_plugin_level Level;
343 switch (DI.getSeverity()) {
345 message(LDPL_FATAL, "LLVM gold plugin has failed to create LTO module: %s",
347 llvm_unreachable("Fatal doesn't return.");
349 Level = LDPL_WARNING;
356 message(Level, "LLVM gold plugin: %s", ErrStorage.c_str());
359 static void diagnosticHandlerForContext(const DiagnosticInfo &DI,
361 diagnosticHandler(DI);
364 static GlobalValue::VisibilityTypes
365 getMinVisibility(GlobalValue::VisibilityTypes A,
366 GlobalValue::VisibilityTypes B) {
367 if (A == GlobalValue::HiddenVisibility)
369 if (B == GlobalValue::HiddenVisibility)
371 if (A == GlobalValue::ProtectedVisibility)
376 /// Called by gold to see whether this file is one that our plugin can handle.
377 /// We'll try to open it and register all the symbols with add_symbol if
379 static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
382 MemoryBufferRef BufferRef;
383 std::unique_ptr<MemoryBuffer> Buffer;
386 if (get_view(file->handle, &view) != LDPS_OK) {
387 message(LDPL_ERROR, "Failed to get a view of %s", file->name);
391 MemoryBufferRef(StringRef((const char *)view, file->filesize), "");
394 // Gold has found what might be IR part-way inside of a file, such as
397 offset = file->offset;
399 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
400 MemoryBuffer::getOpenFileSlice(file->fd, file->name, file->filesize,
402 if (std::error_code EC = BufferOrErr.getError()) {
403 message(LDPL_ERROR, EC.message().c_str());
406 Buffer = std::move(BufferOrErr.get());
407 BufferRef = Buffer->getMemBufferRef();
410 Context.setDiagnosticHandler(diagnosticHandlerForContext);
411 ErrorOr<std::unique_ptr<object::IRObjectFile>> ObjOrErr =
412 object::IRObjectFile::create(BufferRef, Context);
413 std::error_code EC = ObjOrErr.getError();
414 if (EC == object::object_error::invalid_file_type ||
415 EC == object::object_error::bitcode_section_not_found)
421 message(LDPL_ERROR, "LLVM gold plugin has failed to create LTO module: %s",
422 EC.message().c_str());
425 std::unique_ptr<object::IRObjectFile> Obj = std::move(*ObjOrErr);
427 Modules.resize(Modules.size() + 1);
428 claimed_file &cf = Modules.back();
430 cf.handle = file->handle;
432 // If we are doing ThinLTO compilation, don't need to process the symbols.
433 // Later we simply build a combined index file after all files are claimed.
434 if (options::thinlto)
437 for (auto &Sym : Obj->symbols()) {
438 uint32_t Symflags = Sym.getFlags();
439 if (shouldSkip(Symflags))
442 cf.syms.push_back(ld_plugin_symbol());
443 ld_plugin_symbol &sym = cf.syms.back();
444 sym.version = nullptr;
446 SmallString<64> Name;
448 raw_svector_ostream OS(Name);
451 sym.name = strdup(Name.c_str());
453 const GlobalValue *GV = Obj->getSymbolGV(Sym.getRawDataRefImpl());
455 ResolutionInfo &Res = ResInfo[sym.name];
457 sym.visibility = LDPV_DEFAULT;
459 Res.UnnamedAddr &= GV->hasUnnamedAddr();
460 Res.IsLinkonceOdr &= GV->hasLinkOnceLinkage();
461 if (GV->hasCommonLinkage()) {
462 Res.CommonAlign = std::max(Res.CommonAlign, GV->getAlignment());
463 const DataLayout &DL = GV->getParent()->getDataLayout();
464 uint64_t Size = DL.getTypeAllocSize(GV->getType()->getElementType());
465 if (Size >= Res.CommonSize) {
466 Res.CommonSize = Size;
467 Res.CommonFile = &cf;
470 Res.Visibility = getMinVisibility(Res.Visibility, GV->getVisibility());
471 switch (GV->getVisibility()) {
472 case GlobalValue::DefaultVisibility:
473 sym.visibility = LDPV_DEFAULT;
475 case GlobalValue::HiddenVisibility:
476 sym.visibility = LDPV_HIDDEN;
478 case GlobalValue::ProtectedVisibility:
479 sym.visibility = LDPV_PROTECTED;
484 if (Symflags & object::BasicSymbolRef::SF_Undefined) {
485 sym.def = LDPK_UNDEF;
486 if (GV && GV->hasExternalWeakLinkage())
487 sym.def = LDPK_WEAKUNDEF;
491 assert(!GV->hasExternalWeakLinkage() &&
492 !GV->hasAvailableExternallyLinkage() && "Not a declaration!");
493 if (GV->hasCommonLinkage())
494 sym.def = LDPK_COMMON;
495 else if (GV->isWeakForLinker())
496 sym.def = LDPK_WEAKDEF;
501 sym.comdat_key = nullptr;
503 const GlobalObject *Base = getBaseObject(*GV);
505 message(LDPL_FATAL, "Unable to determine comdat of alias!");
506 const Comdat *C = Base->getComdat();
508 sym.comdat_key = strdup(C->getName().str().c_str());
511 sym.resolution = LDPR_UNKNOWN;
514 if (!cf.syms.empty()) {
515 if (add_symbols(cf.handle, cf.syms.size(), cf.syms.data()) != LDPS_OK) {
516 message(LDPL_ERROR, "Unable to add symbols!");
524 static void internalize(GlobalValue &GV) {
525 if (GV.isDeclarationForLinker())
526 return; // We get here if there is a matching asm definition.
527 if (!GV.hasLocalLinkage())
528 GV.setLinkage(GlobalValue::InternalLinkage);
531 static const char *getResolutionName(ld_plugin_symbol_resolution R) {
537 case LDPR_PREVAILING_DEF:
538 return "PREVAILING_DEF";
539 case LDPR_PREVAILING_DEF_IRONLY:
540 return "PREVAILING_DEF_IRONLY";
541 case LDPR_PREEMPTED_REG:
542 return "PREEMPTED_REG";
543 case LDPR_PREEMPTED_IR:
544 return "PREEMPTED_IR";
545 case LDPR_RESOLVED_IR:
546 return "RESOLVED_IR";
547 case LDPR_RESOLVED_EXEC:
548 return "RESOLVED_EXEC";
549 case LDPR_RESOLVED_DYN:
550 return "RESOLVED_DYN";
551 case LDPR_PREVAILING_DEF_IRONLY_EXP:
552 return "PREVAILING_DEF_IRONLY_EXP";
554 llvm_unreachable("Unknown resolution");
557 static void freeSymName(ld_plugin_symbol &Sym) {
559 free(Sym.comdat_key);
561 Sym.comdat_key = nullptr;
564 static std::unique_ptr<FunctionInfoIndex>
565 getFunctionIndexForFile(claimed_file &F, ld_plugin_input_file &Info) {
567 if (get_symbols(F.handle, F.syms.size(), &F.syms[0]) != LDPS_OK)
568 message(LDPL_FATAL, "Failed to get symbol information");
571 if (get_view(F.handle, &View) != LDPS_OK)
572 message(LDPL_FATAL, "Failed to get a view of file");
574 MemoryBufferRef BufferRef(StringRef((const char *)View, Info.filesize),
577 // Don't bother trying to build an index if there is no summary information
578 // in this bitcode file.
579 if (!object::FunctionIndexObjectFile::hasFunctionSummaryInMemBuffer(
580 BufferRef, diagnosticHandler))
581 return std::unique_ptr<FunctionInfoIndex>(nullptr);
583 ErrorOr<std::unique_ptr<object::FunctionIndexObjectFile>> ObjOrErr =
584 object::FunctionIndexObjectFile::create(BufferRef, diagnosticHandler);
586 if (std::error_code EC = ObjOrErr.getError())
587 message(LDPL_FATAL, "Could not read function index bitcode from file : %s",
588 EC.message().c_str());
590 object::FunctionIndexObjectFile &Obj = **ObjOrErr;
592 return Obj.takeIndex();
595 static std::unique_ptr<Module>
596 getModuleForFile(LLVMContext &Context, claimed_file &F,
597 ld_plugin_input_file &Info, raw_fd_ostream *ApiFile,
598 StringSet<> &Internalize, StringSet<> &Maybe,
599 std::vector<GlobalValue *> &Keep) {
601 if (get_symbols(F.handle, F.syms.size(), F.syms.data()) != LDPS_OK)
602 message(LDPL_FATAL, "Failed to get symbol information");
605 if (get_view(F.handle, &View) != LDPS_OK)
606 message(LDPL_FATAL, "Failed to get a view of file");
608 MemoryBufferRef BufferRef(StringRef((const char *)View, Info.filesize),
610 ErrorOr<std::unique_ptr<object::IRObjectFile>> ObjOrErr =
611 object::IRObjectFile::create(BufferRef, Context);
613 if (std::error_code EC = ObjOrErr.getError())
614 message(LDPL_FATAL, "Could not read bitcode from file : %s",
615 EC.message().c_str());
617 object::IRObjectFile &Obj = **ObjOrErr;
619 Module &M = Obj.getModule();
621 M.materializeMetadata();
624 SmallPtrSet<GlobalValue *, 8> Used;
625 collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ false);
628 for (auto &ObjSym : Obj.symbols()) {
629 GlobalValue *GV = Obj.getSymbolGV(ObjSym.getRawDataRefImpl());
630 if (GV && GV->hasAppendingLinkage())
633 if (shouldSkip(ObjSym.getFlags()))
635 ld_plugin_symbol &Sym = F.syms[SymNum];
638 ld_plugin_symbol_resolution Resolution =
639 (ld_plugin_symbol_resolution)Sym.resolution;
641 if (options::generate_api_file)
642 *ApiFile << Sym.name << ' ' << getResolutionName(Resolution) << '\n';
646 continue; // Asm symbol.
649 ResolutionInfo &Res = ResInfo[Sym.name];
650 if (Resolution == LDPR_PREVAILING_DEF_IRONLY_EXP && !Res.IsLinkonceOdr)
651 Resolution = LDPR_PREVAILING_DEF;
653 GV->setUnnamedAddr(Res.UnnamedAddr);
654 GV->setVisibility(Res.Visibility);
656 // Override gold's resolution for common symbols. We want the largest
658 if (GV->hasCommonLinkage()) {
659 cast<GlobalVariable>(GV)->setAlignment(Res.CommonAlign);
660 if (Resolution == LDPR_PREVAILING_DEF_IRONLY)
661 Res.CommonInternal = true;
663 if (Resolution == LDPR_PREVAILING_DEF_IRONLY ||
664 Resolution == LDPR_PREVAILING_DEF)
665 Res.UseCommon = true;
667 if (Res.CommonFile == &F && Res.UseCommon) {
668 if (Res.CommonInternal)
669 Resolution = LDPR_PREVAILING_DEF_IRONLY;
671 Resolution = LDPR_PREVAILING_DEF;
673 Resolution = LDPR_PREEMPTED_IR;
677 switch (Resolution) {
679 llvm_unreachable("Unexpected resolution");
681 case LDPR_RESOLVED_IR:
682 case LDPR_RESOLVED_EXEC:
683 case LDPR_RESOLVED_DYN:
684 case LDPR_PREEMPTED_IR:
685 case LDPR_PREEMPTED_REG:
689 if (!GV->isDeclarationForLinker())
690 assert(GV->hasComdat());
693 case LDPR_PREVAILING_DEF_IRONLY: {
695 // The IR linker has to be able to map this value to a declaration,
696 // so we can only internalize after linking.
698 Internalize.insert(GV->getName());
702 case LDPR_PREVAILING_DEF:
704 // There is a non IR use, so we have to force optimizations to keep this.
705 switch (GV->getLinkage()) {
708 case GlobalValue::LinkOnceAnyLinkage:
709 GV->setLinkage(GlobalValue::WeakAnyLinkage);
711 case GlobalValue::LinkOnceODRLinkage:
712 GV->setLinkage(GlobalValue::WeakODRLinkage);
717 case LDPR_PREVAILING_DEF_IRONLY_EXP: {
718 // We can only check for address uses after we merge the modules. The
719 // reason is that this GV might have a copy in another module
720 // and in that module the address might be significant, but that
721 // copy will be LDPR_PREEMPTED_IR.
722 Maybe.insert(GV->getName());
731 return Obj.takeModule();
734 static void runLTOPasses(Module &M, TargetMachine &TM) {
735 M.setDataLayout(TM.createDataLayout());
737 legacy::PassManager passes;
738 passes.add(createTargetTransformInfoWrapperPass(TM.getTargetIRAnalysis()));
740 PassManagerBuilder PMB;
741 PMB.LibraryInfo = new TargetLibraryInfoImpl(Triple(TM.getTargetTriple()));
742 PMB.Inliner = createFunctionInliningPass();
743 // Unconditionally verify input since it is not verified before this
744 // point and has unknown origin.
745 PMB.VerifyInput = true;
746 PMB.VerifyOutput = !options::DisableVerify;
747 PMB.LoopVectorize = true;
748 PMB.SLPVectorize = true;
749 PMB.OptLevel = options::OptLevel;
750 PMB.populateLTOPassManager(passes);
754 static void saveBCFile(StringRef Path, Module &M) {
756 raw_fd_ostream OS(Path, EC, sys::fs::OpenFlags::F_None);
758 message(LDPL_FATAL, "Failed to write the output file.");
759 WriteBitcodeToFile(&M, OS, /* ShouldPreserveUseListOrder */ true);
762 static void codegen(std::unique_ptr<Module> M) {
763 const std::string &TripleStr = M->getTargetTriple();
764 Triple TheTriple(TripleStr);
767 const Target *TheTarget = TargetRegistry::lookupTarget(TripleStr, ErrMsg);
769 message(LDPL_FATAL, "Target not found: %s", ErrMsg.c_str());
771 if (unsigned NumOpts = options::extra.size())
772 cl::ParseCommandLineOptions(NumOpts, &options::extra[0]);
774 SubtargetFeatures Features;
775 Features.getDefaultSubtargetFeatures(TheTriple);
776 for (const std::string &A : MAttrs)
777 Features.AddFeature(A);
779 TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
780 CodeGenOpt::Level CGOptLevel;
781 switch (options::OptLevel) {
783 CGOptLevel = CodeGenOpt::None;
786 CGOptLevel = CodeGenOpt::Less;
789 CGOptLevel = CodeGenOpt::Default;
792 CGOptLevel = CodeGenOpt::Aggressive;
795 std::unique_ptr<TargetMachine> TM(TheTarget->createTargetMachine(
796 TripleStr, options::mcpu, Features.getString(), Options, RelocationModel,
797 CodeModel::Default, CGOptLevel));
799 runLTOPasses(*M, *TM);
801 if (options::TheOutputType == options::OT_SAVE_TEMPS)
802 saveBCFile(output_name + ".opt.bc", *M);
804 SmallString<128> Filename;
805 if (!options::obj_path.empty())
806 Filename = options::obj_path;
807 else if (options::TheOutputType == options::OT_SAVE_TEMPS)
808 Filename = output_name + ".o";
810 std::vector<SmallString<128>> Filenames(options::Parallelism);
811 bool TempOutFile = Filename.empty();
813 // Open a file descriptor for each backend thread. This is done in a block
814 // so that the output file descriptors are closed before gold opens them.
815 std::list<llvm::raw_fd_ostream> OSs;
816 std::vector<llvm::raw_pwrite_stream *> OSPtrs(options::Parallelism);
817 for (unsigned I = 0; I != options::Parallelism; ++I) {
821 sys::fs::createTemporaryFile("lto-llvm", "o", FD, Filenames[I]);
823 message(LDPL_FATAL, "Could not create temporary file: %s",
824 EC.message().c_str());
826 Filenames[I] = Filename;
827 if (options::Parallelism != 1)
828 Filenames[I] += utostr(I);
830 sys::fs::openFileForWrite(Filenames[I], FD, sys::fs::F_None);
832 message(LDPL_FATAL, "Could not open file: %s", EC.message().c_str());
834 OSs.emplace_back(FD, true);
835 OSPtrs[I] = &OSs.back();
838 // Run backend threads.
839 splitCodeGen(std::move(M), OSPtrs, options::mcpu, Features.getString(),
840 Options, RelocationModel, CodeModel::Default, CGOptLevel);
843 for (auto &Filename : Filenames) {
844 if (add_input_file(Filename.c_str()) != LDPS_OK)
846 "Unable to add .o file to the link. File left behind in: %s",
849 Cleanup.push_back(Filename.c_str());
853 /// gold informs us that all symbols have been read. At this point, we use
854 /// get_symbols to see if any of our definitions have been overridden by a
855 /// native object file. Then, perform optimization and codegen.
856 static ld_plugin_status allSymbolsReadHook(raw_fd_ostream *ApiFile) {
860 // If we are doing ThinLTO compilation, simply build the combined
861 // function index/summary and emit it. We don't need to parse the modules
862 // and link them in this case.
863 if (options::thinlto) {
864 FunctionInfoIndex CombinedIndex;
865 uint64_t NextModuleId = 0;
866 for (claimed_file &F : Modules) {
867 PluginInputFile InputFile(F.handle);
869 std::unique_ptr<FunctionInfoIndex> Index =
870 getFunctionIndexForFile(F, InputFile.file());
872 // Skip files without a function summary.
874 CombinedIndex.mergeFrom(std::move(Index), ++NextModuleId);
878 raw_fd_ostream OS(output_name + ".thinlto.bc", EC,
879 sys::fs::OpenFlags::F_None);
881 message(LDPL_FATAL, "Unable to open %s.thinlto.bc for writing: %s",
882 output_name.data(), EC.message().c_str());
883 WriteFunctionSummaryToFile(CombinedIndex, OS);
891 Context.setDiagnosticHandler(diagnosticHandlerForContext, nullptr, true);
893 std::unique_ptr<Module> Combined(new Module("ld-temp.o", Context));
894 IRMover L(*Combined);
896 std::string DefaultTriple = sys::getDefaultTargetTriple();
898 StringSet<> Internalize;
900 for (claimed_file &F : Modules) {
901 PluginInputFile InputFile(F.handle);
902 std::vector<GlobalValue *> Keep;
903 std::unique_ptr<Module> M = getModuleForFile(
904 Context, F, InputFile.file(), ApiFile, Internalize, Maybe, Keep);
905 if (!options::triple.empty())
906 M->setTargetTriple(options::triple.c_str());
907 else if (M->getTargetTriple().empty())
908 M->setTargetTriple(DefaultTriple);
910 if (L.move(*M, Keep, [](GlobalValue &, IRMover::ValueAdder) {}))
911 message(LDPL_FATAL, "Failed to link module");
914 for (const auto &Name : Internalize) {
915 GlobalValue *GV = Combined->getNamedValue(Name.first());
920 for (const auto &Name : Maybe) {
921 GlobalValue *GV = Combined->getNamedValue(Name.first());
924 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
925 if (canBeOmittedFromSymbolTable(GV))
929 if (options::TheOutputType == options::OT_DISABLE)
932 if (options::TheOutputType != options::OT_NORMAL) {
934 if (options::TheOutputType == options::OT_BC_ONLY)
937 path = output_name + ".bc";
938 saveBCFile(path, *Combined);
939 if (options::TheOutputType == options::OT_BC_ONLY)
943 codegen(std::move(Combined));
945 if (!options::extra_library_path.empty() &&
946 set_extra_library_path(options::extra_library_path.c_str()) != LDPS_OK)
947 message(LDPL_FATAL, "Unable to set the extra library path.");
952 static ld_plugin_status all_symbols_read_hook(void) {
953 ld_plugin_status Ret;
954 if (!options::generate_api_file) {
955 Ret = allSymbolsReadHook(nullptr);
958 raw_fd_ostream ApiFile("apifile.txt", EC, sys::fs::F_None);
960 message(LDPL_FATAL, "Unable to open apifile.txt for writing: %s",
961 EC.message().c_str());
962 Ret = allSymbolsReadHook(&ApiFile);
967 if (options::TheOutputType == options::OT_BC_ONLY ||
968 options::TheOutputType == options::OT_DISABLE) {
969 if (options::TheOutputType == options::OT_DISABLE)
970 // Remove the output file here since ld.bfd creates the output file
972 sys::fs::remove(output_name);
979 static ld_plugin_status cleanup_hook(void) {
980 for (std::string &Name : Cleanup) {
981 std::error_code EC = sys::fs::remove(Name);
983 message(LDPL_ERROR, "Failed to delete '%s': %s", Name.c_str(),
984 EC.message().c_str());