Skip to content

Conversation

@lriggs
Copy link
Contributor

@lriggs lriggs commented Jan 29, 2026

Rationale for this change

Reduces LLVM TargetMachine object creation from 3 to 1. This object is expensive to create and the extra copies weren't needed.

What changes are included in this PR?

Refactor the Engine class to only create one target machine and pass that to the necessary functions.

Before the change (3 TargetMachines created):

First TargetMachine: In Engine::Make(), MakeTargetMachineBuilder() is called, then BuildJIT() is called. Inside LLJITBuilder::create(), when prepareForConstruction() runs, if no DataLayout was set, it calls JTMB->getDefaultDataLayoutForTarget() which creates a temporary TargetMachine just to get the DataLayout.

Second TargetMachine: Inside BuildJIT(), when setCompileFunctionCreator is used with the lambda, that lambda calls JTMB.createTargetMachine() to create a TargetMachine for the TMOwningSimpleCompiler.

Third TargetMachine: Back in Engine::Make(), after BuildJIT() returns, there's an explicit call to jtmb.createTargetMachine() to create target_machine_ for the Engine.

After the change (1 TargetMachine created):

The key changes are:

Create TargetMachine first: The code now creates the TargetMachine explicitly at the start of the Engine in Engine::Make. That machine is passed to BuildJIT. In BuildJiIT that machine's DataLayout is sent to LLJITBuilder which prevents prepareForConstruction() from calling getDefaultDataLayoutForTarget() (which would create a temporary TargetMachine).

Use SimpleCompiler instead of TMOwningSimpleCompiler:
SimpleCompiler takes a reference to an existing TargetMachine rather than owning one, so no new TargetMachine is created.
A shared_ptr is used to ensure that TargetMachine stays around for the lifetime of the LLJIT instance.

Are these changes tested?

Yes, unit and integration.

Are there any user-facing changes?

No.

@github-actions
Copy link

⚠️ GitHub issue #48159 has been automatically assigned in GitHub to PR creator.

Copy link
Member

@kou kou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

@kou kou merged commit e40efd8 into apache:main Jan 30, 2026
49 checks passed
@kou kou removed the awaiting review Awaiting review label Jan 30, 2026
@github-actions github-actions bot added the awaiting merge Awaiting merge label Jan 30, 2026
@raulcd
Copy link
Member

raulcd commented Jan 30, 2026

I think this broke some of our Linux Packaging jobs:

FAILED: src/gandiva/CMakeFiles/gandiva_objlib.dir/engine.cc.o 
/usr/bin/c++ -DARROW_HAVE_NEON -DARROW_WITH_TIMING_TESTS -DBOOST_ALL_NO_LIB -DGANDIVA_EXPORTING -I"/build/apache-arrow-24.0.0~dev20260130/cpp_build/src" -I"/build/apache-arrow-24.0.0~dev20260130/cpp/src" -I"/build/apache-arrow-24.0.0~dev20260130/cpp/src/generated" -isystem /usr/lib/llvm-14/include -isystem "/build/apache-arrow-24.0.0~dev20260130/cpp_build/xsimd_ep/src/xsimd_ep-install/include" -Wredundant-move -Wno-noexcept-type -g -O2 -ffile-prefix-map=/build/apache-arrow-24.0.0~dev20260130=. -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 -fdiagnostics-color=always  -Wall -Wno-conversion -Wno-sign-conversion -Wdate-time -Wimplicit-fallthrough -Wunused-result -fno-semantic-interposition -march=armv8-a  -O2 -g -DNDEBUG -ftree-vectorize -ggdb  -fPIC -Wmissing-declarations -std=c++20 -MD -MT src/gandiva/CMakeFiles/gandiva_objlib.dir/engine.cc.o -MF src/gandiva/CMakeFiles/gandiva_objlib.dir/engine.cc.o.d -o src/gandiva/CMakeFiles/gandiva_objlib.dir/engine.cc.o -c '/build/apache-arrow-24.0.0~dev20260130/cpp/src/gandiva/engine.cc'
/build/apache-arrow-24.0.0~dev20260130/cpp/src/gandiva/engine.cc: In function ‘arrow::Result<std::unique_ptr<llvm::orc::LLJIT> > gandiva::{anonymous}::BuildJIT(llvm::orc::JITTargetMachineBuilder, std::shared_ptr<llvm::TargetMachine>, std::optional<std::reference_wrapper<gandiva::GandivaObjectCache> >)’:
/build/apache-arrow-24.0.0~dev20260130/cpp/src/gandiva/engine.cc:233:47: error: cannot convert ‘std::enable_if_t<true, std::optional<llvm::DataLayout> >’ {aka ‘std::optional<llvm::DataLayout>’} to ‘llvm::Optional<llvm::DataLayout>’
  233 |   jit_builder.setDataLayout(std::make_optional(data_layout));
      |                             ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~
      |                                               |
      |                                               std::enable_if_t<true, std::optional<llvm::DataLayout> > {aka std::optional<llvm::DataLayout>}
In file included from /build/apache-arrow-24.0.0~dev20260130/cpp/src/gandiva/engine.cc:50:
/usr/include/llvm-14/llvm/ExecutionEngine/Orc/LLJIT.h:314:50: note:   initializing argument 1 of ‘SetterImpl& llvm::orc::LLJITBuilderSetters< <template-parameter-1-1>, <template-parameter-1-2>, <template-parameter-1-3> >::setDataLayout(llvm::Optional<llvm::DataLayout>) [with JITType = llvm::orc::LLJIT; SetterImpl = llvm::orc::LLJITBuilder; State = llvm::orc::LLJITBuilderState]’
  314 |   SetterImpl &setDataLayout(Optional<DataLayout> DL) {
      |                             ~~~~~~~~~~~~~~~~~~~~~^~
[1643/1680] /usr/bin/c++ -DARROW_HAVE_NEON -DARROW_WITH_TIMING_TESTS -DBOOST_ALL_NO_LIB -DGANDIVA_EXPORTING -I"/build/apache-arrow-24.0.0~dev20260130/cpp_build/src" -I"/build/apache-arrow-24.0.0~dev20260130/cpp/src" -I"/build/apache-arrow-24.0.0~dev20260130/cpp/src/generated" -isystem /usr/lib/llvm-14/include -isystem "/build/apache-arrow-24.0.0~dev20260130/cpp_build/xsimd_ep/src/xsimd_ep-install/include" -Wredundant-move -Wno-noexcept-type -g -O2 -ffile-prefix-map=/build/apache-arrow-24.0.0~dev20260130=. -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 -fdiagnostics-color=always  -Wall -Wno-conversion -Wno-sign-conversion -Wdate-time -Wimplicit-fallthrough -Wunused-result -fno-semantic-interposition -march=armv8-a  -O2 -g -DNDEBUG -ftree-vectorize -ggdb  -fPIC -Wmissing-declarations -std=c++20 -MD -MT src/gandiva/CMakeFiles/gandiva_objlib.dir/expression.cc.o -MF src/gandiva/CMakeFiles/gandiva_objlib.dir/expression.cc.o.d -o src/gandiva/CMakeFiles/gandiva_objlib.dir/expression.cc.o -c '/build/apache-arrow-24.0.0~dev20260130/cpp/src/gandiva/expression.cc'
[1644/1680] /usr/bin/c++ -DARROW_HAVE_NEON -DARROW_WITH_TIMING_TESTS -DBOOST_ALL_NO_LIB -DGANDIVA_EXPORTING -I"/build/apache-arrow-24.0.0~dev20260130/cpp_build/src" -I"/build/apache-arrow-24.0.0~dev20260130/cpp/src" -I"/build/apache-arrow-24.0.0~dev20260130/cpp/src/generated" -isystem /usr/lib/llvm-14/include -isystem "/build/apache-arrow-24.0.0~dev20260130/cpp_build/xsimd_ep/src/xsimd_ep-install/include" -Wredundant-move -Wno-noexcept-type -g -O2 -ffile-prefix-map=/build/apache-arrow-24.0.0~dev20260130=. -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 -fdiagnostics-color=always  -Wall -Wno-conversion -Wno-sign-conversion -Wdate-time -Wimplicit-fallthrough -Wunused-result -fno-semantic-interposition -march=armv8-a  -O2 -g -DNDEBUG -ftree-vectorize -ggdb  -fPIC -Wmissing-declarations -std=c++20 -MD -MT src/gandiva/CMakeFiles/gandiva_objlib.dir/expr_validator.cc.o -MF src/gandiva/CMakeFiles/gandiva_objlib.dir/expr_validator.cc.o.d -o src/gandiva/CMakeFiles/gandiva_objlib.dir/expr_validator.cc.o -c '/build/apache-arrow-24.0.0~dev20260130/cpp/src/gandiva/expr_validator.cc'
[1645/1680] /usr/bin/c++ -DARROW_HAVE_NEON -DARROW_WITH_TIMING_TESTS -DBOOST_ALL_NO_LIB -DGANDIVA_EXPORTING -I"/build/apache-arrow-24.0.0~dev20260130/cpp_build/src" -I"/build/apache-arrow-24.0.0~dev20260130/cpp/src" -I"/build/apache-arrow-24.0.0~dev20260130/cpp/src/generated" -isystem /usr/lib/llvm-14/include -isystem "/build/apache-arrow-24.0.0~dev20260130/cpp_build/xsimd_ep/src/xsimd_ep-install/include" -Wredundant-move -Wno-noexcept-type -g -O2 -ffile-prefix-map=/build/apache-arrow-24.0.0~dev20260130=. -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 -fdiagnostics-color=always  -Wall -Wno-conversion -Wno-sign-conversion -Wdate-time -Wimplicit-fallthrough -Wunused-result -fno-semantic-interposition -march=armv8-a  -O2 -g -DNDEBUG -ftree-vectorize -ggdb  -fPIC -Wmissing-declarations -std=c++20 -MD -MT src/gandiva/CMakeFiles/gandiva_objlib.dir/expr_decomposer.cc.o -MF src/gandiva/CMakeFiles/gandiva_objlib.dir/expr_decomposer.cc.o.d -o src/gandiva/CMakeFiles/gandiva_objlib.dir/expr_decomposer.cc.o -c '/build/apache-arrow-24.0.0~dev20260130/cpp/src/gandiva/expr_decomposer.cc'
ninja: build stopped: subcommand failed.

Seen this on main:
https://github.com/apache/arrow/actions/runs/21509565491/job/61973040262
I'll open a new issue.

Edit: I've opened:

@kou
Copy link
Member

kou commented Jan 30, 2026

Oh, sorry...

@lriggs Could you take a look at it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants