ac/nir: use llvm.amdgcn.rcp in ac_build_fdiv()
[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 };
565
566 LLVMValueRef ac_build_image_opcode(struct ac_llvm_context *ctx,
567 struct ac_image_args *a);
568 LLVMValueRef ac_build_image_get_sample_count(struct ac_llvm_context *ctx,
569 LLVMValueRef rsrc);
570 LLVMValueRef ac_build_cvt_pkrtz_f16(struct ac_llvm_context *ctx,
571 LLVMValueRef args[2]);
572 LLVMValueRef ac_build_cvt_pknorm_i16(struct ac_llvm_context *ctx,
573 LLVMValueRef args[2]);
574 LLVMValueRef ac_build_cvt_pknorm_u16(struct ac_llvm_context *ctx,
575 LLVMValueRef args[2]);
576 LLVMValueRef ac_build_cvt_pk_i16(struct ac_llvm_context *ctx,
577 LLVMValueRef args[2], unsigned bits, bool hi);
578 LLVMValueRef ac_build_cvt_pk_u16(struct ac_llvm_context *ctx,
579 LLVMValueRef args[2], unsigned bits, bool hi);
580 LLVMValueRef ac_build_wqm_vote(struct ac_llvm_context *ctx, LLVMValueRef i1);
581 void ac_build_kill_if_false(struct ac_llvm_context *ctx, LLVMValueRef i1);
582 LLVMValueRef ac_build_bfe(struct ac_llvm_context *ctx, LLVMValueRef input,
583 LLVMValueRef offset, LLVMValueRef width,
584 bool is_signed);
585 LLVMValueRef ac_build_imad(struct ac_llvm_context *ctx, LLVMValueRef s0,
586 LLVMValueRef s1, LLVMValueRef s2);
587 LLVMValueRef ac_build_fmad(struct ac_llvm_context *ctx, LLVMValueRef s0,
588 LLVMValueRef s1, LLVMValueRef s2);
589
590 void ac_build_waitcnt(struct ac_llvm_context *ctx, unsigned wait_flags);
591
592 LLVMValueRef ac_build_fract(struct ac_llvm_context *ctx, LLVMValueRef src0,
593 unsigned bitsize);
594
595 LLVMValueRef ac_build_fmed3(struct ac_llvm_context *ctx, LLVMValueRef src0,
596 LLVMValueRef src1, LLVMValueRef src2,
597 unsigned bitsize);
598
599 LLVMValueRef ac_build_isign(struct ac_llvm_context *ctx, LLVMValueRef src0,
600 unsigned bitsize);
601
602 LLVMValueRef ac_build_fsign(struct ac_llvm_context *ctx, LLVMValueRef src0,
603 unsigned bitsize);
604
605 LLVMValueRef ac_build_bit_count(struct ac_llvm_context *ctx, LLVMValueRef src0);
606
607 LLVMValueRef ac_build_bitfield_reverse(struct ac_llvm_context *ctx,
608 LLVMValueRef src0);
609
610 void ac_optimize_vs_outputs(struct ac_llvm_context *ac,
611 LLVMValueRef main_fn,
612 uint8_t *vs_output_param_offset,
613 uint32_t num_outputs,
614 uint8_t *num_param_exports);
615 void ac_init_exec_full_mask(struct ac_llvm_context *ctx);
616
617 void ac_declare_lds_as_pointer(struct ac_llvm_context *ac);
618 LLVMValueRef ac_lds_load(struct ac_llvm_context *ctx,
619 LLVMValueRef dw_addr);
620 void ac_lds_store(struct ac_llvm_context *ctx,
621 LLVMValueRef dw_addr, LLVMValueRef value);
622
623 LLVMValueRef ac_find_lsb(struct ac_llvm_context *ctx,
624 LLVMTypeRef dst_type,
625 LLVMValueRef src0);
626
627 LLVMTypeRef ac_array_in_const_addr_space(LLVMTypeRef elem_type);
628 LLVMTypeRef ac_array_in_const32_addr_space(LLVMTypeRef elem_type);
629
630 void ac_build_bgnloop(struct ac_llvm_context *ctx, int lable_id);
631 void ac_build_break(struct ac_llvm_context *ctx);
632 void ac_build_continue(struct ac_llvm_context *ctx);
633 void ac_build_else(struct ac_llvm_context *ctx, int lable_id);
634 void ac_build_endif(struct ac_llvm_context *ctx, int lable_id);
635 void ac_build_endloop(struct ac_llvm_context *ctx, int lable_id);
636 void ac_build_ifcc(struct ac_llvm_context *ctx, LLVMValueRef cond, int label_id);
637 void ac_build_if(struct ac_llvm_context *ctx, LLVMValueRef value,
638 int lable_id);
639 void ac_build_uif(struct ac_llvm_context *ctx, LLVMValueRef value,
640 int lable_id);
641
642 LLVMValueRef ac_build_alloca(struct ac_llvm_context *ac, LLVMTypeRef type,
643 const char *name);
644 LLVMValueRef ac_build_alloca_undef(struct ac_llvm_context *ac, LLVMTypeRef type,
645 const char *name);
646
647 LLVMValueRef ac_cast_ptr(struct ac_llvm_context *ctx, LLVMValueRef ptr,
648 LLVMTypeRef type);
649
650 LLVMValueRef ac_trim_vector(struct ac_llvm_context *ctx, LLVMValueRef value,
651 unsigned count);
652
653 LLVMValueRef ac_unpack_param(struct ac_llvm_context *ctx, LLVMValueRef param,
654 unsigned rshift, unsigned bitwidth);
655
656 void ac_apply_fmask_to_sample(struct ac_llvm_context *ac, LLVMValueRef fmask,
657 LLVMValueRef *addr, bool is_array_tex);
658
659 LLVMValueRef
660 ac_build_ds_swizzle(struct ac_llvm_context *ctx, LLVMValueRef src, unsigned mask);
661
662 LLVMValueRef ac_build_readlane_no_opt_barrier(struct ac_llvm_context *ctx,
663 LLVMValueRef src, LLVMValueRef lane);
664
665 LLVMValueRef
666 ac_build_readlane(struct ac_llvm_context *ctx, LLVMValueRef src, LLVMValueRef lane);
667
668 LLVMValueRef
669 ac_build_writelane(struct ac_llvm_context *ctx, LLVMValueRef src, LLVMValueRef value, LLVMValueRef lane);
670
671 LLVMValueRef
672 ac_build_mbcnt(struct ac_llvm_context *ctx, LLVMValueRef mask);
673
674 LLVMValueRef
675 ac_build_inclusive_scan(struct ac_llvm_context *ctx, LLVMValueRef src, nir_op op);
676
677 LLVMValueRef
678 ac_build_exclusive_scan(struct ac_llvm_context *ctx, LLVMValueRef src, nir_op op);
679
680 LLVMValueRef
681 ac_build_reduce(struct ac_llvm_context *ctx, LLVMValueRef src, nir_op op, unsigned cluster_size);
682
683 /**
684 * Common arguments for a scan/reduce operation that accumulates per-wave
685 * values across an entire workgroup, while respecting the order of waves.
686 */
687 struct ac_wg_scan {
688 bool enable_reduce;
689 bool enable_exclusive;
690 bool enable_inclusive;
691 nir_op op;
692 LLVMValueRef src; /* clobbered! */
693 LLVMValueRef result_reduce;
694 LLVMValueRef result_exclusive;
695 LLVMValueRef result_inclusive;
696 LLVMValueRef extra;
697 LLVMValueRef waveidx;
698 LLVMValueRef numwaves; /* only needed for "reduce" operations */
699
700 /* T addrspace(LDS) pointer to the same type as value, at least maxwaves entries */
701 LLVMValueRef scratch;
702 unsigned maxwaves;
703 };
704
705 void
706 ac_build_wg_wavescan_top(struct ac_llvm_context *ctx, struct ac_wg_scan *ws);
707 void
708 ac_build_wg_wavescan_bottom(struct ac_llvm_context *ctx, struct ac_wg_scan *ws);
709 void
710 ac_build_wg_wavescan(struct ac_llvm_context *ctx, struct ac_wg_scan *ws);
711
712 void
713 ac_build_wg_scan_top(struct ac_llvm_context *ctx, struct ac_wg_scan *ws);
714 void
715 ac_build_wg_scan_bottom(struct ac_llvm_context *ctx, struct ac_wg_scan *ws);
716 void
717 ac_build_wg_scan(struct ac_llvm_context *ctx, struct ac_wg_scan *ws);
718
719 LLVMValueRef
720 ac_build_quad_swizzle(struct ac_llvm_context *ctx, LLVMValueRef src,
721 unsigned lane0, unsigned lane1, unsigned lane2, unsigned lane3);
722
723 LLVMValueRef
724 ac_build_shuffle(struct ac_llvm_context *ctx, LLVMValueRef src, LLVMValueRef index);
725
726 LLVMValueRef
727 ac_build_frexp_exp(struct ac_llvm_context *ctx, LLVMValueRef src0,
728 unsigned bitsize);
729
730 LLVMValueRef
731 ac_build_frexp_mant(struct ac_llvm_context *ctx, LLVMValueRef src0,
732 unsigned bitsize);
733
734 LLVMValueRef
735 ac_build_canonicalize(struct ac_llvm_context *ctx, LLVMValueRef src0,
736 unsigned bitsize);
737
738 LLVMValueRef
739 ac_build_ddxy_interp(struct ac_llvm_context *ctx, LLVMValueRef interp_ij);
740
741 LLVMValueRef
742 ac_build_load_helper_invocation(struct ac_llvm_context *ctx);
743
744 LLVMValueRef
745 ac_build_is_helper_invocation(struct ac_llvm_context *ctx);
746
747 LLVMValueRef ac_build_call(struct ac_llvm_context *ctx, LLVMValueRef func,
748 LLVMValueRef *args, unsigned num_args);
749
750 LLVMValueRef ac_build_atomic_rmw(struct ac_llvm_context *ctx, LLVMAtomicRMWBinOp op,
751 LLVMValueRef ptr, LLVMValueRef val,
752 const char *sync_scope);
753
754 LLVMValueRef ac_build_atomic_cmp_xchg(struct ac_llvm_context *ctx, LLVMValueRef ptr,
755 LLVMValueRef cmp, LLVMValueRef val,
756 const char *sync_scope);
757
758 void
759 ac_export_mrt_z(struct ac_llvm_context *ctx, LLVMValueRef depth,
760 LLVMValueRef stencil, LLVMValueRef samplemask,
761 struct ac_export_args *args);
762
763 void ac_build_sendmsg_gs_alloc_req(struct ac_llvm_context *ctx, LLVMValueRef wave_id,
764 LLVMValueRef vtx_cnt, LLVMValueRef prim_cnt);
765
766 struct ac_ngg_prim {
767 unsigned num_vertices;
768 LLVMValueRef isnull;
769 LLVMValueRef index[3];
770 LLVMValueRef edgeflag[3];
771 LLVMValueRef passthrough;
772 };
773
774 LLVMValueRef ac_pack_prim_export(struct ac_llvm_context *ctx,
775 const struct ac_ngg_prim *prim);
776 void ac_build_export_prim(struct ac_llvm_context *ctx,
777 const struct ac_ngg_prim *prim);
778
779 static inline LLVMValueRef
780 ac_get_arg(struct ac_llvm_context *ctx, struct ac_arg arg)
781 {
782 assert(arg.used);
783 return LLVMGetParam(ctx->main_function, arg.arg_index);
784 }
785
786 enum ac_llvm_calling_convention {
787 AC_LLVM_AMDGPU_VS = 87,
788 AC_LLVM_AMDGPU_GS = 88,
789 AC_LLVM_AMDGPU_PS = 89,
790 AC_LLVM_AMDGPU_CS = 90,
791 AC_LLVM_AMDGPU_HS = 93,
792 };
793
794 LLVMValueRef ac_build_main(const struct ac_shader_args *args,
795 struct ac_llvm_context *ctx,
796 enum ac_llvm_calling_convention convention,
797 const char *name, LLVMTypeRef ret_type,
798 LLVMModuleRef module);
799 void ac_build_s_endpgm(struct ac_llvm_context *ctx);
800
801 LLVMValueRef ac_prefix_bitcount(struct ac_llvm_context *ctx,
802 LLVMValueRef mask, LLVMValueRef index);
803 LLVMValueRef ac_prefix_bitcount_2x64(struct ac_llvm_context *ctx,
804 LLVMValueRef mask[2], LLVMValueRef index);
805 void ac_build_triangle_strip_indices_to_triangle(struct ac_llvm_context *ctx,
806 LLVMValueRef is_odd,
807 LLVMValueRef flatshade_first,
808 LLVMValueRef index[3]);
809
810 #ifdef __cplusplus
811 }
812 #endif
813
814 #endif