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