diff --git a/cpp/src/gandiva/context_helper.cc b/cpp/src/gandiva/context_helper.cc index 752fbea4c331..c60e952292d4 100644 --- a/cpp/src/gandiva/context_helper.cc +++ b/cpp/src/gandiva/context_helper.cc @@ -47,6 +47,13 @@ arrow::Status ExportedContextFunctions::AddMappings(Engine* engine) const { args, reinterpret_cast(gdv_fn_context_arena_malloc)); + // gdv_fn_context_arena_used_ptr + args = {types->i64_type()}; // int64_t context_ptr + + engine->AddGlobalMappingForFunc("gdv_fn_context_arena_used_ptr", types->i8_ptr_type(), + args, + reinterpret_cast(gdv_fn_context_arena_used_ptr)); + // gdv_fn_context_arena_reset args = {types->i64_type()}; // int64_t context_ptr @@ -70,6 +77,11 @@ uint8_t* gdv_fn_context_arena_malloc(int64_t context_ptr, int32_t size) { return context->arena()->Allocate(size); } +uint8_t* gdv_fn_context_arena_used_ptr(int64_t context_ptr) { + auto context = reinterpret_cast(context_ptr); + return context->arena()->used_ptr(); +} + void gdv_fn_context_arena_reset(int64_t context_ptr) { auto context = reinterpret_cast(context_ptr); return context->arena()->Reset(); diff --git a/cpp/src/gandiva/execution_context.h b/cpp/src/gandiva/execution_context.h index 5e3d6f3fe3b9..efa546874456 100644 --- a/cpp/src/gandiva/execution_context.h +++ b/cpp/src/gandiva/execution_context.h @@ -41,17 +41,6 @@ class ExecutionContext { SimpleArena* arena() { return &arena_; } - // Byte offset of the arena's "used" flag from the start of a context. Generated code - // reads the flag through the context pointer it already has. - static int64_t arena_used_offset() { - static const int64_t offset = [] { - ExecutionContext context; - return reinterpret_cast(context.arena()->used_ptr()) - - reinterpret_cast(&context); - }(); - return offset; - } - void Reset() { error_msg_.clear(); arena_.Reset(); diff --git a/cpp/src/gandiva/gdv_function_stubs.h b/cpp/src/gandiva/gdv_function_stubs.h index 89abe3fbf678..6a86b7bd55b3 100644 --- a/cpp/src/gandiva/gdv_function_stubs.h +++ b/cpp/src/gandiva/gdv_function_stubs.h @@ -80,6 +80,8 @@ void gdv_fn_context_set_error_msg(int64_t context_ptr, const char* err_msg); GANDIVA_EXPORT uint8_t* gdv_fn_context_arena_malloc(int64_t context_ptr, int32_t data_len); +uint8_t* gdv_fn_context_arena_used_ptr(int64_t context_ptr); + void gdv_fn_context_arena_reset(int64_t context_ptr); bool in_expr_lookup_int32(int64_t ptr, int32_t value, bool in_validity); diff --git a/cpp/src/gandiva/llvm_generator.cc b/cpp/src/gandiva/llvm_generator.cc index e910352189ff..9e157058234d 100644 --- a/cpp/src/gandiva/llvm_generator.cc +++ b/cpp/src/gandiva/llvm_generator.cc @@ -26,7 +26,6 @@ #include "gandiva/bitmap_accumulator.h" #include "gandiva/decimal_ir.h" #include "gandiva/dex.h" -#include "gandiva/execution_context.h" #include "gandiva/expr_decomposer.h" #include "gandiva/expression.h" #include "gandiva/llvm_types.h" @@ -398,6 +397,13 @@ Status LLVMGenerator::CodeGenExprValue(DexPtr value_expr, int buffer_count, // add jump to "loop block" at the end of the "setup block". builder->SetInsertPoint(loop_entry); + llvm::Value* arena_used_ptr = nullptr; + if (visitor.has_arena_allocs()) { + // Get the arena state pointer once per generated function instead of baking the + // ExecutionContext layout into the generated code. + arena_used_ptr = AddFunctionCall("gdv_fn_context_arena_used_ptr", + types()->i8_ptr_type(), {arg_context_ptr}); + } builder->CreateBr(loop_body); // save the value in the output vector. @@ -437,11 +443,8 @@ Status LLVMGenerator::CodeGenExprValue(DexPtr value_expr, int buffer_count, llvm::BasicBlock::Create(*context(), "arena_reset", fn, loop_exit); llvm::BasicBlock* loop_latch = llvm::BasicBlock::Create(*context(), "loop_latch", fn, loop_exit); - llvm::Value* used_addr = builder->CreateAdd( - arg_context_ptr, types()->i64_constant(ExecutionContext::arena_used_offset())); - llvm::Value* arena_used = builder->CreateLoad( - types()->i8_type(), builder->CreateIntToPtr(used_addr, types()->i8_ptr_type()), - "arena_used"); + llvm::Value* arena_used = + builder->CreateLoad(types()->i8_type(), arena_used_ptr, "arena_used"); builder->CreateCondBr( builder->CreateICmpNE(arena_used, types()->i8_constant(0), "arena_used?"), arena_reset, loop_latch); diff --git a/cpp/src/gandiva/tests/projector_test.cc b/cpp/src/gandiva/tests/projector_test.cc index fe60ea9c91ef..557e7807c596 100644 --- a/cpp/src/gandiva/tests/projector_test.cc +++ b/cpp/src/gandiva/tests/projector_test.cc @@ -1268,10 +1268,10 @@ TEST_F(TestProjector, TestLevenshtein) { EXPECT_ARROW_ARRAY_EQUALS(exp_lev, outputs.at(0)); } -// Per-row arena scratch space is released after each row only if the expression contains -// a function that returns a string. levenshtein returns an int32 but takes -// (shorter_len + 1) * 2 ints of arena scratch per row, so on its own it holds that -// memory until the end of the batch. +// Before this fix, per-row arena scratch space was released after each row only if the +// expression contained a function that returned a string. levenshtein returns an int32 +// but takes (shorter_len + 1) * 2 ints of arena scratch per row, so on its own it holds +// that memory until the end of the batch. TEST_F(TestProjector, TestLevenshteinArenaGrowth) { constexpr int kNumRecords = 1 << 18; constexpr int kStringLen = 16;