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_; }; 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);