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