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