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