offload_table.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. Copyright (c) 2014 Intel Corporation. All Rights Reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions
  5. are met:
  6. * Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. * Neither the name of Intel Corporation nor the names of its
  12. contributors may be used to endorse or promote products derived
  13. from this software without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  18. HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  20. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. /*! \file
  27. \brief Function and Variable tables used by the runtime library
  28. */
  29. #ifndef OFFLOAD_TABLE_H_INCLUDED
  30. #define OFFLOAD_TABLE_H_INCLUDED
  31. #include <iterator>
  32. #include "offload_util.h"
  33. // Template representing double linked list of tables
  34. template <typename T> class TableList {
  35. public:
  36. // table type
  37. typedef T Table;
  38. // List node
  39. struct Node {
  40. Table table;
  41. Node* prev;
  42. Node* next;
  43. };
  44. public:
  45. explicit TableList(Node *node = 0) : m_head(node) {}
  46. void add_table(Node *node) {
  47. m_lock.lock();
  48. if (m_head != 0) {
  49. node->next = m_head;
  50. m_head->prev = node;
  51. }
  52. m_head = node;
  53. m_lock.unlock();
  54. }
  55. void remove_table(Node *node) {
  56. m_lock.lock();
  57. if (node->next != 0) {
  58. node->next->prev = node->prev;
  59. }
  60. if (node->prev != 0) {
  61. node->prev->next = node->next;
  62. }
  63. if (m_head == node) {
  64. m_head = node->next;
  65. }
  66. m_lock.unlock();
  67. }
  68. protected:
  69. Node* m_head;
  70. mutex_t m_lock;
  71. };
  72. // Function lookup table.
  73. struct FuncTable {
  74. //! Function table entry
  75. /*! This table contains functions created from offload regions. */
  76. /*! Each entry consists of a pointer to the function's "key"
  77. and the function address. */
  78. /*! Each shared library or executable may contain one such table. */
  79. /*! The end of the table is marked with an entry whose name field
  80. has value -1. */
  81. struct Entry {
  82. const char* name; //!< Name of the function
  83. void* func; //!< Address of the function
  84. };
  85. // entries
  86. const Entry *entries;
  87. // max name length
  88. int64_t max_name_len;
  89. };
  90. // Function table
  91. class FuncList : public TableList<FuncTable> {
  92. public:
  93. explicit FuncList(Node *node = 0) : TableList<Table>(node),
  94. m_max_name_len(-1)
  95. {}
  96. // add table to the list
  97. void add_table(Node *node) {
  98. // recalculate max function name length
  99. m_max_name_len = -1;
  100. // add table
  101. TableList<Table>::add_table(node);
  102. }
  103. // find function address for the given name
  104. const void* find_addr(const char *name);
  105. // find function name for the given address
  106. const char* find_name(const void *addr);
  107. // max name length from all tables in the list
  108. int64_t max_name_length(void);
  109. // debug dump
  110. void dump(void);
  111. private:
  112. // max name length within from all tables
  113. int64_t m_max_name_len;
  114. };
  115. // Table entry for static variables
  116. struct VarTable {
  117. //! Variable table entry
  118. /*! This table contains statically allocated variables marked with
  119. __declspec(target(mic) or #pragma omp declare target. */
  120. /*! Each entry consists of a pointer to the variable's "key",
  121. the variable address and its size in bytes. */
  122. /*! Because memory allocation is done from the host,
  123. the MIC table does not need the size of the variable. */
  124. /*! Padding to make the table entry size a power of 2 is necessary
  125. to avoid "holes" between table contributions from different object
  126. files on Windows when debug information is specified with /Zi. */
  127. struct Entry {
  128. const char* name; //!< Name of the variable
  129. void* addr; //!< Address of the variable
  130. #if HOST_LIBRARY
  131. uint64_t size;
  132. #ifdef TARGET_WINNT
  133. // padding to make entry size a power of 2
  134. uint64_t padding;
  135. #endif // TARGET_WINNT
  136. #endif
  137. };
  138. // Table terminated by an entry with name == -1
  139. const Entry *entries;
  140. };
  141. // List of var tables
  142. class VarList : public TableList<VarTable> {
  143. public:
  144. VarList() : TableList<Table>()
  145. {}
  146. // debug dump
  147. void dump();
  148. public:
  149. // var table list iterator
  150. class Iterator : public std::iterator<std::input_iterator_tag,
  151. Table::Entry> {
  152. public:
  153. Iterator() : m_node(0), m_entry(0) {}
  154. explicit Iterator(Node *node) {
  155. new_node(node);
  156. }
  157. Iterator& operator++() {
  158. if (m_entry != 0) {
  159. m_entry++;
  160. while (m_entry->name == 0) {
  161. m_entry++;
  162. }
  163. if (m_entry->name == reinterpret_cast<const char*>(-1)) {
  164. new_node(m_node->next);
  165. }
  166. }
  167. return *this;
  168. }
  169. bool operator==(const Iterator &other) const {
  170. return m_entry == other.m_entry;
  171. }
  172. bool operator!=(const Iterator &other) const {
  173. return m_entry != other.m_entry;
  174. }
  175. const Table::Entry* operator*() const {
  176. return m_entry;
  177. }
  178. private:
  179. void new_node(Node *node) {
  180. m_node = node;
  181. m_entry = 0;
  182. while (m_node != 0) {
  183. m_entry = m_node->table.entries;
  184. while (m_entry->name == 0) {
  185. m_entry++;
  186. }
  187. if (m_entry->name != reinterpret_cast<const char*>(-1)) {
  188. break;
  189. }
  190. m_node = m_node->next;
  191. m_entry = 0;
  192. }
  193. }
  194. private:
  195. Node *m_node;
  196. const Table::Entry *m_entry;
  197. };
  198. Iterator begin() const {
  199. return Iterator(m_head);
  200. }
  201. Iterator end() const {
  202. return Iterator();
  203. }
  204. public:
  205. // Entry representation in a copy buffer
  206. struct BufEntry {
  207. intptr_t name;
  208. intptr_t addr;
  209. };
  210. // Calculate the number of elements in the table and
  211. // returns the size of buffer for the table
  212. int64_t table_size(int64_t &nelems);
  213. // Copy table contents to given buffer. It is supposed to be large
  214. // enough to hold all elements as string table.
  215. void table_copy(void *buf, int64_t nelems);
  216. // Patch name offsets in a table after it's been copied to other side
  217. static void table_patch_names(void *buf, int64_t nelems);
  218. };
  219. extern FuncList __offload_entries;
  220. extern FuncList __offload_funcs;
  221. extern VarList __offload_vars;
  222. // Section names where the lookup tables are stored
  223. #ifdef TARGET_WINNT
  224. #define OFFLOAD_ENTRY_TABLE_SECTION_START ".OffloadEntryTable$a"
  225. #define OFFLOAD_ENTRY_TABLE_SECTION_END ".OffloadEntryTable$z"
  226. #define OFFLOAD_FUNC_TABLE_SECTION_START ".OffloadFuncTable$a"
  227. #define OFFLOAD_FUNC_TABLE_SECTION_END ".OffloadFuncTable$z"
  228. #define OFFLOAD_VAR_TABLE_SECTION_START ".OffloadVarTable$a"
  229. #define OFFLOAD_VAR_TABLE_SECTION_END ".OffloadVarTable$z"
  230. #define OFFLOAD_CRTINIT_SECTION_START ".CRT$XCT"
  231. #pragma section(OFFLOAD_CRTINIT_SECTION_START, read)
  232. #else // TARGET_WINNT
  233. #define OFFLOAD_ENTRY_TABLE_SECTION_START ".OffloadEntryTable."
  234. #define OFFLOAD_ENTRY_TABLE_SECTION_END ".OffloadEntryTable."
  235. #define OFFLOAD_FUNC_TABLE_SECTION_START ".OffloadFuncTable."
  236. #define OFFLOAD_FUNC_TABLE_SECTION_END ".OffloadFuncTable."
  237. #define OFFLOAD_VAR_TABLE_SECTION_START ".OffloadVarTable."
  238. #define OFFLOAD_VAR_TABLE_SECTION_END ".OffloadVarTable."
  239. #endif // TARGET_WINNT
  240. #pragma section(OFFLOAD_ENTRY_TABLE_SECTION_START, read, write)
  241. #pragma section(OFFLOAD_ENTRY_TABLE_SECTION_END, read, write)
  242. #pragma section(OFFLOAD_FUNC_TABLE_SECTION_START, read, write)
  243. #pragma section(OFFLOAD_FUNC_TABLE_SECTION_END, read, write)
  244. #pragma section(OFFLOAD_VAR_TABLE_SECTION_START, read, write)
  245. #pragma section(OFFLOAD_VAR_TABLE_SECTION_END, read, write)
  246. // register/unregister given tables
  247. extern "C" void __offload_register_tables(
  248. FuncList::Node *entry_table,
  249. FuncList::Node *func_table,
  250. VarList::Node *var_table
  251. );
  252. extern "C" void __offload_unregister_tables(
  253. FuncList::Node *entry_table,
  254. FuncList::Node *func_table,
  255. VarList::Node *var_table
  256. );
  257. #endif // OFFLOAD_TABLE_H_INCLUDED