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