|
16 | 16 | #ifndef LLVM_LTO_LTO_H
|
17 | 17 | #define LLVM_LTO_LTO_H
|
18 | 18 |
|
| 19 | +#include "llvm/ADT/MapVector.h" |
19 | 20 | #include "llvm/ADT/StringMap.h"
|
| 21 | +#include "llvm/ADT/StringSet.h" |
| 22 | +#include "llvm/CodeGen/Analysis.h" |
| 23 | +#include "llvm/IR/DiagnosticInfo.h" |
20 | 24 | #include "llvm/IR/ModuleSummaryIndex.h"
|
| 25 | +#include "llvm/LTO/Config.h" |
| 26 | +#include "llvm/Linker/IRMover.h" |
| 27 | +#include "llvm/Object/IRObjectFile.h" |
| 28 | +#include "llvm/Support/thread.h" |
| 29 | +#include "llvm/Target/TargetOptions.h" |
| 30 | +#include "llvm/Transforms/IPO/FunctionImport.h" |
21 | 31 |
|
22 | 32 | namespace llvm {
|
23 | 33 |
|
| 34 | +class Error; |
24 | 35 | class LLVMContext;
|
25 | 36 | class MemoryBufferRef;
|
26 | 37 | class Module;
|
| 38 | +class Target; |
| 39 | +class raw_pwrite_stream; |
27 | 40 |
|
28 | 41 | /// Helper to load a module from bitcode.
|
29 | 42 | std::unique_ptr<Module> loadModuleFromBuffer(const MemoryBufferRef &Buffer,
|
@@ -69,6 +82,319 @@ void thinLTOResolveWeakForLinkerInIndex(
|
69 | 82 | void thinLTOInternalizeAndPromoteInIndex(
|
70 | 83 | ModuleSummaryIndex &Index,
|
71 | 84 | function_ref<bool(StringRef, GlobalValue::GUID)> isExported);
|
72 |
| -} |
| 85 | + |
| 86 | +namespace lto { |
| 87 | + |
| 88 | +class LTO; |
| 89 | +struct SymbolResolution; |
| 90 | +class ThinBackendProc; |
| 91 | + |
| 92 | +/// An input file. This is a wrapper for IRObjectFile that exposes only the |
| 93 | +/// information that an LTO client should need in order to do symbol resolution. |
| 94 | +class InputFile { |
| 95 | + // FIXME: Remove LTO class friendship once we have bitcode symbol tables. |
| 96 | + friend LTO; |
| 97 | + InputFile() = default; |
| 98 | + |
| 99 | + // FIXME: Remove the LLVMContext once we have bitcode symbol tables. |
| 100 | + LLVMContext Ctx; |
| 101 | + std::unique_ptr<object::IRObjectFile> Obj; |
| 102 | + |
| 103 | +public: |
| 104 | + /// Create an InputFile. |
| 105 | + static Expected<std::unique_ptr<InputFile>> create(MemoryBufferRef Object); |
| 106 | + |
| 107 | + class symbol_iterator; |
| 108 | + |
| 109 | + /// This is a wrapper for object::basic_symbol_iterator that exposes only the |
| 110 | + /// information that an LTO client should need in order to do symbol |
| 111 | + /// resolution. |
| 112 | + /// |
| 113 | + /// This object is ephemeral; it is only valid as long as an iterator obtained |
| 114 | + /// from symbols() refers to it. |
| 115 | + class Symbol { |
| 116 | + friend symbol_iterator; |
| 117 | + friend LTO; |
| 118 | + |
| 119 | + object::basic_symbol_iterator I; |
| 120 | + const GlobalValue *GV; |
| 121 | + uint32_t Flags; |
| 122 | + SmallString<64> Name; |
| 123 | + |
| 124 | + bool shouldSkip() { |
| 125 | + return !(Flags & object::BasicSymbolRef::SF_Global) || |
| 126 | + (Flags & object::BasicSymbolRef::SF_FormatSpecific); |
| 127 | + } |
| 128 | + |
| 129 | + void skip() { |
| 130 | + const object::SymbolicFile *Obj = I->getObject(); |
| 131 | + auto E = Obj->symbol_end(); |
| 132 | + while (I != E) { |
| 133 | + Flags = I->getFlags(); |
| 134 | + if (!shouldSkip()) |
| 135 | + break; |
| 136 | + ++I; |
| 137 | + } |
| 138 | + if (I == E) |
| 139 | + return; |
| 140 | + |
| 141 | + Name.clear(); |
| 142 | + { |
| 143 | + raw_svector_ostream OS(Name); |
| 144 | + I->printName(OS); |
| 145 | + } |
| 146 | + GV = cast<object::IRObjectFile>(Obj)->getSymbolGV(I->getRawDataRefImpl()); |
| 147 | + } |
| 148 | + |
| 149 | + public: |
| 150 | + Symbol(object::basic_symbol_iterator I) : I(I) { skip(); } |
| 151 | + |
| 152 | + StringRef getName() const { return Name; } |
| 153 | + StringRef getIRName() const { |
| 154 | + if (GV) |
| 155 | + return GV->getName(); |
| 156 | + return StringRef(); |
| 157 | + } |
| 158 | + uint32_t getFlags() const { return Flags; } |
| 159 | + GlobalValue::VisibilityTypes getVisibility() const { |
| 160 | + if (GV) |
| 161 | + return GV->getVisibility(); |
| 162 | + return GlobalValue::DefaultVisibility; |
| 163 | + } |
| 164 | + bool canBeOmittedFromSymbolTable() const { |
| 165 | + return GV && llvm::canBeOmittedFromSymbolTable(GV); |
| 166 | + } |
| 167 | + Expected<const Comdat *> getComdat() const { |
| 168 | + const GlobalObject *GO; |
| 169 | + if (auto *GA = dyn_cast<GlobalAlias>(GV)) { |
| 170 | + GO = GA->getBaseObject(); |
| 171 | + if (!GO) |
| 172 | + return make_error<StringError>("Unable to determine comdat of alias!", |
| 173 | + inconvertibleErrorCode()); |
| 174 | + } else { |
| 175 | + GO = cast<GlobalObject>(GV); |
| 176 | + } |
| 177 | + if (GV) |
| 178 | + return GV->getComdat(); |
| 179 | + return nullptr; |
| 180 | + } |
| 181 | + size_t getCommonSize() const { |
| 182 | + assert(Flags & object::BasicSymbolRef::SF_Common); |
| 183 | + if (!GV) |
| 184 | + return 0; |
| 185 | + return GV->getParent()->getDataLayout().getTypeAllocSize( |
| 186 | + GV->getType()->getElementType()); |
| 187 | + } |
| 188 | + unsigned getCommonAlignment() const { |
| 189 | + assert(Flags & object::BasicSymbolRef::SF_Common); |
| 190 | + if (!GV) |
| 191 | + return 0; |
| 192 | + return GV->getAlignment(); |
| 193 | + } |
| 194 | + }; |
| 195 | + |
| 196 | + class symbol_iterator { |
| 197 | + Symbol Sym; |
| 198 | + |
| 199 | + public: |
| 200 | + symbol_iterator(object::basic_symbol_iterator I) : Sym(I) {} |
| 201 | + |
| 202 | + symbol_iterator &operator++() { |
| 203 | + ++Sym.I; |
| 204 | + Sym.skip(); |
| 205 | + return *this; |
| 206 | + } |
| 207 | + |
| 208 | + symbol_iterator operator++(int) { |
| 209 | + symbol_iterator I = *this; |
| 210 | + ++*this; |
| 211 | + return I; |
| 212 | + } |
| 213 | + |
| 214 | + const Symbol &operator*() const { return Sym; } |
| 215 | + const Symbol *operator->() const { return &Sym; } |
| 216 | + |
| 217 | + bool operator!=(const symbol_iterator &Other) const { |
| 218 | + return Sym.I != Other.Sym.I; |
| 219 | + } |
| 220 | + }; |
| 221 | + |
| 222 | + /// A range over the symbols in this InputFile. |
| 223 | + iterator_range<symbol_iterator> symbols() { |
| 224 | + return llvm::make_range(symbol_iterator(Obj->symbol_begin()), |
| 225 | + symbol_iterator(Obj->symbol_end())); |
| 226 | + } |
| 227 | + |
| 228 | + StringRef getSourceFileName() const { |
| 229 | + return Obj->getModule().getSourceFileName(); |
| 230 | + } |
| 231 | +}; |
| 232 | + |
| 233 | +/// A ThinBackend defines what happens after the thin-link phase during ThinLTO. |
| 234 | +/// The details of this type definition aren't important; clients can only |
| 235 | +/// create a ThinBackend using one of the create*ThinBackend() functions below. |
| 236 | +typedef std::function<std::unique_ptr<ThinBackendProc>( |
| 237 | + Config &C, ModuleSummaryIndex &CombinedIndex, |
| 238 | + StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries, |
| 239 | + AddStreamFn AddStream)> |
| 240 | + ThinBackend; |
| 241 | + |
| 242 | +/// This ThinBackend runs the individual backend jobs in-process. |
| 243 | +ThinBackend createInProcessThinBackend(unsigned ParallelismLevel); |
| 244 | + |
| 245 | +/// This ThinBackend writes individual module indexes to files, instead of |
| 246 | +/// running the individual backend jobs. This backend is for distributed builds |
| 247 | +/// where separate processes will invoke the real backends. |
| 248 | +/// |
| 249 | +/// To find the path to write the index to, the backend checks if the path has a |
| 250 | +/// prefix of OldPrefix; if so, it replaces that prefix with NewPrefix. It then |
| 251 | +/// appends ".thinlto.bc" and writes the index to that path. If |
| 252 | +/// ShouldEmitImportsFiles is true it also writes a list of imported files to a |
| 253 | +/// similar path with ".imports" appended instead. |
| 254 | +ThinBackend createWriteIndexesThinBackend(std::string OldPrefix, |
| 255 | + std::string NewPrefix, |
| 256 | + bool ShouldEmitImportsFiles, |
| 257 | + std::string LinkedObjectsFile); |
| 258 | + |
| 259 | +/// This class implements a resolution-based interface to LLVM's LTO |
| 260 | +/// functionality. It supports regular LTO, parallel LTO code generation and |
| 261 | +/// ThinLTO. You can use it from a linker in the following way: |
| 262 | +/// - Set hooks and code generation options (see lto::Config struct defined in |
| 263 | +/// Config.h), and use the lto::Config object to create an lto::LTO object. |
| 264 | +/// - Create lto::InputFile objects using lto::InputFile::create(), then use |
| 265 | +/// the symbols() function to enumerate its symbols and compute a resolution |
| 266 | +/// for each symbol (see SymbolResolution below). |
| 267 | +/// - After the linker has visited each input file (and each regular object |
| 268 | +/// file) and computed a resolution for each symbol, take each lto::InputFile |
| 269 | +/// and pass it and an array of symbol resolutions to the add() function. |
| 270 | +/// - Call the getMaxTasks() function to get an upper bound on the number of |
| 271 | +/// native object files that LTO may add to the link. |
| 272 | +/// - Call the run() function. This function will use the supplied AddStream |
| 273 | +/// function to add up to getMaxTasks() native object files to the link. |
| 274 | +class LTO { |
| 275 | + friend InputFile; |
| 276 | + |
| 277 | +public: |
| 278 | + /// Create an LTO object. A default constructed LTO object has a reasonable |
| 279 | + /// production configuration, but you can customize it by passing arguments to |
| 280 | + /// this constructor. |
| 281 | + /// FIXME: We do currently require the DiagHandler field to be set in Conf. |
| 282 | + /// Until that is fixed, a Config argument is required. |
| 283 | + LTO(Config Conf, ThinBackend Backend = nullptr, |
| 284 | + unsigned ParallelCodeGenParallelismLevel = 1); |
| 285 | + |
| 286 | + /// Add an input file to the LTO link, using the provided symbol resolutions. |
| 287 | + /// The symbol resolutions must appear in the enumeration order given by |
| 288 | + /// InputFile::symbols(). |
| 289 | + Error add(std::unique_ptr<InputFile> Obj, ArrayRef<SymbolResolution> Res); |
| 290 | + |
| 291 | + /// Returns an upper bound on the number of tasks that the client may expect. |
| 292 | + /// This may only be called after all IR object files have been added. For a |
| 293 | + /// full description of tasks see LTOBackend.h. |
| 294 | + size_t getMaxTasks() const; |
| 295 | + |
| 296 | + /// Runs the LTO pipeline. This function calls the supplied AddStream function |
| 297 | + /// to add native object files to the link. |
| 298 | + Error run(AddStreamFn AddStream); |
| 299 | + |
| 300 | +private: |
| 301 | + Config Conf; |
| 302 | + |
| 303 | + struct RegularLTOState { |
| 304 | + RegularLTOState(unsigned ParallelCodeGenParallelismLevel, Config &Conf); |
| 305 | + |
| 306 | + unsigned ParallelCodeGenParallelismLevel; |
| 307 | + LTOLLVMContext Ctx; |
| 308 | + bool HasModule = false; |
| 309 | + std::unique_ptr<Module> CombinedModule; |
| 310 | + IRMover Mover; |
| 311 | + } RegularLTO; |
| 312 | + |
| 313 | + struct ThinLTOState { |
| 314 | + ThinLTOState(ThinBackend Backend); |
| 315 | + |
| 316 | + ThinBackend Backend; |
| 317 | + ModuleSummaryIndex CombinedIndex; |
| 318 | + MapVector<StringRef, MemoryBufferRef> ModuleMap; |
| 319 | + DenseMap<GlobalValue::GUID, StringRef> PrevailingModuleForGUID; |
| 320 | + } ThinLTO; |
| 321 | + |
| 322 | + // The global resolution for a particular (mangled) symbol name. This is in |
| 323 | + // particular necessary to track whether each symbol can be internalized. |
| 324 | + // Because any input file may introduce a new cross-partition reference, we |
| 325 | + // cannot make any final internalization decisions until all input files have |
| 326 | + // been added and the client has called run(). During run() we apply |
| 327 | + // internalization decisions either directly to the module (for regular LTO) |
| 328 | + // or to the combined index (for ThinLTO). |
| 329 | + struct GlobalResolution { |
| 330 | + /// The unmangled name of the global. |
| 331 | + std::string IRName; |
| 332 | + |
| 333 | + bool UnnamedAddr = true; |
| 334 | + |
| 335 | + /// This field keeps track of the partition number of this global. The |
| 336 | + /// regular LTO object is partition 0, while each ThinLTO object has its own |
| 337 | + /// partition number from 1 onwards. |
| 338 | + /// |
| 339 | + /// Any global that is defined or used by more than one partition, or that |
| 340 | + /// is referenced externally, may not be internalized. |
| 341 | + /// |
| 342 | + /// Partitions generally have a one-to-one correspondence with tasks, except |
| 343 | + /// that we use partition 0 for all parallel LTO code generation partitions. |
| 344 | + /// Any partitioning of the combined LTO object is done internally by the |
| 345 | + /// LTO backend. |
| 346 | + size_t Partition = Unknown; |
| 347 | + |
| 348 | + /// Special partition numbers. |
| 349 | + enum { |
| 350 | + /// A partition number has not yet been assigned to this global. |
| 351 | + Unknown = -1ull, |
| 352 | + |
| 353 | + /// This global is either used by more than one partition or has an |
| 354 | + /// external reference, and therefore cannot be internalized. |
| 355 | + External = -2ull, |
| 356 | + }; |
| 357 | + }; |
| 358 | + |
| 359 | + // Global mapping from mangled symbol names to resolutions. |
| 360 | + StringMap<GlobalResolution> GlobalResolutions; |
| 361 | + |
| 362 | + void writeToResolutionFile(InputFile *Input, ArrayRef<SymbolResolution> Res); |
| 363 | + |
| 364 | + void addSymbolToGlobalRes(object::IRObjectFile *Obj, |
| 365 | + SmallPtrSet<GlobalValue *, 8> &Used, |
| 366 | + const InputFile::Symbol &Sym, SymbolResolution Res, |
| 367 | + size_t Partition); |
| 368 | + |
| 369 | + Error addRegularLTO(std::unique_ptr<InputFile> Input, |
| 370 | + ArrayRef<SymbolResolution> Res); |
| 371 | + Error addThinLTO(std::unique_ptr<InputFile> Input, |
| 372 | + ArrayRef<SymbolResolution> Res); |
| 373 | + |
| 374 | + Error runRegularLTO(AddStreamFn AddStream); |
| 375 | + Error runThinLTO(AddStreamFn AddStream); |
| 376 | + |
| 377 | + mutable bool CalledGetMaxTasks = false; |
| 378 | +}; |
| 379 | + |
| 380 | +/// The resolution for a symbol. The linker must provide a SymbolResolution for |
| 381 | +/// each global symbol based on its internal resolution of that symbol. |
| 382 | +struct SymbolResolution { |
| 383 | + SymbolResolution() |
| 384 | + : Prevailing(0), FinalDefinitionInLinkageUnit(0), VisibleToRegularObj(0) { |
| 385 | + } |
| 386 | + /// The linker has chosen this definition of the symbol. |
| 387 | + unsigned Prevailing : 1; |
| 388 | + |
| 389 | + /// The definition of this symbol is unpreemptable at runtime and is known to |
| 390 | + /// be in this linkage unit. |
| 391 | + unsigned FinalDefinitionInLinkageUnit : 1; |
| 392 | + |
| 393 | + /// The definition of this symbol is visible outside of the LTO unit. |
| 394 | + unsigned VisibleToRegularObj : 1; |
| 395 | +}; |
| 396 | + |
| 397 | +} // namespace lto |
| 398 | +} // namespace llvm |
73 | 399 |
|
74 | 400 | #endif
|
0 commit comments