1616#include < executorch/runtime/core/evalue.h>
1717#include < executorch/runtime/executor/pte_data_map.h>
1818
19- #include < cinttypes>
2019#include < memory>
2120#include < mutex>
2221
@@ -42,13 +41,6 @@ using executorch::runtime::FreeableBuffer;
4241using executorch::runtime::Result;
4342using executorch::runtime::Span;
4443
45- // Global mutex for all XNNPACK operations. This is temporary, tracked by
46- // T272407942.
47- static std::mutex& global_xnnpack_mutex () {
48- static std::mutex m;
49- return m;
50- }
51-
5244class XnnpackBackend final
5345 : public ::executorch::ET_RUNTIME_NAMESPACE ::BackendInterface {
5446 public:
@@ -74,8 +66,6 @@ class XnnpackBackend final
7466 BackendInitContext& context,
7567 FreeableBuffer* processed,
7668 ArrayRef<CompileSpec> compile_specs) const override {
77- const std::lock_guard<std::mutex> global_lock (global_xnnpack_mutex ());
78-
7969 auto executor = context.get_runtime_allocator ()
8070 ->allocateInstance <xnnpack::delegate::XNNExecutor>();
8171 if (executor == nullptr ) {
@@ -139,45 +129,22 @@ class XnnpackBackend final
139129 Error, " XNNCompiler::compileModel failed: 0x%x" , (unsigned int )err);
140130 return err;
141131 }
142-
143- ET_LOG (
144- Info,
145- " XnnpackBackend::init delegate=%p workspace_id=%" PRIu64
146- " workspace_ptr=%p program_id=0x%" PRIxPTR " weight_cache=%s" ,
147- (void *)executor,
148- workspace->id (),
149- (void *)workspace_ptr,
150- program_id,
151- use_weight_cache ? " true" : " false" );
152-
153132 return executor;
154133 }
155134
156135 Error execute (
157136 BackendExecutionContext& context,
158137 DelegateHandle* handle,
159138 Span<EValue*> args) const override {
160- const std::lock_guard<std::mutex> global_lock (global_xnnpack_mutex ());
161-
162139 auto executor = static_cast <xnnpack::delegate::XNNExecutor*>(handle);
163140
164- auto workspace = executor->get_workspace ();
165- ET_LOG (
166- Info,
167- " XnnpackBackend::execute begin delegate=%p workspace_id=%" PRIu64
168- " num_args=%zu weight_cache=%s" ,
169- (void *)executor,
170- workspace->id (),
171- (size_t )args.size (),
172- executor->uses_weight_cache () ? " true" : " false" );
173-
174141 std::unique_lock<std::mutex> lock_weights_cache (
175142 weights_cache_mutex_, std::defer_lock);
176143 if (executor->uses_weight_cache ()) {
177144 lock_weights_cache.lock ();
178145 }
179146
180- auto [raii_lock, _] = workspace ->acquire ();
147+ auto [raii_lock, _] = executor-> get_workspace () ->acquire ();
181148
182149 // Prepare Inputs/Outputs and Propagate Input Shapes
183150 Error err = executor->prepare_args (args);
@@ -194,29 +161,12 @@ class XnnpackBackend final
194161 // Convert output data types if necessary (e.g., int32 -> int64 for Long)
195162 err = executor->convert_outputs (args);
196163
197- ET_LOG (
198- Info,
199- " XnnpackBackend::execute end delegate=%p workspace_id=%" PRIu64
200- " err=0x%x" ,
201- (void *)executor,
202- workspace->id (),
203- (unsigned int )err);
204-
205164 return err;
206165 }
207166
208167 void destroy (DelegateHandle* handle) const override {
209168 if (handle != nullptr ) {
210- const std::lock_guard<std::mutex> global_lock (global_xnnpack_mutex ());
211-
212169 auto executor = static_cast <xnnpack::delegate::XNNExecutor*>(handle);
213- auto workspace = executor->get_workspace ();
214-
215- ET_LOG (
216- Info,
217- " XnnpackBackend::destroy delegate=%p workspace_id=%" PRIu64,
218- (void *)executor,
219- workspace->id ());
220170
221171#ifdef ENABLE_XNNPACK_PROFILING
222172 executor->print_avg_op_timings ();
@@ -233,6 +183,7 @@ class XnnpackBackend final
233183 // the same backend instance. Make sure to hold onto the workspace
234184 // shared_ptr, as the pointer in the executor is freed, which includes
235185 // the mutex referenced by raii_lock.
186+ auto workspace = executor->get_workspace ();
236187 auto [raii_lock, _] = workspace->acquire ();
237188
238189 // XNNExecutor is not trivially destructible. Since this was constructed
0 commit comments