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