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