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