1 /* 2 * Copyright (c) Meta Platforms, Inc. and affiliates. 3 * All rights reserved. 4 * 5 * This source code is licensed under the BSD-style license found in the 6 * LICENSE file in the root directory of this source tree. 7 */ 8 9 #pragma once 10 11 #include <executorch/runtime/core/event_tracer.h> 12 #include <executorch/runtime/core/memory_allocator.h> 13 14 namespace executorch { 15 namespace runtime { 16 17 /** 18 * BackendExecutionContext will be used to inject run time context. 19 */ 20 class BackendExecutionContext final { 21 public: 22 BackendExecutionContext( 23 EventTracer* event_tracer = nullptr, 24 MemoryAllocator* temp_allocator = nullptr, 25 const char* method_name = nullptr) event_tracer_(event_tracer)26 : event_tracer_(event_tracer), 27 temp_allocator_(temp_allocator), 28 method_name_(method_name) {} 29 30 /** 31 * Returns a pointer to an instance of EventTracer to do profiling/debugging 32 * logging inside the delegate backend. Users will need access to this pointer 33 * to use any of the event tracer APIs. 34 */ event_tracer()35 EventTracer* event_tracer() { 36 return event_tracer_; 37 } 38 39 /** 40 * Returns a pointer to the address allocated by temp allocator. This 41 * allocator will be reset after every delegate call during execution. 42 */ 43 void* allocate( 44 size_t size, 45 size_t alignment = MemoryAllocator::kDefaultAlignment) { 46 // TODO(chenlai): depends on the need, we may expose more functionality for 47 // memory allocation. 48 return temp_allocator_->allocate(size, alignment); 49 } 50 51 /** 52 * Returns the temp allocator. This allocator will be reset every instruction. 53 */ get_temp_allocator()54 MemoryAllocator* get_temp_allocator() { 55 return temp_allocator_; 56 } 57 58 /** 59 * Get the name of the executing method from the ExecuTorch runtime. 60 */ get_method_name()61 const char* get_method_name() const { 62 return method_name_; 63 } 64 65 private: 66 EventTracer* event_tracer_ = nullptr; 67 MemoryAllocator* temp_allocator_ = nullptr; 68 const char* method_name_ = nullptr; 69 }; 70 71 } // namespace runtime 72 } // namespace executorch 73 74 namespace torch { 75 namespace executor { 76 // TODO(T197294990): Remove these deprecated aliases once all users have moved 77 // to the new `::executorch` namespaces. 78 using ::executorch::runtime::BackendExecutionContext; 79 } // namespace executor 80 } // namespace torch 81