Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-37751: [C++][Gandiva] Avoid registering exported functions multiple times in gandiva #37752

Merged
merged 3 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions cpp/src/gandiva/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ set(SRC_FILES
expression.cc
expression_registry.cc
exported_funcs_registry.cc
exported_funcs.cc
filter.cc
function_ir_builder.cc
function_registry.cc
Expand Down Expand Up @@ -237,6 +238,7 @@ add_gandiva_test(internals-test
tree_expr_test.cc
encrypt_utils_test.cc
expr_decomposer_test.cc
exported_funcs_registry_test.cc
expression_registry_test.cc
selection_vector_test.cc
lru_cache_test.cc
Expand Down
3 changes: 3 additions & 0 deletions cpp/src/gandiva/engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@

#include "gandiva/configuration.h"
#include "gandiva/decimal_ir.h"
#include "gandiva/exported_funcs.h"
#include "gandiva/exported_funcs_registry.h"

namespace gandiva {
Expand All @@ -103,6 +104,7 @@ std::once_flag llvm_init_once_flag;
static bool llvm_init = false;
static llvm::StringRef cpu_name;
static llvm::SmallVector<std::string, 10> cpu_attrs;
std::once_flag register_exported_funcs_flag;
niyue marked this conversation as resolved.
Show resolved Hide resolved

void Engine::InitOnce() {
DCHECK_EQ(llvm_init, false);
Expand Down Expand Up @@ -142,6 +144,7 @@ Engine::Engine(const std::shared_ptr<Configuration>& conf,
cached_(cached) {}

Status Engine::Init() {
std::call_once(register_exported_funcs_flag, gandiva::RegisterExportedFuncs);
// Add mappings for global functions that can be accessed from LLVM/IR module.
AddGlobalMappings();

Expand Down
30 changes: 30 additions & 0 deletions cpp/src/gandiva/exported_funcs.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

#include <gandiva/exported_funcs.h>
#include <gandiva/exported_funcs_registry.h>

namespace gandiva {
void RegisterExportedFuncs() {
REGISTER_EXPORTED_FUNCS(ExportedStubFunctions);
REGISTER_EXPORTED_FUNCS(ExportedContextFunctions);
REGISTER_EXPORTED_FUNCS(ExportedTimeFunctions);
REGISTER_EXPORTED_FUNCS(ExportedDecimalFunctions);
REGISTER_EXPORTED_FUNCS(ExportedStringFunctions);
REGISTER_EXPORTED_FUNCS(ExportedHashFunctions);
}
} // namespace gandiva
9 changes: 2 additions & 7 deletions cpp/src/gandiva/exported_funcs.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

#pragma once

#include <gandiva/exported_funcs_registry.h>
#include <vector>
#include "gandiva/visibility.h"

namespace gandiva {

Expand All @@ -36,36 +36,31 @@ class ExportedFuncsBase {
class ExportedStubFunctions : public ExportedFuncsBase {
void AddMappings(Engine* engine) const override;
};
REGISTER_EXPORTED_FUNCS(ExportedStubFunctions);

// Class for exporting Context functions
class ExportedContextFunctions : public ExportedFuncsBase {
void AddMappings(Engine* engine) const override;
};
REGISTER_EXPORTED_FUNCS(ExportedContextFunctions);

// Class for exporting Time functions
class ExportedTimeFunctions : public ExportedFuncsBase {
void AddMappings(Engine* engine) const override;
};
REGISTER_EXPORTED_FUNCS(ExportedTimeFunctions);

// Class for exporting Decimal functions
class ExportedDecimalFunctions : public ExportedFuncsBase {
void AddMappings(Engine* engine) const override;
};
REGISTER_EXPORTED_FUNCS(ExportedDecimalFunctions);

// Class for exporting String functions
class ExportedStringFunctions : public ExportedFuncsBase {
void AddMappings(Engine* engine) const override;
};
REGISTER_EXPORTED_FUNCS(ExportedStringFunctions);

// Class for exporting Hash functions
class ExportedHashFunctions : public ExportedFuncsBase {
void AddMappings(Engine* engine) const override;
};
REGISTER_EXPORTED_FUNCS(ExportedHashFunctions);

GANDIVA_EXPORT void RegisterExportedFuncs();
} // namespace gandiva
7 changes: 6 additions & 1 deletion cpp/src/gandiva/exported_funcs_registry.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,14 @@
namespace gandiva {

void ExportedFuncsRegistry::AddMappings(Engine* engine) {
for (auto entry : registered()) {
for (const auto& entry : registered()) {
entry->AddMappings(engine);
}
}

ExportedFuncsRegistry::list_type& ExportedFuncsRegistry::registered() {
static list_type registered_list;
return registered_list;
}

} // namespace gandiva
15 changes: 6 additions & 9 deletions cpp/src/gandiva/exported_funcs_registry.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,31 @@
#include <vector>

#include <gandiva/engine.h>
#include <gandiva/visibility.h>

namespace gandiva {

class ExportedFuncsBase;

/// Registry for classes that export functions which can be accessed by
/// LLVM/IR code.
class ExportedFuncsRegistry {
class GANDIVA_EXPORT ExportedFuncsRegistry {
public:
using list_type = std::vector<std::shared_ptr<ExportedFuncsBase>>;

// Add functions from all the registered classes to the engine.
static void AddMappings(Engine* engine);

static bool Register(std::shared_ptr<ExportedFuncsBase> entry) {
registered().push_back(entry);
registered().emplace_back(std::move(entry));
return true;
}

private:
static list_type& registered() {
static list_type registered_list;
return registered_list;
}
static list_type& registered();
niyue marked this conversation as resolved.
Show resolved Hide resolved
};

#define REGISTER_EXPORTED_FUNCS(classname) \
static bool _registered_##classname = \
#define REGISTER_EXPORTED_FUNCS(classname) \
[[maybe_unused]] static bool _registered_##classname = \
ExportedFuncsRegistry::Register(std::make_shared<classname>())
niyue marked this conversation as resolved.
Show resolved Hide resolved

} // namespace gandiva
28 changes: 28 additions & 0 deletions cpp/src/gandiva/exported_funcs_registry_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

#include "gandiva/exported_funcs_registry.h"
#include <gtest/gtest.h>
#include "gandiva/exported_funcs.h"

namespace gandiva {
TEST(ExportedFuncsRegistry, RegistrationOnlyOnce) {
gandiva::RegisterExportedFuncs();
pitrou marked this conversation as resolved.
Show resolved Hide resolved
auto const& registered_list = ExportedFuncsRegistry::registered();
EXPECT_EQ(registered_list.size(), 6);
niyue marked this conversation as resolved.
Show resolved Hide resolved
}
} // namespace gandiva
Loading