Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions cpp/src/gandiva/context_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ arrow::Status ExportedContextFunctions::AddMappings(Engine* engine) const {
args,
reinterpret_cast<void*>(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<void*>(gdv_fn_context_arena_used_ptr));

// gdv_fn_context_arena_reset
args = {types->i64_type()}; // int64_t context_ptr

Expand All @@ -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<gandiva::ExecutionContext*>(context_ptr);
return context->arena()->used_ptr();
}

void gdv_fn_context_arena_reset(int64_t context_ptr) {
auto context = reinterpret_cast<gandiva::ExecutionContext*>(context_ptr);
return context->arena()->Reset();
Expand Down
11 changes: 0 additions & 11 deletions cpp/src/gandiva/execution_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<char*>(context.arena()->used_ptr()) -
reinterpret_cast<char*>(&context);
}();
return offset;
}

void Reset() {
error_msg_.clear();
arena_.Reset();
Expand Down
2 changes: 2 additions & 0 deletions cpp/src/gandiva/gdv_function_stubs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
15 changes: 9 additions & 6 deletions cpp/src/gandiva/llvm_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions cpp/src/gandiva/tests/projector_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading