Skip to content

Commit

Permalink
Implement LTOModule on top of IRObjectFile.
Browse files Browse the repository at this point in the history
IRObjectFile provides all the logic for producing mangled names and getting
symbols from inline assembly.

LTOModule then adds logic for linking specific tasks, like constructing
llvm.compiler_user or extracting linker options from the bitcode.

The rule of the thumb is that IRObjectFile has the functionality that is
needed by both LTO and llvm-ar.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@212349 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
espindola committed Jul 4, 2014
1 parent 2291f8c commit 1c9687e
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 168 deletions.
46 changes: 21 additions & 25 deletions include/llvm/LTO/LTOModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@

#include "llvm-c/lto.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/IR/Mangler.h"
#include "llvm/IR/Module.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCObjectFileInfo.h"
#include "llvm/Object/IRObjectFile.h"
#include "llvm/Target/TargetMachine.h"
#include <string>
#include <vector>
Expand All @@ -46,9 +46,8 @@ struct LTOModule {
const GlobalValue *symbol;
};

std::unique_ptr<Module> _module;
std::unique_ptr<object::IRObjectFile> IRFile;
std::unique_ptr<TargetMachine> _target;
MCObjectFileInfo ObjFileInfo;
StringSet _linkeropt_strings;
std::vector<const char *> _deplibs;
std::vector<const char *> _linkeropts;
Expand All @@ -58,12 +57,8 @@ struct LTOModule {
StringSet _defines;
StringMap<NameAndAttributes> _undefines;
std::vector<const char*> _asm_undefines;
MCContext _context;

// Use mangler to add GlobalPrefix to names to match linker names.
Mangler _mangler;

LTOModule(std::unique_ptr<Module> M, TargetMachine *TM);
LTOModule(std::unique_ptr<object::IRObjectFile> Obj, TargetMachine *TM);

public:
/// Returns 'true' if the file or memory contents is LLVM bitcode.
Expand Down Expand Up @@ -100,14 +95,21 @@ struct LTOModule {
TargetOptions options, std::string &errMsg,
StringRef path = "");

const Module &getModule() const {
return const_cast<LTOModule*>(this)->getModule();
}
Module &getModule() {
return IRFile->getModule();
}

/// Return the Module's target triple.
const std::string &getTargetTriple() {
return _module->getTargetTriple();
return getModule().getTargetTriple();
}

/// Set the Module's target triple.
void setTargetTriple(StringRef Triple) {
_module->setTargetTriple(Triple);
getModule().setTargetTriple(Triple);
}

/// Get the number of symbols
Expand Down Expand Up @@ -153,9 +155,6 @@ struct LTOModule {
return nullptr;
}

/// Return the Module.
Module *getLLVVMModule() { return _module.get(); }

const std::vector<const char*> &getAsmUndefinedRefs() {
return _asm_undefines;
}
Expand All @@ -170,23 +169,20 @@ struct LTOModule {
bool parseSymbols(std::string &errMsg);

/// Add a symbol which isn't defined just yet to a list to be resolved later.
void addPotentialUndefinedSymbol(const GlobalValue *dcl, bool isFunc);
void addPotentialUndefinedSymbol(const object::BasicSymbolRef &Sym,
bool isFunc);

/// Add a defined symbol to the list.
void addDefinedSymbol(const char *Name, const GlobalValue *Def,
bool IsFunction);

/// Add a function symbol as defined to the list.
void addDefinedFunctionSymbol(const Function *f);
void addDefinedFunctionSymbol(const char *Name, const Function *F);
void addDefinedSymbol(const char *Name, const GlobalValue *def,
bool isFunction);

/// Add a data symbol as defined to the list.
void addDefinedDataSymbol(const GlobalValue *v);
void addDefinedDataSymbol(const char *Name, const GlobalValue *V);
void addDefinedDataSymbol(const object::BasicSymbolRef &Sym);
void addDefinedDataSymbol(const char*Name, const GlobalValue *v);

/// Add global symbols from module-level ASM to the defined or undefined
/// lists.
bool addAsmGlobalSymbols(std::string &errMsg);
/// Add a function symbol as defined to the list.
void addDefinedFunctionSymbol(const object::BasicSymbolRef &Sym);
void addDefinedFunctionSymbol(const char *Name, const Function *F);

/// Add a global symbol from module-level ASM to the defined list.
void addAsmGlobalSymbol(const char *, lto_symbol_attributes scope);
Expand Down
10 changes: 8 additions & 2 deletions include/llvm/Object/IRObjectFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ class IRObjectFile : public SymbolicFile {
std::vector<std::pair<std::string, uint32_t>> AsmSymbols;

public:
IRObjectFile(std::unique_ptr<MemoryBuffer> Object, std::error_code &EC,
LLVMContext &Context);
IRObjectFile(std::unique_ptr<MemoryBuffer> Object, std::unique_ptr<Module> M);
~IRObjectFile();
void moveSymbolNext(DataRefImpl &Symb) const override;
std::error_code printSymbolName(raw_ostream &OS,
Expand All @@ -39,6 +38,13 @@ class IRObjectFile : public SymbolicFile {
basic_symbol_iterator symbol_begin_impl() const override;
basic_symbol_iterator symbol_end_impl() const override;

const Module &getModule() const {
return const_cast<IRObjectFile*>(this)->getModule();
}
Module &getModule() {
return *M;
}

static inline bool classof(const Binary *v) {
return v->isIR();
}
Expand Down
2 changes: 1 addition & 1 deletion lib/LTO/LTOCodeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ void LTOCodeGenerator::initializeLTOPasses() {
}

bool LTOCodeGenerator::addModule(LTOModule* mod, std::string& errMsg) {
bool ret = IRLinker.linkInModule(mod->getLLVVMModule(), &errMsg);
bool ret = IRLinker.linkInModule(&mod->getModule(), &errMsg);

const std::vector<const char*> &undefs = mod->getAsmUndefinedRefs();
for (int i = 0, e = undefs.size(); i != e; ++i)
Expand Down
Loading

0 comments on commit 1c9687e

Please sign in to comment.