nir: make nir_const_value scalar
[mesa.git] / src / amd / common / 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 "ac_nir_to_llvm.h"
25 #include "ac_llvm_build.h"
26 #include "ac_llvm_util.h"
27 #include "ac_binary.h"
28 #include "sid.h"
29 #include "nir/nir.h"
30 #include "nir/nir_deref.h"
31 #include "util/bitscan.h"
32 #include "util/u_math.h"
33 #include "ac_shader_abi.h"
34 #include "ac_shader_util.h"
35
36 struct ac_nir_context {
37 struct ac_llvm_context ac;
38 struct ac_shader_abi *abi;
39
40 gl_shader_stage stage;
41
42 LLVMValueRef *ssa_defs;
43
44 struct hash_table *defs;
45 struct hash_table *phis;
46 struct hash_table *vars;
47
48 LLVMValueRef main_function;
49 LLVMBasicBlockRef continue_block;
50 LLVMBasicBlockRef break_block;
51
52 int num_locals;
53 LLVMValueRef *locals;
54 };
55
56 static LLVMValueRef get_sampler_desc(struct ac_nir_context *ctx,
57 nir_deref_instr *deref_instr,
58 enum ac_descriptor_type desc_type,
59 const nir_instr *instr,
60 bool image, bool write);
61
62 static void
63 build_store_values_extended(struct ac_llvm_context *ac,
64 LLVMValueRef *values,
65 unsigned value_count,
66 unsigned value_stride,
67 LLVMValueRef vec)
68 {
69 LLVMBuilderRef builder = ac->builder;
70 unsigned i;
71
72 for (i = 0; i < value_count; i++) {
73 LLVMValueRef ptr = values[i * value_stride];
74 LLVMValueRef index = LLVMConstInt(ac->i32, i, false);
75 LLVMValueRef value = LLVMBuildExtractElement(builder, vec, index, "");
76 LLVMBuildStore(builder, value, ptr);
77 }
78 }
79
80 static enum ac_image_dim
81 get_ac_sampler_dim(const struct ac_llvm_context *ctx, enum glsl_sampler_dim dim,
82 bool is_array)
83 {
84 switch (dim) {
85 case GLSL_SAMPLER_DIM_1D:
86 if (ctx->chip_class >= GFX9)
87 return is_array ? ac_image_2darray : ac_image_2d;
88 return is_array ? ac_image_1darray : ac_image_1d;
89 case GLSL_SAMPLER_DIM_2D:
90 case GLSL_SAMPLER_DIM_RECT:
91 case GLSL_SAMPLER_DIM_EXTERNAL:
92 return is_array ? ac_image_2darray : ac_image_2d;
93 case GLSL_SAMPLER_DIM_3D:
94 return ac_image_3d;
95 case GLSL_SAMPLER_DIM_CUBE:
96 return ac_image_cube;
97 case GLSL_SAMPLER_DIM_MS:
98 return is_array ? ac_image_2darraymsaa : ac_image_2dmsaa;
99 case GLSL_SAMPLER_DIM_SUBPASS:
100 return ac_image_2darray;
101 case GLSL_SAMPLER_DIM_SUBPASS_MS:
102 return ac_image_2darraymsaa;
103 default:
104 unreachable("bad sampler dim");
105 }
106 }
107
108 static enum ac_image_dim
109 get_ac_image_dim(const struct ac_llvm_context *ctx, enum glsl_sampler_dim sdim,
110 bool is_array)
111 {
112 enum ac_image_dim dim = get_ac_sampler_dim(ctx, sdim, is_array);
113
114 if (dim == ac_image_cube ||
115 (ctx->chip_class <= VI && dim == ac_image_3d))
116 dim = ac_image_2darray;
117
118 return dim;
119 }
120
121 static LLVMTypeRef get_def_type(struct ac_nir_context *ctx,
122 const nir_ssa_def *def)
123 {
124 LLVMTypeRef type = LLVMIntTypeInContext(ctx->ac.context, def->bit_size);
125 if (def->num_components > 1) {
126 type = LLVMVectorType(type, def->num_components);
127 }
128 return type;
129 }
130
131 static LLVMValueRef get_src(struct ac_nir_context *nir, nir_src src)
132 {
133 assert(src.is_ssa);
134 return nir->ssa_defs[src.ssa->index];
135 }
136
137 static LLVMValueRef
138 get_memory_ptr(struct ac_nir_context *ctx, nir_src src)
139 {
140 LLVMValueRef ptr = get_src(ctx, src);
141 ptr = LLVMBuildGEP(ctx->ac.builder, ctx->ac.lds, &ptr, 1, "");
142 int addr_space = LLVMGetPointerAddressSpace(LLVMTypeOf(ptr));
143
144 return LLVMBuildBitCast(ctx->ac.builder, ptr,
145 LLVMPointerType(ctx->ac.i32, addr_space), "");
146 }
147
148 static LLVMBasicBlockRef get_block(struct ac_nir_context *nir,
149 const struct nir_block *b)
150 {
151 struct hash_entry *entry = _mesa_hash_table_search(nir->defs, b);
152 return (LLVMBasicBlockRef)entry->data;
153 }
154
155 static LLVMValueRef get_alu_src(struct ac_nir_context *ctx,
156 nir_alu_src src,
157 unsigned num_components)
158 {
159 LLVMValueRef value = get_src(ctx, src.src);
160 bool need_swizzle = false;
161
162 assert(value);
163 unsigned src_components = ac_get_llvm_num_components(value);
164 for (unsigned i = 0; i < num_components; ++i) {
165 assert(src.swizzle[i] < src_components);
166 if (src.swizzle[i] != i)
167 need_swizzle = true;
168 }
169
170 if (need_swizzle || num_components != src_components) {
171 LLVMValueRef masks[] = {
172 LLVMConstInt(ctx->ac.i32, src.swizzle[0], false),
173 LLVMConstInt(ctx->ac.i32, src.swizzle[1], false),
174 LLVMConstInt(ctx->ac.i32, src.swizzle[2], false),
175 LLVMConstInt(ctx->ac.i32, src.swizzle[3], false)};
176
177 if (src_components > 1 && num_components == 1) {
178 value = LLVMBuildExtractElement(ctx->ac.builder, value,
179 masks[0], "");
180 } else if (src_components == 1 && num_components > 1) {
181 LLVMValueRef values[] = {value, value, value, value};
182 value = ac_build_gather_values(&ctx->ac, values, num_components);
183 } else {
184 LLVMValueRef swizzle = LLVMConstVector(masks, num_components);
185 value = LLVMBuildShuffleVector(ctx->ac.builder, value, value,
186 swizzle, "");
187 }
188 }
189 assert(!src.negate);
190 assert(!src.abs);
191 return value;
192 }
193
194 static LLVMValueRef emit_int_cmp(struct ac_llvm_context *ctx,
195 LLVMIntPredicate pred, LLVMValueRef src0,
196 LLVMValueRef src1)
197 {
198 LLVMValueRef result = LLVMBuildICmp(ctx->builder, pred, src0, src1, "");
199 return LLVMBuildSelect(ctx->builder, result,
200 LLVMConstInt(ctx->i32, 0xFFFFFFFF, false),
201 ctx->i32_0, "");
202 }
203
204 static LLVMValueRef emit_float_cmp(struct ac_llvm_context *ctx,
205 LLVMRealPredicate pred, LLVMValueRef src0,
206 LLVMValueRef src1)
207 {
208 LLVMValueRef result;
209 src0 = ac_to_float(ctx, src0);
210 src1 = ac_to_float(ctx, src1);
211 result = LLVMBuildFCmp(ctx->builder, pred, src0, src1, "");
212 return LLVMBuildSelect(ctx->builder, result,
213 LLVMConstInt(ctx->i32, 0xFFFFFFFF, false),
214 ctx->i32_0, "");
215 }
216
217 static LLVMValueRef emit_intrin_1f_param(struct ac_llvm_context *ctx,
218 const char *intrin,
219 LLVMTypeRef result_type,
220 LLVMValueRef src0)
221 {
222 char name[64];
223 LLVMValueRef params[] = {
224 ac_to_float(ctx, src0),
225 };
226
227 MAYBE_UNUSED const int length = snprintf(name, sizeof(name), "%s.f%d", intrin,
228 ac_get_elem_bits(ctx, result_type));
229 assert(length < sizeof(name));
230 return ac_build_intrinsic(ctx, name, result_type, params, 1, AC_FUNC_ATTR_READNONE);
231 }
232
233 static LLVMValueRef emit_intrin_2f_param(struct ac_llvm_context *ctx,
234 const char *intrin,
235 LLVMTypeRef result_type,
236 LLVMValueRef src0, LLVMValueRef src1)
237 {
238 char name[64];
239 LLVMValueRef params[] = {
240 ac_to_float(ctx, src0),
241 ac_to_float(ctx, src1),
242 };
243
244 MAYBE_UNUSED const int length = snprintf(name, sizeof(name), "%s.f%d", intrin,
245 ac_get_elem_bits(ctx, result_type));
246 assert(length < sizeof(name));
247 return ac_build_intrinsic(ctx, name, result_type, params, 2, AC_FUNC_ATTR_READNONE);
248 }
249
250 static LLVMValueRef emit_intrin_3f_param(struct ac_llvm_context *ctx,
251 const char *intrin,
252 LLVMTypeRef result_type,
253 LLVMValueRef src0, LLVMValueRef src1, LLVMValueRef src2)
254 {
255 char name[64];
256 LLVMValueRef params[] = {
257 ac_to_float(ctx, src0),
258 ac_to_float(ctx, src1),
259 ac_to_float(ctx, src2),
260 };
261
262 MAYBE_UNUSED const int length = snprintf(name, sizeof(name), "%s.f%d", intrin,
263 ac_get_elem_bits(ctx, result_type));
264 assert(length < sizeof(name));
265 return ac_build_intrinsic(ctx, name, result_type, params, 3, AC_FUNC_ATTR_READNONE);
266 }
267
268 static LLVMValueRef emit_bcsel(struct ac_llvm_context *ctx,
269 LLVMValueRef src0, LLVMValueRef src1, LLVMValueRef src2)
270 {
271 assert(LLVMGetTypeKind(LLVMTypeOf(src0)) != LLVMVectorTypeKind);
272
273 LLVMValueRef v = LLVMBuildICmp(ctx->builder, LLVMIntNE, src0,
274 ctx->i32_0, "");
275 return LLVMBuildSelect(ctx->builder, v,
276 ac_to_integer_or_pointer(ctx, src1),
277 ac_to_integer_or_pointer(ctx, src2), "");
278 }
279
280 static LLVMValueRef emit_iabs(struct ac_llvm_context *ctx,
281 LLVMValueRef src0)
282 {
283 return ac_build_imax(ctx, src0, LLVMBuildNeg(ctx->builder, src0, ""));
284 }
285
286 static LLVMValueRef emit_uint_carry(struct ac_llvm_context *ctx,
287 const char *intrin,
288 LLVMValueRef src0, LLVMValueRef src1)
289 {
290 LLVMTypeRef ret_type;
291 LLVMTypeRef types[] = { ctx->i32, ctx->i1 };
292 LLVMValueRef res;
293 LLVMValueRef params[] = { src0, src1 };
294 ret_type = LLVMStructTypeInContext(ctx->context, types,
295 2, true);
296
297 res = ac_build_intrinsic(ctx, intrin, ret_type,
298 params, 2, AC_FUNC_ATTR_READNONE);
299
300 res = LLVMBuildExtractValue(ctx->builder, res, 1, "");
301 res = LLVMBuildZExt(ctx->builder, res, ctx->i32, "");
302 return res;
303 }
304
305 static LLVMValueRef emit_b2f(struct ac_llvm_context *ctx,
306 LLVMValueRef src0,
307 unsigned bitsize)
308 {
309 LLVMValueRef result = LLVMBuildAnd(ctx->builder, src0,
310 LLVMBuildBitCast(ctx->builder, LLVMConstReal(ctx->f32, 1.0), ctx->i32, ""),
311 "");
312 result = LLVMBuildBitCast(ctx->builder, result, ctx->f32, "");
313
314 switch (bitsize) {
315 case 16:
316 return LLVMBuildFPTrunc(ctx->builder, result, ctx->f16, "");
317 case 32:
318 return result;
319 case 64:
320 return LLVMBuildFPExt(ctx->builder, result, ctx->f64, "");
321 default:
322 unreachable("Unsupported bit size.");
323 }
324 }
325
326 static LLVMValueRef emit_f2b(struct ac_llvm_context *ctx,
327 LLVMValueRef src0)
328 {
329 src0 = ac_to_float(ctx, src0);
330 LLVMValueRef zero = LLVMConstNull(LLVMTypeOf(src0));
331 return LLVMBuildSExt(ctx->builder,
332 LLVMBuildFCmp(ctx->builder, LLVMRealUNE, src0, zero, ""),
333 ctx->i32, "");
334 }
335
336 static LLVMValueRef emit_b2i(struct ac_llvm_context *ctx,
337 LLVMValueRef src0,
338 unsigned bitsize)
339 {
340 LLVMValueRef result = LLVMBuildAnd(ctx->builder, src0, ctx->i32_1, "");
341
342 switch (bitsize) {
343 case 8:
344 return LLVMBuildTrunc(ctx->builder, result, ctx->i8, "");
345 case 16:
346 return LLVMBuildTrunc(ctx->builder, result, ctx->i16, "");
347 case 32:
348 return result;
349 case 64:
350 return LLVMBuildZExt(ctx->builder, result, ctx->i64, "");
351 default:
352 unreachable("Unsupported bit size.");
353 }
354 }
355
356 static LLVMValueRef emit_i2b(struct ac_llvm_context *ctx,
357 LLVMValueRef src0)
358 {
359 LLVMValueRef zero = LLVMConstNull(LLVMTypeOf(src0));
360 return LLVMBuildSExt(ctx->builder,
361 LLVMBuildICmp(ctx->builder, LLVMIntNE, src0, zero, ""),
362 ctx->i32, "");
363 }
364
365 static LLVMValueRef emit_f2f16(struct ac_llvm_context *ctx,
366 LLVMValueRef src0)
367 {
368 LLVMValueRef result;
369 LLVMValueRef cond = NULL;
370
371 src0 = ac_to_float(ctx, src0);
372 result = LLVMBuildFPTrunc(ctx->builder, src0, ctx->f16, "");
373
374 if (ctx->chip_class >= VI) {
375 LLVMValueRef args[2];
376 /* Check if the result is a denormal - and flush to 0 if so. */
377 args[0] = result;
378 args[1] = LLVMConstInt(ctx->i32, N_SUBNORMAL | P_SUBNORMAL, false);
379 cond = ac_build_intrinsic(ctx, "llvm.amdgcn.class.f16", ctx->i1, args, 2, AC_FUNC_ATTR_READNONE);
380 }
381
382 /* need to convert back up to f32 */
383 result = LLVMBuildFPExt(ctx->builder, result, ctx->f32, "");
384
385 if (ctx->chip_class >= VI)
386 result = LLVMBuildSelect(ctx->builder, cond, ctx->f32_0, result, "");
387 else {
388 /* for SI/CIK */
389 /* 0x38800000 is smallest half float value (2^-14) in 32-bit float,
390 * so compare the result and flush to 0 if it's smaller.
391 */
392 LLVMValueRef temp, cond2;
393 temp = emit_intrin_1f_param(ctx, "llvm.fabs", ctx->f32, result);
394 cond = LLVMBuildFCmp(ctx->builder, LLVMRealUGT,
395 LLVMBuildBitCast(ctx->builder, LLVMConstInt(ctx->i32, 0x38800000, false), ctx->f32, ""),
396 temp, "");
397 cond2 = LLVMBuildFCmp(ctx->builder, LLVMRealUNE,
398 temp, ctx->f32_0, "");
399 cond = LLVMBuildAnd(ctx->builder, cond, cond2, "");
400 result = LLVMBuildSelect(ctx->builder, cond, ctx->f32_0, result, "");
401 }
402 return result;
403 }
404
405 static LLVMValueRef emit_umul_high(struct ac_llvm_context *ctx,
406 LLVMValueRef src0, LLVMValueRef src1)
407 {
408 LLVMValueRef dst64, result;
409 src0 = LLVMBuildZExt(ctx->builder, src0, ctx->i64, "");
410 src1 = LLVMBuildZExt(ctx->builder, src1, ctx->i64, "");
411
412 dst64 = LLVMBuildMul(ctx->builder, src0, src1, "");
413 dst64 = LLVMBuildLShr(ctx->builder, dst64, LLVMConstInt(ctx->i64, 32, false), "");
414 result = LLVMBuildTrunc(ctx->builder, dst64, ctx->i32, "");
415 return result;
416 }
417
418 static LLVMValueRef emit_imul_high(struct ac_llvm_context *ctx,
419 LLVMValueRef src0, LLVMValueRef src1)
420 {
421 LLVMValueRef dst64, result;
422 src0 = LLVMBuildSExt(ctx->builder, src0, ctx->i64, "");
423 src1 = LLVMBuildSExt(ctx->builder, src1, ctx->i64, "");
424
425 dst64 = LLVMBuildMul(ctx->builder, src0, src1, "");
426 dst64 = LLVMBuildAShr(ctx->builder, dst64, LLVMConstInt(ctx->i64, 32, false), "");
427 result = LLVMBuildTrunc(ctx->builder, dst64, ctx->i32, "");
428 return result;
429 }
430
431 static LLVMValueRef emit_bitfield_extract(struct ac_llvm_context *ctx,
432 bool is_signed,
433 const LLVMValueRef srcs[3])
434 {
435 LLVMValueRef result;
436
437 if (HAVE_LLVM >= 0x0800) {
438 LLVMValueRef icond = LLVMBuildICmp(ctx->builder, LLVMIntEQ, srcs[2], LLVMConstInt(ctx->i32, 32, false), "");
439 result = ac_build_bfe(ctx, srcs[0], srcs[1], srcs[2], is_signed);
440 result = LLVMBuildSelect(ctx->builder, icond, srcs[0], result, "");
441 } else {
442 /* FIXME: LLVM 7+ returns incorrect result when count is 0.
443 * https://bugs.freedesktop.org/show_bug.cgi?id=107276
444 */
445 LLVMValueRef zero = ctx->i32_0;
446 LLVMValueRef icond1 = LLVMBuildICmp(ctx->builder, LLVMIntEQ, srcs[2], LLVMConstInt(ctx->i32, 32, false), "");
447 LLVMValueRef icond2 = LLVMBuildICmp(ctx->builder, LLVMIntEQ, srcs[2], zero, "");
448
449 result = ac_build_bfe(ctx, srcs[0], srcs[1], srcs[2], is_signed);
450 result = LLVMBuildSelect(ctx->builder, icond1, srcs[0], result, "");
451 result = LLVMBuildSelect(ctx->builder, icond2, zero, result, "");
452 }
453
454 return result;
455 }
456
457 static LLVMValueRef emit_bitfield_insert(struct ac_llvm_context *ctx,
458 LLVMValueRef src0, LLVMValueRef src1,
459 LLVMValueRef src2, LLVMValueRef src3)
460 {
461 LLVMValueRef bfi_args[3], result;
462
463 bfi_args[0] = LLVMBuildShl(ctx->builder,
464 LLVMBuildSub(ctx->builder,
465 LLVMBuildShl(ctx->builder,
466 ctx->i32_1,
467 src3, ""),
468 ctx->i32_1, ""),
469 src2, "");
470 bfi_args[1] = LLVMBuildShl(ctx->builder, src1, src2, "");
471 bfi_args[2] = src0;
472
473 LLVMValueRef icond = LLVMBuildICmp(ctx->builder, LLVMIntEQ, src3, LLVMConstInt(ctx->i32, 32, false), "");
474
475 /* Calculate:
476 * (arg0 & arg1) | (~arg0 & arg2) = arg2 ^ (arg0 & (arg1 ^ arg2)
477 * Use the right-hand side, which the LLVM backend can convert to V_BFI.
478 */
479 result = LLVMBuildXor(ctx->builder, bfi_args[2],
480 LLVMBuildAnd(ctx->builder, bfi_args[0],
481 LLVMBuildXor(ctx->builder, bfi_args[1], bfi_args[2], ""), ""), "");
482
483 result = LLVMBuildSelect(ctx->builder, icond, src1, result, "");
484 return result;
485 }
486
487 static LLVMValueRef emit_pack_half_2x16(struct ac_llvm_context *ctx,
488 LLVMValueRef src0)
489 {
490 LLVMValueRef comp[2];
491
492 src0 = ac_to_float(ctx, src0);
493 comp[0] = LLVMBuildExtractElement(ctx->builder, src0, ctx->i32_0, "");
494 comp[1] = LLVMBuildExtractElement(ctx->builder, src0, ctx->i32_1, "");
495
496 return LLVMBuildBitCast(ctx->builder, ac_build_cvt_pkrtz_f16(ctx, comp),
497 ctx->i32, "");
498 }
499
500 static LLVMValueRef emit_unpack_half_2x16(struct ac_llvm_context *ctx,
501 LLVMValueRef src0)
502 {
503 LLVMValueRef const16 = LLVMConstInt(ctx->i32, 16, false);
504 LLVMValueRef temps[2], val;
505 int i;
506
507 for (i = 0; i < 2; i++) {
508 val = i == 1 ? LLVMBuildLShr(ctx->builder, src0, const16, "") : src0;
509 val = LLVMBuildTrunc(ctx->builder, val, ctx->i16, "");
510 val = LLVMBuildBitCast(ctx->builder, val, ctx->f16, "");
511 temps[i] = LLVMBuildFPExt(ctx->builder, val, ctx->f32, "");
512 }
513 return ac_build_gather_values(ctx, temps, 2);
514 }
515
516 static LLVMValueRef emit_ddxy(struct ac_nir_context *ctx,
517 nir_op op,
518 LLVMValueRef src0)
519 {
520 unsigned mask;
521 int idx;
522 LLVMValueRef result;
523
524 if (op == nir_op_fddx_fine)
525 mask = AC_TID_MASK_LEFT;
526 else if (op == nir_op_fddy_fine)
527 mask = AC_TID_MASK_TOP;
528 else
529 mask = AC_TID_MASK_TOP_LEFT;
530
531 /* for DDX we want to next X pixel, DDY next Y pixel. */
532 if (op == nir_op_fddx_fine ||
533 op == nir_op_fddx_coarse ||
534 op == nir_op_fddx)
535 idx = 1;
536 else
537 idx = 2;
538
539 result = ac_build_ddxy(&ctx->ac, mask, idx, src0);
540 return result;
541 }
542
543 static void visit_alu(struct ac_nir_context *ctx, const nir_alu_instr *instr)
544 {
545 LLVMValueRef src[4], result = NULL;
546 unsigned num_components = instr->dest.dest.ssa.num_components;
547 unsigned src_components;
548 LLVMTypeRef def_type = get_def_type(ctx, &instr->dest.dest.ssa);
549
550 assert(nir_op_infos[instr->op].num_inputs <= ARRAY_SIZE(src));
551 switch (instr->op) {
552 case nir_op_vec2:
553 case nir_op_vec3:
554 case nir_op_vec4:
555 src_components = 1;
556 break;
557 case nir_op_pack_half_2x16:
558 src_components = 2;
559 break;
560 case nir_op_unpack_half_2x16:
561 src_components = 1;
562 break;
563 case nir_op_cube_face_coord:
564 case nir_op_cube_face_index:
565 src_components = 3;
566 break;
567 default:
568 src_components = num_components;
569 break;
570 }
571 for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++)
572 src[i] = get_alu_src(ctx, instr->src[i], src_components);
573
574 switch (instr->op) {
575 case nir_op_fmov:
576 case nir_op_imov:
577 result = src[0];
578 break;
579 case nir_op_fneg:
580 src[0] = ac_to_float(&ctx->ac, src[0]);
581 result = LLVMBuildFNeg(ctx->ac.builder, src[0], "");
582 break;
583 case nir_op_ineg:
584 result = LLVMBuildNeg(ctx->ac.builder, src[0], "");
585 break;
586 case nir_op_inot:
587 result = LLVMBuildNot(ctx->ac.builder, src[0], "");
588 break;
589 case nir_op_iadd:
590 result = LLVMBuildAdd(ctx->ac.builder, src[0], src[1], "");
591 break;
592 case nir_op_fadd:
593 src[0] = ac_to_float(&ctx->ac, src[0]);
594 src[1] = ac_to_float(&ctx->ac, src[1]);
595 result = LLVMBuildFAdd(ctx->ac.builder, src[0], src[1], "");
596 break;
597 case nir_op_fsub:
598 src[0] = ac_to_float(&ctx->ac, src[0]);
599 src[1] = ac_to_float(&ctx->ac, src[1]);
600 result = LLVMBuildFSub(ctx->ac.builder, src[0], src[1], "");
601 break;
602 case nir_op_isub:
603 result = LLVMBuildSub(ctx->ac.builder, src[0], src[1], "");
604 break;
605 case nir_op_imul:
606 result = LLVMBuildMul(ctx->ac.builder, src[0], src[1], "");
607 break;
608 case nir_op_imod:
609 result = LLVMBuildSRem(ctx->ac.builder, src[0], src[1], "");
610 break;
611 case nir_op_umod:
612 result = LLVMBuildURem(ctx->ac.builder, src[0], src[1], "");
613 break;
614 case nir_op_fmod:
615 src[0] = ac_to_float(&ctx->ac, src[0]);
616 src[1] = ac_to_float(&ctx->ac, src[1]);
617 result = ac_build_fdiv(&ctx->ac, src[0], src[1]);
618 result = emit_intrin_1f_param(&ctx->ac, "llvm.floor",
619 ac_to_float_type(&ctx->ac, def_type), result);
620 result = LLVMBuildFMul(ctx->ac.builder, src[1] , result, "");
621 result = LLVMBuildFSub(ctx->ac.builder, src[0], result, "");
622 break;
623 case nir_op_frem:
624 src[0] = ac_to_float(&ctx->ac, src[0]);
625 src[1] = ac_to_float(&ctx->ac, src[1]);
626 result = LLVMBuildFRem(ctx->ac.builder, src[0], src[1], "");
627 break;
628 case nir_op_irem:
629 result = LLVMBuildSRem(ctx->ac.builder, src[0], src[1], "");
630 break;
631 case nir_op_idiv:
632 result = LLVMBuildSDiv(ctx->ac.builder, src[0], src[1], "");
633 break;
634 case nir_op_udiv:
635 result = LLVMBuildUDiv(ctx->ac.builder, src[0], src[1], "");
636 break;
637 case nir_op_fmul:
638 src[0] = ac_to_float(&ctx->ac, src[0]);
639 src[1] = ac_to_float(&ctx->ac, src[1]);
640 result = LLVMBuildFMul(ctx->ac.builder, src[0], src[1], "");
641 break;
642 case nir_op_frcp:
643 src[0] = ac_to_float(&ctx->ac, src[0]);
644 result = ac_build_fdiv(&ctx->ac, LLVMConstReal(LLVMTypeOf(src[0]), 1.0), src[0]);
645 break;
646 case nir_op_iand:
647 result = LLVMBuildAnd(ctx->ac.builder, src[0], src[1], "");
648 break;
649 case nir_op_ior:
650 result = LLVMBuildOr(ctx->ac.builder, src[0], src[1], "");
651 break;
652 case nir_op_ixor:
653 result = LLVMBuildXor(ctx->ac.builder, src[0], src[1], "");
654 break;
655 case nir_op_ishl:
656 if (ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src[1])) < ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src[0])))
657 src[1] = LLVMBuildZExt(ctx->ac.builder, src[1],
658 LLVMTypeOf(src[0]), "");
659 else if (ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src[1])) > ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src[0])))
660 src[1] = LLVMBuildTrunc(ctx->ac.builder, src[1],
661 LLVMTypeOf(src[0]), "");
662 result = LLVMBuildShl(ctx->ac.builder, src[0], src[1], "");
663 break;
664 case nir_op_ishr:
665 if (ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src[1])) < ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src[0])))
666 src[1] = LLVMBuildZExt(ctx->ac.builder, src[1],
667 LLVMTypeOf(src[0]), "");
668 else if (ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src[1])) > ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src[0])))
669 src[1] = LLVMBuildTrunc(ctx->ac.builder, src[1],
670 LLVMTypeOf(src[0]), "");
671 result = LLVMBuildAShr(ctx->ac.builder, src[0], src[1], "");
672 break;
673 case nir_op_ushr:
674 if (ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src[1])) < ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src[0])))
675 src[1] = LLVMBuildZExt(ctx->ac.builder, src[1],
676 LLVMTypeOf(src[0]), "");
677 else if (ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src[1])) > ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src[0])))
678 src[1] = LLVMBuildTrunc(ctx->ac.builder, src[1],
679 LLVMTypeOf(src[0]), "");
680 result = LLVMBuildLShr(ctx->ac.builder, src[0], src[1], "");
681 break;
682 case nir_op_ilt32:
683 result = emit_int_cmp(&ctx->ac, LLVMIntSLT, src[0], src[1]);
684 break;
685 case nir_op_ine32:
686 result = emit_int_cmp(&ctx->ac, LLVMIntNE, src[0], src[1]);
687 break;
688 case nir_op_ieq32:
689 result = emit_int_cmp(&ctx->ac, LLVMIntEQ, src[0], src[1]);
690 break;
691 case nir_op_ige32:
692 result = emit_int_cmp(&ctx->ac, LLVMIntSGE, src[0], src[1]);
693 break;
694 case nir_op_ult32:
695 result = emit_int_cmp(&ctx->ac, LLVMIntULT, src[0], src[1]);
696 break;
697 case nir_op_uge32:
698 result = emit_int_cmp(&ctx->ac, LLVMIntUGE, src[0], src[1]);
699 break;
700 case nir_op_feq32:
701 result = emit_float_cmp(&ctx->ac, LLVMRealOEQ, src[0], src[1]);
702 break;
703 case nir_op_fne32:
704 result = emit_float_cmp(&ctx->ac, LLVMRealUNE, src[0], src[1]);
705 break;
706 case nir_op_flt32:
707 result = emit_float_cmp(&ctx->ac, LLVMRealOLT, src[0], src[1]);
708 break;
709 case nir_op_fge32:
710 result = emit_float_cmp(&ctx->ac, LLVMRealOGE, src[0], src[1]);
711 break;
712 case nir_op_fabs:
713 result = emit_intrin_1f_param(&ctx->ac, "llvm.fabs",
714 ac_to_float_type(&ctx->ac, def_type), src[0]);
715 break;
716 case nir_op_iabs:
717 result = emit_iabs(&ctx->ac, src[0]);
718 break;
719 case nir_op_imax:
720 result = ac_build_imax(&ctx->ac, src[0], src[1]);
721 break;
722 case nir_op_imin:
723 result = ac_build_imin(&ctx->ac, src[0], src[1]);
724 break;
725 case nir_op_umax:
726 result = ac_build_umax(&ctx->ac, src[0], src[1]);
727 break;
728 case nir_op_umin:
729 result = ac_build_umin(&ctx->ac, src[0], src[1]);
730 break;
731 case nir_op_isign:
732 result = ac_build_isign(&ctx->ac, src[0],
733 instr->dest.dest.ssa.bit_size);
734 break;
735 case nir_op_fsign:
736 src[0] = ac_to_float(&ctx->ac, src[0]);
737 result = ac_build_fsign(&ctx->ac, src[0],
738 instr->dest.dest.ssa.bit_size);
739 break;
740 case nir_op_ffloor:
741 result = emit_intrin_1f_param(&ctx->ac, "llvm.floor",
742 ac_to_float_type(&ctx->ac, def_type), src[0]);
743 break;
744 case nir_op_ftrunc:
745 result = emit_intrin_1f_param(&ctx->ac, "llvm.trunc",
746 ac_to_float_type(&ctx->ac, def_type), src[0]);
747 break;
748 case nir_op_fceil:
749 result = emit_intrin_1f_param(&ctx->ac, "llvm.ceil",
750 ac_to_float_type(&ctx->ac, def_type), src[0]);
751 break;
752 case nir_op_fround_even:
753 result = emit_intrin_1f_param(&ctx->ac, "llvm.rint",
754 ac_to_float_type(&ctx->ac, def_type),src[0]);
755 break;
756 case nir_op_ffract:
757 src[0] = ac_to_float(&ctx->ac, src[0]);
758 result = ac_build_fract(&ctx->ac, src[0],
759 instr->dest.dest.ssa.bit_size);
760 break;
761 case nir_op_fsin:
762 result = emit_intrin_1f_param(&ctx->ac, "llvm.sin",
763 ac_to_float_type(&ctx->ac, def_type), src[0]);
764 break;
765 case nir_op_fcos:
766 result = emit_intrin_1f_param(&ctx->ac, "llvm.cos",
767 ac_to_float_type(&ctx->ac, def_type), src[0]);
768 break;
769 case nir_op_fsqrt:
770 result = emit_intrin_1f_param(&ctx->ac, "llvm.sqrt",
771 ac_to_float_type(&ctx->ac, def_type), src[0]);
772 break;
773 case nir_op_fexp2:
774 result = emit_intrin_1f_param(&ctx->ac, "llvm.exp2",
775 ac_to_float_type(&ctx->ac, def_type), src[0]);
776 break;
777 case nir_op_flog2:
778 result = emit_intrin_1f_param(&ctx->ac, "llvm.log2",
779 ac_to_float_type(&ctx->ac, def_type), src[0]);
780 break;
781 case nir_op_frsq:
782 result = emit_intrin_1f_param(&ctx->ac, "llvm.sqrt",
783 ac_to_float_type(&ctx->ac, def_type), src[0]);
784 result = ac_build_fdiv(&ctx->ac, LLVMConstReal(LLVMTypeOf(result), 1.0), result);
785 break;
786 case nir_op_frexp_exp:
787 src[0] = ac_to_float(&ctx->ac, src[0]);
788 result = ac_build_frexp_exp(&ctx->ac, src[0],
789 ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src[0])));
790 if (ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src[0])) == 16)
791 result = LLVMBuildSExt(ctx->ac.builder, result,
792 ctx->ac.i32, "");
793 break;
794 case nir_op_frexp_sig:
795 src[0] = ac_to_float(&ctx->ac, src[0]);
796 result = ac_build_frexp_mant(&ctx->ac, src[0],
797 instr->dest.dest.ssa.bit_size);
798 break;
799 case nir_op_fpow:
800 result = emit_intrin_2f_param(&ctx->ac, "llvm.pow",
801 ac_to_float_type(&ctx->ac, def_type), src[0], src[1]);
802 break;
803 case nir_op_fmax:
804 result = emit_intrin_2f_param(&ctx->ac, "llvm.maxnum",
805 ac_to_float_type(&ctx->ac, def_type), src[0], src[1]);
806 if (ctx->ac.chip_class < GFX9 &&
807 instr->dest.dest.ssa.bit_size == 32) {
808 /* Only pre-GFX9 chips do not flush denorms. */
809 result = emit_intrin_1f_param(&ctx->ac, "llvm.canonicalize",
810 ac_to_float_type(&ctx->ac, def_type),
811 result);
812 }
813 break;
814 case nir_op_fmin:
815 result = emit_intrin_2f_param(&ctx->ac, "llvm.minnum",
816 ac_to_float_type(&ctx->ac, def_type), src[0], src[1]);
817 if (ctx->ac.chip_class < GFX9 &&
818 instr->dest.dest.ssa.bit_size == 32) {
819 /* Only pre-GFX9 chips do not flush denorms. */
820 result = emit_intrin_1f_param(&ctx->ac, "llvm.canonicalize",
821 ac_to_float_type(&ctx->ac, def_type),
822 result);
823 }
824 break;
825 case nir_op_ffma:
826 result = emit_intrin_3f_param(&ctx->ac, "llvm.fmuladd",
827 ac_to_float_type(&ctx->ac, def_type), src[0], src[1], src[2]);
828 break;
829 case nir_op_ldexp:
830 src[0] = ac_to_float(&ctx->ac, src[0]);
831 if (ac_get_elem_bits(&ctx->ac, def_type) == 32)
832 result = ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.ldexp.f32", ctx->ac.f32, src, 2, AC_FUNC_ATTR_READNONE);
833 else if (ac_get_elem_bits(&ctx->ac, def_type) == 16)
834 result = ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.ldexp.f16", ctx->ac.f16, src, 2, AC_FUNC_ATTR_READNONE);
835 else
836 result = ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.ldexp.f64", ctx->ac.f64, src, 2, AC_FUNC_ATTR_READNONE);
837 break;
838 case nir_op_ibitfield_extract:
839 result = emit_bitfield_extract(&ctx->ac, true, src);
840 break;
841 case nir_op_ubitfield_extract:
842 result = emit_bitfield_extract(&ctx->ac, false, src);
843 break;
844 case nir_op_bitfield_insert:
845 result = emit_bitfield_insert(&ctx->ac, src[0], src[1], src[2], src[3]);
846 break;
847 case nir_op_bitfield_reverse:
848 result = ac_build_bitfield_reverse(&ctx->ac, src[0]);
849 break;
850 case nir_op_bit_count:
851 result = ac_build_bit_count(&ctx->ac, src[0]);
852 break;
853 case nir_op_vec2:
854 case nir_op_vec3:
855 case nir_op_vec4:
856 for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++)
857 src[i] = ac_to_integer(&ctx->ac, src[i]);
858 result = ac_build_gather_values(&ctx->ac, src, num_components);
859 break;
860 case nir_op_f2i8:
861 case nir_op_f2i16:
862 case nir_op_f2i32:
863 case nir_op_f2i64:
864 src[0] = ac_to_float(&ctx->ac, src[0]);
865 result = LLVMBuildFPToSI(ctx->ac.builder, src[0], def_type, "");
866 break;
867 case nir_op_f2u8:
868 case nir_op_f2u16:
869 case nir_op_f2u32:
870 case nir_op_f2u64:
871 src[0] = ac_to_float(&ctx->ac, src[0]);
872 result = LLVMBuildFPToUI(ctx->ac.builder, src[0], def_type, "");
873 break;
874 case nir_op_i2f16:
875 case nir_op_i2f32:
876 case nir_op_i2f64:
877 result = LLVMBuildSIToFP(ctx->ac.builder, src[0], ac_to_float_type(&ctx->ac, def_type), "");
878 break;
879 case nir_op_u2f16:
880 case nir_op_u2f32:
881 case nir_op_u2f64:
882 result = LLVMBuildUIToFP(ctx->ac.builder, src[0], ac_to_float_type(&ctx->ac, def_type), "");
883 break;
884 case nir_op_f2f16_rtz:
885 src[0] = ac_to_float(&ctx->ac, src[0]);
886 if (LLVMTypeOf(src[0]) == ctx->ac.f64)
887 src[0] = LLVMBuildFPTrunc(ctx->ac.builder, src[0], ctx->ac.f32, "");
888 LLVMValueRef param[2] = { src[0], ctx->ac.f32_0 };
889 result = ac_build_cvt_pkrtz_f16(&ctx->ac, param);
890 result = LLVMBuildExtractElement(ctx->ac.builder, result, ctx->ac.i32_0, "");
891 break;
892 case nir_op_f2f16_rtne:
893 case nir_op_f2f16:
894 case nir_op_f2f32:
895 case nir_op_f2f64:
896 src[0] = ac_to_float(&ctx->ac, src[0]);
897 if (ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src[0])) < ac_get_elem_bits(&ctx->ac, def_type))
898 result = LLVMBuildFPExt(ctx->ac.builder, src[0], ac_to_float_type(&ctx->ac, def_type), "");
899 else
900 result = LLVMBuildFPTrunc(ctx->ac.builder, src[0], ac_to_float_type(&ctx->ac, def_type), "");
901 break;
902 case nir_op_u2u8:
903 case nir_op_u2u16:
904 case nir_op_u2u32:
905 case nir_op_u2u64:
906 if (ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src[0])) < ac_get_elem_bits(&ctx->ac, def_type))
907 result = LLVMBuildZExt(ctx->ac.builder, src[0], def_type, "");
908 else
909 result = LLVMBuildTrunc(ctx->ac.builder, src[0], def_type, "");
910 break;
911 case nir_op_i2i8:
912 case nir_op_i2i16:
913 case nir_op_i2i32:
914 case nir_op_i2i64:
915 if (ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src[0])) < ac_get_elem_bits(&ctx->ac, def_type))
916 result = LLVMBuildSExt(ctx->ac.builder, src[0], def_type, "");
917 else
918 result = LLVMBuildTrunc(ctx->ac.builder, src[0], def_type, "");
919 break;
920 case nir_op_b32csel:
921 result = emit_bcsel(&ctx->ac, src[0], src[1], src[2]);
922 break;
923 case nir_op_find_lsb:
924 result = ac_find_lsb(&ctx->ac, ctx->ac.i32, src[0]);
925 break;
926 case nir_op_ufind_msb:
927 result = ac_build_umsb(&ctx->ac, src[0], ctx->ac.i32);
928 break;
929 case nir_op_ifind_msb:
930 result = ac_build_imsb(&ctx->ac, src[0], ctx->ac.i32);
931 break;
932 case nir_op_uadd_carry:
933 result = emit_uint_carry(&ctx->ac, "llvm.uadd.with.overflow.i32", src[0], src[1]);
934 break;
935 case nir_op_usub_borrow:
936 result = emit_uint_carry(&ctx->ac, "llvm.usub.with.overflow.i32", src[0], src[1]);
937 break;
938 case nir_op_b2f16:
939 case nir_op_b2f32:
940 case nir_op_b2f64:
941 result = emit_b2f(&ctx->ac, src[0], instr->dest.dest.ssa.bit_size);
942 break;
943 case nir_op_f2b32:
944 result = emit_f2b(&ctx->ac, src[0]);
945 break;
946 case nir_op_b2i8:
947 case nir_op_b2i16:
948 case nir_op_b2i32:
949 case nir_op_b2i64:
950 result = emit_b2i(&ctx->ac, src[0], instr->dest.dest.ssa.bit_size);
951 break;
952 case nir_op_i2b32:
953 result = emit_i2b(&ctx->ac, src[0]);
954 break;
955 case nir_op_fquantize2f16:
956 result = emit_f2f16(&ctx->ac, src[0]);
957 break;
958 case nir_op_umul_high:
959 result = emit_umul_high(&ctx->ac, src[0], src[1]);
960 break;
961 case nir_op_imul_high:
962 result = emit_imul_high(&ctx->ac, src[0], src[1]);
963 break;
964 case nir_op_pack_half_2x16:
965 result = emit_pack_half_2x16(&ctx->ac, src[0]);
966 break;
967 case nir_op_unpack_half_2x16:
968 result = emit_unpack_half_2x16(&ctx->ac, src[0]);
969 break;
970 case nir_op_fddx:
971 case nir_op_fddy:
972 case nir_op_fddx_fine:
973 case nir_op_fddy_fine:
974 case nir_op_fddx_coarse:
975 case nir_op_fddy_coarse:
976 result = emit_ddxy(ctx, instr->op, src[0]);
977 break;
978
979 case nir_op_unpack_64_2x32_split_x: {
980 assert(ac_get_llvm_num_components(src[0]) == 1);
981 LLVMValueRef tmp = LLVMBuildBitCast(ctx->ac.builder, src[0],
982 ctx->ac.v2i32,
983 "");
984 result = LLVMBuildExtractElement(ctx->ac.builder, tmp,
985 ctx->ac.i32_0, "");
986 break;
987 }
988
989 case nir_op_unpack_64_2x32_split_y: {
990 assert(ac_get_llvm_num_components(src[0]) == 1);
991 LLVMValueRef tmp = LLVMBuildBitCast(ctx->ac.builder, src[0],
992 ctx->ac.v2i32,
993 "");
994 result = LLVMBuildExtractElement(ctx->ac.builder, tmp,
995 ctx->ac.i32_1, "");
996 break;
997 }
998
999 case nir_op_pack_64_2x32_split: {
1000 LLVMValueRef tmp = ac_build_gather_values(&ctx->ac, src, 2);
1001 result = LLVMBuildBitCast(ctx->ac.builder, tmp, ctx->ac.i64, "");
1002 break;
1003 }
1004
1005 case nir_op_pack_32_2x16_split: {
1006 LLVMValueRef tmp = ac_build_gather_values(&ctx->ac, src, 2);
1007 result = LLVMBuildBitCast(ctx->ac.builder, tmp, ctx->ac.i32, "");
1008 break;
1009 }
1010
1011 case nir_op_unpack_32_2x16_split_x: {
1012 LLVMValueRef tmp = LLVMBuildBitCast(ctx->ac.builder, src[0],
1013 ctx->ac.v2i16,
1014 "");
1015 result = LLVMBuildExtractElement(ctx->ac.builder, tmp,
1016 ctx->ac.i32_0, "");
1017 break;
1018 }
1019
1020 case nir_op_unpack_32_2x16_split_y: {
1021 LLVMValueRef tmp = LLVMBuildBitCast(ctx->ac.builder, src[0],
1022 ctx->ac.v2i16,
1023 "");
1024 result = LLVMBuildExtractElement(ctx->ac.builder, tmp,
1025 ctx->ac.i32_1, "");
1026 break;
1027 }
1028
1029 case nir_op_cube_face_coord: {
1030 src[0] = ac_to_float(&ctx->ac, src[0]);
1031 LLVMValueRef results[2];
1032 LLVMValueRef in[3];
1033 for (unsigned chan = 0; chan < 3; chan++)
1034 in[chan] = ac_llvm_extract_elem(&ctx->ac, src[0], chan);
1035 results[0] = ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.cubetc",
1036 ctx->ac.f32, in, 3, AC_FUNC_ATTR_READNONE);
1037 results[1] = ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.cubesc",
1038 ctx->ac.f32, in, 3, AC_FUNC_ATTR_READNONE);
1039 result = ac_build_gather_values(&ctx->ac, results, 2);
1040 break;
1041 }
1042
1043 case nir_op_cube_face_index: {
1044 src[0] = ac_to_float(&ctx->ac, src[0]);
1045 LLVMValueRef in[3];
1046 for (unsigned chan = 0; chan < 3; chan++)
1047 in[chan] = ac_llvm_extract_elem(&ctx->ac, src[0], chan);
1048 result = ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.cubeid",
1049 ctx->ac.f32, in, 3, AC_FUNC_ATTR_READNONE);
1050 break;
1051 }
1052
1053 case nir_op_fmin3:
1054 result = emit_intrin_2f_param(&ctx->ac, "llvm.minnum",
1055 ac_to_float_type(&ctx->ac, def_type), src[0], src[1]);
1056 result = emit_intrin_2f_param(&ctx->ac, "llvm.minnum",
1057 ac_to_float_type(&ctx->ac, def_type), result, src[2]);
1058 break;
1059 case nir_op_umin3:
1060 result = ac_build_umin(&ctx->ac, src[0], src[1]);
1061 result = ac_build_umin(&ctx->ac, result, src[2]);
1062 break;
1063 case nir_op_imin3:
1064 result = ac_build_imin(&ctx->ac, src[0], src[1]);
1065 result = ac_build_imin(&ctx->ac, result, src[2]);
1066 break;
1067 case nir_op_fmax3:
1068 result = emit_intrin_2f_param(&ctx->ac, "llvm.maxnum",
1069 ac_to_float_type(&ctx->ac, def_type), src[0], src[1]);
1070 result = emit_intrin_2f_param(&ctx->ac, "llvm.maxnum",
1071 ac_to_float_type(&ctx->ac, def_type), result, src[2]);
1072 break;
1073 case nir_op_umax3:
1074 result = ac_build_umax(&ctx->ac, src[0], src[1]);
1075 result = ac_build_umax(&ctx->ac, result, src[2]);
1076 break;
1077 case nir_op_imax3:
1078 result = ac_build_imax(&ctx->ac, src[0], src[1]);
1079 result = ac_build_imax(&ctx->ac, result, src[2]);
1080 break;
1081 case nir_op_fmed3: {
1082 src[0] = ac_to_float(&ctx->ac, src[0]);
1083 src[1] = ac_to_float(&ctx->ac, src[1]);
1084 src[2] = ac_to_float(&ctx->ac, src[2]);
1085 result = ac_build_fmed3(&ctx->ac, src[0], src[1], src[2],
1086 instr->dest.dest.ssa.bit_size);
1087 break;
1088 }
1089 case nir_op_imed3: {
1090 LLVMValueRef tmp1 = ac_build_imin(&ctx->ac, src[0], src[1]);
1091 LLVMValueRef tmp2 = ac_build_imax(&ctx->ac, src[0], src[1]);
1092 tmp2 = ac_build_imin(&ctx->ac, tmp2, src[2]);
1093 result = ac_build_imax(&ctx->ac, tmp1, tmp2);
1094 break;
1095 }
1096 case nir_op_umed3: {
1097 LLVMValueRef tmp1 = ac_build_umin(&ctx->ac, src[0], src[1]);
1098 LLVMValueRef tmp2 = ac_build_umax(&ctx->ac, src[0], src[1]);
1099 tmp2 = ac_build_umin(&ctx->ac, tmp2, src[2]);
1100 result = ac_build_umax(&ctx->ac, tmp1, tmp2);
1101 break;
1102 }
1103
1104 default:
1105 fprintf(stderr, "Unknown NIR alu instr: ");
1106 nir_print_instr(&instr->instr, stderr);
1107 fprintf(stderr, "\n");
1108 abort();
1109 }
1110
1111 if (result) {
1112 assert(instr->dest.dest.is_ssa);
1113 result = ac_to_integer_or_pointer(&ctx->ac, result);
1114 ctx->ssa_defs[instr->dest.dest.ssa.index] = result;
1115 }
1116 }
1117
1118 static void visit_load_const(struct ac_nir_context *ctx,
1119 const nir_load_const_instr *instr)
1120 {
1121 LLVMValueRef values[4], value = NULL;
1122 LLVMTypeRef element_type =
1123 LLVMIntTypeInContext(ctx->ac.context, instr->def.bit_size);
1124
1125 for (unsigned i = 0; i < instr->def.num_components; ++i) {
1126 switch (instr->def.bit_size) {
1127 case 8:
1128 values[i] = LLVMConstInt(element_type,
1129 instr->value[i].u8, false);
1130 break;
1131 case 16:
1132 values[i] = LLVMConstInt(element_type,
1133 instr->value[i].u16, false);
1134 break;
1135 case 32:
1136 values[i] = LLVMConstInt(element_type,
1137 instr->value[i].u32, false);
1138 break;
1139 case 64:
1140 values[i] = LLVMConstInt(element_type,
1141 instr->value[i].u64, false);
1142 break;
1143 default:
1144 fprintf(stderr,
1145 "unsupported nir load_const bit_size: %d\n",
1146 instr->def.bit_size);
1147 abort();
1148 }
1149 }
1150 if (instr->def.num_components > 1) {
1151 value = LLVMConstVector(values, instr->def.num_components);
1152 } else
1153 value = values[0];
1154
1155 ctx->ssa_defs[instr->def.index] = value;
1156 }
1157
1158 static LLVMValueRef
1159 get_buffer_size(struct ac_nir_context *ctx, LLVMValueRef descriptor, bool in_elements)
1160 {
1161 LLVMValueRef size =
1162 LLVMBuildExtractElement(ctx->ac.builder, descriptor,
1163 LLVMConstInt(ctx->ac.i32, 2, false), "");
1164
1165 /* VI only */
1166 if (ctx->ac.chip_class == VI && in_elements) {
1167 /* On VI, the descriptor contains the size in bytes,
1168 * but TXQ must return the size in elements.
1169 * The stride is always non-zero for resources using TXQ.
1170 */
1171 LLVMValueRef stride =
1172 LLVMBuildExtractElement(ctx->ac.builder, descriptor,
1173 ctx->ac.i32_1, "");
1174 stride = LLVMBuildLShr(ctx->ac.builder, stride,
1175 LLVMConstInt(ctx->ac.i32, 16, false), "");
1176 stride = LLVMBuildAnd(ctx->ac.builder, stride,
1177 LLVMConstInt(ctx->ac.i32, 0x3fff, false), "");
1178
1179 size = LLVMBuildUDiv(ctx->ac.builder, size, stride, "");
1180 }
1181 return size;
1182 }
1183
1184 static LLVMValueRef lower_gather4_integer(struct ac_llvm_context *ctx,
1185 nir_variable *var,
1186 struct ac_image_args *args,
1187 const nir_tex_instr *instr)
1188 {
1189 const struct glsl_type *type = glsl_without_array(var->type);
1190 enum glsl_base_type stype = glsl_get_sampler_result_type(type);
1191 LLVMValueRef half_texel[2];
1192 LLVMValueRef compare_cube_wa = NULL;
1193 LLVMValueRef result;
1194
1195 //TODO Rect
1196 {
1197 struct ac_image_args txq_args = { 0 };
1198
1199 txq_args.dim = get_ac_sampler_dim(ctx, instr->sampler_dim, instr->is_array);
1200 txq_args.opcode = ac_image_get_resinfo;
1201 txq_args.dmask = 0xf;
1202 txq_args.lod = ctx->i32_0;
1203 txq_args.resource = args->resource;
1204 txq_args.attributes = AC_FUNC_ATTR_READNONE;
1205 LLVMValueRef size = ac_build_image_opcode(ctx, &txq_args);
1206
1207 for (unsigned c = 0; c < 2; c++) {
1208 half_texel[c] = LLVMBuildExtractElement(ctx->builder, size,
1209 LLVMConstInt(ctx->i32, c, false), "");
1210 half_texel[c] = LLVMBuildUIToFP(ctx->builder, half_texel[c], ctx->f32, "");
1211 half_texel[c] = ac_build_fdiv(ctx, ctx->f32_1, half_texel[c]);
1212 half_texel[c] = LLVMBuildFMul(ctx->builder, half_texel[c],
1213 LLVMConstReal(ctx->f32, -0.5), "");
1214 }
1215 }
1216
1217 LLVMValueRef orig_coords[2] = { args->coords[0], args->coords[1] };
1218
1219 for (unsigned c = 0; c < 2; c++) {
1220 LLVMValueRef tmp;
1221 tmp = LLVMBuildBitCast(ctx->builder, args->coords[c], ctx->f32, "");
1222 args->coords[c] = LLVMBuildFAdd(ctx->builder, tmp, half_texel[c], "");
1223 }
1224
1225 /*
1226 * Apparantly cube has issue with integer types that the workaround doesn't solve,
1227 * so this tests if the format is 8_8_8_8 and an integer type do an alternate
1228 * workaround by sampling using a scaled type and converting.
1229 * This is taken from amdgpu-pro shaders.
1230 */
1231 /* NOTE this produces some ugly code compared to amdgpu-pro,
1232 * LLVM ends up dumping SGPRs into VGPRs to deal with the compare/select,
1233 * and then reads them back. -pro generates two selects,
1234 * one s_cmp for the descriptor rewriting
1235 * one v_cmp for the coordinate and result changes.
1236 */
1237 if (instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE) {
1238 LLVMValueRef tmp, tmp2;
1239
1240 /* workaround 8/8/8/8 uint/sint cube gather bug */
1241 /* first detect it then change to a scaled read and f2i */
1242 tmp = LLVMBuildExtractElement(ctx->builder, args->resource, ctx->i32_1, "");
1243 tmp2 = tmp;
1244
1245 /* extract the DATA_FORMAT */
1246 tmp = ac_build_bfe(ctx, tmp, LLVMConstInt(ctx->i32, 20, false),
1247 LLVMConstInt(ctx->i32, 6, false), false);
1248
1249 /* is the DATA_FORMAT == 8_8_8_8 */
1250 compare_cube_wa = LLVMBuildICmp(ctx->builder, LLVMIntEQ, tmp, LLVMConstInt(ctx->i32, V_008F14_IMG_DATA_FORMAT_8_8_8_8, false), "");
1251
1252 if (stype == GLSL_TYPE_UINT)
1253 /* Create a NUM FORMAT - 0x2 or 0x4 - USCALED or UINT */
1254 tmp = LLVMBuildSelect(ctx->builder, compare_cube_wa, LLVMConstInt(ctx->i32, 0x8000000, false),
1255 LLVMConstInt(ctx->i32, 0x10000000, false), "");
1256 else
1257 /* Create a NUM FORMAT - 0x3 or 0x5 - SSCALED or SINT */
1258 tmp = LLVMBuildSelect(ctx->builder, compare_cube_wa, LLVMConstInt(ctx->i32, 0xc000000, false),
1259 LLVMConstInt(ctx->i32, 0x14000000, false), "");
1260
1261 /* replace the NUM FORMAT in the descriptor */
1262 tmp2 = LLVMBuildAnd(ctx->builder, tmp2, LLVMConstInt(ctx->i32, C_008F14_NUM_FORMAT_GFX6, false), "");
1263 tmp2 = LLVMBuildOr(ctx->builder, tmp2, tmp, "");
1264
1265 args->resource = LLVMBuildInsertElement(ctx->builder, args->resource, tmp2, ctx->i32_1, "");
1266
1267 /* don't modify the coordinates for this case */
1268 for (unsigned c = 0; c < 2; ++c)
1269 args->coords[c] = LLVMBuildSelect(
1270 ctx->builder, compare_cube_wa,
1271 orig_coords[c], args->coords[c], "");
1272 }
1273
1274 args->attributes = AC_FUNC_ATTR_READNONE;
1275 result = ac_build_image_opcode(ctx, args);
1276
1277 if (instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE) {
1278 LLVMValueRef tmp, tmp2;
1279
1280 /* if the cube workaround is in place, f2i the result. */
1281 for (unsigned c = 0; c < 4; c++) {
1282 tmp = LLVMBuildExtractElement(ctx->builder, result, LLVMConstInt(ctx->i32, c, false), "");
1283 if (stype == GLSL_TYPE_UINT)
1284 tmp2 = LLVMBuildFPToUI(ctx->builder, tmp, ctx->i32, "");
1285 else
1286 tmp2 = LLVMBuildFPToSI(ctx->builder, tmp, ctx->i32, "");
1287 tmp = LLVMBuildBitCast(ctx->builder, tmp, ctx->i32, "");
1288 tmp2 = LLVMBuildBitCast(ctx->builder, tmp2, ctx->i32, "");
1289 tmp = LLVMBuildSelect(ctx->builder, compare_cube_wa, tmp2, tmp, "");
1290 tmp = LLVMBuildBitCast(ctx->builder, tmp, ctx->f32, "");
1291 result = LLVMBuildInsertElement(ctx->builder, result, tmp, LLVMConstInt(ctx->i32, c, false), "");
1292 }
1293 }
1294 return result;
1295 }
1296
1297 static nir_deref_instr *get_tex_texture_deref(const nir_tex_instr *instr)
1298 {
1299 nir_deref_instr *texture_deref_instr = NULL;
1300
1301 for (unsigned i = 0; i < instr->num_srcs; i++) {
1302 switch (instr->src[i].src_type) {
1303 case nir_tex_src_texture_deref:
1304 texture_deref_instr = nir_src_as_deref(instr->src[i].src);
1305 break;
1306 default:
1307 break;
1308 }
1309 }
1310 return texture_deref_instr;
1311 }
1312
1313 static LLVMValueRef build_tex_intrinsic(struct ac_nir_context *ctx,
1314 const nir_tex_instr *instr,
1315 struct ac_image_args *args)
1316 {
1317 if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF) {
1318 unsigned mask = nir_ssa_def_components_read(&instr->dest.ssa);
1319
1320 if (ctx->abi->gfx9_stride_size_workaround) {
1321 return ac_build_buffer_load_format_gfx9_safe(&ctx->ac,
1322 args->resource,
1323 args->coords[0],
1324 ctx->ac.i32_0,
1325 util_last_bit(mask),
1326 false, true);
1327 } else {
1328 return ac_build_buffer_load_format(&ctx->ac,
1329 args->resource,
1330 args->coords[0],
1331 ctx->ac.i32_0,
1332 util_last_bit(mask),
1333 false, true);
1334 }
1335 }
1336
1337 args->opcode = ac_image_sample;
1338
1339 switch (instr->op) {
1340 case nir_texop_txf:
1341 case nir_texop_txf_ms:
1342 case nir_texop_samples_identical:
1343 args->opcode = args->level_zero ||
1344 instr->sampler_dim == GLSL_SAMPLER_DIM_MS ?
1345 ac_image_load : ac_image_load_mip;
1346 args->level_zero = false;
1347 break;
1348 case nir_texop_txs:
1349 case nir_texop_query_levels:
1350 args->opcode = ac_image_get_resinfo;
1351 if (!args->lod)
1352 args->lod = ctx->ac.i32_0;
1353 args->level_zero = false;
1354 break;
1355 case nir_texop_tex:
1356 if (ctx->stage != MESA_SHADER_FRAGMENT) {
1357 assert(!args->lod);
1358 args->level_zero = true;
1359 }
1360 break;
1361 case nir_texop_tg4:
1362 args->opcode = ac_image_gather4;
1363 args->level_zero = true;
1364 break;
1365 case nir_texop_lod:
1366 args->opcode = ac_image_get_lod;
1367 break;
1368 default:
1369 break;
1370 }
1371
1372 if (instr->op == nir_texop_tg4 && ctx->ac.chip_class <= VI) {
1373 nir_deref_instr *texture_deref_instr = get_tex_texture_deref(instr);
1374 nir_variable *var = nir_deref_instr_get_variable(texture_deref_instr);
1375 const struct glsl_type *type = glsl_without_array(var->type);
1376 enum glsl_base_type stype = glsl_get_sampler_result_type(type);
1377 if (stype == GLSL_TYPE_UINT || stype == GLSL_TYPE_INT) {
1378 return lower_gather4_integer(&ctx->ac, var, args, instr);
1379 }
1380 }
1381
1382 /* Fixup for GFX9 which allocates 1D textures as 2D. */
1383 if (instr->op == nir_texop_lod && ctx->ac.chip_class >= GFX9) {
1384 if ((args->dim == ac_image_2darray ||
1385 args->dim == ac_image_2d) && !args->coords[1]) {
1386 args->coords[1] = ctx->ac.i32_0;
1387 }
1388 }
1389
1390 args->attributes = AC_FUNC_ATTR_READNONE;
1391 return ac_build_image_opcode(&ctx->ac, args);
1392 }
1393
1394 static LLVMValueRef visit_vulkan_resource_reindex(struct ac_nir_context *ctx,
1395 nir_intrinsic_instr *instr)
1396 {
1397 LLVMValueRef ptr = get_src(ctx, instr->src[0]);
1398 LLVMValueRef index = get_src(ctx, instr->src[1]);
1399
1400 LLVMValueRef result = LLVMBuildGEP(ctx->ac.builder, ptr, &index, 1, "");
1401 LLVMSetMetadata(result, ctx->ac.uniform_md_kind, ctx->ac.empty_md);
1402 return result;
1403 }
1404
1405 static LLVMValueRef visit_load_push_constant(struct ac_nir_context *ctx,
1406 nir_intrinsic_instr *instr)
1407 {
1408 LLVMValueRef ptr, addr;
1409 LLVMValueRef src0 = get_src(ctx, instr->src[0]);
1410 unsigned index = nir_intrinsic_base(instr);
1411
1412 addr = LLVMConstInt(ctx->ac.i32, index, 0);
1413 addr = LLVMBuildAdd(ctx->ac.builder, addr, src0, "");
1414
1415 /* Load constant values from user SGPRS when possible, otherwise
1416 * fallback to the default path that loads directly from memory.
1417 */
1418 if (LLVMIsConstant(src0) &&
1419 instr->dest.ssa.bit_size == 32) {
1420 unsigned count = instr->dest.ssa.num_components;
1421 unsigned offset = index;
1422
1423 offset += LLVMConstIntGetZExtValue(src0);
1424 offset /= 4;
1425
1426 offset -= ctx->abi->base_inline_push_consts;
1427
1428 if (offset + count <= ctx->abi->num_inline_push_consts) {
1429 return ac_build_gather_values(&ctx->ac,
1430 ctx->abi->inline_push_consts + offset,
1431 count);
1432 }
1433 }
1434
1435 ptr = ac_build_gep0(&ctx->ac, ctx->abi->push_constants, addr);
1436
1437 if (instr->dest.ssa.bit_size == 8) {
1438 unsigned load_dwords = instr->dest.ssa.num_components > 1 ? 2 : 1;
1439 LLVMTypeRef vec_type = LLVMVectorType(LLVMInt8TypeInContext(ctx->ac.context), 4 * load_dwords);
1440 ptr = ac_cast_ptr(&ctx->ac, ptr, vec_type);
1441 LLVMValueRef res = LLVMBuildLoad(ctx->ac.builder, ptr, "");
1442
1443 LLVMValueRef params[3];
1444 if (load_dwords > 1) {
1445 LLVMValueRef res_vec = LLVMBuildBitCast(ctx->ac.builder, res, LLVMVectorType(ctx->ac.i32, 2), "");
1446 params[0] = LLVMBuildExtractElement(ctx->ac.builder, res_vec, LLVMConstInt(ctx->ac.i32, 1, false), "");
1447 params[1] = LLVMBuildExtractElement(ctx->ac.builder, res_vec, LLVMConstInt(ctx->ac.i32, 0, false), "");
1448 } else {
1449 res = LLVMBuildBitCast(ctx->ac.builder, res, ctx->ac.i32, "");
1450 params[0] = ctx->ac.i32_0;
1451 params[1] = res;
1452 }
1453 params[2] = addr;
1454 res = ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.alignbyte", ctx->ac.i32, params, 3, 0);
1455
1456 res = LLVMBuildTrunc(ctx->ac.builder, res, LLVMIntTypeInContext(ctx->ac.context, instr->dest.ssa.num_components * 8), "");
1457 if (instr->dest.ssa.num_components > 1)
1458 res = LLVMBuildBitCast(ctx->ac.builder, res, LLVMVectorType(LLVMInt8TypeInContext(ctx->ac.context), instr->dest.ssa.num_components), "");
1459 return res;
1460 } else if (instr->dest.ssa.bit_size == 16) {
1461 unsigned load_dwords = instr->dest.ssa.num_components / 2 + 1;
1462 LLVMTypeRef vec_type = LLVMVectorType(LLVMInt16TypeInContext(ctx->ac.context), 2 * load_dwords);
1463 ptr = ac_cast_ptr(&ctx->ac, ptr, vec_type);
1464 LLVMValueRef res = LLVMBuildLoad(ctx->ac.builder, ptr, "");
1465 res = LLVMBuildBitCast(ctx->ac.builder, res, vec_type, "");
1466 LLVMValueRef cond = LLVMBuildLShr(ctx->ac.builder, addr, ctx->ac.i32_1, "");
1467 cond = LLVMBuildTrunc(ctx->ac.builder, cond, ctx->ac.i1, "");
1468 LLVMValueRef mask[] = { LLVMConstInt(ctx->ac.i32, 0, false), LLVMConstInt(ctx->ac.i32, 1, false),
1469 LLVMConstInt(ctx->ac.i32, 2, false), LLVMConstInt(ctx->ac.i32, 3, false),
1470 LLVMConstInt(ctx->ac.i32, 4, false)};
1471 LLVMValueRef swizzle_aligned = LLVMConstVector(&mask[0], instr->dest.ssa.num_components);
1472 LLVMValueRef swizzle_unaligned = LLVMConstVector(&mask[1], instr->dest.ssa.num_components);
1473 LLVMValueRef shuffle_aligned = LLVMBuildShuffleVector(ctx->ac.builder, res, res, swizzle_aligned, "");
1474 LLVMValueRef shuffle_unaligned = LLVMBuildShuffleVector(ctx->ac.builder, res, res, swizzle_unaligned, "");
1475 res = LLVMBuildSelect(ctx->ac.builder, cond, shuffle_unaligned, shuffle_aligned, "");
1476 return LLVMBuildBitCast(ctx->ac.builder, res, get_def_type(ctx, &instr->dest.ssa), "");
1477 }
1478
1479 ptr = ac_cast_ptr(&ctx->ac, ptr, get_def_type(ctx, &instr->dest.ssa));
1480
1481 return LLVMBuildLoad(ctx->ac.builder, ptr, "");
1482 }
1483
1484 static LLVMValueRef visit_get_buffer_size(struct ac_nir_context *ctx,
1485 const nir_intrinsic_instr *instr)
1486 {
1487 LLVMValueRef index = get_src(ctx, instr->src[0]);
1488
1489 return get_buffer_size(ctx, ctx->abi->load_ssbo(ctx->abi, index, false), false);
1490 }
1491
1492 static uint32_t widen_mask(uint32_t mask, unsigned multiplier)
1493 {
1494 uint32_t new_mask = 0;
1495 for(unsigned i = 0; i < 32 && (1u << i) <= mask; ++i)
1496 if (mask & (1u << i))
1497 new_mask |= ((1u << multiplier) - 1u) << (i * multiplier);
1498 return new_mask;
1499 }
1500
1501 static LLVMValueRef extract_vector_range(struct ac_llvm_context *ctx, LLVMValueRef src,
1502 unsigned start, unsigned count)
1503 {
1504 LLVMValueRef mask[] = {
1505 ctx->i32_0, ctx->i32_1,
1506 LLVMConstInt(ctx->i32, 2, false), LLVMConstInt(ctx->i32, 3, false) };
1507
1508 unsigned src_elements = ac_get_llvm_num_components(src);
1509
1510 if (count == src_elements) {
1511 assert(start == 0);
1512 return src;
1513 } else if (count == 1) {
1514 assert(start < src_elements);
1515 return LLVMBuildExtractElement(ctx->builder, src, mask[start], "");
1516 } else {
1517 assert(start + count <= src_elements);
1518 assert(count <= 4);
1519 LLVMValueRef swizzle = LLVMConstVector(&mask[start], count);
1520 return LLVMBuildShuffleVector(ctx->builder, src, src, swizzle, "");
1521 }
1522 }
1523
1524 static unsigned get_cache_policy(struct ac_nir_context *ctx,
1525 enum gl_access_qualifier access,
1526 bool may_store_unaligned,
1527 bool writeonly_memory)
1528 {
1529 unsigned cache_policy = 0;
1530
1531 /* SI has a TC L1 bug causing corruption of 8bit/16bit stores. All
1532 * store opcodes not aligned to a dword are affected. The only way to
1533 * get unaligned stores is through shader images.
1534 */
1535 if (((may_store_unaligned && ctx->ac.chip_class == SI) ||
1536 /* If this is write-only, don't keep data in L1 to prevent
1537 * evicting L1 cache lines that may be needed by other
1538 * instructions.
1539 */
1540 writeonly_memory ||
1541 access & (ACCESS_COHERENT | ACCESS_VOLATILE))) {
1542 cache_policy |= ac_glc;
1543 }
1544
1545 return cache_policy;
1546 }
1547
1548 static void visit_store_ssbo(struct ac_nir_context *ctx,
1549 nir_intrinsic_instr *instr)
1550 {
1551 LLVMValueRef src_data = get_src(ctx, instr->src[0]);
1552 int elem_size_bytes = ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src_data)) / 8;
1553 unsigned writemask = nir_intrinsic_write_mask(instr);
1554 enum gl_access_qualifier access = nir_intrinsic_access(instr);
1555 bool writeonly_memory = access & ACCESS_NON_READABLE;
1556 unsigned cache_policy = get_cache_policy(ctx, access, false, writeonly_memory);
1557
1558 LLVMValueRef rsrc = ctx->abi->load_ssbo(ctx->abi,
1559 get_src(ctx, instr->src[1]), true);
1560 LLVMValueRef base_data = src_data;
1561 base_data = ac_trim_vector(&ctx->ac, base_data, instr->num_components);
1562 LLVMValueRef base_offset = get_src(ctx, instr->src[2]);
1563
1564 while (writemask) {
1565 int start, count;
1566 LLVMValueRef data, offset;
1567 LLVMTypeRef data_type;
1568
1569 u_bit_scan_consecutive_range(&writemask, &start, &count);
1570
1571 /* Due to an LLVM limitation, split 3-element writes
1572 * into a 2-element and a 1-element write. */
1573 if (count == 3) {
1574 writemask |= 1 << (start + 2);
1575 count = 2;
1576 }
1577 int num_bytes = count * elem_size_bytes; /* count in bytes */
1578
1579 /* we can only store 4 DWords at the same time.
1580 * can only happen for 64 Bit vectors. */
1581 if (num_bytes > 16) {
1582 writemask |= ((1u << (count - 2)) - 1u) << (start + 2);
1583 count = 2;
1584 num_bytes = 16;
1585 }
1586
1587 /* check alignment of 16 Bit stores */
1588 if (elem_size_bytes == 2 && num_bytes > 2 && (start % 2) == 1) {
1589 writemask |= ((1u << (count - 1)) - 1u) << (start + 1);
1590 count = 1;
1591 num_bytes = 2;
1592 }
1593 data = extract_vector_range(&ctx->ac, base_data, start, count);
1594
1595 offset = LLVMBuildAdd(ctx->ac.builder, base_offset,
1596 LLVMConstInt(ctx->ac.i32, start * elem_size_bytes, false), "");
1597
1598 if (num_bytes == 1) {
1599 ac_build_tbuffer_store_byte(&ctx->ac, rsrc, data,
1600 offset, ctx->ac.i32_0,
1601 cache_policy & ac_glc,
1602 writeonly_memory);
1603 } else if (num_bytes == 2) {
1604 ac_build_tbuffer_store_short(&ctx->ac, rsrc, data,
1605 offset, ctx->ac.i32_0,
1606 cache_policy & ac_glc,
1607 writeonly_memory);
1608 } else {
1609 int num_channels = num_bytes / 4;
1610
1611 switch (num_bytes) {
1612 case 16: /* v4f32 */
1613 data_type = ctx->ac.v4f32;
1614 break;
1615 case 8: /* v2f32 */
1616 data_type = ctx->ac.v2f32;
1617 break;
1618 case 4: /* f32 */
1619 data_type = ctx->ac.f32;
1620 break;
1621 default:
1622 unreachable("Malformed vector store.");
1623 }
1624 data = LLVMBuildBitCast(ctx->ac.builder, data, data_type, "");
1625
1626 ac_build_buffer_store_dword(&ctx->ac, rsrc, data,
1627 num_channels, offset,
1628 ctx->ac.i32_0, 0,
1629 cache_policy & ac_glc,
1630 false, writeonly_memory,
1631 false);
1632 }
1633 }
1634 }
1635
1636 static LLVMValueRef visit_atomic_ssbo(struct ac_nir_context *ctx,
1637 const nir_intrinsic_instr *instr)
1638 {
1639 const char *op;
1640 char name[64];
1641 LLVMValueRef params[6];
1642 int arg_count = 0;
1643
1644 if (instr->intrinsic == nir_intrinsic_ssbo_atomic_comp_swap) {
1645 params[arg_count++] = ac_llvm_extract_elem(&ctx->ac, get_src(ctx, instr->src[3]), 0);
1646 }
1647 params[arg_count++] = ac_llvm_extract_elem(&ctx->ac, get_src(ctx, instr->src[2]), 0);
1648 params[arg_count++] = ctx->abi->load_ssbo(ctx->abi,
1649 get_src(ctx, instr->src[0]),
1650 true);
1651 params[arg_count++] = ctx->ac.i32_0; /* vindex */
1652 params[arg_count++] = get_src(ctx, instr->src[1]); /* voffset */
1653 params[arg_count++] = ctx->ac.i1false; /* slc */
1654
1655 switch (instr->intrinsic) {
1656 case nir_intrinsic_ssbo_atomic_add:
1657 op = "add";
1658 break;
1659 case nir_intrinsic_ssbo_atomic_imin:
1660 op = "smin";
1661 break;
1662 case nir_intrinsic_ssbo_atomic_umin:
1663 op = "umin";
1664 break;
1665 case nir_intrinsic_ssbo_atomic_imax:
1666 op = "smax";
1667 break;
1668 case nir_intrinsic_ssbo_atomic_umax:
1669 op = "umax";
1670 break;
1671 case nir_intrinsic_ssbo_atomic_and:
1672 op = "and";
1673 break;
1674 case nir_intrinsic_ssbo_atomic_or:
1675 op = "or";
1676 break;
1677 case nir_intrinsic_ssbo_atomic_xor:
1678 op = "xor";
1679 break;
1680 case nir_intrinsic_ssbo_atomic_exchange:
1681 op = "swap";
1682 break;
1683 case nir_intrinsic_ssbo_atomic_comp_swap:
1684 op = "cmpswap";
1685 break;
1686 default:
1687 abort();
1688 }
1689
1690 if (HAVE_LLVM >= 0x900 &&
1691 instr->intrinsic != nir_intrinsic_ssbo_atomic_comp_swap) {
1692 snprintf(name, sizeof(name),
1693 "llvm.amdgcn.buffer.atomic.%s.i32", op);
1694 } else {
1695 snprintf(name, sizeof(name),
1696 "llvm.amdgcn.buffer.atomic.%s", op);
1697 }
1698
1699 return ac_build_intrinsic(&ctx->ac, name, ctx->ac.i32, params, arg_count, 0);
1700 }
1701
1702 static LLVMValueRef visit_load_buffer(struct ac_nir_context *ctx,
1703 const nir_intrinsic_instr *instr)
1704 {
1705 int elem_size_bytes = instr->dest.ssa.bit_size / 8;
1706 int num_components = instr->num_components;
1707 enum gl_access_qualifier access = nir_intrinsic_access(instr);
1708 unsigned cache_policy = get_cache_policy(ctx, access, false, false);
1709
1710 LLVMValueRef offset = get_src(ctx, instr->src[1]);
1711 LLVMValueRef rsrc = ctx->abi->load_ssbo(ctx->abi,
1712 get_src(ctx, instr->src[0]), false);
1713 LLVMValueRef vindex = ctx->ac.i32_0;
1714
1715 LLVMTypeRef def_type = get_def_type(ctx, &instr->dest.ssa);
1716 LLVMTypeRef def_elem_type = num_components > 1 ? LLVMGetElementType(def_type) : def_type;
1717
1718 LLVMValueRef results[4];
1719 for (int i = 0; i < num_components;) {
1720 int num_elems = num_components - i;
1721 if (elem_size_bytes < 4 && nir_intrinsic_align(instr) % 4 != 0)
1722 num_elems = 1;
1723 if (num_elems * elem_size_bytes > 16)
1724 num_elems = 16 / elem_size_bytes;
1725 int load_bytes = num_elems * elem_size_bytes;
1726
1727 LLVMValueRef immoffset = LLVMConstInt(ctx->ac.i32, i * elem_size_bytes, false);
1728
1729 LLVMValueRef ret;
1730
1731 if (load_bytes == 1) {
1732 ret = ac_build_tbuffer_load_byte(&ctx->ac,
1733 rsrc,
1734 offset,
1735 ctx->ac.i32_0,
1736 immoffset,
1737 cache_policy & ac_glc);
1738 } else if (load_bytes == 2) {
1739 ret = ac_build_tbuffer_load_short(&ctx->ac,
1740 rsrc,
1741 offset,
1742 ctx->ac.i32_0,
1743 immoffset,
1744 cache_policy & ac_glc);
1745 } else {
1746 int num_channels = util_next_power_of_two(load_bytes) / 4;
1747
1748 ret = ac_build_buffer_load(&ctx->ac, rsrc, num_channels,
1749 vindex, offset, immoffset, 0,
1750 cache_policy & ac_glc, 0,
1751 false, false);
1752 }
1753
1754 LLVMTypeRef byte_vec = LLVMVectorType(ctx->ac.i8, ac_get_type_size(LLVMTypeOf(ret)));
1755 ret = LLVMBuildBitCast(ctx->ac.builder, ret, byte_vec, "");
1756 ret = ac_trim_vector(&ctx->ac, ret, load_bytes);
1757
1758 LLVMTypeRef ret_type = LLVMVectorType(def_elem_type, num_elems);
1759 ret = LLVMBuildBitCast(ctx->ac.builder, ret, ret_type, "");
1760
1761 for (unsigned j = 0; j < num_elems; j++) {
1762 results[i + j] = LLVMBuildExtractElement(ctx->ac.builder, ret, LLVMConstInt(ctx->ac.i32, j, false), "");
1763 }
1764 i += num_elems;
1765 }
1766
1767 return ac_build_gather_values(&ctx->ac, results, num_components);
1768 }
1769
1770 static LLVMValueRef visit_load_ubo_buffer(struct ac_nir_context *ctx,
1771 const nir_intrinsic_instr *instr)
1772 {
1773 LLVMValueRef ret;
1774 LLVMValueRef rsrc = get_src(ctx, instr->src[0]);
1775 LLVMValueRef offset = get_src(ctx, instr->src[1]);
1776 int num_components = instr->num_components;
1777
1778 if (ctx->abi->load_ubo)
1779 rsrc = ctx->abi->load_ubo(ctx->abi, rsrc);
1780
1781 if (instr->dest.ssa.bit_size == 64)
1782 num_components *= 2;
1783
1784 if (instr->dest.ssa.bit_size == 16 || instr->dest.ssa.bit_size == 8) {
1785 unsigned load_bytes = instr->dest.ssa.bit_size / 8;
1786 LLVMValueRef results[num_components];
1787 for (unsigned i = 0; i < num_components; ++i) {
1788 LLVMValueRef immoffset = LLVMConstInt(ctx->ac.i32,
1789 load_bytes * i, 0);
1790
1791 if (load_bytes == 1) {
1792 results[i] = ac_build_tbuffer_load_byte(&ctx->ac,
1793 rsrc,
1794 offset,
1795 ctx->ac.i32_0,
1796 immoffset,
1797 false);
1798 } else {
1799 assert(load_bytes == 2);
1800 results[i] = ac_build_tbuffer_load_short(&ctx->ac,
1801 rsrc,
1802 offset,
1803 ctx->ac.i32_0,
1804 immoffset,
1805 false);
1806 }
1807 }
1808 ret = ac_build_gather_values(&ctx->ac, results, num_components);
1809 } else {
1810 ret = ac_build_buffer_load(&ctx->ac, rsrc, num_components, NULL, offset,
1811 NULL, 0, false, false, true, true);
1812
1813 ret = ac_trim_vector(&ctx->ac, ret, num_components);
1814 }
1815
1816 return LLVMBuildBitCast(ctx->ac.builder, ret,
1817 get_def_type(ctx, &instr->dest.ssa), "");
1818 }
1819
1820 static void
1821 get_deref_offset(struct ac_nir_context *ctx, nir_deref_instr *instr,
1822 bool vs_in, unsigned *vertex_index_out,
1823 LLVMValueRef *vertex_index_ref,
1824 unsigned *const_out, LLVMValueRef *indir_out)
1825 {
1826 nir_variable *var = nir_deref_instr_get_variable(instr);
1827 nir_deref_path path;
1828 unsigned idx_lvl = 1;
1829
1830 nir_deref_path_init(&path, instr, NULL);
1831
1832 if (vertex_index_out != NULL || vertex_index_ref != NULL) {
1833 if (vertex_index_ref) {
1834 *vertex_index_ref = get_src(ctx, path.path[idx_lvl]->arr.index);
1835 if (vertex_index_out)
1836 *vertex_index_out = 0;
1837 } else {
1838 *vertex_index_out = nir_src_as_uint(path.path[idx_lvl]->arr.index);
1839 }
1840 ++idx_lvl;
1841 }
1842
1843 uint32_t const_offset = 0;
1844 LLVMValueRef offset = NULL;
1845
1846 if (var->data.compact) {
1847 assert(instr->deref_type == nir_deref_type_array);
1848 const_offset = nir_src_as_uint(instr->arr.index);
1849 goto out;
1850 }
1851
1852 for (; path.path[idx_lvl]; ++idx_lvl) {
1853 const struct glsl_type *parent_type = path.path[idx_lvl - 1]->type;
1854 if (path.path[idx_lvl]->deref_type == nir_deref_type_struct) {
1855 unsigned index = path.path[idx_lvl]->strct.index;
1856
1857 for (unsigned i = 0; i < index; i++) {
1858 const struct glsl_type *ft = glsl_get_struct_field(parent_type, i);
1859 const_offset += glsl_count_attribute_slots(ft, vs_in);
1860 }
1861 } else if(path.path[idx_lvl]->deref_type == nir_deref_type_array) {
1862 unsigned size = glsl_count_attribute_slots(path.path[idx_lvl]->type, vs_in);
1863 LLVMValueRef array_off = LLVMBuildMul(ctx->ac.builder, LLVMConstInt(ctx->ac.i32, size, 0),
1864 get_src(ctx, path.path[idx_lvl]->arr.index), "");
1865 if (offset)
1866 offset = LLVMBuildAdd(ctx->ac.builder, offset, array_off, "");
1867 else
1868 offset = array_off;
1869 } else
1870 unreachable("Uhandled deref type in get_deref_instr_offset");
1871 }
1872
1873 out:
1874 nir_deref_path_finish(&path);
1875
1876 if (const_offset && offset)
1877 offset = LLVMBuildAdd(ctx->ac.builder, offset,
1878 LLVMConstInt(ctx->ac.i32, const_offset, 0),
1879 "");
1880
1881 *const_out = const_offset;
1882 *indir_out = offset;
1883 }
1884
1885 static LLVMValueRef load_tess_varyings(struct ac_nir_context *ctx,
1886 nir_intrinsic_instr *instr,
1887 bool load_inputs)
1888 {
1889 LLVMValueRef result;
1890 LLVMValueRef vertex_index = NULL;
1891 LLVMValueRef indir_index = NULL;
1892 unsigned const_index = 0;
1893
1894 nir_variable *var = nir_deref_instr_get_variable(nir_instr_as_deref(instr->src[0].ssa->parent_instr));
1895
1896 unsigned location = var->data.location;
1897 unsigned driver_location = var->data.driver_location;
1898 const bool is_patch = var->data.patch;
1899 const bool is_compact = var->data.compact;
1900
1901 get_deref_offset(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr),
1902 false, NULL, is_patch ? NULL : &vertex_index,
1903 &const_index, &indir_index);
1904
1905 LLVMTypeRef dest_type = get_def_type(ctx, &instr->dest.ssa);
1906
1907 LLVMTypeRef src_component_type;
1908 if (LLVMGetTypeKind(dest_type) == LLVMVectorTypeKind)
1909 src_component_type = LLVMGetElementType(dest_type);
1910 else
1911 src_component_type = dest_type;
1912
1913 result = ctx->abi->load_tess_varyings(ctx->abi, src_component_type,
1914 vertex_index, indir_index,
1915 const_index, location, driver_location,
1916 var->data.location_frac,
1917 instr->num_components,
1918 is_patch, is_compact, load_inputs);
1919 if (instr->dest.ssa.bit_size == 16) {
1920 result = ac_to_integer(&ctx->ac, result);
1921 result = LLVMBuildTrunc(ctx->ac.builder, result, dest_type, "");
1922 }
1923 return LLVMBuildBitCast(ctx->ac.builder, result, dest_type, "");
1924 }
1925
1926 static unsigned
1927 type_scalar_size_bytes(const struct glsl_type *type)
1928 {
1929 assert(glsl_type_is_vector_or_scalar(type) ||
1930 glsl_type_is_matrix(type));
1931 return glsl_type_is_boolean(type) ? 4 : glsl_get_bit_size(type) / 8;
1932 }
1933
1934 static LLVMValueRef visit_load_var(struct ac_nir_context *ctx,
1935 nir_intrinsic_instr *instr)
1936 {
1937 nir_deref_instr *deref = nir_instr_as_deref(instr->src[0].ssa->parent_instr);
1938 nir_variable *var = nir_deref_instr_get_variable(deref);
1939
1940 LLVMValueRef values[8];
1941 int idx = 0;
1942 int ve = instr->dest.ssa.num_components;
1943 unsigned comp = 0;
1944 LLVMValueRef indir_index;
1945 LLVMValueRef ret;
1946 unsigned const_index;
1947 unsigned stride = 4;
1948 int mode = deref->mode;
1949
1950 if (var) {
1951 bool vs_in = ctx->stage == MESA_SHADER_VERTEX &&
1952 var->data.mode == nir_var_shader_in;
1953 idx = var->data.driver_location;
1954 comp = var->data.location_frac;
1955 mode = var->data.mode;
1956
1957 get_deref_offset(ctx, deref, vs_in, NULL, NULL,
1958 &const_index, &indir_index);
1959
1960 if (var->data.compact) {
1961 stride = 1;
1962 const_index += comp;
1963 comp = 0;
1964 }
1965 }
1966
1967 if (instr->dest.ssa.bit_size == 64 &&
1968 (deref->mode == nir_var_shader_in ||
1969 deref->mode == nir_var_shader_out ||
1970 deref->mode == nir_var_function_temp))
1971 ve *= 2;
1972
1973 switch (mode) {
1974 case nir_var_shader_in:
1975 if (ctx->stage == MESA_SHADER_TESS_CTRL ||
1976 ctx->stage == MESA_SHADER_TESS_EVAL) {
1977 return load_tess_varyings(ctx, instr, true);
1978 }
1979
1980 if (ctx->stage == MESA_SHADER_GEOMETRY) {
1981 LLVMTypeRef type = LLVMIntTypeInContext(ctx->ac.context, instr->dest.ssa.bit_size);
1982 LLVMValueRef indir_index;
1983 unsigned const_index, vertex_index;
1984 get_deref_offset(ctx, deref, false, &vertex_index, NULL,
1985 &const_index, &indir_index);
1986
1987 return ctx->abi->load_inputs(ctx->abi, var->data.location,
1988 var->data.driver_location,
1989 var->data.location_frac,
1990 instr->num_components, vertex_index, const_index, type);
1991 }
1992
1993 for (unsigned chan = comp; chan < ve + comp; chan++) {
1994 if (indir_index) {
1995 unsigned count = glsl_count_attribute_slots(
1996 var->type,
1997 ctx->stage == MESA_SHADER_VERTEX);
1998 count -= chan / 4;
1999 LLVMValueRef tmp_vec = ac_build_gather_values_extended(
2000 &ctx->ac, ctx->abi->inputs + idx + chan, count,
2001 stride, false, true);
2002
2003 values[chan] = LLVMBuildExtractElement(ctx->ac.builder,
2004 tmp_vec,
2005 indir_index, "");
2006 } else
2007 values[chan] = ctx->abi->inputs[idx + chan + const_index * stride];
2008 }
2009 break;
2010 case nir_var_function_temp:
2011 for (unsigned chan = 0; chan < ve; chan++) {
2012 if (indir_index) {
2013 unsigned count = glsl_count_attribute_slots(
2014 var->type, false);
2015 count -= chan / 4;
2016 LLVMValueRef tmp_vec = ac_build_gather_values_extended(
2017 &ctx->ac, ctx->locals + idx + chan, count,
2018 stride, true, true);
2019
2020 values[chan] = LLVMBuildExtractElement(ctx->ac.builder,
2021 tmp_vec,
2022 indir_index, "");
2023 } else {
2024 values[chan] = LLVMBuildLoad(ctx->ac.builder, ctx->locals[idx + chan + const_index * stride], "");
2025 }
2026 }
2027 break;
2028 case nir_var_mem_shared: {
2029 LLVMValueRef address = get_src(ctx, instr->src[0]);
2030 LLVMValueRef val = LLVMBuildLoad(ctx->ac.builder, address, "");
2031 return LLVMBuildBitCast(ctx->ac.builder, val,
2032 get_def_type(ctx, &instr->dest.ssa),
2033 "");
2034 }
2035 case nir_var_shader_out:
2036 if (ctx->stage == MESA_SHADER_TESS_CTRL) {
2037 return load_tess_varyings(ctx, instr, false);
2038 }
2039
2040 for (unsigned chan = comp; chan < ve + comp; chan++) {
2041 if (indir_index) {
2042 unsigned count = glsl_count_attribute_slots(
2043 var->type, false);
2044 count -= chan / 4;
2045 LLVMValueRef tmp_vec = ac_build_gather_values_extended(
2046 &ctx->ac, ctx->abi->outputs + idx + chan, count,
2047 stride, true, true);
2048
2049 values[chan] = LLVMBuildExtractElement(ctx->ac.builder,
2050 tmp_vec,
2051 indir_index, "");
2052 } else {
2053 values[chan] = LLVMBuildLoad(ctx->ac.builder,
2054 ctx->abi->outputs[idx + chan + const_index * stride],
2055 "");
2056 }
2057 }
2058 break;
2059 case nir_var_mem_global: {
2060 LLVMValueRef address = get_src(ctx, instr->src[0]);
2061 unsigned explicit_stride = glsl_get_explicit_stride(deref->type);
2062 unsigned natural_stride = type_scalar_size_bytes(deref->type);
2063 unsigned stride = explicit_stride ? explicit_stride : natural_stride;
2064
2065 LLVMTypeRef result_type = get_def_type(ctx, &instr->dest.ssa);
2066 if (stride != natural_stride) {
2067 LLVMTypeRef ptr_type = LLVMPointerType(LLVMGetElementType(result_type),
2068 LLVMGetPointerAddressSpace(LLVMTypeOf(address)));
2069 address = LLVMBuildBitCast(ctx->ac.builder, address, ptr_type , "");
2070
2071 for (unsigned i = 0; i < instr->dest.ssa.num_components; ++i) {
2072 LLVMValueRef offset = LLVMConstInt(ctx->ac.i32, i * stride / natural_stride, 0);
2073 values[i] = LLVMBuildLoad(ctx->ac.builder,
2074 ac_build_gep_ptr(&ctx->ac, address, offset), "");
2075 }
2076 return ac_build_gather_values(&ctx->ac, values, instr->dest.ssa.num_components);
2077 } else {
2078 LLVMTypeRef ptr_type = LLVMPointerType(result_type,
2079 LLVMGetPointerAddressSpace(LLVMTypeOf(address)));
2080 address = LLVMBuildBitCast(ctx->ac.builder, address, ptr_type , "");
2081 LLVMValueRef val = LLVMBuildLoad(ctx->ac.builder, address, "");
2082 return val;
2083 }
2084 }
2085 default:
2086 unreachable("unhandle variable mode");
2087 }
2088 ret = ac_build_varying_gather_values(&ctx->ac, values, ve, comp);
2089 return LLVMBuildBitCast(ctx->ac.builder, ret, get_def_type(ctx, &instr->dest.ssa), "");
2090 }
2091
2092 static void
2093 visit_store_var(struct ac_nir_context *ctx,
2094 nir_intrinsic_instr *instr)
2095 {
2096 nir_deref_instr *deref = nir_instr_as_deref(instr->src[0].ssa->parent_instr);
2097 nir_variable *var = nir_deref_instr_get_variable(deref);
2098
2099 LLVMValueRef temp_ptr, value;
2100 int idx = 0;
2101 unsigned comp = 0;
2102 LLVMValueRef src = ac_to_float(&ctx->ac, get_src(ctx, instr->src[1]));
2103 int writemask = instr->const_index[0];
2104 LLVMValueRef indir_index;
2105 unsigned const_index;
2106
2107 if (var) {
2108 get_deref_offset(ctx, deref, false,
2109 NULL, NULL, &const_index, &indir_index);
2110 idx = var->data.driver_location;
2111 comp = var->data.location_frac;
2112
2113 if (var->data.compact) {
2114 const_index += comp;
2115 comp = 0;
2116 }
2117 }
2118
2119 if (ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src)) == 64 &&
2120 (deref->mode == nir_var_shader_out ||
2121 deref->mode == nir_var_function_temp)) {
2122
2123 src = LLVMBuildBitCast(ctx->ac.builder, src,
2124 LLVMVectorType(ctx->ac.f32, ac_get_llvm_num_components(src) * 2),
2125 "");
2126
2127 writemask = widen_mask(writemask, 2);
2128 }
2129
2130 writemask = writemask << comp;
2131
2132 switch (deref->mode) {
2133 case nir_var_shader_out:
2134
2135 if (ctx->stage == MESA_SHADER_TESS_CTRL) {
2136 LLVMValueRef vertex_index = NULL;
2137 LLVMValueRef indir_index = NULL;
2138 unsigned const_index = 0;
2139 const bool is_patch = var->data.patch;
2140
2141 get_deref_offset(ctx, deref, false, NULL,
2142 is_patch ? NULL : &vertex_index,
2143 &const_index, &indir_index);
2144
2145 ctx->abi->store_tcs_outputs(ctx->abi, var,
2146 vertex_index, indir_index,
2147 const_index, src, writemask);
2148 return;
2149 }
2150
2151 for (unsigned chan = 0; chan < 8; chan++) {
2152 int stride = 4;
2153 if (!(writemask & (1 << chan)))
2154 continue;
2155
2156 value = ac_llvm_extract_elem(&ctx->ac, src, chan - comp);
2157
2158 if (var->data.compact)
2159 stride = 1;
2160 if (indir_index) {
2161 unsigned count = glsl_count_attribute_slots(
2162 var->type, false);
2163 count -= chan / 4;
2164 LLVMValueRef tmp_vec = ac_build_gather_values_extended(
2165 &ctx->ac, ctx->abi->outputs + idx + chan, count,
2166 stride, true, true);
2167
2168 tmp_vec = LLVMBuildInsertElement(ctx->ac.builder, tmp_vec,
2169 value, indir_index, "");
2170 build_store_values_extended(&ctx->ac, ctx->abi->outputs + idx + chan,
2171 count, stride, tmp_vec);
2172
2173 } else {
2174 temp_ptr = ctx->abi->outputs[idx + chan + const_index * stride];
2175
2176 LLVMBuildStore(ctx->ac.builder, value, temp_ptr);
2177 }
2178 }
2179 break;
2180 case nir_var_function_temp:
2181 for (unsigned chan = 0; chan < 8; chan++) {
2182 if (!(writemask & (1 << chan)))
2183 continue;
2184
2185 value = ac_llvm_extract_elem(&ctx->ac, src, chan);
2186 if (indir_index) {
2187 unsigned count = glsl_count_attribute_slots(
2188 var->type, false);
2189 count -= chan / 4;
2190 LLVMValueRef tmp_vec = ac_build_gather_values_extended(
2191 &ctx->ac, ctx->locals + idx + chan, count,
2192 4, true, true);
2193
2194 tmp_vec = LLVMBuildInsertElement(ctx->ac.builder, tmp_vec,
2195 value, indir_index, "");
2196 build_store_values_extended(&ctx->ac, ctx->locals + idx + chan,
2197 count, 4, tmp_vec);
2198 } else {
2199 temp_ptr = ctx->locals[idx + chan + const_index * 4];
2200
2201 LLVMBuildStore(ctx->ac.builder, value, temp_ptr);
2202 }
2203 }
2204 break;
2205
2206 case nir_var_mem_global:
2207 case nir_var_mem_shared: {
2208 int writemask = instr->const_index[0];
2209 LLVMValueRef address = get_src(ctx, instr->src[0]);
2210 LLVMValueRef val = get_src(ctx, instr->src[1]);
2211
2212 unsigned explicit_stride = glsl_get_explicit_stride(deref->type);
2213 unsigned natural_stride = type_scalar_size_bytes(deref->type);
2214 unsigned stride = explicit_stride ? explicit_stride : natural_stride;
2215
2216 LLVMTypeRef ptr_type = LLVMPointerType(LLVMTypeOf(val),
2217 LLVMGetPointerAddressSpace(LLVMTypeOf(address)));
2218 address = LLVMBuildBitCast(ctx->ac.builder, address, ptr_type , "");
2219
2220 if (writemask == (1u << ac_get_llvm_num_components(val)) - 1 &&
2221 stride == natural_stride) {
2222 LLVMTypeRef ptr_type = LLVMPointerType(LLVMTypeOf(val),
2223 LLVMGetPointerAddressSpace(LLVMTypeOf(address)));
2224 address = LLVMBuildBitCast(ctx->ac.builder, address, ptr_type , "");
2225
2226 val = LLVMBuildBitCast(ctx->ac.builder, val,
2227 LLVMGetElementType(LLVMTypeOf(address)), "");
2228 LLVMBuildStore(ctx->ac.builder, val, address);
2229 } else {
2230 LLVMTypeRef ptr_type = LLVMPointerType(LLVMGetElementType(LLVMTypeOf(val)),
2231 LLVMGetPointerAddressSpace(LLVMTypeOf(address)));
2232 address = LLVMBuildBitCast(ctx->ac.builder, address, ptr_type , "");
2233 for (unsigned chan = 0; chan < 4; chan++) {
2234 if (!(writemask & (1 << chan)))
2235 continue;
2236
2237 LLVMValueRef offset = LLVMConstInt(ctx->ac.i32, chan * stride / natural_stride, 0);
2238
2239 LLVMValueRef ptr = ac_build_gep_ptr(&ctx->ac, address, offset);
2240 LLVMValueRef src = ac_llvm_extract_elem(&ctx->ac, val,
2241 chan);
2242 src = LLVMBuildBitCast(ctx->ac.builder, src,
2243 LLVMGetElementType(LLVMTypeOf(ptr)), "");
2244 LLVMBuildStore(ctx->ac.builder, src, ptr);
2245 }
2246 }
2247 break;
2248 }
2249 default:
2250 abort();
2251 break;
2252 }
2253 }
2254
2255 static int image_type_to_components_count(enum glsl_sampler_dim dim, bool array)
2256 {
2257 switch (dim) {
2258 case GLSL_SAMPLER_DIM_BUF:
2259 return 1;
2260 case GLSL_SAMPLER_DIM_1D:
2261 return array ? 2 : 1;
2262 case GLSL_SAMPLER_DIM_2D:
2263 return array ? 3 : 2;
2264 case GLSL_SAMPLER_DIM_MS:
2265 return array ? 4 : 3;
2266 case GLSL_SAMPLER_DIM_3D:
2267 case GLSL_SAMPLER_DIM_CUBE:
2268 return 3;
2269 case GLSL_SAMPLER_DIM_RECT:
2270 case GLSL_SAMPLER_DIM_SUBPASS:
2271 return 2;
2272 case GLSL_SAMPLER_DIM_SUBPASS_MS:
2273 return 3;
2274 default:
2275 break;
2276 }
2277 return 0;
2278 }
2279
2280 static LLVMValueRef adjust_sample_index_using_fmask(struct ac_llvm_context *ctx,
2281 LLVMValueRef coord_x, LLVMValueRef coord_y,
2282 LLVMValueRef coord_z,
2283 LLVMValueRef sample_index,
2284 LLVMValueRef fmask_desc_ptr)
2285 {
2286 unsigned sample_chan = coord_z ? 3 : 2;
2287 LLVMValueRef addr[4] = {coord_x, coord_y, coord_z};
2288 addr[sample_chan] = sample_index;
2289
2290 ac_apply_fmask_to_sample(ctx, fmask_desc_ptr, addr, coord_z != NULL);
2291 return addr[sample_chan];
2292 }
2293
2294 static nir_deref_instr *get_image_deref(const nir_intrinsic_instr *instr)
2295 {
2296 assert(instr->src[0].is_ssa);
2297 return nir_instr_as_deref(instr->src[0].ssa->parent_instr);
2298 }
2299
2300 static LLVMValueRef get_image_descriptor(struct ac_nir_context *ctx,
2301 const nir_intrinsic_instr *instr,
2302 enum ac_descriptor_type desc_type,
2303 bool write)
2304 {
2305 nir_deref_instr *deref_instr =
2306 instr->src[0].ssa->parent_instr->type == nir_instr_type_deref ?
2307 nir_instr_as_deref(instr->src[0].ssa->parent_instr) : NULL;
2308
2309 return get_sampler_desc(ctx, deref_instr, desc_type, &instr->instr, true, write);
2310 }
2311
2312 static void get_image_coords(struct ac_nir_context *ctx,
2313 const nir_intrinsic_instr *instr,
2314 struct ac_image_args *args,
2315 enum glsl_sampler_dim dim,
2316 bool is_array)
2317 {
2318 LLVMValueRef src0 = get_src(ctx, instr->src[1]);
2319 LLVMValueRef masks[] = {
2320 LLVMConstInt(ctx->ac.i32, 0, false), LLVMConstInt(ctx->ac.i32, 1, false),
2321 LLVMConstInt(ctx->ac.i32, 2, false), LLVMConstInt(ctx->ac.i32, 3, false),
2322 };
2323 LLVMValueRef sample_index = ac_llvm_extract_elem(&ctx->ac, get_src(ctx, instr->src[2]), 0);
2324
2325 int count;
2326 bool add_frag_pos = (dim == GLSL_SAMPLER_DIM_SUBPASS ||
2327 dim == GLSL_SAMPLER_DIM_SUBPASS_MS);
2328 bool is_ms = (dim == GLSL_SAMPLER_DIM_MS ||
2329 dim == GLSL_SAMPLER_DIM_SUBPASS_MS);
2330 bool gfx9_1d = ctx->ac.chip_class >= GFX9 && dim == GLSL_SAMPLER_DIM_1D;
2331 count = image_type_to_components_count(dim, is_array);
2332
2333 if (is_ms && (instr->intrinsic == nir_intrinsic_image_deref_load ||
2334 instr->intrinsic == nir_intrinsic_bindless_image_load)) {
2335 LLVMValueRef fmask_load_address[3];
2336 int chan;
2337
2338 fmask_load_address[0] = LLVMBuildExtractElement(ctx->ac.builder, src0, masks[0], "");
2339 fmask_load_address[1] = LLVMBuildExtractElement(ctx->ac.builder, src0, masks[1], "");
2340 if (is_array)
2341 fmask_load_address[2] = LLVMBuildExtractElement(ctx->ac.builder, src0, masks[2], "");
2342 else
2343 fmask_load_address[2] = NULL;
2344 if (add_frag_pos) {
2345 for (chan = 0; chan < 2; ++chan)
2346 fmask_load_address[chan] =
2347 LLVMBuildAdd(ctx->ac.builder, fmask_load_address[chan],
2348 LLVMBuildFPToUI(ctx->ac.builder, ctx->abi->frag_pos[chan],
2349 ctx->ac.i32, ""), "");
2350 fmask_load_address[2] = ac_to_integer(&ctx->ac, ctx->abi->inputs[ac_llvm_reg_index_soa(VARYING_SLOT_LAYER, 0)]);
2351 }
2352 sample_index = adjust_sample_index_using_fmask(&ctx->ac,
2353 fmask_load_address[0],
2354 fmask_load_address[1],
2355 fmask_load_address[2],
2356 sample_index,
2357 get_sampler_desc(ctx, nir_instr_as_deref(instr->src[0].ssa->parent_instr),
2358 AC_DESC_FMASK, &instr->instr, false, false));
2359 }
2360 if (count == 1 && !gfx9_1d) {
2361 if (instr->src[1].ssa->num_components)
2362 args->coords[0] = LLVMBuildExtractElement(ctx->ac.builder, src0, masks[0], "");
2363 else
2364 args->coords[0] = src0;
2365 } else {
2366 int chan;
2367 if (is_ms)
2368 count--;
2369 for (chan = 0; chan < count; ++chan) {
2370 args->coords[chan] = ac_llvm_extract_elem(&ctx->ac, src0, chan);
2371 }
2372 if (add_frag_pos) {
2373 for (chan = 0; chan < 2; ++chan) {
2374 args->coords[chan] = LLVMBuildAdd(
2375 ctx->ac.builder, args->coords[chan],
2376 LLVMBuildFPToUI(
2377 ctx->ac.builder, ctx->abi->frag_pos[chan],
2378 ctx->ac.i32, ""), "");
2379 }
2380 args->coords[2] = ac_to_integer(&ctx->ac,
2381 ctx->abi->inputs[ac_llvm_reg_index_soa(VARYING_SLOT_LAYER, 0)]);
2382 count++;
2383 }
2384
2385 if (gfx9_1d) {
2386 if (is_array) {
2387 args->coords[2] = args->coords[1];
2388 args->coords[1] = ctx->ac.i32_0;
2389 } else
2390 args->coords[1] = ctx->ac.i32_0;
2391 count++;
2392 }
2393
2394 if (is_ms) {
2395 args->coords[count] = sample_index;
2396 count++;
2397 }
2398 }
2399 }
2400
2401 static LLVMValueRef get_image_buffer_descriptor(struct ac_nir_context *ctx,
2402 const nir_intrinsic_instr *instr, bool write)
2403 {
2404 LLVMValueRef rsrc = get_image_descriptor(ctx, instr, AC_DESC_BUFFER, write);
2405 if (ctx->abi->gfx9_stride_size_workaround) {
2406 LLVMValueRef elem_count = LLVMBuildExtractElement(ctx->ac.builder, rsrc, LLVMConstInt(ctx->ac.i32, 2, 0), "");
2407 LLVMValueRef stride = LLVMBuildExtractElement(ctx->ac.builder, rsrc, LLVMConstInt(ctx->ac.i32, 1, 0), "");
2408 stride = LLVMBuildLShr(ctx->ac.builder, stride, LLVMConstInt(ctx->ac.i32, 16, 0), "");
2409
2410 LLVMValueRef new_elem_count = LLVMBuildSelect(ctx->ac.builder,
2411 LLVMBuildICmp(ctx->ac.builder, LLVMIntUGT, elem_count, stride, ""),
2412 elem_count, stride, "");
2413
2414 rsrc = LLVMBuildInsertElement(ctx->ac.builder, rsrc, new_elem_count,
2415 LLVMConstInt(ctx->ac.i32, 2, 0), "");
2416 }
2417 return rsrc;
2418 }
2419
2420 static LLVMValueRef visit_image_load(struct ac_nir_context *ctx,
2421 const nir_intrinsic_instr *instr,
2422 bool bindless)
2423 {
2424 LLVMValueRef res;
2425
2426 enum glsl_sampler_dim dim;
2427 enum gl_access_qualifier access;
2428 bool is_array;
2429 if (bindless) {
2430 dim = nir_intrinsic_image_dim(instr);
2431 access = nir_intrinsic_access(instr);
2432 is_array = nir_intrinsic_image_array(instr);
2433 } else {
2434 const nir_deref_instr *image_deref = get_image_deref(instr);
2435 const struct glsl_type *type = image_deref->type;
2436 const nir_variable *var = nir_deref_instr_get_variable(image_deref);
2437 dim = glsl_get_sampler_dim(type);
2438 access = var->data.image.access;
2439 is_array = glsl_sampler_type_is_array(type);
2440 }
2441
2442 struct ac_image_args args = {};
2443
2444 args.cache_policy = get_cache_policy(ctx, access, false, false);
2445
2446 if (dim == GLSL_SAMPLER_DIM_BUF) {
2447 unsigned mask = nir_ssa_def_components_read(&instr->dest.ssa);
2448 unsigned num_channels = util_last_bit(mask);
2449 LLVMValueRef rsrc, vindex;
2450
2451 rsrc = get_image_buffer_descriptor(ctx, instr, false);
2452 vindex = LLVMBuildExtractElement(ctx->ac.builder, get_src(ctx, instr->src[1]),
2453 ctx->ac.i32_0, "");
2454
2455 /* TODO: set "can_speculate" when OpenGL needs it. */
2456 res = ac_build_buffer_load_format(&ctx->ac, rsrc, vindex,
2457 ctx->ac.i32_0, num_channels,
2458 !!(args.cache_policy & ac_glc),
2459 false);
2460 res = ac_build_expand_to_vec4(&ctx->ac, res, num_channels);
2461
2462 res = ac_trim_vector(&ctx->ac, res, instr->dest.ssa.num_components);
2463 res = ac_to_integer(&ctx->ac, res);
2464 } else {
2465 args.opcode = ac_image_load;
2466 get_image_coords(ctx, instr, &args, dim, is_array);
2467 args.resource = get_image_descriptor(ctx, instr, AC_DESC_IMAGE, false);
2468 args.dim = get_ac_image_dim(&ctx->ac, dim, is_array);
2469 args.dmask = 15;
2470 args.attributes = AC_FUNC_ATTR_READONLY;
2471
2472 res = ac_build_image_opcode(&ctx->ac, &args);
2473 }
2474 return res;
2475 }
2476
2477 static void visit_image_store(struct ac_nir_context *ctx,
2478 nir_intrinsic_instr *instr,
2479 bool bindless)
2480 {
2481
2482
2483 enum glsl_sampler_dim dim;
2484 enum gl_access_qualifier access;
2485 bool is_array;
2486 if (bindless) {
2487 dim = nir_intrinsic_image_dim(instr);
2488 access = nir_intrinsic_access(instr);
2489 is_array = nir_intrinsic_image_array(instr);
2490 } else {
2491 const nir_deref_instr *image_deref = get_image_deref(instr);
2492 const struct glsl_type *type = image_deref->type;
2493 const nir_variable *var = nir_deref_instr_get_variable(image_deref);
2494 dim = glsl_get_sampler_dim(type);
2495 access = var->data.image.access;
2496 is_array = glsl_sampler_type_is_array(type);
2497 }
2498
2499 bool writeonly_memory = access & ACCESS_NON_READABLE;
2500 struct ac_image_args args = {};
2501
2502 args.cache_policy = get_cache_policy(ctx, access, true, writeonly_memory);
2503
2504 if (dim == GLSL_SAMPLER_DIM_BUF) {
2505 LLVMValueRef rsrc = get_image_buffer_descriptor(ctx, instr, true);
2506 LLVMValueRef src = ac_to_float(&ctx->ac, get_src(ctx, instr->src[3]));
2507 unsigned src_channels = ac_get_llvm_num_components(src);
2508 LLVMValueRef vindex;
2509
2510 if (src_channels == 3)
2511 src = ac_build_expand_to_vec4(&ctx->ac, src, 3);
2512
2513 vindex = LLVMBuildExtractElement(ctx->ac.builder,
2514 get_src(ctx, instr->src[1]),
2515 ctx->ac.i32_0, "");
2516
2517 ac_build_buffer_store_format(&ctx->ac, rsrc, src, vindex,
2518 ctx->ac.i32_0, src_channels,
2519 args.cache_policy & ac_glc,
2520 writeonly_memory);
2521 } else {
2522 args.opcode = ac_image_store;
2523 args.data[0] = ac_to_float(&ctx->ac, get_src(ctx, instr->src[3]));
2524 get_image_coords(ctx, instr, &args, dim, is_array);
2525 args.resource = get_image_descriptor(ctx, instr, AC_DESC_IMAGE, true);
2526 args.dim = get_ac_image_dim(&ctx->ac, dim, is_array);
2527 args.dmask = 15;
2528
2529 ac_build_image_opcode(&ctx->ac, &args);
2530 }
2531
2532 }
2533
2534 static LLVMValueRef visit_image_atomic(struct ac_nir_context *ctx,
2535 const nir_intrinsic_instr *instr,
2536 bool bindless)
2537 {
2538 LLVMValueRef params[7];
2539 int param_count = 0;
2540
2541 bool cmpswap = instr->intrinsic == nir_intrinsic_image_deref_atomic_comp_swap ||
2542 instr->intrinsic == nir_intrinsic_bindless_image_atomic_comp_swap;
2543 const char *atomic_name;
2544 char intrinsic_name[64];
2545 enum ac_atomic_op atomic_subop;
2546 MAYBE_UNUSED int length;
2547
2548 enum glsl_sampler_dim dim;
2549 bool is_unsigned;
2550 bool is_array;
2551 if (bindless) {
2552 if (instr->intrinsic == nir_intrinsic_image_atomic_min ||
2553 instr->intrinsic == nir_intrinsic_image_atomic_max) {
2554 const GLenum format = nir_intrinsic_format(instr);
2555 assert(format == GL_R32UI || format == GL_R32I);
2556 is_unsigned = format == GL_R32UI;
2557 }
2558 dim = nir_intrinsic_image_dim(instr);
2559 is_array = nir_intrinsic_image_array(instr);
2560 } else {
2561 const struct glsl_type *type = get_image_deref(instr)->type;
2562 is_unsigned = glsl_get_sampler_result_type(type) == GLSL_TYPE_UINT;
2563 dim = glsl_get_sampler_dim(type);
2564 is_array = glsl_sampler_type_is_array(type);
2565 }
2566
2567 switch (instr->intrinsic) {
2568 case nir_intrinsic_bindless_image_atomic_add:
2569 case nir_intrinsic_image_deref_atomic_add:
2570 atomic_name = "add";
2571 atomic_subop = ac_atomic_add;
2572 break;
2573 case nir_intrinsic_bindless_image_atomic_min:
2574 case nir_intrinsic_image_deref_atomic_min:
2575 atomic_name = is_unsigned ? "umin" : "smin";
2576 atomic_subop = is_unsigned ? ac_atomic_umin : ac_atomic_smin;
2577 break;
2578 case nir_intrinsic_bindless_image_atomic_max:
2579 case nir_intrinsic_image_deref_atomic_max:
2580 atomic_name = is_unsigned ? "umax" : "smax";
2581 atomic_subop = is_unsigned ? ac_atomic_umax : ac_atomic_smax;
2582 break;
2583 case nir_intrinsic_bindless_image_atomic_and:
2584 case nir_intrinsic_image_deref_atomic_and:
2585 atomic_name = "and";
2586 atomic_subop = ac_atomic_and;
2587 break;
2588 case nir_intrinsic_bindless_image_atomic_or:
2589 case nir_intrinsic_image_deref_atomic_or:
2590 atomic_name = "or";
2591 atomic_subop = ac_atomic_or;
2592 break;
2593 case nir_intrinsic_bindless_image_atomic_xor:
2594 case nir_intrinsic_image_deref_atomic_xor:
2595 atomic_name = "xor";
2596 atomic_subop = ac_atomic_xor;
2597 break;
2598 case nir_intrinsic_bindless_image_atomic_exchange:
2599 case nir_intrinsic_image_deref_atomic_exchange:
2600 atomic_name = "swap";
2601 atomic_subop = ac_atomic_swap;
2602 break;
2603 case nir_intrinsic_bindless_image_atomic_comp_swap:
2604 case nir_intrinsic_image_deref_atomic_comp_swap:
2605 atomic_name = "cmpswap";
2606 atomic_subop = 0; /* not used */
2607 break;
2608 default:
2609 abort();
2610 }
2611
2612 if (cmpswap)
2613 params[param_count++] = get_src(ctx, instr->src[4]);
2614 params[param_count++] = get_src(ctx, instr->src[3]);
2615
2616 if (dim == GLSL_SAMPLER_DIM_BUF) {
2617 params[param_count++] = get_image_buffer_descriptor(ctx, instr, true);
2618 params[param_count++] = LLVMBuildExtractElement(ctx->ac.builder, get_src(ctx, instr->src[1]),
2619 ctx->ac.i32_0, ""); /* vindex */
2620 params[param_count++] = ctx->ac.i32_0; /* voffset */
2621 if (HAVE_LLVM >= 0x800) {
2622 params[param_count++] = ctx->ac.i32_0; /* soffset */
2623 params[param_count++] = ctx->ac.i32_0; /* slc */
2624
2625 length = snprintf(intrinsic_name, sizeof(intrinsic_name),
2626 "llvm.amdgcn.struct.buffer.atomic.%s.i32", atomic_name);
2627 } else {
2628 params[param_count++] = ctx->ac.i1false; /* slc */
2629
2630 length = snprintf(intrinsic_name, sizeof(intrinsic_name),
2631 "llvm.amdgcn.buffer.atomic.%s", atomic_name);
2632 }
2633
2634 assert(length < sizeof(intrinsic_name));
2635 return ac_build_intrinsic(&ctx->ac, intrinsic_name, ctx->ac.i32,
2636 params, param_count, 0);
2637 } else {
2638 struct ac_image_args args = {};
2639 args.opcode = cmpswap ? ac_image_atomic_cmpswap : ac_image_atomic;
2640 args.atomic = atomic_subop;
2641 args.data[0] = params[0];
2642 if (cmpswap)
2643 args.data[1] = params[1];
2644 get_image_coords(ctx, instr, &args, dim, is_array);
2645 args.resource = get_image_descriptor(ctx, instr, AC_DESC_IMAGE, true);
2646 args.dim = get_ac_image_dim(&ctx->ac, dim, is_array);
2647
2648 return ac_build_image_opcode(&ctx->ac, &args);
2649 }
2650 }
2651
2652 static LLVMValueRef visit_image_samples(struct ac_nir_context *ctx,
2653 const nir_intrinsic_instr *instr,
2654 bool bindless)
2655 {
2656 enum glsl_sampler_dim dim;
2657 bool is_array;
2658 if (bindless) {
2659 dim = nir_intrinsic_image_dim(instr);
2660 is_array = nir_intrinsic_image_array(instr);
2661 } else {
2662 const struct glsl_type *type = get_image_deref(instr)->type;
2663 dim = glsl_get_sampler_dim(type);
2664 is_array = glsl_sampler_type_is_array(type);
2665 }
2666
2667 struct ac_image_args args = { 0 };
2668 args.dim = get_ac_sampler_dim(&ctx->ac, dim, is_array);
2669 args.dmask = 0xf;
2670 args.resource = get_image_descriptor(ctx, instr, AC_DESC_IMAGE, false);
2671 args.opcode = ac_image_get_resinfo;
2672 args.lod = ctx->ac.i32_0;
2673 args.attributes = AC_FUNC_ATTR_READNONE;
2674
2675 return ac_build_image_opcode(&ctx->ac, &args);
2676 }
2677
2678 static LLVMValueRef visit_image_size(struct ac_nir_context *ctx,
2679 const nir_intrinsic_instr *instr,
2680 bool bindless)
2681 {
2682 LLVMValueRef res;
2683
2684 enum glsl_sampler_dim dim;
2685 bool is_array;
2686 if (bindless) {
2687 dim = nir_intrinsic_image_dim(instr);
2688 is_array = nir_intrinsic_image_array(instr);
2689 } else {
2690 const struct glsl_type *type = get_image_deref(instr)->type;
2691 dim = glsl_get_sampler_dim(type);
2692 is_array = glsl_sampler_type_is_array(type);
2693 }
2694
2695 if (dim == GLSL_SAMPLER_DIM_BUF)
2696 return get_buffer_size(ctx, get_image_descriptor(ctx, instr, AC_DESC_BUFFER, false), true);
2697
2698 struct ac_image_args args = { 0 };
2699
2700 args.dim = get_ac_image_dim(&ctx->ac, dim, is_array);
2701 args.dmask = 0xf;
2702 args.resource = get_image_descriptor(ctx, instr, AC_DESC_IMAGE, false);
2703 args.opcode = ac_image_get_resinfo;
2704 args.lod = ctx->ac.i32_0;
2705 args.attributes = AC_FUNC_ATTR_READNONE;
2706
2707 res = ac_build_image_opcode(&ctx->ac, &args);
2708
2709 LLVMValueRef two = LLVMConstInt(ctx->ac.i32, 2, false);
2710
2711 if (dim == GLSL_SAMPLER_DIM_CUBE && is_array) {
2712 LLVMValueRef six = LLVMConstInt(ctx->ac.i32, 6, false);
2713 LLVMValueRef z = LLVMBuildExtractElement(ctx->ac.builder, res, two, "");
2714 z = LLVMBuildSDiv(ctx->ac.builder, z, six, "");
2715 res = LLVMBuildInsertElement(ctx->ac.builder, res, z, two, "");
2716 }
2717 if (ctx->ac.chip_class >= GFX9 && dim == GLSL_SAMPLER_DIM_1D && is_array) {
2718 LLVMValueRef layers = LLVMBuildExtractElement(ctx->ac.builder, res, two, "");
2719 res = LLVMBuildInsertElement(ctx->ac.builder, res, layers,
2720 ctx->ac.i32_1, "");
2721
2722 }
2723 return res;
2724 }
2725
2726 static void emit_membar(struct ac_llvm_context *ac,
2727 const nir_intrinsic_instr *instr)
2728 {
2729 unsigned waitcnt = NOOP_WAITCNT;
2730
2731 switch (instr->intrinsic) {
2732 case nir_intrinsic_memory_barrier:
2733 case nir_intrinsic_group_memory_barrier:
2734 waitcnt &= VM_CNT & LGKM_CNT;
2735 break;
2736 case nir_intrinsic_memory_barrier_atomic_counter:
2737 case nir_intrinsic_memory_barrier_buffer:
2738 case nir_intrinsic_memory_barrier_image:
2739 waitcnt &= VM_CNT;
2740 break;
2741 case nir_intrinsic_memory_barrier_shared:
2742 waitcnt &= LGKM_CNT;
2743 break;
2744 default:
2745 break;
2746 }
2747 if (waitcnt != NOOP_WAITCNT)
2748 ac_build_waitcnt(ac, waitcnt);
2749 }
2750
2751 void ac_emit_barrier(struct ac_llvm_context *ac, gl_shader_stage stage)
2752 {
2753 /* SI only (thanks to a hw bug workaround):
2754 * The real barrier instruction isn’t needed, because an entire patch
2755 * always fits into a single wave.
2756 */
2757 if (ac->chip_class == SI && stage == MESA_SHADER_TESS_CTRL) {
2758 ac_build_waitcnt(ac, LGKM_CNT & VM_CNT);
2759 return;
2760 }
2761 ac_build_s_barrier(ac);
2762 }
2763
2764 static void emit_discard(struct ac_nir_context *ctx,
2765 const nir_intrinsic_instr *instr)
2766 {
2767 LLVMValueRef cond;
2768
2769 if (instr->intrinsic == nir_intrinsic_discard_if) {
2770 cond = LLVMBuildICmp(ctx->ac.builder, LLVMIntEQ,
2771 get_src(ctx, instr->src[0]),
2772 ctx->ac.i32_0, "");
2773 } else {
2774 assert(instr->intrinsic == nir_intrinsic_discard);
2775 cond = ctx->ac.i1false;
2776 }
2777
2778 ctx->abi->emit_kill(ctx->abi, cond);
2779 }
2780
2781 static LLVMValueRef
2782 visit_load_local_invocation_index(struct ac_nir_context *ctx)
2783 {
2784 LLVMValueRef result;
2785 LLVMValueRef thread_id = ac_get_thread_id(&ctx->ac);
2786 result = LLVMBuildAnd(ctx->ac.builder, ctx->abi->tg_size,
2787 LLVMConstInt(ctx->ac.i32, 0xfc0, false), "");
2788
2789 return LLVMBuildAdd(ctx->ac.builder, result, thread_id, "");
2790 }
2791
2792 static LLVMValueRef
2793 visit_load_subgroup_id(struct ac_nir_context *ctx)
2794 {
2795 if (ctx->stage == MESA_SHADER_COMPUTE) {
2796 LLVMValueRef result;
2797 result = LLVMBuildAnd(ctx->ac.builder, ctx->abi->tg_size,
2798 LLVMConstInt(ctx->ac.i32, 0xfc0, false), "");
2799 return LLVMBuildLShr(ctx->ac.builder, result, LLVMConstInt(ctx->ac.i32, 6, false), "");
2800 } else {
2801 return LLVMConstInt(ctx->ac.i32, 0, false);
2802 }
2803 }
2804
2805 static LLVMValueRef
2806 visit_load_num_subgroups(struct ac_nir_context *ctx)
2807 {
2808 if (ctx->stage == MESA_SHADER_COMPUTE) {
2809 return LLVMBuildAnd(ctx->ac.builder, ctx->abi->tg_size,
2810 LLVMConstInt(ctx->ac.i32, 0x3f, false), "");
2811 } else {
2812 return LLVMConstInt(ctx->ac.i32, 1, false);
2813 }
2814 }
2815
2816 static LLVMValueRef
2817 visit_first_invocation(struct ac_nir_context *ctx)
2818 {
2819 LLVMValueRef active_set = ac_build_ballot(&ctx->ac, ctx->ac.i32_1);
2820
2821 /* The second argument is whether cttz(0) should be defined, but we do not care. */
2822 LLVMValueRef args[] = {active_set, ctx->ac.i1false};
2823 LLVMValueRef result = ac_build_intrinsic(&ctx->ac,
2824 "llvm.cttz.i64",
2825 ctx->ac.i64, args, 2,
2826 AC_FUNC_ATTR_NOUNWIND |
2827 AC_FUNC_ATTR_READNONE);
2828
2829 return LLVMBuildTrunc(ctx->ac.builder, result, ctx->ac.i32, "");
2830 }
2831
2832 static LLVMValueRef
2833 visit_load_shared(struct ac_nir_context *ctx,
2834 const nir_intrinsic_instr *instr)
2835 {
2836 LLVMValueRef values[4], derived_ptr, index, ret;
2837
2838 LLVMValueRef ptr = get_memory_ptr(ctx, instr->src[0]);
2839
2840 for (int chan = 0; chan < instr->num_components; chan++) {
2841 index = LLVMConstInt(ctx->ac.i32, chan, 0);
2842 derived_ptr = LLVMBuildGEP(ctx->ac.builder, ptr, &index, 1, "");
2843 values[chan] = LLVMBuildLoad(ctx->ac.builder, derived_ptr, "");
2844 }
2845
2846 ret = ac_build_gather_values(&ctx->ac, values, instr->num_components);
2847 return LLVMBuildBitCast(ctx->ac.builder, ret, get_def_type(ctx, &instr->dest.ssa), "");
2848 }
2849
2850 static void
2851 visit_store_shared(struct ac_nir_context *ctx,
2852 const nir_intrinsic_instr *instr)
2853 {
2854 LLVMValueRef derived_ptr, data,index;
2855 LLVMBuilderRef builder = ctx->ac.builder;
2856
2857 LLVMValueRef ptr = get_memory_ptr(ctx, instr->src[1]);
2858 LLVMValueRef src = get_src(ctx, instr->src[0]);
2859
2860 int writemask = nir_intrinsic_write_mask(instr);
2861 for (int chan = 0; chan < 4; chan++) {
2862 if (!(writemask & (1 << chan))) {
2863 continue;
2864 }
2865 data = ac_llvm_extract_elem(&ctx->ac, src, chan);
2866 index = LLVMConstInt(ctx->ac.i32, chan, 0);
2867 derived_ptr = LLVMBuildGEP(builder, ptr, &index, 1, "");
2868 LLVMBuildStore(builder, data, derived_ptr);
2869 }
2870 }
2871
2872 static LLVMValueRef visit_var_atomic(struct ac_nir_context *ctx,
2873 const nir_intrinsic_instr *instr,
2874 LLVMValueRef ptr, int src_idx)
2875 {
2876 LLVMValueRef result;
2877 LLVMValueRef src = get_src(ctx, instr->src[src_idx]);
2878
2879 if (instr->intrinsic == nir_intrinsic_shared_atomic_comp_swap ||
2880 instr->intrinsic == nir_intrinsic_deref_atomic_comp_swap) {
2881 LLVMValueRef src1 = get_src(ctx, instr->src[src_idx + 1]);
2882 result = LLVMBuildAtomicCmpXchg(ctx->ac.builder,
2883 ptr, src, src1,
2884 LLVMAtomicOrderingSequentiallyConsistent,
2885 LLVMAtomicOrderingSequentiallyConsistent,
2886 false);
2887 result = LLVMBuildExtractValue(ctx->ac.builder, result, 0, "");
2888 } else {
2889 LLVMAtomicRMWBinOp op;
2890 switch (instr->intrinsic) {
2891 case nir_intrinsic_shared_atomic_add:
2892 case nir_intrinsic_deref_atomic_add:
2893 op = LLVMAtomicRMWBinOpAdd;
2894 break;
2895 case nir_intrinsic_shared_atomic_umin:
2896 case nir_intrinsic_deref_atomic_umin:
2897 op = LLVMAtomicRMWBinOpUMin;
2898 break;
2899 case nir_intrinsic_shared_atomic_umax:
2900 case nir_intrinsic_deref_atomic_umax:
2901 op = LLVMAtomicRMWBinOpUMax;
2902 break;
2903 case nir_intrinsic_shared_atomic_imin:
2904 case nir_intrinsic_deref_atomic_imin:
2905 op = LLVMAtomicRMWBinOpMin;
2906 break;
2907 case nir_intrinsic_shared_atomic_imax:
2908 case nir_intrinsic_deref_atomic_imax:
2909 op = LLVMAtomicRMWBinOpMax;
2910 break;
2911 case nir_intrinsic_shared_atomic_and:
2912 case nir_intrinsic_deref_atomic_and:
2913 op = LLVMAtomicRMWBinOpAnd;
2914 break;
2915 case nir_intrinsic_shared_atomic_or:
2916 case nir_intrinsic_deref_atomic_or:
2917 op = LLVMAtomicRMWBinOpOr;
2918 break;
2919 case nir_intrinsic_shared_atomic_xor:
2920 case nir_intrinsic_deref_atomic_xor:
2921 op = LLVMAtomicRMWBinOpXor;
2922 break;
2923 case nir_intrinsic_shared_atomic_exchange:
2924 case nir_intrinsic_deref_atomic_exchange:
2925 op = LLVMAtomicRMWBinOpXchg;
2926 break;
2927 default:
2928 return NULL;
2929 }
2930
2931 result = LLVMBuildAtomicRMW(ctx->ac.builder, op, ptr, ac_to_integer(&ctx->ac, src),
2932 LLVMAtomicOrderingSequentiallyConsistent,
2933 false);
2934 }
2935 return result;
2936 }
2937
2938 static LLVMValueRef load_sample_pos(struct ac_nir_context *ctx)
2939 {
2940 LLVMValueRef values[2];
2941 LLVMValueRef pos[2];
2942
2943 pos[0] = ac_to_float(&ctx->ac, ctx->abi->frag_pos[0]);
2944 pos[1] = ac_to_float(&ctx->ac, ctx->abi->frag_pos[1]);
2945
2946 values[0] = ac_build_fract(&ctx->ac, pos[0], 32);
2947 values[1] = ac_build_fract(&ctx->ac, pos[1], 32);
2948 return ac_build_gather_values(&ctx->ac, values, 2);
2949 }
2950
2951 static LLVMValueRef visit_interp(struct ac_nir_context *ctx,
2952 const nir_intrinsic_instr *instr)
2953 {
2954 LLVMValueRef result[4];
2955 LLVMValueRef interp_param;
2956 unsigned location;
2957 unsigned chan;
2958 LLVMValueRef src_c0 = NULL;
2959 LLVMValueRef src_c1 = NULL;
2960 LLVMValueRef src0 = NULL;
2961
2962 nir_deref_instr *deref_instr = nir_instr_as_deref(instr->src[0].ssa->parent_instr);
2963 nir_variable *var = nir_deref_instr_get_variable(deref_instr);
2964 int input_base = ctx->abi->fs_input_attr_indices[var->data.location - VARYING_SLOT_VAR0];
2965 switch (instr->intrinsic) {
2966 case nir_intrinsic_interp_deref_at_centroid:
2967 location = INTERP_CENTROID;
2968 break;
2969 case nir_intrinsic_interp_deref_at_sample:
2970 case nir_intrinsic_interp_deref_at_offset:
2971 location = INTERP_CENTER;
2972 src0 = get_src(ctx, instr->src[1]);
2973 break;
2974 default:
2975 break;
2976 }
2977
2978 if (instr->intrinsic == nir_intrinsic_interp_deref_at_offset) {
2979 src_c0 = ac_to_float(&ctx->ac, LLVMBuildExtractElement(ctx->ac.builder, src0, ctx->ac.i32_0, ""));
2980 src_c1 = ac_to_float(&ctx->ac, LLVMBuildExtractElement(ctx->ac.builder, src0, ctx->ac.i32_1, ""));
2981 } else if (instr->intrinsic == nir_intrinsic_interp_deref_at_sample) {
2982 LLVMValueRef sample_position;
2983 LLVMValueRef halfval = LLVMConstReal(ctx->ac.f32, 0.5f);
2984
2985 /* fetch sample ID */
2986 sample_position = ctx->abi->load_sample_position(ctx->abi, src0);
2987
2988 src_c0 = LLVMBuildExtractElement(ctx->ac.builder, sample_position, ctx->ac.i32_0, "");
2989 src_c0 = LLVMBuildFSub(ctx->ac.builder, src_c0, halfval, "");
2990 src_c1 = LLVMBuildExtractElement(ctx->ac.builder, sample_position, ctx->ac.i32_1, "");
2991 src_c1 = LLVMBuildFSub(ctx->ac.builder, src_c1, halfval, "");
2992 }
2993 interp_param = ctx->abi->lookup_interp_param(ctx->abi, var->data.interpolation, location);
2994
2995 if (location == INTERP_CENTER) {
2996 LLVMValueRef ij_out[2];
2997 LLVMValueRef ddxy_out = ac_build_ddxy_interp(&ctx->ac, interp_param);
2998
2999 /*
3000 * take the I then J parameters, and the DDX/Y for it, and
3001 * calculate the IJ inputs for the interpolator.
3002 * temp1 = ddx * offset/sample.x + I;
3003 * interp_param.I = ddy * offset/sample.y + temp1;
3004 * temp1 = ddx * offset/sample.x + J;
3005 * interp_param.J = ddy * offset/sample.y + temp1;
3006 */
3007 for (unsigned i = 0; i < 2; i++) {
3008 LLVMValueRef ix_ll = LLVMConstInt(ctx->ac.i32, i, false);
3009 LLVMValueRef iy_ll = LLVMConstInt(ctx->ac.i32, i + 2, false);
3010 LLVMValueRef ddx_el = LLVMBuildExtractElement(ctx->ac.builder,
3011 ddxy_out, ix_ll, "");
3012 LLVMValueRef ddy_el = LLVMBuildExtractElement(ctx->ac.builder,
3013 ddxy_out, iy_ll, "");
3014 LLVMValueRef interp_el = LLVMBuildExtractElement(ctx->ac.builder,
3015 interp_param, ix_ll, "");
3016 LLVMValueRef temp1, temp2;
3017
3018 interp_el = LLVMBuildBitCast(ctx->ac.builder, interp_el,
3019 ctx->ac.f32, "");
3020
3021 temp1 = ac_build_fmad(&ctx->ac, ddx_el, src_c0, interp_el);
3022 temp2 = ac_build_fmad(&ctx->ac, ddy_el, src_c1, temp1);
3023
3024 ij_out[i] = LLVMBuildBitCast(ctx->ac.builder,
3025 temp2, ctx->ac.i32, "");
3026 }
3027 interp_param = ac_build_gather_values(&ctx->ac, ij_out, 2);
3028
3029 }
3030
3031 LLVMValueRef attrib_idx = ctx->ac.i32_0;
3032 while(deref_instr->deref_type != nir_deref_type_var) {
3033 if (deref_instr->deref_type == nir_deref_type_array) {
3034 unsigned array_size = glsl_count_attribute_slots(deref_instr->type, false);
3035
3036 LLVMValueRef offset;
3037 if (nir_src_is_const(deref_instr->arr.index)) {
3038 offset = LLVMConstInt(ctx->ac.i32, array_size * nir_src_as_uint(deref_instr->arr.index), false);
3039 } else {
3040 LLVMValueRef indirect = get_src(ctx, deref_instr->arr.index);
3041
3042 offset = LLVMBuildMul(ctx->ac.builder, indirect,
3043 LLVMConstInt(ctx->ac.i32, array_size, false), "");
3044 }
3045
3046 attrib_idx = LLVMBuildAdd(ctx->ac.builder, attrib_idx, offset, "");
3047 deref_instr = nir_src_as_deref(deref_instr->parent);
3048 } else if (deref_instr->deref_type == nir_deref_type_struct) {
3049 LLVMValueRef offset;
3050 unsigned sidx = deref_instr->strct.index;
3051 deref_instr = nir_src_as_deref(deref_instr->parent);
3052 offset = LLVMConstInt(ctx->ac.i32, glsl_get_struct_location_offset(deref_instr->type, sidx), false);
3053 attrib_idx = LLVMBuildAdd(ctx->ac.builder, attrib_idx, offset, "");
3054 } else {
3055 unreachable("Unsupported deref type");
3056 }
3057
3058 }
3059
3060 unsigned attrib_size = glsl_count_attribute_slots(var->type, false);
3061 for (chan = 0; chan < 4; chan++) {
3062 LLVMValueRef gather = LLVMGetUndef(LLVMVectorType(ctx->ac.f32, attrib_size));
3063 LLVMValueRef llvm_chan = LLVMConstInt(ctx->ac.i32, chan, false);
3064
3065 for (unsigned idx = 0; idx < attrib_size; ++idx) {
3066 LLVMValueRef v, attr_number;
3067
3068 attr_number = LLVMConstInt(ctx->ac.i32, input_base + idx, false);
3069 if (interp_param) {
3070 interp_param = LLVMBuildBitCast(ctx->ac.builder,
3071 interp_param, ctx->ac.v2f32, "");
3072 LLVMValueRef i = LLVMBuildExtractElement(
3073 ctx->ac.builder, interp_param, ctx->ac.i32_0, "");
3074 LLVMValueRef j = LLVMBuildExtractElement(
3075 ctx->ac.builder, interp_param, ctx->ac.i32_1, "");
3076
3077 v = ac_build_fs_interp(&ctx->ac, llvm_chan, attr_number,
3078 ctx->abi->prim_mask, i, j);
3079 } else {
3080 v = ac_build_fs_interp_mov(&ctx->ac, LLVMConstInt(ctx->ac.i32, 2, false),
3081 llvm_chan, attr_number, ctx->abi->prim_mask);
3082 }
3083
3084 gather = LLVMBuildInsertElement(ctx->ac.builder, gather, v,
3085 LLVMConstInt(ctx->ac.i32, idx, false), "");
3086 }
3087
3088 result[chan] = LLVMBuildExtractElement(ctx->ac.builder, gather, attrib_idx, "");
3089
3090 }
3091 return ac_build_varying_gather_values(&ctx->ac, result, instr->num_components,
3092 var->data.location_frac);
3093 }
3094
3095 static void visit_intrinsic(struct ac_nir_context *ctx,
3096 nir_intrinsic_instr *instr)
3097 {
3098 LLVMValueRef result = NULL;
3099
3100 switch (instr->intrinsic) {
3101 case nir_intrinsic_ballot:
3102 result = ac_build_ballot(&ctx->ac, get_src(ctx, instr->src[0]));
3103 break;
3104 case nir_intrinsic_read_invocation:
3105 result = ac_build_readlane(&ctx->ac, get_src(ctx, instr->src[0]),
3106 get_src(ctx, instr->src[1]));
3107 break;
3108 case nir_intrinsic_read_first_invocation:
3109 result = ac_build_readlane(&ctx->ac, get_src(ctx, instr->src[0]), NULL);
3110 break;
3111 case nir_intrinsic_load_subgroup_invocation:
3112 result = ac_get_thread_id(&ctx->ac);
3113 break;
3114 case nir_intrinsic_load_work_group_id: {
3115 LLVMValueRef values[3];
3116
3117 for (int i = 0; i < 3; i++) {
3118 values[i] = ctx->abi->workgroup_ids[i] ?
3119 ctx->abi->workgroup_ids[i] : ctx->ac.i32_0;
3120 }
3121
3122 result = ac_build_gather_values(&ctx->ac, values, 3);
3123 break;
3124 }
3125 case nir_intrinsic_load_base_vertex:
3126 case nir_intrinsic_load_first_vertex:
3127 result = ctx->abi->load_base_vertex(ctx->abi);
3128 break;
3129 case nir_intrinsic_load_local_group_size:
3130 result = ctx->abi->load_local_group_size(ctx->abi);
3131 break;
3132 case nir_intrinsic_load_vertex_id:
3133 result = LLVMBuildAdd(ctx->ac.builder, ctx->abi->vertex_id,
3134 ctx->abi->base_vertex, "");
3135 break;
3136 case nir_intrinsic_load_vertex_id_zero_base: {
3137 result = ctx->abi->vertex_id;
3138 break;
3139 }
3140 case nir_intrinsic_load_local_invocation_id: {
3141 result = ctx->abi->local_invocation_ids;
3142 break;
3143 }
3144 case nir_intrinsic_load_base_instance:
3145 result = ctx->abi->start_instance;
3146 break;
3147 case nir_intrinsic_load_draw_id:
3148 result = ctx->abi->draw_id;
3149 break;
3150 case nir_intrinsic_load_view_index:
3151 result = ctx->abi->view_index;
3152 break;
3153 case nir_intrinsic_load_invocation_id:
3154 if (ctx->stage == MESA_SHADER_TESS_CTRL)
3155 result = ac_unpack_param(&ctx->ac, ctx->abi->tcs_rel_ids, 8, 5);
3156 else
3157 result = ctx->abi->gs_invocation_id;
3158 break;
3159 case nir_intrinsic_load_primitive_id:
3160 if (ctx->stage == MESA_SHADER_GEOMETRY) {
3161 result = ctx->abi->gs_prim_id;
3162 } else if (ctx->stage == MESA_SHADER_TESS_CTRL) {
3163 result = ctx->abi->tcs_patch_id;
3164 } else if (ctx->stage == MESA_SHADER_TESS_EVAL) {
3165 result = ctx->abi->tes_patch_id;
3166 } else
3167 fprintf(stderr, "Unknown primitive id intrinsic: %d", ctx->stage);
3168 break;
3169 case nir_intrinsic_load_sample_id:
3170 result = ac_unpack_param(&ctx->ac, ctx->abi->ancillary, 8, 4);
3171 break;
3172 case nir_intrinsic_load_sample_pos:
3173 result = load_sample_pos(ctx);
3174 break;
3175 case nir_intrinsic_load_sample_mask_in:
3176 result = ctx->abi->load_sample_mask_in(ctx->abi);
3177 break;
3178 case nir_intrinsic_load_frag_coord: {
3179 LLVMValueRef values[4] = {
3180 ctx->abi->frag_pos[0],
3181 ctx->abi->frag_pos[1],
3182 ctx->abi->frag_pos[2],
3183 ac_build_fdiv(&ctx->ac, ctx->ac.f32_1, ctx->abi->frag_pos[3])
3184 };
3185 result = ac_to_integer(&ctx->ac,
3186 ac_build_gather_values(&ctx->ac, values, 4));
3187 break;
3188 }
3189 case nir_intrinsic_load_front_face:
3190 result = ctx->abi->front_face;
3191 break;
3192 case nir_intrinsic_load_helper_invocation:
3193 result = ac_build_load_helper_invocation(&ctx->ac);
3194 break;
3195 case nir_intrinsic_load_instance_id:
3196 result = ctx->abi->instance_id;
3197 break;
3198 case nir_intrinsic_load_num_work_groups:
3199 result = ctx->abi->num_work_groups;
3200 break;
3201 case nir_intrinsic_load_local_invocation_index:
3202 result = visit_load_local_invocation_index(ctx);
3203 break;
3204 case nir_intrinsic_load_subgroup_id:
3205 result = visit_load_subgroup_id(ctx);
3206 break;
3207 case nir_intrinsic_load_num_subgroups:
3208 result = visit_load_num_subgroups(ctx);
3209 break;
3210 case nir_intrinsic_first_invocation:
3211 result = visit_first_invocation(ctx);
3212 break;
3213 case nir_intrinsic_load_push_constant:
3214 result = visit_load_push_constant(ctx, instr);
3215 break;
3216 case nir_intrinsic_vulkan_resource_index: {
3217 LLVMValueRef index = get_src(ctx, instr->src[0]);
3218 unsigned desc_set = nir_intrinsic_desc_set(instr);
3219 unsigned binding = nir_intrinsic_binding(instr);
3220
3221 result = ctx->abi->load_resource(ctx->abi, index, desc_set,
3222 binding);
3223 break;
3224 }
3225 case nir_intrinsic_vulkan_resource_reindex:
3226 result = visit_vulkan_resource_reindex(ctx, instr);
3227 break;
3228 case nir_intrinsic_store_ssbo:
3229 visit_store_ssbo(ctx, instr);
3230 break;
3231 case nir_intrinsic_load_ssbo:
3232 result = visit_load_buffer(ctx, instr);
3233 break;
3234 case nir_intrinsic_ssbo_atomic_add:
3235 case nir_intrinsic_ssbo_atomic_imin:
3236 case nir_intrinsic_ssbo_atomic_umin:
3237 case nir_intrinsic_ssbo_atomic_imax:
3238 case nir_intrinsic_ssbo_atomic_umax:
3239 case nir_intrinsic_ssbo_atomic_and:
3240 case nir_intrinsic_ssbo_atomic_or:
3241 case nir_intrinsic_ssbo_atomic_xor:
3242 case nir_intrinsic_ssbo_atomic_exchange:
3243 case nir_intrinsic_ssbo_atomic_comp_swap:
3244 result = visit_atomic_ssbo(ctx, instr);
3245 break;
3246 case nir_intrinsic_load_ubo:
3247 result = visit_load_ubo_buffer(ctx, instr);
3248 break;
3249 case nir_intrinsic_get_buffer_size:
3250 result = visit_get_buffer_size(ctx, instr);
3251 break;
3252 case nir_intrinsic_load_deref:
3253 result = visit_load_var(ctx, instr);
3254 break;
3255 case nir_intrinsic_store_deref:
3256 visit_store_var(ctx, instr);
3257 break;
3258 case nir_intrinsic_load_shared:
3259 result = visit_load_shared(ctx, instr);
3260 break;
3261 case nir_intrinsic_store_shared:
3262 visit_store_shared(ctx, instr);
3263 break;
3264 case nir_intrinsic_bindless_image_samples:
3265 result = visit_image_samples(ctx, instr, true);
3266 break;
3267 case nir_intrinsic_image_deref_samples:
3268 result = visit_image_samples(ctx, instr, false);
3269 break;
3270 case nir_intrinsic_bindless_image_load:
3271 result = visit_image_load(ctx, instr, true);
3272 break;
3273 case nir_intrinsic_image_deref_load:
3274 result = visit_image_load(ctx, instr, false);
3275 break;
3276 case nir_intrinsic_bindless_image_store:
3277 visit_image_store(ctx, instr, true);
3278 break;
3279 case nir_intrinsic_image_deref_store:
3280 visit_image_store(ctx, instr, false);
3281 break;
3282 case nir_intrinsic_bindless_image_atomic_add:
3283 case nir_intrinsic_bindless_image_atomic_min:
3284 case nir_intrinsic_bindless_image_atomic_max:
3285 case nir_intrinsic_bindless_image_atomic_and:
3286 case nir_intrinsic_bindless_image_atomic_or:
3287 case nir_intrinsic_bindless_image_atomic_xor:
3288 case nir_intrinsic_bindless_image_atomic_exchange:
3289 case nir_intrinsic_bindless_image_atomic_comp_swap:
3290 result = visit_image_atomic(ctx, instr, true);
3291 break;
3292 case nir_intrinsic_image_deref_atomic_add:
3293 case nir_intrinsic_image_deref_atomic_min:
3294 case nir_intrinsic_image_deref_atomic_max:
3295 case nir_intrinsic_image_deref_atomic_and:
3296 case nir_intrinsic_image_deref_atomic_or:
3297 case nir_intrinsic_image_deref_atomic_xor:
3298 case nir_intrinsic_image_deref_atomic_exchange:
3299 case nir_intrinsic_image_deref_atomic_comp_swap:
3300 result = visit_image_atomic(ctx, instr, false);
3301 break;
3302 case nir_intrinsic_bindless_image_size:
3303 result = visit_image_size(ctx, instr, true);
3304 break;
3305 case nir_intrinsic_image_deref_size:
3306 result = visit_image_size(ctx, instr, false);
3307 break;
3308 case nir_intrinsic_shader_clock:
3309 result = ac_build_shader_clock(&ctx->ac);
3310 break;
3311 case nir_intrinsic_discard:
3312 case nir_intrinsic_discard_if:
3313 emit_discard(ctx, instr);
3314 break;
3315 case nir_intrinsic_memory_barrier:
3316 case nir_intrinsic_group_memory_barrier:
3317 case nir_intrinsic_memory_barrier_atomic_counter:
3318 case nir_intrinsic_memory_barrier_buffer:
3319 case nir_intrinsic_memory_barrier_image:
3320 case nir_intrinsic_memory_barrier_shared:
3321 emit_membar(&ctx->ac, instr);
3322 break;
3323 case nir_intrinsic_barrier:
3324 ac_emit_barrier(&ctx->ac, ctx->stage);
3325 break;
3326 case nir_intrinsic_shared_atomic_add:
3327 case nir_intrinsic_shared_atomic_imin:
3328 case nir_intrinsic_shared_atomic_umin:
3329 case nir_intrinsic_shared_atomic_imax:
3330 case nir_intrinsic_shared_atomic_umax:
3331 case nir_intrinsic_shared_atomic_and:
3332 case nir_intrinsic_shared_atomic_or:
3333 case nir_intrinsic_shared_atomic_xor:
3334 case nir_intrinsic_shared_atomic_exchange:
3335 case nir_intrinsic_shared_atomic_comp_swap: {
3336 LLVMValueRef ptr = get_memory_ptr(ctx, instr->src[0]);
3337 result = visit_var_atomic(ctx, instr, ptr, 1);
3338 break;
3339 }
3340 case nir_intrinsic_deref_atomic_add:
3341 case nir_intrinsic_deref_atomic_imin:
3342 case nir_intrinsic_deref_atomic_umin:
3343 case nir_intrinsic_deref_atomic_imax:
3344 case nir_intrinsic_deref_atomic_umax:
3345 case nir_intrinsic_deref_atomic_and:
3346 case nir_intrinsic_deref_atomic_or:
3347 case nir_intrinsic_deref_atomic_xor:
3348 case nir_intrinsic_deref_atomic_exchange:
3349 case nir_intrinsic_deref_atomic_comp_swap: {
3350 LLVMValueRef ptr = get_src(ctx, instr->src[0]);
3351 result = visit_var_atomic(ctx, instr, ptr, 1);
3352 break;
3353 }
3354 case nir_intrinsic_interp_deref_at_centroid:
3355 case nir_intrinsic_interp_deref_at_sample:
3356 case nir_intrinsic_interp_deref_at_offset:
3357 result = visit_interp(ctx, instr);
3358 break;
3359 case nir_intrinsic_emit_vertex:
3360 ctx->abi->emit_vertex(ctx->abi, nir_intrinsic_stream_id(instr), ctx->abi->outputs);
3361 break;
3362 case nir_intrinsic_end_primitive:
3363 ctx->abi->emit_primitive(ctx->abi, nir_intrinsic_stream_id(instr));
3364 break;
3365 case nir_intrinsic_load_tess_coord:
3366 result = ctx->abi->load_tess_coord(ctx->abi);
3367 break;
3368 case nir_intrinsic_load_tess_level_outer:
3369 result = ctx->abi->load_tess_level(ctx->abi, VARYING_SLOT_TESS_LEVEL_OUTER);
3370 break;
3371 case nir_intrinsic_load_tess_level_inner:
3372 result = ctx->abi->load_tess_level(ctx->abi, VARYING_SLOT_TESS_LEVEL_INNER);
3373 break;
3374 case nir_intrinsic_load_patch_vertices_in:
3375 result = ctx->abi->load_patch_vertices_in(ctx->abi);
3376 break;
3377 case nir_intrinsic_vote_all: {
3378 LLVMValueRef tmp = ac_build_vote_all(&ctx->ac, get_src(ctx, instr->src[0]));
3379 result = LLVMBuildSExt(ctx->ac.builder, tmp, ctx->ac.i32, "");
3380 break;
3381 }
3382 case nir_intrinsic_vote_any: {
3383 LLVMValueRef tmp = ac_build_vote_any(&ctx->ac, get_src(ctx, instr->src[0]));
3384 result = LLVMBuildSExt(ctx->ac.builder, tmp, ctx->ac.i32, "");
3385 break;
3386 }
3387 case nir_intrinsic_shuffle:
3388 result = ac_build_shuffle(&ctx->ac, get_src(ctx, instr->src[0]),
3389 get_src(ctx, instr->src[1]));
3390 break;
3391 case nir_intrinsic_reduce:
3392 result = ac_build_reduce(&ctx->ac,
3393 get_src(ctx, instr->src[0]),
3394 instr->const_index[0],
3395 instr->const_index[1]);
3396 break;
3397 case nir_intrinsic_inclusive_scan:
3398 result = ac_build_inclusive_scan(&ctx->ac,
3399 get_src(ctx, instr->src[0]),
3400 instr->const_index[0]);
3401 break;
3402 case nir_intrinsic_exclusive_scan:
3403 result = ac_build_exclusive_scan(&ctx->ac,
3404 get_src(ctx, instr->src[0]),
3405 instr->const_index[0]);
3406 break;
3407 case nir_intrinsic_quad_broadcast: {
3408 unsigned lane = nir_src_as_uint(instr->src[1]);
3409 result = ac_build_quad_swizzle(&ctx->ac, get_src(ctx, instr->src[0]),
3410 lane, lane, lane, lane);
3411 break;
3412 }
3413 case nir_intrinsic_quad_swap_horizontal:
3414 result = ac_build_quad_swizzle(&ctx->ac, get_src(ctx, instr->src[0]), 1, 0, 3 ,2);
3415 break;
3416 case nir_intrinsic_quad_swap_vertical:
3417 result = ac_build_quad_swizzle(&ctx->ac, get_src(ctx, instr->src[0]), 2, 3, 0 ,1);
3418 break;
3419 case nir_intrinsic_quad_swap_diagonal:
3420 result = ac_build_quad_swizzle(&ctx->ac, get_src(ctx, instr->src[0]), 3, 2, 1 ,0);
3421 break;
3422 default:
3423 fprintf(stderr, "Unknown intrinsic: ");
3424 nir_print_instr(&instr->instr, stderr);
3425 fprintf(stderr, "\n");
3426 break;
3427 }
3428 if (result) {
3429 ctx->ssa_defs[instr->dest.ssa.index] = result;
3430 }
3431 }
3432
3433 static LLVMValueRef get_bindless_index_from_uniform(struct ac_nir_context *ctx,
3434 unsigned base_index,
3435 unsigned constant_index,
3436 LLVMValueRef dynamic_index)
3437 {
3438 LLVMValueRef offset = LLVMConstInt(ctx->ac.i32, base_index * 4, 0);
3439 LLVMValueRef index = LLVMBuildAdd(ctx->ac.builder, dynamic_index,
3440 LLVMConstInt(ctx->ac.i32, constant_index, 0), "");
3441
3442 /* Bindless uniforms are 64bit so multiple index by 8 */
3443 index = LLVMBuildMul(ctx->ac.builder, index, LLVMConstInt(ctx->ac.i32, 8, 0), "");
3444 offset = LLVMBuildAdd(ctx->ac.builder, offset, index, "");
3445
3446 LLVMValueRef ubo_index = ctx->abi->load_ubo(ctx->abi, ctx->ac.i32_0);
3447
3448 LLVMValueRef ret = ac_build_buffer_load(&ctx->ac, ubo_index, 1, NULL, offset,
3449 NULL, 0, false, false, true, true);
3450
3451 return LLVMBuildBitCast(ctx->ac.builder, ret, ctx->ac.i32, "");
3452 }
3453
3454 static LLVMValueRef get_sampler_desc(struct ac_nir_context *ctx,
3455 nir_deref_instr *deref_instr,
3456 enum ac_descriptor_type desc_type,
3457 const nir_instr *instr,
3458 bool image, bool write)
3459 {
3460 LLVMValueRef index = NULL;
3461 unsigned constant_index = 0;
3462 unsigned descriptor_set;
3463 unsigned base_index;
3464 bool bindless = false;
3465
3466 if (!deref_instr) {
3467 descriptor_set = 0;
3468 if (image) {
3469 nir_intrinsic_instr *img_instr = nir_instr_as_intrinsic(instr);
3470 base_index = 0;
3471 bindless = true;
3472 index = get_src(ctx, img_instr->src[0]);
3473 } else {
3474 nir_tex_instr *tex_instr = nir_instr_as_tex(instr);
3475 int sampSrcIdx = nir_tex_instr_src_index(tex_instr,
3476 nir_tex_src_sampler_handle);
3477 if (sampSrcIdx != -1) {
3478 base_index = 0;
3479 bindless = true;
3480 index = get_src(ctx, tex_instr->src[sampSrcIdx].src);
3481 } else {
3482 assert(tex_instr && !image);
3483 base_index = tex_instr->sampler_index;
3484 }
3485 }
3486 } else {
3487 while(deref_instr->deref_type != nir_deref_type_var) {
3488 if (deref_instr->deref_type == nir_deref_type_array) {
3489 unsigned array_size = glsl_get_aoa_size(deref_instr->type);
3490 if (!array_size)
3491 array_size = 1;
3492
3493 if (nir_src_is_const(deref_instr->arr.index)) {
3494 constant_index += array_size * nir_src_as_uint(deref_instr->arr.index);
3495 } else {
3496 LLVMValueRef indirect = get_src(ctx, deref_instr->arr.index);
3497
3498 indirect = LLVMBuildMul(ctx->ac.builder, indirect,
3499 LLVMConstInt(ctx->ac.i32, array_size, false), "");
3500
3501 if (!index)
3502 index = indirect;
3503 else
3504 index = LLVMBuildAdd(ctx->ac.builder, index, indirect, "");
3505 }
3506
3507 deref_instr = nir_src_as_deref(deref_instr->parent);
3508 } else if (deref_instr->deref_type == nir_deref_type_struct) {
3509 unsigned sidx = deref_instr->strct.index;
3510 deref_instr = nir_src_as_deref(deref_instr->parent);
3511 constant_index += glsl_get_struct_location_offset(deref_instr->type, sidx);
3512 } else {
3513 unreachable("Unsupported deref type");
3514 }
3515 }
3516 descriptor_set = deref_instr->var->data.descriptor_set;
3517
3518 if (deref_instr->var->data.bindless) {
3519 /* For now just assert on unhandled variable types */
3520 assert(deref_instr->var->data.mode == nir_var_uniform);
3521
3522 base_index = deref_instr->var->data.driver_location;
3523 bindless = true;
3524
3525 index = index ? index : ctx->ac.i32_0;
3526 index = get_bindless_index_from_uniform(ctx, base_index,
3527 constant_index, index);
3528 } else
3529 base_index = deref_instr->var->data.binding;
3530 }
3531
3532 return ctx->abi->load_sampler_desc(ctx->abi,
3533 descriptor_set,
3534 base_index,
3535 constant_index, index,
3536 desc_type, image, write, bindless);
3537 }
3538
3539 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
3540 *
3541 * SI-CI:
3542 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
3543 * filtering manually. The driver sets img7 to a mask clearing
3544 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
3545 * s_and_b32 samp0, samp0, img7
3546 *
3547 * VI:
3548 * The ANISO_OVERRIDE sampler field enables this fix in TA.
3549 */
3550 static LLVMValueRef sici_fix_sampler_aniso(struct ac_nir_context *ctx,
3551 LLVMValueRef res, LLVMValueRef samp)
3552 {
3553 LLVMBuilderRef builder = ctx->ac.builder;
3554 LLVMValueRef img7, samp0;
3555
3556 if (ctx->ac.chip_class >= VI)
3557 return samp;
3558
3559 img7 = LLVMBuildExtractElement(builder, res,
3560 LLVMConstInt(ctx->ac.i32, 7, 0), "");
3561 samp0 = LLVMBuildExtractElement(builder, samp,
3562 LLVMConstInt(ctx->ac.i32, 0, 0), "");
3563 samp0 = LLVMBuildAnd(builder, samp0, img7, "");
3564 return LLVMBuildInsertElement(builder, samp, samp0,
3565 LLVMConstInt(ctx->ac.i32, 0, 0), "");
3566 }
3567
3568 static void tex_fetch_ptrs(struct ac_nir_context *ctx,
3569 nir_tex_instr *instr,
3570 LLVMValueRef *res_ptr, LLVMValueRef *samp_ptr,
3571 LLVMValueRef *fmask_ptr)
3572 {
3573 nir_deref_instr *texture_deref_instr = NULL;
3574 nir_deref_instr *sampler_deref_instr = NULL;
3575
3576 for (unsigned i = 0; i < instr->num_srcs; i++) {
3577 switch (instr->src[i].src_type) {
3578 case nir_tex_src_texture_deref:
3579 texture_deref_instr = nir_src_as_deref(instr->src[i].src);
3580 break;
3581 case nir_tex_src_sampler_deref:
3582 sampler_deref_instr = nir_src_as_deref(instr->src[i].src);
3583 break;
3584 default:
3585 break;
3586 }
3587 }
3588
3589 if (!sampler_deref_instr)
3590 sampler_deref_instr = texture_deref_instr;
3591
3592 if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF)
3593 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, AC_DESC_BUFFER, &instr->instr, false, false);
3594 else
3595 *res_ptr = get_sampler_desc(ctx, texture_deref_instr, AC_DESC_IMAGE, &instr->instr, false, false);
3596 if (samp_ptr) {
3597 *samp_ptr = get_sampler_desc(ctx, sampler_deref_instr, AC_DESC_SAMPLER, &instr->instr, false, false);
3598 if (instr->sampler_dim < GLSL_SAMPLER_DIM_RECT)
3599 *samp_ptr = sici_fix_sampler_aniso(ctx, *res_ptr, *samp_ptr);
3600 }
3601 if (fmask_ptr && (instr->op == nir_texop_txf_ms ||
3602 instr->op == nir_texop_samples_identical))
3603 *fmask_ptr = get_sampler_desc(ctx, texture_deref_instr, AC_DESC_FMASK, &instr->instr, false, false);
3604 }
3605
3606 static LLVMValueRef apply_round_slice(struct ac_llvm_context *ctx,
3607 LLVMValueRef coord)
3608 {
3609 coord = ac_to_float(ctx, coord);
3610 coord = ac_build_round(ctx, coord);
3611 coord = ac_to_integer(ctx, coord);
3612 return coord;
3613 }
3614
3615 static void visit_tex(struct ac_nir_context *ctx, nir_tex_instr *instr)
3616 {
3617 LLVMValueRef result = NULL;
3618 struct ac_image_args args = { 0 };
3619 LLVMValueRef fmask_ptr = NULL, sample_index = NULL;
3620 LLVMValueRef ddx = NULL, ddy = NULL;
3621 unsigned offset_src = 0;
3622
3623 tex_fetch_ptrs(ctx, instr, &args.resource, &args.sampler, &fmask_ptr);
3624
3625 for (unsigned i = 0; i < instr->num_srcs; i++) {
3626 switch (instr->src[i].src_type) {
3627 case nir_tex_src_coord: {
3628 LLVMValueRef coord = get_src(ctx, instr->src[i].src);
3629 for (unsigned chan = 0; chan < instr->coord_components; ++chan)
3630 args.coords[chan] = ac_llvm_extract_elem(&ctx->ac, coord, chan);
3631 break;
3632 }
3633 case nir_tex_src_projector:
3634 break;
3635 case nir_tex_src_comparator:
3636 if (instr->is_shadow)
3637 args.compare = get_src(ctx, instr->src[i].src);
3638 break;
3639 case nir_tex_src_offset:
3640 args.offset = get_src(ctx, instr->src[i].src);
3641 offset_src = i;
3642 break;
3643 case nir_tex_src_bias:
3644 if (instr->op == nir_texop_txb)
3645 args.bias = get_src(ctx, instr->src[i].src);
3646 break;
3647 case nir_tex_src_lod: {
3648 if (nir_src_is_const(instr->src[i].src) && nir_src_as_uint(instr->src[i].src) == 0)
3649 args.level_zero = true;
3650 else
3651 args.lod = get_src(ctx, instr->src[i].src);
3652 break;
3653 }
3654 case nir_tex_src_ms_index:
3655 sample_index = get_src(ctx, instr->src[i].src);
3656 break;
3657 case nir_tex_src_ms_mcs:
3658 break;
3659 case nir_tex_src_ddx:
3660 ddx = get_src(ctx, instr->src[i].src);
3661 break;
3662 case nir_tex_src_ddy:
3663 ddy = get_src(ctx, instr->src[i].src);
3664 break;
3665 case nir_tex_src_texture_offset:
3666 case nir_tex_src_sampler_offset:
3667 case nir_tex_src_plane:
3668 default:
3669 break;
3670 }
3671 }
3672
3673 if (instr->op == nir_texop_txs && instr->sampler_dim == GLSL_SAMPLER_DIM_BUF) {
3674 result = get_buffer_size(ctx, args.resource, true);
3675 goto write_result;
3676 }
3677
3678 if (instr->op == nir_texop_texture_samples) {
3679 LLVMValueRef res, samples, is_msaa;
3680 res = LLVMBuildBitCast(ctx->ac.builder, args.resource, ctx->ac.v8i32, "");
3681 samples = LLVMBuildExtractElement(ctx->ac.builder, res,
3682 LLVMConstInt(ctx->ac.i32, 3, false), "");
3683 is_msaa = LLVMBuildLShr(ctx->ac.builder, samples,
3684 LLVMConstInt(ctx->ac.i32, 28, false), "");
3685 is_msaa = LLVMBuildAnd(ctx->ac.builder, is_msaa,
3686 LLVMConstInt(ctx->ac.i32, 0xe, false), "");
3687 is_msaa = LLVMBuildICmp(ctx->ac.builder, LLVMIntEQ, is_msaa,
3688 LLVMConstInt(ctx->ac.i32, 0xe, false), "");
3689
3690 samples = LLVMBuildLShr(ctx->ac.builder, samples,
3691 LLVMConstInt(ctx->ac.i32, 16, false), "");
3692 samples = LLVMBuildAnd(ctx->ac.builder, samples,
3693 LLVMConstInt(ctx->ac.i32, 0xf, false), "");
3694 samples = LLVMBuildShl(ctx->ac.builder, ctx->ac.i32_1,
3695 samples, "");
3696 samples = LLVMBuildSelect(ctx->ac.builder, is_msaa, samples,
3697 ctx->ac.i32_1, "");
3698 result = samples;
3699 goto write_result;
3700 }
3701
3702 if (args.offset && instr->op != nir_texop_txf) {
3703 LLVMValueRef offset[3], pack;
3704 for (unsigned chan = 0; chan < 3; ++chan)
3705 offset[chan] = ctx->ac.i32_0;
3706
3707 unsigned num_components = ac_get_llvm_num_components(args.offset);
3708 for (unsigned chan = 0; chan < num_components; chan++) {
3709 offset[chan] = ac_llvm_extract_elem(&ctx->ac, args.offset, chan);
3710 offset[chan] = LLVMBuildAnd(ctx->ac.builder, offset[chan],
3711 LLVMConstInt(ctx->ac.i32, 0x3f, false), "");
3712 if (chan)
3713 offset[chan] = LLVMBuildShl(ctx->ac.builder, offset[chan],
3714 LLVMConstInt(ctx->ac.i32, chan * 8, false), "");
3715 }
3716 pack = LLVMBuildOr(ctx->ac.builder, offset[0], offset[1], "");
3717 pack = LLVMBuildOr(ctx->ac.builder, pack, offset[2], "");
3718 args.offset = pack;
3719 }
3720
3721 /* TC-compatible HTILE on radeonsi promotes Z16 and Z24 to Z32_FLOAT,
3722 * so the depth comparison value isn't clamped for Z16 and
3723 * Z24 anymore. Do it manually here.
3724 *
3725 * It's unnecessary if the original texture format was
3726 * Z32_FLOAT, but we don't know that here.
3727 */
3728 if (args.compare && ctx->ac.chip_class >= VI && ctx->abi->clamp_shadow_reference)
3729 args.compare = ac_build_clamp(&ctx->ac, ac_to_float(&ctx->ac, args.compare));
3730
3731 /* pack derivatives */
3732 if (ddx || ddy) {
3733 int num_src_deriv_channels, num_dest_deriv_channels;
3734 switch (instr->sampler_dim) {
3735 case GLSL_SAMPLER_DIM_3D:
3736 case GLSL_SAMPLER_DIM_CUBE:
3737 num_src_deriv_channels = 3;
3738 num_dest_deriv_channels = 3;
3739 break;
3740 case GLSL_SAMPLER_DIM_2D:
3741 default:
3742 num_src_deriv_channels = 2;
3743 num_dest_deriv_channels = 2;
3744 break;
3745 case GLSL_SAMPLER_DIM_1D:
3746 num_src_deriv_channels = 1;
3747 if (ctx->ac.chip_class >= GFX9) {
3748 num_dest_deriv_channels = 2;
3749 } else {
3750 num_dest_deriv_channels = 1;
3751 }
3752 break;
3753 }
3754
3755 for (unsigned i = 0; i < num_src_deriv_channels; i++) {
3756 args.derivs[i] = ac_to_float(&ctx->ac,
3757 ac_llvm_extract_elem(&ctx->ac, ddx, i));
3758 args.derivs[num_dest_deriv_channels + i] = ac_to_float(&ctx->ac,
3759 ac_llvm_extract_elem(&ctx->ac, ddy, i));
3760 }
3761 for (unsigned i = num_src_deriv_channels; i < num_dest_deriv_channels; i++) {
3762 args.derivs[i] = ctx->ac.f32_0;
3763 args.derivs[num_dest_deriv_channels + i] = ctx->ac.f32_0;
3764 }
3765 }
3766
3767 if (instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE && args.coords[0]) {
3768 for (unsigned chan = 0; chan < instr->coord_components; chan++)
3769 args.coords[chan] = ac_to_float(&ctx->ac, args.coords[chan]);
3770 if (instr->coord_components == 3)
3771 args.coords[3] = LLVMGetUndef(ctx->ac.f32);
3772 ac_prepare_cube_coords(&ctx->ac,
3773 instr->op == nir_texop_txd, instr->is_array,
3774 instr->op == nir_texop_lod, args.coords, args.derivs);
3775 }
3776
3777 /* Texture coordinates fixups */
3778 if (instr->coord_components > 1 &&
3779 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
3780 instr->is_array &&
3781 instr->op != nir_texop_txf) {
3782 args.coords[1] = apply_round_slice(&ctx->ac, args.coords[1]);
3783 }
3784
3785 if (instr->coord_components > 2 &&
3786 (instr->sampler_dim == GLSL_SAMPLER_DIM_2D ||
3787 instr->sampler_dim == GLSL_SAMPLER_DIM_MS ||
3788 instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS ||
3789 instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS_MS) &&
3790 instr->is_array &&
3791 instr->op != nir_texop_txf && instr->op != nir_texop_txf_ms) {
3792 args.coords[2] = apply_round_slice(&ctx->ac, args.coords[2]);
3793 }
3794
3795 if (ctx->ac.chip_class >= GFX9 &&
3796 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
3797 instr->op != nir_texop_lod) {
3798 LLVMValueRef filler;
3799 if (instr->op == nir_texop_txf)
3800 filler = ctx->ac.i32_0;
3801 else
3802 filler = LLVMConstReal(ctx->ac.f32, 0.5);
3803
3804 if (instr->is_array)
3805 args.coords[2] = args.coords[1];
3806 args.coords[1] = filler;
3807 }
3808
3809 /* Pack sample index */
3810 if (instr->op == nir_texop_txf_ms && sample_index)
3811 args.coords[instr->coord_components] = sample_index;
3812
3813 if (instr->op == nir_texop_samples_identical) {
3814 struct ac_image_args txf_args = { 0 };
3815 memcpy(txf_args.coords, args.coords, sizeof(txf_args.coords));
3816
3817 txf_args.dmask = 0xf;
3818 txf_args.resource = fmask_ptr;
3819 txf_args.dim = instr->is_array ? ac_image_2darray : ac_image_2d;
3820 result = build_tex_intrinsic(ctx, instr, &txf_args);
3821
3822 result = LLVMBuildExtractElement(ctx->ac.builder, result, ctx->ac.i32_0, "");
3823 result = emit_int_cmp(&ctx->ac, LLVMIntEQ, result, ctx->ac.i32_0);
3824 goto write_result;
3825 }
3826
3827 if (instr->sampler_dim == GLSL_SAMPLER_DIM_MS &&
3828 instr->op != nir_texop_txs) {
3829 unsigned sample_chan = instr->is_array ? 3 : 2;
3830 args.coords[sample_chan] = adjust_sample_index_using_fmask(
3831 &ctx->ac, args.coords[0], args.coords[1],
3832 instr->is_array ? args.coords[2] : NULL,
3833 args.coords[sample_chan], fmask_ptr);
3834 }
3835
3836 if (args.offset && instr->op == nir_texop_txf) {
3837 int num_offsets = instr->src[offset_src].src.ssa->num_components;
3838 num_offsets = MIN2(num_offsets, instr->coord_components);
3839 for (unsigned i = 0; i < num_offsets; ++i) {
3840 args.coords[i] = LLVMBuildAdd(
3841 ctx->ac.builder, args.coords[i],
3842 LLVMConstInt(ctx->ac.i32, nir_src_comp_as_uint(instr->src[offset_src].src, i), false), "");
3843 }
3844 args.offset = NULL;
3845 }
3846
3847 /* TODO TG4 support */
3848 args.dmask = 0xf;
3849 if (instr->op == nir_texop_tg4) {
3850 if (instr->is_shadow)
3851 args.dmask = 1;
3852 else
3853 args.dmask = 1 << instr->component;
3854 }
3855
3856 if (instr->sampler_dim != GLSL_SAMPLER_DIM_BUF)
3857 args.dim = get_ac_sampler_dim(&ctx->ac, instr->sampler_dim, instr->is_array);
3858 result = build_tex_intrinsic(ctx, instr, &args);
3859
3860 if (instr->op == nir_texop_query_levels)
3861 result = LLVMBuildExtractElement(ctx->ac.builder, result, LLVMConstInt(ctx->ac.i32, 3, false), "");
3862 else if (instr->is_shadow && instr->is_new_style_shadow &&
3863 instr->op != nir_texop_txs && instr->op != nir_texop_lod &&
3864 instr->op != nir_texop_tg4)
3865 result = LLVMBuildExtractElement(ctx->ac.builder, result, ctx->ac.i32_0, "");
3866 else if (instr->op == nir_texop_txs &&
3867 instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE &&
3868 instr->is_array) {
3869 LLVMValueRef two = LLVMConstInt(ctx->ac.i32, 2, false);
3870 LLVMValueRef six = LLVMConstInt(ctx->ac.i32, 6, false);
3871 LLVMValueRef z = LLVMBuildExtractElement(ctx->ac.builder, result, two, "");
3872 z = LLVMBuildSDiv(ctx->ac.builder, z, six, "");
3873 result = LLVMBuildInsertElement(ctx->ac.builder, result, z, two, "");
3874 } else if (ctx->ac.chip_class >= GFX9 &&
3875 instr->op == nir_texop_txs &&
3876 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
3877 instr->is_array) {
3878 LLVMValueRef two = LLVMConstInt(ctx->ac.i32, 2, false);
3879 LLVMValueRef layers = LLVMBuildExtractElement(ctx->ac.builder, result, two, "");
3880 result = LLVMBuildInsertElement(ctx->ac.builder, result, layers,
3881 ctx->ac.i32_1, "");
3882 } else if (instr->dest.ssa.num_components != 4)
3883 result = ac_trim_vector(&ctx->ac, result, instr->dest.ssa.num_components);
3884
3885 write_result:
3886 if (result) {
3887 assert(instr->dest.is_ssa);
3888 result = ac_to_integer(&ctx->ac, result);
3889 ctx->ssa_defs[instr->dest.ssa.index] = result;
3890 }
3891 }
3892
3893
3894 static void visit_phi(struct ac_nir_context *ctx, nir_phi_instr *instr)
3895 {
3896 LLVMTypeRef type = get_def_type(ctx, &instr->dest.ssa);
3897 LLVMValueRef result = LLVMBuildPhi(ctx->ac.builder, type, "");
3898
3899 ctx->ssa_defs[instr->dest.ssa.index] = result;
3900 _mesa_hash_table_insert(ctx->phis, instr, result);
3901 }
3902
3903 static void visit_post_phi(struct ac_nir_context *ctx,
3904 nir_phi_instr *instr,
3905 LLVMValueRef llvm_phi)
3906 {
3907 nir_foreach_phi_src(src, instr) {
3908 LLVMBasicBlockRef block = get_block(ctx, src->pred);
3909 LLVMValueRef llvm_src = get_src(ctx, src->src);
3910
3911 LLVMAddIncoming(llvm_phi, &llvm_src, &block, 1);
3912 }
3913 }
3914
3915 static void phi_post_pass(struct ac_nir_context *ctx)
3916 {
3917 hash_table_foreach(ctx->phis, entry) {
3918 visit_post_phi(ctx, (nir_phi_instr*)entry->key,
3919 (LLVMValueRef)entry->data);
3920 }
3921 }
3922
3923
3924 static void visit_ssa_undef(struct ac_nir_context *ctx,
3925 const nir_ssa_undef_instr *instr)
3926 {
3927 unsigned num_components = instr->def.num_components;
3928 LLVMTypeRef type = LLVMIntTypeInContext(ctx->ac.context, instr->def.bit_size);
3929 LLVMValueRef undef;
3930
3931 if (num_components == 1)
3932 undef = LLVMGetUndef(type);
3933 else {
3934 undef = LLVMGetUndef(LLVMVectorType(type, num_components));
3935 }
3936 ctx->ssa_defs[instr->def.index] = undef;
3937 }
3938
3939 static void visit_jump(struct ac_llvm_context *ctx,
3940 const nir_jump_instr *instr)
3941 {
3942 switch (instr->type) {
3943 case nir_jump_break:
3944 ac_build_break(ctx);
3945 break;
3946 case nir_jump_continue:
3947 ac_build_continue(ctx);
3948 break;
3949 default:
3950 fprintf(stderr, "Unknown NIR jump instr: ");
3951 nir_print_instr(&instr->instr, stderr);
3952 fprintf(stderr, "\n");
3953 abort();
3954 }
3955 }
3956
3957 static LLVMTypeRef
3958 glsl_base_to_llvm_type(struct ac_llvm_context *ac,
3959 enum glsl_base_type type)
3960 {
3961 switch (type) {
3962 case GLSL_TYPE_INT:
3963 case GLSL_TYPE_UINT:
3964 case GLSL_TYPE_BOOL:
3965 case GLSL_TYPE_SUBROUTINE:
3966 return ac->i32;
3967 case GLSL_TYPE_INT8:
3968 case GLSL_TYPE_UINT8:
3969 return ac->i8;
3970 case GLSL_TYPE_INT16:
3971 case GLSL_TYPE_UINT16:
3972 return ac->i16;
3973 case GLSL_TYPE_FLOAT:
3974 return ac->f32;
3975 case GLSL_TYPE_FLOAT16:
3976 return ac->f16;
3977 case GLSL_TYPE_INT64:
3978 case GLSL_TYPE_UINT64:
3979 return ac->i64;
3980 case GLSL_TYPE_DOUBLE:
3981 return ac->f64;
3982 default:
3983 unreachable("unknown GLSL type");
3984 }
3985 }
3986
3987 static LLVMTypeRef
3988 glsl_to_llvm_type(struct ac_llvm_context *ac,
3989 const struct glsl_type *type)
3990 {
3991 if (glsl_type_is_scalar(type)) {
3992 return glsl_base_to_llvm_type(ac, glsl_get_base_type(type));
3993 }
3994
3995 if (glsl_type_is_vector(type)) {
3996 return LLVMVectorType(
3997 glsl_base_to_llvm_type(ac, glsl_get_base_type(type)),
3998 glsl_get_vector_elements(type));
3999 }
4000
4001 if (glsl_type_is_matrix(type)) {
4002 return LLVMArrayType(
4003 glsl_to_llvm_type(ac, glsl_get_column_type(type)),
4004 glsl_get_matrix_columns(type));
4005 }
4006
4007 if (glsl_type_is_array(type)) {
4008 return LLVMArrayType(
4009 glsl_to_llvm_type(ac, glsl_get_array_element(type)),
4010 glsl_get_length(type));
4011 }
4012
4013 assert(glsl_type_is_struct_or_ifc(type));
4014
4015 LLVMTypeRef member_types[glsl_get_length(type)];
4016
4017 for (unsigned i = 0; i < glsl_get_length(type); i++) {
4018 member_types[i] =
4019 glsl_to_llvm_type(ac,
4020 glsl_get_struct_field(type, i));
4021 }
4022
4023 return LLVMStructTypeInContext(ac->context, member_types,
4024 glsl_get_length(type), false);
4025 }
4026
4027 static void visit_deref(struct ac_nir_context *ctx,
4028 nir_deref_instr *instr)
4029 {
4030 if (instr->mode != nir_var_mem_shared &&
4031 instr->mode != nir_var_mem_global)
4032 return;
4033
4034 LLVMValueRef result = NULL;
4035 switch(instr->deref_type) {
4036 case nir_deref_type_var: {
4037 struct hash_entry *entry = _mesa_hash_table_search(ctx->vars, instr->var);
4038 result = entry->data;
4039 break;
4040 }
4041 case nir_deref_type_struct:
4042 if (instr->mode == nir_var_mem_global) {
4043 nir_deref_instr *parent = nir_deref_instr_parent(instr);
4044 uint64_t offset = glsl_get_struct_field_offset(parent->type,
4045 instr->strct.index);
4046 result = ac_build_gep_ptr(&ctx->ac, get_src(ctx, instr->parent),
4047 LLVMConstInt(ctx->ac.i32, offset, 0));
4048 } else {
4049 result = ac_build_gep0(&ctx->ac, get_src(ctx, instr->parent),
4050 LLVMConstInt(ctx->ac.i32, instr->strct.index, 0));
4051 }
4052 break;
4053 case nir_deref_type_array:
4054 if (instr->mode == nir_var_mem_global) {
4055 nir_deref_instr *parent = nir_deref_instr_parent(instr);
4056 unsigned stride = glsl_get_explicit_stride(parent->type);
4057
4058 if ((glsl_type_is_matrix(parent->type) &&
4059 glsl_matrix_type_is_row_major(parent->type)) ||
4060 (glsl_type_is_vector(parent->type) && stride == 0))
4061 stride = type_scalar_size_bytes(parent->type);
4062
4063 assert(stride > 0);
4064 LLVMValueRef index = get_src(ctx, instr->arr.index);
4065 if (LLVMTypeOf(index) != ctx->ac.i64)
4066 index = LLVMBuildZExt(ctx->ac.builder, index, ctx->ac.i64, "");
4067
4068 LLVMValueRef offset = LLVMBuildMul(ctx->ac.builder, index, LLVMConstInt(ctx->ac.i64, stride, 0), "");
4069
4070 result = ac_build_gep_ptr(&ctx->ac, get_src(ctx, instr->parent), offset);
4071 } else {
4072 result = ac_build_gep0(&ctx->ac, get_src(ctx, instr->parent),
4073 get_src(ctx, instr->arr.index));
4074 }
4075 break;
4076 case nir_deref_type_ptr_as_array:
4077 if (instr->mode == nir_var_mem_global) {
4078 unsigned stride = nir_deref_instr_ptr_as_array_stride(instr);
4079
4080 LLVMValueRef index = get_src(ctx, instr->arr.index);
4081 if (LLVMTypeOf(index) != ctx->ac.i64)
4082 index = LLVMBuildZExt(ctx->ac.builder, index, ctx->ac.i64, "");
4083
4084 LLVMValueRef offset = LLVMBuildMul(ctx->ac.builder, index, LLVMConstInt(ctx->ac.i64, stride, 0), "");
4085
4086 result = ac_build_gep_ptr(&ctx->ac, get_src(ctx, instr->parent), offset);
4087 } else {
4088 result = ac_build_gep_ptr(&ctx->ac, get_src(ctx, instr->parent),
4089 get_src(ctx, instr->arr.index));
4090 }
4091 break;
4092 case nir_deref_type_cast: {
4093 result = get_src(ctx, instr->parent);
4094
4095 /* We can't use the structs from LLVM because the shader
4096 * specifies its own offsets. */
4097 LLVMTypeRef pointee_type = ctx->ac.i8;
4098 if (instr->mode == nir_var_mem_shared)
4099 pointee_type = glsl_to_llvm_type(&ctx->ac, instr->type);
4100
4101 unsigned address_space;
4102
4103 switch(instr->mode) {
4104 case nir_var_mem_shared:
4105 address_space = AC_ADDR_SPACE_LDS;
4106 break;
4107 case nir_var_mem_global:
4108 address_space = AC_ADDR_SPACE_GLOBAL;
4109 break;
4110 default:
4111 unreachable("Unhandled address space");
4112 }
4113
4114 LLVMTypeRef type = LLVMPointerType(pointee_type, address_space);
4115
4116 if (LLVMTypeOf(result) != type) {
4117 if (LLVMGetTypeKind(LLVMTypeOf(result)) == LLVMVectorTypeKind) {
4118 result = LLVMBuildBitCast(ctx->ac.builder, result,
4119 type, "");
4120 } else {
4121 result = LLVMBuildIntToPtr(ctx->ac.builder, result,
4122 type, "");
4123 }
4124 }
4125 break;
4126 }
4127 default:
4128 unreachable("Unhandled deref_instr deref type");
4129 }
4130
4131 ctx->ssa_defs[instr->dest.ssa.index] = result;
4132 }
4133
4134 static void visit_cf_list(struct ac_nir_context *ctx,
4135 struct exec_list *list);
4136
4137 static void visit_block(struct ac_nir_context *ctx, nir_block *block)
4138 {
4139 LLVMBasicBlockRef llvm_block = LLVMGetInsertBlock(ctx->ac.builder);
4140 nir_foreach_instr(instr, block)
4141 {
4142 switch (instr->type) {
4143 case nir_instr_type_alu:
4144 visit_alu(ctx, nir_instr_as_alu(instr));
4145 break;
4146 case nir_instr_type_load_const:
4147 visit_load_const(ctx, nir_instr_as_load_const(instr));
4148 break;
4149 case nir_instr_type_intrinsic:
4150 visit_intrinsic(ctx, nir_instr_as_intrinsic(instr));
4151 break;
4152 case nir_instr_type_tex:
4153 visit_tex(ctx, nir_instr_as_tex(instr));
4154 break;
4155 case nir_instr_type_phi:
4156 visit_phi(ctx, nir_instr_as_phi(instr));
4157 break;
4158 case nir_instr_type_ssa_undef:
4159 visit_ssa_undef(ctx, nir_instr_as_ssa_undef(instr));
4160 break;
4161 case nir_instr_type_jump:
4162 visit_jump(&ctx->ac, nir_instr_as_jump(instr));
4163 break;
4164 case nir_instr_type_deref:
4165 visit_deref(ctx, nir_instr_as_deref(instr));
4166 break;
4167 default:
4168 fprintf(stderr, "Unknown NIR instr type: ");
4169 nir_print_instr(instr, stderr);
4170 fprintf(stderr, "\n");
4171 abort();
4172 }
4173 }
4174
4175 _mesa_hash_table_insert(ctx->defs, block, llvm_block);
4176 }
4177
4178 static void visit_if(struct ac_nir_context *ctx, nir_if *if_stmt)
4179 {
4180 LLVMValueRef value = get_src(ctx, if_stmt->condition);
4181
4182 nir_block *then_block =
4183 (nir_block *) exec_list_get_head(&if_stmt->then_list);
4184
4185 ac_build_uif(&ctx->ac, value, then_block->index);
4186
4187 visit_cf_list(ctx, &if_stmt->then_list);
4188
4189 if (!exec_list_is_empty(&if_stmt->else_list)) {
4190 nir_block *else_block =
4191 (nir_block *) exec_list_get_head(&if_stmt->else_list);
4192
4193 ac_build_else(&ctx->ac, else_block->index);
4194 visit_cf_list(ctx, &if_stmt->else_list);
4195 }
4196
4197 ac_build_endif(&ctx->ac, then_block->index);
4198 }
4199
4200 static void visit_loop(struct ac_nir_context *ctx, nir_loop *loop)
4201 {
4202 nir_block *first_loop_block =
4203 (nir_block *) exec_list_get_head(&loop->body);
4204
4205 ac_build_bgnloop(&ctx->ac, first_loop_block->index);
4206
4207 visit_cf_list(ctx, &loop->body);
4208
4209 ac_build_endloop(&ctx->ac, first_loop_block->index);
4210 }
4211
4212 static void visit_cf_list(struct ac_nir_context *ctx,
4213 struct exec_list *list)
4214 {
4215 foreach_list_typed(nir_cf_node, node, node, list)
4216 {
4217 switch (node->type) {
4218 case nir_cf_node_block:
4219 visit_block(ctx, nir_cf_node_as_block(node));
4220 break;
4221
4222 case nir_cf_node_if:
4223 visit_if(ctx, nir_cf_node_as_if(node));
4224 break;
4225
4226 case nir_cf_node_loop:
4227 visit_loop(ctx, nir_cf_node_as_loop(node));
4228 break;
4229
4230 default:
4231 assert(0);
4232 }
4233 }
4234 }
4235
4236 void
4237 ac_handle_shader_output_decl(struct ac_llvm_context *ctx,
4238 struct ac_shader_abi *abi,
4239 struct nir_shader *nir,
4240 struct nir_variable *variable,
4241 gl_shader_stage stage)
4242 {
4243 unsigned output_loc = variable->data.driver_location / 4;
4244 unsigned attrib_count = glsl_count_attribute_slots(variable->type, false);
4245
4246 /* tess ctrl has it's own load/store paths for outputs */
4247 if (stage == MESA_SHADER_TESS_CTRL)
4248 return;
4249
4250 if (stage == MESA_SHADER_VERTEX ||
4251 stage == MESA_SHADER_TESS_EVAL ||
4252 stage == MESA_SHADER_GEOMETRY) {
4253 int idx = variable->data.location + variable->data.index;
4254 if (idx == VARYING_SLOT_CLIP_DIST0) {
4255 int length = nir->info.clip_distance_array_size +
4256 nir->info.cull_distance_array_size;
4257
4258 if (length > 4)
4259 attrib_count = 2;
4260 else
4261 attrib_count = 1;
4262 }
4263 }
4264
4265 bool is_16bit = glsl_type_is_16bit(glsl_without_array(variable->type));
4266 LLVMTypeRef type = is_16bit ? ctx->f16 : ctx->f32;
4267 for (unsigned i = 0; i < attrib_count; ++i) {
4268 for (unsigned chan = 0; chan < 4; chan++) {
4269 abi->outputs[ac_llvm_reg_index_soa(output_loc + i, chan)] =
4270 ac_build_alloca_undef(ctx, type, "");
4271 }
4272 }
4273 }
4274
4275 static void
4276 setup_locals(struct ac_nir_context *ctx,
4277 struct nir_function *func)
4278 {
4279 int i, j;
4280 ctx->num_locals = 0;
4281 nir_foreach_variable(variable, &func->impl->locals) {
4282 unsigned attrib_count = glsl_count_attribute_slots(variable->type, false);
4283 variable->data.driver_location = ctx->num_locals * 4;
4284 variable->data.location_frac = 0;
4285 ctx->num_locals += attrib_count;
4286 }
4287 ctx->locals = malloc(4 * ctx->num_locals * sizeof(LLVMValueRef));
4288 if (!ctx->locals)
4289 return;
4290
4291 for (i = 0; i < ctx->num_locals; i++) {
4292 for (j = 0; j < 4; j++) {
4293 ctx->locals[i * 4 + j] =
4294 ac_build_alloca_undef(&ctx->ac, ctx->ac.f32, "temp");
4295 }
4296 }
4297 }
4298
4299 static void
4300 setup_shared(struct ac_nir_context *ctx,
4301 struct nir_shader *nir)
4302 {
4303 nir_foreach_variable(variable, &nir->shared) {
4304 LLVMValueRef shared =
4305 LLVMAddGlobalInAddressSpace(
4306 ctx->ac.module, glsl_to_llvm_type(&ctx->ac, variable->type),
4307 variable->name ? variable->name : "",
4308 AC_ADDR_SPACE_LDS);
4309 _mesa_hash_table_insert(ctx->vars, variable, shared);
4310 }
4311 }
4312
4313 void ac_nir_translate(struct ac_llvm_context *ac, struct ac_shader_abi *abi,
4314 struct nir_shader *nir)
4315 {
4316 struct ac_nir_context ctx = {};
4317 struct nir_function *func;
4318
4319 ctx.ac = *ac;
4320 ctx.abi = abi;
4321
4322 ctx.stage = nir->info.stage;
4323
4324 ctx.main_function = LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx.ac.builder));
4325
4326 nir_foreach_variable(variable, &nir->outputs)
4327 ac_handle_shader_output_decl(&ctx.ac, ctx.abi, nir, variable,
4328 ctx.stage);
4329
4330 ctx.defs = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
4331 _mesa_key_pointer_equal);
4332 ctx.phis = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
4333 _mesa_key_pointer_equal);
4334 ctx.vars = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
4335 _mesa_key_pointer_equal);
4336
4337 func = (struct nir_function *)exec_list_get_head(&nir->functions);
4338
4339 nir_index_ssa_defs(func->impl);
4340 ctx.ssa_defs = calloc(func->impl->ssa_alloc, sizeof(LLVMValueRef));
4341
4342 setup_locals(&ctx, func);
4343
4344 if (gl_shader_stage_is_compute(nir->info.stage))
4345 setup_shared(&ctx, nir);
4346
4347 visit_cf_list(&ctx, &func->impl->body);
4348 phi_post_pass(&ctx);
4349
4350 if (!gl_shader_stage_is_compute(nir->info.stage))
4351 ctx.abi->emit_outputs(ctx.abi, AC_LLVM_MAX_OUTPUTS,
4352 ctx.abi->outputs);
4353
4354 free(ctx.locals);
4355 free(ctx.ssa_defs);
4356 ralloc_free(ctx.defs);
4357 ralloc_free(ctx.phis);
4358 ralloc_free(ctx.vars);
4359 }
4360
4361 void
4362 ac_lower_indirect_derefs(struct nir_shader *nir, enum chip_class chip_class)
4363 {
4364 /* While it would be nice not to have this flag, we are constrained
4365 * by the reality that LLVM 5.0 doesn't have working VGPR indexing
4366 * on GFX9.
4367 */
4368 bool llvm_has_working_vgpr_indexing = chip_class <= VI;
4369
4370 /* TODO: Indirect indexing of GS inputs is unimplemented.
4371 *
4372 * TCS and TES load inputs directly from LDS or offchip memory, so
4373 * indirect indexing is trivial.
4374 */
4375 nir_variable_mode indirect_mask = 0;
4376 if (nir->info.stage == MESA_SHADER_GEOMETRY ||
4377 (nir->info.stage != MESA_SHADER_TESS_CTRL &&
4378 nir->info.stage != MESA_SHADER_TESS_EVAL &&
4379 !llvm_has_working_vgpr_indexing)) {
4380 indirect_mask |= nir_var_shader_in;
4381 }
4382 if (!llvm_has_working_vgpr_indexing &&
4383 nir->info.stage != MESA_SHADER_TESS_CTRL)
4384 indirect_mask |= nir_var_shader_out;
4385
4386 /* TODO: We shouldn't need to do this, however LLVM isn't currently
4387 * smart enough to handle indirects without causing excess spilling
4388 * causing the gpu to hang.
4389 *
4390 * See the following thread for more details of the problem:
4391 * https://lists.freedesktop.org/archives/mesa-dev/2017-July/162106.html
4392 */
4393 indirect_mask |= nir_var_function_temp;
4394
4395 nir_lower_indirect_derefs(nir, indirect_mask);
4396 }
4397
4398 static unsigned
4399 get_inst_tessfactor_writemask(nir_intrinsic_instr *intrin)
4400 {
4401 if (intrin->intrinsic != nir_intrinsic_store_deref)
4402 return 0;
4403
4404 nir_variable *var =
4405 nir_deref_instr_get_variable(nir_src_as_deref(intrin->src[0]));
4406
4407 if (var->data.mode != nir_var_shader_out)
4408 return 0;
4409
4410 unsigned writemask = 0;
4411 const int location = var->data.location;
4412 unsigned first_component = var->data.location_frac;
4413 unsigned num_comps = intrin->dest.ssa.num_components;
4414
4415 if (location == VARYING_SLOT_TESS_LEVEL_INNER)
4416 writemask = ((1 << (num_comps + 1)) - 1) << first_component;
4417 else if (location == VARYING_SLOT_TESS_LEVEL_OUTER)
4418 writemask = (((1 << (num_comps + 1)) - 1) << first_component) << 4;
4419
4420 return writemask;
4421 }
4422
4423 static void
4424 scan_tess_ctrl(nir_cf_node *cf_node, unsigned *upper_block_tf_writemask,
4425 unsigned *cond_block_tf_writemask,
4426 bool *tessfactors_are_def_in_all_invocs, bool is_nested_cf)
4427 {
4428 switch (cf_node->type) {
4429 case nir_cf_node_block: {
4430 nir_block *block = nir_cf_node_as_block(cf_node);
4431 nir_foreach_instr(instr, block) {
4432 if (instr->type != nir_instr_type_intrinsic)
4433 continue;
4434
4435 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
4436 if (intrin->intrinsic == nir_intrinsic_barrier) {
4437
4438 /* If we find a barrier in nested control flow put this in the
4439 * too hard basket. In GLSL this is not possible but it is in
4440 * SPIR-V.
4441 */
4442 if (is_nested_cf) {
4443 *tessfactors_are_def_in_all_invocs = false;
4444 return;
4445 }
4446
4447 /* The following case must be prevented:
4448 * gl_TessLevelInner = ...;
4449 * barrier();
4450 * if (gl_InvocationID == 1)
4451 * gl_TessLevelInner = ...;
4452 *
4453 * If you consider disjoint code segments separated by barriers, each
4454 * such segment that writes tess factor channels should write the same
4455 * channels in all codepaths within that segment.
4456 */
4457 if (upper_block_tf_writemask || cond_block_tf_writemask) {
4458 /* Accumulate the result: */
4459 *tessfactors_are_def_in_all_invocs &=
4460 !(*cond_block_tf_writemask & ~(*upper_block_tf_writemask));
4461
4462 /* Analyze the next code segment from scratch. */
4463 *upper_block_tf_writemask = 0;
4464 *cond_block_tf_writemask = 0;
4465 }
4466 } else
4467 *upper_block_tf_writemask |= get_inst_tessfactor_writemask(intrin);
4468 }
4469
4470 break;
4471 }
4472 case nir_cf_node_if: {
4473 unsigned then_tessfactor_writemask = 0;
4474 unsigned else_tessfactor_writemask = 0;
4475
4476 nir_if *if_stmt = nir_cf_node_as_if(cf_node);
4477 foreach_list_typed(nir_cf_node, nested_node, node, &if_stmt->then_list) {
4478 scan_tess_ctrl(nested_node, &then_tessfactor_writemask,
4479 cond_block_tf_writemask,
4480 tessfactors_are_def_in_all_invocs, true);
4481 }
4482
4483 foreach_list_typed(nir_cf_node, nested_node, node, &if_stmt->else_list) {
4484 scan_tess_ctrl(nested_node, &else_tessfactor_writemask,
4485 cond_block_tf_writemask,
4486 tessfactors_are_def_in_all_invocs, true);
4487 }
4488
4489 if (then_tessfactor_writemask || else_tessfactor_writemask) {
4490 /* If both statements write the same tess factor channels,
4491 * we can say that the upper block writes them too.
4492 */
4493 *upper_block_tf_writemask |= then_tessfactor_writemask &
4494 else_tessfactor_writemask;
4495 *cond_block_tf_writemask |= then_tessfactor_writemask |
4496 else_tessfactor_writemask;
4497 }
4498
4499 break;
4500 }
4501 case nir_cf_node_loop: {
4502 nir_loop *loop = nir_cf_node_as_loop(cf_node);
4503 foreach_list_typed(nir_cf_node, nested_node, node, &loop->body) {
4504 scan_tess_ctrl(nested_node, cond_block_tf_writemask,
4505 cond_block_tf_writemask,
4506 tessfactors_are_def_in_all_invocs, true);
4507 }
4508
4509 break;
4510 }
4511 default:
4512 unreachable("unknown cf node type");
4513 }
4514 }
4515
4516 bool
4517 ac_are_tessfactors_def_in_all_invocs(const struct nir_shader *nir)
4518 {
4519 assert(nir->info.stage == MESA_SHADER_TESS_CTRL);
4520
4521 /* The pass works as follows:
4522 * If all codepaths write tess factors, we can say that all
4523 * invocations define tess factors.
4524 *
4525 * Each tess factor channel is tracked separately.
4526 */
4527 unsigned main_block_tf_writemask = 0; /* if main block writes tess factors */
4528 unsigned cond_block_tf_writemask = 0; /* if cond block writes tess factors */
4529
4530 /* Initial value = true. Here the pass will accumulate results from
4531 * multiple segments surrounded by barriers. If tess factors aren't
4532 * written at all, it's a shader bug and we don't care if this will be
4533 * true.
4534 */
4535 bool tessfactors_are_def_in_all_invocs = true;
4536
4537 nir_foreach_function(function, nir) {
4538 if (function->impl) {
4539 foreach_list_typed(nir_cf_node, node, node, &function->impl->body) {
4540 scan_tess_ctrl(node, &main_block_tf_writemask,
4541 &cond_block_tf_writemask,
4542 &tessfactors_are_def_in_all_invocs,
4543 false);
4544 }
4545 }
4546 }
4547
4548 /* Accumulate the result for the last code segment separated by a
4549 * barrier.
4550 */
4551 if (main_block_tf_writemask || cond_block_tf_writemask) {
4552 tessfactors_are_def_in_all_invocs &=
4553 !(cond_block_tf_writemask & ~main_block_tf_writemask);
4554 }
4555
4556 return tessfactors_are_def_in_all_invocs;
4557 }