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