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