Skip to content

Commit

Permalink
LTO: Port the legacy LTO API to ModuleSymbolTable.
Browse files Browse the repository at this point in the history
Differential Revision: https://reviews.llvm.org/D27078

llvm-svn: 289576
  • Loading branch information
pcc committed Dec 13, 2016
1 parent 06df402 commit 77f4c30
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 33 deletions.
24 changes: 12 additions & 12 deletions llvm/include/llvm/LTO/legacy/LTOModule.h
Expand Up @@ -19,6 +19,7 @@
#include "llvm/ADT/StringSet.h"
#include "llvm/IR/Module.h"
#include "llvm/Object/IRObjectFile.h"
#include "llvm/Object/ModuleSymbolTable.h"
#include "llvm/Target/TargetMachine.h"
#include <string>
#include <vector>
Expand Down Expand Up @@ -47,7 +48,9 @@ struct LTOModule {

std::string LinkerOpts;

std::unique_ptr<object::IRObjectFile> IRFile;
std::unique_ptr<Module> Mod;
MemoryBufferRef MBRef;
ModuleSymbolTable SymTab;
std::unique_ptr<TargetMachine> _target;
std::vector<NameAndAttributes> _symbols;

Expand All @@ -56,7 +59,8 @@ struct LTOModule {
StringMap<NameAndAttributes> _undefines;
std::vector<StringRef> _asm_undefines;

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

public:
~LTOModule();
Expand Down Expand Up @@ -108,14 +112,10 @@ struct LTOModule {
size_t length, const TargetOptions &options,
StringRef path);

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

std::unique_ptr<Module> takeModule() { return IRFile->takeModule(); }
std::unique_ptr<Module> takeModule() { return std::move(Mod); }

/// Return the Module's target triple.
const std::string &getTargetTriple() {
Expand Down Expand Up @@ -166,19 +166,19 @@ struct LTOModule {
void parseSymbols();

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

/// Add a defined symbol to the list.
void addDefinedSymbol(StringRef Name, const GlobalValue *def,
bool isFunction);

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

/// Add a function symbol as defined to the list.
void addDefinedFunctionSymbol(const object::BasicSymbolRef &Sym);
void addDefinedFunctionSymbol(ModuleSymbolTable::Symbol Sym);
void addDefinedFunctionSymbol(StringRef Name, const Function *F);

/// Add a global symbol from module-level ASM to the defined list.
Expand Down
40 changes: 19 additions & 21 deletions llvm/lib/LTO/LTOModule.cpp
Expand Up @@ -48,9 +48,11 @@
using namespace llvm;
using namespace llvm::object;

LTOModule::LTOModule(std::unique_ptr<object::IRObjectFile> Obj,
LTOModule::LTOModule(std::unique_ptr<Module> M, MemoryBufferRef MBRef,
llvm::TargetMachine *TM)
: IRFile(std::move(Obj)), _target(TM) {}
: Mod(std::move(M)), MBRef(MBRef), _target(TM) {
SymTab.addModule(Mod.get());
}

LTOModule::~LTOModule() {}

Expand All @@ -76,7 +78,7 @@ bool LTOModule::isBitcodeFile(StringRef Path) {
bool LTOModule::isThinLTO() {
// Right now the detection is only based on the summary presence. We may want
// to add a dedicated flag at some point.
Expected<bool> Result = hasGlobalValueSummary(IRFile->getMemoryBufferRef());
Expected<bool> Result = hasGlobalValueSummary(MBRef);
if (!Result) {
logAllUnhandledErrors(Result.takeError(), errs(), "");
return false;
Expand Down Expand Up @@ -233,10 +235,7 @@ LTOModule::makeLTOModule(MemoryBufferRef Buffer, const TargetOptions &options,
march->createTargetMachine(TripleStr, CPU, FeatureStr, options, None);
M->setDataLayout(target->createDataLayout());

std::unique_ptr<object::IRObjectFile> IRObj(
new object::IRObjectFile(Buffer, std::move(M)));

std::unique_ptr<LTOModule> Ret(new LTOModule(std::move(IRObj), target));
std::unique_ptr<LTOModule> Ret(new LTOModule(std::move(M), Buffer, target));
Ret->parseSymbols();
Ret->parseMetadata();

Expand Down Expand Up @@ -344,15 +343,15 @@ void LTOModule::addObjCClassRef(const GlobalVariable *clgv) {
info.symbol = clgv;
}

void LTOModule::addDefinedDataSymbol(const object::BasicSymbolRef &Sym) {
void LTOModule::addDefinedDataSymbol(ModuleSymbolTable::Symbol Sym) {
SmallString<64> Buffer;
{
raw_svector_ostream OS(Buffer);
Sym.printName(OS);
SymTab.printSymbolName(OS, Sym);
Buffer.c_str();
}

const GlobalValue *V = IRFile->getSymbolGV(Sym.getRawDataRefImpl());
const GlobalValue *V = Sym.get<GlobalValue *>();
addDefinedDataSymbol(Buffer, V);
}

Expand Down Expand Up @@ -406,16 +405,15 @@ void LTOModule::addDefinedDataSymbol(StringRef Name, const GlobalValue *v) {
}
}

void LTOModule::addDefinedFunctionSymbol(const object::BasicSymbolRef &Sym) {
void LTOModule::addDefinedFunctionSymbol(ModuleSymbolTable::Symbol Sym) {
SmallString<64> Buffer;
{
raw_svector_ostream OS(Buffer);
Sym.printName(OS);
SymTab.printSymbolName(OS, Sym);
Buffer.c_str();
}

const Function *F =
cast<Function>(IRFile->getSymbolGV(Sym.getRawDataRefImpl()));
const Function *F = cast<Function>(Sym.get<GlobalValue *>());
addDefinedFunctionSymbol(Buffer, F);
}

Expand Down Expand Up @@ -546,12 +544,12 @@ void LTOModule::addAsmGlobalSymbolUndef(StringRef name) {
}

/// Add a symbol which isn't defined just yet to a list to be resolved later.
void LTOModule::addPotentialUndefinedSymbol(const object::BasicSymbolRef &Sym,
void LTOModule::addPotentialUndefinedSymbol(ModuleSymbolTable::Symbol Sym,
bool isFunc) {
SmallString<64> name;
{
raw_svector_ostream OS(name);
Sym.printName(OS);
SymTab.printSymbolName(OS, Sym);
name.c_str();
}

Expand All @@ -565,7 +563,7 @@ void LTOModule::addPotentialUndefinedSymbol(const object::BasicSymbolRef &Sym,

info.name = IterBool.first->first();

const GlobalValue *decl = IRFile->getSymbolGV(Sym.getRawDataRefImpl());
const GlobalValue *decl = Sym.dyn_cast<GlobalValue *>();

if (decl->hasExternalWeakLinkage())
info.attributes = LTO_SYMBOL_DEFINITION_WEAKUNDEF;
Expand All @@ -577,9 +575,9 @@ void LTOModule::addPotentialUndefinedSymbol(const object::BasicSymbolRef &Sym,
}

void LTOModule::parseSymbols() {
for (auto &Sym : IRFile->symbols()) {
const GlobalValue *GV = IRFile->getSymbolGV(Sym.getRawDataRefImpl());
uint32_t Flags = Sym.getFlags();
for (auto Sym : SymTab.symbols()) {
auto *GV = Sym.dyn_cast<GlobalValue *>();
uint32_t Flags = SymTab.getSymbolFlags(Sym);
if (Flags & object::BasicSymbolRef::SF_FormatSpecific)
continue;

Expand All @@ -589,7 +587,7 @@ void LTOModule::parseSymbols() {
SmallString<64> Buffer;
{
raw_svector_ostream OS(Buffer);
Sym.printName(OS);
SymTab.printSymbolName(OS, Sym);
Buffer.c_str();
}
StringRef Name(Buffer);
Expand Down

0 comments on commit 77f4c30

Please sign in to comment.