b77d62a39b00868559eb33df964094a3bac5b478
[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 /* Fixup for GFX9 which allocates 1D textures as 2D. */
1313 if (instr->op == nir_texop_lod && ctx->ac.chip_class >= GFX9) {
1314 if ((args->dim == ac_image_2darray ||
1315 args->dim == ac_image_2d) && !args->coords[1]) {
1316 args->coords[1] = ctx->ac.i32_0;
1317 }
1318 }
1319
1320 args->attributes = AC_FUNC_ATTR_READNONE;
1321 return ac_build_image_opcode(&ctx->ac, args);
1322 }
1323
1324 static LLVMValueRef visit_vulkan_resource_reindex(struct ac_nir_context *ctx,
1325 nir_intrinsic_instr *instr)
1326 {
1327 LLVMValueRef ptr = get_src(ctx, instr->src[0]);
1328 LLVMValueRef index = get_src(ctx, instr->src[1]);
1329
1330 LLVMValueRef result = LLVMBuildGEP(ctx->ac.builder, ptr, &index, 1, "");
1331 LLVMSetMetadata(result, ctx->ac.uniform_md_kind, ctx->ac.empty_md);
1332 return result;
1333 }
1334
1335 static LLVMValueRef visit_load_push_constant(struct ac_nir_context *ctx,
1336 nir_intrinsic_instr *instr)
1337 {
1338 LLVMValueRef ptr, addr;
1339
1340 addr = LLVMConstInt(ctx->ac.i32, nir_intrinsic_base(instr), 0);
1341 addr = LLVMBuildAdd(ctx->ac.builder, addr,
1342 get_src(ctx, instr->src[0]), "");
1343
1344 ptr = ac_build_gep0(&ctx->ac, ctx->abi->push_constants, addr);
1345 ptr = ac_cast_ptr(&ctx->ac, ptr, get_def_type(ctx, &instr->dest.ssa));
1346
1347 return LLVMBuildLoad(ctx->ac.builder, ptr, "");
1348 }
1349
1350 static LLVMValueRef visit_get_buffer_size(struct ac_nir_context *ctx,
1351 const nir_intrinsic_instr *instr)
1352 {
1353 LLVMValueRef index = get_src(ctx, instr->src[0]);
1354
1355 return get_buffer_size(ctx, ctx->abi->load_ssbo(ctx->abi, index, false), false);
1356 }
1357
1358 static uint32_t widen_mask(uint32_t mask, unsigned multiplier)
1359 {
1360 uint32_t new_mask = 0;
1361 for(unsigned i = 0; i < 32 && (1u << i) <= mask; ++i)
1362 if (mask & (1u << i))
1363 new_mask |= ((1u << multiplier) - 1u) << (i * multiplier);
1364 return new_mask;
1365 }
1366
1367 static LLVMValueRef extract_vector_range(struct ac_llvm_context *ctx, LLVMValueRef src,
1368 unsigned start, unsigned count)
1369 {
1370 LLVMTypeRef type = LLVMTypeOf(src);
1371
1372 if (LLVMGetTypeKind(type) != LLVMVectorTypeKind) {
1373 assert(start == 0);
1374 assert(count == 1);
1375 return src;
1376 }
1377
1378 unsigned src_elements = LLVMGetVectorSize(type);
1379 assert(start < src_elements);
1380 assert(start + count <= src_elements);
1381
1382 if (start == 0 && count == src_elements)
1383 return src;
1384
1385 if (count == 1)
1386 return LLVMBuildExtractElement(ctx->builder, src, LLVMConstInt(ctx->i32, start, false), "");
1387
1388 assert(count <= 8);
1389 LLVMValueRef indices[8];
1390 for (unsigned i = 0; i < count; ++i)
1391 indices[i] = LLVMConstInt(ctx->i32, start + i, false);
1392
1393 LLVMValueRef swizzle = LLVMConstVector(indices, count);
1394 return LLVMBuildShuffleVector(ctx->builder, src, src, swizzle, "");
1395 }
1396
1397 static void visit_store_ssbo(struct ac_nir_context *ctx,
1398 nir_intrinsic_instr *instr)
1399 {
1400 const char *store_name;
1401 LLVMValueRef src_data = get_src(ctx, instr->src[0]);
1402 LLVMTypeRef data_type = ctx->ac.f32;
1403 int elem_size_mult = ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src_data)) / 32;
1404 int components_32bit = elem_size_mult * instr->num_components;
1405 unsigned writemask = nir_intrinsic_write_mask(instr);
1406 LLVMValueRef base_data, base_offset;
1407 LLVMValueRef params[6];
1408
1409 params[1] = ctx->abi->load_ssbo(ctx->abi,
1410 get_src(ctx, instr->src[1]), true);
1411 params[2] = ctx->ac.i32_0; /* vindex */
1412 params[4] = ctx->ac.i1false; /* glc */
1413 params[5] = ctx->ac.i1false; /* slc */
1414
1415 if (components_32bit > 1)
1416 data_type = LLVMVectorType(ctx->ac.f32, components_32bit);
1417
1418 writemask = widen_mask(writemask, elem_size_mult);
1419
1420 base_data = ac_to_float(&ctx->ac, src_data);
1421 base_data = ac_trim_vector(&ctx->ac, base_data, instr->num_components);
1422 base_data = LLVMBuildBitCast(ctx->ac.builder, base_data,
1423 data_type, "");
1424 base_offset = get_src(ctx, instr->src[2]); /* voffset */
1425 while (writemask) {
1426 int start, count;
1427 LLVMValueRef data;
1428 LLVMValueRef offset;
1429
1430 u_bit_scan_consecutive_range(&writemask, &start, &count);
1431
1432 /* Due to an LLVM limitation, split 3-element writes
1433 * into a 2-element and a 1-element write. */
1434 if (count == 3) {
1435 writemask |= 1 << (start + 2);
1436 count = 2;
1437 }
1438
1439 if (count > 4) {
1440 writemask |= ((1u << (count - 4)) - 1u) << (start + 4);
1441 count = 4;
1442 }
1443
1444 if (count == 4) {
1445 store_name = "llvm.amdgcn.buffer.store.v4f32";
1446 } else if (count == 2) {
1447 store_name = "llvm.amdgcn.buffer.store.v2f32";
1448
1449 } else {
1450 assert(count == 1);
1451 store_name = "llvm.amdgcn.buffer.store.f32";
1452 }
1453 data = extract_vector_range(&ctx->ac, base_data, start, count);
1454
1455 offset = base_offset;
1456 if (start != 0) {
1457 offset = LLVMBuildAdd(ctx->ac.builder, offset, LLVMConstInt(ctx->ac.i32, start * 4, false), "");
1458 }
1459 params[0] = data;
1460 params[3] = offset;
1461 ac_build_intrinsic(&ctx->ac, store_name,
1462 ctx->ac.voidt, params, 6, 0);
1463 }
1464 }
1465
1466 static LLVMValueRef visit_atomic_ssbo(struct ac_nir_context *ctx,
1467 const nir_intrinsic_instr *instr)
1468 {
1469 const char *name;
1470 LLVMValueRef params[6];
1471 int arg_count = 0;
1472
1473 if (instr->intrinsic == nir_intrinsic_ssbo_atomic_comp_swap) {
1474 params[arg_count++] = ac_llvm_extract_elem(&ctx->ac, get_src(ctx, instr->src[3]), 0);
1475 }
1476 params[arg_count++] = ac_llvm_extract_elem(&ctx->ac, get_src(ctx, instr->src[2]), 0);
1477 params[arg_count++] = ctx->abi->load_ssbo(ctx->abi,
1478 get_src(ctx, instr->src[0]),
1479 true);
1480 params[arg_count++] = ctx->ac.i32_0; /* vindex */
1481 params[arg_count++] = get_src(ctx, instr->src[1]); /* voffset */
1482 params[arg_count++] = LLVMConstInt(ctx->ac.i1, 0, false); /* slc */
1483
1484 switch (instr->intrinsic) {
1485 case nir_intrinsic_ssbo_atomic_add:
1486 name = "llvm.amdgcn.buffer.atomic.add";
1487 break;
1488 case nir_intrinsic_ssbo_atomic_imin:
1489 name = "llvm.amdgcn.buffer.atomic.smin";
1490 break;
1491 case nir_intrinsic_ssbo_atomic_umin:
1492 name = "llvm.amdgcn.buffer.atomic.umin";
1493 break;
1494 case nir_intrinsic_ssbo_atomic_imax:
1495 name = "llvm.amdgcn.buffer.atomic.smax";
1496 break;
1497 case nir_intrinsic_ssbo_atomic_umax:
1498 name = "llvm.amdgcn.buffer.atomic.umax";
1499 break;
1500 case nir_intrinsic_ssbo_atomic_and:
1501 name = "llvm.amdgcn.buffer.atomic.and";
1502 break;
1503 case nir_intrinsic_ssbo_atomic_or:
1504 name = "llvm.amdgcn.buffer.atomic.or";
1505 break;
1506 case nir_intrinsic_ssbo_atomic_xor:
1507 name = "llvm.amdgcn.buffer.atomic.xor";
1508 break;
1509 case nir_intrinsic_ssbo_atomic_exchange:
1510 name = "llvm.amdgcn.buffer.atomic.swap";
1511 break;
1512 case nir_intrinsic_ssbo_atomic_comp_swap:
1513 name = "llvm.amdgcn.buffer.atomic.cmpswap";
1514 break;
1515 default:
1516 abort();
1517 }
1518
1519 return ac_build_intrinsic(&ctx->ac, name, ctx->ac.i32, params, arg_count, 0);
1520 }
1521
1522 static LLVMValueRef visit_load_buffer(struct ac_nir_context *ctx,
1523 const nir_intrinsic_instr *instr)
1524 {
1525 LLVMValueRef results[2];
1526 int load_components;
1527 int num_components = instr->num_components;
1528 if (instr->dest.ssa.bit_size == 64)
1529 num_components *= 2;
1530
1531 for (int i = 0; i < num_components; i += load_components) {
1532 load_components = MIN2(num_components - i, 4);
1533 const char *load_name;
1534 LLVMTypeRef data_type = ctx->ac.f32;
1535 LLVMValueRef offset = LLVMConstInt(ctx->ac.i32, i * 4, false);
1536 offset = LLVMBuildAdd(ctx->ac.builder, get_src(ctx, instr->src[1]), offset, "");
1537
1538 if (load_components == 3)
1539 data_type = LLVMVectorType(ctx->ac.f32, 4);
1540 else if (load_components > 1)
1541 data_type = LLVMVectorType(ctx->ac.f32, load_components);
1542
1543 if (load_components >= 3)
1544 load_name = "llvm.amdgcn.buffer.load.v4f32";
1545 else if (load_components == 2)
1546 load_name = "llvm.amdgcn.buffer.load.v2f32";
1547 else if (load_components == 1)
1548 load_name = "llvm.amdgcn.buffer.load.f32";
1549 else
1550 unreachable("unhandled number of components");
1551
1552 LLVMValueRef params[] = {
1553 ctx->abi->load_ssbo(ctx->abi,
1554 get_src(ctx, instr->src[0]),
1555 false),
1556 ctx->ac.i32_0,
1557 offset,
1558 ctx->ac.i1false,
1559 ctx->ac.i1false,
1560 };
1561
1562 results[i > 0 ? 1 : 0] = ac_build_intrinsic(&ctx->ac, load_name, data_type, params, 5, 0);
1563 }
1564
1565 assume(results[0]);
1566 LLVMValueRef ret = results[0];
1567 if (num_components > 4 || num_components == 3) {
1568 LLVMValueRef masks[] = {
1569 LLVMConstInt(ctx->ac.i32, 0, false), LLVMConstInt(ctx->ac.i32, 1, false),
1570 LLVMConstInt(ctx->ac.i32, 2, false), LLVMConstInt(ctx->ac.i32, 3, false),
1571 LLVMConstInt(ctx->ac.i32, 4, false), LLVMConstInt(ctx->ac.i32, 5, false),
1572 LLVMConstInt(ctx->ac.i32, 6, false), LLVMConstInt(ctx->ac.i32, 7, false)
1573 };
1574
1575 if (num_components == 6) {
1576 /* we end up with a v4f32 and v2f32 but shuffle fails on that */
1577 results[1] = ac_build_expand_to_vec4(&ctx->ac, results[1], 4);
1578 }
1579
1580 LLVMValueRef swizzle = LLVMConstVector(masks, num_components);
1581 ret = LLVMBuildShuffleVector(ctx->ac.builder, results[0],
1582 results[num_components > 4 ? 1 : 0], swizzle, "");
1583 }
1584
1585 return LLVMBuildBitCast(ctx->ac.builder, ret,
1586 get_def_type(ctx, &instr->dest.ssa), "");
1587 }
1588
1589 static LLVMValueRef visit_load_ubo_buffer(struct ac_nir_context *ctx,
1590 const nir_intrinsic_instr *instr)
1591 {
1592 LLVMValueRef ret;
1593 LLVMValueRef rsrc = get_src(ctx, instr->src[0]);
1594 LLVMValueRef offset = get_src(ctx, instr->src[1]);
1595 int num_components = instr->num_components;
1596
1597 if (ctx->abi->load_ubo)
1598 rsrc = ctx->abi->load_ubo(ctx->abi, rsrc);
1599
1600 if (instr->dest.ssa.bit_size == 64)
1601 num_components *= 2;
1602
1603 ret = ac_build_buffer_load(&ctx->ac, rsrc, num_components, NULL, offset,
1604 NULL, 0, false, false, true, true);
1605 ret = ac_trim_vector(&ctx->ac, ret, num_components);
1606 return LLVMBuildBitCast(ctx->ac.builder, ret,
1607 get_def_type(ctx, &instr->dest.ssa), "");
1608 }
1609
1610 static void
1611 get_deref_offset(struct ac_nir_context *ctx, nir_deref_var *deref,
1612 bool vs_in, unsigned *vertex_index_out,
1613 LLVMValueRef *vertex_index_ref,
1614 unsigned *const_out, LLVMValueRef *indir_out)
1615 {
1616 unsigned const_offset = 0;
1617 nir_deref *tail = &deref->deref;
1618 LLVMValueRef offset = NULL;
1619
1620 if (vertex_index_out != NULL || vertex_index_ref != NULL) {
1621 tail = tail->child;
1622 nir_deref_array *deref_array = nir_deref_as_array(tail);
1623 if (vertex_index_out)
1624 *vertex_index_out = deref_array->base_offset;
1625
1626 if (vertex_index_ref) {
1627 LLVMValueRef vtx = LLVMConstInt(ctx->ac.i32, deref_array->base_offset, false);
1628 if (deref_array->deref_array_type == nir_deref_array_type_indirect) {
1629 vtx = LLVMBuildAdd(ctx->ac.builder, vtx, get_src(ctx, deref_array->indirect), "");
1630 }
1631 *vertex_index_ref = vtx;
1632 }
1633 }
1634
1635 if (deref->var->data.compact) {
1636 assert(tail->child->deref_type == nir_deref_type_array);
1637 assert(glsl_type_is_scalar(glsl_without_array(deref->var->type)));
1638 nir_deref_array *deref_array = nir_deref_as_array(tail->child);
1639 /* We always lower indirect dereferences for "compact" array vars. */
1640 assert(deref_array->deref_array_type == nir_deref_array_type_direct);
1641
1642 const_offset = deref_array->base_offset;
1643 goto out;
1644 }
1645
1646 while (tail->child != NULL) {
1647 const struct glsl_type *parent_type = tail->type;
1648 tail = tail->child;
1649
1650 if (tail->deref_type == nir_deref_type_array) {
1651 nir_deref_array *deref_array = nir_deref_as_array(tail);
1652 LLVMValueRef index, stride, local_offset;
1653 unsigned size = glsl_count_attribute_slots(tail->type, vs_in);
1654
1655 const_offset += size * deref_array->base_offset;
1656 if (deref_array->deref_array_type == nir_deref_array_type_direct)
1657 continue;
1658
1659 assert(deref_array->deref_array_type == nir_deref_array_type_indirect);
1660 index = get_src(ctx, deref_array->indirect);
1661 stride = LLVMConstInt(ctx->ac.i32, size, 0);
1662 local_offset = LLVMBuildMul(ctx->ac.builder, stride, index, "");
1663
1664 if (offset)
1665 offset = LLVMBuildAdd(ctx->ac.builder, offset, local_offset, "");
1666 else
1667 offset = local_offset;
1668 } else if (tail->deref_type == nir_deref_type_struct) {
1669 nir_deref_struct *deref_struct = nir_deref_as_struct(tail);
1670
1671 for (unsigned i = 0; i < deref_struct->index; i++) {
1672 const struct glsl_type *ft = glsl_get_struct_field(parent_type, i);
1673 const_offset += glsl_count_attribute_slots(ft, vs_in);
1674 }
1675 } else
1676 unreachable("unsupported deref type");
1677
1678 }
1679 out:
1680 if (const_offset && offset)
1681 offset = LLVMBuildAdd(ctx->ac.builder, offset,
1682 LLVMConstInt(ctx->ac.i32, const_offset, 0),
1683 "");
1684
1685 *const_out = const_offset;
1686 *indir_out = offset;
1687 }
1688
1689 static LLVMValueRef
1690 build_gep_for_deref(struct ac_nir_context *ctx,
1691 nir_deref_var *deref)
1692 {
1693 struct hash_entry *entry = _mesa_hash_table_search(ctx->vars, deref->var);
1694 assert(entry->data);
1695 LLVMValueRef val = entry->data;
1696 nir_deref *tail = deref->deref.child;
1697 while (tail != NULL) {
1698 LLVMValueRef offset;
1699 switch (tail->deref_type) {
1700 case nir_deref_type_array: {
1701 nir_deref_array *array = nir_deref_as_array(tail);
1702 offset = LLVMConstInt(ctx->ac.i32, array->base_offset, 0);
1703 if (array->deref_array_type ==
1704 nir_deref_array_type_indirect) {
1705 offset = LLVMBuildAdd(ctx->ac.builder, offset,
1706 get_src(ctx,
1707 array->indirect),
1708 "");
1709 }
1710 break;
1711 }
1712 case nir_deref_type_struct: {
1713 nir_deref_struct *deref_struct =
1714 nir_deref_as_struct(tail);
1715 offset = LLVMConstInt(ctx->ac.i32,
1716 deref_struct->index, 0);
1717 break;
1718 }
1719 default:
1720 unreachable("bad deref type");
1721 }
1722 val = ac_build_gep0(&ctx->ac, val, offset);
1723 tail = tail->child;
1724 }
1725 return val;
1726 }
1727
1728 static LLVMValueRef load_tess_varyings(struct ac_nir_context *ctx,
1729 nir_intrinsic_instr *instr,
1730 bool load_inputs)
1731 {
1732 LLVMValueRef result;
1733 LLVMValueRef vertex_index = NULL;
1734 LLVMValueRef indir_index = NULL;
1735 unsigned const_index = 0;
1736 unsigned location = instr->variables[0]->var->data.location;
1737 unsigned driver_location = instr->variables[0]->var->data.driver_location;
1738 const bool is_patch = instr->variables[0]->var->data.patch;
1739 const bool is_compact = instr->variables[0]->var->data.compact;
1740
1741 get_deref_offset(ctx, instr->variables[0],
1742 false, NULL, is_patch ? NULL : &vertex_index,
1743 &const_index, &indir_index);
1744
1745 LLVMTypeRef dest_type = get_def_type(ctx, &instr->dest.ssa);
1746
1747 LLVMTypeRef src_component_type;
1748 if (LLVMGetTypeKind(dest_type) == LLVMVectorTypeKind)
1749 src_component_type = LLVMGetElementType(dest_type);
1750 else
1751 src_component_type = dest_type;
1752
1753 result = ctx->abi->load_tess_varyings(ctx->abi, src_component_type,
1754 vertex_index, indir_index,
1755 const_index, location, driver_location,
1756 instr->variables[0]->var->data.location_frac,
1757 instr->num_components,
1758 is_patch, is_compact, load_inputs);
1759 return LLVMBuildBitCast(ctx->ac.builder, result, dest_type, "");
1760 }
1761
1762 static LLVMValueRef visit_load_var(struct ac_nir_context *ctx,
1763 nir_intrinsic_instr *instr)
1764 {
1765 LLVMValueRef values[8];
1766 int idx = instr->variables[0]->var->data.driver_location;
1767 int ve = instr->dest.ssa.num_components;
1768 unsigned comp = instr->variables[0]->var->data.location_frac;
1769 LLVMValueRef indir_index;
1770 LLVMValueRef ret;
1771 unsigned const_index;
1772 unsigned stride = instr->variables[0]->var->data.compact ? 1 : 4;
1773 bool vs_in = ctx->stage == MESA_SHADER_VERTEX &&
1774 instr->variables[0]->var->data.mode == nir_var_shader_in;
1775 get_deref_offset(ctx, instr->variables[0], vs_in, NULL, NULL,
1776 &const_index, &indir_index);
1777
1778 if (instr->dest.ssa.bit_size == 64)
1779 ve *= 2;
1780
1781 switch (instr->variables[0]->var->data.mode) {
1782 case nir_var_shader_in:
1783 if (ctx->stage == MESA_SHADER_TESS_CTRL ||
1784 ctx->stage == MESA_SHADER_TESS_EVAL) {
1785 return load_tess_varyings(ctx, instr, true);
1786 }
1787
1788 if (ctx->stage == MESA_SHADER_GEOMETRY) {
1789 LLVMTypeRef type = LLVMIntTypeInContext(ctx->ac.context, instr->dest.ssa.bit_size);
1790 LLVMValueRef indir_index;
1791 unsigned const_index, vertex_index;
1792 get_deref_offset(ctx, instr->variables[0],
1793 false, &vertex_index, NULL,
1794 &const_index, &indir_index);
1795
1796 return ctx->abi->load_inputs(ctx->abi, instr->variables[0]->var->data.location,
1797 instr->variables[0]->var->data.driver_location,
1798 instr->variables[0]->var->data.location_frac,
1799 instr->num_components, vertex_index, const_index, type);
1800 }
1801
1802 for (unsigned chan = comp; chan < ve + comp; chan++) {
1803 if (indir_index) {
1804 unsigned count = glsl_count_attribute_slots(
1805 instr->variables[0]->var->type,
1806 ctx->stage == MESA_SHADER_VERTEX);
1807 count -= chan / 4;
1808 LLVMValueRef tmp_vec = ac_build_gather_values_extended(
1809 &ctx->ac, ctx->abi->inputs + idx + chan, count,
1810 stride, false, true);
1811
1812 values[chan] = LLVMBuildExtractElement(ctx->ac.builder,
1813 tmp_vec,
1814 indir_index, "");
1815 } else
1816 values[chan] = ctx->abi->inputs[idx + chan + const_index * stride];
1817 }
1818 break;
1819 case nir_var_local:
1820 for (unsigned chan = 0; chan < ve; chan++) {
1821 if (indir_index) {
1822 unsigned count = glsl_count_attribute_slots(
1823 instr->variables[0]->var->type, false);
1824 count -= chan / 4;
1825 LLVMValueRef tmp_vec = ac_build_gather_values_extended(
1826 &ctx->ac, ctx->locals + idx + chan, count,
1827 stride, true, true);
1828
1829 values[chan] = LLVMBuildExtractElement(ctx->ac.builder,
1830 tmp_vec,
1831 indir_index, "");
1832 } else {
1833 values[chan] = LLVMBuildLoad(ctx->ac.builder, ctx->locals[idx + chan + const_index * stride], "");
1834 }
1835 }
1836 break;
1837 case nir_var_shared: {
1838 LLVMValueRef address = build_gep_for_deref(ctx,
1839 instr->variables[0]);
1840 LLVMValueRef val = LLVMBuildLoad(ctx->ac.builder, address, "");
1841 return LLVMBuildBitCast(ctx->ac.builder, val,
1842 get_def_type(ctx, &instr->dest.ssa),
1843 "");
1844 }
1845 case nir_var_shader_out:
1846 if (ctx->stage == MESA_SHADER_TESS_CTRL) {
1847 return load_tess_varyings(ctx, instr, false);
1848 }
1849
1850 for (unsigned chan = comp; chan < ve + comp; chan++) {
1851 if (indir_index) {
1852 unsigned count = glsl_count_attribute_slots(
1853 instr->variables[0]->var->type, false);
1854 count -= chan / 4;
1855 LLVMValueRef tmp_vec = ac_build_gather_values_extended(
1856 &ctx->ac, ctx->abi->outputs + idx + chan, count,
1857 stride, true, true);
1858
1859 values[chan] = LLVMBuildExtractElement(ctx->ac.builder,
1860 tmp_vec,
1861 indir_index, "");
1862 } else {
1863 values[chan] = LLVMBuildLoad(ctx->ac.builder,
1864 ctx->abi->outputs[idx + chan + const_index * stride],
1865 "");
1866 }
1867 }
1868 break;
1869 default:
1870 unreachable("unhandle variable mode");
1871 }
1872 ret = ac_build_varying_gather_values(&ctx->ac, values, ve, comp);
1873 return LLVMBuildBitCast(ctx->ac.builder, ret, get_def_type(ctx, &instr->dest.ssa), "");
1874 }
1875
1876 static void
1877 visit_store_var(struct ac_nir_context *ctx,
1878 nir_intrinsic_instr *instr)
1879 {
1880 LLVMValueRef temp_ptr, value;
1881 int idx = instr->variables[0]->var->data.driver_location;
1882 unsigned comp = instr->variables[0]->var->data.location_frac;
1883 LLVMValueRef src = ac_to_float(&ctx->ac, get_src(ctx, instr->src[0]));
1884 int writemask = instr->const_index[0];
1885 LLVMValueRef indir_index;
1886 unsigned const_index;
1887 get_deref_offset(ctx, instr->variables[0], false,
1888 NULL, NULL, &const_index, &indir_index);
1889
1890 if (ac_get_elem_bits(&ctx->ac, LLVMTypeOf(src)) == 64) {
1891
1892 src = LLVMBuildBitCast(ctx->ac.builder, src,
1893 LLVMVectorType(ctx->ac.f32, ac_get_llvm_num_components(src) * 2),
1894 "");
1895
1896 writemask = widen_mask(writemask, 2);
1897 }
1898
1899 writemask = writemask << comp;
1900
1901 switch (instr->variables[0]->var->data.mode) {
1902 case nir_var_shader_out:
1903
1904 if (ctx->stage == MESA_SHADER_TESS_CTRL) {
1905 LLVMValueRef vertex_index = NULL;
1906 LLVMValueRef indir_index = NULL;
1907 unsigned const_index = 0;
1908 const bool is_patch = instr->variables[0]->var->data.patch;
1909
1910 get_deref_offset(ctx, instr->variables[0],
1911 false, NULL, is_patch ? NULL : &vertex_index,
1912 &const_index, &indir_index);
1913
1914 ctx->abi->store_tcs_outputs(ctx->abi, instr->variables[0]->var,
1915 vertex_index, indir_index,
1916 const_index, src, writemask);
1917 return;
1918 }
1919
1920 for (unsigned chan = 0; chan < 8; chan++) {
1921 int stride = 4;
1922 if (!(writemask & (1 << chan)))
1923 continue;
1924
1925 value = ac_llvm_extract_elem(&ctx->ac, src, chan - comp);
1926
1927 if (instr->variables[0]->var->data.compact)
1928 stride = 1;
1929 if (indir_index) {
1930 unsigned count = glsl_count_attribute_slots(
1931 instr->variables[0]->var->type, false);
1932 count -= chan / 4;
1933 LLVMValueRef tmp_vec = ac_build_gather_values_extended(
1934 &ctx->ac, ctx->abi->outputs + idx + chan, count,
1935 stride, true, true);
1936
1937 tmp_vec = LLVMBuildInsertElement(ctx->ac.builder, tmp_vec,
1938 value, indir_index, "");
1939 build_store_values_extended(&ctx->ac, ctx->abi->outputs + idx + chan,
1940 count, stride, tmp_vec);
1941
1942 } else {
1943 temp_ptr = ctx->abi->outputs[idx + chan + const_index * stride];
1944
1945 LLVMBuildStore(ctx->ac.builder, value, temp_ptr);
1946 }
1947 }
1948 break;
1949 case nir_var_local:
1950 for (unsigned chan = 0; chan < 8; chan++) {
1951 if (!(writemask & (1 << chan)))
1952 continue;
1953
1954 value = ac_llvm_extract_elem(&ctx->ac, src, chan);
1955 if (indir_index) {
1956 unsigned count = glsl_count_attribute_slots(
1957 instr->variables[0]->var->type, false);
1958 count -= chan / 4;
1959 LLVMValueRef tmp_vec = ac_build_gather_values_extended(
1960 &ctx->ac, ctx->locals + idx + chan, count,
1961 4, true, true);
1962
1963 tmp_vec = LLVMBuildInsertElement(ctx->ac.builder, tmp_vec,
1964 value, indir_index, "");
1965 build_store_values_extended(&ctx->ac, ctx->locals + idx + chan,
1966 count, 4, tmp_vec);
1967 } else {
1968 temp_ptr = ctx->locals[idx + chan + const_index * 4];
1969
1970 LLVMBuildStore(ctx->ac.builder, value, temp_ptr);
1971 }
1972 }
1973 break;
1974 case nir_var_shared: {
1975 int writemask = instr->const_index[0];
1976 LLVMValueRef address = build_gep_for_deref(ctx,
1977 instr->variables[0]);
1978 LLVMValueRef val = get_src(ctx, instr->src[0]);
1979 unsigned components =
1980 glsl_get_vector_elements(
1981 nir_deref_tail(&instr->variables[0]->deref)->type);
1982 if (writemask == (1 << components) - 1) {
1983 val = LLVMBuildBitCast(
1984 ctx->ac.builder, val,
1985 LLVMGetElementType(LLVMTypeOf(address)), "");
1986 LLVMBuildStore(ctx->ac.builder, val, address);
1987 } else {
1988 for (unsigned chan = 0; chan < 4; chan++) {
1989 if (!(writemask & (1 << chan)))
1990 continue;
1991 LLVMValueRef ptr =
1992 LLVMBuildStructGEP(ctx->ac.builder,
1993 address, chan, "");
1994 LLVMValueRef src = ac_llvm_extract_elem(&ctx->ac, val,
1995 chan);
1996 src = LLVMBuildBitCast(
1997 ctx->ac.builder, src,
1998 LLVMGetElementType(LLVMTypeOf(ptr)), "");
1999 LLVMBuildStore(ctx->ac.builder, src, ptr);
2000 }
2001 }
2002 break;
2003 }
2004 default:
2005 break;
2006 }
2007 }
2008
2009 static int image_type_to_components_count(enum glsl_sampler_dim dim, bool array)
2010 {
2011 switch (dim) {
2012 case GLSL_SAMPLER_DIM_BUF:
2013 return 1;
2014 case GLSL_SAMPLER_DIM_1D:
2015 return array ? 2 : 1;
2016 case GLSL_SAMPLER_DIM_2D:
2017 return array ? 3 : 2;
2018 case GLSL_SAMPLER_DIM_MS:
2019 return array ? 4 : 3;
2020 case GLSL_SAMPLER_DIM_3D:
2021 case GLSL_SAMPLER_DIM_CUBE:
2022 return 3;
2023 case GLSL_SAMPLER_DIM_RECT:
2024 case GLSL_SAMPLER_DIM_SUBPASS:
2025 return 2;
2026 case GLSL_SAMPLER_DIM_SUBPASS_MS:
2027 return 3;
2028 default:
2029 break;
2030 }
2031 return 0;
2032 }
2033
2034
2035 /* Adjust the sample index according to FMASK.
2036 *
2037 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
2038 * which is the identity mapping. Each nibble says which physical sample
2039 * should be fetched to get that sample.
2040 *
2041 * For example, 0x11111100 means there are only 2 samples stored and
2042 * the second sample covers 3/4 of the pixel. When reading samples 0
2043 * and 1, return physical sample 0 (determined by the first two 0s
2044 * in FMASK), otherwise return physical sample 1.
2045 *
2046 * The sample index should be adjusted as follows:
2047 * sample_index = (fmask >> (sample_index * 4)) & 0xF;
2048 */
2049 static LLVMValueRef adjust_sample_index_using_fmask(struct ac_llvm_context *ctx,
2050 LLVMValueRef coord_x, LLVMValueRef coord_y,
2051 LLVMValueRef coord_z,
2052 LLVMValueRef sample_index,
2053 LLVMValueRef fmask_desc_ptr)
2054 {
2055 struct ac_image_args args = {0};
2056 LLVMValueRef res;
2057
2058 args.coords[0] = coord_x;
2059 args.coords[1] = coord_y;
2060 if (coord_z)
2061 args.coords[2] = coord_z;
2062
2063 args.opcode = ac_image_load;
2064 args.dim = coord_z ? ac_image_2darray : ac_image_2d;
2065 args.resource = fmask_desc_ptr;
2066 args.dmask = 0xf;
2067 args.attributes = AC_FUNC_ATTR_READNONE;
2068
2069 res = ac_build_image_opcode(ctx, &args);
2070
2071 res = ac_to_integer(ctx, res);
2072 LLVMValueRef four = LLVMConstInt(ctx->i32, 4, false);
2073 LLVMValueRef F = LLVMConstInt(ctx->i32, 0xf, false);
2074
2075 LLVMValueRef fmask = LLVMBuildExtractElement(ctx->builder,
2076 res,
2077 ctx->i32_0, "");
2078
2079 LLVMValueRef sample_index4 =
2080 LLVMBuildMul(ctx->builder, sample_index, four, "");
2081 LLVMValueRef shifted_fmask =
2082 LLVMBuildLShr(ctx->builder, fmask, sample_index4, "");
2083 LLVMValueRef final_sample =
2084 LLVMBuildAnd(ctx->builder, shifted_fmask, F, "");
2085
2086 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
2087 * resource descriptor is 0 (invalid),
2088 */
2089 LLVMValueRef fmask_desc =
2090 LLVMBuildBitCast(ctx->builder, fmask_desc_ptr,
2091 ctx->v8i32, "");
2092
2093 LLVMValueRef fmask_word1 =
2094 LLVMBuildExtractElement(ctx->builder, fmask_desc,
2095 ctx->i32_1, "");
2096
2097 LLVMValueRef word1_is_nonzero =
2098 LLVMBuildICmp(ctx->builder, LLVMIntNE,
2099 fmask_word1, ctx->i32_0, "");
2100
2101 /* Replace the MSAA sample index. */
2102 sample_index =
2103 LLVMBuildSelect(ctx->builder, word1_is_nonzero,
2104 final_sample, sample_index, "");
2105 return sample_index;
2106 }
2107
2108 static void get_image_coords(struct ac_nir_context *ctx,
2109 const nir_intrinsic_instr *instr,
2110 struct ac_image_args *args)
2111 {
2112 const struct glsl_type *type = glsl_without_array(instr->variables[0]->var->type);
2113
2114 LLVMValueRef src0 = get_src(ctx, instr->src[0]);
2115 LLVMValueRef masks[] = {
2116 LLVMConstInt(ctx->ac.i32, 0, false), LLVMConstInt(ctx->ac.i32, 1, false),
2117 LLVMConstInt(ctx->ac.i32, 2, false), LLVMConstInt(ctx->ac.i32, 3, false),
2118 };
2119 LLVMValueRef sample_index = ac_llvm_extract_elem(&ctx->ac, get_src(ctx, instr->src[1]), 0);
2120
2121 int count;
2122 enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
2123 bool is_array = glsl_sampler_type_is_array(type);
2124 bool add_frag_pos = (dim == GLSL_SAMPLER_DIM_SUBPASS ||
2125 dim == GLSL_SAMPLER_DIM_SUBPASS_MS);
2126 bool is_ms = (dim == GLSL_SAMPLER_DIM_MS ||
2127 dim == GLSL_SAMPLER_DIM_SUBPASS_MS);
2128 bool gfx9_1d = ctx->ac.chip_class >= GFX9 && dim == GLSL_SAMPLER_DIM_1D;
2129 count = image_type_to_components_count(dim, is_array);
2130
2131 if (is_ms) {
2132 LLVMValueRef fmask_load_address[3];
2133 int chan;
2134
2135 fmask_load_address[0] = LLVMBuildExtractElement(ctx->ac.builder, src0, masks[0], "");
2136 fmask_load_address[1] = LLVMBuildExtractElement(ctx->ac.builder, src0, masks[1], "");
2137 if (is_array)
2138 fmask_load_address[2] = LLVMBuildExtractElement(ctx->ac.builder, src0, masks[2], "");
2139 else
2140 fmask_load_address[2] = NULL;
2141 if (add_frag_pos) {
2142 for (chan = 0; chan < 2; ++chan)
2143 fmask_load_address[chan] =
2144 LLVMBuildAdd(ctx->ac.builder, fmask_load_address[chan],
2145 LLVMBuildFPToUI(ctx->ac.builder, ctx->abi->frag_pos[chan],
2146 ctx->ac.i32, ""), "");
2147 fmask_load_address[2] = ac_to_integer(&ctx->ac, ctx->abi->inputs[ac_llvm_reg_index_soa(VARYING_SLOT_LAYER, 0)]);
2148 }
2149 sample_index = adjust_sample_index_using_fmask(&ctx->ac,
2150 fmask_load_address[0],
2151 fmask_load_address[1],
2152 fmask_load_address[2],
2153 sample_index,
2154 get_sampler_desc(ctx, instr->variables[0], AC_DESC_FMASK, NULL, true, false));
2155 }
2156 if (count == 1 && !gfx9_1d) {
2157 if (instr->src[0].ssa->num_components)
2158 args->coords[0] = LLVMBuildExtractElement(ctx->ac.builder, src0, masks[0], "");
2159 else
2160 args->coords[0] = src0;
2161 } else {
2162 int chan;
2163 if (is_ms)
2164 count--;
2165 for (chan = 0; chan < count; ++chan) {
2166 args->coords[chan] = ac_llvm_extract_elem(&ctx->ac, src0, chan);
2167 }
2168 if (add_frag_pos) {
2169 for (chan = 0; chan < 2; ++chan) {
2170 args->coords[chan] = LLVMBuildAdd(
2171 ctx->ac.builder, args->coords[chan],
2172 LLVMBuildFPToUI(
2173 ctx->ac.builder, ctx->abi->frag_pos[chan],
2174 ctx->ac.i32, ""), "");
2175 }
2176 args->coords[2] = ac_to_integer(&ctx->ac,
2177 ctx->abi->inputs[ac_llvm_reg_index_soa(VARYING_SLOT_LAYER, 0)]);
2178 count++;
2179 }
2180
2181 if (gfx9_1d) {
2182 if (is_array) {
2183 args->coords[2] = args->coords[1];
2184 args->coords[1] = ctx->ac.i32_0;
2185 } else
2186 args->coords[1] = ctx->ac.i32_0;
2187 count++;
2188 }
2189
2190 if (is_ms) {
2191 args->coords[count] = sample_index;
2192 count++;
2193 }
2194 }
2195 }
2196
2197 static LLVMValueRef get_image_buffer_descriptor(struct ac_nir_context *ctx,
2198 const nir_intrinsic_instr *instr, bool write)
2199 {
2200 LLVMValueRef rsrc = get_sampler_desc(ctx, instr->variables[0], AC_DESC_BUFFER, NULL, true, write);
2201 if (ctx->abi->gfx9_stride_size_workaround) {
2202 LLVMValueRef elem_count = LLVMBuildExtractElement(ctx->ac.builder, rsrc, LLVMConstInt(ctx->ac.i32, 2, 0), "");
2203 LLVMValueRef stride = LLVMBuildExtractElement(ctx->ac.builder, rsrc, LLVMConstInt(ctx->ac.i32, 1, 0), "");
2204 stride = LLVMBuildLShr(ctx->ac.builder, stride, LLVMConstInt(ctx->ac.i32, 16, 0), "");
2205
2206 LLVMValueRef new_elem_count = LLVMBuildSelect(ctx->ac.builder,
2207 LLVMBuildICmp(ctx->ac.builder, LLVMIntUGT, elem_count, stride, ""),
2208 elem_count, stride, "");
2209
2210 rsrc = LLVMBuildInsertElement(ctx->ac.builder, rsrc, new_elem_count,
2211 LLVMConstInt(ctx->ac.i32, 2, 0), "");
2212 }
2213 return rsrc;
2214 }
2215
2216 static LLVMValueRef visit_image_load(struct ac_nir_context *ctx,
2217 const nir_intrinsic_instr *instr)
2218 {
2219 LLVMValueRef res;
2220 const nir_variable *var = instr->variables[0]->var;
2221 const struct glsl_type *type = var->type;
2222
2223 if(instr->variables[0]->deref.child)
2224 type = instr->variables[0]->deref.child->type;
2225
2226 type = glsl_without_array(type);
2227
2228 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
2229 if (dim == GLSL_SAMPLER_DIM_BUF) {
2230 unsigned mask = nir_ssa_def_components_read(&instr->dest.ssa);
2231 unsigned num_channels = util_last_bit(mask);
2232 LLVMValueRef rsrc, vindex;
2233
2234 rsrc = get_image_buffer_descriptor(ctx, instr, false);
2235 vindex = LLVMBuildExtractElement(ctx->ac.builder, get_src(ctx, instr->src[0]),
2236 ctx->ac.i32_0, "");
2237
2238 /* TODO: set "glc" and "can_speculate" when OpenGL needs it. */
2239 res = ac_build_buffer_load_format(&ctx->ac, rsrc, vindex,
2240 ctx->ac.i32_0, num_channels,
2241 false, false);
2242 res = ac_build_expand_to_vec4(&ctx->ac, res, num_channels);
2243
2244 res = ac_trim_vector(&ctx->ac, res, instr->dest.ssa.num_components);
2245 res = ac_to_integer(&ctx->ac, res);
2246 } else {
2247 struct ac_image_args args = {};
2248 args.opcode = ac_image_load;
2249 get_image_coords(ctx, instr, &args);
2250 args.resource = get_sampler_desc(ctx, instr->variables[0],
2251 AC_DESC_IMAGE, NULL, true, false);
2252 args.dim = get_ac_image_dim(&ctx->ac, glsl_get_sampler_dim(type),
2253 glsl_sampler_type_is_array(type));
2254 args.dmask = 15;
2255 args.attributes = AC_FUNC_ATTR_READONLY;
2256 if (var->data.image._volatile || var->data.image.coherent)
2257 args.cache_policy |= ac_glc;
2258
2259 res = ac_build_image_opcode(&ctx->ac, &args);
2260 }
2261 return ac_to_integer(&ctx->ac, res);
2262 }
2263
2264 static void visit_image_store(struct ac_nir_context *ctx,
2265 nir_intrinsic_instr *instr)
2266 {
2267 LLVMValueRef params[8];
2268 const nir_variable *var = instr->variables[0]->var;
2269 const struct glsl_type *type = glsl_without_array(var->type);
2270 const enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
2271 LLVMValueRef glc = ctx->ac.i1false;
2272 bool force_glc = ctx->ac.chip_class == SI;
2273 if (force_glc)
2274 glc = ctx->ac.i1true;
2275
2276 if (dim == GLSL_SAMPLER_DIM_BUF) {
2277 LLVMValueRef rsrc = get_image_buffer_descriptor(ctx, instr, true);
2278
2279 params[0] = ac_to_float(&ctx->ac, get_src(ctx, instr->src[2])); /* data */
2280 params[1] = rsrc;
2281 params[2] = LLVMBuildExtractElement(ctx->ac.builder, get_src(ctx, instr->src[0]),
2282 ctx->ac.i32_0, ""); /* vindex */
2283 params[3] = ctx->ac.i32_0; /* voffset */
2284 params[4] = glc; /* glc */
2285 params[5] = ctx->ac.i1false; /* slc */
2286 ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.buffer.store.format.v4f32", ctx->ac.voidt,
2287 params, 6, 0);
2288 } else {
2289 struct ac_image_args args = {};
2290 args.opcode = ac_image_store;
2291 args.data[0] = ac_to_float(&ctx->ac, get_src(ctx, instr->src[2]));
2292 get_image_coords(ctx, instr, &args);
2293 args.resource = get_sampler_desc(ctx, instr->variables[0],
2294 AC_DESC_IMAGE, NULL, true, false);
2295 args.dim = get_ac_image_dim(&ctx->ac, glsl_get_sampler_dim(type),
2296 glsl_sampler_type_is_array(type));
2297 args.dmask = 15;
2298 if (force_glc || var->data.image._volatile || var->data.image.coherent)
2299 args.cache_policy |= ac_glc;
2300
2301 ac_build_image_opcode(&ctx->ac, &args);
2302 }
2303
2304 }
2305
2306 static LLVMValueRef visit_image_atomic(struct ac_nir_context *ctx,
2307 const nir_intrinsic_instr *instr)
2308 {
2309 LLVMValueRef params[7];
2310 int param_count = 0;
2311 const nir_variable *var = instr->variables[0]->var;
2312
2313 bool cmpswap = instr->intrinsic == nir_intrinsic_image_var_atomic_comp_swap;
2314 const char *atomic_name;
2315 char intrinsic_name[41];
2316 enum ac_atomic_op atomic_subop;
2317 const struct glsl_type *type = glsl_without_array(var->type);
2318 MAYBE_UNUSED int length;
2319
2320 bool is_unsigned = glsl_get_sampler_result_type(type) == GLSL_TYPE_UINT;
2321
2322 switch (instr->intrinsic) {
2323 case nir_intrinsic_image_var_atomic_add:
2324 atomic_name = "add";
2325 atomic_subop = ac_atomic_add;
2326 break;
2327 case nir_intrinsic_image_var_atomic_min:
2328 atomic_name = is_unsigned ? "umin" : "smin";
2329 atomic_subop = is_unsigned ? ac_atomic_umin : ac_atomic_smin;
2330 break;
2331 case nir_intrinsic_image_var_atomic_max:
2332 atomic_name = is_unsigned ? "umax" : "smax";
2333 atomic_subop = is_unsigned ? ac_atomic_umax : ac_atomic_smax;
2334 break;
2335 case nir_intrinsic_image_var_atomic_and:
2336 atomic_name = "and";
2337 atomic_subop = ac_atomic_and;
2338 break;
2339 case nir_intrinsic_image_var_atomic_or:
2340 atomic_name = "or";
2341 atomic_subop = ac_atomic_or;
2342 break;
2343 case nir_intrinsic_image_var_atomic_xor:
2344 atomic_name = "xor";
2345 atomic_subop = ac_atomic_xor;
2346 break;
2347 case nir_intrinsic_image_var_atomic_exchange:
2348 atomic_name = "swap";
2349 atomic_subop = ac_atomic_swap;
2350 break;
2351 case nir_intrinsic_image_var_atomic_comp_swap:
2352 atomic_name = "cmpswap";
2353 atomic_subop = 0; /* not used */
2354 break;
2355 default:
2356 abort();
2357 }
2358
2359 if (cmpswap)
2360 params[param_count++] = get_src(ctx, instr->src[3]);
2361 params[param_count++] = get_src(ctx, instr->src[2]);
2362
2363 if (glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_BUF) {
2364 params[param_count++] = get_image_buffer_descriptor(ctx, instr, true);
2365 params[param_count++] = LLVMBuildExtractElement(ctx->ac.builder, get_src(ctx, instr->src[0]),
2366 ctx->ac.i32_0, ""); /* vindex */
2367 params[param_count++] = ctx->ac.i32_0; /* voffset */
2368 params[param_count++] = ctx->ac.i1false; /* slc */
2369
2370 length = snprintf(intrinsic_name, sizeof(intrinsic_name),
2371 "llvm.amdgcn.buffer.atomic.%s", atomic_name);
2372
2373 assert(length < sizeof(intrinsic_name));
2374 return ac_build_intrinsic(&ctx->ac, intrinsic_name, ctx->ac.i32,
2375 params, param_count, 0);
2376 } else {
2377 struct ac_image_args args = {};
2378 args.opcode = cmpswap ? ac_image_atomic_cmpswap : ac_image_atomic;
2379 args.atomic = atomic_subop;
2380 args.data[0] = params[0];
2381 if (cmpswap)
2382 args.data[1] = params[1];
2383 get_image_coords(ctx, instr, &args);
2384 args.resource = get_sampler_desc(ctx, instr->variables[0],
2385 AC_DESC_IMAGE, NULL, true, false);
2386 args.dim = get_ac_image_dim(&ctx->ac, glsl_get_sampler_dim(type),
2387 glsl_sampler_type_is_array(type));
2388
2389 return ac_build_image_opcode(&ctx->ac, &args);
2390 }
2391 }
2392
2393 static LLVMValueRef visit_image_samples(struct ac_nir_context *ctx,
2394 const nir_intrinsic_instr *instr)
2395 {
2396 const nir_variable *var = instr->variables[0]->var;
2397 const struct glsl_type *type = glsl_without_array(var->type);
2398
2399 struct ac_image_args args = { 0 };
2400 args.dim = get_ac_sampler_dim(&ctx->ac, glsl_get_sampler_dim(type),
2401 glsl_sampler_type_is_array(type));
2402 args.dmask = 0xf;
2403 args.resource = get_sampler_desc(ctx, instr->variables[0],
2404 AC_DESC_IMAGE, NULL, true, false);
2405 args.opcode = ac_image_get_resinfo;
2406 args.lod = ctx->ac.i32_0;
2407 args.attributes = AC_FUNC_ATTR_READNONE;
2408
2409 return ac_build_image_opcode(&ctx->ac, &args);
2410 }
2411
2412 static LLVMValueRef visit_image_size(struct ac_nir_context *ctx,
2413 const nir_intrinsic_instr *instr)
2414 {
2415 LLVMValueRef res;
2416 const nir_variable *var = instr->variables[0]->var;
2417 const struct glsl_type *type = glsl_without_array(var->type);
2418
2419 if (glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_BUF)
2420 return get_buffer_size(ctx,
2421 get_sampler_desc(ctx, instr->variables[0],
2422 AC_DESC_BUFFER, NULL, true, false), true);
2423
2424 struct ac_image_args args = { 0 };
2425
2426 args.dim = get_ac_image_dim(&ctx->ac, glsl_get_sampler_dim(type),
2427 glsl_sampler_type_is_array(type));
2428 args.dmask = 0xf;
2429 args.resource = get_sampler_desc(ctx, instr->variables[0], AC_DESC_IMAGE, NULL, true, false);
2430 args.opcode = ac_image_get_resinfo;
2431 args.lod = ctx->ac.i32_0;
2432 args.attributes = AC_FUNC_ATTR_READNONE;
2433
2434 res = ac_build_image_opcode(&ctx->ac, &args);
2435
2436 LLVMValueRef two = LLVMConstInt(ctx->ac.i32, 2, false);
2437
2438 if (glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_CUBE &&
2439 glsl_sampler_type_is_array(type)) {
2440 LLVMValueRef six = LLVMConstInt(ctx->ac.i32, 6, false);
2441 LLVMValueRef z = LLVMBuildExtractElement(ctx->ac.builder, res, two, "");
2442 z = LLVMBuildSDiv(ctx->ac.builder, z, six, "");
2443 res = LLVMBuildInsertElement(ctx->ac.builder, res, z, two, "");
2444 }
2445 if (ctx->ac.chip_class >= GFX9 &&
2446 glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_1D &&
2447 glsl_sampler_type_is_array(type)) {
2448 LLVMValueRef layers = LLVMBuildExtractElement(ctx->ac.builder, res, two, "");
2449 res = LLVMBuildInsertElement(ctx->ac.builder, res, layers,
2450 ctx->ac.i32_1, "");
2451
2452 }
2453 return res;
2454 }
2455
2456 #define NOOP_WAITCNT 0xf7f
2457 #define LGKM_CNT 0x07f
2458 #define VM_CNT 0xf70
2459
2460 static void emit_membar(struct ac_llvm_context *ac,
2461 const nir_intrinsic_instr *instr)
2462 {
2463 unsigned waitcnt = NOOP_WAITCNT;
2464
2465 switch (instr->intrinsic) {
2466 case nir_intrinsic_memory_barrier:
2467 case nir_intrinsic_group_memory_barrier:
2468 waitcnt &= VM_CNT & LGKM_CNT;
2469 break;
2470 case nir_intrinsic_memory_barrier_atomic_counter:
2471 case nir_intrinsic_memory_barrier_buffer:
2472 case nir_intrinsic_memory_barrier_image:
2473 waitcnt &= VM_CNT;
2474 break;
2475 case nir_intrinsic_memory_barrier_shared:
2476 waitcnt &= LGKM_CNT;
2477 break;
2478 default:
2479 break;
2480 }
2481 if (waitcnt != NOOP_WAITCNT)
2482 ac_build_waitcnt(ac, waitcnt);
2483 }
2484
2485 void ac_emit_barrier(struct ac_llvm_context *ac, gl_shader_stage stage)
2486 {
2487 /* SI only (thanks to a hw bug workaround):
2488 * The real barrier instruction isn’t needed, because an entire patch
2489 * always fits into a single wave.
2490 */
2491 if (ac->chip_class == SI && stage == MESA_SHADER_TESS_CTRL) {
2492 ac_build_waitcnt(ac, LGKM_CNT & VM_CNT);
2493 return;
2494 }
2495 ac_build_intrinsic(ac, "llvm.amdgcn.s.barrier",
2496 ac->voidt, NULL, 0, AC_FUNC_ATTR_CONVERGENT);
2497 }
2498
2499 static void emit_discard(struct ac_nir_context *ctx,
2500 const nir_intrinsic_instr *instr)
2501 {
2502 LLVMValueRef cond;
2503
2504 if (instr->intrinsic == nir_intrinsic_discard_if) {
2505 cond = LLVMBuildICmp(ctx->ac.builder, LLVMIntEQ,
2506 get_src(ctx, instr->src[0]),
2507 ctx->ac.i32_0, "");
2508 } else {
2509 assert(instr->intrinsic == nir_intrinsic_discard);
2510 cond = LLVMConstInt(ctx->ac.i1, false, 0);
2511 }
2512
2513 ctx->abi->emit_kill(ctx->abi, cond);
2514 }
2515
2516 static LLVMValueRef
2517 visit_load_helper_invocation(struct ac_nir_context *ctx)
2518 {
2519 LLVMValueRef result = ac_build_intrinsic(&ctx->ac,
2520 "llvm.amdgcn.ps.live",
2521 ctx->ac.i1, NULL, 0,
2522 AC_FUNC_ATTR_READNONE);
2523 result = LLVMBuildNot(ctx->ac.builder, result, "");
2524 return LLVMBuildSExt(ctx->ac.builder, result, ctx->ac.i32, "");
2525 }
2526
2527 static LLVMValueRef
2528 visit_load_local_invocation_index(struct ac_nir_context *ctx)
2529 {
2530 LLVMValueRef result;
2531 LLVMValueRef thread_id = ac_get_thread_id(&ctx->ac);
2532 result = LLVMBuildAnd(ctx->ac.builder, ctx->abi->tg_size,
2533 LLVMConstInt(ctx->ac.i32, 0xfc0, false), "");
2534
2535 return LLVMBuildAdd(ctx->ac.builder, result, thread_id, "");
2536 }
2537
2538 static LLVMValueRef
2539 visit_load_subgroup_id(struct ac_nir_context *ctx)
2540 {
2541 if (ctx->stage == MESA_SHADER_COMPUTE) {
2542 LLVMValueRef result;
2543 result = LLVMBuildAnd(ctx->ac.builder, ctx->abi->tg_size,
2544 LLVMConstInt(ctx->ac.i32, 0xfc0, false), "");
2545 return LLVMBuildLShr(ctx->ac.builder, result, LLVMConstInt(ctx->ac.i32, 6, false), "");
2546 } else {
2547 return LLVMConstInt(ctx->ac.i32, 0, false);
2548 }
2549 }
2550
2551 static LLVMValueRef
2552 visit_load_num_subgroups(struct ac_nir_context *ctx)
2553 {
2554 if (ctx->stage == MESA_SHADER_COMPUTE) {
2555 return LLVMBuildAnd(ctx->ac.builder, ctx->abi->tg_size,
2556 LLVMConstInt(ctx->ac.i32, 0x3f, false), "");
2557 } else {
2558 return LLVMConstInt(ctx->ac.i32, 1, false);
2559 }
2560 }
2561
2562 static LLVMValueRef
2563 visit_first_invocation(struct ac_nir_context *ctx)
2564 {
2565 LLVMValueRef active_set = ac_build_ballot(&ctx->ac, ctx->ac.i32_1);
2566
2567 /* The second argument is whether cttz(0) should be defined, but we do not care. */
2568 LLVMValueRef args[] = {active_set, LLVMConstInt(ctx->ac.i1, 0, false)};
2569 LLVMValueRef result = ac_build_intrinsic(&ctx->ac,
2570 "llvm.cttz.i64",
2571 ctx->ac.i64, args, 2,
2572 AC_FUNC_ATTR_NOUNWIND |
2573 AC_FUNC_ATTR_READNONE);
2574
2575 return LLVMBuildTrunc(ctx->ac.builder, result, ctx->ac.i32, "");
2576 }
2577
2578 static LLVMValueRef
2579 visit_load_shared(struct ac_nir_context *ctx,
2580 const nir_intrinsic_instr *instr)
2581 {
2582 LLVMValueRef values[4], derived_ptr, index, ret;
2583
2584 LLVMValueRef ptr = get_memory_ptr(ctx, instr->src[0]);
2585
2586 for (int chan = 0; chan < instr->num_components; chan++) {
2587 index = LLVMConstInt(ctx->ac.i32, chan, 0);
2588 derived_ptr = LLVMBuildGEP(ctx->ac.builder, ptr, &index, 1, "");
2589 values[chan] = LLVMBuildLoad(ctx->ac.builder, derived_ptr, "");
2590 }
2591
2592 ret = ac_build_gather_values(&ctx->ac, values, instr->num_components);
2593 return LLVMBuildBitCast(ctx->ac.builder, ret, get_def_type(ctx, &instr->dest.ssa), "");
2594 }
2595
2596 static void
2597 visit_store_shared(struct ac_nir_context *ctx,
2598 const nir_intrinsic_instr *instr)
2599 {
2600 LLVMValueRef derived_ptr, data,index;
2601 LLVMBuilderRef builder = ctx->ac.builder;
2602
2603 LLVMValueRef ptr = get_memory_ptr(ctx, instr->src[1]);
2604 LLVMValueRef src = get_src(ctx, instr->src[0]);
2605
2606 int writemask = nir_intrinsic_write_mask(instr);
2607 for (int chan = 0; chan < 4; chan++) {
2608 if (!(writemask & (1 << chan))) {
2609 continue;
2610 }
2611 data = ac_llvm_extract_elem(&ctx->ac, src, chan);
2612 index = LLVMConstInt(ctx->ac.i32, chan, 0);
2613 derived_ptr = LLVMBuildGEP(builder, ptr, &index, 1, "");
2614 LLVMBuildStore(builder, data, derived_ptr);
2615 }
2616 }
2617
2618 static LLVMValueRef visit_var_atomic(struct ac_nir_context *ctx,
2619 const nir_intrinsic_instr *instr,
2620 LLVMValueRef ptr, int src_idx)
2621 {
2622 LLVMValueRef result;
2623 LLVMValueRef src = get_src(ctx, instr->src[src_idx]);
2624
2625 if (instr->intrinsic == nir_intrinsic_var_atomic_comp_swap ||
2626 instr->intrinsic == nir_intrinsic_shared_atomic_comp_swap) {
2627 LLVMValueRef src1 = get_src(ctx, instr->src[src_idx + 1]);
2628 result = LLVMBuildAtomicCmpXchg(ctx->ac.builder,
2629 ptr, src, src1,
2630 LLVMAtomicOrderingSequentiallyConsistent,
2631 LLVMAtomicOrderingSequentiallyConsistent,
2632 false);
2633 result = LLVMBuildExtractValue(ctx->ac.builder, result, 0, "");
2634 } else {
2635 LLVMAtomicRMWBinOp op;
2636 switch (instr->intrinsic) {
2637 case nir_intrinsic_var_atomic_add:
2638 case nir_intrinsic_shared_atomic_add:
2639 op = LLVMAtomicRMWBinOpAdd;
2640 break;
2641 case nir_intrinsic_var_atomic_umin:
2642 case nir_intrinsic_shared_atomic_umin:
2643 op = LLVMAtomicRMWBinOpUMin;
2644 break;
2645 case nir_intrinsic_var_atomic_umax:
2646 case nir_intrinsic_shared_atomic_umax:
2647 op = LLVMAtomicRMWBinOpUMax;
2648 break;
2649 case nir_intrinsic_var_atomic_imin:
2650 case nir_intrinsic_shared_atomic_imin:
2651 op = LLVMAtomicRMWBinOpMin;
2652 break;
2653 case nir_intrinsic_var_atomic_imax:
2654 case nir_intrinsic_shared_atomic_imax:
2655 op = LLVMAtomicRMWBinOpMax;
2656 break;
2657 case nir_intrinsic_var_atomic_and:
2658 case nir_intrinsic_shared_atomic_and:
2659 op = LLVMAtomicRMWBinOpAnd;
2660 break;
2661 case nir_intrinsic_var_atomic_or:
2662 case nir_intrinsic_shared_atomic_or:
2663 op = LLVMAtomicRMWBinOpOr;
2664 break;
2665 case nir_intrinsic_var_atomic_xor:
2666 case nir_intrinsic_shared_atomic_xor:
2667 op = LLVMAtomicRMWBinOpXor;
2668 break;
2669 case nir_intrinsic_var_atomic_exchange:
2670 case nir_intrinsic_shared_atomic_exchange:
2671 op = LLVMAtomicRMWBinOpXchg;
2672 break;
2673 default:
2674 return NULL;
2675 }
2676
2677 result = LLVMBuildAtomicRMW(ctx->ac.builder, op, ptr, ac_to_integer(&ctx->ac, src),
2678 LLVMAtomicOrderingSequentiallyConsistent,
2679 false);
2680 }
2681 return result;
2682 }
2683
2684 static LLVMValueRef load_sample_pos(struct ac_nir_context *ctx)
2685 {
2686 LLVMValueRef values[2];
2687 LLVMValueRef pos[2];
2688
2689 pos[0] = ac_to_float(&ctx->ac, ctx->abi->frag_pos[0]);
2690 pos[1] = ac_to_float(&ctx->ac, ctx->abi->frag_pos[1]);
2691
2692 values[0] = ac_build_fract(&ctx->ac, pos[0], 32);
2693 values[1] = ac_build_fract(&ctx->ac, pos[1], 32);
2694 return ac_build_gather_values(&ctx->ac, values, 2);
2695 }
2696
2697 static LLVMValueRef visit_interp(struct ac_nir_context *ctx,
2698 const nir_intrinsic_instr *instr)
2699 {
2700 LLVMValueRef result[4];
2701 LLVMValueRef interp_param, attr_number;
2702 unsigned location;
2703 unsigned chan;
2704 LLVMValueRef src_c0 = NULL;
2705 LLVMValueRef src_c1 = NULL;
2706 LLVMValueRef src0 = NULL;
2707 int input_index = instr->variables[0]->var->data.location - VARYING_SLOT_VAR0;
2708 switch (instr->intrinsic) {
2709 case nir_intrinsic_interp_var_at_centroid:
2710 location = INTERP_CENTROID;
2711 break;
2712 case nir_intrinsic_interp_var_at_sample:
2713 case nir_intrinsic_interp_var_at_offset:
2714 location = INTERP_CENTER;
2715 src0 = get_src(ctx, instr->src[0]);
2716 break;
2717 default:
2718 break;
2719 }
2720
2721 if (instr->intrinsic == nir_intrinsic_interp_var_at_offset) {
2722 src_c0 = ac_to_float(&ctx->ac, LLVMBuildExtractElement(ctx->ac.builder, src0, ctx->ac.i32_0, ""));
2723 src_c1 = ac_to_float(&ctx->ac, LLVMBuildExtractElement(ctx->ac.builder, src0, ctx->ac.i32_1, ""));
2724 } else if (instr->intrinsic == nir_intrinsic_interp_var_at_sample) {
2725 LLVMValueRef sample_position;
2726 LLVMValueRef halfval = LLVMConstReal(ctx->ac.f32, 0.5f);
2727
2728 /* fetch sample ID */
2729 sample_position = ctx->abi->load_sample_position(ctx->abi, src0);
2730
2731 src_c0 = LLVMBuildExtractElement(ctx->ac.builder, sample_position, ctx->ac.i32_0, "");
2732 src_c0 = LLVMBuildFSub(ctx->ac.builder, src_c0, halfval, "");
2733 src_c1 = LLVMBuildExtractElement(ctx->ac.builder, sample_position, ctx->ac.i32_1, "");
2734 src_c1 = LLVMBuildFSub(ctx->ac.builder, src_c1, halfval, "");
2735 }
2736 interp_param = ctx->abi->lookup_interp_param(ctx->abi, instr->variables[0]->var->data.interpolation, location);
2737 attr_number = LLVMConstInt(ctx->ac.i32, input_index, false);
2738
2739 if (location == INTERP_CENTER) {
2740 LLVMValueRef ij_out[2];
2741 LLVMValueRef ddxy_out = emit_ddxy_interp(ctx, interp_param);
2742
2743 /*
2744 * take the I then J parameters, and the DDX/Y for it, and
2745 * calculate the IJ inputs for the interpolator.
2746 * temp1 = ddx * offset/sample.x + I;
2747 * interp_param.I = ddy * offset/sample.y + temp1;
2748 * temp1 = ddx * offset/sample.x + J;
2749 * interp_param.J = ddy * offset/sample.y + temp1;
2750 */
2751 for (unsigned i = 0; i < 2; i++) {
2752 LLVMValueRef ix_ll = LLVMConstInt(ctx->ac.i32, i, false);
2753 LLVMValueRef iy_ll = LLVMConstInt(ctx->ac.i32, i + 2, false);
2754 LLVMValueRef ddx_el = LLVMBuildExtractElement(ctx->ac.builder,
2755 ddxy_out, ix_ll, "");
2756 LLVMValueRef ddy_el = LLVMBuildExtractElement(ctx->ac.builder,
2757 ddxy_out, iy_ll, "");
2758 LLVMValueRef interp_el = LLVMBuildExtractElement(ctx->ac.builder,
2759 interp_param, ix_ll, "");
2760 LLVMValueRef temp1, temp2;
2761
2762 interp_el = LLVMBuildBitCast(ctx->ac.builder, interp_el,
2763 ctx->ac.f32, "");
2764
2765 temp1 = LLVMBuildFMul(ctx->ac.builder, ddx_el, src_c0, "");
2766 temp1 = LLVMBuildFAdd(ctx->ac.builder, temp1, interp_el, "");
2767
2768 temp2 = LLVMBuildFMul(ctx->ac.builder, ddy_el, src_c1, "");
2769 temp2 = LLVMBuildFAdd(ctx->ac.builder, temp2, temp1, "");
2770
2771 ij_out[i] = LLVMBuildBitCast(ctx->ac.builder,
2772 temp2, ctx->ac.i32, "");
2773 }
2774 interp_param = ac_build_gather_values(&ctx->ac, ij_out, 2);
2775
2776 }
2777
2778 for (chan = 0; chan < 4; chan++) {
2779 LLVMValueRef llvm_chan = LLVMConstInt(ctx->ac.i32, chan, false);
2780
2781 if (interp_param) {
2782 interp_param = LLVMBuildBitCast(ctx->ac.builder,
2783 interp_param, ctx->ac.v2f32, "");
2784 LLVMValueRef i = LLVMBuildExtractElement(
2785 ctx->ac.builder, interp_param, ctx->ac.i32_0, "");
2786 LLVMValueRef j = LLVMBuildExtractElement(
2787 ctx->ac.builder, interp_param, ctx->ac.i32_1, "");
2788
2789 result[chan] = ac_build_fs_interp(&ctx->ac,
2790 llvm_chan, attr_number,
2791 ctx->abi->prim_mask, i, j);
2792 } else {
2793 result[chan] = ac_build_fs_interp_mov(&ctx->ac,
2794 LLVMConstInt(ctx->ac.i32, 2, false),
2795 llvm_chan, attr_number,
2796 ctx->abi->prim_mask);
2797 }
2798 }
2799 return ac_build_varying_gather_values(&ctx->ac, result, instr->num_components,
2800 instr->variables[0]->var->data.location_frac);
2801 }
2802
2803 static void visit_intrinsic(struct ac_nir_context *ctx,
2804 nir_intrinsic_instr *instr)
2805 {
2806 LLVMValueRef result = NULL;
2807
2808 switch (instr->intrinsic) {
2809 case nir_intrinsic_ballot:
2810 result = ac_build_ballot(&ctx->ac, get_src(ctx, instr->src[0]));
2811 break;
2812 case nir_intrinsic_read_invocation:
2813 result = ac_build_readlane(&ctx->ac, get_src(ctx, instr->src[0]),
2814 get_src(ctx, instr->src[1]));
2815 break;
2816 case nir_intrinsic_read_first_invocation:
2817 result = ac_build_readlane(&ctx->ac, get_src(ctx, instr->src[0]), NULL);
2818 break;
2819 case nir_intrinsic_load_subgroup_invocation:
2820 result = ac_get_thread_id(&ctx->ac);
2821 break;
2822 case nir_intrinsic_load_work_group_id: {
2823 LLVMValueRef values[3];
2824
2825 for (int i = 0; i < 3; i++) {
2826 values[i] = ctx->abi->workgroup_ids[i] ?
2827 ctx->abi->workgroup_ids[i] : ctx->ac.i32_0;
2828 }
2829
2830 result = ac_build_gather_values(&ctx->ac, values, 3);
2831 break;
2832 }
2833 case nir_intrinsic_load_base_vertex:
2834 case nir_intrinsic_load_first_vertex:
2835 result = ctx->abi->load_base_vertex(ctx->abi);
2836 break;
2837 case nir_intrinsic_load_local_group_size:
2838 result = ctx->abi->load_local_group_size(ctx->abi);
2839 break;
2840 case nir_intrinsic_load_vertex_id:
2841 result = LLVMBuildAdd(ctx->ac.builder, ctx->abi->vertex_id,
2842 ctx->abi->base_vertex, "");
2843 break;
2844 case nir_intrinsic_load_vertex_id_zero_base: {
2845 result = ctx->abi->vertex_id;
2846 break;
2847 }
2848 case nir_intrinsic_load_local_invocation_id: {
2849 result = ctx->abi->local_invocation_ids;
2850 break;
2851 }
2852 case nir_intrinsic_load_base_instance:
2853 result = ctx->abi->start_instance;
2854 break;
2855 case nir_intrinsic_load_draw_id:
2856 result = ctx->abi->draw_id;
2857 break;
2858 case nir_intrinsic_load_view_index:
2859 result = ctx->abi->view_index;
2860 break;
2861 case nir_intrinsic_load_invocation_id:
2862 if (ctx->stage == MESA_SHADER_TESS_CTRL)
2863 result = ac_unpack_param(&ctx->ac, ctx->abi->tcs_rel_ids, 8, 5);
2864 else
2865 result = ctx->abi->gs_invocation_id;
2866 break;
2867 case nir_intrinsic_load_primitive_id:
2868 if (ctx->stage == MESA_SHADER_GEOMETRY) {
2869 result = ctx->abi->gs_prim_id;
2870 } else if (ctx->stage == MESA_SHADER_TESS_CTRL) {
2871 result = ctx->abi->tcs_patch_id;
2872 } else if (ctx->stage == MESA_SHADER_TESS_EVAL) {
2873 result = ctx->abi->tes_patch_id;
2874 } else
2875 fprintf(stderr, "Unknown primitive id intrinsic: %d", ctx->stage);
2876 break;
2877 case nir_intrinsic_load_sample_id:
2878 result = ac_unpack_param(&ctx->ac, ctx->abi->ancillary, 8, 4);
2879 break;
2880 case nir_intrinsic_load_sample_pos:
2881 result = load_sample_pos(ctx);
2882 break;
2883 case nir_intrinsic_load_sample_mask_in:
2884 result = ctx->abi->load_sample_mask_in(ctx->abi);
2885 break;
2886 case nir_intrinsic_load_frag_coord: {
2887 LLVMValueRef values[4] = {
2888 ctx->abi->frag_pos[0],
2889 ctx->abi->frag_pos[1],
2890 ctx->abi->frag_pos[2],
2891 ac_build_fdiv(&ctx->ac, ctx->ac.f32_1, ctx->abi->frag_pos[3])
2892 };
2893 result = ac_build_gather_values(&ctx->ac, values, 4);
2894 break;
2895 }
2896 case nir_intrinsic_load_front_face:
2897 result = ctx->abi->front_face;
2898 break;
2899 case nir_intrinsic_load_helper_invocation:
2900 result = visit_load_helper_invocation(ctx);
2901 break;
2902 case nir_intrinsic_load_instance_id:
2903 result = ctx->abi->instance_id;
2904 break;
2905 case nir_intrinsic_load_num_work_groups:
2906 result = ctx->abi->num_work_groups;
2907 break;
2908 case nir_intrinsic_load_local_invocation_index:
2909 result = visit_load_local_invocation_index(ctx);
2910 break;
2911 case nir_intrinsic_load_subgroup_id:
2912 result = visit_load_subgroup_id(ctx);
2913 break;
2914 case nir_intrinsic_load_num_subgroups:
2915 result = visit_load_num_subgroups(ctx);
2916 break;
2917 case nir_intrinsic_first_invocation:
2918 result = visit_first_invocation(ctx);
2919 break;
2920 case nir_intrinsic_load_push_constant:
2921 result = visit_load_push_constant(ctx, instr);
2922 break;
2923 case nir_intrinsic_vulkan_resource_index: {
2924 LLVMValueRef index = get_src(ctx, instr->src[0]);
2925 unsigned desc_set = nir_intrinsic_desc_set(instr);
2926 unsigned binding = nir_intrinsic_binding(instr);
2927
2928 result = ctx->abi->load_resource(ctx->abi, index, desc_set,
2929 binding);
2930 break;
2931 }
2932 case nir_intrinsic_vulkan_resource_reindex:
2933 result = visit_vulkan_resource_reindex(ctx, instr);
2934 break;
2935 case nir_intrinsic_store_ssbo:
2936 visit_store_ssbo(ctx, instr);
2937 break;
2938 case nir_intrinsic_load_ssbo:
2939 result = visit_load_buffer(ctx, instr);
2940 break;
2941 case nir_intrinsic_ssbo_atomic_add:
2942 case nir_intrinsic_ssbo_atomic_imin:
2943 case nir_intrinsic_ssbo_atomic_umin:
2944 case nir_intrinsic_ssbo_atomic_imax:
2945 case nir_intrinsic_ssbo_atomic_umax:
2946 case nir_intrinsic_ssbo_atomic_and:
2947 case nir_intrinsic_ssbo_atomic_or:
2948 case nir_intrinsic_ssbo_atomic_xor:
2949 case nir_intrinsic_ssbo_atomic_exchange:
2950 case nir_intrinsic_ssbo_atomic_comp_swap:
2951 result = visit_atomic_ssbo(ctx, instr);
2952 break;
2953 case nir_intrinsic_load_ubo:
2954 result = visit_load_ubo_buffer(ctx, instr);
2955 break;
2956 case nir_intrinsic_get_buffer_size:
2957 result = visit_get_buffer_size(ctx, instr);
2958 break;
2959 case nir_intrinsic_load_var:
2960 result = visit_load_var(ctx, instr);
2961 break;
2962 case nir_intrinsic_store_var:
2963 visit_store_var(ctx, instr);
2964 break;
2965 case nir_intrinsic_load_shared:
2966 result = visit_load_shared(ctx, instr);
2967 break;
2968 case nir_intrinsic_store_shared:
2969 visit_store_shared(ctx, instr);
2970 break;
2971 case nir_intrinsic_image_var_samples:
2972 result = visit_image_samples(ctx, instr);
2973 break;
2974 case nir_intrinsic_image_var_load:
2975 result = visit_image_load(ctx, instr);
2976 break;
2977 case nir_intrinsic_image_var_store:
2978 visit_image_store(ctx, instr);
2979 break;
2980 case nir_intrinsic_image_var_atomic_add:
2981 case nir_intrinsic_image_var_atomic_min:
2982 case nir_intrinsic_image_var_atomic_max:
2983 case nir_intrinsic_image_var_atomic_and:
2984 case nir_intrinsic_image_var_atomic_or:
2985 case nir_intrinsic_image_var_atomic_xor:
2986 case nir_intrinsic_image_var_atomic_exchange:
2987 case nir_intrinsic_image_var_atomic_comp_swap:
2988 result = visit_image_atomic(ctx, instr);
2989 break;
2990 case nir_intrinsic_image_var_size:
2991 result = visit_image_size(ctx, instr);
2992 break;
2993 case nir_intrinsic_shader_clock:
2994 result = ac_build_shader_clock(&ctx->ac);
2995 break;
2996 case nir_intrinsic_discard:
2997 case nir_intrinsic_discard_if:
2998 emit_discard(ctx, instr);
2999 break;
3000 case nir_intrinsic_memory_barrier:
3001 case nir_intrinsic_group_memory_barrier:
3002 case nir_intrinsic_memory_barrier_atomic_counter:
3003 case nir_intrinsic_memory_barrier_buffer:
3004 case nir_intrinsic_memory_barrier_image:
3005 case nir_intrinsic_memory_barrier_shared:
3006 emit_membar(&ctx->ac, instr);
3007 break;
3008 case nir_intrinsic_barrier:
3009 ac_emit_barrier(&ctx->ac, ctx->stage);
3010 break;
3011 case nir_intrinsic_shared_atomic_add:
3012 case nir_intrinsic_shared_atomic_imin:
3013 case nir_intrinsic_shared_atomic_umin:
3014 case nir_intrinsic_shared_atomic_imax:
3015 case nir_intrinsic_shared_atomic_umax:
3016 case nir_intrinsic_shared_atomic_and:
3017 case nir_intrinsic_shared_atomic_or:
3018 case nir_intrinsic_shared_atomic_xor:
3019 case nir_intrinsic_shared_atomic_exchange:
3020 case nir_intrinsic_shared_atomic_comp_swap: {
3021 LLVMValueRef ptr = get_memory_ptr(ctx, instr->src[0]);
3022 result = visit_var_atomic(ctx, instr, ptr, 1);
3023 break;
3024 }
3025 case nir_intrinsic_var_atomic_add:
3026 case nir_intrinsic_var_atomic_imin:
3027 case nir_intrinsic_var_atomic_umin:
3028 case nir_intrinsic_var_atomic_imax:
3029 case nir_intrinsic_var_atomic_umax:
3030 case nir_intrinsic_var_atomic_and:
3031 case nir_intrinsic_var_atomic_or:
3032 case nir_intrinsic_var_atomic_xor:
3033 case nir_intrinsic_var_atomic_exchange:
3034 case nir_intrinsic_var_atomic_comp_swap: {
3035 LLVMValueRef ptr = build_gep_for_deref(ctx, instr->variables[0]);
3036 result = visit_var_atomic(ctx, instr, ptr, 0);
3037 break;
3038 }
3039 case nir_intrinsic_interp_var_at_centroid:
3040 case nir_intrinsic_interp_var_at_sample:
3041 case nir_intrinsic_interp_var_at_offset:
3042 result = visit_interp(ctx, instr);
3043 break;
3044 case nir_intrinsic_emit_vertex:
3045 ctx->abi->emit_vertex(ctx->abi, nir_intrinsic_stream_id(instr), ctx->abi->outputs);
3046 break;
3047 case nir_intrinsic_end_primitive:
3048 ctx->abi->emit_primitive(ctx->abi, nir_intrinsic_stream_id(instr));
3049 break;
3050 case nir_intrinsic_load_tess_coord:
3051 result = ctx->abi->load_tess_coord(ctx->abi);
3052 break;
3053 case nir_intrinsic_load_tess_level_outer:
3054 result = ctx->abi->load_tess_level(ctx->abi, VARYING_SLOT_TESS_LEVEL_OUTER);
3055 break;
3056 case nir_intrinsic_load_tess_level_inner:
3057 result = ctx->abi->load_tess_level(ctx->abi, VARYING_SLOT_TESS_LEVEL_INNER);
3058 break;
3059 case nir_intrinsic_load_patch_vertices_in:
3060 result = ctx->abi->load_patch_vertices_in(ctx->abi);
3061 break;
3062 case nir_intrinsic_vote_all: {
3063 LLVMValueRef tmp = ac_build_vote_all(&ctx->ac, get_src(ctx, instr->src[0]));
3064 result = LLVMBuildSExt(ctx->ac.builder, tmp, ctx->ac.i32, "");
3065 break;
3066 }
3067 case nir_intrinsic_vote_any: {
3068 LLVMValueRef tmp = ac_build_vote_any(&ctx->ac, get_src(ctx, instr->src[0]));
3069 result = LLVMBuildSExt(ctx->ac.builder, tmp, ctx->ac.i32, "");
3070 break;
3071 }
3072 case nir_intrinsic_shuffle:
3073 result = ac_build_shuffle(&ctx->ac, get_src(ctx, instr->src[0]),
3074 get_src(ctx, instr->src[1]));
3075 break;
3076 case nir_intrinsic_reduce:
3077 result = ac_build_reduce(&ctx->ac,
3078 get_src(ctx, instr->src[0]),
3079 instr->const_index[0],
3080 instr->const_index[1]);
3081 break;
3082 case nir_intrinsic_inclusive_scan:
3083 result = ac_build_inclusive_scan(&ctx->ac,
3084 get_src(ctx, instr->src[0]),
3085 instr->const_index[0]);
3086 break;
3087 case nir_intrinsic_exclusive_scan:
3088 result = ac_build_exclusive_scan(&ctx->ac,
3089 get_src(ctx, instr->src[0]),
3090 instr->const_index[0]);
3091 break;
3092 case nir_intrinsic_quad_broadcast: {
3093 unsigned lane = nir_src_as_const_value(instr->src[1])->u32[0];
3094 result = ac_build_quad_swizzle(&ctx->ac, get_src(ctx, instr->src[0]),
3095 lane, lane, lane, lane);
3096 break;
3097 }
3098 case nir_intrinsic_quad_swap_horizontal:
3099 result = ac_build_quad_swizzle(&ctx->ac, get_src(ctx, instr->src[0]), 1, 0, 3 ,2);
3100 break;
3101 case nir_intrinsic_quad_swap_vertical:
3102 result = ac_build_quad_swizzle(&ctx->ac, get_src(ctx, instr->src[0]), 2, 3, 0 ,1);
3103 break;
3104 case nir_intrinsic_quad_swap_diagonal:
3105 result = ac_build_quad_swizzle(&ctx->ac, get_src(ctx, instr->src[0]), 3, 2, 1 ,0);
3106 break;
3107 default:
3108 fprintf(stderr, "Unknown intrinsic: ");
3109 nir_print_instr(&instr->instr, stderr);
3110 fprintf(stderr, "\n");
3111 break;
3112 }
3113 if (result) {
3114 ctx->ssa_defs[instr->dest.ssa.index] = result;
3115 }
3116 }
3117
3118 static LLVMValueRef get_sampler_desc(struct ac_nir_context *ctx,
3119 const nir_deref_var *deref,
3120 enum ac_descriptor_type desc_type,
3121 const nir_tex_instr *tex_instr,
3122 bool image, bool write)
3123 {
3124 LLVMValueRef index = NULL;
3125 unsigned constant_index = 0;
3126 unsigned descriptor_set;
3127 unsigned base_index;
3128 bool bindless = false;
3129
3130 if (!deref) {
3131 assert(tex_instr && !image);
3132 descriptor_set = 0;
3133 base_index = tex_instr->sampler_index;
3134 } else {
3135 const nir_deref *tail = &deref->deref;
3136 while (tail->child) {
3137 const nir_deref_array *child = nir_deref_as_array(tail->child);
3138 unsigned array_size = glsl_get_aoa_size(tail->child->type);
3139
3140 if (!array_size)
3141 array_size = 1;
3142
3143 assert(child->deref_array_type != nir_deref_array_type_wildcard);
3144
3145 if (child->deref_array_type == nir_deref_array_type_indirect) {
3146 LLVMValueRef indirect = get_src(ctx, child->indirect);
3147
3148 indirect = LLVMBuildMul(ctx->ac.builder, indirect,
3149 LLVMConstInt(ctx->ac.i32, array_size, false), "");
3150
3151 if (!index)
3152 index = indirect;
3153 else
3154 index = LLVMBuildAdd(ctx->ac.builder, index, indirect, "");
3155 }
3156
3157 constant_index += child->base_offset * array_size;
3158
3159 tail = &child->deref;
3160 }
3161 descriptor_set = deref->var->data.descriptor_set;
3162
3163 if (deref->var->data.bindless) {
3164 bindless = deref->var->data.bindless;
3165 base_index = deref->var->data.driver_location;
3166 } else {
3167 base_index = deref->var->data.binding;
3168 }
3169 }
3170
3171 return ctx->abi->load_sampler_desc(ctx->abi,
3172 descriptor_set,
3173 base_index,
3174 constant_index, index,
3175 desc_type, image, write, bindless);
3176 }
3177
3178 /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
3179 *
3180 * SI-CI:
3181 * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
3182 * filtering manually. The driver sets img7 to a mask clearing
3183 * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
3184 * s_and_b32 samp0, samp0, img7
3185 *
3186 * VI:
3187 * The ANISO_OVERRIDE sampler field enables this fix in TA.
3188 */
3189 static LLVMValueRef sici_fix_sampler_aniso(struct ac_nir_context *ctx,
3190 LLVMValueRef res, LLVMValueRef samp)
3191 {
3192 LLVMBuilderRef builder = ctx->ac.builder;
3193 LLVMValueRef img7, samp0;
3194
3195 if (ctx->ac.chip_class >= VI)
3196 return samp;
3197
3198 img7 = LLVMBuildExtractElement(builder, res,
3199 LLVMConstInt(ctx->ac.i32, 7, 0), "");
3200 samp0 = LLVMBuildExtractElement(builder, samp,
3201 LLVMConstInt(ctx->ac.i32, 0, 0), "");
3202 samp0 = LLVMBuildAnd(builder, samp0, img7, "");
3203 return LLVMBuildInsertElement(builder, samp, samp0,
3204 LLVMConstInt(ctx->ac.i32, 0, 0), "");
3205 }
3206
3207 static void tex_fetch_ptrs(struct ac_nir_context *ctx,
3208 nir_tex_instr *instr,
3209 LLVMValueRef *res_ptr, LLVMValueRef *samp_ptr,
3210 LLVMValueRef *fmask_ptr)
3211 {
3212 if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF)
3213 *res_ptr = get_sampler_desc(ctx, instr->texture, AC_DESC_BUFFER, instr, false, false);
3214 else
3215 *res_ptr = get_sampler_desc(ctx, instr->texture, AC_DESC_IMAGE, instr, false, false);
3216 if (samp_ptr) {
3217 if (instr->sampler)
3218 *samp_ptr = get_sampler_desc(ctx, instr->sampler, AC_DESC_SAMPLER, instr, false, false);
3219 else
3220 *samp_ptr = get_sampler_desc(ctx, instr->texture, AC_DESC_SAMPLER, instr, false, false);
3221 if (instr->sampler_dim < GLSL_SAMPLER_DIM_RECT)
3222 *samp_ptr = sici_fix_sampler_aniso(ctx, *res_ptr, *samp_ptr);
3223 }
3224 if (fmask_ptr && !instr->sampler && (instr->op == nir_texop_txf_ms ||
3225 instr->op == nir_texop_samples_identical))
3226 *fmask_ptr = get_sampler_desc(ctx, instr->texture, AC_DESC_FMASK, instr, false, false);
3227 }
3228
3229 static LLVMValueRef apply_round_slice(struct ac_llvm_context *ctx,
3230 LLVMValueRef coord)
3231 {
3232 coord = ac_to_float(ctx, coord);
3233 coord = ac_build_intrinsic(ctx, "llvm.rint.f32", ctx->f32, &coord, 1, 0);
3234 coord = ac_to_integer(ctx, coord);
3235 return coord;
3236 }
3237
3238 static void visit_tex(struct ac_nir_context *ctx, nir_tex_instr *instr)
3239 {
3240 LLVMValueRef result = NULL;
3241 struct ac_image_args args = { 0 };
3242 LLVMValueRef fmask_ptr = NULL, sample_index = NULL;
3243 LLVMValueRef ddx = NULL, ddy = NULL;
3244 unsigned offset_src = 0;
3245
3246 tex_fetch_ptrs(ctx, instr, &args.resource, &args.sampler, &fmask_ptr);
3247
3248 for (unsigned i = 0; i < instr->num_srcs; i++) {
3249 switch (instr->src[i].src_type) {
3250 case nir_tex_src_coord: {
3251 LLVMValueRef coord = get_src(ctx, instr->src[i].src);
3252 for (unsigned chan = 0; chan < instr->coord_components; ++chan)
3253 args.coords[chan] = ac_llvm_extract_elem(&ctx->ac, coord, chan);
3254 break;
3255 }
3256 case nir_tex_src_projector:
3257 break;
3258 case nir_tex_src_comparator:
3259 if (instr->is_shadow)
3260 args.compare = get_src(ctx, instr->src[i].src);
3261 break;
3262 case nir_tex_src_offset:
3263 args.offset = get_src(ctx, instr->src[i].src);
3264 offset_src = i;
3265 break;
3266 case nir_tex_src_bias:
3267 if (instr->op == nir_texop_txb)
3268 args.bias = get_src(ctx, instr->src[i].src);
3269 break;
3270 case nir_tex_src_lod: {
3271 nir_const_value *val = nir_src_as_const_value(instr->src[i].src);
3272
3273 if (val && val->i32[0] == 0)
3274 args.level_zero = true;
3275 else
3276 args.lod = get_src(ctx, instr->src[i].src);
3277 break;
3278 }
3279 case nir_tex_src_ms_index:
3280 sample_index = get_src(ctx, instr->src[i].src);
3281 break;
3282 case nir_tex_src_ms_mcs:
3283 break;
3284 case nir_tex_src_ddx:
3285 ddx = get_src(ctx, instr->src[i].src);
3286 break;
3287 case nir_tex_src_ddy:
3288 ddy = get_src(ctx, instr->src[i].src);
3289 break;
3290 case nir_tex_src_texture_offset:
3291 case nir_tex_src_sampler_offset:
3292 case nir_tex_src_plane:
3293 default:
3294 break;
3295 }
3296 }
3297
3298 if (instr->op == nir_texop_txs && instr->sampler_dim == GLSL_SAMPLER_DIM_BUF) {
3299 result = get_buffer_size(ctx, args.resource, true);
3300 goto write_result;
3301 }
3302
3303 if (instr->op == nir_texop_texture_samples) {
3304 LLVMValueRef res, samples, is_msaa;
3305 res = LLVMBuildBitCast(ctx->ac.builder, args.resource, ctx->ac.v8i32, "");
3306 samples = LLVMBuildExtractElement(ctx->ac.builder, res,
3307 LLVMConstInt(ctx->ac.i32, 3, false), "");
3308 is_msaa = LLVMBuildLShr(ctx->ac.builder, samples,
3309 LLVMConstInt(ctx->ac.i32, 28, false), "");
3310 is_msaa = LLVMBuildAnd(ctx->ac.builder, is_msaa,
3311 LLVMConstInt(ctx->ac.i32, 0xe, false), "");
3312 is_msaa = LLVMBuildICmp(ctx->ac.builder, LLVMIntEQ, is_msaa,
3313 LLVMConstInt(ctx->ac.i32, 0xe, false), "");
3314
3315 samples = LLVMBuildLShr(ctx->ac.builder, samples,
3316 LLVMConstInt(ctx->ac.i32, 16, false), "");
3317 samples = LLVMBuildAnd(ctx->ac.builder, samples,
3318 LLVMConstInt(ctx->ac.i32, 0xf, false), "");
3319 samples = LLVMBuildShl(ctx->ac.builder, ctx->ac.i32_1,
3320 samples, "");
3321 samples = LLVMBuildSelect(ctx->ac.builder, is_msaa, samples,
3322 ctx->ac.i32_1, "");
3323 result = samples;
3324 goto write_result;
3325 }
3326
3327 if (args.offset && instr->op != nir_texop_txf) {
3328 LLVMValueRef offset[3], pack;
3329 for (unsigned chan = 0; chan < 3; ++chan)
3330 offset[chan] = ctx->ac.i32_0;
3331
3332 unsigned num_components = ac_get_llvm_num_components(args.offset);
3333 for (unsigned chan = 0; chan < num_components; chan++) {
3334 offset[chan] = ac_llvm_extract_elem(&ctx->ac, args.offset, chan);
3335 offset[chan] = LLVMBuildAnd(ctx->ac.builder, offset[chan],
3336 LLVMConstInt(ctx->ac.i32, 0x3f, false), "");
3337 if (chan)
3338 offset[chan] = LLVMBuildShl(ctx->ac.builder, offset[chan],
3339 LLVMConstInt(ctx->ac.i32, chan * 8, false), "");
3340 }
3341 pack = LLVMBuildOr(ctx->ac.builder, offset[0], offset[1], "");
3342 pack = LLVMBuildOr(ctx->ac.builder, pack, offset[2], "");
3343 args.offset = pack;
3344 }
3345
3346 /* TC-compatible HTILE on radeonsi promotes Z16 and Z24 to Z32_FLOAT,
3347 * so the depth comparison value isn't clamped for Z16 and
3348 * Z24 anymore. Do it manually here.
3349 *
3350 * It's unnecessary if the original texture format was
3351 * Z32_FLOAT, but we don't know that here.
3352 */
3353 if (args.compare && ctx->ac.chip_class == VI && ctx->abi->clamp_shadow_reference)
3354 args.compare = ac_build_clamp(&ctx->ac, ac_to_float(&ctx->ac, args.compare));
3355
3356 /* pack derivatives */
3357 if (ddx || ddy) {
3358 int num_src_deriv_channels, num_dest_deriv_channels;
3359 switch (instr->sampler_dim) {
3360 case GLSL_SAMPLER_DIM_3D:
3361 case GLSL_SAMPLER_DIM_CUBE:
3362 num_src_deriv_channels = 3;
3363 num_dest_deriv_channels = 3;
3364 break;
3365 case GLSL_SAMPLER_DIM_2D:
3366 default:
3367 num_src_deriv_channels = 2;
3368 num_dest_deriv_channels = 2;
3369 break;
3370 case GLSL_SAMPLER_DIM_1D:
3371 num_src_deriv_channels = 1;
3372 if (ctx->ac.chip_class >= GFX9) {
3373 num_dest_deriv_channels = 2;
3374 } else {
3375 num_dest_deriv_channels = 1;
3376 }
3377 break;
3378 }
3379
3380 for (unsigned i = 0; i < num_src_deriv_channels; i++) {
3381 args.derivs[i] = ac_to_float(&ctx->ac,
3382 ac_llvm_extract_elem(&ctx->ac, ddx, i));
3383 args.derivs[num_dest_deriv_channels + i] = ac_to_float(&ctx->ac,
3384 ac_llvm_extract_elem(&ctx->ac, ddy, i));
3385 }
3386 for (unsigned i = num_src_deriv_channels; i < num_dest_deriv_channels; i++) {
3387 args.derivs[i] = ctx->ac.f32_0;
3388 args.derivs[num_dest_deriv_channels + i] = ctx->ac.f32_0;
3389 }
3390 }
3391
3392 if (instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE && args.coords[0]) {
3393 for (unsigned chan = 0; chan < instr->coord_components; chan++)
3394 args.coords[chan] = ac_to_float(&ctx->ac, args.coords[chan]);
3395 if (instr->coord_components == 3)
3396 args.coords[3] = LLVMGetUndef(ctx->ac.f32);
3397 ac_prepare_cube_coords(&ctx->ac,
3398 instr->op == nir_texop_txd, instr->is_array,
3399 instr->op == nir_texop_lod, args.coords, args.derivs);
3400 }
3401
3402 /* Texture coordinates fixups */
3403 if (instr->coord_components > 1 &&
3404 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
3405 instr->is_array &&
3406 instr->op != nir_texop_txf) {
3407 args.coords[1] = apply_round_slice(&ctx->ac, args.coords[1]);
3408 }
3409
3410 if (instr->coord_components > 2 &&
3411 (instr->sampler_dim == GLSL_SAMPLER_DIM_2D ||
3412 instr->sampler_dim == GLSL_SAMPLER_DIM_MS ||
3413 instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS ||
3414 instr->sampler_dim == GLSL_SAMPLER_DIM_SUBPASS_MS) &&
3415 instr->is_array &&
3416 instr->op != nir_texop_txf && instr->op != nir_texop_txf_ms) {
3417 args.coords[2] = apply_round_slice(&ctx->ac, args.coords[2]);
3418 }
3419
3420 if (ctx->ac.chip_class >= GFX9 &&
3421 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
3422 instr->op != nir_texop_lod) {
3423 LLVMValueRef filler;
3424 if (instr->op == nir_texop_txf)
3425 filler = ctx->ac.i32_0;
3426 else
3427 filler = LLVMConstReal(ctx->ac.f32, 0.5);
3428
3429 if (instr->is_array)
3430 args.coords[2] = args.coords[1];
3431 args.coords[1] = filler;
3432 }
3433
3434 /* Pack sample index */
3435 if (instr->op == nir_texop_txf_ms && sample_index)
3436 args.coords[instr->coord_components] = sample_index;
3437
3438 if (instr->op == nir_texop_samples_identical) {
3439 struct ac_image_args txf_args = { 0 };
3440 memcpy(txf_args.coords, args.coords, sizeof(txf_args.coords));
3441
3442 txf_args.dmask = 0xf;
3443 txf_args.resource = fmask_ptr;
3444 txf_args.dim = instr->is_array ? ac_image_2darray : ac_image_2d;
3445 result = build_tex_intrinsic(ctx, instr, &txf_args);
3446
3447 result = LLVMBuildExtractElement(ctx->ac.builder, result, ctx->ac.i32_0, "");
3448 result = emit_int_cmp(&ctx->ac, LLVMIntEQ, result, ctx->ac.i32_0);
3449 goto write_result;
3450 }
3451
3452 if (instr->sampler_dim == GLSL_SAMPLER_DIM_MS &&
3453 instr->op != nir_texop_txs) {
3454 unsigned sample_chan = instr->is_array ? 3 : 2;
3455 args.coords[sample_chan] = adjust_sample_index_using_fmask(
3456 &ctx->ac, args.coords[0], args.coords[1],
3457 instr->is_array ? args.coords[2] : NULL,
3458 args.coords[sample_chan], fmask_ptr);
3459 }
3460
3461 if (args.offset && instr->op == nir_texop_txf) {
3462 nir_const_value *const_offset =
3463 nir_src_as_const_value(instr->src[offset_src].src);
3464 int num_offsets = instr->src[offset_src].src.ssa->num_components;
3465 assert(const_offset);
3466 num_offsets = MIN2(num_offsets, instr->coord_components);
3467 for (unsigned i = 0; i < num_offsets; ++i) {
3468 args.coords[i] = LLVMBuildAdd(
3469 ctx->ac.builder, args.coords[i],
3470 LLVMConstInt(ctx->ac.i32, const_offset->i32[i], false), "");
3471 }
3472 args.offset = NULL;
3473 }
3474
3475 /* TODO TG4 support */
3476 args.dmask = 0xf;
3477 if (instr->op == nir_texop_tg4) {
3478 if (instr->is_shadow)
3479 args.dmask = 1;
3480 else
3481 args.dmask = 1 << instr->component;
3482 }
3483
3484 if (instr->sampler_dim != GLSL_SAMPLER_DIM_BUF)
3485 args.dim = get_ac_sampler_dim(&ctx->ac, instr->sampler_dim, instr->is_array);
3486 result = build_tex_intrinsic(ctx, instr, &args);
3487
3488 if (instr->op == nir_texop_query_levels)
3489 result = LLVMBuildExtractElement(ctx->ac.builder, result, LLVMConstInt(ctx->ac.i32, 3, false), "");
3490 else if (instr->is_shadow && instr->is_new_style_shadow &&
3491 instr->op != nir_texop_txs && instr->op != nir_texop_lod &&
3492 instr->op != nir_texop_tg4)
3493 result = LLVMBuildExtractElement(ctx->ac.builder, result, ctx->ac.i32_0, "");
3494 else if (instr->op == nir_texop_txs &&
3495 instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE &&
3496 instr->is_array) {
3497 LLVMValueRef two = LLVMConstInt(ctx->ac.i32, 2, false);
3498 LLVMValueRef six = LLVMConstInt(ctx->ac.i32, 6, false);
3499 LLVMValueRef z = LLVMBuildExtractElement(ctx->ac.builder, result, two, "");
3500 z = LLVMBuildSDiv(ctx->ac.builder, z, six, "");
3501 result = LLVMBuildInsertElement(ctx->ac.builder, result, z, two, "");
3502 } else if (ctx->ac.chip_class >= GFX9 &&
3503 instr->op == nir_texop_txs &&
3504 instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
3505 instr->is_array) {
3506 LLVMValueRef two = LLVMConstInt(ctx->ac.i32, 2, false);
3507 LLVMValueRef layers = LLVMBuildExtractElement(ctx->ac.builder, result, two, "");
3508 result = LLVMBuildInsertElement(ctx->ac.builder, result, layers,
3509 ctx->ac.i32_1, "");
3510 } else if (instr->dest.ssa.num_components != 4)
3511 result = ac_trim_vector(&ctx->ac, result, instr->dest.ssa.num_components);
3512
3513 write_result:
3514 if (result) {
3515 assert(instr->dest.is_ssa);
3516 result = ac_to_integer(&ctx->ac, result);
3517 ctx->ssa_defs[instr->dest.ssa.index] = result;
3518 }
3519 }
3520
3521
3522 static void visit_phi(struct ac_nir_context *ctx, nir_phi_instr *instr)
3523 {
3524 LLVMTypeRef type = get_def_type(ctx, &instr->dest.ssa);
3525 LLVMValueRef result = LLVMBuildPhi(ctx->ac.builder, type, "");
3526
3527 ctx->ssa_defs[instr->dest.ssa.index] = result;
3528 _mesa_hash_table_insert(ctx->phis, instr, result);
3529 }
3530
3531 static void visit_post_phi(struct ac_nir_context *ctx,
3532 nir_phi_instr *instr,
3533 LLVMValueRef llvm_phi)
3534 {
3535 nir_foreach_phi_src(src, instr) {
3536 LLVMBasicBlockRef block = get_block(ctx, src->pred);
3537 LLVMValueRef llvm_src = get_src(ctx, src->src);
3538
3539 LLVMAddIncoming(llvm_phi, &llvm_src, &block, 1);
3540 }
3541 }
3542
3543 static void phi_post_pass(struct ac_nir_context *ctx)
3544 {
3545 struct hash_entry *entry;
3546 hash_table_foreach(ctx->phis, entry) {
3547 visit_post_phi(ctx, (nir_phi_instr*)entry->key,
3548 (LLVMValueRef)entry->data);
3549 }
3550 }
3551
3552
3553 static void visit_ssa_undef(struct ac_nir_context *ctx,
3554 const nir_ssa_undef_instr *instr)
3555 {
3556 unsigned num_components = instr->def.num_components;
3557 LLVMTypeRef type = LLVMIntTypeInContext(ctx->ac.context, instr->def.bit_size);
3558 LLVMValueRef undef;
3559
3560 if (num_components == 1)
3561 undef = LLVMGetUndef(type);
3562 else {
3563 undef = LLVMGetUndef(LLVMVectorType(type, num_components));
3564 }
3565 ctx->ssa_defs[instr->def.index] = undef;
3566 }
3567
3568 static void visit_jump(struct ac_llvm_context *ctx,
3569 const nir_jump_instr *instr)
3570 {
3571 switch (instr->type) {
3572 case nir_jump_break:
3573 ac_build_break(ctx);
3574 break;
3575 case nir_jump_continue:
3576 ac_build_continue(ctx);
3577 break;
3578 default:
3579 fprintf(stderr, "Unknown NIR jump instr: ");
3580 nir_print_instr(&instr->instr, stderr);
3581 fprintf(stderr, "\n");
3582 abort();
3583 }
3584 }
3585
3586 static void visit_cf_list(struct ac_nir_context *ctx,
3587 struct exec_list *list);
3588
3589 static void visit_block(struct ac_nir_context *ctx, nir_block *block)
3590 {
3591 LLVMBasicBlockRef llvm_block = LLVMGetInsertBlock(ctx->ac.builder);
3592 nir_foreach_instr(instr, block)
3593 {
3594 switch (instr->type) {
3595 case nir_instr_type_alu:
3596 visit_alu(ctx, nir_instr_as_alu(instr));
3597 break;
3598 case nir_instr_type_load_const:
3599 visit_load_const(ctx, nir_instr_as_load_const(instr));
3600 break;
3601 case nir_instr_type_intrinsic:
3602 visit_intrinsic(ctx, nir_instr_as_intrinsic(instr));
3603 break;
3604 case nir_instr_type_tex:
3605 visit_tex(ctx, nir_instr_as_tex(instr));
3606 break;
3607 case nir_instr_type_phi:
3608 visit_phi(ctx, nir_instr_as_phi(instr));
3609 break;
3610 case nir_instr_type_ssa_undef:
3611 visit_ssa_undef(ctx, nir_instr_as_ssa_undef(instr));
3612 break;
3613 case nir_instr_type_jump:
3614 visit_jump(&ctx->ac, nir_instr_as_jump(instr));
3615 break;
3616 default:
3617 fprintf(stderr, "Unknown NIR instr type: ");
3618 nir_print_instr(instr, stderr);
3619 fprintf(stderr, "\n");
3620 abort();
3621 }
3622 }
3623
3624 _mesa_hash_table_insert(ctx->defs, block, llvm_block);
3625 }
3626
3627 static void visit_if(struct ac_nir_context *ctx, nir_if *if_stmt)
3628 {
3629 LLVMValueRef value = get_src(ctx, if_stmt->condition);
3630
3631 nir_block *then_block =
3632 (nir_block *) exec_list_get_head(&if_stmt->then_list);
3633
3634 ac_build_uif(&ctx->ac, value, then_block->index);
3635
3636 visit_cf_list(ctx, &if_stmt->then_list);
3637
3638 if (!exec_list_is_empty(&if_stmt->else_list)) {
3639 nir_block *else_block =
3640 (nir_block *) exec_list_get_head(&if_stmt->else_list);
3641
3642 ac_build_else(&ctx->ac, else_block->index);
3643 visit_cf_list(ctx, &if_stmt->else_list);
3644 }
3645
3646 ac_build_endif(&ctx->ac, then_block->index);
3647 }
3648
3649 static void visit_loop(struct ac_nir_context *ctx, nir_loop *loop)
3650 {
3651 nir_block *first_loop_block =
3652 (nir_block *) exec_list_get_head(&loop->body);
3653
3654 ac_build_bgnloop(&ctx->ac, first_loop_block->index);
3655
3656 visit_cf_list(ctx, &loop->body);
3657
3658 ac_build_endloop(&ctx->ac, first_loop_block->index);
3659 }
3660
3661 static void visit_cf_list(struct ac_nir_context *ctx,
3662 struct exec_list *list)
3663 {
3664 foreach_list_typed(nir_cf_node, node, node, list)
3665 {
3666 switch (node->type) {
3667 case nir_cf_node_block:
3668 visit_block(ctx, nir_cf_node_as_block(node));
3669 break;
3670
3671 case nir_cf_node_if:
3672 visit_if(ctx, nir_cf_node_as_if(node));
3673 break;
3674
3675 case nir_cf_node_loop:
3676 visit_loop(ctx, nir_cf_node_as_loop(node));
3677 break;
3678
3679 default:
3680 assert(0);
3681 }
3682 }
3683 }
3684
3685 void
3686 ac_handle_shader_output_decl(struct ac_llvm_context *ctx,
3687 struct ac_shader_abi *abi,
3688 struct nir_shader *nir,
3689 struct nir_variable *variable,
3690 gl_shader_stage stage)
3691 {
3692 unsigned output_loc = variable->data.driver_location / 4;
3693 unsigned attrib_count = glsl_count_attribute_slots(variable->type, false);
3694
3695 /* tess ctrl has it's own load/store paths for outputs */
3696 if (stage == MESA_SHADER_TESS_CTRL)
3697 return;
3698
3699 if (stage == MESA_SHADER_VERTEX ||
3700 stage == MESA_SHADER_TESS_EVAL ||
3701 stage == MESA_SHADER_GEOMETRY) {
3702 int idx = variable->data.location + variable->data.index;
3703 if (idx == VARYING_SLOT_CLIP_DIST0) {
3704 int length = nir->info.clip_distance_array_size +
3705 nir->info.cull_distance_array_size;
3706
3707 if (length > 4)
3708 attrib_count = 2;
3709 else
3710 attrib_count = 1;
3711 }
3712 }
3713
3714 for (unsigned i = 0; i < attrib_count; ++i) {
3715 for (unsigned chan = 0; chan < 4; chan++) {
3716 abi->outputs[ac_llvm_reg_index_soa(output_loc + i, chan)] =
3717 ac_build_alloca_undef(ctx, ctx->f32, "");
3718 }
3719 }
3720 }
3721
3722 static LLVMTypeRef
3723 glsl_base_to_llvm_type(struct ac_llvm_context *ac,
3724 enum glsl_base_type type)
3725 {
3726 switch (type) {
3727 case GLSL_TYPE_INT:
3728 case GLSL_TYPE_UINT:
3729 case GLSL_TYPE_BOOL:
3730 case GLSL_TYPE_SUBROUTINE:
3731 return ac->i32;
3732 case GLSL_TYPE_FLOAT: /* TODO handle mediump */
3733 return ac->f32;
3734 case GLSL_TYPE_INT64:
3735 case GLSL_TYPE_UINT64:
3736 return ac->i64;
3737 case GLSL_TYPE_DOUBLE:
3738 return ac->f64;
3739 default:
3740 unreachable("unknown GLSL type");
3741 }
3742 }
3743
3744 static LLVMTypeRef
3745 glsl_to_llvm_type(struct ac_llvm_context *ac,
3746 const struct glsl_type *type)
3747 {
3748 if (glsl_type_is_scalar(type)) {
3749 return glsl_base_to_llvm_type(ac, glsl_get_base_type(type));
3750 }
3751
3752 if (glsl_type_is_vector(type)) {
3753 return LLVMVectorType(
3754 glsl_base_to_llvm_type(ac, glsl_get_base_type(type)),
3755 glsl_get_vector_elements(type));
3756 }
3757
3758 if (glsl_type_is_matrix(type)) {
3759 return LLVMArrayType(
3760 glsl_to_llvm_type(ac, glsl_get_column_type(type)),
3761 glsl_get_matrix_columns(type));
3762 }
3763
3764 if (glsl_type_is_array(type)) {
3765 return LLVMArrayType(
3766 glsl_to_llvm_type(ac, glsl_get_array_element(type)),
3767 glsl_get_length(type));
3768 }
3769
3770 assert(glsl_type_is_struct(type));
3771
3772 LLVMTypeRef member_types[glsl_get_length(type)];
3773
3774 for (unsigned i = 0; i < glsl_get_length(type); i++) {
3775 member_types[i] =
3776 glsl_to_llvm_type(ac,
3777 glsl_get_struct_field(type, i));
3778 }
3779
3780 return LLVMStructTypeInContext(ac->context, member_types,
3781 glsl_get_length(type), false);
3782 }
3783
3784 static void
3785 setup_locals(struct ac_nir_context *ctx,
3786 struct nir_function *func)
3787 {
3788 int i, j;
3789 ctx->num_locals = 0;
3790 nir_foreach_variable(variable, &func->impl->locals) {
3791 unsigned attrib_count = glsl_count_attribute_slots(variable->type, false);
3792 variable->data.driver_location = ctx->num_locals * 4;
3793 variable->data.location_frac = 0;
3794 ctx->num_locals += attrib_count;
3795 }
3796 ctx->locals = malloc(4 * ctx->num_locals * sizeof(LLVMValueRef));
3797 if (!ctx->locals)
3798 return;
3799
3800 for (i = 0; i < ctx->num_locals; i++) {
3801 for (j = 0; j < 4; j++) {
3802 ctx->locals[i * 4 + j] =
3803 ac_build_alloca_undef(&ctx->ac, ctx->ac.f32, "temp");
3804 }
3805 }
3806 }
3807
3808 static void
3809 setup_shared(struct ac_nir_context *ctx,
3810 struct nir_shader *nir)
3811 {
3812 nir_foreach_variable(variable, &nir->shared) {
3813 LLVMValueRef shared =
3814 LLVMAddGlobalInAddressSpace(
3815 ctx->ac.module, glsl_to_llvm_type(&ctx->ac, variable->type),
3816 variable->name ? variable->name : "",
3817 AC_LOCAL_ADDR_SPACE);
3818 _mesa_hash_table_insert(ctx->vars, variable, shared);
3819 }
3820 }
3821
3822 void ac_nir_translate(struct ac_llvm_context *ac, struct ac_shader_abi *abi,
3823 struct nir_shader *nir)
3824 {
3825 struct ac_nir_context ctx = {};
3826 struct nir_function *func;
3827
3828 ctx.ac = *ac;
3829 ctx.abi = abi;
3830
3831 ctx.stage = nir->info.stage;
3832
3833 ctx.main_function = LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx.ac.builder));
3834
3835 nir_foreach_variable(variable, &nir->outputs)
3836 ac_handle_shader_output_decl(&ctx.ac, ctx.abi, nir, variable,
3837 ctx.stage);
3838
3839 ctx.defs = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
3840 _mesa_key_pointer_equal);
3841 ctx.phis = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
3842 _mesa_key_pointer_equal);
3843 ctx.vars = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
3844 _mesa_key_pointer_equal);
3845
3846 func = (struct nir_function *)exec_list_get_head(&nir->functions);
3847
3848 nir_index_ssa_defs(func->impl);
3849 ctx.ssa_defs = calloc(func->impl->ssa_alloc, sizeof(LLVMValueRef));
3850
3851 setup_locals(&ctx, func);
3852
3853 if (nir->info.stage == MESA_SHADER_COMPUTE)
3854 setup_shared(&ctx, nir);
3855
3856 visit_cf_list(&ctx, &func->impl->body);
3857 phi_post_pass(&ctx);
3858
3859 if (nir->info.stage != MESA_SHADER_COMPUTE)
3860 ctx.abi->emit_outputs(ctx.abi, AC_LLVM_MAX_OUTPUTS,
3861 ctx.abi->outputs);
3862
3863 free(ctx.locals);
3864 free(ctx.ssa_defs);
3865 ralloc_free(ctx.defs);
3866 ralloc_free(ctx.phis);
3867 ralloc_free(ctx.vars);
3868 }
3869
3870 void
3871 ac_lower_indirect_derefs(struct nir_shader *nir, enum chip_class chip_class)
3872 {
3873 /* While it would be nice not to have this flag, we are constrained
3874 * by the reality that LLVM 5.0 doesn't have working VGPR indexing
3875 * on GFX9.
3876 */
3877 bool llvm_has_working_vgpr_indexing = chip_class <= VI;
3878
3879 /* TODO: Indirect indexing of GS inputs is unimplemented.
3880 *
3881 * TCS and TES load inputs directly from LDS or offchip memory, so
3882 * indirect indexing is trivial.
3883 */
3884 nir_variable_mode indirect_mask = 0;
3885 if (nir->info.stage == MESA_SHADER_GEOMETRY ||
3886 (nir->info.stage != MESA_SHADER_TESS_CTRL &&
3887 nir->info.stage != MESA_SHADER_TESS_EVAL &&
3888 !llvm_has_working_vgpr_indexing)) {
3889 indirect_mask |= nir_var_shader_in;
3890 }
3891 if (!llvm_has_working_vgpr_indexing &&
3892 nir->info.stage != MESA_SHADER_TESS_CTRL)
3893 indirect_mask |= nir_var_shader_out;
3894
3895 /* TODO: We shouldn't need to do this, however LLVM isn't currently
3896 * smart enough to handle indirects without causing excess spilling
3897 * causing the gpu to hang.
3898 *
3899 * See the following thread for more details of the problem:
3900 * https://lists.freedesktop.org/archives/mesa-dev/2017-July/162106.html
3901 */
3902 indirect_mask |= nir_var_local;
3903
3904 nir_lower_indirect_derefs(nir, indirect_mask);
3905 }