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