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"
37 #define HAVE_32BIT_POINTERS (HAVE_LLVM >= 0x0700)
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 */
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] */
56 struct ac_llvm_context
{
57 LLVMContextRef context
;
59 LLVMBuilderRef builder
;
92 struct ac_llvm_flow
*flow
;
94 unsigned flow_depth_max
;
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
;
103 enum chip_class chip_class
;
104 enum radeon_family family
;
110 ac_llvm_context_init(struct ac_llvm_context
*ctx
,
111 enum chip_class chip_class
, enum radeon_family family
);
114 ac_llvm_context_dispose(struct ac_llvm_context
*ctx
);
117 ac_get_llvm_num_components(LLVMValueRef value
);
120 ac_get_elem_bits(struct ac_llvm_context
*ctx
, LLVMTypeRef type
);
123 ac_llvm_extract_elem(struct ac_llvm_context
*ac
,
127 unsigned ac_get_type_size(LLVMTypeRef type
);
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
);
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
);
139 void ac_build_type_name_for_intr(LLVMTypeRef type
, char *buf
, unsigned bufsize
);
142 ac_build_phi(struct ac_llvm_context
*ctx
, LLVMTypeRef type
,
143 unsigned count_incoming
, LLVMValueRef
*values
,
144 LLVMBasicBlockRef
*blocks
);
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
);
150 LLVMValueRef
ac_build_shader_clock(struct ac_llvm_context
*ctx
);
152 LLVMValueRef
ac_build_ballot(struct ac_llvm_context
*ctx
, LLVMValueRef value
);
154 LLVMValueRef
ac_build_vote_all(struct ac_llvm_context
*ctx
, LLVMValueRef value
);
156 LLVMValueRef
ac_build_vote_any(struct ac_llvm_context
*ctx
, LLVMValueRef value
);
158 LLVMValueRef
ac_build_vote_eq(struct ac_llvm_context
*ctx
, LLVMValueRef value
);
161 ac_build_varying_gather_values(struct ac_llvm_context
*ctx
, LLVMValueRef
*values
,
162 unsigned value_count
, unsigned component
);
165 ac_build_gather_values_extended(struct ac_llvm_context
*ctx
,
166 LLVMValueRef
*values
,
167 unsigned value_count
,
168 unsigned value_stride
,
172 ac_build_gather_values(struct ac_llvm_context
*ctx
,
173 LLVMValueRef
*values
,
174 unsigned value_count
);
175 LLVMValueRef
ac_build_expand_to_vec4(struct ac_llvm_context
*ctx
,
177 unsigned num_channels
);
178 LLVMValueRef
ac_build_round(struct ac_llvm_context
*ctx
, LLVMValueRef value
);
181 ac_build_fdiv(struct ac_llvm_context
*ctx
,
185 LLVMValueRef
ac_build_fast_udiv(struct ac_llvm_context
*ctx
,
187 LLVMValueRef multiplier
,
188 LLVMValueRef pre_shift
,
189 LLVMValueRef post_shift
,
190 LLVMValueRef increment
);
191 LLVMValueRef
ac_build_fast_udiv_nuw(struct ac_llvm_context
*ctx
,
193 LLVMValueRef multiplier
,
194 LLVMValueRef pre_shift
,
195 LLVMValueRef post_shift
,
196 LLVMValueRef increment
);
197 LLVMValueRef
ac_build_fast_udiv_u31_d_not_one(struct ac_llvm_context
*ctx
,
199 LLVMValueRef multiplier
,
200 LLVMValueRef post_shift
);
203 ac_prepare_cube_coords(struct ac_llvm_context
*ctx
,
204 bool is_deriv
, bool is_array
, bool is_lod
,
205 LLVMValueRef
*coords_arg
,
206 LLVMValueRef
*derivs_arg
);
210 ac_build_fs_interp(struct ac_llvm_context
*ctx
,
211 LLVMValueRef llvm_chan
,
212 LLVMValueRef attr_number
,
218 ac_build_fs_interp_mov(struct ac_llvm_context
*ctx
,
219 LLVMValueRef parameter
,
220 LLVMValueRef llvm_chan
,
221 LLVMValueRef attr_number
,
222 LLVMValueRef params
);
225 ac_build_gep0(struct ac_llvm_context
*ctx
,
226 LLVMValueRef base_ptr
,
228 LLVMValueRef
ac_build_pointer_add(struct ac_llvm_context
*ctx
, LLVMValueRef ptr
,
232 ac_build_indexed_store(struct ac_llvm_context
*ctx
,
233 LLVMValueRef base_ptr
, LLVMValueRef index
,
236 LLVMValueRef
ac_build_load(struct ac_llvm_context
*ctx
, LLVMValueRef base_ptr
,
238 LLVMValueRef
ac_build_load_invariant(struct ac_llvm_context
*ctx
,
239 LLVMValueRef base_ptr
, LLVMValueRef index
);
240 LLVMValueRef
ac_build_load_to_sgpr(struct ac_llvm_context
*ctx
,
241 LLVMValueRef base_ptr
, LLVMValueRef index
);
242 LLVMValueRef
ac_build_load_to_sgpr_uint_wraparound(struct ac_llvm_context
*ctx
,
243 LLVMValueRef base_ptr
, LLVMValueRef index
);
246 ac_build_buffer_store_dword(struct ac_llvm_context
*ctx
,
249 unsigned num_channels
,
250 LLVMValueRef voffset
,
251 LLVMValueRef soffset
,
252 unsigned inst_offset
,
255 bool writeonly_memory
,
256 bool swizzle_enable_hint
);
258 ac_build_buffer_load(struct ac_llvm_context
*ctx
,
262 LLVMValueRef voffset
,
263 LLVMValueRef soffset
,
264 unsigned inst_offset
,
270 LLVMValueRef
ac_build_buffer_load_format(struct ac_llvm_context
*ctx
,
273 LLVMValueRef voffset
,
274 unsigned num_channels
,
278 /* load_format that handles the stride & element count better if idxen is
279 * disabled by LLVM. */
280 LLVMValueRef
ac_build_buffer_load_format_gfx9_safe(struct ac_llvm_context
*ctx
,
283 LLVMValueRef voffset
,
284 unsigned num_channels
,
289 ac_build_tbuffer_load_short(struct ac_llvm_context
*ctx
,
292 LLVMValueRef voffset
,
293 LLVMValueRef soffset
,
294 LLVMValueRef immoffset
,
298 ac_get_thread_id(struct ac_llvm_context
*ctx
);
300 #define AC_TID_MASK_TOP_LEFT 0xfffffffc
301 #define AC_TID_MASK_TOP 0xfffffffd
302 #define AC_TID_MASK_LEFT 0xfffffffe
305 ac_build_ddxy(struct ac_llvm_context
*ctx
,
310 #define AC_SENDMSG_GS 2
311 #define AC_SENDMSG_GS_DONE 3
313 #define AC_SENDMSG_GS_OP_NOP (0 << 4)
314 #define AC_SENDMSG_GS_OP_CUT (1 << 4)
315 #define AC_SENDMSG_GS_OP_EMIT (2 << 4)
316 #define AC_SENDMSG_GS_OP_EMIT_CUT (3 << 4)
318 void ac_build_sendmsg(struct ac_llvm_context
*ctx
,
320 LLVMValueRef wave_id
);
322 LLVMValueRef
ac_build_imsb(struct ac_llvm_context
*ctx
,
324 LLVMTypeRef dst_type
);
326 LLVMValueRef
ac_build_umsb(struct ac_llvm_context
*ctx
,
328 LLVMTypeRef dst_type
);
329 LLVMValueRef
ac_build_fmin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
331 LLVMValueRef
ac_build_fmax(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
333 LLVMValueRef
ac_build_imin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
335 LLVMValueRef
ac_build_imax(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
337 LLVMValueRef
ac_build_umin(struct ac_llvm_context
*ctx
, LLVMValueRef a
, LLVMValueRef b
);
338 LLVMValueRef
ac_build_clamp(struct ac_llvm_context
*ctx
, LLVMValueRef value
);
340 struct ac_export_args
{
343 unsigned enabled_channels
;
349 void ac_build_export(struct ac_llvm_context
*ctx
, struct ac_export_args
*a
);
351 void ac_build_export_null(struct ac_llvm_context
*ctx
);
353 enum ac_image_opcode
{
361 ac_image_get_resinfo
,
363 ac_image_atomic_cmpswap
,
383 ac_image_cube
, // includes cube arrays
387 ac_image_2darraymsaa
,
390 /* These cache policy bits match the definitions used by the LLVM intrinsics. */
391 enum ac_image_cache_policy
{
396 struct ac_image_args
{
397 enum ac_image_opcode opcode
: 4;
398 enum ac_atomic_op atomic
: 4; /* for the ac_image_atomic opcode */
399 enum ac_image_dim dim
: 3;
401 unsigned cache_policy
: 2;
404 unsigned attributes
; /* additional call-site specific AC_FUNC_ATTRs */
406 LLVMValueRef resource
;
407 LLVMValueRef sampler
;
408 LLVMValueRef data
[2]; /* data[0] is source data (vector); data[1] is cmp for cmpswap */
411 LLVMValueRef compare
;
412 LLVMValueRef derivs
[6];
413 LLVMValueRef coords
[4];
414 LLVMValueRef lod
; // also used by ac_image_get_resinfo
417 LLVMValueRef
ac_build_image_opcode(struct ac_llvm_context
*ctx
,
418 struct ac_image_args
*a
);
419 LLVMValueRef
ac_build_cvt_pkrtz_f16(struct ac_llvm_context
*ctx
,
420 LLVMValueRef args
[2]);
421 LLVMValueRef
ac_build_cvt_pknorm_i16(struct ac_llvm_context
*ctx
,
422 LLVMValueRef args
[2]);
423 LLVMValueRef
ac_build_cvt_pknorm_u16(struct ac_llvm_context
*ctx
,
424 LLVMValueRef args
[2]);
425 LLVMValueRef
ac_build_cvt_pk_i16(struct ac_llvm_context
*ctx
,
426 LLVMValueRef args
[2], unsigned bits
, bool hi
);
427 LLVMValueRef
ac_build_cvt_pk_u16(struct ac_llvm_context
*ctx
,
428 LLVMValueRef args
[2], unsigned bits
, bool hi
);
429 LLVMValueRef
ac_build_wqm_vote(struct ac_llvm_context
*ctx
, LLVMValueRef i1
);
430 void ac_build_kill_if_false(struct ac_llvm_context
*ctx
, LLVMValueRef i1
);
431 LLVMValueRef
ac_build_bfe(struct ac_llvm_context
*ctx
, LLVMValueRef input
,
432 LLVMValueRef offset
, LLVMValueRef width
,
434 LLVMValueRef
ac_build_imad(struct ac_llvm_context
*ctx
, LLVMValueRef s0
,
435 LLVMValueRef s1
, LLVMValueRef s2
);
436 LLVMValueRef
ac_build_fmad(struct ac_llvm_context
*ctx
, LLVMValueRef s0
,
437 LLVMValueRef s1
, LLVMValueRef s2
);
439 void ac_build_waitcnt(struct ac_llvm_context
*ctx
, unsigned simm16
);
441 LLVMValueRef
ac_build_fract(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
444 LLVMValueRef
ac_build_isign(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
447 LLVMValueRef
ac_build_fsign(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
450 LLVMValueRef
ac_build_bit_count(struct ac_llvm_context
*ctx
, LLVMValueRef src0
);
452 LLVMValueRef
ac_build_bitfield_reverse(struct ac_llvm_context
*ctx
,
455 void ac_optimize_vs_outputs(struct ac_llvm_context
*ac
,
456 LLVMValueRef main_fn
,
457 uint8_t *vs_output_param_offset
,
458 uint32_t num_outputs
,
459 uint8_t *num_param_exports
);
460 void ac_init_exec_full_mask(struct ac_llvm_context
*ctx
);
462 void ac_declare_lds_as_pointer(struct ac_llvm_context
*ac
);
463 LLVMValueRef
ac_lds_load(struct ac_llvm_context
*ctx
,
464 LLVMValueRef dw_addr
);
465 void ac_lds_store(struct ac_llvm_context
*ctx
,
466 LLVMValueRef dw_addr
, LLVMValueRef value
);
468 LLVMValueRef
ac_find_lsb(struct ac_llvm_context
*ctx
,
469 LLVMTypeRef dst_type
,
472 LLVMTypeRef
ac_array_in_const_addr_space(LLVMTypeRef elem_type
);
473 LLVMTypeRef
ac_array_in_const32_addr_space(LLVMTypeRef elem_type
);
475 void ac_build_bgnloop(struct ac_llvm_context
*ctx
, int lable_id
);
476 void ac_build_break(struct ac_llvm_context
*ctx
);
477 void ac_build_continue(struct ac_llvm_context
*ctx
);
478 void ac_build_else(struct ac_llvm_context
*ctx
, int lable_id
);
479 void ac_build_endif(struct ac_llvm_context
*ctx
, int lable_id
);
480 void ac_build_endloop(struct ac_llvm_context
*ctx
, int lable_id
);
481 void ac_build_if(struct ac_llvm_context
*ctx
, LLVMValueRef value
,
483 void ac_build_uif(struct ac_llvm_context
*ctx
, LLVMValueRef value
,
486 LLVMValueRef
ac_build_alloca(struct ac_llvm_context
*ac
, LLVMTypeRef type
,
488 LLVMValueRef
ac_build_alloca_undef(struct ac_llvm_context
*ac
, LLVMTypeRef type
,
491 LLVMValueRef
ac_cast_ptr(struct ac_llvm_context
*ctx
, LLVMValueRef ptr
,
494 LLVMValueRef
ac_trim_vector(struct ac_llvm_context
*ctx
, LLVMValueRef value
,
497 LLVMValueRef
ac_unpack_param(struct ac_llvm_context
*ctx
, LLVMValueRef param
,
498 unsigned rshift
, unsigned bitwidth
);
500 void ac_apply_fmask_to_sample(struct ac_llvm_context
*ac
, LLVMValueRef fmask
,
501 LLVMValueRef
*addr
, bool is_array_tex
);
504 ac_build_ds_swizzle(struct ac_llvm_context
*ctx
, LLVMValueRef src
, unsigned mask
);
507 ac_build_readlane(struct ac_llvm_context
*ctx
, LLVMValueRef src
, LLVMValueRef lane
);
510 ac_build_writelane(struct ac_llvm_context
*ctx
, LLVMValueRef src
, LLVMValueRef value
, LLVMValueRef lane
);
513 ac_build_mbcnt(struct ac_llvm_context
*ctx
, LLVMValueRef mask
);
516 ac_build_inclusive_scan(struct ac_llvm_context
*ctx
, LLVMValueRef src
, nir_op op
);
519 ac_build_exclusive_scan(struct ac_llvm_context
*ctx
, LLVMValueRef src
, nir_op op
);
522 ac_build_reduce(struct ac_llvm_context
*ctx
, LLVMValueRef src
, nir_op op
, unsigned cluster_size
);
525 ac_build_quad_swizzle(struct ac_llvm_context
*ctx
, LLVMValueRef src
,
526 unsigned lane0
, unsigned lane1
, unsigned lane2
, unsigned lane3
);
529 ac_build_shuffle(struct ac_llvm_context
*ctx
, LLVMValueRef src
, LLVMValueRef index
);