2 * Copyright 2016 Bas Nieuwenhuizen
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:
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.
20 * The above copyright notice and this permission notice (including the
21 * next paragraph) shall be included in all copies or substantial portions
25 #ifndef AC_LLVM_BUILD_H
26 #define AC_LLVM_BUILD_H
29 #include <llvm-c/TargetMachine.h>
30 #include "compiler/nir/nir.h"
31 #include "amd_family.h"
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 */
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] */
54 struct ac_llvm_context
{
55 LLVMContextRef context
;
57 LLVMBuilderRef builder
;
94 struct ac_llvm_flow
*flow
;
96 unsigned flow_depth_max
;
98 unsigned range_md_kind
;
99 unsigned invariant_load_md_kind
;
100 unsigned uniform_md_kind
;
101 unsigned fpmath_md_kind
;
102 LLVMValueRef fpmath_md_2p5_ulp
;
103 LLVMValueRef empty_md
;
105 enum chip_class chip_class
;
106 enum radeon_family family
;
112 ac_llvm_context_init(struct ac_llvm_context
*ctx
,
113 enum chip_class chip_class
, enum radeon_family family
);
116 ac_llvm_context_dispose(struct ac_llvm_context
*ctx
);
119 ac_get_llvm_num_components(LLVMValueRef value
);
122 ac_get_elem_bits(struct ac_llvm_context
*ctx
, LLVMTypeRef type
);
125 ac_llvm_extract_elem(struct ac_llvm_context
*ac
,
129 unsigned ac_get_type_size(LLVMTypeRef type
);
131 LLVMTypeRef
ac_to_integer_type(struct ac_llvm_context
*ctx
, LLVMTypeRef t
);
132 LLVMValueRef
ac_to_integer(struct ac_llvm_context
*ctx
, LLVMValueRef v
);
133 LLVMValueRef
ac_to_integer_or_pointer(struct ac_llvm_context
*ctx
, LLVMValueRef v
);
134 LLVMTypeRef
ac_to_float_type(struct ac_llvm_context
*ctx
, LLVMTypeRef t
);
135 LLVMValueRef
ac_to_float(struct ac_llvm_context
*ctx
, LLVMValueRef v
);
138 ac_build_intrinsic(struct ac_llvm_context
*ctx
, const char *name
,
139 LLVMTypeRef return_type
, LLVMValueRef
*params
,
140 unsigned param_count
, unsigned attrib_mask
);
142 void ac_build_type_name_for_intr(LLVMTypeRef type
, char *buf
, unsigned bufsize
);
145 ac_build_phi(struct ac_llvm_context
*ctx
, LLVMTypeRef type
,
146 unsigned count_incoming
, LLVMValueRef
*values
,
147 LLVMBasicBlockRef
*blocks
);
149 void ac_build_s_barrier(struct ac_llvm_context
*ctx
);
150 void ac_build_optimization_barrier(struct ac_llvm_context
*ctx
,
151 LLVMValueRef
*pvgpr
);
153 LLVMValueRef
ac_build_shader_clock(struct ac_llvm_context
*ctx
);
155 LLVMValueRef
ac_build_ballot(struct ac_llvm_context
*ctx
, LLVMValueRef value
);
157 LLVMValueRef
ac_build_vote_all(struct ac_llvm_context
*ctx
, LLVMValueRef value
);
159 LLVMValueRef
ac_build_vote_any(struct ac_llvm_context
*ctx
, LLVMValueRef value
);
161 LLVMValueRef
ac_build_vote_eq(struct ac_llvm_context
*ctx
, LLVMValueRef value
);
164 ac_build_varying_gather_values(struct ac_llvm_context
*ctx
, LLVMValueRef
*values
,
165 unsigned value_count
, unsigned component
);
168 ac_build_gather_values_extended(struct ac_llvm_context
*ctx
,
169 LLVMValueRef
*values
,
170 unsigned value_count
,
171 unsigned value_stride
,
175 ac_build_gather_values(struct ac_llvm_context
*ctx
,
176 LLVMValueRef
*values
,
177 unsigned value_count
);
178 LLVMValueRef
ac_build_expand_to_vec4(struct ac_llvm_context
*ctx
,
180 unsigned num_channels
);
181 LLVMValueRef
ac_build_round(struct ac_llvm_context
*ctx
, LLVMValueRef value
);
184 ac_build_fdiv(struct ac_llvm_context
*ctx
,
188 LLVMValueRef
ac_build_fast_udiv(struct ac_llvm_context
*ctx
,
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
,
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
,
202 LLVMValueRef multiplier
,
203 LLVMValueRef post_shift
);
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
);
213 ac_build_fs_interp(struct ac_llvm_context
*ctx
,
214 LLVMValueRef llvm_chan
,
215 LLVMValueRef attr_number
,
221 ac_build_fs_interp_f16(struct ac_llvm_context
*ctx
,
222 LLVMValueRef llvm_chan
,
223 LLVMValueRef attr_number
,
229 ac_build_fs_interp_mov(struct ac_llvm_context
*ctx
,
230 LLVMValueRef parameter
,
231 LLVMValueRef llvm_chan
,
232 LLVMValueRef attr_number
,
233 LLVMValueRef params
);
236 ac_build_gep_ptr(struct ac_llvm_context
*ctx
,
237 LLVMValueRef base_ptr
,
241 ac_build_gep0(struct ac_llvm_context
*ctx
,
242 LLVMValueRef base_ptr
,
244 LLVMValueRef
ac_build_pointer_add(struct ac_llvm_context
*ctx
, LLVMValueRef ptr
,
248 ac_build_indexed_store(struct ac_llvm_context
*ctx
,
249 LLVMValueRef base_ptr
, LLVMValueRef index
,
252 LLVMValueRef
ac_build_load(struct ac_llvm_context
*ctx
, LLVMValueRef base_ptr
,
254 LLVMValueRef
ac_build_load_invariant(struct ac_llvm_context
*ctx
,
255 LLVMValueRef base_ptr
, LLVMValueRef index
);
256 LLVMValueRef
ac_build_load_to_sgpr(struct ac_llvm_context
*ctx
,
257 LLVMValueRef base_ptr
, LLVMValueRef index
);
258 LLVMValueRef
ac_build_load_to_sgpr_uint_wraparound(struct ac_llvm_context
*ctx
,
259 LLVMValueRef base_ptr
, LLVMValueRef index
);
262 ac_build_buffer_store_dword(struct ac_llvm_context
*ctx
,
265 unsigned num_channels
,
266 LLVMValueRef voffset
,
267 LLVMValueRef soffset
,
268 unsigned inst_offset
,
271 bool writeonly_memory
,
272 bool swizzle_enable_hint
);
275 ac_build_buffer_store_format(struct ac_llvm_context
*ctx
,
279 LLVMValueRef voffset
,
280 unsigned num_channels
,
282 bool writeonly_memory
);
285 ac_build_buffer_load(struct ac_llvm_context
*ctx
,
289 LLVMValueRef voffset
,
290 LLVMValueRef soffset
,
291 unsigned inst_offset
,
297 LLVMValueRef
ac_build_buffer_load_format(struct ac_llvm_context
*ctx
,
300 LLVMValueRef voffset
,
301 unsigned num_channels
,
305 /* load_format that handles the stride & element count better if idxen is
306 * disabled by LLVM. */
307 LLVMValueRef
ac_build_buffer_load_format_gfx9_safe(struct ac_llvm_context
*ctx
,
310 LLVMValueRef voffset
,
311 unsigned num_channels
,
316 ac_build_tbuffer_load_short(struct ac_llvm_context
*ctx
,
318 LLVMValueRef voffset
,
319 LLVMValueRef soffset
,
320 LLVMValueRef immoffset
,
324 ac_build_tbuffer_load_byte(struct ac_llvm_context
*ctx
,
326 LLVMValueRef voffset
,
327 LLVMValueRef soffset
,
328 LLVMValueRef immoffset
,
332 ac_build_struct_tbuffer_load(struct ac_llvm_context
*ctx
,
335 LLVMValueRef voffset
,
336 LLVMValueRef soffset
,
337 LLVMValueRef immoffset
,
338 unsigned num_channels
,
346 ac_build_raw_tbuffer_load(struct ac_llvm_context
*ctx
,
348 LLVMValueRef voffset
,
349 LLVMValueRef soffset
,
350 LLVMValueRef immoffset
,
351 unsigned num_channels
,
359 ac_build_tbuffer_store_short(struct ac_llvm_context
*ctx
,
362 LLVMValueRef voffset
,
363 LLVMValueRef soffset
,
365 bool writeonly_memory
);
368 ac_build_tbuffer_store_byte(struct ac_llvm_context
*ctx
,
371 LLVMValueRef voffset
,
372 LLVMValueRef soffset
,
374 bool writeonly_memory
);
377 ac_build_struct_tbuffer_store(struct ac_llvm_context
*ctx
,
381 LLVMValueRef voffset
,
382 LLVMValueRef soffset
,
383 LLVMValueRef immoffset
,
384 unsigned num_channels
,
389 bool writeonly_memory
);
392 ac_build_raw_tbuffer_store(struct ac_llvm_context
*ctx
,
395 LLVMValueRef voffset
,
396 LLVMValueRef soffset
,
397 LLVMValueRef immoffset
,
398 unsigned num_channels
,
403 bool writeonly_memory
);
406 ac_get_thread_id(struct ac_llvm_context
*ctx
);
408 #define AC_TID_MASK_TOP_LEFT 0xfffffffc
409 #define AC_TID_MASK_TOP 0xfffffffd
410 #define AC_TID_MASK_LEFT 0xfffffffe
413 ac_build_ddxy(struct ac_llvm_context
*ctx
,
418 #define AC_SENDMSG_GS 2
419 #define AC_SENDMSG_GS_DONE 3
421 #define AC_SENDMSG_GS_OP_NOP (0 << 4)
422 #define AC_SENDMSG_GS_OP_CUT (1 << 4)
423 #define AC_SENDMSG_GS_OP_EMIT (2 << 4)
424 #define AC_SENDMSG_GS_OP_EMIT_CUT (3 << 4)
426 void ac_build_sendmsg(struct ac_llvm_context
*ctx
,
428 LLVMValueRef wave_id
);
430 LLVMValueRef
ac_build_imsb(struct ac_llvm_context
*ctx
,
432 LLVMTypeRef dst_type
);
434 LLVMValueRef
ac_build_umsb(struct ac_llvm_context
*ctx
,
436 LLVMTypeRef dst_type
);
437 LLVMValueRef
ac_build_fmin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
439 LLVMValueRef
ac_build_fmax(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
441 LLVMValueRef
ac_build_imin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
443 LLVMValueRef
ac_build_imax(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
445 LLVMValueRef
ac_build_umin(struct ac_llvm_context
*ctx
, LLVMValueRef a
, LLVMValueRef b
);
446 LLVMValueRef
ac_build_umax(struct ac_llvm_context
*ctx
, LLVMValueRef a
, LLVMValueRef b
);
447 LLVMValueRef
ac_build_clamp(struct ac_llvm_context
*ctx
, LLVMValueRef value
);
449 struct ac_export_args
{
452 unsigned enabled_channels
;
458 void ac_build_export(struct ac_llvm_context
*ctx
, struct ac_export_args
*a
);
460 void ac_build_export_null(struct ac_llvm_context
*ctx
);
462 enum ac_image_opcode
{
470 ac_image_get_resinfo
,
472 ac_image_atomic_cmpswap
,
492 ac_image_cube
, // includes cube arrays
496 ac_image_2darraymsaa
,
499 /* These cache policy bits match the definitions used by the LLVM intrinsics. */
500 enum ac_image_cache_policy
{
505 struct ac_image_args
{
506 enum ac_image_opcode opcode
: 4;
507 enum ac_atomic_op atomic
: 4; /* for the ac_image_atomic opcode */
508 enum ac_image_dim dim
: 3;
510 unsigned cache_policy
: 2;
513 unsigned attributes
; /* additional call-site specific AC_FUNC_ATTRs */
515 LLVMValueRef resource
;
516 LLVMValueRef sampler
;
517 LLVMValueRef data
[2]; /* data[0] is source data (vector); data[1] is cmp for cmpswap */
520 LLVMValueRef compare
;
521 LLVMValueRef derivs
[6];
522 LLVMValueRef coords
[4];
523 LLVMValueRef lod
; // also used by ac_image_get_resinfo
526 LLVMValueRef
ac_build_image_opcode(struct ac_llvm_context
*ctx
,
527 struct ac_image_args
*a
);
528 LLVMValueRef
ac_build_cvt_pkrtz_f16(struct ac_llvm_context
*ctx
,
529 LLVMValueRef args
[2]);
530 LLVMValueRef
ac_build_cvt_pknorm_i16(struct ac_llvm_context
*ctx
,
531 LLVMValueRef args
[2]);
532 LLVMValueRef
ac_build_cvt_pknorm_u16(struct ac_llvm_context
*ctx
,
533 LLVMValueRef args
[2]);
534 LLVMValueRef
ac_build_cvt_pk_i16(struct ac_llvm_context
*ctx
,
535 LLVMValueRef args
[2], unsigned bits
, bool hi
);
536 LLVMValueRef
ac_build_cvt_pk_u16(struct ac_llvm_context
*ctx
,
537 LLVMValueRef args
[2], unsigned bits
, bool hi
);
538 LLVMValueRef
ac_build_wqm_vote(struct ac_llvm_context
*ctx
, LLVMValueRef i1
);
539 void ac_build_kill_if_false(struct ac_llvm_context
*ctx
, LLVMValueRef i1
);
540 LLVMValueRef
ac_build_bfe(struct ac_llvm_context
*ctx
, LLVMValueRef input
,
541 LLVMValueRef offset
, LLVMValueRef width
,
543 LLVMValueRef
ac_build_imad(struct ac_llvm_context
*ctx
, LLVMValueRef s0
,
544 LLVMValueRef s1
, LLVMValueRef s2
);
545 LLVMValueRef
ac_build_fmad(struct ac_llvm_context
*ctx
, LLVMValueRef s0
,
546 LLVMValueRef s1
, LLVMValueRef s2
);
548 void ac_build_waitcnt(struct ac_llvm_context
*ctx
, unsigned simm16
);
550 LLVMValueRef
ac_build_fract(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
553 LLVMValueRef
ac_build_fmed3(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
554 LLVMValueRef src1
, LLVMValueRef src2
,
557 LLVMValueRef
ac_build_isign(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
560 LLVMValueRef
ac_build_fsign(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
563 LLVMValueRef
ac_build_bit_count(struct ac_llvm_context
*ctx
, LLVMValueRef src0
);
565 LLVMValueRef
ac_build_bitfield_reverse(struct ac_llvm_context
*ctx
,
568 void ac_optimize_vs_outputs(struct ac_llvm_context
*ac
,
569 LLVMValueRef main_fn
,
570 uint8_t *vs_output_param_offset
,
571 uint32_t num_outputs
,
572 uint8_t *num_param_exports
);
573 void ac_init_exec_full_mask(struct ac_llvm_context
*ctx
);
575 void ac_declare_lds_as_pointer(struct ac_llvm_context
*ac
);
576 LLVMValueRef
ac_lds_load(struct ac_llvm_context
*ctx
,
577 LLVMValueRef dw_addr
);
578 void ac_lds_store(struct ac_llvm_context
*ctx
,
579 LLVMValueRef dw_addr
, LLVMValueRef value
);
581 LLVMValueRef
ac_find_lsb(struct ac_llvm_context
*ctx
,
582 LLVMTypeRef dst_type
,
585 LLVMTypeRef
ac_array_in_const_addr_space(LLVMTypeRef elem_type
);
586 LLVMTypeRef
ac_array_in_const32_addr_space(LLVMTypeRef elem_type
);
588 void ac_build_bgnloop(struct ac_llvm_context
*ctx
, int lable_id
);
589 void ac_build_break(struct ac_llvm_context
*ctx
);
590 void ac_build_continue(struct ac_llvm_context
*ctx
);
591 void ac_build_else(struct ac_llvm_context
*ctx
, int lable_id
);
592 void ac_build_endif(struct ac_llvm_context
*ctx
, int lable_id
);
593 void ac_build_endloop(struct ac_llvm_context
*ctx
, int lable_id
);
594 void ac_build_ifcc(struct ac_llvm_context
*ctx
, LLVMValueRef cond
, int label_id
);
595 void ac_build_if(struct ac_llvm_context
*ctx
, LLVMValueRef value
,
597 void ac_build_uif(struct ac_llvm_context
*ctx
, LLVMValueRef value
,
600 LLVMValueRef
ac_build_alloca(struct ac_llvm_context
*ac
, LLVMTypeRef type
,
602 LLVMValueRef
ac_build_alloca_undef(struct ac_llvm_context
*ac
, LLVMTypeRef type
,
605 LLVMValueRef
ac_cast_ptr(struct ac_llvm_context
*ctx
, LLVMValueRef ptr
,
608 LLVMValueRef
ac_trim_vector(struct ac_llvm_context
*ctx
, LLVMValueRef value
,
611 LLVMValueRef
ac_unpack_param(struct ac_llvm_context
*ctx
, LLVMValueRef param
,
612 unsigned rshift
, unsigned bitwidth
);
614 void ac_apply_fmask_to_sample(struct ac_llvm_context
*ac
, LLVMValueRef fmask
,
615 LLVMValueRef
*addr
, bool is_array_tex
);
618 ac_build_ds_swizzle(struct ac_llvm_context
*ctx
, LLVMValueRef src
, unsigned mask
);
621 ac_build_readlane(struct ac_llvm_context
*ctx
, LLVMValueRef src
, LLVMValueRef lane
);
624 ac_build_writelane(struct ac_llvm_context
*ctx
, LLVMValueRef src
, LLVMValueRef value
, LLVMValueRef lane
);
627 ac_build_mbcnt(struct ac_llvm_context
*ctx
, LLVMValueRef mask
);
630 ac_build_inclusive_scan(struct ac_llvm_context
*ctx
, LLVMValueRef src
, nir_op op
);
633 ac_build_exclusive_scan(struct ac_llvm_context
*ctx
, LLVMValueRef src
, nir_op op
);
636 ac_build_reduce(struct ac_llvm_context
*ctx
, LLVMValueRef src
, nir_op op
, unsigned cluster_size
);
639 * Common arguments for a scan/reduce operation that accumulates per-wave
640 * values across an entire workgroup, while respecting the order of waves.
644 bool enable_exclusive
;
645 bool enable_inclusive
;
647 LLVMValueRef src
; /* clobbered! */
648 LLVMValueRef result_reduce
;
649 LLVMValueRef result_exclusive
;
650 LLVMValueRef result_inclusive
;
652 LLVMValueRef waveidx
;
653 LLVMValueRef numwaves
; /* only needed for "reduce" operations */
655 /* T addrspace(LDS) pointer to the same type as value, at least maxwaves entries */
656 LLVMValueRef scratch
;
661 ac_build_wg_wavescan_top(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
);
663 ac_build_wg_wavescan_bottom(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
);
665 ac_build_wg_wavescan(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
);
668 ac_build_wg_scan_top(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
);
670 ac_build_wg_scan_bottom(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
);
672 ac_build_wg_scan(struct ac_llvm_context
*ctx
, struct ac_wg_scan
*ws
);
675 ac_build_quad_swizzle(struct ac_llvm_context
*ctx
, LLVMValueRef src
,
676 unsigned lane0
, unsigned lane1
, unsigned lane2
, unsigned lane3
);
679 ac_build_shuffle(struct ac_llvm_context
*ctx
, LLVMValueRef src
, LLVMValueRef index
);
682 ac_build_frexp_exp(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
686 ac_build_frexp_mant(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
690 ac_build_ddxy_interp(struct ac_llvm_context
*ctx
, LLVMValueRef interp_ij
);
693 ac_build_load_helper_invocation(struct ac_llvm_context
*ctx
);