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