LLVM 22.0.0git
IRSymtab.cpp
Go to the documentation of this file.
1//===- IRSymtab.cpp - implementation of IR symbol tables ------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
10#include "llvm/ADT/ArrayRef.h"
14#include "llvm/ADT/StringRef.h"
15#include "llvm/ADT/StringSet.h"
17#include "llvm/Config/llvm-config.h"
18#include "llvm/IR/Comdat.h"
19#include "llvm/IR/DataLayout.h"
20#include "llvm/IR/GlobalAlias.h"
22#include "llvm/IR/Mangler.h"
23#include "llvm/IR/Metadata.h"
24#include "llvm/IR/Module.h"
32#include "llvm/Support/Error.h"
34#include "llvm/Support/VCSRevision.h"
37#include <cassert>
38#include <string>
39#include <utility>
40#include <vector>
41
42using namespace llvm;
43using namespace irsymtab;
44
46 "disable-bitcode-version-upgrade", cl::Hidden,
47 cl::desc("Disable automatic bitcode upgrade for version mismatch"));
48
49namespace {
50
51const char *getExpectedProducerName() {
52 static char DefaultName[] = LLVM_VERSION_STRING
53#ifdef LLVM_REVISION
54 " " LLVM_REVISION
55#endif
56 ;
57 // Allows for testing of the irsymtab writer and upgrade mechanism. This
58 // environment variable should not be set by users.
59 if (char *OverrideName = getenv("LLVM_OVERRIDE_PRODUCER"))
60 return OverrideName;
61 return DefaultName;
62}
63
64const char *kExpectedProducerName = getExpectedProducerName();
65
66/// Stores the temporary state that is required to build an IR symbol table.
67struct Builder {
68 SmallVector<char, 0> &Symtab;
69 StringTableBuilder &StrtabBuilder;
70 StringSaver Saver;
71
72 // This ctor initializes a StringSaver using the passed in BumpPtrAllocator.
73 // The StringTableBuilder does not create a copy of any strings added to it,
74 // so this provides somewhere to store any strings that we create.
75 Builder(SmallVector<char, 0> &Symtab, StringTableBuilder &StrtabBuilder,
76 BumpPtrAllocator &Alloc, const Triple &TT)
77 : Symtab(Symtab), StrtabBuilder(StrtabBuilder), Saver(Alloc), TT(TT),
78 Libcalls(TT) {}
79
80 DenseMap<const Comdat *, int> ComdatMap;
81 Mangler Mang;
82 const Triple &TT;
83
84 // FIXME: This shouldn't be here.
85 RTLIB::RuntimeLibcallsInfo Libcalls;
86
87 std::vector<storage::Comdat> Comdats;
88 std::vector<storage::Module> Mods;
89 std::vector<storage::Symbol> Syms;
90 std::vector<storage::Uncommon> Uncommons;
91
92 std::string COFFLinkerOpts;
93 raw_string_ostream COFFLinkerOptsOS{COFFLinkerOpts};
94
95 std::vector<storage::Str> DependentLibraries;
96
97 bool isPreservedName(StringRef Name) {
98 return Libcalls.getSupportedLibcallImpl(Name) != RTLIB::Unsupported;
99 }
100
101 void setStr(storage::Str &S, StringRef Value) {
102 S.Offset = StrtabBuilder.add(Value);
103 S.Size = Value.size();
104 }
105
106 template <typename T>
107 void writeRange(storage::Range<T> &R, const std::vector<T> &Objs) {
108 R.Offset = Symtab.size();
109 R.Size = Objs.size();
110 Symtab.insert(Symtab.end(), reinterpret_cast<const char *>(Objs.data()),
111 reinterpret_cast<const char *>(Objs.data() + Objs.size()));
112 }
113
114 Expected<int> getComdatIndex(const Comdat *C, const Module *M);
115
116 Error addModule(Module *M);
117 Error addSymbol(const ModuleSymbolTable &Msymtab,
118 const SmallPtrSet<GlobalValue *, 4> &Used,
120
122};
123
124Error Builder::addModule(Module *M) {
125 if (M->getDataLayoutStr().empty())
126 return make_error<StringError>("input module has no datalayout",
128
129 // Symbols in the llvm.used list will get the FB_Used bit and will not be
130 // internalized. We do this for llvm.compiler.used as well:
131 //
132 // IR symbol table tracks module-level asm symbol references but not inline
133 // asm. A symbol only referenced by inline asm is not in the IR symbol table,
134 // so we may not know that the definition (in another translation unit) is
135 // referenced. That definition may have __attribute__((used)) (which lowers to
136 // llvm.compiler.used on ELF targets) to communicate to the compiler that it
137 // may be used by inline asm. The usage is perfectly fine, so we treat
138 // llvm.compiler.used conservatively as llvm.used to work around our own
139 // limitation.
141 collectUsedGlobalVariables(*M, UsedV, /*CompilerUsed=*/false);
142 collectUsedGlobalVariables(*M, UsedV, /*CompilerUsed=*/true);
143 SmallPtrSet<GlobalValue *, 4> Used(llvm::from_range, UsedV);
144
145 ModuleSymbolTable Msymtab;
146 Msymtab.addModule(M);
147
148 storage::Module Mod;
149 Mod.Begin = Syms.size();
150 Mod.End = Syms.size() + Msymtab.symbols().size();
151 Mod.UncBegin = Uncommons.size();
152 Mods.push_back(Mod);
153
154 if (TT.isOSBinFormatCOFF()) {
155 if (auto E = M->materializeMetadata())
156 return E;
157 if (NamedMDNode *LinkerOptions =
158 M->getNamedMetadata("llvm.linker.options")) {
159 for (MDNode *MDOptions : LinkerOptions->operands())
160 for (const MDOperand &MDOption : cast<MDNode>(MDOptions)->operands())
161 COFFLinkerOptsOS << " " << cast<MDString>(MDOption)->getString();
162 }
163 }
164
165 if (TT.isOSBinFormatELF()) {
166 if (auto E = M->materializeMetadata())
167 return E;
168 if (NamedMDNode *N = M->getNamedMetadata("llvm.dependent-libraries")) {
169 for (MDNode *MDOptions : N->operands()) {
170 const auto OperandStr =
171 cast<MDString>(cast<MDNode>(MDOptions)->getOperand(0))->getString();
172 storage::Str Specifier;
173 setStr(Specifier, OperandStr);
174 DependentLibraries.emplace_back(Specifier);
175 }
176 }
177 }
178
179 for (ModuleSymbolTable::Symbol Msym : Msymtab.symbols())
180 if (Error Err = addSymbol(Msymtab, Used, Msym))
181 return Err;
182
183 return Error::success();
184}
185
186Expected<int> Builder::getComdatIndex(const Comdat *C, const Module *M) {
187 auto P = ComdatMap.insert(std::make_pair(C, Comdats.size()));
188 if (P.second) {
189 std::string Name;
190 if (TT.isOSBinFormatCOFF()) {
191 const GlobalValue *GV = M->getNamedValue(C->getName());
192 if (!GV)
193 return make_error<StringError>("Could not find leader",
195 // Internal leaders do not affect symbol resolution, therefore they do not
196 // appear in the symbol table.
197 if (GV->hasLocalLinkage()) {
198 P.first->second = -1;
199 return -1;
200 }
201 llvm::raw_string_ostream OS(Name);
202 Mang.getNameWithPrefix(OS, GV, false);
203 } else {
204 Name = std::string(C->getName());
205 }
206
207 storage::Comdat Comdat;
208 setStr(Comdat.Name, Saver.save(Name));
209 Comdat.SelectionKind = C->getSelectionKind();
210 Comdats.push_back(Comdat);
211 }
212
213 return P.first->second;
214}
215
216Error Builder::addSymbol(const ModuleSymbolTable &Msymtab,
217 const SmallPtrSet<GlobalValue *, 4> &Used,
219 Syms.emplace_back();
220 storage::Symbol &Sym = Syms.back();
221 Sym = {};
222
223 storage::Uncommon *Unc = nullptr;
224 auto Uncommon = [&]() -> storage::Uncommon & {
225 if (Unc)
226 return *Unc;
228 Uncommons.emplace_back();
229 Unc = &Uncommons.back();
230 *Unc = {};
231 setStr(Unc->COFFWeakExternFallbackName, "");
232 setStr(Unc->SectionName, "");
233 return *Unc;
234 };
235
236 SmallString<64> Name;
237 {
238 raw_svector_ostream OS(Name);
239 Msymtab.printSymbolName(OS, Msym);
240 }
241 setStr(Sym.Name, Saver.save(Name.str()));
242
243 auto Flags = Msymtab.getSymbolFlags(Msym);
258
259 Sym.ComdatIndex = -1;
260 auto *GV = dyn_cast_if_present<GlobalValue *>(Msym);
261 if (!GV) {
262 // Undefined module asm symbols act as GC roots and are implicitly used.
265 setStr(Sym.IRName, "");
266 return Error::success();
267 }
268
269 StringRef GVName = GV->getName();
270 setStr(Sym.IRName, GVName);
271
272 if (Used.count(GV) || isPreservedName(GVName))
274 if (GV->isThreadLocal())
275 Sym.Flags |= 1 << storage::Symbol::FB_tls;
276 if (GV->hasGlobalUnnamedAddr())
280 Sym.Flags |= unsigned(GV->getVisibility()) << storage::Symbol::FB_visibility;
281
283 auto *GVar = dyn_cast<GlobalVariable>(GV);
284 if (!GVar)
285 return make_error<StringError>("Only variables can have common linkage!",
287 Uncommon().CommonSize =
289 Uncommon().CommonAlign = GVar->getAlign() ? GVar->getAlign()->value() : 0;
290 }
291
292 const GlobalObject *GO = GV->getAliaseeObject();
293 if (!GO) {
294 if (isa<GlobalIFunc>(GV))
295 GO = cast<GlobalIFunc>(GV)->getResolverFunction();
296 if (!GO)
297 return make_error<StringError>("Unable to determine comdat of alias!",
299 }
300 if (const Comdat *C = GO->getComdat()) {
301 Expected<int> ComdatIndexOrErr = getComdatIndex(C, GV->getParent());
302 if (!ComdatIndexOrErr)
303 return ComdatIndexOrErr.takeError();
304 Sym.ComdatIndex = *ComdatIndexOrErr;
305 }
306
307 if (TT.isOSBinFormatCOFF()) {
308 emitLinkerFlagsForGlobalCOFF(COFFLinkerOptsOS, GV, TT, Mang);
309
310 if ((Flags & object::BasicSymbolRef::SF_Weak) &&
313 cast<GlobalAlias>(GV)->getAliasee()->stripPointerCasts());
314 if (!Fallback)
315 return make_error<StringError>("Invalid weak external",
317 std::string FallbackName;
318 raw_string_ostream OS(FallbackName);
319 Msymtab.printSymbolName(OS, Fallback);
320 OS.flush();
321 setStr(Uncommon().COFFWeakExternFallbackName, Saver.save(FallbackName));
322 }
323 }
324
325 if (!GO->getSection().empty())
326 setStr(Uncommon().SectionName, Saver.save(GO->getSection()));
327
328 return Error::success();
329}
330
331Error Builder::build(ArrayRef<Module *> IRMods) {
332 storage::Header Hdr;
333
334 assert(!IRMods.empty());
336 setStr(Hdr.Producer, kExpectedProducerName);
337 setStr(Hdr.TargetTriple, IRMods[0]->getTargetTriple().str());
338 setStr(Hdr.SourceFileName, IRMods[0]->getSourceFileName());
339
340 for (auto *M : IRMods)
341 if (Error Err = addModule(M))
342 return Err;
343
344 COFFLinkerOptsOS.flush();
345 setStr(Hdr.COFFLinkerOpts, Saver.save(COFFLinkerOpts));
346
347 // We are about to fill in the header's range fields, so reserve space for it
348 // and copy it in afterwards.
349 Symtab.resize(sizeof(storage::Header));
350 writeRange(Hdr.Modules, Mods);
351 writeRange(Hdr.Comdats, Comdats);
352 writeRange(Hdr.Symbols, Syms);
353 writeRange(Hdr.Uncommons, Uncommons);
354 writeRange(Hdr.DependentLibraries, DependentLibraries);
355 *reinterpret_cast<storage::Header *>(Symtab.data()) = Hdr;
356 return Error::success();
357}
358
359} // end anonymous namespace
360
362 StringTableBuilder &StrtabBuilder,
364 const Triple &TT = Mods[0]->getTargetTriple();
365 return Builder(Symtab, StrtabBuilder, Alloc, TT).build(Mods);
366}
367
368// Upgrade a vector of bitcode modules created by an old version of LLVM by
369// creating an irsymtab for them in the current format.
371 FileContents FC;
372
373 LLVMContext Ctx;
374 std::vector<Module *> Mods;
375 std::vector<std::unique_ptr<Module>> OwnedMods;
376 for (auto BM : BMs) {
378 BM.getLazyModule(Ctx, /*ShouldLazyLoadMetadata*/ true,
379 /*IsImporting*/ false);
380 if (!MOrErr)
381 return MOrErr.takeError();
382
383 Mods.push_back(MOrErr->get());
384 OwnedMods.push_back(std::move(*MOrErr));
385 }
386
389 if (Error E = build(Mods, FC.Symtab, StrtabBuilder, Alloc))
390 return std::move(E);
391
392 StrtabBuilder.finalizeInOrder();
393 FC.Strtab.resize(StrtabBuilder.getSize());
394 StrtabBuilder.write((uint8_t *)FC.Strtab.data());
395
396 FC.TheReader = {{FC.Symtab.data(), FC.Symtab.size()},
397 {FC.Strtab.data(), FC.Strtab.size()}};
398 return std::move(FC);
399}
400
402 if (BFC.Mods.empty())
403 return make_error<StringError>("Bitcode file does not contain any modules",
405
407 if (BFC.StrtabForSymtab.empty() ||
408 BFC.Symtab.size() < sizeof(storage::Header))
409 return upgrade(BFC.Mods);
410
411 // We cannot use the regular reader to read the version and producer,
412 // because it will expect the header to be in the current format. The only
413 // thing we can rely on is that the version and producer will be present as
414 // the first struct elements.
415 auto *Hdr = reinterpret_cast<const storage::Header *>(BFC.Symtab.data());
416 unsigned Version = Hdr->Version;
417 StringRef Producer = Hdr->Producer.get(BFC.StrtabForSymtab);
419 Producer != kExpectedProducerName)
420 return upgrade(BFC.Mods);
421 }
422
423 FileContents FC;
424 FC.TheReader = {{BFC.Symtab.data(), BFC.Symtab.size()},
426
427 // Finally, make sure that the number of modules in the symbol table matches
428 // the number of modules in the bitcode file. If they differ, it may mean that
429 // the bitcode file was created by binary concatenation, so we need to create
430 // a new symbol table from scratch.
431 if (FC.TheReader.getNumModules() != BFC.Mods.size())
432 return upgrade(std::move(BFC.Mods));
433
434 return std::move(FC);
435}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
AMDGPU Prepare AGPR Alloc
This file defines the BumpPtrAllocator interface.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static void addSymbol(Object &Obj, const NewSymbolInfo &SymInfo, uint8_t DefaultVisibility)
static cl::opt< bool > DisableBitcodeVersionUpgrade("disable-bitcode-version-upgrade", cl::Hidden, cl::desc("Disable automatic bitcode upgrade for version mismatch"))
static Expected< FileContents > upgrade(ArrayRef< BitcodeModule > BMs)
Definition IRSymtab.cpp:370
Module.h This file contains the declarations for the Module class.
Machine Check Debug Module
This file contains the declarations for metadata subclasses.
#define P(N)
if(auto Err=PB.parsePassPipeline(MPM, Passes)) return wrap(std MPM run * Mod
This file defines the SmallPtrSet class.
This file defines the SmallString class.
This file defines the SmallVector class.
StringSet - A set-like wrapper for the StringMap.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:41
bool empty() const
empty - Check if the array is empty.
Definition ArrayRef.h:142
TypeSize getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition DenseMap.h:214
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
Tagged union holding either a T or a Error.
Definition Error.h:485
Error takeError()
Take ownership of the stored error.
Definition Error.h:612
reference get()
Returns a reference to the stored T value.
Definition Error.h:582
StringRef getSection() const
Get the custom section of this global if it has one.
const Comdat * getComdat() const
bool isThreadLocal() const
If the value is "Thread Local", its value isn't shared by the threads.
VisibilityTypes getVisibility() const
bool hasLocalLinkage() const
Module * getParent()
Get the module that this global value is contained inside of...
LLVM_ABI const GlobalObject * getAliaseeObject() const
Definition Globals.cpp:431
LLVM_ABI const DataLayout & getDataLayout() const
Get the data layout of the module this global belongs to.
Definition Globals.cpp:132
bool hasGlobalUnnamedAddr() const
LLVM_ABI bool canBeOmittedFromSymbolTable() const
True if GV can be left out of the object symbol table.
Definition Globals.cpp:456
Type * getValueType() const
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
LLVM_ABI void getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV, bool CannotUsePrivateLabel) const
Print the appropriate prefix and the specified global variable's name.
Definition Mangler.cpp:121
LLVM_ABI void addModule(Module *M)
LLVM_ABI void printSymbolName(raw_ostream &OS, Symbol S) const
PointerUnion< GlobalValue *, AsmSymbol * > Symbol
LLVM_ABI uint32_t getSymbolFlags(Symbol S) const
ArrayRef< Symbol > symbols() const
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
void resize(size_type N)
pointer data()
Return a pointer to the vector's buffer, even if empty().
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
constexpr bool empty() const
empty - Check if the string is empty.
Definition StringRef.h:151
constexpr size_t size() const
size - Get the string size.
Definition StringRef.h:154
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition StringRef.h:148
StringRef save(const char *S)
Definition StringSaver.h:31
Utility for building string tables with deduplicated suffixes.
LLVM_ABI void finalizeInOrder()
Finalize the string table without reording it.
LLVM_ABI void write(raw_ostream &OS) const
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:47
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:322
This file contains the declaration of the Comdat class, which represents a single COMDAT in LLVM.
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
LLVM_ABI Expected< FileContents > readBitcode(const BitcodeFileContents &BFC)
Reads the contents of a bitcode file, creating its irsymtab if necessary.
Definition IRSymtab.cpp:401
LLVM_ABI Error build(ArrayRef< Module * > Mods, SmallVector< char, 0 > &Symtab, StringTableBuilder &StrtabBuilder, BumpPtrAllocator &Alloc)
Fills in Symtab and StrtabBuilder with a valid symbol and string table for Mods.
Definition IRSymtab.cpp:361
This is an optimization pass for GlobalISel generic memory operations.
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:649
LLVM_ABI std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition Error.cpp:98
constexpr from_range_t from_range
auto dyn_cast_if_present(const Y &Val)
dyn_cast_if_present<X> - Functionally identical to dyn_cast, except that a null (or none in the case ...
Definition Casting.h:738
FunctionAddr VTableAddr uintptr_t uintptr_t Version
Definition InstrProf.h:302
BumpPtrAllocatorImpl BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
Definition Allocator.h:383
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:548
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
Definition Error.h:340
ArrayRef(const T &OneElt) -> ArrayRef< T >
LLVM_ABI void emitLinkerFlagsForGlobalCOFF(raw_ostream &OS, const GlobalValue *GV, const Triple &TT, Mangler &Mangler)
Definition Mangler.cpp:214
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:565
LLVM_ABI GlobalVariable * collectUsedGlobalVariables(const Module &M, SmallVectorImpl< GlobalValue * > &Vec, bool CompilerUsed)
Given "llvm.used" or "llvm.compiler.used" as a global name, collect the initializer elements of that ...
Definition Module.cpp:865
#define N
std::vector< BitcodeModule > Mods
The contents of the irsymtab in a bitcode file.
Definition IRSymtab.h:372
Word Version
Version number of the symtab format.
Definition IRSymtab.h:139
Str COFFLinkerOpts
COFF-specific: linker directives.
Definition IRSymtab.h:156
Range< Str > DependentLibraries
Dependent Library Specifiers.
Definition IRSymtab.h:159
Range< Uncommon > Uncommons
Definition IRSymtab.h:151
Str Producer
The producer's version string (LLVM_VERSION_STRING " " LLVM_REVISION).
Definition IRSymtab.h:146
StringRef get(StringRef Strtab) const
Definition IRSymtab.h:59
Str Name
The mangled symbol name.
Definition IRSymtab.h:94
Str IRName
The unmangled symbol name, or the empty string if this is not an IR symbol.
Definition IRSymtab.h:98
Word ComdatIndex
The index into Header::Comdats, or -1 if not a comdat member.
Definition IRSymtab.h:101
Str SectionName
Specified section name, if any.
Definition IRSymtab.h:131
Str COFFWeakExternFallbackName
COFF-specific: the name of the symbol that a weak external resolves to if not defined.
Definition IRSymtab.h:128