0d261bae0977edf1166275faad6917e4bb09fe27
[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 #include "compiler/nir/nir.h"
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 /* Combine these with & instead of |. */
47 #define NOOP_WAITCNT 0xcf7f
48 #define LGKM_CNT 0xc07f
49 #define EXP_CNT 0xcf0f
50 #define VM_CNT 0x0f70 /* On GFX9, vmcnt has 6 bits in [0:3] and [14:15] */
51
52 struct ac_llvm_flow;
53
54 struct ac_llvm_context {
55 LLVMContextRef context;
56 LLVMModuleRef module;
57 LLVMBuilderRef builder;
58
59 LLVMTypeRef voidt;
60 LLVMTypeRef i1;
61 LLVMTypeRef i8;
62 LLVMTypeRef i16;
63 LLVMTypeRef i32;
64 LLVMTypeRef i64;
65 LLVMTypeRef intptr;
66 LLVMTypeRef f16;
67 LLVMTypeRef f32;
68 LLVMTypeRef f64;
69 LLVMTypeRef v2i16;
70 LLVMTypeRef v2i32;
71 LLVMTypeRef v3i32;
72 LLVMTypeRef v4i32;
73 LLVMTypeRef v2f32;
74 LLVMTypeRef v4f32;
75 LLVMTypeRef v8i32;
76
77 LLVMValueRef i32_0;
78 LLVMValueRef i32_1;
79 LLVMValueRef i64_0;
80 LLVMValueRef i64_1;
81 LLVMValueRef f32_0;
82 LLVMValueRef f32_1;
83 LLVMValueRef f64_0;
84 LLVMValueRef f64_1;
85 LLVMValueRef i1true;
86 LLVMValueRef i1false;
87
88 struct ac_llvm_flow *flow;
89 unsigned flow_depth;
90 unsigned flow_depth_max;
91
92 unsigned range_md_kind;
93 unsigned invariant_load_md_kind;
94 unsigned uniform_md_kind;
95 unsigned fpmath_md_kind;
96 LLVMValueRef fpmath_md_2p5_ulp;
97 LLVMValueRef empty_md;
98
99 enum chip_class chip_class;
100 enum radeon_family family;
101
102 LLVMValueRef lds;
103 };
104
105 void
106 ac_llvm_context_init(struct ac_llvm_context *ctx,
107 enum chip_class chip_class, enum radeon_family family);
108
109 void
110 ac_llvm_context_dispose(struct ac_llvm_context *ctx);
111
112 int
113 ac_get_llvm_num_components(LLVMValueRef value);
114
115 int
116 ac_get_elem_bits(struct ac_llvm_context *ctx, LLVMTypeRef type);
117
118 LLVMValueRef
119 ac_llvm_extract_elem(struct ac_llvm_context *ac,
120 LLVMValueRef value,
121 int index);
122
123 unsigned ac_get_type_size(LLVMTypeRef type);
124
125 LLVMTypeRef ac_to_integer_type(struct ac_llvm_context *ctx, LLVMTypeRef t);
126 LLVMValueRef ac_to_integer(struct ac_llvm_context *ctx, LLVMValueRef v);
127 LLVMTypeRef ac_to_float_type(struct ac_llvm_context *ctx, LLVMTypeRef t);
128 LLVMValueRef ac_to_float(struct ac_llvm_context *ctx, LLVMValueRef v);
129
130 LLVMValueRef
131 ac_build_intrinsic(struct ac_llvm_context *ctx, const char *name,
132 LLVMTypeRef return_type, LLVMValueRef *params,
133 unsigned param_count, unsigned attrib_mask);
134
135 void ac_build_type_name_for_intr(LLVMTypeRef type, char *buf, unsigned bufsize);
136
137 LLVMValueRef
138 ac_build_phi(struct ac_llvm_context *ctx, LLVMTypeRef type,
139 unsigned count_incoming, LLVMValueRef *values,
140 LLVMBasicBlockRef *blocks);
141
142 void ac_build_s_barrier(struct ac_llvm_context *ctx);
143 void ac_build_optimization_barrier(struct ac_llvm_context *ctx,
144 LLVMValueRef *pvgpr);
145
146 LLVMValueRef ac_build_shader_clock(struct ac_llvm_context *ctx);
147
148 LLVMValueRef ac_build_ballot(struct ac_llvm_context *ctx, LLVMValueRef value);
149
150 LLVMValueRef ac_build_vote_all(struct ac_llvm_context *ctx, LLVMValueRef value);
151
152 LLVMValueRef ac_build_vote_any(struct ac_llvm_context *ctx, LLVMValueRef value);
153
154 LLVMValueRef ac_build_vote_eq(struct ac_llvm_context *ctx, LLVMValueRef value);
155
156 LLVMValueRef
157 ac_build_varying_gather_values(struct ac_llvm_context *ctx, LLVMValueRef *values,
158 unsigned value_count, unsigned component);
159
160 LLVMValueRef
161 ac_build_gather_values_extended(struct ac_llvm_context *ctx,
162 LLVMValueRef *values,
163 unsigned value_count,
164 unsigned value_stride,
165 bool load,
166 bool always_vector);
167 LLVMValueRef
168 ac_build_gather_values(struct ac_llvm_context *ctx,
169 LLVMValueRef *values,
170 unsigned value_count);
171 LLVMValueRef ac_build_expand_to_vec4(struct ac_llvm_context *ctx,
172 LLVMValueRef value,
173 unsigned num_channels);
174
175 LLVMValueRef
176 ac_build_fdiv(struct ac_llvm_context *ctx,
177 LLVMValueRef num,
178 LLVMValueRef den);
179
180 void
181 ac_prepare_cube_coords(struct ac_llvm_context *ctx,
182 bool is_deriv, bool is_array, bool is_lod,
183 LLVMValueRef *coords_arg,
184 LLVMValueRef *derivs_arg);
185
186
187 LLVMValueRef
188 ac_build_fs_interp(struct ac_llvm_context *ctx,
189 LLVMValueRef llvm_chan,
190 LLVMValueRef attr_number,
191 LLVMValueRef params,
192 LLVMValueRef i,
193 LLVMValueRef j);
194
195 LLVMValueRef
196 ac_build_fs_interp_mov(struct ac_llvm_context *ctx,
197 LLVMValueRef parameter,
198 LLVMValueRef llvm_chan,
199 LLVMValueRef attr_number,
200 LLVMValueRef params);
201
202 LLVMValueRef
203 ac_build_gep0(struct ac_llvm_context *ctx,
204 LLVMValueRef base_ptr,
205 LLVMValueRef index);
206 LLVMValueRef ac_build_pointer_add(struct ac_llvm_context *ctx, LLVMValueRef ptr,
207 LLVMValueRef index);
208
209 void
210 ac_build_indexed_store(struct ac_llvm_context *ctx,
211 LLVMValueRef base_ptr, LLVMValueRef index,
212 LLVMValueRef value);
213
214 LLVMValueRef ac_build_load(struct ac_llvm_context *ctx, LLVMValueRef base_ptr,
215 LLVMValueRef index);
216 LLVMValueRef ac_build_load_invariant(struct ac_llvm_context *ctx,
217 LLVMValueRef base_ptr, LLVMValueRef index);
218 LLVMValueRef ac_build_load_to_sgpr(struct ac_llvm_context *ctx,
219 LLVMValueRef base_ptr, LLVMValueRef index);
220 LLVMValueRef ac_build_load_to_sgpr_uint_wraparound(struct ac_llvm_context *ctx,
221 LLVMValueRef base_ptr, LLVMValueRef index);
222
223 void
224 ac_build_buffer_store_dword(struct ac_llvm_context *ctx,
225 LLVMValueRef rsrc,
226 LLVMValueRef vdata,
227 unsigned num_channels,
228 LLVMValueRef voffset,
229 LLVMValueRef soffset,
230 unsigned inst_offset,
231 bool glc,
232 bool slc,
233 bool writeonly_memory,
234 bool swizzle_enable_hint);
235 LLVMValueRef
236 ac_build_buffer_load(struct ac_llvm_context *ctx,
237 LLVMValueRef rsrc,
238 int num_channels,
239 LLVMValueRef vindex,
240 LLVMValueRef voffset,
241 LLVMValueRef soffset,
242 unsigned inst_offset,
243 unsigned glc,
244 unsigned slc,
245 bool can_speculate,
246 bool allow_smem);
247
248 LLVMValueRef ac_build_buffer_load_format(struct ac_llvm_context *ctx,
249 LLVMValueRef rsrc,
250 LLVMValueRef vindex,
251 LLVMValueRef voffset,
252 unsigned num_channels,
253 bool glc,
254 bool can_speculate);
255
256 /* load_format that handles the stride & element count better if idxen is
257 * disabled by LLVM. */
258 LLVMValueRef ac_build_buffer_load_format_gfx9_safe(struct ac_llvm_context *ctx,
259 LLVMValueRef rsrc,
260 LLVMValueRef vindex,
261 LLVMValueRef voffset,
262 unsigned num_channels,
263 bool glc,
264 bool can_speculate);
265
266 LLVMValueRef
267 ac_build_tbuffer_load_short(struct ac_llvm_context *ctx,
268 LLVMValueRef rsrc,
269 LLVMValueRef vindex,
270 LLVMValueRef voffset,
271 LLVMValueRef soffset,
272 LLVMValueRef immoffset);
273
274 LLVMValueRef
275 ac_get_thread_id(struct ac_llvm_context *ctx);
276
277 #define AC_TID_MASK_TOP_LEFT 0xfffffffc
278 #define AC_TID_MASK_TOP 0xfffffffd
279 #define AC_TID_MASK_LEFT 0xfffffffe
280
281 LLVMValueRef
282 ac_build_ddxy(struct ac_llvm_context *ctx,
283 uint32_t mask,
284 int idx,
285 LLVMValueRef val);
286
287 #define AC_SENDMSG_GS 2
288 #define AC_SENDMSG_GS_DONE 3
289
290 #define AC_SENDMSG_GS_OP_NOP (0 << 4)
291 #define AC_SENDMSG_GS_OP_CUT (1 << 4)
292 #define AC_SENDMSG_GS_OP_EMIT (2 << 4)
293 #define AC_SENDMSG_GS_OP_EMIT_CUT (3 << 4)
294
295 void ac_build_sendmsg(struct ac_llvm_context *ctx,
296 uint32_t msg,
297 LLVMValueRef wave_id);
298
299 LLVMValueRef ac_build_imsb(struct ac_llvm_context *ctx,
300 LLVMValueRef arg,
301 LLVMTypeRef dst_type);
302
303 LLVMValueRef ac_build_umsb(struct ac_llvm_context *ctx,
304 LLVMValueRef arg,
305 LLVMTypeRef dst_type);
306 LLVMValueRef ac_build_fmin(struct ac_llvm_context *ctx, LLVMValueRef a,
307 LLVMValueRef b);
308 LLVMValueRef ac_build_fmax(struct ac_llvm_context *ctx, LLVMValueRef a,
309 LLVMValueRef b);
310 LLVMValueRef ac_build_imin(struct ac_llvm_context *ctx, LLVMValueRef a,
311 LLVMValueRef b);
312 LLVMValueRef ac_build_imax(struct ac_llvm_context *ctx, LLVMValueRef a,
313 LLVMValueRef b);
314 LLVMValueRef ac_build_umin(struct ac_llvm_context *ctx, LLVMValueRef a, LLVMValueRef b);
315 LLVMValueRef ac_build_clamp(struct ac_llvm_context *ctx, LLVMValueRef value);
316
317 struct ac_export_args {
318 LLVMValueRef out[4];
319 unsigned target;
320 unsigned enabled_channels;
321 bool compr;
322 bool done;
323 bool valid_mask;
324 };
325
326 void ac_build_export(struct ac_llvm_context *ctx, struct ac_export_args *a);
327
328 void ac_build_export_null(struct ac_llvm_context *ctx);
329
330 enum ac_image_opcode {
331 ac_image_sample,
332 ac_image_gather4,
333 ac_image_load,
334 ac_image_load_mip,
335 ac_image_store,
336 ac_image_store_mip,
337 ac_image_get_lod,
338 ac_image_get_resinfo,
339 ac_image_atomic,
340 ac_image_atomic_cmpswap,
341 };
342
343 enum ac_atomic_op {
344 ac_atomic_swap,
345 ac_atomic_add,
346 ac_atomic_sub,
347 ac_atomic_smin,
348 ac_atomic_umin,
349 ac_atomic_smax,
350 ac_atomic_umax,
351 ac_atomic_and,
352 ac_atomic_or,
353 ac_atomic_xor,
354 };
355
356 enum ac_image_dim {
357 ac_image_1d,
358 ac_image_2d,
359 ac_image_3d,
360 ac_image_cube, // includes cube arrays
361 ac_image_1darray,
362 ac_image_2darray,
363 ac_image_2dmsaa,
364 ac_image_2darraymsaa,
365 };
366
367 /* These cache policy bits match the definitions used by the LLVM intrinsics. */
368 enum ac_image_cache_policy {
369 ac_glc = 1 << 0,
370 ac_slc = 1 << 1,
371 };
372
373 struct ac_image_args {
374 enum ac_image_opcode opcode : 4;
375 enum ac_atomic_op atomic : 4; /* for the ac_image_atomic opcode */
376 enum ac_image_dim dim : 3;
377 unsigned dmask : 4;
378 unsigned cache_policy : 2;
379 bool unorm : 1;
380 bool level_zero : 1;
381 unsigned attributes; /* additional call-site specific AC_FUNC_ATTRs */
382
383 LLVMValueRef resource;
384 LLVMValueRef sampler;
385 LLVMValueRef data[2]; /* data[0] is source data (vector); data[1] is cmp for cmpswap */
386 LLVMValueRef offset;
387 LLVMValueRef bias;
388 LLVMValueRef compare;
389 LLVMValueRef derivs[6];
390 LLVMValueRef coords[4];
391 LLVMValueRef lod; // also used by ac_image_get_resinfo
392 };
393
394 LLVMValueRef ac_build_image_opcode(struct ac_llvm_context *ctx,
395 struct ac_image_args *a);
396 LLVMValueRef ac_build_cvt_pkrtz_f16(struct ac_llvm_context *ctx,
397 LLVMValueRef args[2]);
398 LLVMValueRef ac_build_cvt_pknorm_i16(struct ac_llvm_context *ctx,
399 LLVMValueRef args[2]);
400 LLVMValueRef ac_build_cvt_pknorm_u16(struct ac_llvm_context *ctx,
401 LLVMValueRef args[2]);
402 LLVMValueRef ac_build_cvt_pk_i16(struct ac_llvm_context *ctx,
403 LLVMValueRef args[2], unsigned bits, bool hi);
404 LLVMValueRef ac_build_cvt_pk_u16(struct ac_llvm_context *ctx,
405 LLVMValueRef args[2], unsigned bits, bool hi);
406 LLVMValueRef ac_build_wqm_vote(struct ac_llvm_context *ctx, LLVMValueRef i1);
407 void ac_build_kill_if_false(struct ac_llvm_context *ctx, LLVMValueRef i1);
408 LLVMValueRef ac_build_bfe(struct ac_llvm_context *ctx, LLVMValueRef input,
409 LLVMValueRef offset, LLVMValueRef width,
410 bool is_signed);
411 LLVMValueRef ac_build_imad(struct ac_llvm_context *ctx, LLVMValueRef s0,
412 LLVMValueRef s1, LLVMValueRef s2);
413 LLVMValueRef ac_build_fmad(struct ac_llvm_context *ctx, LLVMValueRef s0,
414 LLVMValueRef s1, LLVMValueRef s2);
415
416 void ac_build_waitcnt(struct ac_llvm_context *ctx, unsigned simm16);
417
418 LLVMValueRef ac_build_fract(struct ac_llvm_context *ctx, LLVMValueRef src0,
419 unsigned bitsize);
420
421 LLVMValueRef ac_build_isign(struct ac_llvm_context *ctx, LLVMValueRef src0,
422 unsigned bitsize);
423
424 LLVMValueRef ac_build_fsign(struct ac_llvm_context *ctx, LLVMValueRef src0,
425 unsigned bitsize);
426
427 void ac_optimize_vs_outputs(struct ac_llvm_context *ac,
428 LLVMValueRef main_fn,
429 uint8_t *vs_output_param_offset,
430 uint32_t num_outputs,
431 uint8_t *num_param_exports);
432 void ac_init_exec_full_mask(struct ac_llvm_context *ctx);
433
434 void ac_declare_lds_as_pointer(struct ac_llvm_context *ac);
435 LLVMValueRef ac_lds_load(struct ac_llvm_context *ctx,
436 LLVMValueRef dw_addr);
437 void ac_lds_store(struct ac_llvm_context *ctx,
438 LLVMValueRef dw_addr, LLVMValueRef value);
439
440 LLVMValueRef ac_find_lsb(struct ac_llvm_context *ctx,
441 LLVMTypeRef dst_type,
442 LLVMValueRef src0);
443
444 LLVMTypeRef ac_array_in_const_addr_space(LLVMTypeRef elem_type);
445 LLVMTypeRef ac_array_in_const32_addr_space(LLVMTypeRef elem_type);
446
447 void ac_build_bgnloop(struct ac_llvm_context *ctx, int lable_id);
448 void ac_build_break(struct ac_llvm_context *ctx);
449 void ac_build_continue(struct ac_llvm_context *ctx);
450 void ac_build_else(struct ac_llvm_context *ctx, int lable_id);
451 void ac_build_endif(struct ac_llvm_context *ctx, int lable_id);
452 void ac_build_endloop(struct ac_llvm_context *ctx, int lable_id);
453 void ac_build_if(struct ac_llvm_context *ctx, LLVMValueRef value,
454 int lable_id);
455 void ac_build_uif(struct ac_llvm_context *ctx, LLVMValueRef value,
456 int lable_id);
457
458 LLVMValueRef ac_build_alloca(struct ac_llvm_context *ac, LLVMTypeRef type,
459 const char *name);
460 LLVMValueRef ac_build_alloca_undef(struct ac_llvm_context *ac, LLVMTypeRef type,
461 const char *name);
462
463 LLVMValueRef ac_cast_ptr(struct ac_llvm_context *ctx, LLVMValueRef ptr,
464 LLVMTypeRef type);
465
466 LLVMValueRef ac_trim_vector(struct ac_llvm_context *ctx, LLVMValueRef value,
467 unsigned count);
468
469 LLVMValueRef ac_unpack_param(struct ac_llvm_context *ctx, LLVMValueRef param,
470 unsigned rshift, unsigned bitwidth);
471
472 void ac_apply_fmask_to_sample(struct ac_llvm_context *ac, LLVMValueRef fmask,
473 LLVMValueRef *addr, bool is_array_tex);
474
475 LLVMValueRef
476 ac_build_ds_swizzle(struct ac_llvm_context *ctx, LLVMValueRef src, unsigned mask);
477
478 LLVMValueRef
479 ac_build_readlane(struct ac_llvm_context *ctx, LLVMValueRef src, LLVMValueRef lane);
480
481 LLVMValueRef
482 ac_build_writelane(struct ac_llvm_context *ctx, LLVMValueRef src, LLVMValueRef value, LLVMValueRef lane);
483
484 LLVMValueRef
485 ac_build_mbcnt(struct ac_llvm_context *ctx, LLVMValueRef mask);
486
487 LLVMValueRef
488 ac_build_inclusive_scan(struct ac_llvm_context *ctx, LLVMValueRef src, nir_op op);
489
490 LLVMValueRef
491 ac_build_exclusive_scan(struct ac_llvm_context *ctx, LLVMValueRef src, nir_op op);
492
493 LLVMValueRef
494 ac_build_reduce(struct ac_llvm_context *ctx, LLVMValueRef src, nir_op op, unsigned cluster_size);
495
496 LLVMValueRef
497 ac_build_quad_swizzle(struct ac_llvm_context *ctx, LLVMValueRef src,
498 unsigned lane0, unsigned lane1, unsigned lane2, unsigned lane3);
499
500 LLVMValueRef
501 ac_build_shuffle(struct ac_llvm_context *ctx, LLVMValueRef src, LLVMValueRef index);
502
503 #ifdef __cplusplus
504 }
505 #endif
506
507 #endif