Skip to content

Commit

Permalink
Avoid registering exported functions multiple times in gandiva.
Browse files Browse the repository at this point in the history
  • Loading branch information
niyue committed Sep 17, 2023
1 parent 3db6205 commit 5eaf0f8
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 17 deletions.
1 change: 1 addition & 0 deletions cpp/src/gandiva/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,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
3 changes: 3 additions & 0 deletions cpp/src/gandiva/engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,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 @@ -88,6 +89,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;

void Engine::InitOnce() {
DCHECK_EQ(llvm_init, false);
Expand Down Expand Up @@ -127,6 +129,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
32 changes: 32 additions & 0 deletions cpp/src/gandiva/exported_funcs.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// 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 "exported_funcs.h"
#include <gandiva/exported_funcs_registry.h>

namespace gandiva {

void RegisterExportedFuncs() {
ExportedFuncsRegistry::Register(std::make_shared<ExportedStubFunctions>());
ExportedFuncsRegistry::Register(std::make_shared<ExportedContextFunctions>());
ExportedFuncsRegistry::Register(std::make_shared<ExportedTimeFunctions>());
ExportedFuncsRegistry::Register(std::make_shared<ExportedDecimalFunctions>());
ExportedFuncsRegistry::Register(std::make_shared<ExportedStringFunctions>());
ExportedFuncsRegistry::Register(std::make_shared<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,7 +17,6 @@

#pragma once

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

namespace gandiva {
Expand All @@ -36,36 +35,32 @@ 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);

void RegisterExportedFuncs();

} // namespace gandiva
5 changes: 5 additions & 0 deletions cpp/src/gandiva/exported_funcs_registry.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,9 @@ void ExportedFuncsRegistry::AddMappings(Engine* engine) {
}
}

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

} // namespace gandiva
12 changes: 2 additions & 10 deletions cpp/src/gandiva/exported_funcs_registry.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,11 @@ class ExportedFuncsRegistry {
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();
};

#define REGISTER_EXPORTED_FUNCS(classname) \
static bool _registered_##classname = \
ExportedFuncsRegistry::Register(std::make_shared<classname>())

} // namespace gandiva

0 comments on commit 5eaf0f8

Please sign in to comment.