ac: add ac_build_s_barrier
[mesa.git] / src / amd / common / 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/TargetMachine.h>
30 #include "compiler/nir/nir.h"
31 #include "amd_family.h"
32
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36
37 #define HAVE_32BIT_POINTERS (HAVE_LLVM >= 0x0700)
38
39 enum {
40 /* CONST is the only address space that selects SMEM loads */
41 AC_CONST_ADDR_SPACE = HAVE_LLVM >= 0x700 ? 4 : 2,
42 AC_LOCAL_ADDR_SPACE = 3,
43 AC_CONST_32BIT_ADDR_SPACE = 6, /* same as CONST, but the pointer type has 32 bits */
44 };
45
46 struct ac_llvm_flow;
47
48 struct ac_llvm_context {
49 LLVMContextRef context;
50 LLVMModuleRef module;
51 LLVMBuilderRef builder;
52
53 LLVMTypeRef voidt;
54 LLVMTypeRef i1;
55 LLVMTypeRef i8;
56 LLVMTypeRef i16;
57 LLVMTypeRef i32;
58 LLVMTypeRef i64;
59 LLVMTypeRef intptr;
60 LLVMTypeRef f16;
61 LLVMTypeRef f32;
62 LLVMTypeRef f64;
63 LLVMTypeRef v2i16;
64 LLVMTypeRef v2i32;
65 LLVMTypeRef v3i32;
66 LLVMTypeRef v4i32;
67 LLVMTypeRef v2f32;
68 LLVMTypeRef v4f32;
69 LLVMTypeRef v8i32;
70
71 LLVMValueRef i32_0;
72 LLVMValueRef i32_1;
73 LLVMValueRef i64_0;
74 LLVMValueRef i64_1;
75 LLVMValueRef f32_0;
76 LLVMValueRef f32_1;
77 LLVMValueRef f64_0;
78 LLVMValueRef f64_1;
79 LLVMValueRef i1true;
80 LLVMValueRef i1false;
81
82 struct ac_llvm_flow *flow;
83 unsigned flow_depth;
84 unsigned flow_depth_max;
85
86 unsigned range_md_kind;
87 unsigned invariant_load_md_kind;
88 unsigned uniform_md_kind;
89 unsigned fpmath_md_kind;
90 LLVMValueRef fpmath_md_2p5_ulp;
91 LLVMValueRef empty_md;
92
93 enum chip_class chip_class;
94 enum radeon_family family;
95
96 LLVMValueRef lds;
97 };
98
99 void
100 ac_llvm_context_init(struct ac_llvm_context *ctx,
101 enum chip_class chip_class, enum radeon_family family);
102
103 void
104 ac_llvm_context_dispose(struct ac_llvm_context *ctx);
105
106 int
107 ac_get_llvm_num_components(LLVMValueRef value);
108
109 int
110 ac_get_elem_bits(struct ac_llvm_context *ctx, LLVMTypeRef type);
111
112 LLVMValueRef
113 ac_llvm_extract_elem(struct ac_llvm_context *ac,
114 LLVMValueRef value,
115 int index);
116
117 unsigned ac_get_type_size(LLVMTypeRef type);
118
119 LLVMTypeRef ac_to_integer_type(struct ac_llvm_context *ctx, LLVMTypeRef t);
120 LLVMValueRef ac_to_integer(struct ac_llvm_context *ctx, LLVMValueRef v);
121 LLVMTypeRef ac_to_float_type(struct ac_llvm_context *ctx, LLVMTypeRef t);
122 LLVMValueRef ac_to_float(struct ac_llvm_context *ctx, LLVMValueRef v);
123
124 LLVMValueRef
125 ac_build_intrinsic(struct ac_llvm_context *ctx, const char *name,
126 LLVMTypeRef return_type, LLVMValueRef *params,
127 unsigned param_count, unsigned attrib_mask);
128
129 void ac_build_type_name_for_intr(LLVMTypeRef type, char *buf, unsigned bufsize);
130
131 LLVMValueRef
132 ac_build_phi(struct ac_llvm_context *ctx, LLVMTypeRef type,
133 unsigned count_incoming, LLVMValueRef *values,
134 LLVMBasicBlockRef *blocks);
135
136 void ac_build_s_barrier(struct ac_llvm_context *ctx);
137 void ac_build_optimization_barrier(struct ac_llvm_context *ctx,
138 LLVMValueRef *pvgpr);
139
140 LLVMValueRef ac_build_shader_clock(struct ac_llvm_context *ctx);
141
142 LLVMValueRef ac_build_ballot(struct ac_llvm_context *ctx, LLVMValueRef value);
143
144 LLVMValueRef ac_build_vote_all(struct ac_llvm_context *ctx, LLVMValueRef value);
145
146 LLVMValueRef ac_build_vote_any(struct ac_llvm_context *ctx, LLVMValueRef value);
147
148 LLVMValueRef ac_build_vote_eq(struct ac_llvm_context *ctx, LLVMValueRef value);
149
150 LLVMValueRef
151 ac_build_varying_gather_values(struct ac_llvm_context *ctx, LLVMValueRef *values,
152 unsigned value_count, unsigned component);
153
154 LLVMValueRef
155 ac_build_gather_values_extended(struct ac_llvm_context *ctx,
156 LLVMValueRef *values,
157 unsigned value_count,
158 unsigned value_stride,
159 bool load,
160 bool always_vector);
161 LLVMValueRef
162 ac_build_gather_values(struct ac_llvm_context *ctx,
163 LLVMValueRef *values,
164 unsigned value_count);
165 LLVMValueRef ac_build_expand_to_vec4(struct ac_llvm_context *ctx,
166 LLVMValueRef value,
167 unsigned num_channels);
168
169 LLVMValueRef
170 ac_build_fdiv(struct ac_llvm_context *ctx,
171 LLVMValueRef num,
172 LLVMValueRef den);
173
174 void
175 ac_prepare_cube_coords(struct ac_llvm_context *ctx,
176 bool is_deriv, bool is_array, bool is_lod,
177 LLVMValueRef *coords_arg,
178 LLVMValueRef *derivs_arg);
179
180
181 LLVMValueRef
182 ac_build_fs_interp(struct ac_llvm_context *ctx,
183 LLVMValueRef llvm_chan,
184 LLVMValueRef attr_number,
185 LLVMValueRef params,
186 LLVMValueRef i,
187 LLVMValueRef j);
188
189 LLVMValueRef
190 ac_build_fs_interp_mov(struct ac_llvm_context *ctx,
191 LLVMValueRef parameter,
192 LLVMValueRef llvm_chan,
193 LLVMValueRef attr_number,
194 LLVMValueRef params);
195
196 LLVMValueRef
197 ac_build_gep0(struct ac_llvm_context *ctx,
198 LLVMValueRef base_ptr,
199 LLVMValueRef index);
200
201 void
202 ac_build_indexed_store(struct ac_llvm_context *ctx,
203 LLVMValueRef base_ptr, LLVMValueRef index,
204 LLVMValueRef value);
205
206 LLVMValueRef ac_build_load(struct ac_llvm_context *ctx, LLVMValueRef base_ptr,
207 LLVMValueRef index);
208 LLVMValueRef ac_build_load_invariant(struct ac_llvm_context *ctx,
209 LLVMValueRef base_ptr, LLVMValueRef index);
210 LLVMValueRef ac_build_load_to_sgpr(struct ac_llvm_context *ctx,
211 LLVMValueRef base_ptr, LLVMValueRef index);
212
213 void
214 ac_build_buffer_store_dword(struct ac_llvm_context *ctx,
215 LLVMValueRef rsrc,
216 LLVMValueRef vdata,
217 unsigned num_channels,
218 LLVMValueRef voffset,
219 LLVMValueRef soffset,
220 unsigned inst_offset,
221 bool glc,
222 bool slc,
223 bool writeonly_memory,
224 bool swizzle_enable_hint);
225 LLVMValueRef
226 ac_build_buffer_load(struct ac_llvm_context *ctx,
227 LLVMValueRef rsrc,
228 int num_channels,
229 LLVMValueRef vindex,
230 LLVMValueRef voffset,
231 LLVMValueRef soffset,
232 unsigned inst_offset,
233 unsigned glc,
234 unsigned slc,
235 bool can_speculate,
236 bool allow_smem);
237
238 LLVMValueRef ac_build_buffer_load_format(struct ac_llvm_context *ctx,
239 LLVMValueRef rsrc,
240 LLVMValueRef vindex,
241 LLVMValueRef voffset,
242 unsigned num_channels,
243 bool glc,
244 bool can_speculate);
245
246 /* load_format that handles the stride & element count better if idxen is
247 * disabled by LLVM. */
248 LLVMValueRef ac_build_buffer_load_format_gfx9_safe(struct ac_llvm_context *ctx,
249 LLVMValueRef rsrc,
250 LLVMValueRef vindex,
251 LLVMValueRef voffset,
252 unsigned num_channels,
253 bool glc,
254 bool can_speculate);
255
256 LLVMValueRef
257 ac_build_tbuffer_load_short(struct ac_llvm_context *ctx,
258 LLVMValueRef rsrc,
259 LLVMValueRef vindex,
260 LLVMValueRef voffset,
261 LLVMValueRef soffset,
262 LLVMValueRef immoffset);
263
264 LLVMValueRef
265 ac_get_thread_id(struct ac_llvm_context *ctx);
266
267 #define AC_TID_MASK_TOP_LEFT 0xfffffffc
268 #define AC_TID_MASK_TOP 0xfffffffd
269 #define AC_TID_MASK_LEFT 0xfffffffe
270
271 LLVMValueRef
272 ac_build_ddxy(struct ac_llvm_context *ctx,
273 uint32_t mask,
274 int idx,
275 LLVMValueRef val);
276
277 #define AC_SENDMSG_GS 2
278 #define AC_SENDMSG_GS_DONE 3
279
280 #define AC_SENDMSG_GS_OP_NOP (0 << 4)
281 #define AC_SENDMSG_GS_OP_CUT (1 << 4)
282 #define AC_SENDMSG_GS_OP_EMIT (2 << 4)
283 #define AC_SENDMSG_GS_OP_EMIT_CUT (3 << 4)
284
285 void ac_build_sendmsg(struct ac_llvm_context *ctx,
286 uint32_t msg,
287 LLVMValueRef wave_id);
288
289 LLVMValueRef ac_build_imsb(struct ac_llvm_context *ctx,
290 LLVMValueRef arg,
291 LLVMTypeRef dst_type);
292
293 LLVMValueRef ac_build_umsb(struct ac_llvm_context *ctx,
294 LLVMValueRef arg,
295 LLVMTypeRef dst_type);
296 LLVMValueRef ac_build_fmin(struct ac_llvm_context *ctx, LLVMValueRef a,
297 LLVMValueRef b);
298 LLVMValueRef ac_build_fmax(struct ac_llvm_context *ctx, LLVMValueRef a,
299 LLVMValueRef b);
300 LLVMValueRef ac_build_imin(struct ac_llvm_context *ctx, LLVMValueRef a,
301 LLVMValueRef b);
302 LLVMValueRef ac_build_imax(struct ac_llvm_context *ctx, LLVMValueRef a,
303 LLVMValueRef b);
304 LLVMValueRef ac_build_umin(struct ac_llvm_context *ctx, LLVMValueRef a, LLVMValueRef b);
305 LLVMValueRef ac_build_clamp(struct ac_llvm_context *ctx, LLVMValueRef value);
306
307 struct ac_export_args {
308 LLVMValueRef out[4];
309 unsigned target;
310 unsigned enabled_channels;
311 bool compr;
312 bool done;
313 bool valid_mask;
314 };
315
316 void ac_build_export(struct ac_llvm_context *ctx, struct ac_export_args *a);
317
318 void ac_build_export_null(struct ac_llvm_context *ctx);
319
320 enum ac_image_opcode {
321 ac_image_sample,
322 ac_image_gather4,
323 ac_image_load,
324 ac_image_load_mip,
325 ac_image_store,
326 ac_image_store_mip,
327 ac_image_get_lod,
328 ac_image_get_resinfo,
329 ac_image_atomic,
330 ac_image_atomic_cmpswap,
331 };
332
333 enum ac_atomic_op {
334 ac_atomic_swap,
335 ac_atomic_add,
336 ac_atomic_sub,
337 ac_atomic_smin,
338 ac_atomic_umin,
339 ac_atomic_smax,
340 ac_atomic_umax,
341 ac_atomic_and,
342 ac_atomic_or,
343 ac_atomic_xor,
344 };
345
346 enum ac_image_dim {
347 ac_image_1d,
348 ac_image_2d,
349 ac_image_3d,
350 ac_image_cube, // includes cube arrays
351 ac_image_1darray,
352 ac_image_2darray,
353 ac_image_2dmsaa,
354 ac_image_2darraymsaa,
355 };
356
357 /* These cache policy bits match the definitions used by the LLVM intrinsics. */
358 enum ac_image_cache_policy {
359 ac_glc = 1 << 0,
360 ac_slc = 1 << 1,
361 };
362
363 struct ac_image_args {
364 enum ac_image_opcode opcode : 4;
365 enum ac_atomic_op atomic : 4; /* for the ac_image_atomic opcode */
366 enum ac_image_dim dim : 3;
367 unsigned dmask : 4;
368 unsigned cache_policy : 2;
369 bool unorm : 1;
370 bool level_zero : 1;
371 unsigned attributes; /* additional call-site specific AC_FUNC_ATTRs */
372
373 LLVMValueRef resource;
374 LLVMValueRef sampler;
375 LLVMValueRef data[2]; /* data[0] is source data (vector); data[1] is cmp for cmpswap */
376 LLVMValueRef offset;
377 LLVMValueRef bias;
378 LLVMValueRef compare;
379 LLVMValueRef derivs[6];
380 LLVMValueRef coords[4];
381 LLVMValueRef lod; // also used by ac_image_get_resinfo
382 };
383
384 LLVMValueRef ac_build_image_opcode(struct ac_llvm_context *ctx,
385 struct ac_image_args *a);
386 LLVMValueRef ac_build_cvt_pkrtz_f16(struct ac_llvm_context *ctx,
387 LLVMValueRef args[2]);
388 LLVMValueRef ac_build_cvt_pknorm_i16(struct ac_llvm_context *ctx,
389 LLVMValueRef args[2]);
390 LLVMValueRef ac_build_cvt_pknorm_u16(struct ac_llvm_context *ctx,
391 LLVMValueRef args[2]);
392 LLVMValueRef ac_build_cvt_pk_i16(struct ac_llvm_context *ctx,
393 LLVMValueRef args[2], unsigned bits, bool hi);
394 LLVMValueRef ac_build_cvt_pk_u16(struct ac_llvm_context *ctx,
395 LLVMValueRef args[2], unsigned bits, bool hi);
396 LLVMValueRef ac_build_wqm_vote(struct ac_llvm_context *ctx, LLVMValueRef i1);
397 void ac_build_kill_if_false(struct ac_llvm_context *ctx, LLVMValueRef i1);
398 LLVMValueRef ac_build_bfe(struct ac_llvm_context *ctx, LLVMValueRef input,
399 LLVMValueRef offset, LLVMValueRef width,
400 bool is_signed);
401
402 void ac_build_waitcnt(struct ac_llvm_context *ctx, unsigned simm16);
403
404 LLVMValueRef ac_build_fract(struct ac_llvm_context *ctx, LLVMValueRef src0,
405 unsigned bitsize);
406
407 LLVMValueRef ac_build_isign(struct ac_llvm_context *ctx, LLVMValueRef src0,
408 unsigned bitsize);
409
410 LLVMValueRef ac_build_fsign(struct ac_llvm_context *ctx, LLVMValueRef src0,
411 unsigned bitsize);
412
413 void ac_optimize_vs_outputs(struct ac_llvm_context *ac,
414 LLVMValueRef main_fn,
415 uint8_t *vs_output_param_offset,
416 uint32_t num_outputs,
417 uint8_t *num_param_exports);
418 void ac_init_exec_full_mask(struct ac_llvm_context *ctx);
419
420 void ac_declare_lds_as_pointer(struct ac_llvm_context *ac);
421 LLVMValueRef ac_lds_load(struct ac_llvm_context *ctx,
422 LLVMValueRef dw_addr);
423 void ac_lds_store(struct ac_llvm_context *ctx,
424 LLVMValueRef dw_addr, LLVMValueRef value);
425
426 LLVMValueRef ac_find_lsb(struct ac_llvm_context *ctx,
427 LLVMTypeRef dst_type,
428 LLVMValueRef src0);
429
430 LLVMTypeRef ac_array_in_const_addr_space(LLVMTypeRef elem_type);
431 LLVMTypeRef ac_array_in_const32_addr_space(LLVMTypeRef elem_type);
432
433 void ac_build_bgnloop(struct ac_llvm_context *ctx, int lable_id);
434 void ac_build_break(struct ac_llvm_context *ctx);
435 void ac_build_continue(struct ac_llvm_context *ctx);
436 void ac_build_else(struct ac_llvm_context *ctx, int lable_id);
437 void ac_build_endif(struct ac_llvm_context *ctx, int lable_id);
438 void ac_build_endloop(struct ac_llvm_context *ctx, int lable_id);
439 void ac_build_if(struct ac_llvm_context *ctx, LLVMValueRef value,
440 int lable_id);
441 void ac_build_uif(struct ac_llvm_context *ctx, LLVMValueRef value,
442 int lable_id);
443
444 LLVMValueRef ac_build_alloca(struct ac_llvm_context *ac, LLVMTypeRef type,
445 const char *name);
446 LLVMValueRef ac_build_alloca_undef(struct ac_llvm_context *ac, LLVMTypeRef type,
447 const char *name);
448
449 LLVMValueRef ac_cast_ptr(struct ac_llvm_context *ctx, LLVMValueRef ptr,
450 LLVMTypeRef type);
451
452 LLVMValueRef ac_trim_vector(struct ac_llvm_context *ctx, LLVMValueRef value,
453 unsigned count);
454
455 LLVMValueRef ac_unpack_param(struct ac_llvm_context *ctx, LLVMValueRef param,
456 unsigned rshift, unsigned bitwidth);
457
458 void ac_apply_fmask_to_sample(struct ac_llvm_context *ac, LLVMValueRef fmask,
459 LLVMValueRef *addr, bool is_array_tex);
460
461 LLVMValueRef
462 ac_build_ds_swizzle(struct ac_llvm_context *ctx, LLVMValueRef src, unsigned mask);
463
464 LLVMValueRef
465 ac_build_readlane(struct ac_llvm_context *ctx, LLVMValueRef src, LLVMValueRef lane);
466
467 LLVMValueRef
468 ac_build_writelane(struct ac_llvm_context *ctx, LLVMValueRef src, LLVMValueRef value, LLVMValueRef lane);
469
470 LLVMValueRef
471 ac_build_mbcnt(struct ac_llvm_context *ctx, LLVMValueRef mask);
472
473 LLVMValueRef
474 ac_build_inclusive_scan(struct ac_llvm_context *ctx, LLVMValueRef src, nir_op op);
475
476 LLVMValueRef
477 ac_build_exclusive_scan(struct ac_llvm_context *ctx, LLVMValueRef src, nir_op op);
478
479 LLVMValueRef
480 ac_build_reduce(struct ac_llvm_context *ctx, LLVMValueRef src, nir_op op, unsigned cluster_size);
481
482 LLVMValueRef
483 ac_build_quad_swizzle(struct ac_llvm_context *ctx, LLVMValueRef src,
484 unsigned lane0, unsigned lane1, unsigned lane2, unsigned lane3);
485
486 LLVMValueRef
487 ac_build_shuffle(struct ac_llvm_context *ctx, LLVMValueRef src, LLVMValueRef index);
488
489 #ifdef __cplusplus
490 }
491 #endif
492
493 #endif