radeonsi: move FMASK shader logic to shared code
[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 if (LLVMTypeOf(args[0]) != ctx->i32)
411 args[0] = LLVMBuildBitCast(ctx->builder, args[0], ctx->i32, "");
412
413 return ac_build_intrinsic(ctx,
414 "llvm.amdgcn.icmp.i32",
415 ctx->i64, args, 3,
416 AC_FUNC_ATTR_NOUNWIND |
417 AC_FUNC_ATTR_READNONE |
418 AC_FUNC_ATTR_CONVERGENT);
419 }
420
421 LLVMValueRef
422 ac_build_vote_all(struct ac_llvm_context *ctx, LLVMValueRef value)
423 {
424 LLVMValueRef active_set = ac_build_ballot(ctx, ctx->i32_1);
425 LLVMValueRef vote_set = ac_build_ballot(ctx, value);
426 return LLVMBuildICmp(ctx->builder, LLVMIntEQ, vote_set, active_set, "");
427 }
428
429 LLVMValueRef
430 ac_build_vote_any(struct ac_llvm_context *ctx, LLVMValueRef value)
431 {
432 LLVMValueRef vote_set = ac_build_ballot(ctx, value);
433 return LLVMBuildICmp(ctx->builder, LLVMIntNE, vote_set,
434 LLVMConstInt(ctx->i64, 0, 0), "");
435 }
436
437 LLVMValueRef
438 ac_build_vote_eq(struct ac_llvm_context *ctx, LLVMValueRef value)
439 {
440 LLVMValueRef active_set = ac_build_ballot(ctx, ctx->i32_1);
441 LLVMValueRef vote_set = ac_build_ballot(ctx, value);
442
443 LLVMValueRef all = LLVMBuildICmp(ctx->builder, LLVMIntEQ,
444 vote_set, active_set, "");
445 LLVMValueRef none = LLVMBuildICmp(ctx->builder, LLVMIntEQ,
446 vote_set,
447 LLVMConstInt(ctx->i64, 0, 0), "");
448 return LLVMBuildOr(ctx->builder, all, none, "");
449 }
450
451 LLVMValueRef
452 ac_build_varying_gather_values(struct ac_llvm_context *ctx, LLVMValueRef *values,
453 unsigned value_count, unsigned component)
454 {
455 LLVMValueRef vec = NULL;
456
457 if (value_count == 1) {
458 return values[component];
459 } else if (!value_count)
460 unreachable("value_count is 0");
461
462 for (unsigned i = component; i < value_count + component; i++) {
463 LLVMValueRef value = values[i];
464
465 if (i == component)
466 vec = LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value), value_count));
467 LLVMValueRef index = LLVMConstInt(ctx->i32, i - component, false);
468 vec = LLVMBuildInsertElement(ctx->builder, vec, value, index, "");
469 }
470 return vec;
471 }
472
473 LLVMValueRef
474 ac_build_gather_values_extended(struct ac_llvm_context *ctx,
475 LLVMValueRef *values,
476 unsigned value_count,
477 unsigned value_stride,
478 bool load,
479 bool always_vector)
480 {
481 LLVMBuilderRef builder = ctx->builder;
482 LLVMValueRef vec = NULL;
483 unsigned i;
484
485 if (value_count == 1 && !always_vector) {
486 if (load)
487 return LLVMBuildLoad(builder, values[0], "");
488 return values[0];
489 } else if (!value_count)
490 unreachable("value_count is 0");
491
492 for (i = 0; i < value_count; i++) {
493 LLVMValueRef value = values[i * value_stride];
494 if (load)
495 value = LLVMBuildLoad(builder, value, "");
496
497 if (!i)
498 vec = LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value), value_count));
499 LLVMValueRef index = LLVMConstInt(ctx->i32, i, false);
500 vec = LLVMBuildInsertElement(builder, vec, value, index, "");
501 }
502 return vec;
503 }
504
505 LLVMValueRef
506 ac_build_gather_values(struct ac_llvm_context *ctx,
507 LLVMValueRef *values,
508 unsigned value_count)
509 {
510 return ac_build_gather_values_extended(ctx, values, value_count, 1, false, false);
511 }
512
513 /* Expand a scalar or vector to <4 x type> by filling the remaining channels
514 * with undef. Extract at most num_channels components from the input.
515 */
516 LLVMValueRef ac_build_expand_to_vec4(struct ac_llvm_context *ctx,
517 LLVMValueRef value,
518 unsigned num_channels)
519 {
520 LLVMTypeRef elemtype;
521 LLVMValueRef chan[4];
522
523 if (LLVMGetTypeKind(LLVMTypeOf(value)) == LLVMVectorTypeKind) {
524 unsigned vec_size = LLVMGetVectorSize(LLVMTypeOf(value));
525 num_channels = MIN2(num_channels, vec_size);
526
527 if (num_channels >= 4)
528 return value;
529
530 for (unsigned i = 0; i < num_channels; i++)
531 chan[i] = ac_llvm_extract_elem(ctx, value, i);
532
533 elemtype = LLVMGetElementType(LLVMTypeOf(value));
534 } else {
535 if (num_channels) {
536 assert(num_channels == 1);
537 chan[0] = value;
538 }
539 elemtype = LLVMTypeOf(value);
540 }
541
542 while (num_channels < 4)
543 chan[num_channels++] = LLVMGetUndef(elemtype);
544
545 return ac_build_gather_values(ctx, chan, 4);
546 }
547
548 LLVMValueRef
549 ac_build_fdiv(struct ac_llvm_context *ctx,
550 LLVMValueRef num,
551 LLVMValueRef den)
552 {
553 LLVMValueRef ret = LLVMBuildFDiv(ctx->builder, num, den, "");
554
555 /* Use v_rcp_f32 instead of precise division. */
556 if (!LLVMIsConstant(ret))
557 LLVMSetMetadata(ret, ctx->fpmath_md_kind, ctx->fpmath_md_2p5_ulp);
558 return ret;
559 }
560
561 /* Coordinates for cube map selection. sc, tc, and ma are as in Table 8.27
562 * of the OpenGL 4.5 (Compatibility Profile) specification, except ma is
563 * already multiplied by two. id is the cube face number.
564 */
565 struct cube_selection_coords {
566 LLVMValueRef stc[2];
567 LLVMValueRef ma;
568 LLVMValueRef id;
569 };
570
571 static void
572 build_cube_intrinsic(struct ac_llvm_context *ctx,
573 LLVMValueRef in[3],
574 struct cube_selection_coords *out)
575 {
576 LLVMTypeRef f32 = ctx->f32;
577
578 out->stc[1] = ac_build_intrinsic(ctx, "llvm.amdgcn.cubetc",
579 f32, in, 3, AC_FUNC_ATTR_READNONE);
580 out->stc[0] = ac_build_intrinsic(ctx, "llvm.amdgcn.cubesc",
581 f32, in, 3, AC_FUNC_ATTR_READNONE);
582 out->ma = ac_build_intrinsic(ctx, "llvm.amdgcn.cubema",
583 f32, in, 3, AC_FUNC_ATTR_READNONE);
584 out->id = ac_build_intrinsic(ctx, "llvm.amdgcn.cubeid",
585 f32, in, 3, AC_FUNC_ATTR_READNONE);
586 }
587
588 /**
589 * Build a manual selection sequence for cube face sc/tc coordinates and
590 * major axis vector (multiplied by 2 for consistency) for the given
591 * vec3 \p coords, for the face implied by \p selcoords.
592 *
593 * For the major axis, we always adjust the sign to be in the direction of
594 * selcoords.ma; i.e., a positive out_ma means that coords is pointed towards
595 * the selcoords major axis.
596 */
597 static void build_cube_select(struct ac_llvm_context *ctx,
598 const struct cube_selection_coords *selcoords,
599 const LLVMValueRef *coords,
600 LLVMValueRef *out_st,
601 LLVMValueRef *out_ma)
602 {
603 LLVMBuilderRef builder = ctx->builder;
604 LLVMTypeRef f32 = LLVMTypeOf(coords[0]);
605 LLVMValueRef is_ma_positive;
606 LLVMValueRef sgn_ma;
607 LLVMValueRef is_ma_z, is_not_ma_z;
608 LLVMValueRef is_ma_y;
609 LLVMValueRef is_ma_x;
610 LLVMValueRef sgn;
611 LLVMValueRef tmp;
612
613 is_ma_positive = LLVMBuildFCmp(builder, LLVMRealUGE,
614 selcoords->ma, LLVMConstReal(f32, 0.0), "");
615 sgn_ma = LLVMBuildSelect(builder, is_ma_positive,
616 LLVMConstReal(f32, 1.0), LLVMConstReal(f32, -1.0), "");
617
618 is_ma_z = LLVMBuildFCmp(builder, LLVMRealUGE, selcoords->id, LLVMConstReal(f32, 4.0), "");
619 is_not_ma_z = LLVMBuildNot(builder, is_ma_z, "");
620 is_ma_y = LLVMBuildAnd(builder, is_not_ma_z,
621 LLVMBuildFCmp(builder, LLVMRealUGE, selcoords->id, LLVMConstReal(f32, 2.0), ""), "");
622 is_ma_x = LLVMBuildAnd(builder, is_not_ma_z, LLVMBuildNot(builder, is_ma_y, ""), "");
623
624 /* Select sc */
625 tmp = LLVMBuildSelect(builder, is_ma_x, coords[2], coords[0], "");
626 sgn = LLVMBuildSelect(builder, is_ma_y, LLVMConstReal(f32, 1.0),
627 LLVMBuildSelect(builder, is_ma_z, sgn_ma,
628 LLVMBuildFNeg(builder, sgn_ma, ""), ""), "");
629 out_st[0] = LLVMBuildFMul(builder, tmp, sgn, "");
630
631 /* Select tc */
632 tmp = LLVMBuildSelect(builder, is_ma_y, coords[2], coords[1], "");
633 sgn = LLVMBuildSelect(builder, is_ma_y, sgn_ma,
634 LLVMConstReal(f32, -1.0), "");
635 out_st[1] = LLVMBuildFMul(builder, tmp, sgn, "");
636
637 /* Select ma */
638 tmp = LLVMBuildSelect(builder, is_ma_z, coords[2],
639 LLVMBuildSelect(builder, is_ma_y, coords[1], coords[0], ""), "");
640 tmp = ac_build_intrinsic(ctx, "llvm.fabs.f32",
641 ctx->f32, &tmp, 1, AC_FUNC_ATTR_READNONE);
642 *out_ma = LLVMBuildFMul(builder, tmp, LLVMConstReal(f32, 2.0), "");
643 }
644
645 void
646 ac_prepare_cube_coords(struct ac_llvm_context *ctx,
647 bool is_deriv, bool is_array, bool is_lod,
648 LLVMValueRef *coords_arg,
649 LLVMValueRef *derivs_arg)
650 {
651
652 LLVMBuilderRef builder = ctx->builder;
653 struct cube_selection_coords selcoords;
654 LLVMValueRef coords[3];
655 LLVMValueRef invma;
656
657 if (is_array && !is_lod) {
658 LLVMValueRef tmp = coords_arg[3];
659 tmp = ac_build_intrinsic(ctx, "llvm.rint.f32", ctx->f32, &tmp, 1, 0);
660
661 /* Section 8.9 (Texture Functions) of the GLSL 4.50 spec says:
662 *
663 * "For Array forms, the array layer used will be
664 *
665 * max(0, min(d−1, floor(layer+0.5)))
666 *
667 * where d is the depth of the texture array and layer
668 * comes from the component indicated in the tables below.
669 * Workaroudn for an issue where the layer is taken from a
670 * helper invocation which happens to fall on a different
671 * layer due to extrapolation."
672 *
673 * VI and earlier attempt to implement this in hardware by
674 * clamping the value of coords[2] = (8 * layer) + face.
675 * Unfortunately, this means that the we end up with the wrong
676 * face when clamping occurs.
677 *
678 * Clamp the layer earlier to work around the issue.
679 */
680 if (ctx->chip_class <= VI) {
681 LLVMValueRef ge0;
682 ge0 = LLVMBuildFCmp(builder, LLVMRealOGE, tmp, ctx->f32_0, "");
683 tmp = LLVMBuildSelect(builder, ge0, tmp, ctx->f32_0, "");
684 }
685
686 coords_arg[3] = tmp;
687 }
688
689 build_cube_intrinsic(ctx, coords_arg, &selcoords);
690
691 invma = ac_build_intrinsic(ctx, "llvm.fabs.f32",
692 ctx->f32, &selcoords.ma, 1, AC_FUNC_ATTR_READNONE);
693 invma = ac_build_fdiv(ctx, LLVMConstReal(ctx->f32, 1.0), invma);
694
695 for (int i = 0; i < 2; ++i)
696 coords[i] = LLVMBuildFMul(builder, selcoords.stc[i], invma, "");
697
698 coords[2] = selcoords.id;
699
700 if (is_deriv && derivs_arg) {
701 LLVMValueRef derivs[4];
702 int axis;
703
704 /* Convert cube derivatives to 2D derivatives. */
705 for (axis = 0; axis < 2; axis++) {
706 LLVMValueRef deriv_st[2];
707 LLVMValueRef deriv_ma;
708
709 /* Transform the derivative alongside the texture
710 * coordinate. Mathematically, the correct formula is
711 * as follows. Assume we're projecting onto the +Z face
712 * and denote by dx/dh the derivative of the (original)
713 * X texture coordinate with respect to horizontal
714 * window coordinates. The projection onto the +Z face
715 * plane is:
716 *
717 * f(x,z) = x/z
718 *
719 * Then df/dh = df/dx * dx/dh + df/dz * dz/dh
720 * = 1/z * dx/dh - x/z * 1/z * dz/dh.
721 *
722 * This motivatives the implementation below.
723 *
724 * Whether this actually gives the expected results for
725 * apps that might feed in derivatives obtained via
726 * finite differences is anyone's guess. The OpenGL spec
727 * seems awfully quiet about how textureGrad for cube
728 * maps should be handled.
729 */
730 build_cube_select(ctx, &selcoords, &derivs_arg[axis * 3],
731 deriv_st, &deriv_ma);
732
733 deriv_ma = LLVMBuildFMul(builder, deriv_ma, invma, "");
734
735 for (int i = 0; i < 2; ++i)
736 derivs[axis * 2 + i] =
737 LLVMBuildFSub(builder,
738 LLVMBuildFMul(builder, deriv_st[i], invma, ""),
739 LLVMBuildFMul(builder, deriv_ma, coords[i], ""), "");
740 }
741
742 memcpy(derivs_arg, derivs, sizeof(derivs));
743 }
744
745 /* Shift the texture coordinate. This must be applied after the
746 * derivative calculation.
747 */
748 for (int i = 0; i < 2; ++i)
749 coords[i] = LLVMBuildFAdd(builder, coords[i], LLVMConstReal(ctx->f32, 1.5), "");
750
751 if (is_array) {
752 /* for cube arrays coord.z = coord.w(array_index) * 8 + face */
753 /* coords_arg.w component - array_index for cube arrays */
754 LLVMValueRef tmp = LLVMBuildFMul(ctx->builder, coords_arg[3], LLVMConstReal(ctx->f32, 8.0), "");
755 coords[2] = LLVMBuildFAdd(ctx->builder, tmp, coords[2], "");
756 }
757
758 memcpy(coords_arg, coords, sizeof(coords));
759 }
760
761
762 LLVMValueRef
763 ac_build_fs_interp(struct ac_llvm_context *ctx,
764 LLVMValueRef llvm_chan,
765 LLVMValueRef attr_number,
766 LLVMValueRef params,
767 LLVMValueRef i,
768 LLVMValueRef j)
769 {
770 LLVMValueRef args[5];
771 LLVMValueRef p1;
772
773 args[0] = i;
774 args[1] = llvm_chan;
775 args[2] = attr_number;
776 args[3] = params;
777
778 p1 = ac_build_intrinsic(ctx, "llvm.amdgcn.interp.p1",
779 ctx->f32, args, 4, AC_FUNC_ATTR_READNONE);
780
781 args[0] = p1;
782 args[1] = j;
783 args[2] = llvm_chan;
784 args[3] = attr_number;
785 args[4] = params;
786
787 return ac_build_intrinsic(ctx, "llvm.amdgcn.interp.p2",
788 ctx->f32, args, 5, AC_FUNC_ATTR_READNONE);
789 }
790
791 LLVMValueRef
792 ac_build_fs_interp_mov(struct ac_llvm_context *ctx,
793 LLVMValueRef parameter,
794 LLVMValueRef llvm_chan,
795 LLVMValueRef attr_number,
796 LLVMValueRef params)
797 {
798 LLVMValueRef args[4];
799
800 args[0] = parameter;
801 args[1] = llvm_chan;
802 args[2] = attr_number;
803 args[3] = params;
804
805 return ac_build_intrinsic(ctx, "llvm.amdgcn.interp.mov",
806 ctx->f32, args, 4, AC_FUNC_ATTR_READNONE);
807 }
808
809 LLVMValueRef
810 ac_build_gep0(struct ac_llvm_context *ctx,
811 LLVMValueRef base_ptr,
812 LLVMValueRef index)
813 {
814 LLVMValueRef indices[2] = {
815 LLVMConstInt(ctx->i32, 0, 0),
816 index,
817 };
818 return LLVMBuildGEP(ctx->builder, base_ptr,
819 indices, 2, "");
820 }
821
822 void
823 ac_build_indexed_store(struct ac_llvm_context *ctx,
824 LLVMValueRef base_ptr, LLVMValueRef index,
825 LLVMValueRef value)
826 {
827 LLVMBuildStore(ctx->builder, value,
828 ac_build_gep0(ctx, base_ptr, index));
829 }
830
831 /**
832 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad.
833 * It's equivalent to doing a load from &base_ptr[index].
834 *
835 * \param base_ptr Where the array starts.
836 * \param index The element index into the array.
837 * \param uniform Whether the base_ptr and index can be assumed to be
838 * dynamically uniform (i.e. load to an SGPR)
839 * \param invariant Whether the load is invariant (no other opcodes affect it)
840 */
841 static LLVMValueRef
842 ac_build_load_custom(struct ac_llvm_context *ctx, LLVMValueRef base_ptr,
843 LLVMValueRef index, bool uniform, bool invariant)
844 {
845 LLVMValueRef pointer, result;
846
847 pointer = ac_build_gep0(ctx, base_ptr, index);
848 if (uniform)
849 LLVMSetMetadata(pointer, ctx->uniform_md_kind, ctx->empty_md);
850 result = LLVMBuildLoad(ctx->builder, pointer, "");
851 if (invariant)
852 LLVMSetMetadata(result, ctx->invariant_load_md_kind, ctx->empty_md);
853 return result;
854 }
855
856 LLVMValueRef ac_build_load(struct ac_llvm_context *ctx, LLVMValueRef base_ptr,
857 LLVMValueRef index)
858 {
859 return ac_build_load_custom(ctx, base_ptr, index, false, false);
860 }
861
862 LLVMValueRef ac_build_load_invariant(struct ac_llvm_context *ctx,
863 LLVMValueRef base_ptr, LLVMValueRef index)
864 {
865 return ac_build_load_custom(ctx, base_ptr, index, false, true);
866 }
867
868 LLVMValueRef ac_build_load_to_sgpr(struct ac_llvm_context *ctx,
869 LLVMValueRef base_ptr, LLVMValueRef index)
870 {
871 return ac_build_load_custom(ctx, base_ptr, index, true, true);
872 }
873
874 /* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
875 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
876 * or v4i32 (num_channels=3,4).
877 */
878 void
879 ac_build_buffer_store_dword(struct ac_llvm_context *ctx,
880 LLVMValueRef rsrc,
881 LLVMValueRef vdata,
882 unsigned num_channels,
883 LLVMValueRef voffset,
884 LLVMValueRef soffset,
885 unsigned inst_offset,
886 bool glc,
887 bool slc,
888 bool writeonly_memory,
889 bool swizzle_enable_hint)
890 {
891 /* SWIZZLE_ENABLE requires that soffset isn't folded into voffset
892 * (voffset is swizzled, but soffset isn't swizzled).
893 * llvm.amdgcn.buffer.store doesn't have a separate soffset parameter.
894 */
895 if (!swizzle_enable_hint) {
896 /* Split 3 channel stores, becase LLVM doesn't support 3-channel
897 * intrinsics. */
898 if (num_channels == 3) {
899 LLVMValueRef v[3], v01;
900
901 for (int i = 0; i < 3; i++) {
902 v[i] = LLVMBuildExtractElement(ctx->builder, vdata,
903 LLVMConstInt(ctx->i32, i, 0), "");
904 }
905 v01 = ac_build_gather_values(ctx, v, 2);
906
907 ac_build_buffer_store_dword(ctx, rsrc, v01, 2, voffset,
908 soffset, inst_offset, glc, slc,
909 writeonly_memory, swizzle_enable_hint);
910 ac_build_buffer_store_dword(ctx, rsrc, v[2], 1, voffset,
911 soffset, inst_offset + 8,
912 glc, slc,
913 writeonly_memory, swizzle_enable_hint);
914 return;
915 }
916
917 unsigned func = CLAMP(num_channels, 1, 3) - 1;
918 static const char *types[] = {"f32", "v2f32", "v4f32"};
919 char name[256];
920 LLVMValueRef offset = soffset;
921
922 if (inst_offset)
923 offset = LLVMBuildAdd(ctx->builder, offset,
924 LLVMConstInt(ctx->i32, inst_offset, 0), "");
925 if (voffset)
926 offset = LLVMBuildAdd(ctx->builder, offset, voffset, "");
927
928 LLVMValueRef args[] = {
929 ac_to_float(ctx, vdata),
930 LLVMBuildBitCast(ctx->builder, rsrc, ctx->v4i32, ""),
931 LLVMConstInt(ctx->i32, 0, 0),
932 offset,
933 LLVMConstInt(ctx->i1, glc, 0),
934 LLVMConstInt(ctx->i1, slc, 0),
935 };
936
937 snprintf(name, sizeof(name), "llvm.amdgcn.buffer.store.%s",
938 types[func]);
939
940 ac_build_intrinsic(ctx, name, ctx->voidt,
941 args, ARRAY_SIZE(args),
942 writeonly_memory ?
943 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY :
944 AC_FUNC_ATTR_WRITEONLY);
945 return;
946 }
947
948 static unsigned dfmt[] = {
949 V_008F0C_BUF_DATA_FORMAT_32,
950 V_008F0C_BUF_DATA_FORMAT_32_32,
951 V_008F0C_BUF_DATA_FORMAT_32_32_32,
952 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
953 };
954 assert(num_channels >= 1 && num_channels <= 4);
955
956 LLVMValueRef args[] = {
957 rsrc,
958 vdata,
959 LLVMConstInt(ctx->i32, num_channels, 0),
960 voffset ? voffset : LLVMGetUndef(ctx->i32),
961 soffset,
962 LLVMConstInt(ctx->i32, inst_offset, 0),
963 LLVMConstInt(ctx->i32, dfmt[num_channels - 1], 0),
964 LLVMConstInt(ctx->i32, V_008F0C_BUF_NUM_FORMAT_UINT, 0),
965 LLVMConstInt(ctx->i32, voffset != NULL, 0),
966 LLVMConstInt(ctx->i32, 0, 0), /* idxen */
967 LLVMConstInt(ctx->i32, glc, 0),
968 LLVMConstInt(ctx->i32, slc, 0),
969 LLVMConstInt(ctx->i32, 0, 0), /* tfe*/
970 };
971
972 /* The instruction offset field has 12 bits */
973 assert(voffset || inst_offset < (1 << 12));
974
975 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
976 unsigned func = CLAMP(num_channels, 1, 3) - 1;
977 const char *types[] = {"i32", "v2i32", "v4i32"};
978 char name[256];
979 snprintf(name, sizeof(name), "llvm.SI.tbuffer.store.%s", types[func]);
980
981 ac_build_intrinsic(ctx, name, ctx->voidt,
982 args, ARRAY_SIZE(args),
983 AC_FUNC_ATTR_LEGACY);
984 }
985
986 static LLVMValueRef
987 ac_build_buffer_load_common(struct ac_llvm_context *ctx,
988 LLVMValueRef rsrc,
989 LLVMValueRef vindex,
990 LLVMValueRef voffset,
991 unsigned num_channels,
992 bool glc,
993 bool slc,
994 bool can_speculate,
995 bool use_format)
996 {
997 LLVMValueRef args[] = {
998 LLVMBuildBitCast(ctx->builder, rsrc, ctx->v4i32, ""),
999 vindex ? vindex : LLVMConstInt(ctx->i32, 0, 0),
1000 voffset,
1001 LLVMConstInt(ctx->i1, glc, 0),
1002 LLVMConstInt(ctx->i1, slc, 0)
1003 };
1004 unsigned func = CLAMP(num_channels, 1, 3) - 1;
1005
1006 LLVMTypeRef types[] = {ctx->f32, ctx->v2f32, ctx->v4f32};
1007 const char *type_names[] = {"f32", "v2f32", "v4f32"};
1008 char name[256];
1009
1010 if (use_format) {
1011 snprintf(name, sizeof(name), "llvm.amdgcn.buffer.load.format.%s",
1012 type_names[func]);
1013 } else {
1014 snprintf(name, sizeof(name), "llvm.amdgcn.buffer.load.%s",
1015 type_names[func]);
1016 }
1017
1018 return ac_build_intrinsic(ctx, name, types[func], args,
1019 ARRAY_SIZE(args),
1020 ac_get_load_intr_attribs(can_speculate));
1021 }
1022
1023 LLVMValueRef
1024 ac_build_buffer_load(struct ac_llvm_context *ctx,
1025 LLVMValueRef rsrc,
1026 int num_channels,
1027 LLVMValueRef vindex,
1028 LLVMValueRef voffset,
1029 LLVMValueRef soffset,
1030 unsigned inst_offset,
1031 unsigned glc,
1032 unsigned slc,
1033 bool can_speculate,
1034 bool allow_smem)
1035 {
1036 LLVMValueRef offset = LLVMConstInt(ctx->i32, inst_offset, 0);
1037 if (voffset)
1038 offset = LLVMBuildAdd(ctx->builder, offset, voffset, "");
1039 if (soffset)
1040 offset = LLVMBuildAdd(ctx->builder, offset, soffset, "");
1041
1042 /* TODO: VI and later generations can use SMEM with GLC=1.*/
1043 if (allow_smem && !glc && !slc) {
1044 assert(vindex == NULL);
1045
1046 LLVMValueRef result[8];
1047
1048 for (int i = 0; i < num_channels; i++) {
1049 if (i) {
1050 offset = LLVMBuildAdd(ctx->builder, offset,
1051 LLVMConstInt(ctx->i32, 4, 0), "");
1052 }
1053 LLVMValueRef args[2] = {rsrc, offset};
1054 result[i] = ac_build_intrinsic(ctx, "llvm.SI.load.const.v4i32",
1055 ctx->f32, args, 2,
1056 AC_FUNC_ATTR_READNONE |
1057 AC_FUNC_ATTR_LEGACY);
1058 }
1059 if (num_channels == 1)
1060 return result[0];
1061
1062 if (num_channels == 3)
1063 result[num_channels++] = LLVMGetUndef(ctx->f32);
1064 return ac_build_gather_values(ctx, result, num_channels);
1065 }
1066
1067 return ac_build_buffer_load_common(ctx, rsrc, vindex, offset,
1068 num_channels, glc, slc,
1069 can_speculate, false);
1070 }
1071
1072 LLVMValueRef ac_build_buffer_load_format(struct ac_llvm_context *ctx,
1073 LLVMValueRef rsrc,
1074 LLVMValueRef vindex,
1075 LLVMValueRef voffset,
1076 unsigned num_channels,
1077 bool glc,
1078 bool can_speculate)
1079 {
1080 return ac_build_buffer_load_common(ctx, rsrc, vindex, voffset,
1081 num_channels, glc, false,
1082 can_speculate, true);
1083 }
1084
1085 LLVMValueRef ac_build_buffer_load_format_gfx9_safe(struct ac_llvm_context *ctx,
1086 LLVMValueRef rsrc,
1087 LLVMValueRef vindex,
1088 LLVMValueRef voffset,
1089 unsigned num_channels,
1090 bool glc,
1091 bool can_speculate)
1092 {
1093 LLVMValueRef elem_count = LLVMBuildExtractElement(ctx->builder, rsrc, LLVMConstInt(ctx->i32, 2, 0), "");
1094 LLVMValueRef stride = LLVMBuildExtractElement(ctx->builder, rsrc, LLVMConstInt(ctx->i32, 1, 0), "");
1095 stride = LLVMBuildLShr(ctx->builder, stride, LLVMConstInt(ctx->i32, 16, 0), "");
1096
1097 LLVMValueRef new_elem_count = LLVMBuildSelect(ctx->builder,
1098 LLVMBuildICmp(ctx->builder, LLVMIntUGT, elem_count, stride, ""),
1099 elem_count, stride, "");
1100
1101 LLVMValueRef new_rsrc = LLVMBuildInsertElement(ctx->builder, rsrc, new_elem_count,
1102 LLVMConstInt(ctx->i32, 2, 0), "");
1103
1104 return ac_build_buffer_load_common(ctx, new_rsrc, vindex, voffset,
1105 num_channels, glc, false,
1106 can_speculate, true);
1107 }
1108
1109 /**
1110 * Set range metadata on an instruction. This can only be used on load and
1111 * call instructions. If you know an instruction can only produce the values
1112 * 0, 1, 2, you would do set_range_metadata(value, 0, 3);
1113 * \p lo is the minimum value inclusive.
1114 * \p hi is the maximum value exclusive.
1115 */
1116 static void set_range_metadata(struct ac_llvm_context *ctx,
1117 LLVMValueRef value, unsigned lo, unsigned hi)
1118 {
1119 LLVMValueRef range_md, md_args[2];
1120 LLVMTypeRef type = LLVMTypeOf(value);
1121 LLVMContextRef context = LLVMGetTypeContext(type);
1122
1123 md_args[0] = LLVMConstInt(type, lo, false);
1124 md_args[1] = LLVMConstInt(type, hi, false);
1125 range_md = LLVMMDNodeInContext(context, md_args, 2);
1126 LLVMSetMetadata(value, ctx->range_md_kind, range_md);
1127 }
1128
1129 LLVMValueRef
1130 ac_get_thread_id(struct ac_llvm_context *ctx)
1131 {
1132 LLVMValueRef tid;
1133
1134 LLVMValueRef tid_args[2];
1135 tid_args[0] = LLVMConstInt(ctx->i32, 0xffffffff, false);
1136 tid_args[1] = LLVMConstInt(ctx->i32, 0, false);
1137 tid_args[1] = ac_build_intrinsic(ctx,
1138 "llvm.amdgcn.mbcnt.lo", ctx->i32,
1139 tid_args, 2, AC_FUNC_ATTR_READNONE);
1140
1141 tid = ac_build_intrinsic(ctx, "llvm.amdgcn.mbcnt.hi",
1142 ctx->i32, tid_args,
1143 2, AC_FUNC_ATTR_READNONE);
1144 set_range_metadata(ctx, tid, 0, 64);
1145 return tid;
1146 }
1147
1148 /*
1149 * SI implements derivatives using the local data store (LDS)
1150 * All writes to the LDS happen in all executing threads at
1151 * the same time. TID is the Thread ID for the current
1152 * thread and is a value between 0 and 63, representing
1153 * the thread's position in the wavefront.
1154 *
1155 * For the pixel shader threads are grouped into quads of four pixels.
1156 * The TIDs of the pixels of a quad are:
1157 *
1158 * +------+------+
1159 * |4n + 0|4n + 1|
1160 * +------+------+
1161 * |4n + 2|4n + 3|
1162 * +------+------+
1163 *
1164 * So, masking the TID with 0xfffffffc yields the TID of the top left pixel
1165 * of the quad, masking with 0xfffffffd yields the TID of the top pixel of
1166 * the current pixel's column, and masking with 0xfffffffe yields the TID
1167 * of the left pixel of the current pixel's row.
1168 *
1169 * Adding 1 yields the TID of the pixel to the right of the left pixel, and
1170 * adding 2 yields the TID of the pixel below the top pixel.
1171 */
1172 LLVMValueRef
1173 ac_build_ddxy(struct ac_llvm_context *ctx,
1174 uint32_t mask,
1175 int idx,
1176 LLVMValueRef val)
1177 {
1178 LLVMValueRef tl, trbl, args[2];
1179 LLVMValueRef result;
1180
1181 if (ctx->chip_class >= VI) {
1182 LLVMValueRef thread_id, tl_tid, trbl_tid;
1183 thread_id = ac_get_thread_id(ctx);
1184
1185 tl_tid = LLVMBuildAnd(ctx->builder, thread_id,
1186 LLVMConstInt(ctx->i32, mask, false), "");
1187
1188 trbl_tid = LLVMBuildAdd(ctx->builder, tl_tid,
1189 LLVMConstInt(ctx->i32, idx, false), "");
1190
1191 args[0] = LLVMBuildMul(ctx->builder, tl_tid,
1192 LLVMConstInt(ctx->i32, 4, false), "");
1193 args[1] = val;
1194 tl = ac_build_intrinsic(ctx,
1195 "llvm.amdgcn.ds.bpermute", ctx->i32,
1196 args, 2,
1197 AC_FUNC_ATTR_READNONE |
1198 AC_FUNC_ATTR_CONVERGENT);
1199
1200 args[0] = LLVMBuildMul(ctx->builder, trbl_tid,
1201 LLVMConstInt(ctx->i32, 4, false), "");
1202 trbl = ac_build_intrinsic(ctx,
1203 "llvm.amdgcn.ds.bpermute", ctx->i32,
1204 args, 2,
1205 AC_FUNC_ATTR_READNONE |
1206 AC_FUNC_ATTR_CONVERGENT);
1207 } else {
1208 uint32_t masks[2] = {};
1209
1210 switch (mask) {
1211 case AC_TID_MASK_TOP_LEFT:
1212 masks[0] = 0x8000;
1213 if (idx == 1)
1214 masks[1] = 0x8055;
1215 else
1216 masks[1] = 0x80aa;
1217
1218 break;
1219 case AC_TID_MASK_TOP:
1220 masks[0] = 0x8044;
1221 masks[1] = 0x80ee;
1222 break;
1223 case AC_TID_MASK_LEFT:
1224 masks[0] = 0x80a0;
1225 masks[1] = 0x80f5;
1226 break;
1227 default:
1228 assert(0);
1229 }
1230
1231 args[0] = val;
1232 args[1] = LLVMConstInt(ctx->i32, masks[0], false);
1233
1234 tl = ac_build_intrinsic(ctx,
1235 "llvm.amdgcn.ds.swizzle", ctx->i32,
1236 args, 2,
1237 AC_FUNC_ATTR_READNONE |
1238 AC_FUNC_ATTR_CONVERGENT);
1239
1240 args[1] = LLVMConstInt(ctx->i32, masks[1], false);
1241 trbl = ac_build_intrinsic(ctx,
1242 "llvm.amdgcn.ds.swizzle", ctx->i32,
1243 args, 2,
1244 AC_FUNC_ATTR_READNONE |
1245 AC_FUNC_ATTR_CONVERGENT);
1246 }
1247
1248 tl = LLVMBuildBitCast(ctx->builder, tl, ctx->f32, "");
1249 trbl = LLVMBuildBitCast(ctx->builder, trbl, ctx->f32, "");
1250 result = LLVMBuildFSub(ctx->builder, trbl, tl, "");
1251 return result;
1252 }
1253
1254 void
1255 ac_build_sendmsg(struct ac_llvm_context *ctx,
1256 uint32_t msg,
1257 LLVMValueRef wave_id)
1258 {
1259 LLVMValueRef args[2];
1260 args[0] = LLVMConstInt(ctx->i32, msg, false);
1261 args[1] = wave_id;
1262 ac_build_intrinsic(ctx, "llvm.amdgcn.s.sendmsg", ctx->voidt, args, 2, 0);
1263 }
1264
1265 LLVMValueRef
1266 ac_build_imsb(struct ac_llvm_context *ctx,
1267 LLVMValueRef arg,
1268 LLVMTypeRef dst_type)
1269 {
1270 LLVMValueRef msb = ac_build_intrinsic(ctx, "llvm.amdgcn.sffbh.i32",
1271 dst_type, &arg, 1,
1272 AC_FUNC_ATTR_READNONE);
1273
1274 /* The HW returns the last bit index from MSB, but NIR/TGSI wants
1275 * the index from LSB. Invert it by doing "31 - msb". */
1276 msb = LLVMBuildSub(ctx->builder, LLVMConstInt(ctx->i32, 31, false),
1277 msb, "");
1278
1279 LLVMValueRef all_ones = LLVMConstInt(ctx->i32, -1, true);
1280 LLVMValueRef cond = LLVMBuildOr(ctx->builder,
1281 LLVMBuildICmp(ctx->builder, LLVMIntEQ,
1282 arg, LLVMConstInt(ctx->i32, 0, 0), ""),
1283 LLVMBuildICmp(ctx->builder, LLVMIntEQ,
1284 arg, all_ones, ""), "");
1285
1286 return LLVMBuildSelect(ctx->builder, cond, all_ones, msb, "");
1287 }
1288
1289 LLVMValueRef
1290 ac_build_umsb(struct ac_llvm_context *ctx,
1291 LLVMValueRef arg,
1292 LLVMTypeRef dst_type)
1293 {
1294 LLVMValueRef args[2] = {
1295 arg,
1296 ctx->i1true,
1297 };
1298 LLVMValueRef msb = ac_build_intrinsic(ctx, "llvm.ctlz.i32",
1299 dst_type, args, ARRAY_SIZE(args),
1300 AC_FUNC_ATTR_READNONE);
1301
1302 /* The HW returns the last bit index from MSB, but TGSI/NIR wants
1303 * the index from LSB. Invert it by doing "31 - msb". */
1304 msb = LLVMBuildSub(ctx->builder, LLVMConstInt(ctx->i32, 31, false),
1305 msb, "");
1306
1307 /* check for zero */
1308 return LLVMBuildSelect(ctx->builder,
1309 LLVMBuildICmp(ctx->builder, LLVMIntEQ, arg,
1310 LLVMConstInt(ctx->i32, 0, 0), ""),
1311 LLVMConstInt(ctx->i32, -1, true), msb, "");
1312 }
1313
1314 LLVMValueRef ac_build_fmin(struct ac_llvm_context *ctx, LLVMValueRef a,
1315 LLVMValueRef b)
1316 {
1317 LLVMValueRef args[2] = {a, b};
1318 return ac_build_intrinsic(ctx, "llvm.minnum.f32", ctx->f32, args, 2,
1319 AC_FUNC_ATTR_READNONE);
1320 }
1321
1322 LLVMValueRef ac_build_fmax(struct ac_llvm_context *ctx, LLVMValueRef a,
1323 LLVMValueRef b)
1324 {
1325 LLVMValueRef args[2] = {a, b};
1326 return ac_build_intrinsic(ctx, "llvm.maxnum.f32", ctx->f32, args, 2,
1327 AC_FUNC_ATTR_READNONE);
1328 }
1329
1330 LLVMValueRef ac_build_imin(struct ac_llvm_context *ctx, LLVMValueRef a,
1331 LLVMValueRef b)
1332 {
1333 LLVMValueRef cmp = LLVMBuildICmp(ctx->builder, LLVMIntSLE, a, b, "");
1334 return LLVMBuildSelect(ctx->builder, cmp, a, b, "");
1335 }
1336
1337 LLVMValueRef ac_build_imax(struct ac_llvm_context *ctx, LLVMValueRef a,
1338 LLVMValueRef b)
1339 {
1340 LLVMValueRef cmp = LLVMBuildICmp(ctx->builder, LLVMIntSGT, a, b, "");
1341 return LLVMBuildSelect(ctx->builder, cmp, a, b, "");
1342 }
1343
1344 LLVMValueRef ac_build_umin(struct ac_llvm_context *ctx, LLVMValueRef a,
1345 LLVMValueRef b)
1346 {
1347 LLVMValueRef cmp = LLVMBuildICmp(ctx->builder, LLVMIntULE, a, b, "");
1348 return LLVMBuildSelect(ctx->builder, cmp, a, b, "");
1349 }
1350
1351 LLVMValueRef ac_build_clamp(struct ac_llvm_context *ctx, LLVMValueRef value)
1352 {
1353 if (HAVE_LLVM >= 0x0500) {
1354 return ac_build_fmin(ctx, ac_build_fmax(ctx, value, ctx->f32_0),
1355 ctx->f32_1);
1356 }
1357
1358 LLVMValueRef args[3] = {
1359 value,
1360 LLVMConstReal(ctx->f32, 0),
1361 LLVMConstReal(ctx->f32, 1),
1362 };
1363
1364 return ac_build_intrinsic(ctx, "llvm.AMDGPU.clamp.", ctx->f32, args, 3,
1365 AC_FUNC_ATTR_READNONE |
1366 AC_FUNC_ATTR_LEGACY);
1367 }
1368
1369 void ac_build_export(struct ac_llvm_context *ctx, struct ac_export_args *a)
1370 {
1371 LLVMValueRef args[9];
1372
1373 if (HAVE_LLVM >= 0x0500) {
1374 args[0] = LLVMConstInt(ctx->i32, a->target, 0);
1375 args[1] = LLVMConstInt(ctx->i32, a->enabled_channels, 0);
1376
1377 if (a->compr) {
1378 LLVMTypeRef i16 = LLVMInt16TypeInContext(ctx->context);
1379 LLVMTypeRef v2i16 = LLVMVectorType(i16, 2);
1380
1381 args[2] = LLVMBuildBitCast(ctx->builder, a->out[0],
1382 v2i16, "");
1383 args[3] = LLVMBuildBitCast(ctx->builder, a->out[1],
1384 v2i16, "");
1385 args[4] = LLVMConstInt(ctx->i1, a->done, 0);
1386 args[5] = LLVMConstInt(ctx->i1, a->valid_mask, 0);
1387
1388 ac_build_intrinsic(ctx, "llvm.amdgcn.exp.compr.v2i16",
1389 ctx->voidt, args, 6, 0);
1390 } else {
1391 args[2] = a->out[0];
1392 args[3] = a->out[1];
1393 args[4] = a->out[2];
1394 args[5] = a->out[3];
1395 args[6] = LLVMConstInt(ctx->i1, a->done, 0);
1396 args[7] = LLVMConstInt(ctx->i1, a->valid_mask, 0);
1397
1398 ac_build_intrinsic(ctx, "llvm.amdgcn.exp.f32",
1399 ctx->voidt, args, 8, 0);
1400 }
1401 return;
1402 }
1403
1404 args[0] = LLVMConstInt(ctx->i32, a->enabled_channels, 0);
1405 args[1] = LLVMConstInt(ctx->i32, a->valid_mask, 0);
1406 args[2] = LLVMConstInt(ctx->i32, a->done, 0);
1407 args[3] = LLVMConstInt(ctx->i32, a->target, 0);
1408 args[4] = LLVMConstInt(ctx->i32, a->compr, 0);
1409 memcpy(args + 5, a->out, sizeof(a->out[0]) * 4);
1410
1411 ac_build_intrinsic(ctx, "llvm.SI.export", ctx->voidt, args, 9,
1412 AC_FUNC_ATTR_LEGACY);
1413 }
1414
1415 void ac_build_export_null(struct ac_llvm_context *ctx)
1416 {
1417 struct ac_export_args args;
1418
1419 args.enabled_channels = 0x0; /* enabled channels */
1420 args.valid_mask = 1; /* whether the EXEC mask is valid */
1421 args.done = 1; /* DONE bit */
1422 args.target = V_008DFC_SQ_EXP_NULL;
1423 args.compr = 0; /* COMPR flag (0 = 32-bit export) */
1424 args.out[0] = LLVMGetUndef(ctx->f32); /* R */
1425 args.out[1] = LLVMGetUndef(ctx->f32); /* G */
1426 args.out[2] = LLVMGetUndef(ctx->f32); /* B */
1427 args.out[3] = LLVMGetUndef(ctx->f32); /* A */
1428
1429 ac_build_export(ctx, &args);
1430 }
1431
1432 LLVMValueRef ac_build_image_opcode(struct ac_llvm_context *ctx,
1433 struct ac_image_args *a)
1434 {
1435 LLVMValueRef args[11];
1436 unsigned num_args = 0;
1437 const char *name = NULL;
1438 char intr_name[128], type[64];
1439
1440 bool sample = a->opcode == ac_image_sample ||
1441 a->opcode == ac_image_gather4 ||
1442 a->opcode == ac_image_get_lod;
1443
1444 if (sample)
1445 args[num_args++] = ac_to_float(ctx, a->addr);
1446 else
1447 args[num_args++] = a->addr;
1448
1449 args[num_args++] = a->resource;
1450 if (sample)
1451 args[num_args++] = a->sampler;
1452 args[num_args++] = LLVMConstInt(ctx->i32, a->dmask, 0);
1453 if (sample)
1454 args[num_args++] = LLVMConstInt(ctx->i1, a->unorm, 0);
1455 args[num_args++] = ctx->i1false; /* glc */
1456 args[num_args++] = ctx->i1false; /* slc */
1457 args[num_args++] = ctx->i1false; /* lwe */
1458 args[num_args++] = LLVMConstInt(ctx->i1, a->da, 0);
1459
1460 switch (a->opcode) {
1461 case ac_image_sample:
1462 name = "llvm.amdgcn.image.sample";
1463 break;
1464 case ac_image_gather4:
1465 name = "llvm.amdgcn.image.gather4";
1466 break;
1467 case ac_image_load:
1468 name = "llvm.amdgcn.image.load";
1469 break;
1470 case ac_image_load_mip:
1471 name = "llvm.amdgcn.image.load.mip";
1472 break;
1473 case ac_image_get_lod:
1474 name = "llvm.amdgcn.image.getlod";
1475 break;
1476 case ac_image_get_resinfo:
1477 name = "llvm.amdgcn.image.getresinfo";
1478 break;
1479 default:
1480 unreachable("invalid image opcode");
1481 }
1482
1483 ac_build_type_name_for_intr(LLVMTypeOf(args[0]), type,
1484 sizeof(type));
1485
1486 snprintf(intr_name, sizeof(intr_name), "%s%s%s%s.v4f32.%s.v8i32",
1487 name,
1488 a->compare ? ".c" : "",
1489 a->bias ? ".b" :
1490 a->lod ? ".l" :
1491 a->deriv ? ".d" :
1492 a->level_zero ? ".lz" : "",
1493 a->offset ? ".o" : "",
1494 type);
1495
1496 LLVMValueRef result =
1497 ac_build_intrinsic(ctx, intr_name,
1498 ctx->v4f32, args, num_args,
1499 AC_FUNC_ATTR_READNONE);
1500 if (!sample) {
1501 result = LLVMBuildBitCast(ctx->builder, result,
1502 ctx->v4i32, "");
1503 }
1504 return result;
1505 }
1506
1507 LLVMValueRef ac_build_cvt_pkrtz_f16(struct ac_llvm_context *ctx,
1508 LLVMValueRef args[2])
1509 {
1510 if (HAVE_LLVM >= 0x0500) {
1511 LLVMTypeRef v2f16 =
1512 LLVMVectorType(LLVMHalfTypeInContext(ctx->context), 2);
1513 LLVMValueRef res =
1514 ac_build_intrinsic(ctx, "llvm.amdgcn.cvt.pkrtz",
1515 v2f16, args, 2,
1516 AC_FUNC_ATTR_READNONE);
1517 return LLVMBuildBitCast(ctx->builder, res, ctx->i32, "");
1518 }
1519
1520 return ac_build_intrinsic(ctx, "llvm.SI.packf16", ctx->i32, args, 2,
1521 AC_FUNC_ATTR_READNONE |
1522 AC_FUNC_ATTR_LEGACY);
1523 }
1524
1525 /* Upper 16 bits must be zero. */
1526 static LLVMValueRef ac_llvm_pack_two_int16(struct ac_llvm_context *ctx,
1527 LLVMValueRef val[2])
1528 {
1529 return LLVMBuildOr(ctx->builder, val[0],
1530 LLVMBuildShl(ctx->builder, val[1],
1531 LLVMConstInt(ctx->i32, 16, 0),
1532 ""), "");
1533 }
1534
1535 /* Upper 16 bits are ignored and will be dropped. */
1536 static LLVMValueRef ac_llvm_pack_two_int32_as_int16(struct ac_llvm_context *ctx,
1537 LLVMValueRef val[2])
1538 {
1539 LLVMValueRef v[2] = {
1540 LLVMBuildAnd(ctx->builder, val[0],
1541 LLVMConstInt(ctx->i32, 0xffff, 0), ""),
1542 val[1],
1543 };
1544 return ac_llvm_pack_two_int16(ctx, v);
1545 }
1546
1547 LLVMValueRef ac_build_cvt_pknorm_i16(struct ac_llvm_context *ctx,
1548 LLVMValueRef args[2])
1549 {
1550 if (HAVE_LLVM >= 0x0600) {
1551 LLVMValueRef res =
1552 ac_build_intrinsic(ctx, "llvm.amdgcn.cvt.pknorm.i16",
1553 ctx->v2i16, args, 2,
1554 AC_FUNC_ATTR_READNONE);
1555 return LLVMBuildBitCast(ctx->builder, res, ctx->i32, "");
1556 }
1557
1558 LLVMValueRef val[2];
1559
1560 for (int chan = 0; chan < 2; chan++) {
1561 /* Clamp between [-1, 1]. */
1562 val[chan] = ac_build_fmin(ctx, args[chan], ctx->f32_1);
1563 val[chan] = ac_build_fmax(ctx, val[chan], LLVMConstReal(ctx->f32, -1));
1564 /* Convert to a signed integer in [-32767, 32767]. */
1565 val[chan] = LLVMBuildFMul(ctx->builder, val[chan],
1566 LLVMConstReal(ctx->f32, 32767), "");
1567 /* If positive, add 0.5, else add -0.5. */
1568 val[chan] = LLVMBuildFAdd(ctx->builder, val[chan],
1569 LLVMBuildSelect(ctx->builder,
1570 LLVMBuildFCmp(ctx->builder, LLVMRealOGE,
1571 val[chan], ctx->f32_0, ""),
1572 LLVMConstReal(ctx->f32, 0.5),
1573 LLVMConstReal(ctx->f32, -0.5), ""), "");
1574 val[chan] = LLVMBuildFPToSI(ctx->builder, val[chan], ctx->i32, "");
1575 }
1576 return ac_llvm_pack_two_int32_as_int16(ctx, val);
1577 }
1578
1579 LLVMValueRef ac_build_cvt_pknorm_u16(struct ac_llvm_context *ctx,
1580 LLVMValueRef args[2])
1581 {
1582 if (HAVE_LLVM >= 0x0600) {
1583 LLVMValueRef res =
1584 ac_build_intrinsic(ctx, "llvm.amdgcn.cvt.pknorm.u16",
1585 ctx->v2i16, args, 2,
1586 AC_FUNC_ATTR_READNONE);
1587 return LLVMBuildBitCast(ctx->builder, res, ctx->i32, "");
1588 }
1589
1590 LLVMValueRef val[2];
1591
1592 for (int chan = 0; chan < 2; chan++) {
1593 val[chan] = ac_build_clamp(ctx, args[chan]);
1594 val[chan] = LLVMBuildFMul(ctx->builder, val[chan],
1595 LLVMConstReal(ctx->f32, 65535), "");
1596 val[chan] = LLVMBuildFAdd(ctx->builder, val[chan],
1597 LLVMConstReal(ctx->f32, 0.5), "");
1598 val[chan] = LLVMBuildFPToUI(ctx->builder, val[chan],
1599 ctx->i32, "");
1600 }
1601 return ac_llvm_pack_two_int32_as_int16(ctx, val);
1602 }
1603
1604 /* The 8-bit and 10-bit clamping is for HW workarounds. */
1605 LLVMValueRef ac_build_cvt_pk_i16(struct ac_llvm_context *ctx,
1606 LLVMValueRef args[2], unsigned bits, bool hi)
1607 {
1608 assert(bits == 8 || bits == 10 || bits == 16);
1609
1610 LLVMValueRef max_rgb = LLVMConstInt(ctx->i32,
1611 bits == 8 ? 127 : bits == 10 ? 511 : 32767, 0);
1612 LLVMValueRef min_rgb = LLVMConstInt(ctx->i32,
1613 bits == 8 ? -128 : bits == 10 ? -512 : -32768, 0);
1614 LLVMValueRef max_alpha =
1615 bits != 10 ? max_rgb : ctx->i32_1;
1616 LLVMValueRef min_alpha =
1617 bits != 10 ? min_rgb : LLVMConstInt(ctx->i32, -2, 0);
1618 bool has_intrinsic = HAVE_LLVM >= 0x0600;
1619
1620 /* Clamp. */
1621 if (!has_intrinsic || bits != 16) {
1622 for (int i = 0; i < 2; i++) {
1623 bool alpha = hi && i == 1;
1624 args[i] = ac_build_imin(ctx, args[i],
1625 alpha ? max_alpha : max_rgb);
1626 args[i] = ac_build_imax(ctx, args[i],
1627 alpha ? min_alpha : min_rgb);
1628 }
1629 }
1630
1631 if (has_intrinsic) {
1632 LLVMValueRef res =
1633 ac_build_intrinsic(ctx, "llvm.amdgcn.cvt.pk.i16",
1634 ctx->v2i16, args, 2,
1635 AC_FUNC_ATTR_READNONE);
1636 return LLVMBuildBitCast(ctx->builder, res, ctx->i32, "");
1637 }
1638
1639 return ac_llvm_pack_two_int32_as_int16(ctx, args);
1640 }
1641
1642 /* The 8-bit and 10-bit clamping is for HW workarounds. */
1643 LLVMValueRef ac_build_cvt_pk_u16(struct ac_llvm_context *ctx,
1644 LLVMValueRef args[2], unsigned bits, bool hi)
1645 {
1646 assert(bits == 8 || bits == 10 || bits == 16);
1647
1648 LLVMValueRef max_rgb = LLVMConstInt(ctx->i32,
1649 bits == 8 ? 255 : bits == 10 ? 1023 : 65535, 0);
1650 LLVMValueRef max_alpha =
1651 bits != 10 ? max_rgb : LLVMConstInt(ctx->i32, 3, 0);
1652 bool has_intrinsic = HAVE_LLVM >= 0x0600;
1653
1654 /* Clamp. */
1655 if (!has_intrinsic || bits != 16) {
1656 for (int i = 0; i < 2; i++) {
1657 bool alpha = hi && i == 1;
1658 args[i] = ac_build_umin(ctx, args[i],
1659 alpha ? max_alpha : max_rgb);
1660 }
1661 }
1662
1663 if (has_intrinsic) {
1664 LLVMValueRef res =
1665 ac_build_intrinsic(ctx, "llvm.amdgcn.cvt.pk.u16",
1666 ctx->v2i16, args, 2,
1667 AC_FUNC_ATTR_READNONE);
1668 return LLVMBuildBitCast(ctx->builder, res, ctx->i32, "");
1669 }
1670
1671 return ac_llvm_pack_two_int16(ctx, args);
1672 }
1673
1674 LLVMValueRef ac_build_wqm_vote(struct ac_llvm_context *ctx, LLVMValueRef i1)
1675 {
1676 assert(HAVE_LLVM >= 0x0600);
1677 return ac_build_intrinsic(ctx, "llvm.amdgcn.wqm.vote", ctx->i1,
1678 &i1, 1, AC_FUNC_ATTR_READNONE);
1679 }
1680
1681 void ac_build_kill_if_false(struct ac_llvm_context *ctx, LLVMValueRef i1)
1682 {
1683 if (HAVE_LLVM >= 0x0600) {
1684 ac_build_intrinsic(ctx, "llvm.amdgcn.kill", ctx->voidt,
1685 &i1, 1, 0);
1686 return;
1687 }
1688
1689 LLVMValueRef value = LLVMBuildSelect(ctx->builder, i1,
1690 LLVMConstReal(ctx->f32, 1),
1691 LLVMConstReal(ctx->f32, -1), "");
1692 ac_build_intrinsic(ctx, "llvm.AMDGPU.kill", ctx->voidt,
1693 &value, 1, AC_FUNC_ATTR_LEGACY);
1694 }
1695
1696 LLVMValueRef ac_build_bfe(struct ac_llvm_context *ctx, LLVMValueRef input,
1697 LLVMValueRef offset, LLVMValueRef width,
1698 bool is_signed)
1699 {
1700 LLVMValueRef args[] = {
1701 input,
1702 offset,
1703 width,
1704 };
1705
1706 if (HAVE_LLVM >= 0x0500) {
1707 return ac_build_intrinsic(ctx,
1708 is_signed ? "llvm.amdgcn.sbfe.i32" :
1709 "llvm.amdgcn.ubfe.i32",
1710 ctx->i32, args, 3,
1711 AC_FUNC_ATTR_READNONE);
1712 }
1713
1714 return ac_build_intrinsic(ctx,
1715 is_signed ? "llvm.AMDGPU.bfe.i32" :
1716 "llvm.AMDGPU.bfe.u32",
1717 ctx->i32, args, 3,
1718 AC_FUNC_ATTR_READNONE |
1719 AC_FUNC_ATTR_LEGACY);
1720 }
1721
1722 void ac_build_waitcnt(struct ac_llvm_context *ctx, unsigned simm16)
1723 {
1724 LLVMValueRef args[1] = {
1725 LLVMConstInt(ctx->i32, simm16, false),
1726 };
1727 ac_build_intrinsic(ctx, "llvm.amdgcn.s.waitcnt",
1728 ctx->voidt, args, 1, 0);
1729 }
1730
1731 LLVMValueRef ac_build_fract(struct ac_llvm_context *ctx, LLVMValueRef src0,
1732 unsigned bitsize)
1733 {
1734 LLVMTypeRef type;
1735 char *intr;
1736
1737 if (bitsize == 32) {
1738 intr = "llvm.floor.f32";
1739 type = ctx->f32;
1740 } else {
1741 intr = "llvm.floor.f64";
1742 type = ctx->f64;
1743 }
1744
1745 LLVMValueRef params[] = {
1746 src0,
1747 };
1748 LLVMValueRef floor = ac_build_intrinsic(ctx, intr, type, params, 1,
1749 AC_FUNC_ATTR_READNONE);
1750 return LLVMBuildFSub(ctx->builder, src0, floor, "");
1751 }
1752
1753 LLVMValueRef ac_build_isign(struct ac_llvm_context *ctx, LLVMValueRef src0,
1754 unsigned bitsize)
1755 {
1756 LLVMValueRef cmp, val, zero, one;
1757 LLVMTypeRef type;
1758
1759 if (bitsize == 32) {
1760 type = ctx->i32;
1761 zero = ctx->i32_0;
1762 one = ctx->i32_1;
1763 } else {
1764 type = ctx->i64;
1765 zero = ctx->i64_0;
1766 one = ctx->i64_1;
1767 }
1768
1769 cmp = LLVMBuildICmp(ctx->builder, LLVMIntSGT, src0, zero, "");
1770 val = LLVMBuildSelect(ctx->builder, cmp, one, src0, "");
1771 cmp = LLVMBuildICmp(ctx->builder, LLVMIntSGE, val, zero, "");
1772 val = LLVMBuildSelect(ctx->builder, cmp, val, LLVMConstInt(type, -1, true), "");
1773 return val;
1774 }
1775
1776 LLVMValueRef ac_build_fsign(struct ac_llvm_context *ctx, LLVMValueRef src0,
1777 unsigned bitsize)
1778 {
1779 LLVMValueRef cmp, val, zero, one;
1780 LLVMTypeRef type;
1781
1782 if (bitsize == 32) {
1783 type = ctx->f32;
1784 zero = ctx->f32_0;
1785 one = ctx->f32_1;
1786 } else {
1787 type = ctx->f64;
1788 zero = ctx->f64_0;
1789 one = ctx->f64_1;
1790 }
1791
1792 cmp = LLVMBuildFCmp(ctx->builder, LLVMRealOGT, src0, zero, "");
1793 val = LLVMBuildSelect(ctx->builder, cmp, one, src0, "");
1794 cmp = LLVMBuildFCmp(ctx->builder, LLVMRealOGE, val, zero, "");
1795 val = LLVMBuildSelect(ctx->builder, cmp, val, LLVMConstReal(type, -1.0), "");
1796 return val;
1797 }
1798
1799 void ac_get_image_intr_name(const char *base_name,
1800 LLVMTypeRef data_type,
1801 LLVMTypeRef coords_type,
1802 LLVMTypeRef rsrc_type,
1803 char *out_name, unsigned out_len)
1804 {
1805 char coords_type_name[8];
1806
1807 ac_build_type_name_for_intr(coords_type, coords_type_name,
1808 sizeof(coords_type_name));
1809
1810 char data_type_name[8];
1811 char rsrc_type_name[8];
1812
1813 ac_build_type_name_for_intr(data_type, data_type_name,
1814 sizeof(data_type_name));
1815 ac_build_type_name_for_intr(rsrc_type, rsrc_type_name,
1816 sizeof(rsrc_type_name));
1817 snprintf(out_name, out_len, "%s.%s.%s.%s", base_name,
1818 data_type_name, coords_type_name, rsrc_type_name);
1819 }
1820
1821 #define AC_EXP_TARGET (HAVE_LLVM >= 0x0500 ? 0 : 3)
1822 #define AC_EXP_ENABLED_CHANNELS (HAVE_LLVM >= 0x0500 ? 1 : 0)
1823 #define AC_EXP_OUT0 (HAVE_LLVM >= 0x0500 ? 2 : 5)
1824
1825 enum ac_ir_type {
1826 AC_IR_UNDEF,
1827 AC_IR_CONST,
1828 AC_IR_VALUE,
1829 };
1830
1831 struct ac_vs_exp_chan
1832 {
1833 LLVMValueRef value;
1834 float const_float;
1835 enum ac_ir_type type;
1836 };
1837
1838 struct ac_vs_exp_inst {
1839 unsigned offset;
1840 LLVMValueRef inst;
1841 struct ac_vs_exp_chan chan[4];
1842 };
1843
1844 struct ac_vs_exports {
1845 unsigned num;
1846 struct ac_vs_exp_inst exp[VARYING_SLOT_MAX];
1847 };
1848
1849 /* Return true if the PARAM export has been eliminated. */
1850 static bool ac_eliminate_const_output(uint8_t *vs_output_param_offset,
1851 uint32_t num_outputs,
1852 struct ac_vs_exp_inst *exp)
1853 {
1854 unsigned i, default_val; /* SPI_PS_INPUT_CNTL_i.DEFAULT_VAL */
1855 bool is_zero[4] = {}, is_one[4] = {};
1856
1857 for (i = 0; i < 4; i++) {
1858 /* It's a constant expression. Undef outputs are eliminated too. */
1859 if (exp->chan[i].type == AC_IR_UNDEF) {
1860 is_zero[i] = true;
1861 is_one[i] = true;
1862 } else if (exp->chan[i].type == AC_IR_CONST) {
1863 if (exp->chan[i].const_float == 0)
1864 is_zero[i] = true;
1865 else if (exp->chan[i].const_float == 1)
1866 is_one[i] = true;
1867 else
1868 return false; /* other constant */
1869 } else
1870 return false;
1871 }
1872
1873 /* Only certain combinations of 0 and 1 can be eliminated. */
1874 if (is_zero[0] && is_zero[1] && is_zero[2])
1875 default_val = is_zero[3] ? 0 : 1;
1876 else if (is_one[0] && is_one[1] && is_one[2])
1877 default_val = is_zero[3] ? 2 : 3;
1878 else
1879 return false;
1880
1881 /* The PARAM export can be represented as DEFAULT_VAL. Kill it. */
1882 LLVMInstructionEraseFromParent(exp->inst);
1883
1884 /* Change OFFSET to DEFAULT_VAL. */
1885 for (i = 0; i < num_outputs; i++) {
1886 if (vs_output_param_offset[i] == exp->offset) {
1887 vs_output_param_offset[i] =
1888 AC_EXP_PARAM_DEFAULT_VAL_0000 + default_val;
1889 break;
1890 }
1891 }
1892 return true;
1893 }
1894
1895 static bool ac_eliminate_duplicated_output(struct ac_llvm_context *ctx,
1896 uint8_t *vs_output_param_offset,
1897 uint32_t num_outputs,
1898 struct ac_vs_exports *processed,
1899 struct ac_vs_exp_inst *exp)
1900 {
1901 unsigned p, copy_back_channels = 0;
1902
1903 /* See if the output is already in the list of processed outputs.
1904 * The LLVMValueRef comparison relies on SSA.
1905 */
1906 for (p = 0; p < processed->num; p++) {
1907 bool different = false;
1908
1909 for (unsigned j = 0; j < 4; j++) {
1910 struct ac_vs_exp_chan *c1 = &processed->exp[p].chan[j];
1911 struct ac_vs_exp_chan *c2 = &exp->chan[j];
1912
1913 /* Treat undef as a match. */
1914 if (c2->type == AC_IR_UNDEF)
1915 continue;
1916
1917 /* If c1 is undef but c2 isn't, we can copy c2 to c1
1918 * and consider the instruction duplicated.
1919 */
1920 if (c1->type == AC_IR_UNDEF) {
1921 copy_back_channels |= 1 << j;
1922 continue;
1923 }
1924
1925 /* Test whether the channels are not equal. */
1926 if (c1->type != c2->type ||
1927 (c1->type == AC_IR_CONST &&
1928 c1->const_float != c2->const_float) ||
1929 (c1->type == AC_IR_VALUE &&
1930 c1->value != c2->value)) {
1931 different = true;
1932 break;
1933 }
1934 }
1935 if (!different)
1936 break;
1937
1938 copy_back_channels = 0;
1939 }
1940 if (p == processed->num)
1941 return false;
1942
1943 /* If a match was found, but the matching export has undef where the new
1944 * one has a normal value, copy the normal value to the undef channel.
1945 */
1946 struct ac_vs_exp_inst *match = &processed->exp[p];
1947
1948 /* Get current enabled channels mask. */
1949 LLVMValueRef arg = LLVMGetOperand(match->inst, AC_EXP_ENABLED_CHANNELS);
1950 unsigned enabled_channels = LLVMConstIntGetZExtValue(arg);
1951
1952 while (copy_back_channels) {
1953 unsigned chan = u_bit_scan(&copy_back_channels);
1954
1955 assert(match->chan[chan].type == AC_IR_UNDEF);
1956 LLVMSetOperand(match->inst, AC_EXP_OUT0 + chan,
1957 exp->chan[chan].value);
1958 match->chan[chan] = exp->chan[chan];
1959
1960 /* Update number of enabled channels because the original mask
1961 * is not always 0xf.
1962 */
1963 enabled_channels |= (1 << chan);
1964 LLVMSetOperand(match->inst, AC_EXP_ENABLED_CHANNELS,
1965 LLVMConstInt(ctx->i32, enabled_channels, 0));
1966 }
1967
1968 /* The PARAM export is duplicated. Kill it. */
1969 LLVMInstructionEraseFromParent(exp->inst);
1970
1971 /* Change OFFSET to the matching export. */
1972 for (unsigned i = 0; i < num_outputs; i++) {
1973 if (vs_output_param_offset[i] == exp->offset) {
1974 vs_output_param_offset[i] = match->offset;
1975 break;
1976 }
1977 }
1978 return true;
1979 }
1980
1981 void ac_optimize_vs_outputs(struct ac_llvm_context *ctx,
1982 LLVMValueRef main_fn,
1983 uint8_t *vs_output_param_offset,
1984 uint32_t num_outputs,
1985 uint8_t *num_param_exports)
1986 {
1987 LLVMBasicBlockRef bb;
1988 bool removed_any = false;
1989 struct ac_vs_exports exports;
1990
1991 exports.num = 0;
1992
1993 /* Process all LLVM instructions. */
1994 bb = LLVMGetFirstBasicBlock(main_fn);
1995 while (bb) {
1996 LLVMValueRef inst = LLVMGetFirstInstruction(bb);
1997
1998 while (inst) {
1999 LLVMValueRef cur = inst;
2000 inst = LLVMGetNextInstruction(inst);
2001 struct ac_vs_exp_inst exp;
2002
2003 if (LLVMGetInstructionOpcode(cur) != LLVMCall)
2004 continue;
2005
2006 LLVMValueRef callee = ac_llvm_get_called_value(cur);
2007
2008 if (!ac_llvm_is_function(callee))
2009 continue;
2010
2011 const char *name = LLVMGetValueName(callee);
2012 unsigned num_args = LLVMCountParams(callee);
2013
2014 /* Check if this is an export instruction. */
2015 if ((num_args != 9 && num_args != 8) ||
2016 (strcmp(name, "llvm.SI.export") &&
2017 strcmp(name, "llvm.amdgcn.exp.f32")))
2018 continue;
2019
2020 LLVMValueRef arg = LLVMGetOperand(cur, AC_EXP_TARGET);
2021 unsigned target = LLVMConstIntGetZExtValue(arg);
2022
2023 if (target < V_008DFC_SQ_EXP_PARAM)
2024 continue;
2025
2026 target -= V_008DFC_SQ_EXP_PARAM;
2027
2028 /* Parse the instruction. */
2029 memset(&exp, 0, sizeof(exp));
2030 exp.offset = target;
2031 exp.inst = cur;
2032
2033 for (unsigned i = 0; i < 4; i++) {
2034 LLVMValueRef v = LLVMGetOperand(cur, AC_EXP_OUT0 + i);
2035
2036 exp.chan[i].value = v;
2037
2038 if (LLVMIsUndef(v)) {
2039 exp.chan[i].type = AC_IR_UNDEF;
2040 } else if (LLVMIsAConstantFP(v)) {
2041 LLVMBool loses_info;
2042 exp.chan[i].type = AC_IR_CONST;
2043 exp.chan[i].const_float =
2044 LLVMConstRealGetDouble(v, &loses_info);
2045 } else {
2046 exp.chan[i].type = AC_IR_VALUE;
2047 }
2048 }
2049
2050 /* Eliminate constant and duplicated PARAM exports. */
2051 if (ac_eliminate_const_output(vs_output_param_offset,
2052 num_outputs, &exp) ||
2053 ac_eliminate_duplicated_output(ctx,
2054 vs_output_param_offset,
2055 num_outputs, &exports,
2056 &exp)) {
2057 removed_any = true;
2058 } else {
2059 exports.exp[exports.num++] = exp;
2060 }
2061 }
2062 bb = LLVMGetNextBasicBlock(bb);
2063 }
2064
2065 /* Remove holes in export memory due to removed PARAM exports.
2066 * This is done by renumbering all PARAM exports.
2067 */
2068 if (removed_any) {
2069 uint8_t old_offset[VARYING_SLOT_MAX];
2070 unsigned out, i;
2071
2072 /* Make a copy of the offsets. We need the old version while
2073 * we are modifying some of them. */
2074 memcpy(old_offset, vs_output_param_offset,
2075 sizeof(old_offset));
2076
2077 for (i = 0; i < exports.num; i++) {
2078 unsigned offset = exports.exp[i].offset;
2079
2080 /* Update vs_output_param_offset. Multiple outputs can
2081 * have the same offset.
2082 */
2083 for (out = 0; out < num_outputs; out++) {
2084 if (old_offset[out] == offset)
2085 vs_output_param_offset[out] = i;
2086 }
2087
2088 /* Change the PARAM offset in the instruction. */
2089 LLVMSetOperand(exports.exp[i].inst, AC_EXP_TARGET,
2090 LLVMConstInt(ctx->i32,
2091 V_008DFC_SQ_EXP_PARAM + i, 0));
2092 }
2093 *num_param_exports = exports.num;
2094 }
2095 }
2096
2097 void ac_init_exec_full_mask(struct ac_llvm_context *ctx)
2098 {
2099 LLVMValueRef full_mask = LLVMConstInt(ctx->i64, ~0ull, 0);
2100 ac_build_intrinsic(ctx,
2101 "llvm.amdgcn.init.exec", ctx->voidt,
2102 &full_mask, 1, AC_FUNC_ATTR_CONVERGENT);
2103 }
2104
2105 void ac_declare_lds_as_pointer(struct ac_llvm_context *ctx)
2106 {
2107 unsigned lds_size = ctx->chip_class >= CIK ? 65536 : 32768;
2108 ctx->lds = LLVMBuildIntToPtr(ctx->builder, ctx->i32_0,
2109 LLVMPointerType(LLVMArrayType(ctx->i32, lds_size / 4), AC_LOCAL_ADDR_SPACE),
2110 "lds");
2111 }
2112
2113 LLVMValueRef ac_lds_load(struct ac_llvm_context *ctx,
2114 LLVMValueRef dw_addr)
2115 {
2116 return ac_build_load(ctx, ctx->lds, dw_addr);
2117 }
2118
2119 void ac_lds_store(struct ac_llvm_context *ctx,
2120 LLVMValueRef dw_addr,
2121 LLVMValueRef value)
2122 {
2123 value = ac_to_integer(ctx, value);
2124 ac_build_indexed_store(ctx, ctx->lds,
2125 dw_addr, value);
2126 }
2127
2128 LLVMValueRef ac_find_lsb(struct ac_llvm_context *ctx,
2129 LLVMTypeRef dst_type,
2130 LLVMValueRef src0)
2131 {
2132 unsigned src0_bitsize = ac_get_elem_bits(ctx, LLVMTypeOf(src0));
2133 const char *intrin_name;
2134 LLVMTypeRef type;
2135 LLVMValueRef zero;
2136 if (src0_bitsize == 64) {
2137 intrin_name = "llvm.cttz.i64";
2138 type = ctx->i64;
2139 zero = ctx->i64_0;
2140 } else {
2141 intrin_name = "llvm.cttz.i32";
2142 type = ctx->i32;
2143 zero = ctx->i32_0;
2144 }
2145
2146 LLVMValueRef params[2] = {
2147 src0,
2148
2149 /* The value of 1 means that ffs(x=0) = undef, so LLVM won't
2150 * add special code to check for x=0. The reason is that
2151 * the LLVM behavior for x=0 is different from what we
2152 * need here. However, LLVM also assumes that ffs(x) is
2153 * in [0, 31], but GLSL expects that ffs(0) = -1, so
2154 * a conditional assignment to handle 0 is still required.
2155 *
2156 * The hardware already implements the correct behavior.
2157 */
2158 LLVMConstInt(ctx->i1, 1, false),
2159 };
2160
2161 LLVMValueRef lsb = ac_build_intrinsic(ctx, intrin_name, type,
2162 params, 2,
2163 AC_FUNC_ATTR_READNONE);
2164
2165 if (src0_bitsize == 64) {
2166 lsb = LLVMBuildTrunc(ctx->builder, lsb, ctx->i32, "");
2167 }
2168
2169 /* TODO: We need an intrinsic to skip this conditional. */
2170 /* Check for zero: */
2171 return LLVMBuildSelect(ctx->builder, LLVMBuildICmp(ctx->builder,
2172 LLVMIntEQ, src0,
2173 zero, ""),
2174 LLVMConstInt(ctx->i32, -1, 0), lsb, "");
2175 }
2176
2177 LLVMTypeRef ac_array_in_const_addr_space(LLVMTypeRef elem_type)
2178 {
2179 return LLVMPointerType(LLVMArrayType(elem_type, 0),
2180 AC_CONST_ADDR_SPACE);
2181 }
2182
2183 LLVMTypeRef ac_array_in_const32_addr_space(LLVMTypeRef elem_type)
2184 {
2185 if (!HAVE_32BIT_POINTERS)
2186 return ac_array_in_const_addr_space(elem_type);
2187
2188 return LLVMPointerType(LLVMArrayType(elem_type, 0),
2189 AC_CONST_32BIT_ADDR_SPACE);
2190 }
2191
2192 static struct ac_llvm_flow *
2193 get_current_flow(struct ac_llvm_context *ctx)
2194 {
2195 if (ctx->flow_depth > 0)
2196 return &ctx->flow[ctx->flow_depth - 1];
2197 return NULL;
2198 }
2199
2200 static struct ac_llvm_flow *
2201 get_innermost_loop(struct ac_llvm_context *ctx)
2202 {
2203 for (unsigned i = ctx->flow_depth; i > 0; --i) {
2204 if (ctx->flow[i - 1].loop_entry_block)
2205 return &ctx->flow[i - 1];
2206 }
2207 return NULL;
2208 }
2209
2210 static struct ac_llvm_flow *
2211 push_flow(struct ac_llvm_context *ctx)
2212 {
2213 struct ac_llvm_flow *flow;
2214
2215 if (ctx->flow_depth >= ctx->flow_depth_max) {
2216 unsigned new_max = MAX2(ctx->flow_depth << 1,
2217 AC_LLVM_INITIAL_CF_DEPTH);
2218
2219 ctx->flow = realloc(ctx->flow, new_max * sizeof(*ctx->flow));
2220 ctx->flow_depth_max = new_max;
2221 }
2222
2223 flow = &ctx->flow[ctx->flow_depth];
2224 ctx->flow_depth++;
2225
2226 flow->next_block = NULL;
2227 flow->loop_entry_block = NULL;
2228 return flow;
2229 }
2230
2231 static void set_basicblock_name(LLVMBasicBlockRef bb, const char *base,
2232 int label_id)
2233 {
2234 char buf[32];
2235 snprintf(buf, sizeof(buf), "%s%d", base, label_id);
2236 LLVMSetValueName(LLVMBasicBlockAsValue(bb), buf);
2237 }
2238
2239 /* Append a basic block at the level of the parent flow.
2240 */
2241 static LLVMBasicBlockRef append_basic_block(struct ac_llvm_context *ctx,
2242 const char *name)
2243 {
2244 assert(ctx->flow_depth >= 1);
2245
2246 if (ctx->flow_depth >= 2) {
2247 struct ac_llvm_flow *flow = &ctx->flow[ctx->flow_depth - 2];
2248
2249 return LLVMInsertBasicBlockInContext(ctx->context,
2250 flow->next_block, name);
2251 }
2252
2253 LLVMValueRef main_fn =
2254 LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx->builder));
2255 return LLVMAppendBasicBlockInContext(ctx->context, main_fn, name);
2256 }
2257
2258 /* Emit a branch to the given default target for the current block if
2259 * applicable -- that is, if the current block does not already contain a
2260 * branch from a break or continue.
2261 */
2262 static void emit_default_branch(LLVMBuilderRef builder,
2263 LLVMBasicBlockRef target)
2264 {
2265 if (!LLVMGetBasicBlockTerminator(LLVMGetInsertBlock(builder)))
2266 LLVMBuildBr(builder, target);
2267 }
2268
2269 void ac_build_bgnloop(struct ac_llvm_context *ctx, int label_id)
2270 {
2271 struct ac_llvm_flow *flow = push_flow(ctx);
2272 flow->loop_entry_block = append_basic_block(ctx, "LOOP");
2273 flow->next_block = append_basic_block(ctx, "ENDLOOP");
2274 set_basicblock_name(flow->loop_entry_block, "loop", label_id);
2275 LLVMBuildBr(ctx->builder, flow->loop_entry_block);
2276 LLVMPositionBuilderAtEnd(ctx->builder, flow->loop_entry_block);
2277 }
2278
2279 void ac_build_break(struct ac_llvm_context *ctx)
2280 {
2281 struct ac_llvm_flow *flow = get_innermost_loop(ctx);
2282 LLVMBuildBr(ctx->builder, flow->next_block);
2283 }
2284
2285 void ac_build_continue(struct ac_llvm_context *ctx)
2286 {
2287 struct ac_llvm_flow *flow = get_innermost_loop(ctx);
2288 LLVMBuildBr(ctx->builder, flow->loop_entry_block);
2289 }
2290
2291 void ac_build_else(struct ac_llvm_context *ctx, int label_id)
2292 {
2293 struct ac_llvm_flow *current_branch = get_current_flow(ctx);
2294 LLVMBasicBlockRef endif_block;
2295
2296 assert(!current_branch->loop_entry_block);
2297
2298 endif_block = append_basic_block(ctx, "ENDIF");
2299 emit_default_branch(ctx->builder, endif_block);
2300
2301 LLVMPositionBuilderAtEnd(ctx->builder, current_branch->next_block);
2302 set_basicblock_name(current_branch->next_block, "else", label_id);
2303
2304 current_branch->next_block = endif_block;
2305 }
2306
2307 void ac_build_endif(struct ac_llvm_context *ctx, int label_id)
2308 {
2309 struct ac_llvm_flow *current_branch = get_current_flow(ctx);
2310
2311 assert(!current_branch->loop_entry_block);
2312
2313 emit_default_branch(ctx->builder, current_branch->next_block);
2314 LLVMPositionBuilderAtEnd(ctx->builder, current_branch->next_block);
2315 set_basicblock_name(current_branch->next_block, "endif", label_id);
2316
2317 ctx->flow_depth--;
2318 }
2319
2320 void ac_build_endloop(struct ac_llvm_context *ctx, int label_id)
2321 {
2322 struct ac_llvm_flow *current_loop = get_current_flow(ctx);
2323
2324 assert(current_loop->loop_entry_block);
2325
2326 emit_default_branch(ctx->builder, current_loop->loop_entry_block);
2327
2328 LLVMPositionBuilderAtEnd(ctx->builder, current_loop->next_block);
2329 set_basicblock_name(current_loop->next_block, "endloop", label_id);
2330 ctx->flow_depth--;
2331 }
2332
2333 static void if_cond_emit(struct ac_llvm_context *ctx, LLVMValueRef cond,
2334 int label_id)
2335 {
2336 struct ac_llvm_flow *flow = push_flow(ctx);
2337 LLVMBasicBlockRef if_block;
2338
2339 if_block = append_basic_block(ctx, "IF");
2340 flow->next_block = append_basic_block(ctx, "ELSE");
2341 set_basicblock_name(if_block, "if", label_id);
2342 LLVMBuildCondBr(ctx->builder, cond, if_block, flow->next_block);
2343 LLVMPositionBuilderAtEnd(ctx->builder, if_block);
2344 }
2345
2346 void ac_build_if(struct ac_llvm_context *ctx, LLVMValueRef value,
2347 int label_id)
2348 {
2349 LLVMValueRef cond = LLVMBuildFCmp(ctx->builder, LLVMRealUNE,
2350 value, ctx->f32_0, "");
2351 if_cond_emit(ctx, cond, label_id);
2352 }
2353
2354 void ac_build_uif(struct ac_llvm_context *ctx, LLVMValueRef value,
2355 int label_id)
2356 {
2357 LLVMValueRef cond = LLVMBuildICmp(ctx->builder, LLVMIntNE,
2358 ac_to_integer(ctx, value),
2359 ctx->i32_0, "");
2360 if_cond_emit(ctx, cond, label_id);
2361 }
2362
2363 LLVMValueRef ac_build_alloca(struct ac_llvm_context *ac, LLVMTypeRef type,
2364 const char *name)
2365 {
2366 LLVMBuilderRef builder = ac->builder;
2367 LLVMBasicBlockRef current_block = LLVMGetInsertBlock(builder);
2368 LLVMValueRef function = LLVMGetBasicBlockParent(current_block);
2369 LLVMBasicBlockRef first_block = LLVMGetEntryBasicBlock(function);
2370 LLVMValueRef first_instr = LLVMGetFirstInstruction(first_block);
2371 LLVMBuilderRef first_builder = LLVMCreateBuilderInContext(ac->context);
2372 LLVMValueRef res;
2373
2374 if (first_instr) {
2375 LLVMPositionBuilderBefore(first_builder, first_instr);
2376 } else {
2377 LLVMPositionBuilderAtEnd(first_builder, first_block);
2378 }
2379
2380 res = LLVMBuildAlloca(first_builder, type, name);
2381 LLVMBuildStore(builder, LLVMConstNull(type), res);
2382
2383 LLVMDisposeBuilder(first_builder);
2384
2385 return res;
2386 }
2387
2388 LLVMValueRef ac_build_alloca_undef(struct ac_llvm_context *ac,
2389 LLVMTypeRef type, const char *name)
2390 {
2391 LLVMValueRef ptr = ac_build_alloca(ac, type, name);
2392 LLVMBuildStore(ac->builder, LLVMGetUndef(type), ptr);
2393 return ptr;
2394 }
2395
2396 LLVMValueRef ac_cast_ptr(struct ac_llvm_context *ctx, LLVMValueRef ptr,
2397 LLVMTypeRef type)
2398 {
2399 int addr_space = LLVMGetPointerAddressSpace(LLVMTypeOf(ptr));
2400 return LLVMBuildBitCast(ctx->builder, ptr,
2401 LLVMPointerType(type, addr_space), "");
2402 }
2403
2404 LLVMValueRef ac_trim_vector(struct ac_llvm_context *ctx, LLVMValueRef value,
2405 unsigned count)
2406 {
2407 unsigned num_components = ac_get_llvm_num_components(value);
2408 if (count == num_components)
2409 return value;
2410
2411 LLVMValueRef masks[] = {
2412 LLVMConstInt(ctx->i32, 0, false), LLVMConstInt(ctx->i32, 1, false),
2413 LLVMConstInt(ctx->i32, 2, false), LLVMConstInt(ctx->i32, 3, false)};
2414
2415 if (count == 1)
2416 return LLVMBuildExtractElement(ctx->builder, value, masks[0],
2417 "");
2418
2419 LLVMValueRef swizzle = LLVMConstVector(masks, count);
2420 return LLVMBuildShuffleVector(ctx->builder, value, value, swizzle, "");
2421 }
2422
2423 LLVMValueRef ac_unpack_param(struct ac_llvm_context *ctx, LLVMValueRef param,
2424 unsigned rshift, unsigned bitwidth)
2425 {
2426 LLVMValueRef value = param;
2427 if (rshift)
2428 value = LLVMBuildLShr(ctx->builder, value,
2429 LLVMConstInt(ctx->i32, rshift, false), "");
2430
2431 if (rshift + bitwidth < 32) {
2432 unsigned mask = (1 << bitwidth) - 1;
2433 value = LLVMBuildAnd(ctx->builder, value,
2434 LLVMConstInt(ctx->i32, mask, false), "");
2435 }
2436 return value;
2437 }
2438
2439 /* Adjust the sample index according to FMASK.
2440 *
2441 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
2442 * which is the identity mapping. Each nibble says which physical sample
2443 * should be fetched to get that sample.
2444 *
2445 * For example, 0x11111100 means there are only 2 samples stored and
2446 * the second sample covers 3/4 of the pixel. When reading samples 0
2447 * and 1, return physical sample 0 (determined by the first two 0s
2448 * in FMASK), otherwise return physical sample 1.
2449 *
2450 * The sample index should be adjusted as follows:
2451 * addr[sample_index] = (fmask >> (addr[sample_index] * 4)) & 0xF;
2452 */
2453 void ac_apply_fmask_to_sample(struct ac_llvm_context *ac, LLVMValueRef fmask,
2454 LLVMValueRef *addr, bool is_array_tex)
2455 {
2456 struct ac_image_args fmask_load = {};
2457 fmask_load.opcode = ac_image_load;
2458 fmask_load.resource = fmask;
2459 fmask_load.dmask = 0xf;
2460 fmask_load.da = is_array_tex;
2461
2462 LLVMValueRef fmask_addr[4];
2463 memcpy(fmask_addr, addr, sizeof(fmask_addr[0]) * 3);
2464 fmask_addr[3] = LLVMGetUndef(ac->i32);
2465
2466 fmask_load.addr = ac_build_gather_values(ac, fmask_addr,
2467 is_array_tex ? 4 : 2);
2468
2469 LLVMValueRef fmask_value = ac_build_image_opcode(ac, &fmask_load);
2470 fmask_value = LLVMBuildExtractElement(ac->builder, fmask_value,
2471 ac->i32_0, "");
2472
2473 /* Apply the formula. */
2474 unsigned sample_chan = is_array_tex ? 3 : 2;
2475 LLVMValueRef final_sample;
2476 final_sample = LLVMBuildMul(ac->builder, addr[sample_chan],
2477 LLVMConstInt(ac->i32, 4, 0), "");
2478 final_sample = LLVMBuildLShr(ac->builder, fmask_value, final_sample, "");
2479 final_sample = LLVMBuildAnd(ac->builder, final_sample,
2480 LLVMConstInt(ac->i32, 0xF, 0), "");
2481
2482 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
2483 * resource descriptor is 0 (invalid),
2484 */
2485 LLVMValueRef tmp;
2486 tmp = LLVMBuildBitCast(ac->builder, fmask, ac->v8i32, "");
2487 tmp = LLVMBuildExtractElement(ac->builder, tmp, ac->i32_1, "");
2488 tmp = LLVMBuildICmp(ac->builder, LLVMIntNE, tmp, ac->i32_0, "");
2489
2490 /* Replace the MSAA sample index. */
2491 addr[sample_chan] = LLVMBuildSelect(ac->builder, tmp, final_sample,
2492 addr[sample_chan], "");
2493 }