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