diff --git a/runtime/backend/interface.cpp b/runtime/backend/interface.cpp index 84c0bb82d43..d9380f4e1e4 100644 --- a/runtime/backend/interface.cpp +++ b/runtime/backend/interface.cpp @@ -41,6 +41,14 @@ BackendInterface* get_backend_class(const char* name) { return nullptr; } +std::vector get_all_backend_classes() { + std::vector backends(num_registered_backends); + for (size_t i = 0; i < num_registered_backends; i++) { + backends[i] = registered_backends[i].backend; + } + return backends; +} + Error register_backend(const Backend& backend) { if (num_registered_backends >= kMaxRegisteredBackends) { return Error::Internal; diff --git a/runtime/backend/interface.h b/runtime/backend/interface.h index c0305f68cd3..37227b9d1f4 100644 --- a/runtime/backend/interface.h +++ b/runtime/backend/interface.h @@ -9,6 +9,7 @@ #pragma once #include +#include #include #include @@ -120,6 +121,12 @@ class BackendInterface { */ BackendInterface* get_backend_class(const char* name); +/** + * Returns the vector of pointers to all the registered backends. + * The mapping is populated using register_backend method. + */ +std::vector get_all_backend_classes(); + /** * A named instance of a backend. */ diff --git a/runtime/executor/test/backend_integration_test.cpp b/runtime/executor/test/backend_integration_test.cpp index 5c804b4ca42..20f02089d3d 100644 --- a/runtime/executor/test/backend_integration_test.cpp +++ b/runtime/executor/test/backend_integration_test.cpp @@ -330,6 +330,13 @@ TEST_P(BackendIntegrationTest, BackendIsPresent) { ASSERT_EQ(backend, &StubBackend::singleton()); } +TEST_P(BackendIntegrationTest, BackendIsPresentInAll) { + std::vector backends = + executorch::runtime::get_all_backend_classes(); + ASSERT_EQ(backends.size(), 1); + ASSERT_EQ(backends[0], &StubBackend::singleton()); +} + // Demonstrate that installed StubBackend initializes successfully by default. TEST_P(BackendIntegrationTest, BasicInitSucceeds) { Result loader = FileDataLoader::from(program_path());