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