ONE - On-device Neural Engine
Loading...
Searching...
No Matches
onert::odc::CodegenLoader Class Reference

Class to manage loading and unloading of dynamic library containing implementation of ICodegen interface. More...

#include <CodegenLoader.h>

Public Types

using dlhandle_destroy_t = std::function< void(void *)>
 Typedef for function pointer to destroy loaded library handle.
 
using factory_t = ICodegen *(*)()
 Typedef for function pointer to create instance of ICodegen.
 
using codegen_destory_t = void(*)(ICodegen *)
 Typedef for function pointer to destroy instance of ICodegen.
 

Public Member Functions

 CodegenLoader (CodegenLoader const &)=delete
 
CodegenLoaderoperator= (CodegenLoader const &)=delete
 
void loadLibrary (const char *target)
 Load dynamic library containing implementation of ICodegen.
 
void unloadLibrary ()
 Unload dynamic library containing implementation of ICodegen.
 
const ICodegenget () const
 Get instance of ICodegen created through factory method.
 

Static Public Member Functions

static CodegenLoaderinstance ()
 Get singleton instance of CodegenLoader.
 

Detailed Description

Class to manage loading and unloading of dynamic library containing implementation of ICodegen interface.

Definition at line 34 of file CodegenLoader.h.

Member Typedef Documentation

◆ codegen_destory_t

Typedef for function pointer to destroy instance of ICodegen.

Definition at line 48 of file CodegenLoader.h.

◆ dlhandle_destroy_t

using onert::odc::CodegenLoader::dlhandle_destroy_t = std::function<void(void *)>

Typedef for function pointer to destroy loaded library handle.

Definition at line 40 of file CodegenLoader.h.

◆ factory_t

Typedef for function pointer to create instance of ICodegen.

Definition at line 44 of file CodegenLoader.h.

Constructor & Destructor Documentation

◆ CodegenLoader()

onert::odc::CodegenLoader::CodegenLoader ( CodegenLoader const &  )
delete

Member Function Documentation

◆ get()

const ICodegen * onert::odc::CodegenLoader::get ( ) const
inline

Get instance of ICodegen created through factory method.

Returns
Pointer to instance of ICodegen

Definition at line 85 of file CodegenLoader.h.

85{ return _codegen.get(); }

Referenced by loadLibrary(), and unloadLibrary().

◆ instance()

CodegenLoader & onert::odc::CodegenLoader::instance ( )
static

Get singleton instance of CodegenLoader.

Returns
Reference to singleton instance of CodegenLoader

Definition at line 35 of file CodegenLoader.cc.

36{
37 static CodegenLoader singleton;
38 return singleton;
39}
CodegenLoader(CodegenLoader const &)=delete

Referenced by onert::odc::CodegenManager::codegen().

◆ loadLibrary()

void onert::odc::CodegenLoader::loadLibrary ( const char *  target)

Load dynamic library containing implementation of ICodegen.

Parameters
[in]targetTarget backend name This target string will be used to find a backend library. The name of target backend library should follow the following rules: 'lib' + {backend extension} + '-gen' + {lib extension} And the target string should be a name except 'lib' and {lib extension}. For example, if the backend extension is 'aaa', the backend library name should be 'libaaa-gen.so', and the target string should be 'aaa-gen'.

Definition at line 41 of file CodegenLoader.cc.

42{
43 if (get() != nullptr)
44 return;
45
46 const std::string codegen_so = "lib" + std::string{target} + SHARED_LIB_EXT;
47#ifdef __ANDROID__
48 void *handle = dlopen(codegen_so.c_str(), RTLD_LAZY | RTLD_LOCAL);
49#else
50 void *handle = dlmopen(LM_ID_NEWLM, codegen_so.c_str(), RTLD_LAZY | RTLD_LOCAL);
51#endif
52 if (handle == nullptr)
53 {
54 throw std::runtime_error("CodegenLoader: " + std::string{dlerror()});
55 }
56
57 const auto factory = (factory_t)dlsym(handle, "create_codegen");
58 if (factory == nullptr)
59 {
60 const std::string dlerror_msg = dlerror();
61 dlclose(handle);
62 throw std::runtime_error("CodegenLoader: " + dlerror_msg);
63 }
64
65 const auto destroyer = (codegen_destory_t)dlsym(handle, "destroy_codegen");
66 _codegen = std::unique_ptr<ICodegen, codegen_destory_t>(factory(), destroyer);
67 if (_codegen == nullptr)
68 {
69 dlclose(handle);
70 throw std::runtime_error("CodegenLoader: unable to create codegen");
71 }
72
73 // Save backend handle (avoid warning by handle lost without dlclose())
74 _dlhandle = std::unique_ptr<void, dlhandle_destroy_t>{
75 handle, [filename = codegen_so](void *h) {
76 if (dlclose(h))
77 throw std::runtime_error("CodegenLoader: Failed to unload backend " + filename);
78 }};
79}
void(*)(ICodegen *) codegen_destory_t
Typedef for function pointer to destroy instance of ICodegen.
const ICodegen * get() const
Get instance of ICodegen created through factory method.
ICodegen *(*)() factory_t
Typedef for function pointer to create instance of ICodegen.
Option< std::string > target(optname("--target"), overview("select target language to emit for given architecture." "Valid values are '" NNC_TARGET_ARM_CPP "', '" NNC_TARGET_X86_CPP "', '" NNC_TARGET_ARM_GPU_CPP "', '" NNC_TARGET_INTERPRETER "'"), std::string(), optional(false), optvalues(NNC_TARGET_ARM_CPP "," NNC_TARGET_X86_CPP "," NNC_TARGET_ARM_GPU_CPP "," NNC_TARGET_INTERPRETER), nullptr, separators("="))
Definition Options.h:47

References get().

◆ operator=()

CodegenLoader & onert::odc::CodegenLoader::operator= ( CodegenLoader const &  )
delete

◆ unloadLibrary()

void onert::odc::CodegenLoader::unloadLibrary ( )

Unload dynamic library containing implementation of ICodegen.

Definition at line 81 of file CodegenLoader.cc.

82{
83 if (get() == nullptr)
84 return;
85
86 _codegen.reset(nullptr);
87 _dlhandle.reset(nullptr);
88}

References get().


The documentation for this class was generated from the following files: