From 1dea52145702010528ec792d6846c8c56d36a401 Mon Sep 17 00:00:00 2001 From: Logan Riggs Date: Fri, 30 Jan 2026 00:34:22 -0800 Subject: [PATCH 1/2] GH-48159 [C++][Gandiva] Projector make is significantly slower after move to OrcJIT (#49063) ### 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 Issue: #48159 Lead-authored-by: logan.riggs@gmail.com Co-authored-by: Logan Riggs Signed-off-by: Sutou Kouhei --- cpp/src/gandiva/engine.cc | 32 +++++++++++++++++++++----------- cpp/src/gandiva/engine.h | 6 ++++-- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/cpp/src/gandiva/engine.cc b/cpp/src/gandiva/engine.cc index 5806413919d4..8c7beb67b04b 100644 --- a/cpp/src/gandiva/engine.cc +++ b/cpp/src/gandiva/engine.cc @@ -217,7 +217,10 @@ Status UseJITLinkIfEnabled(llvm::orc::LLJITBuilder& jit_builder) { Result> BuildJIT( llvm::orc::JITTargetMachineBuilder jtmb, - std::optional>& object_cache) { + std::shared_ptr target_machine, + std::optional> object_cache) { + auto data_layout = target_machine->createDataLayout(); + llvm::orc::LLJITBuilder jit_builder; #ifdef JIT_LINK_SUPPORTED @@ -225,20 +228,20 @@ Result> BuildJIT( #endif jit_builder.setJITTargetMachineBuilder(std::move(jtmb)); + jit_builder.setDataLayout(std::make_optional(data_layout)); + if (object_cache.has_value()) { jit_builder.setCompileFunctionCreator( - [&object_cache](llvm::orc::JITTargetMachineBuilder JTMB) + [tm = std::move(target_machine), + &object_cache](llvm::orc::JITTargetMachineBuilder JTMB) -> llvm::Expected> { - auto target_machine = JTMB.createTargetMachine(); - if (!target_machine) { - return target_machine.takeError(); - } // after compilation, the object code will be stored into the given object // cache - return std::make_unique( - std::move(*target_machine), &object_cache.value().get()); + return std::make_unique(*tm, + &object_cache.value().get()); }); } + auto maybe_jit = jit_builder.create(); ARROW_ASSIGN_OR_RAISE(auto jit, AsArrowResult(maybe_jit, "Could not create LLJIT instance: ")); @@ -292,7 +295,7 @@ void Engine::InitOnce() { Engine::Engine(const std::shared_ptr& conf, std::unique_ptr lljit, - std::unique_ptr target_machine, bool cached) + std::shared_ptr target_machine, bool cached) : context_(std::make_unique()), lljit_(std::move(lljit)), ir_builder_(std::make_unique>(*context_)), @@ -342,14 +345,21 @@ Result> Engine::Make( std::optional> object_cache) { std::call_once(llvm_init_once_flag, InitOnce); + // Create the target machine ARROW_ASSIGN_OR_RAISE(auto jtmb, MakeTargetMachineBuilder(*conf)); - ARROW_ASSIGN_OR_RAISE(auto jit, BuildJIT(jtmb, object_cache)); auto maybe_tm = jtmb.createTargetMachine(); ARROW_ASSIGN_OR_RAISE(auto target_machine, AsArrowResult(maybe_tm, "Could not create target machine: ")); + auto shared_target_machine = + std::shared_ptr(std::move(target_machine)); + + // Build the LLJIT instance + ARROW_ASSIGN_OR_RAISE(auto jit, + BuildJIT(std::move(jtmb), shared_target_machine, object_cache)); + std::unique_ptr engine{ - new Engine(conf, std::move(jit), std::move(target_machine), cached)}; + new Engine(conf, std::move(jit), std::move(shared_target_machine), cached)}; ARROW_RETURN_NOT_OK(engine->Init()); return engine; diff --git a/cpp/src/gandiva/engine.h b/cpp/src/gandiva/engine.h index 3c8914a7b4d9..20165787cb66 100644 --- a/cpp/src/gandiva/engine.h +++ b/cpp/src/gandiva/engine.h @@ -96,7 +96,7 @@ class GANDIVA_EXPORT Engine { private: Engine(const std::shared_ptr& conf, std::unique_ptr lljit, - std::unique_ptr target_machine, bool cached); + std::shared_ptr target_machine, bool cached); // Post construction init. This _must_ be called after the constructor. Status Init(); @@ -130,7 +130,9 @@ class GANDIVA_EXPORT Engine { bool functions_loaded_ = false; std::shared_ptr function_registry_; std::string module_ir_; - std::unique_ptr target_machine_; + // The lifetime of the TargetMachine is shared with LLJIT. This prevents unnecessary + // duplication of this expensive object. + std::shared_ptr target_machine_; const std::shared_ptr conf_; }; From e6140c32f5f2bef549338de57d6c1a2a65b86de7 Mon Sep 17 00:00:00 2001 From: Logan Riggs Date: Fri, 30 Jan 2026 00:42:15 -0800 Subject: [PATCH 2/2] GH-49034 [C++][Gandiva] Fix binary_string to not trigger error for null strings (#49035) ### Rationale for this change The binary_string function will attempt to allocate 0 bytes of memory, which results in a null ptr being returned and the function interprets that as an error. ### What changes are included in this PR? Add kCanReturnErrors to the function definition to match other string functions. Move the check for 0 byte length input earlier in the binary_string function to prevent the 0 allocation. Add a unit test. ### Are these changes tested? Yes, unit and integration testing. ### Are there any user-facing changes? No. * GitHub Issue: #49034 Authored-by: Logan Riggs Signed-off-by: Sutou Kouhei --- cpp/src/gandiva/function_registry_string.cc | 3 ++- cpp/src/gandiva/precompiled/string_ops.cc | 10 +++++----- .../gandiva/precompiled/string_ops_test.cc | 20 +++++++++++++++---- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/cpp/src/gandiva/function_registry_string.cc b/cpp/src/gandiva/function_registry_string.cc index 7750421360e3..eb8416872da1 100644 --- a/cpp/src/gandiva/function_registry_string.cc +++ b/cpp/src/gandiva/function_registry_string.cc @@ -432,7 +432,8 @@ std::vector GetStringFunctionRegistry() { NativeFunction::kNeedsContext | NativeFunction::kCanReturnErrors), NativeFunction("binary_string", {}, DataTypeVector{utf8()}, binary(), - kResultNullIfNull, "binary_string", NativeFunction::kNeedsContext), + kResultNullIfNull, "binary_string", + NativeFunction::kNeedsContext | NativeFunction::kCanReturnErrors), NativeFunction("left", {}, DataTypeVector{utf8(), int32()}, utf8(), kResultNullIfNull, "left_utf8_int32", NativeFunction::kNeedsContext), diff --git a/cpp/src/gandiva/precompiled/string_ops.cc b/cpp/src/gandiva/precompiled/string_ops.cc index 3849cf7bdf9a..8fe7e0eff87d 100644 --- a/cpp/src/gandiva/precompiled/string_ops.cc +++ b/cpp/src/gandiva/precompiled/string_ops.cc @@ -2252,6 +2252,11 @@ const char* right_utf8_int32(gdv_int64 context, const char* text, gdv_int32 text FORCE_INLINE const char* binary_string(gdv_int64 context, const char* text, gdv_int32 text_len, gdv_int32* out_len) { + if (text_len == 0) { + *out_len = 0; + return ""; + } + gdv_binary ret = reinterpret_cast(gdv_fn_context_arena_malloc(context, text_len)); @@ -2261,11 +2266,6 @@ const char* binary_string(gdv_int64 context, const char* text, gdv_int32 text_le return ""; } - if (text_len == 0) { - *out_len = 0; - return ""; - } - // converting hex encoded string to normal string int j = 0; for (int i = 0; i < text_len; i++, j++) { diff --git a/cpp/src/gandiva/precompiled/string_ops_test.cc b/cpp/src/gandiva/precompiled/string_ops_test.cc index aaa25db0a9f8..dffcb468899f 100644 --- a/cpp/src/gandiva/precompiled/string_ops_test.cc +++ b/cpp/src/gandiva/precompiled/string_ops_test.cc @@ -1883,10 +1883,6 @@ TEST(TestStringOps, TestBinaryString) { std::string output = std::string(out_str, out_len); EXPECT_EQ(output, "TestString"); - out_str = binary_string(ctx_ptr, "", 0, &out_len); - output = std::string(out_str, out_len); - EXPECT_EQ(output, ""); - out_str = binary_string(ctx_ptr, "T", 1, &out_len); output = std::string(out_str, out_len); EXPECT_EQ(output, "T"); @@ -1912,6 +1908,22 @@ TEST(TestStringOps, TestBinaryString) { EXPECT_EQ(output, "OM"); } +TEST(TestStringOps, TestBinaryStringNull) { + // This test is only valid if it is the first to trigger a memory allocation in the + // context. + gandiva::ExecutionContext ctx; + uint64_t ctx_ptr = reinterpret_cast(&ctx); + gdv_int32 out_len = 0; + const char* out_str; + + std::string output; + + out_str = binary_string(ctx_ptr, "", 0, &out_len); + ASSERT_FALSE(ctx.has_error()); + output = std::string(out_str, out_len); + EXPECT_EQ(output, ""); +} + TEST(TestStringOps, TestSplitPart) { gandiva::ExecutionContext ctx; uint64_t ctx_ptr = reinterpret_cast(&ctx);