ac/nir: move cast_ptr() to ac_llvm_build.c
[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
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, LLVMContextRef context,
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_optimization_barrier(struct ac_llvm_context *ctx,
137 LLVMValueRef *pvgpr);
138
139 LLVMValueRef ac_build_shader_clock(struct ac_llvm_context *ctx);
140
141 LLVMValueRef ac_build_ballot(struct ac_llvm_context *ctx, LLVMValueRef value);
142
143 LLVMValueRef ac_build_vote_all(struct ac_llvm_context *ctx, LLVMValueRef value);
144
145 LLVMValueRef ac_build_vote_any(struct ac_llvm_context *ctx, LLVMValueRef value);
146
147 LLVMValueRef ac_build_vote_eq(struct ac_llvm_context *ctx, LLVMValueRef value);
148
149 LLVMValueRef
150 ac_build_varying_gather_values(struct ac_llvm_context *ctx, LLVMValueRef *values,
151 unsigned value_count, unsigned component);
152
153 LLVMValueRef
154 ac_build_gather_values_extended(struct ac_llvm_context *ctx,
155 LLVMValueRef *values,
156 unsigned value_count,
157 unsigned value_stride,
158 bool load,
159 bool always_vector);
160 LLVMValueRef
161 ac_build_gather_values(struct ac_llvm_context *ctx,
162 LLVMValueRef *values,
163 unsigned value_count);
164 LLVMValueRef ac_build_expand_to_vec4(struct ac_llvm_context *ctx,
165 LLVMValueRef value,
166 unsigned num_channels);
167
168 LLVMValueRef
169 ac_build_fdiv(struct ac_llvm_context *ctx,
170 LLVMValueRef num,
171 LLVMValueRef den);
172
173 void
174 ac_prepare_cube_coords(struct ac_llvm_context *ctx,
175 bool is_deriv, bool is_array, bool is_lod,
176 LLVMValueRef *coords_arg,
177 LLVMValueRef *derivs_arg);
178
179
180 LLVMValueRef
181 ac_build_fs_interp(struct ac_llvm_context *ctx,
182 LLVMValueRef llvm_chan,
183 LLVMValueRef attr_number,
184 LLVMValueRef params,
185 LLVMValueRef i,
186 LLVMValueRef j);
187
188 LLVMValueRef
189 ac_build_fs_interp_mov(struct ac_llvm_context *ctx,
190 LLVMValueRef parameter,
191 LLVMValueRef llvm_chan,
192 LLVMValueRef attr_number,
193 LLVMValueRef params);
194
195 LLVMValueRef
196 ac_build_gep0(struct ac_llvm_context *ctx,
197 LLVMValueRef base_ptr,
198 LLVMValueRef index);
199
200 void
201 ac_build_indexed_store(struct ac_llvm_context *ctx,
202 LLVMValueRef base_ptr, LLVMValueRef index,
203 LLVMValueRef value);
204
205 LLVMValueRef ac_build_load(struct ac_llvm_context *ctx, LLVMValueRef base_ptr,
206 LLVMValueRef index);
207 LLVMValueRef ac_build_load_invariant(struct ac_llvm_context *ctx,
208 LLVMValueRef base_ptr, LLVMValueRef index);
209 LLVMValueRef ac_build_load_to_sgpr(struct ac_llvm_context *ctx,
210 LLVMValueRef base_ptr, LLVMValueRef index);
211
212 void
213 ac_build_buffer_store_dword(struct ac_llvm_context *ctx,
214 LLVMValueRef rsrc,
215 LLVMValueRef vdata,
216 unsigned num_channels,
217 LLVMValueRef voffset,
218 LLVMValueRef soffset,
219 unsigned inst_offset,
220 bool glc,
221 bool slc,
222 bool writeonly_memory,
223 bool swizzle_enable_hint);
224 LLVMValueRef
225 ac_build_buffer_load(struct ac_llvm_context *ctx,
226 LLVMValueRef rsrc,
227 int num_channels,
228 LLVMValueRef vindex,
229 LLVMValueRef voffset,
230 LLVMValueRef soffset,
231 unsigned inst_offset,
232 unsigned glc,
233 unsigned slc,
234 bool can_speculate,
235 bool allow_smem);
236
237 LLVMValueRef ac_build_buffer_load_format(struct ac_llvm_context *ctx,
238 LLVMValueRef rsrc,
239 LLVMValueRef vindex,
240 LLVMValueRef voffset,
241 unsigned num_channels,
242 bool glc,
243 bool can_speculate);
244
245 LLVMValueRef
246 ac_get_thread_id(struct ac_llvm_context *ctx);
247
248 #define AC_TID_MASK_TOP_LEFT 0xfffffffc
249 #define AC_TID_MASK_TOP 0xfffffffd
250 #define AC_TID_MASK_LEFT 0xfffffffe
251
252 LLVMValueRef
253 ac_build_ddxy(struct ac_llvm_context *ctx,
254 uint32_t mask,
255 int idx,
256 LLVMValueRef val);
257
258 #define AC_SENDMSG_GS 2
259 #define AC_SENDMSG_GS_DONE 3
260
261 #define AC_SENDMSG_GS_OP_NOP (0 << 4)
262 #define AC_SENDMSG_GS_OP_CUT (1 << 4)
263 #define AC_SENDMSG_GS_OP_EMIT (2 << 4)
264 #define AC_SENDMSG_GS_OP_EMIT_CUT (3 << 4)
265
266 void ac_build_sendmsg(struct ac_llvm_context *ctx,
267 uint32_t msg,
268 LLVMValueRef wave_id);
269
270 LLVMValueRef ac_build_imsb(struct ac_llvm_context *ctx,
271 LLVMValueRef arg,
272 LLVMTypeRef dst_type);
273
274 LLVMValueRef ac_build_umsb(struct ac_llvm_context *ctx,
275 LLVMValueRef arg,
276 LLVMTypeRef dst_type);
277 LLVMValueRef ac_build_fmin(struct ac_llvm_context *ctx, LLVMValueRef a,
278 LLVMValueRef b);
279 LLVMValueRef ac_build_fmax(struct ac_llvm_context *ctx, LLVMValueRef a,
280 LLVMValueRef b);
281 LLVMValueRef ac_build_imin(struct ac_llvm_context *ctx, LLVMValueRef a,
282 LLVMValueRef b);
283 LLVMValueRef ac_build_imax(struct ac_llvm_context *ctx, LLVMValueRef a,
284 LLVMValueRef b);
285 LLVMValueRef ac_build_umin(struct ac_llvm_context *ctx, LLVMValueRef a, LLVMValueRef b);
286 LLVMValueRef ac_build_clamp(struct ac_llvm_context *ctx, LLVMValueRef value);
287
288 struct ac_export_args {
289 LLVMValueRef out[4];
290 unsigned target;
291 unsigned enabled_channels;
292 bool compr;
293 bool done;
294 bool valid_mask;
295 };
296
297 void ac_build_export(struct ac_llvm_context *ctx, struct ac_export_args *a);
298
299 void ac_build_export_null(struct ac_llvm_context *ctx);
300
301 enum ac_image_opcode {
302 ac_image_sample,
303 ac_image_gather4,
304 ac_image_load,
305 ac_image_load_mip,
306 ac_image_get_lod,
307 ac_image_get_resinfo,
308 };
309
310 struct ac_image_args {
311 enum ac_image_opcode opcode;
312 bool level_zero;
313 bool bias;
314 bool lod;
315 bool deriv;
316 bool compare;
317 bool offset;
318
319 LLVMValueRef resource;
320 LLVMValueRef sampler;
321 LLVMValueRef addr;
322 unsigned dmask;
323 bool unorm;
324 bool da;
325 };
326
327 LLVMValueRef ac_build_image_opcode(struct ac_llvm_context *ctx,
328 struct ac_image_args *a);
329 LLVMValueRef ac_build_cvt_pkrtz_f16(struct ac_llvm_context *ctx,
330 LLVMValueRef args[2]);
331 LLVMValueRef ac_build_cvt_pknorm_i16(struct ac_llvm_context *ctx,
332 LLVMValueRef args[2]);
333 LLVMValueRef ac_build_cvt_pknorm_u16(struct ac_llvm_context *ctx,
334 LLVMValueRef args[2]);
335 LLVMValueRef ac_build_cvt_pk_i16(struct ac_llvm_context *ctx,
336 LLVMValueRef args[2], unsigned bits, bool hi);
337 LLVMValueRef ac_build_cvt_pk_u16(struct ac_llvm_context *ctx,
338 LLVMValueRef args[2], unsigned bits, bool hi);
339 LLVMValueRef ac_build_wqm_vote(struct ac_llvm_context *ctx, LLVMValueRef i1);
340 void ac_build_kill_if_false(struct ac_llvm_context *ctx, LLVMValueRef i1);
341 LLVMValueRef ac_build_bfe(struct ac_llvm_context *ctx, LLVMValueRef input,
342 LLVMValueRef offset, LLVMValueRef width,
343 bool is_signed);
344
345 void ac_build_waitcnt(struct ac_llvm_context *ctx, unsigned simm16);
346
347 LLVMValueRef ac_build_fract(struct ac_llvm_context *ctx, LLVMValueRef src0,
348 unsigned bitsize);
349
350 LLVMValueRef ac_build_isign(struct ac_llvm_context *ctx, LLVMValueRef src0,
351 unsigned bitsize);
352
353 LLVMValueRef ac_build_fsign(struct ac_llvm_context *ctx, LLVMValueRef src0,
354 unsigned bitsize);
355
356 void ac_get_image_intr_name(const char *base_name,
357 LLVMTypeRef data_type,
358 LLVMTypeRef coords_type,
359 LLVMTypeRef rsrc_type,
360 char *out_name, unsigned out_len);
361
362 void ac_optimize_vs_outputs(struct ac_llvm_context *ac,
363 LLVMValueRef main_fn,
364 uint8_t *vs_output_param_offset,
365 uint32_t num_outputs,
366 uint8_t *num_param_exports);
367 void ac_init_exec_full_mask(struct ac_llvm_context *ctx);
368
369 void ac_declare_lds_as_pointer(struct ac_llvm_context *ac);
370 LLVMValueRef ac_lds_load(struct ac_llvm_context *ctx,
371 LLVMValueRef dw_addr);
372 void ac_lds_store(struct ac_llvm_context *ctx,
373 LLVMValueRef dw_addr, LLVMValueRef value);
374
375 LLVMValueRef ac_find_lsb(struct ac_llvm_context *ctx,
376 LLVMTypeRef dst_type,
377 LLVMValueRef src0);
378
379 LLVMTypeRef ac_array_in_const_addr_space(LLVMTypeRef elem_type);
380 LLVMTypeRef ac_array_in_const32_addr_space(LLVMTypeRef elem_type);
381
382 void ac_build_bgnloop(struct ac_llvm_context *ctx, int lable_id);
383 void ac_build_break(struct ac_llvm_context *ctx);
384 void ac_build_continue(struct ac_llvm_context *ctx);
385 void ac_build_else(struct ac_llvm_context *ctx, int lable_id);
386 void ac_build_endif(struct ac_llvm_context *ctx, int lable_id);
387 void ac_build_endloop(struct ac_llvm_context *ctx, int lable_id);
388 void ac_build_if(struct ac_llvm_context *ctx, LLVMValueRef value,
389 int lable_id);
390 void ac_build_uif(struct ac_llvm_context *ctx, LLVMValueRef value,
391 int lable_id);
392
393 LLVMValueRef ac_build_alloca(struct ac_llvm_context *ac, LLVMTypeRef type,
394 const char *name);
395 LLVMValueRef ac_build_alloca_undef(struct ac_llvm_context *ac, LLVMTypeRef type,
396 const char *name);
397
398 LLVMValueRef ac_cast_ptr(struct ac_llvm_context *ctx, LLVMValueRef ptr,
399 LLVMTypeRef type);
400
401 #ifdef __cplusplus
402 }
403 #endif
404
405 #endif