ac/nir: use llvm.amdgcn.rsq for nir_op_frsq
[mesa.git] / src / amd / llvm / ac_nir_to_llvm.c
1 /*
2 * Copyright © 2016 Bas Nieuwenhuizen
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include <llvm/Config/llvm-config.h>
25
26 #include "ac_nir_to_llvm.h"
27 #include "ac_llvm_build.h"
28 #include "ac_llvm_util.h"
29 #include "ac_binary.h"
30 #include "sid.h"
31 #include "nir/nir.h"
32 #include "nir/nir_deref.h"
33 #include "util/bitscan.h"
34 #include "util/u_math.h"
35 #include "ac_shader_abi.h"
36 #include "ac_shader_util.h"
37
38 struct ac_nir_context {
39 struct ac_llvm_context ac;
40 struct ac_shader_abi *abi;
41 const struct ac_shader_args *args;
42
43 gl_shader_stage stage;
44 shader_info *info;
45
46 LLVMValueRef *ssa_defs;
47
48 LLVMValueRef scratch;
49 LLVMValueRef constant_data;
50
51 struct hash_table *defs;
52 struct hash_table *phis;
53 struct hash_table *vars;
54
55 LLVMValueRef main_function;
56 LLVMBasicBlockRef continue_block;
57 LLVMBasicBlockRef break_block;
58
59 int num_locals;
60 LLVMValueRef *locals;
61 };
62
63 static LLVMValueRef get_sampler_desc_index(struct ac_nir_context *ctx,
64 nir_deref_instr *deref_instr,
65 const nir_instr *instr,
66 bool image);
67
68 static LLVMValueRef get_sampler_desc(struct ac_nir_context *ctx,
69 nir_deref_instr *deref_instr,
70 enum ac_descriptor_type desc_type,
71 const nir_instr *instr,
72 LLVMValueRef index,
73 bool image, bool write);
74
75 static void
76 build_store_values_extended(struct ac_llvm_context *ac,
77 LLVMValueRef *values,
78 unsigned value_count,
79 unsigned value_stride,
80 LLVMValueRef vec)
81 {
82 LLVMBuilderRef builder = ac->builder;
83 unsigned i;
84
85 for (i = 0; i < value_count; i++) {
86 LLVMValueRef ptr = values[i * value_stride];
87 LLVMValueRef index = LLVMConstInt(ac->i32, i, false);
88 LLVMValueRef value = LLVMBuildExtractElement(builder, vec, index, "");
89 LLVMBuildStore(builder, value, ptr);
90 }
91 }
92
93 static LLVMTypeRef get_def_type(struct ac_nir_context *ctx,
94 const nir_ssa_def *def)
95 {
96 LLVMTypeRef type = LLVMIntTypeInContext(ctx->ac.context, def->bit_size);
97 if (def->num_components > 1) {
98 type = LLVMVectorType(type, def->num_components);
99 }
100 return type;
101 }
102
103 static LLVMValueRef get_src(struct ac_nir_context *nir, nir_src src)
104 {
105 assert(src.is_ssa);
106 return nir->ssa_defs[src.ssa->index];
107 }
108
109 static LLVMValueRef
110 get_memory_ptr(struct ac_nir_context *ctx, nir_src src, unsigned bit_size)
111 {
112 LLVMValueRef ptr = get_src(ctx, src);
113 ptr = LLVMBuildGEP(ctx->ac.builder, ctx->ac.lds, &ptr, 1, "");
114 int addr_space = LLVMGetPointerAddressSpace(LLVMTypeOf(ptr));
115
116 LLVMTypeRef type = LLVMIntTypeInContext(ctx->ac.context, bit_size);
117
118 return LLVMBuildBitCast(ctx->ac.builder, ptr,
119 LLVMPointerType(type, addr_space), "");
120 }
121
122 static LLVMBasicBlockRef get_block(struct ac_nir_context *nir,
123 const struct nir_block *b)
124 {
125 struct hash_entry *entry = _mesa_hash_table_search(nir->defs, b);
126 return (LLVMBasicBlockRef)entry->data;
127 }
128
129 static LLVMValueRef get_alu_src(struct ac_nir_context *ctx,
130 nir_alu_src src,
131 unsigned num_components)
132 {
133 LLVMValueRef value = get_src(ctx, src.src);
134 bool need_swizzle = false;
135
136 assert(value);
137 unsigned src_components = ac_get_llvm_num_components(value);
138 for (unsigned i = 0; i < num_components; ++i) {
139 assert(src.swizzle[i] < src_components);
140 if (src.swizzle[i] != i)
141 need_swizzle = true;
142 }
143
144 if (need_swizzle || num_components != src_components) {
145 LLVMValueRef masks[] = {
146 LLVMConstInt(ctx->ac.i32, src.swizzle[0], false),
147 LLVMConstInt(ctx->ac.i32, src.swizzle[1], false),
148 LLVMConstInt(ctx->ac.i32, src.swizzle[2], false),
149 LLVMConstInt(ctx->ac.i32, src.swizzle[3], false)};
150
151 if (src_components > 1 && num_components == 1) {
152 value = LLVMBuildExtractElement(ctx->ac.builder, value,
153 masks[0], "");
154 } else if (src_components == 1 && num_components > 1) {
155 LLVMValueRef values[] = {value, value, value, value};
156 value = ac_build_gather_values(&ctx->ac, values, num_components);
157 } else {
158 LLVMValueRef swizzle = LLVMConstVector(masks, num_components);
159 value = LLVMBuildShuffleVector(ctx->ac.builder, value, value,
160 swizzle, "");
161 }
162 }
163 assert(!src.negate);
164 assert(!src.abs);
165 return value;
166 }
167
168 static LLVMValueRef emit_int_cmp(struct ac_llvm_context *ctx,
169 LLVMIntPredicate pred, LLVMValueRef src0,
170 LLVMValueRef src1)
171 {
172 LLVMValueRef result = LLVMBuildICmp(ctx->builder, pred, src0, src1, "");
173 return LLVMBuildSelect(ctx->builder, result,
174 LLVMConstInt(ctx->i32, 0xFFFFFFFF, false),
175 ctx->i32_0, "");
176 }
177
178 static LLVMValueRef emit_float_cmp(struct ac_llvm_context *ctx,
179 LLVMRealPredicate pred, LLVMValueRef src0,
180 LLVMValueRef src1)
181 {
182 LLVMValueRef result;
183 src0 = ac_to_float(ctx, src0);
184 src1 = ac_to_float(ctx, src1);
185 result = LLVMBuildFCmp(ctx->builder, pred, src0, src1, "");
186 return LLVMBuildSelect(ctx->builder, result,
187 LLVMConstInt(ctx->i32, 0xFFFFFFFF, false),
188 ctx->i32_0, "");
189 }
190
191 static LLVMValueRef emit_intrin_1f_param(struct ac_llvm_context *ctx,
192 const char *intrin,
193 LLVMTypeRef result_type,
194 LLVMValueRef src0)
195 {
196 char name[64];
197 LLVMValueRef params[] = {
198 ac_to_float(ctx, src0),
199 };
200
201 ASSERTED const int length = snprintf(name, sizeof(name), "%s.f%d", intrin,
202 ac_get_elem_bits(ctx, result_type));
203 assert(length < sizeof(name));
204 return ac_build_intrinsic(ctx, name, result_type, params, 1, AC_FUNC_ATTR_READNONE);
205 }
206
207 static LLVMValueRef emit_intrin_2f_param(struct ac_llvm_context *ctx,
208 const char *intrin,
209 LLVMTypeRef result_type,
210 LLVMValueRef src0, LLVMValueRef src1)
211 {
212 char name[64];
213 LLVMValueRef params[] = {
214 ac_to_float(ctx, src0),
215 ac_to_float(ctx, src1),
216 };
217
218 ASSERTED const int length = snprintf(name, sizeof(name), "%s.f%d", intrin,
219 ac_get_elem_bits(ctx, result_type));
220 assert(length < sizeof(name));
221 return ac_build_intrinsic(ctx, name, result_type, params, 2, AC_FUNC_ATTR_READNONE);
222 }
223
224 static LLVMValueRef emit_intrin_3f_param(struct ac_llvm_context *ctx,
225 const char *intrin,
226 LLVMTypeRef result_type,
227 LLVMValueRef src0, LLVMValueRef src1, LLVMValueRef src2)
228 {
229 char name[64];
230 LLVMValueRef params[] = {
231 ac_to_float(ctx, src0),
232 ac_to_float(ctx, src1),
233 ac_to_float(ctx, src2),
234 };
235
236 ASSERTED const int length = snprintf(name, sizeof(name), "%s.f%d", intrin,
237 ac_get_elem_bits(ctx, result_type));
238 assert(length < sizeof(name));
239 return ac_build_intrinsic(ctx, name, result_type, params, 3, AC_FUNC_ATTR_READNONE);
240 }
241
242 static LLVMValueRef emit_bcsel(struct ac_llvm_context *ctx,
243 LLVMValueRef src0, LLVMValueRef src1, LLVMValueRef src2)
244 {
245 LLVMTypeRef src1_type = LLVMTypeOf(src1);
246 LLVMTypeRef src2_type = LLVMTypeOf(src2);
247
248 assert(LLVMGetTypeKind(LLVMTypeOf(src0)) != LLVMVectorTypeKind);
249
250 if (LLVMGetTypeKind(src1_type) == LLVMPointerTypeKind &&
251 LLVMGetTypeKind(src2_type) != LLVMPointerTypeKind) {
252 src2 = LLVMBuildIntToPtr(ctx->builder, src2, src1_type, "");
253 } else if (LLVMGetTypeKind(src2_type) == LLVMPointerTypeKind &&
254 LLVMGetTypeKind(src1_type) != LLVMPointerTypeKind) {
255 src1 = LLVMBuildIntToPtr(ctx->builder, src1, src2_type, "");
256 }
257
258 LLVMValueRef v = LLVMBuildICmp(ctx->builder, LLVMIntNE, src0,
259 ctx->i32_0, "");
260 return LLVMBuildSelect(ctx->builder, v,
261 ac_to_integer_or_pointer(ctx, src1),
262 ac_to_integer_or_pointer(ctx, src2), "");
263 }
264
265 static LLVMValueRef emit_iabs(struct ac_llvm_context *ctx,
266 LLVMValueRef src0)
267 {
268 return ac_build_imax(ctx, src0, LLVMBuildNeg(ctx->builder, src0, ""));
269 }
270
271 static LLVMValueRef emit_uint_carry(struct ac_llvm_context *ctx,
272 const char *intrin,
273 LLVMValueRef src0, LLVMValueRef src1)
274 {
275 LLVMTypeRef ret_type;
276 LLVMTypeRef types[] = { ctx->i32, ctx->i1 };
277 LLVMValueRef res;
278 LLVMValueRef params[] = { src0, src1 };
279 ret_type = LLVMStructTypeInContext(ctx->context, types,
280 2, true);
281
282 res = ac_build_intrinsic(ctx, intrin, ret_type,
283 params, 2, AC_FUNC_ATTR_READNONE);
284
285 res = LLVMBuildExtractValue(ctx->builder, res, 1, "");
286 res = LLVMBuildZExt(ctx->builder, res, ctx->i32, "");
287 return res;
288 }
289
290 static LLVMValueRef emit_b2f(struct ac_llvm_context *ctx,
291 LLVMValueRef src0,
292 unsigned bitsize)
293 {
294 LLVMValueRef result = LLVMBuildAnd(ctx->builder, src0,
295 LLVMBuildBitCast(ctx->builder, LLVMConstReal(ctx->f32, 1.0), ctx->i32, ""),
296 "");
297 result = LLVMBuildBitCast(ctx->builder, result, ctx->f32, "");
298
299 switch (bitsize) {
300 case 16:
301 return LLVMBuildFPTrunc(ctx->builder, result, ctx->f16, "");
302 case 32:
303 return result;
304 case 64:
305 return LLVMBuildFPExt(ctx->builder, result, ctx->f64, "");
306 default:
307 unreachable("Unsupported bit size.");
308 }
309 }
310
311 static LLVMValueRef emit_f2b(struct ac_llvm_context *ctx,
312 LLVMValueRef src0)
313 {
314 src0 = ac_to_float(ctx, src0);
315 LLVMValueRef zero = LLVMConstNull(LLVMTypeOf(src0));
316 return LLVMBuildSExt(ctx->builder,
317 LLVMBuildFCmp(ctx->builder, LLVMRealUNE, src0, zero, ""),
318 ctx->i32, "");
319 }
320
321 static LLVMValueRef emit_b2i(struct ac_llvm_context *ctx,
322 LLVMValueRef src0,
323 unsigned bitsize)
324 {
325 LLVMValueRef result = LLVMBuildAnd(ctx->builder, src0, ctx->i32_1, "");
326
327 switch (bitsize) {
328 case 8:
329 return LLVMBuildTrunc(ctx->builder, result, ctx->i8, "");
330 case 16:
331 return LLVMBuildTrunc(ctx->builder, result, ctx->i16, "");
332 case 32:
333 return result;
334 case 64:
335 return LLVMBuildZExt(ctx->builder, result, ctx->i64, "");
336 default:
337 unreachable("Unsupported bit size.");
338 }
339 }
340
341 static LLVMValueRef emit_i2b(struct ac_llvm_context *ctx,
342 LLVMValueRef src0)
343 {
344 LLVMValueRef zero = LLVMConstNull(LLVMTypeOf(src0));
345 return LLVMBuildSExt(ctx->builder,
346 LLVMBuildICmp(ctx->builder, LLVMIntNE, src0, zero, ""),
347 ctx->i32, "");
348 }
349
350 static LLVMValueRef emit_f2f16(struct ac_llvm_context *ctx,
351 LLVMValueRef src0)
352 {
353 LLVMValueRef result;
354 LLVMValueRef cond = NULL;
355
356 src0 = ac_to_float(ctx, src0);
357 result = LLVMBuildFPTrunc(ctx->builder, src0, ctx->f16, "");
358
359 if (ctx->chip_class >= GFX8) {
360 LLVMValueRef args[2];
361 /* Check if the result is a denormal - and flush to 0 if so. */
362 args[0] = result;
363 args[1] = LLVMConstInt(ctx->i32, N_SUBNORMAL | P_SUBNORMAL, false);
364 cond = ac_build_intrinsic(ctx, "llvm.amdgcn.class.f16", ctx->i1, args, 2, AC_FUNC_ATTR_READNONE);
365 }
366
367 /* need to convert back up to f32 */
368 result = LLVMBuildFPExt(ctx->builder, result, ctx->f32, "");
369
370 if (ctx->chip_class >= GFX8)
371 result = LLVMBuildSelect(ctx->builder, cond, ctx->f32_0, result, "");
372 else {
373 /* for GFX6-GFX7 */
374 /* 0x38800000 is smallest half float value (2^-14) in 32-bit float,
375 * so compare the result and flush to 0 if it's smaller.
376 */
377 LLVMValueRef temp, cond2;
378 temp = emit_intrin_1f_param(ctx, "llvm.fabs", ctx->f32, result);
379 cond = LLVMBuildFCmp(ctx->builder, LLVMRealOGT,
380 LLVMBuildBitCast(ctx->builder, LLVMConstInt(ctx->i32, 0x38800000, false), ctx->f32, ""),
381 temp, "");
382 cond2 = LLVMBuildFCmp(ctx->builder, LLVMRealONE,
383 temp, ctx->f32_0, "");
384 cond = LLVMBuildAnd(ctx->builder, cond, cond2, "");
385 result = LLVMBuildSelect(ctx->builder, cond, ctx->f32_0, result, "");
386 }
387 return result;
388 }
389
390 static LLVMValueRef emit_umul_high(struct ac_llvm_context *ctx,
391 LLVMValueRef src0, LLVMValueRef src1)
392 {
393 LLVMValueRef dst64, result;
394 src0 = LLVMBuildZExt(ctx->builder, src0, ctx->i64, "");
395 src1 = LLVMBuildZExt(ctx->builder, src1, ctx->i64, "");
396
397 dst64 = LLVMBuildMul(ctx->builder, src0, src1, "");
398 dst64 = LLVMBuildLShr(ctx->builder, dst64, LLVMConstInt(ctx->i64, 32, false), "");
399 result = LLVMBuildTrunc(ctx->builder, dst64, ctx->i32, "");
400 return result;
401 }
402
403 static LLVMValueRef emit_imul_high(struct ac_llvm_context *ctx,
404 LLVMValueRef src0, LLVMValueRef src1)
405 {
406 LLVMValueRef dst64, result;
407 src0 = LLVMBuildSExt(ctx->builder, src0, ctx->i64, "");
408 src1 = LLVMBuildSExt(ctx->builder, src1, ctx->i64, "");
409
410 dst64 = LLVMBuildMul(ctx->builder, src0, src1, "");
411 dst64 = LLVMBuildAShr(ctx->builder, dst64, LLVMConstInt(ctx->i64, 32, false), "");
412 result = LLVMBuildTrunc(ctx->builder, dst64, ctx->i32, "");
413 return result;
414 }
415
416 static LLVMValueRef emit_bfm(struct ac_llvm_context *ctx,
417 LLVMValueRef bits, LLVMValueRef offset)
418 {
419 /* mask = ((1 << bits) - 1) << offset */
420 return LLVMBuildShl(ctx->builder,
421 LLVMBuildSub(ctx->builder,
422 LLVMBuildShl(ctx->builder,
423 ctx->i32_1,
424 bits, ""),
425 ctx->i32_1, ""),
426 offset, "");
427 }
428
429 static LLVMValueRef emit_bitfield_select(struct ac_llvm_context *ctx,
430 LLVMValueRef mask, LLVMValueRef insert,
431 LLVMValueRef base)
432 {
433 /* Calculate:
434 * (mask & insert) | (~mask & base) = base ^ (mask & (insert ^ base))
435 * Use the right-hand side, which the LLVM backend can convert to V_BFI.
436 */
437 return LLVMBuildXor(ctx->builder, base,
438 LLVMBuildAnd(ctx->builder, mask,
439 LLVMBuildXor(ctx->builder, insert, base, ""), ""), "");
440 }
441
442 static LLVMValueRef emit_pack_2x16(struct ac_llvm_context *ctx,
443 LLVMValueRef src0,
444 LLVMValueRef (*pack)(struct ac_llvm_context *ctx,
445 LLVMValueRef args[2]))
446 {
447 LLVMValueRef comp[2];
448
449 src0 = ac_to_float(ctx, src0);
450 comp[0] = LLVMBuildExtractElement(ctx->builder, src0, ctx->i32_0, "");
451 comp[1] = LLVMBuildExtractElement(ctx->builder, src0, ctx->i32_1, "");
452
453 return LLVMBuildBitCast(ctx->builder, pack(ctx, comp), ctx->i32, "");
454 }
455
456 static LLVMValueRef emit_unpack_half_2x16(struct ac_llvm_context *ctx,
457 LLVMValueRef src0)
458 {
459 LLVMValueRef const16 = LLVMConstInt(ctx->i32, 16, false);
460 LLVMValueRef temps[2], val;
461 int i;
462
463 for (i = 0; i < 2; i++) {
464 val = i == 1 ? LLVMBuildLShr(ctx->builder, src0, const16, "") : src0;
465 val = LLVMBuildTrunc(ctx->builder, val, ctx->i16, "");
466 val = LLVMBuildBitCast(ctx->builder, val, ctx->f16, "");
467 temps[i] = LLVMBuildFPExt(ctx->builder, val, ctx->f32, "");
468 }
469 return ac_build_gather_values(ctx, temps, 2);
470 }
471
472 static LLVMValueRef emit_ddxy(struct ac_nir_context *ctx,
473 nir_op op,
474 LLVMValueRef src0)
475 {
476 unsigned mask;
477 int idx;
478 LLVMValueRef result;
479
480 if (op == nir_op_fddx_fine)
481 mask = AC_TID_MASK_LEFT;
482 else if (op == nir_op_fddy_fine)
483 mask = AC_TID_MASK_TOP;
484 else
485 mask = AC_TID_MASK_TOP_LEFT;
486
487 /* for DDX we want to next X pixel, DDY next Y pixel. */
488 if (op == nir_op_fddx_fine ||
489 op == nir_op_fddx_coarse ||
490 op == nir_op_fddx)
491 idx = 1;
492 else
493 idx = 2;
494
495 result = ac_build_ddxy(&ctx->ac, mask, idx, src0);
496 return result;
497 }
498
499 struct waterfall_context {
500 LLVMBasicBlockRef phi_bb[2];
501 bool use_waterfall;
502 };
503
504 /* To deal with divergent descriptors we can create a loop that handles all
505 * lanes with the same descriptor on a given iteration (henceforth a
506 * waterfall loop).
507 *
508 * These helper create the begin and end of the loop leaving the caller
509 * to implement the body.
510 *
511 * params:
512 * - ctx is the usal nir context
513 * - wctx is a temporary struct containing some loop info. Can be left uninitialized.
514 * - value is the possibly divergent value for which we built the loop
515 * - divergent is whether value is actually divergent. If false we just pass
516 * things through.
517 */
518 static LLVMValueRef enter_waterfall(struct ac_nir_context *ctx,
519 struct waterfall_context *wctx,
520 LLVMValueRef value, bool divergent)
521 {
522 /* If the app claims the value is divergent but it is constant we can
523 * end up with a dynamic index of NULL. */
524 if (!value)
525 divergent = false;
526
527 wctx->use_waterfall = divergent;
528 if (!divergent)
529 return value;
530
531 ac_build_bgnloop(&ctx->ac, 6000);
532
533 LLVMValueRef scalar_value = ac_build_readlane(&ctx->ac, value, NULL);
534
535 LLVMValueRef active = LLVMBuildICmp(ctx->ac.builder, LLVMIntEQ, value,
536 scalar_value, "uniform_active");
537
538 wctx->phi_bb[0] = LLVMGetInsertBlock(ctx->ac.builder);
539 ac_build_ifcc(&ctx->ac, active, 6001);
540
541 return scalar_value;
542 }
543
544 static LLVMValueRef exit_waterfall(struct ac_nir_context *ctx,
545 struct waterfall_context *wctx,
546 LLVMValueRef value)
547 {
548 LLVMValueRef ret = NULL;
549 LLVMValueRef phi_src[2];
550 LLVMValueRef cc_phi_src[2] = {
551 LLVMConstInt(ctx->ac.i32, 0, false),
552 LLVMConstInt(ctx->ac.i32, 0xffffffff, false),
553 };
554
555 if (!wctx->use_waterfall)
556 return value;
557
558 wctx->phi_bb[1] = LLVMGetInsertBlock(ctx->ac.builder);
559
560 ac_build_endif(&ctx->ac, 6001);
561
562 if (value) {
563 phi_src[0] = LLVMGetUndef(LLVMTypeOf(value));
564 phi_src[1] = value;
565
566 ret = ac_build_phi(&ctx->ac, LLVMTypeOf(value), 2, phi_src, wctx->phi_bb);
567 }
568
569 /*
570 * By using the optimization barrier on the exit decision, we decouple
571 * the operations from the break, and hence avoid LLVM hoisting the
572 * opteration into the break block.
573 */
574 LLVMValueRef cc = ac_build_phi(&ctx->ac, ctx->ac.i32, 2, cc_phi_src, wctx->phi_bb);
575 ac_build_optimization_barrier(&ctx->ac, &cc);
576
577 LLVMValueRef active = LLVMBuildICmp(ctx->ac.builder, LLVMIntNE, cc, ctx->ac.i32_0, "uniform_active2");
578 ac_build_ifcc(&ctx->ac, active, 6002);
579 ac_build_break(&ctx->ac);
580 ac_build_endif(&ctx->ac, 6002);
581
582 ac_build_endloop(&ctx->ac, 6000);
583 return ret;
584 }
585
586 static void visit_alu(struct ac_nir_context *ctx, const nir_alu_instr *instr)
587 {
588 LLVMValueRef src[4], result = NULL;
589 unsigned num_components = instr->dest.dest.ssa.num_components;
590 unsigned src_components;
591 LLVMTypeRef def_type = get_def_type(ctx, &instr->dest.dest.ssa);
592
593 assert(nir_op_infos[instr->op].num_inputs <= ARRAY_SIZE(src));
594 switch (instr->op) {
595 case nir_op_vec2:
596 case nir_op_vec3:
597 case nir_op_vec4:
598 src_components = 1;
599 break;
600 case nir_op_pack_half_2x16:
601 case nir_op_pack_snorm_2x16:
602 case nir_op_pack_unorm_2x16:
603 src_components = 2;
604 break;
605 case nir_op_unpack_half_2x16:
606 src_components = 1;
607 break;
608 case nir_op_cube_face_coord:
609 case nir_op_cube_face_index:
610 src_components = 3;
611 break;
612 default:
613 src_components = num_components;
614 break;
615 }
616 for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++)
617 src[i] = get_alu_src(ctx, instr->src[i], src_components);
618
619 switch (instr->op) {
620 case nir_op_mov:
621 result = src[0];
622 break;
623 case nir_op_fneg:
624 src[0] = ac_to_float(&ctx->ac, src[0]);
625 result = LLVMBuildFNeg(ctx->ac.builder, src[0], "");
626 if (ctx->ac.float_mode == AC_FLOAT_MODE_DENORM_FLUSH_TO_ZERO) {
627 /* fneg will be optimized by backend compiler with sign
628 * bit removed via XOR. This is probably a LLVM bug.
629 */
630 result = ac_build_canonicalize(&ctx->ac, result,
631 instr->dest.dest.ssa.bit_size);
632 }
633 break;
634 case nir_op_ineg:
635 result = LLVMBuildNeg(ctx->ac.builder, src[0], "");
636 break;
637 case nir_op_inot:
638 result = LLVMBuildNot(ctx->ac.builder, src[0], "");
639 break;
640 case nir_op_iadd:
641 result = LLVMBuildAdd(ctx->ac.builder, src[0], src[1], "");
642 break;
643 case nir_op_fadd:
644 src[0] = ac_to_float(&ctx->ac, src[0]);
645 src[1] = ac_to_float(&ctx->ac, src[1]);
646 result = LLVMBuildFAdd(ctx->ac.builder, src[0], src[1], "");
647 break;
648 case nir_op_fsub:
649 src[0] = ac_to_float(&ctx->ac, src[0]);
650 src[1] = ac_to_float(&ctx->ac, src[1]);
651 result = LLVMBuildFSub(ctx->ac.builder, src[0], src[1], "");
652 break;
653 case nir_op_isub:
654 result = LLVMBuildSub(ctx->ac.builder, src[0], src[1], "");
655 break;
656 case nir_op_imul:
657 result = LLVMBuildMul(ctx->ac.builder, src[0], src[1], "");
658 break;
659 case nir_op_imod:
660 result = LLVMBuildSRem(ctx->ac.builder, src[0], src[1], "");
661 break;
662 case nir_op_umod:
663 result = LLVMBuildURem(ctx->ac.builder, src[0], src[1], "");
664 break;
665 case nir_op_fmod:
666 /* lower_fmod only lower 16-bit and 32-bit fmod */
667 assert(instr->dest.dest.ssa.bit_size == 64);
668 src[0] = ac_to_float(&ctx->ac, src[0]);
669 src[1] = ac_to_float(&ctx->ac, src[1]);
670 result = ac_build_fdiv(&ctx->ac, src[0], src[1]);
671 result = emit_intrin_1f_param(&ctx->ac, "llvm.floor",
672 ac_to_float_type(&ctx->ac, def_type), result);
673 result = LLVMBuildFMul(ctx->ac.builder, src[1] , result, "");
674 result = LLVMBuildFSub(ctx->ac.builder, src[0], result, "");
675 break;
676 case nir_op_irem:
677 result = LLVMBuildSRem(ctx->ac.builder, src[0], src[1], "");
678 break;
679 case nir_op_idiv:
680 result = LLVMBuildSDiv(ctx->ac.builder, src[0], src[1], "");
681 break;
682 case nir_op_udiv:
683 result = LLVMBuildUDiv(ctx->ac.builder, src[0], src[1], "");
684 break;
685 case nir_op_fmul:
686 src[0] = ac_to_float(&ctx->ac, src[0]);
687 src[1] = ac_to_float(&ctx->ac, src[1]);
688 result = LLVMBuildFMul(ctx->ac.builder, src[0], src[1], "");
689 break;
690 case nir_op_frcp:
691 result = emit_intrin_1f_param(&ctx->ac, "llvm.amdgcn.rcp",
692 ac_to_float_type(&ctx->ac, def_type), src[0]);
693 break;
694 case nir_op_iand:
695 result = LLVMBuildAnd(ctx->ac.builder, src[0], src[1], "");
696 break;
697 case nir_op_ior:
698 result = LLVMBuildOr(ctx->ac.builder, src[0], src[1], "");
699 break;
700 case nir_op_ixor:
701 result = LLVMBuildXor(ctx->ac.builder, src[0], src[1], "");
702 break;
703 case nir_op_ishl:
704 if (ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src[1])) < ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src[0])))
705 src[1] = LLVMBuildZExt(ctx->ac.builder, src[1],
706 LLVMTypeOf(src[0]), "");
707 else if (ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src[1])) > ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src[0])))
708 src[1] = LLVMBuildTrunc(ctx->ac.builder, src[1],
709 LLVMTypeOf(src[0]), "");
710 result = LLVMBuildShl(ctx->ac.builder, src[0], src[1], "");
711 break;
712 case nir_op_ishr:
713 if (ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src[1])) < ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src[0])))
714 src[1] = LLVMBuildZExt(ctx->ac.builder, src[1],
715 LLVMTypeOf(src[0]), "");
716 else if (ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src[1])) > ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src[0])))
717 src[1] = LLVMBuildTrunc(ctx->ac.builder, src[1],
718 LLVMTypeOf(src[0]), "");
719 result = LLVMBuildAShr(ctx->ac.builder, src[0], src[1], "");
720 break;
721 case nir_op_ushr:
722 if (ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src[1])) < ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src[0])))
723 src[1] = LLVMBuildZExt(ctx->ac.builder, src[1],
724 LLVMTypeOf(src[0]), "");
725 else if (ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src[1])) > ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src[0])))
726 src[1] = LLVMBuildTrunc(ctx->ac.builder, src[1],
727 LLVMTypeOf(src[0]), "");
728 result = LLVMBuildLShr(ctx->ac.builder, src[0], src[1], "");
729 break;
730 case nir_op_ilt32:
731 result = emit_int_cmp(&ctx->ac, LLVMIntSLT, src[0], src[1]);
732 break;
733 case nir_op_ine32:
734 result = emit_int_cmp(&ctx->ac, LLVMIntNE, src[0], src[1]);
735 break;
736 case nir_op_ieq32:
737 result = emit_int_cmp(&ctx->ac, LLVMIntEQ, src[0], src[1]);
738 break;
739 case nir_op_ige32:
740 result = emit_int_cmp(&ctx->ac, LLVMIntSGE, src[0], src[1]);
741 break;
742 case nir_op_ult32:
743 result = emit_int_cmp(&ctx->ac, LLVMIntULT, src[0], src[1]);
744 break;
745 case nir_op_uge32:
746 result = emit_int_cmp(&ctx->ac, LLVMIntUGE, src[0], src[1]);
747 break;
748 case nir_op_feq32:
749 result = emit_float_cmp(&ctx->ac, LLVMRealOEQ, src[0], src[1]);
750 break;
751 case nir_op_fne32:
752 result = emit_float_cmp(&ctx->ac, LLVMRealUNE, src[0], src[1]);
753 break;
754 case nir_op_flt32:
755 result = emit_float_cmp(&ctx->ac, LLVMRealOLT, src[0], src[1]);
756 break;
757 case nir_op_fge32:
758 result = emit_float_cmp(&ctx->ac, LLVMRealOGE, src[0], src[1]);
759 break;
760 case nir_op_fabs:
761 result = emit_intrin_1f_param(&ctx->ac, "llvm.fabs",
762 ac_to_float_type(&ctx->ac, def_type), src[0]);
763 if (ctx->ac.float_mode == AC_FLOAT_MODE_DENORM_FLUSH_TO_ZERO) {
764 /* fabs will be optimized by backend compiler with sign
765 * bit removed via AND.
766 */
767 result = ac_build_canonicalize(&ctx->ac, result,
768 instr->dest.dest.ssa.bit_size);
769 }
770 break;
771 case nir_op_iabs:
772 result = emit_iabs(&ctx->ac, src[0]);
773 break;
774 case nir_op_imax:
775 result = ac_build_imax(&ctx->ac, src[0], src[1]);
776 break;
777 case nir_op_imin:
778 result = ac_build_imin(&ctx->ac, src[0], src[1]);
779 break;
780 case nir_op_umax:
781 result = ac_build_umax(&ctx->ac, src[0], src[1]);
782 break;
783 case nir_op_umin:
784 result = ac_build_umin(&ctx->ac, src[0], src[1]);
785 break;
786 case nir_op_isign:
787 result = ac_build_isign(&ctx->ac, src[0],
788 instr->dest.dest.ssa.bit_size);
789 break;
790 case nir_op_fsign:
791 src[0] = ac_to_float(&ctx->ac, src[0]);
792 result = ac_build_fsign(&ctx->ac, src[0],
793 instr->dest.dest.ssa.bit_size);
794 break;
795 case nir_op_ffloor:
796 result = emit_intrin_1f_param(&ctx->ac, "llvm.floor",
797 ac_to_float_type(&ctx->ac, def_type), src[0]);
798 break;
799 case nir_op_ftrunc:
800 result = emit_intrin_1f_param(&ctx->ac, "llvm.trunc",
801 ac_to_float_type(&ctx->ac, def_type), src[0]);
802 break;
803 case nir_op_fceil:
804 result = emit_intrin_1f_param(&ctx->ac, "llvm.ceil",
805 ac_to_float_type(&ctx->ac, def_type), src[0]);
806 break;
807 case nir_op_fround_even:
808 result = emit_intrin_1f_param(&ctx->ac, "llvm.rint",
809 ac_to_float_type(&ctx->ac, def_type),src[0]);
810 break;
811 case nir_op_ffract:
812 src[0] = ac_to_float(&ctx->ac, src[0]);
813 result = ac_build_fract(&ctx->ac, src[0],
814 instr->dest.dest.ssa.bit_size);
815 break;
816 case nir_op_fsin:
817 result = emit_intrin_1f_param(&ctx->ac, "llvm.sin",
818 ac_to_float_type(&ctx->ac, def_type), src[0]);
819 break;
820 case nir_op_fcos:
821 result = emit_intrin_1f_param(&ctx->ac, "llvm.cos",
822 ac_to_float_type(&ctx->ac, def_type), src[0]);
823 break;
824 case nir_op_fsqrt:
825 result = emit_intrin_1f_param(&ctx->ac, "llvm.sqrt",
826 ac_to_float_type(&ctx->ac, def_type), src[0]);
827 break;
828 case nir_op_fexp2:
829 result = emit_intrin_1f_param(&ctx->ac, "llvm.exp2",
830 ac_to_float_type(&ctx->ac, def_type), src[0]);
831 break;
832 case nir_op_flog2:
833 result = emit_intrin_1f_param(&ctx->ac, "llvm.log2",
834 ac_to_float_type(&ctx->ac, def_type), src[0]);
835 break;
836 case nir_op_frsq:
837 result = emit_intrin_1f_param(&ctx->ac, "llvm.amdgcn.rsq",
838 ac_to_float_type(&ctx->ac, def_type), src[0]);
839 break;
840 case nir_op_frexp_exp:
841 src[0] = ac_to_float(&ctx->ac, src[0]);
842 result = ac_build_frexp_exp(&ctx->ac, src[0],
843 ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src[0])));
844 if (ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src[0])) == 16)
845 result = LLVMBuildSExt(ctx->ac.builder, result,
846 ctx->ac.i32, "");
847 break;
848 case nir_op_frexp_sig:
849 src[0] = ac_to_float(&ctx->ac, src[0]);
850 result = ac_build_frexp_mant(&ctx->ac, src[0],
851 instr->dest.dest.ssa.bit_size);
852 break;
853 case nir_op_fpow:
854 result = emit_intrin_2f_param(&ctx->ac, "llvm.pow",
855 ac_to_float_type(&ctx->ac, def_type), src[0], src[1]);
856 break;
857 case nir_op_fmax:
858 result = emit_intrin_2f_param(&ctx->ac, "llvm.maxnum",
859 ac_to_float_type(&ctx->ac, def_type), src[0], src[1]);
860 if (ctx->ac.chip_class < GFX9 &&
861 instr->dest.dest.ssa.bit_size == 32) {
862 /* Only pre-GFX9 chips do not flush denorms. */
863 result = ac_build_canonicalize(&ctx->ac, result,
864 instr->dest.dest.ssa.bit_size);
865 }
866 break;
867 case nir_op_fmin:
868 result = emit_intrin_2f_param(&ctx->ac, "llvm.minnum",
869 ac_to_float_type(&ctx->ac, def_type), src[0], src[1]);
870 if (ctx->ac.chip_class < GFX9 &&
871 instr->dest.dest.ssa.bit_size == 32) {
872 /* Only pre-GFX9 chips do not flush denorms. */
873 result = ac_build_canonicalize(&ctx->ac, result,
874 instr->dest.dest.ssa.bit_size);
875 }
876 break;
877 case nir_op_ffma:
878 /* FMA is better on GFX10, because it has FMA units instead of MUL-ADD units. */
879 result = emit_intrin_3f_param(&ctx->ac, ctx->ac.chip_class >= GFX10 ? "llvm.fma" : "llvm.fmuladd",
880 ac_to_float_type(&ctx->ac, def_type), src[0], src[1], src[2]);
881 break;
882 case nir_op_ldexp:
883 src[0] = ac_to_float(&ctx->ac, src[0]);
884 if (ac_get_elem_bits(&ctx->ac, def_type) == 32)
885 result = ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.ldexp.f32", ctx->ac.f32, src, 2, AC_FUNC_ATTR_READNONE);
886 else if (ac_get_elem_bits(&ctx->ac, def_type) == 16)
887 result = ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.ldexp.f16", ctx->ac.f16, src, 2, AC_FUNC_ATTR_READNONE);
888 else
889 result = ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.ldexp.f64", ctx->ac.f64, src, 2, AC_FUNC_ATTR_READNONE);
890 break;
891 case nir_op_bfm:
892 result = emit_bfm(&ctx->ac, src[0], src[1]);
893 break;
894 case nir_op_bitfield_select:
895 result = emit_bitfield_select(&ctx->ac, src[0], src[1], src[2]);
896 break;
897 case nir_op_ubfe:
898 result = ac_build_bfe(&ctx->ac, src[0], src[1], src[2], false);
899 break;
900 case nir_op_ibfe:
901 result = ac_build_bfe(&ctx->ac, src[0], src[1], src[2], true);
902 break;
903 case nir_op_bitfield_reverse:
904 result = ac_build_bitfield_reverse(&ctx->ac, src[0]);
905 break;
906 case nir_op_bit_count:
907 result = ac_build_bit_count(&ctx->ac, src[0]);
908 break;
909 case nir_op_vec2:
910 case nir_op_vec3:
911 case nir_op_vec4:
912 for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++)
913 src[i] = ac_to_integer(&ctx->ac, src[i]);
914 result = ac_build_gather_values(&ctx->ac, src, num_components);
915 break;
916 case nir_op_f2i8:
917 case nir_op_f2i16:
918 case nir_op_f2i32:
919 case nir_op_f2i64:
920 src[0] = ac_to_float(&ctx->ac, src[0]);
921 result = LLVMBuildFPToSI(ctx->ac.builder, src[0], def_type, "");
922 break;
923 case nir_op_f2u8:
924 case nir_op_f2u16:
925 case nir_op_f2u32:
926 case nir_op_f2u64:
927 src[0] = ac_to_float(&ctx->ac, src[0]);
928 result = LLVMBuildFPToUI(ctx->ac.builder, src[0], def_type, "");
929 break;
930 case nir_op_i2f16:
931 case nir_op_i2f32:
932 case nir_op_i2f64:
933 result = LLVMBuildSIToFP(ctx->ac.builder, src[0], ac_to_float_type(&ctx->ac, def_type), "");
934 break;
935 case nir_op_u2f16:
936 case nir_op_u2f32:
937 case nir_op_u2f64:
938 result = LLVMBuildUIToFP(ctx->ac.builder, src[0], ac_to_float_type(&ctx->ac, def_type), "");
939 break;
940 case nir_op_f2f16_rtz:
941 src[0] = ac_to_float(&ctx->ac, src[0]);
942 if (LLVMTypeOf(src[0]) == ctx->ac.f64)
943 src[0] = LLVMBuildFPTrunc(ctx->ac.builder, src[0], ctx->ac.f32, "");
944 LLVMValueRef param[2] = { src[0], ctx->ac.f32_0 };
945 result = ac_build_cvt_pkrtz_f16(&ctx->ac, param);
946 result = LLVMBuildExtractElement(ctx->ac.builder, result, ctx->ac.i32_0, "");
947 break;
948 case nir_op_f2f16_rtne:
949 case nir_op_f2f16:
950 case nir_op_f2f32:
951 case nir_op_f2f64:
952 src[0] = ac_to_float(&ctx->ac, src[0]);
953 if (ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src[0])) < ac_get_elem_bits(&ctx->ac, def_type))
954 result = LLVMBuildFPExt(ctx->ac.builder, src[0], ac_to_float_type(&ctx->ac, def_type), "");
955 else
956 result = LLVMBuildFPTrunc(ctx->ac.builder, src[0], ac_to_float_type(&ctx->ac, def_type), "");
957 break;
958 case nir_op_u2u8:
959 case nir_op_u2u16:
960 case nir_op_u2u32:
961 case nir_op_u2u64:
962 if (ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src[0])) < ac_get_elem_bits(&ctx->ac, def_type))
963 result = LLVMBuildZExt(ctx->ac.builder, src[0], def_type, "");
964 else
965 result = LLVMBuildTrunc(ctx->ac.builder, src[0], def_type, "");
966 break;
967 case nir_op_i2i8:
968 case nir_op_i2i16:
969 case nir_op_i2i32:
970 case nir_op_i2i64:
971 if (ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src[0])) < ac_get_elem_bits(&ctx->ac, def_type))
972 result = LLVMBuildSExt(ctx->ac.builder, src[0], def_type, "");
973 else
974 result = LLVMBuildTrunc(ctx->ac.builder, src[0], def_type, "");
975 break;
976 case nir_op_b32csel:
977 result = emit_bcsel(&ctx->ac, src[0], src[1], src[2]);
978 break;
979 case nir_op_find_lsb:
980 result = ac_find_lsb(&ctx->ac, ctx->ac.i32, src[0]);
981 break;
982 case nir_op_ufind_msb:
983 result = ac_build_umsb(&ctx->ac, src[0], ctx->ac.i32);
984 break;
985 case nir_op_ifind_msb:
986 result = ac_build_imsb(&ctx->ac, src[0], ctx->ac.i32);
987 break;
988 case nir_op_uadd_carry:
989 result = emit_uint_carry(&ctx->ac, "llvm.uadd.with.overflow.i32", src[0], src[1]);
990 break;
991 case nir_op_usub_borrow:
992 result = emit_uint_carry(&ctx->ac, "llvm.usub.with.overflow.i32", src[0], src[1]);
993 break;
994 case nir_op_b2f16:
995 case nir_op_b2f32:
996 case nir_op_b2f64:
997 result = emit_b2f(&ctx->ac, src[0], instr->dest.dest.ssa.bit_size);
998 break;
999 case nir_op_f2b32:
1000 result = emit_f2b(&ctx->ac, src[0]);
1001 break;
1002 case nir_op_b2i8:
1003 case nir_op_b2i16:
1004 case nir_op_b2i32:
1005 case nir_op_b2i64:
1006 result = emit_b2i(&ctx->ac, src[0], instr->dest.dest.ssa.bit_size);
1007 break;
1008 case nir_op_i2b32:
1009 result = emit_i2b(&ctx->ac, src[0]);
1010 break;
1011 case nir_op_fquantize2f16:
1012 result = emit_f2f16(&ctx->ac, src[0]);
1013 break;
1014 case nir_op_umul_high:
1015 result = emit_umul_high(&ctx->ac, src[0], src[1]);
1016 break;
1017 case nir_op_imul_high:
1018 result = emit_imul_high(&ctx->ac, src[0], src[1]);
1019 break;
1020 case nir_op_pack_half_2x16:
1021 result = emit_pack_2x16(&ctx->ac, src[0], ac_build_cvt_pkrtz_f16);
1022 break;
1023 case nir_op_pack_snorm_2x16:
1024 result = emit_pack_2x16(&ctx->ac, src[0], ac_build_cvt_pknorm_i16);
1025 break;
1026 case nir_op_pack_unorm_2x16:
1027 result = emit_pack_2x16(&ctx->ac, src[0], ac_build_cvt_pknorm_u16);
1028 break;
1029 case nir_op_unpack_half_2x16:
1030 result = emit_unpack_half_2x16(&ctx->ac, src[0]);
1031 break;
1032 case nir_op_fddx:
1033 case nir_op_fddy:
1034 case nir_op_fddx_fine:
1035 case nir_op_fddy_fine:
1036 case nir_op_fddx_coarse:
1037 case nir_op_fddy_coarse:
1038 result = emit_ddxy(ctx, instr->op, src[0]);
1039 break;
1040
1041 case nir_op_unpack_64_2x32_split_x: {
1042 assert(ac_get_llvm_num_components(src[0]) == 1);
1043 LLVMValueRef tmp = LLVMBuildBitCast(ctx->ac.builder, src[0],
1044 ctx->ac.v2i32,
1045 "");
1046 result = LLVMBuildExtractElement(ctx->ac.builder, tmp,
1047 ctx->ac.i32_0, "");
1048 break;
1049 }
1050
1051 case nir_op_unpack_64_2x32_split_y: {
1052 assert(ac_get_llvm_num_components(src[0]) == 1);
1053 LLVMValueRef tmp = LLVMBuildBitCast(ctx->ac.builder, src[0],
1054 ctx->ac.v2i32,
1055 "");
1056 result = LLVMBuildExtractElement(ctx->ac.builder, tmp,
1057 ctx->ac.i32_1, "");
1058 break;
1059 }
1060
1061 case nir_op_pack_64_2x32_split: {
1062 LLVMValueRef tmp = ac_build_gather_values(&ctx->ac, src, 2);
1063 result = LLVMBuildBitCast(ctx->ac.builder, tmp, ctx->ac.i64, "");
1064 break;
1065 }
1066
1067 case nir_op_pack_32_2x16_split: {
1068 LLVMValueRef tmp = ac_build_gather_values(&ctx->ac, src, 2);
1069 result = LLVMBuildBitCast(ctx->ac.builder, tmp, ctx->ac.i32, "");
1070 break;
1071 }
1072
1073 case nir_op_unpack_32_2x16_split_x: {
1074 LLVMValueRef tmp = LLVMBuildBitCast(ctx->ac.builder, src[0],
1075 ctx->ac.v2i16,
1076 "");
1077 result = LLVMBuildExtractElement(ctx->ac.builder, tmp,
1078 ctx->ac.i32_0, "");
1079 break;
1080 }
1081
1082 case nir_op_unpack_32_2x16_split_y: {
1083 LLVMValueRef tmp = LLVMBuildBitCast(ctx->ac.builder, src[0],
1084 ctx->ac.v2i16,
1085 "");
1086 result = LLVMBuildExtractElement(ctx->ac.builder, tmp,
1087 ctx->ac.i32_1, "");
1088 break;
1089 }
1090
1091 case nir_op_cube_face_coord: {
1092 src[0] = ac_to_float(&ctx->ac, src[0]);
1093 LLVMValueRef results[2];
1094 LLVMValueRef in[3];
1095 for (unsigned chan = 0; chan < 3; chan++)
1096 in[chan] = ac_llvm_extract_elem(&ctx->ac, src[0], chan);
1097 results[0] = ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.cubesc",
1098 ctx->ac.f32, in, 3, AC_FUNC_ATTR_READNONE);
1099 results[1] = ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.cubetc",
1100 ctx->ac.f32, in, 3, AC_FUNC_ATTR_READNONE);
1101 LLVMValueRef ma = ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.cubema",
1102 ctx->ac.f32, in, 3, AC_FUNC_ATTR_READNONE);
1103 results[0] = ac_build_fdiv(&ctx->ac, results[0], ma);
1104 results[1] = ac_build_fdiv(&ctx->ac, results[1], ma);
1105 LLVMValueRef offset = LLVMConstReal(ctx->ac.f32, 0.5);
1106 results[0] = LLVMBuildFAdd(ctx->ac.builder, results[0], offset, "");
1107 results[1] = LLVMBuildFAdd(ctx->ac.builder, results[1], offset, "");
1108 result = ac_build_gather_values(&ctx->ac, results, 2);
1109 break;
1110 }
1111
1112 case nir_op_cube_face_index: {
1113 src[0] = ac_to_float(&ctx->ac, src[0]);
1114 LLVMValueRef in[3];
1115 for (unsigned chan = 0; chan < 3; chan++)
1116 in[chan] = ac_llvm_extract_elem(&ctx->ac, src[0], chan);
1117 result = ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.cubeid",
1118 ctx->ac.f32, in, 3, AC_FUNC_ATTR_READNONE);
1119 break;
1120 }
1121
1122 case nir_op_fmin3:
1123 result = emit_intrin_2f_param(&ctx->ac, "llvm.minnum",
1124 ac_to_float_type(&ctx->ac, def_type), src[0], src[1]);
1125 result = emit_intrin_2f_param(&ctx->ac, "llvm.minnum",
1126 ac_to_float_type(&ctx->ac, def_type), result, src[2]);
1127 break;
1128 case nir_op_umin3:
1129 result = ac_build_umin(&ctx->ac, src[0], src[1]);
1130 result = ac_build_umin(&ctx->ac, result, src[2]);
1131 break;
1132 case nir_op_imin3:
1133 result = ac_build_imin(&ctx->ac, src[0], src[1]);
1134 result = ac_build_imin(&ctx->ac, result, src[2]);
1135 break;
1136 case nir_op_fmax3:
1137 result = emit_intrin_2f_param(&ctx->ac, "llvm.maxnum",
1138 ac_to_float_type(&ctx->ac, def_type), src[0], src[1]);
1139 result = emit_intrin_2f_param(&ctx->ac, "llvm.maxnum",
1140 ac_to_float_type(&ctx->ac, def_type), result, src[2]);
1141 break;
1142 case nir_op_umax3:
1143 result = ac_build_umax(&ctx->ac, src[0], src[1]);
1144 result = ac_build_umax(&ctx->ac, result, src[2]);
1145 break;
1146 case nir_op_imax3:
1147 result = ac_build_imax(&ctx->ac, src[0], src[1]);
1148 result = ac_build_imax(&ctx->ac, result, src[2]);
1149 break;
1150 case nir_op_fmed3: {
1151 src[0] = ac_to_float(&ctx->ac, src[0]);
1152 src[1] = ac_to_float(&ctx->ac, src[1]);
1153 src[2] = ac_to_float(&ctx->ac, src[2]);
1154 result = ac_build_fmed3(&ctx->ac, src[0], src[1], src[2],
1155 instr->dest.dest.ssa.bit_size);
1156 break;
1157 }
1158 case nir_op_imed3: {
1159 LLVMValueRef tmp1 = ac_build_imin(&ctx->ac, src[0], src[1]);
1160 LLVMValueRef tmp2 = ac_build_imax(&ctx->ac, src[0], src[1]);
1161 tmp2 = ac_build_imin(&ctx->ac, tmp2, src[2]);
1162 result = ac_build_imax(&ctx->ac, tmp1, tmp2);
1163 break;
1164 }
1165 case nir_op_umed3: {
1166 LLVMValueRef tmp1 = ac_build_umin(&ctx->ac, src[0], src[1]);
1167 LLVMValueRef tmp2 = ac_build_umax(&ctx->ac, src[0], src[1]);
1168 tmp2 = ac_build_umin(&ctx->ac, tmp2, src[2]);
1169 result = ac_build_umax(&ctx->ac, tmp1, tmp2);
1170 break;
1171 }
1172
1173 default:
1174 fprintf(stderr, "Unknown NIR alu instr: ");
1175 nir_print_instr(&instr->instr, stderr);
1176 fprintf(stderr, "\n");
1177 abort();
1178 }
1179
1180 if (result) {
1181 assert(instr->dest.dest.is_ssa);
1182 result = ac_to_integer_or_pointer(&ctx->ac, result);
1183 ctx->ssa_defs[instr->dest.dest.ssa.index] = result;
1184 }
1185 }
1186
1187 static void visit_load_const(struct ac_nir_context *ctx,
1188 const nir_load_const_instr *instr)
1189 {
1190 LLVMValueRef values[4], value = NULL;
1191 LLVMTypeRef element_type =
1192 LLVMIntTypeInContext(ctx->ac.context, instr->def.bit_size);
1193
1194 for (unsigned i = 0; i < instr->def.num_components; ++i) {
1195 switch (instr->def.bit_size) {
1196 case 8:
1197 values[i] = LLVMConstInt(element_type,
1198 instr->value[i].u8, false);
1199 break;
1200 case 16:
1201 values[i] = LLVMConstInt(element_type,
1202 instr->value[i].u16, false);
1203 break;
1204 case 32:
1205 values[i] = LLVMConstInt(element_type,
1206 instr->value[i].u32, false);
1207 break;
1208 case 64:
1209 values[i] = LLVMConstInt(element_type,
1210 instr->value[i].u64, false);
1211 break;
1212 default:
1213 fprintf(stderr,
1214 "unsupported nir load_const bit_size: %d\n",
1215 instr->def.bit_size);
1216 abort();
1217 }
1218 }
1219 if (instr->def.num_components > 1) {
1220 value = LLVMConstVector(values, instr->def.num_components);
1221 } else
1222 value = values[0];
1223
1224 ctx->ssa_defs[instr->def.index] = value;
1225 }
1226
1227 static LLVMValueRef
1228 get_buffer_size(struct ac_nir_context *ctx, LLVMValueRef descriptor, bool in_elements)
1229 {
1230 LLVMValueRef size =
1231 LLVMBuildExtractElement(ctx->ac.builder, descriptor,
1232 LLVMConstInt(ctx->ac.i32, 2, false), "");
1233
1234 /* GFX8 only */
1235 if (ctx->ac.chip_class == GFX8 && in_elements) {
1236 /* On GFX8, the descriptor contains the size in bytes,
1237 * but TXQ must return the size in elements.
1238 * The stride is always non-zero for resources using TXQ.
1239 */
1240 LLVMValueRef stride =
1241 LLVMBuildExtractElement(ctx->ac.builder, descriptor,
1242 ctx->ac.i32_1, "");
1243 stride = LLVMBuildLShr(ctx->ac.builder, stride,
1244 LLVMConstInt(ctx->ac.i32, 16, false), "");
1245 stride = LLVMBuildAnd(ctx->ac.builder, stride,
1246 LLVMConstInt(ctx->ac.i32, 0x3fff, false), "");
1247
1248 size = LLVMBuildUDiv(ctx->ac.builder, size, stride, "");
1249 }
1250 return size;
1251 }
1252
1253 /* Gather4 should follow the same rules as bilinear filtering, but the hardware
1254 * incorrectly forces nearest filtering if the texture format is integer.
1255 * The only effect it has on Gather4, which always returns 4 texels for
1256 * bilinear filtering, is that the final coordinates are off by 0.5 of
1257 * the texel size.
1258 *
1259 * The workaround is to subtract 0.5 from the unnormalized coordinates,
1260 * or (0.5 / size) from the normalized coordinates.
1261 *
1262 * However, cube textures with 8_8_8_8 data formats require a different
1263 * workaround of overriding the num format to USCALED/SSCALED. This would lose
1264 * precision in 32-bit data formats, so it needs to be applied dynamically at
1265 * runtime. In this case, return an i1 value that indicates whether the
1266 * descriptor was overridden (and hence a fixup of the sampler result is needed).
1267 */
1268 static LLVMValueRef lower_gather4_integer(struct ac_llvm_context *ctx,
1269 nir_variable *var,
1270 struct ac_image_args *args,
1271 const nir_tex_instr *instr)
1272 {
1273 const struct glsl_type *type = glsl_without_array(var->type);
1274 enum glsl_base_type stype = glsl_get_sampler_result_type(type);
1275 LLVMValueRef wa_8888 = NULL;
1276 LLVMValueRef half_texel[2];
1277 LLVMValueRef result;
1278
1279 assert(stype == GLSL_TYPE_INT || stype == GLSL_TYPE_UINT);
1280
1281 if (instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE) {
1282 LLVMValueRef formats;
1283 LLVMValueRef data_format;
1284 LLVMValueRef wa_formats;
1285
1286 formats = LLVMBuildExtractElement(ctx->builder, args->resource, ctx->i32_1, "");
1287
1288 data_format = LLVMBuildLShr(ctx->builder, formats,
1289 LLVMConstInt(ctx->i32, 20, false), "");
1290 data_format = LLVMBuildAnd(ctx->builder, data_format,
1291 LLVMConstInt(ctx->i32, (1u << 6) - 1, false), "");
1292 wa_8888 = LLVMBuildICmp(
1293 ctx->builder, LLVMIntEQ, data_format,
1294 LLVMConstInt(ctx->i32, V_008F14_IMG_DATA_FORMAT_8_8_8_8, false),
1295 "");
1296
1297 uint32_t wa_num_format =
1298 stype == GLSL_TYPE_UINT ?
1299 S_008F14_NUM_FORMAT(V_008F14_IMG_NUM_FORMAT_USCALED) :
1300 S_008F14_NUM_FORMAT(V_008F14_IMG_NUM_FORMAT_SSCALED);
1301 wa_formats = LLVMBuildAnd(ctx->builder, formats,
1302 LLVMConstInt(ctx->i32, C_008F14_NUM_FORMAT, false),
1303 "");
1304 wa_formats = LLVMBuildOr(ctx->builder, wa_formats,
1305 LLVMConstInt(ctx->i32, wa_num_format, false), "");
1306
1307 formats = LLVMBuildSelect(ctx->builder, wa_8888, wa_formats, formats, "");
1308 args->resource = LLVMBuildInsertElement(
1309 ctx->builder, args->resource, formats, ctx->i32_1, "");
1310 }
1311
1312 if (instr->sampler_dim == GLSL_SAMPLER_DIM_RECT) {
1313 assert(!wa_8888);
1314 half_texel[0] = half_texel[1] = LLVMConstReal(ctx->f32, -0.5);
1315 } else {
1316 struct ac_image_args resinfo = {};
1317 LLVMBasicBlockRef bbs[2];
1318
1319 LLVMValueRef unnorm = NULL;
1320 LLVMValueRef default_offset = ctx->f32_0;
1321 if (instr->sampler_dim == GLSL_SAMPLER_DIM_2D &&
1322 !instr->is_array) {
1323 /* In vulkan, whether the sampler uses unnormalized
1324 * coordinates or not is a dynamic property of the
1325 * sampler. Hence, to figure out whether or not we
1326 * need to divide by the texture size, we need to test
1327 * the sampler at runtime. This tests the bit set by
1328 * radv_init_sampler().
1329 */
1330 LLVMValueRef sampler0 =
1331 LLVMBuildExtractElement(ctx->builder, args->sampler, ctx->i32_0, "");
1332 sampler0 = LLVMBuildLShr(ctx->builder, sampler0,
1333 LLVMConstInt(ctx->i32, 15, false), "");
1334 sampler0 = LLVMBuildAnd(ctx->builder, sampler0, ctx->i32_1, "");
1335 unnorm = LLVMBuildICmp(ctx->builder, LLVMIntEQ, sampler0, ctx->i32_1, "");
1336 default_offset = LLVMConstReal(ctx->f32, -0.5);
1337 }
1338
1339 bbs[0] = LLVMGetInsertBlock(ctx->builder);
1340 if (wa_8888 || unnorm) {
1341 assert(!(wa_8888 && unnorm));
1342 LLVMValueRef not_needed = wa_8888 ? wa_8888 : unnorm;
1343 /* Skip the texture size query entirely if we don't need it. */
1344 ac_build_ifcc(ctx, LLVMBuildNot(ctx->builder, not_needed, ""), 2000);
1345 bbs[1] = LLVMGetInsertBlock(ctx->builder);
1346 }
1347
1348 /* Query the texture size. */
1349 resinfo.dim = ac_get_sampler_dim(ctx->chip_class, instr->sampler_dim, instr->is_array);
1350 resinfo.opcode = ac_image_get_resinfo;
1351 resinfo.dmask = 0xf;
1352 resinfo.lod = ctx->i32_0;
1353 resinfo.resource = args->resource;
1354 resinfo.attributes = AC_FUNC_ATTR_READNONE;
1355 LLVMValueRef size = ac_build_image_opcode(ctx, &resinfo);
1356
1357 /* Compute -0.5 / size. */
1358 for (unsigned c = 0; c < 2; c++) {
1359 half_texel[c] =
1360 LLVMBuildExtractElement(ctx->builder, size,
1361 LLVMConstInt(ctx->i32, c, 0), "");
1362 half_texel[c] = LLVMBuildUIToFP(ctx->builder, half_texel[c], ctx->f32, "");
1363 half_texel[c] = ac_build_fdiv(ctx, ctx->f32_1, half_texel[c]);
1364 half_texel[c] = LLVMBuildFMul(ctx->builder, half_texel[c],
1365 LLVMConstReal(ctx->f32, -0.5), "");
1366 }
1367
1368 if (wa_8888 || unnorm) {
1369 ac_build_endif(ctx, 2000);
1370
1371 for (unsigned c = 0; c < 2; c++) {
1372 LLVMValueRef values[2] = { default_offset, half_texel[c] };
1373 half_texel[c] = ac_build_phi(ctx, ctx->f32, 2,
1374 values, bbs);
1375 }
1376 }
1377 }
1378
1379 for (unsigned c = 0; c < 2; c++) {
1380 LLVMValueRef tmp;
1381 tmp = LLVMBuildBitCast(ctx->builder, args->coords[c], ctx->f32, "");
1382 args->coords[c] = LLVMBuildFAdd(ctx->builder, tmp, half_texel[c], "");
1383 }
1384
1385 args->attributes = AC_FUNC_ATTR_READNONE;
1386 result = ac_build_image_opcode(ctx, args);
1387
1388 if (instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE) {
1389 LLVMValueRef tmp, tmp2;
1390
1391 /* if the cube workaround is in place, f2i the result. */
1392 for (unsigned c = 0; c < 4; c++) {
1393 tmp = LLVMBuildExtractElement(ctx->builder, result, LLVMConstInt(ctx->i32, c, false), "");
1394 if (stype == GLSL_TYPE_UINT)
1395 tmp2 = LLVMBuildFPToUI(ctx->builder, tmp, ctx->i32, "");
1396 else
1397 tmp2 = LLVMBuildFPToSI(ctx->builder, tmp, ctx->i32, "");
1398 tmp = LLVMBuildBitCast(ctx->builder, tmp, ctx->i32, "");
1399 tmp2 = LLVMBuildBitCast(ctx->builder, tmp2, ctx->i32, "");
1400 tmp = LLVMBuildSelect(ctx->builder, wa_8888, tmp2, tmp, "");
1401 tmp = LLVMBuildBitCast(ctx->builder, tmp, ctx->f32, "");
1402 result = LLVMBuildInsertElement(ctx->builder, result, tmp, LLVMConstInt(ctx->i32, c, false), "");
1403 }
1404 }
1405 return result;
1406 }
1407
1408 static nir_deref_instr *get_tex_texture_deref(const nir_tex_instr *instr)
1409 {
1410 nir_deref_instr *texture_deref_instr = NULL;
1411
1412 for (unsigned i = 0; i < instr->num_srcs; i++) {
1413 switch (instr->src[i].src_type) {
1414 case nir_tex_src_texture_deref:
1415 texture_deref_instr = nir_src_as_deref(instr->src[i].src);
1416 break;
1417 default:
1418 break;
1419 }
1420 }
1421 return texture_deref_instr;
1422 }
1423
1424 static LLVMValueRef build_tex_intrinsic(struct ac_nir_context *ctx,
1425 const nir_tex_instr *instr,
1426 struct ac_image_args *args)
1427 {
1428 if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF) {
1429 unsigned mask = nir_ssa_def_components_read(&instr->dest.ssa);
1430
1431 return ac_build_buffer_load_format(&ctx->ac,
1432 args->resource,
1433 args->coords[0],
1434 ctx->ac.i32_0,
1435 util_last_bit(mask),
1436 0, true);
1437 }
1438
1439 args->opcode = ac_image_sample;
1440
1441 switch (instr->op) {
1442 case nir_texop_txf:
1443 case nir_texop_txf_ms:
1444 case nir_texop_samples_identical:
1445 args->opcode = args->level_zero ||
1446 instr->sampler_dim == GLSL_SAMPLER_DIM_MS ?
1447 ac_image_load : ac_image_load_mip;
1448 args->level_zero = false;
1449 break;
1450 case nir_texop_txs:
1451 case nir_texop_query_levels:
1452 args->opcode = ac_image_get_resinfo;
1453 if (!args->lod)
1454 args->lod = ctx->ac.i32_0;
1455 args->level_zero = false;
1456 break;
1457 case nir_texop_tex:
1458 if (ctx->stage != MESA_SHADER_FRAGMENT) {
1459 assert(!args->lod);
1460 args->level_zero = true;
1461 }
1462 break;
1463 case nir_texop_tg4:
1464 args->opcode = ac_image_gather4;
1465 args->level_zero = true;
1466 break;
1467 case nir_texop_lod:
1468 args->opcode = ac_image_get_lod;
1469 break;
1470 case nir_texop_fragment_fetch:
1471 case nir_texop_fragment_mask_fetch:
1472 args->opcode = ac_image_load;
1473 args->level_zero = false;
1474 break;
1475 default:
1476 break;
1477 }
1478
1479 if (instr->op == nir_texop_tg4 && ctx->ac.chip_class <= GFX8) {
1480 nir_deref_instr *texture_deref_instr = get_tex_texture_deref(instr);
1481 nir_variable *var = nir_deref_instr_get_variable(texture_deref_instr);
1482 const struct glsl_type *type = glsl_without_array(var->type);
1483 enum glsl_base_type stype = glsl_get_sampler_result_type(type);
1484 if (stype == GLSL_TYPE_UINT || stype == GLSL_TYPE_INT) {
1485 return lower_gather4_integer(&ctx->ac, var, args, instr);
1486 }
1487 }
1488
1489 /* Fixup for GFX9 which allocates 1D textures as 2D. */
1490 if (instr->op == nir_texop_lod && ctx->ac.chip_class == GFX9) {
1491 if ((args->dim == ac_image_2darray ||
1492 args->dim == ac_image_2d) && !args->coords[1]) {
1493 args->coords[1] = ctx->ac.i32_0;
1494 }
1495 }
1496
1497 args->attributes = AC_FUNC_ATTR_READNONE;
1498 bool cs_derivs = ctx->stage == MESA_SHADER_COMPUTE &&
1499 ctx->info->cs.derivative_group != DERIVATIVE_GROUP_NONE;
1500 if (ctx->stage == MESA_SHADER_FRAGMENT || cs_derivs) {
1501 /* Prevent texture instructions with implicit derivatives from being
1502 * sinked into branches. */
1503 switch (instr->op) {
1504 case nir_texop_tex:
1505 case nir_texop_txb:
1506 case nir_texop_lod:
1507 args->attributes |= AC_FUNC_ATTR_CONVERGENT;
1508 break;
1509 default:
1510 break;
1511 }
1512 }
1513
1514 return ac_build_image_opcode(&ctx->ac, args);
1515 }
1516
1517 static LLVMValueRef visit_vulkan_resource_reindex(struct ac_nir_context *ctx,
1518 nir_intrinsic_instr *instr)
1519 {
1520 LLVMValueRef ptr = get_src(ctx, instr->src[0]);
1521 LLVMValueRef index = get_src(ctx, instr->src[1]);
1522
1523 LLVMValueRef result = LLVMBuildGEP(ctx->ac.builder, ptr, &index, 1, "");
1524 LLVMSetMetadata(result, ctx->ac.uniform_md_kind, ctx->ac.empty_md);
1525 return result;
1526 }
1527
1528 static LLVMValueRef visit_load_push_constant(struct ac_nir_context *ctx,
1529 nir_intrinsic_instr *instr)
1530 {
1531 LLVMValueRef ptr, addr;
1532 LLVMValueRef src0 = get_src(ctx, instr->src[0]);
1533 unsigned index = nir_intrinsic_base(instr);
1534
1535 addr = LLVMConstInt(ctx->ac.i32, index, 0);
1536 addr = LLVMBuildAdd(ctx->ac.builder, addr, src0, "");
1537
1538 /* Load constant values from user SGPRS when possible, otherwise
1539 * fallback to the default path that loads directly from memory.
1540 */
1541 if (LLVMIsConstant(src0) &&
1542 instr->dest.ssa.bit_size == 32) {
1543 unsigned count = instr->dest.ssa.num_components;
1544 unsigned offset = index;
1545
1546 offset += LLVMConstIntGetZExtValue(src0);
1547 offset /= 4;
1548
1549 offset -= ctx->args->base_inline_push_consts;
1550
1551 unsigned num_inline_push_consts = ctx->args->num_inline_push_consts;
1552 if (offset + count <= num_inline_push_consts) {
1553 LLVMValueRef push_constants[num_inline_push_consts];
1554 for (unsigned i = 0; i < num_inline_push_consts; i++)
1555 push_constants[i] = ac_get_arg(&ctx->ac,
1556 ctx->args->inline_push_consts[i]);
1557 return ac_build_gather_values(&ctx->ac,
1558 push_constants + offset,
1559 count);
1560 }
1561 }
1562
1563 ptr = LLVMBuildGEP(ctx->ac.builder,
1564 ac_get_arg(&ctx->ac, ctx->args->push_constants), &addr, 1, "");
1565
1566 if (instr->dest.ssa.bit_size == 8) {
1567 unsigned load_dwords = instr->dest.ssa.num_components > 1 ? 2 : 1;
1568 LLVMTypeRef vec_type = LLVMVectorType(LLVMInt8TypeInContext(ctx->ac.context), 4 * load_dwords);
1569 ptr = ac_cast_ptr(&ctx->ac, ptr, vec_type);
1570 LLVMValueRef res = LLVMBuildLoad(ctx->ac.builder, ptr, "");
1571
1572 LLVMValueRef params[3];
1573 if (load_dwords > 1) {
1574 LLVMValueRef res_vec = LLVMBuildBitCast(ctx->ac.builder, res, LLVMVectorType(ctx->ac.i32, 2), "");
1575 params[0] = LLVMBuildExtractElement(ctx->ac.builder, res_vec, LLVMConstInt(ctx->ac.i32, 1, false), "");
1576 params[1] = LLVMBuildExtractElement(ctx->ac.builder, res_vec, LLVMConstInt(ctx->ac.i32, 0, false), "");
1577 } else {
1578 res = LLVMBuildBitCast(ctx->ac.builder, res, ctx->ac.i32, "");
1579 params[0] = ctx->ac.i32_0;
1580 params[1] = res;
1581 }
1582 params[2] = addr;
1583 res = ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.alignbyte", ctx->ac.i32, params, 3, 0);
1584
1585 res = LLVMBuildTrunc(ctx->ac.builder, res, LLVMIntTypeInContext(ctx->ac.context, instr->dest.ssa.num_components * 8), "");
1586 if (instr->dest.ssa.num_components > 1)
1587 res = LLVMBuildBitCast(ctx->ac.builder, res, LLVMVectorType(LLVMInt8TypeInContext(ctx->ac.context), instr->dest.ssa.num_components), "");
1588 return res;
1589 } else if (instr->dest.ssa.bit_size == 16) {
1590 unsigned load_dwords = instr->dest.ssa.num_components / 2 + 1;
1591 LLVMTypeRef vec_type = LLVMVectorType(LLVMInt16TypeInContext(ctx->ac.context), 2 * load_dwords);
1592 ptr = ac_cast_ptr(&ctx->ac, ptr, vec_type);
1593 LLVMValueRef res = LLVMBuildLoad(ctx->ac.builder, ptr, "");
1594 res = LLVMBuildBitCast(ctx->ac.builder, res, vec_type, "");
1595 LLVMValueRef cond = LLVMBuildLShr(ctx->ac.builder, addr, ctx->ac.i32_1, "");
1596 cond = LLVMBuildTrunc(ctx->ac.builder, cond, ctx->ac.i1, "");
1597 LLVMValueRef mask[] = { LLVMConstInt(ctx->ac.i32, 0, false), LLVMConstInt(ctx->ac.i32, 1, false),
1598 LLVMConstInt(ctx->ac.i32, 2, false), LLVMConstInt(ctx->ac.i32, 3, false),
1599 LLVMConstInt(ctx->ac.i32, 4, false)};
1600 LLVMValueRef swizzle_aligned = LLVMConstVector(&mask[0], instr->dest.ssa.num_components);
1601 LLVMValueRef swizzle_unaligned = LLVMConstVector(&mask[1], instr->dest.ssa.num_components);
1602 LLVMValueRef shuffle_aligned = LLVMBuildShuffleVector(ctx->ac.builder, res, res, swizzle_aligned, "");
1603 LLVMValueRef shuffle_unaligned = LLVMBuildShuffleVector(ctx->ac.builder, res, res, swizzle_unaligned, "");
1604 res = LLVMBuildSelect(ctx->ac.builder, cond, shuffle_unaligned, shuffle_aligned, "");
1605 return LLVMBuildBitCast(ctx->ac.builder, res, get_def_type(ctx, &instr->dest.ssa), "");
1606 }
1607
1608 ptr = ac_cast_ptr(&ctx->ac, ptr, get_def_type(ctx, &instr->dest.ssa));
1609
1610 return LLVMBuildLoad(ctx->ac.builder, ptr, "");
1611 }
1612
1613 static LLVMValueRef visit_get_buffer_size(struct ac_nir_context *ctx,
1614 const nir_intrinsic_instr *instr)
1615 {
1616 LLVMValueRef index = get_src(ctx, instr->src[0]);
1617
1618 return get_buffer_size(ctx, ctx->abi->load_ssbo(ctx->abi, index, false), false);
1619 }
1620
1621 static uint32_t widen_mask(uint32_t mask, unsigned multiplier)
1622 {
1623 uint32_t new_mask = 0;
1624 for(unsigned i = 0; i < 32 && (1u << i) <= mask; ++i)
1625 if (mask & (1u << i))
1626 new_mask |= ((1u << multiplier) - 1u) << (i * multiplier);
1627 return new_mask;
1628 }
1629
1630 static LLVMValueRef extract_vector_range(struct ac_llvm_context *ctx, LLVMValueRef src,
1631 unsigned start, unsigned count)
1632 {
1633 LLVMValueRef mask[] = {
1634 ctx->i32_0, ctx->i32_1,
1635 LLVMConstInt(ctx->i32, 2, false), LLVMConstInt(ctx->i32, 3, false) };
1636
1637 unsigned src_elements = ac_get_llvm_num_components(src);
1638
1639 if (count == src_elements) {
1640 assert(start == 0);
1641 return src;
1642 } else if (count == 1) {
1643 assert(start < src_elements);
1644 return LLVMBuildExtractElement(ctx->builder, src, mask[start], "");
1645 } else {
1646 assert(start + count <= src_elements);
1647 assert(count <= 4);
1648 LLVMValueRef swizzle = LLVMConstVector(&mask[start], count);
1649 return LLVMBuildShuffleVector(ctx->builder, src, src, swizzle, "");
1650 }
1651 }
1652
1653 static unsigned get_cache_policy(struct ac_nir_context *ctx,
1654 enum gl_access_qualifier access,
1655 bool may_store_unaligned,
1656 bool writeonly_memory)
1657 {
1658 unsigned cache_policy = 0;
1659
1660 /* GFX6 has a TC L1 bug causing corruption of 8bit/16bit stores. All
1661 * store opcodes not aligned to a dword are affected. The only way to
1662 * get unaligned stores is through shader images.
1663 */
1664 if (((may_store_unaligned && ctx->ac.chip_class == GFX6) ||
1665 /* If this is write-only, don't keep data in L1 to prevent
1666 * evicting L1 cache lines that may be needed by other
1667 * instructions.
1668 */
1669 writeonly_memory ||
1670 access & (ACCESS_COHERENT | ACCESS_VOLATILE))) {
1671 cache_policy |= ac_glc;
1672 }
1673
1674 if (access & ACCESS_STREAM_CACHE_POLICY)
1675 cache_policy |= ac_slc;
1676
1677 return cache_policy;
1678 }
1679
1680 static LLVMValueRef enter_waterfall_ssbo(struct ac_nir_context *ctx,
1681 struct waterfall_context *wctx,
1682 const nir_intrinsic_instr *instr,
1683 nir_src src)
1684 {
1685 return enter_waterfall(ctx, wctx, get_src(ctx, src),
1686 nir_intrinsic_access(instr) & ACCESS_NON_UNIFORM);
1687 }
1688
1689 static void visit_store_ssbo(struct ac_nir_context *ctx,
1690 nir_intrinsic_instr *instr)
1691 {
1692 if (ctx->ac.postponed_kill) {
1693 LLVMValueRef cond = LLVMBuildLoad(ctx->ac.builder,
1694 ctx->ac.postponed_kill, "");
1695 ac_build_ifcc(&ctx->ac, cond, 7000);
1696 }
1697
1698 LLVMValueRef src_data = get_src(ctx, instr->src[0]);
1699 int elem_size_bytes = ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src_data)) / 8;
1700 unsigned writemask = nir_intrinsic_write_mask(instr);
1701 enum gl_access_qualifier access = nir_intrinsic_access(instr);
1702 bool writeonly_memory = access & ACCESS_NON_READABLE;
1703 unsigned cache_policy = get_cache_policy(ctx, access, false, writeonly_memory);
1704
1705 struct waterfall_context wctx;
1706 LLVMValueRef rsrc_base = enter_waterfall_ssbo(ctx, &wctx, instr, instr->src[1]);
1707
1708 LLVMValueRef rsrc = ctx->abi->load_ssbo(ctx->abi, rsrc_base, true);
1709 LLVMValueRef base_data = src_data;
1710 base_data = ac_trim_vector(&ctx->ac, base_data, instr->num_components);
1711 LLVMValueRef base_offset = get_src(ctx, instr->src[2]);
1712
1713 while (writemask) {
1714 int start, count;
1715 LLVMValueRef data, offset;
1716 LLVMTypeRef data_type;
1717
1718 u_bit_scan_consecutive_range(&writemask, &start, &count);
1719
1720 /* Due to an LLVM limitation with LLVM < 9, split 3-element
1721 * writes into a 2-element and a 1-element write. */
1722 if (count == 3 &&
1723 (elem_size_bytes != 4 || !ac_has_vec3_support(ctx->ac.chip_class, false))) {
1724 writemask |= 1 << (start + 2);
1725 count = 2;
1726 }
1727 int num_bytes = count * elem_size_bytes; /* count in bytes */
1728
1729 /* we can only store 4 DWords at the same time.
1730 * can only happen for 64 Bit vectors. */
1731 if (num_bytes > 16) {
1732 writemask |= ((1u << (count - 2)) - 1u) << (start + 2);
1733 count = 2;
1734 num_bytes = 16;
1735 }
1736
1737 /* check alignment of 16 Bit stores */
1738 if (elem_size_bytes == 2 && num_bytes > 2 && (start % 2) == 1) {
1739 writemask |= ((1u << (count - 1)) - 1u) << (start + 1);
1740 count = 1;
1741 num_bytes = 2;
1742 }
1743 data = extract_vector_range(&ctx->ac, base_data, start, count);
1744
1745 offset = LLVMBuildAdd(ctx->ac.builder, base_offset,
1746 LLVMConstInt(ctx->ac.i32, start * elem_size_bytes, false), "");
1747
1748 if (num_bytes == 1) {
1749 ac_build_tbuffer_store_byte(&ctx->ac, rsrc, data,
1750 offset, ctx->ac.i32_0,
1751 cache_policy);
1752 } else if (num_bytes == 2) {
1753 ac_build_tbuffer_store_short(&ctx->ac, rsrc, data,
1754 offset, ctx->ac.i32_0,
1755 cache_policy);
1756 } else {
1757 int num_channels = num_bytes / 4;
1758
1759 switch (num_bytes) {
1760 case 16: /* v4f32 */
1761 data_type = ctx->ac.v4f32;
1762 break;
1763 case 12: /* v3f32 */
1764 data_type = ctx->ac.v3f32;
1765 break;
1766 case 8: /* v2f32 */
1767 data_type = ctx->ac.v2f32;
1768 break;
1769 case 4: /* f32 */
1770 data_type = ctx->ac.f32;
1771 break;
1772 default:
1773 unreachable("Malformed vector store.");
1774 }
1775 data = LLVMBuildBitCast(ctx->ac.builder, data, data_type, "");
1776
1777 ac_build_buffer_store_dword(&ctx->ac, rsrc, data,
1778 num_channels, offset,
1779 ctx->ac.i32_0, 0,
1780 cache_policy);
1781 }
1782 }
1783
1784 exit_waterfall(ctx, &wctx, NULL);
1785
1786 if (ctx->ac.postponed_kill)
1787 ac_build_endif(&ctx->ac, 7000);
1788 }
1789
1790 static LLVMValueRef emit_ssbo_comp_swap_64(struct ac_nir_context *ctx,
1791 LLVMValueRef descriptor,
1792 LLVMValueRef offset,
1793 LLVMValueRef compare,
1794 LLVMValueRef exchange)
1795 {
1796 LLVMBasicBlockRef start_block = NULL, then_block = NULL;
1797 if (ctx->abi->robust_buffer_access) {
1798 LLVMValueRef size = ac_llvm_extract_elem(&ctx->ac, descriptor, 2);
1799
1800 LLVMValueRef cond = LLVMBuildICmp(ctx->ac.builder, LLVMIntULT, offset, size, "");
1801 start_block = LLVMGetInsertBlock(ctx->ac.builder);
1802
1803 ac_build_ifcc(&ctx->ac, cond, -1);
1804
1805 then_block = LLVMGetInsertBlock(ctx->ac.builder);
1806 }
1807
1808 LLVMValueRef ptr_parts[2] = {
1809 ac_llvm_extract_elem(&ctx->ac, descriptor, 0),
1810 LLVMBuildAnd(ctx->ac.builder,
1811 ac_llvm_extract_elem(&ctx->ac, descriptor, 1),
1812 LLVMConstInt(ctx->ac.i32, 65535, 0), "")
1813 };
1814
1815 ptr_parts[1] = LLVMBuildTrunc(ctx->ac.builder, ptr_parts[1], ctx->ac.i16, "");
1816 ptr_parts[1] = LLVMBuildSExt(ctx->ac.builder, ptr_parts[1], ctx->ac.i32, "");
1817
1818 offset = LLVMBuildZExt(ctx->ac.builder, offset, ctx->ac.i64, "");
1819
1820 LLVMValueRef ptr = ac_build_gather_values(&ctx->ac, ptr_parts, 2);
1821 ptr = LLVMBuildBitCast(ctx->ac.builder, ptr, ctx->ac.i64, "");
1822 ptr = LLVMBuildAdd(ctx->ac.builder, ptr, offset, "");
1823 ptr = LLVMBuildIntToPtr(ctx->ac.builder, ptr, LLVMPointerType(ctx->ac.i64, AC_ADDR_SPACE_GLOBAL), "");
1824
1825 LLVMValueRef result = ac_build_atomic_cmp_xchg(&ctx->ac, ptr, compare, exchange, "singlethread-one-as");
1826 result = LLVMBuildExtractValue(ctx->ac.builder, result, 0, "");
1827
1828 if (ctx->abi->robust_buffer_access) {
1829 ac_build_endif(&ctx->ac, -1);
1830
1831 LLVMBasicBlockRef incoming_blocks[2] = {
1832 start_block,
1833 then_block,
1834 };
1835
1836 LLVMValueRef incoming_values[2] = {
1837 LLVMConstInt(ctx->ac.i64, 0, 0),
1838 result,
1839 };
1840 LLVMValueRef ret = LLVMBuildPhi(ctx->ac.builder, ctx->ac.i64, "");
1841 LLVMAddIncoming(ret, incoming_values, incoming_blocks, 2);
1842 return ret;
1843 } else {
1844 return result;
1845 }
1846 }
1847
1848 static LLVMValueRef visit_atomic_ssbo(struct ac_nir_context *ctx,
1849 nir_intrinsic_instr *instr)
1850 {
1851 if (ctx->ac.postponed_kill) {
1852 LLVMValueRef cond = LLVMBuildLoad(ctx->ac.builder,
1853 ctx->ac.postponed_kill, "");
1854 ac_build_ifcc(&ctx->ac, cond, 7001);
1855 }
1856
1857 LLVMTypeRef return_type = LLVMTypeOf(get_src(ctx, instr->src[2]));
1858 const char *op;
1859 char name[64], type[8];
1860 LLVMValueRef params[6], descriptor;
1861 LLVMValueRef result;
1862 int arg_count = 0;
1863
1864 struct waterfall_context wctx;
1865 LLVMValueRef rsrc_base = enter_waterfall_ssbo(ctx, &wctx, instr, instr->src[0]);
1866
1867 switch (instr->intrinsic) {
1868 case nir_intrinsic_ssbo_atomic_add:
1869 op = "add";
1870 break;
1871 case nir_intrinsic_ssbo_atomic_imin:
1872 op = "smin";
1873 break;
1874 case nir_intrinsic_ssbo_atomic_umin:
1875 op = "umin";
1876 break;
1877 case nir_intrinsic_ssbo_atomic_imax:
1878 op = "smax";
1879 break;
1880 case nir_intrinsic_ssbo_atomic_umax:
1881 op = "umax";
1882 break;
1883 case nir_intrinsic_ssbo_atomic_and:
1884 op = "and";
1885 break;
1886 case nir_intrinsic_ssbo_atomic_or:
1887 op = "or";
1888 break;
1889 case nir_intrinsic_ssbo_atomic_xor:
1890 op = "xor";
1891 break;
1892 case nir_intrinsic_ssbo_atomic_exchange:
1893 op = "swap";
1894 break;
1895 case nir_intrinsic_ssbo_atomic_comp_swap:
1896 op = "cmpswap";
1897 break;
1898 default:
1899 abort();
1900 }
1901
1902 descriptor = ctx->abi->load_ssbo(ctx->abi,
1903 rsrc_base,
1904 true);
1905
1906 if (instr->intrinsic == nir_intrinsic_ssbo_atomic_comp_swap &&
1907 return_type == ctx->ac.i64) {
1908 result = emit_ssbo_comp_swap_64(ctx, descriptor,
1909 get_src(ctx, instr->src[1]),
1910 get_src(ctx, instr->src[2]),
1911 get_src(ctx, instr->src[3]));
1912 } else {
1913 if (instr->intrinsic == nir_intrinsic_ssbo_atomic_comp_swap) {
1914 params[arg_count++] = ac_llvm_extract_elem(&ctx->ac, get_src(ctx, instr->src[3]), 0);
1915 }
1916 params[arg_count++] = ac_llvm_extract_elem(&ctx->ac, get_src(ctx, instr->src[2]), 0);
1917 params[arg_count++] = descriptor;
1918
1919 if (LLVM_VERSION_MAJOR >= 9) {
1920 /* XXX: The new raw/struct atomic intrinsics are buggy with
1921 * LLVM 8, see r358579.
1922 */
1923 params[arg_count++] = get_src(ctx, instr->src[1]); /* voffset */
1924 params[arg_count++] = ctx->ac.i32_0; /* soffset */
1925 params[arg_count++] = ctx->ac.i32_0; /* slc */
1926
1927 ac_build_type_name_for_intr(return_type, type, sizeof(type));
1928 snprintf(name, sizeof(name),
1929 "llvm.amdgcn.raw.buffer.atomic.%s.%s", op, type);
1930 } else {
1931 params[arg_count++] = ctx->ac.i32_0; /* vindex */
1932 params[arg_count++] = get_src(ctx, instr->src[1]); /* voffset */
1933 params[arg_count++] = ctx->ac.i1false; /* slc */
1934
1935 assert(return_type == ctx->ac.i32);
1936 snprintf(name, sizeof(name),
1937 "llvm.amdgcn.buffer.atomic.%s", op);
1938 }
1939
1940 result = ac_build_intrinsic(&ctx->ac, name, return_type, params,
1941 arg_count, 0);
1942 }
1943
1944 result = exit_waterfall(ctx, &wctx, result);
1945 if (ctx->ac.postponed_kill)
1946 ac_build_endif(&ctx->ac, 7001);
1947 return result;
1948 }
1949
1950 static LLVMValueRef visit_load_buffer(struct ac_nir_context *ctx,
1951 nir_intrinsic_instr *instr)
1952 {
1953 struct waterfall_context wctx;
1954 LLVMValueRef rsrc_base = enter_waterfall_ssbo(ctx, &wctx, instr, instr->src[0]);
1955
1956 int elem_size_bytes = instr->dest.ssa.bit_size / 8;
1957 int num_components = instr->num_components;
1958 enum gl_access_qualifier access = nir_intrinsic_access(instr);
1959 unsigned cache_policy = get_cache_policy(ctx, access, false, false);
1960
1961 LLVMValueRef offset = get_src(ctx, instr->src[1]);
1962 LLVMValueRef rsrc = ctx->abi->load_ssbo(ctx->abi, rsrc_base, false);
1963 LLVMValueRef vindex = ctx->ac.i32_0;
1964
1965 LLVMTypeRef def_type = get_def_type(ctx, &instr->dest.ssa);
1966 LLVMTypeRef def_elem_type = num_components > 1 ? LLVMGetElementType(def_type) : def_type;
1967
1968 LLVMValueRef results[4];
1969 for (int i = 0; i < num_components;) {
1970 int num_elems = num_components - i;
1971 if (elem_size_bytes < 4 && nir_intrinsic_align(instr) % 4 != 0)
1972 num_elems = 1;
1973 if (num_elems * elem_size_bytes > 16)
1974 num_elems = 16 / elem_size_bytes;
1975 int load_bytes = num_elems * elem_size_bytes;
1976
1977 LLVMValueRef immoffset = LLVMConstInt(ctx->ac.i32, i * elem_size_bytes, false);
1978
1979 LLVMValueRef ret;
1980
1981 if (load_bytes == 1) {
1982 ret = ac_build_tbuffer_load_byte(&ctx->ac,
1983 rsrc,
1984 offset,
1985 ctx->ac.i32_0,
1986 immoffset,
1987 cache_policy);
1988 } else if (load_bytes == 2) {
1989 ret = ac_build_tbuffer_load_short(&ctx->ac,
1990 rsrc,
1991 offset,
1992 ctx->ac.i32_0,
1993 immoffset,
1994 cache_policy);
1995 } else {
1996 int num_channels = util_next_power_of_two(load_bytes) / 4;
1997 bool can_speculate = access & ACCESS_CAN_REORDER;
1998
1999 ret = ac_build_buffer_load(&ctx->ac, rsrc, num_channels,
2000 vindex, offset, immoffset, 0,
2001 cache_policy, can_speculate, false);
2002 }
2003
2004 LLVMTypeRef byte_vec = LLVMVectorType(ctx->ac.i8, ac_get_type_size(LLVMTypeOf(ret)));
2005 ret = LLVMBuildBitCast(ctx->ac.builder, ret, byte_vec, "");
2006 ret = ac_trim_vector(&ctx->ac, ret, load_bytes);
2007
2008 LLVMTypeRef ret_type = LLVMVectorType(def_elem_type, num_elems);
2009 ret = LLVMBuildBitCast(ctx->ac.builder, ret, ret_type, "");
2010
2011 for (unsigned j = 0; j < num_elems; j++) {
2012 results[i + j] = LLVMBuildExtractElement(ctx->ac.builder, ret, LLVMConstInt(ctx->ac.i32, j, false), "");
2013 }
2014 i += num_elems;
2015 }
2016
2017 LLVMValueRef ret = ac_build_gather_values(&ctx->ac, results, num_components);
2018 return exit_waterfall(ctx, &wctx, ret);
2019 }
2020
2021 static LLVMValueRef enter_waterfall_ubo(struct ac_nir_context *ctx,
2022 struct waterfall_context *wctx,
2023 const nir_intrinsic_instr *instr)
2024 {
2025 return enter_waterfall(ctx, wctx, get_src(ctx, instr->src[0]),
2026 nir_intrinsic_access(instr) & ACCESS_NON_UNIFORM);
2027 }
2028
2029 static LLVMValueRef visit_load_ubo_buffer(struct ac_nir_context *ctx,
2030 nir_intrinsic_instr *instr)
2031 {
2032 struct waterfall_context wctx;
2033 LLVMValueRef rsrc_base = enter_waterfall_ubo(ctx, &wctx, instr);
2034
2035 LLVMValueRef ret;
2036 LLVMValueRef rsrc = rsrc_base;
2037 LLVMValueRef offset = get_src(ctx, instr->src[1]);
2038 int num_components = instr->num_components;
2039
2040 if (ctx->abi->load_ubo)
2041 rsrc = ctx->abi->load_ubo(ctx->abi, rsrc);
2042
2043 if (instr->dest.ssa.bit_size == 64)
2044 num_components *= 2;
2045
2046 if (instr->dest.ssa.bit_size == 16 || instr->dest.ssa.bit_size == 8) {
2047 unsigned load_bytes = instr->dest.ssa.bit_size / 8;
2048 LLVMValueRef results[num_components];
2049 for (unsigned i = 0; i < num_components; ++i) {
2050 LLVMValueRef immoffset = LLVMConstInt(ctx->ac.i32,
2051 load_bytes * i, 0);
2052
2053 if (load_bytes == 1) {
2054 results[i] = ac_build_tbuffer_load_byte(&ctx->ac,
2055 rsrc,
2056 offset,
2057 ctx->ac.i32_0,
2058 immoffset,
2059 0);
2060 } else {
2061 assert(load_bytes == 2);
2062 results[i] = ac_build_tbuffer_load_short(&ctx->ac,
2063 rsrc,
2064 offset,
2065 ctx->ac.i32_0,
2066 immoffset,
2067 0);
2068 }
2069 }
2070 ret = ac_build_gather_values(&ctx->ac, results, num_components);
2071 } else {
2072 ret = ac_build_buffer_load(&ctx->ac, rsrc, num_components, NULL, offset,
2073 NULL, 0, 0, true, true);
2074
2075 ret = ac_trim_vector(&ctx->ac, ret, num_components);
2076 }
2077
2078 ret = LLVMBuildBitCast(ctx->ac.builder, ret,
2079 get_def_type(ctx, &instr->dest.ssa), "");
2080
2081 return exit_waterfall(ctx, &wctx, ret);
2082 }
2083
2084 static void
2085 get_deref_offset(struct ac_nir_context *ctx, nir_deref_instr *instr,
2086 bool vs_in, unsigned *vertex_index_out,
2087 LLVMValueRef *vertex_index_ref,
2088 unsigned *const_out, LLVMValueRef *indir_out)
2089 {
2090 nir_variable *var = nir_deref_instr_get_variable(instr);
2091 nir_deref_path path;
2092 unsigned idx_lvl = 1;
2093
2094 nir_deref_path_init(&path, instr, NULL);
2095
2096 if (vertex_index_out != NULL || vertex_index_ref != NULL) {
2097 if (vertex_index_ref) {
2098 *vertex_index_ref = get_src(ctx, path.path[idx_lvl]->arr.index);
2099 if (vertex_index_out)
2100 *vertex_index_out = 0;
2101 } else {
2102 *vertex_index_out = nir_src_as_uint(path.path[idx_lvl]->arr.index);
2103 }
2104 ++idx_lvl;
2105 }
2106
2107 uint32_t const_offset = 0;
2108 LLVMValueRef offset = NULL;
2109
2110 if (var->data.compact) {
2111 assert(instr->deref_type == nir_deref_type_array);
2112 const_offset = nir_src_as_uint(instr->arr.index);
2113 goto out;
2114 }
2115
2116 for (; path.path[idx_lvl]; ++idx_lvl) {
2117 const struct glsl_type *parent_type = path.path[idx_lvl - 1]->type;
2118 if (path.path[idx_lvl]->deref_type == nir_deref_type_struct) {
2119 unsigned index = path.path[idx_lvl]->strct.index;
2120
2121 for (unsigned i = 0; i < index; i++) {
2122 const struct glsl_type *ft = glsl_get_struct_field(parent_type, i);
2123 const_offset += glsl_count_attribute_slots(ft, vs_in);
2124 }
2125 } else if(path.path[idx_lvl]->deref_type == nir_deref_type_array) {
2126 unsigned size = glsl_count_attribute_slots(path.path[idx_lvl]->type, vs_in);
2127 if (nir_src_is_const(path.path[idx_lvl]->arr.index)) {
2128 const_offset += size *
2129 nir_src_as_uint(path.path[idx_lvl]->arr.index);
2130 } else {
2131 LLVMValueRef array_off = LLVMBuildMul(ctx->ac.builder, LLVMConstInt(ctx->ac.i32, size, 0),
2132 get_src(ctx, path.path[idx_lvl]->arr.index), "");
2133 if (offset)
2134 offset = LLVMBuildAdd(ctx->ac.builder, offset, array_off, "");
2135 else
2136 offset = array_off;
2137 }
2138 } else
2139 unreachable("Uhandled deref type in get_deref_instr_offset");
2140 }
2141
2142 out:
2143 nir_deref_path_finish(&path);
2144
2145 if (const_offset && offset)
2146 offset = LLVMBuildAdd(ctx->ac.builder, offset,
2147 LLVMConstInt(ctx->ac.i32, const_offset, 0),
2148 "");
2149
2150 *const_out = const_offset;
2151 *indir_out = offset;
2152 }
2153
2154 static LLVMValueRef load_tess_varyings(struct ac_nir_context *ctx,
2155 nir_intrinsic_instr *instr,
2156 bool load_inputs)
2157 {
2158 LLVMValueRef result;
2159 LLVMValueRef vertex_index = NULL;
2160 LLVMValueRef indir_index = NULL;
2161 unsigned const_index = 0;
2162
2163 nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
2164
2165 unsigned location = var->data.location;
2166 unsigned driver_location = var->data.driver_location;
2167 const bool is_patch = var->data.patch ||
2168 var->data.location == VARYING_SLOT_TESS_LEVEL_INNER ||
2169 var->data.location == VARYING_SLOT_TESS_LEVEL_OUTER;
2170 const bool is_compact = var->data.compact;
2171
2172 get_deref_offset(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr),
2173 false, NULL, is_patch ? NULL : &vertex_index,
2174 &const_index, &indir_index);
2175
2176 LLVMTypeRef dest_type = get_def_type(ctx, &instr->dest.ssa);
2177
2178 LLVMTypeRef src_component_type;
2179 if (LLVMGetTypeKind(dest_type) == LLVMVectorTypeKind)
2180 src_component_type = LLVMGetElementType(dest_type);
2181 else
2182 src_component_type = dest_type;
2183
2184 result = ctx->abi->load_tess_varyings(ctx->abi, src_component_type,
2185 vertex_index, indir_index,
2186 const_index, location, driver_location,
2187 var->data.location_frac,
2188 instr->num_components,
2189 is_patch, is_compact, load_inputs);
2190 if (instr->dest.ssa.bit_size == 16) {
2191 result = ac_to_integer(&ctx->ac, result);
2192 result = LLVMBuildTrunc(ctx->ac.builder, result, dest_type, "");
2193 }
2194 return LLVMBuildBitCast(ctx->ac.builder, result, dest_type, "");
2195 }
2196
2197 static unsigned
2198 type_scalar_size_bytes(const struct glsl_type *type)
2199 {
2200 assert(glsl_type_is_vector_or_scalar(type) ||
2201 glsl_type_is_matrix(type));
2202 return glsl_type_is_boolean(type) ? 4 : glsl_get_bit_size(type) / 8;
2203 }
2204
2205 static LLVMValueRef visit_load_var(struct ac_nir_context *ctx,
2206 nir_intrinsic_instr *instr)
2207 {
2208 nir_deref_instr *deref = nir_instr_as_deref(instr->src[0].ssa->parent_instr);
2209 nir_variable *var = nir_deref_instr_get_variable(deref);
2210
2211 LLVMValueRef values[8];
2212 int idx = 0;
2213 int ve = instr->dest.ssa.num_components;
2214 unsigned comp = 0;
2215 LLVMValueRef indir_index;
2216 LLVMValueRef ret;
2217 unsigned const_index;
2218 unsigned stride = 4;
2219 int mode = deref->mode;
2220
2221 if (var) {
2222 bool vs_in = ctx->stage == MESA_SHADER_VERTEX &&
2223 var->data.mode == nir_var_shader_in;
2224 idx = var->data.driver_location;
2225 comp = var->data.location_frac;
2226 mode = var->data.mode;
2227
2228 get_deref_offset(ctx, deref, vs_in, NULL, NULL,
2229 &const_index, &indir_index);
2230
2231 if (var->data.compact) {
2232 stride = 1;
2233 const_index += comp;
2234 comp = 0;
2235 }
2236 }
2237
2238 if (instr->dest.ssa.bit_size == 64 &&
2239 (deref->mode == nir_var_shader_in ||
2240 deref->mode == nir_var_shader_out ||
2241 deref->mode == nir_var_function_temp))
2242 ve *= 2;
2243
2244 switch (mode) {
2245 case nir_var_shader_in:
2246 if (ctx->stage == MESA_SHADER_TESS_CTRL ||
2247 ctx->stage == MESA_SHADER_TESS_EVAL) {
2248 return load_tess_varyings(ctx, instr, true);
2249 }
2250
2251 if (ctx->stage == MESA_SHADER_GEOMETRY) {
2252 LLVMTypeRef type = LLVMIntTypeInContext(ctx->ac.context, instr->dest.ssa.bit_size);
2253 LLVMValueRef indir_index;
2254 unsigned const_index, vertex_index;
2255 get_deref_offset(ctx, deref, false, &vertex_index, NULL,
2256 &const_index, &indir_index);
2257 assert(indir_index == NULL);
2258
2259 return ctx->abi->load_inputs(ctx->abi, var->data.location,
2260 var->data.driver_location,
2261 var->data.location_frac,
2262 instr->num_components, vertex_index, const_index, type);
2263 }
2264
2265 for (unsigned chan = comp; chan < ve + comp; chan++) {
2266 if (indir_index) {
2267 unsigned count = glsl_count_attribute_slots(
2268 var->type,
2269 ctx->stage == MESA_SHADER_VERTEX);
2270 count -= chan / 4;
2271 LLVMValueRef tmp_vec = ac_build_gather_values_extended(
2272 &ctx->ac, ctx->abi->inputs + idx + chan, count,
2273 stride, false, true);
2274
2275 values[chan] = LLVMBuildExtractElement(ctx->ac.builder,
2276 tmp_vec,
2277 indir_index, "");
2278 } else
2279 values[chan] = ctx->abi->inputs[idx + chan + const_index * stride];
2280 }
2281 break;
2282 case nir_var_function_temp:
2283 for (unsigned chan = 0; chan < ve; chan++) {
2284 if (indir_index) {
2285 unsigned count = glsl_count_attribute_slots(
2286 var->type, false);
2287 count -= chan / 4;
2288 LLVMValueRef tmp_vec = ac_build_gather_values_extended(
2289 &ctx->ac, ctx->locals + idx + chan, count,
2290 stride, true, true);
2291
2292 values[chan] = LLVMBuildExtractElement(ctx->ac.builder,
2293 tmp_vec,
2294 indir_index, "");
2295 } else {
2296 values[chan] = LLVMBuildLoad(ctx->ac.builder, ctx->locals[idx + chan + const_index * stride], "");
2297 }
2298 }
2299 break;
2300 case nir_var_shader_out:
2301 if (ctx->stage == MESA_SHADER_TESS_CTRL) {
2302 return load_tess_varyings(ctx, instr, false);
2303 }
2304
2305 if (ctx->stage == MESA_SHADER_FRAGMENT &&
2306 var->data.fb_fetch_output &&
2307 ctx->abi->emit_fbfetch)
2308 return ctx->abi->emit_fbfetch(ctx->abi);
2309
2310 for (unsigned chan = comp; chan < ve + comp; chan++) {
2311 if (indir_index) {
2312 unsigned count = glsl_count_attribute_slots(
2313 var->type, false);
2314 count -= chan / 4;
2315 LLVMValueRef tmp_vec = ac_build_gather_values_extended(
2316 &ctx->ac, ctx->abi->outputs + idx + chan, count,
2317 stride, true, true);
2318
2319 values[chan] = LLVMBuildExtractElement(ctx->ac.builder,
2320 tmp_vec,
2321 indir_index, "");
2322 } else {
2323 values[chan] = LLVMBuildLoad(ctx->ac.builder,
2324 ctx->abi->outputs[idx + chan + const_index * stride],
2325 "");
2326 }
2327 }
2328 break;
2329 case nir_var_mem_global: {
2330 LLVMValueRef address = get_src(ctx, instr->src[0]);
2331 unsigned explicit_stride = glsl_get_explicit_stride(deref->type);
2332 unsigned natural_stride = type_scalar_size_bytes(deref->type);
2333 unsigned stride = explicit_stride ? explicit_stride : natural_stride;
2334
2335 LLVMTypeRef result_type = get_def_type(ctx, &instr->dest.ssa);
2336 if (stride != natural_stride) {
2337 LLVMTypeRef ptr_type = LLVMPointerType(LLVMGetElementType(result_type),
2338 LLVMGetPointerAddressSpace(LLVMTypeOf(address)));
2339 address = LLVMBuildBitCast(ctx->ac.builder, address, ptr_type , "");
2340
2341 for (unsigned i = 0; i < instr->dest.ssa.num_components; ++i) {
2342 LLVMValueRef offset = LLVMConstInt(ctx->ac.i32, i * stride / natural_stride, 0);
2343 values[i] = LLVMBuildLoad(ctx->ac.builder,
2344 ac_build_gep_ptr(&ctx->ac, address, offset), "");
2345 }
2346 return ac_build_gather_values(&ctx->ac, values, instr->dest.ssa.num_components);
2347 } else {
2348 LLVMTypeRef ptr_type = LLVMPointerType(result_type,
2349 LLVMGetPointerAddressSpace(LLVMTypeOf(address)));
2350 address = LLVMBuildBitCast(ctx->ac.builder, address, ptr_type , "");
2351 LLVMValueRef val = LLVMBuildLoad(ctx->ac.builder, address, "");
2352 return val;
2353 }
2354 }
2355 default:
2356 unreachable("unhandle variable mode");
2357 }
2358 ret = ac_build_varying_gather_values(&ctx->ac, values, ve, comp);
2359 return LLVMBuildBitCast(ctx->ac.builder, ret, get_def_type(ctx, &instr->dest.ssa), "");
2360 }
2361
2362 static void
2363 visit_store_var(struct ac_nir_context *ctx,
2364 nir_intrinsic_instr *instr)
2365 {
2366 if (ctx->ac.postponed_kill) {
2367 LLVMValueRef cond = LLVMBuildLoad(ctx->ac.builder,
2368 ctx->ac.postponed_kill, "");
2369 ac_build_ifcc(&ctx->ac, cond, 7002);
2370 }
2371
2372 nir_deref_instr *deref = nir_instr_as_deref(instr->src[0].ssa->parent_instr);
2373 nir_variable *var = nir_deref_instr_get_variable(deref);
2374
2375 LLVMValueRef temp_ptr, value;
2376 int idx = 0;
2377 unsigned comp = 0;
2378 LLVMValueRef src = ac_to_float(&ctx->ac, get_src(ctx, instr->src[1]));
2379 int writemask = instr->const_index[0];
2380 LLVMValueRef indir_index;
2381 unsigned const_index;
2382
2383 if (var) {
2384 get_deref_offset(ctx, deref, false,
2385 NULL, NULL, &const_index, &indir_index);
2386 idx = var->data.driver_location;
2387 comp = var->data.location_frac;
2388
2389 if (var->data.compact) {
2390 const_index += comp;
2391 comp = 0;
2392 }
2393 }
2394
2395 if (ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src)) == 64 &&
2396 (deref->mode == nir_var_shader_out ||
2397 deref->mode == nir_var_function_temp)) {
2398
2399 src = LLVMBuildBitCast(ctx->ac.builder, src,
2400 LLVMVectorType(ctx->ac.f32, ac_get_llvm_num_components(src) * 2),
2401 "");
2402
2403 writemask = widen_mask(writemask, 2);
2404 }
2405
2406 writemask = writemask << comp;
2407
2408 switch (deref->mode) {
2409 case nir_var_shader_out:
2410
2411 if (ctx->stage == MESA_SHADER_TESS_CTRL) {
2412 LLVMValueRef vertex_index = NULL;
2413 LLVMValueRef indir_index = NULL;
2414 unsigned const_index = 0;
2415 const bool is_patch = var->data.patch ||
2416 var->data.location == VARYING_SLOT_TESS_LEVEL_INNER ||
2417 var->data.location == VARYING_SLOT_TESS_LEVEL_OUTER;
2418
2419 get_deref_offset(ctx, deref, false, NULL,
2420 is_patch ? NULL : &vertex_index,
2421 &const_index, &indir_index);
2422
2423 ctx->abi->store_tcs_outputs(ctx->abi, var,
2424 vertex_index, indir_index,
2425 const_index, src, writemask);
2426 break;
2427 }
2428
2429 for (unsigned chan = 0; chan < 8; chan++) {
2430 int stride = 4;
2431 if (!(writemask & (1 << chan)))
2432 continue;
2433
2434 value = ac_llvm_extract_elem(&ctx->ac, src, chan - comp);
2435
2436 if (var->data.compact)
2437 stride = 1;
2438 if (indir_index) {
2439 unsigned count = glsl_count_attribute_slots(
2440 var->type, false);
2441 count -= chan / 4;
2442 LLVMValueRef tmp_vec = ac_build_gather_values_extended(
2443 &ctx->ac, ctx->abi->outputs + idx + chan, count,
2444 stride, true, true);
2445
2446 tmp_vec = LLVMBuildInsertElement(ctx->ac.builder, tmp_vec,
2447 value, indir_index, "");
2448 build_store_values_extended(&ctx->ac, ctx->abi->outputs + idx + chan,
2449 count, stride, tmp_vec);
2450
2451 } else {
2452 temp_ptr = ctx->abi->outputs[idx + chan + const_index * stride];
2453
2454 LLVMBuildStore(ctx->ac.builder, value, temp_ptr);
2455 }
2456 }
2457 break;
2458 case nir_var_function_temp:
2459 for (unsigned chan = 0; chan < 8; chan++) {
2460 if (!(writemask & (1 << chan)))
2461 continue;
2462
2463 value = ac_llvm_extract_elem(&ctx->ac, src, chan);
2464 if (indir_index) {
2465 unsigned count = glsl_count_attribute_slots(
2466 var->type, false);
2467 count -= chan / 4;
2468 LLVMValueRef tmp_vec = ac_build_gather_values_extended(
2469 &ctx->ac, ctx->locals + idx + chan, count,
2470 4, true, true);
2471
2472 tmp_vec = LLVMBuildInsertElement(ctx->ac.builder, tmp_vec,
2473 value, indir_index, "");
2474 build_store_values_extended(&ctx->ac, ctx->locals + idx + chan,
2475 count, 4, tmp_vec);
2476 } else {
2477 temp_ptr = ctx->locals[idx + chan + const_index * 4];
2478
2479 LLVMBuildStore(ctx->ac.builder, value, temp_ptr);
2480 }
2481 }
2482 break;
2483
2484 case nir_var_mem_global: {
2485 int writemask = instr->const_index[0];
2486 LLVMValueRef address = get_src(ctx, instr->src[0]);
2487 LLVMValueRef val = get_src(ctx, instr->src[1]);
2488
2489 unsigned explicit_stride = glsl_get_explicit_stride(deref->type);
2490 unsigned natural_stride = type_scalar_size_bytes(deref->type);
2491 unsigned stride = explicit_stride ? explicit_stride : natural_stride;
2492
2493 LLVMTypeRef ptr_type = LLVMPointerType(LLVMTypeOf(val),
2494 LLVMGetPointerAddressSpace(LLVMTypeOf(address)));
2495 address = LLVMBuildBitCast(ctx->ac.builder, address, ptr_type , "");
2496
2497 if (writemask == (1u << ac_get_llvm_num_components(val)) - 1 &&
2498 stride == natural_stride) {
2499 LLVMTypeRef ptr_type = LLVMPointerType(LLVMTypeOf(val),
2500 LLVMGetPointerAddressSpace(LLVMTypeOf(address)));
2501 address = LLVMBuildBitCast(ctx->ac.builder, address, ptr_type , "");
2502
2503 val = LLVMBuildBitCast(ctx->ac.builder, val,
2504 LLVMGetElementType(LLVMTypeOf(address)), "");
2505 LLVMBuildStore(ctx->ac.builder, val, address);
2506 } else {
2507 LLVMTypeRef ptr_type = LLVMPointerType(LLVMGetElementType(LLVMTypeOf(val)),
2508 LLVMGetPointerAddressSpace(LLVMTypeOf(address)));
2509 address = LLVMBuildBitCast(ctx->ac.builder, address, ptr_type , "");
2510 for (unsigned chan = 0; chan < 4; chan++) {
2511 if (!(writemask & (1 << chan)))
2512 continue;
2513
2514 LLVMValueRef offset = LLVMConstInt(ctx->ac.i32, chan * stride / natural_stride, 0);
2515
2516 LLVMValueRef ptr = ac_build_gep_ptr(&ctx->ac, address, offset);
2517 LLVMValueRef src = ac_llvm_extract_elem(&ctx->ac, val,
2518 chan);
2519 src = LLVMBuildBitCast(ctx->ac.builder, src,
2520 LLVMGetElementType(LLVMTypeOf(ptr)), "");
2521 LLVMBuildStore(ctx->ac.builder, src, ptr);
2522 }
2523 }
2524 break;
2525 }
2526 default:
2527 abort();
2528 break;
2529 }
2530
2531 if (ctx->ac.postponed_kill)
2532 ac_build_endif(&ctx->ac, 7002);
2533 }
2534
2535 static int image_type_to_components_count(enum glsl_sampler_dim dim, bool array)
2536 {
2537 switch (dim) {
2538 case GLSL_SAMPLER_DIM_BUF:
2539 return 1;
2540 case GLSL_SAMPLER_DIM_1D:
2541 return array ? 2 : 1;
2542 case GLSL_SAMPLER_DIM_2D:
2543 return array ? 3 : 2;
2544 case GLSL_SAMPLER_DIM_MS:
2545 return array ? 4 : 3;
2546 case GLSL_SAMPLER_DIM_3D:
2547 case GLSL_SAMPLER_DIM_CUBE:
2548 return 3;
2549 case GLSL_SAMPLER_DIM_RECT:
2550 case GLSL_SAMPLER_DIM_SUBPASS:
2551 return 2;
2552 case GLSL_SAMPLER_DIM_SUBPASS_MS:
2553 return 3;
2554 default:
2555 break;
2556 }
2557 return 0;
2558 }
2559
2560 static LLVMValueRef adjust_sample_index_using_fmask(struct ac_llvm_context *ctx,
2561 LLVMValueRef coord_x, LLVMValueRef coord_y,
2562 LLVMValueRef coord_z,
2563 LLVMValueRef sample_index,
2564 LLVMValueRef fmask_desc_ptr)
2565 {
2566 unsigned sample_chan = coord_z ? 3 : 2;
2567 LLVMValueRef addr[4] = {coord_x, coord_y, coord_z};
2568 addr[sample_chan] = sample_index;
2569
2570 ac_apply_fmask_to_sample(ctx, fmask_desc_ptr, addr, coord_z != NULL);
2571 return addr[sample_chan];
2572 }
2573
2574 static nir_deref_instr *get_image_deref(const nir_intrinsic_instr *instr)
2575 {
2576 assert(instr->src[0].is_ssa);
2577 return nir_instr_as_deref(instr->src[0].ssa->parent_instr);
2578 }
2579
2580 static LLVMValueRef get_image_descriptor(struct ac_nir_context *ctx,
2581 const nir_intrinsic_instr *instr,
2582 LLVMValueRef dynamic_index,
2583 enum ac_descriptor_type desc_type,
2584 bool write)
2585 {
2586 nir_deref_instr *deref_instr =
2587 instr->src[0].ssa->parent_instr->type == nir_instr_type_deref ?
2588 nir_instr_as_deref(instr->src[0].ssa->parent_instr) : NULL;
2589
2590 return get_sampler_desc(ctx, deref_instr, desc_type, &instr->instr, dynamic_index, true, write);
2591 }
2592
2593 static void get_image_coords(struct ac_nir_context *ctx,
2594 const nir_intrinsic_instr *instr,
2595 LLVMValueRef dynamic_desc_index,
2596 struct ac_image_args *args,
2597 enum glsl_sampler_dim dim,
2598 bool is_array)
2599 {
2600 LLVMValueRef src0 = get_src(ctx, instr->src[1]);
2601 LLVMValueRef masks[] = {
2602 LLVMConstInt(ctx->ac.i32, 0, false), LLVMConstInt(ctx->ac.i32, 1, false),
2603 LLVMConstInt(ctx->ac.i32, 2, false), LLVMConstInt(ctx->ac.i32, 3, false),
2604 };
2605 LLVMValueRef sample_index = ac_llvm_extract_elem(&ctx->ac, get_src(ctx, instr->src[2]), 0);
2606
2607 int count;
2608 ASSERTED bool add_frag_pos = (dim == GLSL_SAMPLER_DIM_SUBPASS ||
2609 dim == GLSL_SAMPLER_DIM_SUBPASS_MS);
2610 bool is_ms = (dim == GLSL_SAMPLER_DIM_MS ||
2611 dim == GLSL_SAMPLER_DIM_SUBPASS_MS);
2612 bool gfx9_1d = ctx->ac.chip_class == GFX9 && dim == GLSL_SAMPLER_DIM_1D;
2613 assert(!add_frag_pos && "Input attachments should be lowered by this point.");
2614 count = image_type_to_components_count(dim, is_array);
2615
2616 if (is_ms && (instr->intrinsic == nir_intrinsic_image_deref_load ||
2617 instr->intrinsic == nir_intrinsic_bindless_image_load)) {
2618 LLVMValueRef fmask_load_address[3];
2619
2620 fmask_load_address[0] = LLVMBuildExtractElement(ctx->ac.builder, src0, masks[0], "");
2621 fmask_load_address[1] = LLVMBuildExtractElement(ctx->ac.builder, src0, masks[1], "");
2622 if (is_array)
2623 fmask_load_address[2] = LLVMBuildExtractElement(ctx->ac.builder, src0, masks[2], "");
2624 else
2625 fmask_load_address[2] = NULL;
2626
2627 sample_index = adjust_sample_index_using_fmask(&ctx->ac,
2628 fmask_load_address[0],
2629 fmask_load_address[1],
2630 fmask_load_address[2],
2631 sample_index,
2632 get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr),
2633 AC_DESC_FMASK, &instr->instr, dynamic_desc_index, true, false));
2634 }
2635 if (count == 1 && !gfx9_1d) {
2636 if (instr->src[1].ssa->num_components)
2637 args->coords[0] = LLVMBuildExtractElement(ctx->ac.builder, src0, masks[0], "");
2638 else
2639 args->coords[0] = src0;
2640 } else {
2641 int chan;
2642 if (is_ms)
2643 count--;
2644 for (chan = 0; chan < count; ++chan) {
2645 args->coords[chan] = ac_llvm_extract_elem(&ctx->ac, src0, chan);
2646 }
2647
2648 if (gfx9_1d) {
2649 if (is_array) {
2650 args->coords[2] = args->coords[1];
2651 args->coords[1] = ctx->ac.i32_0;
2652 } else
2653 args->coords[1] = ctx->ac.i32_0;
2654 count++;
2655 }
2656 if (ctx->ac.chip_class == GFX9 &&
2657 dim == GLSL_SAMPLER_DIM_2D &&
2658 !is_array) {
2659 /* The hw can't bind a slice of a 3D image as a 2D
2660 * image, because it ignores BASE_ARRAY if the target
2661 * is 3D. The workaround is to read BASE_ARRAY and set
2662 * it as the 3rd address operand for all 2D images.
2663 */
2664 LLVMValueRef first_layer, const5, mask;
2665
2666 const5 = LLVMConstInt(ctx->ac.i32, 5, 0);
2667 mask = LLVMConstInt(ctx->ac.i32, S_008F24_BASE_ARRAY(~0), 0);
2668 first_layer = LLVMBuildExtractElement(ctx->ac.builder, args->resource, const5, "");
2669 first_layer = LLVMBuildAnd(ctx->ac.builder, first_layer, mask, "");
2670
2671 args->coords[count] = first_layer;
2672 count++;
2673 }
2674
2675
2676 if (is_ms) {
2677 args->coords[count] = sample_index;
2678 count++;
2679 }
2680 }
2681 }
2682
2683 static LLVMValueRef get_image_buffer_descriptor(struct ac_nir_context *ctx,
2684 const nir_intrinsic_instr *instr,
2685 LLVMValueRef dynamic_index,
2686 bool write, bool atomic)
2687 {
2688 LLVMValueRef rsrc = get_image_descriptor(ctx, instr, dynamic_index, AC_DESC_BUFFER, write);
2689 if (ctx->ac.chip_class == GFX9 && LLVM_VERSION_MAJOR < 9 && atomic) {
2690 LLVMValueRef elem_count = LLVMBuildExtractElement(ctx->ac.builder, rsrc, LLVMConstInt(ctx->ac.i32, 2, 0), "");
2691 LLVMValueRef stride = LLVMBuildExtractElement(ctx->ac.builder, rsrc, LLVMConstInt(ctx->ac.i32, 1, 0), "");
2692 stride = LLVMBuildLShr(ctx->ac.builder, stride, LLVMConstInt(ctx->ac.i32, 16, 0), "");
2693
2694 LLVMValueRef new_elem_count = LLVMBuildSelect(ctx->ac.builder,
2695 LLVMBuildICmp(ctx->ac.builder, LLVMIntUGT, elem_count, stride, ""),
2696 elem_count, stride, "");
2697
2698 rsrc = LLVMBuildInsertElement(ctx->ac.builder, rsrc, new_elem_count,
2699 LLVMConstInt(ctx->ac.i32, 2, 0), "");
2700 }
2701 return rsrc;
2702 }
2703
2704 static LLVMValueRef enter_waterfall_image(struct ac_nir_context *ctx,
2705 struct waterfall_context *wctx,
2706 const nir_intrinsic_instr *instr)
2707 {
2708 nir_deref_instr *deref_instr = NULL;
2709
2710 if (instr->src[0].ssa->parent_instr->type == nir_instr_type_deref)
2711 deref_instr = nir_instr_as_deref(instr->src[0].ssa->parent_instr);
2712
2713 LLVMValueRef value = get_sampler_desc_index(ctx, deref_instr, &instr->instr, true);
2714 return enter_waterfall(ctx, wctx, value, nir_intrinsic_access(instr) & ACCESS_NON_UNIFORM);
2715 }
2716
2717 static LLVMValueRef visit_image_load(struct ac_nir_context *ctx,
2718 const nir_intrinsic_instr *instr,
2719 bool bindless)
2720 {
2721 LLVMValueRef res;
2722
2723 enum glsl_sampler_dim dim;
2724 enum gl_access_qualifier access;
2725 bool is_array;
2726 if (bindless) {
2727 dim = nir_intrinsic_image_dim(instr);
2728 access = nir_intrinsic_access(instr);
2729 is_array = nir_intrinsic_image_array(instr);
2730 } else {
2731 const nir_deref_instr *image_deref = get_image_deref(instr);
2732 const struct glsl_type *type = image_deref->type;
2733 const nir_variable *var = nir_deref_instr_get_variable(image_deref);
2734 dim = glsl_get_sampler_dim(type);
2735 access = var->data.access;
2736 is_array = glsl_sampler_type_is_array(type);
2737 }
2738
2739 struct waterfall_context wctx;
2740 LLVMValueRef dynamic_index = enter_waterfall_image(ctx, &wctx, instr);
2741
2742 struct ac_image_args args = {};
2743
2744 args.cache_policy = get_cache_policy(ctx, access, false, false);
2745
2746 if (dim == GLSL_SAMPLER_DIM_BUF) {
2747 unsigned mask = nir_ssa_def_components_read(&instr->dest.ssa);
2748 unsigned num_channels = util_last_bit(mask);
2749 LLVMValueRef rsrc, vindex;
2750
2751 rsrc = get_image_buffer_descriptor(ctx, instr, dynamic_index, false, false);
2752 vindex = LLVMBuildExtractElement(ctx->ac.builder, get_src(ctx, instr->src[1]),
2753 ctx->ac.i32_0, "");
2754
2755 bool can_speculate = access & ACCESS_CAN_REORDER;
2756 res = ac_build_buffer_load_format(&ctx->ac, rsrc, vindex,
2757 ctx->ac.i32_0, num_channels,
2758 args.cache_policy,
2759 can_speculate);
2760 res = ac_build_expand_to_vec4(&ctx->ac, res, num_channels);
2761
2762 res = ac_trim_vector(&ctx->ac, res, instr->dest.ssa.num_components);
2763 res = ac_to_integer(&ctx->ac, res);
2764 } else {
2765 bool level_zero = nir_src_is_const(instr->src[3]) && nir_src_as_uint(instr->sr