cffc980e51f9d34e54e94bddc46d71c9242abc4d
[mesa.git] / src / amd / common / ac_nir_to_llvm.c
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 "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include "ac_nir_to_llvm.h"
25 #include "ac_llvm_build.h"
26 #include "ac_llvm_util.h"
27 #include "ac_binary.h"
28 #include "sid.h"
29 #include "nir/nir.h"
30 #include "nir/nir_deref.h"
31 #include "util/bitscan.h"
32 #include "util/u_math.h"
33 #include "ac_shader_abi.h"
34 #include "ac_shader_util.h"
35
36 struct ac_nir_context {
37 struct ac_llvm_context ac;
38 struct ac_shader_abi *abi;
39
40 gl_shader_stage stage;
41
42 LLVMValueRef *ssa_defs;
43
44 struct hash_table *defs;
45 struct hash_table *phis;
46 struct hash_table *vars;
47
48 LLVMValueRef main_function;
49 LLVMBasicBlockRef continue_block;
50 LLVMBasicBlockRef break_block;
51
52 int num_locals;
53 LLVMValueRef *locals;
54 };
55
56 static LLVMValueRef get_sampler_desc(struct ac_nir_context *ctx,
57 nir_deref_instr *deref_instr,
58 enum ac_descriptor_type desc_type,
59 const nir_tex_instr *instr,
60 bool image, bool write);
61
62 static void
63 build_store_values_extended(struct ac_llvm_context *ac,
64 LLVMValueRef *values,
65 unsigned value_count,
66 unsigned value_stride,
67 LLVMValueRef vec)
68 {
69 LLVMBuilderRef builder = ac->builder;
70 unsigned i;
71
72 for (i = 0; i < value_count; i++) {
73 LLVMValueRef ptr = values[i * value_stride];
74 LLVMValueRef index = LLVMConstInt(ac->i32, i, false);
75 LLVMValueRef value = LLVMBuildExtractElement(builder, vec, index, "");
76 LLVMBuildStore(builder, value, ptr);
77 }
78 }
79
80 static enum ac_image_dim
81 get_ac_sampler_dim(const struct ac_llvm_context *ctx, enum glsl_sampler_dim dim,
82 bool is_array)
83 {
84 switch (dim) {
85 case GLSL_SAMPLER_DIM_1D:
86 if (ctx->chip_class >= GFX9)
87 return is_array ? ac_image_2darray : ac_image_2d;
88 return is_array ? ac_image_1darray : ac_image_1d;
89 case GLSL_SAMPLER_DIM_2D:
90 case GLSL_SAMPLER_DIM_RECT:
91 case GLSL_SAMPLER_DIM_EXTERNAL:
92 return is_array ? ac_image_2darray : ac_image_2d;
93 case GLSL_SAMPLER_DIM_3D:
94 return ac_image_3d;
95 case GLSL_SAMPLER_DIM_CUBE:
96 return ac_image_cube;
97 case GLSL_SAMPLER_DIM_MS:
98 return is_array ? ac_image_2darraymsaa : ac_image_2dmsaa;
99 case GLSL_SAMPLER_DIM_SUBPASS:
100 return ac_image_2darray;
101 case GLSL_SAMPLER_DIM_SUBPASS_MS:
102 return ac_image_2darraymsaa;
103 default:
104 unreachable("bad sampler dim");
105 }
106 }
107
108 static enum ac_image_dim
109 get_ac_image_dim(const struct ac_llvm_context *ctx, enum glsl_sampler_dim sdim,
110 bool is_array)
111 {
112 enum ac_image_dim dim = get_ac_sampler_dim(ctx, sdim, is_array);
113
114 if (dim == ac_image_cube ||
115 (ctx->chip_class <= VI && dim == ac_image_3d))
116 dim = ac_image_2darray;
117
118 return dim;
119 }
120
121 static LLVMTypeRef get_def_type(struct ac_nir_context *ctx,
122 const nir_ssa_def *def)
123 {
124 LLVMTypeRef type = LLVMIntTypeInContext(ctx->ac.context, def->bit_size);
125 if (def->num_components > 1) {
126 type = LLVMVectorType(type, def->num_components);
127 }
128 return type;
129 }
130
131 static LLVMValueRef get_src(struct ac_nir_context *nir, nir_src src)
132 {
133 assert(src.is_ssa);
134 return nir->ssa_defs[src.ssa->index];
135 }
136
137 static LLVMValueRef
138 get_memory_ptr(struct ac_nir_context *ctx, nir_src src)
139 {
140 LLVMValueRef ptr = get_src(ctx, src);
141 ptr = LLVMBuildGEP(ctx->ac.builder, ctx->ac.lds, &ptr, 1, "");
142 int addr_space = LLVMGetPointerAddressSpace(LLVMTypeOf(ptr));
143
144 return LLVMBuildBitCast(ctx->ac.builder, ptr,
145 LLVMPointerType(ctx->ac.i32, addr_space), "");
146 }
147
148 static LLVMBasicBlockRef get_block(struct ac_nir_context *nir,
149 const struct nir_block *b)
150 {
151 struct hash_entry *entry = _mesa_hash_table_search(nir->defs, b);
152 return (LLVMBasicBlockRef)entry->data;
153 }
154
155 static LLVMValueRef get_alu_src(struct ac_nir_context *ctx,
156 nir_alu_src src,
157 unsigned num_components)
158 {
159 LLVMValueRef value = get_src(ctx, src.src);
160 bool need_swizzle = false;
161
162 assert(value);
163 unsigned src_components = ac_get_llvm_num_components(value);
164 for (unsigned i = 0; i < num_components; ++i) {
165 assert(src.swizzle[i] < src_components);
166 if (src.swizzle[i] != i)
167 need_swizzle = true;
168 }
169
170 if (need_swizzle || num_components != src_components) {
171 LLVMValueRef masks[] = {
172 LLVMConstInt(ctx->ac.i32, src.swizzle[0], false),
173 LLVMConstInt(ctx->ac.i32, src.swizzle[1], false),
174 LLVMConstInt(ctx->ac.i32, src.swizzle[2], false),
175 LLVMConstInt(ctx->ac.i32, src.swizzle[3], false)};
176
177 if (src_components > 1 && num_components == 1) {
178 value = LLVMBuildExtractElement(ctx->ac.builder, value,
179 masks[0], "");
180 } else if (src_components == 1 && num_components > 1) {
181 LLVMValueRef values[] = {value, value, value, value};
182 value = ac_build_gather_values(&ctx->ac, values, num_components);
183 } else {
184 LLVMValueRef swizzle = LLVMConstVector(masks, num_components);
185 value = LLVMBuildShuffleVector(ctx->ac.builder, value, value,
186 swizzle, "");
187 }
188 }
189 assert(!src.negate);
190 assert(!src.abs);
191 return value;
192 }
193
194 static LLVMValueRef emit_int_cmp(struct ac_llvm_context *ctx,
195 LLVMIntPredicate pred, LLVMValueRef src0,
196 LLVMValueRef src1)
197 {
198 LLVMValueRef result = LLVMBuildICmp(ctx->builder, pred, src0, src1, "");
199 return LLVMBuildSelect(ctx->builder, result,
200 LLVMConstInt(ctx->i32, 0xFFFFFFFF, false),
201 ctx->i32_0, "");
202 }
203
204 static LLVMValueRef emit_float_cmp(struct ac_llvm_context *ctx,
205 LLVMRealPredicate pred, LLVMValueRef src0,
206 LLVMValueRef src1)
207 {
208 LLVMValueRef result;
209 src0 = ac_to_float(ctx, src0);
210 src1 = ac_to_float(ctx, src1);
211 result = LLVMBuildFCmp(ctx->builder, pred, src0, src1, "");
212 return LLVMBuildSelect(ctx->builder, result,
213 LLVMConstInt(ctx->i32, 0xFFFFFFFF, false),
214 ctx->i32_0, "");
215 }
216
217 static LLVMValueRef emit_intrin_1f_param(struct ac_llvm_context *ctx,
218 const char *intrin,
219 LLVMTypeRef result_type,
220 LLVMValueRef src0)
221 {
222 char name[64];
223 LLVMValueRef params[] = {
224 ac_to_float(ctx, src0),
225 };
226
227 MAYBE_UNUSED const int length = snprintf(name, sizeof(name), "%s.f%d", intrin,
228 ac_get_elem_bits(ctx, result_type));
229 assert(length < sizeof(name));
230 return ac_build_intrinsic(ctx, name, result_type, params, 1, AC_FUNC_ATTR_READNONE);
231 }
232
233 static LLVMValueRef emit_intrin_2f_param(struct ac_llvm_context *ctx,
234 const char *intrin,
235 LLVMTypeRef result_type,
236 LLVMValueRef src0, LLVMValueRef src1)
237 {
238 char name[64];
239 LLVMValueRef params[] = {
240 ac_to_float(ctx, src0),
241 ac_to_float(ctx, src1),
242 };
243
244 MAYBE_UNUSED const int length = snprintf(name, sizeof(name), "%s.f%d", intrin,
245 ac_get_elem_bits(ctx, result_type));
246 assert(length < sizeof(name));
247 return ac_build_intrinsic(ctx, name, result_type, params, 2, AC_FUNC_ATTR_READNONE);
248 }
249
250 static LLVMValueRef emit_intrin_3f_param(struct ac_llvm_context *ctx,
251 const char *intrin,
252 LLVMTypeRef result_type,
253 LLVMValueRef src0, LLVMValueRef src1, LLVMValueRef src2)
254 {
255 char name[64];
256 LLVMValueRef params[] = {
257 ac_to_float(ctx, src0),
258 ac_to_float(ctx, src1),
259 ac_to_float(ctx, src2),
260 };
261
262 MAYBE_UNUSED const int length = snprintf(name, sizeof(name), "%s.f%d", intrin,
263 ac_get_elem_bits(ctx, result_type));
264 assert(length < sizeof(name));
265 return ac_build_intrinsic(ctx, name, result_type, params, 3, AC_FUNC_ATTR_READNONE);
266 }
267
268 static LLVMValueRef emit_bcsel(struct ac_llvm_context *ctx,
269 LLVMValueRef src0, LLVMValueRef src1, LLVMValueRef src2)
270 {
271 LLVMValueRef v = LLVMBuildICmp(ctx->builder, LLVMIntNE, src0,
272 ctx->i32_0, "");
273 return LLVMBuildSelect(ctx->builder, v, ac_to_integer(ctx, src1),
274 ac_to_integer(ctx, src2), "");
275 }
276
277 static LLVMValueRef emit_minmax_int(struct ac_llvm_context *ctx,
278 LLVMIntPredicate pred,
279 LLVMValueRef src0, LLVMValueRef src1)
280 {
281 return LLVMBuildSelect(ctx->builder,
282 LLVMBuildICmp(ctx->builder, pred, src0, src1, ""),
283 src0,
284 src1, "");
285
286 }
287 static LLVMValueRef emit_iabs(struct ac_llvm_context *ctx,
288 LLVMValueRef src0)
289 {
290 return emit_minmax_int(ctx, LLVMIntSGT, src0,
291 LLVMBuildNeg(ctx->builder, src0, ""));
292 }
293
294 static LLVMValueRef emit_uint_carry(struct ac_llvm_context *ctx,
295 const char *intrin,
296 LLVMValueRef src0, LLVMValueRef src1)
297 {
298 LLVMTypeRef ret_type;
299 LLVMTypeRef types[] = { ctx->i32, ctx->i1 };
300 LLVMValueRef res;
301 LLVMValueRef params[] = { src0, src1 };
302 ret_type = LLVMStructTypeInContext(ctx->context, types,
303 2, true);
304
305 res = ac_build_intrinsic(ctx, intrin, ret_type,
306 params, 2, AC_FUNC_ATTR_READNONE);
307
308 res = LLVMBuildExtractValue(ctx->builder, res, 1, "");
309 res = LLVMBuildZExt(ctx->builder, res, ctx->i32, "");
310 return res;
311 }
312
313 static LLVMValueRef emit_b2f(struct ac_llvm_context *ctx,
314 LLVMValueRef src0)
315 {
316 return LLVMBuildAnd(ctx->builder, src0, LLVMBuildBitCast(ctx->builder, LLVMConstReal(ctx->f32, 1.0), ctx->i32, ""), "");
317 }
318
319 static LLVMValueRef emit_f2b(struct ac_llvm_context *ctx,
320 LLVMValueRef src0)
321 {
322 src0 = ac_to_float(ctx, src0);
323 LLVMValueRef zero = LLVMConstNull(LLVMTypeOf(src0));
324 return LLVMBuildSExt(ctx->builder,
325 LLVMBuildFCmp(ctx->builder, LLVMRealUNE, src0, zero, ""),
326 ctx->i32, "");
327 }
328
329 static LLVMValueRef emit_b2i(struct ac_llvm_context *ctx,
330 LLVMValueRef src0,
331 unsigned bitsize)
332 {
333 LLVMValueRef result = LLVMBuildAnd(ctx->builder, src0, ctx->i32_1, "");
334
335 if (bitsize == 32)
336 return result;
337
338 return LLVMBuildZExt(ctx->builder, result, ctx->i64, "");
339 }
340
341 static LLVMValueRef emit_i2b(struct ac_llvm_context *ctx,
342 LLVMValueRef src0)
343 {
344 LLVMValueRef zero = LLVMConstNull(LLVMTypeOf(src0));
345 return LLVMBuildSExt(ctx->builder,
346 LLVMBuildICmp(ctx->builder, LLVMIntNE, src0, zero, ""),
347 ctx->i32, "");
348 }
349
350 static LLVMValueRef emit_f2f16(struct ac_llvm_context *ctx,
351 LLVMValueRef src0)
352 {
353 LLVMValueRef result;
354 LLVMValueRef cond = NULL;
355
356 src0 = ac_to_float(ctx, src0);
357 result = LLVMBuildFPTrunc(ctx->builder, src0, ctx->f16, "");
358
359 if (ctx->chip_class >= VI) {
360 LLVMValueRef args[2];
361 /* Check if the result is a denormal - and flush to 0 if so. */
362 args[0] = result;
363 args[1] = LLVMConstInt(ctx->i32, N_SUBNORMAL | P_SUBNORMAL, false);
364 cond = ac_build_intrinsic(ctx, "llvm.amdgcn.class.f16", ctx->i1, args, 2, AC_FUNC_ATTR_READNONE);
365 }
366
367 /* need to convert back up to f32 */
368 result = LLVMBuildFPExt(ctx->builder, result, ctx->f32, "");
369
370 if (ctx->chip_class >= VI)
371 result = LLVMBuildSelect(ctx->builder, cond, ctx->f32_0, result, "");
372 else {
373 /* for SI/CIK */
374 /* 0x38800000 is smallest half float value (2^-14) in 32-bit float,
375 * so compare the result and flush to 0 if it's smaller.
376 */
377 LLVMValueRef temp, cond2;
378 temp = emit_intrin_1f_param(ctx, "llvm.fabs", ctx->f32, result);
379 cond = LLVMBuildFCmp(ctx->builder, LLVMRealUGT,
380 LLVMBuildBitCast(ctx->builder, LLVMConstInt(ctx->i32, 0x38800000, false), ctx->f32, ""),
381 temp, "");
382 cond2 = LLVMBuildFCmp(ctx->builder, LLVMRealUNE,
383 temp, ctx->f32_0, "");
384 cond = LLVMBuildAnd(ctx->builder, cond, cond2, "");
385 result = LLVMBuildSelect(ctx->builder, cond, ctx->f32_0, result, "");
386 }
387 return result;
388 }
389
390 static LLVMValueRef emit_umul_high(struct ac_llvm_context *ctx,
391 LLVMValueRef src0, LLVMValueRef src1)
392 {
393 LLVMValueRef dst64, result;
394 src0 = LLVMBuildZExt(ctx->builder, src0, ctx->i64, "");
395 src1 = LLVMBuildZExt(ctx->builder, src1, ctx->i64, "");
396
397 dst64 = LLVMBuildMul(ctx->builder, src0, src1, "");
398 dst64 = LLVMBuildLShr(ctx->builder, dst64, LLVMConstInt(ctx->i64, 32, false), "");
399 result = LLVMBuildTrunc(ctx->builder, dst64, ctx->i32, "");
400 return result;
401 }
402
403 static LLVMValueRef emit_imul_high(struct ac_llvm_context *ctx,
404 LLVMValueRef src0, LLVMValueRef src1)
405 {
406 LLVMValueRef dst64, result;
407 src0 = LLVMBuildSExt(ctx->builder, src0, ctx->i64, "");
408 src1 = LLVMBuildSExt(ctx->builder, src1, ctx->i64, "");
409
410 dst64 = LLVMBuildMul(ctx->builder, src0, src1, "");
411 dst64 = LLVMBuildAShr(ctx->builder, dst64, LLVMConstInt(ctx->i64, 32, false), "");
412 result = LLVMBuildTrunc(ctx->builder, dst64, ctx->i32, "");
413 return result;
414 }
415
416 static LLVMValueRef emit_bitfield_extract(struct ac_llvm_context *ctx,
417 bool is_signed,
418 const LLVMValueRef srcs[3])
419 {
420 LLVMValueRef result;
421
422 if (HAVE_LLVM < 0x0700) {
423 LLVMValueRef icond = LLVMBuildICmp(ctx->builder, LLVMIntEQ, srcs[2], LLVMConstInt(ctx->i32, 32, false), "");
424 result = ac_build_bfe(ctx, srcs[0], srcs[1], srcs[2], is_signed);
425 result = LLVMBuildSelect(ctx->builder, icond, srcs[0], result, "");
426 } else {
427 /* FIXME: LLVM 7 returns incorrect result when count is 0.
428 * https://bugs.freedesktop.org/show_bug.cgi?id=107276
429 */
430 LLVMValueRef zero = LLVMConstInt(ctx->i32, 0, false);
431 LLVMValueRef icond1 = LLVMBuildICmp(ctx->builder, LLVMIntEQ, srcs[2], LLVMConstInt(ctx->i32, 32, false), "");
432 LLVMValueRef icond2 = LLVMBuildICmp(ctx->builder, LLVMIntEQ, srcs[2], zero, "");
433
434 result = ac_build_bfe(ctx, srcs[0], srcs[1], srcs[2], is_signed);
435 result = LLVMBuildSelect(ctx->builder, icond1, srcs[0], result, "");
436 result = LLVMBuildSelect(ctx->builder, icond2, zero, result, "");
437 }
438
439 return result;
440 }
441
442 static LLVMValueRef emit_bitfield_insert(struct ac_llvm_context *ctx,
443 LLVMValueRef src0, LLVMValueRef src1,
444 LLVMValueRef src2, LLVMValueRef src3)
445 {
446 LLVMValueRef bfi_args[3], result;
447
448 bfi_args[0] = LLVMBuildShl(ctx->builder,
449 LLVMBuildSub(ctx->builder,
450 LLVMBuildShl(ctx->builder,
451 ctx->i32_1,
452 src3, ""),
453 ctx->i32_1, ""),
454 src2, "");
455 bfi_args[1] = LLVMBuildShl(ctx->builder, src1, src2, "");
456 bfi_args[2] = src0;
457
458 LLVMValueRef icond = LLVMBuildICmp(ctx->builder, LLVMIntEQ, src3, LLVMConstInt(ctx->i32, 32, false), "");
459
460 /* Calculate:
461 * (arg0 & arg1) | (~arg0 & arg2) = arg2 ^ (arg0 & (arg1 ^ arg2)
462 * Use the right-hand side, which the LLVM backend can convert to V_BFI.
463 */
464 result = LLVMBuildXor(ctx->builder, bfi_args[2],
465 LLVMBuildAnd(ctx->builder, bfi_args[0],
466 LLVMBuildXor(ctx->builder, bfi_args[1], bfi_args[2], ""), ""), "");
467
468 result = LLVMBuildSelect(ctx->builder, icond, src1, result, "");
469 return result;
470 }
471
472 static LLVMValueRef emit_pack_half_2x16(struct ac_llvm_context *ctx,
473 LLVMValueRef src0)
474 {
475 LLVMValueRef comp[2];
476
477 src0 = ac_to_float(ctx, src0);
478 comp[0] = LLVMBuildExtractElement(ctx->builder, src0, ctx->i32_0, "");
479 comp[1] = LLVMBuildExtractElement(ctx->builder, src0, ctx->i32_1, "");
480
481 return LLVMBuildBitCast(ctx->builder, ac_build_cvt_pkrtz_f16(ctx, comp),
482 ctx->i32, "");
483 }
484
485 static LLVMValueRef emit_unpack_half_2x16(struct ac_llvm_context *ctx,
486 LLVMValueRef src0)
487 {
488 LLVMValueRef const16 = LLVMConstInt(ctx->i32, 16, false);
489 LLVMValueRef temps[2], result, val;
490 int i;
491
492 for (i = 0; i < 2; i++) {
493 val = i == 1 ? LLVMBuildLShr(ctx->builder, src0, const16, "") : src0;
494 val = LLVMBuildTrunc(ctx->builder, val, ctx->i16, "");
495 val = LLVMBuildBitCast(ctx->builder, val, ctx->f16, "");
496 temps[i] = LLVMBuildFPExt(ctx->builder, val, ctx->f32, "");
497 }
498
499 result = LLVMBuildInsertElement(ctx->builder, LLVMGetUndef(ctx->v2f32), temps[0],
500 ctx->i32_0, "");
501 result = LLVMBuildInsertElement(ctx->builder, result, temps[1],
502 ctx->i32_1, "");
503 return result;
504 }
505
506 static LLVMValueRef emit_ddxy(struct ac_nir_context *ctx,
507 nir_op op,
508 LLVMValueRef src0)
509 {
510 unsigned mask;
511 int idx;
512 LLVMValueRef result;
513
514 if (op == nir_op_fddx_fine)
515 mask = AC_TID_MASK_LEFT;
516 else if (op == nir_op_fddy_fine)
517 mask = AC_TID_MASK_TOP;
518 else
519 mask = AC_TID_MASK_TOP_LEFT;
520
521 /* for DDX we want to next X pixel, DDY next Y pixel. */
522 if (op == nir_op_fddx_fine ||
523 op == nir_op_fddx_coarse ||
524 op == nir_op_fddx)
525 idx = 1;
526 else
527 idx = 2;
528
529 result = ac_build_ddxy(&ctx->ac, mask, idx, src0);
530 return result;
531 }
532
533 /*
534 * this takes an I,J coordinate pair,
535 * and works out the X and Y derivatives.
536 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
537 */
538 static LLVMValueRef emit_ddxy_interp(
539 struct ac_nir_context *ctx,
540 LLVMValueRef interp_ij)
541 {
542 LLVMValueRef result[4], a;
543 unsigned i;
544
545 for (i = 0; i < 2; i++) {
546 a = LLVMBuildExtractElement(ctx->ac.builder, interp_ij,
547 LLVMConstInt(ctx->ac.i32, i, false), "");
548 result[i] = emit_ddxy(ctx, nir_op_fddx, a);
549 result[2+i] = emit_ddxy(ctx, nir_op_fddy, a);
550 }
551 return ac_build_gather_values(&ctx->ac, result, 4);
552 }
553
554 static void visit_alu(struct ac_nir_context *ctx, const nir_alu_instr *instr)
555 {
556 LLVMValueRef src[4], result = NULL;
557 unsigned num_components = instr->dest.dest.ssa.num_components;
558 unsigned src_components;
559 LLVMTypeRef def_type = get_def_type(ctx, &instr->dest.dest.ssa);
560
561 assert(nir_op_infos[instr->op].num_inputs <= ARRAY_SIZE(src));
562 switch (instr->op) {
563 case nir_op_vec2:
564 case nir_op_vec3:
565 case nir_op_vec4:
566 src_components = 1;
567 break;
568 case nir_op_pack_half_2x16:
569 src_components = 2;
570 break;
571 case nir_op_unpack_half_2x16:
572 src_components = 1;
573 break;
574 case nir_op_cube_face_coord:
575 case nir_op_cube_face_index:
576 src_components = 3;
577 break;
578 default:
579 src_components = num_components;
580 break;
581 }
582 for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++)
583 src[i] = get_alu_src(ctx, instr->src[i], src_components);
584
585 switch (instr->op) {
586 case nir_op_fmov:
587 case nir_op_imov:
588 result = src[0];
589 break;
590 case nir_op_fneg:
591 src[0] = ac_to_float(&ctx->ac, src[0]);
592 result = LLVMBuildFNeg(ctx->ac.builder, src[0], "");
593 break;
594 case nir_op_ineg:
595 result = LLVMBuildNeg(ctx->ac.builder, src[0], "");
596 break;
597 case nir_op_inot:
598 result = LLVMBuildNot(ctx->ac.builder, src[0], "");
599 break;
600 case nir_op_iadd:
601 result = LLVMBuildAdd(ctx->ac.builder, src[0], src[1], "");
602 break;
603 case nir_op_fadd:
604 src[0] = ac_to_float(&ctx->ac, src[0]);
605 src[1] = ac_to_float(&ctx->ac, src[1]);
606 result = LLVMBuildFAdd(ctx->ac.builder, src[0], src[1], "");
607 break;
608 case nir_op_fsub:
609 src[0] = ac_to_float(&ctx->ac, src[0]);
610 src[1] = ac_to_float(&ctx->ac, src[1]);
611 result = LLVMBuildFSub(ctx->ac.builder, src[0], src[1], "");
612 break;
613 case nir_op_isub:
614 result = LLVMBuildSub(ctx->ac.builder, src[0], src[1], "");
615 break;
616 case nir_op_imul:
617 result = LLVMBuildMul(ctx->ac.builder, src[0], src[1], "");
618 break;
619 case nir_op_imod:
620 result = LLVMBuildSRem(ctx->ac.builder, src[0], src[1], "");
621 break;
622 case nir_op_umod:
623 result = LLVMBuildURem(ctx->ac.builder, src[0], src[1], "");
624 break;
625 case nir_op_fmod:
626 src[0] = ac_to_float(&ctx->ac, src[0]);
627 src[1] = ac_to_float(&ctx->ac, src[1]);
628 result = ac_build_fdiv(&ctx->ac, src[0], src[1]);
629 result = emit_intrin_1f_param(&ctx->ac, "llvm.floor",
630 ac_to_float_type(&ctx->ac, def_type), result);
631 result = LLVMBuildFMul(ctx->ac.builder, src[1] , result, "");
632 result = LLVMBuildFSub(ctx->ac.builder, src[0], result, "");
633 break;
634 case nir_op_frem:
635 src[0] = ac_to_float(&ctx->ac, src[0]);
636 src[1] = ac_to_float(&ctx->ac, src[1]);
637 result = LLVMBuildFRem(ctx->ac.builder, src[0], src[1], "");
638 break;
639 case nir_op_irem:
640 result = LLVMBuildSRem(ctx->ac.builder, src[0], src[1], "");
641 break;
642 case nir_op_idiv:
643 result = LLVMBuildSDiv(ctx->ac.builder, src[0], src[1], "");
644 break;
645 case nir_op_udiv:
646 result = LLVMBuildUDiv(ctx->ac.builder, src[0], src[1], "");
647 break;
648 case nir_op_fmul:
649 src[0] = ac_to_float(&ctx->ac, src[0]);
650 src[1] = ac_to_float(&ctx->ac, src[1]);
651 result = LLVMBuildFMul(ctx->ac.builder, src[0], src[1], "");
652 break;
653 case nir_op_frcp:
654 src[0] = ac_to_float(&ctx->ac, src[0]);
655 result = ac_build_fdiv(&ctx->ac, instr->dest.dest.ssa.bit_size == 32 ? ctx->ac.f32_1 : ctx->ac.f64_1,
656 src[0]);
657 break;
658 case nir_op_iand:
659 result = LLVMBuildAnd(ctx->ac.builder, src[0], src[1], "");
660 break;
661 case nir_op_ior:
662 result = LLVMBuildOr(ctx->ac.builder, src[0], src[1], "");
663 break;
664 case nir_op_ixor:
665 result = LLVMBuildXor(ctx->ac.builder, src[0], src[1], "");
666 break;
667 case nir_op_ishl:
668 result = LLVMBuildShl(ctx->ac.builder, src[0],
669 LLVMBuildZExt(ctx->ac.builder, src[1],
670 LLVMTypeOf(src[0]), ""),
671 "");
672 break;
673 case nir_op_ishr:
674 result = LLVMBuildAShr(ctx->ac.builder, src[0],
675 LLVMBuildZExt(ctx->ac.builder, src[1],
676 LLVMTypeOf(src[0]), ""),
677 "");
678 break;
679 case nir_op_ushr:
680 result = LLVMBuildLShr(ctx->ac.builder, src[0],
681 LLVMBuildZExt(ctx->ac.builder, src[1],
682 LLVMTypeOf(src[0]), ""),
683 "");
684 break;
685 case nir_op_ilt:
686 result = emit_int_cmp(&ctx->ac, LLVMIntSLT, src[0], src[1]);
687 break;
688 case nir_op_ine:
689 result = emit_int_cmp(&ctx->ac, LLVMIntNE, src[0], src[1]);
690 break;
691 case nir_op_ieq:
692 result = emit_int_cmp(&ctx->ac, LLVMIntEQ, src[0], src[1]);
693 break;
694 case nir_op_ige:
695 result = emit_int_cmp(&ctx->ac, LLVMIntSGE, src[0], src[1]);
696 break;
697 case nir_op_ult:
698 result = emit_int_cmp(&ctx->ac, LLVMIntULT, src[0], src[1]);
699 break;
700 case nir_op_uge:
701 result = emit_int_cmp(&ctx->ac, LLVMIntUGE, src[0], src[1]);
702 break;
703 case nir_op_feq:
704 result = emit_float_cmp(&ctx->ac, LLVMRealOEQ, src[0], src[1]);
705 break;
706 case nir_op_fne:
707 result = emit_float_cmp(&ctx->ac, LLVMRealUNE, src[0], src[1]);
708 break;
709 case nir_op_flt:
710 result = emit_float_cmp(&ctx->ac, LLVMRealOLT, src[0], src[1]);
711 break;
712 case nir_op_fge:
713 result = emit_float_cmp(&ctx->ac, LLVMRealOGE, src[0], src[1]);
714 break;
715 case nir_op_fabs:
716 result = emit_intrin_1f_param(&ctx->ac, "llvm.fabs",
717 ac_to_float_type(&ctx->ac, def_type), src[0]);
718 break;
719 case nir_op_iabs:
720 result = emit_iabs(&ctx->ac, src[0]);
721 break;
722 case nir_op_imax:
723 result = emit_minmax_int(&ctx->ac, LLVMIntSGT, src[0], src[1]);
724 break;
725 case nir_op_imin:
726 result = emit_minmax_int(&ctx->ac, LLVMIntSLT, src[0], src[1]);
727 break;
728 case nir_op_umax:
729 result = emit_minmax_int(&ctx->ac, LLVMIntUGT, src[0], src[1]);
730 break;
731 case nir_op_umin:
732 result = emit_minmax_int(&ctx->ac, LLVMIntULT, src[0], src[1]);
733 break;
734 case nir_op_isign:
735 result = ac_build_isign(&ctx->ac, src[0],
736 instr->dest.dest.ssa.bit_size);
737 break;
738 case nir_op_fsign:
739 src[0] = ac_to_float(&ctx->ac, src[0]);
740 result = ac_build_fsign(&ctx->ac, src[0],
741 instr->dest.dest.ssa.bit_size);
742 break;
743 case nir_op_ffloor:
744 result = emit_intrin_1f_param(&ctx->ac, "llvm.floor",
745 ac_to_float_type(&ctx->ac, def_type), src[0]);
746 break;
747 case nir_op_ftrunc:
748 result = emit_intrin_1f_param(&ctx->ac, "llvm.trunc",
749 ac_to_float_type(&ctx->ac, def_type), src[0]);
750 break;
751 case nir_op_fceil:
752 result = emit_intrin_1f_param(&ctx->ac, "llvm.ceil",
753 ac_to_float_type(&ctx->ac, def_type), src[0]);
754 break;
755 case nir_op_fround_even:
756 result = emit_intrin_1f_param(&ctx->ac, "llvm.rint",
757 ac_to_float_type(&ctx->ac, def_type),src[0]);
758 break;
759 case nir_op_ffract:
760 src[0] = ac_to_float(&ctx->ac, src[0]);
761 result = ac_build_fract(&ctx->ac, src[0],
762 instr->dest.dest.ssa.bit_size);
763 break;
764 case nir_op_fsin:
765 result = emit_intrin_1f_param(&ctx->ac, "llvm.sin",
766 ac_to_float_type(&ctx->ac, def_type), src[0]);
767 break;
768 case nir_op_fcos:
769 result = emit_intrin_1f_param(&ctx->ac, "llvm.cos",
770 ac_to_float_type(&ctx->ac, def_type), src[0]);
771 break;
772 case nir_op_fsqrt:
773 result = emit_intrin_1f_param(&ctx->ac, "llvm.sqrt",
774 ac_to_float_type(&ctx->ac, def_type), src[0]);
775 break;
776 case nir_op_fexp2:
777 result = emit_intrin_1f_param(&ctx->ac, "llvm.exp2",
778 ac_to_float_type(&ctx->ac, def_type), src[0]);
779 break;
780 case nir_op_flog2:
781 result = emit_intrin_1f_param(&ctx->ac, "llvm.log2",
782 ac_to_float_type(&ctx->ac, def_type), src[0]);
783 break;
784 case nir_op_frsq:
785 result = emit_intrin_1f_param(&ctx->ac, "llvm.sqrt",
786 ac_to_float_type(&ctx->ac, def_type), src[0]);
787 result = ac_build_fdiv(&ctx->ac, instr->dest.dest.ssa.bit_size == 32 ? ctx->ac.f32_1 : ctx->ac.f64_1,
788 result);
789 break;
790 case nir_op_frexp_exp:
791 src[0] = ac_to_float(&ctx->ac, src[0]);
792 result = ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.frexp.exp.i32.f64",
793 ctx->ac.i32, src, 1, AC_FUNC_ATTR_READNONE);
794
795 break;
796 case nir_op_frexp_sig:
797 src[0] = ac_to_float(&ctx->ac, src[0]);
798 result = ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.frexp.mant.f64",
799 ctx->ac.f64, src, 1, AC_FUNC_ATTR_READNONE);
800 break;
801 case nir_op_fmax:
802 result = emit_intrin_2f_param(&ctx->ac, "llvm.maxnum",
803 ac_to_float_type(&ctx->ac, def_type), src[0], src[1]);
804 if (ctx->ac.chip_class < GFX9 &&
805 instr->dest.dest.ssa.bit_size == 32) {
806 /* Only pre-GFX9 chips do not flush denorms. */
807 result = emit_intrin_1f_param(&ctx->ac, "llvm.canonicalize",
808 ac_to_float_type(&ctx->ac, def_type),
809 result);
810 }
811 break;
812 case nir_op_fmin:
813 result = emit_intrin_2f_param(&ctx->ac, "llvm.minnum",
814 ac_to_float_type(&ctx->ac, def_type), src[0], src[1]);
815 if (ctx->ac.chip_class < GFX9 &&
816 instr->dest.dest.ssa.bit_size == 32) {
817 /* Only pre-GFX9 chips do not flush denorms. */
818 result = emit_intrin_1f_param(&ctx->ac, "llvm.canonicalize",
819 ac_to_float_type(&ctx->ac, def_type),
820 result);
821 }
822 break;
823 case nir_op_ffma:
824 result = emit_intrin_3f_param(&ctx->ac, "llvm.fmuladd",
825 ac_to_float_type(&ctx->ac, def_type), src[0], src[1], src[2]);
826 break;
827 case nir_op_ldexp:
828 src[0] = ac_to_float(&ctx->ac, src[0]);
829 if (ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src[0])) == 32)
830 result = ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.ldexp.f32", ctx->ac.f32, src, 2, AC_FUNC_ATTR_READNONE);
831 else
832 result = ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.ldexp.f64", ctx->ac.f64, src, 2, AC_FUNC_ATTR_READNONE);
833 break;
834 case nir_op_ibitfield_extract:
835 result = emit_bitfield_extract(&ctx->ac, true, src);
836 break;
837 case nir_op_ubitfield_extract:
838 result = emit_bitfield_extract(&ctx->ac, false, src);
839 break;
840 case nir_op_bitfield_insert:
841 result = emit_bitfield_insert(&ctx->ac, src[0], src[1], src[2], src[3]);
842 break;
843 case nir_op_bitfield_reverse:
844 result = ac_build_intrinsic(&ctx->ac, "llvm.bitreverse.i32", ctx->ac.i32, src, 1, AC_FUNC_ATTR_READNONE);
845 break;
846 case nir_op_bit_count:
847 if (ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src[0])) == 32)
848 result = ac_build_intrinsic(&ctx->ac, "llvm.ctpop.i32", ctx->ac.i32, src, 1, AC_FUNC_ATTR_READNONE);
849 else {
850 result = ac_build_intrinsic(&ctx->ac, "llvm.ctpop.i64", ctx->ac.i64, src, 1, AC_FUNC_ATTR_READNONE);
851 result = LLVMBuildTrunc(ctx->ac.builder, result, ctx->ac.i32, "");
852 }
853 break;
854 case nir_op_vec2:
855 case nir_op_vec3:
856 case nir_op_vec4:
857 for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++)
858 src[i] = ac_to_integer(&ctx->ac, src[i]);
859 result = ac_build_gather_values(&ctx->ac, src, num_components);
860 break;
861 case nir_op_f2i16:
862 case nir_op_f2i32:
863 case nir_op_f2i64:
864 src[0] = ac_to_float(&ctx->ac, src[0]);
865 result = LLVMBuildFPToSI(ctx->ac.builder, src[0], def_type, "");
866 break;
867 case nir_op_f2u16:
868 case nir_op_f2u32:
869 case nir_op_f2u64:
870 src[0] = ac_to_float(&ctx->ac, src[0]);
871 result = LLVMBuildFPToUI(ctx->ac.builder, src[0], def_type, "");
872 break;
873 case nir_op_i2f16:
874 case nir_op_i2f32:
875 case nir_op_i2f64:
876 src[0] = ac_to_integer(&ctx->ac, src[0]);
877 result = LLVMBuildSIToFP(ctx->ac.builder, src[0], ac_to_float_type(&ctx->ac, def_type), "");
878 break;
879 case nir_op_u2f16:
880 case nir_op_u2f32:
881 case nir_op_u2f64:
882 src[0] = ac_to_integer(&ctx->ac, src[0]);
883 result = LLVMBuildUIToFP(ctx->ac.builder, src[0], ac_to_float_type(&ctx->ac, def_type), "");
884 break;
885 case nir_op_f2f16_rtz:
886 src[0] = ac_to_float(&ctx->ac, src[0]);
887 LLVMValueRef param[2] = { src[0], ctx->ac.f32_0 };
888 result = ac_build_cvt_pkrtz_f16(&ctx->ac, param);
889 result = LLVMBuildExtractElement(ctx->ac.builder, result, ctx->ac.i32_0, "");
890 break;
891 case nir_op_f2f16_rtne:
892 case nir_op_f2f16:
893 case nir_op_f2f32:
894 case nir_op_f2f64:
895 src[0] = ac_to_float(&ctx->ac, src[0]);
896 if (ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src[0])) < ac_get_elem_bits(&ctx->ac, def_type))
897 result = LLVMBuildFPExt(ctx->ac.builder, src[0], ac_to_float_type(&ctx->ac, def_type), "");
898 else
899 result = LLVMBuildFPTrunc(ctx->ac.builder, src[0], ac_to_float_type(&ctx->ac, def_type), "");
900 break;
901 case nir_op_u2u16:
902 case nir_op_u2u32:
903 case nir_op_u2u64:
904 src[0] = ac_to_integer(&ctx->ac, src[0]);
905 if (ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src[0])) < ac_get_elem_bits(&ctx->ac, def_type))
906 result = LLVMBuildZExt(ctx->ac.builder, src[0], def_type, "");
907 else
908 result = LLVMBuildTrunc(ctx->ac.builder, src[0], def_type, "");
909 break;
910 case nir_op_i2i16:
911 case nir_op_i2i32:
912 case nir_op_i2i64:
913 src[0] = ac_to_integer(&ctx->ac, src[0]);
914 if (ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src[0])) < ac_get_elem_bits(&ctx->ac, def_type))
915 result = LLVMBuildSExt(ctx->ac.builder, src[0], def_type, "");
916 else
917 result = LLVMBuildTrunc(ctx->ac.builder, src[0], def_type, "");
918 break;
919 case nir_op_bcsel:
920 result = emit_bcsel(&ctx->ac, src[0], src[1], src[2]);
921 break;
922 case nir_op_find_lsb:
923 src[0] = ac_to_integer(&ctx->ac, src[0]);
924 result = ac_find_lsb(&ctx->ac, ctx->ac.i32, src[0]);
925 break;
926 case nir_op_ufind_msb:
927 src[0] = ac_to_integer(&ctx->ac, src[0]);
928 result = ac_build_umsb(&ctx->ac, src[0], ctx->ac.i32);
929 break;
930 case nir_op_ifind_msb:
931 src[0] = ac_to_integer(&ctx->ac, src[0]);
932 result = ac_build_imsb(&ctx->ac, src[0], ctx->ac.i32);
933 break;
934 case nir_op_uadd_carry:
935 src[0] = ac_to_integer(&ctx->ac, src[0]);
936 src[1] = ac_to_integer(&ctx->ac, src[1]);
937 result = emit_uint_carry(&ctx->ac, "llvm.uadd.with.overflow.i32", src[0], src[1]);
938 break;
939 case nir_op_usub_borrow:
940 src[0] = ac_to_integer(&ctx->ac, src[0]);
941 src[1] = ac_to_integer(&ctx->ac, src[1]);
942 result = emit_uint_carry(&ctx->ac, "llvm.usub.with.overflow.i32", src[0], src[1]);
943 break;
944 case nir_op_b2f:
945 result = emit_b2f(&ctx->ac, src[0]);
946 break;
947 case nir_op_f2b:
948 result = emit_f2b(&ctx->ac, src[0]);
949 break;
950 case nir_op_b2i:
951 result = emit_b2i(&ctx->ac, src[0], instr->dest.dest.ssa.bit_size);
952 break;
953 case nir_op_i2b:
954 src[0] = ac_to_integer(&ctx->ac, src[0]);
955 result = emit_i2b(&ctx->ac, src[0]);
956 break;
957 case nir_op_fquantize2f16:
958 result = emit_f2f16(&ctx->ac, src[0]);
959 break;
960 case nir_op_umul_high:
961 src[0] = ac_to_integer(&ctx->ac, src[0]);
962 src[1] = ac_to_integer(&ctx->ac, src[1]);
963 result = emit_umul_high(&ctx->ac, src[0], src[1]);
964 break;
965 case nir_op_imul_high:
966 src[0] = ac_to_integer(&ctx->ac, src[0]);
967 src[1] = ac_to_integer(&ctx->ac, src[1]);
968 result = emit_imul_high(&ctx->ac, src[0], src[1]);
969 break;
970 case nir_op_pack_half_2x16:
971 result = emit_pack_half_2x16(&ctx->ac, src[0]);
972 break;
973 case nir_op_unpack_half_2x16:
974 result = emit_unpack_half_2x16(&ctx->ac, src[0]);
975 break;
976 case nir_op_fddx:
977 case nir_op_fddy:
978 case nir_op_fddx_fine:
979 case nir_op_fddy_fine:
980 case nir_op_fddx_coarse:
981 case nir_op_fddy_coarse:
982 result = emit_ddxy(ctx, instr->op, src[0]);
983 break;
984
985 case nir_op_unpack_64_2x32_split_x: {
986 assert(ac_get_llvm_num_components(src[0]) == 1);
987 LLVMValueRef tmp = LLVMBuildBitCast(ctx->ac.builder, src[0],
988 ctx->ac.v2i32,
989 "");
990 result = LLVMBuildExtractElement(ctx->ac.builder, tmp,
991 ctx->ac.i32_0, "");
992 break;
993 }
994
995 case nir_op_unpack_64_2x32_split_y: {
996 assert(ac_get_llvm_num_components(src[0]) == 1);
997 LLVMValueRef tmp = LLVMBuildBitCast(ctx->ac.builder, src[0],
998 ctx->ac.v2i32,
999 "");
1000 result = LLVMBuildExtractElement(ctx->ac.builder, tmp,
1001 ctx->ac.i32_1, "");
1002 break;
1003 }
1004
1005 case nir_op_pack_64_2x32_split: {
1006 LLVMValueRef tmp = LLVMGetUndef(ctx->ac.v2i32);
1007 tmp = LLVMBuildInsertElement(ctx->ac.builder, tmp,
1008 src[0], ctx->ac.i32_0, "");
1009 tmp = LLVMBuildInsertElement(ctx->ac.builder, tmp,
1010 src[1], ctx->ac.i32_1, "");
1011 result = LLVMBuildBitCast(ctx->ac.builder, tmp, ctx->ac.i64, "");
1012 break;
1013 }
1014
1015 case nir_op_cube_face_coord: {
1016 src[0] = ac_to_float(&ctx->ac, src[0]);
1017 LLVMValueRef results[2];
1018 LLVMValueRef in[3];
1019 for (unsigned chan = 0; chan < 3; chan++)
1020 in[chan] = ac_llvm_extract_elem(&ctx->ac, src[0], chan);
1021 results[0] = ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.cubetc",
1022 ctx->ac.f32, in, 3, AC_FUNC_ATTR_READNONE);
1023 results[1] = ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.cubesc",
1024 ctx->ac.f32, in, 3, AC_FUNC_ATTR_READNONE);
1025 result = ac_build_gather_values(&ctx->ac, results, 2);
1026 break;
1027 }
1028
1029 case nir_op_cube_face_index: {
1030 src[0] = ac_to_float(&ctx->ac, src[0]);
1031 LLVMValueRef in[3];
1032 for (unsigned chan = 0; chan < 3; chan++)
1033 in[chan] = ac_llvm_extract_elem(&ctx->ac, src[0], chan);
1034 result = ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.cubeid",
1035 ctx->ac.f32, in, 3, AC_FUNC_ATTR_READNONE);
1036 break;
1037 }
1038
1039 case nir_op_fmin3:
1040 result = emit_intrin_2f_param(&ctx->ac, "llvm.minnum",
1041 ac_to_float_type(&ctx->ac, def_type), src[0], src[1]);
1042 result = emit_intrin_2f_param(&ctx->ac, "llvm.minnum",
1043 ac_to_float_type(&ctx->ac, def_type), result, src[2]);
1044 break;
1045 case nir_op_umin3:
1046 result = emit_minmax_int(&ctx->ac, LLVMIntULT, src[0], src[1]);
1047 result = emit_minmax_int(&ctx->ac, LLVMIntULT, result, src[2]);
1048 break;
1049 case nir_op_imin3:
1050 result = emit_minmax_int(&ctx->ac, LLVMIntSLT, src[0], src[1]);
1051 result = emit_minmax_int(&ctx->ac, LLVMIntSLT, result, src[2]);
1052 break;
1053 case nir_op_fmax3:
1054 result = emit_intrin_2f_param(&ctx->ac, "llvm.maxnum",
1055 ac_to_float_type(&ctx->ac, def_type), src[0], src[1]);
1056 result = emit_intrin_2f_param(&ctx->ac, "llvm.maxnum",
1057 ac_to_float_type(&ctx->ac, def_type), result, src[2]);
1058 break;
1059 case nir_op_umax3:
1060 result = emit_minmax_int(&ctx->ac, LLVMIntUGT, src[0], src[1]);
1061 result = emit_minmax_int(&ctx->ac, LLVMIntUGT, result, src[2]);
1062 break;
1063 case nir_op_imax3:
1064 result = emit_minmax_int(&ctx->ac, LLVMIntSGT, src[0], src[1]);
1065 result = emit_minmax_int(&ctx->ac, LLVMIntSGT, result, src[2]);
1066 break;
1067 case nir_op_fmed3: {
1068 LLVMValueRef tmp1 = emit_intrin_2f_param(&ctx->ac, "llvm.minnum",
1069 ac_to_float_type(&ctx->ac, def_type), src[0], src[1]);
1070 LLVMValueRef tmp2 = emit_intrin_2f_param(&ctx->ac, "llvm.maxnum",
1071 ac_to_float_type(&ctx->ac, def_type), src[0], src[1]);
1072 tmp2 = emit_intrin_2f_param(&ctx->ac, "llvm.minnum",
1073 ac_to_float_type(&ctx->ac, def_type), tmp2, src[2]);
1074 result = emit_intrin_2f_param(&ctx->ac, "llvm.maxnum",
1075 ac_to_float_type(&ctx->ac, def_type), tmp1, tmp2);
1076 break;
1077 }
1078 case nir_op_imed3: {
1079 LLVMValueRef tmp1 = emit_minmax_int(&ctx->ac, LLVMIntSLT, src[0], src[1]);
1080 LLVMValueRef tmp2 = emit_minmax_int(&ctx->ac, LLVMIntSGT, src[0], src[1]);
1081 tmp2 = emit_minmax_int(&ctx->ac, LLVMIntSLT, tmp2, src[2]);
1082 result = emit_minmax_int(&ctx->ac, LLVMIntSGT, tmp1, tmp2);
1083 break;
1084 }
1085 case nir_op_umed3: {
1086 LLVMValueRef tmp1 = emit_minmax_int(&ctx->ac, LLVMIntULT, src[0], src[1]);
1087 LLVMValueRef tmp2 = emit_minmax_int(&ctx->ac, LLVMIntUGT, src[0], src[1]);
1088 tmp2 = emit_minmax_int(&ctx->ac, LLVMIntULT, tmp2, src[2]);
1089 result = emit_minmax_int(&ctx->ac, LLVMIntUGT, tmp1, tmp2);
1090 break;
1091 }
1092
1093 default:
1094 fprintf(stderr, "Unknown NIR alu instr: ");
1095 nir_print_instr(&instr->instr, stderr);
1096 fprintf(stderr, "\n");
1097 abort();
1098 }
1099
1100 if (result) {
1101 assert(instr->dest.dest.is_ssa);
1102 result = ac_to_integer(&ctx->ac, result);
1103 ctx->ssa_defs[instr->dest.dest.ssa.index] = result;
1104 }
1105 }
1106
1107 static void visit_load_const(struct ac_nir_context *ctx,
1108 const nir_load_const_instr *instr)
1109 {
1110 LLVMValueRef values[4], value = NULL;
1111 LLVMTypeRef element_type =
1112 LLVMIntTypeInContext(ctx->ac.context, instr->def.bit_size);
1113
1114 for (unsigned i = 0; i < instr->def.num_components; ++i) {
1115 switch (instr->def.bit_size) {
1116 case 16:
1117 values[i] = LLVMConstInt(element_type,
1118 instr->value.u16[i], false);
1119 break;
1120 case 32:
1121 values[i] = LLVMConstInt(element_type,
1122 instr->value.u32[i], false);
1123 break;
1124 case 64:
1125 values[i] = LLVMConstInt(element_type,
1126 instr->value.u64[i], false);
1127 break;
1128 default:
1129 fprintf(stderr,
1130 "unsupported nir load_const bit_size: %d\n",
1131 instr->def.bit_size);
1132 abort();
1133 }
1134 }
1135 if (instr->def.num_components > 1) {
1136 value = LLVMConstVector(values, instr->def.num_components);
1137 } else
1138 value = values[0];
1139
1140 ctx->ssa_defs[instr->def.index] = value;
1141 }
1142
1143 static LLVMValueRef
1144 get_buffer_size(struct ac_nir_context *ctx, LLVMValueRef descriptor, bool in_elements)
1145 {
1146 LLVMValueRef size =
1147 LLVMBuildExtractElement(ctx->ac.builder, descriptor,
1148 LLVMConstInt(ctx->ac.i32, 2, false), "");
1149
1150 /* VI only */
1151 if (ctx->ac.chip_class == VI && in_elements) {
1152 /* On VI, the descriptor contains the size in bytes,
1153 * but TXQ must return the size in elements.
1154 * The stride is always non-zero for resources using TXQ.
1155 */
1156 LLVMValueRef stride =
1157 LLVMBuildExtractElement(ctx->ac.builder, descriptor,
1158 ctx->ac.i32_1, "");
1159 stride = LLVMBuildLShr(ctx->ac.builder, stride,
1160 LLVMConstInt(ctx->ac.i32, 16, false), "");
1161 stride = LLVMBuildAnd(ctx->ac.builder, stride,
1162 LLVMConstInt(ctx->ac.i32, 0x3fff, false), "");
1163
1164 size = LLVMBuildUDiv(ctx->ac.builder, size, stride, "");
1165 }
1166 return size;
1167 }
1168
1169 static LLVMValueRef lower_gather4_integer(struct ac_llvm_context *ctx,
1170 nir_variable *var,
1171 struct ac_image_args *args,
1172 const nir_tex_instr *instr)
1173 {
1174 enum glsl_base_type stype = glsl_get_sampler_result_type(var->type);
1175 LLVMValueRef half_texel[2];
1176 LLVMValueRef compare_cube_wa = NULL;
1177 LLVMValueRef result;
1178
1179 //TODO Rect
1180 {
1181 struct ac_image_args txq_args = { 0 };
1182
1183 txq_args.dim = get_ac_sampler_dim(ctx, instr->sampler_dim, instr->is_array);
1184 txq_args.opcode = ac_image_get_resinfo;
1185 txq_args.dmask = 0xf;
1186 txq_args.lod = ctx->i32_0;
1187 txq_args.resource = args->resource;
1188 txq_args.attributes = AC_FUNC_ATTR_READNONE;
1189 LLVMValueRef size = ac_build_image_opcode(ctx, &txq_args);
1190
1191 for (unsigned c = 0; c < 2; c++) {
1192 half_texel[c] = LLVMBuildExtractElement(ctx->builder, size,
1193 LLVMConstInt(ctx->i32, c, false), "");
1194 half_texel[c] = LLVMBuildUIToFP(ctx->builder, half_texel[c], ctx->f32, "");
1195 half_texel[c] = ac_build_fdiv(ctx, ctx->f32_1, half_texel[c]);
1196 half_texel[c] = LLVMBuildFMul(ctx->builder, half_texel[c],
1197 LLVMConstReal(ctx->f32, -0.5), "");
1198 }
1199 }
1200
1201 LLVMValueRef orig_coords[2] = { args->coords[0], args->coords[1] };
1202
1203 for (unsigned c = 0; c < 2; c++) {
1204 LLVMValueRef tmp;
1205 tmp = LLVMBuildBitCast(ctx->builder, args->coords[c], ctx->f32, "");
1206 args->coords[c] = LLVMBuildFAdd(ctx->builder, tmp, half_texel[c], "");
1207 }
1208
1209 /*
1210 * Apparantly cube has issue with integer types that the workaround doesn't solve,
1211 * so this tests if the format is 8_8_8_8 and an integer type do an alternate
1212 * workaround by sampling using a scaled type and converting.
1213 * This is taken from amdgpu-pro shaders.
1214 */
1215 /* NOTE this produces some ugly code compared to amdgpu-pro,
1216 * LLVM ends up dumping SGPRs into VGPRs to deal with the compare/select,
1217 * and then reads them back. -pro generates two selects,
1218 * one s_cmp for the descriptor rewriting
1219 * one v_cmp for the coordinate and result changes.
1220 */
1221 if (instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE) {
1222 LLVMValueRef tmp, tmp2;
1223
1224 /* workaround 8/8/8/8 uint/sint cube gather bug */
1225 /* first detect it then change to a scaled read and f2i */
1226 tmp = LLVMBuildExtractElement(ctx->builder, args->resource, ctx->i32_1, "");
1227 tmp2 = tmp;
1228
1229 /* extract the DATA_FORMAT */
1230 tmp = ac_build_bfe(ctx, tmp, LLVMConstInt(ctx->i32, 20, false),
1231 LLVMConstInt(ctx->i32, 6, false), false);
1232
1233 /* is the DATA_FORMAT == 8_8_8_8 */
1234 compare_cube_wa = LLVMBuildICmp(ctx->builder, LLVMIntEQ, tmp, LLVMConstInt(ctx->i32, V_008F14_IMG_DATA_FORMAT_8_8_8_8, false), "");
1235
1236 if (stype == GLSL_TYPE_UINT)
1237 /* Create a NUM FORMAT - 0x2 or 0x4 - USCALED or UINT */
1238 tmp = LLVMBuildSelect(ctx->builder, compare_cube_wa, LLVMConstInt(ctx->i32, 0x8000000, false),
1239 LLVMConstInt(ctx->i32, 0x10000000, false), "");
1240 else
1241 /* Create a NUM FORMAT - 0x3 or 0x5 - SSCALED or SINT */
1242 tmp = LLVMBuildSelect(ctx->builder, compare_cube_wa, LLVMConstInt(ctx->i32, 0xc000000, false),
1243 LLVMConstInt(ctx->i32, 0x14000000, false), "");
1244
1245 /* replace the NUM FORMAT in the descriptor */
1246 tmp2 = LLVMBuildAnd(ctx->builder, tmp2, LLVMConstInt(ctx->i32, C_008F14_NUM_FORMAT_GFX6, false), "");
1247 tmp2 = LLVMBuildOr(ctx->builder, tmp2, tmp, "");
1248
1249 args->resource = LLVMBuildInsertElement(ctx->builder, args->resource, tmp2, ctx->i32_1, "");
1250
1251 /* don't modify the coordinates for this case */
1252 for (unsigned c = 0; c < 2; ++c)
1253 args->coords[c] = LLVMBuildSelect(
1254 ctx->builder, compare_cube_wa,
1255 orig_coords[c], args->coords[c], "");
1256 }
1257
1258 args->attributes = AC_FUNC_ATTR_READNONE;
1259 result = ac_build_image_opcode(ctx, args);
1260
1261 if (instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE) {
1262 LLVMValueRef tmp, tmp2;
1263
1264 /* if the cube workaround is in place, f2i the result. */
1265 for (unsigned c = 0; c < 4; c++) {
1266 tmp = LLVMBuildExtractElement(ctx->builder, result, LLVMConstInt(ctx->i32, c, false), "");
1267 if (stype == GLSL_TYPE_UINT)
1268 tmp2 = LLVMBuildFPToUI(ctx->builder, tmp, ctx->i32, "");
1269 else
1270 tmp2 = LLVMBuildFPToSI(ctx->builder, tmp, ctx->i32, "");
1271 tmp = LLVMBuildBitCast(ctx->builder, tmp, ctx->i32, "");
1272 tmp2 = LLVMBuildBitCast(ctx->builder, tmp2, ctx->i32, "");
1273 tmp = LLVMBuildSelect(ctx->builder, compare_cube_wa, tmp2, tmp, "");
1274 tmp = LLVMBuildBitCast(ctx->builder, tmp, ctx->f32, "");
1275 result = LLVMBuildInsertElement(ctx->builder, result, tmp, LLVMConstInt(ctx->i32, c, false), "");
1276 }
1277 }
1278 return result;
1279 }
1280
1281 static nir_deref_instr *get_tex_texture_deref(const nir_tex_instr *instr)
1282 {
1283 nir_deref_instr *texture_deref_instr = NULL;
1284
1285 for (unsigned i = 0; i < instr->num_srcs; i++) {
1286 switch (instr->src[i].src_type) {
1287 case nir_tex_src_texture_deref:
1288 texture_deref_instr = nir_src_as_deref(instr->src[i].src);
1289 break;
1290 default:
1291 break;
1292 }
1293 }
1294 return texture_deref_instr;
1295 }
1296
1297 static LLVMValueRef build_tex_intrinsic(struct ac_nir_context *ctx,
1298 const nir_tex_instr *instr,
1299 struct ac_image_args *args)
1300 {
1301 if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF) {
1302 unsigned mask = nir_ssa_def_components_read(&instr->dest.ssa);
1303
1304 if (ctx->abi->gfx9_stride_size_workaround) {
1305 return ac_build_buffer_load_format_gfx9_safe(&ctx->ac,
1306 args->resource,
1307 args->coords[0],
1308 ctx->ac.i32_0,
1309 util_last_bit(mask),
1310 false, true);
1311 } else {
1312 return ac_build_buffer_load_format(&ctx->ac,
1313 args->resource,
1314 args->coords[0],
1315 ctx->ac.i32_0,
1316 util_last_bit(mask),
1317 false, true);
1318 }
1319 }
1320
1321 args->opcode = ac_image_sample;
1322
1323 switch (instr->op) {
1324 case nir_texop_txf:
1325 case nir_texop_txf_ms:
1326 case nir_texop_samples_identical:
1327 args->opcode = args->level_zero ||
1328 instr->sampler_dim == GLSL_SAMPLER_DIM_MS ?
1329 ac_image_load : ac_image_load_mip;
1330 args->level_zero = false;
1331 break;
1332 case nir_texop_txs:
1333 case nir_texop_query_levels:
1334 args->opcode = ac_image_get_resinfo;
1335 if (!args->lod)
1336 args->lod = ctx->ac.i32_0;
1337 args->level_zero = false;
1338 break;
1339 case nir_texop_tex:
1340 if (ctx->stage != MESA_SHADER_FRAGMENT) {
1341 assert(!args->lod);
1342 args->level_zero = true;
1343 }
1344 break;
1345 case nir_texop_tg4:
1346 args->opcode = ac_image_gather4;
1347 args->level_zero = true;
1348 break;
1349 case nir_texop_lod:
1350 args->opcode = ac_image_get_lod;
1351 break;
1352 default:
1353 break;
1354 }
1355
1356 if (instr->op == nir_texop_tg4 && ctx->ac.chip_class <= VI) {
1357 nir_deref_instr *texture_deref_instr = get_tex_texture_deref(instr);
1358 nir_variable *var = nir_deref_instr_get_variable(texture_deref_instr);
1359 enum glsl_base_type stype = glsl_get_sampler_result_type(var->type);
1360 if (stype == GLSL_TYPE_UINT || stype == GLSL_TYPE_INT) {
1361 return lower_gather4_integer(&ctx->ac, var, args, instr);
1362 }
1363 }
1364
1365 /* Fixup for GFX9 which allocates 1D textures as 2D. */
1366 if (instr->op == nir_texop_lod && ctx->ac.chip_class >= GFX9) {
1367 if ((args->dim == ac_image_2darray ||
1368 args->dim == ac_image_2d) && !args->coords[1]) {
1369 args->coords[1] = ctx->ac.i32_0;
1370 }
1371 }
1372
1373 args->attributes = AC_FUNC_ATTR_READNONE;
1374 return ac_build_image_opcode(&ctx->ac, args);
1375 }
1376
1377 static LLVMValueRef visit_vulkan_resource_reindex(struct ac_nir_context *ctx,
1378 nir_intrinsic_instr *instr)
1379 {
1380 LLVMValueRef ptr = get_src(ctx, instr->src[0]);
1381 LLVMValueRef index = get_src(ctx, instr->src[1]);
1382
1383 LLVMValueRef result = LLVMBuildGEP(ctx->ac.builder, ptr, &index, 1, "");
1384 LLVMSetMetadata(result, ctx->ac.uniform_md_kind, ctx->ac.empty_md);
1385 return result;
1386 }
1387
1388 static LLVMValueRef visit_load_push_constant(struct ac_nir_context *ctx,
1389 nir_intrinsic_instr *instr)
1390 {
1391 LLVMValueRef ptr, addr;
1392
1393 addr = LLVMConstInt(ctx->ac.i32, nir_intrinsic_base(instr), 0);
1394 addr = LLVMBuildAdd(ctx->ac.builder, addr,
1395 get_src(ctx, instr->src[0]), "");
1396
1397 ptr = ac_build_gep0(&ctx->ac, ctx->abi->push_constants, addr);
1398
1399 if (instr->dest.ssa.bit_size == 16) {
1400 unsigned load_dwords = instr->dest.ssa.num_components / 2 + 1;
1401 LLVMTypeRef vec_type = LLVMVectorType(LLVMInt16Type(), 2 * load_dwords);
1402 ptr = ac_cast_ptr(&ctx->ac, ptr, vec_type);
1403 LLVMValueRef res = LLVMBuildLoad(ctx->ac.builder, ptr, "");
1404 res = LLVMBuildBitCast(ctx->ac.builder, res, vec_type, "");
1405 LLVMValueRef cond = LLVMBuildLShr(ctx->ac.builder, addr, ctx->ac.i32_1, "");
1406 cond = LLVMBuildTrunc(ctx->ac.builder, cond, LLVMInt1Type(), "");
1407 LLVMValueRef mask[] = { LLVMConstInt(ctx->ac.i32, 0, false), LLVMConstInt(ctx->ac.i32, 1, false),
1408 LLVMConstInt(ctx->ac.i32, 2, false), LLVMConstInt(ctx->ac.i32, 3, false),
1409 LLVMConstInt(ctx->ac.i32, 4, false)};
1410 LLVMValueRef swizzle_aligned = LLVMConstVector(&mask[0], instr->dest.ssa.num_components);
1411 LLVMValueRef swizzle_unaligned = LLVMConstVector(&mask[1], instr->dest.ssa.num_components);
1412 LLVMValueRef shuffle_aligned = LLVMBuildShuffleVector(ctx->ac.builder, res, res, swizzle_aligned, "");
1413 LLVMValueRef shuffle_unaligned = LLVMBuildShuffleVector(ctx->ac.builder, res, res, swizzle_unaligned, "");
1414 res = LLVMBuildSelect(ctx->ac.builder, cond, shuffle_unaligned, shuffle_aligned, "");
1415 return LLVMBuildBitCast(ctx->ac.builder, res, get_def_type(ctx, &instr->dest.ssa), "");
1416 }
1417
1418 ptr = ac_cast_ptr(&ctx->ac, ptr, get_def_type(ctx, &instr->dest.ssa));
1419
1420 return LLVMBuildLoad(ctx->ac.builder, ptr, "");
1421 }
1422
1423 static LLVMValueRef visit_get_buffer_size(struct ac_nir_context *ctx,
1424 const nir_intrinsic_instr *instr)
1425 {
1426 LLVMValueRef index = get_src(ctx, instr->src[0]);
1427
1428 return get_buffer_size(ctx, ctx->abi->load_ssbo(ctx->abi, index, false), false);
1429 }
1430
1431 static uint32_t widen_mask(uint32_t mask, unsigned multiplier)
1432 {
1433 uint32_t new_mask = 0;
1434 for(unsigned i = 0; i < 32 && (1u << i) <= mask; ++i)
1435 if (mask & (1u << i))
1436 new_mask |= ((1u << multiplier) - 1u) << (i * multiplier);
1437 return new_mask;
1438 }
1439
1440 static LLVMValueRef extract_vector_range(struct ac_llvm_context *ctx, LLVMValueRef src,
1441 unsigned start, unsigned count)
1442 {
1443 LLVMValueRef mask[] = {
1444 LLVMConstInt(ctx->i32, 0, false), LLVMConstInt(ctx->i32, 1, false),
1445 LLVMConstInt(ctx->i32, 2, false), LLVMConstInt(ctx->i32, 3, false) };
1446
1447 unsigned src_elements = ac_get_llvm_num_components(src);
1448
1449 if (count == src_elements) {
1450 assert(start == 0);
1451 return src;
1452 } else if (count == 1) {
1453 assert(start < src_elements);
1454 return LLVMBuildExtractElement(ctx->builder, src, mask[start], "");
1455 } else {
1456 assert(start + count <= src_elements);
1457 assert(count <= 4);
1458 LLVMValueRef swizzle = LLVMConstVector(&mask[start], count);
1459 return LLVMBuildShuffleVector(ctx->builder, src, src, swizzle, "");
1460 }
1461 }
1462
1463 static void visit_store_ssbo(struct ac_nir_context *ctx,
1464 nir_intrinsic_instr *instr)
1465 {
1466 const char *store_name;
1467 LLVMValueRef src_data = get_src(ctx, instr->src[0]);
1468 int elem_size_bytes = ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src_data)) / 8;
1469 unsigned writemask = nir_intrinsic_write_mask(instr);
1470
1471 LLVMValueRef rsrc = ctx->abi->load_ssbo(ctx->abi,
1472 get_src(ctx, instr->src[1]), true);
1473 LLVMValueRef base_data = ac_to_float(&ctx->ac, src_data);
1474 base_data = ac_trim_vector(&ctx->ac, base_data, instr->num_components);
1475 LLVMValueRef base_offset = get_src(ctx, instr->src[2]);
1476
1477 while (writemask) {
1478 int start, count;
1479 LLVMValueRef data, offset;
1480 LLVMTypeRef data_type;
1481
1482 u_bit_scan_consecutive_range(&writemask, &start, &count);
1483
1484 /* Due to an LLVM limitation, split 3-element writes
1485 * into a 2-element and a 1-element write. */
1486 if (count == 3) {
1487 writemask |= 1 << (start + 2);
1488 count = 2;
1489 }
1490 int num_bytes = count * elem_size_bytes; /* count in bytes */
1491
1492 /* we can only store 4 DWords at the same time.
1493 * can only happen for 64 Bit vectors. */
1494 if (num_bytes > 16) {
1495 writemask |= ((1u << (count - 2)) - 1u) << (start + 2);
1496 count = 2;
1497 num_bytes = 16;
1498 }
1499
1500 /* check alignment of 16 Bit stores */
1501 if (elem_size_bytes == 2 && num_bytes > 2 && (start % 2) == 1) {
1502 writemask |= ((1u << (count - 1)) - 1u) << (start + 1);
1503 count = 1;
1504 num_bytes = 2;
1505 }
1506 data = extract_vector_range(&ctx->ac, base_data, start, count);
1507
1508 if (start == 0) {
1509 offset = base_offset;
1510 } else {
1511 offset = LLVMBuildAdd(ctx->ac.builder, base_offset,
1512 LLVMConstInt(ctx->ac.i32, start * elem_size_bytes, false), "");
1513 }
1514 if (num_bytes == 2) {
1515 store_name = "llvm.amdgcn.tbuffer.store.i32";
1516 data_type = ctx->ac.i32;
1517 LLVMValueRef tbuffer_params[] = {
1518 data,
1519 rsrc,
1520 ctx->ac.i32_0, /* vindex */
1521 offset, /* voffset */
1522 ctx->ac.i32_0,
1523 ctx->ac.i32_0,
1524 LLVMConstInt(ctx->ac.i32, 2, false), // dfmt (= 16bit)
1525 LLVMConstInt(ctx->ac.i32, 4, false), // nfmt (= uint)
1526 ctx->ac.i1false,
1527 ctx->ac.i1false,
1528 };
1529 ac_build_intrinsic(&ctx->ac, store_name,
1530 ctx->ac.voidt, tbuffer_params, 10, 0);
1531 } else {
1532 switch (num_bytes) {
1533 case 16: /* v4f32 */
1534 store_name = "llvm.amdgcn.buffer.store.v4f32";
1535 data_type = ctx->ac.v4f32;
1536 break;
1537 case 8: /* v2f32 */
1538 store_name = "llvm.amdgcn.buffer.store.v2f32";
1539 data_type = ctx->ac.v2f32;
1540 break;
1541 case 4: /* f32 */
1542 store_name = "llvm.amdgcn.buffer.store.f32";
1543 data_type = ctx->ac.f32;
1544 break;
1545 default:
1546 unreachable("Malformed vector store.");
1547 }
1548 data = LLVMBuildBitCast(ctx->ac.builder, data, data_type, "");
1549 LLVMValueRef params[] = {
1550 data,
1551 rsrc,
1552 ctx->ac.i32_0, /* vindex */
1553 offset,
1554 ctx->ac.i1false, /* glc */
1555 ctx->ac.i1false, /* slc */
1556 };
1557 ac_build_intrinsic(&ctx->ac, store_name,
1558 ctx->ac.voidt, params, 6, 0);
1559 }
1560 }
1561 }
1562
1563 static LLVMValueRef visit_atomic_ssbo(struct ac_nir_context *ctx,
1564 const nir_intrinsic_instr *instr)
1565 {
1566 const char *name;
1567 LLVMValueRef params[6];
1568 int arg_count = 0;
1569
1570 if (instr->intrinsic == nir_intrinsic_ssbo_atomic_comp_swap) {
1571 params[arg_count++] = ac_llvm_extract_elem(&ctx->ac, get_src(ctx, instr->src[3]), 0);
1572 }
1573 params[arg_count++] = ac_llvm_extract_elem(&ctx->ac, get_src(ctx, instr->src[2]), 0);
1574 params[arg_count++] = ctx->abi->load_ssbo(ctx->abi,
1575 get_src(ctx, instr->src[0]),
1576 true);
1577 params[arg_count++] = ctx->ac.i32_0; /* vindex */
1578 params[arg_count++] = get_src(ctx, instr->src[1]); /* voffset */
1579 params[arg_count++] = LLVMConstInt(ctx->ac.i1, 0, false); /* slc */
1580
1581 switch (instr->intrinsic) {
1582 case nir_intrinsic_ssbo_atomic_add:
1583 name = "llvm.amdgcn.buffer.atomic.add";
1584 break;
1585 case nir_intrinsic_ssbo_atomic_imin:
1586 name = "llvm.amdgcn.buffer.atomic.smin";
1587 break;
1588 case nir_intrinsic_ssbo_atomic_umin:
1589 name = "llvm.amdgcn.buffer.atomic.umin";
1590 break;
1591 case nir_intrinsic_ssbo_atomic_imax:
1592 name = "llvm.amdgcn.buffer.atomic.smax";
1593 break;
1594 case nir_intrinsic_ssbo_atomic_umax:
1595 name = "llvm.amdgcn.buffer.atomic.umax";
1596 break;
1597 case nir_intrinsic_ssbo_atomic_and:
1598 name = "llvm.amdgcn.buffer.atomic.and";
1599 break;
1600 case nir_intrinsic_ssbo_atomic_or:
1601 name = "llvm.amdgcn.buffer.atomic.or";
1602 break;
1603 case nir_intrinsic_ssbo_atomic_xor:
1604 name = "llvm.amdgcn.buffer.atomic.xor";
1605 break;
1606 case nir_intrinsic_ssbo_atomic_exchange:
1607 name = "llvm.amdgcn.buffer.atomic.swap";
1608 break;
1609 case nir_intrinsic_ssbo_atomic_comp_swap:
1610 name = "llvm.amdgcn.buffer.atomic.cmpswap";
1611 break;
1612 default:
1613 abort();
1614 }
1615
1616 return ac_build_intrinsic(&ctx->ac, name, ctx->ac.i32, params, arg_count, 0);
1617 }
1618
1619 static LLVMValueRef visit_load_buffer(struct ac_nir_context *ctx,
1620 const nir_intrinsic_instr *instr)
1621 {
1622 LLVMValueRef results[2];
1623 int load_bytes;
1624 int elem_size_bytes = instr->dest.ssa.bit_size / 8;
1625 int num_components = instr->num_components;
1626 int num_bytes = num_components * elem_size_bytes;
1627
1628 for (int i = 0; i < num_bytes; i += load_bytes) {
1629 load_bytes = MIN2(num_bytes - i, 16);
1630 const char *load_name;
1631 LLVMTypeRef data_type;
1632 LLVMValueRef offset = get_src(ctx, instr->src[1]);
1633 LLVMValueRef immoffset = LLVMConstInt(ctx->ac.i32, i, false);
1634 LLVMValueRef rsrc = ctx->abi->load_ssbo(ctx->abi,
1635 get_src(ctx, instr->src[0]), false);
1636 LLVMValueRef vindex = ctx->ac.i32_0;
1637
1638 int idx = i ? 1 : 0;
1639 if (load_bytes == 2) {
1640 results[idx] = ac_build_tbuffer_load_short(&ctx->ac,
1641 rsrc,
1642 vindex,
1643 offset,
1644 ctx->ac.i32_0,
1645 immoffset);
1646 } else {
1647 switch (load_bytes) {
1648 case 16:
1649 case 12:
1650 load_name = "llvm.amdgcn.buffer.load.v4f32";
1651 data_type = ctx->ac.v4f32;
1652 break;
1653 case 8:
1654 case 6:
1655 load_name = "llvm.amdgcn.buffer.load.v2f32";
1656 data_type = ctx->ac.v2f32;
1657 break;
1658 case 4:
1659 load_name = "llvm.amdgcn.buffer.load.f32";
1660 data_type = ctx->ac.f32;
1661 break;
1662 default:
1663 unreachable("Malformed load buffer.");
1664 }
1665 LLVMValueRef params[] = {
1666 rsrc,
1667 vindex,
1668 LLVMBuildAdd(ctx->ac.builder, offset, immoffset, ""),
1669 ctx->ac.i1false,
1670 ctx->ac.i1false,
1671 };
1672 results[idx] = ac_build_intrinsic(&ctx->ac, load_name, data_type, params, 5, 0);
1673 unsigned num_elems = ac_get_type_size(data_type) / elem_size_bytes;
1674 LLVMTypeRef resTy = LLVMVectorType(LLVMIntType(instr->dest.ssa.bit_size), num_elems);
1675 results[idx] = LLVMBuildBitCast(ctx->ac.builder, results[idx], resTy, "");
1676 }
1677 }
1678
1679 assume(results[0]);
1680 LLVMValueRef ret = results[0];
1681 if (num_bytes > 16 || num_components == 3) {
1682 LLVMValueRef masks[] = {
1683 LLVMConstInt(ctx->ac.i32, 0, false), LLVMConstInt(ctx->ac.i32, 1, false),
1684 LLVMConstInt(ctx->ac.i32, 2, false), LLVMConstInt(ctx->ac.i32, 3, false),
1685 };
1686
1687 if (num_bytes > 16 && num_components == 3) {
1688 /* we end up with a v4f32 and v2f32 but shuffle fails on that */
1689 results[1] = ac_build_expand_to_vec4(&ctx->ac, results[1], 2);
1690 }
1691
1692 LLVMValueRef swizzle = LLVMConstVector(masks, num_components);
1693 ret = LLVMBuildShuffleVector(ctx->ac.builder, results[0],
1694 results[num_bytes > 16 ? 1 : 0], swizzle, "");
1695 }
1696
1697 return LLVMBuildBitCast(ctx->ac.builder, ret,
1698 get_def_type(ctx, &instr->dest.ssa), "");
1699 }
1700
1701 static LLVMValueRef visit_load_ubo_buffer(struct ac_nir_context *ctx,
1702 const nir_intrinsic_instr *instr)
1703 {
1704 LLVMValueRef ret;
1705 LLVMValueRef rsrc = get_src(ctx, instr->src[0]);
1706 LLVMValueRef offset = get_src(ctx, instr->src[1]);
1707 int num_components = instr->num_components;
1708
1709 if (ctx->abi->load_ubo)
1710 rsrc = ctx->abi->load_ubo(ctx->abi, rsrc);
1711
1712 if (instr->dest.ssa.bit_size == 64)
1713 num_components *= 2;
1714
1715 if (instr->dest.ssa.bit_size == 16) {
1716 LLVMValueRef results[num_components];
1717 for (unsigned i = 0; i < num_components; ++i) {
1718 results[i] = ac_build_tbuffer_load_short(&ctx->ac,
1719 rsrc,
1720 ctx->ac.i32_0,
1721 offset,
1722 ctx->ac.i32_0,
1723 LLVMConstInt(ctx->ac.i32, 2 * i, 0));
1724 }
1725 ret = ac_build_gather_values(&ctx->ac, results, num_components);
1726 } else {
1727 ret = ac_build_buffer_load(&ctx->ac, rsrc, num_components, NULL, offset,
1728 NULL, 0, false, false, true, true);
1729
1730 ret = ac_trim_vector(&ctx->ac, ret, num_components);
1731 }
1732
1733 return LLVMBuildBitCast(ctx->ac.builder, ret,
1734 get_def_type(ctx, &instr->dest.ssa), "");
1735 }
1736
1737 static void
1738 get_deref_offset(struct ac_nir_context *ctx, nir_deref_instr *instr,
1739 bool vs_in, unsigned *vertex_index_out,
1740 LLVMValueRef *vertex_index_ref,
1741 unsigned *const_out, LLVMValueRef *indir_out)
1742 {
1743 nir_variable *var = nir_deref_instr_get_variable(instr);
1744 nir_deref_path path;
1745 unsigned idx_lvl = 1;
1746
1747 nir_deref_path_init(&path, instr, NULL);
1748
1749 if (vertex_index_out != NULL || vertex_index_ref != NULL) {
1750 if (vertex_index_ref) {
1751 *vertex_index_ref = get_src(ctx, path.path[idx_lvl]->arr.index);
1752 if (vertex_index_out)
1753 *vertex_index_out = 0;
1754 } else {
1755 nir_const_value *v = nir_src_as_const_value(path.path[idx_lvl]->arr.index);
1756 assert(v);
1757 *vertex_index_out = v->u32[0];
1758 }
1759 ++idx_lvl;
1760 }
1761
1762 uint32_t const_offset = 0;
1763 LLVMValueRef offset = NULL;
1764
1765 if (var->data.compact) {
1766 assert(instr->deref_type == nir_deref_type_array);
1767 nir_const_value *v = nir_src_as_const_value(instr->arr.index);
1768 assert(v);
1769 const_offset = v->u32[0];
1770 goto out;
1771 }
1772
1773 for (; path.path[idx_lvl]; ++idx_lvl) {
1774 const struct glsl_type *parent_type = path.path[idx_lvl - 1]->type;
1775 if (path.path[idx_lvl]->deref_type == nir_deref_type_struct) {
1776 unsigned index = path.path[idx_lvl]->strct.index;
1777
1778 for (unsigned i = 0; i < index; i++) {
1779 const struct glsl_type *ft = glsl_get_struct_field(parent_type, i);
1780 const_offset += glsl_count_attribute_slots(ft, vs_in);
1781 }
1782 } else if(path.path[idx_lvl]->deref_type == nir_deref_type_array) {
1783 unsigned size = glsl_count_attribute_slots(path.path[idx_lvl]->type, vs_in);
1784 LLVMValueRef array_off = LLVMBuildMul(ctx->ac.builder, LLVMConstInt(ctx->ac.i32, size, 0),
1785 get_src(ctx, path.path[idx_lvl]->arr.index), "");
1786 if (offset)
1787 offset = LLVMBuildAdd(ctx->ac.builder, offset, array_off, "");
1788 else
1789 offset = array_off;
1790 } else
1791 unreachable("Uhandled deref type in get_deref_instr_offset");
1792 }
1793
1794 out:
1795 nir_deref_path_finish(&path);
1796
1797 if (const_offset && offset)
1798 offset = LLVMBuildAdd(ctx->ac.builder, offset,
1799 LLVMConstInt(ctx->ac.i32, const_offset, 0),
1800 "");
1801
1802 *const_out = const_offset;
1803 *indir_out = offset;
1804 }
1805
1806 static LLVMValueRef load_tess_varyings(struct ac_nir_context *ctx,
1807 nir_intrinsic_instr *instr,
1808 bool load_inputs)
1809 {
1810 LLVMValueRef result;
1811 LLVMValueRef vertex_index = NULL;
1812 LLVMValueRef indir_index = NULL;
1813 unsigned const_index = 0;
1814
1815 nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
1816
1817 unsigned location = var->data.location;
1818 unsigned driver_location = var->data.driver_location;
1819 const bool is_patch = var->data.patch;
1820 const bool is_compact = var->data.compact;
1821
1822 get_deref_offset(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr),
1823 false, NULL, is_patch ? NULL : &vertex_index,
1824 &const_index, &indir_index);
1825
1826 LLVMTypeRef dest_type = get_def_type(ctx, &instr->dest.ssa);
1827
1828 LLVMTypeRef src_component_type;
1829 if (LLVMGetTypeKind(dest_type) == LLVMVectorTypeKind)
1830 src_component_type = LLVMGetElementType(dest_type);
1831 else
1832 src_component_type = dest_type;
1833
1834 result = ctx->abi->load_tess_varyings(ctx->abi, src_component_type,
1835 vertex_index, indir_index,
1836 const_index, location, driver_location,
1837 var->data.location_frac,
1838 instr->num_components,
1839 is_patch, is_compact, load_inputs);
1840 if (instr->dest.ssa.bit_size == 16) {
1841 result = ac_to_integer(&ctx->ac, result);
1842 result = LLVMBuildTrunc(ctx->ac.builder, result, dest_type, "");
1843 }
1844 return LLVMBuildBitCast(ctx->ac.builder, result, dest_type, "");
1845 }
1846
1847 static LLVMValueRef visit_load_var(struct ac_nir_context *ctx,
1848 nir_intrinsic_instr *instr)
1849 {
1850 nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
1851
1852 LLVMValueRef values[8];
1853 int idx = var->data.driver_location;
1854 int ve = instr->dest.ssa.num_components;
1855 unsigned comp = var->data.location_frac;
1856 LLVMValueRef indir_index;
1857 LLVMValueRef ret;
1858 unsigned const_index;
1859 unsigned stride = var->data.compact ? 1 : 4;
1860 bool vs_in = ctx->stage == MESA_SHADER_VERTEX &&
1861 var->data.mode == nir_var_shader_in;
1862
1863 get_deref_offset(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), vs_in, NULL, NULL,
1864 &const_index, &indir_index);
1865
1866 if (instr->dest.ssa.bit_size == 64)
1867 ve *= 2;
1868
1869 switch (var->data.mode) {
1870 case nir_var_shader_in:
1871 if (ctx->stage == MESA_SHADER_TESS_CTRL ||
1872 ctx->stage == MESA_SHADER_TESS_EVAL) {
1873 return load_tess_varyings(ctx, instr, true);
1874 }
1875
1876 if (ctx->stage == MESA_SHADER_GEOMETRY) {
1877 LLVMTypeRef type = LLVMIntTypeInContext(ctx->ac.context, instr->dest.ssa.bit_size);
1878 LLVMValueRef indir_index;
1879 unsigned const_index, vertex_index;
1880 get_deref_offset(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr),
1881 false, &vertex_index, NULL, &const_index, &indir_index);
1882
1883 return ctx->abi->load_inputs(ctx->abi, var->data.location,
1884 var->data.driver_location,
1885 var->data.location_frac,
1886 instr->num_components, vertex_index, const_index, type);
1887 }
1888
1889 for (unsigned chan = comp; chan < ve + comp; chan++) {
1890 if (indir_index) {
1891 unsigned count = glsl_count_attribute_slots(
1892 var->type,
1893 ctx->stage == MESA_SHADER_VERTEX);
1894 count -= chan / 4;
1895 LLVMValueRef tmp_vec = ac_build_gather_values_extended(
1896 &ctx->ac, ctx->abi->inputs + idx + chan, count,
1897 stride, false, true);
1898
1899 values[chan] = LLVMBuildExtractElement(ctx->ac.builder,
1900 tmp_vec,
1901 indir_index, "");
1902 } else
1903 values[chan] = ctx->abi->inputs[idx + chan + const_index * stride];
1904 }
1905 break;
1906 case nir_var_local:
1907 for (unsigned chan = 0; chan < ve; chan++) {
1908 if (indir_index) {
1909 unsigned count = glsl_count_attribute_slots(
1910 var->type, false);
1911 count -= chan / 4;
1912 LLVMValueRef tmp_vec = ac_build_gather_values_extended(
1913 &ctx->ac, ctx->locals + idx + chan, count,
1914 stride, true, true);
1915
1916 values[chan] = LLVMBuildExtractElement(ctx->ac.builder,
1917 tmp_vec,
1918 indir_index, "");
1919 } else {
1920 values[chan] = LLVMBuildLoad(ctx->ac.builder, ctx->locals[idx + chan + const_index * stride], "");
1921 }
1922 }
1923 break;
1924 case nir_var_shared: {
1925 LLVMValueRef address = get_src(ctx, instr->src[0]);
1926 LLVMValueRef val = LLVMBuildLoad(ctx->ac.builder, address, "");
1927 return LLVMBuildBitCast(ctx->ac.builder, val,
1928 get_def_type(ctx, &instr->dest.ssa),
1929 "");
1930 }
1931 case nir_var_shader_out:
1932 if (ctx->stage == MESA_SHADER_TESS_CTRL) {
1933 return load_tess_varyings(ctx, instr, false);
1934 }
1935
1936 for (unsigned chan = comp; chan < ve + comp; chan++) {
1937 if (indir_index) {
1938 unsigned count = glsl_count_attribute_slots(
1939 var->type, false);
1940 count -= chan / 4;
1941 LLVMValueRef tmp_vec = ac_build_gather_values_extended(
1942 &ctx->ac, ctx->abi->outputs + idx + chan, count,
1943 stride, true, true);
1944
1945 values[chan] = LLVMBuildExtractElement(ctx->ac.builder,
1946 tmp_vec,
1947 indir_index, "");
1948 } else {
1949 values[chan] = LLVMBuildLoad(ctx->ac.builder,
1950 ctx->abi->outputs[idx + chan + const_index * stride],
1951 "");
1952 }
1953 }
1954 break;
1955 default:
1956 unreachable("unhandle variable mode");
1957 }
1958 ret = ac_build_varying_gather_values(&ctx->ac, values, ve, comp);
1959 return LLVMBuildBitCast(ctx->ac.builder, ret, get_def_type(ctx, &instr->dest.ssa), "");
1960 }
1961
1962 static void
1963 visit_store_var(struct ac_nir_context *ctx,
1964 nir_intrinsic_instr *instr)
1965 {
1966 nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
1967
1968 LLVMValueRef temp_ptr, value;
1969 int idx = var->data.driver_location;
1970 unsigned comp = var->data.location_frac;
1971 LLVMValueRef src = ac_to_float(&ctx->ac, get_src(ctx, instr->src[1]));
1972 int writemask = instr->const_index[0];
1973 LLVMValueRef indir_index;
1974 unsigned const_index;
1975
1976 get_deref_offset(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), false,
1977 NULL, NULL, &const_index, &indir_index);
1978
1979 if (ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src)) == 64) {
1980
1981 src = LLVMBuildBitCast(ctx->ac.builder, src,
1982 LLVMVectorType(ctx->ac.f32, ac_get_llvm_num_components(src) * 2),
1983 "");
1984
1985 writemask = widen_mask(writemask, 2);
1986 }
1987
1988 writemask = writemask << comp;
1989
1990 switch (var->data.mode) {
1991 case nir_var_shader_out:
1992
1993 if (ctx->stage == MESA_SHADER_TESS_CTRL) {
1994 LLVMValueRef vertex_index = NULL;
1995 LLVMValueRef indir_index = NULL;
1996 unsigned const_index = 0;
1997 const bool is_patch = var->data.patch;
1998
1999 get_deref_offset(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr),
2000 false, NULL, is_patch ? NULL : &vertex_index,
2001 &const_index, &indir_index);
2002
2003 ctx->abi->store_tcs_outputs(ctx->abi, var,
2004 vertex_index, indir_index,
2005 const_index, src, writemask);
2006 return;
2007 }
2008
2009 for (unsigned chan = 0; chan < 8; chan++) {
2010 int stride = 4;
2011 if (!(writemask & (1 << chan)))
2012 continue;
2013
2014 value = ac_llvm_extract_elem(&ctx->ac, src, chan - comp);
2015
2016 if (var->data.compact)
2017 stride = 1;
2018 if (indir_index) {
2019 unsigned count = glsl_count_attribute_slots(
2020 var->type, false);
2021 count -= chan / 4;
2022 LLVMValueRef tmp_vec = ac_build_gather_values_extended(
2023 &ctx->ac, ctx->abi->outputs + idx + chan, count,
2024 stride, true, true);
2025
2026 tmp_vec = LLVMBuildInsertElement(ctx->ac.builder, tmp_vec,
2027 value, indir_index, "");
2028 build_store_values_extended(&ctx->ac, ctx->abi->outputs + idx + chan,
2029 count, stride, tmp_vec);
2030
2031 } else {
2032 temp_ptr = ctx->abi->outputs[idx + chan + const_index * stride];
2033
2034 LLVMBuildStore(ctx->ac.builder, value, temp_ptr);
2035 }
2036 }
2037 break;
2038 case nir_var_local:
2039 for (unsigned chan = 0; chan < 8; chan++) {
2040 if (!(writemask & (1 << chan)))
2041 continue;
2042
2043 value = ac_llvm_extract_elem(&ctx->ac, src, chan);
2044 if (indir_index) {
2045 unsigned count = glsl_count_attribute_slots(
2046 var->type, false);
2047 count -= chan / 4;
2048 LLVMValueRef tmp_vec = ac_build_gather_values_extended(
2049 &ctx->ac, ctx->locals + idx + chan, count,
2050 4, true, true);
2051
2052 tmp_vec = LLVMBuildInsertElement(ctx->ac.builder, tmp_vec,
2053 value, indir_index, "");
2054 build_store_values_extended(&ctx->ac, ctx->locals + idx + chan,
2055 count, 4, tmp_vec);
2056 } else {
2057 temp_ptr = ctx->locals[idx + chan + const_index * 4];
2058
2059 LLVMBuildStore(ctx->ac.builder, value, temp_ptr);
2060 }
2061 }
2062 break;
2063 case nir_var_shared: {
2064 int writemask = instr->const_index[0];
2065 LLVMValueRef address = get_src(ctx, instr->src[0]);
2066 LLVMValueRef val = get_src(ctx, instr->src[1]);
2067 if (util_is_power_of_two_nonzero(writemask)) {
2068 val = LLVMBuildBitCast(
2069 ctx->ac.builder, val,
2070 LLVMGetElementType(LLVMTypeOf(address)), "");
2071 LLVMBuildStore(ctx->ac.builder, val, address);
2072 } else {
2073 for (unsigned chan = 0; chan < 4; chan++) {
2074 if (!(writemask & (1 << chan)))
2075 continue;
2076 LLVMValueRef ptr =
2077 LLVMBuildStructGEP(ctx->ac.builder,
2078 address, chan, "");
2079 LLVMValueRef src = ac_llvm_extract_elem(&ctx->ac, val,
2080 chan);
2081 src = LLVMBuildBitCast(
2082 ctx->ac.builder, src,
2083 LLVMGetElementType(LLVMTypeOf(ptr)), "");
2084 LLVMBuildStore(ctx->ac.builder, src, ptr);
2085 }
2086 }
2087 break;
2088 }
2089 default:
2090 break;
2091 }
2092 }
2093
2094 static int image_type_to_components_count(enum glsl_sampler_dim dim, bool array)
2095 {
2096 switch (dim) {
2097 case GLSL_SAMPLER_DIM_BUF:
2098 return 1;
2099 case GLSL_SAMPLER_DIM_1D:
2100 return array ? 2 : 1;
2101 case GLSL_SAMPLER_DIM_2D:
2102 return array ? 3 : 2;
2103 case GLSL_SAMPLER_DIM_MS:
2104 return array ? 4 : 3;
2105 case GLSL_SAMPLER_DIM_3D:
2106 case GLSL_SAMPLER_DIM_CUBE:
2107 return 3;
2108 case GLSL_SAMPLER_DIM_RECT:
2109 case GLSL_SAMPLER_DIM_SUBPASS:
2110 return 2;
2111 case GLSL_SAMPLER_DIM_SUBPASS_MS:
2112 return 3;
2113 default:
2114 break;
2115 }
2116 return 0;
2117 }
2118
2119
2120 /* Adjust the sample index according to FMASK.
2121 *
2122 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
2123 * which is the identity mapping. Each nibble says which physical sample
2124 * should be fetched to get that sample.
2125 *
2126 * For example, 0x11111100 means there are only 2 samples stored and
2127 * the second sample covers 3/4 of the pixel. When reading samples 0
2128 * and 1, return physical sample 0 (determined by the first two 0s
2129 * in FMASK), otherwise return physical sample 1.
2130 *
2131 * The sample index should be adjusted as follows:
2132 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
2133 */
2134 static LLVMValueRef adjust_sample_index_using_fmask(struct ac_llvm_context *ctx,
2135 LLVMValueRef coord_x, LLVMValueRef coord_y,
2136 LLVMValueRef coord_z,
2137 LLVMValueRef sample_index,
2138 LLVMValueRef fmask_desc_ptr)
2139 {
2140 struct ac_image_args args = {0};
2141 LLVMValueRef res;
2142
2143 args.coords[0] = coord_x;
2144 args.coords[1] = coord_y;
2145 if (coord_z)
2146 args.coords[2] = coord_z;
2147
2148 args.opcode = ac_image_load;
2149 args.dim = coord_z ? ac_image_2darray : ac_image_2d;
2150 args.resource = fmask_desc_ptr;
2151 args.dmask = 0xf;
2152 args.attributes = AC_FUNC_ATTR_READNONE;
2153
2154 res = ac_build_image_opcode(ctx, &args);
2155
2156 res = ac_to_integer(ctx, res);
2157 LLVMValueRef four = LLVMConstInt(ctx->i32, 4, false);
2158 LLVMValueRef F = LLVMConstInt(ctx->i32, 0xf, false);
2159
2160 LLVMValueRef fmask = LLVMBuildExtractElement(ctx->builder,
2161 res,
2162 ctx->i32_0, "");
2163
2164 LLVMValueRef sample_index4 =
2165 LLVMBuildMul(ctx->builder, sample_index, four, "");
2166 LLVMValueRef shifted_fmask =
2167 LLVMBuildLShr(ctx->builder, fmask, sample_index4, "");
2168 LLVMValueRef final_sample =
2169 LLVMBuildAnd(ctx->builder, shifted_fmask, F, "");
2170
2171 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
2172 * resource descriptor is 0 (invalid),
2173 */
2174 LLVMValueRef fmask_desc =
2175 LLVMBuildBitCast(ctx->builder, fmask_desc_ptr,
2176 ctx->v8i32, "");
2177
2178 LLVMValueRef fmask_word1 =
2179 LLVMBuildExtractElement(ctx->builder, fmask_desc,
2180 ctx->i32_1, "");
2181
2182 LLVMValueRef word1_is_nonzero =
2183 LLVMBuildICmp(ctx->builder, LLVMIntNE,
2184 fmask_word1, ctx->i32_0, "");
2185
2186 /* Replace the MSAA sample index. */
2187 sample_index =
2188 LLVMBuildSelect(ctx->builder, word1_is_nonzero,
2189 final_sample, sample_index, "");
2190 return sample_index;
2191 }
2192
2193 static nir_variable *get_image_variable(const nir_intrinsic_instr *instr)
2194 {
2195 assert(instr->src[0].is_ssa);
2196 return nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
2197 }
2198
2199 static LLVMValueRef get_image_descriptor(struct ac_nir_context *ctx,
2200 const nir_intrinsic_instr *instr,
2201 enum ac_descriptor_type desc_type,
2202 bool write)
2203 {
2204 return get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr), desc_type, NULL, true, write);
2205 }
2206
2207 static void get_image_coords(struct ac_nir_context *ctx,
2208 const nir_intrinsic_instr *instr,
2209 struct ac_image_args *args)
2210 {
2211 const struct glsl_type *type = glsl_without_array(get_image_variable(instr)->type);
2212
2213 LLVMValueRef src0 = get_src(ctx, instr->src[1]);
2214 LLVMValueRef masks[] = {
2215 LLVMConstInt(ctx->ac.i32, 0, false), LLVMConstInt(ctx->ac.i32, 1, false),
2216 LLVMConstInt(ctx->ac.i32, 2, false), LLVMConstInt(ctx->ac.i32, 3, false),
2217 };
2218 LLVMValueRef sample_index = ac_llvm_extract_elem(&ctx->ac, get_src(ctx, instr->src[2]), 0);
2219
2220 int count;
2221 enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
2222 bool is_array = glsl_sampler_type_is_array(type);
2223 bool add_frag_pos = (dim == GLSL_SAMPLER_DIM_SUBPASS ||
2224 dim == GLSL_SAMPLER_DIM_SUBPASS_MS);
2225 bool is_ms = (dim == GLSL_SAMPLER_DIM_MS ||
2226 dim == GLSL_SAMPLER_DIM_SUBPASS_MS);
2227 bool gfx9_1d = ctx->ac.chip_class >= GFX9 && dim == GLSL_SAMPLER_DIM_1D;
2228 count = image_type_to_components_count(dim, is_array);
2229
2230 if (is_ms) {
2231 LLVMValueRef fmask_load_address[3];
2232 int chan;
2233
2234 fmask_load_address[0] = LLVMBuildExtractElement(ctx->ac.builder, src0, masks[0], "");
2235 fmask_load_address[1] = LLVMBuildExtractElement(ctx->ac.builder, src0, masks[1], "");
2236 if (is_array)
2237 fmask_load_address[2] = LLVMBuildExtractElement(ctx->ac.builder, src0, masks[2], "");
2238 else
2239 fmask_load_address[2] = NULL;
2240 if (add_frag_pos) {
2241 for (chan = 0; chan < 2; ++chan)
2242 fmask_load_address[chan] =
2243 LLVMBuildAdd(ctx->ac.builder, fmask_load_address[chan],
2244 LLVMBuildFPToUI(ctx->ac.builder, ctx->abi->frag_pos[chan],
2245 ctx->ac.i32, ""), "");
2246 fmask_load_address[2] = ac_to_integer(&ctx->ac, ctx->abi->inputs[ac_llvm_reg_index_soa(VARYING_SLOT_LAYER, 0)]);
2247 }
2248 sample_index = adjust_sample_index_using_fmask(&ctx->ac,
2249 fmask_load_address[0],
2250 fmask_load_address[1],
2251 fmask_load_address[2],
2252 sample_index,
2253 get_image_descriptor(ctx, instr, AC_DESC_FMASK, false));
2254 }
2255 if (count == 1 && !gfx9_1d) {
2256 if (instr->src[1].ssa->num_components)
2257 args->coords[0] = LLVMBuildExtractElement(ctx->ac.builder, src0, masks[0], "");
2258 else
2259 args->coords[0] = src0;
2260 } else {
2261 int chan;
2262 if (is_ms)
2263 count--;
2264 for (chan = 0; chan < count; ++chan) {
2265 args->coords[chan] = ac_llvm_extract_elem(&ctx->ac, src0, chan);
2266 }
2267 if (add_frag_pos) {
2268 for (chan = 0; chan < 2; ++chan) {
2269 args->coords[chan] = LLVMBuildAdd(
2270 ctx->ac.builder, args->coords[chan],
2271 LLVMBuildFPToUI(
2272 ctx->ac.builder, ctx->abi->frag_pos[chan],
2273 ctx->ac.i32, ""), "");
2274 }
2275 args->coords[2] = ac_to_integer(&ctx->ac,
2276 ctx->abi->inputs[ac_llvm_reg_index_soa(VARYING_SLOT_LAYER, 0)]);
2277 count++;
2278 }
2279
2280 if (gfx9_1d) {
2281 if (is_array) {
2282 args->coords[2] = args->coords[1];
2283 args->coords[1] = ctx->ac.i32_0;
2284 } else
2285 args->coords[1] = ctx->ac.i32_0;
2286 count++;
2287 }
2288
2289 if (is_ms) {
2290 args->coords[count] = sample_index;
2291 count++;
2292 }
2293 }
2294 }
2295
2296 static LLVMValueRef get_image_buffer_descriptor(struct ac_nir_context *ctx,
2297 const nir_intrinsic_instr *instr, bool write)
2298 {
2299 LLVMValueRef rsrc = get_image_descriptor(ctx, instr, AC_DESC_BUFFER, write);
2300 if (ctx->abi->gfx9_stride_size_workaround) {
2301 LLVMValueRef elem_count = LLVMBuildExtractElement(ctx->ac.builder, rsrc, LLVMConstInt(ctx->ac.i32, 2, 0), "");
2302 LLVMValueRef stride = LLVMBuildExtractElement(ctx->ac.builder, rsrc, LLVMConstInt(ctx->ac.i32, 1, 0), "");
2303 stride = LLVMBuildLShr(ctx->ac.builder, stride, LLVMConstInt(ctx->ac.i32, 16, 0), "");
2304
2305 LLVMValueRef new_elem_count = LLVMBuildSelect(ctx->ac.builder,
2306 LLVMBuildICmp(ctx->ac.builder, LLVMIntUGT, elem_count, stride, ""),
2307 elem_count, stride, "");
2308
2309 rsrc = LLVMBuildInsertElement(ctx->ac.builder, rsrc, new_elem_count,
2310 LLVMConstInt(ctx->ac.i32, 2, 0), "");
2311 }
2312 return rsrc;
2313 }
2314
2315 static LLVMValueRef visit_image_load(struct ac_nir_context *ctx,
2316 const nir_intrinsic_instr *instr)
2317 {
2318 LLVMValueRef res;
2319 const nir_variable *var = get_image_variable(instr);
2320 const struct glsl_type *type = var->type;
2321
2322 type = glsl_without_array(type);
2323
2324 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
2325 if (dim == GLSL_SAMPLER_DIM_BUF) {
2326 unsigned mask = nir_ssa_def_components_read(&instr->dest.ssa);
2327 unsigned num_channels = util_last_bit(mask);
2328 LLVMValueRef rsrc, vindex;
2329
2330 rsrc = get_image_buffer_descriptor(ctx, instr, false);
2331 vindex = LLVMBuildExtractElement(ctx->ac.builder, get_src(ctx, instr->src[1]),
2332 ctx->ac.i32_0, "");
2333
2334 /* TODO: set "glc" and "can_speculate" when OpenGL needs it. */
2335 res = ac_build_buffer_load_format(&ctx->ac, rsrc, vindex,
2336 ctx->ac.i32_0, num_channels,
2337 false, false);
2338 res = ac_build_expand_to_vec4(&ctx->ac, res, num_channels);
2339
2340 res = ac_trim_vector(&ctx->ac, res, instr->dest.ssa.num_components);
2341 res = ac_to_integer(&ctx->ac, res);
2342 } else {
2343 struct ac_image_args args = {};
2344 args.opcode = ac_image_load;
2345 get_image_coords(ctx, instr, &args);
2346 args.resource = get_image_descriptor(ctx, instr, AC_DESC_IMAGE, false);
2347 args.dim = get_ac_image_dim(&ctx->ac, glsl_get_sampler_dim(type),
2348 glsl_sampler_type_is_array(type));
2349 args.dmask = 15;
2350 args.attributes = AC_FUNC_ATTR_READONLY;
2351 if (var->data.image._volatile || var->data.image.coherent)
2352 args.cache_policy |= ac_glc;
2353
2354 res = ac_build_image_opcode(&ctx->ac, &args);
2355 }
2356 return ac_to_integer(&ctx->ac, res);
2357 }
2358
2359 static void visit_image_store(struct ac_nir_context *ctx,
2360 nir_intrinsic_instr *instr)
2361 {
2362 LLVMValueRef params[8];
2363 const nir_variable *var = get_image_variable(instr);
2364 const struct glsl_type *type = glsl_without_array(var->type);
2365 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
2366 LLVMValueRef glc = ctx->ac.i1false;
2367 bool force_glc = ctx->ac.chip_class == SI;
2368 if (force_glc)
2369 glc = ctx->ac.i1true;
2370
2371 if (dim == GLSL_SAMPLER_DIM_BUF) {
2372 LLVMValueRef rsrc = get_image_buffer_descriptor(ctx, instr, true);
2373
2374 params[0] = ac_to_float(&ctx->ac, get_src(ctx, instr->src[3])); /* data */
2375 params[1] = rsrc;
2376 params[2] = LLVMBuildExtractElement(ctx->ac.builder, get_src(ctx, instr->src[1]),
2377 ctx->ac.i32_0, ""); /* vindex */
2378 params[3] = ctx->ac.i32_0; /* voffset */
2379 params[4] = glc; /* glc */
2380 params[5] = ctx->ac.i1false; /* slc */
2381 ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.buffer.store.format.v4f32", ctx->ac.voidt,
2382 params, 6, 0);
2383 } else {
2384 struct ac_image_args args = {};
2385 args.opcode = ac_image_store;
2386 args.data[0] = ac_to_float(&ctx->ac, get_src(ctx, instr->src[3]));
2387 get_image_coords(ctx, instr, &args);
2388 args.resource = get_image_descriptor(ctx, instr, AC_DESC_IMAGE, true);
2389 args.dim = get_ac_image_dim(&ctx->ac, glsl_get_sampler_dim(type),
2390 glsl_sampler_type_is_array(type));
2391 args.dmask = 15;
2392 if (force_glc || var->data.image._volatile || var->data.image.coherent)
2393 args.cache_policy |= ac_glc;
2394
2395 ac_build_image_opcode(&ctx->ac, &args);
2396 }
2397
2398 }
2399
2400 static LLVMValueRef visit_image_atomic(struct ac_nir_context *ctx,
2401 const nir_intrinsic_instr *instr)
2402 {
2403 LLVMValueRef params[7];
2404 int param_count = 0;
2405 const nir_variable *var = get_image_variable(instr);
2406
2407 bool cmpswap = instr->intrinsic == nir_intrinsic_image_deref_atomic_comp_swap;
2408 const char *atomic_name;
2409 char intrinsic_name[41];
2410 enum ac_atomic_op atomic_subop;
2411 const struct glsl_type *type = glsl_without_array(var->type);
2412 MAYBE_UNUSED int length;
2413
2414 bool is_unsigned = glsl_get_sampler_result_type(type) == GLSL_TYPE_UINT;
2415
2416 switch (instr->intrinsic) {
2417 case nir_intrinsic_image_deref_atomic_add:
2418 atomic_name = "add";
2419 atomic_subop = ac_atomic_add;
2420 break;
2421 case nir_intrinsic_image_deref_atomic_min:
2422 atomic_name = is_unsigned ? "umin" : "smin";
2423 atomic_subop = is_unsigned ? ac_atomic_umin : ac_atomic_smin;
2424 break;
2425 case nir_intrinsic_image_deref_atomic_max:
2426 atomic_name = is_unsigned ? "umax" : "smax";
2427 atomic_subop = is_unsigned ? ac_atomic_umax : ac_atomic_smax;
2428 break;
2429 case nir_intrinsic_image_deref_atomic_and:
2430 atomic_name = "and";
2431 atomic_subop = ac_atomic_and;
2432 break;
2433 case nir_intrinsic_image_deref_atomic_or:
2434 atomic_name = "or";
2435 atomic_subop = ac_atomic_or;
2436 break;
2437 case nir_intrinsic_image_deref_atomic_xor:
2438 atomic_name = "xor";
2439 atomic_subop = ac_atomic_xor;
2440 break;
2441 case nir_intrinsic_image_deref_atomic_exchange:
2442 atomic_name = "swap";
2443 atomic_subop = ac_atomic_swap;
2444 break;
2445 case nir_intrinsic_image_deref_atomic_comp_swap:
2446 atomic_name = "cmpswap";
2447 atomic_subop = 0; /* not used */
2448 break;
2449 default:
2450 abort();
2451 }
2452
2453 if (cmpswap)
2454 params[param_count++] = get_src(ctx, instr->src[4]);
2455 params[param_count++] = get_src(ctx, instr->src[3]);
2456
2457 if (glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_BUF) {
2458 params[param_count++] = get_image_buffer_descriptor(ctx, instr, true);
2459 params[param_count++] = LLVMBuildExtractElement(ctx->ac.builder, get_src(ctx, instr->src[1]),
2460 ctx->ac.i32_0, ""); /* vindex */
2461 params[param_count++] = ctx->ac.i32_0; /* voffset */
2462 params[param_count++] = ctx->ac.i1false; /* slc */
2463
2464 length = snprintf(intrinsic_name, sizeof(intrinsic_name),
2465 "llvm.amdgcn.buffer.atomic.%s", atomic_name);
2466
2467 assert(length < sizeof(intrinsic_name));
2468 return ac_build_intrinsic(&ctx->ac, intrinsic_name, ctx->ac.i32,
2469 params, param_count, 0);
2470 } else {
2471 struct ac_image_args args = {};
2472 args.opcode = cmpswap ? ac_image_atomic_cmpswap : ac_image_atomic;
2473 args.atomic = atomic_subop;
2474 args.data[0] = params[0];
2475 if (cmpswap)
2476 args.data[1] = params[1];
2477 get_image_coords(ctx, instr, &args);
2478 args.resource = get_image_descriptor(ctx, instr, AC_DESC_IMAGE, true);
2479 args.dim = get_ac_image_dim(&ctx->ac, glsl_get_sampler_dim(type),
2480 glsl_sampler_type_is_array(type));
2481
2482 return ac_build_image_opcode(&ctx->ac, &args);
2483 }
2484 }
2485
2486 static LLVMValueRef visit_image_samples(struct ac_nir_context *ctx,
2487 const nir_intrinsic_instr *instr)
2488 {
2489 const nir_variable *var = get_image_variable(instr);
2490 const struct glsl_type *type = glsl_without_array(var->type);
2491
2492 struct ac_image_args args = { 0 };
2493 args.dim = get_ac_sampler_dim(&ctx->ac, glsl_get_sampler_dim(type),
2494 glsl_sampler_type_is_array(type));
2495 args.dmask = 0xf;
2496 args.resource = get_image_descriptor(ctx, instr, AC_DESC_IMAGE, false);
2497 args.opcode = ac_image_get_resinfo;
2498 args.lod = ctx->ac.i32_0;
2499 args.attributes = AC_FUNC_ATTR_READNONE;
2500
2501 return ac_build_image_opcode(&ctx->ac, &args);
2502 }
2503
2504 static LLVMValueRef visit_image_size(struct ac_nir_context *ctx,
2505 const nir_intrinsic_instr *instr)
2506 {
2507 LLVMValueRef res;
2508 const nir_variable *var = get_image_variable(instr);
2509 const struct glsl_type *type = glsl_without_array(var->type);
2510
2511 if (glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_BUF)
2512 return get_buffer_size(ctx, get_image_descriptor(ctx, instr, AC_DESC_BUFFER, false), true);
2513
2514 struct ac_image_args args = { 0 };
2515
2516 args.dim = get_ac_image_dim(&ctx->ac, glsl_get_sampler_dim(type),
2517 glsl_sampler_type_is_array(type));
2518 args.dmask = 0xf;
2519 args.resource = get_image_descriptor(ctx, instr, AC_DESC_IMAGE, false);
2520 args.opcode = ac_image_get_resinfo;
2521 args.lod = ctx->ac.i32_0;
2522 args.attributes = AC_FUNC_ATTR_READNONE;
2523
2524 res = ac_build_image_opcode(&ctx->ac, &args);
2525
2526 LLVMValueRef two = LLVMConstInt(ctx->ac.i32, 2, false);
2527
2528 if (glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_CUBE &&
2529 glsl_sampler_type_is_array(type)) {
2530 LLVMValueRef six = LLVMConstInt(ctx->ac.i32, 6, false);
2531 LLVMValueRef z = LLVMBuildExtractElement(ctx->ac.builder, res, two, "");
2532 z = LLVMBuildSDiv(ctx->ac.builder, z, six, "");
2533 res = LLVMBuildInsertElement(ctx->ac.builder, res, z, two, "");
2534 }
2535 if (ctx->ac.chip_class >= GFX9 &&
2536 glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_1D &&
2537 glsl_sampler_type_is_array(type)) {
2538 LLVMValueRef layers = LLVMBuildExtractElement(ctx->ac.builder, res, two, "");
2539 res = LLVMBuildInsertElement(ctx->ac.builder, res, layers,
2540 ctx->ac.i32_1, "");
2541
2542 }
2543 return res;
2544 }
2545
2546 #define NOOP_WAITCNT 0xf7f
2547 #define LGKM_CNT 0x07f
2548 #define VM_CNT 0xf70
2549
2550 static void emit_membar(struct ac_llvm_context *ac,
2551 const nir_intrinsic_instr *instr)
2552 {
2553 unsigned waitcnt = NOOP_WAITCNT;
2554
2555 switch (instr->intrinsic) {
2556 case nir_intrinsic_memory_barrier:
2557 case nir_intrinsic_group_memory_barrier:
2558 waitcnt &= VM_CNT & LGKM_CNT;
2559 break;
2560 case nir_intrinsic_memory_barrier_atomic_counter:
2561 case nir_intrinsic_memory_barrier_buffer:
2562 case nir_intrinsic_memory_barrier_image:
2563 waitcnt &= VM_CNT;
2564 break;
2565 case nir_intrinsic_memory_barrier_shared:
2566 waitcnt &= LGKM_CNT;
2567 break;
2568 default:
2569 break;
2570 }
2571 if (waitcnt != NOOP_WAITCNT)
2572 ac_build_waitcnt(ac, waitcnt);
2573 }
2574
2575 void ac_emit_barrier(struct ac_llvm_context *ac, gl_shader_stage stage)
2576 {
2577 /* SI only (thanks to a hw bug workaround):
2578 * The real barrier instruction isn’t needed, because an entire patch
2579 * always fits into a single wave.
2580 */
2581 if (ac->chip_class == SI && stage == MESA_SHADER_TESS_CTRL) {
2582 ac_build_waitcnt(ac, LGKM_CNT & VM_CNT);
2583 return;
2584 }
2585 ac_build_intrinsic(ac, "llvm.amdgcn.s.barrier",
2586 ac->voidt, NULL, 0, AC_FUNC_ATTR_CONVERGENT);
2587 }
2588
2589 static void emit_discard(struct ac_nir_context *ctx,
2590 const nir_intrinsic_instr *instr)
2591 {
2592 LLVMValueRef cond;
2593
2594 if (instr->intrinsic == nir_intrinsic_discard_if) {
2595 cond = LLVMBuildICmp(ctx->ac.builder, LLVMIntEQ,
2596 get_src(ctx, instr->src[0]),
2597 ctx->ac.i32_0, "");
2598 } else {
2599 assert(instr->intrinsic == nir_intrinsic_discard);
2600 cond = LLVMConstInt(ctx->ac.i1, false, 0);
2601 }
2602
2603 ctx->abi->emit_kill(ctx->abi, cond);
2604 }
2605
2606 static LLVMValueRef
2607 visit_load_helper_invocation(struct ac_nir_context *ctx)
2608 {
2609 LLVMValueRef result = ac_build_intrinsic(&ctx->ac,
2610 "llvm.amdgcn.ps.live",
2611 ctx->ac.i1, NULL, 0,
2612 AC_FUNC_ATTR_READNONE);
2613 result = LLVMBuildNot(ctx->ac.builder, result, "");
2614 return LLVMBuildSExt(ctx->ac.builder, result, ctx->ac.i32, "");
2615 }
2616
2617 static LLVMValueRef
2618 visit_load_local_invocation_index(struct ac_nir_context *ctx)
2619 {
2620 LLVMValueRef result;
2621 LLVMValueRef thread_id = ac_get_thread_id(&ctx->ac);
2622 result = LLVMBuildAnd(ctx->ac.builder, ctx->abi->tg_size,
2623 LLVMConstInt(ctx->ac.i32, 0xfc0, false), "");
2624
2625 return LLVMBuildAdd(ctx->ac.builder, result, thread_id, "");
2626 }
2627
2628 static LLVMValueRef
2629 visit_load_subgroup_id(struct ac_nir_context *ctx)
2630 {
2631 if (ctx->stage == MESA_SHADER_COMPUTE) {
2632 LLVMValueRef result;
2633 result = LLVMBuildAnd(ctx->ac.builder, ctx->abi->tg_size,
2634 LLVMConstInt(ctx->ac.i32, 0xfc0, false), "");
2635 return LLVMBuildLShr(ctx->ac.builder, result, LLVMConstInt(ctx->ac.i32, 6, false), "");
2636 } else {
2637 return LLVMConstInt(ctx->ac.i32, 0, false);
2638 }
2639 }
2640
2641 static LLVMValueRef
2642 visit_load_num_subgroups(struct ac_nir_context *ctx)
2643 {
2644 if (ctx->stage == MESA_SHADER_COMPUTE) {
2645 return LLVMBuildAnd(ctx->ac.builder, ctx->abi->tg_size,
2646 LLVMConstInt(ctx->ac.i32, 0x3f, false), "");
2647 } else {
2648 return LLVMConstInt(ctx->ac.i32, 1, false);
2649 }
2650 }
2651
2652 static LLVMValueRef
2653 visit_first_invocation(struct ac_nir_context *ctx)
2654 {
2655 LLVMValueRef active_set = ac_build_ballot(&ctx->ac, ctx->ac.i32_1);
2656
2657 /* The second argument is whether cttz(0) should be defined, but we do not care. */
2658 LLVMValueRef args[] = {active_set, LLVMConstInt(ctx->ac.i1, 0, false)};
2659 LLVMValueRef result = ac_build_intrinsic(&ctx->ac,
2660 "llvm.cttz.i64",
2661 ctx->ac.i64, args, 2,
2662 AC_FUNC_ATTR_NOUNWIND |
2663 AC_FUNC_ATTR_READNONE);
2664
2665 return LLVMBuildTrunc(ctx->ac.builder, result, ctx->ac.i32, "");
2666 }
2667
2668 static LLVMValueRef
2669 visit_load_shared(struct ac_nir_context *ctx,
2670 const nir_intrinsic_instr *instr)
2671 {
2672 LLVMValueRef values[4], derived_ptr, index, ret;
2673
2674 LLVMValueRef ptr = get_memory_ptr(ctx, instr->src[0]);
2675
2676 for (int chan = 0; chan < instr->num_components; chan++) {
2677 index = LLVMConstInt(ctx->ac.i32, chan, 0);
2678 derived_ptr = LLVMBuildGEP(ctx->ac.builder, ptr, &index, 1, "");
2679 values[chan] = LLVMBuildLoad(ctx->ac.builder, derived_ptr, "");
2680 }
2681
2682 ret = ac_build_gather_values(&ctx->ac, values, instr->num_components);
2683 return LLVMBuildBitCast(ctx->ac.builder, ret, get_def_type(ctx, &instr->dest.ssa), "");
2684 }
2685
2686 static void
2687 visit_store_shared(struct ac_nir_context *ctx,
2688 const nir_intrinsic_instr *instr)
2689 {
2690 LLVMValueRef derived_ptr, data,index;
2691 LLVMBuilderRef builder = ctx->ac.builder;
2692
2693 LLVMValueRef ptr = get_memory_ptr(ctx, instr->src[1]);
2694 LLVMValueRef src = get_src(ctx, instr->src[0]);
2695
2696 int writemask = nir_intrinsic_write_mask(instr);
2697 for (int chan = 0; chan < 4; chan++) {
2698 if (!(writemask & (1 << chan))) {
2699 continue;
2700 }
2701 data = ac_llvm_extract_elem(&ctx->ac, src, chan);
2702 index = LLVMConstInt(ctx->ac.i32, chan, 0);
2703 derived_ptr = LLVMBuildGEP(builder, ptr, &index, 1, "");
2704 LLVMBuildStore(builder, data, derived_ptr);
2705 }
2706 }
2707
2708 static LLVMValueRef visit_var_atomic(struct ac_nir_context *ctx,
2709 const nir_intrinsic_instr *instr,
2710 LLVMValueRef ptr, int src_idx)
2711 {
2712 LLVMValueRef result;
2713 LLVMValueRef src = get_src(ctx, instr->src[src_idx]);
2714
2715 if (instr->intrinsic == nir_intrinsic_shared_atomic_comp_swap ||
2716 instr->intrinsic == nir_intrinsic_deref_atomic_comp_swap) {
2717 LLVMValueRef src1 = get_src(ctx, instr->src[src_idx + 1]);
2718 result = LLVMBuildAtomicCmpXchg(ctx->ac.builder,
2719 ptr, src, src1,
2720 LLVMAtomicOrderingSequentiallyConsistent,
2721 LLVMAtomicOrderingSequentiallyConsistent,
2722 false);
2723 result = LLVMBuildExtractValue(ctx->ac.builder, result, 0, "");
2724 } else {
2725 LLVMAtomicRMWBinOp op;
2726 switch (instr->intrinsic) {
2727 case nir_intrinsic_shared_atomic_add:
2728 case nir_intrinsic_deref_atomic_add:
2729 op = LLVMAtomicRMWBinOpAdd;
2730 break;
2731 case nir_intrinsic_shared_atomic_umin:
2732 case nir_intrinsic_deref_atomic_umin:
2733 op = LLVMAtomicRMWBinOpUMin;
2734 break;
2735 case nir_intrinsic_shared_atomic_umax:
2736 case nir_intrinsic_deref_atomic_umax:
2737 op = LLVMAtomicRMWBinOpUMax;
2738 break;
2739 case nir_intrinsic_shared_atomic_imin:
2740 case nir_intrinsic_deref_atomic_imin:
2741 op = LLVMAtomicRMWBinOpMin;
2742 break;
2743 case nir_intrinsic_shared_atomic_imax:
2744 case nir_intrinsic_deref_atomic_imax:
2745 op = LLVMAtomicRMWBinOpMax;
2746 break;
2747 case nir_intrinsic_shared_atomic_and:
2748 case nir_intrinsic_deref_atomic_and:
2749 op = LLVMAtomicRMWBinOpAnd;
2750 break;
2751 case nir_intrinsic_shared_atomic_or:
2752 case nir_intrinsic_deref_atomic_or:
2753 op = LLVMAtomicRMWBinOpOr;
2754 break;
2755 case nir_intrinsic_shared_atomic_xor:
2756 case nir_intrinsic_deref_atomic_xor:
2757 op = LLVMAtomicRMWBinOpXor;
2758 break;
2759 case nir_intrinsic_shared_atomic_exchange:
2760 case nir_intrinsic_deref_atomic_exchange:
2761 op = LLVMAtomicRMWBinOpXchg;
2762 break;
2763 default:
2764 return NULL;
2765 }
2766
2767 result = LLVMBuildAtomicRMW(ctx->ac.builder, op, ptr, ac_to_integer(&ctx->ac, src),
2768 LLVMAtomicOrderingSequentiallyConsistent,
2769 false);
2770 }
2771 return result;
2772 }
2773
2774 static LLVMValueRef load_sample_pos(struct ac_nir_context *ctx)
2775 {
2776 LLVMValueRef values[2];
2777 LLVMValueRef pos[2];
2778
2779 pos[0] = ac_to_float(&ctx->ac, ctx->abi->frag_pos[0]);
2780 pos[1] = ac_to_float(&ctx->ac, ctx->abi->frag_pos[1]);
2781
2782 values[0] = ac_build_fract(&ctx->ac, pos[0], 32);
2783 values[1] = ac_build_fract(&ctx->ac, pos[1], 32);
2784 return ac_build_gather_values(&ctx->ac, values, 2);
2785 }
2786
2787 static LLVMValueRef visit_interp(struct ac_nir_context *ctx,
2788 const nir_intrinsic_instr *instr)
2789 {
2790 LLVMValueRef result[4];
2791 LLVMValueRef interp_param, attr_number;
2792 unsigned location;
2793 unsigned chan;
2794 LLVMValueRef src_c0 = NULL;
2795 LLVMValueRef src_c1 = NULL;
2796 LLVMValueRef src0 = NULL;
2797
2798 nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
2799 int input_index = var->data.location - VARYING_SLOT_VAR0;
2800 switch (instr->intrinsic) {
2801 case nir_intrinsic_interp_deref_at_centroid:
2802 location = INTERP_CENTROID;
2803 break;
2804 case nir_intrinsic_interp_deref_at_sample:
2805 case nir_intrinsic_interp_deref_at_offset:
2806 location = INTERP_CENTER;
2807 src0 = get_src(ctx, instr->src[1]);
2808 break;
2809 default:
2810 break;
2811 }
2812
2813 if (instr->intrinsic == nir_intrinsic_interp_deref_at_offset) {
2814 src_c0 = ac_to_float(&ctx->ac, LLVMBuildExtractElement(ctx->ac.builder, src0, ctx->ac.i32_0, ""));
2815 src_c1 = ac_to_float(&ctx->ac, LLVMBuildExtractElement(ctx->ac.builder, src0, ctx->ac.i32_1, ""));
2816 } else if (instr->intrinsic == nir_intrinsic_interp_deref_at_sample) {
2817 LLVMValueRef sample_position;
2818 LLVMValueRef halfval = LLVMConstReal(ctx->ac.f32, 0.5f);
2819
2820 /* fetch sample ID */
2821 sample_position = ctx->abi->load_sample_position(ctx->abi, src0);
2822
2823 src_c0 = LLVMBuildExtractElement(ctx->ac.builder, sample_position, ctx->ac.i32_0, "");
2824 src_c0 = LLVMBuildFSub(ctx->ac.builder, src_c0, halfval, "");
2825 src_c1 = LLVMBuildExtractElement(ctx->ac.builder, sample_position, ctx->ac.i32_1, "");
2826 src_c1 = LLVMBuildFSub(ctx->ac.builder, src_c1, halfval, "");
2827 }
2828 interp_param = ctx->abi->lookup_interp_param(ctx->abi, var->data.interpolation, location);
2829 attr_number = LLVMConstInt(ctx->ac.i32, input_index, false);
2830
2831 if (location == INTERP_CENTER) {
2832 LLVMValueRef ij_out[2];
2833 LLVMValueRef ddxy_out = emit_ddxy_interp(ctx, interp_param);
2834
2835 /*
2836 * take the I then J parameters, and the DDX/Y for it, and
2837 * calculate the IJ inputs for the interpolator.
2838 * temp1 = ddx * offset/sample.x + I;
2839 * interp_param.I = ddy * offset/sample.y + temp1;
2840 * temp1 = ddx * offset/sample.x + J;
2841 * interp_param.J = ddy * offset/sample.y + temp1;
2842 */
2843 for (unsigned i = 0; i < 2; i++) {
2844 LLVMValueRef ix_ll = LLVMConstInt(ctx->ac.i32, i, false);
2845 LLVMValueRef iy_ll = LLVMConstInt(ctx->ac.i32, i + 2, false);
2846 LLVMValueRef ddx_el = LLVMBuildExtractElement(ctx->ac.builder,
2847 ddxy_out, ix_ll, "");
2848 LLVMValueRef ddy_el = LLVMBuildExtractElement(ctx->ac.builder,
2849 ddxy_out, iy_ll, "");
2850 LLVMValueRef interp_el = LLVMBuildExtractElement(ctx->ac.builder,
2851 interp_param, ix_ll, "");
2852 LLVMValueRef temp1, temp2;
2853
2854 interp_el = LLVMBuildBitCast(ctx->ac.builder, interp_el,
2855 ctx->ac.f32, "");
2856
2857 temp1 = LLVMBuildFMul(ctx->ac.builder, ddx_el, src_c0, "");
2858 temp1 = LLVMBuildFAdd(ctx->ac.builder, temp1, interp_el, "");
2859
2860 temp2 = LLVMBuildFMul(ctx->ac.builder, ddy_el, src_c1, "");
2861 temp2 = LLVMBuildFAdd(ctx->ac.builder, temp2, temp1, "");
2862
2863 ij_out[i] = LLVMBuildBitCast(ctx->ac.builder,
2864 temp2, ctx->ac.i32, "");
2865 }
2866 interp_param = ac_build_gather_values(&ctx->ac, ij_out, 2);
2867
2868 }
2869
2870 for (chan = 0; chan < 4; chan++) {
2871 LLVMValueRef llvm_chan = LLVMConstInt(ctx->ac.i32, chan, false);
2872
2873 if (interp_param) {
2874 interp_param = LLVMBuildBitCast(ctx->ac.builder,
2875 interp_param, ctx->ac.v2f32, "");
2876 LLVMValueRef i = LLVMBuildExtractElement(
2877 ctx->ac.builder, interp_param, ctx->ac.i32_0, "");
2878 LLVMValueRef j = LLVMBuildExtractElement(
2879 ctx->ac.builder, interp_param, ctx->ac.i32_1, "");
2880
2881 result[chan] = ac_build_fs_interp(&ctx->ac,
2882 llvm_chan, attr_number,
2883 ctx->abi->prim_mask, i, j);
2884 } else {
2885 result[chan] = ac_build_fs_interp_mov(&ctx->ac,
2886 LLVMConstInt(ctx->ac.i32, 2, false),
2887 llvm_chan, attr_number,
2888 ctx->abi->prim_mask);
2889 }
2890 }
2891 return ac_build_varying_gather_values(&ctx->ac, result, instr->num_components,
2892 var->data.location_frac);
2893 }
2894
2895 static void visit_intrinsic(struct ac_nir_context *ctx,
2896 nir_intrinsic_instr *instr)
2897 {
2898 LLVMValueRef result = NULL;
2899
2900 switch (instr->intrinsic) {
2901 case nir_intrinsic_ballot:
2902 result = ac_build_ballot(&ctx->ac, get_src(ctx, instr->src[0]));
2903 break;
2904 case nir_intrinsic_read_invocation:
2905 result = ac_build_readlane(&ctx->ac, get_src(ctx, instr->src[0]),
2906 get_src(ctx, instr->src[1]));
2907 break;
2908 case nir_intrinsic_read_first_invocation:
2909 result = ac_build_readlane(&ctx->ac, get_src(ctx, instr->src[0]), NULL);
2910 break;
2911 case nir_intrinsic_load_subgroup_invocation:
2912 result = ac_get_thread_id(&ctx->ac);
2913 break;
2914 case nir_intrinsic_load_work_group_id: {
2915 LLVMValueRef values[3];
2916
2917 for (int i = 0; i < 3; i++) {
2918 values[i] = ctx->abi->workgroup_ids[i] ?
2919 ctx->abi->workgroup_ids[i] : ctx->ac.i32_0;
2920 }
2921
2922 result = ac_build_gather_values(&ctx->ac, values, 3);
2923 break;
2924 }
2925 case nir_intrinsic_load_base_vertex:
2926 case nir_intrinsic_load_first_vertex:
2927 result = ctx->abi->load_base_vertex(ctx->abi);
2928 break;
2929 case nir_intrinsic_load_local_group_size:
2930 result = ctx->abi->load_local_group_size(ctx->abi);
2931 break;
2932 case nir_intrinsic_load_vertex_id:
2933 result = LLVMBuildAdd(ctx->ac.builder, ctx->abi->vertex_id,
2934 ctx->abi->base_vertex, "");
2935 break;
2936 case nir_intrinsic_load_vertex_id_zero_base: {
2937 result = ctx->abi->vertex_id;
2938 break;
2939 }
2940 case nir_intrinsic_load_local_invocation_id: {
2941 result = ctx->abi->local_invocation_ids;
2942 break;
2943 }
2944 case nir_intrinsic_load_base_instance:
2945 result = ctx->abi->start_instance;
2946 break;
2947 case nir_intrinsic_load_draw_id:
2948 result = ctx->abi->draw_id;
2949 break;
2950 case nir_intrinsic_load_view_index:
2951 result = ctx->abi->view_index;
2952 break;
2953 case nir_intrinsic_load_invocation_id:
2954 if (ctx->stage == MESA_SHADER_TESS_CTRL)
2955 result = ac_unpack_param(&ctx->ac, ctx->abi->tcs_rel_ids, 8, 5);
2956 else
2957 result = ctx->abi->gs_invocation_id;
2958 break;
2959 case nir_intrinsic_load_primitive_id:
2960 if (ctx->stage == MESA_SHADER_GEOMETRY) {
2961 result = ctx->abi->gs_prim_id;
2962 } else if (ctx->stage == MESA_SHADER_TESS_CTRL) {
2963 result = ctx->abi->tcs_patch_id;
2964 } else if (ctx->stage == MESA_SHADER_TESS_EVAL) {
2965 result = ctx->abi->tes_patch_id;
2966 } else
2967 fprintf(stderr, "Unknown primitive id intrinsic: %d", ctx->stage);
2968 break;
2969 case nir_intrinsic_load_sample_id:
2970 result = ac_unpack_param(&ctx->ac, ctx->abi->ancillary, 8, 4);
2971 break;
2972 case nir_intrinsic_load_sample_pos:
2973 result = load_sample_pos(ctx);
2974 break;
2975 case nir_intrinsic_load_sample_mask_in:
2976 result = ctx->abi->load_sample_mask_in(ctx->abi);
2977 break;
2978 case nir_intrinsic_load_frag_coord: {
2979 LLVMValueRef values[4] = {
2980 ctx->abi->frag_pos[0],
2981 ctx->abi->frag_pos[1],
2982 ctx->abi->frag_pos[2],
2983 ac_build_fdiv(&ctx->ac, ctx->ac.f32_1, ctx->abi->frag_pos[3])
2984 };
2985 result = ac_build_gather_values(&ctx->ac, values, 4);
2986 break;
2987 }
2988 case nir_intrinsic_load_front_face:
2989 result = ctx->abi->front_face;
2990 break;
2991 case nir_intrinsic_load_helper_invocation:
2992 result = visit_load_helper_invocation(ctx);
2993 break;
2994 case nir_intrinsic_load_instance_id:
2995 result = ctx->abi->instance_id;
2996 break;
2997 case nir_intrinsic_load_num_work_groups:
2998 result = ctx->abi->num_work_groups;
2999 break;
3000 case nir_intrinsic_load_local_invocation_index:
3001 result = visit_load_local_invocation_index(ctx);
3002 break;
3003 case nir_intrinsic_load_subgroup_id:
3004 result = visit_load_subgroup_id(ctx);
3005 break;
3006 case nir_intrinsic_load_num_subgroups:
3007 result = visit_load_num_subgroups(ctx);
3008 break;
3009 case nir_intrinsic_first_invocation:
3010 result = visit_first_invocation(ctx);
3011 break;
3012 case nir_intrinsic_load_push_constant:
3013 result = visit_load_push_constant(ctx, instr);
3014 break;
3015 case nir_intrinsic_vulkan_resource_index: {
3016 LLVMValueRef index = get_src(ctx, instr->src[0]);
3017 unsigned desc_set = nir_intrinsic_desc_set(instr);
3018 unsigned binding = nir_intrinsic_binding(instr);
3019
3020 result = ctx->abi->load_resource(ctx->abi, index, desc_set,
3021 binding);
3022 break;
3023 }
3024 case nir_intrinsic_vulkan_resource_reindex:
3025 result = visit_vulkan_resource_reindex(ctx, instr);
3026 break;
3027 case nir_intrinsic_store_ssbo:
3028 visit_store_ssbo(ctx, instr);
3029 break;
3030 case nir_intrinsic_load_ssbo:
3031 result = visit_load_buffer(ctx, instr);
3032 break;
3033 case nir_intrinsic_ssbo_atomic_add:
3034 case nir_intrinsic_ssbo_atomic_imin:
3035 case nir_intrinsic_ssbo_atomic_umin:
3036 case nir_intrinsic_ssbo_atomic_imax:
3037 case nir_intrinsic_ssbo_atomic_umax:
3038 case nir_intrinsic_ssbo_atomic_and:
3039 case nir_intrinsic_ssbo_atomic_or:
3040 case nir_intrinsic_ssbo_atomic_xor:
3041 case nir_intrinsic_ssbo_atomic_exchange:
3042 case nir_intrinsic_ssbo_atomic_comp_swap:
3043 result = visit_atomic_ssbo(ctx, instr);
3044 break;
3045 case nir_intrinsic_load_ubo:
3046 result = visit_load_ubo_buffer(ctx, instr);
3047 break;
3048 case nir_intrinsic_get_buffer_size:
3049 result = visit_get_buffer_size(ctx, instr);
3050 break;
3051 case nir_intrinsic_load_deref:
3052 result = visit_load_var(ctx, instr);
3053 break;
3054 case nir_intrinsic_store_deref:
3055 visit_store_var(ctx, instr);
3056 break;
3057 case nir_intrinsic_load_shared:
3058 result = visit_load_shared(ctx, instr);
3059 break;
3060 case nir_intrinsic_store_shared:
3061 visit_store_shared(ctx, instr);
3062 break;
3063 case nir_intrinsic_image_deref_samples:
3064 result = visit_image_samples(ctx, instr);
3065 break;
3066 case nir_intrinsic_image_deref_load:
3067 result = visit_image_load(ctx, instr);
3068 break;
3069 case nir_intrinsic_image_deref_store:
3070 visit_image_store(ctx, instr);
3071 break;
3072 case nir_intrinsic_image_deref_atomic_add:
3073 case nir_intrinsic_image_deref_atomic_min:
3074 case nir_intrinsic_image_deref_atomic_max:
3075 case nir_intrinsic_image_deref_atomic_and:
3076 case nir_intrinsic_image_deref_atomic_or:
3077 case nir_intrinsic_image_deref_atomic_xor:
3078 case nir_intrinsic_image_deref_atomic_exchange:
3079 case nir_intrinsic_image_deref_atomic_comp_swap:
3080 result = visit_image_atomic(ctx, instr);
3081 break;
3082 case nir_intrinsic_image_deref_size:
3083 result = visit_image_size(ctx, instr);
3084 break;
3085 case nir_intrinsic_shader_clock:
3086 result = ac_build_shader_clock(&ctx->ac);
3087 break;
3088 case nir_intrinsic_discard:
3089 case nir_intrinsic_discard_if:
3090 emit_discard(ctx, instr);
3091 break;
3092 case nir_intrinsic_memory_barrier:
3093 case nir_intrinsic_group_memory_barrier:
3094 case nir_intrinsic_memory_barrier_atomic_counter:
3095 case nir_intrinsic_memory_barrier_buffer:
3096 case nir_intrinsic_memory_barrier_image:
3097 case nir_intrinsic_memory_barrier_shared:
3098 emit_membar(&ctx->ac, instr);
3099 break;
3100 case nir_intrinsic_barrier:
3101 ac_emit_barrier(&ctx->ac, ctx->stage);
3102 break;
3103 case nir_intrinsic_shared_atomic_add:
3104 case nir_intrinsic_shared_atomic_imin:
3105 case nir_intrinsic_shared_atomic_umin:
3106 case nir_intrinsic_shared_atomic_imax:
3107 case nir_intrinsic_shared_atomic_umax:
3108 case nir_intrinsic_shared_atomic_and:
3109 case nir_intrinsic_shared_atomic_or:
3110 case nir_intrinsic_shared_atomic_xor:
3111 case nir_intrinsic_shared_atomic_exchange:
3112 case nir_intrinsic_shared_atomic_comp_swap: {
3113 LLVMValueRef ptr = get_memory_ptr(ctx, instr->src[0]);
3114 result = visit_var_atomic(ctx, instr, ptr, 1);
3115 break;
3116 }
3117 case nir_intrinsic_deref_atomic_add:
3118 case nir_intrinsic_deref_atomic_imin:
3119 case nir_intrinsic_deref_atomic_umin:
3120 case nir_intrinsic_deref_atomic_imax:
3121 case nir_intrinsic_deref_atomic_umax:
3122 case nir_intrinsic_deref_atomic_and:
3123 case nir_intrinsic_deref_atomic_or:
3124 case nir_intrinsic_deref_atomic_xor:
3125 case nir_intrinsic_deref_atomic_exchange:
3126 case nir_intrinsic_deref_atomic_comp_swap: {
3127 LLVMValueRef ptr = get_src(ctx, instr->src[0]);
3128 result = visit_var_atomic(ctx, instr, ptr, 1);
3129 break;
3130 }
3131 case nir_intrinsic_interp_deref_at_centroid:
3132 case nir_intrinsic_interp_deref_at_sample:
3133 case nir_intrinsic_interp_deref_at_offset:
3134 result = visit_interp(ctx, instr);
3135 break;
3136 case nir_intrinsic_emit_vertex:
3137 ctx->abi->emit_vertex(ctx->abi, nir_intrinsic_stream_id(instr), ctx->abi->outputs);
3138 break;
3139 case nir_intrinsic_end_primitive:
3140 ctx->abi->emit_primitive(ctx->abi, nir_intrinsic_stream_id(instr));
3141 break;
3142 case nir_intrinsic_load_tess_coord:
3143 result = ctx->abi->load_tess_coord(ctx->abi);
3144 break;
3145 case nir_intrinsic_load_tess_level_outer:
3146 result = ctx->abi->load_tess_level(ctx->abi, VARYING_SLOT_TESS_LEVEL_OUTER);
3147 break;
3148 case nir_intrinsic_load_tess_level_inner:
3149 result = ctx->abi->load_tess_level(ctx->abi, VARYING_SLOT_TESS_LEVEL_INNER);
3150 break;
3151 case nir_intrinsic_load_patch_vertices_in:
3152 result = ctx->abi->load_patch_vertices_in(ctx->abi);
3153 break;
3154 case nir_intrinsic_vote_all: {
3155 LLVMValueRef tmp = ac_build_vote_all(&ctx->ac, get_src(ctx, instr->src[0]));
3156 result = LLVMBuildSExt(ctx->ac.builder, tmp, ctx->ac.i32, "");
3157 break;
3158 }
3159 case nir_intrinsic_vote_any: {
3160 LLVMValueRef tmp = ac_build_vote_any(&ctx->ac, get_src(ctx, instr->src[0]));
3161 result = LLVMBuildSExt(ctx->ac.builder, tmp, ctx->ac.i32, "");
3162 break;
3163 }
3164 case nir_intrinsic_shuffle:
3165 result = ac_build_shuffle(&ctx->ac, get_src(ctx, instr->src[0]),
3166 get_src(ctx, instr->src[1]));
3167 break;
3168 case nir_intrinsic_reduce:
3169 result = ac_build_reduce(&ctx->ac,
3170 get_src(ctx, instr->src[0]),
3171 instr->const_index[0],
3172 instr->const_index[1]);
3173 break;
3174 case nir_intrinsic_inclusive_scan:
3175 result = ac_build_inclusive_scan(&ctx->ac,
3176 get_src(ctx, instr->src[0]),
3177 instr->const_index[0]);
3178 break;
3179 case nir_intrinsic_exclusive_scan:
3180 result = ac_build_exclusive_scan(&ctx->ac,
3181 get_src(ctx, instr->src[0]),
3182 instr->const_index[0]);
3183 break;
3184 case nir_intrinsic_quad_broadcast: {
3185 unsigned lane = nir_src_as_const_value(instr->src[1])->u32[0];
3186 result = ac_build_quad_swizzle(&ctx->ac, get_src(ctx, instr->src[0]),
3187 lane, lane, lane, lane);
3188 break;
3189 }
3190 case nir_intrinsic_quad_swap_horizontal:
3191 result = ac_build_quad_swizzle(&ctx->ac, get_src(ctx, instr->src[0]), 1, 0, 3 ,2);
3192 break;
3193 case nir_intrinsic_quad_swap_vertical:
3194 result = ac_build_quad_swizzle(&ctx->ac, get_src(ctx, instr->src[0]), 2, 3, 0 ,1);
3195 break;
3196 case nir_intrinsic_quad_swap_diagonal:
3197 result = ac_build_quad_swizzle(&ctx->ac, get_src(ctx, instr->src[0]), 3, 2, 1 ,0);
3198 break;
3199 default:
3200 fprintf(stderr, "Unknown intrinsic: ");
3201 nir_print_instr(&instr->instr, stderr);
3202 fprintf(stderr, "\n");
3203 break;
3204 }
3205 if (result) {
3206 ctx->ssa_defs[instr->dest.ssa.index] = result;
3207 }
3208 }
3209
3210 static LLVMValueRef get_sampler_desc(struct ac_nir_context *ctx,
3211 nir_deref_instr *deref_instr,
3212 enum ac_descriptor_type desc_type,
3213 const nir_tex_instr *tex_instr,
3214 bool image, bool write)
3215 {
3216 LLVMValueRef index = NULL;
3217 unsigned constant_index = 0;
3218 unsigned descriptor_set;
3219 unsigned base_index;
3220 bool bindless = false;
3221
3222 if (!deref_instr) {
3223 assert(tex_instr && !image);
3224 descriptor_set = 0;
3225 base_index = tex_instr->sampler_index;
3226 } else {
3227 while(deref_instr->deref_type != nir_deref_type_var) {
3228 unsigned array_size = glsl_get_aoa_size(deref_instr->type);
3229 if (!array_size)
3230 array_size = 1;
3231
3232 assert(deref_instr->deref_type == nir_deref_type_array);
3233 nir_const_value *const_value = nir_src_as_const_value(deref_instr->arr.index);
3234 if (const_value) {
3235 constant_index += array_size * const_value->u32[0];
3236 } else {
3237 LLVMValueRef indirect = get_src(ctx, deref_instr->arr.index);
3238
3239 indirect = LLVMBuildMul(ctx->ac.builder, indirect,
3240 LLVMConstInt(ctx->ac.i32, array_size, false), "");
3241
3242 if (!index)
3243 index = indirect;
3244 else
3245 index = LLVMBuildAdd(ctx->ac.builder, index, indirect, "");
3246 }
3247
3248 deref_instr = nir_src_as_deref(deref_instr->parent);
3249 }
3250 descriptor_set = deref_instr->var->data.descriptor_set;
3251 base_index = deref_instr->var->data.binding;
3252 }
3253
3254 return ctx->abi->load_sampler_desc(ctx->abi,
3255 descriptor_set,
3256 base_index,
3257 constant_index, index,
3258 desc_type, image, write, bindless);
3259 }
3260
3261 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
3262 *
3263 * SI-CI:
3264 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
3265 * filtering manually. The driver sets img7 to a mask clearing
3266 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
3267 * s_and_b32 samp0, samp0, img7
3268 *
3269 * VI:
3270 * The ANISO_OVERRIDE sampler field enables this fix in TA.
3271 */
3272 static LLVMValueRef sici_fix_sampler_aniso(struct ac_nir_context *ctx,
3273 LLVMValueRef res, LLVMValueRef samp)
3274 {
3275 LLVMBuilderRef builder = ctx->ac.builder;
3276 LLVMValueRef img7, samp0;
3277
3278 if (ctx->ac.chip_class >= VI)
3279 return samp;
3280
3281 img7 = LLVMBuildExtractElement(builder, res,
3282 LLVMConstInt(ctx->ac.i32, 7, 0), "");
3283 samp0 = LLVMBuildExtractElement(builder, samp,
3284 LLVMConstInt(ctx->ac.i32, 0, 0), "");
3285 samp0 = LLVMBuildAnd(builder, samp0, img7, "");
3286 return LLVMBuildInsertElement(builder, samp, samp0,
3287 LLVMConstInt(ctx->ac.i32, 0, 0), "");
3288 }
3289
3290 static void tex_fetch_ptrs(struct ac_nir_context *ctx,
3291 nir_tex_instr *instr,
3292 LLVMValueRef *res_ptr, LLVMValueRef *samp_ptr,
3293 LLVMValueRef *fmask_ptr)
3294 {
3295 nir_deref_instr *texture_deref_instr = NULL;
3296 nir_deref_instr *sampler_deref_instr = NULL;
3297
3298 for (unsigned i = 0; i < instr->num_srcs; i++) {
3299 switch (instr->src[i].src_type) {
3300 case nir_tex_src_texture_deref:
3301 texture_deref_instr = nir_src_as_deref(instr->src[i].src);
3302 break;
3303 case nir_tex_src_sampler_deref:
3304 sampler_deref_instr = nir_src_as_deref(instr->src[i].src);
3305 break;
3306 default:
3307 break;
3308 }
3309 }
3310
3311 if (!sampler_deref_instr)
3312 sampler_deref_instr = texture_deref_instr;
3313
3314 if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF)
3315 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, AC_DESC_BUFFER, instr, false, false);
3316 else
3317 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, AC_DESC_IMAGE, instr, false, false);
3318 if (samp_ptr) {
3319 *samp_ptr = get_sampler_desc(ctx, sampler_deref_instr, AC_DESC_SAMPLER, instr, false, false);
3320 if (instr->sampler_dim < GLSL_SAMPLER_DIM_RECT)
3321 *samp_ptr = sici_fix_sampler_aniso(ctx, *res_ptr, *samp_ptr);
3322 }
3323 if (fmask_ptr && (instr->op == nir_texop_txf_ms ||
3324 instr->op == nir_texop_samples_identical))
3325 *fmask_ptr = get_sampler_desc(ctx, texture_deref_instr, AC_DESC_FMASK, instr, false, false);
3326 }
3327
3328 static LLVMValueRef apply_round_slice(struct ac_llvm_context *ctx,
3329 LLVMValueRef coord)
3330 {
3331 coord = ac_to_float(ctx, coord);
3332 coord = ac_build_intrinsic(ctx, "llvm.rint.f32", ctx->f32, &coord, 1, 0);
3333 coord = ac_to_integer(ctx, coord);
3334 return coord;
3335 }
3336
3337 static void visit_tex(struct ac_nir_context *ctx, nir_tex_instr *instr)
3338 {
3339 LLVMValueRef result = NULL;
3340 struct ac_image_args args = { 0 };
3341 LLVMValueRef fmask_ptr = NULL, sample_index = NULL;
3342 LLVMValueRef ddx = NULL, ddy = NULL;
3343 unsigned offset_src = 0;
3344
3345 tex_fetch_ptrs(ctx, instr, &args.resource, &args.sampler, &fmask_ptr);
3346
3347 for (unsigned i = 0; i < instr->num_srcs; i++) {
3348 switch (instr->src[i].src_type) {
3349 case nir_tex_src_coord: {
3350 LLVMValueRef coord = get_src(ctx, instr->src[i].src);
3351 for (unsigned chan = 0; chan < instr->coord_components; ++chan)
3352 args.coords[chan] = ac_llvm_extract_elem(&ctx->ac, coord, chan);
3353 break;
3354 }
3355 case nir_tex_src_projector:
3356 break;
3357 case nir_tex_src_comparator:
3358 if (instr->is_shadow)
3359 args.compare = get_src(ctx, instr->src[i].src);
3360 break;
3361 case nir_tex_src_offset:
3362 args.offset = get_src(ctx, instr->src[i].src);
3363 offset_src = i;
3364 break;
3365 case nir_tex_src_bias:
3366 if (instr->op == nir_texop_txb)
3367 args.bias = get_src(ctx, instr->src[i].src);
3368 break;
3369 case nir_tex_src_lod: {
3370 nir_const_value *val = nir_src_as_const_value(instr->src[i].src);
3371
3372 if (val && val->i32[0] == 0)
3373 args.level_zero = true;
3374 else
3375 args.lod = get_src(ctx, instr->src[i].src);
3376 break;
3377 }
3378 case nir_tex_src_ms_index:
3379 sample_index = get_src(ctx, instr->src[i].src);
3380 break;
3381 case nir_tex_src_ms_mcs:
3382 break;
3383 case nir_tex_src_ddx:
3384 ddx = get_src(ctx, instr->src[i].src);
3385 break;
3386 case nir_tex_src_ddy:
3387 ddy = get_src(ctx, instr->src[i].src);
3388 break;
3389 case nir_tex_src_texture_offset:
3390 case nir_tex_src_sampler_offset:
3391 case nir_tex_src_plane:
3392 default:
3393 break;
3394 }
3395 }
3396
3397 if (instr->op == nir_texop_txs && instr->sampler_dim == GLSL_SAMPLER_DIM_BUF) {
3398 result = get_buffer_size(ctx, args.resource, true);
3399 goto write_result;
3400 }
3401
3402 if (instr->op == nir_texop_texture_samples) {
3403 LLVMValueRef res, samples, is_msaa;
3404 res = LLVMBuildBitCast(ctx->ac.builder, args.resource, ctx->ac.v8i32, "");
3405 samples = LLVMBuildExtractElement(ctx->ac.builder, res,
3406 LLVMConstInt(ctx->ac.i32, 3, false), "");
3407 is_msaa = LLVMBuildLShr(ctx->ac.builder, samples,
3408 LLVMConstInt(ctx->ac.i32, 28, false), "");
3409 is_msaa = LLVMBuildAnd(ctx->ac.builder, is_msaa,
3410 LLVMConstInt(ctx->ac.i32, 0xe, false), "");
3411 is_msaa = LLVMBuildICmp(ctx->ac.builder, LLVMIntEQ, is_msaa,
3412 LLVMConstInt(ctx->ac.i32, 0xe, false), "");
3413
3414 samples = LLVMBuildLShr(ctx->ac.builder, samples,
3415 LLVMConstInt(ctx->ac.i32, 16, false), "");
3416 samples = LLVMBuildAnd(ctx->ac.builder, samples,
3417 LLVMConstInt(ctx->ac.i32, 0xf, false), "");
3418 samples = LLVMBuildShl(ctx->ac.builder, ctx->ac.i32_1,
3419 samples, "");
3420 samples = LLVMBuildSelect(ctx->ac.builder, is_msaa, samples,
3421 ctx->ac.i32_1, "");
3422 result = samples;
3423 goto write_result;
3424 }
3425
3426 if (args.offset && instr->op != nir_texop_txf) {
3427 LLVMValueRef offset[3], pack;
3428 for (unsigned chan = 0; chan < 3; ++chan)
3429 offset[chan] = ctx->ac.i32_0;
3430
3431 unsigned num_components = ac_get_llvm_num_components(args.offset);
3432 for (unsigned chan = 0; chan < num_components; chan++) {
3433 offset[chan] = ac_llvm_extract_elem(&ctx->ac, args.offset, chan);
3434 offset[chan] = LLVMBuildAnd(ctx->ac.builder, offset[chan],
3435 LLVMConstInt(ctx->ac.i32, 0x3f, false), "");
3436 if (chan)
3437 offset[chan] = LLVMBuildShl(ctx->ac.builder, offset[chan],
3438 LLVMConstInt(ctx->ac.i32, chan * 8, false), "");
3439 }
3440 pack = LLVMBuildOr(ctx->ac.builder, offset[0], offset[1], "");
3441 pack = LLVMBuildOr(ctx->ac.builder, pack, offset[2], "");
3442 args.offset = pack;
3443 }
3444
3445 /* TC-compatible HTILE on radeonsi promotes Z16 and Z24 to Z32_FLOAT,
3446 * so the depth comparison value isn't clamped for Z16 and
3447 * Z24 anymore. Do it manually here.
3448 *
3449 * It's unnecessary if the original texture format was
3450 * Z32_FLOAT, but we don't know that here.
3451 */
3452 if (args.compare && ctx->ac.chip_class == VI && ctx->abi->clamp_shadow_reference)
3453 args.compare = ac_build_clamp(&ctx->ac, ac_to_float(&ctx->ac, args.compare));
3454
3455 /* pack derivatives */
3456 if (ddx || ddy) {
3457 int num_src_deriv_channels, num_dest_deriv_channels;
3458 switch (instr->sampler_dim) {
3459 case GLSL_SAMPLER_DIM_3D:
3460 case GLSL_SAMPLER_DIM_CUBE:
3461 num_src_deriv_channels = 3;
3462 num_dest_deriv_channels = 3;
3463 break;
3464 case GLSL_SAMPLER_DIM_2D:
3465 default:
3466 num_src_deriv_channels = 2;
3467 num_dest_deriv_channels = 2;
3468 break;
3469 case GLSL_SAMPLER_DIM_1D:
3470 num_src_deriv_channels = 1;
3471 if (ctx->ac.chip_class >= GFX9) {
3472 num_dest_deriv_channels = 2;
3473 } else {
3474 num_dest_deriv_channels = 1;
3475 }
3476 break;
3477 }
3478
3479 for (unsigned i = 0; i < num_src_deriv_channels; i++) {
3480 args.derivs[i] = ac_to_float(&ctx->ac,
3481 ac_llvm_extract_elem(&ctx->ac, ddx, i));
3482 args.derivs[num_dest_deriv_channels + i] = ac_to_float(&ctx->ac,
3483 ac_llvm_extract_elem(&ctx->ac, ddy, i));
3484 }
3485 for (unsigned i = num_src_deriv_channels; i < num_dest_deriv_channels; i++) {
3486 args.derivs[i] = ctx->ac.f32_0;
3487 args.derivs[num_dest_deriv_channels + i] = ctx->ac.f32_0;
3488 }
3489 }
3490
3491 if (instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE && args.coords[0]) {
3492 for (unsigned chan = 0; chan < instr->coord_components; chan++)
3493 args.coords[chan] = ac_to_float(&ctx->ac, args.coords[chan]);
3494 if (instr->coord_components == 3)
3495 args.coords[3] = LLVMGetUndef(ctx->ac.f32);
3496 ac_prepare_cube_coords(&ctx->ac,
3497 instr->op == nir_texop_txd, instr->is_array,
3498 instr->op == nir_texop_lod, args.coords, args.derivs);
3499 }
3500
3501 /* Texture coordinates fixups */
3502 if (instr->coord_components > 1 &&
3503 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
3504 instr->is_array &&
3505 instr->op != nir_texop_txf) {
3506 args.coords[1] = apply_round_slice(&ctx->ac, args.coords[1]);
3507 }
3508
3509 if (instr->coord_components > 2 &&
3510 (instr->sampler_dim == GLSL_SAMPLER_DIM_2D ||
3511 instr->sampler_dim == GLSL_SAMPLER_DIM_MS ||
3512 instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS ||
3513 instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS_MS) &&
3514 instr->is_array &&
3515 instr->op != nir_texop_txf && instr->op != nir_texop_txf_ms) {
3516 args.coords[2] = apply_round_slice(&ctx->ac, args.coords[2]);
3517 }
3518
3519 if (ctx->ac.chip_class >= GFX9 &&
3520 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
3521 instr->op != nir_texop_lod) {
3522 LLVMValueRef filler;
3523 if (instr->op == nir_texop_txf)
3524 filler = ctx->ac.i32_0;
3525 else
3526 filler = LLVMConstReal(ctx->ac.f32, 0.5);
3527
3528 if (instr->is_array)
3529 args.coords[2] = args.coords[1];
3530 args.coords[1] = filler;
3531 }
3532
3533 /* Pack sample index */
3534 if (instr->op == nir_texop_txf_ms && sample_index)
3535 args.coords[instr->coord_components] = sample_index;
3536
3537 if (instr->op == nir_texop_samples_identical) {
3538 struct ac_image_args txf_args = { 0 };
3539 memcpy(txf_args.coords, args.coords, sizeof(txf_args.coords));
3540
3541 txf_args.dmask = 0xf;
3542 txf_args.resource = fmask_ptr;
3543 txf_args.dim = instr->is_array ? ac_image_2darray : ac_image_2d;
3544 result = build_tex_intrinsic(ctx, instr, &txf_args);
3545
3546 result = LLVMBuildExtractElement(ctx->ac.builder, result, ctx->ac.i32_0, "");
3547 result = emit_int_cmp(&ctx->ac, LLVMIntEQ, result, ctx->ac.i32_0);
3548 goto write_result;
3549 }
3550
3551 if (instr->sampler_dim == GLSL_SAMPLER_DIM_MS &&
3552 instr->op != nir_texop_txs) {
3553 unsigned sample_chan = instr->is_array ? 3 : 2;
3554 args.coords[sample_chan] = adjust_sample_index_using_fmask(
3555 &ctx->ac, args.coords[0], args.coords[1],
3556 instr->is_array ? args.coords[2] : NULL,
3557 args.coords[sample_chan], fmask_ptr);
3558 }
3559
3560 if (args.offset && instr->op == nir_texop_txf) {
3561 nir_const_value *const_offset =
3562 nir_src_as_const_value(instr->src[offset_src].src);
3563 int num_offsets = instr->src[offset_src].src.ssa->num_components;
3564 assert(const_offset);
3565 num_offsets = MIN2(num_offsets, instr->coord_components);
3566 for (unsigned i = 0; i < num_offsets; ++i) {
3567 args.coords[i] = LLVMBuildAdd(
3568 ctx->ac.builder, args.coords[i],
3569 LLVMConstInt(ctx->ac.i32, const_offset->i32[i], false), "");
3570 }
3571 args.offset = NULL;
3572 }
3573
3574 /* TODO TG4 support */
3575 args.dmask = 0xf;
3576 if (instr->op == nir_texop_tg4) {
3577 if (instr->is_shadow)
3578 args.dmask = 1;
3579 else
3580 args.dmask = 1 << instr->component;
3581 }
3582
3583 if (instr->sampler_dim != GLSL_SAMPLER_DIM_BUF)
3584 args.dim = get_ac_sampler_dim(&ctx->ac, instr->sampler_dim, instr->is_array);
3585 result = build_tex_intrinsic(ctx, instr, &args);
3586
3587 if (instr->op == nir_texop_query_levels)
3588 result = LLVMBuildExtractElement(ctx->ac.builder, result, LLVMConstInt(ctx->ac.i32, 3, false), "");
3589 else if (instr->is_shadow && instr->is_new_style_shadow &&
3590 instr->op != nir_texop_txs && instr->op != nir_texop_lod &&
3591 instr->op != nir_texop_tg4)
3592 result = LLVMBuildExtractElement(ctx->ac.builder, result, ctx->ac.i32_0, "");
3593 else if (instr->op == nir_texop_txs &&
3594 instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE &&
3595 instr->is_array) {
3596 LLVMValueRef two = LLVMConstInt(ctx->ac.i32, 2, false);
3597 LLVMValueRef six = LLVMConstInt(ctx->ac.i32, 6, false);
3598 LLVMValueRef z = LLVMBuildExtractElement(ctx->ac.builder, result, two, "");
3599 z = LLVMBuildSDiv(ctx->ac.builder, z, six, "");
3600 result = LLVMBuildInsertElement(ctx->ac.builder, result, z, two, "");
3601 } else if (ctx->ac.chip_class >= GFX9 &&
3602 instr->op == nir_texop_txs &&
3603 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
3604 instr->is_array) {
3605 LLVMValueRef two = LLVMConstInt(ctx->ac.i32, 2, false);
3606 LLVMValueRef layers = LLVMBuildExtractElement(ctx->ac.builder, result, two, "");
3607 result = LLVMBuildInsertElement(ctx->ac.builder, result, layers,
3608 ctx->ac.i32_1, "");
3609 } else if (instr->dest.ssa.num_components != 4)
3610 result = ac_trim_vector(&ctx->ac, result, instr->dest.ssa.num_components);
3611
3612 write_result:
3613 if (result) {
3614 assert(instr->dest.is_ssa);
3615 result = ac_to_integer(&ctx->ac, result);
3616 ctx->ssa_defs[instr->dest.ssa.index] = result;
3617 }
3618 }
3619
3620
3621 static void visit_phi(struct ac_nir_context *ctx, nir_phi_instr *instr)
3622 {
3623 LLVMTypeRef type = get_def_type(ctx, &instr->dest.ssa);
3624 LLVMValueRef result = LLVMBuildPhi(ctx->ac.builder, type, "");
3625
3626 ctx->ssa_defs[instr->dest.ssa.index] = result;
3627 _mesa_hash_table_insert(ctx->phis, instr, result);
3628 }
3629
3630 static void visit_post_phi(struct ac_nir_context *ctx,
3631 nir_phi_instr *instr,
3632 LLVMValueRef llvm_phi)
3633 {
3634 nir_foreach_phi_src(src, instr) {
3635 LLVMBasicBlockRef block = get_block(ctx, src->pred);
3636 LLVMValueRef llvm_src = get_src(ctx, src->src);
3637
3638 LLVMAddIncoming(llvm_phi, &llvm_src, &block, 1);
3639 }
3640 }
3641
3642 static void phi_post_pass(struct ac_nir_context *ctx)
3643 {
3644 struct hash_entry *entry;
3645 hash_table_foreach(ctx->phis, entry) {
3646 visit_post_phi(ctx, (nir_phi_instr*)entry->key,
3647 (LLVMValueRef)entry->data);
3648 }
3649 }
3650
3651
3652 static void visit_ssa_undef(struct ac_nir_context *ctx,
3653 const nir_ssa_undef_instr *instr)
3654 {
3655 unsigned num_components = instr->def.num_components;
3656 LLVMTypeRef type = LLVMIntTypeInContext(ctx->ac.context, instr->def.bit_size);
3657 LLVMValueRef undef;
3658
3659 if (num_components == 1)
3660 undef = LLVMGetUndef(type);
3661 else {
3662 undef = LLVMGetUndef(LLVMVectorType(type, num_components));
3663 }
3664 ctx->ssa_defs[instr->def.index] = undef;
3665 }
3666
3667 static void visit_jump(struct ac_llvm_context *ctx,
3668 const nir_jump_instr *instr)
3669 {
3670 switch (instr->type) {
3671 case nir_jump_break:
3672 ac_build_break(ctx);
3673 break;
3674 case nir_jump_continue:
3675 ac_build_continue(ctx);
3676 break;
3677 default:
3678 fprintf(stderr, "Unknown NIR jump instr: ");
3679 nir_print_instr(&instr->instr, stderr);
3680 fprintf(stderr, "\n");
3681 abort();
3682 }
3683 }
3684
3685 static void visit_deref(struct ac_nir_context *ctx,
3686 nir_deref_instr *instr)
3687 {
3688 if (instr->mode != nir_var_shared)
3689 return;
3690
3691 LLVMValueRef result = NULL;
3692 switch(instr->deref_type) {
3693 case nir_deref_type_var: {
3694 struct hash_entry *entry = _mesa_hash_table_search(ctx->vars, instr->var);
3695 result = entry->data;
3696 break;
3697 }
3698 case nir_deref_type_struct:
3699 result = ac_build_gep0(&ctx->ac, get_src(ctx, instr->parent),
3700 LLVMConstInt(ctx->ac.i32, instr->strct.index, 0));
3701 break;
3702 case nir_deref_type_array:
3703 result = ac_build_gep0(&ctx->ac, get_src(ctx, instr->parent),
3704 get_src(ctx, instr->arr.index));
3705 break;
3706 default:
3707 unreachable("Unhandled deref_instr deref type");
3708 }
3709
3710 ctx->ssa_defs[instr->dest.ssa.index] = result;
3711 }
3712
3713 static void visit_cf_list(struct ac_nir_context *ctx,
3714 struct exec_list *list);
3715
3716 static void visit_block(struct ac_nir_context *ctx, nir_block *block)
3717 {
3718 LLVMBasicBlockRef llvm_block = LLVMGetInsertBlock(ctx->ac.builder);
3719 nir_foreach_instr(instr, block)
3720 {
3721 switch (instr->type) {
3722 case nir_instr_type_alu:
3723 visit_alu(ctx, nir_instr_as_alu(instr));
3724 break;
3725 case nir_instr_type_load_const:
3726 visit_load_const(ctx, nir_instr_as_load_const(instr));
3727 break;
3728 case nir_instr_type_intrinsic:
3729 visit_intrinsic(ctx, nir_instr_as_intrinsic(instr));
3730 break;
3731 case nir_instr_type_tex:
3732 visit_tex(ctx, nir_instr_as_tex(instr));
3733 break;
3734 case nir_instr_type_phi:
3735 visit_phi(ctx, nir_instr_as_phi(instr));
3736 break;
3737 case nir_instr_type_ssa_undef:
3738 visit_ssa_undef(ctx, nir_instr_as_ssa_undef(instr));
3739 break;
3740 case nir_instr_type_jump:
3741 visit_jump(&ctx->ac, nir_instr_as_jump(instr));
3742 break;
3743 case nir_instr_type_deref:
3744 visit_deref(ctx, nir_instr_as_deref(instr));
3745 break;
3746 default:
3747 fprintf(stderr, "Unknown NIR instr type: ");
3748 nir_print_instr(instr, stderr);
3749 fprintf(stderr, "\n");
3750 abort();
3751 }
3752 }
3753
3754 _mesa_hash_table_insert(ctx->defs, block, llvm_block);
3755 }
3756
3757 static void visit_if(struct ac_nir_context *ctx, nir_if *if_stmt)
3758 {
3759 LLVMValueRef value = get_src(ctx, if_stmt->condition);
3760
3761 nir_block *then_block =
3762 (nir_block *) exec_list_get_head(&if_stmt->then_list);
3763
3764 ac_build_uif(&ctx->ac, value, then_block->index);
3765
3766 visit_cf_list(ctx, &if_stmt->then_list);
3767
3768 if (!exec_list_is_empty(&if_stmt->else_list)) {
3769 nir_block *else_block =
3770 (nir_block *) exec_list_get_head(&if_stmt->else_list);
3771
3772 ac_build_else(&ctx->ac, else_block->index);
3773 visit_cf_list(ctx, &if_stmt->else_list);
3774 }
3775
3776 ac_build_endif(&ctx->ac, then_block->index);
3777 }
3778
3779 static void visit_loop(struct ac_nir_context *ctx, nir_loop *loop)
3780 {
3781 nir_block *first_loop_block =
3782 (nir_block *) exec_list_get_head(&loop->body);
3783
3784 ac_build_bgnloop(&ctx->ac, first_loop_block->index);
3785
3786 visit_cf_list(ctx, &loop->body);
3787
3788 ac_build_endloop(&ctx->ac, first_loop_block->index);
3789 }
3790
3791 static void visit_cf_list(struct ac_nir_context *ctx,
3792 struct exec_list *list)
3793 {
3794 foreach_list_typed(nir_cf_node, node, node, list)
3795 {
3796 switch (node->type) {
3797 case nir_cf_node_block:
3798 visit_block(ctx, nir_cf_node_as_block(node));
3799 break;
3800
3801 case nir_cf_node_if:
3802 visit_if(ctx, nir_cf_node_as_if(node));
3803 break;
3804
3805 case nir_cf_node_loop:
3806 visit_loop(ctx, nir_cf_node_as_loop(node));
3807 break;
3808
3809 default:
3810 assert(0);
3811 }
3812 }
3813 }
3814
3815 void
3816 ac_handle_shader_output_decl(struct ac_llvm_context *ctx,
3817 struct ac_shader_abi *abi,
3818 struct nir_shader *nir,
3819 struct nir_variable *variable,
3820 gl_shader_stage stage)
3821 {
3822 unsigned output_loc = variable->data.driver_location / 4;
3823 unsigned attrib_count = glsl_count_attribute_slots(variable->type, false);
3824
3825 /* tess ctrl has it's own load/store paths for outputs */
3826 if (stage == MESA_SHADER_TESS_CTRL)
3827 return;
3828
3829 if (stage == MESA_SHADER_VERTEX ||
3830 stage == MESA_SHADER_TESS_EVAL ||
3831 stage == MESA_SHADER_GEOMETRY) {
3832 int idx = variable->data.location + variable->data.index;
3833 if (idx == VARYING_SLOT_CLIP_DIST0) {
3834 int length = nir->info.clip_distance_array_size +
3835 nir->info.cull_distance_array_size;
3836
3837 if (length > 4)
3838 attrib_count = 2;
3839 else
3840 attrib_count = 1;
3841 }
3842 }
3843
3844 bool is_16bit = glsl_type_is_16bit(variable->type);
3845 LLVMTypeRef type = is_16bit ? ctx->f16 : ctx->f32;
3846 for (unsigned i = 0; i < attrib_count; ++i) {
3847 for (unsigned chan = 0; chan < 4; chan++) {
3848 abi->outputs[ac_llvm_reg_index_soa(output_loc + i, chan)] =
3849 ac_build_alloca_undef(ctx, type, "");
3850 }
3851 }
3852 }
3853
3854 static LLVMTypeRef
3855 glsl_base_to_llvm_type(struct ac_llvm_context *ac,
3856 enum glsl_base_type type)
3857 {
3858 switch (type) {
3859 case GLSL_TYPE_INT:
3860 case GLSL_TYPE_UINT:
3861 case GLSL_TYPE_BOOL:
3862 case GLSL_TYPE_SUBROUTINE:
3863 return ac->i32;
3864 case GLSL_TYPE_FLOAT: /* TODO handle mediump */
3865 return ac->f32;
3866 case GLSL_TYPE_INT64:
3867 case GLSL_TYPE_UINT64:
3868 return ac->i64;
3869 case GLSL_TYPE_DOUBLE:
3870 return ac->f64;
3871 default:
3872 unreachable("unknown GLSL type");
3873 }
3874 }
3875
3876 static LLVMTypeRef
3877 glsl_to_llvm_type(struct ac_llvm_context *ac,
3878 const struct glsl_type *type)
3879 {
3880 if (glsl_type_is_scalar(type)) {
3881 return glsl_base_to_llvm_type(ac, glsl_get_base_type(type));
3882 }
3883
3884 if (glsl_type_is_vector(type)) {
3885 return LLVMVectorType(
3886 glsl_base_to_llvm_type(ac, glsl_get_base_type(type)),
3887 glsl_get_vector_elements(type));
3888 }
3889
3890 if (glsl_type_is_matrix(type)) {
3891 return LLVMArrayType(
3892 glsl_to_llvm_type(ac, glsl_get_column_type(type)),
3893 glsl_get_matrix_columns(type));
3894 }
3895
3896 if (glsl_type_is_array(type)) {
3897 return LLVMArrayType(
3898 glsl_to_llvm_type(ac, glsl_get_array_element(type)),
3899 glsl_get_length(type));
3900 }
3901
3902 assert(glsl_type_is_struct(type));
3903
3904 LLVMTypeRef member_types[glsl_get_length(type)];
3905
3906 for (unsigned i = 0; i < glsl_get_length(type); i++) {
3907 member_types[i] =
3908 glsl_to_llvm_type(ac,
3909 glsl_get_struct_field(type, i));
3910 }
3911
3912 return LLVMStructTypeInContext(ac->context, member_types,
3913 glsl_get_length(type), false);
3914 }
3915
3916 static void
3917 setup_locals(struct ac_nir_context *ctx,
3918 struct nir_function *func)
3919 {
3920 int i, j;
3921 ctx->num_locals = 0;
3922 nir_foreach_variable(variable, &func->impl->locals) {
3923 unsigned attrib_count = glsl_count_attribute_slots(variable->type, false);
3924 variable->data.driver_location = ctx->num_locals * 4;
3925 variable->data.location_frac = 0;
3926 ctx->num_locals += attrib_count;
3927 }
3928 ctx->locals = malloc(4 * ctx->num_locals * sizeof(LLVMValueRef));
3929 if (!ctx->locals)
3930 return;
3931
3932 for (i = 0; i < ctx->num_locals; i++) {
3933 for (j = 0; j < 4; j++) {
3934 ctx->locals[i * 4 + j] =
3935 ac_build_alloca_undef(&ctx->ac, ctx->ac.f32, "temp");
3936 }
3937 }
3938 }
3939
3940 static void
3941 setup_shared(struct ac_nir_context *ctx,
3942 struct nir_shader *nir)
3943 {
3944 nir_foreach_variable(variable, &nir->shared) {
3945 LLVMValueRef shared =
3946 LLVMAddGlobalInAddressSpace(
3947 ctx->ac.module, glsl_to_llvm_type(&ctx->ac, variable->type),
3948 variable->name ? variable->name : "",
3949 AC_LOCAL_ADDR_SPACE);
3950 _mesa_hash_table_insert(ctx->vars, variable, shared);
3951 }
3952 }
3953
3954 void ac_nir_translate(struct ac_llvm_context *ac, struct ac_shader_abi *abi,
3955 struct nir_shader *nir)
3956 {
3957 struct ac_nir_context ctx = {};
3958 struct nir_function *func;
3959
3960 ctx.ac = *ac;
3961 ctx.abi = abi;
3962
3963 ctx.stage = nir->info.stage;
3964
3965 ctx.main_function = LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx.ac.builder));
3966
3967 nir_foreach_variable(variable, &nir->outputs)
3968 ac_handle_shader_output_decl(&ctx.ac, ctx.abi, nir, variable,
3969 ctx.stage);
3970
3971 ctx.defs = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
3972 _mesa_key_pointer_equal);
3973 ctx.phis = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
3974 _mesa_key_pointer_equal);
3975 ctx.vars = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
3976 _mesa_key_pointer_equal);
3977
3978 func = (struct nir_function *)exec_list_get_head(&nir->functions);
3979
3980 nir_index_ssa_defs(func->impl);
3981 ctx.ssa_defs = calloc(func->impl->ssa_alloc, sizeof(LLVMValueRef));
3982
3983 setup_locals(&ctx, func);
3984
3985 if (nir->info.stage == MESA_SHADER_COMPUTE)
3986 setup_shared(&ctx, nir);
3987
3988 visit_cf_list(&ctx, &func->impl->body);
3989 phi_post_pass(&ctx);
3990
3991 if (nir->info.stage != MESA_SHADER_COMPUTE)
3992 ctx.abi->emit_outputs(ctx.abi, AC_LLVM_MAX_OUTPUTS,
3993 ctx.abi->outputs);
3994
3995 free(ctx.locals);
3996 free(ctx.ssa_defs);
3997 ralloc_free(ctx.defs);
3998 ralloc_free(ctx.phis);
3999 ralloc_free(ctx.vars);
4000 }
4001
4002 void
4003 ac_lower_indirect_derefs(struct nir_shader *nir, enum chip_class chip_class)
4004 {
4005 /* While it would be nice not to have this flag, we are constrained
4006 * by the reality that LLVM 5.0 doesn't have working VGPR indexing
4007 * on GFX9.
4008 */
4009 bool llvm_has_working_vgpr_indexing = chip_class <= VI;
4010
4011 /* TODO: Indirect indexing of GS inputs is unimplemented.
4012 *
4013 * TCS and TES load inputs directly from LDS or offchip memory, so
4014 * indirect indexing is trivial.
4015 */
4016 nir_variable_mode indirect_mask = 0;
4017 if (nir->info.stage == MESA_SHADER_GEOMETRY ||
4018 (nir->info.stage != MESA_SHADER_TESS_CTRL &&
4019 nir->info.stage != MESA_SHADER_TESS_EVAL &&
4020 !llvm_has_working_vgpr_indexing)) {
4021 indirect_mask |= nir_var_shader_in;
4022 }
4023 if (!llvm_has_working_vgpr_indexing &&
4024 nir->info.stage != MESA_SHADER_TESS_CTRL)
4025 indirect_mask |= nir_var_shader_out;
4026
4027 /* TODO: We shouldn't need to do this, however LLVM isn't currently
4028 * smart enough to handle indirects without causing excess spilling
4029 * causing the gpu to hang.
4030 *
4031 * See the following thread for more details of the problem:
4032 * https://lists.freedesktop.org/archives/mesa-dev/2017-July/162106.html
4033 */
4034 indirect_mask |= nir_var_local;
4035
4036 nir_lower_indirect_derefs(nir, indirect_mask);
4037 }