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