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