Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 40 additions & 4 deletions cpp/src/gandiva/engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -449,12 +449,47 @@ Status Engine::LoadPreCompiledIR() {

std::unique_ptr<llvm::MemoryBuffer> buffer = std::move(buffer_or_error.get());

/// Parse the IR module.
/// Parse the IR module. Function bodies are only materialized when linked.
llvm::Expected<std::unique_ptr<llvm::Module>> module_or_error =
llvm::getOwningLazyBitcodeModule(std::move(buffer), *context());
// NOTE: llvm::handleAllErrors() fails linking with RTTI-disabled LLVM builds
// (ARROW-5148)
ARROW_RETURN_NOT_OK(VerifyAndLinkModule(*module_, std::move(module_or_error)));
ARROW_ASSIGN_OR_RAISE(
precompiled_module_,
AsArrowResult(module_or_error, "Could not parse precompiled IR: "));
precompiled_module_->setDataLayout(module_->getDataLayout());

// Declare the pre-compiled functions in module_, so that code generation can refer to
// them. Linking all ~1600 function bodies for every expression dominates compile time,
// so LinkPreCompiledIR() links in just the ones that end up being used.
for (const llvm::Function& fn : *precompiled_module_) {
if (fn.isDeclaration() || fn.hasLocalLinkage() ||
module_->getNamedValue(fn.getName()) != nullptr) {
continue;
}
auto* decl = llvm::Function::Create(
fn.getFunctionType(), llvm::GlobalValue::ExternalLinkage, fn.getName(), *module_);
decl->setCallingConv(fn.getCallingConv());
decl->setAttributes(fn.getAttributes());
precompiled_declarations_.push_back(decl);
}
return Status::OK();
}

Status Engine::LinkPreCompiledIR() {
if (precompiled_module_ == nullptr) {
return Status::OK();
}
// LinkOnlyNeeded links the definitions for declarations present in module_ (and
// whatever those depend on), so drop the declarations nothing refers to.
for (llvm::Function* decl : precompiled_declarations_) {
if (decl->use_empty()) {
decl->eraseFromParent();
}
}
precompiled_declarations_.clear();

ARROW_RETURN_IF(llvm::Linker::linkModules(*module_, std::move(precompiled_module_),
llvm::Linker::Flags::LinkOnlyNeeded),
Status::CodeGenError("failed to link precompiled IR module"));
return Status::OK();
}

Expand Down Expand Up @@ -572,6 +607,7 @@ static void OptimizeModuleWithLegacyPassManager(llvm::Module& module,
// Optimise and compile the module.
Status Engine::FinalizeModule() {
if (!cached_) {
ARROW_RETURN_NOT_OK(LinkPreCompiledIR());
ARROW_RETURN_NOT_OK(RemoveUnusedFunctions());

if (optimize_) {
Expand Down
8 changes: 8 additions & 0 deletions cpp/src/gandiva/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ class GANDIVA_EXPORT Engine {
/// the main module.
Status LoadPreCompiledIR();

/// Link the pre-compiled functions referenced by module_ into it.
Status LinkPreCompiledIR();

// load external pre-compiled bitcodes into module
Status LoadExternalPreCompiledIR();

Expand All @@ -129,6 +132,11 @@ class GANDIVA_EXPORT Engine {
std::unique_ptr<llvm::orc::LLJIT> lljit_;
std::unique_ptr<llvm::IRBuilder<>> ir_builder_;
std::unique_ptr<llvm::Module> module_;
/// The pre-compiled function library, loaded lazily. Only the functions that the
/// generated code refers to get materialized and linked into module_.
std::unique_ptr<llvm::Module> precompiled_module_;
/// Declarations added to module_ for the functions in precompiled_module_.
std::vector<llvm::Function*> precompiled_declarations_;
LLVMTypes types_;

std::vector<std::string> functions_to_compile_;
Expand Down
Loading