diff --git a/cpp/src/gandiva/engine.cc b/cpp/src/gandiva/engine.cc index f2541fceb6c9f..9ccf911f8b1d3 100644 --- a/cpp/src/gandiva/engine.cc +++ b/cpp/src/gandiva/engine.cc @@ -237,10 +237,9 @@ Status Engine::LoadFunctionIRs() { } /// factory method to construct the engine. -Status Engine::Make( +Result> Engine::Make( const std::shared_ptr& conf, bool cached, - std::optional> object_cache, - std::unique_ptr* out) { + std::optional> object_cache) { std::call_once(llvm_init_once_flag, InitOnce); ARROW_ASSIGN_OR_RAISE(auto jtmb, GetTargetMachineBuilder(*conf)); @@ -253,8 +252,7 @@ Status Engine::Make( new Engine(conf, std::move(jit), std::move(target_machine), cached)}; ARROW_RETURN_NOT_OK(engine->Init()); - *out = std::move(engine); - return Status::OK(); + return engine; } static arrow::Status VerifyAndLinkModule( diff --git a/cpp/src/gandiva/engine.h b/cpp/src/gandiva/engine.h index 3fc3e4145f608..4e6a6fe038c82 100644 --- a/cpp/src/gandiva/engine.h +++ b/cpp/src/gandiva/engine.h @@ -53,11 +53,10 @@ class GANDIVA_EXPORT Engine { /// \param[in] config the engine configuration /// \param[in] cached flag to mark if the module is already compiled and cached /// \param[in] object_cache an optional object_cache used for building the module - /// \param[out] engine the created engine - static Status Make( + /// \return arrow::Result containing the created engine + static Result> Make( const std::shared_ptr& config, bool cached, - std::optional> object_cache, - std::unique_ptr* engine); + std::optional> object_cache = std::nullopt); /// Add the function to the list of IR functions that need to be compiled. /// Compiling only the functions that are used by the module saves time. diff --git a/cpp/src/gandiva/engine_llvm_test.cc b/cpp/src/gandiva/engine_llvm_test.cc index 881e5bf500897..c962aeb234856 100644 --- a/cpp/src/gandiva/engine_llvm_test.cc +++ b/cpp/src/gandiva/engine_llvm_test.cc @@ -100,7 +100,7 @@ class TestEngine : public ::testing::Test { } void BuildEngine() { - ASSERT_OK(Engine::Make(TestConfiguration(), false, std::nullopt, &engine)); + ASSERT_OK_AND_ASSIGN(engine, Engine::Make(TestConfiguration(), false)); } std::unique_ptr engine; diff --git a/cpp/src/gandiva/filter.cc b/cpp/src/gandiva/filter.cc index 6bb8c2a906d21..2e9952089e948 100644 --- a/cpp/src/gandiva/filter.cc +++ b/cpp/src/gandiva/filter.cc @@ -65,9 +65,8 @@ Status Filter::Make(SchemaPtr schema, ConditionPtr condition, GandivaObjectCache obj_cache(cache, cache_key); // Build LLVM generator, and generate code for the specified expression - std::unique_ptr llvm_gen; - ARROW_RETURN_NOT_OK( - LLVMGenerator::Make(configuration, is_cached, obj_cache, &llvm_gen)); + ARROW_ASSIGN_OR_RAISE(auto llvm_gen, + LLVMGenerator::Make(configuration, is_cached, obj_cache)); if (!is_cached) { // Run the validation on the expression. diff --git a/cpp/src/gandiva/llvm_generator.cc b/cpp/src/gandiva/llvm_generator.cc index 4016bf3e775b0..148d4313c6dfa 100644 --- a/cpp/src/gandiva/llvm_generator.cc +++ b/cpp/src/gandiva/llvm_generator.cc @@ -42,18 +42,15 @@ LLVMGenerator::LLVMGenerator(bool cached, function_registry_(std::move(function_registry)), enable_ir_traces_(false) {} -Status LLVMGenerator::Make( +Result> LLVMGenerator::Make( const std::shared_ptr& config, bool cached, - std::optional> object_cache, - std::unique_ptr* llvm_generator) { - std::unique_ptr llvmgen_obj( + std::optional> object_cache) { + std::unique_ptr llvm_generator( new LLVMGenerator(cached, config->function_registry())); - ARROW_RETURN_NOT_OK( - Engine::Make(config, cached, object_cache, &(llvmgen_obj->engine_))); - *llvm_generator = std::move(llvmgen_obj); - - return Status::OK(); + ARROW_ASSIGN_OR_RAISE(auto engine, Engine::Make(config, cached, object_cache)); + llvm_generator->engine_ = std::move(engine); + return llvm_generator; } std::shared_ptr>> diff --git a/cpp/src/gandiva/llvm_generator.h b/cpp/src/gandiva/llvm_generator.h index 1a4217e1d008b..bd692e4ecb9d9 100644 --- a/cpp/src/gandiva/llvm_generator.h +++ b/cpp/src/gandiva/llvm_generator.h @@ -49,10 +49,10 @@ class FunctionHolder; class GANDIVA_EXPORT LLVMGenerator { public: /// \brief Factory method to initialize the generator. - static Status Make( + static Result> Make( const std::shared_ptr& config, bool cached, - std::optional> object_cache, - std::unique_ptr* llvm_generator); + std::optional> object_cache = + std::nullopt); /// \brief Get the cache to be used for LLVM ObjectCache. static std::shared_ptr>> diff --git a/cpp/src/gandiva/llvm_generator_test.cc b/cpp/src/gandiva/llvm_generator_test.cc index 7149d510f0e08..f35ebdd701660 100644 --- a/cpp/src/gandiva/llvm_generator_test.cc +++ b/cpp/src/gandiva/llvm_generator_test.cc @@ -47,8 +47,7 @@ class TestLLVMGenerator : public ::testing::Test { auto external_registry = std::make_shared(); auto config = config_factory(std::move(external_registry)); - std::unique_ptr generator; - ASSERT_OK(LLVMGenerator::Make(config, false, std::nullopt, &generator)); + ASSERT_OK_AND_ASSIGN(auto generator, LLVMGenerator::Make(config, false)); auto module = generator->module(); ASSERT_OK(generator->engine_->LoadFunctionIRs()); @@ -58,8 +57,7 @@ class TestLLVMGenerator : public ::testing::Test { // Verify that a valid pc function exists for every function in the registry. TEST_F(TestLLVMGenerator, VerifyPCFunctions) { - std::unique_ptr generator; - ASSERT_OK(LLVMGenerator::Make(TestConfiguration(), false, std::nullopt, &generator)); + ASSERT_OK_AND_ASSIGN(auto generator, LLVMGenerator::Make(TestConfiguration(), false)); llvm::Module* module = generator->module(); ASSERT_OK(generator->engine_->LoadFunctionIRs()); @@ -70,9 +68,8 @@ TEST_F(TestLLVMGenerator, VerifyPCFunctions) { TEST_F(TestLLVMGenerator, TestAdd) { // Setup LLVM generator to do an arithmetic add of two vectors - std::unique_ptr generator; - ASSERT_OK( - LLVMGenerator::Make(TestConfigWithIrDumping(), false, std::nullopt, &generator)); + ASSERT_OK_AND_ASSIGN(auto generator, + LLVMGenerator::Make(TestConfigWithIrDumping(), false)); Annotator annotator; auto field0 = std::make_shared("f0", arrow::int32()); diff --git a/cpp/src/gandiva/projector.cc b/cpp/src/gandiva/projector.cc index 93be623efcaae..5008207e0eb45 100644 --- a/cpp/src/gandiva/projector.cc +++ b/cpp/src/gandiva/projector.cc @@ -80,9 +80,8 @@ Status Projector::Make(SchemaPtr schema, const ExpressionVector& exprs, GandivaObjectCache obj_cache(cache, cache_key); // Build LLVM generator, and generate code for the specified expressions - std::unique_ptr llvm_gen; - ARROW_RETURN_NOT_OK( - LLVMGenerator::Make(configuration, is_cached, obj_cache, &llvm_gen)); + ARROW_ASSIGN_OR_RAISE(auto llvm_gen, + LLVMGenerator::Make(configuration, is_cached, obj_cache)); // Run the validation on the expressions. // Return if any of the expression is invalid since