ac/nir: remove backlink to nir_to_llvm_context
[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 int
96 ac_get_elem_bits(struct ac_llvm_context *ctx, LLVMTypeRef type);
97
98 LLVMValueRef
99 ac_llvm_extract_elem(struct ac_llvm_context *ac,
100 LLVMValueRef value,
101 int index);
102
103 unsigned ac_get_type_size(LLVMTypeRef type);
104
105 LLVMTypeRef ac_to_integer_type(struct ac_llvm_context *ctx, LLVMTypeRef t);
106 LLVMValueRef ac_to_integer(struct ac_llvm_context *ctx, LLVMValueRef v);
107 LLVMTypeRef ac_to_float_type(struct ac_llvm_context *ctx, LLVMTypeRef t);
108 LLVMValueRef ac_to_float(struct ac_llvm_context *ctx, LLVMValueRef v);
109
110 LLVMValueRef
111 ac_build_intrinsic(struct ac_llvm_context *ctx, const char *name,
112 LLVMTypeRef return_type, LLVMValueRef *params,
113 unsigned param_count, unsigned attrib_mask);
114
115 void ac_build_type_name_for_intr(LLVMTypeRef type, char *buf, unsigned bufsize);
116
117 LLVMValueRef
118 ac_build_phi(struct ac_llvm_context *ctx, LLVMTypeRef type,
119 unsigned count_incoming, LLVMValueRef *values,
120 LLVMBasicBlockRef *blocks);
121
122 void ac_build_optimization_barrier(struct ac_llvm_context *ctx,
123 LLVMValueRef *pvgpr);
124
125 LLVMValueRef ac_build_shader_clock(struct ac_llvm_context *ctx);
126
127 LLVMValueRef ac_build_ballot(struct ac_llvm_context *ctx, LLVMValueRef value);
128
129 LLVMValueRef ac_build_vote_all(struct ac_llvm_context *ctx, LLVMValueRef value);
130
131 LLVMValueRef ac_build_vote_any(struct ac_llvm_context *ctx, LLVMValueRef value);
132
133 LLVMValueRef ac_build_vote_eq(struct ac_llvm_context *ctx, LLVMValueRef value);
134
135 LLVMValueRef
136 ac_build_varying_gather_values(struct ac_llvm_context *ctx, LLVMValueRef *values,
137 unsigned value_count, unsigned component);
138
139 LLVMValueRef
140 ac_build_gather_values_extended(struct ac_llvm_context *ctx,
141 LLVMValueRef *values,
142 unsigned value_count,
143 unsigned value_stride,
144 bool load,
145 bool always_vector);
146 LLVMValueRef
147 ac_build_gather_values(struct ac_llvm_context *ctx,
148 LLVMValueRef *values,
149 unsigned value_count);
150 LLVMValueRef ac_build_expand_to_vec4(struct ac_llvm_context *ctx,
151 LLVMValueRef value,
152 unsigned num_channels);
153
154 LLVMValueRef
155 ac_build_fdiv(struct ac_llvm_context *ctx,
156 LLVMValueRef num,
157 LLVMValueRef den);
158
159 void
160 ac_prepare_cube_coords(struct ac_llvm_context *ctx,
161 bool is_deriv, bool is_array, bool is_lod,
162 LLVMValueRef *coords_arg,
163 LLVMValueRef *derivs_arg);
164
165
166 LLVMValueRef
167 ac_build_fs_interp(struct ac_llvm_context *ctx,
168 LLVMValueRef llvm_chan,
169 LLVMValueRef attr_number,
170 LLVMValueRef params,
171 LLVMValueRef i,
172 LLVMValueRef j);
173
174 LLVMValueRef
175 ac_build_fs_interp_mov(struct ac_llvm_context *ctx,
176 LLVMValueRef parameter,
177 LLVMValueRef llvm_chan,
178 LLVMValueRef attr_number,
179 LLVMValueRef params);
180
181 LLVMValueRef
182 ac_build_gep0(struct ac_llvm_context *ctx,
183 LLVMValueRef base_ptr,
184 LLVMValueRef index);
185
186 void
187 ac_build_indexed_store(struct ac_llvm_context *ctx,
188 LLVMValueRef base_ptr, LLVMValueRef index,
189 LLVMValueRef value);
190
191 LLVMValueRef ac_build_load(struct ac_llvm_context *ctx, LLVMValueRef base_ptr,
192 LLVMValueRef index);
193 LLVMValueRef ac_build_load_invariant(struct ac_llvm_context *ctx,
194 LLVMValueRef base_ptr, LLVMValueRef index);
195 LLVMValueRef ac_build_load_to_sgpr(struct ac_llvm_context *ctx,
196 LLVMValueRef base_ptr, LLVMValueRef index);
197
198 void
199 ac_build_buffer_store_dword(struct ac_llvm_context *ctx,
200 LLVMValueRef rsrc,
201 LLVMValueRef vdata,
202 unsigned num_channels,
203 LLVMValueRef voffset,
204 LLVMValueRef soffset,
205 unsigned inst_offset,
206 bool glc,
207 bool slc,
208 bool writeonly_memory,
209 bool swizzle_enable_hint);
210 LLVMValueRef
211 ac_build_buffer_load(struct ac_llvm_context *ctx,
212 LLVMValueRef rsrc,
213 int num_channels,
214 LLVMValueRef vindex,
215 LLVMValueRef voffset,
216 LLVMValueRef soffset,
217 unsigned inst_offset,
218 unsigned glc,
219 unsigned slc,
220 bool can_speculate,
221 bool allow_smem);
222
223 LLVMValueRef ac_build_buffer_load_format(struct ac_llvm_context *ctx,
224 LLVMValueRef rsrc,
225 LLVMValueRef vindex,
226 LLVMValueRef voffset,
227 unsigned num_channels,
228 bool glc,
229 bool can_speculate);
230
231 LLVMValueRef
232 ac_get_thread_id(struct ac_llvm_context *ctx);
233
234 #define AC_TID_MASK_TOP_LEFT 0xfffffffc
235 #define AC_TID_MASK_TOP 0xfffffffd
236 #define AC_TID_MASK_LEFT 0xfffffffe
237
238 LLVMValueRef
239 ac_build_ddxy(struct ac_llvm_context *ctx,
240 uint32_t mask,
241 int idx,
242 LLVMValueRef val);
243
244 #define AC_SENDMSG_GS 2
245 #define AC_SENDMSG_GS_DONE 3
246
247 #define AC_SENDMSG_GS_OP_NOP (0 << 4)
248 #define AC_SENDMSG_GS_OP_CUT (1 << 4)
249 #define AC_SENDMSG_GS_OP_EMIT (2 << 4)
250 #define AC_SENDMSG_GS_OP_EMIT_CUT (3 << 4)
251
252 void ac_build_sendmsg(struct ac_llvm_context *ctx,
253 uint32_t msg,
254 LLVMValueRef wave_id);
255
256 LLVMValueRef ac_build_imsb(struct ac_llvm_context *ctx,
257 LLVMValueRef arg,
258 LLVMTypeRef dst_type);
259
260 LLVMValueRef ac_build_umsb(struct ac_llvm_context *ctx,
261 LLVMValueRef arg,
262 LLVMTypeRef dst_type);
263 LLVMValueRef ac_build_fmin(struct ac_llvm_context *ctx, LLVMValueRef a,
264 LLVMValueRef b);
265 LLVMValueRef ac_build_fmax(struct ac_llvm_context *ctx, LLVMValueRef a,
266 LLVMValueRef b);
267 LLVMValueRef ac_build_imin(struct ac_llvm_context *ctx, LLVMValueRef a,
268 LLVMValueRef b);
269 LLVMValueRef ac_build_imax(struct ac_llvm_context *ctx, LLVMValueRef a,
270 LLVMValueRef b);
271 LLVMValueRef ac_build_umin(struct ac_llvm_context *ctx, LLVMValueRef a, LLVMValueRef b);
272 LLVMValueRef ac_build_clamp(struct ac_llvm_context *ctx, LLVMValueRef value);
273
274 struct ac_export_args {
275 LLVMValueRef out[4];
276 unsigned target;
277 unsigned enabled_channels;
278 bool compr;
279 bool done;
280 bool valid_mask;
281 };
282
283 void ac_build_export(struct ac_llvm_context *ctx, struct ac_export_args *a);
284
285 void ac_build_export_null(struct ac_llvm_context *ctx);
286
287 enum ac_image_opcode {
288 ac_image_sample,
289 ac_image_gather4,
290 ac_image_load,
291 ac_image_load_mip,
292 ac_image_get_lod,
293 ac_image_get_resinfo,
294 };
295
296 struct ac_image_args {
297 enum ac_image_opcode opcode;
298 bool level_zero;
299 bool bias;
300 bool lod;
301 bool deriv;
302 bool compare;
303 bool offset;
304
305 LLVMValueRef resource;
306 LLVMValueRef sampler;
307 LLVMValueRef addr;
308 unsigned dmask;
309 bool unorm;
310 bool da;
311 };
312
313 LLVMValueRef ac_build_image_opcode(struct ac_llvm_context *ctx,
314 struct ac_image_args *a);
315 LLVMValueRef ac_build_cvt_pkrtz_f16(struct ac_llvm_context *ctx,
316 LLVMValueRef args[2]);
317 LLVMValueRef ac_build_cvt_pknorm_i16(struct ac_llvm_context *ctx,
318 LLVMValueRef args[2]);
319 LLVMValueRef ac_build_cvt_pknorm_u16(struct ac_llvm_context *ctx,
320 LLVMValueRef args[2]);
321 LLVMValueRef ac_build_cvt_pk_i16(struct ac_llvm_context *ctx,
322 LLVMValueRef args[2], unsigned bits, bool hi);
323 LLVMValueRef ac_build_cvt_pk_u16(struct ac_llvm_context *ctx,
324 LLVMValueRef args[2], unsigned bits, bool hi);
325 LLVMValueRef ac_build_wqm_vote(struct ac_llvm_context *ctx, LLVMValueRef i1);
326 void ac_build_kill_if_false(struct ac_llvm_context *ctx, LLVMValueRef i1);
327 LLVMValueRef ac_build_bfe(struct ac_llvm_context *ctx, LLVMValueRef input,
328 LLVMValueRef offset, LLVMValueRef width,
329 bool is_signed);
330
331 void ac_build_waitcnt(struct ac_llvm_context *ctx, unsigned simm16);
332
333 void ac_get_image_intr_name(const char *base_name,
334 LLVMTypeRef data_type,
335 LLVMTypeRef coords_type,
336 LLVMTypeRef rsrc_type,
337 char *out_name, unsigned out_len);
338
339 void ac_optimize_vs_outputs(struct ac_llvm_context *ac,
340 LLVMValueRef main_fn,
341 uint8_t *vs_output_param_offset,
342 uint32_t num_outputs,
343 uint8_t *num_param_exports);
344 void ac_init_exec_full_mask(struct ac_llvm_context *ctx);
345
346 void ac_declare_lds_as_pointer(struct ac_llvm_context *ac);
347 LLVMValueRef ac_lds_load(struct ac_llvm_context *ctx,
348 LLVMValueRef dw_addr);
349 void ac_lds_store(struct ac_llvm_context *ctx,
350 LLVMValueRef dw_addr, LLVMValueRef value);
351
352 LLVMValueRef ac_find_lsb(struct ac_llvm_context *ctx,
353 LLVMTypeRef dst_type,
354 LLVMValueRef src0);
355
356 LLVMTypeRef ac_array_in_const_addr_space(LLVMTypeRef elem_type);
357
358 #ifdef __cplusplus
359 }
360 #endif
361
362 #endif