From ef89f613fca4bb462e4dbc8d7fba50cf6a22c40a Mon Sep 17 00:00:00 2001 From: Logan Date: Wed, 11 Nov 2026 15:00:11 -0800 Subject: [PATCH] GH: Gandiva: link only the precompiled functions a query uses Gandiva links its entire precompiled bitcode library (~1600 functions) into every expression module and then relies on Internalize + global DCE to strip the unused ones. That link-and-prune runs on every uncached compile and is a large fixed cost independent of the expression. Instead, parse the precompiled module lazily and add only *declarations* for its functions to the module up front; then at FinalizeModule use llvm::Linker::LinkOnlyNeeded to pull in just the function bodies the generated code actually references (and their dependencies), dropping declarations that went unused. No change to generated code or results; this only reduces per-compile linking work. Cuts the fixed compile floor substantially on cold compiles. --- cpp/src/gandiva/engine.cc | 44 +++++++++++++++++++++++++++++++++++---- cpp/src/gandiva/engine.h | 8 +++++++ 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/cpp/src/gandiva/engine.cc b/cpp/src/gandiva/engine.cc index c868f059acb..b8d4ec78421 100644 --- a/cpp/src/gandiva/engine.cc +++ b/cpp/src/gandiva/engine.cc @@ -449,12 +449,47 @@ Status Engine::LoadPreCompiledIR() { std::unique_ptr buffer = std::move(buffer_or_error.get()); - /// Parse the IR module. + /// Parse the IR module. Function bodies are only materialized when linked. llvm::Expected> 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(); } @@ -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_) { diff --git a/cpp/src/gandiva/engine.h b/cpp/src/gandiva/engine.h index b172918a9b3..6468954e1ac 100644 --- a/cpp/src/gandiva/engine.h +++ b/cpp/src/gandiva/engine.h @@ -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(); @@ -129,6 +132,11 @@ class GANDIVA_EXPORT Engine { std::unique_ptr lljit_; std::unique_ptr> ir_builder_; std::unique_ptr 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 precompiled_module_; + /// Declarations added to module_ for the functions in precompiled_module_. + std::vector precompiled_declarations_; LLVMTypes types_; std::vector functions_to_compile_;