Skip to content

Commit

Permalink
Rename C interface function to C function for simplicity.
Browse files Browse the repository at this point in the history
  • Loading branch information
niyue committed Nov 9, 2023
1 parent 33703f0 commit 258f613
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 60 deletions.
2 changes: 1 addition & 1 deletion cpp/src/gandiva/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ set(SRC_FILES
expression_registry.cc
exported_funcs_registry.cc
exported_funcs.cc
external_c_interface_functions.cc
external_c_functions.cc
filter.cc
function_ir_builder.cc
function_holder_maker_registry.cc
Expand Down
4 changes: 2 additions & 2 deletions cpp/src/gandiva/engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -449,8 +449,8 @@ void Engine::AddGlobalMappingForFunc(const std::string& name, llvm::Type* ret_ty

arrow::Status Engine::AddGlobalMappings() {
ARROW_RETURN_NOT_OK(ExportedFuncsRegistry::AddMappings(this));
ExternalCInterfaceFunctions c_interface_funcs(function_registry_);
return c_interface_funcs.AddMappings(this);
ExternalCFunctions c_funcs(function_registry_);
return c_funcs.AddMappings(this);
}

std::string Engine::DumpIR() {
Expand Down
4 changes: 2 additions & 2 deletions cpp/src/gandiva/exported_funcs.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ class ExportedHashFunctions : public ExportedFuncsBase {
arrow::Status AddMappings(Engine* engine) const override;
};

class ExternalCInterfaceFunctions : public ExportedFuncsBase {
class ExternalCFunctions : public ExportedFuncsBase {
public:
explicit ExternalCInterfaceFunctions(
explicit ExternalCFunctions(
std::shared_ptr<FunctionRegistry> function_registry)
: function_registry_(std::move(function_registry)) {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,39 +51,38 @@ static arrow::Result<llvm::Type*> AsLLVMType(const DataTypePtr& from_type,
// map from a NativeFunction's signature to the corresponding LLVM signature
static arrow::Result<std::pair<std::vector<llvm::Type*>, llvm::Type*>> MapToLLVMSignature(
const FunctionSignature& sig, const NativeFunction& func, LLVMTypes* types) {
std::vector<llvm::Type*> args;
args.reserve(sig.param_types().size());
std::vector<llvm::Type*> arg_llvm_types;
arg_llvm_types.reserve(sig.param_types().size());
if (func.NeedsContext()) {
args.emplace_back(types->i64_type());
arg_llvm_types.emplace_back(types->i64_type());
}
if (func.NeedsFunctionHolder()) {
args.emplace_back(types->i64_type());
arg_llvm_types.emplace_back(types->i64_type());
}
for (auto const& arg : sig.param_types()) {
if (arg->id() == arrow::Type::STRING) {
args.emplace_back(types->i8_ptr_type());
args.emplace_back(types->i32_type());
arg_llvm_types.emplace_back(types->i8_ptr_type());
arg_llvm_types.emplace_back(types->i32_type());
} else {
ARROW_ASSIGN_OR_RAISE(auto arg_llvm_type, AsLLVMType(arg, types));
args.emplace_back(arg_llvm_type);
arg_llvm_types.emplace_back(arg_llvm_type);
}
}
llvm::Type* ret_llvm_type;
if (sig.ret_type()->id() == arrow::Type::STRING) {
// for string output, the last arg is the output length
args.emplace_back(types->i32_ptr_type());
arg_llvm_types.emplace_back(types->i32_ptr_type());
ret_llvm_type = types->i8_ptr_type();
} else {
ARROW_ASSIGN_OR_RAISE(ret_llvm_type, AsLLVMType(sig.ret_type(), types));
}
auto return_type = AsLLVMType(sig.ret_type(), types);
return std::make_pair(args, ret_llvm_type);
return std::make_pair(std::move(arg_llvm_types), ret_llvm_type);
}

arrow::Status ExternalCInterfaceFunctions::AddMappings(Engine* engine) const {
auto const& c_interface_funcs = function_registry_->GetCInterfaceFunctions();
arrow::Status ExternalCFunctions::AddMappings(Engine* engine) const {
auto const& c_funcs = function_registry_->GetCFunctions();
auto types = engine->types();
for (auto& [func, func_ptr] : c_interface_funcs) {
for (auto& [func, func_ptr] : c_funcs) {
for (auto const& sig : func.signatures()) {
ARROW_ASSIGN_OR_RAISE(auto llvm_signature, MapToLLVMSignature(sig, func, types));
auto& [args, ret_llvm_type] = llvm_signature;
Expand Down
13 changes: 5 additions & 8 deletions cpp/src/gandiva/function_holder_maker_registry.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <functional>

#include "arrow/util/string.h"
#include "gandiva/function_holder.h"
#include "gandiva/interval_holder.h"
#include "gandiva/random_generator_holder.h"
Expand All @@ -27,18 +28,14 @@

namespace gandiva {

using arrow::internal::AsciiToLower;

FunctionHolderMakerRegistry::FunctionHolderMakerRegistry()
: function_holder_makers_(DefaultHolderMakers()) {}

static std::string to_lower(const std::string& str) {
std::string data = str;
std::transform(data.begin(), data.end(), data.begin(),
[](unsigned char c) { return std::tolower(c); });
return data;
}
arrow::Status FunctionHolderMakerRegistry::Register(const std::string& name,
FunctionHolderMaker holder_maker) {
function_holder_makers_.emplace(to_lower(name), std::move(holder_maker));
function_holder_makers_.emplace(AsciiToLower(name), std::move(holder_maker));
return arrow::Status::OK();
}

Expand All @@ -51,7 +48,7 @@ static arrow::Result<FunctionHolderPtr> HolderMaker(const FunctionNode& node) {

arrow::Result<FunctionHolderPtr> FunctionHolderMakerRegistry::Make(
const std::string& name, const FunctionNode& node) {
auto lowered_name = to_lower(name);
auto lowered_name = AsciiToLower(name);
auto found = function_holder_makers_.find(lowered_name);
if (found == function_holder_makers_.end()) {
return Status::Invalid("function holder not registered for function " + name);
Expand Down
10 changes: 5 additions & 5 deletions cpp/src/gandiva/function_registry.cc
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,15 @@ arrow::Status FunctionRegistry::Register(const std::vector<NativeFunction>& func
}

arrow::Status FunctionRegistry::Register(
NativeFunction func, void* stub_function_ptr,
NativeFunction func, void* c_function_ptr,
std::optional<FunctionHolderMaker> function_holder_maker) {
if (function_holder_maker.has_value()) {
// all signatures should have the same base name, use the first signature's base name
auto const& func_base_name = func.signatures().begin()->base_name();
ARROW_RETURN_NOT_OK(holder_maker_registry_.Register(
func_base_name, std::move(function_holder_maker.value())));
}
c_interface_functions_.emplace_back(func, stub_function_ptr);
c_functions_.emplace_back(func, c_function_ptr);
ARROW_RETURN_NOT_OK(FunctionRegistry::Add(std::move(func)));
return Status::OK();
}
Expand All @@ -128,9 +128,9 @@ const std::vector<std::shared_ptr<arrow::Buffer>>& FunctionRegistry::GetBitcodeB
return bitcode_memory_buffers_;
}

const std::vector<std::pair<NativeFunction, void*>>&
FunctionRegistry::GetCInterfaceFunctions() const {
return c_interface_functions_;
const std::vector<std::pair<NativeFunction, void*>>& FunctionRegistry::GetCFunctions()
const {
return c_functions_;
}

const FunctionHolderMakerRegistry& FunctionRegistry::GetFunctionHolderMakerRegistry()
Expand Down
12 changes: 6 additions & 6 deletions cpp/src/gandiva/function_registry.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,21 @@ class GANDIVA_EXPORT FunctionRegistry {
arrow::Status Register(const std::vector<NativeFunction>& funcs,
std::shared_ptr<arrow::Buffer> bitcode_buffer);

/// \brief register a C interface function into the function registry
/// \brief register a C function into the function registry
/// @param func the registered function's metadata
/// @param c_interface_function_ptr the function pointer to the
/// @param c_function_ptr the function pointer to the
/// registered function's implementation
/// @param function_holder_maker this will be used as the function holder if the
/// function requires a function holder
arrow::Status Register(
NativeFunction func, void* c_interface_function_ptr,
NativeFunction func, void* c_function_ptr,
std::optional<FunctionHolderMaker> function_holder_maker = std::nullopt);

/// \brief get a list of bitcode memory buffers saved in the registry
const std::vector<std::shared_ptr<arrow::Buffer>>& GetBitcodeBuffers() const;

/// \brief get a list of C interface functions saved in the registry
const std::vector<std::pair<NativeFunction, void*>>& GetCInterfaceFunctions() const;
/// \brief get a list of C functions saved in the registry
const std::vector<std::pair<NativeFunction, void*>>& GetCFunctions() const;

const FunctionHolderMakerRegistry& GetFunctionHolderMakerRegistry() const;

Expand All @@ -86,7 +86,7 @@ class GANDIVA_EXPORT FunctionRegistry {
std::vector<NativeFunction> pc_registry_;
SignatureMap pc_registry_map_;
std::vector<std::shared_ptr<arrow::Buffer>> bitcode_memory_buffers_;
std::vector<std::pair<NativeFunction, void*>> c_interface_functions_;
std::vector<std::pair<NativeFunction, void*>> c_functions_;
FunctionHolderMakerRegistry holder_maker_registry_;

Status Add(NativeFunction func);
Expand Down
2 changes: 0 additions & 2 deletions cpp/src/gandiva/gdv_string_function_stubs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
// specific language governing permissions and limitations
// under the License.

// #pragma once

#include "gandiva/gdv_function_stubs.h"

#include <utf8proc.h>
Expand Down
4 changes: 2 additions & 2 deletions cpp/src/gandiva/llvm_generator_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ TEST_F(TestLLVMGenerator, VerifyExtendedPCFunctions) {
});
}

TEST_F(TestLLVMGenerator, VerifyExtendedCInterfaceFunctions) {
TEST_F(TestLLVMGenerator, VerifyExtendedCFunctions) {
VerifyFunctionMapping("multiply_by_three_int32", [](auto registry) {
return TestConfigWithCInterfaceFunction(std::move(registry));
return TestConfigWithCFunction(std::move(registry));
});

VerifyFunctionMapping("multiply_by_n_int32_int32", [](auto registry) {
Expand Down
9 changes: 4 additions & 5 deletions cpp/src/gandiva/tests/projector_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3608,7 +3608,7 @@ TEST_F(TestProjector, TestExtendedFunctions) {
EXPECT_ARROW_ARRAY_EQUALS(out, outs.at(0));
}

TEST_F(TestProjector, TestExtendedCInterfaceFunctions) {
TEST_F(TestProjector, TestExtendedCFunctions) {
auto in_field = field("in", arrow::int32());
auto schema = arrow::schema({in_field});
auto out_field = field("out", arrow::int64());
Expand All @@ -3617,8 +3617,7 @@ TEST_F(TestProjector, TestExtendedCInterfaceFunctions) {

std::shared_ptr<Projector> projector;
auto external_registry = std::make_shared<FunctionRegistry>();
auto config_with_func_registry =
TestConfigWithCInterfaceFunction(std::move(external_registry));
auto config_with_func_registry = TestConfigWithCFunction(std::move(external_registry));
ARROW_EXPECT_OK(
Projector::Make(schema, {multiply}, config_with_func_registry, &projector));

Expand All @@ -3632,7 +3631,7 @@ TEST_F(TestProjector, TestExtendedCInterfaceFunctions) {
EXPECT_ARROW_ARRAY_EQUALS(out, outs.at(0));
}

TEST_F(TestProjector, TestExtendedCInterfaceFunctionsWithFunctionHolder) {
TEST_F(TestProjector, TestExtendedCFunctionsWithFunctionHolder) {
auto multiple = TreeExprBuilder::MakeLiteral(5);
auto in_field = field("in", arrow::int32());
auto schema = arrow::schema({in_field});
Expand Down Expand Up @@ -3660,7 +3659,7 @@ TEST_F(TestProjector, TestExtendedCInterfaceFunctionsWithFunctionHolder) {
EXPECT_ARROW_ARRAY_EQUALS(out, outs.at(0));
}

TEST_F(TestProjector, TestExtendedCInterfaceFunctionThatNeedsContext) {
TEST_F(TestProjector, TestExtendedCFunctionThatNeedsContext) {
auto in_field = field("in", arrow::utf8());
auto schema = arrow::schema({in_field});
auto out_field = field("out", arrow::utf8());
Expand Down
19 changes: 7 additions & 12 deletions cpp/src/gandiva/tests/test_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ NativeFunction GetTestExternalFunction() {
return multiply_by_two_func;
}

static NativeFunction GetTestExternalCInterfaceFunction() {
static NativeFunction GetTestExternalCFunction() {
NativeFunction multiply_by_three_func(
"multiply_by_three", {}, {arrow::int32()}, arrow::int64(),
ResultNullableType::kResultNullIfNull, "multiply_by_three_int32");
Expand Down Expand Up @@ -89,7 +89,7 @@ class MultiplyHolder : public FunctionHolder {
public:
explicit MultiplyHolder(int32_t num) : num_(num) {}

static Status Make(const FunctionNode& node, std::shared_ptr<MultiplyHolder>* holder) {
static arrow::Result<std::shared_ptr<MultiplyHolder>> Make(const FunctionNode& node) {
ARROW_RETURN_IF(node.children().size() != 2,
Status::Invalid("'multiply_by_n' function requires two parameters"));

Expand All @@ -105,9 +105,8 @@ class MultiplyHolder : public FunctionHolder {
Status::Invalid(
"'multiply_by_n' function requires an int32 literal as the 2nd parameter"));

*holder = std::make_shared<MultiplyHolder>(
return std::make_shared<MultiplyHolder>(
literal->is_null() ? 0 : std::get<int32_t>(literal->holder()));
return Status::OK();
}

int32_t operator()() const { return num_; }
Expand All @@ -117,7 +116,7 @@ class MultiplyHolder : public FunctionHolder {
};

extern "C" {
// this function is used as an external stub function for testing so it has to be declared
// this function is used as an external C function for testing so it has to be declared
// with extern C
static int64_t multiply_by_three(int32_t value) { return value * 3; }

Expand All @@ -143,10 +142,10 @@ static const char* multiply_by_two_formula(int64_t ctx, const char* value,
}
}

std::shared_ptr<Configuration> TestConfigWithCInterfaceFunction(
std::shared_ptr<Configuration> TestConfigWithCFunction(
std::shared_ptr<FunctionRegistry> registry) {
return BuildConfigurationWithRegistry(std::move(registry), [](auto reg) {
return reg->Register(GetTestExternalCInterfaceFunction(),
return reg->Register(GetTestExternalCFunction(),
reinterpret_cast<void*>(multiply_by_three));
});
}
Expand All @@ -156,11 +155,7 @@ std::shared_ptr<Configuration> TestConfigWithHolderFunction(
return BuildConfigurationWithRegistry(std::move(registry), [](auto reg) {
return reg->Register(
GetTestFunctionWithFunctionHolder(), reinterpret_cast<void*>(multiply_by_n),
[](const FunctionNode& node) -> arrow::Result<FunctionHolderPtr> {
std::shared_ptr<MultiplyHolder> derived_instance;
ARROW_RETURN_NOT_OK(MultiplyHolder::Make(node, &derived_instance));
return derived_instance;
});
[](const FunctionNode& node) { return MultiplyHolder::Make(node); });
});
}

Expand Down
4 changes: 2 additions & 2 deletions cpp/src/gandiva/tests/test_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ std::shared_ptr<Configuration> TestConfiguration();
std::shared_ptr<Configuration> TestConfigWithFunctionRegistry(
std::shared_ptr<FunctionRegistry> registry);

// helper function to create a Configuration with an external stub function registered to
// helper function to create a Configuration with an external C function registered to
// the given function registry
std::shared_ptr<Configuration> TestConfigWithCInterfaceFunction(
std::shared_ptr<Configuration> TestConfigWithCFunction(
std::shared_ptr<FunctionRegistry> registry);

// helper function to create a Configuration with an external function registered
Expand Down

0 comments on commit 258f613

Please sign in to comment.