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