amd/common: pass new enum ac_image_dim to ac_build_image_opcode
[mesa.git] / src / amd / common / ac_llvm_build.c
1 /*
2 * Copyright 2014 Advanced Micro Devices, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sub license, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
15 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
17 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
18 * USE OR OTHER DEALINGS IN THE SOFTWARE.
19 *
20 * The above copyright notice and this permission notice (including the
21 * next paragraph) shall be included in all copies or substantial portions
22 * of the Software.
23 *
24 */
25 /* based on pieces from si_pipe.c and radeon_llvm_emit.c */
26 #include "ac_llvm_build.h"
27
28 #include <llvm-c/Core.h>
29
30 #include "c11/threads.h"
31
32 #include <assert.h>
33 #include <stdio.h>
34
35 #include "ac_llvm_util.h"
36 #include "ac_exp_param.h"
37 #include "util/bitscan.h"
38 #include "util/macros.h"
39 #include "util/u_atomic.h"
40 #include "sid.h"
41
42 #include "shader_enums.h"
43
44 #define AC_LLVM_INITIAL_CF_DEPTH 4
45
46 /* Data for if/else/endif and bgnloop/endloop control flow structures.
47 */
48 struct ac_llvm_flow {
49 /* Loop exit or next part of if/else/endif. */
50 LLVMBasicBlockRef next_block;
51 LLVMBasicBlockRef loop_entry_block;
52 };
53
54 /* Initialize module-independent parts of the context.
55 *
56 * The caller is responsible for initializing ctx::module and ctx::builder.
57 */
58 void
59 ac_llvm_context_init(struct ac_llvm_context *ctx, LLVMContextRef context,
60 enum chip_class chip_class, enum radeon_family family)
61 {
62 LLVMValueRef args[1];
63
64 ctx->chip_class = chip_class;
65 ctx->family = family;
66
67 ctx->context = context;
68 ctx->module = NULL;
69 ctx->builder = NULL;
70
71 ctx->voidt = LLVMVoidTypeInContext(ctx->context);
72 ctx->i1 = LLVMInt1TypeInContext(ctx->context);
73 ctx->i8 = LLVMInt8TypeInContext(ctx->context);
74 ctx->i16 = LLVMIntTypeInContext(ctx->context, 16);
75 ctx->i32 = LLVMIntTypeInContext(ctx->context, 32);
76 ctx->i64 = LLVMIntTypeInContext(ctx->context, 64);
77 ctx->intptr = HAVE_32BIT_POINTERS ? ctx->i32 : ctx->i64;
78 ctx->f16 = LLVMHalfTypeInContext(ctx->context);
79 ctx->f32 = LLVMFloatTypeInContext(ctx->context);
80 ctx->f64 = LLVMDoubleTypeInContext(ctx->context);
81 ctx->v2i16 = LLVMVectorType(ctx->i16, 2);
82 ctx->v2i32 = LLVMVectorType(ctx->i32, 2);
83 ctx->v3i32 = LLVMVectorType(ctx->i32, 3);
84 ctx->v4i32 = LLVMVectorType(ctx->i32, 4);
85 ctx->v2f32 = LLVMVectorType(ctx->f32, 2);
86 ctx->v4f32 = LLVMVectorType(ctx->f32, 4);
87 ctx->v8i32 = LLVMVectorType(ctx->i32, 8);
88
89 ctx->i32_0 = LLVMConstInt(ctx->i32, 0, false);
90 ctx->i32_1 = LLVMConstInt(ctx->i32, 1, false);
91 ctx->i64_0 = LLVMConstInt(ctx->i64, 0, false);
92 ctx->i64_1 = LLVMConstInt(ctx->i64, 1, false);
93 ctx->f32_0 = LLVMConstReal(ctx->f32, 0.0);
94 ctx->f32_1 = LLVMConstReal(ctx->f32, 1.0);
95 ctx->f64_0 = LLVMConstReal(ctx->f64, 0.0);
96 ctx->f64_1 = LLVMConstReal(ctx->f64, 1.0);
97
98 ctx->i1false = LLVMConstInt(ctx->i1, 0, false);
99 ctx->i1true = LLVMConstInt(ctx->i1, 1, false);
100
101 ctx->range_md_kind = LLVMGetMDKindIDInContext(ctx->context,
102 "range", 5);
103
104 ctx->invariant_load_md_kind = LLVMGetMDKindIDInContext(ctx->context,
105 "invariant.load", 14);
106
107 ctx->fpmath_md_kind = LLVMGetMDKindIDInContext(ctx->context, "fpmath", 6);
108
109 args[0] = LLVMConstReal(ctx->f32, 2.5);
110 ctx->fpmath_md_2p5_ulp = LLVMMDNodeInContext(ctx->context, args, 1);
111
112 ctx->uniform_md_kind = LLVMGetMDKindIDInContext(ctx->context,
113 "amdgpu.uniform", 14);
114
115 ctx->empty_md = LLVMMDNodeInContext(ctx->context, NULL, 0);
116 }
117
118 void
119 ac_llvm_context_dispose(struct ac_llvm_context *ctx)
120 {
121 free(ctx->flow);
122 ctx->flow = NULL;
123 ctx->flow_depth_max = 0;
124 }
125
126 int
127 ac_get_llvm_num_components(LLVMValueRef value)
128 {
129 LLVMTypeRef type = LLVMTypeOf(value);
130 unsigned num_components = LLVMGetTypeKind(type) == LLVMVectorTypeKind
131 ? LLVMGetVectorSize(type)
132 : 1;
133 return num_components;
134 }
135
136 LLVMValueRef
137 ac_llvm_extract_elem(struct ac_llvm_context *ac,
138 LLVMValueRef value,
139 int index)
140 {
141 if (LLVMGetTypeKind(LLVMTypeOf(value)) != LLVMVectorTypeKind) {
142 assert(index == 0);
143 return value;
144 }
145
146 return LLVMBuildExtractElement(ac->builder, value,
147 LLVMConstInt(ac->i32, index, false), "");
148 }
149
150 int
151 ac_get_elem_bits(struct ac_llvm_context *ctx, LLVMTypeRef type)
152 {
153 if (LLVMGetTypeKind(type) == LLVMVectorTypeKind)
154 type = LLVMGetElementType(type);
155
156 if (LLVMGetTypeKind(type) == LLVMIntegerTypeKind)
157 return LLVMGetIntTypeWidth(type);
158
159 if (type == ctx->f16)
160 return 16;
161 if (type == ctx->f32)
162 return 32;
163 if (type == ctx->f64)
164 return 64;
165
166 unreachable("Unhandled type kind in get_elem_bits");
167 }
168
169 unsigned
170 ac_get_type_size(LLVMTypeRef type)
171 {
172 LLVMTypeKind kind = LLVMGetTypeKind(type);
173
174 switch (kind) {
175 case LLVMIntegerTypeKind:
176 return LLVMGetIntTypeWidth(type) / 8;
177 case LLVMFloatTypeKind:
178 return 4;
179 case LLVMDoubleTypeKind:
180 return 8;
181 case LLVMPointerTypeKind:
182 if (LLVMGetPointerAddressSpace(type) == AC_CONST_32BIT_ADDR_SPACE)
183 return 4;
184 return 8;
185 case LLVMVectorTypeKind:
186 return LLVMGetVectorSize(type) *
187 ac_get_type_size(LLVMGetElementType(type));
188 case LLVMArrayTypeKind:
189 return LLVMGetArrayLength(type) *
190 ac_get_type_size(LLVMGetElementType(type));
191 default:
192 assert(0);
193 return 0;
194 }
195 }
196
197 static LLVMTypeRef to_integer_type_scalar(struct ac_llvm_context *ctx, LLVMTypeRef t)
198 {
199 if (t == ctx->f16 || t == ctx->i16)
200 return ctx->i16;
201 else if (t == ctx->f32 || t == ctx->i32)
202 return ctx->i32;
203 else if (t == ctx->f64 || t == ctx->i64)
204 return ctx->i64;
205 else
206 unreachable("Unhandled integer size");
207 }
208
209 LLVMTypeRef
210 ac_to_integer_type(struct ac_llvm_context *ctx, LLVMTypeRef t)
211 {
212 if (LLVMGetTypeKind(t) == LLVMVectorTypeKind) {
213 LLVMTypeRef elem_type = LLVMGetElementType(t);
214 return LLVMVectorType(to_integer_type_scalar(ctx, elem_type),
215 LLVMGetVectorSize(t));
216 }
217 return to_integer_type_scalar(ctx, t);
218 }
219
220 LLVMValueRef
221 ac_to_integer(struct ac_llvm_context *ctx, LLVMValueRef v)
222 {
223 LLVMTypeRef type = LLVMTypeOf(v);
224 return LLVMBuildBitCast(ctx->builder, v, ac_to_integer_type(ctx, type), "");
225 }
226
227 static LLVMTypeRef to_float_type_scalar(struct ac_llvm_context *ctx, LLVMTypeRef t)
228 {
229 if (t == ctx->i16 || t == ctx->f16)
230 return ctx->f16;
231 else if (t == ctx->i32 || t == ctx->f32)
232 return ctx->f32;
233 else if (t == ctx->i64 || t == ctx->f64)
234 return ctx->f64;
235 else
236 unreachable("Unhandled float size");
237 }
238
239 LLVMTypeRef
240 ac_to_float_type(struct ac_llvm_context *ctx, LLVMTypeRef t)
241 {
242 if (LLVMGetTypeKind(t) == LLVMVectorTypeKind) {
243 LLVMTypeRef elem_type = LLVMGetElementType(t);
244 return LLVMVectorType(to_float_type_scalar(ctx, elem_type),
245 LLVMGetVectorSize(t));
246 }
247 return to_float_type_scalar(ctx, t);
248 }
249
250 LLVMValueRef
251 ac_to_float(struct ac_llvm_context *ctx, LLVMValueRef v)
252 {
253 LLVMTypeRef type = LLVMTypeOf(v);
254 return LLVMBuildBitCast(ctx->builder, v, ac_to_float_type(ctx, type), "");
255 }
256
257
258 LLVMValueRef
259 ac_build_intrinsic(struct ac_llvm_context *ctx, const char *name,
260 LLVMTypeRef return_type, LLVMValueRef *params,
261 unsigned param_count, unsigned attrib_mask)
262 {
263 LLVMValueRef function, call;
264 bool set_callsite_attrs = !(attrib_mask & AC_FUNC_ATTR_LEGACY);
265
266 function = LLVMGetNamedFunction(ctx->module, name);
267 if (!function) {
268 LLVMTypeRef param_types[32], function_type;
269 unsigned i;
270
271 assert(param_count <= 32);
272
273 for (i = 0; i < param_count; ++i) {
274 assert(params[i]);
275 param_types[i] = LLVMTypeOf(params[i]);
276 }
277 function_type =
278 LLVMFunctionType(return_type, param_types, param_count, 0);
279 function = LLVMAddFunction(ctx->module, name, function_type);
280
281 LLVMSetFunctionCallConv(function, LLVMCCallConv);
282 LLVMSetLinkage(function, LLVMExternalLinkage);
283
284 if (!set_callsite_attrs)
285 ac_add_func_attributes(ctx->context, function, attrib_mask);
286 }
287
288 call = LLVMBuildCall(ctx->builder, function, params, param_count, "");
289 if (set_callsite_attrs)
290 ac_add_func_attributes(ctx->context, call, attrib_mask);
291 return call;
292 }
293
294 /**
295 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
296 * intrinsic names).
297 */
298 void ac_build_type_name_for_intr(LLVMTypeRef type, char *buf, unsigned bufsize)
299 {
300 LLVMTypeRef elem_type = type;
301
302 assert(bufsize >= 8);
303
304 if (LLVMGetTypeKind(type) == LLVMVectorTypeKind) {
305 int ret = snprintf(buf, bufsize, "v%u",
306 LLVMGetVectorSize(type));
307 if (ret < 0) {
308 char *type_name = LLVMPrintTypeToString(type);
309 fprintf(stderr, "Error building type name for: %s\n",
310 type_name);
311 return;
312 }
313 elem_type = LLVMGetElementType(type);
314 buf += ret;
315 bufsize -= ret;
316 }
317 switch (LLVMGetTypeKind(elem_type)) {
318 default: break;
319 case LLVMIntegerTypeKind:
320 snprintf(buf, bufsize, "i%d", LLVMGetIntTypeWidth(elem_type));
321 break;
322 case LLVMFloatTypeKind:
323 snprintf(buf, bufsize, "f32");
324 break;
325 case LLVMDoubleTypeKind:
326 snprintf(buf, bufsize, "f64");
327 break;
328 }
329 }
330
331 /**
332 * Helper function that builds an LLVM IR PHI node and immediately adds
333 * incoming edges.
334 */
335 LLVMValueRef
336 ac_build_phi(struct ac_llvm_context *ctx, LLVMTypeRef type,
337 unsigned count_incoming, LLVMValueRef *values,
338 LLVMBasicBlockRef *blocks)
339 {
340 LLVMValueRef phi = LLVMBuildPhi(ctx->builder, type, "");
341 LLVMAddIncoming(phi, values, blocks, count_incoming);
342 return phi;
343 }
344
345 /* Prevent optimizations (at least of memory accesses) across the current
346 * point in the program by emitting empty inline assembly that is marked as
347 * having side effects.
348 *
349 * Optionally, a value can be passed through the inline assembly to prevent
350 * LLVM from hoisting calls to ReadNone functions.
351 */
352 void
353 ac_build_optimization_barrier(struct ac_llvm_context *ctx,
354 LLVMValueRef *pvgpr)
355 {
356 static int counter = 0;
357
358 LLVMBuilderRef builder = ctx->builder;
359 char code[16];
360
361 snprintf(code, sizeof(code), "; %d", p_atomic_inc_return(&counter));
362
363 if (!pvgpr) {
364 LLVMTypeRef ftype = LLVMFunctionType(ctx->voidt, NULL, 0, false);
365 LLVMValueRef inlineasm = LLVMConstInlineAsm(ftype, code, "", true, false);
366 LLVMBuildCall(builder, inlineasm, NULL, 0, "");
367 } else {
368 LLVMTypeRef ftype = LLVMFunctionType(ctx->i32, &ctx->i32, 1, false);
369 LLVMValueRef inlineasm = LLVMConstInlineAsm(ftype, code, "=v,0", true, false);
370 LLVMValueRef vgpr = *pvgpr;
371 LLVMTypeRef vgpr_type = LLVMTypeOf(vgpr);
372 unsigned vgpr_size = ac_get_type_size(vgpr_type);
373 LLVMValueRef vgpr0;
374
375 assert(vgpr_size % 4 == 0);
376
377 vgpr = LLVMBuildBitCast(builder, vgpr, LLVMVectorType(ctx->i32, vgpr_size / 4), "");
378 vgpr0 = LLVMBuildExtractElement(builder, vgpr, ctx->i32_0, "");
379 vgpr0 = LLVMBuildCall(builder, inlineasm, &vgpr0, 1, "");
380 vgpr = LLVMBuildInsertElement(builder, vgpr, vgpr0, ctx->i32_0, "");
381 vgpr = LLVMBuildBitCast(builder, vgpr, vgpr_type, "");
382
383 *pvgpr = vgpr;
384 }
385 }
386
387 LLVMValueRef
388 ac_build_shader_clock(struct ac_llvm_context *ctx)
389 {
390 LLVMValueRef tmp = ac_build_intrinsic(ctx, "llvm.readcyclecounter",
391 ctx->i64, NULL, 0, 0);
392 return LLVMBuildBitCast(ctx->builder, tmp, ctx->v2i32, "");
393 }
394
395 LLVMValueRef
396 ac_build_ballot(struct ac_llvm_context *ctx,
397 LLVMValueRef value)
398 {
399 LLVMValueRef args[3] = {
400 value,
401 ctx->i32_0,
402 LLVMConstInt(ctx->i32, LLVMIntNE, 0)
403 };
404
405 /* We currently have no other way to prevent LLVM from lifting the icmp
406 * calls to a dominating basic block.
407 */
408 ac_build_optimization_barrier(ctx, &args[0]);
409
410 args[0] = ac_to_integer(ctx, args[0]);
411
412 return ac_build_intrinsic(ctx,
413 "llvm.amdgcn.icmp.i32",
414 ctx->i64, args, 3,
415 AC_FUNC_ATTR_NOUNWIND |
416 AC_FUNC_ATTR_READNONE |
417 AC_FUNC_ATTR_CONVERGENT);
418 }
419
420 LLVMValueRef
421 ac_build_vote_all(struct ac_llvm_context *ctx, LLVMValueRef value)
422 {
423 LLVMValueRef active_set = ac_build_ballot(ctx, ctx->i32_1);
424 LLVMValueRef vote_set = ac_build_ballot(ctx, value);
425 return LLVMBuildICmp(ctx->builder, LLVMIntEQ, vote_set, active_set, "");
426 }
427
428 LLVMValueRef
429 ac_build_vote_any(struct ac_llvm_context *ctx, LLVMValueRef value)
430 {
431 LLVMValueRef vote_set = ac_build_ballot(ctx, value);
432 return LLVMBuildICmp(ctx->builder, LLVMIntNE, vote_set,
433 LLVMConstInt(ctx->i64, 0, 0), "");
434 }
435
436 LLVMValueRef
437 ac_build_vote_eq(struct ac_llvm_context *ctx, LLVMValueRef value)
438 {
439 LLVMValueRef active_set = ac_build_ballot(ctx, ctx->i32_1);
440 LLVMValueRef vote_set = ac_build_ballot(ctx, value);
441
442 LLVMValueRef all = LLVMBuildICmp(ctx->builder, LLVMIntEQ,
443 vote_set, active_set, "");
444 LLVMValueRef none = LLVMBuildICmp(ctx->builder, LLVMIntEQ,
445 vote_set,
446 LLVMConstInt(ctx->i64, 0, 0), "");
447 return LLVMBuildOr(ctx->builder, all, none, "");
448 }
449
450 LLVMValueRef
451 ac_build_varying_gather_values(struct ac_llvm_context *ctx, LLVMValueRef *values,
452 unsigned value_count, unsigned component)
453 {
454 LLVMValueRef vec = NULL;
455
456 if (value_count == 1) {
457 return values[component];
458 } else if (!value_count)
459 unreachable("value_count is 0");
460
461 for (unsigned i = component; i < value_count + component; i++) {
462 LLVMValueRef value = values[i];
463
464 if (i == component)
465 vec = LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value), value_count));
466 LLVMValueRef index = LLVMConstInt(ctx->i32, i - component, false);
467 vec = LLVMBuildInsertElement(ctx->builder, vec, value, index, "");
468 }
469 return vec;
470 }
471
472 LLVMValueRef
473 ac_build_gather_values_extended(struct ac_llvm_context *ctx,
474 LLVMValueRef *values,
475 unsigned value_count,
476 unsigned value_stride,
477 bool load,
478 bool always_vector)
479 {
480 LLVMBuilderRef builder = ctx->builder;
481 LLVMValueRef vec = NULL;
482 unsigned i;
483
484 if (value_count == 1 && !always_vector) {
485 if (load)
486 return LLVMBuildLoad(builder, values[0], "");
487 return values[0];
488 } else if (!value_count)
489 unreachable("value_count is 0");
490
491 for (i = 0; i < value_count; i++) {
492 LLVMValueRef value = values[i * value_stride];
493 if (load)
494 value = LLVMBuildLoad(builder, value, "");
495
496 if (!i)
497 vec = LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value), value_count));
498 LLVMValueRef index = LLVMConstInt(ctx->i32, i, false);
499 vec = LLVMBuildInsertElement(builder, vec, value, index, "");
500 }
501 return vec;
502 }
503
504 LLVMValueRef
505 ac_build_gather_values(struct ac_llvm_context *ctx,
506 LLVMValueRef *values,
507 unsigned value_count)
508 {
509 return ac_build_gather_values_extended(ctx, values, value_count, 1, false, false);
510 }
511
512 /* Expand a scalar or vector to <4 x type> by filling the remaining channels
513 * with undef. Extract at most num_channels components from the input.
514 */
515 LLVMValueRef ac_build_expand_to_vec4(struct ac_llvm_context *ctx,
516 LLVMValueRef value,
517 unsigned num_channels)
518 {
519 LLVMTypeRef elemtype;
520 LLVMValueRef chan[4];
521
522 if (LLVMGetTypeKind(LLVMTypeOf(value)) == LLVMVectorTypeKind) {
523 unsigned vec_size = LLVMGetVectorSize(LLVMTypeOf(value));
524 num_channels = MIN2(num_channels, vec_size);
525
526 if (num_channels >= 4)
527 return value;
528
529 for (unsigned i = 0; i < num_channels; i++)
530 chan[i] = ac_llvm_extract_elem(ctx, value, i);
531
532 elemtype = LLVMGetElementType(LLVMTypeOf(value));
533 } else {
534 if (num_channels) {
535 assert(num_channels == 1);
536 chan[0] = value;
537 }
538 elemtype = LLVMTypeOf(value);
539 }
540
541 while (num_channels < 4)
542 chan[num_channels++] = LLVMGetUndef(elemtype);
543
544 return ac_build_gather_values(ctx, chan, 4);
545 }
546
547 LLVMValueRef
548 ac_build_fdiv(struct ac_llvm_context *ctx,
549 LLVMValueRef num,
550 LLVMValueRef den)
551 {
552 LLVMValueRef ret = LLVMBuildFDiv(ctx->builder, num, den, "");
553
554 /* Use v_rcp_f32 instead of precise division. */
555 if (!LLVMIsConstant(ret))
556 LLVMSetMetadata(ret, ctx->fpmath_md_kind, ctx->fpmath_md_2p5_ulp);
557 return ret;
558 }
559
560 /* Coordinates for cube map selection. sc, tc, and ma are as in Table 8.27
561 * of the OpenGL 4.5 (Compatibility Profile) specification, except ma is
562 * already multiplied by two. id is the cube face number.
563 */
564 struct cube_selection_coords {
565 LLVMValueRef stc[2];
566 LLVMValueRef ma;
567 LLVMValueRef id;
568 };
569
570 static void
571 build_cube_intrinsic(struct ac_llvm_context *ctx,
572 LLVMValueRef in[3],
573 struct cube_selection_coords *out)
574 {
575 LLVMTypeRef f32 = ctx->f32;
576
577 out->stc[1] = ac_build_intrinsic(ctx, "llvm.amdgcn.cubetc",
578 f32, in, 3, AC_FUNC_ATTR_READNONE);
579 out->stc[0] = ac_build_intrinsic(ctx, "llvm.amdgcn.cubesc",
580 f32, in, 3, AC_FUNC_ATTR_READNONE);
581 out->ma = ac_build_intrinsic(ctx, "llvm.amdgcn.cubema",
582 f32, in, 3, AC_FUNC_ATTR_READNONE);
583 out->id = ac_build_intrinsic(ctx, "llvm.amdgcn.cubeid",
584 f32, in, 3, AC_FUNC_ATTR_READNONE);
585 }
586
587 /**
588 * Build a manual selection sequence for cube face sc/tc coordinates and
589 * major axis vector (multiplied by 2 for consistency) for the given
590 * vec3 \p coords, for the face implied by \p selcoords.
591 *
592 * For the major axis, we always adjust the sign to be in the direction of
593 * selcoords.ma; i.e., a positive out_ma means that coords is pointed towards
594 * the selcoords major axis.
595 */
596 static void build_cube_select(struct ac_llvm_context *ctx,
597 const struct cube_selection_coords *selcoords,
598 const LLVMValueRef *coords,
599 LLVMValueRef *out_st,
600 LLVMValueRef *out_ma)
601 {
602 LLVMBuilderRef builder = ctx->builder;
603 LLVMTypeRef f32 = LLVMTypeOf(coords[0]);
604 LLVMValueRef is_ma_positive;
605 LLVMValueRef sgn_ma;
606 LLVMValueRef is_ma_z, is_not_ma_z;
607 LLVMValueRef is_ma_y;
608 LLVMValueRef is_ma_x;
609 LLVMValueRef sgn;
610 LLVMValueRef tmp;
611
612 is_ma_positive = LLVMBuildFCmp(builder, LLVMRealUGE,
613 selcoords->ma, LLVMConstReal(f32, 0.0), "");
614 sgn_ma = LLVMBuildSelect(builder, is_ma_positive,
615 LLVMConstReal(f32, 1.0), LLVMConstReal(f32, -1.0), "");
616
617 is_ma_z = LLVMBuildFCmp(builder, LLVMRealUGE, selcoords->id, LLVMConstReal(f32, 4.0), "");
618 is_not_ma_z = LLVMBuildNot(builder, is_ma_z, "");
619 is_ma_y = LLVMBuildAnd(builder, is_not_ma_z,
620 LLVMBuildFCmp(builder, LLVMRealUGE, selcoords->id, LLVMConstReal(f32, 2.0), ""), "");
621 is_ma_x = LLVMBuildAnd(builder, is_not_ma_z, LLVMBuildNot(builder, is_ma_y, ""), "");
622
623 /* Select sc */
624 tmp = LLVMBuildSelect(builder, is_ma_x, coords[2], coords[0], "");
625 sgn = LLVMBuildSelect(builder, is_ma_y, LLVMConstReal(f32, 1.0),
626 LLVMBuildSelect(builder, is_ma_z, sgn_ma,
627 LLVMBuildFNeg(builder, sgn_ma, ""), ""), "");
628 out_st[0] = LLVMBuildFMul(builder, tmp, sgn, "");
629
630 /* Select tc */
631 tmp = LLVMBuildSelect(builder, is_ma_y, coords[2], coords[1], "");
632 sgn = LLVMBuildSelect(builder, is_ma_y, sgn_ma,
633 LLVMConstReal(f32, -1.0), "");
634 out_st[1] = LLVMBuildFMul(builder, tmp, sgn, "");
635
636 /* Select ma */
637 tmp = LLVMBuildSelect(builder, is_ma_z, coords[2],
638 LLVMBuildSelect(builder, is_ma_y, coords[1], coords[0], ""), "");
639 tmp = ac_build_intrinsic(ctx, "llvm.fabs.f32",
640 ctx->f32, &tmp, 1, AC_FUNC_ATTR_READNONE);
641 *out_ma = LLVMBuildFMul(builder, tmp, LLVMConstReal(f32, 2.0), "");
642 }
643
644 void
645 ac_prepare_cube_coords(struct ac_llvm_context *ctx,
646 bool is_deriv, bool is_array, bool is_lod,
647 LLVMValueRef *coords_arg,
648 LLVMValueRef *derivs_arg)
649 {
650
651 LLVMBuilderRef builder = ctx->builder;
652 struct cube_selection_coords selcoords;
653 LLVMValueRef coords[3];
654 LLVMValueRef invma;
655
656 if (is_array && !is_lod) {
657 LLVMValueRef tmp = coords_arg[3];
658 tmp = ac_build_intrinsic(ctx, "llvm.rint.f32", ctx->f32, &tmp, 1, 0);
659
660 /* Section 8.9 (Texture Functions) of the GLSL 4.50 spec says:
661 *
662 * "For Array forms, the array layer used will be
663 *
664 * max(0, min(d−1, floor(layer+0.5)))
665 *
666 * where d is the depth of the texture array and layer
667 * comes from the component indicated in the tables below.
668 * Workaroudn for an issue where the layer is taken from a
669 * helper invocation which happens to fall on a different
670 * layer due to extrapolation."
671 *
672 * VI and earlier attempt to implement this in hardware by
673 * clamping the value of coords[2] = (8 * layer) + face.
674 * Unfortunately, this means that the we end up with the wrong
675 * face when clamping occurs.
676 *
677 * Clamp the layer earlier to work around the issue.
678 */
679 if (ctx->chip_class <= VI) {
680 LLVMValueRef ge0;
681 ge0 = LLVMBuildFCmp(builder, LLVMRealOGE, tmp, ctx->f32_0, "");
682 tmp = LLVMBuildSelect(builder, ge0, tmp, ctx->f32_0, "");
683 }
684
685 coords_arg[3] = tmp;
686 }
687
688 build_cube_intrinsic(ctx, coords_arg, &selcoords);
689
690 invma = ac_build_intrinsic(ctx, "llvm.fabs.f32",
691 ctx->f32, &selcoords.ma, 1, AC_FUNC_ATTR_READNONE);
692 invma = ac_build_fdiv(ctx, LLVMConstReal(ctx->f32, 1.0), invma);
693
694 for (int i = 0; i < 2; ++i)
695 coords[i] = LLVMBuildFMul(builder, selcoords.stc[i], invma, "");
696
697 coords[2] = selcoords.id;
698
699 if (is_deriv && derivs_arg) {
700 LLVMValueRef derivs[4];
701 int axis;
702
703 /* Convert cube derivatives to 2D derivatives. */
704 for (axis = 0; axis < 2; axis++) {
705 LLVMValueRef deriv_st[2];
706 LLVMValueRef deriv_ma;
707
708 /* Transform the derivative alongside the texture
709 * coordinate. Mathematically, the correct formula is
710 * as follows. Assume we're projecting onto the +Z face
711 * and denote by dx/dh the derivative of the (original)
712 * X texture coordinate with respect to horizontal
713 * window coordinates. The projection onto the +Z face
714 * plane is:
715 *
716 * f(x,z) = x/z
717 *
718 * Then df/dh = df/dx * dx/dh + df/dz * dz/dh
719 * = 1/z * dx/dh - x/z * 1/z * dz/dh.
720 *
721 * This motivatives the implementation below.
722 *
723 * Whether this actually gives the expected results for
724 * apps that might feed in derivatives obtained via
725 * finite differences is anyone's guess. The OpenGL spec
726 * seems awfully quiet about how textureGrad for cube
727 * maps should be handled.
728 */
729 build_cube_select(ctx, &selcoords, &derivs_arg[axis * 3],
730 deriv_st, &deriv_ma);
731
732 deriv_ma = LLVMBuildFMul(builder, deriv_ma, invma, "");
733
734 for (int i = 0; i < 2; ++i)
735 derivs[axis * 2 + i] =
736 LLVMBuildFSub(builder,
737 LLVMBuildFMul(builder, deriv_st[i], invma, ""),
738 LLVMBuildFMul(builder, deriv_ma, coords[i], ""), "");
739 }
740
741 memcpy(derivs_arg, derivs, sizeof(derivs));
742 }
743
744 /* Shift the texture coordinate. This must be applied after the
745 * derivative calculation.
746 */
747 for (int i = 0; i < 2; ++i)
748 coords[i] = LLVMBuildFAdd(builder, coords[i], LLVMConstReal(ctx->f32, 1.5), "");
749
750 if (is_array) {
751 /* for cube arrays coord.z = coord.w(array_index) * 8 + face */
752 /* coords_arg.w component - array_index for cube arrays */
753 LLVMValueRef tmp = LLVMBuildFMul(ctx->builder, coords_arg[3], LLVMConstReal(ctx->f32, 8.0), "");
754 coords[2] = LLVMBuildFAdd(ctx->builder, tmp, coords[2], "");
755 }
756
757 memcpy(coords_arg, coords, sizeof(coords));
758 }
759
760
761 LLVMValueRef
762 ac_build_fs_interp(struct ac_llvm_context *ctx,
763 LLVMValueRef llvm_chan,
764 LLVMValueRef attr_number,
765 LLVMValueRef params,
766 LLVMValueRef i,
767 LLVMValueRef j)
768 {
769 LLVMValueRef args[5];
770 LLVMValueRef p1;
771
772 args[0] = i;
773 args[1] = llvm_chan;
774 args[2] = attr_number;
775 args[3] = params;
776
777 p1 = ac_build_intrinsic(ctx, "llvm.amdgcn.interp.p1",
778 ctx->f32, args, 4, AC_FUNC_ATTR_READNONE);
779
780 args[0] = p1;
781 args[1] = j;
782 args[2] = llvm_chan;
783 args[3] = attr_number;
784 args[4] = params;
785
786 return ac_build_intrinsic(ctx, "llvm.amdgcn.interp.p2",
787 ctx->f32, args, 5, AC_FUNC_ATTR_READNONE);
788 }
789
790 LLVMValueRef
791 ac_build_fs_interp_mov(struct ac_llvm_context *ctx,
792 LLVMValueRef parameter,
793 LLVMValueRef llvm_chan,
794 LLVMValueRef attr_number,
795 LLVMValueRef params)
796 {
797 LLVMValueRef args[4];
798
799 args[0] = parameter;
800 args[1] = llvm_chan;
801 args[2] = attr_number;
802 args[3] = params;
803
804 return ac_build_intrinsic(ctx, "llvm.amdgcn.interp.mov",
805 ctx->f32, args, 4, AC_FUNC_ATTR_READNONE);
806 }
807
808 LLVMValueRef
809 ac_build_gep0(struct ac_llvm_context *ctx,
810 LLVMValueRef base_ptr,
811 LLVMValueRef index)
812 {
813 LLVMValueRef indices[2] = {
814 LLVMConstInt(ctx->i32, 0, 0),
815 index,
816 };
817 return LLVMBuildGEP(ctx->builder, base_ptr,
818 indices, 2, "");
819 }
820
821 void
822 ac_build_indexed_store(struct ac_llvm_context *ctx,
823 LLVMValueRef base_ptr, LLVMValueRef index,
824 LLVMValueRef value)
825 {
826 LLVMBuildStore(ctx->builder, value,
827 ac_build_gep0(ctx, base_ptr, index));
828 }
829
830 /**
831 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad.
832 * It's equivalent to doing a load from &base_ptr[index].
833 *
834 * \param base_ptr Where the array starts.
835 * \param index The element index into the array.
836 * \param uniform Whether the base_ptr and index can be assumed to be
837 * dynamically uniform (i.e. load to an SGPR)
838 * \param invariant Whether the load is invariant (no other opcodes affect it)
839 */
840 static LLVMValueRef
841 ac_build_load_custom(struct ac_llvm_context *ctx, LLVMValueRef base_ptr,
842 LLVMValueRef index, bool uniform, bool invariant)
843 {
844 LLVMValueRef pointer, result;
845
846 pointer = ac_build_gep0(ctx, base_ptr, index);
847 if (uniform)
848 LLVMSetMetadata(pointer, ctx->uniform_md_kind, ctx->empty_md);
849 result = LLVMBuildLoad(ctx->builder, pointer, "");
850 if (invariant)
851 LLVMSetMetadata(result, ctx->invariant_load_md_kind, ctx->empty_md);
852 return result;
853 }
854
855 LLVMValueRef ac_build_load(struct ac_llvm_context *ctx, LLVMValueRef base_ptr,
856 LLVMValueRef index)
857 {
858 return ac_build_load_custom(ctx, base_ptr, index, false, false);
859 }
860
861 LLVMValueRef ac_build_load_invariant(struct ac_llvm_context *ctx,
862 LLVMValueRef base_ptr, LLVMValueRef index)
863 {
864 return ac_build_load_custom(ctx, base_ptr, index, false, true);
865 }
866
867 LLVMValueRef ac_build_load_to_sgpr(struct ac_llvm_context *ctx,
868 LLVMValueRef base_ptr, LLVMValueRef index)
869 {
870 return ac_build_load_custom(ctx, base_ptr, index, true, true);
871 }
872
873 /* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
874 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
875 * or v4i32 (num_channels=3,4).
876 */
877 void
878 ac_build_buffer_store_dword(struct ac_llvm_context *ctx,
879 LLVMValueRef rsrc,
880 LLVMValueRef vdata,
881 unsigned num_channels,
882 LLVMValueRef voffset,
883 LLVMValueRef soffset,
884 unsigned inst_offset,
885 bool glc,
886 bool slc,
887 bool writeonly_memory,
888 bool swizzle_enable_hint)
889 {
890 /* SWIZZLE_ENABLE requires that soffset isn't folded into voffset
891 * (voffset is swizzled, but soffset isn't swizzled).
892 * llvm.amdgcn.buffer.store doesn't have a separate soffset parameter.
893 */
894 if (!swizzle_enable_hint) {
895 /* Split 3 channel stores, becase LLVM doesn't support 3-channel
896 * intrinsics. */
897 if (num_channels == 3) {
898 LLVMValueRef v[3], v01;
899
900 for (int i = 0; i < 3; i++) {
901 v[i] = LLVMBuildExtractElement(ctx->builder, vdata,
902 LLVMConstInt(ctx->i32, i, 0), "");
903 }
904 v01 = ac_build_gather_values(ctx, v, 2);
905
906 ac_build_buffer_store_dword(ctx, rsrc, v01, 2, voffset,
907 soffset, inst_offset, glc, slc,
908 writeonly_memory, swizzle_enable_hint);
909 ac_build_buffer_store_dword(ctx, rsrc, v[2], 1, voffset,
910 soffset, inst_offset + 8,
911 glc, slc,
912 writeonly_memory, swizzle_enable_hint);
913 return;
914 }
915
916 unsigned func = CLAMP(num_channels, 1, 3) - 1;
917 static const char *types[] = {"f32", "v2f32", "v4f32"};
918 char name[256];
919 LLVMValueRef offset = soffset;
920
921 if (inst_offset)
922 offset = LLVMBuildAdd(ctx->builder, offset,
923 LLVMConstInt(ctx->i32, inst_offset, 0), "");
924 if (voffset)
925 offset = LLVMBuildAdd(ctx->builder, offset, voffset, "");
926
927 LLVMValueRef args[] = {
928 ac_to_float(ctx, vdata),
929 LLVMBuildBitCast(ctx->builder, rsrc, ctx->v4i32, ""),
930 LLVMConstInt(ctx->i32, 0, 0),
931 offset,
932 LLVMConstInt(ctx->i1, glc, 0),
933 LLVMConstInt(ctx->i1, slc, 0),
934 };
935
936 snprintf(name, sizeof(name), "llvm.amdgcn.buffer.store.%s",
937 types[func]);
938
939 ac_build_intrinsic(ctx, name, ctx->voidt,
940 args, ARRAY_SIZE(args),
941 writeonly_memory ?
942 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY :
943 AC_FUNC_ATTR_WRITEONLY);
944 return;
945 }
946
947 static unsigned dfmt[] = {
948 V_008F0C_BUF_DATA_FORMAT_32,
949 V_008F0C_BUF_DATA_FORMAT_32_32,
950 V_008F0C_BUF_DATA_FORMAT_32_32_32,
951 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
952 };
953 assert(num_channels >= 1 && num_channels <= 4);
954
955 LLVMValueRef args[] = {
956 rsrc,
957 vdata,
958 LLVMConstInt(ctx->i32, num_channels, 0),
959 voffset ? voffset : LLVMGetUndef(ctx->i32),
960 soffset,
961 LLVMConstInt(ctx->i32, inst_offset, 0),
962 LLVMConstInt(ctx->i32, dfmt[num_channels - 1], 0),
963 LLVMConstInt(ctx->i32, V_008F0C_BUF_NUM_FORMAT_UINT, 0),
964 LLVMConstInt(ctx->i32, voffset != NULL, 0),
965 LLVMConstInt(ctx->i32, 0, 0), /* idxen */
966 LLVMConstInt(ctx->i32, glc, 0),
967 LLVMConstInt(ctx->i32, slc, 0),
968 LLVMConstInt(ctx->i32, 0, 0), /* tfe*/
969 };
970
971 /* The instruction offset field has 12 bits */
972 assert(voffset || inst_offset < (1 << 12));
973
974 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
975 unsigned func = CLAMP(num_channels, 1, 3) - 1;
976 const char *types[] = {"i32", "v2i32", "v4i32"};
977 char name[256];
978 snprintf(name, sizeof(name), "llvm.SI.tbuffer.store.%s", types[func]);
979
980 ac_build_intrinsic(ctx, name, ctx->voidt,
981 args, ARRAY_SIZE(args),
982 AC_FUNC_ATTR_LEGACY);
983 }
984
985 static LLVMValueRef
986 ac_build_buffer_load_common(struct ac_llvm_context *ctx,
987 LLVMValueRef rsrc,
988 LLVMValueRef vindex,
989 LLVMValueRef voffset,
990 unsigned num_channels,
991 bool glc,
992 bool slc,
993 bool can_speculate,
994 bool use_format)
995 {
996 LLVMValueRef args[] = {
997 LLVMBuildBitCast(ctx->builder, rsrc, ctx->v4i32, ""),
998 vindex ? vindex : LLVMConstInt(ctx->i32, 0, 0),
999 voffset,
1000 LLVMConstInt(ctx->i1, glc, 0),
1001 LLVMConstInt(ctx->i1, slc, 0)
1002 };
1003 unsigned func = CLAMP(num_channels, 1, 3) - 1;
1004
1005 LLVMTypeRef types[] = {ctx->f32, ctx->v2f32, ctx->v4f32};
1006 const char *type_names[] = {"f32", "v2f32", "v4f32"};
1007 char name[256];
1008
1009 if (use_format) {
1010 snprintf(name, sizeof(name), "llvm.amdgcn.buffer.load.format.%s",
1011 type_names[func]);
1012 } else {
1013 snprintf(name, sizeof(name), "llvm.amdgcn.buffer.load.%s",
1014 type_names[func]);
1015 }
1016
1017 return ac_build_intrinsic(ctx, name, types[func], args,
1018 ARRAY_SIZE(args),
1019 ac_get_load_intr_attribs(can_speculate));
1020 }
1021
1022 LLVMValueRef
1023 ac_build_buffer_load(struct ac_llvm_context *ctx,
1024 LLVMValueRef rsrc,
1025 int num_channels,
1026 LLVMValueRef vindex,
1027 LLVMValueRef voffset,
1028 LLVMValueRef soffset,
1029 unsigned inst_offset,
1030 unsigned glc,
1031 unsigned slc,
1032 bool can_speculate,
1033 bool allow_smem)
1034 {
1035 LLVMValueRef offset = LLVMConstInt(ctx->i32, inst_offset, 0);
1036 if (voffset)
1037 offset = LLVMBuildAdd(ctx->builder, offset, voffset, "");
1038 if (soffset)
1039 offset = LLVMBuildAdd(ctx->builder, offset, soffset, "");
1040
1041 /* TODO: VI and later generations can use SMEM with GLC=1.*/
1042 if (allow_smem && !glc && !slc) {
1043 assert(vindex == NULL);
1044
1045 LLVMValueRef result[8];
1046
1047 for (int i = 0; i < num_channels; i++) {
1048 if (i) {
1049 offset = LLVMBuildAdd(ctx->builder, offset,
1050 LLVMConstInt(ctx->i32, 4, 0), "");
1051 }
1052 LLVMValueRef args[2] = {rsrc, offset};
1053 result[i] = ac_build_intrinsic(ctx, "llvm.SI.load.const.v4i32",
1054 ctx->f32, args, 2,
1055 AC_FUNC_ATTR_READNONE |
1056 AC_FUNC_ATTR_LEGACY);
1057 }
1058 if (num_channels == 1)
1059 return result[0];
1060
1061 if (num_channels == 3)
1062 result[num_channels++] = LLVMGetUndef(ctx->f32);
1063 return ac_build_gather_values(ctx, result, num_channels);
1064 }
1065
1066 return ac_build_buffer_load_common(ctx, rsrc, vindex, offset,
1067 num_channels, glc, slc,
1068 can_speculate, false);
1069 }
1070
1071 LLVMValueRef ac_build_buffer_load_format(struct ac_llvm_context *ctx,
1072 LLVMValueRef rsrc,
1073 LLVMValueRef vindex,
1074 LLVMValueRef voffset,
1075 unsigned num_channels,
1076 bool glc,
1077 bool can_speculate)
1078 {
1079 return ac_build_buffer_load_common(ctx, rsrc, vindex, voffset,
1080 num_channels, glc, false,
1081 can_speculate, true);
1082 }
1083
1084 LLVMValueRef ac_build_buffer_load_format_gfx9_safe(struct ac_llvm_context *ctx,
1085 LLVMValueRef rsrc,
1086 LLVMValueRef vindex,
1087 LLVMValueRef voffset,
1088 unsigned num_channels,
1089 bool glc,
1090 bool can_speculate)
1091 {
1092 LLVMValueRef elem_count = LLVMBuildExtractElement(ctx->builder, rsrc, LLVMConstInt(ctx->i32, 2, 0), "");
1093 LLVMValueRef stride = LLVMBuildExtractElement(ctx->builder, rsrc, LLVMConstInt(ctx->i32, 1, 0), "");
1094 stride = LLVMBuildLShr(ctx->builder, stride, LLVMConstInt(ctx->i32, 16, 0), "");
1095
1096 LLVMValueRef new_elem_count = LLVMBuildSelect(ctx->builder,
1097 LLVMBuildICmp(ctx->builder, LLVMIntUGT, elem_count, stride, ""),
1098 elem_count, stride, "");
1099
1100 LLVMValueRef new_rsrc = LLVMBuildInsertElement(ctx->builder, rsrc, new_elem_count,
1101 LLVMConstInt(ctx->i32, 2, 0), "");
1102
1103 return ac_build_buffer_load_common(ctx, new_rsrc, vindex, voffset,
1104 num_channels, glc, false,
1105 can_speculate, true);
1106 }
1107
1108 /**
1109 * Set range metadata on an instruction. This can only be used on load and
1110 * call instructions. If you know an instruction can only produce the values
1111 * 0, 1, 2, you would do set_range_metadata(value, 0, 3);
1112 * \p lo is the minimum value inclusive.
1113 * \p hi is the maximum value exclusive.
1114 */
1115 static void set_range_metadata(struct ac_llvm_context *ctx,
1116 LLVMValueRef value, unsigned lo, unsigned hi)
1117 {
1118 LLVMValueRef range_md, md_args[2];
1119 LLVMTypeRef type = LLVMTypeOf(value);
1120 LLVMContextRef context = LLVMGetTypeContext(type);
1121
1122 md_args[0] = LLVMConstInt(type, lo, false);
1123 md_args[1] = LLVMConstInt(type, hi, false);
1124 range_md = LLVMMDNodeInContext(context, md_args, 2);
1125 LLVMSetMetadata(value, ctx->range_md_kind, range_md);
1126 }
1127
1128 LLVMValueRef
1129 ac_get_thread_id(struct ac_llvm_context *ctx)
1130 {
1131 LLVMValueRef tid;
1132
1133 LLVMValueRef tid_args[2];
1134 tid_args[0] = LLVMConstInt(ctx->i32, 0xffffffff, false);
1135 tid_args[1] = LLVMConstInt(ctx->i32, 0, false);
1136 tid_args[1] = ac_build_intrinsic(ctx,
1137 "llvm.amdgcn.mbcnt.lo", ctx->i32,
1138 tid_args, 2, AC_FUNC_ATTR_READNONE);
1139
1140 tid = ac_build_intrinsic(ctx, "llvm.amdgcn.mbcnt.hi",
1141 ctx->i32, tid_args,
1142 2, AC_FUNC_ATTR_READNONE);
1143 set_range_metadata(ctx, tid, 0, 64);
1144 return tid;
1145 }
1146
1147 /*
1148 * SI implements derivatives using the local data store (LDS)
1149 * All writes to the LDS happen in all executing threads at
1150 * the same time. TID is the Thread ID for the current
1151 * thread and is a value between 0 and 63, representing
1152 * the thread's position in the wavefront.
1153 *
1154 * For the pixel shader threads are grouped into quads of four pixels.
1155 * The TIDs of the pixels of a quad are:
1156 *
1157 * +------+------+
1158 * |4n + 0|4n + 1|
1159 * +------+------+
1160 * |4n + 2|4n + 3|
1161 * +------+------+
1162 *
1163 * So, masking the TID with 0xfffffffc yields the TID of the top left pixel
1164 * of the quad, masking with 0xfffffffd yields the TID of the top pixel of
1165 * the current pixel's column, and masking with 0xfffffffe yields the TID
1166 * of the left pixel of the current pixel's row.
1167 *
1168 * Adding 1 yields the TID of the pixel to the right of the left pixel, and
1169 * adding 2 yields the TID of the pixel below the top pixel.
1170 */
1171 LLVMValueRef
1172 ac_build_ddxy(struct ac_llvm_context *ctx,
1173 uint32_t mask,
1174 int idx,
1175 LLVMValueRef val)
1176 {
1177 LLVMValueRef tl, trbl, args[2];
1178 LLVMValueRef result;
1179
1180 if (ctx->chip_class >= VI) {
1181 LLVMValueRef thread_id, tl_tid, trbl_tid;
1182 thread_id = ac_get_thread_id(ctx);
1183
1184 tl_tid = LLVMBuildAnd(ctx->builder, thread_id,
1185 LLVMConstInt(ctx->i32, mask, false), "");
1186
1187 trbl_tid = LLVMBuildAdd(ctx->builder, tl_tid,
1188 LLVMConstInt(ctx->i32, idx, false), "");
1189
1190 args[0] = LLVMBuildMul(ctx->builder, tl_tid,
1191 LLVMConstInt(ctx->i32, 4, false), "");
1192 args[1] = val;
1193 tl = ac_build_intrinsic(ctx,
1194 "llvm.amdgcn.ds.bpermute", ctx->i32,
1195 args, 2,
1196 AC_FUNC_ATTR_READNONE |
1197 AC_FUNC_ATTR_CONVERGENT);
1198
1199 args[0] = LLVMBuildMul(ctx->builder, trbl_tid,
1200 LLVMConstInt(ctx->i32, 4, false), "");
1201 trbl = ac_build_intrinsic(ctx,
1202 "llvm.amdgcn.ds.bpermute", ctx->i32,
1203 args, 2,
1204 AC_FUNC_ATTR_READNONE |
1205 AC_FUNC_ATTR_CONVERGENT);
1206 } else {
1207 uint32_t masks[2] = {};
1208
1209 switch (mask) {
1210 case AC_TID_MASK_TOP_LEFT:
1211 masks[0] = 0x8000;
1212 if (idx == 1)
1213 masks[1] = 0x8055;
1214 else
1215 masks[1] = 0x80aa;
1216
1217 break;
1218 case AC_TID_MASK_TOP:
1219 masks[0] = 0x8044;
1220 masks[1] = 0x80ee;
1221 break;
1222 case AC_TID_MASK_LEFT:
1223 masks[0] = 0x80a0;
1224 masks[1] = 0x80f5;
1225 break;
1226 default:
1227 assert(0);
1228 }
1229
1230 args[0] = val;
1231 args[1] = LLVMConstInt(ctx->i32, masks[0], false);
1232
1233 tl = ac_build_intrinsic(ctx,
1234 "llvm.amdgcn.ds.swizzle", ctx->i32,
1235 args, 2,
1236 AC_FUNC_ATTR_READNONE |
1237 AC_FUNC_ATTR_CONVERGENT);
1238
1239 args[1] = LLVMConstInt(ctx->i32, masks[1], false);
1240 trbl = ac_build_intrinsic(ctx,
1241 "llvm.amdgcn.ds.swizzle", ctx->i32,
1242 args, 2,
1243 AC_FUNC_ATTR_READNONE |
1244 AC_FUNC_ATTR_CONVERGENT);
1245 }
1246
1247 tl = LLVMBuildBitCast(ctx->builder, tl, ctx->f32, "");
1248 trbl = LLVMBuildBitCast(ctx->builder, trbl, ctx->f32, "");
1249 result = LLVMBuildFSub(ctx->builder, trbl, tl, "");
1250 return result;
1251 }
1252
1253 void
1254 ac_build_sendmsg(struct ac_llvm_context *ctx,
1255 uint32_t msg,
1256 LLVMValueRef wave_id)
1257 {
1258 LLVMValueRef args[2];
1259 args[0] = LLVMConstInt(ctx->i32, msg, false);
1260 args[1] = wave_id;
1261 ac_build_intrinsic(ctx, "llvm.amdgcn.s.sendmsg", ctx->voidt, args, 2, 0);
1262 }
1263
1264 LLVMValueRef
1265 ac_build_imsb(struct ac_llvm_context *ctx,
1266 LLVMValueRef arg,
1267 LLVMTypeRef dst_type)
1268 {
1269 LLVMValueRef msb = ac_build_intrinsic(ctx, "llvm.amdgcn.sffbh.i32",
1270 dst_type, &arg, 1,
1271 AC_FUNC_ATTR_READNONE);
1272
1273 /* The HW returns the last bit index from MSB, but NIR/TGSI wants
1274 * the index from LSB. Invert it by doing "31 - msb". */
1275 msb = LLVMBuildSub(ctx->builder, LLVMConstInt(ctx->i32, 31, false),
1276 msb, "");
1277
1278 LLVMValueRef all_ones = LLVMConstInt(ctx->i32, -1, true);
1279 LLVMValueRef cond = LLVMBuildOr(ctx->builder,
1280 LLVMBuildICmp(ctx->builder, LLVMIntEQ,
1281 arg, LLVMConstInt(ctx->i32, 0, 0), ""),
1282 LLVMBuildICmp(ctx->builder, LLVMIntEQ,
1283 arg, all_ones, ""), "");
1284
1285 return LLVMBuildSelect(ctx->builder, cond, all_ones, msb, "");
1286 }
1287
1288 LLVMValueRef
1289 ac_build_umsb(struct ac_llvm_context *ctx,
1290 LLVMValueRef arg,
1291 LLVMTypeRef dst_type)
1292 {
1293 const char *intrin_name;
1294 LLVMTypeRef type;
1295 LLVMValueRef highest_bit;
1296 LLVMValueRef zero;
1297
1298 if (ac_get_elem_bits(ctx, LLVMTypeOf(arg)) == 64) {
1299 intrin_name = "llvm.ctlz.i64";
1300 type = ctx->i64;
1301 highest_bit = LLVMConstInt(ctx->i64, 63, false);
1302 zero = ctx->i64_0;
1303 } else {
1304 intrin_name = "llvm.ctlz.i32";
1305 type = ctx->i32;
1306 highest_bit = LLVMConstInt(ctx->i32, 31, false);
1307 zero = ctx->i32_0;
1308 }
1309
1310 LLVMValueRef params[2] = {
1311 arg,
1312 ctx->i1true,
1313 };
1314
1315 LLVMValueRef msb = ac_build_intrinsic(ctx, intrin_name, type,
1316 params, 2,
1317 AC_FUNC_ATTR_READNONE);
1318
1319 /* The HW returns the last bit index from MSB, but TGSI/NIR wants
1320 * the index from LSB. Invert it by doing "31 - msb". */
1321 msb = LLVMBuildSub(ctx->builder, highest_bit, msb, "");
1322 msb = LLVMBuildTruncOrBitCast(ctx->builder, msb, ctx->i32, "");
1323
1324 /* check for zero */
1325 return LLVMBuildSelect(ctx->builder,
1326 LLVMBuildICmp(ctx->builder, LLVMIntEQ, arg, zero, ""),
1327 LLVMConstInt(ctx->i32, -1, true), msb, "");
1328 }
1329
1330 LLVMValueRef ac_build_fmin(struct ac_llvm_context *ctx, LLVMValueRef a,
1331 LLVMValueRef b)
1332 {
1333 LLVMValueRef args[2] = {a, b};
1334 return ac_build_intrinsic(ctx, "llvm.minnum.f32", ctx->f32, args, 2,
1335 AC_FUNC_ATTR_READNONE);
1336 }
1337
1338 LLVMValueRef ac_build_fmax(struct ac_llvm_context *ctx, LLVMValueRef a,
1339 LLVMValueRef b)
1340 {
1341 LLVMValueRef args[2] = {a, b};
1342 return ac_build_intrinsic(ctx, "llvm.maxnum.f32", ctx->f32, args, 2,
1343 AC_FUNC_ATTR_READNONE);
1344 }
1345
1346 LLVMValueRef ac_build_imin(struct ac_llvm_context *ctx, LLVMValueRef a,
1347 LLVMValueRef b)
1348 {
1349 LLVMValueRef cmp = LLVMBuildICmp(ctx->builder, LLVMIntSLE, a, b, "");
1350 return LLVMBuildSelect(ctx->builder, cmp, a, b, "");
1351 }
1352
1353 LLVMValueRef ac_build_imax(struct ac_llvm_context *ctx, LLVMValueRef a,
1354 LLVMValueRef b)
1355 {
1356 LLVMValueRef cmp = LLVMBuildICmp(ctx->builder, LLVMIntSGT, a, b, "");
1357 return LLVMBuildSelect(ctx->builder, cmp, a, b, "");
1358 }
1359
1360 LLVMValueRef ac_build_umin(struct ac_llvm_context *ctx, LLVMValueRef a,
1361 LLVMValueRef b)
1362 {
1363 LLVMValueRef cmp = LLVMBuildICmp(ctx->builder, LLVMIntULE, a, b, "");
1364 return LLVMBuildSelect(ctx->builder, cmp, a, b, "");
1365 }
1366
1367 LLVMValueRef ac_build_clamp(struct ac_llvm_context *ctx, LLVMValueRef value)
1368 {
1369 if (HAVE_LLVM >= 0x0500) {
1370 return ac_build_fmin(ctx, ac_build_fmax(ctx, value, ctx->f32_0),
1371 ctx->f32_1);
1372 }
1373
1374 LLVMValueRef args[3] = {
1375 value,
1376 LLVMConstReal(ctx->f32, 0),
1377 LLVMConstReal(ctx->f32, 1),
1378 };
1379
1380 return ac_build_intrinsic(ctx, "llvm.AMDGPU.clamp.", ctx->f32, args, 3,
1381 AC_FUNC_ATTR_READNONE |
1382 AC_FUNC_ATTR_LEGACY);
1383 }
1384
1385 void ac_build_export(struct ac_llvm_context *ctx, struct ac_export_args *a)
1386 {
1387 LLVMValueRef args[9];
1388
1389 if (HAVE_LLVM >= 0x0500) {
1390 args[0] = LLVMConstInt(ctx->i32, a->target, 0);
1391 args[1] = LLVMConstInt(ctx->i32, a->enabled_channels, 0);
1392
1393 if (a->compr) {
1394 LLVMTypeRef i16 = LLVMInt16TypeInContext(ctx->context);
1395 LLVMTypeRef v2i16 = LLVMVectorType(i16, 2);
1396
1397 args[2] = LLVMBuildBitCast(ctx->builder, a->out[0],
1398 v2i16, "");
1399 args[3] = LLVMBuildBitCast(ctx->builder, a->out[1],
1400 v2i16, "");
1401 args[4] = LLVMConstInt(ctx->i1, a->done, 0);
1402 args[5] = LLVMConstInt(ctx->i1, a->valid_mask, 0);
1403
1404 ac_build_intrinsic(ctx, "llvm.amdgcn.exp.compr.v2i16",
1405 ctx->voidt, args, 6, 0);
1406 } else {
1407 args[2] = a->out[0];
1408 args[3] = a->out[1];
1409 args[4] = a->out[2];
1410 args[5] = a->out[3];
1411 args[6] = LLVMConstInt(ctx->i1, a->done, 0);
1412 args[7] = LLVMConstInt(ctx->i1, a->valid_mask, 0);
1413
1414 ac_build_intrinsic(ctx, "llvm.amdgcn.exp.f32",
1415 ctx->voidt, args, 8, 0);
1416 }
1417 return;
1418 }
1419
1420 args[0] = LLVMConstInt(ctx->i32, a->enabled_channels, 0);
1421 args[1] = LLVMConstInt(ctx->i32, a->valid_mask, 0);
1422 args[2] = LLVMConstInt(ctx->i32, a->done, 0);
1423 args[3] = LLVMConstInt(ctx->i32, a->target, 0);
1424 args[4] = LLVMConstInt(ctx->i32, a->compr, 0);
1425 memcpy(args + 5, a->out, sizeof(a->out[0]) * 4);
1426
1427 ac_build_intrinsic(ctx, "llvm.SI.export", ctx->voidt, args, 9,
1428 AC_FUNC_ATTR_LEGACY);
1429 }
1430
1431 void ac_build_export_null(struct ac_llvm_context *ctx)
1432 {
1433 struct ac_export_args args;
1434
1435 args.enabled_channels = 0x0; /* enabled channels */
1436 args.valid_mask = 1; /* whether the EXEC mask is valid */
1437 args.done = 1; /* DONE bit */
1438 args.target = V_008DFC_SQ_EXP_NULL;
1439 args.compr = 0; /* COMPR flag (0 = 32-bit export) */
1440 args.out[0] = LLVMGetUndef(ctx->f32); /* R */
1441 args.out[1] = LLVMGetUndef(ctx->f32); /* G */
1442 args.out[2] = LLVMGetUndef(ctx->f32); /* B */
1443 args.out[3] = LLVMGetUndef(ctx->f32); /* A */
1444
1445 ac_build_export(ctx, &args);
1446 }
1447
1448 LLVMValueRef ac_build_image_opcode(struct ac_llvm_context *ctx,
1449 struct ac_image_args *a)
1450 {
1451 LLVMValueRef args[11];
1452 unsigned num_args = 0;
1453 const char *name = NULL;
1454 char intr_name[128], type[64];
1455
1456 bool sample = a->opcode == ac_image_sample ||
1457 a->opcode == ac_image_gather4 ||
1458 a->opcode == ac_image_get_lod;
1459 bool da = a->dim == ac_image_cube ||
1460 a->dim == ac_image_1darray ||
1461 a->dim == ac_image_2darray ||
1462 a->dim == ac_image_2darraymsaa;
1463 if (a->opcode == ac_image_get_lod)
1464 da = false;
1465
1466 if (sample)
1467 args[num_args++] = ac_to_float(ctx, a->addr);
1468 else
1469 args[num_args++] = a->addr;
1470
1471 args[num_args++] = a->resource;
1472 if (sample)
1473 args[num_args++] = a->sampler;
1474 args[num_args++] = LLVMConstInt(ctx->i32, a->dmask, 0);
1475 if (sample)
1476 args[num_args++] = LLVMConstInt(ctx->i1, a->unorm, 0);
1477 args[num_args++] = ctx->i1false; /* glc */
1478 args[num_args++] = ctx->i1false; /* slc */
1479 args[num_args++] = ctx->i1false; /* lwe */
1480 args[num_args++] = LLVMConstInt(ctx->i1, da, 0);
1481
1482 switch (a->opcode) {
1483 case ac_image_sample:
1484 name = "llvm.amdgcn.image.sample";
1485 break;
1486 case ac_image_gather4:
1487 name = "llvm.amdgcn.image.gather4";
1488 break;
1489 case ac_image_load:
1490 name = "llvm.amdgcn.image.load";
1491 break;
1492 case ac_image_load_mip:
1493 name = "llvm.amdgcn.image.load.mip";
1494 break;
1495 case ac_image_get_lod:
1496 name = "llvm.amdgcn.image.getlod";
1497 break;
1498 case ac_image_get_resinfo:
1499 name = "llvm.amdgcn.image.getresinfo";
1500 break;
1501 default:
1502 unreachable("invalid image opcode");
1503 }
1504
1505 ac_build_type_name_for_intr(LLVMTypeOf(args[0]), type,
1506 sizeof(type));
1507
1508 snprintf(intr_name, sizeof(intr_name), "%s%s%s%s.v4f32.%s.v8i32",
1509 name,
1510 a->compare ? ".c" : "",
1511 a->bias ? ".b" :
1512 a->lod ? ".l" :
1513 a->deriv ? ".d" :
1514 a->level_zero ? ".lz" : "",
1515 a->offset ? ".o" : "",
1516 type);
1517
1518 LLVMValueRef result =
1519 ac_build_intrinsic(ctx, intr_name,
1520 ctx->v4f32, args, num_args,
1521 AC_FUNC_ATTR_READNONE);
1522 if (!sample) {
1523 result = LLVMBuildBitCast(ctx->builder, result,
1524 ctx->v4i32, "");
1525 }
1526 return result;
1527 }
1528
1529 LLVMValueRef ac_build_cvt_pkrtz_f16(struct ac_llvm_context *ctx,
1530 LLVMValueRef args[2])
1531 {
1532 if (HAVE_LLVM >= 0x0500) {
1533 LLVMTypeRef v2f16 =
1534 LLVMVectorType(LLVMHalfTypeInContext(ctx->context), 2);
1535 LLVMValueRef res =
1536 ac_build_intrinsic(ctx, "llvm.amdgcn.cvt.pkrtz",
1537 v2f16, args, 2,
1538 AC_FUNC_ATTR_READNONE);
1539 return LLVMBuildBitCast(ctx->builder, res, ctx->i32, "");
1540 }
1541
1542 return ac_build_intrinsic(ctx, "llvm.SI.packf16", ctx->i32, args, 2,
1543 AC_FUNC_ATTR_READNONE |
1544 AC_FUNC_ATTR_LEGACY);
1545 }
1546
1547 /* Upper 16 bits must be zero. */
1548 static LLVMValueRef ac_llvm_pack_two_int16(struct ac_llvm_context *ctx,
1549 LLVMValueRef val[2])
1550 {
1551 return LLVMBuildOr(ctx->builder, val[0],
1552 LLVMBuildShl(ctx->builder, val[1],
1553 LLVMConstInt(ctx->i32, 16, 0),
1554 ""), "");
1555 }
1556
1557 /* Upper 16 bits are ignored and will be dropped. */
1558 static LLVMValueRef ac_llvm_pack_two_int32_as_int16(struct ac_llvm_context *ctx,
1559 LLVMValueRef val[2])
1560 {
1561 LLVMValueRef v[2] = {
1562 LLVMBuildAnd(ctx->builder, val[0],
1563 LLVMConstInt(ctx->i32, 0xffff, 0), ""),
1564 val[1],
1565 };
1566 return ac_llvm_pack_two_int16(ctx, v);
1567 }
1568
1569 LLVMValueRef ac_build_cvt_pknorm_i16(struct ac_llvm_context *ctx,
1570 LLVMValueRef args[2])
1571 {
1572 if (HAVE_LLVM >= 0x0600) {
1573 LLVMValueRef res =
1574 ac_build_intrinsic(ctx, "llvm.amdgcn.cvt.pknorm.i16",
1575 ctx->v2i16, args, 2,
1576 AC_FUNC_ATTR_READNONE);
1577 return LLVMBuildBitCast(ctx->builder, res, ctx->i32, "");
1578 }
1579
1580 LLVMValueRef val[2];
1581
1582 for (int chan = 0; chan < 2; chan++) {
1583 /* Clamp between [-1, 1]. */
1584 val[chan] = ac_build_fmin(ctx, args[chan], ctx->f32_1);
1585 val[chan] = ac_build_fmax(ctx, val[chan], LLVMConstReal(ctx->f32, -1));
1586 /* Convert to a signed integer in [-32767, 32767]. */
1587 val[chan] = LLVMBuildFMul(ctx->builder, val[chan],
1588 LLVMConstReal(ctx->f32, 32767), "");
1589 /* If positive, add 0.5, else add -0.5. */
1590 val[chan] = LLVMBuildFAdd(ctx->builder, val[chan],
1591 LLVMBuildSelect(ctx->builder,
1592 LLVMBuildFCmp(ctx->builder, LLVMRealOGE,
1593 val[chan], ctx->f32_0, ""),
1594 LLVMConstReal(ctx->f32, 0.5),
1595 LLVMConstReal(ctx->f32, -0.5), ""), "");
1596 val[chan] = LLVMBuildFPToSI(ctx->builder, val[chan], ctx->i32, "");
1597 }
1598 return ac_llvm_pack_two_int32_as_int16(ctx, val);
1599 }
1600
1601 LLVMValueRef ac_build_cvt_pknorm_u16(struct ac_llvm_context *ctx,
1602 LLVMValueRef args[2])
1603 {
1604 if (HAVE_LLVM >= 0x0600) {
1605 LLVMValueRef res =
1606 ac_build_intrinsic(ctx, "llvm.amdgcn.cvt.pknorm.u16",
1607 ctx->v2i16, args, 2,
1608 AC_FUNC_ATTR_READNONE);
1609 return LLVMBuildBitCast(ctx->builder, res, ctx->i32, "");
1610 }
1611
1612 LLVMValueRef val[2];
1613
1614 for (int chan = 0; chan < 2; chan++) {
1615 val[chan] = ac_build_clamp(ctx, args[chan]);
1616 val[chan] = LLVMBuildFMul(ctx->builder, val[chan],
1617 LLVMConstReal(ctx->f32, 65535), "");
1618 val[chan] = LLVMBuildFAdd(ctx->builder, val[chan],
1619 LLVMConstReal(ctx->f32, 0.5), "");
1620 val[chan] = LLVMBuildFPToUI(ctx->builder, val[chan],
1621 ctx->i32, "");
1622 }
1623 return ac_llvm_pack_two_int32_as_int16(ctx, val);
1624 }
1625
1626 /* The 8-bit and 10-bit clamping is for HW workarounds. */
1627 LLVMValueRef ac_build_cvt_pk_i16(struct ac_llvm_context *ctx,
1628 LLVMValueRef args[2], unsigned bits, bool hi)
1629 {
1630 assert(bits == 8 || bits == 10 || bits == 16);
1631
1632 LLVMValueRef max_rgb = LLVMConstInt(ctx->i32,
1633 bits == 8 ? 127 : bits == 10 ? 511 : 32767, 0);
1634 LLVMValueRef min_rgb = LLVMConstInt(ctx->i32,
1635 bits == 8 ? -128 : bits == 10 ? -512 : -32768, 0);
1636 LLVMValueRef max_alpha =
1637 bits != 10 ? max_rgb : ctx->i32_1;
1638 LLVMValueRef min_alpha =
1639 bits != 10 ? min_rgb : LLVMConstInt(ctx->i32, -2, 0);
1640 bool has_intrinsic = HAVE_LLVM >= 0x0600;
1641
1642 /* Clamp. */
1643 if (!has_intrinsic || bits != 16) {
1644 for (int i = 0; i < 2; i++) {
1645 bool alpha = hi && i == 1;
1646 args[i] = ac_build_imin(ctx, args[i],
1647 alpha ? max_alpha : max_rgb);
1648 args[i] = ac_build_imax(ctx, args[i],
1649 alpha ? min_alpha : min_rgb);
1650 }
1651 }
1652
1653 if (has_intrinsic) {
1654 LLVMValueRef res =
1655 ac_build_intrinsic(ctx, "llvm.amdgcn.cvt.pk.i16",
1656 ctx->v2i16, args, 2,
1657 AC_FUNC_ATTR_READNONE);
1658 return LLVMBuildBitCast(ctx->builder, res, ctx->i32, "");
1659 }
1660
1661 return ac_llvm_pack_two_int32_as_int16(ctx, args);
1662 }
1663
1664 /* The 8-bit and 10-bit clamping is for HW workarounds. */
1665 LLVMValueRef ac_build_cvt_pk_u16(struct ac_llvm_context *ctx,
1666 LLVMValueRef args[2], unsigned bits, bool hi)
1667 {
1668 assert(bits == 8 || bits == 10 || bits == 16);
1669
1670 LLVMValueRef max_rgb = LLVMConstInt(ctx->i32,
1671 bits == 8 ? 255 : bits == 10 ? 1023 : 65535, 0);
1672 LLVMValueRef max_alpha =
1673 bits != 10 ? max_rgb : LLVMConstInt(ctx->i32, 3, 0);
1674 bool has_intrinsic = HAVE_LLVM >= 0x0600;
1675
1676 /* Clamp. */
1677 if (!has_intrinsic || bits != 16) {
1678 for (int i = 0; i < 2; i++) {
1679 bool alpha = hi && i == 1;
1680 args[i] = ac_build_umin(ctx, args[i],
1681 alpha ? max_alpha : max_rgb);
1682 }
1683 }
1684
1685 if (has_intrinsic) {
1686 LLVMValueRef res =
1687 ac_build_intrinsic(ctx, "llvm.amdgcn.cvt.pk.u16",
1688 ctx->v2i16, args, 2,
1689 AC_FUNC_ATTR_READNONE);
1690 return LLVMBuildBitCast(ctx->builder, res, ctx->i32, "");
1691 }
1692
1693 return ac_llvm_pack_two_int16(ctx, args);
1694 }
1695
1696 LLVMValueRef ac_build_wqm_vote(struct ac_llvm_context *ctx, LLVMValueRef i1)
1697 {
1698 assert(HAVE_LLVM >= 0x0600);
1699 return ac_build_intrinsic(ctx, "llvm.amdgcn.wqm.vote", ctx->i1,
1700 &i1, 1, AC_FUNC_ATTR_READNONE);
1701 }
1702
1703 void ac_build_kill_if_false(struct ac_llvm_context *ctx, LLVMValueRef i1)
1704 {
1705 if (HAVE_LLVM >= 0x0600) {
1706 ac_build_intrinsic(ctx, "llvm.amdgcn.kill", ctx->voidt,
1707 &i1, 1, 0);
1708 return;
1709 }
1710
1711 LLVMValueRef value = LLVMBuildSelect(ctx->builder, i1,
1712 LLVMConstReal(ctx->f32, 1),
1713 LLVMConstReal(ctx->f32, -1), "");
1714 ac_build_intrinsic(ctx, "llvm.AMDGPU.kill", ctx->voidt,
1715 &value, 1, AC_FUNC_ATTR_LEGACY);
1716 }
1717
1718 LLVMValueRef ac_build_bfe(struct ac_llvm_context *ctx, LLVMValueRef input,
1719 LLVMValueRef offset, LLVMValueRef width,
1720 bool is_signed)
1721 {
1722 LLVMValueRef args[] = {
1723 input,
1724 offset,
1725 width,
1726 };
1727
1728 if (HAVE_LLVM >= 0x0500) {
1729 return ac_build_intrinsic(ctx,
1730 is_signed ? "llvm.amdgcn.sbfe.i32" :
1731 "llvm.amdgcn.ubfe.i32",
1732 ctx->i32, args, 3,
1733 AC_FUNC_ATTR_READNONE);
1734 }
1735
1736 return ac_build_intrinsic(ctx,
1737 is_signed ? "llvm.AMDGPU.bfe.i32" :
1738 "llvm.AMDGPU.bfe.u32",
1739 ctx->i32, args, 3,
1740 AC_FUNC_ATTR_READNONE |
1741 AC_FUNC_ATTR_LEGACY);
1742 }
1743
1744 void ac_build_waitcnt(struct ac_llvm_context *ctx, unsigned simm16)
1745 {
1746 LLVMValueRef args[1] = {
1747 LLVMConstInt(ctx->i32, simm16, false),
1748 };
1749 ac_build_intrinsic(ctx, "llvm.amdgcn.s.waitcnt",
1750 ctx->voidt, args, 1, 0);
1751 }
1752
1753 LLVMValueRef ac_build_fract(struct ac_llvm_context *ctx, LLVMValueRef src0,
1754 unsigned bitsize)
1755 {
1756 LLVMTypeRef type;
1757 char *intr;
1758
1759 if (bitsize == 32) {
1760 intr = "llvm.floor.f32";
1761 type = ctx->f32;
1762 } else {
1763 intr = "llvm.floor.f64";
1764 type = ctx->f64;
1765 }
1766
1767 LLVMValueRef params[] = {
1768 src0,
1769 };
1770 LLVMValueRef floor = ac_build_intrinsic(ctx, intr, type, params, 1,
1771 AC_FUNC_ATTR_READNONE);
1772 return LLVMBuildFSub(ctx->builder, src0, floor, "");
1773 }
1774
1775 LLVMValueRef ac_build_isign(struct ac_llvm_context *ctx, LLVMValueRef src0,
1776 unsigned bitsize)
1777 {
1778 LLVMValueRef cmp, val, zero, one;
1779 LLVMTypeRef type;
1780
1781 if (bitsize == 32) {
1782 type = ctx->i32;
1783 zero = ctx->i32_0;
1784 one = ctx->i32_1;
1785 } else {
1786 type = ctx->i64;
1787 zero = ctx->i64_0;
1788 one = ctx->i64_1;
1789 }
1790
1791 cmp = LLVMBuildICmp(ctx->builder, LLVMIntSGT, src0, zero, "");
1792 val = LLVMBuildSelect(ctx->builder, cmp, one, src0, "");
1793 cmp = LLVMBuildICmp(ctx->builder, LLVMIntSGE, val, zero, "");
1794 val = LLVMBuildSelect(ctx->builder, cmp, val, LLVMConstInt(type, -1, true), "");
1795 return val;
1796 }
1797
1798 LLVMValueRef ac_build_fsign(struct ac_llvm_context *ctx, LLVMValueRef src0,
1799 unsigned bitsize)
1800 {
1801 LLVMValueRef cmp, val, zero, one;
1802 LLVMTypeRef type;
1803
1804 if (bitsize == 32) {
1805 type = ctx->f32;
1806 zero = ctx->f32_0;
1807 one = ctx->f32_1;
1808 } else {
1809 type = ctx->f64;
1810 zero = ctx->f64_0;
1811 one = ctx->f64_1;
1812 }
1813
1814 cmp = LLVMBuildFCmp(ctx->builder, LLVMRealOGT, src0, zero, "");
1815 val = LLVMBuildSelect(ctx->builder, cmp, one, src0, "");
1816 cmp = LLVMBuildFCmp(ctx->builder, LLVMRealOGE, val, zero, "");
1817 val = LLVMBuildSelect(ctx->builder, cmp, val, LLVMConstReal(type, -1.0), "");
1818 return val;
1819 }
1820
1821 void ac_get_image_intr_name(const char *base_name,
1822 LLVMTypeRef data_type,
1823 LLVMTypeRef coords_type,
1824 LLVMTypeRef rsrc_type,
1825 char *out_name, unsigned out_len)
1826 {
1827 char coords_type_name[8];
1828
1829 ac_build_type_name_for_intr(coords_type, coords_type_name,
1830 sizeof(coords_type_name));
1831
1832 char data_type_name[8];
1833 char rsrc_type_name[8];
1834
1835 ac_build_type_name_for_intr(data_type, data_type_name,
1836 sizeof(data_type_name));
1837 ac_build_type_name_for_intr(rsrc_type, rsrc_type_name,
1838 sizeof(rsrc_type_name));
1839 snprintf(out_name, out_len, "%s.%s.%s.%s", base_name,
1840 data_type_name, coords_type_name, rsrc_type_name);
1841 }
1842
1843 #define AC_EXP_TARGET (HAVE_LLVM >= 0x0500 ? 0 : 3)
1844 #define AC_EXP_ENABLED_CHANNELS (HAVE_LLVM >= 0x0500 ? 1 : 0)
1845 #define AC_EXP_OUT0 (HAVE_LLVM >= 0x0500 ? 2 : 5)
1846
1847 enum ac_ir_type {
1848 AC_IR_UNDEF,
1849 AC_IR_CONST,
1850 AC_IR_VALUE,
1851 };
1852
1853 struct ac_vs_exp_chan
1854 {
1855 LLVMValueRef value;
1856 float const_float;
1857 enum ac_ir_type type;
1858 };
1859
1860 struct ac_vs_exp_inst {
1861 unsigned offset;
1862 LLVMValueRef inst;
1863 struct ac_vs_exp_chan chan[4];
1864 };
1865
1866 struct ac_vs_exports {
1867 unsigned num;
1868 struct ac_vs_exp_inst exp[VARYING_SLOT_MAX];
1869 };
1870
1871 /* Return true if the PARAM export has been eliminated. */
1872 static bool ac_eliminate_const_output(uint8_t *vs_output_param_offset,
1873 uint32_t num_outputs,
1874 struct ac_vs_exp_inst *exp)
1875 {
1876 unsigned i, default_val; /* SPI_PS_INPUT_CNTL_i.DEFAULT_VAL */
1877 bool is_zero[4] = {}, is_one[4] = {};
1878
1879 for (i = 0; i < 4; i++) {
1880 /* It's a constant expression. Undef outputs are eliminated too. */
1881 if (exp->chan[i].type == AC_IR_UNDEF) {
1882 is_zero[i] = true;
1883 is_one[i] = true;
1884 } else if (exp->chan[i].type == AC_IR_CONST) {
1885 if (exp->chan[i].const_float == 0)
1886 is_zero[i] = true;
1887 else if (exp->chan[i].const_float == 1)
1888 is_one[i] = true;
1889 else
1890 return false; /* other constant */
1891 } else
1892 return false;
1893 }
1894
1895 /* Only certain combinations of 0 and 1 can be eliminated. */
1896 if (is_zero[0] && is_zero[1] && is_zero[2])
1897 default_val = is_zero[3] ? 0 : 1;
1898 else if (is_one[0] && is_one[1] && is_one[2])
1899 default_val = is_zero[3] ? 2 : 3;
1900 else
1901 return false;
1902
1903 /* The PARAM export can be represented as DEFAULT_VAL. Kill it. */
1904 LLVMInstructionEraseFromParent(exp->inst);
1905
1906 /* Change OFFSET to DEFAULT_VAL. */
1907 for (i = 0; i < num_outputs; i++) {
1908 if (vs_output_param_offset[i] == exp->offset) {
1909 vs_output_param_offset[i] =
1910 AC_EXP_PARAM_DEFAULT_VAL_0000 + default_val;
1911 break;
1912 }
1913 }
1914 return true;
1915 }
1916
1917 static bool ac_eliminate_duplicated_output(struct ac_llvm_context *ctx,
1918 uint8_t *vs_output_param_offset,
1919 uint32_t num_outputs,
1920 struct ac_vs_exports *processed,
1921 struct ac_vs_exp_inst *exp)
1922 {
1923 unsigned p, copy_back_channels = 0;
1924
1925 /* See if the output is already in the list of processed outputs.
1926 * The LLVMValueRef comparison relies on SSA.
1927 */
1928 for (p = 0; p < processed->num; p++) {
1929 bool different = false;
1930
1931 for (unsigned j = 0; j < 4; j++) {
1932 struct ac_vs_exp_chan *c1 = &processed->exp[p].chan[j];
1933 struct ac_vs_exp_chan *c2 = &exp->chan[j];
1934
1935 /* Treat undef as a match. */
1936 if (c2->type == AC_IR_UNDEF)
1937 continue;
1938
1939 /* If c1 is undef but c2 isn't, we can copy c2 to c1
1940 * and consider the instruction duplicated.
1941 */
1942 if (c1->type == AC_IR_UNDEF) {
1943 copy_back_channels |= 1 << j;
1944 continue;
1945 }
1946
1947 /* Test whether the channels are not equal. */
1948 if (c1->type != c2->type ||
1949 (c1->type == AC_IR_CONST &&
1950 c1->const_float != c2->const_float) ||
1951 (c1->type == AC_IR_VALUE &&
1952 c1->value != c2->value)) {
1953 different = true;
1954 break;
1955 }
1956 }
1957 if (!different)
1958 break;
1959
1960 copy_back_channels = 0;
1961 }
1962 if (p == processed->num)
1963 return false;
1964
1965 /* If a match was found, but the matching export has undef where the new
1966 * one has a normal value, copy the normal value to the undef channel.
1967 */
1968 struct ac_vs_exp_inst *match = &processed->exp[p];
1969
1970 /* Get current enabled channels mask. */
1971 LLVMValueRef arg = LLVMGetOperand(match->inst, AC_EXP_ENABLED_CHANNELS);
1972 unsigned enabled_channels = LLVMConstIntGetZExtValue(arg);
1973
1974 while (copy_back_channels) {
1975 unsigned chan = u_bit_scan(&copy_back_channels);
1976
1977 assert(match->chan[chan].type == AC_IR_UNDEF);
1978 LLVMSetOperand(match->inst, AC_EXP_OUT0 + chan,
1979 exp->chan[chan].value);
1980 match->chan[chan] = exp->chan[chan];
1981
1982 /* Update number of enabled channels because the original mask
1983 * is not always 0xf.
1984 */
1985 enabled_channels |= (1 << chan);
1986 LLVMSetOperand(match->inst, AC_EXP_ENABLED_CHANNELS,
1987 LLVMConstInt(ctx->i32, enabled_channels, 0));
1988 }
1989
1990 /* The PARAM export is duplicated. Kill it. */
1991 LLVMInstructionEraseFromParent(exp->inst);
1992
1993 /* Change OFFSET to the matching export. */
1994 for (unsigned i = 0; i < num_outputs; i++) {
1995 if (vs_output_param_offset[i] == exp->offset) {
1996 vs_output_param_offset[i] = match->offset;
1997 break;
1998 }
1999 }
2000 return true;
2001 }
2002
2003 void ac_optimize_vs_outputs(struct ac_llvm_context *ctx,
2004 LLVMValueRef main_fn,
2005 uint8_t *vs_output_param_offset,
2006 uint32_t num_outputs,
2007 uint8_t *num_param_exports)
2008 {
2009 LLVMBasicBlockRef bb;
2010 bool removed_any = false;
2011 struct ac_vs_exports exports;
2012
2013 exports.num = 0;
2014
2015 /* Process all LLVM instructions. */
2016 bb = LLVMGetFirstBasicBlock(main_fn);
2017 while (bb) {
2018 LLVMValueRef inst = LLVMGetFirstInstruction(bb);
2019
2020 while (inst) {
2021 LLVMValueRef cur = inst;
2022 inst = LLVMGetNextInstruction(inst);
2023 struct ac_vs_exp_inst exp;
2024
2025 if (LLVMGetInstructionOpcode(cur) != LLVMCall)
2026 continue;
2027
2028 LLVMValueRef callee = ac_llvm_get_called_value(cur);
2029
2030 if (!ac_llvm_is_function(callee))
2031 continue;
2032
2033 const char *name = LLVMGetValueName(callee);
2034 unsigned num_args = LLVMCountParams(callee);
2035
2036 /* Check if this is an export instruction. */
2037 if ((num_args != 9 && num_args != 8) ||
2038 (strcmp(name, "llvm.SI.export") &&
2039 strcmp(name, "llvm.amdgcn.exp.f32")))
2040 continue;
2041
2042 LLVMValueRef arg = LLVMGetOperand(cur, AC_EXP_TARGET);
2043 unsigned target = LLVMConstIntGetZExtValue(arg);
2044
2045 if (target < V_008DFC_SQ_EXP_PARAM)
2046 continue;
2047
2048 target -= V_008DFC_SQ_EXP_PARAM;
2049
2050 /* Parse the instruction. */
2051 memset(&exp, 0, sizeof(exp));
2052 exp.offset = target;
2053 exp.inst = cur;
2054
2055 for (unsigned i = 0; i < 4; i++) {
2056 LLVMValueRef v = LLVMGetOperand(cur, AC_EXP_OUT0 + i);
2057
2058 exp.chan[i].value = v;
2059
2060 if (LLVMIsUndef(v)) {
2061 exp.chan[i].type = AC_IR_UNDEF;
2062 } else if (LLVMIsAConstantFP(v)) {
2063 LLVMBool loses_info;
2064 exp.chan[i].type = AC_IR_CONST;
2065 exp.chan[i].const_float =
2066 LLVMConstRealGetDouble(v, &loses_info);
2067 } else {
2068 exp.chan[i].type = AC_IR_VALUE;
2069 }
2070 }
2071
2072 /* Eliminate constant and duplicated PARAM exports. */
2073 if (ac_eliminate_const_output(vs_output_param_offset,
2074 num_outputs, &exp) ||
2075 ac_eliminate_duplicated_output(ctx,
2076 vs_output_param_offset,
2077 num_outputs, &exports,
2078 &exp)) {
2079 removed_any = true;
2080 } else {
2081 exports.exp[exports.num++] = exp;
2082 }
2083 }
2084 bb = LLVMGetNextBasicBlock(bb);
2085 }
2086
2087 /* Remove holes in export memory due to removed PARAM exports.
2088 * This is done by renumbering all PARAM exports.
2089 */
2090 if (removed_any) {
2091 uint8_t old_offset[VARYING_SLOT_MAX];
2092 unsigned out, i;
2093
2094 /* Make a copy of the offsets. We need the old version while
2095 * we are modifying some of them. */
2096 memcpy(old_offset, vs_output_param_offset,
2097 sizeof(old_offset));
2098
2099 for (i = 0; i < exports.num; i++) {
2100 unsigned offset = exports.exp[i].offset;
2101
2102 /* Update vs_output_param_offset. Multiple outputs can
2103 * have the same offset.
2104 */
2105 for (out = 0; out < num_outputs; out++) {
2106 if (old_offset[out] == offset)
2107 vs_output_param_offset[out] = i;
2108 }
2109
2110 /* Change the PARAM offset in the instruction. */
2111 LLVMSetOperand(exports.exp[i].inst, AC_EXP_TARGET,
2112 LLVMConstInt(ctx->i32,
2113 V_008DFC_SQ_EXP_PARAM + i, 0));
2114 }
2115 *num_param_exports = exports.num;
2116 }
2117 }
2118
2119 void ac_init_exec_full_mask(struct ac_llvm_context *ctx)
2120 {
2121 LLVMValueRef full_mask = LLVMConstInt(ctx->i64, ~0ull, 0);
2122 ac_build_intrinsic(ctx,
2123 "llvm.amdgcn.init.exec", ctx->voidt,
2124 &full_mask, 1, AC_FUNC_ATTR_CONVERGENT);
2125 }
2126
2127 void ac_declare_lds_as_pointer(struct ac_llvm_context *ctx)
2128 {
2129 unsigned lds_size = ctx->chip_class >= CIK ? 65536 : 32768;
2130 ctx->lds = LLVMBuildIntToPtr(ctx->builder, ctx->i32_0,
2131 LLVMPointerType(LLVMArrayType(ctx->i32, lds_size / 4), AC_LOCAL_ADDR_SPACE),
2132 "lds");
2133 }
2134
2135 LLVMValueRef ac_lds_load(struct ac_llvm_context *ctx,
2136 LLVMValueRef dw_addr)
2137 {
2138 return ac_build_load(ctx, ctx->lds, dw_addr);
2139 }
2140
2141 void ac_lds_store(struct ac_llvm_context *ctx,
2142 LLVMValueRef dw_addr,
2143 LLVMValueRef value)
2144 {
2145 value = ac_to_integer(ctx, value);
2146 ac_build_indexed_store(ctx, ctx->lds,
2147 dw_addr, value);
2148 }
2149
2150 LLVMValueRef ac_find_lsb(struct ac_llvm_context *ctx,
2151 LLVMTypeRef dst_type,
2152 LLVMValueRef src0)
2153 {
2154 unsigned src0_bitsize = ac_get_elem_bits(ctx, LLVMTypeOf(src0));
2155 const char *intrin_name;
2156 LLVMTypeRef type;
2157 LLVMValueRef zero;
2158 if (src0_bitsize == 64) {
2159 intrin_name = "llvm.cttz.i64";
2160 type = ctx->i64;
2161 zero = ctx->i64_0;
2162 } else {
2163 intrin_name = "llvm.cttz.i32";
2164 type = ctx->i32;
2165 zero = ctx->i32_0;
2166 }
2167
2168 LLVMValueRef params[2] = {
2169 src0,
2170
2171 /* The value of 1 means that ffs(x=0) = undef, so LLVM won't
2172 * add special code to check for x=0. The reason is that
2173 * the LLVM behavior for x=0 is different from what we
2174 * need here. However, LLVM also assumes that ffs(x) is
2175 * in [0, 31], but GLSL expects that ffs(0) = -1, so
2176 * a conditional assignment to handle 0 is still required.
2177 *
2178 * The hardware already implements the correct behavior.
2179 */
2180 LLVMConstInt(ctx->i1, 1, false),
2181 };
2182
2183 LLVMValueRef lsb = ac_build_intrinsic(ctx, intrin_name, type,
2184 params, 2,
2185 AC_FUNC_ATTR_READNONE);
2186
2187 if (src0_bitsize == 64) {
2188 lsb = LLVMBuildTrunc(ctx->builder, lsb, ctx->i32, "");
2189 }
2190
2191 /* TODO: We need an intrinsic to skip this conditional. */
2192 /* Check for zero: */
2193 return LLVMBuildSelect(ctx->builder, LLVMBuildICmp(ctx->builder,
2194 LLVMIntEQ, src0,
2195 zero, ""),
2196 LLVMConstInt(ctx->i32, -1, 0), lsb, "");
2197 }
2198
2199 LLVMTypeRef ac_array_in_const_addr_space(LLVMTypeRef elem_type)
2200 {
2201 return LLVMPointerType(LLVMArrayType(elem_type, 0),
2202 AC_CONST_ADDR_SPACE);
2203 }
2204
2205 LLVMTypeRef ac_array_in_const32_addr_space(LLVMTypeRef elem_type)
2206 {
2207 if (!HAVE_32BIT_POINTERS)
2208 return ac_array_in_const_addr_space(elem_type);
2209
2210 return LLVMPointerType(LLVMArrayType(elem_type, 0),
2211 AC_CONST_32BIT_ADDR_SPACE);
2212 }
2213
2214 static struct ac_llvm_flow *
2215 get_current_flow(struct ac_llvm_context *ctx)
2216 {
2217 if (ctx->flow_depth > 0)
2218 return &ctx->flow[ctx->flow_depth - 1];
2219 return NULL;
2220 }
2221
2222 static struct ac_llvm_flow *
2223 get_innermost_loop(struct ac_llvm_context *ctx)
2224 {
2225 for (unsigned i = ctx->flow_depth; i > 0; --i) {
2226 if (ctx->flow[i - 1].loop_entry_block)
2227 return &ctx->flow[i - 1];
2228 }
2229 return NULL;
2230 }
2231
2232 static struct ac_llvm_flow *
2233 push_flow(struct ac_llvm_context *ctx)
2234 {
2235 struct ac_llvm_flow *flow;
2236
2237 if (ctx->flow_depth >= ctx->flow_depth_max) {
2238 unsigned new_max = MAX2(ctx->flow_depth << 1,
2239 AC_LLVM_INITIAL_CF_DEPTH);
2240
2241 ctx->flow = realloc(ctx->flow, new_max * sizeof(*ctx->flow));
2242 ctx->flow_depth_max = new_max;
2243 }
2244
2245 flow = &ctx->flow[ctx->flow_depth];
2246 ctx->flow_depth++;
2247
2248 flow->next_block = NULL;
2249 flow->loop_entry_block = NULL;
2250 return flow;
2251 }
2252
2253 static void set_basicblock_name(LLVMBasicBlockRef bb, const char *base,
2254 int label_id)
2255 {
2256 char buf[32];
2257 snprintf(buf, sizeof(buf), "%s%d", base, label_id);
2258 LLVMSetValueName(LLVMBasicBlockAsValue(bb), buf);
2259 }
2260
2261 /* Append a basic block at the level of the parent flow.
2262 */
2263 static LLVMBasicBlockRef append_basic_block(struct ac_llvm_context *ctx,
2264 const char *name)
2265 {
2266 assert(ctx->flow_depth >= 1);
2267
2268 if (ctx->flow_depth >= 2) {
2269 struct ac_llvm_flow *flow = &ctx->flow[ctx->flow_depth - 2];
2270
2271 return LLVMInsertBasicBlockInContext(ctx->context,
2272 flow->next_block, name);
2273 }
2274
2275 LLVMValueRef main_fn =
2276 LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx->builder));
2277 return LLVMAppendBasicBlockInContext(ctx->context, main_fn, name);
2278 }
2279
2280 /* Emit a branch to the given default target for the current block if
2281 * applicable -- that is, if the current block does not already contain a
2282 * branch from a break or continue.
2283 */
2284 static void emit_default_branch(LLVMBuilderRef builder,
2285 LLVMBasicBlockRef target)
2286 {
2287 if (!LLVMGetBasicBlockTerminator(LLVMGetInsertBlock(builder)))
2288 LLVMBuildBr(builder, target);
2289 }
2290
2291 void ac_build_bgnloop(struct ac_llvm_context *ctx, int label_id)
2292 {
2293 struct ac_llvm_flow *flow = push_flow(ctx);
2294 flow->loop_entry_block = append_basic_block(ctx, "LOOP");
2295 flow->next_block = append_basic_block(ctx, "ENDLOOP");
2296 set_basicblock_name(flow->loop_entry_block, "loop", label_id);
2297 LLVMBuildBr(ctx->builder, flow->loop_entry_block);
2298 LLVMPositionBuilderAtEnd(ctx->builder, flow->loop_entry_block);
2299 }
2300
2301 void ac_build_break(struct ac_llvm_context *ctx)
2302 {
2303 struct ac_llvm_flow *flow = get_innermost_loop(ctx);
2304 LLVMBuildBr(ctx->builder, flow->next_block);
2305 }
2306
2307 void ac_build_continue(struct ac_llvm_context *ctx)
2308 {
2309 struct ac_llvm_flow *flow = get_innermost_loop(ctx);
2310 LLVMBuildBr(ctx->builder, flow->loop_entry_block);
2311 }
2312
2313 void ac_build_else(struct ac_llvm_context *ctx, int label_id)
2314 {
2315 struct ac_llvm_flow *current_branch = get_current_flow(ctx);
2316 LLVMBasicBlockRef endif_block;
2317
2318 assert(!current_branch->loop_entry_block);
2319
2320 endif_block = append_basic_block(ctx, "ENDIF");
2321 emit_default_branch(ctx->builder, endif_block);
2322
2323 LLVMPositionBuilderAtEnd(ctx->builder, current_branch->next_block);
2324 set_basicblock_name(current_branch->next_block, "else", label_id);
2325
2326 current_branch->next_block = endif_block;
2327 }
2328
2329 void ac_build_endif(struct ac_llvm_context *ctx, int label_id)
2330 {
2331 struct ac_llvm_flow *current_branch = get_current_flow(ctx);
2332
2333 assert(!current_branch->loop_entry_block);
2334
2335 emit_default_branch(ctx->builder, current_branch->next_block);
2336 LLVMPositionBuilderAtEnd(ctx->builder, current_branch->next_block);
2337 set_basicblock_name(current_branch->next_block, "endif", label_id);
2338
2339 ctx->flow_depth--;
2340 }
2341
2342 void ac_build_endloop(struct ac_llvm_context *ctx, int label_id)
2343 {
2344 struct ac_llvm_flow *current_loop = get_current_flow(ctx);
2345
2346 assert(current_loop->loop_entry_block);
2347
2348 emit_default_branch(ctx->builder, current_loop->loop_entry_block);
2349
2350 LLVMPositionBuilderAtEnd(ctx->builder, current_loop->next_block);
2351 set_basicblock_name(current_loop->next_block, "endloop", label_id);
2352 ctx->flow_depth--;
2353 }
2354
2355 static void if_cond_emit(struct ac_llvm_context *ctx, LLVMValueRef cond,
2356 int label_id)
2357 {
2358 struct ac_llvm_flow *flow = push_flow(ctx);
2359 LLVMBasicBlockRef if_block;
2360
2361 if_block = append_basic_block(ctx, "IF");
2362 flow->next_block = append_basic_block(ctx, "ELSE");
2363 set_basicblock_name(if_block, "if", label_id);
2364 LLVMBuildCondBr(ctx->builder, cond, if_block, flow->next_block);
2365 LLVMPositionBuilderAtEnd(ctx->builder, if_block);
2366 }
2367
2368 void ac_build_if(struct ac_llvm_context *ctx, LLVMValueRef value,
2369 int label_id)
2370 {
2371 LLVMValueRef cond = LLVMBuildFCmp(ctx->builder, LLVMRealUNE,
2372 value, ctx->f32_0, "");
2373 if_cond_emit(ctx, cond, label_id);
2374 }
2375
2376 void ac_build_uif(struct ac_llvm_context *ctx, LLVMValueRef value,
2377 int label_id)
2378 {
2379 LLVMValueRef cond = LLVMBuildICmp(ctx->builder, LLVMIntNE,
2380 ac_to_integer(ctx, value),
2381 ctx->i32_0, "");
2382 if_cond_emit(ctx, cond, label_id);
2383 }
2384
2385 LLVMValueRef ac_build_alloca(struct ac_llvm_context *ac, LLVMTypeRef type,
2386 const char *name)
2387 {
2388 LLVMBuilderRef builder = ac->builder;
2389 LLVMBasicBlockRef current_block = LLVMGetInsertBlock(builder);
2390 LLVMValueRef function = LLVMGetBasicBlockParent(current_block);
2391 LLVMBasicBlockRef first_block = LLVMGetEntryBasicBlock(function);
2392 LLVMValueRef first_instr = LLVMGetFirstInstruction(first_block);
2393 LLVMBuilderRef first_builder = LLVMCreateBuilderInContext(ac->context);
2394 LLVMValueRef res;
2395
2396 if (first_instr) {
2397 LLVMPositionBuilderBefore(first_builder, first_instr);
2398 } else {
2399 LLVMPositionBuilderAtEnd(first_builder, first_block);
2400 }
2401
2402 res = LLVMBuildAlloca(first_builder, type, name);
2403 LLVMBuildStore(builder, LLVMConstNull(type), res);
2404
2405 LLVMDisposeBuilder(first_builder);
2406
2407 return res;
2408 }
2409
2410 LLVMValueRef ac_build_alloca_undef(struct ac_llvm_context *ac,
2411 LLVMTypeRef type, const char *name)
2412 {
2413 LLVMValueRef ptr = ac_build_alloca(ac, type, name);
2414 LLVMBuildStore(ac->builder, LLVMGetUndef(type), ptr);
2415 return ptr;
2416 }
2417
2418 LLVMValueRef ac_cast_ptr(struct ac_llvm_context *ctx, LLVMValueRef ptr,
2419 LLVMTypeRef type)
2420 {
2421 int addr_space = LLVMGetPointerAddressSpace(LLVMTypeOf(ptr));
2422 return LLVMBuildBitCast(ctx->builder, ptr,
2423 LLVMPointerType(type, addr_space), "");
2424 }
2425
2426 LLVMValueRef ac_trim_vector(struct ac_llvm_context *ctx, LLVMValueRef value,
2427 unsigned count)
2428 {
2429 unsigned num_components = ac_get_llvm_num_components(value);
2430 if (count == num_components)
2431 return value;
2432
2433 LLVMValueRef masks[] = {
2434 LLVMConstInt(ctx->i32, 0, false), LLVMConstInt(ctx->i32, 1, false),
2435 LLVMConstInt(ctx->i32, 2, false), LLVMConstInt(ctx->i32, 3, false)};
2436
2437 if (count == 1)
2438 return LLVMBuildExtractElement(ctx->builder, value, masks[0],
2439 "");
2440
2441 LLVMValueRef swizzle = LLVMConstVector(masks, count);
2442 return LLVMBuildShuffleVector(ctx->builder, value, value, swizzle, "");
2443 }
2444
2445 LLVMValueRef ac_unpack_param(struct ac_llvm_context *ctx, LLVMValueRef param,
2446 unsigned rshift, unsigned bitwidth)
2447 {
2448 LLVMValueRef value = param;
2449 if (rshift)
2450 value = LLVMBuildLShr(ctx->builder, value,
2451 LLVMConstInt(ctx->i32, rshift, false), "");
2452
2453 if (rshift + bitwidth < 32) {
2454 unsigned mask = (1 << bitwidth) - 1;
2455 value = LLVMBuildAnd(ctx->builder, value,
2456 LLVMConstInt(ctx->i32, mask, false), "");
2457 }
2458 return value;
2459 }
2460
2461 /* Adjust the sample index according to FMASK.
2462 *
2463 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
2464 * which is the identity mapping. Each nibble says which physical sample
2465 * should be fetched to get that sample.
2466 *
2467 * For example, 0x11111100 means there are only 2 samples stored and
2468 * the second sample covers 3/4 of the pixel. When reading samples 0
2469 * and 1, return physical sample 0 (determined by the first two 0s
2470 * in FMASK), otherwise return physical sample 1.
2471 *
2472 * The sample index should be adjusted as follows:
2473 * addr[sample_index] = (fmask >> (addr[sample_index] * 4)) & 0xF;
2474 */
2475 void ac_apply_fmask_to_sample(struct ac_llvm_context *ac, LLVMValueRef fmask,
2476 LLVMValueRef *addr, bool is_array_tex)
2477 {
2478 struct ac_image_args fmask_load = {};
2479 fmask_load.opcode = ac_image_load;
2480 fmask_load.resource = fmask;
2481 fmask_load.dmask = 0xf;
2482 fmask_load.dim = is_array_tex ? ac_image_2darray : ac_image_2d;
2483
2484 LLVMValueRef fmask_addr[4];
2485 memcpy(fmask_addr, addr, sizeof(fmask_addr[0]) * 3);
2486 fmask_addr[3] = LLVMGetUndef(ac->i32);
2487
2488 fmask_load.addr = ac_build_gather_values(ac, fmask_addr,
2489 is_array_tex ? 4 : 2);
2490
2491 LLVMValueRef fmask_value = ac_build_image_opcode(ac, &fmask_load);
2492 fmask_value = LLVMBuildExtractElement(ac->builder, fmask_value,
2493 ac->i32_0, "");
2494
2495 /* Apply the formula. */
2496 unsigned sample_chan = is_array_tex ? 3 : 2;
2497 LLVMValueRef final_sample;
2498 final_sample = LLVMBuildMul(ac->builder, addr[sample_chan],
2499 LLVMConstInt(ac->i32, 4, 0), "");
2500 final_sample = LLVMBuildLShr(ac->builder, fmask_value, final_sample, "");
2501 final_sample = LLVMBuildAnd(ac->builder, final_sample,
2502 LLVMConstInt(ac->i32, 0xF, 0), "");
2503
2504 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
2505 * resource descriptor is 0 (invalid),
2506 */
2507 LLVMValueRef tmp;
2508 tmp = LLVMBuildBitCast(ac->builder, fmask, ac->v8i32, "");
2509 tmp = LLVMBuildExtractElement(ac->builder, tmp, ac->i32_1, "");
2510 tmp = LLVMBuildICmp(ac->builder, LLVMIntNE, tmp, ac->i32_0, "");
2511
2512 /* Replace the MSAA sample index. */
2513 addr[sample_chan] = LLVMBuildSelect(ac->builder, tmp, final_sample,
2514 addr[sample_chan], "");
2515 }
2516
2517 static LLVMValueRef
2518 _ac_build_readlane(struct ac_llvm_context *ctx, LLVMValueRef src, LLVMValueRef lane)
2519 {
2520 ac_build_optimization_barrier(ctx, &src);
2521 return ac_build_intrinsic(ctx,
2522 lane == NULL ? "llvm.amdgcn.readfirstlane" : "llvm.amdgcn.readlane",
2523 LLVMTypeOf(src), (LLVMValueRef []) {
2524 src, lane },
2525 lane == NULL ? 1 : 2,
2526 AC_FUNC_ATTR_READNONE |
2527 AC_FUNC_ATTR_CONVERGENT);
2528 }
2529
2530 /**
2531 * Builds the "llvm.amdgcn.readlane" or "llvm.amdgcn.readfirstlane" intrinsic.
2532 * @param ctx
2533 * @param src
2534 * @param lane - id of the lane or NULL for the first active lane
2535 * @return value of the lane
2536 */
2537 LLVMValueRef
2538 ac_build_readlane(struct ac_llvm_context *ctx, LLVMValueRef src, LLVMValueRef lane)
2539 {
2540 LLVMTypeRef src_type = LLVMTypeOf(src);
2541 src = ac_to_integer(ctx, src);
2542 unsigned bits = LLVMGetIntTypeWidth(LLVMTypeOf(src));
2543 LLVMValueRef ret;
2544
2545 if (bits == 32) {
2546 ret = _ac_build_readlane(ctx, src, lane);
2547 } else {
2548 assert(bits % 32 == 0);
2549 LLVMTypeRef vec_type = LLVMVectorType(ctx->i32, bits / 32);
2550 LLVMValueRef src_vector =
2551 LLVMBuildBitCast(ctx->builder, src, vec_type, "");
2552 ret = LLVMGetUndef(vec_type);
2553 for (unsigned i = 0; i < bits / 32; i++) {
2554 src = LLVMBuildExtractElement(ctx->builder, src_vector,
2555 LLVMConstInt(ctx->i32, i, 0), "");
2556 LLVMValueRef ret_comp = _ac_build_readlane(ctx, src, lane);
2557 ret = LLVMBuildInsertElement(ctx->builder, ret, ret_comp,
2558 LLVMConstInt(ctx->i32, i, 0), "");
2559 }
2560 }
2561 return LLVMBuildBitCast(ctx->builder, ret, src_type, "");
2562 }
2563
2564 LLVMValueRef
2565 ac_build_writelane(struct ac_llvm_context *ctx, LLVMValueRef src, LLVMValueRef value, LLVMValueRef lane)
2566 {
2567 /* TODO: Use the actual instruction when LLVM adds an intrinsic for it.
2568 */
2569 LLVMValueRef pred = LLVMBuildICmp(ctx->builder, LLVMIntEQ, lane,
2570 ac_get_thread_id(ctx), "");
2571 return LLVMBuildSelect(ctx->builder, pred, value, src, "");
2572 }
2573
2574 LLVMValueRef
2575 ac_build_mbcnt(struct ac_llvm_context *ctx, LLVMValueRef mask)
2576 {
2577 LLVMValueRef mask_vec = LLVMBuildBitCast(ctx->builder, mask,
2578 LLVMVectorType(ctx->i32, 2),
2579 "");
2580 LLVMValueRef mask_lo = LLVMBuildExtractElement(ctx->builder, mask_vec,
2581 ctx->i32_0, "");
2582 LLVMValueRef mask_hi = LLVMBuildExtractElement(ctx->builder, mask_vec,
2583 ctx->i32_1, "");
2584 LLVMValueRef val =
2585 ac_build_intrinsic(ctx, "llvm.amdgcn.mbcnt.lo", ctx->i32,
2586 (LLVMValueRef []) { mask_lo, ctx->i32_0 },
2587 2, AC_FUNC_ATTR_READNONE);
2588 val = ac_build_intrinsic(ctx, "llvm.amdgcn.mbcnt.hi", ctx->i32,
2589 (LLVMValueRef []) { mask_hi, val },
2590 2, AC_FUNC_ATTR_READNONE);
2591 return val;
2592 }
2593
2594 enum dpp_ctrl {
2595 _dpp_quad_perm = 0x000,
2596 _dpp_row_sl = 0x100,
2597 _dpp_row_sr = 0x110,
2598 _dpp_row_rr = 0x120,
2599 dpp_wf_sl1 = 0x130,
2600 dpp_wf_rl1 = 0x134,
2601 dpp_wf_sr1 = 0x138,
2602 dpp_wf_rr1 = 0x13C,
2603 dpp_row_mirror = 0x140,
2604 dpp_row_half_mirror = 0x141,
2605 dpp_row_bcast15 = 0x142,
2606 dpp_row_bcast31 = 0x143
2607 };
2608
2609 static inline enum dpp_ctrl
2610 dpp_quad_perm(unsigned lane0, unsigned lane1, unsigned lane2, unsigned lane3)
2611 {
2612 assert(lane0 < 4 && lane1 < 4 && lane2 < 4 && lane3 < 4);
2613 return _dpp_quad_perm | lane0 | (lane1 << 2) | (lane2 << 4) | (lane3 << 6);
2614 }
2615
2616 static inline enum dpp_ctrl
2617 dpp_row_sl(unsigned amount)
2618 {
2619 assert(amount > 0 && amount < 16);
2620 return _dpp_row_sl | amount;
2621 }
2622
2623 static inline enum dpp_ctrl
2624 dpp_row_sr(unsigned amount)
2625 {
2626 assert(amount > 0 && amount < 16);
2627 return _dpp_row_sr | amount;
2628 }
2629
2630 static LLVMValueRef
2631 _ac_build_dpp(struct ac_llvm_context *ctx, LLVMValueRef old, LLVMValueRef src,
2632 enum dpp_ctrl dpp_ctrl, unsigned row_mask, unsigned bank_mask,
2633 bool bound_ctrl)
2634 {
2635 return ac_build_intrinsic(ctx, "llvm.amdgcn.update.dpp.i32",
2636 LLVMTypeOf(old),
2637 (LLVMValueRef[]) {
2638 old, src,
2639 LLVMConstInt(ctx->i32, dpp_ctrl, 0),
2640 LLVMConstInt(ctx->i32, row_mask, 0),
2641 LLVMConstInt(ctx->i32, bank_mask, 0),
2642 LLVMConstInt(ctx->i1, bound_ctrl, 0) },
2643 6, AC_FUNC_ATTR_READNONE | AC_FUNC_ATTR_CONVERGENT);
2644 }
2645
2646 static LLVMValueRef
2647 ac_build_dpp(struct ac_llvm_context *ctx, LLVMValueRef old, LLVMValueRef src,
2648 enum dpp_ctrl dpp_ctrl, unsigned row_mask, unsigned bank_mask,
2649 bool bound_ctrl)
2650 {
2651 LLVMTypeRef src_type = LLVMTypeOf(src);
2652 src = ac_to_integer(ctx, src);
2653 old = ac_to_integer(ctx, old);
2654 unsigned bits = LLVMGetIntTypeWidth(LLVMTypeOf(src));
2655 LLVMValueRef ret;
2656 if (bits == 32) {
2657 ret = _ac_build_dpp(ctx, old, src, dpp_ctrl, row_mask,
2658 bank_mask, bound_ctrl);
2659 } else {
2660 assert(bits % 32 == 0);
2661 LLVMTypeRef vec_type = LLVMVectorType(ctx->i32, bits / 32);
2662 LLVMValueRef src_vector =
2663 LLVMBuildBitCast(ctx->builder, src, vec_type, "");
2664 LLVMValueRef old_vector =
2665 LLVMBuildBitCast(ctx->builder, old, vec_type, "");
2666 ret = LLVMGetUndef(vec_type);
2667 for (unsigned i = 0; i < bits / 32; i++) {
2668 src = LLVMBuildExtractElement(ctx->builder, src_vector,
2669 LLVMConstInt(ctx->i32, i,
2670 0), "");
2671 old = LLVMBuildExtractElement(ctx->builder, old_vector,
2672 LLVMConstInt(ctx->i32, i,
2673 0), "");
2674 LLVMValueRef ret_comp = _ac_build_dpp(ctx, old, src,
2675 dpp_ctrl,
2676 row_mask,
2677 bank_mask,
2678 bound_ctrl);
2679 ret = LLVMBuildInsertElement(ctx->builder, ret,
2680 ret_comp,
2681 LLVMConstInt(ctx->i32, i,
2682 0), "");
2683 }
2684 }
2685 return LLVMBuildBitCast(ctx->builder, ret, src_type, "");
2686 }
2687
2688 static inline unsigned
2689 ds_pattern_bitmode(unsigned and_mask, unsigned or_mask, unsigned xor_mask)
2690 {
2691 assert(and_mask < 32 && or_mask < 32 && xor_mask < 32);
2692 return and_mask | (or_mask << 5) | (xor_mask << 10);
2693 }
2694
2695 static LLVMValueRef
2696 _ac_build_ds_swizzle(struct ac_llvm_context *ctx, LLVMValueRef src, unsigned mask)
2697 {
2698 return ac_build_intrinsic(ctx, "llvm.amdgcn.ds.swizzle",
2699 LLVMTypeOf(src), (LLVMValueRef []) {
2700 src, LLVMConstInt(ctx->i32, mask, 0) },
2701 2, AC_FUNC_ATTR_READNONE | AC_FUNC_ATTR_CONVERGENT);
2702 }
2703
2704 LLVMValueRef
2705 ac_build_ds_swizzle(struct ac_llvm_context *ctx, LLVMValueRef src, unsigned mask)
2706 {
2707 LLVMTypeRef src_type = LLVMTypeOf(src);
2708 src = ac_to_integer(ctx, src);
2709 unsigned bits = LLVMGetIntTypeWidth(LLVMTypeOf(src));
2710 LLVMValueRef ret;
2711 if (bits == 32) {
2712 ret = _ac_build_ds_swizzle(ctx, src, mask);
2713 } else {
2714 assert(bits % 32 == 0);
2715 LLVMTypeRef vec_type = LLVMVectorType(ctx->i32, bits / 32);
2716 LLVMValueRef src_vector =
2717 LLVMBuildBitCast(ctx->builder, src, vec_type, "");
2718 ret = LLVMGetUndef(vec_type);
2719 for (unsigned i = 0; i < bits / 32; i++) {
2720 src = LLVMBuildExtractElement(ctx->builder, src_vector,
2721 LLVMConstInt(ctx->i32, i,
2722 0), "");
2723 LLVMValueRef ret_comp = _ac_build_ds_swizzle(ctx, src,
2724 mask);
2725 ret = LLVMBuildInsertElement(ctx->builder, ret,
2726 ret_comp,
2727 LLVMConstInt(ctx->i32, i,
2728 0), "");
2729 }
2730 }
2731 return LLVMBuildBitCast(ctx->builder, ret, src_type, "");
2732 }
2733
2734 static LLVMValueRef
2735 ac_build_wwm(struct ac_llvm_context *ctx, LLVMValueRef src)
2736 {
2737 char name[32], type[8];
2738 ac_build_type_name_for_intr(LLVMTypeOf(src), type, sizeof(type));
2739 snprintf(name, sizeof(name), "llvm.amdgcn.wwm.%s", type);
2740 return ac_build_intrinsic(ctx, name, LLVMTypeOf(src),
2741 (LLVMValueRef []) { src }, 1,
2742 AC_FUNC_ATTR_READNONE);
2743 }
2744
2745 static LLVMValueRef
2746 ac_build_set_inactive(struct ac_llvm_context *ctx, LLVMValueRef src,
2747 LLVMValueRef inactive)
2748 {
2749 char name[32], type[8];
2750 LLVMTypeRef src_type = LLVMTypeOf(src);
2751 src = ac_to_integer(ctx, src);
2752 inactive = ac_to_integer(ctx, inactive);
2753 ac_build_type_name_for_intr(LLVMTypeOf(src), type, sizeof(type));
2754 snprintf(name, sizeof(name), "llvm.amdgcn.set.inactive.%s", type);
2755 LLVMValueRef ret =
2756 ac_build_intrinsic(ctx, name,
2757 LLVMTypeOf(src), (LLVMValueRef []) {
2758 src, inactive }, 2,
2759 AC_FUNC_ATTR_READNONE |
2760 AC_FUNC_ATTR_CONVERGENT);
2761 return LLVMBuildBitCast(ctx->builder, ret, src_type, "");
2762 }
2763
2764 static LLVMValueRef
2765 get_reduction_identity(struct ac_llvm_context *ctx, nir_op op, unsigned type_size)
2766 {
2767 if (type_size == 4) {
2768 switch (op) {
2769 case nir_op_iadd: return ctx->i32_0;
2770 case nir_op_fadd: return ctx->f32_0;
2771 case nir_op_imul: return ctx->i32_1;
2772 case nir_op_fmul: return ctx->f32_1;
2773 case nir_op_imin: return LLVMConstInt(ctx->i32, INT32_MAX, 0);
2774 case nir_op_umin: return LLVMConstInt(ctx->i32, UINT32_MAX, 0);
2775 case nir_op_fmin: return LLVMConstReal(ctx->f32, INFINITY);
2776 case nir_op_imax: return LLVMConstInt(ctx->i32, INT32_MIN, 0);
2777 case nir_op_umax: return ctx->i32_0;
2778 case nir_op_fmax: return LLVMConstReal(ctx->f32, -INFINITY);
2779 case nir_op_iand: return LLVMConstInt(ctx->i32, -1, 0);
2780 case nir_op_ior: return ctx->i32_0;
2781 case nir_op_ixor: return ctx->i32_0;
2782 default:
2783 unreachable("bad reduction intrinsic");
2784 }
2785 } else { /* type_size == 64bit */
2786 switch (op) {
2787 case nir_op_iadd: return ctx->i64_0;
2788 case nir_op_fadd: return ctx->f64_0;
2789 case nir_op_imul: return ctx->i64_1;
2790 case nir_op_fmul: return ctx->f64_1;
2791 case nir_op_imin: return LLVMConstInt(ctx->i64, INT64_MAX, 0);
2792 case nir_op_umin: return LLVMConstInt(ctx->i64, UINT64_MAX, 0);
2793 case nir_op_fmin: return LLVMConstReal(ctx->f64, INFINITY);
2794 case nir_op_imax: return LLVMConstInt(ctx->i64, INT64_MIN, 0);
2795 case nir_op_umax: return ctx->i64_0;
2796 case nir_op_fmax: return LLVMConstReal(ctx->f64, -INFINITY);
2797 case nir_op_iand: return LLVMConstInt(ctx->i64, -1, 0);
2798 case nir_op_ior: return ctx->i64_0;
2799 case nir_op_ixor: return ctx->i64_0;
2800 default:
2801 unreachable("bad reduction intrinsic");
2802 }
2803 }
2804 }
2805
2806 static LLVMValueRef
2807 ac_build_alu_op(struct ac_llvm_context *ctx, LLVMValueRef lhs, LLVMValueRef rhs, nir_op op)
2808 {
2809 bool _64bit = ac_get_type_size(LLVMTypeOf(lhs)) == 8;
2810 switch (op) {
2811 case nir_op_iadd: return LLVMBuildAdd(ctx->builder, lhs, rhs, "");
2812 case nir_op_fadd: return LLVMBuildFAdd(ctx->builder, lhs, rhs, "");
2813 case nir_op_imul: return LLVMBuildMul(ctx->builder, lhs, rhs, "");
2814 case nir_op_fmul: return LLVMBuildFMul(ctx->builder, lhs, rhs, "");
2815 case nir_op_imin: return LLVMBuildSelect(ctx->builder,
2816 LLVMBuildICmp(ctx->builder, LLVMIntSLT, lhs, rhs, ""),
2817 lhs, rhs, "");
2818 case nir_op_umin: return LLVMBuildSelect(ctx->builder,
2819 LLVMBuildICmp(ctx->builder, LLVMIntULT, lhs, rhs, ""),
2820 lhs, rhs, "");
2821 case nir_op_fmin: return ac_build_intrinsic(ctx,
2822 _64bit ? "llvm.minnum.f64" : "llvm.minnum.f32",
2823 _64bit ? ctx->f64 : ctx->f32,
2824 (LLVMValueRef[]){lhs, rhs}, 2, AC_FUNC_ATTR_READNONE);
2825 case nir_op_imax: return LLVMBuildSelect(ctx->builder,
2826 LLVMBuildICmp(ctx->builder, LLVMIntSGT, lhs, rhs, ""),
2827 lhs, rhs, "");
2828 case nir_op_umax: return LLVMBuildSelect(ctx->builder,
2829 LLVMBuildICmp(ctx->builder, LLVMIntUGT, lhs, rhs, ""),
2830 lhs, rhs, "");
2831 case nir_op_fmax: return ac_build_intrinsic(ctx,
2832 _64bit ? "llvm.maxnum.f64" : "llvm.maxnum.f32",
2833 _64bit ? ctx->f64 : ctx->f32,
2834 (LLVMValueRef[]){lhs, rhs}, 2, AC_FUNC_ATTR_READNONE);
2835 case nir_op_iand: return LLVMBuildAnd(ctx->builder, lhs, rhs, "");
2836 case nir_op_ior: return LLVMBuildOr(ctx->builder, lhs, rhs, "");
2837 case nir_op_ixor: return LLVMBuildXor(ctx->builder, lhs, rhs, "");
2838 default:
2839 unreachable("bad reduction intrinsic");
2840 }
2841 }
2842
2843 /* TODO: add inclusive and excluse scan functions for SI chip class. */
2844 static LLVMValueRef
2845 ac_build_scan(struct ac_llvm_context *ctx, nir_op op, LLVMValueRef src, LLVMValueRef identity)
2846 {
2847 LLVMValueRef result, tmp;
2848 result = src;
2849 tmp = ac_build_dpp(ctx, identity, src, dpp_row_sr(1), 0xf, 0xf, false);
2850 result = ac_build_alu_op(ctx, result, tmp, op);
2851 tmp = ac_build_dpp(ctx, identity, src, dpp_row_sr(2), 0xf, 0xf, false);
2852 result = ac_build_alu_op(ctx, result, tmp, op);
2853 tmp = ac_build_dpp(ctx, identity, src, dpp_row_sr(3), 0xf, 0xf, false);
2854 result = ac_build_alu_op(ctx, result, tmp, op);
2855 tmp = ac_build_dpp(ctx, identity, result, dpp_row_sr(4), 0xf, 0xe, false);
2856 result = ac_build_alu_op(ctx, result, tmp, op);
2857 tmp = ac_build_dpp(ctx, identity, result, dpp_row_sr(8), 0xf, 0xc, false);
2858 result = ac_build_alu_op(ctx, result, tmp, op);
2859 tmp = ac_build_dpp(ctx, identity, result, dpp_row_bcast15, 0xa, 0xf, false);
2860 result = ac_build_alu_op(ctx, result, tmp, op);
2861 tmp = ac_build_dpp(ctx, identity, result, dpp_row_bcast31, 0xc, 0xf, false);
2862 result = ac_build_alu_op(ctx, result, tmp, op);
2863 return result;
2864 }
2865
2866 LLVMValueRef
2867 ac_build_inclusive_scan(struct ac_llvm_context *ctx, LLVMValueRef src, nir_op op)
2868 {
2869 ac_build_optimization_barrier(ctx, &src);
2870 LLVMValueRef result;
2871 LLVMValueRef identity = get_reduction_identity(ctx, op,
2872 ac_get_type_size(LLVMTypeOf(src)));
2873 result = LLVMBuildBitCast(ctx->builder,
2874 ac_build_set_inactive(ctx, src, identity),
2875 LLVMTypeOf(identity), "");
2876 result = ac_build_scan(ctx, op, result, identity);
2877
2878 return ac_build_wwm(ctx, result);
2879 }
2880
2881 LLVMValueRef
2882 ac_build_exclusive_scan(struct ac_llvm_context *ctx, LLVMValueRef src, nir_op op)
2883 {
2884 ac_build_optimization_barrier(ctx, &src);
2885 LLVMValueRef result;
2886 LLVMValueRef identity = get_reduction_identity(ctx, op,
2887 ac_get_type_size(LLVMTypeOf(src)));
2888 result = LLVMBuildBitCast(ctx->builder,
2889 ac_build_set_inactive(ctx, src, identity),
2890 LLVMTypeOf(identity), "");
2891 result = ac_build_dpp(ctx, identity, result, dpp_wf_sr1, 0xf, 0xf, false);
2892 result = ac_build_scan(ctx, op, result, identity);
2893
2894 return ac_build_wwm(ctx, result);
2895 }
2896
2897 LLVMValueRef
2898 ac_build_reduce(struct ac_llvm_context *ctx, LLVMValueRef src, nir_op op, unsigned cluster_size)
2899 {
2900 if (cluster_size == 1) return src;
2901 ac_build_optimization_barrier(ctx, &src);
2902 LLVMValueRef result, swap;
2903 LLVMValueRef identity = get_reduction_identity(ctx, op,
2904 ac_get_type_size(LLVMTypeOf(src)));
2905 result = LLVMBuildBitCast(ctx->builder,
2906 ac_build_set_inactive(ctx, src, identity),
2907 LLVMTypeOf(identity), "");
2908 swap = ac_build_quad_swizzle(ctx, result, 1, 0, 3, 2);
2909 result = ac_build_alu_op(ctx, result, swap, op);
2910 if (cluster_size == 2) return ac_build_wwm(ctx, result);
2911
2912 swap = ac_build_quad_swizzle(ctx, result, 2, 3, 0, 1);
2913 result = ac_build_alu_op(ctx, result, swap, op);
2914 if (cluster_size == 4) return ac_build_wwm(ctx, result);
2915
2916 if (ctx->chip_class >= VI)
2917 swap = ac_build_dpp(ctx, identity, result, dpp_row_half_mirror, 0xf, 0xf, false);
2918 else
2919 swap = ac_build_ds_swizzle(ctx, result, ds_pattern_bitmode(0x1f, 0, 0x04));
2920 result = ac_build_alu_op(ctx, result, swap, op);
2921 if (cluster_size == 8) return ac_build_wwm(ctx, result);
2922
2923 if (ctx->chip_class >= VI)
2924 swap = ac_build_dpp(ctx, identity, result, dpp_row_mirror, 0xf, 0xf, false);
2925 else
2926 swap = ac_build_ds_swizzle(ctx, result, ds_pattern_bitmode(0x1f, 0, 0x08));
2927 result = ac_build_alu_op(ctx, result, swap, op);
2928 if (cluster_size == 16) return ac_build_wwm(ctx, result);
2929
2930 if (ctx->chip_class >= VI && cluster_size != 32)
2931 swap = ac_build_dpp(ctx, identity, result, dpp_row_bcast15, 0xa, 0xf, false);
2932 else
2933 swap = ac_build_ds_swizzle(ctx, result, ds_pattern_bitmode(0x1f, 0, 0x10));
2934 result = ac_build_alu_op(ctx, result, swap, op);
2935 if (cluster_size == 32) return ac_build_wwm(ctx, result);
2936
2937 if (ctx->chip_class >= VI) {
2938 swap = ac_build_dpp(ctx, identity, result, dpp_row_bcast31, 0xc, 0xf, false);
2939 result = ac_build_alu_op(ctx, result, swap, op);
2940 result = ac_build_readlane(ctx, result, LLVMConstInt(ctx->i32, 63, 0));
2941 return ac_build_wwm(ctx, result);
2942 } else {
2943 swap = ac_build_readlane(ctx, result, ctx->i32_0);
2944 result = ac_build_readlane(ctx, result, LLVMConstInt(ctx->i32, 32, 0));
2945 result = ac_build_alu_op(ctx, result, swap, op);
2946 return ac_build_wwm(ctx, result);
2947 }
2948 }
2949
2950 LLVMValueRef
2951 ac_build_quad_swizzle(struct ac_llvm_context *ctx, LLVMValueRef src,
2952 unsigned lane0, unsigned lane1, unsigned lane2, unsigned lane3)
2953 {
2954 unsigned mask = dpp_quad_perm(lane0, lane1, lane2, lane3);
2955 if (ctx->chip_class >= VI && HAVE_LLVM >= 0x0600) {
2956 return ac_build_dpp(ctx, src, src, mask, 0xf, 0xf, false);
2957 } else {
2958 return ac_build_ds_swizzle(ctx, src, (1 << 15) | mask);
2959 }
2960 }
2961
2962 LLVMValueRef
2963 ac_build_shuffle(struct ac_llvm_context *ctx, LLVMValueRef src, LLVMValueRef index)
2964 {
2965 index = LLVMBuildMul(ctx->builder, index, LLVMConstInt(ctx->i32, 4, 0), "");
2966 return ac_build_intrinsic(ctx,
2967 "llvm.amdgcn.ds.bpermute", ctx->i32,
2968 (LLVMValueRef []) {index, src}, 2,
2969 AC_FUNC_ATTR_READNONE |
2970 AC_FUNC_ATTR_CONVERGENT);
2971 }