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