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