Index: llvm/include/llvm/Object/IRObjectFile.h =================================================================== --- llvm/include/llvm/Object/IRObjectFile.h +++ llvm/include/llvm/Object/IRObjectFile.h @@ -15,6 +15,7 @@ #define LLVM_OBJECT_IROBJECTFILE_H #include "llvm/ADT/PointerUnion.h" +#include "llvm/Object/ModuleSymbolTable.h" #include "llvm/Object/SymbolicFile.h" namespace llvm { @@ -28,15 +29,7 @@ class IRObjectFile : public SymbolicFile { std::unique_ptr M; - std::unique_ptr Mang; - typedef std::pair AsmSymbol; - SpecificBumpPtrAllocator AsmSymbols; - - typedef PointerUnion Sym; - std::vector SymTab; - static Sym getSym(DataRefImpl &Symb) { - return *reinterpret_cast(Symb.p); - } + ModuleSymbolTable SymTab; public: IRObjectFile(MemoryBufferRef Object, std::unique_ptr M); @@ -70,15 +63,6 @@ /// error code if not found. static ErrorOr findBitcodeInObject(const ObjectFile &Obj); - /// Parse inline ASM and collect the symbols that are not defined in - /// the current module. - /// - /// For each found symbol, call \p AsmUndefinedRefs with the name of the - /// symbol found and the associated flags. - static void CollectAsmUndefinedRefs( - const Triple &TheTriple, StringRef InlineAsm, - function_ref AsmUndefinedRefs); - /// \brief Finds and returns bitcode in the given memory buffer (which may /// be either a bitcode file or a native object file with embedded bitcode), /// or an error code if not found. Index: llvm/include/llvm/Object/ModuleSymbolTable.h =================================================================== --- /dev/null +++ llvm/include/llvm/Object/ModuleSymbolTable.h @@ -0,0 +1,61 @@ +//===- ModuleSymbolTable.h - symbol table for in-memory IR ----------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This class represents a symbol table built from in-memory IR. It provides +// access to GlobalValues and should only be used if such access is required +// (e.g. in the LTO implementation). +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_OBJECT_MODULESYMBOLTABLE_H +#define LLVM_OBJECT_MODULESYMBOLTABLE_H + +#include "llvm/ADT/PointerUnion.h" +#include "llvm/ADT/Triple.h" +#include "llvm/IR/Mangler.h" +#include "llvm/Object/SymbolicFile.h" +#include +#include + +namespace llvm { + +class GlobalValue; + +class ModuleSymbolTable { +public: + typedef std::pair AsmSymbol; + typedef PointerUnion Symbol; + +private: + Module *FirstMod = nullptr; + + SpecificBumpPtrAllocator AsmSymbols; + std::vector SymTab; + Mangler Mang; + +public: + ArrayRef symbols() const { return SymTab; } + void addModule(Module *M); + + void printSymbolName(raw_ostream &OS, Symbol S) const; + uint32_t getSymbolFlags(Symbol S) const; + + /// Parse inline ASM and collect the symbols that are defined or referenced in + /// the current module. + /// + /// For each found symbol, call \p AsmSymbol with the name of the symbol found + /// and the associated flags. + static void CollectAsmSymbols( + const Triple &TheTriple, StringRef InlineAsm, + function_ref AsmSymbol); +}; + +} + +#endif Index: llvm/lib/Analysis/ModuleSummaryAnalysis.cpp =================================================================== --- llvm/lib/Analysis/ModuleSummaryAnalysis.cpp +++ llvm/lib/Analysis/ModuleSummaryAnalysis.cpp @@ -269,9 +269,7 @@ // Also, any values used but not defined within module level asm should // be listed on the llvm.used or llvm.compiler.used global and marked as // referenced from there. - // FIXME: Rename CollectAsmUndefinedRefs to something more general, as we - // are also using it to find the file-scope locals defined in module asm. - object::IRObjectFile::CollectAsmUndefinedRefs( + ModuleSymbolTable::CollectAsmSymbols( Triple(M.getTargetTriple()), M.getModuleInlineAsm(), [&M, &Index](StringRef Name, object::BasicSymbolRef::Flags Flags) { // Symbols not marked as Weak or Global are local definitions. Index: llvm/lib/LTO/LTOBackend.cpp =================================================================== --- llvm/lib/LTO/LTOBackend.cpp +++ llvm/lib/LTO/LTOBackend.cpp @@ -280,7 +280,7 @@ // Collect the list of undefined symbols used in asm and update // llvm.compiler.used to prevent optimization to drop these from the output. StringSet<> AsmUndefinedRefs; - object::IRObjectFile::CollectAsmUndefinedRefs( + ModuleSymbolTable::CollectAsmSymbols( Triple(Mod.getTargetTriple()), Mod.getModuleInlineAsm(), [&AsmUndefinedRefs](StringRef Name, object::BasicSymbolRef::Flags Flags) { if (Flags & object::BasicSymbolRef::SF_Undefined) Index: llvm/lib/Object/CMakeLists.txt =================================================================== --- llvm/lib/Object/CMakeLists.txt +++ llvm/lib/Object/CMakeLists.txt @@ -10,6 +10,7 @@ MachOObjectFile.cpp MachOUniversal.cpp ModuleSummaryIndexObjectFile.cpp + ModuleSymbolTable.cpp Object.cpp ObjectFile.cpp RecordStreamer.cpp Index: llvm/lib/Object/IRObjectFile.cpp =================================================================== --- llvm/lib/Object/IRObjectFile.cpp +++ llvm/lib/Object/IRObjectFile.cpp @@ -37,162 +37,27 @@ IRObjectFile::IRObjectFile(MemoryBufferRef Object, std::unique_ptr Mod) : SymbolicFile(Binary::ID_IR, Object), M(std::move(Mod)) { - Mang.reset(new Mangler()); - - for (Function &F : *M) - SymTab.push_back(&F); - for (GlobalVariable &GV : M->globals()) - SymTab.push_back(&GV); - for (GlobalAlias &GA : M->aliases()) - SymTab.push_back(&GA); - - CollectAsmUndefinedRefs(Triple(M->getTargetTriple()), M->getModuleInlineAsm(), - [this](StringRef Name, BasicSymbolRef::Flags Flags) { - SymTab.push_back(new (AsmSymbols.Allocate()) - AsmSymbol(Name, Flags)); - }); + SymTab.addModule(M.get()); } -// Parse inline ASM and collect the list of symbols that are not defined in -// the current module. This is inspired from IRObjectFile. -void IRObjectFile::CollectAsmUndefinedRefs( - const Triple &TT, StringRef InlineAsm, - function_ref AsmUndefinedRefs) { - if (InlineAsm.empty()) - return; - - std::string Err; - const Target *T = TargetRegistry::lookupTarget(TT.str(), Err); - assert(T && T->hasMCAsmParser()); - - std::unique_ptr MRI(T->createMCRegInfo(TT.str())); - if (!MRI) - return; - - std::unique_ptr MAI(T->createMCAsmInfo(*MRI, TT.str())); - if (!MAI) - return; - - std::unique_ptr STI( - T->createMCSubtargetInfo(TT.str(), "", "")); - if (!STI) - return; - - std::unique_ptr MCII(T->createMCInstrInfo()); - if (!MCII) - return; - - MCObjectFileInfo MOFI; - MCContext MCCtx(MAI.get(), MRI.get(), &MOFI); - MOFI.InitMCObjectFileInfo(TT, /*PIC*/ false, CodeModel::Default, MCCtx); - RecordStreamer Streamer(MCCtx); - T->createNullTargetStreamer(Streamer); - - std::unique_ptr Buffer(MemoryBuffer::getMemBuffer(InlineAsm)); - SourceMgr SrcMgr; - SrcMgr.AddNewSourceBuffer(std::move(Buffer), SMLoc()); - std::unique_ptr Parser( - createMCAsmParser(SrcMgr, MCCtx, Streamer, *MAI)); - - MCTargetOptions MCOptions; - std::unique_ptr TAP( - T->createMCAsmParser(*STI, *Parser, *MCII, MCOptions)); - if (!TAP) - return; - - Parser->setTargetParser(*TAP); - if (Parser->Run(false)) - return; +IRObjectFile::~IRObjectFile() {} - for (auto &KV : Streamer) { - StringRef Key = KV.first(); - RecordStreamer::State Value = KV.second; - uint32_t Res = BasicSymbolRef::SF_None; - switch (Value) { - case RecordStreamer::NeverSeen: - llvm_unreachable("NeverSeen should have been replaced earlier"); - case RecordStreamer::DefinedGlobal: - Res |= BasicSymbolRef::SF_Global; - break; - case RecordStreamer::Defined: - break; - case RecordStreamer::Global: - case RecordStreamer::Used: - Res |= BasicSymbolRef::SF_Undefined; - Res |= BasicSymbolRef::SF_Global; - break; - case RecordStreamer::DefinedWeak: - Res |= BasicSymbolRef::SF_Weak; - Res |= BasicSymbolRef::SF_Global; - break; - case RecordStreamer::UndefinedWeak: - Res |= BasicSymbolRef::SF_Weak; - Res |= BasicSymbolRef::SF_Undefined; - } - AsmUndefinedRefs(Key, BasicSymbolRef::Flags(Res)); - } +static ModuleSymbolTable::Symbol getSym(DataRefImpl &Symb) { + return *reinterpret_cast(Symb.p); } -IRObjectFile::~IRObjectFile() {} - void IRObjectFile::moveSymbolNext(DataRefImpl &Symb) const { - Symb.p += sizeof(Sym); + Symb.p += sizeof(ModuleSymbolTable::Symbol); } std::error_code IRObjectFile::printSymbolName(raw_ostream &OS, DataRefImpl Symb) const { - Sym S = getSym(Symb); - if (S.is()) { - OS << S.get()->first; - return std::error_code(); - } - - auto *GV = S.get(); - if (GV->hasDLLImportStorageClass()) - OS << "__imp_"; - - if (Mang) - Mang->getNameWithPrefix(OS, GV, false); - else - OS << GV->getName(); - + SymTab.printSymbolName(OS, getSym(Symb)); return std::error_code(); } uint32_t IRObjectFile::getSymbolFlags(DataRefImpl Symb) const { - Sym S = getSym(Symb); - if (S.is()) - return S.get()->second; - - auto *GV = S.get(); - - uint32_t Res = BasicSymbolRef::SF_None; - if (GV->isDeclarationForLinker()) - Res |= BasicSymbolRef::SF_Undefined; - else if (GV->hasHiddenVisibility() && !GV->hasLocalLinkage()) - Res |= BasicSymbolRef::SF_Hidden; - if (const GlobalVariable *GVar = dyn_cast(GV)) { - if (GVar->isConstant()) - Res |= BasicSymbolRef::SF_Const; - } - if (GV->hasPrivateLinkage()) - Res |= BasicSymbolRef::SF_FormatSpecific; - if (!GV->hasLocalLinkage()) - Res |= BasicSymbolRef::SF_Global; - if (GV->hasCommonLinkage()) - Res |= BasicSymbolRef::SF_Common; - if (GV->hasLinkOnceLinkage() || GV->hasWeakLinkage() || - GV->hasExternalWeakLinkage()) - Res |= BasicSymbolRef::SF_Weak; - - if (GV->getName().startswith("llvm.")) - Res |= BasicSymbolRef::SF_FormatSpecific; - else if (auto *Var = dyn_cast(GV)) { - if (Var->getSection() == "llvm.metadata") - Res |= BasicSymbolRef::SF_FormatSpecific; - } - - return Res; + return SymTab.getSymbolFlags(getSym(Symb)); } GlobalValue *IRObjectFile::getSymbolGV(DataRefImpl Symb) { @@ -203,13 +68,14 @@ basic_symbol_iterator IRObjectFile::symbol_begin() const { DataRefImpl Ret; - Ret.p = reinterpret_cast(SymTab.data()); + Ret.p = reinterpret_cast(SymTab.symbols().data()); return basic_symbol_iterator(BasicSymbolRef(Ret, this)); } basic_symbol_iterator IRObjectFile::symbol_end() const { DataRefImpl Ret; - Ret.p = reinterpret_cast(SymTab.data() + SymTab.size()); + Ret.p = reinterpret_cast(SymTab.symbols().data() + + SymTab.symbols().size()); return basic_symbol_iterator(BasicSymbolRef(Ret, this)); } Index: llvm/lib/Object/ModuleSymbolTable.cpp =================================================================== --- llvm/lib/Object/ModuleSymbolTable.cpp +++ llvm/lib/Object/ModuleSymbolTable.cpp @@ -1,4 +1,4 @@ -//===- IRObjectFile.cpp - IR object file implementation ---------*- C++ -*-===// +//===- ModuleSymbolTable.cpp - symbol table for in-memory IR ----*- C++ -*-===// // // The LLVM Compiler Infrastructure // @@ -7,7 +7,9 @@ // //===----------------------------------------------------------------------===// // -// Part of the IRObjectFile class implementation. +// This class represents a symbol table built from in-memory IR. It provides +// access to GlobalValues and should only be used if such access is required +// (e.g. in the LTO implementation). // //===----------------------------------------------------------------------===// @@ -35,9 +37,11 @@ using namespace llvm; using namespace object; -IRObjectFile::IRObjectFile(MemoryBufferRef Object, std::unique_ptr Mod) - : SymbolicFile(Binary::ID_IR, Object), M(std::move(Mod)) { - Mang.reset(new Mangler()); +void ModuleSymbolTable::addModule(Module *M) { + if (FirstMod) + assert(FirstMod->getTargetTriple() == M->getTargetTriple()); + else + FirstMod = M; for (Function &F : *M) SymTab.push_back(&F); @@ -46,18 +50,16 @@ for (GlobalAlias &GA : M->aliases()) SymTab.push_back(&GA); - CollectAsmUndefinedRefs(Triple(M->getTargetTriple()), M->getModuleInlineAsm(), - [this](StringRef Name, BasicSymbolRef::Flags Flags) { - SymTab.push_back(new (AsmSymbols.Allocate()) - AsmSymbol(Name, Flags)); - }); + CollectAsmSymbols(Triple(M->getTargetTriple()), M->getModuleInlineAsm(), + [this](StringRef Name, BasicSymbolRef::Flags Flags) { + SymTab.push_back(new (AsmSymbols.Allocate()) + AsmSymbol(Name, Flags)); + }); } -// Parse inline ASM and collect the list of symbols that are not defined in -// the current module. This is inspired from IRObjectFile. -void IRObjectFile::CollectAsmUndefinedRefs( +void ModuleSymbolTable::CollectAsmSymbols( const Triple &TT, StringRef InlineAsm, - function_ref AsmUndefinedRefs) { + function_ref AsmSymbol) { if (InlineAsm.empty()) return; @@ -129,38 +131,24 @@ Res |= BasicSymbolRef::SF_Weak; Res |= BasicSymbolRef::SF_Undefined; } - AsmUndefinedRefs(Key, BasicSymbolRef::Flags(Res)); + AsmSymbol(Key, BasicSymbolRef::Flags(Res)); } } -IRObjectFile::~IRObjectFile() {} - -void IRObjectFile::moveSymbolNext(DataRefImpl &Symb) const { - Symb.p += sizeof(Sym); -} - -std::error_code IRObjectFile::printSymbolName(raw_ostream &OS, - DataRefImpl Symb) const { - Sym S = getSym(Symb); +void ModuleSymbolTable::printSymbolName(raw_ostream &OS, Symbol S) const { if (S.is()) { OS << S.get()->first; - return std::error_code(); + return; } auto *GV = S.get(); if (GV->hasDLLImportStorageClass()) OS << "__imp_"; - if (Mang) - Mang->getNameWithPrefix(OS, GV, false); - else - OS << GV->getName(); - - return std::error_code(); + Mang.getNameWithPrefix(OS, GV, false); } -uint32_t IRObjectFile::getSymbolFlags(DataRefImpl Symb) const { - Sym S = getSym(Symb); +uint32_t ModuleSymbolTable::getSymbolFlags(Symbol S) const { if (S.is()) return S.get()->second; @@ -194,72 +182,3 @@ return Res; } - -GlobalValue *IRObjectFile::getSymbolGV(DataRefImpl Symb) { - return getSym(Symb).dyn_cast(); -} - -std::unique_ptr IRObjectFile::takeModule() { return std::move(M); } - -basic_symbol_iterator IRObjectFile::symbol_begin() const { - DataRefImpl Ret; - Ret.p = reinterpret_cast(SymTab.data()); - return basic_symbol_iterator(BasicSymbolRef(Ret, this)); -} - -basic_symbol_iterator IRObjectFile::symbol_end() const { - DataRefImpl Ret; - Ret.p = reinterpret_cast(SymTab.data() + SymTab.size()); - return basic_symbol_iterator(BasicSymbolRef(Ret, this)); -} - -StringRef IRObjectFile::getTargetTriple() const { return M->getTargetTriple(); } - -ErrorOr IRObjectFile::findBitcodeInObject(const ObjectFile &Obj) { - for (const SectionRef &Sec : Obj.sections()) { - if (Sec.isBitcode()) { - StringRef SecContents; - if (std::error_code EC = Sec.getContents(SecContents)) - return EC; - return MemoryBufferRef(SecContents, Obj.getFileName()); - } - } - - return object_error::bitcode_section_not_found; -} - -ErrorOr IRObjectFile::findBitcodeInMemBuffer(MemoryBufferRef Object) { - sys::fs::file_magic Type = sys::fs::identify_magic(Object.getBuffer()); - switch (Type) { - case sys::fs::file_magic::bitcode: - return Object; - case sys::fs::file_magic::elf_relocatable: - case sys::fs::file_magic::macho_object: - case sys::fs::file_magic::coff_object: { - Expected> ObjFile = - ObjectFile::createObjectFile(Object, Type); - if (!ObjFile) - return errorToErrorCode(ObjFile.takeError()); - return findBitcodeInObject(*ObjFile->get()); - } - default: - return object_error::invalid_file_type; - } -} - -Expected> -llvm::object::IRObjectFile::create(MemoryBufferRef Object, - LLVMContext &Context) { - ErrorOr BCOrErr = findBitcodeInMemBuffer(Object); - if (!BCOrErr) - return errorCodeToError(BCOrErr.getError()); - - Expected> MOrErr = - getLazyBitcodeModule(*BCOrErr, Context, - /*ShouldLazyLoadMetadata*/ true); - if (!MOrErr) - return MOrErr.takeError(); - - std::unique_ptr &M = MOrErr.get(); - return llvm::make_unique(BCOrErr.get(), std::move(M)); -} Index: llvm/lib/Transforms/IPO/FunctionImport.cpp =================================================================== --- llvm/lib/Transforms/IPO/FunctionImport.cpp +++ llvm/lib/Transforms/IPO/FunctionImport.cpp @@ -555,7 +555,7 @@ // Parse inline ASM and collect the list of symbols that are not defined in // the current module. StringSet<> AsmUndefinedRefs; - object::IRObjectFile::CollectAsmUndefinedRefs( + ModuleSymbolTable::CollectAsmSymbols( Triple(TheModule.getTargetTriple()), TheModule.getModuleInlineAsm(), [&AsmUndefinedRefs](StringRef Name, object::BasicSymbolRef::Flags Flags) { if (Flags & object::BasicSymbolRef::SF_Undefined)