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