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