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