1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  *   http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied.  See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
20 #ifndef TVM_RUNTIME_HEXAGON_HEXAGON_MODULE_H_
21 #define TVM_RUNTIME_HEXAGON_HEXAGON_MODULE_H_
22 
23 #include <dmlc/logging.h>
24 #include <tvm/runtime/module.h>
25 
26 #include <array>
27 #include <memory>
28 #include <set>
29 #include <string>
30 #include <unordered_map>
31 
32 #include "../meta_data.h"
33 
34 namespace tvm {
35 namespace runtime {
36 
37 /*!
38  * \brief Create a Hexagon module from data.
39  * \param data          The module data.
40  * \param fmt           The format of the data, can be "obj".
41  * \param fmap          The function information map of each function.
42  * \param asm_str       String with the generated assembly source.
43  * \param obj_str       String with the object file data.
44  * \param ir_str        String with the disassembled LLVM IR source.
45  * \param bc_str        String with the bitcode LLVM IR.
46  * \param packed_c_abi  Set of names of functions using PackedC calling
47  *                      convention.
48  */
49 Module HexagonModuleCreate(std::string data, std::string fmt,
50                            std::unordered_map<std::string, FunctionInfo> fmap, std::string asm_str,
51                            std::string obj_str, std::string ir_str, std::string bc_str,
52                            const std::set<std::string>& packed_c_abi);
53 
54 namespace hexagon {
55 
56 /*!
57  * \brief Low-level interface for communicating with Hexagon devices.
58  */
59 class Device {
60  public:
61   /*!
62    * \brief Allocate memory on device.
63    * \param size    Requested size.
64    * \param align   Requested alignment.
65    * \return        Pointer (local to the device) of the allocated memory,
66    *                or nullptr if allocation failed.
67    */
68   virtual void* Alloc(unsigned size, unsigned align) = 0;
69   /*!
70    * \brief Release allocated memory on device.
71    * \param ptr     Pointer to memory previously allocated by \ref Alloc.
72    */
73   virtual void Free(void* ptr) = 0;
74   /*!
75    * \brief Allocate VTCM memory on device.
76    * \param size    Requested size.
77    * \param align   Requested alignment.
78    * \return        Pointer (local to the device) of the allocated memory,
79    *                or nullptr if allocation failed.
80    */
81   virtual void* AllocVtcm(unsigned size, unsigned align) = 0;
82   /*!
83    * \brief Release allocated VTCM memory on device.
84    * \param ptr     Pointer to memory previously allocated by \ref AllocVtcm.
85    */
86   virtual void FreeVtcm(void* ptr) = 0;
87   /*!
88    * \brief Copy a block of data on device to another location on the device.
89    * \param dst     Pointer (local to device) to the destination buffer.
90    * \param src     Pointer (local to device) of the source buffer.
91    * \param len     Number of bytes to copy.
92    */
93   virtual void CopyDeviceToDevice(void* dst, const void* src, unsigned len) = 0;
94   /*!
95    * \brief Copy a block of data from device to host.
96    * \param host_dst  Pointer (local to host) to the destination buffer.
97    * \param src       Pointer (local to device) to the source buffer.
98    * \param len       Number of bytes to copy.
99    */
100   virtual void CopyDeviceToHost(void* host_dst, const void* src, unsigned len) = 0;
101   /*!
102    * \brief Copy a block of data from host to device.
103    * \param dst       Pointer (local to device) to the destination buffer.
104    * \param host_src  Pointer (local to host) to the source buffer.
105    * \param len       Number of bytes to copy.
106    */
107   virtual void CopyHostToDevice(void* dst, const void* host_src, unsigned len) = 0;
108   /*!
109    * \brief Load a module (typically a shared library) into device.
110    * \param data    Name of the shared library.
111    * \param fmt     Format of the library (currently ignored).
112    * \return        Pointer to the loaded module.
113    * \note Currently only one module can be loaded at any given time.
114    */
115   virtual void* Load(const std::string& data, const std::string& fmt) = 0;
116   /*!
117    * \brief Unload a module from device.
118    * \param mod     Pointer to a loaded module returned by \ref Load.
119    */
120   virtual void Unload(void* mod) = 0;
121   /*!
122    * \brief Find the address of an object in the currently loaded module.
123    * \param sym     Name of the object.
124    * \return Address of the located object, or nullptr if object was
125    *         not found.
126    */
127   virtual void* Resolve(const std::string& sym) = 0;
128   /*!
129    * \brief Invoke a function on device with given arguments.
130    * \param func    Address (local to device) of the function to call.
131    * \param scalar  Pointer to an array of 32-bit values that will be
132    *                passed via consecutive registers: r0..r5. This array
133    *                includes dummy values for skipped registers.
134    * \param sc_num  Number of values in the "scalar" array.
135    * \param stack   Pointer to an array of 32-bit values that will be
136    *                passed on the stack. This array includes dummy values
137    *                for padding.
138    * \param st_num  Number of values in the "stack" array.
139    */
140   virtual void Call(void* func, uint32_t* scalar, unsigned sc_num, uint32_t* stack,
141                     unsigned st_num) = 0;
142 
143   virtual ~Device() = 0;
144 
145   static std::shared_ptr<Device> Global();
ValidateDeviceId(decltype (DLContext::device_id)device_id)146   static bool ValidateDeviceId(decltype(DLContext::device_id) device_id) {
147     // Only supporting a single device for now.
148     return device_id == 0;
149   }
150 };
151 
152 }  // namespace hexagon
153 
154 }  // namespace runtime
155 }  // namespace tvm
156 #endif  // TVM_RUNTIME_HEXAGON_HEXAGON_MODULE_H_
157