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