aboutsummaryrefslogtreecommitdiff
path: root/src/backends
diff options
context:
space:
mode:
Diffstat (limited to 'src/backends')
-rw-r--r--src/backends/backendsCommon/test/BackendRegistryTests.cpp44
-rw-r--r--src/backends/neon/NeonRegistryInitializer.cpp12
2 files changed, 55 insertions, 1 deletions
diff --git a/src/backends/backendsCommon/test/BackendRegistryTests.cpp b/src/backends/backendsCommon/test/BackendRegistryTests.cpp
index 213d114248..ce8acbbf2a 100644
--- a/src/backends/backendsCommon/test/BackendRegistryTests.cpp
+++ b/src/backends/backendsCommon/test/BackendRegistryTests.cpp
@@ -7,6 +7,7 @@
#include <armnn/BackendRegistry.hpp>
#include <armnn/backends/IBackendInternal.hpp>
+#include <reference/RefBackend.hpp>
#include <boost/test/unit_test.hpp>
@@ -103,4 +104,47 @@ BOOST_AUTO_TEST_CASE(TestDirectCallToRegistry)
BackendRegistryInstance().Deregister("HelloWorld");
}
+// Test that backends can throw exceptions during their factory function to prevent loading in an unsuitable
+// environment. For example Neon Backend loading on armhf device without neon support.
+// In reality the dynamic backend is loaded in during the LoadDynamicBackends(options.m_DynamicBackendsPath)
+// step of runtime constructor, then the factory function is called to check if supported, in case
+// of Neon not being detected the exception is raised and so the backend is not added to the supportedBackends
+// list
+
+BOOST_AUTO_TEST_CASE(ThrowBackendUnavailableException)
+{
+ using namespace armnn;
+
+ const BackendId mockBackendId("MockDynamicBackend");
+
+ const std::string exceptionMessage("Neon support not found on device, could not register CpuAcc Backend.\n");
+
+ // Register the mock backend with a factory function lambda equivalent to NeonRegisterInitializer
+ BackendRegistryInstance().Register(mockBackendId,
+ [exceptionMessage]()
+ {
+ if (false)
+ {
+ return IBackendInternalUniquePtr(new RefBackend);
+ }
+ ARMNN_LOG(info) << "Neon support not found on device, could not register CpuAcc Backend.";
+ throw armnn::BackendUnavailableException(exceptionMessage);
+ });
+
+ // Get the factory function of the mock backend
+ auto factoryFunc = BackendRegistryInstance().GetFactory(mockBackendId);
+
+ try
+ {
+ // Call the factory function as done during runtime backend registering
+ auto backend = factoryFunc();
+ }
+ catch (const BackendUnavailableException& e)
+ {
+ // Caught
+ BOOST_CHECK_EQUAL(e.what(), exceptionMessage);
+ BOOST_TEST_MESSAGE("ThrowBackendUnavailableExceptionImpl: BackendUnavailableException caught.");
+ }
+}
+
BOOST_AUTO_TEST_SUITE_END()
diff --git a/src/backends/neon/NeonRegistryInitializer.cpp b/src/backends/neon/NeonRegistryInitializer.cpp
index fd2e84dd5f..fc981ab270 100644
--- a/src/backends/neon/NeonRegistryInitializer.cpp
+++ b/src/backends/neon/NeonRegistryInitializer.cpp
@@ -6,6 +6,7 @@
#include "NeonBackend.hpp"
#include <armnn/BackendRegistry.hpp>
+#include <armnn/Utils.hpp>
namespace
{
@@ -18,7 +19,16 @@ static BackendRegistry::StaticRegistryInitializer g_RegisterHelper
NeonBackend::GetIdStatic(),
[]()
{
- return IBackendInternalUniquePtr(new NeonBackend);
+ // Check if device supports Neon.
+ if (NeonDetected())
+ {
+ return IBackendInternalUniquePtr(new NeonBackend);
+ }
+
+ // If device does not support Neon throw exception so the Backend is not added to supportedBackends
+ ARMNN_LOG(info) << "Neon support not found on device, could not register CpuAcc Backend.";
+ throw armnn::BackendUnavailableException(
+ "Neon support not found on device, could not register CpuAcc Backend.\n");
}
};