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