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