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