Rui Ueyama | 2599248 | 2016-03-22 20:52:10 | [diff] [blame] | 1 | //===- LTO.cpp ------------------------------------------------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "LTO.h" |
| 10 | #include "Config.h" |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 | [diff] [blame] | 11 | #include "InputFiles.h" |
Rafael Espindola | 4b075bb | 2017-07-26 23:39:10 | [diff] [blame] | 12 | #include "SymbolTable.h" |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 | [diff] [blame] | 13 | #include "Symbols.h" |
Sam Clegg | 5cdc91d | 2019-01-30 20:46:18 | [diff] [blame] | 14 | #include "lld/Common/Args.h" |
Fangrui Song | f7669ba | 2024-01-23 19:38:15 | [diff] [blame] | 15 | #include "lld/Common/CommonLinkerContext.h" |
Bob Haarman | b8a59c8 | 2017-10-25 22:28:38 | [diff] [blame] | 16 | #include "lld/Common/ErrorHandler.h" |
Matheus Izvekov | 8ff77a8 | 2023-09-27 22:06:48 | [diff] [blame] | 17 | #include "lld/Common/Filesystem.h" |
Fangrui Song | 27bb799 | 2022-02-08 05:53:34 | [diff] [blame] | 18 | #include "lld/Common/Strings.h" |
Rui Ueyama | 3f85170 | 2017-10-02 21:00:41 | [diff] [blame] | 19 | #include "lld/Common/TargetOptionsCommandFlags.h" |
Rui Ueyama | 8c6a5aa | 2016-11-05 22:37:59 | [diff] [blame] | 20 | #include "llvm/ADT/SmallString.h" |
Eugene Zelenko | 22886a2 | 2016-11-05 01:00:56 | [diff] [blame] | 21 | #include "llvm/ADT/StringRef.h" |
| 22 | #include "llvm/ADT/Twine.h" |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 | [diff] [blame] | 23 | #include "llvm/BinaryFormat/ELF.h" |
Rumeet Dhindsa | d366e36 | 2018-05-02 21:40:07 | [diff] [blame] | 24 | #include "llvm/Bitcode/BitcodeWriter.h" |
Eugene Zelenko | 22886a2 | 2016-11-05 01:00:56 | [diff] [blame] | 25 | #include "llvm/LTO/Config.h" |
Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 | [diff] [blame] | 26 | #include "llvm/LTO/LTO.h" |
Noah Shutty | e678c51 | 2021-10-19 01:40:57 | [diff] [blame] | 27 | #include "llvm/Support/Caching.h" |
Eugene Zelenko | 22886a2 | 2016-11-05 01:00:56 | [diff] [blame] | 28 | #include "llvm/Support/CodeGen.h" |
Eugene Zelenko | 22886a2 | 2016-11-05 01:00:56 | [diff] [blame] | 29 | #include "llvm/Support/Error.h" |
| 30 | #include "llvm/Support/FileSystem.h" |
| 31 | #include "llvm/Support/MemoryBuffer.h" |
Fangrui Song | f7669ba | 2024-01-23 19:38:15 | [diff] [blame] | 32 | #include "llvm/Support/Path.h" |
Eugene Zelenko | 22886a2 | 2016-11-05 01:00:56 | [diff] [blame] | 33 | #include <algorithm> |
| 34 | #include <cstddef> |
| 35 | #include <memory> |
| 36 | #include <string> |
| 37 | #include <system_error> |
| 38 | #include <vector> |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 | [diff] [blame] | 39 | |
| 40 | using namespace llvm; |
| 41 | using namespace llvm::object; |
| 42 | using namespace llvm::ELF; |
Fangrui Song | 07837b8 | 2020-05-15 05:18:58 | [diff] [blame] | 43 | using namespace lld; |
| 44 | using namespace lld::elf; |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 | [diff] [blame] | 45 | |
Fangrui Song | 6b56a27 | 2024-09-21 17:11:37 | [diff] [blame] | 46 | static std::string getThinLTOOutputFile(Ctx &ctx, StringRef modulePath) { |
| 47 | return lto::getThinLTOOutputFile(modulePath, ctx.arg.thinLTOPrefixReplaceOld, |
| 48 | ctx.arg.thinLTOPrefixReplaceNew); |
Rui Ueyama | d54f1c2 | 2018-05-07 22:11:24 | [diff] [blame] | 49 | } |
| 50 | |
Fangrui Song | 6b56a27 | 2024-09-21 17:11:37 | [diff] [blame] | 51 | static lto::Config createConfig(Ctx &ctx) { |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 52 | lto::Config c; |
Davide Italiano | d26c4a1 | 2016-05-15 19:29:38 | [diff] [blame] | 53 | |
Peter Collingbourne | 82a7f14 | 2018-08-06 20:12:12 | [diff] [blame] | 54 | // LLD supports the new relocations and address-significance tables. |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 55 | c.Options = initTargetOptionsFromCodeGenFlags(); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 56 | c.Options.EmitAddrsig = true; |
Fangrui Song | 6b56a27 | 2024-09-21 17:11:37 | [diff] [blame] | 57 | for (StringRef C : ctx.arg.mllvmOpts) |
Mircea Trofin | c625c17 | 2022-09-16 04:01:56 | [diff] [blame] | 58 | c.MllvmArgs.emplace_back(C.str()); |
Davide Italiano | df24d5b | 2016-06-02 22:58:11 | [diff] [blame] | 59 | |
Davide Italiano | 957f120 | 2017-07-24 20:15:07 | [diff] [blame] | 60 | // Always emit a section per function/datum with LTO. |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 61 | c.Options.FunctionSections = true; |
| 62 | c.Options.DataSections = true; |
Davide Italiano | 1f4e29c | 2017-07-24 19:38:13 | [diff] [blame] | 63 | |
Sriraman Tallam | 9431787 | 2020-04-07 13:48:18 | [diff] [blame] | 64 | // Check if basic block sections must be used. |
Rahman Lavaee | 1f17c2d | 2024-10-07 16:22:36 | [diff] [blame] | 65 | // Allowed values for --lto-basic-block-sections are "all", |
Sriraman Tallam | 9431787 | 2020-04-07 13:48:18 | [diff] [blame] | 66 | // "<file name specifying basic block ids>", or none. This is the equivalent |
Sriraman Tallam | e0bca46 | 2020-06-02 06:17:29 | [diff] [blame] | 67 | // of -fbasic-block-sections= flag in clang. |
Fangrui Song | 6b56a27 | 2024-09-21 17:11:37 | [diff] [blame] | 68 | if (!ctx.arg.ltoBasicBlockSections.empty()) { |
| 69 | if (ctx.arg.ltoBasicBlockSections == "all") { |
Sriraman Tallam | 9431787 | 2020-04-07 13:48:18 | [diff] [blame] | 70 | c.Options.BBSections = BasicBlockSection::All; |
Fangrui Song | 6b56a27 | 2024-09-21 17:11:37 | [diff] [blame] | 71 | } else if (ctx.arg.ltoBasicBlockSections == "labels") { |
Rahman Lavaee | 7b7747d | 2024-09-26 05:03:10 | [diff] [blame] | 72 | c.Options.BBAddrMap = true; |
Fangrui Song | f8bae3a | 2024-11-07 06:19:31 | [diff] [blame] | 73 | Warn(ctx) |
| 74 | << "'--lto-basic-block-sections=labels' is deprecated; Please use " |
| 75 | "'--lto-basic-block-address-map' instead"; |
Fangrui Song | 6b56a27 | 2024-09-21 17:11:37 | [diff] [blame] | 76 | } else if (ctx.arg.ltoBasicBlockSections == "none") { |
Sriraman Tallam | 9431787 | 2020-04-07 13:48:18 | [diff] [blame] | 77 | c.Options.BBSections = BasicBlockSection::None; |
| 78 | } else { |
| 79 | ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = |
Fangrui Song | 6b56a27 | 2024-09-21 17:11:37 | [diff] [blame] | 80 | MemoryBuffer::getFile(ctx.arg.ltoBasicBlockSections.str()); |
Sriraman Tallam | 9431787 | 2020-04-07 13:48:18 | [diff] [blame] | 81 | if (!MBOrErr) { |
Fangrui Song | 09c2c5e | 2024-11-07 06:04:52 | [diff] [blame] | 82 | ErrAlways(ctx) << "cannot open " << ctx.arg.ltoBasicBlockSections << ":" |
| 83 | << MBOrErr.getError().message(); |
Sriraman Tallam | 9431787 | 2020-04-07 13:48:18 | [diff] [blame] | 84 | } else { |
| 85 | c.Options.BBSectionsFuncListBuf = std::move(*MBOrErr); |
| 86 | } |
| 87 | c.Options.BBSections = BasicBlockSection::List; |
| 88 | } |
| 89 | } |
| 90 | |
Rahman Lavaee | 7b7747d | 2024-09-26 05:03:10 | [diff] [blame] | 91 | c.Options.BBAddrMap = ctx.arg.ltoBBAddrMap; |
| 92 | |
Sriraman Tallam | e0bca46 | 2020-06-02 06:17:29 | [diff] [blame] | 93 | c.Options.UniqueBasicBlockSectionNames = |
Fangrui Song | 6b56a27 | 2024-09-21 17:11:37 | [diff] [blame] | 94 | ctx.arg.ltoUniqueBasicBlockSectionNames; |
Sriraman Tallam | 9431787 | 2020-04-07 13:48:18 | [diff] [blame] | 95 | |
Petr Hosek | ae4c30a | 2019-07-20 21:59:47 | [diff] [blame] | 96 | if (auto relocModel = getRelocModelFromCMModel()) |
| 97 | c.RelocModel = *relocModel; |
Fangrui Song | 6b56a27 | 2024-09-21 17:11:37 | [diff] [blame] | 98 | else if (ctx.arg.relocatable) |
Kazu Hirata | c68af42 | 2022-12-03 07:12:36 | [diff] [blame] | 99 | c.RelocModel = std::nullopt; |
Fangrui Song | 6b56a27 | 2024-09-21 17:11:37 | [diff] [blame] | 100 | else if (ctx.arg.isPic) |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 101 | c.RelocModel = Reloc::PIC_; |
Evgeniy Stepanov | f12ac5b | 2017-05-22 21:11:44 | [diff] [blame] | 102 | else |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 103 | c.RelocModel = Reloc::Static; |
Rui Ueyama | 397dffd | 2018-05-07 23:24:07 | [diff] [blame] | 104 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 105 | c.CodeModel = getCodeModelFromCMModel(); |
Fangrui Song | 6b56a27 | 2024-09-21 17:11:37 | [diff] [blame] | 106 | c.DisableVerify = ctx.arg.disableVerify; |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 107 | c.DiagHandler = diagnosticHandler; |
Fangrui Song | 6b56a27 | 2024-09-21 17:11:37 | [diff] [blame] | 108 | c.OptLevel = ctx.arg.ltoo; |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 109 | c.CPU = getCPUStr(); |
| 110 | c.MAttrs = getMAttrs(); |
Fangrui Song | 6b56a27 | 2024-09-21 17:11:37 | [diff] [blame] | 111 | c.CGOptLevel = ctx.arg.ltoCgo; |
Davide Italiano | df24d5b | 2016-06-02 22:58:11 | [diff] [blame] | 112 | |
Wei Mi | 21a4710 | 2020-01-10 04:58:31 | [diff] [blame] | 113 | c.PTO.LoopVectorization = c.OptLevel > 1; |
| 114 | c.PTO.SLPVectorization = c.OptLevel > 1; |
| 115 | |
Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 | [diff] [blame] | 116 | // Set up a custom pipeline if we've been asked to. |
Fangrui Song | 6b56a27 | 2024-09-21 17:11:37 | [diff] [blame] | 117 | c.OptPipeline = std::string(ctx.arg.ltoNewPmPasses); |
| 118 | c.AAPipeline = std::string(ctx.arg.ltoAAPipeline); |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 | [diff] [blame] | 119 | |
Davide Italiano | db4b0a7 | 2017-02-13 17:49:18 | [diff] [blame] | 120 | // Set up optimization remarks if we've been asked to. |
Fangrui Song | 6b56a27 | 2024-09-21 17:11:37 | [diff] [blame] | 121 | c.RemarksFilename = std::string(ctx.arg.optRemarksFilename); |
| 122 | c.RemarksPasses = std::string(ctx.arg.optRemarksPasses); |
| 123 | c.RemarksWithHotness = ctx.arg.optRemarksWithHotness; |
| 124 | c.RemarksHotnessThreshold = ctx.arg.optRemarksHotnessThreshold; |
| 125 | c.RemarksFormat = std::string(ctx.arg.optRemarksFormat); |
Rui Ueyama | 397dffd | 2018-05-07 23:24:07 | [diff] [blame] | 126 | |
wangliushuai | 1c04b52 | 2022-03-17 03:53:44 | [diff] [blame] | 127 | // Set up output file to emit statistics. |
Fangrui Song | 6b56a27 | 2024-09-21 17:11:37 | [diff] [blame] | 128 | c.StatsFile = std::string(ctx.arg.optStatsFilename); |
wangliushuai | 1c04b52 | 2022-03-17 03:53:44 | [diff] [blame] | 129 | |
Fangrui Song | 6b56a27 | 2024-09-21 17:11:37 | [diff] [blame] | 130 | c.SampleProfile = std::string(ctx.arg.ltoSampleProfile); |
| 131 | for (StringRef pluginFn : ctx.arg.passPlugins) |
Jakob Koschel | 0c86198 | 2022-03-24 06:52:16 | [diff] [blame] | 132 | c.PassPlugins.push_back(std::string(pluginFn)); |
Fangrui Song | 6b56a27 | 2024-09-21 17:11:37 | [diff] [blame] | 133 | c.DebugPassManager = ctx.arg.ltoDebugPassManager; |
| 134 | c.DwoDir = std::string(ctx.arg.dwoDir); |
Davide Italiano | db4b0a7 | 2017-02-13 17:49:18 | [diff] [blame] | 135 | |
Fangrui Song | 6b56a27 | 2024-09-21 17:11:37 | [diff] [blame] | 136 | c.HasWholeProgramVisibility = ctx.arg.ltoWholeProgramVisibility; |
modimo | 272bd6f | 2023-07-14 02:02:52 | [diff] [blame] | 137 | c.ValidateAllVtablesHaveTypeInfos = |
Fangrui Song | 6b56a27 | 2024-09-21 17:11:37 | [diff] [blame] | 138 | ctx.arg.ltoValidateAllVtablesHaveTypeInfos; |
modimo | 272bd6f | 2023-07-14 02:02:52 | [diff] [blame] | 139 | c.AllVtablesHaveTypeInfos = ctx.ltoAllVtablesHaveTypeInfos; |
Fangrui Song | 6b56a27 | 2024-09-21 17:11:37 | [diff] [blame] | 140 | c.AlwaysEmitRegularLTOObj = !ctx.arg.ltoObjPath.empty(); |
Mingming Liu | 09b231c | 2024-09-09 18:16:58 | [diff] [blame] | 141 | c.KeepSymbolNameCopies = false; |
Teresa Johnson | 2f63d54 | 2020-01-24 20:24:18 | [diff] [blame] | 142 | |
Fangrui Song | 6b56a27 | 2024-09-21 17:11:37 | [diff] [blame] | 143 | for (const llvm::StringRef &name : ctx.arg.thinLTOModulesToCompile) |
Hongtao Yu | 2638aaf | 2020-05-21 20:19:44 | [diff] [blame] | 144 | c.ThinLTOModulesToCompile.emplace_back(name); |
| 145 | |
Fangrui Song | 6b56a27 | 2024-09-21 17:11:37 | [diff] [blame] | 146 | c.TimeTraceEnabled = ctx.arg.timeTraceEnabled; |
| 147 | c.TimeTraceGranularity = ctx.arg.timeTraceGranularity; |
Russell Gallop | e7cb374 | 2020-01-28 16:05:13 | [diff] [blame] | 148 | |
Fangrui Song | 6b56a27 | 2024-09-21 17:11:37 | [diff] [blame] | 149 | c.CSIRProfile = std::string(ctx.arg.ltoCSProfileFile); |
| 150 | c.RunCSIRInstr = ctx.arg.ltoCSProfileGenerate; |
| 151 | c.PGOWarnMismatch = ctx.arg.ltoPGOWarnMismatch; |
Rong Xu | f92e59c | 2019-03-11 22:51:38 | [diff] [blame] | 152 | |
Fangrui Song | 6b56a27 | 2024-09-21 17:11:37 | [diff] [blame] | 153 | if (ctx.arg.emitLLVM) { |
| 154 | c.PreCodeGenModuleHook = [&ctx](size_t task, const Module &m) { |
Jianzhou Zhao | 1120131 | 2020-09-12 19:35:17 | [diff] [blame] | 155 | if (std::unique_ptr<raw_fd_ostream> os = |
Fangrui Song | 6b56a27 | 2024-09-21 17:11:37 | [diff] [blame] | 156 | openLTOOutputFile(ctx.arg.outputFile)) |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 157 | WriteBitcodeToFile(m, *os, false); |
Rui Ueyama | 9f49990 | 2018-12-14 21:58:49 | [diff] [blame] | 158 | return false; |
| 159 | }; |
| 160 | } |
| 161 | |
Fangrui Song | 6b56a27 | 2024-09-21 17:11:37 | [diff] [blame] | 162 | if (ctx.arg.ltoEmitAsm) { |
Arthur Eubanks | 0a1aa6c | 2023-09-14 21:10:14 | [diff] [blame] | 163 | c.CGFileType = CodeGenFileType::AssemblyFile; |
Pierre van Houtryve | 6aff41e | 2023-01-09 10:33:38 | [diff] [blame] | 164 | c.Options.MCOptions.AsmVerbose = true; |
| 165 | } |
Hongtao Yu | 964ef8e | 2020-04-01 17:01:23 | [diff] [blame] | 166 | |
Fangrui Song | 6b56a27 | 2024-09-21 17:11:37 | [diff] [blame] | 167 | if (!ctx.arg.saveTempsArgs.empty()) |
Fangrui Song | 8f238f6 | 2024-11-17 06:28:54 | [diff] [blame] | 168 | checkError(ctx.e, c.addSaveTemps(ctx.arg.outputFile.str() + ".", |
| 169 | /*UseInputModulePath*/ true, |
| 170 | ctx.arg.saveTempsArgs)); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 171 | return c; |
Rui Ueyama | 397dffd | 2018-05-07 23:24:07 | [diff] [blame] | 172 | } |
Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 | [diff] [blame] | 173 | |
Fangrui Song | 6b56a27 | 2024-09-21 17:11:37 | [diff] [blame] | 174 | BitcodeCompiler::BitcodeCompiler(Ctx &ctx) : ctx(ctx) { |
Fangrui Song | 47cfe8f | 2019-07-16 05:50:45 | [diff] [blame] | 175 | // Initialize indexFile. |
Fangrui Song | 6b56a27 | 2024-09-21 17:11:37 | [diff] [blame] | 176 | if (!ctx.arg.thinLTOIndexOnlyArg.empty()) |
| 177 | indexFile = openFile(ctx.arg.thinLTOIndexOnlyArg); |
Rui Ueyama | 98e4a5c | 2018-09-11 14:37:27 | [diff] [blame] | 178 | |
Fangrui Song | 47cfe8f | 2019-07-16 05:50:45 | [diff] [blame] | 179 | // Initialize ltoObj. |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 180 | lto::ThinBackend backend; |
Jin Xin Ng | 22f1273 | 2022-06-01 17:49:36 | [diff] [blame] | 181 | auto onIndexWrite = [&](StringRef s) { thinIndices.erase(s); }; |
Fangrui Song | 6b56a27 | 2024-09-21 17:11:37 | [diff] [blame] | 182 | if (ctx.arg.thinLTOIndexOnly) { |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 183 | backend = lto::createWriteIndexesThinBackend( |
Nuri Amari | 2edd897 | 2024-10-07 15:16:46 | [diff] [blame] | 184 | llvm::hardware_concurrency(ctx.arg.thinLTOJobs), |
Fangrui Song | 6b56a27 | 2024-09-21 17:11:37 | [diff] [blame] | 185 | std::string(ctx.arg.thinLTOPrefixReplaceOld), |
| 186 | std::string(ctx.arg.thinLTOPrefixReplaceNew), |
| 187 | std::string(ctx.arg.thinLTOPrefixReplaceNativeObject), |
| 188 | ctx.arg.thinLTOEmitImportsFiles, indexFile.get(), onIndexWrite); |
Alexandre Ganea | 0915825 | 2020-03-27 14:20:39 | [diff] [blame] | 189 | } else { |
| 190 | backend = lto::createInProcessThinBackend( |
Fangrui Song | 6b56a27 | 2024-09-21 17:11:37 | [diff] [blame] | 191 | llvm::heavyweight_hardware_concurrency(ctx.arg.thinLTOJobs), |
| 192 | onIndexWrite, ctx.arg.thinLTOEmitIndexFiles, |
| 193 | ctx.arg.thinLTOEmitImportsFiles); |
Rumeet Dhindsa | d366e36 | 2018-05-02 21:40:07 | [diff] [blame] | 194 | } |
| 195 | |
Matthew Voss | ab9b3c8 | 2023-07-18 23:13:58 | [diff] [blame] | 196 | constexpr llvm::lto::LTO::LTOKind ltoModes[3] = |
| 197 | {llvm::lto::LTO::LTOKind::LTOK_UnifiedThin, |
| 198 | llvm::lto::LTO::LTOKind::LTOK_UnifiedRegular, |
| 199 | llvm::lto::LTO::LTOKind::LTOK_Default}; |
Fangrui Song | 6b56a27 | 2024-09-21 17:11:37 | [diff] [blame] | 200 | ltoObj = std::make_unique<lto::LTO>(createConfig(ctx), backend, |
| 201 | ctx.arg.ltoPartitions, |
| 202 | ltoModes[ctx.arg.ltoKind]); |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 | [diff] [blame] | 203 | |
Fangrui Song | 47cfe8f | 2019-07-16 05:50:45 | [diff] [blame] | 204 | // Initialize usedStartStop. |
Fangrui Song | 34fa860 | 2022-10-01 19:06:33 | [diff] [blame] | 205 | if (ctx.bitcodeFiles.empty()) |
Fangrui Song | 33319dd | 2021-12-23 09:52:54 | [diff] [blame] | 206 | return; |
Fangrui Song | df0864e | 2024-09-23 17:33:43 | [diff] [blame] | 207 | for (Symbol *sym : ctx.symtab->getSymbols()) { |
Fangrui Song | e9262ed | 2021-12-27 02:11:45 | [diff] [blame] | 208 | if (sym->isPlaceholder()) |
| 209 | continue; |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 210 | StringRef s = sym->getName(); |
| 211 | for (StringRef prefix : {"__start_", "__stop_"}) |
Fangrui Song | 8d85c96 | 2023-06-05 21:36:19 | [diff] [blame] | 212 | if (s.starts_with(prefix)) |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 213 | usedStartStop.insert(s.substr(prefix.size())); |
Fangrui Song | a2fc964 | 2019-11-20 19:16:15 | [diff] [blame] | 214 | } |
Rafael Espindola | 4b075bb | 2017-07-26 23:39:10 | [diff] [blame] | 215 | } |
Rafael Espindola | ae605c1 | 2016-04-21 20:35:25 | [diff] [blame] | 216 | |
Eugene Zelenko | 22886a2 | 2016-11-05 01:00:56 | [diff] [blame] | 217 | BitcodeCompiler::~BitcodeCompiler() = default; |
Rui Ueyama | 412c802b | 2016-04-22 21:16:18 | [diff] [blame] | 218 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 219 | void BitcodeCompiler::add(BitcodeFile &f) { |
| 220 | lto::InputFile &obj = *f.obj; |
Fangrui Song | a7e14e2 | 2024-09-22 05:54:37 | [diff] [blame] | 221 | bool isExec = !ctx.arg.shared && !ctx.arg.relocatable; |
Rumeet Dhindsa | d366e36 | 2018-05-02 21:40:07 | [diff] [blame] | 222 | |
Fangrui Song | a7e14e2 | 2024-09-22 05:54:37 | [diff] [blame] | 223 | if (ctx.arg.thinLTOEmitIndexFiles) |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 224 | thinIndices.insert(obj.getName()); |
Rumeet Dhindsa | d366e36 | 2018-05-02 21:40:07 | [diff] [blame] | 225 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 226 | ArrayRef<Symbol *> syms = f.getSymbols(); |
| 227 | ArrayRef<lto::InputFile::Symbol> objSyms = obj.symbols(); |
| 228 | std::vector<lto::SymbolResolution> resols(syms.size()); |
Davide Italiano | 334fce9 | 2016-05-11 01:07:22 | [diff] [blame] | 229 | |
Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 | [diff] [blame] | 230 | // Provide a resolution to the LTO API for each symbol. |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 231 | for (size_t i = 0, e = syms.size(); i != e; ++i) { |
| 232 | Symbol *sym = syms[i]; |
| 233 | const lto::InputFile::Symbol &objSym = objSyms[i]; |
| 234 | lto::SymbolResolution &r = resols[i]; |
Davide Italiano | 86f2bd5 | 2016-03-29 21:46:35 | [diff] [blame] | 235 | |
Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 | [diff] [blame] | 236 | // Ideally we shouldn't check for SF_Undefined but currently IRObjectFile |
| 237 | // reports two symbols for module ASM defined. Without this check, lld |
| 238 | // flags an undefined in IR with a definition in ASM as prevailing. |
| 239 | // Once IRObjectFile is fixed to report only one symbol this hack can |
| 240 | // be removed. |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 241 | r.Prevailing = !objSym.isUndefined() && sym->file == &f; |
Peter Collingbourne | 3ad1c1e | 2016-05-05 17:13:49 | [diff] [blame] | 242 | |
George Rimar | 3a1af22 | 2017-08-22 08:36:54 | [diff] [blame] | 243 | // We ask LTO to preserve following global symbols: |
| 244 | // 1) All symbols when doing relocatable link, so that them can be used |
| 245 | // for doing final link. |
| 246 | // 2) Symbols that are used in regular objects. |
| 247 | // 3) C named sections if we have corresponding __start_/__stop_ symbol. |
Shoaib Meenai | 1af25a9 | 2022-04-20 01:04:17 | [diff] [blame] | 248 | // 4) Symbols that are defined in bitcode files and used for dynamic |
| 249 | // linking. |
| 250 | // 5) Symbols that will be referenced after linker wrapping is performed. |
Fangrui Song | a7e14e2 | 2024-09-22 05:54:37 | [diff] [blame] | 251 | r.VisibleToRegularObj = ctx.arg.relocatable || sym->isUsedInRegularObj || |
Shoaib Meenai | 1af25a9 | 2022-04-20 01:04:17 | [diff] [blame] | 252 | sym->referencedAfterWrap || |
Fangrui Song | 3733ed6 | 2024-12-09 06:40:14 | [diff] [blame] | 253 | (r.Prevailing && sym->isExported) || |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 254 | usedStartStop.count(objSym.getSectionName()); |
Teresa Johnson | 1487747 | 2020-12-30 23:56:53 | [diff] [blame] | 255 | // Identify symbols exported dynamically, and that therefore could be |
| 256 | // referenced by a shared library not visible to the linker. |
Fangrui Song | aedc81b | 2024-12-08 17:17:38 | [diff] [blame] | 257 | r.ExportDynamic = sym->computeBinding(ctx) != STB_LOCAL && |
Fangrui Song | 3733ed6 | 2024-12-09 06:40:14 | [diff] [blame] | 258 | (ctx.arg.exportDynamic || sym->isExported); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 259 | const auto *dr = dyn_cast<Defined>(sym); |
| 260 | r.FinalDefinitionInLinkageUnit = |
Fangrui Song | 82ed93ea | 2022-09-05 00:27:35 | [diff] [blame] | 261 | (isExec || sym->visibility() != STV_DEFAULT) && dr && |
Dmitry Mikulin | c84e0ee | 2018-02-07 00:49:51 | [diff] [blame] | 262 | // Skip absolute symbols from ELF objects, otherwise PC-rel relocations |
| 263 | // will be generated by for them, triggering linker errors. |
| 264 | // Symbol section is always null for bitcode symbols, hence the check |
Dmitry Mikulin | 8ddd922 | 2018-02-08 04:25:52 | [diff] [blame] | 265 | // for isElf(). Skip linker script defined symbols as well: they have |
| 266 | // no File defined. |
Fangrui Song | 43b1334 | 2024-01-22 17:09:46 | [diff] [blame] | 267 | !(dr->section == nullptr && |
| 268 | (sym->file->isInternal() || sym->file->isElf())); |
Rafael Espindola | c6df38c | 2018-01-16 16:49:05 | [diff] [blame] | 269 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 270 | if (r.Prevailing) |
Fangrui Song | 43b1334 | 2024-01-22 17:09:46 | [diff] [blame] | 271 | Undefined(ctx.internalFile, StringRef(), STB_GLOBAL, STV_DEFAULT, |
| 272 | sym->type) |
Fangrui Song | 7a58dd1 | 2022-09-28 20:11:31 | [diff] [blame] | 273 | .overwrite(*sym); |
George Rimar | d28c26b | 2017-09-25 09:31:43 | [diff] [blame] | 274 | |
George Rimar | c4ccfb5 | 2018-01-30 09:04:27 | [diff] [blame] | 275 | // We tell LTO to not apply interprocedural optimization for wrapped |
| 276 | // (with --wrap) symbols because otherwise LTO would inline them while |
| 277 | // their values are still not final. |
Fangrui Song | 7c67592 | 2022-02-05 20:00:34 | [diff] [blame] | 278 | r.LinkerRedefined = sym->scriptDefined; |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 | [diff] [blame] | 279 | } |
Fangrui Song | 8f238f6 | 2024-11-17 06:28:54 | [diff] [blame] | 280 | checkError(ctx.e, ltoObj->add(std::move(f.obj), resols)); |
Davide Italiano | bc17663 | 2016-04-15 22:38:10 | [diff] [blame] | 281 | } |
| 282 | |
Fangrui Song | 0178cff | 2019-05-02 14:05:20 | [diff] [blame] | 283 | // If LazyObjFile has not been added to link, emit empty index files. |
| 284 | // This is needed because this is what GNU gold plugin does and we have a |
| 285 | // distributed build system that depends on that behavior. |
Fangrui Song | bab5d5b | 2024-09-29 21:45:00 | [diff] [blame] | 286 | static void thinLTOCreateEmptyIndexFiles(Ctx &ctx) { |
Wei Wang | 5529a75 | 2022-09-13 00:35:51 | [diff] [blame] | 287 | DenseSet<StringRef> linkedBitCodeFiles; |
Fangrui Song | 34fa860 | 2022-10-01 19:06:33 | [diff] [blame] | 288 | for (BitcodeFile *f : ctx.bitcodeFiles) |
Wei Wang | 5529a75 | 2022-09-13 00:35:51 | [diff] [blame] | 289 | linkedBitCodeFiles.insert(f->getName()); |
| 290 | |
Fangrui Song | 34fa860 | 2022-10-01 19:06:33 | [diff] [blame] | 291 | for (BitcodeFile *f : ctx.lazyBitcodeFiles) { |
Fangrui Song | 3a5fb57 | 2021-12-23 01:41:50 | [diff] [blame] | 292 | if (!f->lazy) |
Fangrui Song | 0178cff | 2019-05-02 14:05:20 | [diff] [blame] | 293 | continue; |
Wei Wang | 5529a75 | 2022-09-13 00:35:51 | [diff] [blame] | 294 | if (linkedBitCodeFiles.contains(f->getName())) |
| 295 | continue; |
Aiden Grossman | f741815 | 2022-10-01 06:24:32 | [diff] [blame] | 296 | std::string path = |
Fangrui Song | c4c34f0 | 2024-10-04 06:06:18 | [diff] [blame] | 297 | replaceThinLTOSuffix(ctx, getThinLTOOutputFile(ctx, f->obj->getName())); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 298 | std::unique_ptr<raw_fd_ostream> os = openFile(path + ".thinlto.bc"); |
| 299 | if (!os) |
Fangrui Song | 0178cff | 2019-05-02 14:05:20 | [diff] [blame] | 300 | continue; |
Rui Ueyama | f06d494 | 2018-05-17 18:27:12 | [diff] [blame] | 301 | |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 302 | ModuleSummaryIndex m(/*HaveGVs*/ false); |
| 303 | m.setSkipModuleByDistributedBackend(); |
Fangrui Song | 7aaf024 | 2022-02-01 00:46:11 | [diff] [blame] | 304 | writeIndexToFile(m, *os); |
Fangrui Song | a7e14e2 | 2024-09-22 05:54:37 | [diff] [blame] | 305 | if (ctx.arg.thinLTOEmitImportsFiles) |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 306 | openFile(path + ".imports"); |
Fangrui Song | 0178cff | 2019-05-02 14:05:20 | [diff] [blame] | 307 | } |
Rui Ueyama | f06d494 | 2018-05-17 18:27:12 | [diff] [blame] | 308 | } |
| 309 | |
Rui Ueyama | 2599248 | 2016-03-22 20:52:10 | [diff] [blame] | 310 | // Merge all the bitcode files we have seen, codegen the result |
Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 | [diff] [blame] | 311 | // and return the resulting ObjectFile(s). |
Fangrui Song | dbf37e9 | 2024-11-17 07:50:34 | [diff] [blame] | 312 | SmallVector<std::unique_ptr<InputFile>, 0> BitcodeCompiler::compile() { |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 313 | unsigned maxTasks = ltoObj->getMaxTasks(); |
| 314 | buf.resize(maxTasks); |
| 315 | files.resize(maxTasks); |
Fangrui Song | f7669ba | 2024-01-23 19:38:15 | [diff] [blame] | 316 | filenames.resize(maxTasks); |
Davide Italiano | 828ac541 | 2016-03-28 15:44:21 | [diff] [blame] | 317 | |
Peter Collingbourne | e02775f | 2017-03-01 23:00:10 | [diff] [blame] | 318 | // The --thinlto-cache-dir option specifies the path to a directory in which |
| 319 | // to cache native object files for ThinLTO incremental builds. If a path was |
| 320 | // specified, configure LTO to use it as the cache directory. |
Noah Shutty | d788c44 | 2021-11-04 19:59:59 | [diff] [blame] | 321 | FileCache cache; |
Fangrui Song | 6b56a27 | 2024-09-21 17:11:37 | [diff] [blame] | 322 | if (!ctx.arg.thinLTOCacheDir.empty()) |
| 323 | cache = check(localCache("ThinLTO", "Thin", ctx.arg.thinLTOCacheDir, |
Zequan Wu | 84be92d | 2022-11-22 21:46:42 | [diff] [blame] | 324 | [&](size_t task, const Twine &moduleName, |
| 325 | std::unique_ptr<MemoryBuffer> mb) { |
| 326 | files[task] = std::move(mb); |
Fangrui Song | f7669ba | 2024-01-23 19:38:15 | [diff] [blame] | 327 | filenames[task] = moduleName.str(); |
Zequan Wu | 84be92d | 2022-11-22 21:46:42 | [diff] [blame] | 328 | })); |
Peter Collingbourne | e02775f | 2017-03-01 23:00:10 | [diff] [blame] | 329 | |
Fangrui Song | 34fa860 | 2022-10-01 19:06:33 | [diff] [blame] | 330 | if (!ctx.bitcodeFiles.empty()) |
Fangrui Song | 8f238f6 | 2024-11-17 06:28:54 | [diff] [blame] | 331 | checkError(ctx.e, ltoObj->run( |
| 332 | [&](size_t task, const Twine &moduleName) { |
| 333 | buf[task].first = moduleName.str(); |
| 334 | return std::make_unique<CachedFileStream>( |
| 335 | std::make_unique<raw_svector_ostream>( |
| 336 | buf[task].second)); |
| 337 | }, |
| 338 | cache)); |
Rafael Espindola | abf6c65 | 2016-04-17 23:20:08 | [diff] [blame] | 339 | |
Hongtao Yu | 2638aaf | 2020-05-21 20:19:44 | [diff] [blame] | 340 | // Emit empty index files for non-indexed files but not in single-module mode. |
Fangrui Song | 6b56a27 | 2024-09-21 17:11:37 | [diff] [blame] | 341 | if (ctx.arg.thinLTOModulesToCompile.empty()) { |
Hongtao Yu | 2638aaf | 2020-05-21 20:19:44 | [diff] [blame] | 342 | for (StringRef s : thinIndices) { |
Fangrui Song | 6b56a27 | 2024-09-21 17:11:37 | [diff] [blame] | 343 | std::string path = getThinLTOOutputFile(ctx, s); |
Hongtao Yu | 2638aaf | 2020-05-21 20:19:44 | [diff] [blame] | 344 | openFile(path + ".thinlto.bc"); |
Fangrui Song | 6b56a27 | 2024-09-21 17:11:37 | [diff] [blame] | 345 | if (ctx.arg.thinLTOEmitImportsFiles) |
Hongtao Yu | 2638aaf | 2020-05-21 20:19:44 | [diff] [blame] | 346 | openFile(path + ".imports"); |
| 347 | } |
Davide Italiano | 786d8e3 | 2016-09-29 00:40:08 | [diff] [blame] | 348 | } |
Peter Collingbourne | e02775f | 2017-03-01 23:00:10 | [diff] [blame] | 349 | |
Fangrui Song | 6b56a27 | 2024-09-21 17:11:37 | [diff] [blame] | 350 | if (ctx.arg.thinLTOEmitIndexFiles) |
Fangrui Song | bab5d5b | 2024-09-29 21:45:00 | [diff] [blame] | 351 | thinLTOCreateEmptyIndexFiles(ctx); |
Rui Ueyama | d54f1c2 | 2018-05-07 22:11:24 | [diff] [blame] | 352 | |
Fangrui Song | 6b56a27 | 2024-09-21 17:11:37 | [diff] [blame] | 353 | if (ctx.arg.thinLTOIndexOnly) { |
| 354 | if (!ctx.arg.ltoObjPath.empty()) |
| 355 | saveBuffer(buf[0].second, ctx.arg.ltoObjPath); |
Rumeet Dhindsa | 1888326 | 2018-05-08 20:12:07 | [diff] [blame] | 356 | |
Rui Ueyama | d54f1c2 | 2018-05-07 22:11:24 | [diff] [blame] | 357 | // ThinLTO with index only option is required to generate only the index |
| 358 | // files. After that, we exit from linker and ThinLTO backend runs in a |
| 359 | // distributed environment. |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 360 | if (indexFile) |
| 361 | indexFile->close(); |
Rui Ueyama | 554adb2 | 2018-05-07 22:11:34 | [diff] [blame] | 362 | return {}; |
Rui Ueyama | d54f1c2 | 2018-05-07 22:11:24 | [diff] [blame] | 363 | } |
Rui Ueyama | f06d494 | 2018-05-17 18:27:12 | [diff] [blame] | 364 | |
Fangrui Song | 6b56a27 | 2024-09-21 17:11:37 | [diff] [blame] | 365 | if (!ctx.arg.thinLTOCacheDir.empty()) |
| 366 | pruneCache(ctx.arg.thinLTOCacheDir, ctx.arg.thinLTOCachePolicy, files); |
Rumeet Dhindsa | 1888326 | 2018-05-08 20:12:07 | [diff] [blame] | 367 | |
Fangrui Song | 6b56a27 | 2024-09-21 17:11:37 | [diff] [blame] | 368 | if (!ctx.arg.ltoObjPath.empty()) { |
| 369 | saveBuffer(buf[0].second, ctx.arg.ltoObjPath); |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 370 | for (unsigned i = 1; i != maxTasks; ++i) |
Fangrui Song | 6b56a27 | 2024-09-21 17:11:37 | [diff] [blame] | 371 | saveBuffer(buf[i].second, ctx.arg.ltoObjPath + Twine(i)); |
Rumeet Dhindsa | 1888326 | 2018-05-08 20:12:07 | [diff] [blame] | 372 | } |
Rumeet Dhindsa | d366e36 | 2018-05-02 21:40:07 | [diff] [blame] | 373 | |
Fangrui Song | 6b56a27 | 2024-09-21 17:11:37 | [diff] [blame] | 374 | bool savePrelink = ctx.arg.saveTempsArgs.contains("prelink"); |
Fangrui Song | dbf37e9 | 2024-11-17 07:50:34 | [diff] [blame] | 375 | SmallVector<std::unique_ptr<InputFile>, 0> ret; |
Fangrui Song | 6b56a27 | 2024-09-21 17:11:37 | [diff] [blame] | 376 | const char *ext = ctx.arg.ltoEmitAsm ? ".s" : ".o"; |
Fangrui Song | f7669ba | 2024-01-23 19:38:15 | [diff] [blame] | 377 | for (unsigned i = 0; i != maxTasks; ++i) { |
| 378 | StringRef bitcodeFilePath; |
| 379 | StringRef objBuf; |
| 380 | if (files[i]) { |
| 381 | // When files[i] is not null, we get the native relocatable file from the |
| 382 | // cache. filenames[i] contains the original BitcodeFile's identifier. |
| 383 | objBuf = files[i]->getBuffer(); |
| 384 | bitcodeFilePath = filenames[i]; |
| 385 | } else { |
| 386 | // Get the native relocatable file after in-process LTO compilation. |
| 387 | objBuf = buf[i].second; |
| 388 | bitcodeFilePath = buf[i].first; |
| 389 | } |
| 390 | if (objBuf.empty()) |
| 391 | continue; |
Bill Wendling | 01706bd | 2019-02-26 19:29:14 | [diff] [blame] | 392 | |
Fangrui Song | f7669ba | 2024-01-23 19:38:15 | [diff] [blame] | 393 | // If the input bitcode file is path/to/x.o and -o specifies a.out, the |
| 394 | // corresponding native relocatable file path will look like: |
| 395 | // path/to/a.out.lto.x.o. |
| 396 | StringRef ltoObjName; |
| 397 | if (bitcodeFilePath == "ld-temp.o") { |
| 398 | ltoObjName = |
Fangrui Song | 2991a4e | 2024-11-17 06:34:12 | [diff] [blame] | 399 | ctx.saver.save(Twine(ctx.arg.outputFile) + ".lto" + |
| 400 | (i == 0 ? Twine("") : Twine('.') + Twine(i)) + ext); |
Fangrui Song | f7669ba | 2024-01-23 19:38:15 | [diff] [blame] | 401 | } else { |
| 402 | StringRef directory = sys::path::parent_path(bitcodeFilePath); |
| 403 | // For an archive member, which has an identifier like "d/a.a(coll.o at |
| 404 | // 8)" (see BitcodeFile::BitcodeFile), use the filename; otherwise, use |
| 405 | // the stem (d/a.o => a). |
| 406 | StringRef baseName = bitcodeFilePath.ends_with(")") |
| 407 | ? sys::path::filename(bitcodeFilePath) |
| 408 | : sys::path::stem(bitcodeFilePath); |
Fangrui Song | 6b56a27 | 2024-09-21 17:11:37 | [diff] [blame] | 409 | StringRef outputFileBaseName = sys::path::filename(ctx.arg.outputFile); |
Fangrui Song | f7669ba | 2024-01-23 19:38:15 | [diff] [blame] | 410 | SmallString<256> path; |
| 411 | sys::path::append(path, directory, |
| 412 | outputFileBaseName + ".lto." + baseName + ext); |
| 413 | sys::path::remove_dots(path, true); |
Fangrui Song | 2991a4e | 2024-11-17 06:34:12 | [diff] [blame] | 414 | ltoObjName = ctx.saver.save(path.str()); |
Fangrui Song | f7669ba | 2024-01-23 19:38:15 | [diff] [blame] | 415 | } |
Fangrui Song | 6b56a27 | 2024-09-21 17:11:37 | [diff] [blame] | 416 | if (savePrelink || ctx.arg.ltoEmitAsm) |
Fangrui Song | f7669ba | 2024-01-23 19:38:15 | [diff] [blame] | 417 | saveBuffer(buf[i].second, ltoObjName); |
Fangrui Song | 6b56a27 | 2024-09-21 17:11:37 | [diff] [blame] | 418 | if (!ctx.arg.ltoEmitAsm) |
Fangrui Song | b672071 | 2024-10-07 01:09:52 | [diff] [blame] | 419 | ret.push_back(createObjFile(ctx, MemoryBufferRef(objBuf, ltoObjName))); |
Fangrui Song | f7669ba | 2024-01-23 19:38:15 | [diff] [blame] | 420 | } |
Rui Ueyama | 3837f42 | 2019-07-10 05:00:37 | [diff] [blame] | 421 | return ret; |
Rui Ueyama | 961f2ff | 2016-03-23 21:19:27 | [diff] [blame] | 422 | } |