radeonsi: implement 32-bit pointers in user data SGPRs (v2)
[mesa.git] / src / amd / common / ac_llvm_build.h
1 /*
2 * Copyright 2016 Bas Nieuwenhuizen
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sub license, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
15 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
17 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
18 * USE OR OTHER DEALINGS IN THE SOFTWARE.
19 *
20 * The above copyright notice and this permission notice (including the
21 * next paragraph) shall be included in all copies or substantial portions
22 * of the Software.
23 *
24 */
25 #ifndef AC_LLVM_BUILD_H
26 #define AC_LLVM_BUILD_H
27
28 #include <stdbool.h>
29 #include <llvm-c/TargetMachine.h>
30
31 #include "amd_family.h"
32
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36
37 #define HAVE_32BIT_POINTERS (HAVE_LLVM >= 0x0700)
38
39 enum {
40 /* CONST is the only address space that selects SMEM loads */
41 AC_CONST_ADDR_SPACE = HAVE_LLVM >= 0x700 ? 4 : 2,
42 AC_LOCAL_ADDR_SPACE = 3,
43 AC_CONST_32BIT_ADDR_SPACE = 6, /* same as CONST, but the pointer type has 32 bits */
44 };
45
46 struct ac_llvm_context {
47 LLVMContextRef context;
48 LLVMModuleRef module;
49 LLVMBuilderRef builder;
50
51 LLVMTypeRef voidt;
52 LLVMTypeRef i1;
53 LLVMTypeRef i8;
54 LLVMTypeRef i16;
55 LLVMTypeRef i32;
56 LLVMTypeRef i64;
57 LLVMTypeRef intptr;
58 LLVMTypeRef f16;
59 LLVMTypeRef f32;
60 LLVMTypeRef f64;
61 LLVMTypeRef v2i16;
62 LLVMTypeRef v2i32;
63 LLVMTypeRef v3i32;
64 LLVMTypeRef v4i32;
65 LLVMTypeRef v2f32;
66 LLVMTypeRef v4f32;
67 LLVMTypeRef v8i32;
68
69 LLVMValueRef i32_0;
70 LLVMValueRef i32_1;
71 LLVMValueRef i64_0;
72 LLVMValueRef i64_1;
73 LLVMValueRef f32_0;
74 LLVMValueRef f32_1;
75 LLVMValueRef f64_0;
76 LLVMValueRef f64_1;
77 LLVMValueRef i1true;
78 LLVMValueRef i1false;
79
80 unsigned range_md_kind;
81 unsigned invariant_load_md_kind;
82 unsigned uniform_md_kind;
83 unsigned fpmath_md_kind;
84 LLVMValueRef fpmath_md_2p5_ulp;
85 LLVMValueRef empty_md;
86
87 enum chip_class chip_class;
88 enum radeon_family family;
89
90 LLVMValueRef lds;
91 };
92
93 void
94 ac_llvm_context_init(struct ac_llvm_context *ctx, LLVMContextRef context,
95 enum chip_class chip_class, enum radeon_family family);
96
97 int
98 ac_get_llvm_num_components(LLVMValueRef value);
99
100 int
101 ac_get_elem_bits(struct ac_llvm_context *ctx, LLVMTypeRef type);
102
103 LLVMValueRef
104 ac_llvm_extract_elem(struct ac_llvm_context *ac,
105 LLVMValueRef value,
106 int index);
107
108 unsigned ac_get_type_size(LLVMTypeRef type);
109
110 LLVMTypeRef ac_to_integer_type(struct ac_llvm_context *ctx, LLVMTypeRef t);
111 LLVMValueRef ac_to_integer(struct ac_llvm_context *ctx, LLVMValueRef v);
112 LLVMTypeRef ac_to_float_type(struct ac_llvm_context *ctx, LLVMTypeRef t);
113 LLVMValueRef ac_to_float(struct ac_llvm_context *ctx, LLVMValueRef v);
114
115 LLVMValueRef
116 ac_build_intrinsic(struct ac_llvm_context *ctx, const char *name,
117 LLVMTypeRef return_type, LLVMValueRef *params,
118 unsigned param_count, unsigned attrib_mask);
119
120 void ac_build_type_name_for_intr(LLVMTypeRef type, char *buf, unsigned bufsize);
121
122 LLVMValueRef
123 ac_build_phi(struct ac_llvm_context *ctx, LLVMTypeRef type,
124 unsigned count_incoming, LLVMValueRef *values,
125 LLVMBasicBlockRef *blocks);
126
127 void ac_build_optimization_barrier(struct ac_llvm_context *ctx,
128 LLVMValueRef *pvgpr);
129
130 LLVMValueRef ac_build_shader_clock(struct ac_llvm_context *ctx);
131
132 LLVMValueRef ac_build_ballot(struct ac_llvm_context *ctx, LLVMValueRef value);
133
134 LLVMValueRef ac_build_vote_all(struct ac_llvm_context *ctx, LLVMValueRef value);
135
136 LLVMValueRef ac_build_vote_any(struct ac_llvm_context *ctx, LLVMValueRef value);
137
138 LLVMValueRef ac_build_vote_eq(struct ac_llvm_context *ctx, LLVMValueRef value);
139
140 LLVMValueRef
141 ac_build_varying_gather_values(struct ac_llvm_context *ctx, LLVMValueRef *values,
142 unsigned value_count, unsigned component);
143
144 LLVMValueRef
145 ac_build_gather_values_extended(struct ac_llvm_context *ctx,
146 LLVMValueRef *values,
147 unsigned value_count,
148 unsigned value_stride,
149 bool load,
150 bool always_vector);
151 LLVMValueRef
152 ac_build_gather_values(struct ac_llvm_context *ctx,
153 LLVMValueRef *values,
154 unsigned value_count);
155 LLVMValueRef ac_build_expand_to_vec4(struct ac_llvm_context *ctx,
156 LLVMValueRef value,
157 unsigned num_channels);
158
159 LLVMValueRef
160 ac_build_fdiv(struct ac_llvm_context *ctx,
161 LLVMValueRef num,
162 LLVMValueRef den);
163
164 void
165 ac_prepare_cube_coords(struct ac_llvm_context *ctx,
166 bool is_deriv, bool is_array, bool is_lod,
167 LLVMValueRef *coords_arg,
168 LLVMValueRef *derivs_arg);
169
170
171 LLVMValueRef
172 ac_build_fs_interp(struct ac_llvm_context *ctx,
173 LLVMValueRef llvm_chan,
174 LLVMValueRef attr_number,
175 LLVMValueRef params,
176 LLVMValueRef i,
177 LLVMValueRef j);
178
179 LLVMValueRef
180 ac_build_fs_interp_mov(struct ac_llvm_context *ctx,
181 LLVMValueRef parameter,
182 LLVMValueRef llvm_chan,
183 LLVMValueRef attr_number,
184 LLVMValueRef params);
185
186 LLVMValueRef
187 ac_build_gep0(struct ac_llvm_context *ctx,
188 LLVMValueRef base_ptr,
189 LLVMValueRef index);
190
191 void
192 ac_build_indexed_store(struct ac_llvm_context *ctx,
193 LLVMValueRef base_ptr, LLVMValueRef index,
194 LLVMValueRef value);
195
196 LLVMValueRef ac_build_load(struct ac_llvm_context *ctx, LLVMValueRef base_ptr,
197 LLVMValueRef index);
198 LLVMValueRef ac_build_load_invariant(struct ac_llvm_context *ctx,
199 LLVMValueRef base_ptr, LLVMValueRef index);
200 LLVMValueRef ac_build_load_to_sgpr(struct ac_llvm_context *ctx,
201 LLVMValueRef base_ptr, LLVMValueRef index);
202
203 void
204 ac_build_buffer_store_dword(struct ac_llvm_context *ctx,
205 LLVMValueRef rsrc,
206 LLVMValueRef vdata,
207 unsigned num_channels,
208 LLVMValueRef voffset,
209 LLVMValueRef soffset,
210 unsigned inst_offset,
211 bool glc,
212 bool slc,
213 bool writeonly_memory,
214 bool swizzle_enable_hint);
215 LLVMValueRef
216 ac_build_buffer_load(struct ac_llvm_context *ctx,
217 LLVMValueRef rsrc,
218 int num_channels,
219 LLVMValueRef vindex,
220 LLVMValueRef voffset,
221 LLVMValueRef soffset,
222 unsigned inst_offset,
223 unsigned glc,
224 unsigned slc,
225 bool can_speculate,
226 bool allow_smem);
227
228 LLVMValueRef ac_build_buffer_load_format(struct ac_llvm_context *ctx,
229 LLVMValueRef rsrc,
230 LLVMValueRef vindex,
231 LLVMValueRef voffset,
232 unsigned num_channels,
233 bool glc,
234 bool can_speculate);
235
236 LLVMValueRef
237 ac_get_thread_id(struct ac_llvm_context *ctx);
238
239 #define AC_TID_MASK_TOP_LEFT 0xfffffffc
240 #define AC_TID_MASK_TOP 0xfffffffd
241 #define AC_TID_MASK_LEFT 0xfffffffe
242
243 LLVMValueRef
244 ac_build_ddxy(struct ac_llvm_context *ctx,
245 uint32_t mask,
246 int idx,
247 LLVMValueRef val);
248
249 #define AC_SENDMSG_GS 2
250 #define AC_SENDMSG_GS_DONE 3
251
252 #define AC_SENDMSG_GS_OP_NOP (0 << 4)
253 #define AC_SENDMSG_GS_OP_CUT (1 << 4)
254 #define AC_SENDMSG_GS_OP_EMIT (2 << 4)
255 #define AC_SENDMSG_GS_OP_EMIT_CUT (3 << 4)
256
257 void ac_build_sendmsg(struct ac_llvm_context *ctx,
258 uint32_t msg,
259 LLVMValueRef wave_id);
260
261 LLVMValueRef ac_build_imsb(struct ac_llvm_context *ctx,
262 LLVMValueRef arg,
263 LLVMTypeRef dst_type);
264
265 LLVMValueRef ac_build_umsb(struct ac_llvm_context *ctx,
266 LLVMValueRef arg,
267 LLVMTypeRef dst_type);
268 LLVMValueRef ac_build_fmin(struct ac_llvm_context *ctx, LLVMValueRef a,
269 LLVMValueRef b);
270 LLVMValueRef ac_build_fmax(struct ac_llvm_context *ctx, LLVMValueRef a,
271 LLVMValueRef b);
272 LLVMValueRef ac_build_imin(struct ac_llvm_context *ctx, LLVMValueRef a,
273 LLVMValueRef b);
274 LLVMValueRef ac_build_imax(struct ac_llvm_context *ctx, LLVMValueRef a,
275 LLVMValueRef b);
276 LLVMValueRef ac_build_umin(struct ac_llvm_context *ctx, LLVMValueRef a, LLVMValueRef b);
277 LLVMValueRef ac_build_clamp(struct ac_llvm_context *ctx, LLVMValueRef value);
278
279 struct ac_export_args {
280 LLVMValueRef out[4];
281 unsigned target;
282 unsigned enabled_channels;
283 bool compr;
284 bool done;
285 bool valid_mask;
286 };
287
288 void ac_build_export(struct ac_llvm_context *ctx, struct ac_export_args *a);
289
290 void ac_build_export_null(struct ac_llvm_context *ctx);
291
292 enum ac_image_opcode {
293 ac_image_sample,
294 ac_image_gather4,
295 ac_image_load,
296 ac_image_load_mip,
297 ac_image_get_lod,
298 ac_image_get_resinfo,
299 };
300
301 struct ac_image_args {
302 enum ac_image_opcode opcode;
303 bool level_zero;
304 bool bias;
305 bool lod;
306 bool deriv;
307 bool compare;
308 bool offset;
309
310 LLVMValueRef resource;
311 LLVMValueRef sampler;
312 LLVMValueRef addr;
313 unsigned dmask;
314 bool unorm;
315 bool da;
316 };
317
318 LLVMValueRef ac_build_image_opcode(struct ac_llvm_context *ctx,
319 struct ac_image_args *a);
320 LLVMValueRef ac_build_cvt_pkrtz_f16(struct ac_llvm_context *ctx,
321 LLVMValueRef args[2]);
322 LLVMValueRef ac_build_cvt_pknorm_i16(struct ac_llvm_context *ctx,
323 LLVMValueRef args[2]);
324 LLVMValueRef ac_build_cvt_pknorm_u16(struct ac_llvm_context *ctx,
325 LLVMValueRef args[2]);
326 LLVMValueRef ac_build_cvt_pk_i16(struct ac_llvm_context *ctx,
327 LLVMValueRef args[2], unsigned bits, bool hi);
328 LLVMValueRef ac_build_cvt_pk_u16(struct ac_llvm_context *ctx,
329 LLVMValueRef args[2], unsigned bits, bool hi);
330 LLVMValueRef ac_build_wqm_vote(struct ac_llvm_context *ctx, LLVMValueRef i1);
331 void ac_build_kill_if_false(struct ac_llvm_context *ctx, LLVMValueRef i1);
332 LLVMValueRef ac_build_bfe(struct ac_llvm_context *ctx, LLVMValueRef input,
333 LLVMValueRef offset, LLVMValueRef width,
334 bool is_signed);
335
336 void ac_build_waitcnt(struct ac_llvm_context *ctx, unsigned simm16);
337
338 void ac_get_image_intr_name(const char *base_name,
339 LLVMTypeRef data_type,
340 LLVMTypeRef coords_type,
341 LLVMTypeRef rsrc_type,
342 char *out_name, unsigned out_len);
343
344 void ac_optimize_vs_outputs(struct ac_llvm_context *ac,
345 LLVMValueRef main_fn,
346 uint8_t *vs_output_param_offset,
347 uint32_t num_outputs,
348 uint8_t *num_param_exports);
349 void ac_init_exec_full_mask(struct ac_llvm_context *ctx);
350
351 void ac_declare_lds_as_pointer(struct ac_llvm_context *ac);
352 LLVMValueRef ac_lds_load(struct ac_llvm_context *ctx,
353 LLVMValueRef dw_addr);
354 void ac_lds_store(struct ac_llvm_context *ctx,
355 LLVMValueRef dw_addr, LLVMValueRef value);
356
357 LLVMValueRef ac_find_lsb(struct ac_llvm_context *ctx,
358 LLVMTypeRef dst_type,
359 LLVMValueRef src0);
360
361 LLVMTypeRef ac_array_in_const_addr_space(LLVMTypeRef elem_type);
362 LLVMTypeRef ac_array_in_const32_addr_space(LLVMTypeRef elem_type);
363
364 #ifdef __cplusplus
365 }
366 #endif
367
368 #endif