Skip to content

Commit

Permalink
[LLVMIRCodeGen] Make it possible for backends to provide custom logic…
Browse files Browse the repository at this point in the history
… to decide which symbols need to be preserved during the LLVM's internalizeModule pass

Some backends may have a custom logic for deciding which symbols need to be preserved.
  • Loading branch information
opti-mix committed Apr 29, 2019
1 parent b2e8f3d commit 6d4f9c2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
3 changes: 3 additions & 0 deletions include/glow/LLVMIRCodeGen/LLVMIRGen.h
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,9 @@ class LLVMIRGen {
virtual void markArgAsUnspecialized(llvm::Value *val);
/// \returns bit-width of the target size_t.
virtual unsigned getTargetSizeTWidth() const;
/// \returns true if a global symbol \p GV needs to be preserved in the module
/// and not interalized during optimizations.
virtual bool preserveSymbol(const llvm::GlobalValue &GV);
};

} // namespace glow
Expand Down
28 changes: 16 additions & 12 deletions lib/LLVMIRCodeGen/Pipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,27 +54,31 @@ using namespace glow;
using llvm::dyn_cast;
using llvm::isa;

bool LLVMIRGen::preserveSymbol(const llvm::GlobalValue &GV) {
auto name = GV.getName();
// Do not preserve any internal symbols, which typically have no name or
// start with libjit_.
if (name.empty() || name.startswith("libjit_"))
return false;
return true;
}

void LLVMIRGen::optimizeLLVMModule(llvm::Function *F, llvm::TargetMachine &TM) {
auto *M = F->getParent();

// Make all of the definitions from libjit and unnamed symbols internal and
// optimizable. Everything else should be preserved as is.
auto preserveSymbols = [=](const llvm::GlobalValue &GV) {
auto name = GV.getName();
auto preserveSymbolCallback = [&](const llvm::GlobalValue &GV) -> bool {
// Do not internalize declarations.
if (GV.isDeclaration())
if (GV.isDeclaration()) {
return true;
// Do not preserve any internal symbols, which typically have no name or
// start with jit_
if (name.empty() || name.startswith("libjit_"))
return false;
return true;
}
return preserveSymbol(GV);
};

// Internalize functions libjit. In this part of the code we change the
// visibility of the symbols in the module and make 'main' the only visibile
// function.
llvm::internalizeModule(*M, preserveSymbols);
// Internalize functions in the module using a backend-specific logic.
// Typically only the entry point would be preserved.
llvm::internalizeModule(*M, preserveSymbolCallback);

// Next, we remove all of the 'no-inline' attributes that clang in -O0 adds to
// all functions.
Expand Down

0 comments on commit 6d4f9c2

Please sign in to comment.