• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- gold-plugin.cpp - Plugin to gold for Link Time Optimization  ------===//
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 is a gold plugin for LLVM. It provides an LLVM implementation of the
11 // interface described in http://gcc.gnu.org/wiki/whopr/driver .
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "llvm/ADT/StringSet.h"
16 #include "llvm/Analysis/TargetLibraryInfo.h"
17 #include "llvm/Analysis/TargetTransformInfo.h"
18 #include "llvm/Bitcode/ReaderWriter.h"
19 #include "llvm/CodeGen/Analysis.h"
20 #include "llvm/CodeGen/CommandFlags.h"
21 #include "llvm/CodeGen/ParallelCG.h"
22 #include "llvm/Config/config.h" // plugin-api.h requires HAVE_STDINT_H
23 #include "llvm/IR/AutoUpgrade.h"
24 #include "llvm/IR/Constants.h"
25 #include "llvm/IR/DiagnosticInfo.h"
26 #include "llvm/IR/DiagnosticPrinter.h"
27 #include "llvm/IR/LLVMContext.h"
28 #include "llvm/IR/LegacyPassManager.h"
29 #include "llvm/IR/Module.h"
30 #include "llvm/IR/Verifier.h"
31 #include "llvm/LTO/LTO.h"
32 #include "llvm/Linker/IRMover.h"
33 #include "llvm/MC/SubtargetFeature.h"
34 #include "llvm/Object/IRObjectFile.h"
35 #include "llvm/Object/ModuleSummaryIndexObjectFile.h"
36 #include "llvm/Support/Host.h"
37 #include "llvm/Support/ManagedStatic.h"
38 #include "llvm/Support/MemoryBuffer.h"
39 #include "llvm/Support/Path.h"
40 #include "llvm/Support/TargetRegistry.h"
41 #include "llvm/Support/TargetSelect.h"
42 #include "llvm/Support/ThreadPool.h"
43 #include "llvm/Support/raw_ostream.h"
44 #include "llvm/Support/thread.h"
45 #include "llvm/Transforms/IPO.h"
46 #include "llvm/Transforms/IPO/FunctionImport.h"
47 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
48 #include "llvm/Transforms/Utils/FunctionImportUtils.h"
49 #include "llvm/Transforms/Utils/GlobalStatus.h"
50 #include "llvm/Transforms/Utils/ValueMapper.h"
51 #include <list>
52 #include <plugin-api.h>
53 #include <system_error>
54 #include <utility>
55 #include <vector>
56 
57 // FIXME: remove this declaration when we stop maintaining Ubuntu Quantal and
58 // Precise and Debian Wheezy (binutils 2.23 is required)
59 #define LDPO_PIE 3
60 
61 #define LDPT_GET_SYMBOLS_V3 28
62 
63 using namespace llvm;
64 
discard_message(int level,const char * format,...)65 static ld_plugin_status discard_message(int level, const char *format, ...) {
66   // Die loudly. Recent versions of Gold pass ld_plugin_message as the first
67   // callback in the transfer vector. This should never be called.
68   abort();
69 }
70 
71 static ld_plugin_release_input_file release_input_file = nullptr;
72 static ld_plugin_get_input_file get_input_file = nullptr;
73 static ld_plugin_message message = discard_message;
74 
75 namespace {
76 struct claimed_file {
77   void *handle;
78   void *leader_handle;
79   std::vector<ld_plugin_symbol> syms;
80   off_t filesize;
81   std::string name;
82 };
83 
84 /// RAII wrapper to manage opening and releasing of a ld_plugin_input_file.
85 struct PluginInputFile {
86   void *Handle;
87   std::unique_ptr<ld_plugin_input_file> File;
88 
PluginInputFile__anon1f3f5eac0111::PluginInputFile89   PluginInputFile(void *Handle) : Handle(Handle) {
90     File = llvm::make_unique<ld_plugin_input_file>();
91     if (get_input_file(Handle, File.get()) != LDPS_OK)
92       message(LDPL_FATAL, "Failed to get file information");
93   }
~PluginInputFile__anon1f3f5eac0111::PluginInputFile94   ~PluginInputFile() {
95     // File would have been reset to nullptr if we moved this object
96     // to a new owner.
97     if (File)
98       if (release_input_file(Handle) != LDPS_OK)
99         message(LDPL_FATAL, "Failed to release file information");
100   }
101 
file__anon1f3f5eac0111::PluginInputFile102   ld_plugin_input_file &file() { return *File; }
103 
104   PluginInputFile(PluginInputFile &&RHS) = default;
105   PluginInputFile &operator=(PluginInputFile &&RHS) = default;
106 };
107 
108 struct ResolutionInfo {
109   uint64_t CommonSize = 0;
110   unsigned CommonAlign = 0;
111   bool IsLinkonceOdr = true;
112   GlobalValue::UnnamedAddr UnnamedAddr = GlobalValue::UnnamedAddr::Global;
113   GlobalValue::VisibilityTypes Visibility = GlobalValue::DefaultVisibility;
114   bool CommonInternal = false;
115   bool UseCommon = false;
116 };
117 
118 /// Class to own information used by a task or during its cleanup for a
119 /// ThinLTO backend instantiation.
120 class ThinLTOTaskInfo {
121   /// The output stream the task will codegen into.
122   std::unique_ptr<raw_fd_ostream> OS;
123 
124   /// The file name corresponding to the output stream, used during cleanup.
125   std::string Filename;
126 
127   /// Flag indicating whether the output file is a temp file that must be
128   /// added to the cleanup list during cleanup.
129   bool TempOutFile;
130 
131 public:
ThinLTOTaskInfo(std::unique_ptr<raw_fd_ostream> OS,std::string Filename,bool TempOutFile)132   ThinLTOTaskInfo(std::unique_ptr<raw_fd_ostream> OS, std::string Filename,
133                   bool TempOutFile)
134       : OS(std::move(OS)), Filename(std::move(Filename)),
135         TempOutFile(TempOutFile) {}
136 
137   /// Performs task related cleanup activities that must be done
138   /// single-threaded (i.e. call backs to gold).
139   void cleanup();
140 };
141 }
142 
143 static ld_plugin_add_symbols add_symbols = nullptr;
144 static ld_plugin_get_symbols get_symbols = nullptr;
145 static ld_plugin_add_input_file add_input_file = nullptr;
146 static ld_plugin_set_extra_library_path set_extra_library_path = nullptr;
147 static ld_plugin_get_view get_view = nullptr;
148 static Optional<Reloc::Model> RelocationModel;
149 static std::string output_name = "";
150 static std::list<claimed_file> Modules;
151 static DenseMap<int, void *> FDToLeaderHandle;
152 static StringMap<ResolutionInfo> ResInfo;
153 static std::vector<std::string> Cleanup;
154 static llvm::TargetOptions TargetOpts;
155 static std::string DefaultTriple = sys::getDefaultTargetTriple();
156 
157 namespace options {
158   enum OutputType {
159     OT_NORMAL,
160     OT_DISABLE,
161     OT_BC_ONLY,
162     OT_SAVE_TEMPS
163   };
164   static bool generate_api_file = false;
165   static OutputType TheOutputType = OT_NORMAL;
166   static unsigned OptLevel = 2;
167   // Default parallelism of 0 used to indicate that user did not specify.
168   // Actual parallelism default value depends on implementation.
169   // Currently, code generation defaults to no parallelism, whereas
170   // ThinLTO uses the hardware_concurrency as the default.
171   static unsigned Parallelism = 0;
172 #ifdef NDEBUG
173   static bool DisableVerify = true;
174 #else
175   static bool DisableVerify = false;
176 #endif
177   static std::string obj_path;
178   static std::string extra_library_path;
179   static std::string triple;
180   static std::string mcpu;
181   // When the thinlto plugin option is specified, only read the function
182   // the information from intermediate files and write a combined
183   // global index for the ThinLTO backends.
184   static bool thinlto = false;
185   // If false, all ThinLTO backend compilations through code gen are performed
186   // using multiple threads in the gold-plugin, before handing control back to
187   // gold. If true, write individual backend index files which reflect
188   // the import decisions, and exit afterwards. The assumption is
189   // that the build system will launch the backend processes.
190   static bool thinlto_index_only = false;
191   // If true, when generating individual index files for distributed backends,
192   // also generate a "${bitcodefile}.imports" file at the same location for each
193   // bitcode file, listing the files it imports from in plain text. This is to
194   // support distributed build file staging.
195   static bool thinlto_emit_imports_files = false;
196   // Option to control where files for a distributed backend (the individual
197   // index files and optional imports files) are created.
198   // If specified, expects a string of the form "oldprefix:newprefix", and
199   // instead of generating these files in the same directory path as the
200   // corresponding bitcode file, will use a path formed by replacing the
201   // bitcode file's path prefix matching oldprefix with newprefix.
202   static std::string thinlto_prefix_replace;
203   // Additional options to pass into the code generator.
204   // Note: This array will contain all plugin options which are not claimed
205   // as plugin exclusive to pass to the code generator.
206   // For example, "generate-api-file" and "as"options are for the plugin
207   // use only and will not be passed.
208   static std::vector<const char *> extra;
209 
process_plugin_option(const char * opt_)210   static void process_plugin_option(const char *opt_)
211   {
212     if (opt_ == nullptr)
213       return;
214     llvm::StringRef opt = opt_;
215 
216     if (opt == "generate-api-file") {
217       generate_api_file = true;
218     } else if (opt.startswith("mcpu=")) {
219       mcpu = opt.substr(strlen("mcpu="));
220     } else if (opt.startswith("extra-library-path=")) {
221       extra_library_path = opt.substr(strlen("extra_library_path="));
222     } else if (opt.startswith("mtriple=")) {
223       triple = opt.substr(strlen("mtriple="));
224     } else if (opt.startswith("obj-path=")) {
225       obj_path = opt.substr(strlen("obj-path="));
226     } else if (opt == "emit-llvm") {
227       TheOutputType = OT_BC_ONLY;
228     } else if (opt == "save-temps") {
229       TheOutputType = OT_SAVE_TEMPS;
230     } else if (opt == "disable-output") {
231       TheOutputType = OT_DISABLE;
232     } else if (opt == "thinlto") {
233       thinlto = true;
234     } else if (opt == "thinlto-index-only") {
235       thinlto_index_only = true;
236     } else if (opt == "thinlto-emit-imports-files") {
237       thinlto_emit_imports_files = true;
238     } else if (opt.startswith("thinlto-prefix-replace=")) {
239       thinlto_prefix_replace = opt.substr(strlen("thinlto-prefix-replace="));
240       if (thinlto_prefix_replace.find(";") == std::string::npos)
241         message(LDPL_FATAL, "thinlto-prefix-replace expects 'old;new' format");
242     } else if (opt.size() == 2 && opt[0] == 'O') {
243       if (opt[1] < '0' || opt[1] > '3')
244         message(LDPL_FATAL, "Optimization level must be between 0 and 3");
245       OptLevel = opt[1] - '0';
246     } else if (opt.startswith("jobs=")) {
247       if (StringRef(opt_ + 5).getAsInteger(10, Parallelism))
248         message(LDPL_FATAL, "Invalid parallelism level: %s", opt_ + 5);
249     } else if (opt == "disable-verify") {
250       DisableVerify = true;
251     } else {
252       // Save this option to pass to the code generator.
253       // ParseCommandLineOptions() expects argv[0] to be program name. Lazily
254       // add that.
255       if (extra.empty())
256         extra.push_back("LLVMgold");
257 
258       extra.push_back(opt_);
259     }
260   }
261 }
262 
263 static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
264                                         int *claimed);
265 static ld_plugin_status all_symbols_read_hook(void);
266 static ld_plugin_status cleanup_hook(void);
267 
268 extern "C" ld_plugin_status onload(ld_plugin_tv *tv);
onload(ld_plugin_tv * tv)269 ld_plugin_status onload(ld_plugin_tv *tv) {
270   InitializeAllTargetInfos();
271   InitializeAllTargets();
272   InitializeAllTargetMCs();
273   InitializeAllAsmParsers();
274   InitializeAllAsmPrinters();
275 
276   // We're given a pointer to the first transfer vector. We read through them
277   // until we find one where tv_tag == LDPT_NULL. The REGISTER_* tagged values
278   // contain pointers to functions that we need to call to register our own
279   // hooks. The others are addresses of functions we can use to call into gold
280   // for services.
281 
282   bool registeredClaimFile = false;
283   bool RegisteredAllSymbolsRead = false;
284 
285   for (; tv->tv_tag != LDPT_NULL; ++tv) {
286     // Cast tv_tag to int to allow values not in "enum ld_plugin_tag", like, for
287     // example, LDPT_GET_SYMBOLS_V3 when building against an older plugin-api.h
288     // header.
289     switch (static_cast<int>(tv->tv_tag)) {
290     case LDPT_OUTPUT_NAME:
291       output_name = tv->tv_u.tv_string;
292       break;
293     case LDPT_LINKER_OUTPUT:
294       switch (tv->tv_u.tv_val) {
295       case LDPO_REL: // .o
296       case LDPO_DYN: // .so
297       case LDPO_PIE: // position independent executable
298         RelocationModel = Reloc::PIC_;
299         break;
300       case LDPO_EXEC: // .exe
301         RelocationModel = Reloc::Static;
302         break;
303       default:
304         message(LDPL_ERROR, "Unknown output file type %d", tv->tv_u.tv_val);
305         return LDPS_ERR;
306       }
307       break;
308     case LDPT_OPTION:
309       options::process_plugin_option(tv->tv_u.tv_string);
310       break;
311     case LDPT_REGISTER_CLAIM_FILE_HOOK: {
312       ld_plugin_register_claim_file callback;
313       callback = tv->tv_u.tv_register_claim_file;
314 
315       if (callback(claim_file_hook) != LDPS_OK)
316         return LDPS_ERR;
317 
318       registeredClaimFile = true;
319     } break;
320     case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK: {
321       ld_plugin_register_all_symbols_read callback;
322       callback = tv->tv_u.tv_register_all_symbols_read;
323 
324       if (callback(all_symbols_read_hook) != LDPS_OK)
325         return LDPS_ERR;
326 
327       RegisteredAllSymbolsRead = true;
328     } break;
329     case LDPT_REGISTER_CLEANUP_HOOK: {
330       ld_plugin_register_cleanup callback;
331       callback = tv->tv_u.tv_register_cleanup;
332 
333       if (callback(cleanup_hook) != LDPS_OK)
334         return LDPS_ERR;
335     } break;
336     case LDPT_GET_INPUT_FILE:
337       get_input_file = tv->tv_u.tv_get_input_file;
338       break;
339     case LDPT_RELEASE_INPUT_FILE:
340       release_input_file = tv->tv_u.tv_release_input_file;
341       break;
342     case LDPT_ADD_SYMBOLS:
343       add_symbols = tv->tv_u.tv_add_symbols;
344       break;
345     case LDPT_GET_SYMBOLS_V2:
346       // Do not override get_symbols_v3 with get_symbols_v2.
347       if (!get_symbols)
348         get_symbols = tv->tv_u.tv_get_symbols;
349       break;
350     case LDPT_GET_SYMBOLS_V3:
351       get_symbols = tv->tv_u.tv_get_symbols;
352       break;
353     case LDPT_ADD_INPUT_FILE:
354       add_input_file = tv->tv_u.tv_add_input_file;
355       break;
356     case LDPT_SET_EXTRA_LIBRARY_PATH:
357       set_extra_library_path = tv->tv_u.tv_set_extra_library_path;
358       break;
359     case LDPT_GET_VIEW:
360       get_view = tv->tv_u.tv_get_view;
361       break;
362     case LDPT_MESSAGE:
363       message = tv->tv_u.tv_message;
364       break;
365     default:
366       break;
367     }
368   }
369 
370   if (!registeredClaimFile) {
371     message(LDPL_ERROR, "register_claim_file not passed to LLVMgold.");
372     return LDPS_ERR;
373   }
374   if (!add_symbols) {
375     message(LDPL_ERROR, "add_symbols not passed to LLVMgold.");
376     return LDPS_ERR;
377   }
378 
379   if (!RegisteredAllSymbolsRead)
380     return LDPS_OK;
381 
382   if (!get_input_file) {
383     message(LDPL_ERROR, "get_input_file not passed to LLVMgold.");
384     return LDPS_ERR;
385   }
386   if (!release_input_file) {
387     message(LDPL_ERROR, "release_input_file not passed to LLVMgold.");
388     return LDPS_ERR;
389   }
390 
391   return LDPS_OK;
392 }
393 
getBaseObject(const GlobalValue & GV)394 static const GlobalObject *getBaseObject(const GlobalValue &GV) {
395   if (auto *GA = dyn_cast<GlobalAlias>(&GV))
396     return GA->getBaseObject();
397   return cast<GlobalObject>(&GV);
398 }
399 
shouldSkip(uint32_t Symflags)400 static bool shouldSkip(uint32_t Symflags) {
401   if (!(Symflags & object::BasicSymbolRef::SF_Global))
402     return true;
403   if (Symflags & object::BasicSymbolRef::SF_FormatSpecific)
404     return true;
405   return false;
406 }
407 
diagnosticHandler(const DiagnosticInfo & DI)408 static void diagnosticHandler(const DiagnosticInfo &DI) {
409   if (const auto *BDI = dyn_cast<BitcodeDiagnosticInfo>(&DI)) {
410     std::error_code EC = BDI->getError();
411     if (EC == BitcodeError::InvalidBitcodeSignature)
412       return;
413   }
414 
415   std::string ErrStorage;
416   {
417     raw_string_ostream OS(ErrStorage);
418     DiagnosticPrinterRawOStream DP(OS);
419     DI.print(DP);
420   }
421   ld_plugin_level Level;
422   switch (DI.getSeverity()) {
423   case DS_Error:
424     message(LDPL_FATAL, "LLVM gold plugin has failed to create LTO module: %s",
425             ErrStorage.c_str());
426   case DS_Warning:
427     Level = LDPL_WARNING;
428     break;
429   case DS_Note:
430   case DS_Remark:
431     Level = LDPL_INFO;
432     break;
433   }
434   message(Level, "LLVM gold plugin: %s",  ErrStorage.c_str());
435 }
436 
diagnosticHandlerForContext(const DiagnosticInfo & DI,void * Context)437 static void diagnosticHandlerForContext(const DiagnosticInfo &DI,
438                                         void *Context) {
439   diagnosticHandler(DI);
440 }
441 
442 static GlobalValue::VisibilityTypes
getMinVisibility(GlobalValue::VisibilityTypes A,GlobalValue::VisibilityTypes B)443 getMinVisibility(GlobalValue::VisibilityTypes A,
444                  GlobalValue::VisibilityTypes B) {
445   if (A == GlobalValue::HiddenVisibility)
446     return A;
447   if (B == GlobalValue::HiddenVisibility)
448     return B;
449   if (A == GlobalValue::ProtectedVisibility)
450     return A;
451   return B;
452 }
453 
454 /// Called by gold to see whether this file is one that our plugin can handle.
455 /// We'll try to open it and register all the symbols with add_symbol if
456 /// possible.
claim_file_hook(const ld_plugin_input_file * file,int * claimed)457 static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
458                                         int *claimed) {
459   LLVMContext Context;
460   MemoryBufferRef BufferRef;
461   std::unique_ptr<MemoryBuffer> Buffer;
462   if (get_view) {
463     const void *view;
464     if (get_view(file->handle, &view) != LDPS_OK) {
465       message(LDPL_ERROR, "Failed to get a view of %s", file->name);
466       return LDPS_ERR;
467     }
468     BufferRef =
469         MemoryBufferRef(StringRef((const char *)view, file->filesize), "");
470   } else {
471     int64_t offset = 0;
472     // Gold has found what might be IR part-way inside of a file, such as
473     // an .a archive.
474     if (file->offset) {
475       offset = file->offset;
476     }
477     ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
478         MemoryBuffer::getOpenFileSlice(file->fd, file->name, file->filesize,
479                                        offset);
480     if (std::error_code EC = BufferOrErr.getError()) {
481       message(LDPL_ERROR, EC.message().c_str());
482       return LDPS_ERR;
483     }
484     Buffer = std::move(BufferOrErr.get());
485     BufferRef = Buffer->getMemBufferRef();
486   }
487 
488   Context.setDiagnosticHandler(diagnosticHandlerForContext);
489   ErrorOr<std::unique_ptr<object::IRObjectFile>> ObjOrErr =
490       object::IRObjectFile::create(BufferRef, Context);
491   std::error_code EC = ObjOrErr.getError();
492   if (EC == object::object_error::invalid_file_type ||
493       EC == object::object_error::bitcode_section_not_found)
494     return LDPS_OK;
495 
496   *claimed = 1;
497 
498   if (EC) {
499     message(LDPL_ERROR, "LLVM gold plugin has failed to create LTO module: %s",
500             EC.message().c_str());
501     return LDPS_ERR;
502   }
503   std::unique_ptr<object::IRObjectFile> Obj = std::move(*ObjOrErr);
504 
505   Modules.resize(Modules.size() + 1);
506   claimed_file &cf = Modules.back();
507 
508   cf.handle = file->handle;
509   // Keep track of the first handle for each file descriptor, since there are
510   // multiple in the case of an archive. This is used later in the case of
511   // ThinLTO parallel backends to ensure that each file is only opened and
512   // released once.
513   auto LeaderHandle =
514       FDToLeaderHandle.insert(std::make_pair(file->fd, file->handle)).first;
515   cf.leader_handle = LeaderHandle->second;
516   // Save the filesize since for parallel ThinLTO backends we can only
517   // invoke get_input_file once per archive (only for the leader handle).
518   cf.filesize = file->filesize;
519   // In the case of an archive library, all but the first member must have a
520   // non-zero offset, which we can append to the file name to obtain a
521   // unique name.
522   cf.name = file->name;
523   if (file->offset)
524     cf.name += ".llvm." + std::to_string(file->offset) + "." +
525                sys::path::filename(Obj->getModule().getSourceFileName()).str();
526 
527   for (auto &Sym : Obj->symbols()) {
528     uint32_t Symflags = Sym.getFlags();
529     if (shouldSkip(Symflags))
530       continue;
531 
532     cf.syms.push_back(ld_plugin_symbol());
533     ld_plugin_symbol &sym = cf.syms.back();
534     sym.version = nullptr;
535 
536     SmallString<64> Name;
537     {
538       raw_svector_ostream OS(Name);
539       Sym.printName(OS);
540     }
541     sym.name = strdup(Name.c_str());
542 
543     const GlobalValue *GV = Obj->getSymbolGV(Sym.getRawDataRefImpl());
544 
545     ResolutionInfo &Res = ResInfo[sym.name];
546 
547     sym.visibility = LDPV_DEFAULT;
548     if (GV) {
549       Res.UnnamedAddr =
550           GlobalValue::getMinUnnamedAddr(Res.UnnamedAddr, GV->getUnnamedAddr());
551       Res.IsLinkonceOdr &= GV->hasLinkOnceLinkage();
552       Res.Visibility = getMinVisibility(Res.Visibility, GV->getVisibility());
553       switch (GV->getVisibility()) {
554       case GlobalValue::DefaultVisibility:
555         break;
556       case GlobalValue::HiddenVisibility:
557         sym.visibility = LDPV_HIDDEN;
558         break;
559       case GlobalValue::ProtectedVisibility:
560         sym.visibility = LDPV_PROTECTED;
561         break;
562       }
563     }
564 
565     if (Symflags & object::BasicSymbolRef::SF_Undefined) {
566       sym.def = LDPK_UNDEF;
567       if (GV && GV->hasExternalWeakLinkage())
568         sym.def = LDPK_WEAKUNDEF;
569     } else {
570       sym.def = LDPK_DEF;
571       if (GV) {
572         assert(!GV->hasExternalWeakLinkage() &&
573                !GV->hasAvailableExternallyLinkage() && "Not a declaration!");
574         if (GV->hasCommonLinkage())
575           sym.def = LDPK_COMMON;
576         else if (GV->isWeakForLinker())
577           sym.def = LDPK_WEAKDEF;
578       }
579     }
580 
581     sym.size = 0;
582     sym.comdat_key = nullptr;
583     if (GV) {
584       const GlobalObject *Base = getBaseObject(*GV);
585       if (!Base)
586         message(LDPL_FATAL, "Unable to determine comdat of alias!");
587       const Comdat *C = Base->getComdat();
588       if (C)
589         sym.comdat_key = strdup(C->getName().str().c_str());
590     }
591 
592     sym.resolution = LDPR_UNKNOWN;
593   }
594 
595   if (!cf.syms.empty()) {
596     if (add_symbols(cf.handle, cf.syms.size(), cf.syms.data()) != LDPS_OK) {
597       message(LDPL_ERROR, "Unable to add symbols!");
598       return LDPS_ERR;
599     }
600   }
601 
602   return LDPS_OK;
603 }
604 
internalize(GlobalValue & GV)605 static void internalize(GlobalValue &GV) {
606   if (GV.isDeclarationForLinker())
607     return; // We get here if there is a matching asm definition.
608   if (!GV.hasLocalLinkage())
609     GV.setLinkage(GlobalValue::InternalLinkage);
610 }
611 
getResolutionName(ld_plugin_symbol_resolution R)612 static const char *getResolutionName(ld_plugin_symbol_resolution R) {
613   switch (R) {
614   case LDPR_UNKNOWN:
615     return "UNKNOWN";
616   case LDPR_UNDEF:
617     return "UNDEF";
618   case LDPR_PREVAILING_DEF:
619     return "PREVAILING_DEF";
620   case LDPR_PREVAILING_DEF_IRONLY:
621     return "PREVAILING_DEF_IRONLY";
622   case LDPR_PREEMPTED_REG:
623     return "PREEMPTED_REG";
624   case LDPR_PREEMPTED_IR:
625     return "PREEMPTED_IR";
626   case LDPR_RESOLVED_IR:
627     return "RESOLVED_IR";
628   case LDPR_RESOLVED_EXEC:
629     return "RESOLVED_EXEC";
630   case LDPR_RESOLVED_DYN:
631     return "RESOLVED_DYN";
632   case LDPR_PREVAILING_DEF_IRONLY_EXP:
633     return "PREVAILING_DEF_IRONLY_EXP";
634   }
635   llvm_unreachable("Unknown resolution");
636 }
637 
freeSymName(ld_plugin_symbol & Sym)638 static void freeSymName(ld_plugin_symbol &Sym) {
639   free(Sym.name);
640   free(Sym.comdat_key);
641   Sym.name = nullptr;
642   Sym.comdat_key = nullptr;
643 }
644 
645 /// Helper to get a file's symbols and a view into it via gold callbacks.
getSymbolsAndView(claimed_file & F)646 static const void *getSymbolsAndView(claimed_file &F) {
647   ld_plugin_status status = get_symbols(F.handle, F.syms.size(), F.syms.data());
648   if (status == LDPS_NO_SYMS)
649     return nullptr;
650 
651   if (status != LDPS_OK)
652     message(LDPL_FATAL, "Failed to get symbol information");
653 
654   const void *View;
655   if (get_view(F.handle, &View) != LDPS_OK)
656     message(LDPL_FATAL, "Failed to get a view of file");
657 
658   return View;
659 }
660 
661 static std::unique_ptr<ModuleSummaryIndex>
getModuleSummaryIndexForFile(claimed_file & F)662 getModuleSummaryIndexForFile(claimed_file &F) {
663   const void *View = getSymbolsAndView(F);
664   if (!View)
665     return nullptr;
666 
667   MemoryBufferRef BufferRef(StringRef((const char *)View, F.filesize), F.name);
668 
669   // Don't bother trying to build an index if there is no summary information
670   // in this bitcode file.
671   if (!object::ModuleSummaryIndexObjectFile::hasGlobalValueSummaryInMemBuffer(
672           BufferRef, diagnosticHandler))
673     return std::unique_ptr<ModuleSummaryIndex>(nullptr);
674 
675   ErrorOr<std::unique_ptr<object::ModuleSummaryIndexObjectFile>> ObjOrErr =
676       object::ModuleSummaryIndexObjectFile::create(BufferRef,
677                                                    diagnosticHandler);
678 
679   if (std::error_code EC = ObjOrErr.getError())
680     message(LDPL_FATAL,
681             "Could not read module summary index bitcode from file : %s",
682             EC.message().c_str());
683 
684   object::ModuleSummaryIndexObjectFile &Obj = **ObjOrErr;
685 
686   return Obj.takeIndex();
687 }
688 
689 static std::unique_ptr<Module>
getModuleForFile(LLVMContext & Context,claimed_file & F,const void * View,StringRef Name,raw_fd_ostream * ApiFile,StringSet<> & Internalize,std::vector<GlobalValue * > & Keep,StringMap<unsigned> & Realign)690 getModuleForFile(LLVMContext &Context, claimed_file &F, const void *View,
691                  StringRef Name, raw_fd_ostream *ApiFile,
692                  StringSet<> &Internalize, std::vector<GlobalValue *> &Keep,
693                  StringMap<unsigned> &Realign) {
694   MemoryBufferRef BufferRef(StringRef((const char *)View, F.filesize), Name);
695   ErrorOr<std::unique_ptr<object::IRObjectFile>> ObjOrErr =
696       object::IRObjectFile::create(BufferRef, Context);
697 
698   if (std::error_code EC = ObjOrErr.getError())
699     message(LDPL_FATAL, "Could not read bitcode from file : %s",
700             EC.message().c_str());
701 
702   object::IRObjectFile &Obj = **ObjOrErr;
703 
704   Module &M = Obj.getModule();
705 
706   M.materializeMetadata();
707   UpgradeDebugInfo(M);
708 
709   SmallPtrSet<GlobalValue *, 8> Used;
710   collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ false);
711 
712   unsigned SymNum = 0;
713   for (auto &ObjSym : Obj.symbols()) {
714     GlobalValue *GV = Obj.getSymbolGV(ObjSym.getRawDataRefImpl());
715     if (GV && GV->hasAppendingLinkage())
716       Keep.push_back(GV);
717 
718     if (shouldSkip(ObjSym.getFlags()))
719       continue;
720     ld_plugin_symbol &Sym = F.syms[SymNum];
721     ++SymNum;
722 
723     ld_plugin_symbol_resolution Resolution =
724         (ld_plugin_symbol_resolution)Sym.resolution;
725 
726     if (options::generate_api_file)
727       *ApiFile << Sym.name << ' ' << getResolutionName(Resolution) << '\n';
728 
729     if (!GV) {
730       freeSymName(Sym);
731       continue; // Asm symbol.
732     }
733 
734     ResolutionInfo &Res = ResInfo[Sym.name];
735     if (Resolution == LDPR_PREVAILING_DEF_IRONLY_EXP && !Res.IsLinkonceOdr)
736       Resolution = LDPR_PREVAILING_DEF;
737 
738     GV->setUnnamedAddr(Res.UnnamedAddr);
739     GV->setVisibility(Res.Visibility);
740 
741     // Override gold's resolution for common symbols. We want the largest
742     // one to win.
743     if (GV->hasCommonLinkage()) {
744       if (Resolution == LDPR_PREVAILING_DEF_IRONLY)
745         Res.CommonInternal = true;
746 
747       if (Resolution == LDPR_PREVAILING_DEF_IRONLY ||
748           Resolution == LDPR_PREVAILING_DEF)
749         Res.UseCommon = true;
750 
751       const DataLayout &DL = GV->getParent()->getDataLayout();
752       uint64_t Size = DL.getTypeAllocSize(GV->getType()->getElementType());
753       unsigned Align = GV->getAlignment();
754 
755       if (Res.UseCommon && Size >= Res.CommonSize) {
756         // Take GV.
757         if (Res.CommonInternal)
758           Resolution = LDPR_PREVAILING_DEF_IRONLY;
759         else
760           Resolution = LDPR_PREVAILING_DEF;
761         cast<GlobalVariable>(GV)->setAlignment(
762             std::max(Res.CommonAlign, Align));
763       } else {
764         // Do not take GV, it's smaller than what we already have in the
765         // combined module.
766         Resolution = LDPR_PREEMPTED_IR;
767         if (Align > Res.CommonAlign)
768           // Need to raise the alignment though.
769           Realign[Sym.name] = Align;
770       }
771 
772       Res.CommonSize = std::max(Res.CommonSize, Size);
773       Res.CommonAlign = std::max(Res.CommonAlign, Align);
774     }
775 
776     switch (Resolution) {
777     case LDPR_UNKNOWN:
778       llvm_unreachable("Unexpected resolution");
779 
780     case LDPR_RESOLVED_IR:
781     case LDPR_RESOLVED_EXEC:
782     case LDPR_RESOLVED_DYN:
783     case LDPR_PREEMPTED_IR:
784     case LDPR_PREEMPTED_REG:
785       break;
786 
787     case LDPR_UNDEF:
788       if (!GV->isDeclarationForLinker())
789         assert(GV->hasComdat());
790       break;
791 
792     case LDPR_PREVAILING_DEF_IRONLY: {
793       Keep.push_back(GV);
794       // The IR linker has to be able to map this value to a declaration,
795       // so we can only internalize after linking.
796       if (!Used.count(GV))
797         Internalize.insert(GV->getName());
798       break;
799     }
800 
801     case LDPR_PREVAILING_DEF:
802       Keep.push_back(GV);
803       // There is a non IR use, so we have to force optimizations to keep this.
804       switch (GV->getLinkage()) {
805       default:
806         break;
807       case GlobalValue::LinkOnceAnyLinkage:
808         GV->setLinkage(GlobalValue::WeakAnyLinkage);
809         break;
810       case GlobalValue::LinkOnceODRLinkage:
811         GV->setLinkage(GlobalValue::WeakODRLinkage);
812         break;
813       }
814       break;
815 
816     case LDPR_PREVAILING_DEF_IRONLY_EXP: {
817       Keep.push_back(GV);
818       if (canBeOmittedFromSymbolTable(GV))
819         Internalize.insert(GV->getName());
820       break;
821     }
822     }
823 
824     freeSymName(Sym);
825   }
826 
827   return Obj.takeModule();
828 }
829 
saveBCFile(StringRef Path,Module & M)830 static void saveBCFile(StringRef Path, Module &M) {
831   std::error_code EC;
832   raw_fd_ostream OS(Path, EC, sys::fs::OpenFlags::F_None);
833   if (EC)
834     message(LDPL_FATAL, "Failed to write the output file.");
835   WriteBitcodeToFile(&M, OS, /* ShouldPreserveUseListOrder */ false);
836 }
837 
recordFile(std::string Filename,bool TempOutFile)838 static void recordFile(std::string Filename, bool TempOutFile) {
839   if (add_input_file(Filename.c_str()) != LDPS_OK)
840     message(LDPL_FATAL,
841             "Unable to add .o file to the link. File left behind in: %s",
842             Filename.c_str());
843   if (TempOutFile)
844     Cleanup.push_back(Filename.c_str());
845 }
846 
cleanup()847 void ThinLTOTaskInfo::cleanup() {
848   // Close the output file descriptor before we pass it to gold.
849   OS->close();
850 
851   recordFile(Filename, TempOutFile);
852 }
853 
854 namespace {
855 /// Class to manage optimization and code generation for a module, possibly
856 /// in a thread (ThinLTO).
857 class CodeGen {
858   /// The module for which this will generate code.
859   std::unique_ptr<llvm::Module> M;
860 
861   /// The output stream to generate code into.
862   raw_fd_ostream *OS;
863 
864   /// The task ID when this was invoked in a thread (ThinLTO).
865   int TaskID;
866 
867   /// The module summary index for ThinLTO tasks.
868   const ModuleSummaryIndex *CombinedIndex;
869 
870   /// The target machine for generating code for this module.
871   std::unique_ptr<TargetMachine> TM;
872 
873   /// Filename to use as base when save-temps is enabled, used to get
874   /// a unique and identifiable save-temps output file for each ThinLTO backend.
875   std::string SaveTempsFilename;
876 
877   /// Map from a module name to the corresponding buffer holding a view of the
878   /// bitcode provided via the get_view gold callback.
879   StringMap<MemoryBufferRef> *ModuleMap;
880 
881   // Functions to import into this module.
882   FunctionImporter::ImportMapTy *ImportList;
883 
884   // Map of globals defined in this module to their summary.
885   std::map<GlobalValue::GUID, GlobalValueSummary *> *DefinedGlobals;
886 
887 public:
888   /// Constructor used by full LTO.
CodeGen(std::unique_ptr<llvm::Module> M)889   CodeGen(std::unique_ptr<llvm::Module> M)
890       : M(std::move(M)), OS(nullptr), TaskID(-1), CombinedIndex(nullptr),
891         ModuleMap(nullptr) {
892     initTargetMachine();
893   }
894   /// Constructor used by ThinLTO.
CodeGen(std::unique_ptr<llvm::Module> M,raw_fd_ostream * OS,int TaskID,const ModuleSummaryIndex * CombinedIndex,std::string Filename,StringMap<MemoryBufferRef> * ModuleMap,FunctionImporter::ImportMapTy * ImportList,std::map<GlobalValue::GUID,GlobalValueSummary * > * DefinedGlobals)895   CodeGen(std::unique_ptr<llvm::Module> M, raw_fd_ostream *OS, int TaskID,
896           const ModuleSummaryIndex *CombinedIndex, std::string Filename,
897           StringMap<MemoryBufferRef> *ModuleMap,
898           FunctionImporter::ImportMapTy *ImportList,
899           std::map<GlobalValue::GUID, GlobalValueSummary *> *DefinedGlobals)
900       : M(std::move(M)), OS(OS), TaskID(TaskID), CombinedIndex(CombinedIndex),
901         SaveTempsFilename(std::move(Filename)), ModuleMap(ModuleMap),
902         ImportList(ImportList), DefinedGlobals(DefinedGlobals) {
903     assert(options::thinlto == !!CombinedIndex &&
904            "Expected module summary index iff performing ThinLTO");
905     initTargetMachine();
906   }
907 
908   /// Invoke LTO passes and the code generator for the module.
909   void runAll();
910 
911   /// Invoke the actual code generation to emit Module's object to file.
912   void runCodegenPasses();
913 
914 private:
915   const Target *TheTarget;
916   std::string TripleStr;
917   std::string FeaturesString;
918   TargetOptions Options;
919 
920   /// Create a target machine for the module. Must be unique for each
921   /// module/task.
922   void initTargetMachine();
923 
924   std::unique_ptr<TargetMachine> createTargetMachine();
925 
926   /// Run all LTO passes on the module.
927   void runLTOPasses();
928 
929   /// Sets up output files necessary to perform optional multi-threaded
930   /// split code generation, and invokes the code generation implementation.
931   /// If BCFileName is not empty, saves bitcode for module partitions into
932   /// {BCFileName}0 .. {BCFileName}N.
933   void runSplitCodeGen(const SmallString<128> &BCFilename);
934 };
935 }
936 
getFeatures(Triple & TheTriple)937 static SubtargetFeatures getFeatures(Triple &TheTriple) {
938   SubtargetFeatures Features;
939   Features.getDefaultSubtargetFeatures(TheTriple);
940   for (const std::string &A : MAttrs)
941     Features.AddFeature(A);
942   return Features;
943 }
944 
getCGOptLevel()945 static CodeGenOpt::Level getCGOptLevel() {
946   switch (options::OptLevel) {
947   case 0:
948     return CodeGenOpt::None;
949   case 1:
950     return CodeGenOpt::Less;
951   case 2:
952     return CodeGenOpt::Default;
953   case 3:
954     return CodeGenOpt::Aggressive;
955   }
956   llvm_unreachable("Invalid optimization level");
957 }
958 
initTargetMachine()959 void CodeGen::initTargetMachine() {
960   TripleStr = M->getTargetTriple();
961   Triple TheTriple(TripleStr);
962 
963   std::string ErrMsg;
964   TheTarget = TargetRegistry::lookupTarget(TripleStr, ErrMsg);
965   if (!TheTarget)
966     message(LDPL_FATAL, "Target not found: %s", ErrMsg.c_str());
967 
968   SubtargetFeatures Features = getFeatures(TheTriple);
969   FeaturesString = Features.getString();
970   Options = InitTargetOptionsFromCodeGenFlags();
971 
972   // Disable the new X86 relax relocations since gold might not support them.
973   // FIXME: Check the gold version or add a new option to enable them.
974   Options.RelaxELFRelocations = false;
975 
976   TM = createTargetMachine();
977 }
978 
createTargetMachine()979 std::unique_ptr<TargetMachine> CodeGen::createTargetMachine() {
980   CodeGenOpt::Level CGOptLevel = getCGOptLevel();
981 
982   return std::unique_ptr<TargetMachine>(TheTarget->createTargetMachine(
983       TripleStr, options::mcpu, FeaturesString, Options, RelocationModel,
984       CodeModel::Default, CGOptLevel));
985 }
986 
runLTOPasses()987 void CodeGen::runLTOPasses() {
988   M->setDataLayout(TM->createDataLayout());
989 
990   if (CombinedIndex) {
991     // Apply summary-based LinkOnce/Weak resolution decisions.
992     thinLTOResolveWeakForLinkerModule(*M, *DefinedGlobals);
993 
994     // Apply summary-based internalization decisions. Skip if there are no
995     // defined globals from the summary since not only is it unnecessary, but
996     // if this module did not have a summary section the internalizer will
997     // assert if it finds any definitions in this module that aren't in the
998     // DefinedGlobals set.
999     if (!DefinedGlobals->empty())
1000       thinLTOInternalizeModule(*M, *DefinedGlobals);
1001 
1002     // Create a loader that will parse the bitcode from the buffers
1003     // in the ModuleMap.
1004     ModuleLoader Loader(M->getContext(), *ModuleMap);
1005 
1006     // Perform function importing.
1007     FunctionImporter Importer(*CombinedIndex, Loader);
1008     Importer.importFunctions(*M, *ImportList);
1009   }
1010 
1011   legacy::PassManager passes;
1012   passes.add(createTargetTransformInfoWrapperPass(TM->getTargetIRAnalysis()));
1013 
1014   PassManagerBuilder PMB;
1015   PMB.LibraryInfo = new TargetLibraryInfoImpl(Triple(TM->getTargetTriple()));
1016   PMB.Inliner = createFunctionInliningPass();
1017   // Unconditionally verify input since it is not verified before this
1018   // point and has unknown origin.
1019   PMB.VerifyInput = true;
1020   PMB.VerifyOutput = !options::DisableVerify;
1021   PMB.LoopVectorize = true;
1022   PMB.SLPVectorize = true;
1023   PMB.OptLevel = options::OptLevel;
1024   if (options::thinlto)
1025     PMB.populateThinLTOPassManager(passes);
1026   else
1027     PMB.populateLTOPassManager(passes);
1028   passes.run(*M);
1029 }
1030 
1031 /// Open a file and return the new file descriptor given a base input
1032 /// file name, a flag indicating whether a temp file should be generated,
1033 /// and an optional task id. The new filename generated is
1034 /// returned in \p NewFilename.
openOutputFile(SmallString<128> InFilename,bool TempOutFile,SmallString<128> & NewFilename,int TaskID=-1)1035 static int openOutputFile(SmallString<128> InFilename, bool TempOutFile,
1036                           SmallString<128> &NewFilename, int TaskID = -1) {
1037   int FD;
1038   if (TempOutFile) {
1039     std::error_code EC =
1040         sys::fs::createTemporaryFile("lto-llvm", "o", FD, NewFilename);
1041     if (EC)
1042       message(LDPL_FATAL, "Could not create temporary file: %s",
1043               EC.message().c_str());
1044   } else {
1045     NewFilename = InFilename;
1046     if (TaskID >= 0)
1047       NewFilename += utostr(TaskID);
1048     std::error_code EC =
1049         sys::fs::openFileForWrite(NewFilename, FD, sys::fs::F_None);
1050     if (EC)
1051       message(LDPL_FATAL, "Could not open file: %s", EC.message().c_str());
1052   }
1053   return FD;
1054 }
1055 
runCodegenPasses()1056 void CodeGen::runCodegenPasses() {
1057   assert(OS && "Output stream must be set before emitting to file");
1058   legacy::PassManager CodeGenPasses;
1059   if (TM->addPassesToEmitFile(CodeGenPasses, *OS,
1060                               TargetMachine::CGFT_ObjectFile))
1061     report_fatal_error("Failed to setup codegen");
1062   CodeGenPasses.run(*M);
1063 }
1064 
runSplitCodeGen(const SmallString<128> & BCFilename)1065 void CodeGen::runSplitCodeGen(const SmallString<128> &BCFilename) {
1066   SmallString<128> Filename;
1067   // Note that openOutputFile will append a unique ID for each task
1068   if (!options::obj_path.empty())
1069     Filename = options::obj_path;
1070   else if (options::TheOutputType == options::OT_SAVE_TEMPS)
1071     Filename = output_name + ".o";
1072 
1073   // Note that the default parallelism is 1 instead of the
1074   // hardware_concurrency, as there are behavioral differences between
1075   // parallelism levels (e.g. symbol ordering will be different, and some uses
1076   // of inline asm currently have issues with parallelism >1).
1077   unsigned int MaxThreads = options::Parallelism ? options::Parallelism : 1;
1078 
1079   std::vector<SmallString<128>> Filenames(MaxThreads);
1080   std::vector<SmallString<128>> BCFilenames(MaxThreads);
1081   bool TempOutFile = Filename.empty();
1082   {
1083     // Open a file descriptor for each backend task. This is done in a block
1084     // so that the output file descriptors are closed before gold opens them.
1085     std::list<llvm::raw_fd_ostream> OSs;
1086     std::vector<llvm::raw_pwrite_stream *> OSPtrs(MaxThreads);
1087     for (unsigned I = 0; I != MaxThreads; ++I) {
1088       int FD = openOutputFile(Filename, TempOutFile, Filenames[I],
1089                               // Only append ID if there are multiple tasks.
1090                               MaxThreads > 1 ? I : -1);
1091       OSs.emplace_back(FD, true);
1092       OSPtrs[I] = &OSs.back();
1093     }
1094 
1095     std::list<llvm::raw_fd_ostream> BCOSs;
1096     std::vector<llvm::raw_pwrite_stream *> BCOSPtrs;
1097     if (!BCFilename.empty() && MaxThreads > 1) {
1098       for (unsigned I = 0; I != MaxThreads; ++I) {
1099         int FD = openOutputFile(BCFilename, false, BCFilenames[I], I);
1100         BCOSs.emplace_back(FD, true);
1101         BCOSPtrs.push_back(&BCOSs.back());
1102       }
1103     }
1104 
1105     // Run backend tasks.
1106     splitCodeGen(std::move(M), OSPtrs, BCOSPtrs,
1107                  [&]() { return createTargetMachine(); });
1108   }
1109 
1110   for (auto &Filename : Filenames)
1111     recordFile(Filename.c_str(), TempOutFile);
1112 }
1113 
runAll()1114 void CodeGen::runAll() {
1115   runLTOPasses();
1116 
1117   SmallString<128> OptFilename;
1118   if (options::TheOutputType == options::OT_SAVE_TEMPS) {
1119     OptFilename = output_name;
1120     // If the CodeGen client provided a filename, use it. Always expect
1121     // a provided filename if we are in a task (i.e. ThinLTO backend).
1122     assert(!SaveTempsFilename.empty() || TaskID == -1);
1123     if (!SaveTempsFilename.empty())
1124       OptFilename = SaveTempsFilename;
1125     OptFilename += ".opt.bc";
1126     saveBCFile(OptFilename, *M);
1127   }
1128 
1129   // If we are already in a thread (i.e. ThinLTO), just perform
1130   // codegen passes directly.
1131   if (TaskID >= 0)
1132     runCodegenPasses();
1133   // Otherwise attempt split code gen.
1134   else
1135     runSplitCodeGen(OptFilename);
1136 }
1137 
1138 /// Links the module in \p View from file \p F into the combined module
1139 /// saved in the IRMover \p L.
linkInModule(LLVMContext & Context,IRMover & L,claimed_file & F,const void * View,StringRef Name,raw_fd_ostream * ApiFile,StringSet<> & Internalize,bool SetName=false)1140 static void linkInModule(LLVMContext &Context, IRMover &L, claimed_file &F,
1141                          const void *View, StringRef Name,
1142                          raw_fd_ostream *ApiFile, StringSet<> &Internalize,
1143                          bool SetName = false) {
1144   std::vector<GlobalValue *> Keep;
1145   StringMap<unsigned> Realign;
1146   std::unique_ptr<Module> M = getModuleForFile(Context, F, View, Name, ApiFile,
1147                                                Internalize, Keep, Realign);
1148   if (!M.get())
1149     return;
1150   if (!options::triple.empty())
1151     M->setTargetTriple(options::triple.c_str());
1152   else if (M->getTargetTriple().empty()) {
1153     M->setTargetTriple(DefaultTriple);
1154   }
1155 
1156   // For ThinLTO we want to propagate the source file name to ensure
1157   // we can create the correct global identifiers matching those in the
1158   // original module.
1159   if (SetName)
1160     L.getModule().setSourceFileName(M->getSourceFileName());
1161 
1162   if (Error E = L.move(std::move(M), Keep,
1163                        [](GlobalValue &, IRMover::ValueAdder) {})) {
1164     handleAllErrors(std::move(E), [&](const llvm::ErrorInfoBase &EIB) {
1165       message(LDPL_FATAL, "Failed to link module %s: %s", Name.str().c_str(),
1166               EIB.message().c_str());
1167     });
1168   }
1169 
1170   for (const auto &I : Realign) {
1171     GlobalValue *Dst = L.getModule().getNamedValue(I.first());
1172     if (!Dst)
1173       continue;
1174     cast<GlobalVariable>(Dst)->setAlignment(I.second);
1175   }
1176 }
1177 
1178 /// Perform the ThinLTO backend on a single module, invoking the LTO and codegen
1179 /// pipelines.
thinLTOBackendTask(claimed_file & F,const void * View,StringRef Name,raw_fd_ostream * ApiFile,const ModuleSummaryIndex & CombinedIndex,raw_fd_ostream * OS,unsigned TaskID,StringMap<MemoryBufferRef> & ModuleMap,FunctionImporter::ImportMapTy & ImportList,std::map<GlobalValue::GUID,GlobalValueSummary * > & DefinedGlobals)1180 static void thinLTOBackendTask(claimed_file &F, const void *View,
1181                                StringRef Name, raw_fd_ostream *ApiFile,
1182                                const ModuleSummaryIndex &CombinedIndex,
1183                                raw_fd_ostream *OS, unsigned TaskID,
1184                                StringMap<MemoryBufferRef> &ModuleMap,
1185                                FunctionImporter::ImportMapTy &ImportList,
1186                                std::map<GlobalValue::GUID, GlobalValueSummary *> &DefinedGlobals) {
1187   // Need to use a separate context for each task
1188   LLVMContext Context;
1189   Context.setDiscardValueNames(options::TheOutputType !=
1190                                options::OT_SAVE_TEMPS);
1191   Context.enableDebugTypeODRUniquing(); // Merge debug info types.
1192   Context.setDiagnosticHandler(diagnosticHandlerForContext, nullptr, true);
1193 
1194   std::unique_ptr<llvm::Module> NewModule(new llvm::Module(Name, Context));
1195   IRMover L(*NewModule.get());
1196 
1197   StringSet<> Dummy;
1198   linkInModule(Context, L, F, View, Name, ApiFile, Dummy, true);
1199   if (renameModuleForThinLTO(*NewModule, CombinedIndex))
1200     message(LDPL_FATAL, "Failed to rename module for ThinLTO");
1201 
1202   CodeGen codeGen(std::move(NewModule), OS, TaskID, &CombinedIndex, Name,
1203                   &ModuleMap, &ImportList, &DefinedGlobals);
1204   codeGen.runAll();
1205 }
1206 
1207 /// Launch each module's backend pipeline in a separate task in a thread pool.
1208 static void
thinLTOBackends(raw_fd_ostream * ApiFile,const ModuleSummaryIndex & CombinedIndex,StringMap<MemoryBufferRef> & ModuleMap,StringMap<FunctionImporter::ImportMapTy> & ImportLists,StringMap<std::map<GlobalValue::GUID,GlobalValueSummary * >> & ModuleToDefinedGVSummaries)1209 thinLTOBackends(raw_fd_ostream *ApiFile,
1210                 const ModuleSummaryIndex &CombinedIndex,
1211                 StringMap<MemoryBufferRef> &ModuleMap,
1212                 StringMap<FunctionImporter::ImportMapTy> &ImportLists,
1213   StringMap<std::map<GlobalValue::GUID, GlobalValueSummary *>>
1214       &ModuleToDefinedGVSummaries) {
1215   unsigned TaskCount = 0;
1216   std::vector<ThinLTOTaskInfo> Tasks;
1217   Tasks.reserve(Modules.size());
1218   unsigned int MaxThreads = options::Parallelism
1219                                 ? options::Parallelism
1220                                 : thread::hardware_concurrency();
1221 
1222   // Create ThreadPool in nested scope so that threads will be joined
1223   // on destruction.
1224   {
1225     ThreadPool ThinLTOThreadPool(MaxThreads);
1226     for (claimed_file &F : Modules) {
1227       // Do all the gold callbacks in the main thread, since gold is not thread
1228       // safe by default.
1229       const void *View = getSymbolsAndView(F);
1230       if (!View)
1231         continue;
1232 
1233       SmallString<128> Filename;
1234       if (!options::obj_path.empty())
1235         // Note that openOutputFile will append a unique ID for each task
1236         Filename = options::obj_path;
1237       else if (options::TheOutputType == options::OT_SAVE_TEMPS) {
1238         // Use the input file name so that we get a unique and identifiable
1239         // output file for each ThinLTO backend task.
1240         Filename = F.name;
1241         Filename += ".thinlto.o";
1242       }
1243       bool TempOutFile = Filename.empty();
1244 
1245       SmallString<128> NewFilename;
1246       int FD = openOutputFile(Filename, TempOutFile, NewFilename,
1247                               // Only append the TaskID if we will use the
1248                               // non-unique obj_path.
1249                               !options::obj_path.empty() ? TaskCount : -1);
1250       TaskCount++;
1251       std::unique_ptr<raw_fd_ostream> OS =
1252           llvm::make_unique<raw_fd_ostream>(FD, true);
1253 
1254       // Enqueue the task
1255       ThinLTOThreadPool.async(thinLTOBackendTask, std::ref(F), View, F.name,
1256                               ApiFile, std::ref(CombinedIndex), OS.get(),
1257                               TaskCount, std::ref(ModuleMap),
1258                               std::ref(ImportLists[F.name]),
1259                               std::ref(ModuleToDefinedGVSummaries[F.name]));
1260 
1261       // Record the information needed by the task or during its cleanup
1262       // to a ThinLTOTaskInfo instance. For information needed by the task
1263       // the unique_ptr ownership is transferred to the ThinLTOTaskInfo.
1264       Tasks.emplace_back(std::move(OS), NewFilename.c_str(), TempOutFile);
1265     }
1266   }
1267 
1268   for (auto &Task : Tasks)
1269     Task.cleanup();
1270 }
1271 
1272 /// Parse the thinlto_prefix_replace option into the \p OldPrefix and
1273 /// \p NewPrefix strings, if it was specified.
getThinLTOOldAndNewPrefix(std::string & OldPrefix,std::string & NewPrefix)1274 static void getThinLTOOldAndNewPrefix(std::string &OldPrefix,
1275                                       std::string &NewPrefix) {
1276   StringRef PrefixReplace = options::thinlto_prefix_replace;
1277   assert(PrefixReplace.empty() || PrefixReplace.find(";") != StringRef::npos);
1278   std::pair<StringRef, StringRef> Split = PrefixReplace.split(";");
1279   OldPrefix = Split.first.str();
1280   NewPrefix = Split.second.str();
1281 }
1282 
1283 /// Given the original \p Path to an output file, replace any path
1284 /// prefix matching \p OldPrefix with \p NewPrefix. Also, create the
1285 /// resulting directory if it does not yet exist.
getThinLTOOutputFile(const std::string & Path,const std::string & OldPrefix,const std::string & NewPrefix)1286 static std::string getThinLTOOutputFile(const std::string &Path,
1287                                         const std::string &OldPrefix,
1288                                         const std::string &NewPrefix) {
1289   if (OldPrefix.empty() && NewPrefix.empty())
1290     return Path;
1291   SmallString<128> NewPath(Path);
1292   llvm::sys::path::replace_path_prefix(NewPath, OldPrefix, NewPrefix);
1293   StringRef ParentPath = llvm::sys::path::parent_path(NewPath.str());
1294   if (!ParentPath.empty()) {
1295     // Make sure the new directory exists, creating it if necessary.
1296     if (std::error_code EC = llvm::sys::fs::create_directories(ParentPath))
1297       llvm::errs() << "warning: could not create directory '" << ParentPath
1298                    << "': " << EC.message() << '\n';
1299   }
1300   return NewPath.str();
1301 }
1302 
1303 /// Perform ThinLTO link, which creates the combined index file.
1304 /// Also, either launch backend threads or (under thinlto-index-only)
1305 /// emit individual index files for distributed backends and exit.
thinLTOLink(raw_fd_ostream * ApiFile)1306 static ld_plugin_status thinLTOLink(raw_fd_ostream *ApiFile) {
1307   // Map from a module name to the corresponding buffer holding a view of the
1308   // bitcode provided via the get_view gold callback.
1309   StringMap<MemoryBufferRef> ModuleMap;
1310   // Map to own RAII objects that manage the file opening and releasing
1311   // interfaces with gold.
1312   DenseMap<void *, std::unique_ptr<PluginInputFile>> HandleToInputFile;
1313 
1314   // Keep track of symbols that must not be internalized because they
1315   // are referenced outside of a single IR module.
1316   DenseSet<GlobalValue::GUID> Preserve;
1317 
1318   // Keep track of the prevailing copy for each GUID, for use in resolving
1319   // weak linkages.
1320   DenseMap<GlobalValue::GUID, const GlobalValueSummary *> PrevailingCopy;
1321 
1322   ModuleSummaryIndex CombinedIndex;
1323   uint64_t NextModuleId = 0;
1324   for (claimed_file &F : Modules) {
1325     if (!HandleToInputFile.count(F.leader_handle))
1326       HandleToInputFile.insert(std::make_pair(
1327           F.leader_handle, llvm::make_unique<PluginInputFile>(F.handle)));
1328     // Pass this into getModuleSummaryIndexForFile
1329     const void *View = getSymbolsAndView(F);
1330     if (!View)
1331       continue;
1332 
1333     MemoryBufferRef ModuleBuffer(StringRef((const char *)View, F.filesize),
1334                                  F.name);
1335     assert(ModuleMap.find(ModuleBuffer.getBufferIdentifier()) ==
1336                ModuleMap.end() &&
1337            "Expect unique Buffer Identifier");
1338     ModuleMap[ModuleBuffer.getBufferIdentifier()] = ModuleBuffer;
1339 
1340     std::unique_ptr<ModuleSummaryIndex> Index = getModuleSummaryIndexForFile(F);
1341 
1342     // Use gold's symbol resolution information to identify symbols referenced
1343     // by more than a single IR module (i.e. referenced by multiple IR modules
1344     // or by a non-IR module). Cross references introduced by importing are
1345     // checked separately via the export lists. Also track the prevailing copy
1346     // for later symbol resolution.
1347     for (auto &Sym : F.syms) {
1348       ld_plugin_symbol_resolution Resolution =
1349           (ld_plugin_symbol_resolution)Sym.resolution;
1350       GlobalValue::GUID SymGUID = GlobalValue::getGUID(Sym.name);
1351       if (Resolution != LDPR_PREVAILING_DEF_IRONLY)
1352         Preserve.insert(SymGUID);
1353 
1354       if (Index && (Resolution == LDPR_PREVAILING_DEF ||
1355                     Resolution == LDPR_PREVAILING_DEF_IRONLY ||
1356                     Resolution == LDPR_PREVAILING_DEF_IRONLY_EXP))
1357         PrevailingCopy[SymGUID] = Index->getGlobalValueSummary(SymGUID);
1358     }
1359 
1360     // Skip files without a module summary.
1361     if (Index)
1362       CombinedIndex.mergeFrom(std::move(Index), ++NextModuleId);
1363   }
1364 
1365   // Collect for each module the list of function it defines (GUID ->
1366   // Summary).
1367   StringMap<std::map<GlobalValue::GUID, GlobalValueSummary *>>
1368       ModuleToDefinedGVSummaries(NextModuleId);
1369   CombinedIndex.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
1370 
1371   StringMap<FunctionImporter::ImportMapTy> ImportLists(NextModuleId);
1372   StringMap<FunctionImporter::ExportSetTy> ExportLists(NextModuleId);
1373   ComputeCrossModuleImport(CombinedIndex, ModuleToDefinedGVSummaries,
1374                            ImportLists, ExportLists);
1375 
1376   auto isPrevailing = [&](GlobalValue::GUID GUID, const GlobalValueSummary *S) {
1377     const auto &Prevailing = PrevailingCopy.find(GUID);
1378     assert(Prevailing != PrevailingCopy.end());
1379     return Prevailing->second == S;
1380   };
1381 
1382   // Callback for internalization, to prevent internalization of symbols
1383   // that were not candidates initially, and those that are being imported
1384   // (which introduces new cross references).
1385   auto isExported = [&](StringRef ModuleIdentifier, GlobalValue::GUID GUID) {
1386     const auto &ExportList = ExportLists.find(ModuleIdentifier);
1387     return (ExportList != ExportLists.end() &&
1388             ExportList->second.count(GUID)) ||
1389            Preserve.count(GUID);
1390   };
1391 
1392   thinLTOResolveWeakForLinkerInIndex(
1393       CombinedIndex, isPrevailing,
1394       [](StringRef ModuleIdentifier, GlobalValue::GUID GUID,
1395          GlobalValue::LinkageTypes NewLinkage) {});
1396 
1397   // Use global summary-based analysis to identify symbols that can be
1398   // internalized (because they aren't exported or preserved as per callback).
1399   // Changes are made in the index, consumed in the ThinLTO backends.
1400   thinLTOInternalizeAndPromoteInIndex(CombinedIndex, isExported);
1401 
1402   if (options::thinlto_emit_imports_files && !options::thinlto_index_only)
1403     message(LDPL_WARNING,
1404             "thinlto-emit-imports-files ignored unless thinlto-index-only");
1405 
1406   if (options::thinlto_index_only) {
1407     // If the thinlto-prefix-replace option was specified, parse it and
1408     // extract the old and new prefixes.
1409     std::string OldPrefix, NewPrefix;
1410     getThinLTOOldAndNewPrefix(OldPrefix, NewPrefix);
1411 
1412     // For each input bitcode file, generate an individual index that
1413     // contains summaries only for its own global values, and for any that
1414     // should be imported.
1415     for (claimed_file &F : Modules) {
1416       std::error_code EC;
1417 
1418       std::string NewModulePath =
1419           getThinLTOOutputFile(F.name, OldPrefix, NewPrefix);
1420       raw_fd_ostream OS((Twine(NewModulePath) + ".thinlto.bc").str(), EC,
1421                         sys::fs::OpenFlags::F_None);
1422       if (EC)
1423         message(LDPL_FATAL, "Unable to open %s.thinlto.bc for writing: %s",
1424                 NewModulePath.c_str(), EC.message().c_str());
1425       // Build a map of module to the GUIDs and summary objects that should
1426       // be written to its index.
1427       std::map<std::string, GVSummaryMapTy> ModuleToSummariesForIndex;
1428       gatherImportedSummariesForModule(F.name, ModuleToDefinedGVSummaries,
1429                                        ImportLists, ModuleToSummariesForIndex);
1430       WriteIndexToFile(CombinedIndex, OS, &ModuleToSummariesForIndex);
1431 
1432       if (options::thinlto_emit_imports_files) {
1433         if ((EC = EmitImportsFiles(F.name,
1434                                    (Twine(NewModulePath) + ".imports").str(),
1435                                    ImportLists)))
1436           message(LDPL_FATAL, "Unable to open %s.imports",
1437                   NewModulePath.c_str(), EC.message().c_str());
1438       }
1439     }
1440 
1441     cleanup_hook();
1442     exit(0);
1443   }
1444 
1445   // Create OS in nested scope so that it will be closed on destruction.
1446   {
1447     std::error_code EC;
1448     raw_fd_ostream OS(output_name + ".thinlto.bc", EC,
1449                       sys::fs::OpenFlags::F_None);
1450     if (EC)
1451       message(LDPL_FATAL, "Unable to open %s.thinlto.bc for writing: %s",
1452               output_name.data(), EC.message().c_str());
1453     WriteIndexToFile(CombinedIndex, OS);
1454   }
1455 
1456   thinLTOBackends(ApiFile, CombinedIndex, ModuleMap, ImportLists,
1457                   ModuleToDefinedGVSummaries);
1458   return LDPS_OK;
1459 }
1460 
1461 /// gold informs us that all symbols have been read. At this point, we use
1462 /// get_symbols to see if any of our definitions have been overridden by a
1463 /// native object file. Then, perform optimization and codegen.
allSymbolsReadHook(raw_fd_ostream * ApiFile)1464 static ld_plugin_status allSymbolsReadHook(raw_fd_ostream *ApiFile) {
1465   if (Modules.empty())
1466     return LDPS_OK;
1467 
1468   if (unsigned NumOpts = options::extra.size())
1469     cl::ParseCommandLineOptions(NumOpts, &options::extra[0]);
1470 
1471   if (options::thinlto)
1472     return thinLTOLink(ApiFile);
1473 
1474   LLVMContext Context;
1475   Context.setDiscardValueNames(options::TheOutputType !=
1476                                options::OT_SAVE_TEMPS);
1477   Context.enableDebugTypeODRUniquing(); // Merge debug info types.
1478   Context.setDiagnosticHandler(diagnosticHandlerForContext, nullptr, true);
1479 
1480   std::unique_ptr<Module> Combined(new Module("ld-temp.o", Context));
1481   IRMover L(*Combined);
1482 
1483   StringSet<> Internalize;
1484   for (claimed_file &F : Modules) {
1485     // RAII object to manage the file opening and releasing interfaces with
1486     // gold.
1487     PluginInputFile InputFile(F.handle);
1488     const void *View = getSymbolsAndView(F);
1489     if (!View)
1490       continue;
1491     linkInModule(Context, L, F, View, F.name, ApiFile, Internalize);
1492   }
1493 
1494   for (const auto &Name : Internalize) {
1495     GlobalValue *GV = Combined->getNamedValue(Name.first());
1496     if (GV)
1497       internalize(*GV);
1498   }
1499 
1500   if (options::TheOutputType == options::OT_DISABLE)
1501     return LDPS_OK;
1502 
1503   if (options::TheOutputType != options::OT_NORMAL) {
1504     std::string path;
1505     if (options::TheOutputType == options::OT_BC_ONLY)
1506       path = output_name;
1507     else
1508       path = output_name + ".bc";
1509     saveBCFile(path, *Combined);
1510     if (options::TheOutputType == options::OT_BC_ONLY)
1511       return LDPS_OK;
1512   }
1513 
1514   CodeGen codeGen(std::move(Combined));
1515   codeGen.runAll();
1516 
1517   if (!options::extra_library_path.empty() &&
1518       set_extra_library_path(options::extra_library_path.c_str()) != LDPS_OK)
1519     message(LDPL_FATAL, "Unable to set the extra library path.");
1520 
1521   return LDPS_OK;
1522 }
1523 
all_symbols_read_hook(void)1524 static ld_plugin_status all_symbols_read_hook(void) {
1525   ld_plugin_status Ret;
1526   if (!options::generate_api_file) {
1527     Ret = allSymbolsReadHook(nullptr);
1528   } else {
1529     std::error_code EC;
1530     raw_fd_ostream ApiFile("apifile.txt", EC, sys::fs::F_None);
1531     if (EC)
1532       message(LDPL_FATAL, "Unable to open apifile.txt for writing: %s",
1533               EC.message().c_str());
1534     Ret = allSymbolsReadHook(&ApiFile);
1535   }
1536 
1537   llvm_shutdown();
1538 
1539   if (options::TheOutputType == options::OT_BC_ONLY ||
1540       options::TheOutputType == options::OT_DISABLE) {
1541     if (options::TheOutputType == options::OT_DISABLE) {
1542       // Remove the output file here since ld.bfd creates the output file
1543       // early.
1544       std::error_code EC = sys::fs::remove(output_name);
1545       if (EC)
1546         message(LDPL_ERROR, "Failed to delete '%s': %s", output_name.c_str(),
1547                 EC.message().c_str());
1548     }
1549     exit(0);
1550   }
1551 
1552   return Ret;
1553 }
1554 
cleanup_hook(void)1555 static ld_plugin_status cleanup_hook(void) {
1556   for (std::string &Name : Cleanup) {
1557     std::error_code EC = sys::fs::remove(Name);
1558     if (EC)
1559       message(LDPL_ERROR, "Failed to delete '%s': %s", Name.c_str(),
1560               EC.message().c_str());
1561   }
1562 
1563   return LDPS_OK;
1564 }
1565