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