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