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