Skip to content

Commit

Permalink
graph: backend: change registration exception to status code
Browse files Browse the repository at this point in the history
  • Loading branch information
TaoLv committed Sep 5, 2024
1 parent 022b2f6 commit 7cfc834
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 18 deletions.
5 changes: 3 additions & 2 deletions src/graph/backend/dnnl/dnnl_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,10 @@ graph::utils::optional_t<memory::desc> dnnl_backend_t::get_mem_desc(
} // namespace dnnl_impl

// This function should be called by backend_registry_t
void register_dnnl_backend() {
backend_registry_t::get_singleton().register_backend(
status_t register_dnnl_backend() {
const status_t ret = backend_registry_t::get_singleton().register_backend(
&dnnl_impl::dnnl_backend_t::get_singleton());
return ret;
}

} // namespace graph
Expand Down
7 changes: 4 additions & 3 deletions src/graph/backend/fake/fake_backend.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright 2021-2023 Intel Corporation
* Copyright 2021-2024 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -37,9 +37,10 @@ graph::pass::pass_registry_t fake_backend_t::pass_registry_
} // namespace fake_impl

// This function must be called by backend_registry_t
void register_fake_backend() {
backend_registry_t::get_singleton().register_backend(
status_t register_fake_backend() {
const status_t ret = backend_registry_t::get_singleton().register_backend(
&fake_impl::fake_backend_t::get_singleton());
return ret;
}

} // namespace graph
Expand Down
5 changes: 3 additions & 2 deletions src/graph/backend/graph_compiler/compiler_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,10 @@ status_t compiler_backend_t::get_partitions(

} // namespace compiler_impl

void register_compiler_backend() {
backend_registry_t::get_singleton().register_backend(
status_t register_compiler_backend() {
const status_t ret = backend_registry_t::get_singleton().register_backend(
&compiler_impl::compiler_backend_t::get_singleton());
return ret;
}

} // namespace graph
Expand Down
16 changes: 7 additions & 9 deletions src/graph/interface/backend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ namespace impl {
namespace graph {

// forward declaration
void register_dnnl_backend();
void register_fake_backend();
status_t register_dnnl_backend();
status_t register_fake_backend();
#ifdef DNNL_ENABLE_COMPILER_BACKEND
// register graph compiler backend
void register_compiler_backend();
status_t register_compiler_backend();
#endif

class backend_t {
Expand Down Expand Up @@ -131,10 +131,11 @@ class backend_registry_t {
return inst;
}

backend_t *register_backend(const backend_t *abackend) {
status_t register_backend(const backend_t *abackend) {
auto has_colliding_name = [&](const backend_t *backend) {
return backend->get_name().compare(abackend->get_name()) == 0;
};

auto backend_already_registered = [&]() {
return std::find_if(sorted_backends_.begin(),
sorted_backends_.end(), has_colliding_name)
Expand All @@ -145,18 +146,15 @@ class backend_registry_t {
return l->get_priority() > r->get_priority();
};

if (backend_already_registered()) {
throw std::runtime_error(
"backend name not unique: " + abackend->get_name());
}
if (backend_already_registered()) { return status::runtime_error; }

std::lock_guard<std::mutex> lock(m_);

backends_[abackend->get_id()] = abackend;
sorted_backends_.emplace_back(abackend);
std::sort(sorted_backends_.begin(), sorted_backends_.end(),
compare_priority);
return const_cast<backend_t *>(abackend);
return status::success;
}

// This interface will firstly register all available backends and then
Expand Down
4 changes: 2 additions & 2 deletions tests/gtests/graph/unit/interface/test_backend.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright 2022-2023 Intel Corporation
* Copyright 2022-2024 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -48,5 +48,5 @@ TEST(test_interface_backend, CompareLogicalTensor) {
TEST(test_interface_backend, RegisterBackend) {
auto &registry = graph::backend_registry_t::get_singleton();
auto bkds = registry.get_registered_backends();
EXPECT_THROW(registry.register_backend(bkds[0]), std::runtime_error);
EXPECT_EQ(registry.register_backend(bkds[0]), graph::status::runtime_error);
}

0 comments on commit 7cfc834

Please sign in to comment.