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