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