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