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