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