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