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/Linker.h"
33 #include "llvm/MC/SubtargetFeature.h"
34 #include "llvm/Object/IRObjectFile.h"
35 #include "llvm/Object/FunctionIndexObjectFile.h"
36 #include "llvm/Support/raw_ostream.h"
37 #include "llvm/Support/Host.h"
38 #include "llvm/Support/ManagedStatic.h"
39 #include "llvm/Support/MemoryBuffer.h"
40 #include "llvm/Support/TargetRegistry.h"
41 #include "llvm/Support/TargetSelect.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)
63 std::vector<ld_plugin_symbol> syms;
67 static ld_plugin_status discard_message(int level, const char *format, ...) {
68 // Die loudly. Recent versions of Gold pass ld_plugin_message as the first
69 // callback in the transfer vector. This should never be called.
73 static ld_plugin_get_input_file get_input_file = nullptr;
74 static ld_plugin_release_input_file release_input_file = nullptr;
75 static ld_plugin_add_symbols add_symbols = nullptr;
76 static ld_plugin_get_symbols get_symbols = nullptr;
77 static ld_plugin_add_input_file add_input_file = nullptr;
78 static ld_plugin_set_extra_library_path set_extra_library_path = nullptr;
79 static ld_plugin_get_view get_view = nullptr;
80 static ld_plugin_message message = discard_message;
81 static Reloc::Model RelocationModel = Reloc::Default;
82 static std::string output_name = "";
83 static std::list<claimed_file> Modules;
84 static std::vector<std::string> Cleanup;
85 static llvm::TargetOptions TargetOpts;
94 static bool generate_api_file = false;
95 static OutputType TheOutputType = OT_NORMAL;
96 static unsigned OptLevel = 2;
97 static unsigned Parallelism = 1;
99 static bool DisableVerify = true;
101 static bool DisableVerify = false;
103 static std::string obj_path;
104 static std::string extra_library_path;
105 static std::string triple;
106 static std::string mcpu;
107 // When the thinlto plugin option is specified, only read the function
108 // the information from intermediate files and write a combined
109 // global index for the ThinLTO backends.
110 static bool thinlto = false;
111 // Additional options to pass into the code generator.
112 // Note: This array will contain all plugin options which are not claimed
113 // as plugin exclusive to pass to the code generator.
114 // For example, "generate-api-file" and "as"options are for the plugin
115 // use only and will not be passed.
116 static std::vector<const char *> extra;
118 static void process_plugin_option(const char *opt_)
122 llvm::StringRef opt = opt_;
124 if (opt == "generate-api-file") {
125 generate_api_file = true;
126 } else if (opt.startswith("mcpu=")) {
127 mcpu = opt.substr(strlen("mcpu="));
128 } else if (opt.startswith("extra-library-path=")) {
129 extra_library_path = opt.substr(strlen("extra_library_path="));
130 } else if (opt.startswith("mtriple=")) {
131 triple = opt.substr(strlen("mtriple="));
132 } else if (opt.startswith("obj-path=")) {
133 obj_path = opt.substr(strlen("obj-path="));
134 } else if (opt == "emit-llvm") {
135 TheOutputType = OT_BC_ONLY;
136 } else if (opt == "save-temps") {
137 TheOutputType = OT_SAVE_TEMPS;
138 } else if (opt == "disable-output") {
139 TheOutputType = OT_DISABLE;
140 } else if (opt == "thinlto") {
142 } else if (opt.size() == 2 && opt[0] == 'O') {
143 if (opt[1] < '0' || opt[1] > '3')
144 message(LDPL_FATAL, "Optimization level must be between 0 and 3");
145 OptLevel = opt[1] - '0';
146 } else if (opt.startswith("jobs=")) {
147 if (StringRef(opt_ + 5).getAsInteger(10, Parallelism))
148 message(LDPL_FATAL, "Invalid parallelism level: %s", opt_ + 5);
149 } else if (opt == "disable-verify") {
150 DisableVerify = true;
152 // Save this option to pass to the code generator.
153 // ParseCommandLineOptions() expects argv[0] to be program name. Lazily
156 extra.push_back("LLVMgold");
158 extra.push_back(opt_);
163 static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
165 static ld_plugin_status all_symbols_read_hook(void);
166 static ld_plugin_status cleanup_hook(void);
168 extern "C" ld_plugin_status onload(ld_plugin_tv *tv);
169 ld_plugin_status onload(ld_plugin_tv *tv) {
170 InitializeAllTargetInfos();
171 InitializeAllTargets();
172 InitializeAllTargetMCs();
173 InitializeAllAsmParsers();
174 InitializeAllAsmPrinters();
176 // We're given a pointer to the first transfer vector. We read through them
177 // until we find one where tv_tag == LDPT_NULL. The REGISTER_* tagged values
178 // contain pointers to functions that we need to call to register our own
179 // hooks. The others are addresses of functions we can use to call into gold
182 bool registeredClaimFile = false;
183 bool RegisteredAllSymbolsRead = false;
185 for (; tv->tv_tag != LDPT_NULL; ++tv) {
186 switch (tv->tv_tag) {
187 case LDPT_OUTPUT_NAME:
188 output_name = tv->tv_u.tv_string;
190 case LDPT_LINKER_OUTPUT:
191 switch (tv->tv_u.tv_val) {
193 case LDPO_DYN: // .so
194 case LDPO_PIE: // position independent executable
195 RelocationModel = Reloc::PIC_;
197 case LDPO_EXEC: // .exe
198 RelocationModel = Reloc::Static;
201 message(LDPL_ERROR, "Unknown output file type %d", tv->tv_u.tv_val);
206 options::process_plugin_option(tv->tv_u.tv_string);
208 case LDPT_REGISTER_CLAIM_FILE_HOOK: {
209 ld_plugin_register_claim_file callback;
210 callback = tv->tv_u.tv_register_claim_file;
212 if (callback(claim_file_hook) != LDPS_OK)
215 registeredClaimFile = true;
217 case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK: {
218 ld_plugin_register_all_symbols_read callback;
219 callback = tv->tv_u.tv_register_all_symbols_read;
221 if (callback(all_symbols_read_hook) != LDPS_OK)
224 RegisteredAllSymbolsRead = true;
226 case LDPT_REGISTER_CLEANUP_HOOK: {
227 ld_plugin_register_cleanup callback;
228 callback = tv->tv_u.tv_register_cleanup;
230 if (callback(cleanup_hook) != LDPS_OK)
233 case LDPT_GET_INPUT_FILE:
234 get_input_file = tv->tv_u.tv_get_input_file;
236 case LDPT_RELEASE_INPUT_FILE:
237 release_input_file = tv->tv_u.tv_release_input_file;
239 case LDPT_ADD_SYMBOLS:
240 add_symbols = tv->tv_u.tv_add_symbols;
242 case LDPT_GET_SYMBOLS_V2:
243 get_symbols = tv->tv_u.tv_get_symbols;
245 case LDPT_ADD_INPUT_FILE:
246 add_input_file = tv->tv_u.tv_add_input_file;
248 case LDPT_SET_EXTRA_LIBRARY_PATH:
249 set_extra_library_path = tv->tv_u.tv_set_extra_library_path;
252 get_view = tv->tv_u.tv_get_view;
255 message = tv->tv_u.tv_message;
262 if (!registeredClaimFile) {
263 message(LDPL_ERROR, "register_claim_file not passed to LLVMgold.");
267 message(LDPL_ERROR, "add_symbols not passed to LLVMgold.");
271 if (!RegisteredAllSymbolsRead)
274 if (!get_input_file) {
275 message(LDPL_ERROR, "get_input_file not passed to LLVMgold.");
278 if (!release_input_file) {
279 message(LDPL_ERROR, "relesase_input_file not passed to LLVMgold.");
286 static const GlobalObject *getBaseObject(const GlobalValue &GV) {
287 if (auto *GA = dyn_cast<GlobalAlias>(&GV))
288 return GA->getBaseObject();
289 return cast<GlobalObject>(&GV);
292 static bool shouldSkip(uint32_t Symflags) {
293 if (!(Symflags & object::BasicSymbolRef::SF_Global))
295 if (Symflags & object::BasicSymbolRef::SF_FormatSpecific)
300 static void diagnosticHandler(const DiagnosticInfo &DI, void *Context) {
301 if (const auto *BDI = dyn_cast<BitcodeDiagnosticInfo>(&DI)) {
302 std::error_code EC = BDI->getError();
303 if (EC == BitcodeError::InvalidBitcodeSignature)
307 std::string ErrStorage;
309 raw_string_ostream OS(ErrStorage);
310 DiagnosticPrinterRawOStream DP(OS);
313 ld_plugin_level Level;
314 switch (DI.getSeverity()) {
316 message(LDPL_FATAL, "LLVM gold plugin has failed to create LTO module: %s",
318 llvm_unreachable("Fatal doesn't return.");
320 Level = LDPL_WARNING;
327 message(Level, "LLVM gold plugin: %s", ErrStorage.c_str());
330 /// Called by gold to see whether this file is one that our plugin can handle.
331 /// We'll try to open it and register all the symbols with add_symbol if
333 static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
336 MemoryBufferRef BufferRef;
337 std::unique_ptr<MemoryBuffer> Buffer;
340 if (get_view(file->handle, &view) != LDPS_OK) {
341 message(LDPL_ERROR, "Failed to get a view of %s", file->name);
345 MemoryBufferRef(StringRef((const char *)view, file->filesize), "");
348 // Gold has found what might be IR part-way inside of a file, such as
351 offset = file->offset;
353 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
354 MemoryBuffer::getOpenFileSlice(file->fd, file->name, file->filesize,
356 if (std::error_code EC = BufferOrErr.getError()) {
357 message(LDPL_ERROR, EC.message().c_str());
360 Buffer = std::move(BufferOrErr.get());
361 BufferRef = Buffer->getMemBufferRef();
364 Context.setDiagnosticHandler(diagnosticHandler);
365 ErrorOr<std::unique_ptr<object::IRObjectFile>> ObjOrErr =
366 object::IRObjectFile::create(BufferRef, Context);
367 std::error_code EC = ObjOrErr.getError();
368 if (EC == object::object_error::invalid_file_type ||
369 EC == object::object_error::bitcode_section_not_found)
375 message(LDPL_ERROR, "LLVM gold plugin has failed to create LTO module: %s",
376 EC.message().c_str());
379 std::unique_ptr<object::IRObjectFile> Obj = std::move(*ObjOrErr);
381 Modules.resize(Modules.size() + 1);
382 claimed_file &cf = Modules.back();
384 cf.handle = file->handle;
386 // If we are doing ThinLTO compilation, don't need to process the symbols.
387 // Later we simply build a combined index file after all files are claimed.
388 if (options::thinlto) return LDPS_OK;
390 for (auto &Sym : Obj->symbols()) {
391 uint32_t Symflags = Sym.getFlags();
392 if (shouldSkip(Symflags))
395 cf.syms.push_back(ld_plugin_symbol());
396 ld_plugin_symbol &sym = cf.syms.back();
397 sym.version = nullptr;
399 SmallString<64> Name;
401 raw_svector_ostream OS(Name);
404 sym.name = strdup(Name.c_str());
406 const GlobalValue *GV = Obj->getSymbolGV(Sym.getRawDataRefImpl());
408 sym.visibility = LDPV_DEFAULT;
410 switch (GV->getVisibility()) {
411 case GlobalValue::DefaultVisibility:
412 sym.visibility = LDPV_DEFAULT;
414 case GlobalValue::HiddenVisibility:
415 sym.visibility = LDPV_HIDDEN;
417 case GlobalValue::ProtectedVisibility:
418 sym.visibility = LDPV_PROTECTED;
423 if (Symflags & object::BasicSymbolRef::SF_Undefined) {
424 sym.def = LDPK_UNDEF;
425 if (GV && GV->hasExternalWeakLinkage())
426 sym.def = LDPK_WEAKUNDEF;
430 assert(!GV->hasExternalWeakLinkage() &&
431 !GV->hasAvailableExternallyLinkage() && "Not a declaration!");
432 if (GV->hasCommonLinkage())
433 sym.def = LDPK_COMMON;
434 else if (GV->isWeakForLinker())
435 sym.def = LDPK_WEAKDEF;
440 sym.comdat_key = nullptr;
442 const GlobalObject *Base = getBaseObject(*GV);
444 message(LDPL_FATAL, "Unable to determine comdat of alias!");
445 const Comdat *C = Base->getComdat();
447 sym.comdat_key = strdup(C->getName().str().c_str());
448 else if (Base->hasWeakLinkage() || Base->hasLinkOnceLinkage())
449 sym.comdat_key = strdup(sym.name);
452 sym.resolution = LDPR_UNKNOWN;
455 if (!cf.syms.empty()) {
456 if (add_symbols(cf.handle, cf.syms.size(), cf.syms.data()) != LDPS_OK) {
457 message(LDPL_ERROR, "Unable to add symbols!");
465 static void keepGlobalValue(GlobalValue &GV,
466 std::vector<GlobalAlias *> &KeptAliases) {
467 assert(!GV.hasLocalLinkage());
469 if (auto *GA = dyn_cast<GlobalAlias>(&GV))
470 KeptAliases.push_back(GA);
472 switch (GV.getLinkage()) {
475 case GlobalValue::LinkOnceAnyLinkage:
476 GV.setLinkage(GlobalValue::WeakAnyLinkage);
478 case GlobalValue::LinkOnceODRLinkage:
479 GV.setLinkage(GlobalValue::WeakODRLinkage);
483 assert(!GV.isDiscardableIfUnused());
486 static void internalize(GlobalValue &GV) {
487 if (GV.isDeclarationForLinker())
488 return; // We get here if there is a matching asm definition.
489 if (!GV.hasLocalLinkage())
490 GV.setLinkage(GlobalValue::InternalLinkage);
493 static void drop(GlobalValue &GV) {
494 if (auto *F = dyn_cast<Function>(&GV)) {
496 F->setComdat(nullptr); // Should deleteBody do this?
500 if (auto *Var = dyn_cast<GlobalVariable>(&GV)) {
501 Var->setInitializer(nullptr);
503 GlobalValue::ExternalLinkage); // Should setInitializer do this?
504 Var->setComdat(nullptr); // and this?
508 auto &Alias = cast<GlobalAlias>(GV);
509 Module &M = *Alias.getParent();
510 PointerType &Ty = *cast<PointerType>(Alias.getType());
511 GlobalValue::LinkageTypes L = Alias.getLinkage();
513 new GlobalVariable(M, Ty.getElementType(), /*isConstant*/ false, L,
514 /*Initializer*/ nullptr);
515 Var->takeName(&Alias);
516 Alias.replaceAllUsesWith(Var);
517 Alias.eraseFromParent();
520 static const char *getResolutionName(ld_plugin_symbol_resolution R) {
526 case LDPR_PREVAILING_DEF:
527 return "PREVAILING_DEF";
528 case LDPR_PREVAILING_DEF_IRONLY:
529 return "PREVAILING_DEF_IRONLY";
530 case LDPR_PREEMPTED_REG:
531 return "PREEMPTED_REG";
532 case LDPR_PREEMPTED_IR:
533 return "PREEMPTED_IR";
534 case LDPR_RESOLVED_IR:
535 return "RESOLVED_IR";
536 case LDPR_RESOLVED_EXEC:
537 return "RESOLVED_EXEC";
538 case LDPR_RESOLVED_DYN:
539 return "RESOLVED_DYN";
540 case LDPR_PREVAILING_DEF_IRONLY_EXP:
541 return "PREVAILING_DEF_IRONLY_EXP";
543 llvm_unreachable("Unknown resolution");
547 class LocalValueMaterializer final : public ValueMaterializer {
548 DenseSet<GlobalValue *> &Dropped;
549 DenseMap<GlobalObject *, GlobalObject *> LocalVersions;
552 LocalValueMaterializer(DenseSet<GlobalValue *> &Dropped) : Dropped(Dropped) {}
553 Value *materializeValueFor(Value *V) override;
557 Value *LocalValueMaterializer::materializeValueFor(Value *V) {
558 auto *GO = dyn_cast<GlobalObject>(V);
562 auto I = LocalVersions.find(GO);
563 if (I != LocalVersions.end())
566 if (!Dropped.count(GO))
569 Module &M = *GO->getParent();
570 GlobalValue::LinkageTypes L = GO->getLinkage();
571 GlobalObject *Declaration;
572 if (auto *F = dyn_cast<Function>(GO)) {
573 Declaration = Function::Create(F->getFunctionType(), L, "", &M);
575 auto *Var = cast<GlobalVariable>(GO);
576 Declaration = new GlobalVariable(M, Var->getType()->getElementType(),
577 Var->isConstant(), L,
578 /*Initializer*/ nullptr);
580 Declaration->takeName(GO);
581 Declaration->copyAttributesFrom(GO);
583 GO->setLinkage(GlobalValue::InternalLinkage);
584 GO->setName(Declaration->getName());
586 GO->replaceAllUsesWith(Declaration);
588 LocalVersions[Declaration] = GO;
593 static Constant *mapConstantToLocalCopy(Constant *C, ValueToValueMapTy &VM,
594 LocalValueMaterializer *Materializer) {
595 return MapValue(C, VM, RF_IgnoreMissingEntries, nullptr, Materializer);
598 static void freeSymName(ld_plugin_symbol &Sym) {
600 free(Sym.comdat_key);
602 Sym.comdat_key = nullptr;
605 static std::unique_ptr<FunctionInfoIndex> getFunctionIndexForFile(
606 LLVMContext &Context, claimed_file &F, ld_plugin_input_file &Info) {
608 if (get_symbols(F.handle, F.syms.size(), &F.syms[0]) != LDPS_OK)
609 message(LDPL_FATAL, "Failed to get symbol information");
612 if (get_view(F.handle, &View) != LDPS_OK)
613 message(LDPL_FATAL, "Failed to get a view of file");
615 MemoryBufferRef BufferRef(StringRef((const char *)View, Info.filesize),
617 ErrorOr<std::unique_ptr<object::FunctionIndexObjectFile>> ObjOrErr =
618 object::FunctionIndexObjectFile::create(BufferRef, Context);
620 if (std::error_code EC = ObjOrErr.getError())
621 message(LDPL_FATAL, "Could not read function index bitcode from file : %s",
622 EC.message().c_str());
624 object::FunctionIndexObjectFile &Obj = **ObjOrErr;
626 return Obj.takeIndex();
629 static std::unique_ptr<Module>
630 getModuleForFile(LLVMContext &Context, claimed_file &F,
631 ld_plugin_input_file &Info, raw_fd_ostream *ApiFile,
632 StringSet<> &Internalize, StringSet<> &Maybe) {
634 if (get_symbols(F.handle, F.syms.size(), F.syms.data()) != LDPS_OK)
635 message(LDPL_FATAL, "Failed to get symbol information");
638 if (get_view(F.handle, &View) != LDPS_OK)
639 message(LDPL_FATAL, "Failed to get a view of file");
641 MemoryBufferRef BufferRef(StringRef((const char *)View, Info.filesize),
643 ErrorOr<std::unique_ptr<object::IRObjectFile>> ObjOrErr =
644 object::IRObjectFile::create(BufferRef, Context);
646 if (std::error_code EC = ObjOrErr.getError())
647 message(LDPL_FATAL, "Could not read bitcode from file : %s",
648 EC.message().c_str());
650 object::IRObjectFile &Obj = **ObjOrErr;
652 Module &M = Obj.getModule();
654 M.materializeMetadata();
657 SmallPtrSet<GlobalValue *, 8> Used;
658 collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ false);
660 DenseSet<GlobalValue *> Drop;
661 std::vector<GlobalAlias *> KeptAliases;
664 for (auto &ObjSym : Obj.symbols()) {
665 if (shouldSkip(ObjSym.getFlags()))
667 ld_plugin_symbol &Sym = F.syms[SymNum];
670 ld_plugin_symbol_resolution Resolution =
671 (ld_plugin_symbol_resolution)Sym.resolution;
673 if (options::generate_api_file)
674 *ApiFile << Sym.name << ' ' << getResolutionName(Resolution) << '\n';
676 GlobalValue *GV = Obj.getSymbolGV(ObjSym.getRawDataRefImpl());
679 continue; // Asm symbol.
682 if (Resolution != LDPR_PREVAILING_DEF_IRONLY && GV->hasCommonLinkage()) {
683 // Common linkage is special. There is no single symbol that wins the
684 // resolution. Instead we have to collect the maximum alignment and size.
685 // The IR linker does that for us if we just pass it every common GV.
686 // We still have to keep track of LDPR_PREVAILING_DEF_IRONLY so we
687 // internalize once the IR linker has done its job.
692 switch (Resolution) {
694 llvm_unreachable("Unexpected resolution");
696 case LDPR_RESOLVED_IR:
697 case LDPR_RESOLVED_EXEC:
698 case LDPR_RESOLVED_DYN:
699 assert(GV->isDeclarationForLinker());
703 if (!GV->isDeclarationForLinker()) {
704 assert(GV->hasComdat());
709 case LDPR_PREVAILING_DEF_IRONLY: {
710 keepGlobalValue(*GV, KeptAliases);
711 if (!Used.count(GV)) {
712 // Since we use the regular lib/Linker, we cannot just internalize GV
713 // now or it will not be copied to the merged module. Instead we force
714 // it to be copied and then internalize it.
715 Internalize.insert(GV->getName());
720 case LDPR_PREVAILING_DEF:
721 keepGlobalValue(*GV, KeptAliases);
724 case LDPR_PREEMPTED_IR:
725 // Gold might have selected a linkonce_odr and preempted a weak_odr.
726 // In that case we have to make sure we don't end up internalizing it.
727 if (!GV->isDiscardableIfUnused())
728 Maybe.erase(GV->getName());
731 case LDPR_PREEMPTED_REG:
735 case LDPR_PREVAILING_DEF_IRONLY_EXP: {
736 // We can only check for address uses after we merge the modules. The
737 // reason is that this GV might have a copy in another module
738 // and in that module the address might be significant, but that
739 // copy will be LDPR_PREEMPTED_IR.
740 if (GV->hasLinkOnceODRLinkage())
741 Maybe.insert(GV->getName());
742 keepGlobalValue(*GV, KeptAliases);
750 ValueToValueMapTy VM;
751 LocalValueMaterializer Materializer(Drop);
752 for (GlobalAlias *GA : KeptAliases) {
753 // Gold told us to keep GA. It is possible that a GV usied in the aliasee
754 // expression is being dropped. If that is the case, that GV must be copied.
755 Constant *Aliasee = GA->getAliasee();
756 Constant *Replacement = mapConstantToLocalCopy(Aliasee, VM, &Materializer);
757 GA->setAliasee(Replacement);
760 for (auto *GV : Drop)
763 return Obj.takeModule();
766 static void runLTOPasses(Module &M, TargetMachine &TM) {
767 M.setDataLayout(TM.createDataLayout());
769 legacy::PassManager passes;
770 passes.add(createTargetTransformInfoWrapperPass(TM.getTargetIRAnalysis()));
772 PassManagerBuilder PMB;
773 PMB.LibraryInfo = new TargetLibraryInfoImpl(Triple(TM.getTargetTriple()));
774 PMB.Inliner = createFunctionInliningPass();
775 // Unconditionally verify input since it is not verified before this
776 // point and has unknown origin.
777 PMB.VerifyInput = true;
778 PMB.VerifyOutput = !options::DisableVerify;
779 PMB.LoopVectorize = true;
780 PMB.SLPVectorize = true;
781 PMB.OptLevel = options::OptLevel;
782 PMB.populateLTOPassManager(passes);
786 static void saveBCFile(StringRef Path, Module &M) {
788 raw_fd_ostream OS(Path, EC, sys::fs::OpenFlags::F_None);
790 message(LDPL_FATAL, "Failed to write the output file.");
791 WriteBitcodeToFile(&M, OS, /* ShouldPreserveUseListOrder */ true);
794 static void codegen(std::unique_ptr<Module> M) {
795 const std::string &TripleStr = M->getTargetTriple();
796 Triple TheTriple(TripleStr);
799 const Target *TheTarget = TargetRegistry::lookupTarget(TripleStr, ErrMsg);
801 message(LDPL_FATAL, "Target not found: %s", ErrMsg.c_str());
803 if (unsigned NumOpts = options::extra.size())
804 cl::ParseCommandLineOptions(NumOpts, &options::extra[0]);
806 SubtargetFeatures Features;
807 Features.getDefaultSubtargetFeatures(TheTriple);
808 for (const std::string &A : MAttrs)
809 Features.AddFeature(A);
811 TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
812 CodeGenOpt::Level CGOptLevel;
813 switch (options::OptLevel) {
815 CGOptLevel = CodeGenOpt::None;
818 CGOptLevel = CodeGenOpt::Less;
821 CGOptLevel = CodeGenOpt::Default;
824 CGOptLevel = CodeGenOpt::Aggressive;
827 std::unique_ptr<TargetMachine> TM(TheTarget->createTargetMachine(
828 TripleStr, options::mcpu, Features.getString(), Options, RelocationModel,
829 CodeModel::Default, CGOptLevel));
831 runLTOPasses(*M, *TM);
833 if (options::TheOutputType == options::OT_SAVE_TEMPS)
834 saveBCFile(output_name + ".opt.bc", *M);
836 SmallString<128> Filename;
837 if (!options::obj_path.empty())
838 Filename = options::obj_path;
839 else if (options::TheOutputType == options::OT_SAVE_TEMPS)
840 Filename = output_name + ".o";
842 std::vector<SmallString<128>> Filenames(options::Parallelism);
843 bool TempOutFile = Filename.empty();
845 // Open a file descriptor for each backend thread. This is done in a block
846 // so that the output file descriptors are closed before gold opens them.
847 std::list<llvm::raw_fd_ostream> OSs;
848 std::vector<llvm::raw_pwrite_stream *> OSPtrs(options::Parallelism);
849 for (unsigned I = 0; I != options::Parallelism; ++I) {
853 sys::fs::createTemporaryFile("lto-llvm", "o", FD, Filenames[I]);
855 message(LDPL_FATAL, "Could not create temporary file: %s",
856 EC.message().c_str());
858 Filenames[I] = Filename;
859 if (options::Parallelism != 1)
860 Filenames[I] += utostr(I);
862 sys::fs::openFileForWrite(Filenames[I], FD, sys::fs::F_None);
864 message(LDPL_FATAL, "Could not open file: %s", EC.message().c_str());
866 OSs.emplace_back(FD, true);
867 OSPtrs[I] = &OSs.back();
870 // Run backend threads.
871 splitCodeGen(std::move(M), OSPtrs, options::mcpu, Features.getString(),
872 Options, RelocationModel, CodeModel::Default, CGOptLevel);
875 for (auto &Filename : Filenames) {
876 if (add_input_file(Filename.c_str()) != LDPS_OK)
878 "Unable to add .o file to the link. File left behind in: %s",
881 Cleanup.push_back(Filename.c_str());
885 /// gold informs us that all symbols have been read. At this point, we use
886 /// get_symbols to see if any of our definitions have been overridden by a
887 /// native object file. Then, perform optimization and codegen.
888 static ld_plugin_status allSymbolsReadHook(raw_fd_ostream *ApiFile) {
893 Context.setDiagnosticHandler(diagnosticHandler, nullptr, true);
895 // If we are doing ThinLTO compilation, simply build the combined
896 // function index/summary and emit it. We don't need to parse the modules
897 // and link them in this case.
898 if (options::thinlto) {
899 FunctionInfoIndex CombinedIndex;
900 uint64_t NextModuleId = 0;
901 for (claimed_file &F : Modules) {
902 ld_plugin_input_file File;
903 if (get_input_file(F.handle, &File) != LDPS_OK)
904 message(LDPL_FATAL, "Failed to get file information");
906 std::unique_ptr<FunctionInfoIndex> Index =
907 getFunctionIndexForFile(Context, F, File);
908 CombinedIndex.mergeFrom(std::move(Index), ++NextModuleId);
912 raw_fd_ostream OS(output_name + ".thinlto.bc", EC,
913 sys::fs::OpenFlags::F_None);
915 message(LDPL_FATAL, "Unable to open %s.thinlto.bc for writing: %s",
916 output_name.data(), EC.message().c_str());
917 WriteFunctionSummaryToFile(CombinedIndex, OS);
924 std::unique_ptr<Module> Combined(new Module("ld-temp.o", Context));
925 Linker L(Combined.get());
927 std::string DefaultTriple = sys::getDefaultTargetTriple();
929 StringSet<> Internalize;
931 for (claimed_file &F : Modules) {
932 ld_plugin_input_file File;
933 if (get_input_file(F.handle, &File) != LDPS_OK)
934 message(LDPL_FATAL, "Failed to get file information");
935 std::unique_ptr<Module> M =
936 getModuleForFile(Context, F, File, ApiFile, Internalize, Maybe);
937 if (!options::triple.empty())
938 M->setTargetTriple(options::triple.c_str());
939 else if (M->getTargetTriple().empty()) {
940 M->setTargetTriple(DefaultTriple);
943 if (L.linkInModule(M.get()))
944 message(LDPL_FATAL, "Failed to link module");
945 if (release_input_file(F.handle) != LDPS_OK)
946 message(LDPL_FATAL, "Failed to release file information");
949 for (const auto &Name : Internalize) {
950 GlobalValue *GV = Combined->getNamedValue(Name.first());
955 for (const auto &Name : Maybe) {
956 GlobalValue *GV = Combined->getNamedValue(Name.first());
959 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
960 if (canBeOmittedFromSymbolTable(GV))
964 if (options::TheOutputType == options::OT_DISABLE)
967 if (options::TheOutputType != options::OT_NORMAL) {
969 if (options::TheOutputType == options::OT_BC_ONLY)
972 path = output_name + ".bc";
973 saveBCFile(path, *L.getModule());
974 if (options::TheOutputType == options::OT_BC_ONLY)
978 codegen(std::move(Combined));
980 if (!options::extra_library_path.empty() &&
981 set_extra_library_path(options::extra_library_path.c_str()) != LDPS_OK)
982 message(LDPL_FATAL, "Unable to set the extra library path.");
987 static ld_plugin_status all_symbols_read_hook(void) {
988 ld_plugin_status Ret;
989 if (!options::generate_api_file) {
990 Ret = allSymbolsReadHook(nullptr);
993 raw_fd_ostream ApiFile("apifile.txt", EC, sys::fs::F_None);
995 message(LDPL_FATAL, "Unable to open apifile.txt for writing: %s",
996 EC.message().c_str());
997 Ret = allSymbolsReadHook(&ApiFile);
1002 if (options::TheOutputType == options::OT_BC_ONLY ||
1003 options::TheOutputType == options::OT_DISABLE) {
1004 if (options::TheOutputType == options::OT_DISABLE)
1005 // Remove the output file here since ld.bfd creates the output file
1007 sys::fs::remove(output_name);
1014 static ld_plugin_status cleanup_hook(void) {
1015 for (std::string &Name : Cleanup) {
1016 std::error_code EC = sys::fs::remove(Name);
1018 message(LDPL_ERROR, "Failed to delete '%s': %s", Name.c_str(),
1019 EC.message().c_str());