blob: b70c072f9bfb46cc44659392a11e110c231f4f9e [file] [log] [blame]
Nick Lewyckyfb643e42009-02-03 07:13:24 +00001//===-- 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
Dylan Noblesmith9e5b1782011-12-22 23:04:07 +000015#include "llvm/Config/config.h" // plugin-api.h requires HAVE_STDINT_H
Rafael Espindolaf7ecb112014-08-22 23:26:10 +000016#include "llvm/ADT/DenseSet.h"
Chandler Carruth07baed52014-01-13 08:04:33 +000017#include "llvm/ADT/StringSet.h"
Chandler Carruth62d42152015-01-15 02:16:27 +000018#include "llvm/Analysis/TargetLibraryInfo.h"
NAKAMURA Takumib0a52832015-02-02 05:47:30 +000019#include "llvm/Analysis/TargetTransformInfo.h"
Rafael Espindola33466a72014-08-21 20:28:55 +000020#include "llvm/Bitcode/ReaderWriter.h"
21#include "llvm/CodeGen/Analysis.h"
Rafael Espindola6b244b12014-06-19 21:14:13 +000022#include "llvm/CodeGen/CommandFlags.h"
Peter Collingbourne87202a42015-09-01 20:40:22 +000023#include "llvm/CodeGen/ParallelCG.h"
Rafael Espindola0d68b4c2015-03-30 21:36:43 +000024#include "llvm/IR/AutoUpgrade.h"
Rafael Espindola890db272014-09-09 20:08:22 +000025#include "llvm/IR/Constants.h"
Rafael Espindolad0b23be2015-01-10 00:07:30 +000026#include "llvm/IR/DiagnosticInfo.h"
27#include "llvm/IR/DiagnosticPrinter.h"
Rafael Espindola33466a72014-08-21 20:28:55 +000028#include "llvm/IR/LLVMContext.h"
Chandler Carruth30d69c22015-02-13 10:01:29 +000029#include "llvm/IR/LegacyPassManager.h"
Rafael Espindola33466a72014-08-21 20:28:55 +000030#include "llvm/IR/Module.h"
31#include "llvm/IR/Verifier.h"
Rafael Espindolacaabe222015-12-10 14:19:35 +000032#include "llvm/Linker/IRMover.h"
Rafael Espindola33466a72014-08-21 20:28:55 +000033#include "llvm/MC/SubtargetFeature.h"
Teresa Johnson26ab5772016-03-15 00:04:37 +000034#include "llvm/Object/ModuleSummaryIndexObjectFile.h"
Teresa Johnsonb13dbd62015-12-09 19:45:55 +000035#include "llvm/Object/IRObjectFile.h"
Rafael Espindola33466a72014-08-21 20:28:55 +000036#include "llvm/Support/Host.h"
Rafael Espindola947bdb62014-11-25 20:52:49 +000037#include "llvm/Support/ManagedStatic.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000038#include "llvm/Support/MemoryBuffer.h"
Rafael Espindola33466a72014-08-21 20:28:55 +000039#include "llvm/Support/TargetRegistry.h"
Rafael Espindola6b244b12014-06-19 21:14:13 +000040#include "llvm/Support/TargetSelect.h"
Teresa Johnson7cffaf32016-03-04 17:06:02 +000041#include "llvm/Support/ThreadPool.h"
Teresa Johnsonb13dbd62015-12-09 19:45:55 +000042#include "llvm/Support/raw_ostream.h"
Teresa Johnson7cffaf32016-03-04 17:06:02 +000043#include "llvm/Support/thread.h"
Rafael Espindola33466a72014-08-21 20:28:55 +000044#include "llvm/Transforms/IPO.h"
45#include "llvm/Transforms/IPO/PassManagerBuilder.h"
Teresa Johnson7cffaf32016-03-04 17:06:02 +000046#include "llvm/Transforms/Utils/FunctionImportUtils.h"
Rafael Espindola33466a72014-08-21 20:28:55 +000047#include "llvm/Transforms/Utils/GlobalStatus.h"
48#include "llvm/Transforms/Utils/ModuleUtils.h"
Rafael Espindolaf7ecb112014-08-22 23:26:10 +000049#include "llvm/Transforms/Utils/ValueMapper.h"
Nick Lewyckyfb643e42009-02-03 07:13:24 +000050#include <list>
Chandler Carruth07baed52014-01-13 08:04:33 +000051#include <plugin-api.h>
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000052#include <system_error>
Nick Lewyckyfb643e42009-02-03 07:13:24 +000053#include <vector>
54
Sylvestre Ledru53999792014-02-11 17:30:18 +000055// FIXME: remove this declaration when we stop maintaining Ubuntu Quantal and
56// Precise and Debian Wheezy (binutils 2.23 is required)
Evgeniy Stepanov330c5a62016-03-04 00:23:29 +000057#define LDPO_PIE 3
58
59#define LDPT_GET_SYMBOLS_V3 28
Sylvestre Ledru53999792014-02-11 17:30:18 +000060
Nick Lewyckyfb643e42009-02-03 07:13:24 +000061using namespace llvm;
62
Teresa Johnsoncb15b732015-12-16 16:34:06 +000063static ld_plugin_status discard_message(int level, const char *format, ...) {
64 // Die loudly. Recent versions of Gold pass ld_plugin_message as the first
65 // callback in the transfer vector. This should never be called.
66 abort();
67}
68
69static ld_plugin_release_input_file release_input_file = nullptr;
70static ld_plugin_get_input_file get_input_file = nullptr;
71static ld_plugin_message message = discard_message;
72
Nick Lewyckyfb643e42009-02-03 07:13:24 +000073namespace {
Rafael Espindolabfb8b912014-06-20 01:37:35 +000074struct claimed_file {
75 void *handle;
76 std::vector<ld_plugin_symbol> syms;
77};
Rafael Espindolacaabe222015-12-10 14:19:35 +000078
Teresa Johnsoncb15b732015-12-16 16:34:06 +000079/// RAII wrapper to manage opening and releasing of a ld_plugin_input_file.
80struct PluginInputFile {
Teresa Johnson031bed22015-12-16 21:37:48 +000081 void *Handle;
Teresa Johnson7cffaf32016-03-04 17:06:02 +000082 std::unique_ptr<ld_plugin_input_file> File;
Teresa Johnsoncb15b732015-12-16 16:34:06 +000083
Teresa Johnson031bed22015-12-16 21:37:48 +000084 PluginInputFile(void *Handle) : Handle(Handle) {
Teresa Johnson7cffaf32016-03-04 17:06:02 +000085 File = llvm::make_unique<ld_plugin_input_file>();
86 if (get_input_file(Handle, File.get()) != LDPS_OK)
Teresa Johnsoncb15b732015-12-16 16:34:06 +000087 message(LDPL_FATAL, "Failed to get file information");
88 }
89 ~PluginInputFile() {
Teresa Johnson7cffaf32016-03-04 17:06:02 +000090 // File would have been reset to nullptr if we moved this object
91 // to a new owner.
92 if (File)
93 if (release_input_file(Handle) != LDPS_OK)
94 message(LDPL_FATAL, "Failed to release file information");
Teresa Johnsoncb15b732015-12-16 16:34:06 +000095 }
Teresa Johnson7cffaf32016-03-04 17:06:02 +000096
97 ld_plugin_input_file &file() { return *File; }
98
99 PluginInputFile(PluginInputFile &&RHS) = default;
100 PluginInputFile &operator=(PluginInputFile &&RHS) = default;
Teresa Johnsoncb15b732015-12-16 16:34:06 +0000101};
102
Rafael Espindolacaabe222015-12-10 14:19:35 +0000103struct ResolutionInfo {
Evgeniy Stepanov4dc3c8d2016-03-11 00:51:57 +0000104 uint64_t CommonSize = 0;
105 unsigned CommonAlign = 0;
Rafael Espindolacaabe222015-12-10 14:19:35 +0000106 bool IsLinkonceOdr = true;
107 bool UnnamedAddr = true;
108 GlobalValue::VisibilityTypes Visibility = GlobalValue::DefaultVisibility;
109 bool CommonInternal = false;
110 bool UseCommon = false;
Rafael Espindolacaabe222015-12-10 14:19:35 +0000111};
Teresa Johnson7cffaf32016-03-04 17:06:02 +0000112
113/// Class to own information used by a task or during its cleanup for a
114/// ThinLTO backend instantiation.
115class ThinLTOTaskInfo {
116 /// The input file holding the module bitcode read by the ThinLTO task.
117 PluginInputFile InputFile;
118
119 /// The output stream the task will codegen into.
120 std::unique_ptr<raw_fd_ostream> OS;
121
122 /// The file name corresponding to the output stream, used during cleanup.
123 std::string Filename;
124
125 /// Flag indicating whether the output file is a temp file that must be
126 /// added to the cleanup list during cleanup.
127 bool TempOutFile;
128
129public:
130 ThinLTOTaskInfo(PluginInputFile InputFile, std::unique_ptr<raw_fd_ostream> OS,
131 std::string Filename, bool TempOutFile)
132 : InputFile(std::move(InputFile)), OS(std::move(OS)), Filename(Filename),
133 TempOutFile(TempOutFile) {}
134
135 /// Performs task related cleanup activities that must be done
136 /// single-threaded (i.e. call backs to gold).
137 void cleanup();
138};
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000139}
Rafael Espindolabfb8b912014-06-20 01:37:35 +0000140
Rafael Espindola176e6642014-07-29 21:46:05 +0000141static ld_plugin_add_symbols add_symbols = nullptr;
142static ld_plugin_get_symbols get_symbols = nullptr;
143static ld_plugin_add_input_file add_input_file = nullptr;
144static ld_plugin_set_extra_library_path set_extra_library_path = nullptr;
145static ld_plugin_get_view get_view = nullptr;
Rafael Espindola33466a72014-08-21 20:28:55 +0000146static Reloc::Model RelocationModel = Reloc::Default;
Rafael Espindolabfb8b912014-06-20 01:37:35 +0000147static std::string output_name = "";
148static std::list<claimed_file> Modules;
Rafael Espindolacaabe222015-12-10 14:19:35 +0000149static StringMap<ResolutionInfo> ResInfo;
Rafael Espindolabfb8b912014-06-20 01:37:35 +0000150static std::vector<std::string> Cleanup;
Rafael Espindola6b244b12014-06-19 21:14:13 +0000151static llvm::TargetOptions TargetOpts;
Teresa Johnsona9f65552016-03-04 16:36:06 +0000152static std::string DefaultTriple = sys::getDefaultTargetTriple();
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000153
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +0000154namespace options {
Rafael Espindola6953a3a2014-11-24 21:18:14 +0000155 enum OutputType {
156 OT_NORMAL,
157 OT_DISABLE,
158 OT_BC_ONLY,
159 OT_SAVE_TEMPS
160 };
Dan Gohmanebb4ae02010-04-16 00:42:57 +0000161 static bool generate_api_file = false;
Rafael Espindola6953a3a2014-11-24 21:18:14 +0000162 static OutputType TheOutputType = OT_NORMAL;
Peter Collingbourne070843d2015-03-19 22:01:00 +0000163 static unsigned OptLevel = 2;
Teresa Johnsona9f65552016-03-04 16:36:06 +0000164 // Default parallelism of 0 used to indicate that user did not specify.
165 // Actual parallelism default value depends on implementation.
Teresa Johnson7cffaf32016-03-04 17:06:02 +0000166 // Currently, code generation defaults to no parallelism, whereas
167 // ThinLTO uses the hardware_concurrency as the default.
Teresa Johnsona9f65552016-03-04 16:36:06 +0000168 static unsigned Parallelism = 0;
Teresa Johnson8c8fe5a2015-09-16 18:06:45 +0000169#ifdef NDEBUG
170 static bool DisableVerify = true;
171#else
172 static bool DisableVerify = false;
173#endif
Shuxin Yang1826ae22013-08-12 21:07:31 +0000174 static std::string obj_path;
Rafael Espindolaef498152010-06-23 20:20:59 +0000175 static std::string extra_library_path;
Rafael Espindola4ef89f52010-08-09 21:09:46 +0000176 static std::string triple;
Rafael Espindolaccab1dd2010-08-11 00:15:13 +0000177 static std::string mcpu;
Teresa Johnson403a7872015-10-04 14:33:43 +0000178 // When the thinlto plugin option is specified, only read the function
179 // the information from intermediate files and write a combined
180 // global index for the ThinLTO backends.
181 static bool thinlto = false;
Teresa Johnson7cffaf32016-03-04 17:06:02 +0000182 // If false, all ThinLTO backend compilations through code gen are performed
183 // using multiple threads in the gold-plugin, before handing control back to
184 // gold. If true, exit after creating the combined index, the assuming is
185 // that the build system will launch the backend processes.
186 static bool thinlto_index_only = false;
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +0000187 // Additional options to pass into the code generator.
Nick Lewycky0ac5e222010-06-03 17:10:17 +0000188 // Note: This array will contain all plugin options which are not claimed
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +0000189 // as plugin exclusive to pass to the code generator.
Nick Lewycky0ac5e222010-06-03 17:10:17 +0000190 // For example, "generate-api-file" and "as"options are for the plugin
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +0000191 // use only and will not be passed.
Rafael Espindola125b9242014-07-29 19:17:44 +0000192 static std::vector<const char *> extra;
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +0000193
Nick Lewycky7282dd72015-08-05 21:16:02 +0000194 static void process_plugin_option(const char *opt_)
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +0000195 {
Rafael Espindola176e6642014-07-29 21:46:05 +0000196 if (opt_ == nullptr)
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +0000197 return;
Rafael Espindolac4dca3a2010-06-07 16:45:22 +0000198 llvm::StringRef opt = opt_;
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +0000199
Rafael Espindolac4dca3a2010-06-07 16:45:22 +0000200 if (opt == "generate-api-file") {
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +0000201 generate_api_file = true;
Rafael Espindolaccab1dd2010-08-11 00:15:13 +0000202 } else if (opt.startswith("mcpu=")) {
203 mcpu = opt.substr(strlen("mcpu="));
Rafael Espindolaef498152010-06-23 20:20:59 +0000204 } else if (opt.startswith("extra-library-path=")) {
205 extra_library_path = opt.substr(strlen("extra_library_path="));
Rafael Espindola148c3282010-08-10 16:32:15 +0000206 } else if (opt.startswith("mtriple=")) {
Rafael Espindola4ef89f52010-08-09 21:09:46 +0000207 triple = opt.substr(strlen("mtriple="));
Shuxin Yang1826ae22013-08-12 21:07:31 +0000208 } else if (opt.startswith("obj-path=")) {
209 obj_path = opt.substr(strlen("obj-path="));
Rafael Espindolac4dca3a2010-06-07 16:45:22 +0000210 } else if (opt == "emit-llvm") {
Rafael Espindola6953a3a2014-11-24 21:18:14 +0000211 TheOutputType = OT_BC_ONLY;
Rafael Espindola4a3b6cf2014-10-29 23:54:45 +0000212 } else if (opt == "save-temps") {
Rafael Espindola6953a3a2014-11-24 21:18:14 +0000213 TheOutputType = OT_SAVE_TEMPS;
214 } else if (opt == "disable-output") {
215 TheOutputType = OT_DISABLE;
Teresa Johnson403a7872015-10-04 14:33:43 +0000216 } else if (opt == "thinlto") {
217 thinlto = true;
Teresa Johnson7cffaf32016-03-04 17:06:02 +0000218 } else if (opt == "thinlto-index-only") {
219 thinlto_index_only = true;
Peter Collingbourne070843d2015-03-19 22:01:00 +0000220 } else if (opt.size() == 2 && opt[0] == 'O') {
221 if (opt[1] < '0' || opt[1] > '3')
Peter Collingbourne87202a42015-09-01 20:40:22 +0000222 message(LDPL_FATAL, "Optimization level must be between 0 and 3");
Peter Collingbourne070843d2015-03-19 22:01:00 +0000223 OptLevel = opt[1] - '0';
Peter Collingbourne87202a42015-09-01 20:40:22 +0000224 } else if (opt.startswith("jobs=")) {
225 if (StringRef(opt_ + 5).getAsInteger(10, Parallelism))
226 message(LDPL_FATAL, "Invalid parallelism level: %s", opt_ + 5);
Teresa Johnson8c8fe5a2015-09-16 18:06:45 +0000227 } else if (opt == "disable-verify") {
228 DisableVerify = true;
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +0000229 } else {
230 // Save this option to pass to the code generator.
Rafael Espindola33466a72014-08-21 20:28:55 +0000231 // ParseCommandLineOptions() expects argv[0] to be program name. Lazily
232 // add that.
233 if (extra.empty())
234 extra.push_back("LLVMgold");
235
Rafael Espindola125b9242014-07-29 19:17:44 +0000236 extra.push_back(opt_);
Viktor Kutuzovfd7ddd92009-10-28 18:55:55 +0000237 }
238 }
239}
240
Dan Gohmanebb4ae02010-04-16 00:42:57 +0000241static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
242 int *claimed);
243static ld_plugin_status all_symbols_read_hook(void);
244static ld_plugin_status cleanup_hook(void);
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000245
246extern "C" ld_plugin_status onload(ld_plugin_tv *tv);
247ld_plugin_status onload(ld_plugin_tv *tv) {
Peter Collingbourne1505c0a2014-07-03 23:28:03 +0000248 InitializeAllTargetInfos();
249 InitializeAllTargets();
250 InitializeAllTargetMCs();
251 InitializeAllAsmParsers();
252 InitializeAllAsmPrinters();
253
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000254 // We're given a pointer to the first transfer vector. We read through them
255 // until we find one where tv_tag == LDPT_NULL. The REGISTER_* tagged values
256 // contain pointers to functions that we need to call to register our own
257 // hooks. The others are addresses of functions we can use to call into gold
258 // for services.
259
260 bool registeredClaimFile = false;
Rafael Espindola6b244b12014-06-19 21:14:13 +0000261 bool RegisteredAllSymbolsRead = false;
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000262
263 for (; tv->tv_tag != LDPT_NULL; ++tv) {
Evgeniy Stepanov330c5a62016-03-04 00:23:29 +0000264 // Cast tv_tag to int to allow values not in "enum ld_plugin_tag", like, for
265 // example, LDPT_GET_SYMBOLS_V3 when building against an older plugin-api.h
266 // header.
267 switch (static_cast<int>(tv->tv_tag)) {
268 case LDPT_OUTPUT_NAME:
269 output_name = tv->tv_u.tv_string;
270 break;
271 case LDPT_LINKER_OUTPUT:
272 switch (tv->tv_u.tv_val) {
273 case LDPO_REL: // .o
274 case LDPO_DYN: // .so
275 case LDPO_PIE: // position independent executable
276 RelocationModel = Reloc::PIC_;
Rafael Espindola8fb957e2010-06-03 21:11:20 +0000277 break;
Evgeniy Stepanov330c5a62016-03-04 00:23:29 +0000278 case LDPO_EXEC: // .exe
279 RelocationModel = Reloc::Static;
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000280 break;
281 default:
Evgeniy Stepanov330c5a62016-03-04 00:23:29 +0000282 message(LDPL_ERROR, "Unknown output file type %d", tv->tv_u.tv_val);
283 return LDPS_ERR;
284 }
285 break;
286 case LDPT_OPTION:
287 options::process_plugin_option(tv->tv_u.tv_string);
288 break;
289 case LDPT_REGISTER_CLAIM_FILE_HOOK: {
290 ld_plugin_register_claim_file callback;
291 callback = tv->tv_u.tv_register_claim_file;
292
293 if (callback(claim_file_hook) != LDPS_OK)
294 return LDPS_ERR;
295
296 registeredClaimFile = true;
297 } break;
298 case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK: {
299 ld_plugin_register_all_symbols_read callback;
300 callback = tv->tv_u.tv_register_all_symbols_read;
301
302 if (callback(all_symbols_read_hook) != LDPS_OK)
303 return LDPS_ERR;
304
305 RegisteredAllSymbolsRead = true;
306 } break;
307 case LDPT_REGISTER_CLEANUP_HOOK: {
308 ld_plugin_register_cleanup callback;
309 callback = tv->tv_u.tv_register_cleanup;
310
311 if (callback(cleanup_hook) != LDPS_OK)
312 return LDPS_ERR;
313 } break;
314 case LDPT_GET_INPUT_FILE:
315 get_input_file = tv->tv_u.tv_get_input_file;
316 break;
317 case LDPT_RELEASE_INPUT_FILE:
318 release_input_file = tv->tv_u.tv_release_input_file;
319 break;
320 case LDPT_ADD_SYMBOLS:
321 add_symbols = tv->tv_u.tv_add_symbols;
322 break;
323 case LDPT_GET_SYMBOLS_V2:
324 // Do not override get_symbols_v3 with get_symbols_v2.
325 if (!get_symbols)
326 get_symbols = tv->tv_u.tv_get_symbols;
327 break;
328 case LDPT_GET_SYMBOLS_V3:
329 get_symbols = tv->tv_u.tv_get_symbols;
330 break;
331 case LDPT_ADD_INPUT_FILE:
332 add_input_file = tv->tv_u.tv_add_input_file;
333 break;
334 case LDPT_SET_EXTRA_LIBRARY_PATH:
335 set_extra_library_path = tv->tv_u.tv_set_extra_library_path;
336 break;
337 case LDPT_GET_VIEW:
338 get_view = tv->tv_u.tv_get_view;
339 break;
340 case LDPT_MESSAGE:
341 message = tv->tv_u.tv_message;
342 break;
343 default:
344 break;
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000345 }
346 }
347
Rafael Espindolae08484d2009-02-18 08:30:15 +0000348 if (!registeredClaimFile) {
Rafael Espindola5ad21fa2014-07-30 00:38:58 +0000349 message(LDPL_ERROR, "register_claim_file not passed to LLVMgold.");
Rafael Espindola6add6182009-02-18 17:49:06 +0000350 return LDPS_ERR;
351 }
Rafael Espindolae08484d2009-02-18 08:30:15 +0000352 if (!add_symbols) {
Rafael Espindola5ad21fa2014-07-30 00:38:58 +0000353 message(LDPL_ERROR, "add_symbols not passed to LLVMgold.");
Rafael Espindola6add6182009-02-18 17:49:06 +0000354 return LDPS_ERR;
355 }
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000356
Rafael Espindolaa0d30a92014-06-19 22:20:07 +0000357 if (!RegisteredAllSymbolsRead)
358 return LDPS_OK;
Rafael Espindola6b244b12014-06-19 21:14:13 +0000359
Rafael Espindola33466a72014-08-21 20:28:55 +0000360 if (!get_input_file) {
361 message(LDPL_ERROR, "get_input_file not passed to LLVMgold.");
362 return LDPS_ERR;
Rafael Espindolac273aac2014-06-19 22:54:47 +0000363 }
Rafael Espindola33466a72014-08-21 20:28:55 +0000364 if (!release_input_file) {
365 message(LDPL_ERROR, "relesase_input_file not passed to LLVMgold.");
366 return LDPS_ERR;
Tom Roederb5081192014-06-26 20:43:27 +0000367 }
368
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000369 return LDPS_OK;
370}
371
Rafael Espindolaf7ecb112014-08-22 23:26:10 +0000372static const GlobalObject *getBaseObject(const GlobalValue &GV) {
373 if (auto *GA = dyn_cast<GlobalAlias>(&GV))
374 return GA->getBaseObject();
375 return cast<GlobalObject>(&GV);
376}
377
Rafael Espindola527e8462014-12-09 16:13:59 +0000378static bool shouldSkip(uint32_t Symflags) {
379 if (!(Symflags & object::BasicSymbolRef::SF_Global))
380 return true;
381 if (Symflags & object::BasicSymbolRef::SF_FormatSpecific)
382 return true;
383 return false;
384}
385
NAKAMURA Takumib13e63c2015-11-19 10:43:44 +0000386static void diagnosticHandler(const DiagnosticInfo &DI) {
Rafael Espindola503f8832015-03-02 19:08:03 +0000387 if (const auto *BDI = dyn_cast<BitcodeDiagnosticInfo>(&DI)) {
388 std::error_code EC = BDI->getError();
389 if (EC == BitcodeError::InvalidBitcodeSignature)
390 return;
391 }
Rafael Espindolad0b23be2015-01-10 00:07:30 +0000392
393 std::string ErrStorage;
394 {
395 raw_string_ostream OS(ErrStorage);
396 DiagnosticPrinterRawOStream DP(OS);
397 DI.print(DP);
398 }
Rafael Espindola503f8832015-03-02 19:08:03 +0000399 ld_plugin_level Level;
400 switch (DI.getSeverity()) {
401 case DS_Error:
402 message(LDPL_FATAL, "LLVM gold plugin has failed to create LTO module: %s",
403 ErrStorage.c_str());
Rafael Espindola503f8832015-03-02 19:08:03 +0000404 case DS_Warning:
405 Level = LDPL_WARNING;
406 break;
407 case DS_Note:
Rafael Espindolaf3f18542015-03-04 18:51:45 +0000408 case DS_Remark:
Rafael Espindola503f8832015-03-02 19:08:03 +0000409 Level = LDPL_INFO;
410 break;
Rafael Espindola503f8832015-03-02 19:08:03 +0000411 }
412 message(Level, "LLVM gold plugin: %s", ErrStorage.c_str());
Rafael Espindolad0b23be2015-01-10 00:07:30 +0000413}
414
NAKAMURA Takumib13e63c2015-11-19 10:43:44 +0000415static void diagnosticHandlerForContext(const DiagnosticInfo &DI,
416 void *Context) {
417 diagnosticHandler(DI);
418}
419
Rafael Espindolacaabe222015-12-10 14:19:35 +0000420static GlobalValue::VisibilityTypes
421getMinVisibility(GlobalValue::VisibilityTypes A,
422 GlobalValue::VisibilityTypes B) {
423 if (A == GlobalValue::HiddenVisibility)
424 return A;
425 if (B == GlobalValue::HiddenVisibility)
426 return B;
427 if (A == GlobalValue::ProtectedVisibility)
428 return A;
429 return B;
430}
431
Rafael Espindolae54d8212014-07-06 14:31:22 +0000432/// Called by gold to see whether this file is one that our plugin can handle.
433/// We'll try to open it and register all the symbols with add_symbol if
434/// possible.
Dan Gohmanebb4ae02010-04-16 00:42:57 +0000435static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
436 int *claimed) {
Rafael Espindola33466a72014-08-21 20:28:55 +0000437 LLVMContext Context;
Rafael Espindolaeeec8e62014-08-27 20:25:55 +0000438 MemoryBufferRef BufferRef;
439 std::unique_ptr<MemoryBuffer> Buffer;
Rafael Espindolaece7c9c2011-04-07 21:11:00 +0000440 if (get_view) {
Rafael Espindola33466a72014-08-21 20:28:55 +0000441 const void *view;
Rafael Espindolaece7c9c2011-04-07 21:11:00 +0000442 if (get_view(file->handle, &view) != LDPS_OK) {
Rafael Espindola5ad21fa2014-07-30 00:38:58 +0000443 message(LDPL_ERROR, "Failed to get a view of %s", file->name);
Rafael Espindolaece7c9c2011-04-07 21:11:00 +0000444 return LDPS_ERR;
445 }
Nick Lewycky7282dd72015-08-05 21:16:02 +0000446 BufferRef =
447 MemoryBufferRef(StringRef((const char *)view, file->filesize), "");
Ivan Krasin5021af52011-09-12 21:47:50 +0000448 } else {
Ivan Krasin639222d2011-09-15 23:13:00 +0000449 int64_t offset = 0;
Nick Lewycky8691c472009-02-05 04:14:23 +0000450 // Gold has found what might be IR part-way inside of a file, such as
451 // an .a archive.
Ivan Krasin5021af52011-09-12 21:47:50 +0000452 if (file->offset) {
453 offset = file->offset;
454 }
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000455 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
456 MemoryBuffer::getOpenFileSlice(file->fd, file->name, file->filesize,
457 offset);
458 if (std::error_code EC = BufferOrErr.getError()) {
Rafael Espindola5ad21fa2014-07-30 00:38:58 +0000459 message(LDPL_ERROR, EC.message().c_str());
Ivan Krasin5021af52011-09-12 21:47:50 +0000460 return LDPS_ERR;
461 }
Rafael Espindolaeeec8e62014-08-27 20:25:55 +0000462 Buffer = std::move(BufferOrErr.get());
463 BufferRef = Buffer->getMemBufferRef();
Rafael Espindola56e41f72011-02-08 22:40:47 +0000464 }
Ivan Krasin5021af52011-09-12 21:47:50 +0000465
NAKAMURA Takumib13e63c2015-11-19 10:43:44 +0000466 Context.setDiagnosticHandler(diagnosticHandlerForContext);
David Blaikie10a27df2014-09-03 17:59:23 +0000467 ErrorOr<std::unique_ptr<object::IRObjectFile>> ObjOrErr =
Rafael Espindola5dec7ea2014-12-09 20:36:13 +0000468 object::IRObjectFile::create(BufferRef, Context);
Rafael Espindola33466a72014-08-21 20:28:55 +0000469 std::error_code EC = ObjOrErr.getError();
Rafael Espindolad0b23be2015-01-10 00:07:30 +0000470 if (EC == object::object_error::invalid_file_type ||
Peter Collingbourne10039c02014-09-18 21:28:49 +0000471 EC == object::object_error::bitcode_section_not_found)
Ivan Krasin5021af52011-09-12 21:47:50 +0000472 return LDPS_OK;
473
Rafael Espindola6c472e52014-07-29 20:46:19 +0000474 *claimed = 1;
475
Rafael Espindola33466a72014-08-21 20:28:55 +0000476 if (EC) {
Rafael Espindola5ad21fa2014-07-30 00:38:58 +0000477 message(LDPL_ERROR, "LLVM gold plugin has failed to create LTO module: %s",
Rafael Espindola33466a72014-08-21 20:28:55 +0000478 EC.message().c_str());
Rafael Espindola6c472e52014-07-29 20:46:19 +0000479 return LDPS_ERR;
Ivan Krasind5f2d8c2011-09-09 00:14:04 +0000480 }
David Blaikie10a27df2014-09-03 17:59:23 +0000481 std::unique_ptr<object::IRObjectFile> Obj = std::move(*ObjOrErr);
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000482
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000483 Modules.resize(Modules.size() + 1);
484 claimed_file &cf = Modules.back();
Rafael Espindola4ef89f52010-08-09 21:09:46 +0000485
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000486 cf.handle = file->handle;
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000487
Teresa Johnson403a7872015-10-04 14:33:43 +0000488 // If we are doing ThinLTO compilation, don't need to process the symbols.
489 // Later we simply build a combined index file after all files are claimed.
Teresa Johnson7cffaf32016-03-04 17:06:02 +0000490 if (options::thinlto && options::thinlto_index_only)
Teresa Johnsonf72278f2015-11-02 18:02:11 +0000491 return LDPS_OK;
Teresa Johnson403a7872015-10-04 14:33:43 +0000492
Rafael Espindola33466a72014-08-21 20:28:55 +0000493 for (auto &Sym : Obj->symbols()) {
494 uint32_t Symflags = Sym.getFlags();
Rafael Espindola527e8462014-12-09 16:13:59 +0000495 if (shouldSkip(Symflags))
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000496 continue;
497
498 cf.syms.push_back(ld_plugin_symbol());
499 ld_plugin_symbol &sym = cf.syms.back();
Rafael Espindola176e6642014-07-29 21:46:05 +0000500 sym.version = nullptr;
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000501
Rafael Espindola33466a72014-08-21 20:28:55 +0000502 SmallString<64> Name;
503 {
504 raw_svector_ostream OS(Name);
505 Sym.printName(OS);
506 }
507 sym.name = strdup(Name.c_str());
508
509 const GlobalValue *GV = Obj->getSymbolGV(Sym.getRawDataRefImpl());
510
Rafael Espindolacaabe222015-12-10 14:19:35 +0000511 ResolutionInfo &Res = ResInfo[sym.name];
512
Rafael Espindola33466a72014-08-21 20:28:55 +0000513 sym.visibility = LDPV_DEFAULT;
514 if (GV) {
Rafael Espindolacaabe222015-12-10 14:19:35 +0000515 Res.UnnamedAddr &= GV->hasUnnamedAddr();
516 Res.IsLinkonceOdr &= GV->hasLinkOnceLinkage();
Rafael Espindolacaabe222015-12-10 14:19:35 +0000517 Res.Visibility = getMinVisibility(Res.Visibility, GV->getVisibility());
Rafael Espindola33466a72014-08-21 20:28:55 +0000518 switch (GV->getVisibility()) {
519 case GlobalValue::DefaultVisibility:
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000520 sym.visibility = LDPV_DEFAULT;
521 break;
Rafael Espindola33466a72014-08-21 20:28:55 +0000522 case GlobalValue::HiddenVisibility:
523 sym.visibility = LDPV_HIDDEN;
524 break;
525 case GlobalValue::ProtectedVisibility:
526 sym.visibility = LDPV_PROTECTED;
527 break;
528 }
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000529 }
530
Rafael Espindola33466a72014-08-21 20:28:55 +0000531 if (Symflags & object::BasicSymbolRef::SF_Undefined) {
532 sym.def = LDPK_UNDEF;
533 if (GV && GV->hasExternalWeakLinkage())
Rafael Espindola56548522009-04-24 16:55:21 +0000534 sym.def = LDPK_WEAKUNDEF;
Rafael Espindola33466a72014-08-21 20:28:55 +0000535 } else {
536 sym.def = LDPK_DEF;
537 if (GV) {
538 assert(!GV->hasExternalWeakLinkage() &&
539 !GV->hasAvailableExternallyLinkage() && "Not a declaration!");
540 if (GV->hasCommonLinkage())
541 sym.def = LDPK_COMMON;
542 else if (GV->isWeakForLinker())
543 sym.def = LDPK_WEAKDEF;
544 }
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000545 }
546
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000547 sym.size = 0;
Rafael Espindola33466a72014-08-21 20:28:55 +0000548 sym.comdat_key = nullptr;
Rafael Espindolaf7ecb112014-08-22 23:26:10 +0000549 if (GV) {
550 const GlobalObject *Base = getBaseObject(*GV);
551 if (!Base)
552 message(LDPL_FATAL, "Unable to determine comdat of alias!");
553 const Comdat *C = Base->getComdat();
554 if (C)
555 sym.comdat_key = strdup(C->getName().str().c_str());
Rafael Espindolaf7ecb112014-08-22 23:26:10 +0000556 }
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000557
558 sym.resolution = LDPR_UNKNOWN;
559 }
560
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000561 if (!cf.syms.empty()) {
Nick Lewycky7282dd72015-08-05 21:16:02 +0000562 if (add_symbols(cf.handle, cf.syms.size(), cf.syms.data()) != LDPS_OK) {
Rafael Espindola5ad21fa2014-07-30 00:38:58 +0000563 message(LDPL_ERROR, "Unable to add symbols!");
Nick Lewyckyfb643e42009-02-03 07:13:24 +0000564 return LDPS_ERR;
565 }
566 }
567
568 return LDPS_OK;
569}
570
Rafael Espindola33466a72014-08-21 20:28:55 +0000571static void internalize(GlobalValue &GV) {
Rafael Espindolad4bcefc2014-10-24 18:13:04 +0000572 if (GV.isDeclarationForLinker())
Rafael Espindola33466a72014-08-21 20:28:55 +0000573 return; // We get here if there is a matching asm definition.
574 if (!GV.hasLocalLinkage())
575 GV.setLinkage(GlobalValue::InternalLinkage);
576}
577
Rafael Espindola33466a72014-08-21 20:28:55 +0000578static const char *getResolutionName(ld_plugin_symbol_resolution R) {
579 switch (R) {
580 case LDPR_UNKNOWN:
581 return "UNKNOWN";
582 case LDPR_UNDEF:
583 return "UNDEF";
584 case LDPR_PREVAILING_DEF:
585 return "PREVAILING_DEF";
586 case LDPR_PREVAILING_DEF_IRONLY:
587 return "PREVAILING_DEF_IRONLY";
588 case LDPR_PREEMPTED_REG:
589 return "PREEMPTED_REG";
590 case LDPR_PREEMPTED_IR:
591 return "PREEMPTED_IR";
592 case LDPR_RESOLVED_IR:
593 return "RESOLVED_IR";
594 case LDPR_RESOLVED_EXEC:
595 return "RESOLVED_EXEC";
596 case LDPR_RESOLVED_DYN:
597 return "RESOLVED_DYN";
598 case LDPR_PREVAILING_DEF_IRONLY_EXP:
599 return "PREVAILING_DEF_IRONLY_EXP";
600 }
Rafael Espindola2754dbb2014-10-10 00:48:13 +0000601 llvm_unreachable("Unknown resolution");
Rafael Espindola33466a72014-08-21 20:28:55 +0000602}
603
Rafael Espindola538c9a82014-12-23 18:18:37 +0000604static void freeSymName(ld_plugin_symbol &Sym) {
605 free(Sym.name);
606 free(Sym.comdat_key);
607 Sym.name = nullptr;
608 Sym.comdat_key = nullptr;
609}
610
Teresa Johnsona9f65552016-03-04 16:36:06 +0000611/// Helper to get a file's symbols and a view into it via gold callbacks.
612static const void *getSymbolsAndView(claimed_file &F) {
Benjamin Kramer39988a02016-03-08 14:02:46 +0000613 ld_plugin_status status = get_symbols(F.handle, F.syms.size(), F.syms.data());
Evgeniy Stepanov330c5a62016-03-04 00:23:29 +0000614 if (status == LDPS_NO_SYMS)
615 return nullptr;
Teresa Johnson403a7872015-10-04 14:33:43 +0000616
Evgeniy Stepanov330c5a62016-03-04 00:23:29 +0000617 if (status != LDPS_OK)
Teresa Johnson403a7872015-10-04 14:33:43 +0000618 message(LDPL_FATAL, "Failed to get symbol information");
619
620 const void *View;
621 if (get_view(F.handle, &View) != LDPS_OK)
622 message(LDPL_FATAL, "Failed to get a view of file");
623
Teresa Johnsona9f65552016-03-04 16:36:06 +0000624 return View;
625}
626
Teresa Johnson26ab5772016-03-15 00:04:37 +0000627static std::unique_ptr<ModuleSummaryIndex>
628getModuleSummaryIndexForFile(claimed_file &F, ld_plugin_input_file &Info) {
Teresa Johnsona9f65552016-03-04 16:36:06 +0000629 const void *View = getSymbolsAndView(F);
Evgeniy Stepanov4dc3c8d2016-03-11 00:51:57 +0000630 if (!View)
631 return nullptr;
Teresa Johnsona9f65552016-03-04 16:36:06 +0000632
Teresa Johnson403a7872015-10-04 14:33:43 +0000633 MemoryBufferRef BufferRef(StringRef((const char *)View, Info.filesize),
634 Info.name);
Teresa Johnson6290dbc2015-11-21 21:55:48 +0000635
636 // Don't bother trying to build an index if there is no summary information
637 // in this bitcode file.
Teresa Johnson26ab5772016-03-15 00:04:37 +0000638 if (!object::ModuleSummaryIndexObjectFile::hasGlobalValueSummaryInMemBuffer(
Teresa Johnson6290dbc2015-11-21 21:55:48 +0000639 BufferRef, diagnosticHandler))
Teresa Johnson26ab5772016-03-15 00:04:37 +0000640 return std::unique_ptr<ModuleSummaryIndex>(nullptr);
Teresa Johnson6290dbc2015-11-21 21:55:48 +0000641
Teresa Johnson26ab5772016-03-15 00:04:37 +0000642 ErrorOr<std::unique_ptr<object::ModuleSummaryIndexObjectFile>> ObjOrErr =
643 object::ModuleSummaryIndexObjectFile::create(BufferRef,
644 diagnosticHandler);
Teresa Johnson403a7872015-10-04 14:33:43 +0000645
646 if (std::error_code EC = ObjOrErr.getError())
Teresa Johnson26ab5772016-03-15 00:04:37 +0000647 message(LDPL_FATAL,
648 "Could not read module summary index bitcode from file : %s",
Teresa Johnson403a7872015-10-04 14:33:43 +0000649 EC.message().c_str());
650
Teresa Johnson26ab5772016-03-15 00:04:37 +0000651 object::ModuleSummaryIndexObjectFile &Obj = **ObjOrErr;
Teresa Johnson403a7872015-10-04 14:33:43 +0000652
653 return Obj.takeIndex();
654}
655
Rafael Espindola33466a72014-08-21 20:28:55 +0000656static std::unique_ptr<Module>
Teresa Johnsona9f65552016-03-04 16:36:06 +0000657getModuleForFile(LLVMContext &Context, claimed_file &F, const void *View,
Rafael Espindola503f8832015-03-02 19:08:03 +0000658 ld_plugin_input_file &Info, raw_fd_ostream *ApiFile,
Rafael Espindolacaabe222015-12-10 14:19:35 +0000659 StringSet<> &Internalize, StringSet<> &Maybe,
Evgeniy Stepanov4dc3c8d2016-03-11 00:51:57 +0000660 std::vector<GlobalValue *> &Keep,
661 StringMap<unsigned> &Realign) {
Rafael Espindola503f8832015-03-02 19:08:03 +0000662 MemoryBufferRef BufferRef(StringRef((const char *)View, Info.filesize),
663 Info.name);
Rafael Espindola527e8462014-12-09 16:13:59 +0000664 ErrorOr<std::unique_ptr<object::IRObjectFile>> ObjOrErr =
Rafael Espindola5dec7ea2014-12-09 20:36:13 +0000665 object::IRObjectFile::create(BufferRef, Context);
Rafael Espindola527e8462014-12-09 16:13:59 +0000666
667 if (std::error_code EC = ObjOrErr.getError())
Peter Collingbourne10039c02014-09-18 21:28:49 +0000668 message(LDPL_FATAL, "Could not read bitcode from file : %s",
669 EC.message().c_str());
670
Rafael Espindola527e8462014-12-09 16:13:59 +0000671 object::IRObjectFile &Obj = **ObjOrErr;
Rafael Espindola33466a72014-08-21 20:28:55 +0000672
Rafael Espindola527e8462014-12-09 16:13:59 +0000673 Module &M = Obj.getModule();
Rafael Espindola33466a72014-08-21 20:28:55 +0000674
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000675 M.materializeMetadata();
676 UpgradeDebugInfo(M);
Rafael Espindola503f8832015-03-02 19:08:03 +0000677
Rafael Espindola33466a72014-08-21 20:28:55 +0000678 SmallPtrSet<GlobalValue *, 8> Used;
Rafael Espindola527e8462014-12-09 16:13:59 +0000679 collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ false);
Rafael Espindola33466a72014-08-21 20:28:55 +0000680
Rafael Espindola527e8462014-12-09 16:13:59 +0000681 unsigned SymNum = 0;
682 for (auto &ObjSym : Obj.symbols()) {
Rafael Espindolacaabe222015-12-10 14:19:35 +0000683 GlobalValue *GV = Obj.getSymbolGV(ObjSym.getRawDataRefImpl());
684 if (GV && GV->hasAppendingLinkage())
685 Keep.push_back(GV);
686
Rafael Espindola527e8462014-12-09 16:13:59 +0000687 if (shouldSkip(ObjSym.getFlags()))
688 continue;
689 ld_plugin_symbol &Sym = F.syms[SymNum];
690 ++SymNum;
691
Rafael Espindola33466a72014-08-21 20:28:55 +0000692 ld_plugin_symbol_resolution Resolution =
693 (ld_plugin_symbol_resolution)Sym.resolution;
694
695 if (options::generate_api_file)
696 *ApiFile << Sym.name << ' ' << getResolutionName(Resolution) << '\n';
697
Rafael Espindola538c9a82014-12-23 18:18:37 +0000698 if (!GV) {
699 freeSymName(Sym);
Rafael Espindola33466a72014-08-21 20:28:55 +0000700 continue; // Asm symbol.
Rafael Espindola538c9a82014-12-23 18:18:37 +0000701 }
Rafael Espindola33466a72014-08-21 20:28:55 +0000702
Rafael Espindolacaabe222015-12-10 14:19:35 +0000703 ResolutionInfo &Res = ResInfo[Sym.name];
704 if (Resolution == LDPR_PREVAILING_DEF_IRONLY_EXP && !Res.IsLinkonceOdr)
705 Resolution = LDPR_PREVAILING_DEF;
706
Teresa Johnsona17f2cd2016-03-04 17:48:35 +0000707 // In ThinLTO mode change all prevailing resolutions to LDPR_PREVAILING_DEF.
708 // For ThinLTO the IR files are compiled through the backend independently,
709 // so we need to ensure that any prevailing linkonce copy will be emitted
710 // into the object file by making it weak. Additionally, we can skip the
711 // IRONLY handling for internalization, which isn't performed in ThinLTO
712 // mode currently anyway.
713 if (options::thinlto && (Resolution == LDPR_PREVAILING_DEF_IRONLY_EXP ||
714 Resolution == LDPR_PREVAILING_DEF_IRONLY))
715 Resolution = LDPR_PREVAILING_DEF;
716
Rafael Espindolacaabe222015-12-10 14:19:35 +0000717 GV->setUnnamedAddr(Res.UnnamedAddr);
718 GV->setVisibility(Res.Visibility);
719
720 // Override gold's resolution for common symbols. We want the largest
721 // one to win.
722 if (GV->hasCommonLinkage()) {
Rafael Espindolacaabe222015-12-10 14:19:35 +0000723 if (Resolution == LDPR_PREVAILING_DEF_IRONLY)
724 Res.CommonInternal = true;
725
726 if (Resolution == LDPR_PREVAILING_DEF_IRONLY ||
727 Resolution == LDPR_PREVAILING_DEF)
728 Res.UseCommon = true;
729
Evgeniy Stepanov4dc3c8d2016-03-11 00:51:57 +0000730 const DataLayout &DL = GV->getParent()->getDataLayout();
731 uint64_t Size = DL.getTypeAllocSize(GV->getType()->getElementType());
732 unsigned Align = GV->getAlignment();
733
734 if (Res.UseCommon && Size >= Res.CommonSize) {
735 // Take GV.
Rafael Espindolacaabe222015-12-10 14:19:35 +0000736 if (Res.CommonInternal)
737 Resolution = LDPR_PREVAILING_DEF_IRONLY;
738 else
739 Resolution = LDPR_PREVAILING_DEF;
Evgeniy Stepanov4dc3c8d2016-03-11 00:51:57 +0000740 cast<GlobalVariable>(GV)->setAlignment(
741 std::max(Res.CommonAlign, Align));
Rafael Espindolacaabe222015-12-10 14:19:35 +0000742 } else {
Evgeniy Stepanov4dc3c8d2016-03-11 00:51:57 +0000743 // Do not take GV, it's smaller than what we already have in the
744 // combined module.
Rafael Espindolacaabe222015-12-10 14:19:35 +0000745 Resolution = LDPR_PREEMPTED_IR;
Evgeniy Stepanov4dc3c8d2016-03-11 00:51:57 +0000746 if (Align > Res.CommonAlign)
747 // Need to raise the alignment though.
748 Realign[Sym.name] = Align;
Rafael Espindolacaabe222015-12-10 14:19:35 +0000749 }
Evgeniy Stepanov4dc3c8d2016-03-11 00:51:57 +0000750
751 Res.CommonSize = std::max(Res.CommonSize, Size);
752 Res.CommonAlign = std::max(Res.CommonAlign, Align);
Rafael Espindola890db272014-09-09 20:08:22 +0000753 }
754
Rafael Espindola33466a72014-08-21 20:28:55 +0000755 switch (Resolution) {
756 case LDPR_UNKNOWN:
757 llvm_unreachable("Unexpected resolution");
758
759 case LDPR_RESOLVED_IR:
760 case LDPR_RESOLVED_EXEC:
761 case LDPR_RESOLVED_DYN:
Rafael Espindolacaabe222015-12-10 14:19:35 +0000762 case LDPR_PREEMPTED_IR:
763 case LDPR_PREEMPTED_REG:
Rafael Espindola33466a72014-08-21 20:28:55 +0000764 break;
765
Rafael Espindola5ca7fa12015-01-14 13:53:50 +0000766 case LDPR_UNDEF:
Rafael Espindolacaabe222015-12-10 14:19:35 +0000767 if (!GV->isDeclarationForLinker())
Rafael Espindola0fd9e5f2015-01-14 19:43:32 +0000768 assert(GV->hasComdat());
Rafael Espindola5ca7fa12015-01-14 13:53:50 +0000769 break;
770
Rafael Espindola33466a72014-08-21 20:28:55 +0000771 case LDPR_PREVAILING_DEF_IRONLY: {
Rafael Espindolacaabe222015-12-10 14:19:35 +0000772 Keep.push_back(GV);
773 // The IR linker has to be able to map this value to a declaration,
774 // so we can only internalize after linking.
775 if (!Used.count(GV))
Rafael Espindolaa4f104b2014-12-09 16:50:57 +0000776 Internalize.insert(GV->getName());
Rafael Espindola33466a72014-08-21 20:28:55 +0000777 break;
778 }
779
780 case LDPR_PREVAILING_DEF:
Rafael Espindolacaabe222015-12-10 14:19:35 +0000781 Keep.push_back(GV);
782 // There is a non IR use, so we have to force optimizations to keep this.
783 switch (GV->getLinkage()) {
784 default:
785 break;
786 case GlobalValue::LinkOnceAnyLinkage:
787 GV->setLinkage(GlobalValue::WeakAnyLinkage);
788 break;
789 case GlobalValue::LinkOnceODRLinkage:
790 GV->setLinkage(GlobalValue::WeakODRLinkage);
791 break;
792 }
Rafael Espindola33466a72014-08-21 20:28:55 +0000793 break;
794
795 case LDPR_PREVAILING_DEF_IRONLY_EXP: {
796 // We can only check for address uses after we merge the modules. The
797 // reason is that this GV might have a copy in another module
798 // and in that module the address might be significant, but that
799 // copy will be LDPR_PREEMPTED_IR.
Rafael Espindolacaabe222015-12-10 14:19:35 +0000800 Maybe.insert(GV->getName());
801 Keep.push_back(GV);
Rafael Espindola33466a72014-08-21 20:28:55 +0000802 break;
803 }
804 }
805
Rafael Espindola538c9a82014-12-23 18:18:37 +0000806 freeSymName(Sym);
Rafael Espindola33466a72014-08-21 20:28:55 +0000807 }
808
Rafael Espindola527e8462014-12-09 16:13:59 +0000809 return Obj.takeModule();
Rafael Espindola33466a72014-08-21 20:28:55 +0000810}
811
Duncan P. N. Exon Smithe406c842015-04-15 00:13:51 +0000812static void saveBCFile(StringRef Path, Module &M) {
Rafael Espindola4a3b6cf2014-10-29 23:54:45 +0000813 std::error_code EC;
814 raw_fd_ostream OS(Path, EC, sys::fs::OpenFlags::F_None);
815 if (EC)
816 message(LDPL_FATAL, "Failed to write the output file.");
Rafael Espindola24052982015-12-22 23:45:49 +0000817 WriteBitcodeToFile(&M, OS, /* ShouldPreserveUseListOrder */ false);
Rafael Espindola4a3b6cf2014-10-29 23:54:45 +0000818}
819
Teresa Johnsona9f65552016-03-04 16:36:06 +0000820static void recordFile(std::string Filename, bool TempOutFile) {
821 if (add_input_file(Filename.c_str()) != LDPS_OK)
822 message(LDPL_FATAL,
823 "Unable to add .o file to the link. File left behind in: %s",
824 Filename.c_str());
825 if (TempOutFile)
826 Cleanup.push_back(Filename.c_str());
827}
Rafael Espindola33466a72014-08-21 20:28:55 +0000828
Teresa Johnson7cffaf32016-03-04 17:06:02 +0000829void ThinLTOTaskInfo::cleanup() {
830 // Close the output file descriptor before we pass it to gold.
831 OS->close();
832
833 recordFile(Filename, TempOutFile);
834}
835
Teresa Johnsona9f65552016-03-04 16:36:06 +0000836namespace {
Teresa Johnson7cffaf32016-03-04 17:06:02 +0000837/// Class to manage optimization and code generation for a module, possibly
838/// in a thread (ThinLTO).
Teresa Johnsona9f65552016-03-04 16:36:06 +0000839class CodeGen {
840 /// The module for which this will generate code.
841 std::unique_ptr<llvm::Module> M;
Rafael Espindola33466a72014-08-21 20:28:55 +0000842
Teresa Johnson7cffaf32016-03-04 17:06:02 +0000843 /// The output stream to generate code into.
844 raw_fd_ostream *OS;
845
846 /// The task ID when this was invoked in a thread (ThinLTO).
847 int TaskID;
848
Teresa Johnson26ab5772016-03-15 00:04:37 +0000849 /// The module summary index for ThinLTO tasks.
850 const ModuleSummaryIndex *CombinedIndex;
Teresa Johnson7cffaf32016-03-04 17:06:02 +0000851
Teresa Johnsona9f65552016-03-04 16:36:06 +0000852 /// The target machine for generating code for this module.
853 std::unique_ptr<TargetMachine> TM;
Rafael Espindola33466a72014-08-21 20:28:55 +0000854
Teresa Johnson7cffaf32016-03-04 17:06:02 +0000855 /// Filename to use as base when save-temps is enabled, used to get
856 /// a unique and identifiable save-temps output file for each ThinLTO backend.
857 std::string SaveTempsFilename;
858
Teresa Johnsona9f65552016-03-04 16:36:06 +0000859public:
860 /// Constructor used by full LTO.
Teresa Johnson7cffaf32016-03-04 17:06:02 +0000861 CodeGen(std::unique_ptr<llvm::Module> M)
862 : M(std::move(M)), OS(nullptr), TaskID(-1), CombinedIndex(nullptr) {
863 initTargetMachine();
864 }
865 /// Constructor used by ThinLTO.
866 CodeGen(std::unique_ptr<llvm::Module> M, raw_fd_ostream *OS, int TaskID,
Teresa Johnson26ab5772016-03-15 00:04:37 +0000867 const ModuleSummaryIndex *CombinedIndex, std::string Filename)
Teresa Johnson7cffaf32016-03-04 17:06:02 +0000868 : M(std::move(M)), OS(OS), TaskID(TaskID), CombinedIndex(CombinedIndex),
869 SaveTempsFilename(Filename) {
870 assert(options::thinlto == !!CombinedIndex &&
Teresa Johnson26ab5772016-03-15 00:04:37 +0000871 "Expected module summary index iff performing ThinLTO");
Teresa Johnsona9f65552016-03-04 16:36:06 +0000872 initTargetMachine();
873 }
874
875 /// Invoke LTO passes and the code generator for the module.
876 void runAll();
877
Teresa Johnson7cffaf32016-03-04 17:06:02 +0000878 /// Invoke the actual code generation to emit Module's object to file.
879 void runCodegenPasses();
880
Teresa Johnsona9f65552016-03-04 16:36:06 +0000881private:
882 /// Create a target machine for the module. Must be unique for each
883 /// module/task.
884 void initTargetMachine();
885
886 /// Run all LTO passes on the module.
887 void runLTOPasses();
888
889 /// Sets up output files necessary to perform optional multi-threaded
890 /// split code generation, and invokes the code generation implementation.
891 void runSplitCodeGen();
892};
893}
894
895static SubtargetFeatures getFeatures(Triple &TheTriple) {
Rafael Espindola33466a72014-08-21 20:28:55 +0000896 SubtargetFeatures Features;
897 Features.getDefaultSubtargetFeatures(TheTriple);
898 for (const std::string &A : MAttrs)
899 Features.AddFeature(A);
Teresa Johnsona9f65552016-03-04 16:36:06 +0000900 return Features;
901}
Rafael Espindola33466a72014-08-21 20:28:55 +0000902
Teresa Johnsona9f65552016-03-04 16:36:06 +0000903static CodeGenOpt::Level getCGOptLevel() {
Peter Collingbourne070843d2015-03-19 22:01:00 +0000904 switch (options::OptLevel) {
905 case 0:
Davide Italiano2788a822016-03-19 02:34:33 +0000906 return CodeGenOpt::None;
Peter Collingbourne070843d2015-03-19 22:01:00 +0000907 case 1:
Davide Italiano2788a822016-03-19 02:34:33 +0000908 return CodeGenOpt::Less;
Peter Collingbourne070843d2015-03-19 22:01:00 +0000909 case 2:
Davide Italiano2788a822016-03-19 02:34:33 +0000910 return CodeGenOpt::Default;
Peter Collingbourne070843d2015-03-19 22:01:00 +0000911 case 3:
Davide Italiano2788a822016-03-19 02:34:33 +0000912 return CodeGenOpt::Aggressive;
Peter Collingbourne070843d2015-03-19 22:01:00 +0000913 }
Davide Italiano2788a822016-03-19 02:34:33 +0000914 llvm_unreachable("Invalid optimization level");
Teresa Johnsona9f65552016-03-04 16:36:06 +0000915}
916
917void CodeGen::initTargetMachine() {
918 const std::string &TripleStr = M->getTargetTriple();
919 Triple TheTriple(TripleStr);
920
921 std::string ErrMsg;
922 const Target *TheTarget = TargetRegistry::lookupTarget(TripleStr, ErrMsg);
923 if (!TheTarget)
924 message(LDPL_FATAL, "Target not found: %s", ErrMsg.c_str());
925
926 SubtargetFeatures Features = getFeatures(TheTriple);
927 TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
928 CodeGenOpt::Level CGOptLevel = getCGOptLevel();
929
930 TM.reset(TheTarget->createTargetMachine(
Rafael Espindola33466a72014-08-21 20:28:55 +0000931 TripleStr, options::mcpu, Features.getString(), Options, RelocationModel,
Peter Collingbourne070843d2015-03-19 22:01:00 +0000932 CodeModel::Default, CGOptLevel));
Teresa Johnsona9f65552016-03-04 16:36:06 +0000933}
Rafael Espindola33466a72014-08-21 20:28:55 +0000934
Teresa Johnsona9f65552016-03-04 16:36:06 +0000935void CodeGen::runLTOPasses() {
936 M->setDataLayout(TM->createDataLayout());
Rafael Espindola33466a72014-08-21 20:28:55 +0000937
Teresa Johnsona9f65552016-03-04 16:36:06 +0000938 legacy::PassManager passes;
939 passes.add(createTargetTransformInfoWrapperPass(TM->getTargetIRAnalysis()));
940
941 PassManagerBuilder PMB;
942 PMB.LibraryInfo = new TargetLibraryInfoImpl(Triple(TM->getTargetTriple()));
943 PMB.Inliner = createFunctionInliningPass();
944 // Unconditionally verify input since it is not verified before this
945 // point and has unknown origin.
946 PMB.VerifyInput = true;
947 PMB.VerifyOutput = !options::DisableVerify;
948 PMB.LoopVectorize = true;
949 PMB.SLPVectorize = true;
950 PMB.OptLevel = options::OptLevel;
Teresa Johnson26ab5772016-03-15 00:04:37 +0000951 PMB.ModuleSummary = CombinedIndex;
Teresa Johnsona9f65552016-03-04 16:36:06 +0000952 PMB.populateLTOPassManager(passes);
953 passes.run(*M);
954}
955
956/// Open a file and return the new file descriptor given a base input
957/// file name, a flag indicating whether a temp file should be generated,
958/// and an optional task id. The new filename generated is
959/// returned in \p NewFilename.
960static int openOutputFile(SmallString<128> InFilename, bool TempOutFile,
961 SmallString<128> &NewFilename, int TaskID = -1) {
962 int FD;
963 if (TempOutFile) {
964 std::error_code EC =
965 sys::fs::createTemporaryFile("lto-llvm", "o", FD, NewFilename);
966 if (EC)
967 message(LDPL_FATAL, "Could not create temporary file: %s",
968 EC.message().c_str());
969 } else {
970 NewFilename = InFilename;
971 if (TaskID >= 0)
972 NewFilename += utostr(TaskID);
973 std::error_code EC =
974 sys::fs::openFileForWrite(NewFilename, FD, sys::fs::F_None);
975 if (EC)
976 message(LDPL_FATAL, "Could not open file: %s", EC.message().c_str());
977 }
978 return FD;
979}
980
Teresa Johnson7cffaf32016-03-04 17:06:02 +0000981void CodeGen::runCodegenPasses() {
982 assert(OS && "Output stream must be set before emitting to file");
983 legacy::PassManager CodeGenPasses;
984 if (TM->addPassesToEmitFile(CodeGenPasses, *OS,
985 TargetMachine::CGFT_ObjectFile))
986 report_fatal_error("Failed to setup codegen");
987 CodeGenPasses.run(*M);
988}
989
Teresa Johnsona9f65552016-03-04 16:36:06 +0000990void CodeGen::runSplitCodeGen() {
991 const std::string &TripleStr = M->getTargetTriple();
992 Triple TheTriple(TripleStr);
993
994 SubtargetFeatures Features = getFeatures(TheTriple);
995
996 TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
997 CodeGenOpt::Level CGOptLevel = getCGOptLevel();
Rafael Espindola33466a72014-08-21 20:28:55 +0000998
999 SmallString<128> Filename;
Teresa Johnsona9f65552016-03-04 16:36:06 +00001000 // Note that openOutputFile will append a unique ID for each task
Rafael Espindola92200d22015-06-15 13:36:27 +00001001 if (!options::obj_path.empty())
1002 Filename = options::obj_path;
1003 else if (options::TheOutputType == options::OT_SAVE_TEMPS)
1004 Filename = output_name + ".o";
1005
Teresa Johnsona9f65552016-03-04 16:36:06 +00001006 // Note that the default parallelism is 1 instead of the
1007 // hardware_concurrency, as there are behavioral differences between
1008 // parallelism levels (e.g. symbol ordering will be different, and some uses
1009 // of inline asm currently have issues with parallelism >1).
1010 unsigned int MaxThreads = options::Parallelism ? options::Parallelism : 1;
1011
1012 std::vector<SmallString<128>> Filenames(MaxThreads);
Rafael Espindola92200d22015-06-15 13:36:27 +00001013 bool TempOutFile = Filename.empty();
Rafael Espindola33466a72014-08-21 20:28:55 +00001014 {
Teresa Johnsona9f65552016-03-04 16:36:06 +00001015 // Open a file descriptor for each backend task. This is done in a block
Peter Collingbourne87202a42015-09-01 20:40:22 +00001016 // so that the output file descriptors are closed before gold opens them.
1017 std::list<llvm::raw_fd_ostream> OSs;
Teresa Johnsona9f65552016-03-04 16:36:06 +00001018 std::vector<llvm::raw_pwrite_stream *> OSPtrs(MaxThreads);
1019 for (unsigned I = 0; I != MaxThreads; ++I) {
Teresa Johnson3b8f6122016-03-04 18:16:00 +00001020 int FD = openOutputFile(Filename, TempOutFile, Filenames[I],
1021 // Only append ID if there are multiple tasks.
1022 MaxThreads > 1 ? I : -1);
Peter Collingbourne87202a42015-09-01 20:40:22 +00001023 OSs.emplace_back(FD, true);
1024 OSPtrs[I] = &OSs.back();
1025 }
Rafael Espindola33466a72014-08-21 20:28:55 +00001026
Teresa Johnsona9f65552016-03-04 16:36:06 +00001027 // Run backend tasks.
Peter Collingbourne87202a42015-09-01 20:40:22 +00001028 splitCodeGen(std::move(M), OSPtrs, options::mcpu, Features.getString(),
1029 Options, RelocationModel, CodeModel::Default, CGOptLevel);
Rafael Espindola33466a72014-08-21 20:28:55 +00001030 }
1031
Teresa Johnsona9f65552016-03-04 16:36:06 +00001032 for (auto &Filename : Filenames)
1033 recordFile(Filename.c_str(), TempOutFile);
1034}
1035
1036void CodeGen::runAll() {
1037 runLTOPasses();
1038
1039 if (options::TheOutputType == options::OT_SAVE_TEMPS) {
Teresa Johnson7cffaf32016-03-04 17:06:02 +00001040 std::string OptFilename = output_name;
1041 // If the CodeGen client provided a filename, use it. Always expect
1042 // a provided filename if we are in a task (i.e. ThinLTO backend).
1043 assert(!SaveTempsFilename.empty() || TaskID == -1);
1044 if (!SaveTempsFilename.empty())
1045 OptFilename = SaveTempsFilename;
1046 saveBCFile(OptFilename + ".opt.bc", *M);
Peter Collingbourne87202a42015-09-01 20:40:22 +00001047 }
Teresa Johnsona9f65552016-03-04 16:36:06 +00001048
Teresa Johnson7cffaf32016-03-04 17:06:02 +00001049 // If we are already in a thread (i.e. ThinLTO), just perform
1050 // codegen passes directly.
1051 if (TaskID >= 0)
1052 runCodegenPasses();
1053 // Otherwise attempt split code gen.
1054 else
1055 runSplitCodeGen();
Teresa Johnsona9f65552016-03-04 16:36:06 +00001056}
1057
1058/// Links the module in \p View from file \p F into the combined module
1059/// saved in the IRMover \p L. Returns true on error, false on success.
1060static bool linkInModule(LLVMContext &Context, IRMover &L, claimed_file &F,
1061 const void *View, ld_plugin_input_file &File,
1062 raw_fd_ostream *ApiFile, StringSet<> &Internalize,
1063 StringSet<> &Maybe) {
1064 std::vector<GlobalValue *> Keep;
Evgeniy Stepanov4dc3c8d2016-03-11 00:51:57 +00001065 StringMap<unsigned> Realign;
1066 std::unique_ptr<Module> M = getModuleForFile(
1067 Context, F, View, File, ApiFile, Internalize, Maybe, Keep, Realign);
Teresa Johnsona9f65552016-03-04 16:36:06 +00001068 if (!M.get())
1069 return false;
1070 if (!options::triple.empty())
1071 M->setTargetTriple(options::triple.c_str());
1072 else if (M->getTargetTriple().empty()) {
1073 M->setTargetTriple(DefaultTriple);
1074 }
1075
Evgeniy Stepanov4dc3c8d2016-03-11 00:51:57 +00001076 if (!L.move(std::move(M), Keep, [](GlobalValue &, IRMover::ValueAdder) {}))
1077 return false;
1078
1079 for (const auto &I : Realign) {
1080 GlobalValue *Dst = L.getModule().getNamedValue(I.first());
1081 if (!Dst)
1082 continue;
1083 cast<GlobalVariable>(Dst)->setAlignment(I.second);
1084 }
1085
1086 return true;
Rafael Espindola282a4702013-10-31 20:51:58 +00001087}
1088
Teresa Johnson7cffaf32016-03-04 17:06:02 +00001089/// Perform the ThinLTO backend on a single module, invoking the LTO and codegen
1090/// pipelines.
1091static void thinLTOBackendTask(claimed_file &F, const void *View,
1092 ld_plugin_input_file &File,
1093 raw_fd_ostream *ApiFile,
Teresa Johnson26ab5772016-03-15 00:04:37 +00001094 const ModuleSummaryIndex &CombinedIndex,
Teresa Johnson7cffaf32016-03-04 17:06:02 +00001095 raw_fd_ostream *OS, unsigned TaskID) {
1096 // Need to use a separate context for each task
1097 LLVMContext Context;
1098 Context.setDiagnosticHandler(diagnosticHandlerForContext, nullptr, true);
1099
1100 std::unique_ptr<llvm::Module> NewModule(new llvm::Module(File.name, Context));
1101 IRMover L(*NewModule.get());
1102
1103 StringSet<> Dummy;
1104 if (linkInModule(Context, L, F, View, File, ApiFile, Dummy, Dummy))
1105 message(LDPL_FATAL, "Failed to rename module for ThinLTO");
Mehdi Aminid2d98962016-03-09 01:55:15 +00001106 if (renameModuleForThinLTO(*NewModule, CombinedIndex))
Teresa Johnson7cffaf32016-03-04 17:06:02 +00001107 message(LDPL_FATAL, "Failed to rename module for ThinLTO");
1108
1109 CodeGen codeGen(std::move(NewModule), OS, TaskID, &CombinedIndex, File.name);
1110 codeGen.runAll();
1111}
1112
1113/// Launch each module's backend pipeline in a separate task in a thread pool.
1114static void thinLTOBackends(raw_fd_ostream *ApiFile,
Teresa Johnson26ab5772016-03-15 00:04:37 +00001115 const ModuleSummaryIndex &CombinedIndex) {
Teresa Johnson7cffaf32016-03-04 17:06:02 +00001116 unsigned TaskCount = 0;
1117 std::vector<ThinLTOTaskInfo> Tasks;
1118 Tasks.reserve(Modules.size());
1119 unsigned int MaxThreads = options::Parallelism
1120 ? options::Parallelism
1121 : thread::hardware_concurrency();
1122
1123 // Create ThreadPool in nested scope so that threads will be joined
1124 // on destruction.
1125 {
1126 ThreadPool ThinLTOThreadPool(MaxThreads);
1127 for (claimed_file &F : Modules) {
1128 // Do all the gold callbacks in the main thread, since gold is not thread
1129 // safe by default.
1130 PluginInputFile InputFile(F.handle);
1131 const void *View = getSymbolsAndView(F);
Evgeniy Stepanov4dc3c8d2016-03-11 00:51:57 +00001132 if (!View)
1133 continue;
Teresa Johnson7cffaf32016-03-04 17:06:02 +00001134
1135 SmallString<128> Filename;
1136 if (!options::obj_path.empty())
1137 // Note that openOutputFile will append a unique ID for each task
1138 Filename = options::obj_path;
1139 else if (options::TheOutputType == options::OT_SAVE_TEMPS) {
1140 // Use the input file name so that we get a unique and identifiable
1141 // output file for each ThinLTO backend task.
1142 Filename = InputFile.file().name;
1143 Filename += ".thinlto.o";
1144 }
1145 bool TempOutFile = Filename.empty();
1146
1147 SmallString<128> NewFilename;
1148 int FD = openOutputFile(Filename, TempOutFile, NewFilename,
1149 // Only append the TaskID if we will use the
1150 // non-unique obj_path.
1151 !options::obj_path.empty() ? TaskCount : -1);
1152 TaskCount++;
1153 std::unique_ptr<raw_fd_ostream> OS =
1154 llvm::make_unique<raw_fd_ostream>(FD, true);
1155
1156 // Enqueue the task
1157 ThinLTOThreadPool.async(thinLTOBackendTask, std::ref(F), View,
1158 std::ref(InputFile.file()), ApiFile,
1159 std::ref(CombinedIndex), OS.get(), TaskCount);
1160
1161 // Record the information needed by the task or during its cleanup
1162 // to a ThinLTOTaskInfo instance. For information needed by the task
1163 // the unique_ptr ownership is transferred to the ThinLTOTaskInfo.
1164 Tasks.emplace_back(std::move(InputFile), std::move(OS),
1165 NewFilename.c_str(), TempOutFile);
1166 }
1167 }
1168
1169 for (auto &Task : Tasks)
1170 Task.cleanup();
1171}
1172
Rafael Espindolab6393292014-07-30 01:23:45 +00001173/// gold informs us that all symbols have been read. At this point, we use
1174/// get_symbols to see if any of our definitions have been overridden by a
1175/// native object file. Then, perform optimization and codegen.
Rafael Espindola33466a72014-08-21 20:28:55 +00001176static ld_plugin_status allSymbolsReadHook(raw_fd_ostream *ApiFile) {
1177 if (Modules.empty())
1178 return LDPS_OK;
Rafael Espindola9ef90d52011-02-20 18:28:29 +00001179
Teresa Johnsona9f65552016-03-04 16:36:06 +00001180 if (unsigned NumOpts = options::extra.size())
1181 cl::ParseCommandLineOptions(NumOpts, &options::extra[0]);
1182
Teresa Johnson403a7872015-10-04 14:33:43 +00001183 // If we are doing ThinLTO compilation, simply build the combined
Teresa Johnson26ab5772016-03-15 00:04:37 +00001184 // module index/summary and emit it. We don't need to parse the modules
Teresa Johnson403a7872015-10-04 14:33:43 +00001185 // and link them in this case.
1186 if (options::thinlto) {
Teresa Johnson26ab5772016-03-15 00:04:37 +00001187 ModuleSummaryIndex CombinedIndex;
Teresa Johnson403a7872015-10-04 14:33:43 +00001188 uint64_t NextModuleId = 0;
1189 for (claimed_file &F : Modules) {
Teresa Johnsoncb15b732015-12-16 16:34:06 +00001190 PluginInputFile InputFile(F.handle);
Teresa Johnson403a7872015-10-04 14:33:43 +00001191
Teresa Johnson26ab5772016-03-15 00:04:37 +00001192 std::unique_ptr<ModuleSummaryIndex> Index =
1193 getModuleSummaryIndexForFile(F, InputFile.file());
Teresa Johnson6290dbc2015-11-21 21:55:48 +00001194
Teresa Johnson26ab5772016-03-15 00:04:37 +00001195 // Skip files without a module summary.
Teresa Johnson9d5b71b2015-12-10 16:11:23 +00001196 if (Index)
1197 CombinedIndex.mergeFrom(std::move(Index), ++NextModuleId);
Teresa Johnson403a7872015-10-04 14:33:43 +00001198 }
1199
1200 std::error_code EC;
1201 raw_fd_ostream OS(output_name + ".thinlto.bc", EC,
1202 sys::fs::OpenFlags::F_None);
1203 if (EC)
1204 message(LDPL_FATAL, "Unable to open %s.thinlto.bc for writing: %s",
1205 output_name.data(), EC.message().c_str());
Teresa Johnson76a1c1d2016-03-11 18:52:24 +00001206 WriteIndexToFile(CombinedIndex, OS);
Teresa Johnson403a7872015-10-04 14:33:43 +00001207 OS.close();
1208
Teresa Johnson7cffaf32016-03-04 17:06:02 +00001209 if (options::thinlto_index_only) {
1210 cleanup_hook();
1211 exit(0);
1212 }
1213
1214 thinLTOBackends(ApiFile, CombinedIndex);
1215 return LDPS_OK;
Teresa Johnson403a7872015-10-04 14:33:43 +00001216 }
1217
Teresa Johnsonaf9e9312015-12-09 19:49:40 +00001218 LLVMContext Context;
1219 Context.setDiagnosticHandler(diagnosticHandlerForContext, nullptr, true);
1220
Rafael Espindola33466a72014-08-21 20:28:55 +00001221 std::unique_ptr<Module> Combined(new Module("ld-temp.o", Context));
Rafael Espindola9d2bfc42015-12-14 23:17:03 +00001222 IRMover L(*Combined);
Rafael Espindola33466a72014-08-21 20:28:55 +00001223
Rafael Espindola33466a72014-08-21 20:28:55 +00001224 StringSet<> Internalize;
1225 StringSet<> Maybe;
Rafael Espindolad2aac572014-07-30 01:52:40 +00001226 for (claimed_file &F : Modules) {
Teresa Johnsoncb15b732015-12-16 16:34:06 +00001227 PluginInputFile InputFile(F.handle);
Teresa Johnsona9f65552016-03-04 16:36:06 +00001228 const void *View = getSymbolsAndView(F);
Evgeniy Stepanov4dc3c8d2016-03-11 00:51:57 +00001229 if (!View)
1230 continue;
Teresa Johnsona9f65552016-03-04 16:36:06 +00001231 if (linkInModule(Context, L, F, View, InputFile.file(), ApiFile,
1232 Internalize, Maybe))
Rafael Espindolad12b4a32014-10-25 04:06:10 +00001233 message(LDPL_FATAL, "Failed to link module");
Rafael Espindola77b6d012010-06-14 21:20:52 +00001234 }
Nick Lewyckyfb643e42009-02-03 07:13:24 +00001235
Rafael Espindola33466a72014-08-21 20:28:55 +00001236 for (const auto &Name : Internalize) {
1237 GlobalValue *GV = Combined->getNamedValue(Name.first());
1238 if (GV)
1239 internalize(*GV);
1240 }
1241
1242 for (const auto &Name : Maybe) {
1243 GlobalValue *GV = Combined->getNamedValue(Name.first());
1244 if (!GV)
1245 continue;
1246 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1247 if (canBeOmittedFromSymbolTable(GV))
1248 internalize(*GV);
1249 }
Rafael Espindolaccab1dd2010-08-11 00:15:13 +00001250
Rafael Espindola6953a3a2014-11-24 21:18:14 +00001251 if (options::TheOutputType == options::OT_DISABLE)
1252 return LDPS_OK;
1253
1254 if (options::TheOutputType != options::OT_NORMAL) {
Rafael Espindola8fb957e2010-06-03 21:11:20 +00001255 std::string path;
Rafael Espindola6953a3a2014-11-24 21:18:14 +00001256 if (options::TheOutputType == options::OT_BC_ONLY)
Rafael Espindola8fb957e2010-06-03 21:11:20 +00001257 path = output_name;
Rafael Espindola8fb957e2010-06-03 21:11:20 +00001258 else
1259 path = output_name + ".bc";
Rafael Espindola0e309fe2015-12-01 19:50:54 +00001260 saveBCFile(path, *Combined);
Rafael Espindola6953a3a2014-11-24 21:18:14 +00001261 if (options::TheOutputType == options::OT_BC_ONLY)
Rafael Espindola55b32542014-08-11 19:06:54 +00001262 return LDPS_OK;
Rafael Espindolaba3398b2010-05-13 13:39:31 +00001263 }
Rafael Espindola143fc3b2013-10-16 12:47:04 +00001264
Teresa Johnsona9f65552016-03-04 16:36:06 +00001265 CodeGen codeGen(std::move(Combined));
1266 codeGen.runAll();
Nick Lewyckyfb643e42009-02-03 07:13:24 +00001267
Rafael Espindolaef498152010-06-23 20:20:59 +00001268 if (!options::extra_library_path.empty() &&
Rafael Espindola33466a72014-08-21 20:28:55 +00001269 set_extra_library_path(options::extra_library_path.c_str()) != LDPS_OK)
1270 message(LDPL_FATAL, "Unable to set the extra library path.");
Shuxin Yang1826ae22013-08-12 21:07:31 +00001271
Nick Lewyckyfb643e42009-02-03 07:13:24 +00001272 return LDPS_OK;
1273}
1274
Rafael Espindola55b32542014-08-11 19:06:54 +00001275static ld_plugin_status all_symbols_read_hook(void) {
1276 ld_plugin_status Ret;
1277 if (!options::generate_api_file) {
1278 Ret = allSymbolsReadHook(nullptr);
1279 } else {
Rafael Espindola3fd1e992014-08-25 18:16:47 +00001280 std::error_code EC;
1281 raw_fd_ostream ApiFile("apifile.txt", EC, sys::fs::F_None);
1282 if (EC)
Rafael Espindola55b32542014-08-11 19:06:54 +00001283 message(LDPL_FATAL, "Unable to open apifile.txt for writing: %s",
Rafael Espindola3fd1e992014-08-25 18:16:47 +00001284 EC.message().c_str());
Rafael Espindola33466a72014-08-21 20:28:55 +00001285 Ret = allSymbolsReadHook(&ApiFile);
Rafael Espindola55b32542014-08-11 19:06:54 +00001286 }
1287
Rafael Espindola947bdb62014-11-25 20:52:49 +00001288 llvm_shutdown();
1289
Rafael Espindola6953a3a2014-11-24 21:18:14 +00001290 if (options::TheOutputType == options::OT_BC_ONLY ||
Michael Kupersteina07d9b92015-02-12 18:21:50 +00001291 options::TheOutputType == options::OT_DISABLE) {
Davide Italiano289a43e2016-03-20 20:12:33 +00001292 if (options::TheOutputType == options::OT_DISABLE) {
Michael Kupersteina07d9b92015-02-12 18:21:50 +00001293 // Remove the output file here since ld.bfd creates the output file
1294 // early.
Davide Italiano289a43e2016-03-20 20:12:33 +00001295 std::error_code EC = sys::fs::remove(output_name);
1296 if (EC)
1297 message(LDPL_ERROR, "Failed to delete '%s': %s", output_name.c_str(),
1298 EC.message().c_str());
1299 }
Rafael Espindola55b32542014-08-11 19:06:54 +00001300 exit(0);
Michael Kupersteina07d9b92015-02-12 18:21:50 +00001301 }
Rafael Espindola55b32542014-08-11 19:06:54 +00001302
1303 return Ret;
1304}
1305
Dan Gohmanebb4ae02010-04-16 00:42:57 +00001306static ld_plugin_status cleanup_hook(void) {
Rafael Espindolad2aac572014-07-30 01:52:40 +00001307 for (std::string &Name : Cleanup) {
1308 std::error_code EC = sys::fs::remove(Name);
Rafael Espindola55ab87f2013-06-17 18:38:18 +00001309 if (EC)
Rafael Espindolad2aac572014-07-30 01:52:40 +00001310 message(LDPL_ERROR, "Failed to delete '%s': %s", Name.c_str(),
Rafael Espindola5ad21fa2014-07-30 00:38:58 +00001311 EC.message().c_str());
Rafael Espindola55ab87f2013-06-17 18:38:18 +00001312 }
Nick Lewyckyfb643e42009-02-03 07:13:24 +00001313
1314 return LDPS_OK;
1315}