17#include "llvm/Config/llvm-config.h"
33#include "llvm/Support/VCSRevision.h"
46 cl::desc(
"Disable automatic bitcode upgrade for version mismatch"));
50const char *getExpectedProducerName() {
51 static char DefaultName[] = LLVM_VERSION_STRING
58 if (
char *OverrideName = getenv(
"LLVM_OVERRIDE_PRODUCER"))
63const char *kExpectedProducerName = getExpectedProducerName();
67 SmallVector<char, 0> &Symtab;
68 StringTableBuilder &StrtabBuilder;
74 Builder(SmallVector<char, 0> &Symtab, StringTableBuilder &StrtabBuilder,
76 : Symtab(Symtab), StrtabBuilder(StrtabBuilder), Saver(
Alloc), TT(TT) {}
78 DenseMap<const Comdat *, int> ComdatMap;
82 std::vector<storage::Comdat> Comdats;
83 std::vector<storage::Module> Mods;
84 std::vector<storage::Symbol> Syms;
85 std::vector<storage::Uncommon> Uncommons;
87 std::string COFFLinkerOpts;
88 raw_string_ostream COFFLinkerOptsOS{COFFLinkerOpts};
90 std::vector<storage::Str> DependentLibraries;
92 void setStr(storage::Str &S, StringRef
Value) {
98 void writeRange(storage::Range<T> &R,
const std::vector<T> &Objs) {
99 R.Offset = Symtab.size();
100 R.Size = Objs.size();
101 Symtab.insert(Symtab.end(),
reinterpret_cast<const char *
>(Objs.data()),
102 reinterpret_cast<const char *
>(Objs.data() + Objs.size()));
105 Expected<int> getComdatIndex(
const Comdat *
C,
const Module *M);
109 const SmallPtrSet<GlobalValue *, 4> &Used,
116 if (
M->getDataLayoutStr().empty())
136 ModuleSymbolTable Msymtab;
140 Mod.Begin = Syms.size();
141 Mod.End = Syms.size() + Msymtab.
symbols().size();
142 Mod.UncBegin = Uncommons.size();
145 if (
TT.isOSBinFormatCOFF()) {
146 if (
auto E =
M->materializeMetadata())
148 if (NamedMDNode *LinkerOptions =
149 M->getNamedMetadata(
"llvm.linker.options")) {
150 for (MDNode *MDOptions : LinkerOptions->operands())
151 for (
const MDOperand &MDOption :
cast<MDNode>(MDOptions)->operands())
156 if (
TT.isOSBinFormatELF()) {
157 if (
auto E =
M->materializeMetadata())
159 if (NamedMDNode *
N =
M->getNamedMetadata(
"llvm.dependent-libraries")) {
160 for (MDNode *MDOptions :
N->operands()) {
161 const auto OperandStr =
164 setStr(Specifier, OperandStr);
165 DependentLibraries.emplace_back(Specifier);
177Expected<int> Builder::getComdatIndex(
const Comdat *
C,
const Module *M) {
178 auto P = ComdatMap.
insert(std::make_pair(
C, Comdats.size()));
181 if (
TT.isOSBinFormatCOFF()) {
182 const GlobalValue *GV =
M->getNamedValue(
C->getName());
189 P.first->second = -1;
192 llvm::raw_string_ostream OS(Name);
195 Name = std::string(
C->getName());
198 storage::Comdat Comdat;
199 setStr(Comdat.
Name, Saver.
save(Name));
201 Comdats.push_back(Comdat);
204 return P.first->second;
207Error Builder::addSymbol(
const ModuleSymbolTable &Msymtab,
208 const SmallPtrSet<GlobalValue *, 4> &Used,
211 storage::Symbol &Sym = Syms.back();
214 storage::Uncommon *Unc =
nullptr;
215 auto Uncommon = [&]() -> storage::Uncommon & {
219 Uncommons.emplace_back();
220 Unc = &Uncommons.back();
227 SmallString<64>
Name;
229 raw_svector_ostream OS(Name);
260 StringRef GVName = GV->
getName();
261 setStr(Sym.
IRName, GVName);
278 Uncommon().CommonSize = GVar->getGlobalSize(GV->
getDataLayout());
279 Uncommon().CommonAlign = GVar->getAlign() ? GVar->getAlign()->value() : 0;
291 Expected<int> ComdatIndexOrErr = getComdatIndex(
C, GV->
getParent());
292 if (!ComdatIndexOrErr)
297 if (
TT.isOSBinFormatCOFF()) {
307 std::string FallbackName;
308 raw_string_ostream OS(FallbackName);
310 setStr(Uncommon().COFFWeakExternFallbackName, Saver.
save(FallbackName));
325 setStr(Hdr.
Producer, kExpectedProducerName);
326 setStr(Hdr.
TargetTriple, IRMods[0]->getTargetTriple().str());
329 for (
auto *M : IRMods)
330 if (
Error Err = addModule(M))
337 Symtab.
resize(
sizeof(storage::Header));
339 writeRange(Hdr.
Comdats, Comdats);
343 *
reinterpret_cast<storage::Header *
>(Symtab.
data()) = Hdr;
352 const Triple &TT = Mods[0]->getTargetTriple();
353 return Builder(Symtab, StrtabBuilder,
Alloc, TT).build(Mods);
362 std::vector<Module *> Mods;
363 std::vector<std::unique_ptr<Module>> OwnedMods;
364 for (
auto BM : BMs) {
366 BM.getLazyModule(Ctx,
true,
371 Mods.push_back(MOrErr->
get());
372 OwnedMods.push_back(std::move(*MOrErr));
381 FC.Strtab.resize(StrtabBuilder.
getSize());
384 FC.TheReader = {{FC.Symtab.data(), FC.Symtab.size()},
385 {FC.Strtab.data(), FC.Strtab.size()}};
386 return std::move(FC);
390 if (BFC.
Mods.empty())
407 Producer != kExpectedProducerName)
419 if (FC.TheReader.getNumModules() != BFC.
Mods.size())
422 return std::move(FC);
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)
Module.h This file contains the declarations for the Module class.
Machine Check Debug Module
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),...
bool empty() const
empty - Check if the array is empty.
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
Tagged union holding either a T or a Error.
Error takeError()
Take ownership of the stored error.
reference get()
Returns a reference to the stored T value.
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
LLVM_ABI const DataLayout & getDataLayout() const
Get the data layout of the module this global belongs to.
bool hasGlobalUnnamedAddr() const
LLVM_ABI bool canBeOmittedFromSymbolTable() const
True if GV can be left out of the object symbol table.
This is an important class for using LLVM in a threaded context.
LLVM_ABI void getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV, bool CannotUsePrivateLabel) const
Print the appropriate prefix and the specified global variable's name.
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.
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.
constexpr bool empty() const
empty - Check if the string is empty.
constexpr size_t size() const
size - Get the string size.
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
StringRef save(const char *S)
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.
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
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.
LLVM_ABI Expected< FileContents > readBitcode(const BitcodeFileContents &BFC)
Reads the contents of a bitcode file, creating its irsymtab if necessary.
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.
This is an optimization pass for GlobalISel generic memory operations.
FunctionAddr VTableAddr Value
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
LLVM_ABI std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
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 ...
FunctionAddr VTableAddr uintptr_t uintptr_t Version
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...
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
ArrayRef(const T &OneElt) -> ArrayRef< T >
LLVM_ABI void emitLinkerFlagsForGlobalCOFF(raw_ostream &OS, const GlobalValue *GV, const Triple &TT, Mangler &Mangler)
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
BumpPtrAllocatorImpl<> BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
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 ...
StringRef StrtabForSymtab
std::vector< BitcodeModule > Mods
The contents of the irsymtab in a bitcode file.
StringRef get(StringRef Strtab) const
Str Name
The mangled symbol name.
Str IRName
The unmangled symbol name, or the empty string if this is not an IR symbol.
Word ComdatIndex
The index into Header::Comdats, or -1 if not a comdat member.
Str SectionName
Specified section name, if any.
Str COFFWeakExternFallbackName
COFF-specific: the name of the symbol that a weak external resolves to if not defined.