ac: add ac_build_export_null() helper
[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 enum {
38 AC_CONST_ADDR_SPACE = 2, /* CONST is the only address space that selects SMEM loads */
39 AC_LOCAL_ADDR_SPACE = 3,
40 };
41
42 struct ac_llvm_context {
43 LLVMContextRef context;
44 LLVMModuleRef module;
45 LLVMBuilderRef builder;
46
47 LLVMTypeRef voidt;
48 LLVMTypeRef i1;
49 LLVMTypeRef i8;
50 LLVMTypeRef i16;
51 LLVMTypeRef i32;
52 LLVMTypeRef i64;
53 LLVMTypeRef f16;
54 LLVMTypeRef f32;
55 LLVMTypeRef f64;
56 LLVMTypeRef v2i16;
57 LLVMTypeRef v2i32;
58 LLVMTypeRef v3i32;
59 LLVMTypeRef v4i32;
60 LLVMTypeRef v2f32;
61 LLVMTypeRef v4f32;
62 LLVMTypeRef v8i32;
63
64 LLVMValueRef i32_0;
65 LLVMValueRef i32_1;
66 LLVMValueRef i64_0;
67 LLVMValueRef i64_1;
68 LLVMValueRef f32_0;
69 LLVMValueRef f32_1;
70 LLVMValueRef f64_0;
71 LLVMValueRef f64_1;
72 LLVMValueRef i1true;
73 LLVMValueRef i1false;
74
75 unsigned range_md_kind;
76 unsigned invariant_load_md_kind;
77 unsigned uniform_md_kind;
78 unsigned fpmath_md_kind;
79 LLVMValueRef fpmath_md_2p5_ulp;
80 LLVMValueRef empty_md;
81
82 enum chip_class chip_class;
83 enum radeon_family family;
84
85 LLVMValueRef lds;
86 };
87
88 void
89 ac_llvm_context_init(struct ac_llvm_context *ctx, LLVMContextRef context,
90 enum chip_class chip_class, enum radeon_family family);
91
92 int
93 ac_get_llvm_num_components(LLVMValueRef value);
94
95 LLVMValueRef
96 ac_llvm_extract_elem(struct ac_llvm_context *ac,
97 LLVMValueRef value,
98 int index);
99
100 unsigned ac_get_type_size(LLVMTypeRef type);
101
102 LLVMTypeRef ac_to_integer_type(struct ac_llvm_context *ctx, LLVMTypeRef t);
103 LLVMValueRef ac_to_integer(struct ac_llvm_context *ctx, LLVMValueRef v);
104 LLVMTypeRef ac_to_float_type(struct ac_llvm_context *ctx, LLVMTypeRef t);
105 LLVMValueRef ac_to_float(struct ac_llvm_context *ctx, LLVMValueRef v);
106
107 LLVMValueRef
108 ac_build_intrinsic(struct ac_llvm_context *ctx, const char *name,
109 LLVMTypeRef return_type, LLVMValueRef *params,
110 unsigned param_count, unsigned attrib_mask);
111
112 void ac_build_type_name_for_intr(LLVMTypeRef type, char *buf, unsigned bufsize);
113
114 LLVMValueRef
115 ac_build_phi(struct ac_llvm_context *ctx, LLVMTypeRef type,
116 unsigned count_incoming, LLVMValueRef *values,
117 LLVMBasicBlockRef *blocks);
118
119 void ac_build_optimization_barrier(struct ac_llvm_context *ctx,
120 LLVMValueRef *pvgpr);
121
122 LLVMValueRef ac_build_shader_clock(struct ac_llvm_context *ctx);
123
124 LLVMValueRef ac_build_ballot(struct ac_llvm_context *ctx, LLVMValueRef value);
125
126 LLVMValueRef ac_build_vote_all(struct ac_llvm_context *ctx, LLVMValueRef value);
127
128 LLVMValueRef ac_build_vote_any(struct ac_llvm_context *ctx, LLVMValueRef value);
129
130 LLVMValueRef ac_build_vote_eq(struct ac_llvm_context *ctx, LLVMValueRef value);
131
132 LLVMValueRef
133 ac_build_varying_gather_values(struct ac_llvm_context *ctx, LLVMValueRef *values,
134 unsigned value_count, unsigned component);
135
136 LLVMValueRef
137 ac_build_gather_values_extended(struct ac_llvm_context *ctx,
138 LLVMValueRef *values,
139 unsigned value_count,
140 unsigned value_stride,
141 bool load,
142 bool always_vector);
143 LLVMValueRef
144 ac_build_gather_values(struct ac_llvm_context *ctx,
145 LLVMValueRef *values,
146 unsigned value_count);
147 LLVMValueRef ac_build_expand_to_vec4(struct ac_llvm_context *ctx,
148 LLVMValueRef value,
149 unsigned num_channels);
150
151 LLVMValueRef
152 ac_build_fdiv(struct ac_llvm_context *ctx,
153 LLVMValueRef num,
154 LLVMValueRef den);
155
156 void
157 ac_prepare_cube_coords(struct ac_llvm_context *ctx,
158 bool is_deriv, bool is_array, bool is_lod,
159 LLVMValueRef *coords_arg,
160 LLVMValueRef *derivs_arg);
161
162
163 LLVMValueRef
164 ac_build_fs_interp(struct ac_llvm_context *ctx,
165 LLVMValueRef llvm_chan,
166 LLVMValueRef attr_number,
167 LLVMValueRef params,
168 LLVMValueRef i,
169 LLVMValueRef j);
170
171 LLVMValueRef
172 ac_build_fs_interp_mov(struct ac_llvm_context *ctx,
173 LLVMValueRef parameter,
174 LLVMValueRef llvm_chan,
175 LLVMValueRef attr_number,
176 LLVMValueRef params);
177
178 LLVMValueRef
179 ac_build_gep0(struct ac_llvm_context *ctx,
180 LLVMValueRef base_ptr,
181 LLVMValueRef index);
182
183 void
184 ac_build_indexed_store(struct ac_llvm_context *ctx,
185 LLVMValueRef base_ptr, LLVMValueRef index,
186 LLVMValueRef value);
187
188 LLVMValueRef ac_build_load(struct ac_llvm_context *ctx, LLVMValueRef base_ptr,
189 LLVMValueRef index);
190 LLVMValueRef ac_build_load_invariant(struct ac_llvm_context *ctx,
191 LLVMValueRef base_ptr, LLVMValueRef index);
192 LLVMValueRef ac_build_load_to_sgpr(struct ac_llvm_context *ctx,
193 LLVMValueRef base_ptr, LLVMValueRef index);
194
195 void
196 ac_build_buffer_store_dword(struct ac_llvm_context *ctx,
197 LLVMValueRef rsrc,
198 LLVMValueRef vdata,
199 unsigned num_channels,
200 LLVMValueRef voffset,
201 LLVMValueRef soffset,
202 unsigned inst_offset,
203 bool glc,
204 bool slc,
205 bool writeonly_memory,
206 bool swizzle_enable_hint);
207 LLVMValueRef
208 ac_build_buffer_load(struct ac_llvm_context *ctx,
209 LLVMValueRef rsrc,
210 int num_channels,
211 LLVMValueRef vindex,
212 LLVMValueRef voffset,
213 LLVMValueRef soffset,
214 unsigned inst_offset,
215 unsigned glc,
216 unsigned slc,
217 bool can_speculate,
218 bool allow_smem);
219
220 LLVMValueRef ac_build_buffer_load_format(struct ac_llvm_context *ctx,
221 LLVMValueRef rsrc,
222 LLVMValueRef vindex,
223 LLVMValueRef voffset,
224 unsigned num_channels,
225 bool glc,
226 bool can_speculate);
227
228 LLVMValueRef
229 ac_get_thread_id(struct ac_llvm_context *ctx);
230
231 #define AC_TID_MASK_TOP_LEFT 0xfffffffc
232 #define AC_TID_MASK_TOP 0xfffffffd
233 #define AC_TID_MASK_LEFT 0xfffffffe
234
235 LLVMValueRef
236 ac_build_ddxy(struct ac_llvm_context *ctx,
237 uint32_t mask,
238 int idx,
239 LLVMValueRef val);
240
241 #define AC_SENDMSG_GS 2
242 #define AC_SENDMSG_GS_DONE 3
243
244 #define AC_SENDMSG_GS_OP_NOP (0 << 4)
245 #define AC_SENDMSG_GS_OP_CUT (1 << 4)
246 #define AC_SENDMSG_GS_OP_EMIT (2 << 4)
247 #define AC_SENDMSG_GS_OP_EMIT_CUT (3 << 4)
248
249 void ac_build_sendmsg(struct ac_llvm_context *ctx,
250 uint32_t msg,
251 LLVMValueRef wave_id);
252
253 LLVMValueRef ac_build_imsb(struct ac_llvm_context *ctx,
254 LLVMValueRef arg,
255 LLVMTypeRef dst_type);
256
257 LLVMValueRef ac_build_umsb(struct ac_llvm_context *ctx,
258 LLVMValueRef arg,
259 LLVMTypeRef dst_type);
260 LLVMValueRef ac_build_fmin(struct ac_llvm_context *ctx, LLVMValueRef a,
261 LLVMValueRef b);
262 LLVMValueRef ac_build_fmax(struct ac_llvm_context *ctx, LLVMValueRef a,
263 LLVMValueRef b);
264 LLVMValueRef ac_build_imin(struct ac_llvm_context *ctx, LLVMValueRef a,
265 LLVMValueRef b);
266 LLVMValueRef ac_build_imax(struct ac_llvm_context *ctx, LLVMValueRef a,
267 LLVMValueRef b);
268 LLVMValueRef ac_build_umin(struct ac_llvm_context *ctx, LLVMValueRef a, LLVMValueRef b);
269 LLVMValueRef ac_build_clamp(struct ac_llvm_context *ctx, LLVMValueRef value);
270
271 struct ac_export_args {
272 LLVMValueRef out[4];
273 unsigned target;
274 unsigned enabled_channels;
275 bool compr;
276 bool done;
277 bool valid_mask;
278 };
279
280 void ac_build_export(struct ac_llvm_context *ctx, struct ac_export_args *a);
281
282 void ac_build_export_null(struct ac_llvm_context *ctx);
283
284 enum ac_image_opcode {
285 ac_image_sample,
286 ac_image_gather4,
287 ac_image_load,
288 ac_image_load_mip,
289 ac_image_get_lod,
290 ac_image_get_resinfo,
291 };
292
293 struct ac_image_args {
294 enum ac_image_opcode opcode;
295 bool level_zero;
296 bool bias;
297 bool lod;
298 bool deriv;
299 bool compare;
300 bool offset;
301
302 LLVMValueRef resource;
303 LLVMValueRef sampler;
304 LLVMValueRef addr;
305 unsigned dmask;
306 bool unorm;
307 bool da;
308 };
309
310 LLVMValueRef ac_build_image_opcode(struct ac_llvm_context *ctx,
311 struct ac_image_args *a);
312 LLVMValueRef ac_build_cvt_pkrtz_f16(struct ac_llvm_context *ctx,
313 LLVMValueRef args[2]);
314 LLVMValueRef ac_build_cvt_pknorm_i16(struct ac_llvm_context *ctx,
315 LLVMValueRef args[2]);
316 LLVMValueRef ac_build_cvt_pknorm_u16(struct ac_llvm_context *ctx,
317 LLVMValueRef args[2]);
318 LLVMValueRef ac_build_cvt_pk_i16(struct ac_llvm_context *ctx,
319 LLVMValueRef args[2], unsigned bits, bool hi);
320 LLVMValueRef ac_build_cvt_pk_u16(struct ac_llvm_context *ctx,
321 LLVMValueRef args[2], unsigned bits, bool hi);
322 LLVMValueRef ac_build_wqm_vote(struct ac_llvm_context *ctx, LLVMValueRef i1);
323 void ac_build_kill_if_false(struct ac_llvm_context *ctx, LLVMValueRef i1);
324 LLVMValueRef ac_build_bfe(struct ac_llvm_context *ctx, LLVMValueRef input,
325 LLVMValueRef offset, LLVMValueRef width,
326 bool is_signed);
327
328 void ac_build_waitcnt(struct ac_llvm_context *ctx, unsigned simm16);
329
330 void ac_get_image_intr_name(const char *base_name,
331 LLVMTypeRef data_type,
332 LLVMTypeRef coords_type,
333 LLVMTypeRef rsrc_type,
334 char *out_name, unsigned out_len);
335
336 void ac_optimize_vs_outputs(struct ac_llvm_context *ac,
337 LLVMValueRef main_fn,
338 uint8_t *vs_output_param_offset,
339 uint32_t num_outputs,
340 uint8_t *num_param_exports);
341 void ac_init_exec_full_mask(struct ac_llvm_context *ctx);
342
343 void ac_declare_lds_as_pointer(struct ac_llvm_context *ac);
344 LLVMValueRef ac_lds_load(struct ac_llvm_context *ctx,
345 LLVMValueRef dw_addr);
346 void ac_lds_store(struct ac_llvm_context *ctx,
347 LLVMValueRef dw_addr, LLVMValueRef value);
348
349 LLVMValueRef ac_find_lsb(struct ac_llvm_context *ctx,
350 LLVMTypeRef dst_type,
351 LLVMValueRef src0);
352
353 LLVMTypeRef ac_array_in_const_addr_space(LLVMTypeRef elem_type);
354
355 #ifdef __cplusplus
356 }
357 #endif
358
359 #endif