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