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 /* 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 */
48 struct ac_llvm_context
{
49 LLVMContextRef context
;
51 LLVMBuilderRef builder
;
82 struct ac_llvm_flow
*flow
;
84 unsigned flow_depth_max
;
86 unsigned range_md_kind
;
87 unsigned invariant_load_md_kind
;
88 unsigned uniform_md_kind
;
89 unsigned fpmath_md_kind
;
90 LLVMValueRef fpmath_md_2p5_ulp
;
91 LLVMValueRef empty_md
;
93 enum chip_class chip_class
;
94 enum radeon_family family
;
100 ac_llvm_context_init(struct ac_llvm_context
*ctx
, LLVMContextRef context
,
101 enum chip_class chip_class
, enum radeon_family family
);
104 ac_llvm_context_dispose(struct ac_llvm_context
*ctx
);
107 ac_get_llvm_num_components(LLVMValueRef value
);
110 ac_get_elem_bits(struct ac_llvm_context
*ctx
, LLVMTypeRef type
);
113 ac_llvm_extract_elem(struct ac_llvm_context
*ac
,
117 unsigned ac_get_type_size(LLVMTypeRef type
);
119 LLVMTypeRef
ac_to_integer_type(struct ac_llvm_context
*ctx
, LLVMTypeRef t
);
120 LLVMValueRef
ac_to_integer(struct ac_llvm_context
*ctx
, LLVMValueRef v
);
121 LLVMTypeRef
ac_to_float_type(struct ac_llvm_context
*ctx
, LLVMTypeRef t
);
122 LLVMValueRef
ac_to_float(struct ac_llvm_context
*ctx
, LLVMValueRef v
);
125 ac_build_intrinsic(struct ac_llvm_context
*ctx
, const char *name
,
126 LLVMTypeRef return_type
, LLVMValueRef
*params
,
127 unsigned param_count
, unsigned attrib_mask
);
129 void ac_build_type_name_for_intr(LLVMTypeRef type
, char *buf
, unsigned bufsize
);
132 ac_build_phi(struct ac_llvm_context
*ctx
, LLVMTypeRef type
,
133 unsigned count_incoming
, LLVMValueRef
*values
,
134 LLVMBasicBlockRef
*blocks
);
136 void ac_build_optimization_barrier(struct ac_llvm_context
*ctx
,
137 LLVMValueRef
*pvgpr
);
139 LLVMValueRef
ac_build_shader_clock(struct ac_llvm_context
*ctx
);
141 LLVMValueRef
ac_build_ballot(struct ac_llvm_context
*ctx
, LLVMValueRef value
);
143 LLVMValueRef
ac_build_vote_all(struct ac_llvm_context
*ctx
, LLVMValueRef value
);
145 LLVMValueRef
ac_build_vote_any(struct ac_llvm_context
*ctx
, LLVMValueRef value
);
147 LLVMValueRef
ac_build_vote_eq(struct ac_llvm_context
*ctx
, LLVMValueRef value
);
150 ac_build_varying_gather_values(struct ac_llvm_context
*ctx
, LLVMValueRef
*values
,
151 unsigned value_count
, unsigned component
);
154 ac_build_gather_values_extended(struct ac_llvm_context
*ctx
,
155 LLVMValueRef
*values
,
156 unsigned value_count
,
157 unsigned value_stride
,
161 ac_build_gather_values(struct ac_llvm_context
*ctx
,
162 LLVMValueRef
*values
,
163 unsigned value_count
);
164 LLVMValueRef
ac_build_expand_to_vec4(struct ac_llvm_context
*ctx
,
166 unsigned num_channels
);
169 ac_build_fdiv(struct ac_llvm_context
*ctx
,
174 ac_prepare_cube_coords(struct ac_llvm_context
*ctx
,
175 bool is_deriv
, bool is_array
, bool is_lod
,
176 LLVMValueRef
*coords_arg
,
177 LLVMValueRef
*derivs_arg
);
181 ac_build_fs_interp(struct ac_llvm_context
*ctx
,
182 LLVMValueRef llvm_chan
,
183 LLVMValueRef attr_number
,
189 ac_build_fs_interp_mov(struct ac_llvm_context
*ctx
,
190 LLVMValueRef parameter
,
191 LLVMValueRef llvm_chan
,
192 LLVMValueRef attr_number
,
193 LLVMValueRef params
);
196 ac_build_gep0(struct ac_llvm_context
*ctx
,
197 LLVMValueRef base_ptr
,
201 ac_build_indexed_store(struct ac_llvm_context
*ctx
,
202 LLVMValueRef base_ptr
, LLVMValueRef index
,
205 LLVMValueRef
ac_build_load(struct ac_llvm_context
*ctx
, LLVMValueRef base_ptr
,
207 LLVMValueRef
ac_build_load_invariant(struct ac_llvm_context
*ctx
,
208 LLVMValueRef base_ptr
, LLVMValueRef index
);
209 LLVMValueRef
ac_build_load_to_sgpr(struct ac_llvm_context
*ctx
,
210 LLVMValueRef base_ptr
, LLVMValueRef index
);
213 ac_build_buffer_store_dword(struct ac_llvm_context
*ctx
,
216 unsigned num_channels
,
217 LLVMValueRef voffset
,
218 LLVMValueRef soffset
,
219 unsigned inst_offset
,
222 bool writeonly_memory
,
223 bool swizzle_enable_hint
);
225 ac_build_buffer_load(struct ac_llvm_context
*ctx
,
229 LLVMValueRef voffset
,
230 LLVMValueRef soffset
,
231 unsigned inst_offset
,
237 LLVMValueRef
ac_build_buffer_load_format(struct ac_llvm_context
*ctx
,
240 LLVMValueRef voffset
,
241 unsigned num_channels
,
245 /* load_format that handles the stride & element count better if idxen is
246 * disabled by LLVM. */
247 LLVMValueRef
ac_build_buffer_load_format_gfx9_safe(struct ac_llvm_context
*ctx
,
250 LLVMValueRef voffset
,
251 unsigned num_channels
,
256 ac_get_thread_id(struct ac_llvm_context
*ctx
);
258 #define AC_TID_MASK_TOP_LEFT 0xfffffffc
259 #define AC_TID_MASK_TOP 0xfffffffd
260 #define AC_TID_MASK_LEFT 0xfffffffe
263 ac_build_ddxy(struct ac_llvm_context
*ctx
,
268 #define AC_SENDMSG_GS 2
269 #define AC_SENDMSG_GS_DONE 3
271 #define AC_SENDMSG_GS_OP_NOP (0 << 4)
272 #define AC_SENDMSG_GS_OP_CUT (1 << 4)
273 #define AC_SENDMSG_GS_OP_EMIT (2 << 4)
274 #define AC_SENDMSG_GS_OP_EMIT_CUT (3 << 4)
276 void ac_build_sendmsg(struct ac_llvm_context
*ctx
,
278 LLVMValueRef wave_id
);
280 LLVMValueRef
ac_build_imsb(struct ac_llvm_context
*ctx
,
282 LLVMTypeRef dst_type
);
284 LLVMValueRef
ac_build_umsb(struct ac_llvm_context
*ctx
,
286 LLVMTypeRef dst_type
);
287 LLVMValueRef
ac_build_fmin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
289 LLVMValueRef
ac_build_fmax(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
291 LLVMValueRef
ac_build_imin(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
293 LLVMValueRef
ac_build_imax(struct ac_llvm_context
*ctx
, LLVMValueRef a
,
295 LLVMValueRef
ac_build_umin(struct ac_llvm_context
*ctx
, LLVMValueRef a
, LLVMValueRef b
);
296 LLVMValueRef
ac_build_clamp(struct ac_llvm_context
*ctx
, LLVMValueRef value
);
298 struct ac_export_args
{
301 unsigned enabled_channels
;
307 void ac_build_export(struct ac_llvm_context
*ctx
, struct ac_export_args
*a
);
309 void ac_build_export_null(struct ac_llvm_context
*ctx
);
311 enum ac_image_opcode
{
317 ac_image_get_resinfo
,
320 struct ac_image_args
{
321 enum ac_image_opcode opcode
;
329 LLVMValueRef resource
;
330 LLVMValueRef sampler
;
337 LLVMValueRef
ac_build_image_opcode(struct ac_llvm_context
*ctx
,
338 struct ac_image_args
*a
);
339 LLVMValueRef
ac_build_cvt_pkrtz_f16(struct ac_llvm_context
*ctx
,
340 LLVMValueRef args
[2]);
341 LLVMValueRef
ac_build_cvt_pknorm_i16(struct ac_llvm_context
*ctx
,
342 LLVMValueRef args
[2]);
343 LLVMValueRef
ac_build_cvt_pknorm_u16(struct ac_llvm_context
*ctx
,
344 LLVMValueRef args
[2]);
345 LLVMValueRef
ac_build_cvt_pk_i16(struct ac_llvm_context
*ctx
,
346 LLVMValueRef args
[2], unsigned bits
, bool hi
);
347 LLVMValueRef
ac_build_cvt_pk_u16(struct ac_llvm_context
*ctx
,
348 LLVMValueRef args
[2], unsigned bits
, bool hi
);
349 LLVMValueRef
ac_build_wqm_vote(struct ac_llvm_context
*ctx
, LLVMValueRef i1
);
350 void ac_build_kill_if_false(struct ac_llvm_context
*ctx
, LLVMValueRef i1
);
351 LLVMValueRef
ac_build_bfe(struct ac_llvm_context
*ctx
, LLVMValueRef input
,
352 LLVMValueRef offset
, LLVMValueRef width
,
355 void ac_build_waitcnt(struct ac_llvm_context
*ctx
, unsigned simm16
);
357 LLVMValueRef
ac_build_fract(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
360 LLVMValueRef
ac_build_isign(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
363 LLVMValueRef
ac_build_fsign(struct ac_llvm_context
*ctx
, LLVMValueRef src0
,
366 void ac_get_image_intr_name(const char *base_name
,
367 LLVMTypeRef data_type
,
368 LLVMTypeRef coords_type
,
369 LLVMTypeRef rsrc_type
,
370 char *out_name
, unsigned out_len
);
372 void ac_optimize_vs_outputs(struct ac_llvm_context
*ac
,
373 LLVMValueRef main_fn
,
374 uint8_t *vs_output_param_offset
,
375 uint32_t num_outputs
,
376 uint8_t *num_param_exports
);
377 void ac_init_exec_full_mask(struct ac_llvm_context
*ctx
);
379 void ac_declare_lds_as_pointer(struct ac_llvm_context
*ac
);
380 LLVMValueRef
ac_lds_load(struct ac_llvm_context
*ctx
,
381 LLVMValueRef dw_addr
);
382 void ac_lds_store(struct ac_llvm_context
*ctx
,
383 LLVMValueRef dw_addr
, LLVMValueRef value
);
385 LLVMValueRef
ac_find_lsb(struct ac_llvm_context
*ctx
,
386 LLVMTypeRef dst_type
,
389 LLVMTypeRef
ac_array_in_const_addr_space(LLVMTypeRef elem_type
);
390 LLVMTypeRef
ac_array_in_const32_addr_space(LLVMTypeRef elem_type
);
392 void ac_build_bgnloop(struct ac_llvm_context
*ctx
, int lable_id
);
393 void ac_build_break(struct ac_llvm_context
*ctx
);
394 void ac_build_continue(struct ac_llvm_context
*ctx
);
395 void ac_build_else(struct ac_llvm_context
*ctx
, int lable_id
);
396 void ac_build_endif(struct ac_llvm_context
*ctx
, int lable_id
);
397 void ac_build_endloop(struct ac_llvm_context
*ctx
, int lable_id
);
398 void ac_build_if(struct ac_llvm_context
*ctx
, LLVMValueRef value
,
400 void ac_build_uif(struct ac_llvm_context
*ctx
, LLVMValueRef value
,
403 LLVMValueRef
ac_build_alloca(struct ac_llvm_context
*ac
, LLVMTypeRef type
,
405 LLVMValueRef
ac_build_alloca_undef(struct ac_llvm_context
*ac
, LLVMTypeRef type
,
408 LLVMValueRef
ac_cast_ptr(struct ac_llvm_context
*ctx
, LLVMValueRef ptr
,
411 LLVMValueRef
ac_trim_vector(struct ac_llvm_context
*ctx
, LLVMValueRef value
,
414 LLVMValueRef
ac_unpack_param(struct ac_llvm_context
*ctx
, LLVMValueRef param
,
415 unsigned rshift
, unsigned bitwidth
);
417 void ac_apply_fmask_to_sample(struct ac_llvm_context
*ac
, LLVMValueRef fmask
,
418 LLVMValueRef
*addr
, bool is_array_tex
);
421 ac_build_ds_swizzle(struct ac_llvm_context
*ctx
, LLVMValueRef src
, unsigned mask
);
424 ac_build_readlane(struct ac_llvm_context
*ctx
, LLVMValueRef src
, LLVMValueRef lane
);
427 ac_build_writelane(struct ac_llvm_context
*ctx
, LLVMValueRef src
, LLVMValueRef value
, LLVMValueRef lane
);
430 ac_build_mbcnt(struct ac_llvm_context
*ctx
, LLVMValueRef mask
);
433 ac_build_inclusive_scan(struct ac_llvm_context
*ctx
, LLVMValueRef src
, nir_op op
);
436 ac_build_exclusive_scan(struct ac_llvm_context
*ctx
, LLVMValueRef src
, nir_op op
);
439 ac_build_reduce(struct ac_llvm_context
*ctx
, LLVMValueRef src
, nir_op op
, unsigned cluster_size
);
442 ac_build_quad_swizzle(struct ac_llvm_context
*ctx
, LLVMValueRef src
,
443 unsigned lane0
, unsigned lane1
, unsigned lane2
, unsigned lane3
);
446 ac_build_shuffle(struct ac_llvm_context
*ctx
, LLVMValueRef src
, LLVMValueRef index
);