703f7ea31ccd2411d7204488e13711fb61dec408
[mesa.git] / src / amd / llvm / 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/Core.h>
30 #include "compiler/nir/nir.h"
31 #include "amd_family.h"
32 #include "ac_shader_util.h"
33 #include "ac_shader_args.h"
34 #include "ac_shader_abi.h"
35
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39
40 enum {
41 AC_ADDR_SPACE_FLAT = 0, /* Slower than global. */
42 AC_ADDR_SPACE_GLOBAL = 1,
43 AC_ADDR_SPACE_GDS = 2,
44 AC_ADDR_SPACE_LDS = 3,
45 AC_ADDR_SPACE_CONST = 4, /* Global allowing SMEM. */
46 AC_ADDR_SPACE_CONST_32BIT = 6, /* same as CONST, but the pointer type has 32 bits */
47 };
48
49 #define AC_WAIT_LGKM (1 << 0) /* LDS, GDS, constant, message */
50 #define AC_WAIT_VLOAD (1 << 1) /* VMEM load/sample instructions */
51 #define AC_WAIT_VSTORE (1 << 2) /* VMEM store instructions */
52
53 struct ac_llvm_flow;
54 struct ac_llvm_compiler;
55 enum ac_float_mode;
56
57 struct ac_llvm_flow_state {
58 struct ac_llvm_flow *stack;
59 unsigned depth_max;
60 unsigned depth;
61 };
62
63 struct ac_llvm_context {
64 LLVMContextRef context;
65 LLVMModuleRef module;
66 LLVMBuilderRef builder;
67
68 LLVMValueRef main_function;
69
70 LLVMTypeRef voidt;
71 LLVMTypeRef i1;
72 LLVMTypeRef i8;
73 LLVMTypeRef i16;
74 LLVMTypeRef i32;
75 LLVMTypeRef i64;
76 LLVMTypeRef i128;
77 LLVMTypeRef intptr;
78 LLVMTypeRef f16;
79 LLVMTypeRef f32;
80 LLVMTypeRef f64;
81 LLVMTypeRef v2i16;
82 LLVMTypeRef v2i32;
83 LLVMTypeRef v3i32;
84 LLVMTypeRef v4i32;
85 LLVMTypeRef v2f32;
86 LLVMTypeRef v3f32;
87 LLVMTypeRef v4f32;
88 LLVMTypeRef v8i32;
89 LLVMTypeRef iN_wavemask;
90 LLVMTypeRef iN_ballotmask;
91
92 LLVMValueRef i8_0;
93 LLVMValueRef i8_1;
94 LLVMValueRef i16_0;
95 LLVMValueRef i16_1;
96 LLVMValueRef i32_0;
97 LLVMValueRef i32_1;
98 LLVMValueRef i64_0;
99 LLVMValueRef i64_1;
100 LLVMValueRef i128_0;
101 LLVMValueRef i128_1;
102 LLVMValueRef f16_0;
103 LLVMValueRef f16_1;
104 LLVMValueRef f32_0;
105 LLVMValueRef f32_1;
106 LLVMValueRef f64_0;
107 LLVMValueRef f64_1;
108 LLVMValueRef i1true;
109 LLVMValueRef i1false;
110
111 /* Temporary helper to implement demote_to_helper:
112 * True = live lanes
113 * False = demoted lanes
114 */
115 LLVMValueRef postponed_kill;
116
117 /* Since ac_nir_translate makes a local copy of ac_llvm_context, there
118 * are two ac_llvm_contexts. Declare a pointer here, so that the control
119 * flow stack is shared by both ac_llvm_contexts.
120 */
121 struct ac_llvm_flow_state *flow;
122
123 unsigned range_md_kind;
124 unsigned invariant_load_md_kind;
125 unsigned uniform_md_kind;
126 LLVMValueRef empty_md;
127
128 enum chip_class chip_class;
129 enum radeon_family family;
130
131 unsigned wave_size;
132 unsigned ballot_mask_bits;
133
134 unsigned float_mode;
135
136 LLVMValueRef lds;
137 };
138
139 void
140 ac_llvm_context_init(struct ac_llvm_context *ctx,
141 struct ac_llvm_compiler *compiler,
142 enum chip_class chip_class, enum radeon_family family,
143 enum ac_float_mode float_mode, unsigned wave_size,
144 unsigned ballot_mask_bits);
145
146 void
147 ac_llvm_context_dispose(struct ac_llvm_context *ctx);
148
149 int
150 ac_get_llvm_num_components(LLVMValueRef value);
151
152 int
153 ac_get_elem_bits(struct ac_llvm_context *ctx, LLVMTypeRef type);
154
155 LLVMValueRef
156 ac_llvm_extract_elem(struct ac_llvm_context *ac,
157 LLVMValueRef value,
158 int index);
159
160 unsigned ac_get_type_size(LLVMTypeRef type);
161
162 LLVMTypeRef ac_to_integer_type(struct ac_llvm_context *ctx, LLVMTypeRef t);
163 LLVMValueRef ac_to_integer(struct ac_llvm_context *ctx, LLVMValueRef v);
164 LLVMValueRef ac_to_integer_or_pointer(struct ac_llvm_context *ctx, LLVMValueRef v);
165 LLVMTypeRef ac_to_float_type(struct ac_llvm_context *ctx, LLVMTypeRef t);
166 LLVMValueRef ac_to_float(struct ac_llvm_context *ctx, LLVMValueRef v);
167
168 LLVMValueRef
169 ac_build_intrinsic(struct ac_llvm_context *ctx, const char *name,
170 LLVMTypeRef return_type, LLVMValueRef *params,
171 unsigned param_count, unsigned attrib_mask);
172
173 void ac_build_type_name_for_intr(LLVMTypeRef type, char *buf, unsigned bufsize);
174
175 LLVMValueRef
176 ac_build_phi(struct ac_llvm_context *ctx, LLVMTypeRef type,
177 unsigned count_incoming, LLVMValueRef *values,
178 LLVMBasicBlockRef *blocks);
179
180 void ac_build_s_barrier(struct ac_llvm_context *ctx);
181 void ac_build_optimization_barrier(struct ac_llvm_context *ctx,
182 LLVMValueRef *pvgpr);
183
184 LLVMValueRef ac_build_shader_clock(struct ac_llvm_context *ctx);
185
186 LLVMValueRef ac_build_ballot(struct ac_llvm_context *ctx, LLVMValueRef value);
187 LLVMValueRef ac_get_i1_sgpr_mask(struct ac_llvm_context *ctx,
188 LLVMValueRef value);
189
190 LLVMValueRef ac_build_vote_all(struct ac_llvm_context *ctx, LLVMValueRef value);
191
192 LLVMValueRef ac_build_vote_any(struct ac_llvm_context *ctx, LLVMValueRef value);
193
194 LLVMValueRef ac_build_vote_eq(struct ac_llvm_context *ctx, LLVMValueRef value);
195
196 LLVMValueRef
197 ac_build_varying_gather_values(struct ac_llvm_context *ctx, LLVMValueRef *values,
198 unsigned value_count, unsigned component);
199
200 LLVMValueRef
201 ac_build_gather_values_extended(struct ac_llvm_context *ctx,
202 LLVMValueRef *values,
203 unsigned value_count,
204 unsigned value_stride,
205 bool load,
206 bool always_vector);
207 LLVMValueRef
208 ac_build_gather_values(struct ac_llvm_context *ctx,
209 LLVMValueRef *values,
210 unsigned value_count);
211
212 LLVMValueRef
213 ac_extract_components(struct ac_llvm_context *ctx,
214 LLVMValueRef value,
215 unsigned start,
216 unsigned channels);
217
218 LLVMValueRef ac_build_expand_to_vec4(struct ac_llvm_context *ctx,
219 LLVMValueRef value,
220 unsigned num_channels);
221 LLVMValueRef ac_build_round(struct ac_llvm_context *ctx, LLVMValueRef value);
222
223 LLVMValueRef
224 ac_build_fdiv(struct ac_llvm_context *ctx,
225 LLVMValueRef num,
226 LLVMValueRef den);
227
228 LLVMValueRef ac_build_fast_udiv(struct ac_llvm_context *ctx,
229 LLVMValueRef num,
230 LLVMValueRef multiplier,
231 LLVMValueRef pre_shift,
232 LLVMValueRef post_shift,
233 LLVMValueRef increment);
234 LLVMValueRef ac_build_fast_udiv_nuw(struct ac_llvm_context *ctx,
235 LLVMValueRef num,
236 LLVMValueRef multiplier,
237 LLVMValueRef pre_shift,
238 LLVMValueRef post_shift,
239 LLVMValueRef increment);
240 LLVMValueRef ac_build_fast_udiv_u31_d_not_one(struct ac_llvm_context *ctx,
241 LLVMValueRef num,
242 LLVMValueRef multiplier,
243 LLVMValueRef post_shift);
244
245 void
246 ac_prepare_cube_coords(struct ac_llvm_context *ctx,
247 bool is_deriv, bool is_array, bool is_lod,
248 LLVMValueRef *coords_arg,
249 LLVMValueRef *derivs_arg);
250
251
252 LLVMValueRef
253 ac_build_fs_interp(struct ac_llvm_context *ctx,
254 LLVMValueRef llvm_chan,
255 LLVMValueRef attr_number,
256 LLVMValueRef params,
257 LLVMValueRef i,
258 LLVMValueRef j);
259
260 LLVMValueRef
261 ac_build_fs_interp_f16(struct ac_llvm_context *ctx,
262 LLVMValueRef llvm_chan,
263 LLVMValueRef attr_number,
264 LLVMValueRef params,
265 LLVMValueRef i,
266 LLVMValueRef j);
267
268 LLVMValueRef
269 ac_build_fs_interp_mov(struct ac_llvm_context *ctx,
270 LLVMValueRef parameter,
271 LLVMValueRef llvm_chan,
272 LLVMValueRef attr_number,
273 LLVMValueRef params);
274
275 LLVMValueRef
276 ac_build_gep_ptr(struct ac_llvm_context *ctx,
277 LLVMValueRef base_ptr,
278 LLVMValueRef index);
279
280 LLVMValueRef
281 ac_build_gep0(struct ac_llvm_context *ctx,
282 LLVMValueRef base_ptr,
283 LLVMValueRef index);
284 LLVMValueRef ac_build_pointer_add(struct ac_llvm_context *ctx, LLVMValueRef ptr,
285 LLVMValueRef index);
286
287 void
288 ac_build_indexed_store(struct ac_llvm_context *ctx,
289 LLVMValueRef base_ptr, LLVMValueRef index,
290 LLVMValueRef value);
291
292 LLVMValueRef ac_build_load(struct ac_llvm_context *ctx, LLVMValueRef base_ptr,
293 LLVMValueRef index);
294 LLVMValueRef ac_build_load_invariant(struct ac_llvm_context *ctx,
295 LLVMValueRef base_ptr, LLVMValueRef index);
296 LLVMValueRef ac_build_load_to_sgpr(struct ac_llvm_context *ctx,
297 LLVMValueRef base_ptr, LLVMValueRef index);
298 LLVMValueRef ac_build_load_to_sgpr_uint_wraparound(struct ac_llvm_context *ctx,
299 LLVMValueRef base_ptr, LLVMValueRef index);
300
301 void
302 ac_build_buffer_store_dword(struct ac_llvm_context *ctx,
303 LLVMValueRef rsrc,
304 LLVMValueRef vdata,
305 unsigned num_channels,
306 LLVMValueRef voffset,
307 LLVMValueRef soffset,
308 unsigned inst_offset,
309 unsigned cache_policy);
310
311 void
312 ac_build_buffer_store_format(struct ac_llvm_context *ctx,
313 LLVMValueRef rsrc,
314 LLVMValueRef data,
315 LLVMValueRef vindex,
316 LLVMValueRef voffset,
317 unsigned num_channels,
318 unsigned cache_policy);
319
320 LLVMValueRef
321 ac_build_buffer_load(struct ac_llvm_context *ctx,
322 LLVMValueRef rsrc,
323 int num_channels,
324 LLVMValueRef vindex,
325 LLVMValueRef voffset,
326 LLVMValueRef soffset,
327 unsigned inst_offset,
328 unsigned cache_policy,
329 bool can_speculate,
330 bool allow_smem);
331
332 LLVMValueRef ac_build_buffer_load_format(struct ac_llvm_context *ctx,
333 LLVMValueRef rsrc,
334 LLVMValueRef vindex,
335 LLVMValueRef voffset,
336 unsigned num_channels,
337 unsigned cache_policy,
338 bool can_speculate);
339
340 LLVMValueRef
341 ac_build_tbuffer_load_short(struct ac_llvm_context *ctx,
342 LLVMValueRef rsrc,
343 LLVMValueRef voffset,
344 LLVMValueRef soffset,
345 LLVMValueRef immoffset,
346 unsigned cache_policy);
347
348 LLVMValueRef
349 ac_build_tbuffer_load_byte(struct ac_llvm_context *ctx,
350 LLVMValueRef rsrc,
351 LLVMValueRef voffset,
352 LLVMValueRef soffset,
353 LLVMValueRef immoffset,
354 unsigned cache_policy);
355
356 LLVMValueRef
357 ac_build_struct_tbuffer_load(struct ac_llvm_context *ctx,
358 LLVMValueRef rsrc,
359 LLVMValueRef vindex,
360 LLVMValueRef voffset,
361 LLVMValueRef soffset,
362 LLVMValueRef immoffset,
363 unsigned num_channels,
364 unsigned dfmt,
365 unsigned nfmt,
366 unsigned cache_policy,
367 bool can_speculate);
368
369 LLVMValueRef
370 ac_build_raw_tbuffer_load(struct ac_llvm_context *ctx,
371 LLVMValueRef rsrc,
372 LLVMValueRef voffset,
373 LLVMValueRef soffset,
374 LLVMValueRef immoffset,
375 unsigned num_channels,
376 unsigned dfmt,
377 unsigned nfmt,
378 unsigned cache_policy,
379 bool can_speculate);
380
381 /* For ac_build_fetch_format.
382 *
383 * Note: FLOAT must be 0 (used for convenience of encoding in radeonsi).
384 */
385 enum {
386 AC_FETCH_FORMAT_FLOAT = 0,
387 AC_FETCH_FORMAT_FIXED,
388 AC_FETCH_FORMAT_UNORM,
389 AC_FETCH_FORMAT_SNORM,
390 AC_FETCH_FORMAT_USCALED,
391 AC_FETCH_FORMAT_SSCALED,
392 AC_FETCH_FORMAT_UINT,
393 AC_FETCH_FORMAT_SINT,
394 };
395
396 LLVMValueRef
397 ac_build_opencoded_load_format(struct ac_llvm_context *ctx,
398 unsigned log_size,
399 unsigned num_channels,
400 unsigned format,
401 bool reverse,
402 bool known_aligned,
403 LLVMValueRef rsrc,
404 LLVMValueRef vindex,
405 LLVMValueRef voffset,
406 LLVMValueRef soffset,
407 unsigned cache_policy,
408 bool can_speculate);
409
410 void
411 ac_build_tbuffer_store_short(struct ac_llvm_context *ctx,
412 LLVMValueRef rsrc,
413 LLVMValueRef vdata,
414 LLVMValueRef voffset,
415 LLVMValueRef soffset,
416 unsigned cache_policy);
417
418 void
419 ac_build_tbuffer_store_byte(struct ac_llvm_context *ctx,
420 LLVMValueRef rsrc,
421 LLVMValueRef vdata,
422 LLVMValueRef voffset,
423 LLVMValueRef soffset,
424 unsigned cache_policy);
425
426 void
427 ac_build_struct_tbuffer_store(struct ac_llvm_context *ctx,
428 LLVMValueRef rsrc,
429 LLVMValueRef vdata,
430 LLVMValueRef vindex,
431 LLVMValueRef voffset,
432 LLVMValueRef soffset,
433 LLVMValueRef immoffset,
434 unsigned num_channels,
435 unsigned dfmt,
436 unsigned nfmt,
437 unsigned cache_policy);
438
439 void
440 ac_build_raw_tbuffer_store(struct ac_llvm_context *ctx,
441 LLVMValueRef rsrc,
442 LLVMValueRef vdata,
443 LLVMValueRef voffset,
444 LLVMValueRef soffset,
445 LLVMValueRef immoffset,
446 unsigned num_channels,
447 unsigned dfmt,
448 unsigned nfmt,
449 unsigned cache_policy);
450
451 LLVMValueRef
452 ac_get_thread_id(struct ac_llvm_context *ctx);
453
454 #define AC_TID_MASK_TOP_LEFT 0xfffffffc
455 #define AC_TID_MASK_TOP 0xfffffffd
456 #define AC_TID_MASK_LEFT 0xfffffffe
457
458 LLVMValueRef
459 ac_build_ddxy(struct ac_llvm_context *ctx,
460 uint32_t mask,
461 int idx,
462 LLVMValueRef val);
463
464 #define AC_SENDMSG_GS 2
465 #define AC_SENDMSG_GS_DONE 3
466 #define AC_SENDMSG_GS_ALLOC_REQ 9
467
468 #define AC_SENDMSG_GS_OP_NOP (0 << 4)
469 #define AC_SENDMSG_GS_OP_CUT (1 << 4)
470 #define AC_SENDMSG_GS_OP_EMIT (2 << 4)
471 #define AC_SENDMSG_GS_OP_EMIT_CUT (3 << 4)
472
473 void ac_build_sendmsg(struct ac_llvm_context *ctx,
474 uint32_t msg,
475 LLVMValueRef wave_id);
476
477 LLVMValueRef ac_build_imsb(struct ac_llvm_context *ctx,
478 LLVMValueRef arg,
479 LLVMTypeRef dst_type);
480
481 LLVMValueRef ac_build_umsb(struct ac_llvm_context *ctx,
482 LLVMValueRef arg,
483 LLVMTypeRef dst_type);
484 LLVMValueRef ac_build_fmin(struct ac_llvm_context *ctx, LLVMValueRef a,
485 LLVMValueRef b);
486 LLVMValueRef ac_build_fmax(struct ac_llvm_context *ctx, LLVMValueRef a,
487 LLVMValueRef b);
488 LLVMValueRef ac_build_imin(struct ac_llvm_context *ctx, LLVMValueRef a,
489 LLVMValueRef b);
490 LLVMValueRef ac_build_imax(struct ac_llvm_context *ctx, LLVMValueRef a,
491 LLVMValueRef b);
492 LLVMValueRef ac_build_umin(struct ac_llvm_context *ctx, LLVMValueRef a, LLVMValueRef b);
493 LLVMValueRef ac_build_umax(struct ac_llvm_context *ctx, LLVMValueRef a, LLVMValueRef b);
494 LLVMValueRef ac_build_clamp(struct ac_llvm_context *ctx, LLVMValueRef value);
495
496 struct ac_export_args {
497 LLVMValueRef out[4];
498 unsigned target;
499 unsigned enabled_channels;
500 bool compr;
501 bool done;
502 bool valid_mask;
503 };
504
505 void ac_build_export(struct ac_llvm_context *ctx, struct ac_export_args *a);
506
507 void ac_build_export_null(struct ac_llvm_context *ctx);
508
509 enum ac_image_opcode {
510 ac_image_sample,
511 ac_image_gather4,
512 ac_image_load,
513 ac_image_load_mip,
514 ac_image_store,
515 ac_image_store_mip,
516 ac_image_get_lod,
517 ac_image_get_resinfo,
518 ac_image_atomic,
519 ac_image_atomic_cmpswap,
520 };
521
522 enum ac_atomic_op {
523 ac_atomic_swap,
524 ac_atomic_add,
525 ac_atomic_sub,
526 ac_atomic_smin,
527 ac_atomic_umin,
528 ac_atomic_smax,
529 ac_atomic_umax,
530 ac_atomic_and,
531 ac_atomic_or,
532 ac_atomic_xor,
533 ac_atomic_inc_wrap,
534 ac_atomic_dec_wrap,
535 };
536
537 /* These cache policy bits match the definitions used by the LLVM intrinsics. */
538 enum ac_image_cache_policy {
539 ac_glc = 1 << 0, /* per-CU cache control */
540 ac_slc = 1 << 1, /* global L2 cache control */
541 ac_dlc = 1 << 2, /* per-shader-array cache control */
542 ac_swizzled = 1 << 3, /* the access is swizzled, disabling load/store merging */
543 };
544
545 struct ac_image_args {
546 enum ac_image_opcode opcode : 4;
547 enum ac_atomic_op atomic : 4; /* for the ac_image_atomic opcode */
548 enum ac_image_dim dim : 3;
549 unsigned dmask : 4;
550 unsigned cache_policy : 3;
551 bool unorm : 1;
552 bool level_zero : 1;
553 unsigned attributes; /* additional call-site specific AC_FUNC_ATTRs */
554
555 LLVMValueRef resource;
556 LLVMValueRef sampler;
557 LLVMValueRef data[2]; /* data[0] is source data (vector); data[1] is cmp for cmpswap */
558 LLVMValueRef offset;
559 LLVMValueRef bias;
560 LLVMValueRef compare;
561 LLVMValueRef derivs[6];
562 LLVMValueRef coords[4];
563 LLVMValueRef lod; // also used by ac_image_get_resinfo
564 LLVMValueRef min_lod;
565 };
566
567 LLVMValueRef ac_build_image_opcode(struct ac_llvm_context *ctx,
568 struct ac_image_args *a);
569 LLVMValueRef ac_build_image_get_sample_count(struct ac_llvm_context *ctx,
570 LLVMValueRef rsrc);
571 LLVMValueRef ac_build_cvt_pkrtz_f16(struct ac_llvm_context *ctx,
572 LLVMValueRef args[2]);
573 LLVMValueRef ac_build_cvt_pknorm_i16(struct ac_llvm_context *ctx,
574 LLVMValueRef args[2]);
575 LLVMValueRef ac_build_cvt_pknorm_u16(struct ac_llvm_context *ctx,
576 LLVMValueRef args[2]);
577 LLVMValueRef ac_build_cvt_pk_i16(struct ac_llvm_context *ctx,
578 LLVMValueRef args[2], unsigned bits, bool hi);
579 LLVMValueRef ac_build_cvt_pk_u16(struct ac_llvm_context *ctx,
580 LLVMValueRef args[2], unsigned bits, bool hi);
581 LLVMValueRef ac_build_wqm_vote(struct ac_llvm_context *ctx, LLVMValueRef i1);
582 void ac_build_kill_if_false(struct ac_llvm_context *ctx, LLVMValueRef i1);
583 LLVMValueRef ac_build_bfe(struct ac_llvm_context *ctx, LLVMValueRef input,
584 LLVMValueRef offset, LLVMValueRef width,
585 bool is_signed);
586 LLVMValueRef ac_build_imad(struct ac_llvm_context *ctx, LLVMValueRef s0,
587 LLVMValueRef s1, LLVMValueRef s2);
588 LLVMValueRef ac_build_fmad(struct ac_llvm_context *ctx, LLVMValueRef s0,
589 LLVMValueRef s1, LLVMValueRef s2);
590
591 void ac_build_waitcnt(struct ac_llvm_context *ctx, unsigned wait_flags);
592
593 LLVMValueRef ac_build_fract(struct ac_llvm_context *ctx, LLVMValueRef src0,
594 unsigned bitsize);
595
596 LLVMValueRef ac_build_fmed3(struct ac_llvm_context *ctx, LLVMValueRef src0,
597 LLVMValueRef src1, LLVMValueRef src2,
598 unsigned bitsize);
599
600 LLVMValueRef ac_build_isign(struct ac_llvm_context *ctx, LLVMValueRef src0,
601 unsigned bitsize);
602
603 LLVMValueRef ac_build_fsign(struct ac_llvm_context *ctx, LLVMValueRef src0,
604 unsigned bitsize);
605
606 LLVMValueRef ac_build_bit_count(struct ac_llvm_context *ctx, LLVMValueRef src0);
607
608 LLVMValueRef ac_build_bitfield_reverse(struct ac_llvm_context *ctx,
609 LLVMValueRef src0);
610
611 void ac_optimize_vs_outputs(struct ac_llvm_context *ac,
612 LLVMValueRef main_fn,
613 uint8_t *vs_output_param_offset,
614 uint32_t num_outputs,
615 uint32_t skip_output_mask,
616 uint8_t *num_param_exports);
617 void ac_init_exec_full_mask(struct ac_llvm_context *ctx);
618
619 void ac_declare_lds_as_pointer(struct ac_llvm_context *ac);
620 LLVMValueRef ac_lds_load(struct ac_llvm_context *ctx,
621 LLVMValueRef dw_addr);
622 void ac_lds_store(struct ac_llvm_context *ctx,
623 LLVMValueRef dw_addr, LLVMValueRef value);
624
625 LLVMValueRef ac_find_lsb(struct ac_llvm_context *ctx,
626 LLVMTypeRef dst_type,
627 LLVMValueRef src0);
628
629 LLVMTypeRef ac_array_in_const_addr_space(LLVMTypeRef elem_type);
630 LLVMTypeRef ac_array_in_const32_addr_space(LLVMTypeRef elem_type);
631
632 void ac_build_bgnloop(struct ac_llvm_context *ctx, int lable_id);
633 void ac_build_break(struct ac_llvm_context *ctx);
634 void ac_build_continue(struct ac_llvm_context *ctx);
635 void ac_build_else(struct ac_llvm_context *ctx, int lable_id);
636 void ac_build_endif(struct ac_llvm_context *ctx, int lable_id);
637 void ac_build_endloop(struct ac_llvm_context *ctx, int lable_id);
638 void ac_build_ifcc(struct ac_llvm_context *ctx, LLVMValueRef cond, int label_id);
639 void ac_build_if(struct ac_llvm_context *ctx, LLVMValueRef value,
640 int lable_id);
641 void ac_build_uif(struct ac_llvm_context *ctx, LLVMValueRef value,
642 int lable_id);
643
644 LLVMValueRef ac_build_alloca(struct ac_llvm_context *ac, LLVMTypeRef type,
645 const char *name);
646 LLVMValueRef ac_build_alloca_undef(struct ac_llvm_context *ac, LLVMTypeRef type,
647 const char *name);
648
649 LLVMValueRef ac_cast_ptr(struct ac_llvm_context *ctx, LLVMValueRef ptr,
650 LLVMTypeRef type);
651
652 LLVMValueRef ac_trim_vector(struct ac_llvm_context *ctx, LLVMValueRef value,
653 unsigned count);
654
655 LLVMValueRef ac_unpack_param(struct ac_llvm_context *ctx, LLVMValueRef param,
656 unsigned rshift, unsigned bitwidth);
657
658 void ac_apply_fmask_to_sample(struct ac_llvm_context *ac, LLVMValueRef fmask,
659 LLVMValueRef *addr, bool is_array_tex);
660
661 LLVMValueRef
662 ac_build_ds_swizzle(struct ac_llvm_context *ctx, LLVMValueRef src, unsigned mask);
663
664 LLVMValueRef ac_build_readlane_no_opt_barrier(struct ac_llvm_context *ctx,
665 LLVMValueRef src, LLVMValueRef lane);
666
667 LLVMValueRef
668 ac_build_readlane(struct ac_llvm_context *ctx, LLVMValueRef src, LLVMValueRef lane);
669
670 LLVMValueRef
671 ac_build_writelane(struct ac_llvm_context *ctx, LLVMValueRef src, LLVMValueRef value, LLVMValueRef lane);
672
673 LLVMValueRef
674 ac_build_mbcnt(struct ac_llvm_context *ctx, LLVMValueRef mask);
675
676 LLVMValueRef
677 ac_build_inclusive_scan(struct ac_llvm_context *ctx, LLVMValueRef src, nir_op op);
678
679 LLVMValueRef
680 ac_build_exclusive_scan(struct ac_llvm_context *ctx, LLVMValueRef src, nir_op op);
681
682 LLVMValueRef
683 ac_build_reduce(struct ac_llvm_context *ctx, LLVMValueRef src, nir_op op, unsigned cluster_size);
684
685 /**
686 * Common arguments for a scan/reduce operation that accumulates per-wave
687 * values across an entire workgroup, while respecting the order of waves.
688 */
689 struct ac_wg_scan {
690 bool enable_reduce;
691 bool enable_exclusive;
692 bool enable_inclusive;
693 nir_op op;
694 LLVMValueRef src; /* clobbered! */
695 LLVMValueRef result_reduce;
696 LLVMValueRef result_exclusive;
697 LLVMValueRef result_inclusive;
698 LLVMValueRef extra;
699 LLVMValueRef waveidx;
700 LLVMValueRef numwaves; /* only needed for "reduce" operations */
701
702 /* T addrspace(LDS) pointer to the same type as value, at least maxwaves entries */
703 LLVMValueRef scratch;
704 unsigned maxwaves;
705 };
706
707 void
708 ac_build_wg_wavescan_top(struct ac_llvm_context *ctx, struct ac_wg_scan *ws);
709 void
710 ac_build_wg_wavescan_bottom(struct ac_llvm_context *ctx, struct ac_wg_scan *ws);
711 void
712 ac_build_wg_wavescan(struct ac_llvm_context *ctx, struct ac_wg_scan *ws);
713
714 void
715 ac_build_wg_scan_top(struct ac_llvm_context *ctx, struct ac_wg_scan *ws);
716 void
717 ac_build_wg_scan_bottom(struct ac_llvm_context *ctx, struct ac_wg_scan *ws);
718 void
719 ac_build_wg_scan(struct ac_llvm_context *ctx, struct ac_wg_scan *ws);
720
721 LLVMValueRef
722 ac_build_quad_swizzle(struct ac_llvm_context *ctx, LLVMValueRef src,
723 unsigned lane0, unsigned lane1, unsigned lane2, unsigned lane3);
724
725 LLVMValueRef
726 ac_build_shuffle(struct ac_llvm_context *ctx, LLVMValueRef src, LLVMValueRef index);
727
728 LLVMValueRef
729 ac_build_frexp_exp(struct ac_llvm_context *ctx, LLVMValueRef src0,
730 unsigned bitsize);
731
732 LLVMValueRef
733 ac_build_frexp_mant(struct ac_llvm_context *ctx, LLVMValueRef src0,
734 unsigned bitsize);
735
736 LLVMValueRef
737 ac_build_canonicalize(struct ac_llvm_context *ctx, LLVMValueRef src0,
738 unsigned bitsize);
739
740 LLVMValueRef
741 ac_build_ddxy_interp(struct ac_llvm_context *ctx, LLVMValueRef interp_ij);
742
743 LLVMValueRef
744 ac_build_load_helper_invocation(struct ac_llvm_context *ctx);
745
746 LLVMValueRef
747 ac_build_is_helper_invocation(struct ac_llvm_context *ctx);
748
749 LLVMValueRef ac_build_call(struct ac_llvm_context *ctx, LLVMValueRef func,
750 LLVMValueRef *args, unsigned num_args);
751
752 LLVMValueRef ac_build_atomic_rmw(struct ac_llvm_context *ctx, LLVMAtomicRMWBinOp op,
753 LLVMValueRef ptr, LLVMValueRef val,
754 const char *sync_scope);
755
756 LLVMValueRef ac_build_atomic_cmp_xchg(struct ac_llvm_context *ctx, LLVMValueRef ptr,
757 LLVMValueRef cmp, LLVMValueRef val,
758 const char *sync_scope);
759
760 void
761 ac_export_mrt_z(struct ac_llvm_context *ctx, LLVMValueRef depth,
762 LLVMValueRef stencil, LLVMValueRef samplemask,
763 struct ac_export_args *args);
764
765 void ac_build_sendmsg_gs_alloc_req(struct ac_llvm_context *ctx, LLVMValueRef wave_id,
766 LLVMValueRef vtx_cnt, LLVMValueRef prim_cnt);
767
768 struct ac_ngg_prim {
769 unsigned num_vertices;
770 LLVMValueRef isnull;
771 LLVMValueRef index[3];
772 LLVMValueRef edgeflag[3];
773 LLVMValueRef passthrough;
774 };
775
776 LLVMValueRef ac_pack_prim_export(struct ac_llvm_context *ctx,
777 const struct ac_ngg_prim *prim);
778 void ac_build_export_prim(struct ac_llvm_context *ctx,
779 const struct ac_ngg_prim *prim);
780
781 static inline LLVMValueRef
782 ac_get_arg(struct ac_llvm_context *ctx, struct ac_arg arg)
783 {
784 assert(arg.used);
785 return LLVMGetParam(ctx->main_function, arg.arg_index);
786 }
787
788 enum ac_llvm_calling_convention {
789 AC_LLVM_AMDGPU_VS = 87,
790 AC_LLVM_AMDGPU_GS = 88,
791 AC_LLVM_AMDGPU_PS = 89,
792 AC_LLVM_AMDGPU_CS = 90,
793 AC_LLVM_AMDGPU_HS = 93,
794 };
795
796 LLVMValueRef ac_build_main(const struct ac_shader_args *args,
797 struct ac_llvm_context *ctx,
798 enum ac_llvm_calling_convention convention,
799 const char *name, LLVMTypeRef ret_type,
800 LLVMModuleRef module);
801 void ac_build_s_endpgm(struct ac_llvm_context *ctx);
802
803 LLVMValueRef ac_prefix_bitcount(struct ac_llvm_context *ctx,
804 LLVMValueRef mask, LLVMValueRef index);
805 LLVMValueRef ac_prefix_bitcount_2x64(struct ac_llvm_context *ctx,
806 LLVMValueRef mask[2], LLVMValueRef index);
807 void ac_build_triangle_strip_indices_to_triangle(struct ac_llvm_context *ctx,
808 LLVMValueRef is_odd,
809 LLVMValueRef flatshade_first,
810 LLVMValueRef index[3]);
811
812 #ifdef __cplusplus
813 }
814 #endif
815
816 #endif