1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Minimal HSA declarations without a ROCm dependency.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef SANITIZER_OFFLOAD_HSA_H
14#define SANITIZER_OFFLOAD_HSA_H
15
16#include <stddef.h>
17#include <stdint.h>
18
19#define SANITIZER_HSA_LIBRARY "libhsa-runtime64"
20
21#define SANITIZER_HSA_FUNCTIONS(X) \
22 X(hsa_iterate_agents) \
23 X(hsa_agent_get_info) \
24 X(hsa_executable_get_symbol_by_name) \
25 X(hsa_executable_symbol_get_info) \
26 X(hsa_memory_copy) \
27 X(hsa_amd_memory_pool_allocate) \
28 X(hsa_amd_memory_pool_free) \
29 X(hsa_amd_agents_allow_access) \
30 X(hsa_amd_memory_pool_get_info) \
31 X(hsa_amd_agent_iterate_memory_pools) \
32 X(hsa_system_get_major_extension_table) \
33 X(hsa_amd_signal_create) \
34 X(hsa_signal_destroy) \
35 X(hsa_signal_store_screlease) \
36 X(hsa_signal_wait_scacquire)
37
38extern "C" {
39
40typedef struct hsa_agent_s {
41 uint64_t handle;
42} hsa_agent_t;
43typedef struct hsa_executable_s {
44 uint64_t handle;
45} hsa_executable_t;
46typedef struct hsa_executable_symbol_s {
47 uint64_t handle;
48} hsa_executable_symbol_t;
49typedef struct hsa_amd_memory_pool_s {
50 uint64_t handle;
51} hsa_amd_memory_pool_t;
52typedef struct hsa_signal_s {
53 uint64_t handle;
54} hsa_signal_t;
55typedef struct hsa_loaded_code_object_s {
56 uint64_t handle;
57} hsa_loaded_code_object_t;
58typedef struct hsa_code_object_reader_s {
59 uint64_t handle;
60} hsa_code_object_reader_t;
61
62// POSIX file descriptor, or a Win32 HANDLE stored as void* so windows.h stays
63// out of this mirror.
64#if defined(_WIN32) || defined(_WIN64)
65typedef void* hsa_file_t;
66#else
67typedef int hsa_file_t;
68#endif
69
70typedef int64_t hsa_signal_value_t;
71
72typedef enum {
73 HSA_STATUS_SUCCESS = 0,
74 HSA_STATUS_INFO_BREAK = 0x1,
75 HSA_STATUS_ERROR = 0x1000,
76} hsa_status_t;
77
78typedef enum {
79 HSA_DEVICE_TYPE_CPU = 0,
80 HSA_DEVICE_TYPE_GPU = 1,
81} hsa_device_type_t;
82
83typedef enum {
84 HSA_AGENT_INFO_NAME = 0,
85 HSA_AGENT_INFO_WAVEFRONT_SIZE = 6,
86 HSA_AGENT_INFO_DEVICE = 17,
87} hsa_agent_info_t;
88
89typedef enum { HSA_AMD_SEGMENT_GLOBAL = 0 } hsa_amd_segment_t;
90
91typedef enum {
92 HSA_AMD_MEMORY_POOL_INFO_SEGMENT = 0,
93 HSA_AMD_MEMORY_POOL_INFO_GLOBAL_FLAGS = 1,
94 HSA_AMD_MEMORY_POOL_INFO_RUNTIME_ALLOC_ALLOWED = 5,
95} hsa_amd_memory_pool_info_t;
96
97typedef enum {
98 HSA_AMD_MEMORY_POOL_GLOBAL_FLAG_FINE_GRAINED = 2,
99 HSA_AMD_MEMORY_POOL_GLOBAL_FLAG_COARSE_GRAINED = 4,
100} hsa_amd_memory_pool_global_flag_t;
101
102typedef enum {
103 HSA_AMD_AGENT_INFO_COMPUTE_UNIT_COUNT = 0xA002,
104 HSA_AMD_AGENT_INFO_MAX_WAVES_PER_CU = 0xA00A,
105} hsa_amd_agent_info_t;
106
107typedef enum {
108 HSA_SIGNAL_CONDITION_NE = 1,
109} hsa_signal_condition_t;
110
111typedef enum {
112 HSA_WAIT_STATE_BLOCKED = 0,
113} hsa_wait_state_t;
114
115typedef enum {
116 HSA_SYMBOL_KIND_VARIABLE = 0,
117} hsa_symbol_kind_t;
118
119typedef enum {
120 HSA_EXECUTABLE_SYMBOL_INFO_TYPE = 0,
121 HSA_EXECUTABLE_SYMBOL_INFO_NAME_LENGTH = 1,
122 HSA_EXECUTABLE_SYMBOL_INFO_NAME = 2,
123 HSA_EXECUTABLE_SYMBOL_INFO_VARIABLE_ADDRESS = 21,
124} hsa_executable_symbol_info_t;
125
126typedef enum { HSA_EXTENSION_AMD_LOADER = 0x201 } hsa_extension_t;
127
128typedef enum {
129 HSA_VEN_AMD_LOADER_LOADED_CODE_OBJECT_INFO_KIND = 2,
130 HSA_VEN_AMD_LOADER_LOADED_CODE_OBJECT_INFO_AGENT = 3,
131 HSA_VEN_AMD_LOADER_LOADED_CODE_OBJECT_INFO_CODE_OBJECT_STORAGE_TYPE = 4,
132 HSA_VEN_AMD_LOADER_LOADED_CODE_OBJECT_INFO_CODE_OBJECT_STORAGE_MEMORY_BASE =
133 5,
134 HSA_VEN_AMD_LOADER_LOADED_CODE_OBJECT_INFO_CODE_OBJECT_STORAGE_MEMORY_SIZE =
135 6,
136 HSA_VEN_AMD_LOADER_LOADED_CODE_OBJECT_INFO_LOAD_BASE = 9,
137 HSA_VEN_AMD_LOADER_LOADED_CODE_OBJECT_INFO_LOAD_SIZE = 10,
138} hsa_ven_amd_loader_loaded_code_object_info_t;
139
140typedef enum {
141 HSA_VEN_AMD_LOADER_LOADED_CODE_OBJECT_KIND_AGENT = 2,
142} hsa_ven_amd_loader_loaded_code_object_kind_t;
143
144typedef enum {
145 HSA_VEN_AMD_LOADER_CODE_OBJECT_STORAGE_TYPE_MEMORY = 2,
146} hsa_ven_amd_loader_code_object_storage_type_t;
147
148// hsa_ven_amd_loader_segment_descriptor_t. storage type is uint32_t so the
149// mirror does not depend on the compiler's enum width.
150struct hsa_ven_amd_loader_segment_descriptor_t {
151 hsa_agent_t agent;
152 hsa_executable_t executable;
153 uint32_t code_object_storage_type;
154 const void* code_object_storage_base;
155 size_t code_object_storage_size;
156 size_t code_object_storage_offset;
157 const void* segment_base;
158 size_t segment_size;
159};
160static_assert(sizeof(hsa_ven_amd_loader_segment_descriptor_t) == 64,
161 "layout drift");
162
163// Version 1.04 of HSA_EXTENSION_AMD_LOADER. Slot order is the extension ABI.
164struct LoaderApi {
165 hsa_status_t (*QueryHostAddress)(const void*, const void**);
166 hsa_status_t (*QuerySegmentDescriptors)(
167 hsa_ven_amd_loader_segment_descriptor_t*, size_t*);
168 hsa_status_t (*QueryExecutable)(const void*, hsa_executable_t*);
169 hsa_status_t (*IterateLoadedCodeObjects)(
170 hsa_executable_t,
171 hsa_status_t (*)(hsa_executable_t, hsa_loaded_code_object_t, void*),
172 void*);
173 hsa_status_t (*GetCodeObjectInfo)(
174 hsa_loaded_code_object_t, hsa_ven_amd_loader_loaded_code_object_info_t,
175 void*);
176 hsa_status_t (*CreateCodeObjectReaderFromFile)(hsa_file_t, size_t, size_t,
177 hsa_code_object_reader_t*);
178 hsa_status_t (*IterateExecutables)(hsa_status_t (*)(hsa_executable_t, void*),
179 void*);
180 hsa_status_t (*CodeObjectReaderGetLoadSize)(hsa_code_object_reader_t,
181 size_t*);
182 hsa_status_t (*LoadAgentCodeObjectAtAddress)(hsa_executable_t, hsa_agent_t,
183 hsa_code_object_reader_t,
184 const char*, void*, size_t,
185 hsa_loaded_code_object_t*);
186};
187static_assert(sizeof(LoaderApi) == 9 * sizeof(void*), "layout drift");
188
189hsa_status_t hsa_init(void);
190hsa_status_t hsa_shut_down(void);
191hsa_status_t hsa_iterate_agents(hsa_status_t (*callback)(hsa_agent_t, void*),
192 void* data);
193hsa_status_t hsa_agent_get_info(hsa_agent_t agent, hsa_agent_info_t attribute,
194 void* value);
195hsa_status_t hsa_executable_destroy(hsa_executable_t executable);
196hsa_status_t hsa_executable_freeze(hsa_executable_t executable,
197 const char* options);
198hsa_status_t hsa_executable_get_symbol_by_name(hsa_executable_t executable,
199 const char* symbol_name,
200 const hsa_agent_t* agent,
201 hsa_executable_symbol_t* symbol);
202hsa_status_t hsa_executable_symbol_get_info(
203 hsa_executable_symbol_t symbol, hsa_executable_symbol_info_t attribute,
204 void* value);
205hsa_status_t hsa_executable_iterate_agent_symbols(
206 hsa_executable_t executable, hsa_agent_t agent,
207 hsa_status_t (*callback)(hsa_executable_t, hsa_agent_t,
208 hsa_executable_symbol_t, void*),
209 void* data);
210hsa_status_t hsa_amd_memory_pool_allocate(hsa_amd_memory_pool_t memory_pool,
211 size_t size, uint32_t flags,
212 void** ptr);
213hsa_status_t hsa_amd_memory_pool_free(void* ptr);
214hsa_status_t hsa_amd_memory_pool_get_info(hsa_amd_memory_pool_t memory_pool,
215 hsa_amd_memory_pool_info_t attribute,
216 void* value);
217hsa_status_t hsa_amd_agent_iterate_memory_pools(
218 hsa_agent_t agent, hsa_status_t (*callback)(hsa_amd_memory_pool_t, void*),
219 void* data);
220hsa_status_t hsa_amd_agents_allow_access(uint32_t num_agents,
221 const hsa_agent_t* agents,
222 const uint32_t* flags,
223 const void* ptr);
224hsa_status_t hsa_system_get_major_extension_table(uint16_t extension,
225 uint16_t version_major,
226 size_t table_length,
227 void* table);
228hsa_status_t hsa_memory_copy(void* dst, const void* src, size_t size);
229hsa_status_t hsa_amd_signal_create(hsa_signal_value_t initial_value,
230 uint32_t num_consumers,
231 const hsa_agent_t* consumers,
232 uint64_t attributes, hsa_signal_t* signal);
233hsa_status_t hsa_signal_destroy(hsa_signal_t signal);
234void hsa_signal_store_screlease(hsa_signal_t signal, hsa_signal_value_t value);
235hsa_signal_value_t hsa_signal_wait_scacquire(hsa_signal_t signal,
236 hsa_signal_condition_t condition,
237 hsa_signal_value_t compare_value,
238 uint64_t timeout_hint,
239 hsa_wait_state_t wait_state_hint);
240
241} // extern "C"
242
243#endif // SANITIZER_OFFLOAD_HSA_H
244