44092a394878b52d0e858c5de7fb07d65e7f09f3
[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 #include <llvm/Config/llvm-config.h>
30
31 #include "c11/threads.h"
32
33 #include <assert.h>
34 #include <stdio.h>
35
36 #include "ac_llvm_util.h"
37 #include "ac_shader_util.h"
38 #include "ac_exp_param.h"
39 #include "util/bitscan.h"
40 #include "util/macros.h"
41 #include "util/u_atomic.h"
42 #include "util/u_math.h"
43 #include "sid.h"
44
45 #include "shader_enums.h"
46
47 #define AC_LLVM_INITIAL_CF_DEPTH 4
48
49 /* Data for if/else/endif and bgnloop/endloop control flow structures.
50 */
51 struct ac_llvm_flow {
52 /* Loop exit or next part of if/else/endif. */
53 LLVMBasicBlockRef next_block;
54 LLVMBasicBlockRef loop_entry_block;
55 };
56
57 /* Initialize module-independent parts of the context.
58 *
59 * The caller is responsible for initializing ctx::module and ctx::builder.
60 */
61 void
62 ac_llvm_context_init(struct ac_llvm_context *ctx,
63 struct ac_llvm_compiler *compiler,
64 enum chip_class chip_class, enum radeon_family family,
65 enum ac_float_mode float_mode, unsigned wave_size,
66 unsigned ballot_mask_bits)
67 {
68 LLVMValueRef args[1];
69
70 ctx->context = LLVMContextCreate();
71
72 ctx->chip_class = chip_class;
73 ctx->family = family;
74 ctx->wave_size = wave_size;
75 ctx->ballot_mask_bits = ballot_mask_bits;
76 ctx->module = ac_create_module(wave_size == 32 ? compiler->tm_wave32
77 : compiler->tm,
78 ctx->context);
79 ctx->builder = ac_create_builder(ctx->context, float_mode);
80
81 ctx->voidt = LLVMVoidTypeInContext(ctx->context);
82 ctx->i1 = LLVMInt1TypeInContext(ctx->context);
83 ctx->i8 = LLVMInt8TypeInContext(ctx->context);
84 ctx->i16 = LLVMIntTypeInContext(ctx->context, 16);
85 ctx->i32 = LLVMIntTypeInContext(ctx->context, 32);
86 ctx->i64 = LLVMIntTypeInContext(ctx->context, 64);
87 ctx->intptr = ctx->i32;
88 ctx->f16 = LLVMHalfTypeInContext(ctx->context);
89 ctx->f32 = LLVMFloatTypeInContext(ctx->context);
90 ctx->f64 = LLVMDoubleTypeInContext(ctx->context);
91 ctx->v2i16 = LLVMVectorType(ctx->i16, 2);
92 ctx->v2i32 = LLVMVectorType(ctx->i32, 2);
93 ctx->v3i32 = LLVMVectorType(ctx->i32, 3);
94 ctx->v4i32 = LLVMVectorType(ctx->i32, 4);
95 ctx->v2f32 = LLVMVectorType(ctx->f32, 2);
96 ctx->v3f32 = LLVMVectorType(ctx->f32, 3);
97 ctx->v4f32 = LLVMVectorType(ctx->f32, 4);
98 ctx->v8i32 = LLVMVectorType(ctx->i32, 8);
99 ctx->iN_wavemask = LLVMIntTypeInContext(ctx->context, ctx->wave_size);
100 ctx->iN_ballotmask = LLVMIntTypeInContext(ctx->context, ballot_mask_bits);
101
102 ctx->i8_0 = LLVMConstInt(ctx->i8, 0, false);
103 ctx->i8_1 = LLVMConstInt(ctx->i8, 1, false);
104 ctx->i16_0 = LLVMConstInt(ctx->i16, 0, false);
105 ctx->i16_1 = LLVMConstInt(ctx->i16, 1, false);
106 ctx->i32_0 = LLVMConstInt(ctx->i32, 0, false);
107 ctx->i32_1 = LLVMConstInt(ctx->i32, 1, false);
108 ctx->i64_0 = LLVMConstInt(ctx->i64, 0, false);
109 ctx->i64_1 = LLVMConstInt(ctx->i64, 1, false);
110 ctx->f16_0 = LLVMConstReal(ctx->f16, 0.0);
111 ctx->f16_1 = LLVMConstReal(ctx->f16, 1.0);
112 ctx->f32_0 = LLVMConstReal(ctx->f32, 0.0);
113 ctx->f32_1 = LLVMConstReal(ctx->f32, 1.0);
114 ctx->f64_0 = LLVMConstReal(ctx->f64, 0.0);
115 ctx->f64_1 = LLVMConstReal(ctx->f64, 1.0);
116
117 ctx->i1false = LLVMConstInt(ctx->i1, 0, false);
118 ctx->i1true = LLVMConstInt(ctx->i1, 1, false);
119
120 ctx->range_md_kind = LLVMGetMDKindIDInContext(ctx->context,
121 "range", 5);
122
123 ctx->invariant_load_md_kind = LLVMGetMDKindIDInContext(ctx->context,
124 "invariant.load", 14);
125
126 ctx->fpmath_md_kind = LLVMGetMDKindIDInContext(ctx->context, "fpmath", 6);
127
128 args[0] = LLVMConstReal(ctx->f32, 2.5);
129 ctx->fpmath_md_2p5_ulp = LLVMMDNodeInContext(ctx->context, args, 1);
130
131 ctx->uniform_md_kind = LLVMGetMDKindIDInContext(ctx->context,
132 "amdgpu.uniform", 14);
133
134 ctx->empty_md = LLVMMDNodeInContext(ctx->context, NULL, 0);
135 ctx->flow = calloc(1, sizeof(*ctx->flow));
136 }
137
138 void
139 ac_llvm_context_dispose(struct ac_llvm_context *ctx)
140 {
141 free(ctx->flow->stack);
142 free(ctx->flow);
143 ctx->flow = NULL;
144 }
145
146 int
147 ac_get_llvm_num_components(LLVMValueRef value)
148 {
149 LLVMTypeRef type = LLVMTypeOf(value);
150 unsigned num_components = LLVMGetTypeKind(type) == LLVMVectorTypeKind
151 ? LLVMGetVectorSize(type)
152 : 1;
153 return num_components;
154 }
155
156 LLVMValueRef
157 ac_llvm_extract_elem(struct ac_llvm_context *ac,
158 LLVMValueRef value,
159 int index)
160 {
161 if (LLVMGetTypeKind(LLVMTypeOf(value)) != LLVMVectorTypeKind) {
162 assert(index == 0);
163 return value;
164 }
165
166 return LLVMBuildExtractElement(ac->builder, value,
167 LLVMConstInt(ac->i32, index, false), "");
168 }
169
170 int
171 ac_get_elem_bits(struct ac_llvm_context *ctx, LLVMTypeRef type)
172 {
173 if (LLVMGetTypeKind(type) == LLVMVectorTypeKind)
174 type = LLVMGetElementType(type);
175
176 if (LLVMGetTypeKind(type) == LLVMIntegerTypeKind)
177 return LLVMGetIntTypeWidth(type);
178
179 if (type == ctx->f16)
180 return 16;
181 if (type == ctx->f32)
182 return 32;
183 if (type == ctx->f64)
184 return 64;
185
186 unreachable("Unhandled type kind in get_elem_bits");
187 }
188
189 unsigned
190 ac_get_type_size(LLVMTypeRef type)
191 {
192 LLVMTypeKind kind = LLVMGetTypeKind(type);
193
194 switch (kind) {
195 case LLVMIntegerTypeKind:
196 return LLVMGetIntTypeWidth(type) / 8;
197 case LLVMHalfTypeKind:
198 return 2;
199 case LLVMFloatTypeKind:
200 return 4;
201 case LLVMDoubleTypeKind:
202 return 8;
203 case LLVMPointerTypeKind:
204 if (LLVMGetPointerAddressSpace(type) == AC_ADDR_SPACE_CONST_32BIT)
205 return 4;
206 return 8;
207 case LLVMVectorTypeKind:
208 return LLVMGetVectorSize(type) *
209 ac_get_type_size(LLVMGetElementType(type));
210 case LLVMArrayTypeKind:
211 return LLVMGetArrayLength(type) *
212 ac_get_type_size(LLVMGetElementType(type));
213 default:
214 assert(0);
215 return 0;
216 }
217 }
218
219 static LLVMTypeRef to_integer_type_scalar(struct ac_llvm_context *ctx, LLVMTypeRef t)
220 {
221 if (t == ctx->i8)
222 return ctx->i8;
223 else if (t == ctx->f16 || t == ctx->i16)
224 return ctx->i16;
225 else if (t == ctx->f32 || t == ctx->i32)
226 return ctx->i32;
227 else if (t == ctx->f64 || t == ctx->i64)
228 return ctx->i64;
229 else
230 unreachable("Unhandled integer size");
231 }
232
233 LLVMTypeRef
234 ac_to_integer_type(struct ac_llvm_context *ctx, LLVMTypeRef t)
235 {
236 if (LLVMGetTypeKind(t) == LLVMVectorTypeKind) {
237 LLVMTypeRef elem_type = LLVMGetElementType(t);
238 return LLVMVectorType(to_integer_type_scalar(ctx, elem_type),
239 LLVMGetVectorSize(t));
240 }
241 if (LLVMGetTypeKind(t) == LLVMPointerTypeKind) {
242 switch (LLVMGetPointerAddressSpace(t)) {
243 case AC_ADDR_SPACE_GLOBAL:
244 return ctx->i64;
245 case AC_ADDR_SPACE_LDS:
246 return ctx->i32;
247 default:
248 unreachable("unhandled address space");
249 }
250 }
251 return to_integer_type_scalar(ctx, t);
252 }
253
254 LLVMValueRef
255 ac_to_integer(struct ac_llvm_context *ctx, LLVMValueRef v)
256 {
257 LLVMTypeRef type = LLVMTypeOf(v);
258 if (LLVMGetTypeKind(type) == LLVMPointerTypeKind) {
259 return LLVMBuildPtrToInt(ctx->builder, v, ac_to_integer_type(ctx, type), "");
260 }
261 return LLVMBuildBitCast(ctx->builder, v, ac_to_integer_type(ctx, type), "");
262 }
263
264 LLVMValueRef
265 ac_to_integer_or_pointer(struct ac_llvm_context *ctx, LLVMValueRef v)
266 {
267 LLVMTypeRef type = LLVMTypeOf(v);
268 if (LLVMGetTypeKind(type) == LLVMPointerTypeKind)
269 return v;
270 return ac_to_integer(ctx, v);
271 }
272
273 static LLVMTypeRef to_float_type_scalar(struct ac_llvm_context *ctx, LLVMTypeRef t)
274 {
275 if (t == ctx->i8)
276 return ctx->i8;
277 else if (t == ctx->i16 || t == ctx->f16)
278 return ctx->f16;
279 else if (t == ctx->i32 || t == ctx->f32)
280 return ctx->f32;
281 else if (t == ctx->i64 || t == ctx->f64)
282 return ctx->f64;
283 else
284 unreachable("Unhandled float size");
285 }
286
287 LLVMTypeRef
288 ac_to_float_type(struct ac_llvm_context *ctx, LLVMTypeRef t)
289 {
290 if (LLVMGetTypeKind(t) == LLVMVectorTypeKind) {
291 LLVMTypeRef elem_type = LLVMGetElementType(t);
292 return LLVMVectorType(to_float_type_scalar(ctx, elem_type),
293 LLVMGetVectorSize(t));
294 }
295 return to_float_type_scalar(ctx, t);
296 }
297
298 LLVMValueRef
299 ac_to_float(struct ac_llvm_context *ctx, LLVMValueRef v)
300 {
301 LLVMTypeRef type = LLVMTypeOf(v);
302 return LLVMBuildBitCast(ctx->builder, v, ac_to_float_type(ctx, type), "");
303 }
304
305
306 LLVMValueRef
307 ac_build_intrinsic(struct ac_llvm_context *ctx, const char *name,
308 LLVMTypeRef return_type, LLVMValueRef *params,
309 unsigned param_count, unsigned attrib_mask)
310 {
311 LLVMValueRef function, call;
312 bool set_callsite_attrs = !(attrib_mask & AC_FUNC_ATTR_LEGACY);
313
314 function = LLVMGetNamedFunction(ctx->module, name);
315 if (!function) {
316 LLVMTypeRef param_types[32], function_type;
317 unsigned i;
318
319 assert(param_count <= 32);
320
321 for (i = 0; i < param_count; ++i) {
322 assert(params[i]);
323 param_types[i] = LLVMTypeOf(params[i]);
324 }
325 function_type =
326 LLVMFunctionType(return_type, param_types, param_count, 0);
327 function = LLVMAddFunction(ctx->module, name, function_type);
328
329 LLVMSetFunctionCallConv(function, LLVMCCallConv);
330 LLVMSetLinkage(function, LLVMExternalLinkage);
331
332 if (!set_callsite_attrs)
333 ac_add_func_attributes(ctx->context, function, attrib_mask);
334 }
335
336 call = LLVMBuildCall(ctx->builder, function, params, param_count, "");
337 if (set_callsite_attrs)
338 ac_add_func_attributes(ctx->context, call, attrib_mask);
339 return call;
340 }
341
342 /**
343 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
344 * intrinsic names).
345 */
346 void ac_build_type_name_for_intr(LLVMTypeRef type, char *buf, unsigned bufsize)
347 {
348 LLVMTypeRef elem_type = type;
349
350 assert(bufsize >= 8);
351
352 if (LLVMGetTypeKind(type) == LLVMVectorTypeKind) {
353 int ret = snprintf(buf, bufsize, "v%u",
354 LLVMGetVectorSize(type));
355 if (ret < 0) {
356 char *type_name = LLVMPrintTypeToString(type);
357 fprintf(stderr, "Error building type name for: %s\n",
358 type_name);
359 LLVMDisposeMessage(type_name);
360 return;
361 }
362 elem_type = LLVMGetElementType(type);
363 buf += ret;
364 bufsize -= ret;
365 }
366 switch (LLVMGetTypeKind(elem_type)) {
367 default: break;
368 case LLVMIntegerTypeKind:
369 snprintf(buf, bufsize, "i%d", LLVMGetIntTypeWidth(elem_type));
370 break;
371 case LLVMHalfTypeKind:
372 snprintf(buf, bufsize, "f16");
373 break;
374 case LLVMFloatTypeKind:
375 snprintf(buf, bufsize, "f32");
376 break;
377 case LLVMDoubleTypeKind:
378 snprintf(buf, bufsize, "f64");
379 break;
380 }
381 }
382
383 /**
384 * Helper function that builds an LLVM IR PHI node and immediately adds
385 * incoming edges.
386 */
387 LLVMValueRef
388 ac_build_phi(struct ac_llvm_context *ctx, LLVMTypeRef type,
389 unsigned count_incoming, LLVMValueRef *values,
390 LLVMBasicBlockRef *blocks)
391 {
392 LLVMValueRef phi = LLVMBuildPhi(ctx->builder, type, "");
393 LLVMAddIncoming(phi, values, blocks, count_incoming);
394 return phi;
395 }
396
397 void ac_build_s_barrier(struct ac_llvm_context *ctx)
398 {
399 ac_build_intrinsic(ctx, "llvm.amdgcn.s.barrier", ctx->voidt, NULL,
400 0, AC_FUNC_ATTR_CONVERGENT);
401 }
402
403 /* Prevent optimizations (at least of memory accesses) across the current
404 * point in the program by emitting empty inline assembly that is marked as
405 * having side effects.
406 *
407 * Optionally, a value can be passed through the inline assembly to prevent
408 * LLVM from hoisting calls to ReadNone functions.
409 */
410 void
411 ac_build_optimization_barrier(struct ac_llvm_context *ctx,
412 LLVMValueRef *pvgpr)
413 {
414 static int counter = 0;
415
416 LLVMBuilderRef builder = ctx->builder;
417 char code[16];
418
419 snprintf(code, sizeof(code), "; %d", p_atomic_inc_return(&counter));
420
421 if (!pvgpr) {
422 LLVMTypeRef ftype = LLVMFunctionType(ctx->voidt, NULL, 0, false);
423 LLVMValueRef inlineasm = LLVMConstInlineAsm(ftype, code, "", true, false);
424 LLVMBuildCall(builder, inlineasm, NULL, 0, "");
425 } else {
426 LLVMTypeRef ftype = LLVMFunctionType(ctx->i32, &ctx->i32, 1, false);
427 LLVMValueRef inlineasm = LLVMConstInlineAsm(ftype, code, "=v,0", true, false);
428 LLVMValueRef vgpr = *pvgpr;
429 LLVMTypeRef vgpr_type = LLVMTypeOf(vgpr);
430 unsigned vgpr_size = ac_get_type_size(vgpr_type);
431 LLVMValueRef vgpr0;
432
433 assert(vgpr_size % 4 == 0);
434
435 vgpr = LLVMBuildBitCast(builder, vgpr, LLVMVectorType(ctx->i32, vgpr_size / 4), "");
436 vgpr0 = LLVMBuildExtractElement(builder, vgpr, ctx->i32_0, "");
437 vgpr0 = LLVMBuildCall(builder, inlineasm, &vgpr0, 1, "");
438 vgpr = LLVMBuildInsertElement(builder, vgpr, vgpr0, ctx->i32_0, "");
439 vgpr = LLVMBuildBitCast(builder, vgpr, vgpr_type, "");
440
441 *pvgpr = vgpr;
442 }
443 }
444
445 LLVMValueRef
446 ac_build_shader_clock(struct ac_llvm_context *ctx)
447 {
448 const char *intr = LLVM_VERSION_MAJOR >= 9 && ctx->chip_class >= GFX8 ?
449 "llvm.amdgcn.s.memrealtime" : "llvm.readcyclecounter";
450 LLVMValueRef tmp = ac_build_intrinsic(ctx, intr, ctx->i64, NULL, 0, 0);
451 return LLVMBuildBitCast(ctx->builder, tmp, ctx->v2i32, "");
452 }
453
454 LLVMValueRef
455 ac_build_ballot(struct ac_llvm_context *ctx,
456 LLVMValueRef value)
457 {
458 const char *name;
459
460 if (LLVM_VERSION_MAJOR >= 9) {
461 if (ctx->wave_size == 64)
462 name = "llvm.amdgcn.icmp.i64.i32";
463 else
464 name = "llvm.amdgcn.icmp.i32.i32";
465 } else {
466 name = "llvm.amdgcn.icmp.i32";
467 }
468 LLVMValueRef args[3] = {
469 value,
470 ctx->i32_0,
471 LLVMConstInt(ctx->i32, LLVMIntNE, 0)
472 };
473
474 /* We currently have no other way to prevent LLVM from lifting the icmp
475 * calls to a dominating basic block.
476 */
477 ac_build_optimization_barrier(ctx, &args[0]);
478
479 args[0] = ac_to_integer(ctx, args[0]);
480
481 return ac_build_intrinsic(ctx, name, ctx->iN_wavemask, args, 3,
482 AC_FUNC_ATTR_NOUNWIND |
483 AC_FUNC_ATTR_READNONE |
484 AC_FUNC_ATTR_CONVERGENT);
485 }
486
487 LLVMValueRef ac_get_i1_sgpr_mask(struct ac_llvm_context *ctx,
488 LLVMValueRef value)
489 {
490 const char *name = LLVM_VERSION_MAJOR >= 9 ? "llvm.amdgcn.icmp.i64.i1" : "llvm.amdgcn.icmp.i1";
491 LLVMValueRef args[3] = {
492 value,
493 ctx->i1false,
494 LLVMConstInt(ctx->i32, LLVMIntNE, 0),
495 };
496
497 return ac_build_intrinsic(ctx, name, ctx->i64, args, 3,
498 AC_FUNC_ATTR_NOUNWIND |
499 AC_FUNC_ATTR_READNONE |
500 AC_FUNC_ATTR_CONVERGENT);
501 }
502
503 LLVMValueRef
504 ac_build_vote_all(struct ac_llvm_context *ctx, LLVMValueRef value)
505 {
506 LLVMValueRef active_set = ac_build_ballot(ctx, ctx->i32_1);
507 LLVMValueRef vote_set = ac_build_ballot(ctx, value);
508 return LLVMBuildICmp(ctx->builder, LLVMIntEQ, vote_set, active_set, "");
509 }
510
511 LLVMValueRef
512 ac_build_vote_any(struct ac_llvm_context *ctx, LLVMValueRef value)
513 {
514 LLVMValueRef vote_set = ac_build_ballot(ctx, value);
515 return LLVMBuildICmp(ctx->builder, LLVMIntNE, vote_set,
516 LLVMConstInt(ctx->iN_wavemask, 0, 0), "");
517 }
518
519 LLVMValueRef
520 ac_build_vote_eq(struct ac_llvm_context *ctx, LLVMValueRef value)
521 {
522 LLVMValueRef active_set = ac_build_ballot(ctx, ctx->i32_1);
523 LLVMValueRef vote_set = ac_build_ballot(ctx, value);
524
525 LLVMValueRef all = LLVMBuildICmp(ctx->builder, LLVMIntEQ,
526 vote_set, active_set, "");
527 LLVMValueRef none = LLVMBuildICmp(ctx->builder, LLVMIntEQ,
528 vote_set,
529 LLVMConstInt(ctx->iN_wavemask, 0, 0), "");
530 return LLVMBuildOr(ctx->builder, all, none, "");
531 }
532
533 LLVMValueRef
534 ac_build_varying_gather_values(struct ac_llvm_context *ctx, LLVMValueRef *values,
535 unsigned value_count, unsigned component)
536 {
537 LLVMValueRef vec = NULL;
538
539 if (value_count == 1) {
540 return values[component];
541 } else if (!value_count)
542 unreachable("value_count is 0");
543
544 for (unsigned i = component; i < value_count + component; i++) {
545 LLVMValueRef value = values[i];
546
547 if (i == component)
548 vec = LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value), value_count));
549 LLVMValueRef index = LLVMConstInt(ctx->i32, i - component, false);
550 vec = LLVMBuildInsertElement(ctx->builder, vec, value, index, "");
551 }
552 return vec;
553 }
554
555 LLVMValueRef
556 ac_build_gather_values_extended(struct ac_llvm_context *ctx,
557 LLVMValueRef *values,
558 unsigned value_count,
559 unsigned value_stride,
560 bool load,
561 bool always_vector)
562 {
563 LLVMBuilderRef builder = ctx->builder;
564 LLVMValueRef vec = NULL;
565 unsigned i;
566
567 if (value_count == 1 && !always_vector) {
568 if (load)
569 return LLVMBuildLoad(builder, values[0], "");
570 return values[0];
571 } else if (!value_count)
572 unreachable("value_count is 0");
573
574 for (i = 0; i < value_count; i++) {
575 LLVMValueRef value = values[i * value_stride];
576 if (load)
577 value = LLVMBuildLoad(builder, value, "");
578
579 if (!i)
580 vec = LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value), value_count));
581 LLVMValueRef index = LLVMConstInt(ctx->i32, i, false);
582 vec = LLVMBuildInsertElement(builder, vec, value, index, "");
583 }
584 return vec;
585 }
586
587 LLVMValueRef
588 ac_build_gather_values(struct ac_llvm_context *ctx,
589 LLVMValueRef *values,
590 unsigned value_count)
591 {
592 return ac_build_gather_values_extended(ctx, values, value_count, 1, false, false);
593 }
594
595 /* Expand a scalar or vector to <dst_channels x type> by filling the remaining
596 * channels with undef. Extract at most src_channels components from the input.
597 */
598 static LLVMValueRef
599 ac_build_expand(struct ac_llvm_context *ctx,
600 LLVMValueRef value,
601 unsigned src_channels,
602 unsigned dst_channels)
603 {
604 LLVMTypeRef elemtype;
605 LLVMValueRef chan[dst_channels];
606
607 if (LLVMGetTypeKind(LLVMTypeOf(value)) == LLVMVectorTypeKind) {
608 unsigned vec_size = LLVMGetVectorSize(LLVMTypeOf(value));
609
610 if (src_channels == dst_channels && vec_size == dst_channels)
611 return value;
612
613 src_channels = MIN2(src_channels, vec_size);
614
615 for (unsigned i = 0; i < src_channels; i++)
616 chan[i] = ac_llvm_extract_elem(ctx, value, i);
617
618 elemtype = LLVMGetElementType(LLVMTypeOf(value));
619 } else {
620 if (src_channels) {
621 assert(src_channels == 1);
622 chan[0] = value;
623 }
624 elemtype = LLVMTypeOf(value);
625 }
626
627 for (unsigned i = src_channels; i < dst_channels; i++)
628 chan[i] = LLVMGetUndef(elemtype);
629
630 return ac_build_gather_values(ctx, chan, dst_channels);
631 }
632
633 /* Extract components [start, start + channels) from a vector.
634 */
635 LLVMValueRef
636 ac_extract_components(struct ac_llvm_context *ctx,
637 LLVMValueRef value,
638 unsigned start,
639 unsigned channels)
640 {
641 LLVMValueRef chan[channels];
642
643 for (unsigned i = 0; i < channels; i++)
644 chan[i] = ac_llvm_extract_elem(ctx, value, i + start);
645
646 return ac_build_gather_values(ctx, chan, channels);
647 }
648
649 /* Expand a scalar or vector to <4 x type> by filling the remaining channels
650 * with undef. Extract at most num_channels components from the input.
651 */
652 LLVMValueRef ac_build_expand_to_vec4(struct ac_llvm_context *ctx,
653 LLVMValueRef value,
654 unsigned num_channels)
655 {
656 return ac_build_expand(ctx, value, num_channels, 4);
657 }
658
659 LLVMValueRef ac_build_round(struct ac_llvm_context *ctx, LLVMValueRef value)
660 {
661 unsigned type_size = ac_get_type_size(LLVMTypeOf(value));
662 const char *name;
663
664 if (type_size == 2)
665 name = "llvm.rint.f16";
666 else if (type_size == 4)
667 name = "llvm.rint.f32";
668 else
669 name = "llvm.rint.f64";
670
671 return ac_build_intrinsic(ctx, name, LLVMTypeOf(value), &value, 1,
672 AC_FUNC_ATTR_READNONE);
673 }
674
675 LLVMValueRef
676 ac_build_fdiv(struct ac_llvm_context *ctx,
677 LLVMValueRef num,
678 LLVMValueRef den)
679 {
680 /* If we do (num / den), LLVM >= 7.0 does:
681 * return num * v_rcp_f32(den * (fabs(den) > 0x1.0p+96f ? 0x1.0p-32f : 1.0f));
682 *
683 * If we do (num * (1 / den)), LLVM does:
684 * return num * v_rcp_f32(den);
685 */
686 LLVMValueRef one = LLVMConstReal(LLVMTypeOf(num), 1.0);
687 LLVMValueRef rcp = LLVMBuildFDiv(ctx->builder, one, den, "");
688 LLVMValueRef ret = LLVMBuildFMul(ctx->builder, num, rcp, "");
689
690 /* Use v_rcp_f32 instead of precise division. */
691 if (!LLVMIsConstant(ret))
692 LLVMSetMetadata(ret, ctx->fpmath_md_kind, ctx->fpmath_md_2p5_ulp);
693 return ret;
694 }
695
696 /* See fast_idiv_by_const.h. */
697 /* Set: increment = util_fast_udiv_info::increment ? multiplier : 0; */
698 LLVMValueRef ac_build_fast_udiv(struct ac_llvm_context *ctx,
699 LLVMValueRef num,
700 LLVMValueRef multiplier,
701 LLVMValueRef pre_shift,
702 LLVMValueRef post_shift,
703 LLVMValueRef increment)
704 {
705 LLVMBuilderRef builder = ctx->builder;
706
707 num = LLVMBuildLShr(builder, num, pre_shift, "");
708 num = LLVMBuildMul(builder,
709 LLVMBuildZExt(builder, num, ctx->i64, ""),
710 LLVMBuildZExt(builder, multiplier, ctx->i64, ""), "");
711 num = LLVMBuildAdd(builder, num,
712 LLVMBuildZExt(builder, increment, ctx->i64, ""), "");
713 num = LLVMBuildLShr(builder, num, LLVMConstInt(ctx->i64, 32, 0), "");
714 num = LLVMBuildTrunc(builder, num, ctx->i32, "");
715 return LLVMBuildLShr(builder, num, post_shift, "");
716 }
717
718 /* See fast_idiv_by_const.h. */
719 /* If num != UINT_MAX, this more efficient version can be used. */
720 /* Set: increment = util_fast_udiv_info::increment; */
721 LLVMValueRef ac_build_fast_udiv_nuw(struct ac_llvm_context *ctx,
722 LLVMValueRef num,
723 LLVMValueRef multiplier,
724 LLVMValueRef pre_shift,
725 LLVMValueRef post_shift,
726 LLVMValueRef increment)
727 {
728 LLVMBuilderRef builder = ctx->builder;
729
730 num = LLVMBuildLShr(builder, num, pre_shift, "");
731 num = LLVMBuildNUWAdd(builder, num, increment, "");
732 num = LLVMBuildMul(builder,
733 LLVMBuildZExt(builder, num, ctx->i64, ""),
734 LLVMBuildZExt(builder, multiplier, ctx->i64, ""), "");
735 num = LLVMBuildLShr(builder, num, LLVMConstInt(ctx->i64, 32, 0), "");
736 num = LLVMBuildTrunc(builder, num, ctx->i32, "");
737 return LLVMBuildLShr(builder, num, post_shift, "");
738 }
739
740 /* See fast_idiv_by_const.h. */
741 /* Both operands must fit in 31 bits and the divisor must not be 1. */
742 LLVMValueRef ac_build_fast_udiv_u31_d_not_one(struct ac_llvm_context *ctx,
743 LLVMValueRef num,
744 LLVMValueRef multiplier,
745 LLVMValueRef post_shift)
746 {
747 LLVMBuilderRef builder = ctx->builder;
748
749 num = LLVMBuildMul(builder,
750 LLVMBuildZExt(builder, num, ctx->i64, ""),
751 LLVMBuildZExt(builder, multiplier, ctx->i64, ""), "");
752 num = LLVMBuildLShr(builder, num, LLVMConstInt(ctx->i64, 32, 0), "");
753 num = LLVMBuildTrunc(builder, num, ctx->i32, "");
754 return LLVMBuildLShr(builder, num, post_shift, "");
755 }
756
757 /* Coordinates for cube map selection. sc, tc, and ma are as in Table 8.27
758 * of the OpenGL 4.5 (Compatibility Profile) specification, except ma is
759 * already multiplied by two. id is the cube face number.
760 */
761 struct cube_selection_coords {
762 LLVMValueRef stc[2];
763 LLVMValueRef ma;
764 LLVMValueRef id;
765 };
766
767 static void
768 build_cube_intrinsic(struct ac_llvm_context *ctx,
769 LLVMValueRef in[3],
770 struct cube_selection_coords *out)
771 {
772 LLVMTypeRef f32 = ctx->f32;
773
774 out->stc[1] = ac_build_intrinsic(ctx, "llvm.amdgcn.cubetc",
775 f32, in, 3, AC_FUNC_ATTR_READNONE);
776 out->stc[0] = ac_build_intrinsic(ctx, "llvm.amdgcn.cubesc",
777 f32, in, 3, AC_FUNC_ATTR_READNONE);
778 out->ma = ac_build_intrinsic(ctx, "llvm.amdgcn.cubema",
779 f32, in, 3, AC_FUNC_ATTR_READNONE);
780 out->id = ac_build_intrinsic(ctx, "llvm.amdgcn.cubeid",
781 f32, in, 3, AC_FUNC_ATTR_READNONE);
782 }
783
784 /**
785 * Build a manual selection sequence for cube face sc/tc coordinates and
786 * major axis vector (multiplied by 2 for consistency) for the given
787 * vec3 \p coords, for the face implied by \p selcoords.
788 *
789 * For the major axis, we always adjust the sign to be in the direction of
790 * selcoords.ma; i.e., a positive out_ma means that coords is pointed towards
791 * the selcoords major axis.
792 */
793 static void build_cube_select(struct ac_llvm_context *ctx,
794 const struct cube_selection_coords *selcoords,
795 const LLVMValueRef *coords,
796 LLVMValueRef *out_st,
797 LLVMValueRef *out_ma)
798 {
799 LLVMBuilderRef builder = ctx->builder;
800 LLVMTypeRef f32 = LLVMTypeOf(coords[0]);
801 LLVMValueRef is_ma_positive;
802 LLVMValueRef sgn_ma;
803 LLVMValueRef is_ma_z, is_not_ma_z;
804 LLVMValueRef is_ma_y;
805 LLVMValueRef is_ma_x;
806 LLVMValueRef sgn;
807 LLVMValueRef tmp;
808
809 is_ma_positive = LLVMBuildFCmp(builder, LLVMRealUGE,
810 selcoords->ma, LLVMConstReal(f32, 0.0), "");
811 sgn_ma = LLVMBuildSelect(builder, is_ma_positive,
812 LLVMConstReal(f32, 1.0), LLVMConstReal(f32, -1.0), "");
813
814 is_ma_z = LLVMBuildFCmp(builder, LLVMRealUGE, selcoords->id, LLVMConstReal(f32, 4.0), "");
815 is_not_ma_z = LLVMBuildNot(builder, is_ma_z, "");
816 is_ma_y = LLVMBuildAnd(builder, is_not_ma_z,
817 LLVMBuildFCmp(builder, LLVMRealUGE, selcoords->id, LLVMConstReal(f32, 2.0), ""), "");
818 is_ma_x = LLVMBuildAnd(builder, is_not_ma_z, LLVMBuildNot(builder, is_ma_y, ""), "");
819
820 /* Select sc */
821 tmp = LLVMBuildSelect(builder, is_ma_x, coords[2], coords[0], "");
822 sgn = LLVMBuildSelect(builder, is_ma_y, LLVMConstReal(f32, 1.0),
823 LLVMBuildSelect(builder, is_ma_z, sgn_ma,
824 LLVMBuildFNeg(builder, sgn_ma, ""), ""), "");
825 out_st[0] = LLVMBuildFMul(builder, tmp, sgn, "");
826
827 /* Select tc */
828 tmp = LLVMBuildSelect(builder, is_ma_y, coords[2], coords[1], "");
829 sgn = LLVMBuildSelect(builder, is_ma_y, sgn_ma,
830 LLVMConstReal(f32, -1.0), "");
831 out_st[1] = LLVMBuildFMul(builder, tmp, sgn, "");
832
833 /* Select ma */
834 tmp = LLVMBuildSelect(builder, is_ma_z, coords[2],
835 LLVMBuildSelect(builder, is_ma_y, coords[1], coords[0], ""), "");
836 tmp = ac_build_intrinsic(ctx, "llvm.fabs.f32",
837 ctx->f32, &tmp, 1, AC_FUNC_ATTR_READNONE);
838 *out_ma = LLVMBuildFMul(builder, tmp, LLVMConstReal(f32, 2.0), "");
839 }
840
841 void
842 ac_prepare_cube_coords(struct ac_llvm_context *ctx,
843 bool is_deriv, bool is_array, bool is_lod,
844 LLVMValueRef *coords_arg,
845 LLVMValueRef *derivs_arg)
846 {
847
848 LLVMBuilderRef builder = ctx->builder;
849 struct cube_selection_coords selcoords;
850 LLVMValueRef coords[3];
851 LLVMValueRef invma;
852
853 if (is_array && !is_lod) {
854 LLVMValueRef tmp = ac_build_round(ctx, coords_arg[3]);
855
856 /* Section 8.9 (Texture Functions) of the GLSL 4.50 spec says:
857 *
858 * "For Array forms, the array layer used will be
859 *
860 * max(0, min(d−1, floor(layer+0.5)))
861 *
862 * where d is the depth of the texture array and layer
863 * comes from the component indicated in the tables below.
864 * Workaroudn for an issue where the layer is taken from a
865 * helper invocation which happens to fall on a different
866 * layer due to extrapolation."
867 *
868 * GFX8 and earlier attempt to implement this in hardware by
869 * clamping the value of coords[2] = (8 * layer) + face.
870 * Unfortunately, this means that the we end up with the wrong
871 * face when clamping occurs.
872 *
873 * Clamp the layer earlier to work around the issue.
874 */
875 if (ctx->chip_class <= GFX8) {
876 LLVMValueRef ge0;
877 ge0 = LLVMBuildFCmp(builder, LLVMRealOGE, tmp, ctx->f32_0, "");
878 tmp = LLVMBuildSelect(builder, ge0, tmp, ctx->f32_0, "");
879 }
880
881 coords_arg[3] = tmp;
882 }
883
884 build_cube_intrinsic(ctx, coords_arg, &selcoords);
885
886 invma = ac_build_intrinsic(ctx, "llvm.fabs.f32",
887 ctx->f32, &selcoords.ma, 1, AC_FUNC_ATTR_READNONE);
888 invma = ac_build_fdiv(ctx, LLVMConstReal(ctx->f32, 1.0), invma);
889
890 for (int i = 0; i < 2; ++i)
891 coords[i] = LLVMBuildFMul(builder, selcoords.stc[i], invma, "");
892
893 coords[2] = selcoords.id;
894
895 if (is_deriv && derivs_arg) {
896 LLVMValueRef derivs[4];
897 int axis;
898
899 /* Convert cube derivatives to 2D derivatives. */
900 for (axis = 0; axis < 2; axis++) {
901 LLVMValueRef deriv_st[2];
902 LLVMValueRef deriv_ma;
903
904 /* Transform the derivative alongside the texture
905 * coordinate. Mathematically, the correct formula is
906 * as follows. Assume we're projecting onto the +Z face
907 * and denote by dx/dh the derivative of the (original)
908 * X texture coordinate with respect to horizontal
909 * window coordinates. The projection onto the +Z face
910 * plane is:
911 *
912 * f(x,z) = x/z
913 *
914 * Then df/dh = df/dx * dx/dh + df/dz * dz/dh
915 * = 1/z * dx/dh - x/z * 1/z * dz/dh.
916 *
917 * This motivatives the implementation below.
918 *
919 * Whether this actually gives the expected results for
920 * apps that might feed in derivatives obtained via
921 * finite differences is anyone's guess. The OpenGL spec
922 * seems awfully quiet about how textureGrad for cube
923 * maps should be handled.
924 */
925 build_cube_select(ctx, &selcoords, &derivs_arg[axis * 3],
926 deriv_st, &deriv_ma);
927
928 deriv_ma = LLVMBuildFMul(builder, deriv_ma, invma, "");
929
930 for (int i = 0; i < 2; ++i)
931 derivs[axis * 2 + i] =
932 LLVMBuildFSub(builder,
933 LLVMBuildFMul(builder, deriv_st[i], invma, ""),
934 LLVMBuildFMul(builder, deriv_ma, coords[i], ""), "");
935 }
936
937 memcpy(derivs_arg, derivs, sizeof(derivs));
938 }
939
940 /* Shift the texture coordinate. This must be applied after the
941 * derivative calculation.
942 */
943 for (int i = 0; i < 2; ++i)
944 coords[i] = LLVMBuildFAdd(builder, coords[i], LLVMConstReal(ctx->f32, 1.5), "");
945
946 if (is_array) {
947 /* for cube arrays coord.z = coord.w(array_index) * 8 + face */
948 /* coords_arg.w component - array_index for cube arrays */
949 coords[2] = ac_build_fmad(ctx, coords_arg[3], LLVMConstReal(ctx->f32, 8.0), coords[2]);
950 }
951
952 memcpy(coords_arg, coords, sizeof(coords));
953 }
954
955
956 LLVMValueRef
957 ac_build_fs_interp(struct ac_llvm_context *ctx,
958 LLVMValueRef llvm_chan,
959 LLVMValueRef attr_number,
960 LLVMValueRef params,
961 LLVMValueRef i,
962 LLVMValueRef j)
963 {
964 LLVMValueRef args[5];
965 LLVMValueRef p1;
966
967 args[0] = i;
968 args[1] = llvm_chan;
969 args[2] = attr_number;
970 args[3] = params;
971
972 p1 = ac_build_intrinsic(ctx, "llvm.amdgcn.interp.p1",
973 ctx->f32, args, 4, AC_FUNC_ATTR_READNONE);
974
975 args[0] = p1;
976 args[1] = j;
977 args[2] = llvm_chan;
978 args[3] = attr_number;
979 args[4] = params;
980
981 return ac_build_intrinsic(ctx, "llvm.amdgcn.interp.p2",
982 ctx->f32, args, 5, AC_FUNC_ATTR_READNONE);
983 }
984
985 LLVMValueRef
986 ac_build_fs_interp_f16(struct ac_llvm_context *ctx,
987 LLVMValueRef llvm_chan,
988 LLVMValueRef attr_number,
989 LLVMValueRef params,
990 LLVMValueRef i,
991 LLVMValueRef j)
992 {
993 LLVMValueRef args[6];
994 LLVMValueRef p1;
995
996 args[0] = i;
997 args[1] = llvm_chan;
998 args[2] = attr_number;
999 args[3] = ctx->i1false;
1000 args[4] = params;
1001
1002 p1 = ac_build_intrinsic(ctx, "llvm.amdgcn.interp.p1.f16",
1003 ctx->f32, args, 5, AC_FUNC_ATTR_READNONE);
1004
1005 args[0] = p1;
1006 args[1] = j;
1007 args[2] = llvm_chan;
1008 args[3] = attr_number;
1009 args[4] = ctx->i1false;
1010 args[5] = params;
1011
1012 return ac_build_intrinsic(ctx, "llvm.amdgcn.interp.p2.f16",
1013 ctx->f16, args, 6, AC_FUNC_ATTR_READNONE);
1014 }
1015
1016 LLVMValueRef
1017 ac_build_fs_interp_mov(struct ac_llvm_context *ctx,
1018 LLVMValueRef parameter,
1019 LLVMValueRef llvm_chan,
1020 LLVMValueRef attr_number,
1021 LLVMValueRef params)
1022 {
1023 LLVMValueRef args[4];
1024
1025 args[0] = parameter;
1026 args[1] = llvm_chan;
1027 args[2] = attr_number;
1028 args[3] = params;
1029
1030 return ac_build_intrinsic(ctx, "llvm.amdgcn.interp.mov",
1031 ctx->f32, args, 4, AC_FUNC_ATTR_READNONE);
1032 }
1033
1034 LLVMValueRef
1035 ac_build_gep_ptr(struct ac_llvm_context *ctx,
1036 LLVMValueRef base_ptr,
1037 LLVMValueRef index)
1038 {
1039 return LLVMBuildGEP(ctx->builder, base_ptr, &index, 1, "");
1040 }
1041
1042 LLVMValueRef
1043 ac_build_gep0(struct ac_llvm_context *ctx,
1044 LLVMValueRef base_ptr,
1045 LLVMValueRef index)
1046 {
1047 LLVMValueRef indices[2] = {
1048 ctx->i32_0,
1049 index,
1050 };
1051 return LLVMBuildGEP(ctx->builder, base_ptr, indices, 2, "");
1052 }
1053
1054 LLVMValueRef ac_build_pointer_add(struct ac_llvm_context *ctx, LLVMValueRef ptr,
1055 LLVMValueRef index)
1056 {
1057 return LLVMBuildPointerCast(ctx->builder,
1058 LLVMBuildGEP(ctx->builder, ptr, &index, 1, ""),
1059 LLVMTypeOf(ptr), "");
1060 }
1061
1062 void
1063 ac_build_indexed_store(struct ac_llvm_context *ctx,
1064 LLVMValueRef base_ptr, LLVMValueRef index,
1065 LLVMValueRef value)
1066 {
1067 LLVMBuildStore(ctx->builder, value,
1068 ac_build_gep0(ctx, base_ptr, index));
1069 }
1070
1071 /**
1072 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad.
1073 * It's equivalent to doing a load from &base_ptr[index].
1074 *
1075 * \param base_ptr Where the array starts.
1076 * \param index The element index into the array.
1077 * \param uniform Whether the base_ptr and index can be assumed to be
1078 * dynamically uniform (i.e. load to an SGPR)
1079 * \param invariant Whether the load is invariant (no other opcodes affect it)
1080 * \param no_unsigned_wraparound
1081 * For all possible re-associations and re-distributions of an expression
1082 * "base_ptr + index * elemsize" into "addr + offset" (excluding GEPs
1083 * without inbounds in base_ptr), this parameter is true if "addr + offset"
1084 * does not result in an unsigned integer wraparound. This is used for
1085 * optimal code generation of 32-bit pointer arithmetic.
1086 *
1087 * For example, a 32-bit immediate offset that causes a 32-bit unsigned
1088 * integer wraparound can't be an imm offset in s_load_dword, because
1089 * the instruction performs "addr + offset" in 64 bits.
1090 *
1091 * Expected usage for bindless textures by chaining GEPs:
1092 * // possible unsigned wraparound, don't use InBounds:
1093 * ptr1 = LLVMBuildGEP(base_ptr, index);
1094 * image = load(ptr1); // becomes "s_load ptr1, 0"
1095 *
1096 * ptr2 = LLVMBuildInBoundsGEP(ptr1, 32 / elemsize);
1097 * sampler = load(ptr2); // becomes "s_load ptr1, 32" thanks to InBounds
1098 */
1099 static LLVMValueRef
1100 ac_build_load_custom(struct ac_llvm_context *ctx, LLVMValueRef base_ptr,
1101 LLVMValueRef index, bool uniform, bool invariant,
1102 bool no_unsigned_wraparound)
1103 {
1104 LLVMValueRef pointer, result;
1105
1106 if (no_unsigned_wraparound &&
1107 LLVMGetPointerAddressSpace(LLVMTypeOf(base_ptr)) == AC_ADDR_SPACE_CONST_32BIT)
1108 pointer = LLVMBuildInBoundsGEP(ctx->builder, base_ptr, &index, 1, "");
1109 else
1110 pointer = LLVMBuildGEP(ctx->builder, base_ptr, &index, 1, "");
1111
1112 if (uniform)
1113 LLVMSetMetadata(pointer, ctx->uniform_md_kind, ctx->empty_md);
1114 result = LLVMBuildLoad(ctx->builder, pointer, "");
1115 if (invariant)
1116 LLVMSetMetadata(result, ctx->invariant_load_md_kind, ctx->empty_md);
1117 return result;
1118 }
1119
1120 LLVMValueRef ac_build_load(struct ac_llvm_context *ctx, LLVMValueRef base_ptr,
1121 LLVMValueRef index)
1122 {
1123 return ac_build_load_custom(ctx, base_ptr, index, false, false, false);
1124 }
1125
1126 LLVMValueRef ac_build_load_invariant(struct ac_llvm_context *ctx,
1127 LLVMValueRef base_ptr, LLVMValueRef index)
1128 {
1129 return ac_build_load_custom(ctx, base_ptr, index, false, true, false);
1130 }
1131
1132 /* This assumes that there is no unsigned integer wraparound during the address
1133 * computation, excluding all GEPs within base_ptr. */
1134 LLVMValueRef ac_build_load_to_sgpr(struct ac_llvm_context *ctx,
1135 LLVMValueRef base_ptr, LLVMValueRef index)
1136 {
1137 return ac_build_load_custom(ctx, base_ptr, index, true, true, true);
1138 }
1139
1140 /* See ac_build_load_custom() documentation. */
1141 LLVMValueRef ac_build_load_to_sgpr_uint_wraparound(struct ac_llvm_context *ctx,
1142 LLVMValueRef base_ptr, LLVMValueRef index)
1143 {
1144 return ac_build_load_custom(ctx, base_ptr, index, true, true, false);
1145 }
1146
1147 static unsigned get_load_cache_policy(struct ac_llvm_context *ctx,
1148 unsigned cache_policy)
1149 {
1150 return cache_policy |
1151 (ctx->chip_class >= GFX10 && cache_policy & ac_glc ? ac_dlc : 0);
1152 }
1153
1154 static void
1155 ac_build_buffer_store_common(struct ac_llvm_context *ctx,
1156 LLVMValueRef rsrc,
1157 LLVMValueRef data,
1158 LLVMValueRef vindex,
1159 LLVMValueRef voffset,
1160 LLVMValueRef soffset,
1161 unsigned num_channels,
1162 LLVMTypeRef return_channel_type,
1163 unsigned cache_policy,
1164 bool use_format,
1165 bool structurized)
1166 {
1167 LLVMValueRef args[6];
1168 int idx = 0;
1169 args[idx++] = data;
1170 args[idx++] = LLVMBuildBitCast(ctx->builder, rsrc, ctx->v4i32, "");
1171 if (structurized)
1172 args[idx++] = vindex ? vindex : ctx->i32_0;
1173 args[idx++] = voffset ? voffset : ctx->i32_0;
1174 args[idx++] = soffset ? soffset : ctx->i32_0;
1175 args[idx++] = LLVMConstInt(ctx->i32, cache_policy, 0);
1176 unsigned func = !ac_has_vec3_support(ctx->chip_class, use_format) && num_channels == 3 ? 4 : num_channels;
1177 const char *indexing_kind = structurized ? "struct" : "raw";
1178 char name[256], type_name[8];
1179
1180 LLVMTypeRef type = func > 1 ? LLVMVectorType(return_channel_type, func) : return_channel_type;
1181 ac_build_type_name_for_intr(type, type_name, sizeof(type_name));
1182
1183 if (use_format) {
1184 snprintf(name, sizeof(name), "llvm.amdgcn.%s.buffer.store.format.%s",
1185 indexing_kind, type_name);
1186 } else {
1187 snprintf(name, sizeof(name), "llvm.amdgcn.%s.buffer.store.%s",
1188 indexing_kind, type_name);
1189 }
1190
1191 ac_build_intrinsic(ctx, name, ctx->voidt, args, idx,
1192 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY);
1193 }
1194
1195 void
1196 ac_build_buffer_store_format(struct ac_llvm_context *ctx,
1197 LLVMValueRef rsrc,
1198 LLVMValueRef data,
1199 LLVMValueRef vindex,
1200 LLVMValueRef voffset,
1201 unsigned num_channels,
1202 unsigned cache_policy)
1203 {
1204 ac_build_buffer_store_common(ctx, rsrc, data, vindex,
1205 voffset, NULL, num_channels,
1206 ctx->f32, cache_policy,
1207 true, true);
1208 }
1209
1210 /* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
1211 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
1212 * or v4i32 (num_channels=3,4).
1213 */
1214 void
1215 ac_build_buffer_store_dword(struct ac_llvm_context *ctx,
1216 LLVMValueRef rsrc,
1217 LLVMValueRef vdata,
1218 unsigned num_channels,
1219 LLVMValueRef voffset,
1220 LLVMValueRef soffset,
1221 unsigned inst_offset,
1222 unsigned cache_policy,
1223 bool swizzle_enable_hint)
1224 {
1225 /* Split 3 channel stores, because only LLVM 9+ support 3-channel
1226 * intrinsics. */
1227 if (num_channels == 3 && !ac_has_vec3_support(ctx->chip_class, false)) {
1228 LLVMValueRef v[3], v01;
1229
1230 for (int i = 0; i < 3; i++) {
1231 v[i] = LLVMBuildExtractElement(ctx->builder, vdata,
1232 LLVMConstInt(ctx->i32, i, 0), "");
1233 }
1234 v01 = ac_build_gather_values(ctx, v, 2);
1235
1236 ac_build_buffer_store_dword(ctx, rsrc, v01, 2, voffset,
1237 soffset, inst_offset, cache_policy,
1238 swizzle_enable_hint);
1239 ac_build_buffer_store_dword(ctx, rsrc, v[2], 1, voffset,
1240 soffset, inst_offset + 8,
1241 cache_policy,
1242 swizzle_enable_hint);
1243 return;
1244 }
1245
1246 /* SWIZZLE_ENABLE requires that soffset isn't folded into voffset
1247 * (voffset is swizzled, but soffset isn't swizzled).
1248 * llvm.amdgcn.buffer.store doesn't have a separate soffset parameter.
1249 */
1250 if (!swizzle_enable_hint) {
1251 LLVMValueRef offset = soffset;
1252
1253 if (inst_offset)
1254 offset = LLVMBuildAdd(ctx->builder, offset,
1255 LLVMConstInt(ctx->i32, inst_offset, 0), "");
1256
1257 ac_build_buffer_store_common(ctx, rsrc, ac_to_float(ctx, vdata),
1258 ctx->i32_0, voffset, offset,
1259 num_channels, ctx->f32,
1260 cache_policy, false, false);
1261 return;
1262 }
1263
1264 static const unsigned dfmts[] = {
1265 V_008F0C_BUF_DATA_FORMAT_32,
1266 V_008F0C_BUF_DATA_FORMAT_32_32,
1267 V_008F0C_BUF_DATA_FORMAT_32_32_32,
1268 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
1269 };
1270 unsigned dfmt = dfmts[num_channels - 1];
1271 unsigned nfmt = V_008F0C_BUF_NUM_FORMAT_UINT;
1272 LLVMValueRef immoffset = LLVMConstInt(ctx->i32, inst_offset, 0);
1273
1274 ac_build_raw_tbuffer_store(ctx, rsrc, vdata, voffset, soffset,
1275 immoffset, num_channels, dfmt, nfmt, cache_policy);
1276 }
1277
1278 static LLVMValueRef
1279 ac_build_buffer_load_common(struct ac_llvm_context *ctx,
1280 LLVMValueRef rsrc,
1281 LLVMValueRef vindex,
1282 LLVMValueRef voffset,
1283 LLVMValueRef soffset,
1284 unsigned num_channels,
1285 LLVMTypeRef channel_type,
1286 unsigned cache_policy,
1287 bool can_speculate,
1288 bool use_format,
1289 bool structurized)
1290 {
1291 LLVMValueRef args[5];
1292 int idx = 0;
1293 args[idx++] = LLVMBuildBitCast(ctx->builder, rsrc, ctx->v4i32, "");
1294 if (structurized)
1295 args[idx++] = vindex ? vindex : ctx->i32_0;
1296 args[idx++] = voffset ? voffset : ctx->i32_0;
1297 args[idx++] = soffset ? soffset : ctx->i32_0;
1298 args[idx++] = LLVMConstInt(ctx->i32, get_load_cache_policy(ctx, cache_policy), 0);
1299 unsigned func = !ac_has_vec3_support(ctx->chip_class, use_format) && num_channels == 3 ? 4 : num_channels;
1300 const char *indexing_kind = structurized ? "struct" : "raw";
1301 char name[256], type_name[8];
1302
1303 LLVMTypeRef type = func > 1 ? LLVMVectorType(channel_type, func) : channel_type;
1304 ac_build_type_name_for_intr(type, type_name, sizeof(type_name));
1305
1306 if (use_format) {
1307 snprintf(name, sizeof(name), "llvm.amdgcn.%s.buffer.load.format.%s",
1308 indexing_kind, type_name);
1309 } else {
1310 snprintf(name, sizeof(name), "llvm.amdgcn.%s.buffer.load.%s",
1311 indexing_kind, type_name);
1312 }
1313
1314 return ac_build_intrinsic(ctx, name, type, args, idx,
1315 ac_get_load_intr_attribs(can_speculate));
1316 }
1317
1318 LLVMValueRef
1319 ac_build_buffer_load(struct ac_llvm_context *ctx,
1320 LLVMValueRef rsrc,
1321 int num_channels,
1322 LLVMValueRef vindex,
1323 LLVMValueRef voffset,
1324 LLVMValueRef soffset,
1325 unsigned inst_offset,
1326 unsigned cache_policy,
1327 bool can_speculate,
1328 bool allow_smem)
1329 {
1330 LLVMValueRef offset = LLVMConstInt(ctx->i32, inst_offset, 0);
1331 if (voffset)
1332 offset = LLVMBuildAdd(ctx->builder, offset, voffset, "");
1333 if (soffset)
1334 offset = LLVMBuildAdd(ctx->builder, offset, soffset, "");
1335
1336 if (allow_smem && !(cache_policy & ac_slc) &&
1337 (!(cache_policy & ac_glc) || ctx->chip_class >= GFX8)) {
1338 assert(vindex == NULL);
1339
1340 LLVMValueRef result[8];
1341
1342 for (int i = 0; i < num_channels; i++) {
1343 if (i) {
1344 offset = LLVMBuildAdd(ctx->builder, offset,
1345 LLVMConstInt(ctx->i32, 4, 0), "");
1346 }
1347 LLVMValueRef args[3] = {
1348 rsrc,
1349 offset,
1350 LLVMConstInt(ctx->i32, get_load_cache_policy(ctx, cache_policy), 0),
1351 };
1352 result[i] = ac_build_intrinsic(ctx,
1353 "llvm.amdgcn.s.buffer.load.f32",
1354 ctx->f32, args, 3,
1355 AC_FUNC_ATTR_READNONE);
1356 }
1357 if (num_channels == 1)
1358 return result[0];
1359
1360 if (num_channels == 3 && !ac_has_vec3_support(ctx->chip_class, false))
1361 result[num_channels++] = LLVMGetUndef(ctx->f32);
1362 return ac_build_gather_values(ctx, result, num_channels);
1363 }
1364
1365 return ac_build_buffer_load_common(ctx, rsrc, vindex,
1366 offset, ctx->i32_0,
1367 num_channels, ctx->f32,
1368 cache_policy,
1369 can_speculate, false, false);
1370 }
1371
1372 LLVMValueRef ac_build_buffer_load_format(struct ac_llvm_context *ctx,
1373 LLVMValueRef rsrc,
1374 LLVMValueRef vindex,
1375 LLVMValueRef voffset,
1376 unsigned num_channels,
1377 unsigned cache_policy,
1378 bool can_speculate)
1379 {
1380 return ac_build_buffer_load_common(ctx, rsrc, vindex, voffset,
1381 ctx->i32_0, num_channels, ctx->f32,
1382 cache_policy, can_speculate,
1383 true, true);
1384 }
1385
1386 static LLVMValueRef
1387 ac_build_tbuffer_load(struct ac_llvm_context *ctx,
1388 LLVMValueRef rsrc,
1389 LLVMValueRef vindex,
1390 LLVMValueRef voffset,
1391 LLVMValueRef soffset,
1392 LLVMValueRef immoffset,
1393 unsigned num_channels,
1394 unsigned dfmt,
1395 unsigned nfmt,
1396 unsigned cache_policy,
1397 bool can_speculate,
1398 bool structurized)
1399 {
1400 voffset = LLVMBuildAdd(ctx->builder, voffset, immoffset, "");
1401
1402 LLVMValueRef args[6];
1403 int idx = 0;
1404 args[idx++] = LLVMBuildBitCast(ctx->builder, rsrc, ctx->v4i32, "");
1405 if (structurized)
1406 args[idx++] = vindex ? vindex : ctx->i32_0;
1407 args[idx++] = voffset ? voffset : ctx->i32_0;
1408 args[idx++] = soffset ? soffset : ctx->i32_0;
1409 args[idx++] = LLVMConstInt(ctx->i32, ac_get_tbuffer_format(ctx->chip_class, dfmt, nfmt), 0);
1410 args[idx++] = LLVMConstInt(ctx->i32, get_load_cache_policy(ctx, cache_policy), 0);
1411 unsigned func = !ac_has_vec3_support(ctx->chip_class, true) && num_channels == 3 ? 4 : num_channels;
1412 const char *indexing_kind = structurized ? "struct" : "raw";
1413 char name[256], type_name[8];
1414
1415 LLVMTypeRef type = func > 1 ? LLVMVectorType(ctx->i32, func) : ctx->i32;
1416 ac_build_type_name_for_intr(type, type_name, sizeof(type_name));
1417
1418 snprintf(name, sizeof(name), "llvm.amdgcn.%s.tbuffer.load.%s",
1419 indexing_kind, type_name);
1420
1421 return ac_build_intrinsic(ctx, name, type, args, idx,
1422 ac_get_load_intr_attribs(can_speculate));
1423 }
1424
1425 LLVMValueRef
1426 ac_build_struct_tbuffer_load(struct ac_llvm_context *ctx,
1427 LLVMValueRef rsrc,
1428 LLVMValueRef vindex,
1429 LLVMValueRef voffset,
1430 LLVMValueRef soffset,
1431 LLVMValueRef immoffset,
1432 unsigned num_channels,
1433 unsigned dfmt,
1434 unsigned nfmt,
1435 unsigned cache_policy,
1436 bool can_speculate)
1437 {
1438 return ac_build_tbuffer_load(ctx, rsrc, vindex, voffset, soffset,
1439 immoffset, num_channels, dfmt, nfmt,
1440 cache_policy, can_speculate, true);
1441 }
1442
1443 LLVMValueRef
1444 ac_build_raw_tbuffer_load(struct ac_llvm_context *ctx,
1445 LLVMValueRef rsrc,
1446 LLVMValueRef voffset,
1447 LLVMValueRef soffset,
1448 LLVMValueRef immoffset,
1449 unsigned num_channels,
1450 unsigned dfmt,
1451 unsigned nfmt,
1452 unsigned cache_policy,
1453 bool can_speculate)
1454 {
1455 return ac_build_tbuffer_load(ctx, rsrc, NULL, voffset, soffset,
1456 immoffset, num_channels, dfmt, nfmt,
1457 cache_policy, can_speculate, false);
1458 }
1459
1460 LLVMValueRef
1461 ac_build_tbuffer_load_short(struct ac_llvm_context *ctx,
1462 LLVMValueRef rsrc,
1463 LLVMValueRef voffset,
1464 LLVMValueRef soffset,
1465 LLVMValueRef immoffset,
1466 unsigned cache_policy)
1467 {
1468 LLVMValueRef res;
1469
1470 if (LLVM_VERSION_MAJOR >= 9) {
1471 voffset = LLVMBuildAdd(ctx->builder, voffset, immoffset, "");
1472
1473 /* LLVM 9+ supports i8/i16 with struct/raw intrinsics. */
1474 res = ac_build_buffer_load_common(ctx, rsrc, NULL,
1475 voffset, soffset,
1476 1, ctx->i16, cache_policy,
1477 false, false, false);
1478 } else {
1479 unsigned dfmt = V_008F0C_BUF_DATA_FORMAT_16;
1480 unsigned nfmt = V_008F0C_BUF_NUM_FORMAT_UINT;
1481
1482 res = ac_build_raw_tbuffer_load(ctx, rsrc, voffset, soffset,
1483 immoffset, 1, dfmt, nfmt, cache_policy,
1484 false);
1485
1486 res = LLVMBuildTrunc(ctx->builder, res, ctx->i16, "");
1487 }
1488
1489 return res;
1490 }
1491
1492 LLVMValueRef
1493 ac_build_tbuffer_load_byte(struct ac_llvm_context *ctx,
1494 LLVMValueRef rsrc,
1495 LLVMValueRef voffset,
1496 LLVMValueRef soffset,
1497 LLVMValueRef immoffset,
1498 unsigned cache_policy)
1499 {
1500 LLVMValueRef res;
1501
1502 if (LLVM_VERSION_MAJOR >= 9) {
1503 voffset = LLVMBuildAdd(ctx->builder, voffset, immoffset, "");
1504
1505 /* LLVM 9+ supports i8/i16 with struct/raw intrinsics. */
1506 res = ac_build_buffer_load_common(ctx, rsrc, NULL,
1507 voffset, soffset,
1508 1, ctx->i8, cache_policy,
1509 false, false, false);
1510 } else {
1511 unsigned dfmt = V_008F0C_BUF_DATA_FORMAT_8;
1512 unsigned nfmt = V_008F0C_BUF_NUM_FORMAT_UINT;
1513
1514 res = ac_build_raw_tbuffer_load(ctx, rsrc, voffset, soffset,
1515 immoffset, 1, dfmt, nfmt, cache_policy,
1516 false);
1517
1518 res = LLVMBuildTrunc(ctx->builder, res, ctx->i8, "");
1519 }
1520
1521 return res;
1522 }
1523
1524 /**
1525 * Convert an 11- or 10-bit unsigned floating point number to an f32.
1526 *
1527 * The input exponent is expected to be biased analogous to IEEE-754, i.e. by
1528 * 2^(exp_bits-1) - 1 (as defined in OpenGL and other graphics APIs).
1529 */
1530 static LLVMValueRef
1531 ac_ufN_to_float(struct ac_llvm_context *ctx, LLVMValueRef src, unsigned exp_bits, unsigned mant_bits)
1532 {
1533 assert(LLVMTypeOf(src) == ctx->i32);
1534
1535 LLVMValueRef tmp;
1536 LLVMValueRef mantissa;
1537 mantissa = LLVMBuildAnd(ctx->builder, src, LLVMConstInt(ctx->i32, (1 << mant_bits) - 1, false), "");
1538
1539 /* Converting normal numbers is just a shift + correcting the exponent bias */
1540 unsigned normal_shift = 23 - mant_bits;
1541 unsigned bias_shift = 127 - ((1 << (exp_bits - 1)) - 1);
1542 LLVMValueRef shifted, normal;
1543
1544 shifted = LLVMBuildShl(ctx->builder, src, LLVMConstInt(ctx->i32, normal_shift, false), "");
1545 normal = LLVMBuildAdd(ctx->builder, shifted, LLVMConstInt(ctx->i32, bias_shift << 23, false), "");
1546
1547 /* Converting nan/inf numbers is the same, but with a different exponent update */
1548 LLVMValueRef naninf;
1549 naninf = LLVMBuildOr(ctx->builder, normal, LLVMConstInt(ctx->i32, 0xff << 23, false), "");
1550
1551 /* Converting denormals is the complex case: determine the leading zeros of the
1552 * mantissa to obtain the correct shift for the mantissa and exponent correction.
1553 */
1554 LLVMValueRef denormal;
1555 LLVMValueRef params[2] = {
1556 mantissa,
1557 ctx->i1true, /* result can be undef when arg is 0 */
1558 };
1559 LLVMValueRef ctlz = ac_build_intrinsic(ctx, "llvm.ctlz.i32", ctx->i32,
1560 params, 2, AC_FUNC_ATTR_READNONE);
1561
1562 /* Shift such that the leading 1 ends up as the LSB of the exponent field. */
1563 tmp = LLVMBuildSub(ctx->builder, ctlz, LLVMConstInt(ctx->i32, 8, false), "");
1564 denormal = LLVMBuildShl(ctx->builder, mantissa, tmp, "");
1565
1566 unsigned denormal_exp = bias_shift + (32 - mant_bits) - 1;
1567 tmp = LLVMBuildSub(ctx->builder, LLVMConstInt(ctx->i32, denormal_exp, false), ctlz, "");
1568 tmp = LLVMBuildShl(ctx->builder, tmp, LLVMConstInt(ctx->i32, 23, false), "");
1569 denormal = LLVMBuildAdd(ctx->builder, denormal, tmp, "");
1570
1571 /* Select the final result. */
1572 LLVMValueRef result;
1573
1574 tmp = LLVMBuildICmp(ctx->builder, LLVMIntUGE, src,
1575 LLVMConstInt(ctx->i32, ((1 << exp_bits) - 1) << mant_bits, false), "");
1576 result = LLVMBuildSelect(ctx->builder, tmp, naninf, normal, "");
1577
1578 tmp = LLVMBuildICmp(ctx->builder, LLVMIntUGE, src,
1579 LLVMConstInt(ctx->i32, 1 << mant_bits, false), "");
1580 result = LLVMBuildSelect(ctx->builder, tmp, result, denormal, "");
1581
1582 tmp = LLVMBuildICmp(ctx->builder, LLVMIntNE, src, ctx->i32_0, "");
1583 result = LLVMBuildSelect(ctx->builder, tmp, result, ctx->i32_0, "");
1584
1585 return ac_to_float(ctx, result);
1586 }
1587
1588 /**
1589 * Generate a fully general open coded buffer format fetch with all required
1590 * fixups suitable for vertex fetch, using non-format buffer loads.
1591 *
1592 * Some combinations of argument values have special interpretations:
1593 * - size = 8 bytes, format = fixed indicates PIPE_FORMAT_R11G11B10_FLOAT
1594 * - size = 8 bytes, format != {float,fixed} indicates a 2_10_10_10 data format
1595 *
1596 * \param log_size log(size of channel in bytes)
1597 * \param num_channels number of channels (1 to 4)
1598 * \param format AC_FETCH_FORMAT_xxx value
1599 * \param reverse whether XYZ channels are reversed
1600 * \param known_aligned whether the source is known to be aligned to hardware's
1601 * effective element size for loading the given format
1602 * (note: this means dword alignment for 8_8_8_8, 16_16, etc.)
1603 * \param rsrc buffer resource descriptor
1604 * \return the resulting vector of floats or integers bitcast to <4 x i32>
1605 */
1606 LLVMValueRef
1607 ac_build_opencoded_load_format(struct ac_llvm_context *ctx,
1608 unsigned log_size,
1609 unsigned num_channels,
1610 unsigned format,
1611 bool reverse,
1612 bool known_aligned,
1613 LLVMValueRef rsrc,
1614 LLVMValueRef vindex,
1615 LLVMValueRef voffset,
1616 LLVMValueRef soffset,
1617 unsigned cache_policy,
1618 bool can_speculate)
1619 {
1620 LLVMValueRef tmp;
1621 unsigned load_log_size = log_size;
1622 unsigned load_num_channels = num_channels;
1623 if (log_size == 3) {
1624 load_log_size = 2;
1625 if (format == AC_FETCH_FORMAT_FLOAT) {
1626 load_num_channels = 2 * num_channels;
1627 } else {
1628 load_num_channels = 1; /* 10_11_11 or 2_10_10_10 */
1629 }
1630 }
1631
1632 int log_recombine = 0;
1633 if (ctx->chip_class == GFX6 && !known_aligned) {
1634 /* Avoid alignment restrictions by loading one byte at a time. */
1635 load_num_channels <<= load_log_size;
1636 log_recombine = load_log_size;
1637 load_log_size = 0;
1638 } else if (load_num_channels == 2 || load_num_channels == 4) {
1639 log_recombine = -util_logbase2(load_num_channels);
1640 load_num_channels = 1;
1641 load_log_size += -log_recombine;
1642 }
1643
1644 assert(load_log_size >= 2 || LLVM_VERSION_MAJOR >= 9);
1645
1646 LLVMValueRef loads[32]; /* up to 32 bytes */
1647 for (unsigned i = 0; i < load_num_channels; ++i) {
1648 tmp = LLVMBuildAdd(ctx->builder, soffset,
1649 LLVMConstInt(ctx->i32, i << load_log_size, false), "");
1650 LLVMTypeRef channel_type = load_log_size == 0 ? ctx->i8 :
1651 load_log_size == 1 ? ctx->i16 : ctx->i32;
1652 unsigned num_channels = 1 << (MAX2(load_log_size, 2) - 2);
1653 loads[i] = ac_build_buffer_load_common(
1654 ctx, rsrc, vindex, voffset, tmp,
1655 num_channels, channel_type, cache_policy,
1656 can_speculate, false, true);
1657 if (load_log_size >= 2)
1658 loads[i] = ac_to_integer(ctx, loads[i]);
1659 }
1660
1661 if (log_recombine > 0) {
1662 /* Recombine bytes if necessary (GFX6 only) */
1663 LLVMTypeRef dst_type = log_recombine == 2 ? ctx->i32 : ctx->i16;
1664
1665 for (unsigned src = 0, dst = 0; src < load_num_channels; ++dst) {
1666 LLVMValueRef accum = NULL;
1667 for (unsigned i = 0; i < (1 << log_recombine); ++i, ++src) {
1668 tmp = LLVMBuildZExt(ctx->builder, loads[src], dst_type, "");
1669 if (i == 0) {
1670 accum = tmp;
1671 } else {
1672 tmp = LLVMBuildShl(ctx->builder, tmp,
1673 LLVMConstInt(dst_type, 8 * i, false), "");
1674 accum = LLVMBuildOr(ctx->builder, accum, tmp, "");
1675 }
1676 }
1677 loads[dst] = accum;
1678 }
1679 } else if (log_recombine < 0) {
1680 /* Split vectors of dwords */
1681 if (load_log_size > 2) {
1682 assert(load_num_channels == 1);
1683 LLVMValueRef loaded = loads[0];
1684 unsigned log_split = load_log_size - 2;
1685 log_recombine += log_split;
1686 load_num_channels = 1 << log_split;
1687 load_log_size = 2;
1688 for (unsigned i = 0; i < load_num_channels; ++i) {
1689 tmp = LLVMConstInt(ctx->i32, i, false);
1690 loads[i] = LLVMBuildExtractElement(ctx->builder, loaded, tmp, "");
1691 }
1692 }
1693
1694 /* Further split dwords and shorts if required */
1695 if (log_recombine < 0) {
1696 for (unsigned src = load_num_channels,
1697 dst = load_num_channels << -log_recombine;
1698 src > 0; --src) {
1699 unsigned dst_bits = 1 << (3 + load_log_size + log_recombine);
1700 LLVMTypeRef dst_type = LLVMIntTypeInContext(ctx->context, dst_bits);
1701 LLVMValueRef loaded = loads[src - 1];
1702 LLVMTypeRef loaded_type = LLVMTypeOf(loaded);
1703 for (unsigned i = 1 << -log_recombine; i > 0; --i, --dst) {
1704 tmp = LLVMConstInt(loaded_type, dst_bits * (i - 1), false);
1705 tmp = LLVMBuildLShr(ctx->builder, loaded, tmp, "");
1706 loads[dst - 1] = LLVMBuildTrunc(ctx->builder, tmp, dst_type, "");
1707 }
1708 }
1709 }
1710 }
1711
1712 if (log_size == 3) {
1713 if (format == AC_FETCH_FORMAT_FLOAT) {
1714 for (unsigned i = 0; i < num_channels; ++i) {
1715 tmp = ac_build_gather_values(ctx, &loads[2 * i], 2);
1716 loads[i] = LLVMBuildBitCast(ctx->builder, tmp, ctx->f64, "");
1717 }
1718 } else if (format == AC_FETCH_FORMAT_FIXED) {
1719 /* 10_11_11_FLOAT */
1720 LLVMValueRef data = loads[0];
1721 LLVMValueRef i32_2047 = LLVMConstInt(ctx->i32, 2047, false);
1722 LLVMValueRef r = LLVMBuildAnd(ctx->builder, data, i32_2047, "");
1723 tmp = LLVMBuildLShr(ctx->builder, data, LLVMConstInt(ctx->i32, 11, false), "");
1724 LLVMValueRef g = LLVMBuildAnd(ctx->builder, tmp, i32_2047, "");
1725 LLVMValueRef b = LLVMBuildLShr(ctx->builder, data, LLVMConstInt(ctx->i32, 22, false), "");
1726
1727 loads[0] = ac_to_integer(ctx, ac_ufN_to_float(ctx, r, 5, 6));
1728 loads[1] = ac_to_integer(ctx, ac_ufN_to_float(ctx, g, 5, 6));
1729 loads[2] = ac_to_integer(ctx, ac_ufN_to_float(ctx, b, 5, 5));
1730
1731 num_channels = 3;
1732 log_size = 2;
1733 format = AC_FETCH_FORMAT_FLOAT;
1734 } else {
1735 /* 2_10_10_10 data formats */
1736 LLVMValueRef data = loads[0];
1737 LLVMTypeRef i10 = LLVMIntTypeInContext(ctx->context, 10);
1738 LLVMTypeRef i2 = LLVMIntTypeInContext(ctx->context, 2);
1739 loads[0] = LLVMBuildTrunc(ctx->builder, data, i10, "");
1740 tmp = LLVMBuildLShr(ctx->builder, data, LLVMConstInt(ctx->i32, 10, false), "");
1741 loads[1] = LLVMBuildTrunc(ctx->builder, tmp, i10, "");
1742 tmp = LLVMBuildLShr(ctx->builder, data, LLVMConstInt(ctx->i32, 20, false), "");
1743 loads[2] = LLVMBuildTrunc(ctx->builder, tmp, i10, "");
1744 tmp = LLVMBuildLShr(ctx->builder, data, LLVMConstInt(ctx->i32, 30, false), "");
1745 loads[3] = LLVMBuildTrunc(ctx->builder, tmp, i2, "");
1746
1747 num_channels = 4;
1748 }
1749 }
1750
1751 if (format == AC_FETCH_FORMAT_FLOAT) {
1752 if (log_size != 2) {
1753 for (unsigned chan = 0; chan < num_channels; ++chan) {
1754 tmp = ac_to_float(ctx, loads[chan]);
1755 if (log_size == 3)
1756 tmp = LLVMBuildFPTrunc(ctx->builder, tmp, ctx->f32, "");
1757 else if (log_size == 1)
1758 tmp = LLVMBuildFPExt(ctx->builder, tmp, ctx->f32, "");
1759 loads[chan] = ac_to_integer(ctx, tmp);
1760 }
1761 }
1762 } else if (format == AC_FETCH_FORMAT_UINT) {
1763 if (log_size != 2) {
1764 for (unsigned chan = 0; chan < num_channels; ++chan)
1765 loads[chan] = LLVMBuildZExt(ctx->builder, loads[chan], ctx->i32, "");
1766 }
1767 } else if (format == AC_FETCH_FORMAT_SINT) {
1768 if (log_size != 2) {
1769 for (unsigned chan = 0; chan < num_channels; ++chan)
1770 loads[chan] = LLVMBuildSExt(ctx->builder, loads[chan], ctx->i32, "");
1771 }
1772 } else {
1773 bool unsign = format == AC_FETCH_FORMAT_UNORM ||
1774 format == AC_FETCH_FORMAT_USCALED ||
1775 format == AC_FETCH_FORMAT_UINT;
1776
1777 for (unsigned chan = 0; chan < num_channels; ++chan) {
1778 if (unsign) {
1779 tmp = LLVMBuildUIToFP(ctx->builder, loads[chan], ctx->f32, "");
1780 } else {
1781 tmp = LLVMBuildSIToFP(ctx->builder, loads[chan], ctx->f32, "");
1782 }
1783
1784 LLVMValueRef scale = NULL;
1785 if (format == AC_FETCH_FORMAT_FIXED) {
1786 assert(log_size == 2);
1787 scale = LLVMConstReal(ctx->f32, 1.0 / 0x10000);
1788 } else if (format == AC_FETCH_FORMAT_UNORM) {
1789 unsigned bits = LLVMGetIntTypeWidth(LLVMTypeOf(loads[chan]));
1790 scale = LLVMConstReal(ctx->f32, 1.0 / (((uint64_t)1 << bits) - 1));
1791 } else if (format == AC_FETCH_FORMAT_SNORM) {
1792 unsigned bits = LLVMGetIntTypeWidth(LLVMTypeOf(loads[chan]));
1793 scale = LLVMConstReal(ctx->f32, 1.0 / (((uint64_t)1 << (bits - 1)) - 1));
1794 }
1795 if (scale)
1796 tmp = LLVMBuildFMul(ctx->builder, tmp, scale, "");
1797
1798 if (format == AC_FETCH_FORMAT_SNORM) {
1799 /* Clamp to [-1, 1] */
1800 LLVMValueRef neg_one = LLVMConstReal(ctx->f32, -1.0);
1801 LLVMValueRef clamp =
1802 LLVMBuildFCmp(ctx->builder, LLVMRealULT, tmp, neg_one, "");
1803 tmp = LLVMBuildSelect(ctx->builder, clamp, neg_one, tmp, "");
1804 }
1805
1806 loads[chan] = ac_to_integer(ctx, tmp);
1807 }
1808 }
1809
1810 while (num_channels < 4) {
1811 if (format == AC_FETCH_FORMAT_UINT || format == AC_FETCH_FORMAT_SINT) {
1812 loads[num_channels] = num_channels == 3 ? ctx->i32_1 : ctx->i32_0;
1813 } else {
1814 loads[num_channels] = ac_to_integer(ctx, num_channels == 3 ? ctx->f32_1 : ctx->f32_0);
1815 }
1816 num_channels++;
1817 }
1818
1819 if (reverse) {
1820 tmp = loads[0];
1821 loads[0] = loads[2];
1822 loads[2] = tmp;
1823 }
1824
1825 return ac_build_gather_values(ctx, loads, 4);
1826 }
1827
1828 static void
1829 ac_build_tbuffer_store(struct ac_llvm_context *ctx,
1830 LLVMValueRef rsrc,
1831 LLVMValueRef vdata,
1832 LLVMValueRef vindex,
1833 LLVMValueRef voffset,
1834 LLVMValueRef soffset,
1835 LLVMValueRef immoffset,
1836 unsigned num_channels,
1837 unsigned dfmt,
1838 unsigned nfmt,
1839 unsigned cache_policy,
1840 bool structurized)
1841 {
1842 voffset = LLVMBuildAdd(ctx->builder, voffset ? voffset : ctx->i32_0,
1843 immoffset, "");
1844
1845 LLVMValueRef args[7];
1846 int idx = 0;
1847 args[idx++] = vdata;
1848 args[idx++] = LLVMBuildBitCast(ctx->builder, rsrc, ctx->v4i32, "");
1849 if (structurized)
1850 args[idx++] = vindex ? vindex : ctx->i32_0;
1851 args[idx++] = voffset ? voffset : ctx->i32_0;
1852 args[idx++] = soffset ? soffset : ctx->i32_0;
1853 args[idx++] = LLVMConstInt(ctx->i32, ac_get_tbuffer_format(ctx->chip_class, dfmt, nfmt), 0);
1854 args[idx++] = LLVMConstInt(ctx->i32, cache_policy, 0);
1855 unsigned func = !ac_has_vec3_support(ctx->chip_class, true) && num_channels == 3 ? 4 : num_channels;
1856 const char *indexing_kind = structurized ? "struct" : "raw";
1857 char name[256], type_name[8];
1858
1859 LLVMTypeRef type = func > 1 ? LLVMVectorType(ctx->i32, func) : ctx->i32;
1860 ac_build_type_name_for_intr(type, type_name, sizeof(type_name));
1861
1862 snprintf(name, sizeof(name), "llvm.amdgcn.%s.tbuffer.store.%s",
1863 indexing_kind, type_name);
1864
1865 ac_build_intrinsic(ctx, name, ctx->voidt, args, idx,
1866 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY);
1867 }
1868
1869 void
1870 ac_build_struct_tbuffer_store(struct ac_llvm_context *ctx,
1871 LLVMValueRef rsrc,
1872 LLVMValueRef vdata,
1873 LLVMValueRef vindex,
1874 LLVMValueRef voffset,
1875 LLVMValueRef soffset,
1876 LLVMValueRef immoffset,
1877 unsigned num_channels,
1878 unsigned dfmt,
1879 unsigned nfmt,
1880 unsigned cache_policy)
1881 {
1882 ac_build_tbuffer_store(ctx, rsrc, vdata, vindex, voffset, soffset,
1883 immoffset, num_channels, dfmt, nfmt, cache_policy,
1884 true);
1885 }
1886
1887 void
1888 ac_build_raw_tbuffer_store(struct ac_llvm_context *ctx,
1889 LLVMValueRef rsrc,
1890 LLVMValueRef vdata,
1891 LLVMValueRef voffset,
1892 LLVMValueRef soffset,
1893 LLVMValueRef immoffset,
1894 unsigned num_channels,
1895 unsigned dfmt,
1896 unsigned nfmt,
1897 unsigned cache_policy)
1898 {
1899 ac_build_tbuffer_store(ctx, rsrc, vdata, NULL, voffset, soffset,
1900 immoffset, num_channels, dfmt, nfmt, cache_policy,
1901 false);
1902 }
1903
1904 void
1905 ac_build_tbuffer_store_short(struct ac_llvm_context *ctx,
1906 LLVMValueRef rsrc,
1907 LLVMValueRef vdata,
1908 LLVMValueRef voffset,
1909 LLVMValueRef soffset,
1910 unsigned cache_policy)
1911 {
1912 vdata = LLVMBuildBitCast(ctx->builder, vdata, ctx->i16, "");
1913
1914 if (LLVM_VERSION_MAJOR >= 9) {
1915 /* LLVM 9+ supports i8/i16 with struct/raw intrinsics. */
1916 ac_build_buffer_store_common(ctx, rsrc, vdata, NULL,
1917 voffset, soffset, 1,
1918 ctx->i16, cache_policy,
1919 false, false);
1920 } else {
1921 unsigned dfmt = V_008F0C_BUF_DATA_FORMAT_16;
1922 unsigned nfmt = V_008F0C_BUF_NUM_FORMAT_UINT;
1923
1924 vdata = LLVMBuildZExt(ctx->builder, vdata, ctx->i32, "");
1925
1926 ac_build_raw_tbuffer_store(ctx, rsrc, vdata, voffset, soffset,
1927 ctx->i32_0, 1, dfmt, nfmt, cache_policy);
1928 }
1929 }
1930
1931 void
1932 ac_build_tbuffer_store_byte(struct ac_llvm_context *ctx,
1933 LLVMValueRef rsrc,
1934 LLVMValueRef vdata,
1935 LLVMValueRef voffset,
1936 LLVMValueRef soffset,
1937 unsigned cache_policy)
1938 {
1939 vdata = LLVMBuildBitCast(ctx->builder, vdata, ctx->i8, "");
1940
1941 if (LLVM_VERSION_MAJOR >= 9) {
1942 /* LLVM 9+ supports i8/i16 with struct/raw intrinsics. */
1943 ac_build_buffer_store_common(ctx, rsrc, vdata, NULL,
1944 voffset, soffset, 1,
1945 ctx->i8, cache_policy,
1946 false, false);
1947 } else {
1948 unsigned dfmt = V_008F0C_BUF_DATA_FORMAT_8;
1949 unsigned nfmt = V_008F0C_BUF_NUM_FORMAT_UINT;
1950
1951 vdata = LLVMBuildZExt(ctx->builder, vdata, ctx->i32, "");
1952
1953 ac_build_raw_tbuffer_store(ctx, rsrc, vdata, voffset, soffset,
1954 ctx->i32_0, 1, dfmt, nfmt, cache_policy);
1955 }
1956 }
1957 /**
1958 * Set range metadata on an instruction. This can only be used on load and
1959 * call instructions. If you know an instruction can only produce the values
1960 * 0, 1, 2, you would do set_range_metadata(value, 0, 3);
1961 * \p lo is the minimum value inclusive.
1962 * \p hi is the maximum value exclusive.
1963 */
1964 static void set_range_metadata(struct ac_llvm_context *ctx,
1965 LLVMValueRef value, unsigned lo, unsigned hi)
1966 {
1967 LLVMValueRef range_md, md_args[2];
1968 LLVMTypeRef type = LLVMTypeOf(value);
1969 LLVMContextRef context = LLVMGetTypeContext(type);
1970
1971 md_args[0] = LLVMConstInt(type, lo, false);
1972 md_args[1] = LLVMConstInt(type, hi, false);
1973 range_md = LLVMMDNodeInContext(context, md_args, 2);
1974 LLVMSetMetadata(value, ctx->range_md_kind, range_md);
1975 }
1976
1977 LLVMValueRef
1978 ac_get_thread_id(struct ac_llvm_context *ctx)
1979 {
1980 LLVMValueRef tid;
1981
1982 LLVMValueRef tid_args[2];
1983 tid_args[0] = LLVMConstInt(ctx->i32, 0xffffffff, false);
1984 tid_args[1] = ctx->i32_0;
1985 tid_args[1] = ac_build_intrinsic(ctx,
1986 "llvm.amdgcn.mbcnt.lo", ctx->i32,
1987 tid_args, 2, AC_FUNC_ATTR_READNONE);
1988
1989 if (ctx->wave_size == 32) {
1990 tid = tid_args[1];
1991 } else {
1992 tid = ac_build_intrinsic(ctx, "llvm.amdgcn.mbcnt.hi",
1993 ctx->i32, tid_args,
1994 2, AC_FUNC_ATTR_READNONE);
1995 }
1996 set_range_metadata(ctx, tid, 0, ctx->wave_size);
1997 return tid;
1998 }
1999
2000 /*
2001 * AMD GCN implements derivatives using the local data store (LDS)
2002 * All writes to the LDS happen in all executing threads at
2003 * the same time. TID is the Thread ID for the current
2004 * thread and is a value between 0 and 63, representing
2005 * the thread's position in the wavefront.
2006 *
2007 * For the pixel shader threads are grouped into quads of four pixels.
2008 * The TIDs of the pixels of a quad are:
2009 *
2010 * +------+------+
2011 * |4n + 0|4n + 1|
2012 * +------+------+
2013 * |4n + 2|4n + 3|
2014 * +------+------+
2015 *
2016 * So, masking the TID with 0xfffffffc yields the TID of the top left pixel
2017 * of the quad, masking with 0xfffffffd yields the TID of the top pixel of
2018 * the current pixel's column, and masking with 0xfffffffe yields the TID
2019 * of the left pixel of the current pixel's row.
2020 *
2021 * Adding 1 yields the TID of the pixel to the right of the left pixel, and
2022 * adding 2 yields the TID of the pixel below the top pixel.
2023 */
2024 LLVMValueRef
2025 ac_build_ddxy(struct ac_llvm_context *ctx,
2026 uint32_t mask,
2027 int idx,
2028 LLVMValueRef val)
2029 {
2030 unsigned tl_lanes[4], trbl_lanes[4];
2031 char name[32], type[8];
2032 LLVMValueRef tl, trbl;
2033 LLVMTypeRef result_type;
2034 LLVMValueRef result;
2035
2036 result_type = ac_to_float_type(ctx, LLVMTypeOf(val));
2037
2038 if (result_type == ctx->f16)
2039 val = LLVMBuildZExt(ctx->builder, val, ctx->i32, "");
2040
2041 for (unsigned i = 0; i < 4; ++i) {
2042 tl_lanes[i] = i & mask;
2043 trbl_lanes[i] = (i & mask) + idx;
2044 }
2045
2046 tl = ac_build_quad_swizzle(ctx, val,
2047 tl_lanes[0], tl_lanes[1],
2048 tl_lanes[2], tl_lanes[3]);
2049 trbl = ac_build_quad_swizzle(ctx, val,
2050 trbl_lanes[0], trbl_lanes[1],
2051 trbl_lanes[2], trbl_lanes[3]);
2052
2053 if (result_type == ctx->f16) {
2054 tl = LLVMBuildTrunc(ctx->builder, tl, ctx->i16, "");
2055 trbl = LLVMBuildTrunc(ctx->builder, trbl, ctx->i16, "");
2056 }
2057
2058 tl = LLVMBuildBitCast(ctx->builder, tl, result_type, "");
2059 trbl = LLVMBuildBitCast(ctx->builder, trbl, result_type, "");
2060 result = LLVMBuildFSub(ctx->builder, trbl, tl, "");
2061
2062 ac_build_type_name_for_intr(result_type, type, sizeof(type));
2063 snprintf(name, sizeof(name), "llvm.amdgcn.wqm.%s", type);
2064
2065 return ac_build_intrinsic(ctx, name, result_type, &result, 1, 0);
2066 }
2067
2068 void
2069 ac_build_sendmsg(struct ac_llvm_context *ctx,
2070 uint32_t msg,
2071 LLVMValueRef wave_id)
2072 {
2073 LLVMValueRef args[2];
2074 args[0] = LLVMConstInt(ctx->i32, msg, false);
2075 args[1] = wave_id;
2076 ac_build_intrinsic(ctx, "llvm.amdgcn.s.sendmsg", ctx->voidt, args, 2, 0);
2077 }
2078
2079 LLVMValueRef
2080 ac_build_imsb(struct ac_llvm_context *ctx,
2081 LLVMValueRef arg,
2082 LLVMTypeRef dst_type)
2083 {
2084 LLVMValueRef msb = ac_build_intrinsic(ctx, "llvm.amdgcn.sffbh.i32",
2085 dst_type, &arg, 1,
2086 AC_FUNC_ATTR_READNONE);
2087
2088 /* The HW returns the last bit index from MSB, but NIR/TGSI wants
2089 * the index from LSB. Invert it by doing "31 - msb". */
2090 msb = LLVMBuildSub(ctx->builder, LLVMConstInt(ctx->i32, 31, false),
2091 msb, "");
2092
2093 LLVMValueRef all_ones = LLVMConstInt(ctx->i32, -1, true);
2094 LLVMValueRef cond = LLVMBuildOr(ctx->builder,
2095 LLVMBuildICmp(ctx->builder, LLVMIntEQ,
2096 arg, ctx->i32_0, ""),
2097 LLVMBuildICmp(ctx->builder, LLVMIntEQ,
2098 arg, all_ones, ""), "");
2099
2100 return LLVMBuildSelect(ctx->builder, cond, all_ones, msb, "");
2101 }
2102
2103 LLVMValueRef
2104 ac_build_umsb(struct ac_llvm_context *ctx,
2105 LLVMValueRef arg,
2106 LLVMTypeRef dst_type)
2107 {
2108 const char *intrin_name;
2109 LLVMTypeRef type;
2110 LLVMValueRef highest_bit;
2111 LLVMValueRef zero;
2112 unsigned bitsize;
2113
2114 bitsize = ac_get_elem_bits(ctx, LLVMTypeOf(arg));
2115 switch (bitsize) {
2116 case 64:
2117 intrin_name = "llvm.ctlz.i64";
2118 type = ctx->i64;
2119 highest_bit = LLVMConstInt(ctx->i64, 63, false);
2120 zero = ctx->i64_0;
2121 break;
2122 case 32:
2123 intrin_name = "llvm.ctlz.i32";
2124 type = ctx->i32;
2125 highest_bit = LLVMConstInt(ctx->i32, 31, false);
2126 zero = ctx->i32_0;
2127 break;
2128 case 16:
2129 intrin_name = "llvm.ctlz.i16";
2130 type = ctx->i16;
2131 highest_bit = LLVMConstInt(ctx->i16, 15, false);
2132 zero = ctx->i16_0;
2133 break;
2134 case 8:
2135 intrin_name = "llvm.ctlz.i8";
2136 type = ctx->i8;
2137 highest_bit = LLVMConstInt(ctx->i8, 7, false);
2138 zero = ctx->i8_0;
2139 break;
2140 default:
2141 unreachable(!"invalid bitsize");
2142 break;
2143 }
2144
2145 LLVMValueRef params[2] = {
2146 arg,
2147 ctx->i1true,
2148 };
2149
2150 LLVMValueRef msb = ac_build_intrinsic(ctx, intrin_name, type,
2151 params, 2,
2152 AC_FUNC_ATTR_READNONE);
2153
2154 /* The HW returns the last bit index from MSB, but TGSI/NIR wants
2155 * the index from LSB. Invert it by doing "31 - msb". */
2156 msb = LLVMBuildSub(ctx->builder, highest_bit, msb, "");
2157
2158 if (bitsize == 64) {
2159 msb = LLVMBuildTrunc(ctx->builder, msb, ctx->i32, "");
2160 } else if (bitsize < 32) {
2161 msb = LLVMBuildSExt(ctx->builder, msb, ctx->i32, "");
2162 }
2163
2164 /* check for zero */
2165 return LLVMBuildSelect(ctx->builder,
2166 LLVMBuildICmp(ctx->builder, LLVMIntEQ, arg, zero, ""),
2167 LLVMConstInt(ctx->i32, -1, true), msb, "");
2168 }
2169
2170 LLVMValueRef ac_build_fmin(struct ac_llvm_context *ctx, LLVMValueRef a,
2171 LLVMValueRef b)
2172 {
2173 char name[64];
2174 snprintf(name, sizeof(name), "llvm.minnum.f%d", ac_get_elem_bits(ctx, LLVMTypeOf(a)));
2175 LLVMValueRef args[2] = {a, b};
2176 return ac_build_intrinsic(ctx, name, LLVMTypeOf(a), args, 2,
2177 AC_FUNC_ATTR_READNONE);
2178 }
2179
2180 LLVMValueRef ac_build_fmax(struct ac_llvm_context *ctx, LLVMValueRef a,
2181 LLVMValueRef b)
2182 {
2183 char name[64];
2184 snprintf(name, sizeof(name), "llvm.maxnum.f%d", ac_get_elem_bits(ctx, LLVMTypeOf(a)));
2185 LLVMValueRef args[2] = {a, b};
2186 return ac_build_intrinsic(ctx, name, LLVMTypeOf(a), args, 2,
2187 AC_FUNC_ATTR_READNONE);
2188 }
2189
2190 LLVMValueRef ac_build_imin(struct ac_llvm_context *ctx, LLVMValueRef a,
2191 LLVMValueRef b)
2192 {
2193 LLVMValueRef cmp = LLVMBuildICmp(ctx->builder, LLVMIntSLE, a, b, "");
2194 return LLVMBuildSelect(ctx->builder, cmp, a, b, "");
2195 }
2196
2197 LLVMValueRef ac_build_imax(struct ac_llvm_context *ctx, LLVMValueRef a,
2198 LLVMValueRef b)
2199 {
2200 LLVMValueRef cmp = LLVMBuildICmp(ctx->builder, LLVMIntSGT, a, b, "");
2201 return LLVMBuildSelect(ctx->builder, cmp, a, b, "");
2202 }
2203
2204 LLVMValueRef ac_build_umin(struct ac_llvm_context *ctx, LLVMValueRef a,
2205 LLVMValueRef b)
2206 {
2207 LLVMValueRef cmp = LLVMBuildICmp(ctx->builder, LLVMIntULE, a, b, "");
2208 return LLVMBuildSelect(ctx->builder, cmp, a, b, "");
2209 }
2210
2211 LLVMValueRef ac_build_umax(struct ac_llvm_context *ctx, LLVMValueRef a,
2212 LLVMValueRef b)
2213 {
2214 LLVMValueRef cmp = LLVMBuildICmp(ctx->builder, LLVMIntUGE, a, b, "");
2215 return LLVMBuildSelect(ctx->builder, cmp, a, b, "");
2216 }
2217
2218 LLVMValueRef ac_build_clamp(struct ac_llvm_context *ctx, LLVMValueRef value)
2219 {
2220 LLVMTypeRef t = LLVMTypeOf(value);
2221 return ac_build_fmin(ctx, ac_build_fmax(ctx, value, LLVMConstReal(t, 0.0)),
2222 LLVMConstReal(t, 1.0));
2223 }
2224
2225 void ac_build_export(struct ac_llvm_context *ctx, struct ac_export_args *a)
2226 {
2227 LLVMValueRef args[9];
2228
2229 args[0] = LLVMConstInt(ctx->i32, a->target, 0);
2230 args[1] = LLVMConstInt(ctx->i32, a->enabled_channels, 0);
2231
2232 if (a->compr) {
2233 LLVMTypeRef i16 = LLVMInt16TypeInContext(ctx->context);
2234 LLVMTypeRef v2i16 = LLVMVectorType(i16, 2);
2235
2236 args[2] = LLVMBuildBitCast(ctx->builder, a->out[0],
2237 v2i16, "");
2238 args[3] = LLVMBuildBitCast(ctx->builder, a->out[1],
2239 v2i16, "");
2240 args[4] = LLVMConstInt(ctx->i1, a->done, 0);
2241 args[5] = LLVMConstInt(ctx->i1, a->valid_mask, 0);
2242
2243 ac_build_intrinsic(ctx, "llvm.amdgcn.exp.compr.v2i16",
2244 ctx->voidt, args, 6, 0);
2245 } else {
2246 args[2] = a->out[0];
2247 args[3] = a->out[1];
2248 args[4] = a->out[2];
2249 args[5] = a->out[3];
2250 args[6] = LLVMConstInt(ctx->i1, a->done, 0);
2251 args[7] = LLVMConstInt(ctx->i1, a->valid_mask, 0);
2252
2253 ac_build_intrinsic(ctx, "llvm.amdgcn.exp.f32",
2254 ctx->voidt, args, 8, 0);
2255 }
2256 }
2257
2258 void ac_build_export_null(struct ac_llvm_context *ctx)
2259 {
2260 struct ac_export_args args;
2261
2262 args.enabled_channels = 0x0; /* enabled channels */
2263 args.valid_mask = 1; /* whether the EXEC mask is valid */
2264 args.done = 1; /* DONE bit */
2265 args.target = V_008DFC_SQ_EXP_NULL;
2266 args.compr = 0; /* COMPR flag (0 = 32-bit export) */
2267 args.out[0] = LLVMGetUndef(ctx->f32); /* R */
2268 args.out[1] = LLVMGetUndef(ctx->f32); /* G */
2269 args.out[2] = LLVMGetUndef(ctx->f32); /* B */
2270 args.out[3] = LLVMGetUndef(ctx->f32); /* A */
2271
2272 ac_build_export(ctx, &args);
2273 }
2274
2275 static unsigned ac_num_coords(enum ac_image_dim dim)
2276 {
2277 switch (dim) {
2278 case ac_image_1d:
2279 return 1;
2280 case ac_image_2d:
2281 case ac_image_1darray:
2282 return 2;
2283 case ac_image_3d:
2284 case ac_image_cube:
2285 case ac_image_2darray:
2286 case ac_image_2dmsaa:
2287 return 3;
2288 case ac_image_2darraymsaa:
2289 return 4;
2290 default:
2291 unreachable("ac_num_coords: bad dim");
2292 }
2293 }
2294
2295 static unsigned ac_num_derivs(enum ac_image_dim dim)
2296 {
2297 switch (dim) {
2298 case ac_image_1d:
2299 case ac_image_1darray:
2300 return 2;
2301 case ac_image_2d:
2302 case ac_image_2darray:
2303 case ac_image_cube:
2304 return 4;
2305 case ac_image_3d:
2306 return 6;
2307 case ac_image_2dmsaa:
2308 case ac_image_2darraymsaa:
2309 default:
2310 unreachable("derivatives not supported");
2311 }
2312 }
2313
2314 static const char *get_atomic_name(enum ac_atomic_op op)
2315 {
2316 switch (op) {
2317 case ac_atomic_swap: return "swap";
2318 case ac_atomic_add: return "add";
2319 case ac_atomic_sub: return "sub";
2320 case ac_atomic_smin: return "smin";
2321 case ac_atomic_umin: return "umin";
2322 case ac_atomic_smax: return "smax";
2323 case ac_atomic_umax: return "umax";
2324 case ac_atomic_and: return "and";
2325 case ac_atomic_or: return "or";
2326 case ac_atomic_xor: return "xor";
2327 case ac_atomic_inc_wrap: return "inc";
2328 case ac_atomic_dec_wrap: return "dec";
2329 }
2330 unreachable("bad atomic op");
2331 }
2332
2333 LLVMValueRef ac_build_image_opcode(struct ac_llvm_context *ctx,
2334 struct ac_image_args *a)
2335 {
2336 const char *overload[3] = { "", "", "" };
2337 unsigned num_overloads = 0;
2338 LLVMValueRef args[18];
2339 unsigned num_args = 0;
2340 enum ac_image_dim dim = a->dim;
2341
2342 assert(!a->lod || a->lod == ctx->i32_0 || a->lod == ctx->f32_0 ||
2343 !a->level_zero);
2344 assert((a->opcode != ac_image_get_resinfo && a->opcode != ac_image_load_mip &&
2345 a->opcode != ac_image_store_mip) ||
2346 a->lod);
2347 assert(a->opcode == ac_image_sample || a->opcode == ac_image_gather4 ||
2348 (!a->compare && !a->offset));
2349 assert((a->opcode == ac_image_sample || a->opcode == ac_image_gather4 ||
2350 a->opcode == ac_image_get_lod) ||
2351 !a->bias);
2352 assert((a->bias ? 1 : 0) +
2353 (a->lod ? 1 : 0) +
2354 (a->level_zero ? 1 : 0) +
2355 (a->derivs[0] ? 1 : 0) <= 1);
2356
2357 if (a->opcode == ac_image_get_lod) {
2358 switch (dim) {
2359 case ac_image_1darray:
2360 dim = ac_image_1d;
2361 break;
2362 case ac_image_2darray:
2363 case ac_image_cube:
2364 dim = ac_image_2d;
2365 break;
2366 default:
2367 break;
2368 }
2369 }
2370
2371 bool sample = a->opcode == ac_image_sample ||
2372 a->opcode == ac_image_gather4 ||
2373 a->opcode == ac_image_get_lod;
2374 bool atomic = a->opcode == ac_image_atomic ||
2375 a->opcode == ac_image_atomic_cmpswap;
2376 bool load = a->opcode == ac_image_sample ||
2377 a->opcode == ac_image_gather4 ||
2378 a->opcode == ac_image_load ||
2379 a->opcode == ac_image_load_mip;
2380 LLVMTypeRef coord_type = sample ? ctx->f32 : ctx->i32;
2381
2382 if (atomic || a->opcode == ac_image_store || a->opcode == ac_image_store_mip) {
2383 args[num_args++] = a->data[0];
2384 if (a->opcode == ac_image_atomic_cmpswap)
2385 args[num_args++] = a->data[1];
2386 }
2387
2388 if (!atomic)
2389 args[num_args++] = LLVMConstInt(ctx->i32, a->dmask, false);
2390
2391 if (a->offset)
2392 args[num_args++] = ac_to_integer(ctx, a->offset);
2393 if (a->bias) {
2394 args[num_args++] = ac_to_float(ctx, a->bias);
2395 overload[num_overloads++] = ".f32";
2396 }
2397 if (a->compare)
2398 args[num_args++] = ac_to_float(ctx, a->compare);
2399 if (a->derivs[0]) {
2400 unsigned count = ac_num_derivs(dim);
2401 for (unsigned i = 0; i < count; ++i)
2402 args[num_args++] = ac_to_float(ctx, a->derivs[i]);
2403 overload[num_overloads++] = ".f32";
2404 }
2405 unsigned num_coords =
2406 a->opcode != ac_image_get_resinfo ? ac_num_coords(dim) : 0;
2407 for (unsigned i = 0; i < num_coords; ++i)
2408 args[num_args++] = LLVMBuildBitCast(ctx->builder, a->coords[i], coord_type, "");
2409 if (a->lod)
2410 args[num_args++] = LLVMBuildBitCast(ctx->builder, a->lod, coord_type, "");
2411 overload[num_overloads++] = sample ? ".f32" : ".i32";
2412
2413 args[num_args++] = a->resource;
2414 if (sample) {
2415 args[num_args++] = a->sampler;
2416 args[num_args++] = LLVMConstInt(ctx->i1, a->unorm, false);
2417 }
2418
2419 args[num_args++] = ctx->i32_0; /* texfailctrl */
2420 args[num_args++] = LLVMConstInt(ctx->i32,
2421 load ? get_load_cache_policy(ctx, a->cache_policy) :
2422 a->cache_policy, false);
2423
2424 const char *name;
2425 const char *atomic_subop = "";
2426 switch (a->opcode) {
2427 case ac_image_sample: name = "sample"; break;
2428 case ac_image_gather4: name = "gather4"; break;
2429 case ac_image_load: name = "load"; break;
2430 case ac_image_load_mip: name = "load.mip"; break;
2431 case ac_image_store: name = "store"; break;
2432 case ac_image_store_mip: name = "store.mip"; break;
2433 case ac_image_atomic:
2434 name = "atomic.";
2435 atomic_subop = get_atomic_name(a->atomic);
2436 break;
2437 case ac_image_atomic_cmpswap:
2438 name = "atomic.";
2439 atomic_subop = "cmpswap";
2440 break;
2441 case ac_image_get_lod: name = "getlod"; break;
2442 case ac_image_get_resinfo: name = "getresinfo"; break;
2443 default: unreachable("invalid image opcode");
2444 }
2445
2446 const char *dimname;
2447 switch (dim) {
2448 case ac_image_1d: dimname = "1d"; break;
2449 case ac_image_2d: dimname = "2d"; break;
2450 case ac_image_3d: dimname = "3d"; break;
2451 case ac_image_cube: dimname = "cube"; break;
2452 case ac_image_1darray: dimname = "1darray"; break;
2453 case ac_image_2darray: dimname = "2darray"; break;
2454 case ac_image_2dmsaa: dimname = "2dmsaa"; break;
2455 case ac_image_2darraymsaa: dimname = "2darraymsaa"; break;
2456 default: unreachable("invalid dim");
2457 }
2458
2459 bool lod_suffix =
2460 a->lod && (a->opcode == ac_image_sample || a->opcode == ac_image_gather4);
2461 char intr_name[96];
2462 snprintf(intr_name, sizeof(intr_name),
2463 "llvm.amdgcn.image.%s%s" /* base name */
2464 "%s%s%s" /* sample/gather modifiers */
2465 ".%s.%s%s%s%s", /* dimension and type overloads */
2466 name, atomic_subop,
2467 a->compare ? ".c" : "",
2468 a->bias ? ".b" :
2469 lod_suffix ? ".l" :
2470 a->derivs[0] ? ".d" :
2471 a->level_zero ? ".lz" : "",
2472 a->offset ? ".o" : "",
2473 dimname,
2474 atomic ? "i32" : "v4f32",
2475 overload[0], overload[1], overload[2]);
2476
2477 LLVMTypeRef retty;
2478 if (atomic)
2479 retty = ctx->i32;
2480 else if (a->opcode == ac_image_store || a->opcode == ac_image_store_mip)
2481 retty = ctx->voidt;
2482 else
2483 retty = ctx->v4f32;
2484
2485 LLVMValueRef result =
2486 ac_build_intrinsic(ctx, intr_name, retty, args, num_args,
2487 a->attributes);
2488 if (!sample && retty == ctx->v4f32) {
2489 result = LLVMBuildBitCast(ctx->builder, result,
2490 ctx->v4i32, "");
2491 }
2492 return result;
2493 }
2494
2495 LLVMValueRef ac_build_cvt_pkrtz_f16(struct ac_llvm_context *ctx,
2496 LLVMValueRef args[2])
2497 {
2498 LLVMTypeRef v2f16 =
2499 LLVMVectorType(LLVMHalfTypeInContext(ctx->context), 2);
2500
2501 return ac_build_intrinsic(ctx, "llvm.amdgcn.cvt.pkrtz", v2f16,
2502 args, 2, AC_FUNC_ATTR_READNONE);
2503 }
2504
2505 LLVMValueRef ac_build_cvt_pknorm_i16(struct ac_llvm_context *ctx,
2506 LLVMValueRef args[2])
2507 {
2508 LLVMValueRef res =
2509 ac_build_intrinsic(ctx, "llvm.amdgcn.cvt.pknorm.i16",
2510 ctx->v2i16, args, 2,
2511 AC_FUNC_ATTR_READNONE);
2512 return LLVMBuildBitCast(ctx->builder, res, ctx->i32, "");
2513 }
2514
2515 LLVMValueRef ac_build_cvt_pknorm_u16(struct ac_llvm_context *ctx,
2516 LLVMValueRef args[2])
2517 {
2518 LLVMValueRef res =
2519 ac_build_intrinsic(ctx, "llvm.amdgcn.cvt.pknorm.u16",
2520 ctx->v2i16, args, 2,
2521 AC_FUNC_ATTR_READNONE);
2522 return LLVMBuildBitCast(ctx->builder, res, ctx->i32, "");
2523 }
2524
2525 /* The 8-bit and 10-bit clamping is for HW workarounds. */
2526 LLVMValueRef ac_build_cvt_pk_i16(struct ac_llvm_context *ctx,
2527 LLVMValueRef args[2], unsigned bits, bool hi)
2528 {
2529 assert(bits == 8 || bits == 10 || bits == 16);
2530
2531 LLVMValueRef max_rgb = LLVMConstInt(ctx->i32,
2532 bits == 8 ? 127 : bits == 10 ? 511 : 32767, 0);
2533 LLVMValueRef min_rgb = LLVMConstInt(ctx->i32,
2534 bits == 8 ? -128 : bits == 10 ? -512 : -32768, 0);
2535 LLVMValueRef max_alpha =
2536 bits != 10 ? max_rgb : ctx->i32_1;
2537 LLVMValueRef min_alpha =
2538 bits != 10 ? min_rgb : LLVMConstInt(ctx->i32, -2, 0);
2539
2540 /* Clamp. */
2541 if (bits != 16) {
2542 for (int i = 0; i < 2; i++) {
2543 bool alpha = hi && i == 1;
2544 args[i] = ac_build_imin(ctx, args[i],
2545 alpha ? max_alpha : max_rgb);
2546 args[i] = ac_build_imax(ctx, args[i],
2547 alpha ? min_alpha : min_rgb);
2548 }
2549 }
2550
2551 LLVMValueRef res =
2552 ac_build_intrinsic(ctx, "llvm.amdgcn.cvt.pk.i16",
2553 ctx->v2i16, args, 2,
2554 AC_FUNC_ATTR_READNONE);
2555 return LLVMBuildBitCast(ctx->builder, res, ctx->i32, "");
2556 }
2557
2558 /* The 8-bit and 10-bit clamping is for HW workarounds. */
2559 LLVMValueRef ac_build_cvt_pk_u16(struct ac_llvm_context *ctx,
2560 LLVMValueRef args[2], unsigned bits, bool hi)
2561 {
2562 assert(bits == 8 || bits == 10 || bits == 16);
2563
2564 LLVMValueRef max_rgb = LLVMConstInt(ctx->i32,
2565 bits == 8 ? 255 : bits == 10 ? 1023 : 65535, 0);
2566 LLVMValueRef max_alpha =
2567 bits != 10 ? max_rgb : LLVMConstInt(ctx->i32, 3, 0);
2568
2569 /* Clamp. */
2570 if (bits != 16) {
2571 for (int i = 0; i < 2; i++) {
2572 bool alpha = hi && i == 1;
2573 args[i] = ac_build_umin(ctx, args[i],
2574 alpha ? max_alpha : max_rgb);
2575 }
2576 }
2577
2578 LLVMValueRef res =
2579 ac_build_intrinsic(ctx, "llvm.amdgcn.cvt.pk.u16",
2580 ctx->v2i16, args, 2,
2581 AC_FUNC_ATTR_READNONE);
2582 return LLVMBuildBitCast(ctx->builder, res, ctx->i32, "");
2583 }
2584
2585 LLVMValueRef ac_build_wqm_vote(struct ac_llvm_context *ctx, LLVMValueRef i1)
2586 {
2587 return ac_build_intrinsic(ctx, "llvm.amdgcn.wqm.vote", ctx->i1,
2588 &i1, 1, AC_FUNC_ATTR_READNONE);
2589 }
2590
2591 void ac_build_kill_if_false(struct ac_llvm_context *ctx, LLVMValueRef i1)
2592 {
2593 ac_build_intrinsic(ctx, "llvm.amdgcn.kill", ctx->voidt,
2594 &i1, 1, 0);
2595 }
2596
2597 LLVMValueRef ac_build_bfe(struct ac_llvm_context *ctx, LLVMValueRef input,
2598 LLVMValueRef offset, LLVMValueRef width,
2599 bool is_signed)
2600 {
2601 LLVMValueRef args[] = {
2602 input,
2603 offset,
2604 width,
2605 };
2606
2607 return ac_build_intrinsic(ctx, is_signed ? "llvm.amdgcn.sbfe.i32" :
2608 "llvm.amdgcn.ubfe.i32",
2609 ctx->i32, args, 3, AC_FUNC_ATTR_READNONE);
2610
2611 }
2612
2613 LLVMValueRef ac_build_imad(struct ac_llvm_context *ctx, LLVMValueRef s0,
2614 LLVMValueRef s1, LLVMValueRef s2)
2615 {
2616 return LLVMBuildAdd(ctx->builder,
2617 LLVMBuildMul(ctx->builder, s0, s1, ""), s2, "");
2618 }
2619
2620 LLVMValueRef ac_build_fmad(struct ac_llvm_context *ctx, LLVMValueRef s0,
2621 LLVMValueRef s1, LLVMValueRef s2)
2622 {
2623 /* FMA is better on GFX10, because it has FMA units instead of MUL-ADD units. */
2624 if (ctx->chip_class >= GFX10) {
2625 return ac_build_intrinsic(ctx, "llvm.fma.f32", ctx->f32,
2626 (LLVMValueRef []) {s0, s1, s2}, 3,
2627 AC_FUNC_ATTR_READNONE);
2628 }
2629
2630 return LLVMBuildFAdd(ctx->builder,
2631 LLVMBuildFMul(ctx->builder, s0, s1, ""), s2, "");
2632 }
2633
2634 void ac_build_waitcnt(struct ac_llvm_context *ctx, unsigned wait_flags)
2635 {
2636 if (!wait_flags)
2637 return;
2638
2639 unsigned lgkmcnt = 63;
2640 unsigned vmcnt = ctx->chip_class >= GFX9 ? 63 : 15;
2641 unsigned vscnt = 63;
2642
2643 if (wait_flags & AC_WAIT_LGKM)
2644 lgkmcnt = 0;
2645 if (wait_flags & AC_WAIT_VLOAD)
2646 vmcnt = 0;
2647
2648 if (wait_flags & AC_WAIT_VSTORE) {
2649 if (ctx->chip_class >= GFX10)
2650 vscnt = 0;
2651 else
2652 vmcnt = 0;
2653 }
2654
2655 /* There is no intrinsic for vscnt(0), so use a fence. */
2656 if ((wait_flags & AC_WAIT_LGKM &&
2657 wait_flags & AC_WAIT_VLOAD &&
2658 wait_flags & AC_WAIT_VSTORE) ||
2659 vscnt == 0) {
2660 LLVMBuildFence(ctx->builder, LLVMAtomicOrderingRelease, false, "");
2661 return;
2662 }
2663
2664 unsigned simm16 = (lgkmcnt << 8) |
2665 (7 << 4) | /* expcnt */
2666 (vmcnt & 0xf) |
2667 ((vmcnt >> 4) << 14);
2668
2669 LLVMValueRef args[1] = {
2670 LLVMConstInt(ctx->i32, simm16, false),
2671 };
2672 ac_build_intrinsic(ctx, "llvm.amdgcn.s.waitcnt",
2673 ctx->voidt, args, 1, 0);
2674 }
2675
2676 LLVMValueRef ac_build_fmed3(struct ac_llvm_context *ctx, LLVMValueRef src0,
2677 LLVMValueRef src1, LLVMValueRef src2,
2678 unsigned bitsize)
2679 {
2680 LLVMTypeRef type;
2681 char *intr;
2682
2683 if (bitsize == 16) {
2684 intr = "llvm.amdgcn.fmed3.f16";
2685 type = ctx->f16;
2686 } else if (bitsize == 32) {
2687 intr = "llvm.amdgcn.fmed3.f32";
2688 type = ctx->f32;
2689 } else {
2690 intr = "llvm.amdgcn.fmed3.f64";
2691 type = ctx->f64;
2692 }
2693
2694 LLVMValueRef params[] = {
2695 src0,
2696 src1,
2697 src2,
2698 };
2699 return ac_build_intrinsic(ctx, intr, type, params, 3,
2700 AC_FUNC_ATTR_READNONE);
2701 }
2702
2703 LLVMValueRef ac_build_fract(struct ac_llvm_context *ctx, LLVMValueRef src0,
2704 unsigned bitsize)
2705 {
2706 LLVMTypeRef type;
2707 char *intr;
2708
2709 if (bitsize == 16) {
2710 intr = "llvm.amdgcn.fract.f16";
2711 type = ctx->f16;
2712 } else if (bitsize == 32) {
2713 intr = "llvm.amdgcn.fract.f32";
2714 type = ctx->f32;
2715 } else {
2716 intr = "llvm.amdgcn.fract.f64";
2717 type = ctx->f64;
2718 }
2719
2720 LLVMValueRef params[] = {
2721 src0,
2722 };
2723 return ac_build_intrinsic(ctx, intr, type, params, 1,
2724 AC_FUNC_ATTR_READNONE);
2725 }
2726
2727 LLVMValueRef ac_build_isign(struct ac_llvm_context *ctx, LLVMValueRef src0,
2728 unsigned bitsize)
2729 {
2730 LLVMTypeRef type = LLVMIntTypeInContext(ctx->context, bitsize);
2731 LLVMValueRef zero = LLVMConstInt(type, 0, false);
2732 LLVMValueRef one = LLVMConstInt(type, 1, false);
2733
2734 LLVMValueRef cmp, val;
2735 cmp = LLVMBuildICmp(ctx->builder, LLVMIntSGT, src0, zero, "");
2736 val = LLVMBuildSelect(ctx->builder, cmp, one, src0, "");
2737 cmp = LLVMBuildICmp(ctx->builder, LLVMIntSGE, val, zero, "");
2738 val = LLVMBuildSelect(ctx->builder, cmp, val, LLVMConstInt(type, -1, true), "");
2739 return val;
2740 }
2741
2742 LLVMValueRef ac_build_fsign(struct ac_llvm_context *ctx, LLVMValueRef src0,
2743 unsigned bitsize)
2744 {
2745 LLVMValueRef cmp, val, zero, one;
2746 LLVMTypeRef type;
2747
2748 if (bitsize == 16) {
2749 type = ctx->f16;
2750 zero = ctx->f16_0;
2751 one = ctx->f16_1;
2752 } else if (bitsize == 32) {
2753 type = ctx->f32;
2754 zero = ctx->f32_0;
2755 one = ctx->f32_1;
2756 } else {
2757 type = ctx->f64;
2758 zero = ctx->f64_0;
2759 one = ctx->f64_1;
2760 }
2761
2762 cmp = LLVMBuildFCmp(ctx->builder, LLVMRealOGT, src0, zero, "");
2763 val = LLVMBuildSelect(ctx->builder, cmp, one, src0, "");
2764 cmp = LLVMBuildFCmp(ctx->builder, LLVMRealOGE, val, zero, "");
2765 val = LLVMBuildSelect(ctx->builder, cmp, val, LLVMConstReal(type, -1.0), "");
2766 return val;
2767 }
2768
2769 LLVMValueRef ac_build_bit_count(struct ac_llvm_context *ctx, LLVMValueRef src0)
2770 {
2771 LLVMValueRef result;
2772 unsigned bitsize;
2773
2774 bitsize = ac_get_elem_bits(ctx, LLVMTypeOf(src0));
2775
2776 switch (bitsize) {
2777 case 64:
2778 result = ac_build_intrinsic(ctx, "llvm.ctpop.i64", ctx->i64,
2779 (LLVMValueRef []) { src0 }, 1,
2780 AC_FUNC_ATTR_READNONE);
2781
2782 result = LLVMBuildTrunc(ctx->builder, result, ctx->i32, "");
2783 break;
2784 case 32:
2785 result = ac_build_intrinsic(ctx, "llvm.ctpop.i32", ctx->i32,
2786 (LLVMValueRef []) { src0 }, 1,
2787 AC_FUNC_ATTR_READNONE);
2788 break;
2789 case 16:
2790 result = ac_build_intrinsic(ctx, "llvm.ctpop.i16", ctx->i16,
2791 (LLVMValueRef []) { src0 }, 1,
2792 AC_FUNC_ATTR_READNONE);
2793
2794 result = LLVMBuildZExt(ctx->builder, result, ctx->i32, "");
2795 break;
2796 case 8:
2797 result = ac_build_intrinsic(ctx, "llvm.ctpop.i8", ctx->i8,
2798 (LLVMValueRef []) { src0 }, 1,
2799 AC_FUNC_ATTR_READNONE);
2800
2801 result = LLVMBuildZExt(ctx->builder, result, ctx->i32, "");
2802 break;
2803 default:
2804 unreachable(!"invalid bitsize");
2805 break;
2806 }
2807
2808 return result;
2809 }
2810
2811 LLVMValueRef ac_build_bitfield_reverse(struct ac_llvm_context *ctx,
2812 LLVMValueRef src0)
2813 {
2814 LLVMValueRef result;
2815 unsigned bitsize;
2816
2817 bitsize = ac_get_elem_bits(ctx, LLVMTypeOf(src0));
2818
2819 switch (bitsize) {
2820 case 64:
2821 result = ac_build_intrinsic(ctx, "llvm.bitreverse.i64", ctx->i64,
2822 (LLVMValueRef []) { src0 }, 1,
2823 AC_FUNC_ATTR_READNONE);
2824
2825 result = LLVMBuildTrunc(ctx->builder, result, ctx->i32, "");
2826 break;
2827 case 32:
2828 result = ac_build_intrinsic(ctx, "llvm.bitreverse.i32", ctx->i32,
2829 (LLVMValueRef []) { src0 }, 1,
2830 AC_FUNC_ATTR_READNONE);
2831 break;
2832 case 16:
2833 result = ac_build_intrinsic(ctx, "llvm.bitreverse.i16", ctx->i16,
2834 (LLVMValueRef []) { src0 }, 1,
2835 AC_FUNC_ATTR_READNONE);
2836
2837 result = LLVMBuildZExt(ctx->builder, result, ctx->i32, "");
2838 break;
2839 case 8:
2840 result = ac_build_intrinsic(ctx, "llvm.bitreverse.i8", ctx->i8,
2841 (LLVMValueRef []) { src0 }, 1,
2842 AC_FUNC_ATTR_READNONE);
2843
2844 result = LLVMBuildZExt(ctx->builder, result, ctx->i32, "");
2845 break;
2846 default:
2847 unreachable(!"invalid bitsize");
2848 break;
2849 }
2850
2851 return result;
2852 }
2853
2854 #define AC_EXP_TARGET 0
2855 #define AC_EXP_ENABLED_CHANNELS 1
2856 #define AC_EXP_OUT0 2
2857
2858 enum ac_ir_type {
2859 AC_IR_UNDEF,
2860 AC_IR_CONST,
2861 AC_IR_VALUE,
2862 };
2863
2864 struct ac_vs_exp_chan
2865 {
2866 LLVMValueRef value;
2867 float const_float;
2868 enum ac_ir_type type;
2869 };
2870
2871 struct ac_vs_exp_inst {
2872 unsigned offset;
2873 LLVMValueRef inst;
2874 struct ac_vs_exp_chan chan[4];
2875 };
2876
2877 struct ac_vs_exports {
2878 unsigned num;
2879 struct ac_vs_exp_inst exp[VARYING_SLOT_MAX];
2880 };
2881
2882 /* Return true if the PARAM export has been eliminated. */
2883 static bool ac_eliminate_const_output(uint8_t *vs_output_param_offset,
2884 uint32_t num_outputs,
2885 struct ac_vs_exp_inst *exp)
2886 {
2887 unsigned i, default_val; /* SPI_PS_INPUT_CNTL_i.DEFAULT_VAL */
2888 bool is_zero[4] = {}, is_one[4] = {};
2889
2890 for (i = 0; i < 4; i++) {
2891 /* It's a constant expression. Undef outputs are eliminated too. */
2892 if (exp->chan[i].type == AC_IR_UNDEF) {
2893 is_zero[i] = true;
2894 is_one[i] = true;
2895 } else if (exp->chan[i].type == AC_IR_CONST) {
2896 if (exp->chan[i].const_float == 0)
2897 is_zero[i] = true;
2898 else if (exp->chan[i].const_float == 1)
2899 is_one[i] = true;
2900 else
2901 return false; /* other constant */
2902 } else
2903 return false;
2904 }
2905
2906 /* Only certain combinations of 0 and 1 can be eliminated. */
2907 if (is_zero[0] && is_zero[1] && is_zero[2])
2908 default_val = is_zero[3] ? 0 : 1;
2909 else if (is_one[0] && is_one[1] && is_one[2])
2910 default_val = is_zero[3] ? 2 : 3;
2911 else
2912 return false;
2913
2914 /* The PARAM export can be represented as DEFAULT_VAL. Kill it. */
2915 LLVMInstructionEraseFromParent(exp->inst);
2916
2917 /* Change OFFSET to DEFAULT_VAL. */
2918 for (i = 0; i < num_outputs; i++) {
2919 if (vs_output_param_offset[i] == exp->offset) {
2920 vs_output_param_offset[i] =
2921 AC_EXP_PARAM_DEFAULT_VAL_0000 + default_val;
2922 break;
2923 }
2924 }
2925 return true;
2926 }
2927
2928 static bool ac_eliminate_duplicated_output(struct ac_llvm_context *ctx,
2929 uint8_t *vs_output_param_offset,
2930 uint32_t num_outputs,
2931 struct ac_vs_exports *processed,
2932 struct ac_vs_exp_inst *exp)
2933 {
2934 unsigned p, copy_back_channels = 0;
2935
2936 /* See if the output is already in the list of processed outputs.
2937 * The LLVMValueRef comparison relies on SSA.
2938 */
2939 for (p = 0; p < processed->num; p++) {
2940 bool different = false;
2941
2942 for (unsigned j = 0; j < 4; j++) {
2943 struct ac_vs_exp_chan *c1 = &processed->exp[p].chan[j];
2944 struct ac_vs_exp_chan *c2 = &exp->chan[j];
2945
2946 /* Treat undef as a match. */
2947 if (c2->type == AC_IR_UNDEF)
2948 continue;
2949
2950 /* If c1 is undef but c2 isn't, we can copy c2 to c1
2951 * and consider the instruction duplicated.
2952 */
2953 if (c1->type == AC_IR_UNDEF) {
2954 copy_back_channels |= 1 << j;
2955 continue;
2956 }
2957
2958 /* Test whether the channels are not equal. */
2959 if (c1->type != c2->type ||
2960 (c1->type == AC_IR_CONST &&
2961 c1->const_float != c2->const_float) ||
2962 (c1->type == AC_IR_VALUE &&
2963 c1->value != c2->value)) {
2964 different = true;
2965 break;
2966 }
2967 }
2968 if (!different)
2969 break;
2970
2971 copy_back_channels = 0;
2972 }
2973 if (p == processed->num)
2974 return false;
2975
2976 /* If a match was found, but the matching export has undef where the new
2977 * one has a normal value, copy the normal value to the undef channel.
2978 */
2979 struct ac_vs_exp_inst *match = &processed->exp[p];
2980
2981 /* Get current enabled channels mask. */
2982 LLVMValueRef arg = LLVMGetOperand(match->inst, AC_EXP_ENABLED_CHANNELS);
2983 unsigned enabled_channels = LLVMConstIntGetZExtValue(arg);
2984
2985 while (copy_back_channels) {
2986 unsigned chan = u_bit_scan(&copy_back_channels);
2987
2988 assert(match->chan[chan].type == AC_IR_UNDEF);
2989 LLVMSetOperand(match->inst, AC_EXP_OUT0 + chan,
2990 exp->chan[chan].value);
2991 match->chan[chan] = exp->chan[chan];
2992
2993 /* Update number of enabled channels because the original mask
2994 * is not always 0xf.
2995 */
2996 enabled_channels |= (1 << chan);
2997 LLVMSetOperand(match->inst, AC_EXP_ENABLED_CHANNELS,
2998 LLVMConstInt(ctx->i32, enabled_channels, 0));
2999 }
3000
3001 /* The PARAM export is duplicated. Kill it. */
3002 LLVMInstructionEraseFromParent(exp->inst);
3003
3004 /* Change OFFSET to the matching export. */
3005 for (unsigned i = 0; i < num_outputs; i++) {
3006 if (vs_output_param_offset[i] == exp->offset) {
3007 vs_output_param_offset[i] = match->offset;
3008 break;
3009 }
3010 }
3011 return true;
3012 }
3013
3014 void ac_optimize_vs_outputs(struct ac_llvm_context *ctx,
3015 LLVMValueRef main_fn,
3016 uint8_t *vs_output_param_offset,
3017 uint32_t num_outputs,
3018 uint8_t *num_param_exports)
3019 {
3020 LLVMBasicBlockRef bb;
3021 bool removed_any = false;
3022 struct ac_vs_exports exports;
3023
3024 exports.num = 0;
3025
3026 /* Process all LLVM instructions. */
3027 bb = LLVMGetFirstBasicBlock(main_fn);
3028 while (bb) {
3029 LLVMValueRef inst = LLVMGetFirstInstruction(bb);
3030
3031 while (inst) {
3032 LLVMValueRef cur = inst;
3033 inst = LLVMGetNextInstruction(inst);
3034 struct ac_vs_exp_inst exp;
3035
3036 if (LLVMGetInstructionOpcode(cur) != LLVMCall)
3037 continue;
3038
3039 LLVMValueRef callee = ac_llvm_get_called_value(cur);
3040
3041 if (!ac_llvm_is_function(callee))
3042 continue;
3043
3044 const char *name = LLVMGetValueName(callee);
3045 unsigned num_args = LLVMCountParams(callee);
3046
3047 /* Check if this is an export instruction. */
3048 if ((num_args != 9 && num_args != 8) ||
3049 (strcmp(name, "llvm.SI.export") &&
3050 strcmp(name, "llvm.amdgcn.exp.f32")))
3051 continue;
3052
3053 LLVMValueRef arg = LLVMGetOperand(cur, AC_EXP_TARGET);
3054 unsigned target = LLVMConstIntGetZExtValue(arg);
3055
3056 if (target < V_008DFC_SQ_EXP_PARAM)
3057 continue;
3058
3059 target -= V_008DFC_SQ_EXP_PARAM;
3060
3061 /* Parse the instruction. */
3062 memset(&exp, 0, sizeof(exp));
3063 exp.offset = target;
3064 exp.inst = cur;
3065
3066 for (unsigned i = 0; i < 4; i++) {
3067 LLVMValueRef v = LLVMGetOperand(cur, AC_EXP_OUT0 + i);
3068
3069 exp.chan[i].value = v;
3070
3071 if (LLVMIsUndef(v)) {
3072 exp.chan[i].type = AC_IR_UNDEF;
3073 } else if (LLVMIsAConstantFP(v)) {
3074 LLVMBool loses_info;
3075 exp.chan[i].type = AC_IR_CONST;
3076 exp.chan[i].const_float =
3077 LLVMConstRealGetDouble(v, &loses_info);
3078 } else {
3079 exp.chan[i].type = AC_IR_VALUE;
3080 }
3081 }
3082
3083 /* Eliminate constant and duplicated PARAM exports. */
3084 if (ac_eliminate_const_output(vs_output_param_offset,
3085 num_outputs, &exp) ||
3086 ac_eliminate_duplicated_output(ctx,
3087 vs_output_param_offset,
3088 num_outputs, &exports,
3089 &exp)) {
3090 removed_any = true;
3091 } else {
3092 exports.exp[exports.num++] = exp;
3093 }
3094 }
3095 bb = LLVMGetNextBasicBlock(bb);
3096 }
3097
3098 /* Remove holes in export memory due to removed PARAM exports.
3099 * This is done by renumbering all PARAM exports.
3100 */
3101 if (removed_any) {
3102 uint8_t old_offset[VARYING_SLOT_MAX];
3103 unsigned out, i;
3104
3105 /* Make a copy of the offsets. We need the old version while
3106 * we are modifying some of them. */
3107 memcpy(old_offset, vs_output_param_offset,
3108 sizeof(old_offset));
3109
3110 for (i = 0; i < exports.num; i++) {
3111 unsigned offset = exports.exp[i].offset;
3112
3113 /* Update vs_output_param_offset. Multiple outputs can
3114 * have the same offset.
3115 */
3116 for (out = 0; out < num_outputs; out++) {
3117 if (old_offset[out] == offset)
3118 vs_output_param_offset[out] = i;
3119 }
3120
3121 /* Change the PARAM offset in the instruction. */
3122 LLVMSetOperand(exports.exp[i].inst, AC_EXP_TARGET,
3123 LLVMConstInt(ctx->i32,
3124 V_008DFC_SQ_EXP_PARAM + i, 0));
3125 }
3126 *num_param_exports = exports.num;
3127 }
3128 }
3129
3130 void ac_init_exec_full_mask(struct ac_llvm_context *ctx)
3131 {
3132 LLVMValueRef full_mask = LLVMConstInt(ctx->i64, ~0ull, 0);
3133 ac_build_intrinsic(ctx,
3134 "llvm.amdgcn.init.exec", ctx->voidt,
3135 &full_mask, 1, AC_FUNC_ATTR_CONVERGENT);
3136 }
3137
3138 void ac_declare_lds_as_pointer(struct ac_llvm_context *ctx)
3139 {
3140 unsigned lds_size = ctx->chip_class >= GFX7 ? 65536 : 32768;
3141 ctx->lds = LLVMBuildIntToPtr(ctx->builder, ctx->i32_0,
3142 LLVMPointerType(LLVMArrayType(ctx->i32, lds_size / 4), AC_ADDR_SPACE_LDS),
3143 "lds");
3144 }
3145
3146 LLVMValueRef ac_lds_load(struct ac_llvm_context *ctx,
3147 LLVMValueRef dw_addr)
3148 {
3149 return LLVMBuildLoad(ctx->builder, ac_build_gep0(ctx, ctx->lds, dw_addr), "");
3150 }
3151
3152 void ac_lds_store(struct ac_llvm_context *ctx,
3153 LLVMValueRef dw_addr,
3154 LLVMValueRef value)
3155 {
3156 value = ac_to_integer(ctx, value);
3157 ac_build_indexed_store(ctx, ctx->lds,
3158 dw_addr, value);
3159 }
3160
3161 LLVMValueRef ac_find_lsb(struct ac_llvm_context *ctx,
3162 LLVMTypeRef dst_type,
3163 LLVMValueRef src0)
3164 {
3165 unsigned src0_bitsize = ac_get_elem_bits(ctx, LLVMTypeOf(src0));
3166 const char *intrin_name;
3167 LLVMTypeRef type;
3168 LLVMValueRef zero;
3169
3170 switch (src0_bitsize) {
3171 case 64:
3172 intrin_name = "llvm.cttz.i64";
3173 type = ctx->i64;
3174 zero = ctx->i64_0;
3175 break;
3176 case 32:
3177 intrin_name = "llvm.cttz.i32";
3178 type = ctx->i32;
3179 zero = ctx->i32_0;
3180 break;
3181 case 16:
3182 intrin_name = "llvm.cttz.i16";
3183 type = ctx->i16;
3184 zero = ctx->i16_0;
3185 break;
3186 case 8:
3187 intrin_name = "llvm.cttz.i8";
3188 type = ctx->i8;
3189 zero = ctx->i8_0;
3190 break;
3191 default:
3192 unreachable(!"invalid bitsize");
3193 }
3194
3195 LLVMValueRef params[2] = {
3196 src0,
3197
3198 /* The value of 1 means that ffs(x=0) = undef, so LLVM won't
3199 * add special code to check for x=0. The reason is that
3200 * the LLVM behavior for x=0 is different from what we
3201 * need here. However, LLVM also assumes that ffs(x) is
3202 * in [0, 31], but GLSL expects that ffs(0) = -1, so
3203 * a conditional assignment to handle 0 is still required.
3204 *
3205 * The hardware already implements the correct behavior.
3206 */
3207 ctx->i1true,
3208 };
3209
3210 LLVMValueRef lsb = ac_build_intrinsic(ctx, intrin_name, type,
3211 params, 2,
3212 AC_FUNC_ATTR_READNONE);
3213
3214 if (src0_bitsize == 64) {
3215 lsb = LLVMBuildTrunc(ctx->builder, lsb, ctx->i32, "");
3216 } else if (src0_bitsize < 32) {
3217 lsb = LLVMBuildSExt(ctx->builder, lsb, ctx->i32, "");
3218 }
3219
3220 /* TODO: We need an intrinsic to skip this conditional. */
3221 /* Check for zero: */
3222 return LLVMBuildSelect(ctx->builder, LLVMBuildICmp(ctx->builder,
3223 LLVMIntEQ, src0,
3224 zero, ""),
3225 LLVMConstInt(ctx->i32, -1, 0), lsb, "");
3226 }
3227
3228 LLVMTypeRef ac_array_in_const_addr_space(LLVMTypeRef elem_type)
3229 {
3230 return LLVMPointerType(elem_type, AC_ADDR_SPACE_CONST);
3231 }
3232
3233 LLVMTypeRef ac_array_in_const32_addr_space(LLVMTypeRef elem_type)
3234 {
3235 return LLVMPointerType(elem_type, AC_ADDR_SPACE_CONST_32BIT);
3236 }
3237
3238 static struct ac_llvm_flow *
3239 get_current_flow(struct ac_llvm_context *ctx)
3240 {
3241 if (ctx->flow->depth > 0)
3242 return &ctx->flow->stack[ctx->flow->depth - 1];
3243 return NULL;
3244 }
3245
3246 static struct ac_llvm_flow *
3247 get_innermost_loop(struct ac_llvm_context *ctx)
3248 {
3249 for (unsigned i = ctx->flow->depth; i > 0; --i) {
3250 if (ctx->flow->stack[i - 1].loop_entry_block)
3251 return &ctx->flow->stack[i - 1];
3252 }
3253 return NULL;
3254 }
3255
3256 static struct ac_llvm_flow *
3257 push_flow(struct ac_llvm_context *ctx)
3258 {
3259 struct ac_llvm_flow *flow;
3260
3261 if (ctx->flow->depth >= ctx->flow->depth_max) {
3262 unsigned new_max = MAX2(ctx->flow->depth << 1,
3263 AC_LLVM_INITIAL_CF_DEPTH);
3264
3265 ctx->flow->stack = realloc(ctx->flow->stack, new_max * sizeof(*ctx->flow->stack));
3266 ctx->flow->depth_max = new_max;
3267 }
3268
3269 flow = &ctx->flow->stack[ctx->flow->depth];
3270 ctx->flow->depth++;
3271
3272 flow->next_block = NULL;
3273 flow->loop_entry_block = NULL;
3274 return flow;
3275 }
3276
3277 static void set_basicblock_name(LLVMBasicBlockRef bb, const char *base,
3278 int label_id)
3279 {
3280 char buf[32];
3281 snprintf(buf, sizeof(buf), "%s%d", base, label_id);
3282 LLVMSetValueName(LLVMBasicBlockAsValue(bb), buf);
3283 }
3284
3285 /* Append a basic block at the level of the parent flow.
3286 */
3287 static LLVMBasicBlockRef append_basic_block(struct ac_llvm_context *ctx,
3288 const char *name)
3289 {
3290 assert(ctx->flow->depth >= 1);
3291
3292 if (ctx->flow->depth >= 2) {
3293 struct ac_llvm_flow *flow = &ctx->flow->stack[ctx->flow->depth - 2];
3294
3295 return LLVMInsertBasicBlockInContext(ctx->context,
3296 flow->next_block, name);
3297 }
3298
3299 LLVMValueRef main_fn =
3300 LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx->builder));
3301 return LLVMAppendBasicBlockInContext(ctx->context, main_fn, name);
3302 }
3303
3304 /* Emit a branch to the given default target for the current block if
3305 * applicable -- that is, if the current block does not already contain a
3306 * branch from a break or continue.
3307 */
3308 static void emit_default_branch(LLVMBuilderRef builder,
3309 LLVMBasicBlockRef target)
3310 {
3311 if (!LLVMGetBasicBlockTerminator(LLVMGetInsertBlock(builder)))
3312 LLVMBuildBr(builder, target);
3313 }
3314
3315 void ac_build_bgnloop(struct ac_llvm_context *ctx, int label_id)
3316 {
3317 struct ac_llvm_flow *flow = push_flow(ctx);
3318 flow->loop_entry_block = append_basic_block(ctx, "LOOP");
3319 flow->next_block = append_basic_block(ctx, "ENDLOOP");
3320 set_basicblock_name(flow->loop_entry_block, "loop", label_id);
3321 LLVMBuildBr(ctx->builder, flow->loop_entry_block);
3322 LLVMPositionBuilderAtEnd(ctx->builder, flow->loop_entry_block);
3323 }
3324
3325 void ac_build_break(struct ac_llvm_context *ctx)
3326 {
3327 struct ac_llvm_flow *flow = get_innermost_loop(ctx);
3328 LLVMBuildBr(ctx->builder, flow->next_block);
3329 }
3330
3331 void ac_build_continue(struct ac_llvm_context *ctx)
3332 {
3333 struct ac_llvm_flow *flow = get_innermost_loop(ctx);
3334 LLVMBuildBr(ctx->builder, flow->loop_entry_block);
3335 }
3336
3337 void ac_build_else(struct ac_llvm_context *ctx, int label_id)
3338 {
3339 struct ac_llvm_flow *current_branch = get_current_flow(ctx);
3340 LLVMBasicBlockRef endif_block;
3341
3342 assert(!current_branch->loop_entry_block);
3343
3344 endif_block = append_basic_block(ctx, "ENDIF");
3345 emit_default_branch(ctx->builder, endif_block);
3346
3347 LLVMPositionBuilderAtEnd(ctx->builder, current_branch->next_block);
3348 set_basicblock_name(current_branch->next_block, "else", label_id);
3349
3350 current_branch->next_block = endif_block;
3351 }
3352
3353 void ac_build_endif(struct ac_llvm_context *ctx, int label_id)
3354 {
3355 struct ac_llvm_flow *current_branch = get_current_flow(ctx);
3356
3357 assert(!current_branch->loop_entry_block);
3358
3359 emit_default_branch(ctx->builder, current_branch->next_block);
3360 LLVMPositionBuilderAtEnd(ctx->builder, current_branch->next_block);
3361 set_basicblock_name(current_branch->next_block, "endif", label_id);
3362
3363 ctx->flow->depth--;
3364 }
3365
3366 void ac_build_endloop(struct ac_llvm_context *ctx, int label_id)
3367 {
3368 struct ac_llvm_flow *current_loop = get_current_flow(ctx);
3369
3370 assert(current_loop->loop_entry_block);
3371
3372 emit_default_branch(ctx->builder, current_loop->loop_entry_block);
3373
3374 LLVMPositionBuilderAtEnd(ctx->builder, current_loop->next_block);
3375 set_basicblock_name(current_loop->next_block, "endloop", label_id);
3376 ctx->flow->depth--;
3377 }
3378
3379 void ac_build_ifcc(struct ac_llvm_context *ctx, LLVMValueRef cond, int label_id)
3380 {
3381 struct ac_llvm_flow *flow = push_flow(ctx);
3382 LLVMBasicBlockRef if_block;
3383
3384 if_block = append_basic_block(ctx, "IF");
3385 flow->next_block = append_basic_block(ctx, "ELSE");
3386 set_basicblock_name(if_block, "if", label_id);
3387 LLVMBuildCondBr(ctx->builder, cond, if_block, flow->next_block);
3388 LLVMPositionBuilderAtEnd(ctx->builder, if_block);
3389 }
3390
3391 void ac_build_if(struct ac_llvm_context *ctx, LLVMValueRef value,
3392 int label_id)
3393 {
3394 LLVMValueRef cond = LLVMBuildFCmp(ctx->builder, LLVMRealUNE,
3395 value, ctx->f32_0, "");
3396 ac_build_ifcc(ctx, cond, label_id);
3397 }
3398
3399 void ac_build_uif(struct ac_llvm_context *ctx, LLVMValueRef value,
3400 int label_id)
3401 {
3402 LLVMValueRef cond = LLVMBuildICmp(ctx->builder, LLVMIntNE,
3403 ac_to_integer(ctx, value),
3404 ctx->i32_0, "");
3405 ac_build_ifcc(ctx, cond, label_id);
3406 }
3407
3408 LLVMValueRef ac_build_alloca_undef(struct ac_llvm_context *ac, LLVMTypeRef type,
3409 const char *name)
3410 {
3411 LLVMBuilderRef builder = ac->builder;
3412 LLVMBasicBlockRef current_block = LLVMGetInsertBlock(builder);
3413 LLVMValueRef function = LLVMGetBasicBlockParent(current_block);
3414 LLVMBasicBlockRef first_block = LLVMGetEntryBasicBlock(function);
3415 LLVMValueRef first_instr = LLVMGetFirstInstruction(first_block);
3416 LLVMBuilderRef first_builder = LLVMCreateBuilderInContext(ac->context);
3417 LLVMValueRef res;
3418
3419 if (first_instr) {
3420 LLVMPositionBuilderBefore(first_builder, first_instr);
3421 } else {
3422 LLVMPositionBuilderAtEnd(first_builder, first_block);
3423 }
3424
3425 res = LLVMBuildAlloca(first_builder, type, name);
3426 LLVMDisposeBuilder(first_builder);
3427 return res;
3428 }
3429
3430 LLVMValueRef ac_build_alloca(struct ac_llvm_context *ac,
3431 LLVMTypeRef type, const char *name)
3432 {
3433 LLVMValueRef ptr = ac_build_alloca_undef(ac, type, name);
3434 LLVMBuildStore(ac->builder, LLVMConstNull(type), ptr);
3435 return ptr;
3436 }
3437
3438 LLVMValueRef ac_cast_ptr(struct ac_llvm_context *ctx, LLVMValueRef ptr,
3439 LLVMTypeRef type)
3440 {
3441 int addr_space = LLVMGetPointerAddressSpace(LLVMTypeOf(ptr));
3442 return LLVMBuildBitCast(ctx->builder, ptr,
3443 LLVMPointerType(type, addr_space), "");
3444 }
3445
3446 LLVMValueRef ac_trim_vector(struct ac_llvm_context *ctx, LLVMValueRef value,
3447 unsigned count)
3448 {
3449 unsigned num_components = ac_get_llvm_num_components(value);
3450 if (count == num_components)
3451 return value;
3452
3453 LLVMValueRef masks[MAX2(count, 2)];
3454 masks[0] = ctx->i32_0;
3455 masks[1] = ctx->i32_1;
3456 for (unsigned i = 2; i < count; i++)
3457 masks[i] = LLVMConstInt(ctx->i32, i, false);
3458
3459 if (count == 1)
3460 return LLVMBuildExtractElement(ctx->builder, value, masks[0],
3461 "");
3462
3463 LLVMValueRef swizzle = LLVMConstVector(masks, count);
3464 return LLVMBuildShuffleVector(ctx->builder, value, value, swizzle, "");
3465 }
3466
3467 LLVMValueRef ac_unpack_param(struct ac_llvm_context *ctx, LLVMValueRef param,
3468 unsigned rshift, unsigned bitwidth)
3469 {
3470 LLVMValueRef value = param;
3471 if (rshift)
3472 value = LLVMBuildLShr(ctx->builder, value,
3473 LLVMConstInt(ctx->i32, rshift, false), "");
3474
3475 if (rshift + bitwidth < 32) {
3476 unsigned mask = (1 << bitwidth) - 1;
3477 value = LLVMBuildAnd(ctx->builder, value,
3478 LLVMConstInt(ctx->i32, mask, false), "");
3479 }
3480 return value;
3481 }
3482
3483 /* Adjust the sample index according to FMASK.
3484 *
3485 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
3486 * which is the identity mapping. Each nibble says which physical sample
3487 * should be fetched to get that sample.
3488 *
3489 * For example, 0x11111100 means there are only 2 samples stored and
3490 * the second sample covers 3/4 of the pixel. When reading samples 0
3491 * and 1, return physical sample 0 (determined by the first two 0s
3492 * in FMASK), otherwise return physical sample 1.
3493 *
3494 * The sample index should be adjusted as follows:
3495 * addr[sample_index] = (fmask >> (addr[sample_index] * 4)) & 0xF;
3496 */
3497 void ac_apply_fmask_to_sample(struct ac_llvm_context *ac, LLVMValueRef fmask,
3498 LLVMValueRef *addr, bool is_array_tex)
3499 {
3500 struct ac_image_args fmask_load = {};
3501 fmask_load.opcode = ac_image_load;
3502 fmask_load.resource = fmask;
3503 fmask_load.dmask = 0xf;
3504 fmask_load.dim = is_array_tex ? ac_image_2darray : ac_image_2d;
3505 fmask_load.attributes = AC_FUNC_ATTR_READNONE;
3506
3507 fmask_load.coords[0] = addr[0];
3508 fmask_load.coords[1] = addr[1];
3509 if (is_array_tex)
3510 fmask_load.coords[2] = addr[2];
3511
3512 LLVMValueRef fmask_value = ac_build_image_opcode(ac, &fmask_load);
3513 fmask_value = LLVMBuildExtractElement(ac->builder, fmask_value,
3514 ac->i32_0, "");
3515
3516 /* Apply the formula. */
3517 unsigned sample_chan = is_array_tex ? 3 : 2;
3518 LLVMValueRef final_sample;
3519 final_sample = LLVMBuildMul(ac->builder, addr[sample_chan],
3520 LLVMConstInt(ac->i32, 4, 0), "");
3521 final_sample = LLVMBuildLShr(ac->builder, fmask_value, final_sample, "");
3522 /* Mask the sample index by 0x7, because 0x8 means an unknown value
3523 * with EQAA, so those will map to 0. */
3524 final_sample = LLVMBuildAnd(ac->builder, final_sample,
3525 LLVMConstInt(ac->i32, 0x7, 0), "");
3526
3527 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
3528 * resource descriptor is 0 (invalid).
3529 */
3530 LLVMValueRef tmp;
3531 tmp = LLVMBuildBitCast(ac->builder, fmask, ac->v8i32, "");
3532 tmp = LLVMBuildExtractElement(ac->builder, tmp, ac->i32_1, "");
3533 tmp = LLVMBuildICmp(ac->builder, LLVMIntNE, tmp, ac->i32_0, "");
3534
3535 /* Replace the MSAA sample index. */
3536 addr[sample_chan] = LLVMBuildSelect(ac->builder, tmp, final_sample,
3537 addr[sample_chan], "");
3538 }
3539
3540 static LLVMValueRef
3541 _ac_build_readlane(struct ac_llvm_context *ctx, LLVMValueRef src, LLVMValueRef lane)
3542 {
3543 ac_build_optimization_barrier(ctx, &src);
3544 return ac_build_intrinsic(ctx,
3545 lane == NULL ? "llvm.amdgcn.readfirstlane" : "llvm.amdgcn.readlane",
3546 LLVMTypeOf(src), (LLVMValueRef []) {
3547 src, lane },
3548 lane == NULL ? 1 : 2,
3549 AC_FUNC_ATTR_READNONE |
3550 AC_FUNC_ATTR_CONVERGENT);
3551 }
3552
3553 /**
3554 * Builds the "llvm.amdgcn.readlane" or "llvm.amdgcn.readfirstlane" intrinsic.
3555 * @param ctx
3556 * @param src
3557 * @param lane - id of the lane or NULL for the first active lane
3558 * @return value of the lane
3559 */
3560 LLVMValueRef
3561 ac_build_readlane(struct ac_llvm_context *ctx, LLVMValueRef src, LLVMValueRef lane)
3562 {
3563 LLVMTypeRef src_type = LLVMTypeOf(src);
3564 src = ac_to_integer(ctx, src);
3565 unsigned bits = LLVMGetIntTypeWidth(LLVMTypeOf(src));
3566 LLVMValueRef ret;
3567
3568 if (bits == 32) {
3569 ret = _ac_build_readlane(ctx, src, lane);
3570 } else {
3571 assert(bits % 32 == 0);
3572 LLVMTypeRef vec_type = LLVMVectorType(ctx->i32, bits / 32);
3573 LLVMValueRef src_vector =
3574 LLVMBuildBitCast(ctx->builder, src, vec_type, "");
3575 ret = LLVMGetUndef(vec_type);
3576 for (unsigned i = 0; i < bits / 32; i++) {
3577 src = LLVMBuildExtractElement(ctx->builder, src_vector,
3578 LLVMConstInt(ctx->i32, i, 0), "");
3579 LLVMValueRef ret_comp = _ac_build_readlane(ctx, src, lane);
3580 ret = LLVMBuildInsertElement(ctx->builder, ret, ret_comp,
3581 LLVMConstInt(ctx->i32, i, 0), "");
3582 }
3583 }
3584 if (LLVMGetTypeKind(src_type) == LLVMPointerTypeKind)
3585 return LLVMBuildIntToPtr(ctx->builder, ret, src_type, "");
3586 return LLVMBuildBitCast(ctx->builder, ret, src_type, "");
3587 }
3588
3589 LLVMValueRef
3590 ac_build_writelane(struct ac_llvm_context *ctx, LLVMValueRef src, LLVMValueRef value, LLVMValueRef lane)
3591 {
3592 return ac_build_intrinsic(ctx, "llvm.amdgcn.writelane", ctx->i32,
3593 (LLVMValueRef []) {value, lane, src}, 3,
3594 AC_FUNC_ATTR_READNONE | AC_FUNC_ATTR_CONVERGENT);
3595 }
3596
3597 LLVMValueRef
3598 ac_build_mbcnt(struct ac_llvm_context *ctx, LLVMValueRef mask)
3599 {
3600 if (ctx->wave_size == 32) {
3601 return ac_build_intrinsic(ctx, "llvm.amdgcn.mbcnt.lo", ctx->i32,
3602 (LLVMValueRef []) { mask, ctx->i32_0 },
3603 2, AC_FUNC_ATTR_READNONE);
3604 }
3605 LLVMValueRef mask_vec = LLVMBuildBitCast(ctx->builder, mask,
3606 LLVMVectorType(ctx->i32, 2),
3607 "");
3608 LLVMValueRef mask_lo = LLVMBuildExtractElement(ctx->builder, mask_vec,
3609 ctx->i32_0, "");
3610 LLVMValueRef mask_hi = LLVMBuildExtractElement(ctx->builder, mask_vec,
3611 ctx->i32_1, "");
3612 LLVMValueRef val =
3613 ac_build_intrinsic(ctx, "llvm.amdgcn.mbcnt.lo", ctx->i32,
3614 (LLVMValueRef []) { mask_lo, ctx->i32_0 },
3615 2, AC_FUNC_ATTR_READNONE);
3616 val = ac_build_intrinsic(ctx, "llvm.amdgcn.mbcnt.hi", ctx->i32,
3617 (LLVMValueRef []) { mask_hi, val },
3618 2, AC_FUNC_ATTR_READNONE);
3619 return val;
3620 }
3621
3622 enum dpp_ctrl {
3623 _dpp_quad_perm = 0x000,
3624 _dpp_row_sl = 0x100,
3625 _dpp_row_sr = 0x110,
3626 _dpp_row_rr = 0x120,
3627 dpp_wf_sl1 = 0x130,
3628 dpp_wf_rl1 = 0x134,
3629 dpp_wf_sr1 = 0x138,
3630 dpp_wf_rr1 = 0x13C,
3631 dpp_row_mirror = 0x140,
3632 dpp_row_half_mirror = 0x141,
3633 dpp_row_bcast15 = 0x142,
3634 dpp_row_bcast31 = 0x143
3635 };
3636
3637 static inline enum dpp_ctrl
3638 dpp_quad_perm(unsigned lane0, unsigned lane1, unsigned lane2, unsigned lane3)
3639 {
3640 assert(lane0 < 4 && lane1 < 4 && lane2 < 4 && lane3 < 4);
3641 return _dpp_quad_perm | lane0 | (lane1 << 2) | (lane2 << 4) | (lane3 << 6);
3642 }
3643
3644 static inline enum dpp_ctrl
3645 dpp_row_sl(unsigned amount)
3646 {
3647 assert(amount > 0 && amount < 16);
3648 return _dpp_row_sl | amount;
3649 }
3650
3651 static inline enum dpp_ctrl
3652 dpp_row_sr(unsigned amount)
3653 {
3654 assert(amount > 0 && amount < 16);
3655 return _dpp_row_sr | amount;
3656 }
3657
3658 static LLVMValueRef
3659 _ac_build_dpp(struct ac_llvm_context *ctx, LLVMValueRef old, LLVMValueRef src,
3660 enum dpp_ctrl dpp_ctrl, unsigned row_mask, unsigned bank_mask,
3661 bool bound_ctrl)
3662 {
3663 return ac_build_intrinsic(ctx, "llvm.amdgcn.update.dpp.i32",
3664 LLVMTypeOf(old),
3665 (LLVMValueRef[]) {
3666 old, src,
3667 LLVMConstInt(ctx->i32, dpp_ctrl, 0),
3668 LLVMConstInt(ctx->i32, row_mask, 0),
3669 LLVMConstInt(ctx->i32, bank_mask, 0),
3670 LLVMConstInt(ctx->i1, bound_ctrl, 0) },
3671 6, AC_FUNC_ATTR_READNONE | AC_FUNC_ATTR_CONVERGENT);
3672 }
3673
3674 static LLVMValueRef
3675 ac_build_dpp(struct ac_llvm_context *ctx, LLVMValueRef old, LLVMValueRef src,
3676 enum dpp_ctrl dpp_ctrl, unsigned row_mask, unsigned bank_mask,
3677 bool bound_ctrl)
3678 {
3679 LLVMTypeRef src_type = LLVMTypeOf(src);
3680 src = ac_to_integer(ctx, src);
3681 old = ac_to_integer(ctx, old);
3682 unsigned bits = LLVMGetIntTypeWidth(LLVMTypeOf(src));
3683 LLVMValueRef ret;
3684 if (bits == 32) {
3685 ret = _ac_build_dpp(ctx, old, src, dpp_ctrl, row_mask,
3686 bank_mask, bound_ctrl);
3687 } else {
3688 assert(bits % 32 == 0);
3689 LLVMTypeRef vec_type = LLVMVectorType(ctx->i32, bits / 32);
3690 LLVMValueRef src_vector =
3691 LLVMBuildBitCast(ctx->builder, src, vec_type, "");
3692 LLVMValueRef old_vector =
3693 LLVMBuildBitCast(ctx->builder, old, vec_type, "");
3694 ret = LLVMGetUndef(vec_type);
3695 for (unsigned i = 0; i < bits / 32; i++) {
3696 src = LLVMBuildExtractElement(ctx->builder, src_vector,
3697 LLVMConstInt(ctx->i32, i,
3698 0), "");
3699 old = LLVMBuildExtractElement(ctx->builder, old_vector,
3700 LLVMConstInt(ctx->i32, i,
3701 0), "");
3702 LLVMValueRef ret_comp = _ac_build_dpp(ctx, old, src,
3703 dpp_ctrl,
3704 row_mask,
3705 bank_mask,
3706 bound_ctrl);
3707 ret = LLVMBuildInsertElement(ctx->builder, ret,
3708 ret_comp,
3709 LLVMConstInt(ctx->i32, i,
3710 0), "");
3711 }
3712 }
3713 return LLVMBuildBitCast(ctx->builder, ret, src_type, "");
3714 }
3715
3716 static LLVMValueRef
3717 _ac_build_permlane16(struct ac_llvm_context *ctx, LLVMValueRef src, uint64_t sel,
3718 bool exchange_rows, bool bound_ctrl)
3719 {
3720 LLVMValueRef args[6] = {
3721 src,
3722 src,
3723 LLVMConstInt(ctx->i32, sel, false),
3724 LLVMConstInt(ctx->i32, sel >> 32, false),
3725 ctx->i1true, /* fi */
3726 bound_ctrl ? ctx->i1true : ctx->i1false,
3727 };
3728 return ac_build_intrinsic(ctx, exchange_rows ? "llvm.amdgcn.permlanex16"
3729 : "llvm.amdgcn.permlane16",
3730 ctx->i32, args, 6,
3731 AC_FUNC_ATTR_READNONE | AC_FUNC_ATTR_CONVERGENT);
3732 }
3733
3734 static LLVMValueRef
3735 ac_build_permlane16(struct ac_llvm_context *ctx, LLVMValueRef src, uint64_t sel,
3736 bool exchange_rows, bool bound_ctrl)
3737 {
3738 LLVMTypeRef src_type = LLVMTypeOf(src);
3739 src = ac_to_integer(ctx, src);
3740 unsigned bits = LLVMGetIntTypeWidth(LLVMTypeOf(src));
3741 LLVMValueRef ret;
3742 if (bits == 32) {
3743 ret = _ac_build_permlane16(ctx, src, sel, exchange_rows,
3744 bound_ctrl);
3745 } else {
3746 assert(bits % 32 == 0);
3747 LLVMTypeRef vec_type = LLVMVectorType(ctx->i32, bits / 32);
3748 LLVMValueRef src_vector =
3749 LLVMBuildBitCast(ctx->builder, src, vec_type, "");
3750 ret = LLVMGetUndef(vec_type);
3751 for (unsigned i = 0; i < bits / 32; i++) {
3752 src = LLVMBuildExtractElement(ctx->builder, src_vector,
3753 LLVMConstInt(ctx->i32, i,
3754 0), "");
3755 LLVMValueRef ret_comp =
3756 _ac_build_permlane16(ctx, src, sel,
3757 exchange_rows,
3758 bound_ctrl);
3759 ret = LLVMBuildInsertElement(ctx->builder, ret,
3760 ret_comp,
3761 LLVMConstInt(ctx->i32, i,
3762 0), "");
3763 }
3764 }
3765 return LLVMBuildBitCast(ctx->builder, ret, src_type, "");
3766 }
3767
3768 static inline unsigned
3769 ds_pattern_bitmode(unsigned and_mask, unsigned or_mask, unsigned xor_mask)
3770 {
3771 assert(and_mask < 32 && or_mask < 32 && xor_mask < 32);
3772 return and_mask | (or_mask << 5) | (xor_mask << 10);
3773 }
3774
3775 static LLVMValueRef
3776 _ac_build_ds_swizzle(struct ac_llvm_context *ctx, LLVMValueRef src, unsigned mask)
3777 {
3778 return ac_build_intrinsic(ctx, "llvm.amdgcn.ds.swizzle",
3779 LLVMTypeOf(src), (LLVMValueRef []) {
3780 src, LLVMConstInt(ctx->i32, mask, 0) },
3781 2, AC_FUNC_ATTR_READNONE | AC_FUNC_ATTR_CONVERGENT);
3782 }
3783
3784 LLVMValueRef
3785 ac_build_ds_swizzle(struct ac_llvm_context *ctx, LLVMValueRef src, unsigned mask)
3786 {
3787 LLVMTypeRef src_type = LLVMTypeOf(src);
3788 src = ac_to_integer(ctx, src);
3789 unsigned bits = LLVMGetIntTypeWidth(LLVMTypeOf(src));
3790 LLVMValueRef ret;
3791 if (bits == 32) {
3792 ret = _ac_build_ds_swizzle(ctx, src, mask);
3793 } else {
3794 assert(bits % 32 == 0);
3795 LLVMTypeRef vec_type = LLVMVectorType(ctx->i32, bits / 32);
3796 LLVMValueRef src_vector =
3797 LLVMBuildBitCast(ctx->builder, src, vec_type, "");
3798 ret = LLVMGetUndef(vec_type);
3799 for (unsigned i = 0; i < bits / 32; i++) {
3800 src = LLVMBuildExtractElement(ctx->builder, src_vector,
3801 LLVMConstInt(ctx->i32, i,
3802 0), "");
3803 LLVMValueRef ret_comp = _ac_build_ds_swizzle(ctx, src,
3804 mask);
3805 ret = LLVMBuildInsertElement(ctx->builder, ret,
3806 ret_comp,
3807 LLVMConstInt(ctx->i32, i,
3808 0), "");
3809 }
3810 }
3811 return LLVMBuildBitCast(ctx->builder, ret, src_type, "");
3812 }
3813
3814 static LLVMValueRef
3815 ac_build_wwm(struct ac_llvm_context *ctx, LLVMValueRef src)
3816 {
3817 char name[32], type[8];
3818 ac_build_type_name_for_intr(LLVMTypeOf(src), type, sizeof(type));
3819 snprintf(name, sizeof(name), "llvm.amdgcn.wwm.%s", type);
3820 return ac_build_intrinsic(ctx, name, LLVMTypeOf(src),
3821 (LLVMValueRef []) { src }, 1,
3822 AC_FUNC_ATTR_READNONE);
3823 }
3824
3825 static LLVMValueRef
3826 ac_build_set_inactive(struct ac_llvm_context *ctx, LLVMValueRef src,
3827 LLVMValueRef inactive)
3828 {
3829 char name[33], type[8];
3830 LLVMTypeRef src_type = LLVMTypeOf(src);
3831 src = ac_to_integer(ctx, src);
3832 inactive = ac_to_integer(ctx, inactive);
3833 ac_build_type_name_for_intr(LLVMTypeOf(src), type, sizeof(type));
3834 snprintf(name, sizeof(name), "llvm.amdgcn.set.inactive.%s", type);
3835 LLVMValueRef ret =
3836 ac_build_intrinsic(ctx, name,
3837 LLVMTypeOf(src), (LLVMValueRef []) {
3838 src, inactive }, 2,
3839 AC_FUNC_ATTR_READNONE |
3840 AC_FUNC_ATTR_CONVERGENT);
3841 return LLVMBuildBitCast(ctx->builder, ret, src_type, "");
3842 }
3843
3844 static LLVMValueRef
3845 get_reduction_identity(struct ac_llvm_context *ctx, nir_op op, unsigned type_size)
3846 {
3847 if (type_size == 4) {
3848 switch (op) {
3849 case nir_op_iadd: return ctx->i32_0;
3850 case nir_op_fadd: return ctx->f32_0;
3851 case nir_op_imul: return ctx->i32_1;
3852 case nir_op_fmul: return ctx->f32_1;
3853 case nir_op_imin: return LLVMConstInt(ctx->i32, INT32_MAX, 0);
3854 case nir_op_umin: return LLVMConstInt(ctx->i32, UINT32_MAX, 0);
3855 case nir_op_fmin: return LLVMConstReal(ctx->f32, INFINITY);
3856 case nir_op_imax: return LLVMConstInt(ctx->i32, INT32_MIN, 0);
3857 case nir_op_umax: return ctx->i32_0;
3858 case nir_op_fmax: return LLVMConstReal(ctx->f32, -INFINITY);
3859 case nir_op_iand: return LLVMConstInt(ctx->i32, -1, 0);
3860 case nir_op_ior: return ctx->i32_0;
3861 case nir_op_ixor: return ctx->i32_0;
3862 default:
3863 unreachable("bad reduction intrinsic");
3864 }
3865 } else { /* type_size == 64bit */
3866 switch (op) {
3867 case nir_op_iadd: return ctx->i64_0;
3868 case nir_op_fadd: return ctx->f64_0;
3869 case nir_op_imul: return ctx->i64_1;
3870 case nir_op_fmul: return ctx->f64_1;
3871 case nir_op_imin: return LLVMConstInt(ctx->i64, INT64_MAX, 0);
3872 case nir_op_umin: return LLVMConstInt(ctx->i64, UINT64_MAX, 0);
3873 case nir_op_fmin: return LLVMConstReal(ctx->f64, INFINITY);
3874 case nir_op_imax: return LLVMConstInt(ctx->i64, INT64_MIN, 0);
3875 case nir_op_umax: return ctx->i64_0;
3876 case nir_op_fmax: return LLVMConstReal(ctx->f64, -INFINITY);
3877 case nir_op_iand: return LLVMConstInt(ctx->i64, -1, 0);
3878 case nir_op_ior: return ctx->i64_0;
3879 case nir_op_ixor: return ctx->i64_0;
3880 default:
3881 unreachable("bad reduction intrinsic");
3882 }
3883 }
3884 }
3885
3886 static LLVMValueRef
3887 ac_build_alu_op(struct ac_llvm_context *ctx, LLVMValueRef lhs, LLVMValueRef rhs, nir_op op)
3888 {
3889 bool _64bit = ac_get_type_size(LLVMTypeOf(lhs)) == 8;
3890 switch (op) {
3891 case nir_op_iadd: return LLVMBuildAdd(ctx->builder, lhs, rhs, "");
3892 case nir_op_fadd: return LLVMBuildFAdd(ctx->builder, lhs, rhs, "");
3893 case nir_op_imul: return LLVMBuildMul(ctx->builder, lhs, rhs, "");
3894 case nir_op_fmul: return LLVMBuildFMul(ctx->builder, lhs, rhs, "");
3895 case nir_op_imin: return LLVMBuildSelect(ctx->builder,
3896 LLVMBuildICmp(ctx->builder, LLVMIntSLT, lhs, rhs, ""),
3897 lhs, rhs, "");
3898 case nir_op_umin: return LLVMBuildSelect(ctx->builder,
3899 LLVMBuildICmp(ctx->builder, LLVMIntULT, lhs, rhs, ""),
3900 lhs, rhs, "");
3901 case nir_op_fmin: return ac_build_intrinsic(ctx,
3902 _64bit ? "llvm.minnum.f64" : "llvm.minnum.f32",
3903 _64bit ? ctx->f64 : ctx->f32,
3904 (LLVMValueRef[]){lhs, rhs}, 2, AC_FUNC_ATTR_READNONE);
3905 case nir_op_imax: return LLVMBuildSelect(ctx->builder,
3906 LLVMBuildICmp(ctx->builder, LLVMIntSGT, lhs, rhs, ""),
3907 lhs, rhs, "");
3908 case nir_op_umax: return LLVMBuildSelect(ctx->builder,
3909 LLVMBuildICmp(ctx->builder, LLVMIntUGT, lhs, rhs, ""),
3910 lhs, rhs, "");
3911 case nir_op_fmax: return ac_build_intrinsic(ctx,
3912 _64bit ? "llvm.maxnum.f64" : "llvm.maxnum.f32",
3913 _64bit ? ctx->f64 : ctx->f32,
3914 (LLVMValueRef[]){lhs, rhs}, 2, AC_FUNC_ATTR_READNONE);
3915 case nir_op_iand: return LLVMBuildAnd(ctx->builder, lhs, rhs, "");
3916 case nir_op_ior: return LLVMBuildOr(ctx->builder, lhs, rhs, "");
3917 case nir_op_ixor: return LLVMBuildXor(ctx->builder, lhs, rhs, "");
3918 default:
3919 unreachable("bad reduction intrinsic");
3920 }
3921 }
3922
3923 /**
3924 * \param maxprefix specifies that the result only needs to be correct for a
3925 * prefix of this many threads
3926 *
3927 * TODO: add inclusive and excluse scan functions for GFX6.
3928 */
3929 static LLVMValueRef
3930 ac_build_scan(struct ac_llvm_context *ctx, nir_op op, LLVMValueRef src, LLVMValueRef identity,
3931 unsigned maxprefix, bool inclusive)
3932 {
3933 LLVMValueRef result, tmp;
3934
3935 if (ctx->chip_class >= GFX10) {
3936 result = inclusive ? src : identity;
3937 } else {
3938 if (!inclusive)
3939 src = ac_build_dpp(ctx, identity, src, dpp_wf_sr1, 0xf, 0xf, false);
3940 result = src;
3941 }
3942 if (maxprefix <= 1)
3943 return result;
3944 tmp = ac_build_dpp(ctx, identity, src, dpp_row_sr(1), 0xf, 0xf, false);
3945 result = ac_build_alu_op(ctx, result, tmp, op);
3946 if (maxprefix <= 2)
3947 return result;
3948 tmp = ac_build_dpp(ctx, identity, src, dpp_row_sr(2), 0xf, 0xf, false);
3949 result = ac_build_alu_op(ctx, result, tmp, op);
3950 if (maxprefix <= 3)
3951 return result;
3952 tmp = ac_build_dpp(ctx, identity, src, dpp_row_sr(3), 0xf, 0xf, false);
3953 result = ac_build_alu_op(ctx, result, tmp, op);
3954 if (maxprefix <= 4)
3955 return result;
3956 tmp = ac_build_dpp(ctx, identity, result, dpp_row_sr(4), 0xf, 0xe, false);
3957 result = ac_build_alu_op(ctx, result, tmp, op);
3958 if (maxprefix <= 8)
3959 return result;
3960 tmp = ac_build_dpp(ctx, identity, result, dpp_row_sr(8), 0xf, 0xc, false);
3961 result = ac_build_alu_op(ctx, result, tmp, op);
3962 if (maxprefix <= 16)
3963 return result;
3964
3965 if (ctx->chip_class >= GFX10) {
3966 /* dpp_row_bcast{15,31} are not supported on gfx10. */
3967 LLVMBuilderRef builder = ctx->builder;
3968 LLVMValueRef tid = ac_get_thread_id(ctx);
3969 LLVMValueRef cc;
3970 /* TODO-GFX10: Can we get better code-gen by putting this into
3971 * a branch so that LLVM generates EXEC mask manipulations? */
3972 if (inclusive)
3973 tmp = result;
3974 else
3975 tmp = ac_build_alu_op(ctx, result, src, op);
3976 tmp = ac_build_permlane16(ctx, tmp, ~(uint64_t)0, true, false);
3977 tmp = ac_build_alu_op(ctx, result, tmp, op);
3978 cc = LLVMBuildAnd(builder, tid, LLVMConstInt(ctx->i32, 16, false), "");
3979 cc = LLVMBuildICmp(builder, LLVMIntNE, cc, ctx->i32_0, "");
3980 result = LLVMBuildSelect(builder, cc, tmp, result, "");
3981 if (maxprefix <= 32)
3982 return result;
3983
3984 if (inclusive)
3985 tmp = result;
3986 else
3987 tmp = ac_build_alu_op(ctx, result, src, op);
3988 tmp = ac_build_readlane(ctx, tmp, LLVMConstInt(ctx->i32, 31, false));
3989 tmp = ac_build_alu_op(ctx, result, tmp, op);
3990 cc = LLVMBuildICmp(builder, LLVMIntUGE, tid,
3991 LLVMConstInt(ctx->i32, 32, false), "");
3992 result = LLVMBuildSelect(builder, cc, tmp, result, "");
3993 return result;
3994 }
3995
3996 tmp = ac_build_dpp(ctx, identity, result, dpp_row_bcast15, 0xa, 0xf, false);
3997 result = ac_build_alu_op(ctx, result, tmp, op);
3998 if (maxprefix <= 32)
3999 return result;
4000 tmp = ac_build_dpp(ctx, identity, result, dpp_row_bcast31, 0xc, 0xf, false);
4001 result = ac_build_alu_op(ctx, result, tmp, op);
4002 return result;
4003 }
4004
4005 LLVMValueRef
4006 ac_build_inclusive_scan(struct ac_llvm_context *ctx, LLVMValueRef src, nir_op op)
4007 {
4008 LLVMValueRef result;
4009
4010 if (LLVMTypeOf(src) == ctx->i1 && op == nir_op_iadd) {
4011 LLVMBuilderRef builder = ctx->builder;
4012 src = LLVMBuildZExt(builder, src, ctx->i32, "");
4013 result = ac_build_ballot(ctx, src);
4014 result = ac_build_mbcnt(ctx, result);
4015 result = LLVMBuildAdd(builder, result, src, "");
4016 return result;
4017 }
4018
4019 ac_build_optimization_barrier(ctx, &src);
4020
4021 LLVMValueRef identity =
4022 get_reduction_identity(ctx, op, ac_get_type_size(LLVMTypeOf(src)));
4023 result = LLVMBuildBitCast(ctx->builder, ac_build_set_inactive(ctx, src, identity),
4024 LLVMTypeOf(identity), "");
4025 result = ac_build_scan(ctx, op, result, identity, ctx->wave_size, true);
4026
4027 return ac_build_wwm(ctx, result);
4028 }
4029
4030 LLVMValueRef
4031 ac_build_exclusive_scan(struct ac_llvm_context *ctx, LLVMValueRef src, nir_op op)
4032 {
4033 LLVMValueRef result;
4034
4035 if (LLVMTypeOf(src) == ctx->i1 && op == nir_op_iadd) {
4036 LLVMBuilderRef builder = ctx->builder;
4037 src = LLVMBuildZExt(builder, src, ctx->i32, "");
4038 result = ac_build_ballot(ctx, src);
4039 result = ac_build_mbcnt(ctx, result);
4040 return result;
4041 }
4042
4043 ac_build_optimization_barrier(ctx, &src);
4044
4045 LLVMValueRef identity =
4046 get_reduction_identity(ctx, op, ac_get_type_size(LLVMTypeOf(src)));
4047 result = LLVMBuildBitCast(ctx->builder, ac_build_set_inactive(ctx, src, identity),
4048 LLVMTypeOf(identity), "");
4049 result = ac_build_scan(ctx, op, result, identity, ctx->wave_size, false);
4050
4051 return ac_build_wwm(ctx, result);
4052 }
4053
4054 LLVMValueRef
4055 ac_build_reduce(struct ac_llvm_context *ctx, LLVMValueRef src, nir_op op, unsigned cluster_size)
4056 {
4057 if (cluster_size == 1) return src;
4058 ac_build_optimization_barrier(ctx, &src);
4059 LLVMValueRef result, swap;
4060 LLVMValueRef identity = get_reduction_identity(ctx, op,
4061 ac_get_type_size(LLVMTypeOf(src)));
4062 result = LLVMBuildBitCast(ctx->builder,
4063 ac_build_set_inactive(ctx, src, identity),
4064 LLVMTypeOf(identity), "");
4065 swap = ac_build_quad_swizzle(ctx, result, 1, 0, 3, 2);
4066 result = ac_build_alu_op(ctx, result, swap, op);
4067 if (cluster_size == 2) return ac_build_wwm(ctx, result);
4068
4069 swap = ac_build_quad_swizzle(ctx, result, 2, 3, 0, 1);
4070 result = ac_build_alu_op(ctx, result, swap, op);
4071 if (cluster_size == 4) return ac_build_wwm(ctx, result);
4072
4073 if (ctx->chip_class >= GFX8)
4074 swap = ac_build_dpp(ctx, identity, result, dpp_row_half_mirror, 0xf, 0xf, false);
4075 else
4076 swap = ac_build_ds_swizzle(ctx, result, ds_pattern_bitmode(0x1f, 0, 0x04));
4077 result = ac_build_alu_op(ctx, result, swap, op);
4078 if (cluster_size == 8) return ac_build_wwm(ctx, result);
4079
4080 if (ctx->chip_class >= GFX8)
4081 swap = ac_build_dpp(ctx, identity, result, dpp_row_mirror, 0xf, 0xf, false);
4082 else
4083 swap = ac_build_ds_swizzle(ctx, result, ds_pattern_bitmode(0x1f, 0, 0x08));
4084 result = ac_build_alu_op(ctx, result, swap, op);
4085 if (cluster_size == 16) return ac_build_wwm(ctx, result);
4086
4087 if (ctx->chip_class >= GFX10)
4088 swap = ac_build_permlane16(ctx, result, 0, true, false);
4089 else if (ctx->chip_class >= GFX8 && cluster_size != 32)
4090 swap = ac_build_dpp(ctx, identity, result, dpp_row_bcast15, 0xa, 0xf, false);
4091 else
4092 swap = ac_build_ds_swizzle(ctx, result, ds_pattern_bitmode(0x1f, 0, 0x10));
4093 result = ac_build_alu_op(ctx, result, swap, op);
4094 if (cluster_size == 32) return ac_build_wwm(ctx, result);
4095
4096 if (ctx->chip_class >= GFX8) {
4097 if (ctx->chip_class >= GFX10)
4098 swap = ac_build_readlane(ctx, result, LLVMConstInt(ctx->i32, 31, false));
4099 else
4100 swap = ac_build_dpp(ctx, identity, result, dpp_row_bcast31, 0xc, 0xf, false);
4101 result = ac_build_alu_op(ctx, result, swap, op);
4102 result = ac_build_readlane(ctx, result, LLVMConstInt(ctx->i32, 63, 0));
4103 return ac_build_wwm(ctx, result);
4104 } else {
4105 swap = ac_build_readlane(ctx, result, ctx->i32_0);
4106 result = ac_build_readlane(ctx, result, LLVMConstInt(ctx->i32, 32, 0));
4107 result = ac_build_alu_op(ctx, result, swap, op);
4108 return ac_build_wwm(ctx, result);
4109 }
4110 }
4111
4112 /**
4113 * "Top half" of a scan that reduces per-wave values across an entire
4114 * workgroup.
4115 *
4116 * The source value must be present in the highest lane of the wave, and the
4117 * highest lane must be live.
4118 */
4119 void
4120 ac_build_wg_wavescan_top(struct ac_llvm_context *ctx, struct ac_wg_scan *ws)
4121 {
4122 if (ws->maxwaves <= 1)
4123 return;
4124
4125 const LLVMValueRef last_lane = LLVMConstInt(ctx->i32, ctx->wave_size - 1, false);
4126 LLVMBuilderRef builder = ctx->builder;
4127 LLVMValueRef tid = ac_get_thread_id(ctx);
4128 LLVMValueRef tmp;
4129
4130 tmp = LLVMBuildICmp(builder, LLVMIntEQ, tid, last_lane, "");
4131 ac_build_ifcc(ctx, tmp, 1000);
4132 LLVMBuildStore(builder, ws->src, LLVMBuildGEP(builder, ws->scratch, &ws->waveidx, 1, ""));
4133 ac_build_endif(ctx, 1000);
4134 }
4135
4136 /**
4137 * "Bottom half" of a scan that reduces per-wave values across an entire
4138 * workgroup.
4139 *
4140 * The caller must place a barrier between the top and bottom halves.
4141 */
4142 void
4143 ac_build_wg_wavescan_bottom(struct ac_llvm_context *ctx, struct ac_wg_scan *ws)
4144 {
4145 const LLVMTypeRef type = LLVMTypeOf(ws->src);
4146 const LLVMValueRef identity =
4147 get_reduction_identity(ctx, ws->op, ac_get_type_size(type));
4148
4149 if (ws->maxwaves <= 1) {
4150 ws->result_reduce = ws->src;
4151 ws->result_inclusive = ws->src;
4152 ws->result_exclusive = identity;
4153 return;
4154 }
4155 assert(ws->maxwaves <= 32);
4156
4157 LLVMBuilderRef builder = ctx->builder;
4158 LLVMValueRef tid = ac_get_thread_id(ctx);
4159 LLVMBasicBlockRef bbs[2];
4160 LLVMValueRef phivalues_scan[2];
4161 LLVMValueRef tmp, tmp2;
4162
4163 bbs[0] = LLVMGetInsertBlock(builder);
4164 phivalues_scan[0] = LLVMGetUndef(type);
4165
4166 if (ws->enable_reduce)
4167 tmp = LLVMBuildICmp(builder, LLVMIntULT, tid, ws->numwaves, "");
4168 else if (ws->enable_inclusive)
4169 tmp = LLVMBuildICmp(builder, LLVMIntULE, tid, ws->waveidx, "");
4170 else
4171 tmp = LLVMBuildICmp(builder, LLVMIntULT, tid, ws->waveidx, "");
4172 ac_build_ifcc(ctx, tmp, 1001);
4173 {
4174 tmp = LLVMBuildLoad(builder, LLVMBuildGEP(builder, ws->scratch, &tid, 1, ""), "");
4175
4176 ac_build_optimization_barrier(ctx, &tmp);
4177
4178 bbs[1] = LLVMGetInsertBlock(builder);
4179 phivalues_scan[1] = ac_build_scan(ctx, ws->op, tmp, identity, ws->maxwaves, true);
4180 }
4181 ac_build_endif(ctx, 1001);
4182
4183 const LLVMValueRef scan = ac_build_phi(ctx, type, 2, phivalues_scan, bbs);
4184
4185 if (ws->enable_reduce) {
4186 tmp = LLVMBuildSub(builder, ws->numwaves, ctx->i32_1, "");
4187 ws->result_reduce = ac_build_readlane(ctx, scan, tmp);
4188 }
4189 if (ws->enable_inclusive)
4190 ws->result_inclusive = ac_build_readlane(ctx, scan, ws->waveidx);
4191 if (ws->enable_exclusive) {
4192 tmp = LLVMBuildSub(builder, ws->waveidx, ctx->i32_1, "");
4193 tmp = ac_build_readlane(ctx, scan, tmp);
4194 tmp2 = LLVMBuildICmp(builder, LLVMIntEQ, ws->waveidx, ctx->i32_0, "");
4195 ws->result_exclusive = LLVMBuildSelect(builder, tmp2, identity, tmp, "");
4196 }
4197 }
4198
4199 /**
4200 * Inclusive scan of a per-wave value across an entire workgroup.
4201 *
4202 * This implies an s_barrier instruction.
4203 *
4204 * Unlike ac_build_inclusive_scan, the caller \em must ensure that all threads
4205 * of the workgroup are live. (This requirement cannot easily be relaxed in a
4206 * useful manner because of the barrier in the algorithm.)
4207 */
4208 void
4209 ac_build_wg_wavescan(struct ac_llvm_context *ctx, struct ac_wg_scan *ws)
4210 {
4211 ac_build_wg_wavescan_top(ctx, ws);
4212 ac_build_s_barrier(ctx);
4213 ac_build_wg_wavescan_bottom(ctx, ws);
4214 }
4215
4216 /**
4217 * "Top half" of a scan that reduces per-thread values across an entire
4218 * workgroup.
4219 *
4220 * All lanes must be active when this code runs.
4221 */
4222 void
4223 ac_build_wg_scan_top(struct ac_llvm_context *ctx, struct ac_wg_scan *ws)
4224 {
4225 if (ws->enable_exclusive) {
4226 ws->extra = ac_build_exclusive_scan(ctx, ws->src, ws->op);
4227 if (LLVMTypeOf(ws->src) == ctx->i1 && ws->op == nir_op_iadd)
4228 ws->src = LLVMBuildZExt(ctx->builder, ws->src, ctx->i32, "");
4229 ws->src = ac_build_alu_op(ctx, ws->extra, ws->src, ws->op);
4230 } else {
4231 ws->src = ac_build_inclusive_scan(ctx, ws->src, ws->op);
4232 }
4233
4234 bool enable_inclusive = ws->enable_inclusive;
4235 bool enable_exclusive = ws->enable_exclusive;
4236 ws->enable_inclusive = false;
4237 ws->enable_exclusive = ws->enable_exclusive || enable_inclusive;
4238 ac_build_wg_wavescan_top(ctx, ws);
4239 ws->enable_inclusive = enable_inclusive;
4240 ws->enable_exclusive = enable_exclusive;
4241 }
4242
4243 /**
4244 * "Bottom half" of a scan that reduces per-thread values across an entire
4245 * workgroup.
4246 *
4247 * The caller must place a barrier between the top and bottom halves.
4248 */
4249 void
4250 ac_build_wg_scan_bottom(struct ac_llvm_context *ctx, struct ac_wg_scan *ws)
4251 {
4252 bool enable_inclusive = ws->enable_inclusive;
4253 bool enable_exclusive = ws->enable_exclusive;
4254 ws->enable_inclusive = false;
4255 ws->enable_exclusive = ws->enable_exclusive || enable_inclusive;
4256 ac_build_wg_wavescan_bottom(ctx, ws);
4257 ws->enable_inclusive = enable_inclusive;
4258 ws->enable_exclusive = enable_exclusive;
4259
4260 /* ws->result_reduce is already the correct value */
4261 if (ws->enable_inclusive)
4262 ws->result_inclusive = ac_build_alu_op(ctx, ws->result_inclusive, ws->src, ws->op);
4263 if (ws->enable_exclusive)
4264 ws->result_exclusive = ac_build_alu_op(ctx, ws->result_exclusive, ws->extra, ws->op);
4265 }
4266
4267 /**
4268 * A scan that reduces per-thread values across an entire workgroup.
4269 *
4270 * The caller must ensure that all lanes are active when this code runs
4271 * (WWM is insufficient!), because there is an implied barrier.
4272 */
4273 void
4274 ac_build_wg_scan(struct ac_llvm_context *ctx, struct ac_wg_scan *ws)
4275 {
4276 ac_build_wg_scan_top(ctx, ws);
4277 ac_build_s_barrier(ctx);
4278 ac_build_wg_scan_bottom(ctx, ws);
4279 }
4280
4281 LLVMValueRef
4282 ac_build_quad_swizzle(struct ac_llvm_context *ctx, LLVMValueRef src,
4283 unsigned lane0, unsigned lane1, unsigned lane2, unsigned lane3)
4284 {
4285 unsigned mask = dpp_quad_perm(lane0, lane1, lane2, lane3);
4286 if (ctx->chip_class >= GFX8) {
4287 return ac_build_dpp(ctx, src, src, mask, 0xf, 0xf, false);
4288 } else {
4289 return ac_build_ds_swizzle(ctx, src, (1 << 15) | mask);
4290 }
4291 }
4292
4293 LLVMValueRef
4294 ac_build_shuffle(struct ac_llvm_context *ctx, LLVMValueRef src, LLVMValueRef index)
4295 {
4296 index = LLVMBuildMul(ctx->builder, index, LLVMConstInt(ctx->i32, 4, 0), "");
4297 return ac_build_intrinsic(ctx,
4298 "llvm.amdgcn.ds.bpermute", ctx->i32,
4299 (LLVMValueRef []) {index, src}, 2,
4300 AC_FUNC_ATTR_READNONE |
4301 AC_FUNC_ATTR_CONVERGENT);
4302 }
4303
4304 LLVMValueRef
4305 ac_build_frexp_exp(struct ac_llvm_context *ctx, LLVMValueRef src0,
4306 unsigned bitsize)
4307 {
4308 LLVMTypeRef type;
4309 char *intr;
4310
4311 if (bitsize == 16) {
4312 intr = "llvm.amdgcn.frexp.exp.i16.f16";
4313 type = ctx->i16;
4314 } else if (bitsize == 32) {
4315 intr = "llvm.amdgcn.frexp.exp.i32.f32";
4316 type = ctx->i32;
4317 } else {
4318 intr = "llvm.amdgcn.frexp.exp.i32.f64";
4319 type = ctx->i32;
4320 }
4321
4322 LLVMValueRef params[] = {
4323 src0,
4324 };
4325 return ac_build_intrinsic(ctx, intr, type, params, 1,
4326 AC_FUNC_ATTR_READNONE);
4327 }
4328 LLVMValueRef
4329 ac_build_frexp_mant(struct ac_llvm_context *ctx, LLVMValueRef src0,
4330 unsigned bitsize)
4331 {
4332 LLVMTypeRef type;
4333 char *intr;
4334
4335 if (bitsize == 16) {
4336 intr = "llvm.amdgcn.frexp.mant.f16";
4337 type = ctx->f16;
4338 } else if (bitsize == 32) {
4339 intr = "llvm.amdgcn.frexp.mant.f32";
4340 type = ctx->f32;
4341 } else {
4342 intr = "llvm.amdgcn.frexp.mant.f64";
4343 type = ctx->f64;
4344 }
4345
4346 LLVMValueRef params[] = {
4347 src0,
4348 };
4349 return ac_build_intrinsic(ctx, intr, type, params, 1,
4350 AC_FUNC_ATTR_READNONE);
4351 }
4352
4353 /*
4354 * this takes an I,J coordinate pair,
4355 * and works out the X and Y derivatives.
4356 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
4357 */
4358 LLVMValueRef
4359 ac_build_ddxy_interp(struct ac_llvm_context *ctx, LLVMValueRef interp_ij)
4360 {
4361 LLVMValueRef result[4], a;
4362 unsigned i;
4363
4364 for (i = 0; i < 2; i++) {
4365 a = LLVMBuildExtractElement(ctx->builder, interp_ij,
4366 LLVMConstInt(ctx->i32, i, false), "");
4367 result[i] = ac_build_ddxy(ctx, AC_TID_MASK_TOP_LEFT, 1, a);
4368 result[2+i] = ac_build_ddxy(ctx, AC_TID_MASK_TOP_LEFT, 2, a);
4369 }
4370 return ac_build_gather_values(ctx, result, 4);
4371 }
4372
4373 LLVMValueRef
4374 ac_build_load_helper_invocation(struct ac_llvm_context *ctx)
4375 {
4376 LLVMValueRef result = ac_build_intrinsic(ctx, "llvm.amdgcn.ps.live",
4377 ctx->i1, NULL, 0,
4378 AC_FUNC_ATTR_READNONE);
4379 result = LLVMBuildNot(ctx->builder, result, "");
4380 return LLVMBuildSExt(ctx->builder, result, ctx->i32, "");
4381 }
4382
4383 LLVMValueRef ac_build_call(struct ac_llvm_context *ctx, LLVMValueRef func,
4384 LLVMValueRef *args, unsigned num_args)
4385 {
4386 LLVMValueRef ret = LLVMBuildCall(ctx->builder, func, args, num_args, "");
4387 LLVMSetInstructionCallConv(ret, LLVMGetFunctionCallConv(func));
4388 return ret;
4389 }
4390
4391 void
4392 ac_export_mrt_z(struct ac_llvm_context *ctx, LLVMValueRef depth,
4393 LLVMValueRef stencil, LLVMValueRef samplemask,
4394 struct ac_export_args *args)
4395 {
4396 unsigned mask = 0;
4397 unsigned format = ac_get_spi_shader_z_format(depth != NULL,
4398 stencil != NULL,
4399 samplemask != NULL);
4400
4401 assert(depth || stencil || samplemask);
4402
4403 memset(args, 0, sizeof(*args));
4404
4405 args->valid_mask = 1; /* whether the EXEC mask is valid */
4406 args->done = 1; /* DONE bit */
4407
4408 /* Specify the target we are exporting */
4409 args->target = V_008DFC_SQ_EXP_MRTZ;
4410
4411 args->compr = 0; /* COMP flag */
4412 args->out[0] = LLVMGetUndef(ctx->f32); /* R, depth */
4413 args->out[1] = LLVMGetUndef(ctx->f32); /* G, stencil test val[0:7], stencil op val[8:15] */
4414 args->out[2] = LLVMGetUndef(ctx->f32); /* B, sample mask */
4415 args->out[3] = LLVMGetUndef(ctx->f32); /* A, alpha to mask */
4416
4417 if (format == V_028710_SPI_SHADER_UINT16_ABGR) {
4418 assert(!depth);
4419 args->compr = 1; /* COMPR flag */
4420
4421 if (stencil) {
4422 /* Stencil should be in X[23:16]. */
4423 stencil = ac_to_integer(ctx, stencil);
4424 stencil = LLVMBuildShl(ctx->builder, stencil,
4425 LLVMConstInt(ctx->i32, 16, 0), "");
4426 args->out[0] = ac_to_float(ctx, stencil);
4427 mask |= 0x3;
4428 }
4429 if (samplemask) {
4430 /* SampleMask should be in Y[15:0]. */
4431 args->out[1] = samplemask;
4432 mask |= 0xc;
4433 }
4434 } else {
4435 if (depth) {
4436 args->out[0] = depth;
4437 mask |= 0x1;
4438 }
4439 if (stencil) {
4440 args->out[1] = stencil;
4441 mask |= 0x2;
4442 }
4443 if (samplemask) {
4444 args->out[2] = samplemask;
4445 mask |= 0x4;
4446 }
4447 }
4448
4449 /* GFX6 (except OLAND and HAINAN) has a bug that it only looks
4450 * at the X writemask component. */
4451 if (ctx->chip_class == GFX6 &&
4452 ctx->family != CHIP_OLAND &&
4453 ctx->family != CHIP_HAINAN)
4454 mask |= 0x1;
4455
4456 /* Specify which components to enable */
4457 args->enabled_channels = mask;
4458 }
4459