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 "ac_llvm_util.h"
29#include "ac_shader_abi.h"
30#include "ac_shader_args.h"
31#include "ac_shader_util.h"
32#include "amd_family.h"
33#include "compiler/nir/nir.h"
34#include <llvm-c/Core.h>
35
36#include <stdbool.h>
37
38#ifdef __cplusplus
39extern "C" {
40#endif
41
42enum
43{
44   AC_ADDR_SPACE_FLAT = 0, /* Slower than global. */
45   AC_ADDR_SPACE_GLOBAL = 1,
46   AC_ADDR_SPACE_GDS = 2,
47   AC_ADDR_SPACE_LDS = 3,
48   AC_ADDR_SPACE_CONST = 4,       /* Global allowing SMEM. */
49   AC_ADDR_SPACE_CONST_32BIT = 6, /* same as CONST, but the pointer type has 32 bits */
50};
51
52#define AC_WAIT_LGKM   (1 << 0) /* LDS, GDS, constant, message */
53#define AC_WAIT_VLOAD  (1 << 1) /* VMEM load/sample instructions */
54#define AC_WAIT_VSTORE (1 << 2) /* VMEM store instructions */
55#define AC_WAIT_EXP    (1 << 3) /* EXP instructions */
56
57struct ac_llvm_flow;
58struct ac_llvm_compiler;
59
60struct ac_llvm_flow_state {
61   struct ac_llvm_flow *stack;
62   unsigned depth_max;
63   unsigned depth;
64};
65
66struct ac_llvm_context {
67   LLVMContextRef context;
68   LLVMModuleRef module;
69   LLVMBuilderRef builder;
70
71   LLVMValueRef main_function;
72
73   LLVMTypeRef voidt;
74   LLVMTypeRef i1;
75   LLVMTypeRef i8;
76   LLVMTypeRef i16;
77   LLVMTypeRef i32;
78   LLVMTypeRef i64;
79   LLVMTypeRef i128;
80   LLVMTypeRef intptr;
81   LLVMTypeRef f16;
82   LLVMTypeRef f32;
83   LLVMTypeRef f64;
84   LLVMTypeRef v2i16;
85   LLVMTypeRef v4i16;
86   LLVMTypeRef v2f16;
87   LLVMTypeRef v4f16;
88   LLVMTypeRef v2i32;
89   LLVMTypeRef v3i32;
90   LLVMTypeRef v4i32;
91   LLVMTypeRef v2f32;
92   LLVMTypeRef v3f32;
93   LLVMTypeRef v4f32;
94   LLVMTypeRef v8i32;
95   LLVMTypeRef iN_wavemask;
96   LLVMTypeRef iN_ballotmask;
97
98   LLVMValueRef i8_0;
99   LLVMValueRef i8_1;
100   LLVMValueRef i16_0;
101   LLVMValueRef i16_1;
102   LLVMValueRef i32_0;
103   LLVMValueRef i32_1;
104   LLVMValueRef i64_0;
105   LLVMValueRef i64_1;
106   LLVMValueRef i128_0;
107   LLVMValueRef i128_1;
108   LLVMValueRef f16_0;
109   LLVMValueRef f16_1;
110   LLVMValueRef f32_0;
111   LLVMValueRef f32_1;
112   LLVMValueRef f64_0;
113   LLVMValueRef f64_1;
114   LLVMValueRef i1true;
115   LLVMValueRef i1false;
116
117   /* Temporary helper to implement demote_to_helper:
118    * True = live lanes
119    * False = demoted lanes
120    */
121   LLVMValueRef postponed_kill;
122   bool conditional_demote_seen;
123
124   /* Since ac_nir_translate makes a local copy of ac_llvm_context, there
125    * are two ac_llvm_contexts. Declare a pointer here, so that the control
126    * flow stack is shared by both ac_llvm_contexts.
127    */
128   struct ac_llvm_flow_state *flow;
129
130   unsigned range_md_kind;
131   unsigned invariant_load_md_kind;
132   unsigned uniform_md_kind;
133   LLVMValueRef empty_md;
134
135   enum amd_gfx_level gfx_level;
136   enum radeon_family family;
137   bool has_3d_cube_border_color_mipmap;
138
139   unsigned wave_size;
140   unsigned ballot_mask_bits;
141
142   unsigned float_mode;
143
144   LLVMValueRef lds;
145};
146
147void ac_llvm_context_init(struct ac_llvm_context *ctx, struct ac_llvm_compiler *compiler,
148                          enum amd_gfx_level gfx_level, enum radeon_family family,
149                          bool has_3d_cube_border_color_mipmap,
150                          enum ac_float_mode float_mode, unsigned wave_size,
151                          unsigned ballot_mask_bits);
152
153void ac_llvm_context_dispose(struct ac_llvm_context *ctx);
154
155int ac_get_llvm_num_components(LLVMValueRef value);
156
157int ac_get_elem_bits(struct ac_llvm_context *ctx, LLVMTypeRef type);
158
159LLVMValueRef ac_llvm_extract_elem(struct ac_llvm_context *ac, LLVMValueRef value, int index);
160
161unsigned ac_get_type_size(LLVMTypeRef type);
162
163LLVMTypeRef ac_to_integer_type(struct ac_llvm_context *ctx, LLVMTypeRef t);
164LLVMValueRef ac_to_integer(struct ac_llvm_context *ctx, LLVMValueRef v);
165LLVMValueRef ac_to_integer_or_pointer(struct ac_llvm_context *ctx, LLVMValueRef v);
166LLVMTypeRef ac_to_float_type(struct ac_llvm_context *ctx, LLVMTypeRef t);
167LLVMValueRef ac_to_float(struct ac_llvm_context *ctx, LLVMValueRef v);
168
169LLVMValueRef ac_build_intrinsic(struct ac_llvm_context *ctx, const char *name,
170                                LLVMTypeRef return_type, LLVMValueRef *params, unsigned param_count,
171                                unsigned attrib_mask);
172
173void ac_build_type_name_for_intr(LLVMTypeRef type, char *buf, unsigned bufsize);
174
175LLVMValueRef ac_build_phi(struct ac_llvm_context *ctx, LLVMTypeRef type, unsigned count_incoming,
176                          LLVMValueRef *values, LLVMBasicBlockRef *blocks);
177
178void ac_build_s_barrier(struct ac_llvm_context *ctx, gl_shader_stage stage);
179void ac_build_optimization_barrier(struct ac_llvm_context *ctx, LLVMValueRef *pgpr, bool sgpr);
180
181LLVMValueRef ac_build_shader_clock(struct ac_llvm_context *ctx, nir_scope scope);
182
183LLVMValueRef ac_build_ballot(struct ac_llvm_context *ctx, LLVMValueRef value);
184LLVMValueRef ac_get_i1_sgpr_mask(struct ac_llvm_context *ctx, LLVMValueRef value);
185
186LLVMValueRef ac_build_vote_all(struct ac_llvm_context *ctx, LLVMValueRef value);
187
188LLVMValueRef ac_build_vote_any(struct ac_llvm_context *ctx, LLVMValueRef value);
189
190LLVMValueRef ac_build_vote_eq(struct ac_llvm_context *ctx, LLVMValueRef value);
191
192LLVMValueRef ac_build_varying_gather_values(struct ac_llvm_context *ctx, LLVMValueRef *values,
193                                            unsigned value_count, unsigned component);
194
195LLVMValueRef ac_build_gather_values_extended(struct ac_llvm_context *ctx, LLVMValueRef *values,
196                                             unsigned value_count, unsigned value_stride,
197                                             bool always_vector);
198LLVMValueRef ac_build_gather_values(struct ac_llvm_context *ctx, LLVMValueRef *values,
199                                    unsigned value_count);
200
201LLVMValueRef ac_build_concat(struct ac_llvm_context *ctx, LLVMValueRef a, LLVMValueRef b);
202
203LLVMValueRef ac_extract_components(struct ac_llvm_context *ctx, LLVMValueRef value, unsigned start,
204                                   unsigned channels);
205
206LLVMValueRef ac_build_expand(struct ac_llvm_context *ctx, LLVMValueRef value,
207                             unsigned src_channels, unsigned dst_channels);
208
209LLVMValueRef ac_build_expand_to_vec4(struct ac_llvm_context *ctx, LLVMValueRef value,
210                                     unsigned num_channels);
211LLVMValueRef ac_build_round(struct ac_llvm_context *ctx, LLVMValueRef value);
212
213LLVMValueRef ac_build_fdiv(struct ac_llvm_context *ctx, LLVMValueRef num, LLVMValueRef den);
214
215LLVMValueRef ac_build_fast_udiv(struct ac_llvm_context *ctx, LLVMValueRef num,
216                                LLVMValueRef multiplier, LLVMValueRef pre_shift,
217                                LLVMValueRef post_shift, LLVMValueRef increment);
218LLVMValueRef ac_build_fast_udiv_nuw(struct ac_llvm_context *ctx, LLVMValueRef num,
219                                    LLVMValueRef multiplier, LLVMValueRef pre_shift,
220                                    LLVMValueRef post_shift, LLVMValueRef increment);
221LLVMValueRef ac_build_fast_udiv_u31_d_not_one(struct ac_llvm_context *ctx, LLVMValueRef num,
222                                              LLVMValueRef multiplier, LLVMValueRef post_shift);
223
224void ac_prepare_cube_coords(struct ac_llvm_context *ctx, bool is_deriv, bool is_array, bool is_lod,
225                            LLVMValueRef *coords_arg, LLVMValueRef *derivs_arg);
226
227LLVMValueRef ac_build_fs_interp(struct ac_llvm_context *ctx, LLVMValueRef llvm_chan,
228                                LLVMValueRef attr_number, LLVMValueRef params, LLVMValueRef i,
229                                LLVMValueRef j);
230
231LLVMValueRef ac_build_fs_interp_f16(struct ac_llvm_context *ctx, LLVMValueRef llvm_chan,
232                                    LLVMValueRef attr_number, LLVMValueRef params, LLVMValueRef i,
233                                    LLVMValueRef j, bool high_16bits);
234
235LLVMValueRef ac_build_fs_interp_mov(struct ac_llvm_context *ctx, LLVMValueRef parameter,
236                                    LLVMValueRef llvm_chan, LLVMValueRef attr_number,
237                                    LLVMValueRef params);
238
239LLVMValueRef ac_build_gep_ptr(struct ac_llvm_context *ctx, LLVMValueRef base_ptr,
240                              LLVMValueRef index);
241
242LLVMValueRef ac_build_gep0(struct ac_llvm_context *ctx, LLVMValueRef base_ptr, LLVMValueRef index);
243LLVMValueRef ac_build_pointer_add(struct ac_llvm_context *ctx, LLVMValueRef ptr,
244                                  LLVMValueRef index);
245
246void ac_build_indexed_store(struct ac_llvm_context *ctx, LLVMValueRef base_ptr, LLVMValueRef index,
247                            LLVMValueRef value);
248
249LLVMValueRef ac_build_load(struct ac_llvm_context *ctx, LLVMValueRef base_ptr, LLVMValueRef index);
250LLVMValueRef ac_build_load_invariant(struct ac_llvm_context *ctx, LLVMValueRef base_ptr,
251                                     LLVMValueRef index);
252LLVMValueRef ac_build_load_to_sgpr(struct ac_llvm_context *ctx, LLVMValueRef base_ptr,
253                                   LLVMValueRef index);
254LLVMValueRef ac_build_load_to_sgpr_uint_wraparound(struct ac_llvm_context *ctx,
255                                                   LLVMValueRef base_ptr, LLVMValueRef index);
256
257void ac_build_buffer_store_dword(struct ac_llvm_context *ctx, LLVMValueRef rsrc, LLVMValueRef vdata,
258                                 LLVMValueRef vindex, LLVMValueRef voffset, LLVMValueRef soffset,
259                                 unsigned cache_policy);
260
261void ac_build_buffer_store_format(struct ac_llvm_context *ctx, LLVMValueRef rsrc, LLVMValueRef data,
262                                  LLVMValueRef vindex, LLVMValueRef voffset, unsigned cache_policy);
263
264LLVMValueRef ac_build_buffer_load(struct ac_llvm_context *ctx, LLVMValueRef rsrc, int num_channels,
265                                  LLVMValueRef vindex, LLVMValueRef voffset, LLVMValueRef soffset,
266                                  LLVMTypeRef channel_type, unsigned cache_policy,
267                                  bool can_speculate, bool allow_smem);
268
269LLVMValueRef ac_build_buffer_load_format(struct ac_llvm_context *ctx, LLVMValueRef rsrc,
270                                         LLVMValueRef vindex, LLVMValueRef voffset,
271                                         unsigned num_channels, unsigned cache_policy,
272                                         bool can_speculate, bool d16, bool tfe);
273
274LLVMValueRef ac_build_buffer_load_short(struct ac_llvm_context *ctx, LLVMValueRef rsrc,
275                                        LLVMValueRef voffset, LLVMValueRef soffset,
276                                        unsigned cache_policy);
277
278LLVMValueRef ac_build_buffer_load_byte(struct ac_llvm_context *ctx, LLVMValueRef rsrc,
279                                       LLVMValueRef voffset, LLVMValueRef soffset,
280                                       unsigned cache_policy);
281
282LLVMValueRef ac_build_struct_tbuffer_load(struct ac_llvm_context *ctx, LLVMValueRef rsrc,
283                                          LLVMValueRef vindex, LLVMValueRef voffset,
284                                          LLVMValueRef soffset, unsigned num_channels,
285                                          unsigned dfmt, unsigned nfmt, unsigned cache_policy,
286                                          bool can_speculate);
287
288LLVMValueRef ac_build_opencoded_load_format(struct ac_llvm_context *ctx, unsigned log_size,
289                                            unsigned num_channels, unsigned format, bool reverse,
290                                            bool known_aligned, LLVMValueRef rsrc,
291                                            LLVMValueRef vindex, LLVMValueRef voffset,
292                                            LLVMValueRef soffset, unsigned cache_policy,
293                                            bool can_speculate);
294
295void ac_build_buffer_store_short(struct ac_llvm_context *ctx, LLVMValueRef rsrc,
296                                 LLVMValueRef vdata, LLVMValueRef voffset, LLVMValueRef soffset,
297                                 unsigned cache_policy);
298
299void ac_build_buffer_store_byte(struct ac_llvm_context *ctx, LLVMValueRef rsrc, LLVMValueRef vdata,
300                                LLVMValueRef voffset, LLVMValueRef soffset, unsigned cache_policy);
301
302void ac_set_range_metadata(struct ac_llvm_context *ctx, LLVMValueRef value, unsigned lo,
303                           unsigned hi);
304LLVMValueRef ac_get_thread_id(struct ac_llvm_context *ctx);
305
306#define AC_TID_MASK_TOP_LEFT 0xfffffffc
307#define AC_TID_MASK_TOP      0xfffffffd
308#define AC_TID_MASK_LEFT     0xfffffffe
309
310LLVMValueRef ac_build_ddxy(struct ac_llvm_context *ctx, uint32_t mask, int idx, LLVMValueRef val);
311
312#define AC_SENDMSG_GS           2
313#define AC_SENDMSG_GS_DONE      3
314#define AC_SENDMSG_GS_ALLOC_REQ 9
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
321void ac_build_sendmsg(struct ac_llvm_context *ctx, uint32_t msg, LLVMValueRef wave_id);
322
323LLVMValueRef ac_build_imsb(struct ac_llvm_context *ctx, LLVMValueRef arg, LLVMTypeRef dst_type);
324
325LLVMValueRef ac_build_umsb(struct ac_llvm_context *ctx, LLVMValueRef arg, LLVMTypeRef dst_type);
326LLVMValueRef ac_build_fmin(struct ac_llvm_context *ctx, LLVMValueRef a, LLVMValueRef b);
327LLVMValueRef ac_build_fmax(struct ac_llvm_context *ctx, LLVMValueRef a, LLVMValueRef b);
328LLVMValueRef ac_build_imin(struct ac_llvm_context *ctx, LLVMValueRef a, LLVMValueRef b);
329LLVMValueRef ac_build_imax(struct ac_llvm_context *ctx, LLVMValueRef a, LLVMValueRef b);
330LLVMValueRef ac_build_umin(struct ac_llvm_context *ctx, LLVMValueRef a, LLVMValueRef b);
331LLVMValueRef ac_build_umax(struct ac_llvm_context *ctx, LLVMValueRef a, LLVMValueRef b);
332LLVMValueRef ac_build_clamp(struct ac_llvm_context *ctx, LLVMValueRef value);
333
334struct ac_export_args {
335   LLVMValueRef out[4];
336   unsigned target;
337   unsigned enabled_channels;
338   bool compr;
339   bool done;
340   bool valid_mask;
341};
342
343void ac_build_export(struct ac_llvm_context *ctx, struct ac_export_args *a);
344
345void ac_build_export_null(struct ac_llvm_context *ctx, bool uses_discard);
346
347enum ac_image_opcode
348{
349   ac_image_sample,
350   ac_image_gather4,
351   ac_image_load,
352   ac_image_load_mip,
353   ac_image_store,
354   ac_image_store_mip,
355   ac_image_get_lod,
356   ac_image_get_resinfo,
357   ac_image_atomic,
358   ac_image_atomic_cmpswap,
359};
360
361enum ac_atomic_op
362{
363   ac_atomic_swap,
364   ac_atomic_add,
365   ac_atomic_sub,
366   ac_atomic_smin,
367   ac_atomic_umin,
368   ac_atomic_smax,
369   ac_atomic_umax,
370   ac_atomic_and,
371   ac_atomic_or,
372   ac_atomic_xor,
373   ac_atomic_inc_wrap,
374   ac_atomic_dec_wrap,
375   ac_atomic_fmin,
376   ac_atomic_fmax,
377};
378
379/* These cache policy bits match the definitions used by the LLVM intrinsics. */
380enum ac_image_cache_policy
381{
382   ac_glc = 1 << 0,      /* per-CU cache control */
383   ac_slc = 1 << 1,      /* global L2 cache control */
384   ac_dlc = 1 << 2,      /* per-shader-array cache control */
385   ac_swizzled = 1 << 3, /* the access is swizzled, disabling load/store merging */
386};
387
388struct ac_image_args {
389   enum ac_image_opcode opcode;
390   enum ac_atomic_op atomic; /* for the ac_image_atomic opcode */
391   enum ac_image_dim dim;
392   unsigned dmask : 4;
393   unsigned cache_policy : 3;
394   bool unorm : 1;
395   bool level_zero : 1;
396   bool d16 : 1;        /* GFX8+: data and return values are 16-bit */
397   bool a16 : 1;        /* GFX9+: address components except compare, offset and bias are 16-bit */
398   bool g16 : 1;        /* GFX10+: derivatives are 16-bit; GFX<=9: must be equal to a16 */
399   bool tfe : 1;
400   unsigned attributes; /* additional call-site specific AC_FUNC_ATTRs */
401
402   LLVMValueRef resource;
403   LLVMValueRef sampler;
404   LLVMValueRef data[2]; /* data[0] is source data (vector); data[1] is cmp for cmpswap */
405   LLVMValueRef offset;
406   LLVMValueRef bias;
407   LLVMValueRef compare;
408   LLVMValueRef derivs[6];
409   LLVMValueRef coords[4];
410   LLVMValueRef lod; // also used by ac_image_get_resinfo
411   LLVMValueRef min_lod;
412};
413
414LLVMValueRef ac_build_image_opcode(struct ac_llvm_context *ctx, struct ac_image_args *a);
415LLVMValueRef ac_build_image_get_sample_count(struct ac_llvm_context *ctx, LLVMValueRef rsrc);
416LLVMValueRef ac_build_cvt_pkrtz_f16(struct ac_llvm_context *ctx, LLVMValueRef args[2]);
417LLVMValueRef ac_build_cvt_pknorm_i16(struct ac_llvm_context *ctx, LLVMValueRef args[2]);
418LLVMValueRef ac_build_cvt_pknorm_u16(struct ac_llvm_context *ctx, LLVMValueRef args[2]);
419LLVMValueRef ac_build_cvt_pknorm_i16_f16(struct ac_llvm_context *ctx, LLVMValueRef args[2]);
420LLVMValueRef ac_build_cvt_pknorm_u16_f16(struct ac_llvm_context *ctx, LLVMValueRef args[2]);
421LLVMValueRef ac_build_cvt_pk_i16(struct ac_llvm_context *ctx, LLVMValueRef args[2], unsigned bits,
422                                 bool hi);
423LLVMValueRef ac_build_cvt_pk_u16(struct ac_llvm_context *ctx, LLVMValueRef args[2], unsigned bits,
424                                 bool hi);
425LLVMValueRef ac_build_wqm_vote(struct ac_llvm_context *ctx, LLVMValueRef i1);
426void ac_build_kill_if_false(struct ac_llvm_context *ctx, LLVMValueRef i1);
427LLVMValueRef ac_build_bfe(struct ac_llvm_context *ctx, LLVMValueRef input, LLVMValueRef offset,
428                          LLVMValueRef width, bool is_signed);
429LLVMValueRef ac_build_imad(struct ac_llvm_context *ctx, LLVMValueRef s0, LLVMValueRef s1,
430                           LLVMValueRef s2);
431LLVMValueRef ac_build_fmad(struct ac_llvm_context *ctx, LLVMValueRef s0, LLVMValueRef s1,
432                           LLVMValueRef s2);
433
434void ac_build_waitcnt(struct ac_llvm_context *ctx, unsigned wait_flags);
435
436LLVMValueRef ac_build_fract(struct ac_llvm_context *ctx, LLVMValueRef src0, unsigned bitsize);
437LLVMValueRef ac_const_uint_vec(struct ac_llvm_context *ctx, LLVMTypeRef type, uint64_t value);
438LLVMValueRef ac_build_isign(struct ac_llvm_context *ctx, LLVMValueRef src0);
439LLVMValueRef ac_build_fsign(struct ac_llvm_context *ctx, LLVMValueRef src);
440LLVMValueRef ac_build_bit_count(struct ac_llvm_context *ctx, LLVMValueRef src0);
441
442LLVMValueRef ac_build_fsat(struct ac_llvm_context *ctx, LLVMValueRef src,
443                           LLVMTypeRef type);
444
445LLVMValueRef ac_build_bitfield_reverse(struct ac_llvm_context *ctx, LLVMValueRef src0);
446
447void ac_init_exec_full_mask(struct ac_llvm_context *ctx);
448
449void ac_declare_lds_as_pointer(struct ac_llvm_context *ac);
450LLVMValueRef ac_lds_load(struct ac_llvm_context *ctx, LLVMValueRef dw_addr);
451void ac_lds_store(struct ac_llvm_context *ctx, LLVMValueRef dw_addr, LLVMValueRef value);
452
453LLVMValueRef ac_find_lsb(struct ac_llvm_context *ctx, LLVMTypeRef dst_type, LLVMValueRef src0);
454
455LLVMTypeRef ac_array_in_const_addr_space(LLVMTypeRef elem_type);
456LLVMTypeRef ac_array_in_const32_addr_space(LLVMTypeRef elem_type);
457
458void ac_build_bgnloop(struct ac_llvm_context *ctx, int lable_id);
459void ac_build_break(struct ac_llvm_context *ctx);
460void ac_build_continue(struct ac_llvm_context *ctx);
461void ac_build_else(struct ac_llvm_context *ctx, int lable_id);
462void ac_build_endif(struct ac_llvm_context *ctx, int lable_id);
463void ac_build_endloop(struct ac_llvm_context *ctx, int lable_id);
464void ac_build_ifcc(struct ac_llvm_context *ctx, LLVMValueRef cond, int label_id);
465
466LLVMValueRef ac_build_alloca(struct ac_llvm_context *ac, LLVMTypeRef type, const char *name);
467LLVMValueRef ac_build_alloca_undef(struct ac_llvm_context *ac, LLVMTypeRef type, const char *name);
468LLVMValueRef ac_build_alloca_init(struct ac_llvm_context *ac, LLVMValueRef val, const char *name);
469
470LLVMValueRef ac_cast_ptr(struct ac_llvm_context *ctx, LLVMValueRef ptr, LLVMTypeRef type);
471
472LLVMValueRef ac_trim_vector(struct ac_llvm_context *ctx, LLVMValueRef value, unsigned count);
473
474LLVMValueRef ac_unpack_param(struct ac_llvm_context *ctx, LLVMValueRef param, unsigned rshift,
475                             unsigned bitwidth);
476
477void ac_apply_fmask_to_sample(struct ac_llvm_context *ac, LLVMValueRef fmask, LLVMValueRef *addr,
478                              bool is_array_tex);
479
480LLVMValueRef ac_build_ds_swizzle(struct ac_llvm_context *ctx, LLVMValueRef src, unsigned mask);
481
482LLVMValueRef ac_build_readlane_no_opt_barrier(struct ac_llvm_context *ctx, LLVMValueRef src,
483                                              LLVMValueRef lane);
484
485LLVMValueRef ac_build_readlane(struct ac_llvm_context *ctx, LLVMValueRef src, LLVMValueRef lane);
486
487LLVMValueRef ac_build_writelane(struct ac_llvm_context *ctx, LLVMValueRef src, LLVMValueRef value,
488                                LLVMValueRef lane);
489
490LLVMValueRef ac_build_mbcnt_add(struct ac_llvm_context *ctx, LLVMValueRef mask, LLVMValueRef add_src);
491LLVMValueRef ac_build_mbcnt(struct ac_llvm_context *ctx, LLVMValueRef mask);
492
493LLVMValueRef ac_build_inclusive_scan(struct ac_llvm_context *ctx, LLVMValueRef src, nir_op op);
494
495LLVMValueRef ac_build_exclusive_scan(struct ac_llvm_context *ctx, LLVMValueRef src, nir_op op);
496
497LLVMValueRef ac_build_reduce(struct ac_llvm_context *ctx, LLVMValueRef src, nir_op op,
498                             unsigned cluster_size);
499
500/**
501 * Common arguments for a scan/reduce operation that accumulates per-wave
502 * values across an entire workgroup, while respecting the order of waves.
503 */
504struct ac_wg_scan {
505   gl_shader_stage stage;
506   bool enable_reduce;
507   bool enable_exclusive;
508   bool enable_inclusive;
509   nir_op op;
510   LLVMValueRef src; /* clobbered! */
511   LLVMValueRef result_reduce;
512   LLVMValueRef result_exclusive;
513   LLVMValueRef result_inclusive;
514   LLVMValueRef extra;
515   LLVMValueRef waveidx;
516   LLVMValueRef numwaves; /* only needed for "reduce" operations */
517
518   /* T addrspace(LDS) pointer to the same type as value, at least maxwaves entries */
519   LLVMValueRef scratch;
520   unsigned maxwaves;
521};
522
523void ac_build_wg_wavescan_top(struct ac_llvm_context *ctx, struct ac_wg_scan *ws);
524void ac_build_wg_wavescan_bottom(struct ac_llvm_context *ctx, struct ac_wg_scan *ws);
525void ac_build_wg_wavescan(struct ac_llvm_context *ctx, struct ac_wg_scan *ws);
526
527void ac_build_wg_scan_top(struct ac_llvm_context *ctx, struct ac_wg_scan *ws);
528void ac_build_wg_scan_bottom(struct ac_llvm_context *ctx, struct ac_wg_scan *ws);
529void ac_build_wg_scan(struct ac_llvm_context *ctx, struct ac_wg_scan *ws);
530
531LLVMValueRef ac_build_quad_swizzle(struct ac_llvm_context *ctx, LLVMValueRef src, unsigned lane0,
532                                   unsigned lane1, unsigned lane2, unsigned lane3);
533
534LLVMValueRef ac_build_shuffle(struct ac_llvm_context *ctx, LLVMValueRef src, LLVMValueRef index);
535
536LLVMValueRef ac_build_frexp_exp(struct ac_llvm_context *ctx, LLVMValueRef src0, unsigned bitsize);
537
538LLVMValueRef ac_build_frexp_mant(struct ac_llvm_context *ctx, LLVMValueRef src0, unsigned bitsize);
539
540LLVMValueRef ac_build_canonicalize(struct ac_llvm_context *ctx, LLVMValueRef src0,
541                                   unsigned bitsize);
542
543LLVMValueRef ac_build_ddxy_interp(struct ac_llvm_context *ctx, LLVMValueRef interp_ij);
544
545LLVMValueRef ac_build_load_helper_invocation(struct ac_llvm_context *ctx);
546
547LLVMValueRef ac_build_is_helper_invocation(struct ac_llvm_context *ctx);
548
549LLVMValueRef ac_build_call(struct ac_llvm_context *ctx, LLVMValueRef func, LLVMValueRef *args,
550                           unsigned num_args);
551
552LLVMValueRef ac_build_atomic_rmw(struct ac_llvm_context *ctx, LLVMAtomicRMWBinOp op,
553                                 LLVMValueRef ptr, LLVMValueRef val, const char *sync_scope);
554
555LLVMValueRef ac_build_atomic_cmp_xchg(struct ac_llvm_context *ctx, LLVMValueRef ptr,
556                                      LLVMValueRef cmp, LLVMValueRef val, const char *sync_scope);
557
558void ac_export_mrt_z(struct ac_llvm_context *ctx, LLVMValueRef depth, LLVMValueRef stencil,
559                     LLVMValueRef samplemask, LLVMValueRef mrt0_alpha, bool is_last,
560                     struct ac_export_args *args);
561
562void ac_build_sendmsg_gs_alloc_req(struct ac_llvm_context *ctx, LLVMValueRef wave_id,
563                                   LLVMValueRef vtx_cnt, LLVMValueRef prim_cnt);
564
565struct ac_ngg_prim {
566   unsigned num_vertices;
567   LLVMValueRef isnull;
568   LLVMValueRef index[3];
569   LLVMValueRef edgeflags;
570   LLVMValueRef passthrough;
571};
572
573LLVMValueRef ac_pack_edgeflags_for_export(struct ac_llvm_context *ctx,
574                                          const struct ac_shader_args *args);
575LLVMValueRef ac_pack_prim_export(struct ac_llvm_context *ctx, const struct ac_ngg_prim *prim);
576void ac_build_export_prim(struct ac_llvm_context *ctx, const struct ac_ngg_prim *prim);
577
578static inline LLVMValueRef ac_get_arg(struct ac_llvm_context *ctx, struct ac_arg arg)
579{
580   assert(arg.used);
581   return LLVMGetParam(ctx->main_function, arg.arg_index);
582}
583
584enum ac_llvm_calling_convention
585{
586   AC_LLVM_AMDGPU_VS = 87,
587   AC_LLVM_AMDGPU_GS = 88,
588   AC_LLVM_AMDGPU_PS = 89,
589   AC_LLVM_AMDGPU_CS = 90,
590   AC_LLVM_AMDGPU_HS = 93,
591};
592
593LLVMValueRef ac_build_main(const struct ac_shader_args *args, struct ac_llvm_context *ctx,
594                           enum ac_llvm_calling_convention convention, const char *name,
595                           LLVMTypeRef ret_type, LLVMModuleRef module);
596void ac_build_s_endpgm(struct ac_llvm_context *ctx);
597
598void ac_build_triangle_strip_indices_to_triangle(struct ac_llvm_context *ctx, LLVMValueRef is_odd,
599                                                 LLVMValueRef flatshade_first,
600                                                 LLVMValueRef index[3]);
601LLVMValueRef ac_build_is_inf_or_nan(struct ac_llvm_context *ctx, LLVMValueRef a);
602
603void ac_build_dual_src_blend_swizzle(struct ac_llvm_context *ctx,
604                                     struct ac_export_args *mrt0,
605                                     struct ac_export_args *mrt1);
606
607#ifdef __cplusplus
608}
609#endif
610
611#endif
612