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