nvc0: add support for GL_EXT_demote_to_helper_invocation
[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_image_get_sample_count(struct ac_llvm_context *ctx,
2496 LLVMValueRef rsrc)
2497 {
2498 LLVMValueRef samples;
2499
2500 /* Read the samples from the descriptor directly.
2501 * Hardware doesn't have any instruction for this.
2502 */
2503 samples = LLVMBuildExtractElement(ctx->builder, rsrc,
2504 LLVMConstInt(ctx->i32, 3, 0), "");
2505 samples = LLVMBuildLShr(ctx->builder, samples,
2506 LLVMConstInt(ctx->i32, 16, 0), "");
2507 samples = LLVMBuildAnd(ctx->builder, samples,
2508 LLVMConstInt(ctx->i32, 0xf, 0), "");
2509 samples = LLVMBuildShl(ctx->builder, ctx->i32_1,
2510 samples, "");
2511 return samples;
2512 }
2513
2514 LLVMValueRef ac_build_cvt_pkrtz_f16(struct ac_llvm_context *ctx,
2515 LLVMValueRef args[2])
2516 {
2517 LLVMTypeRef v2f16 =
2518 LLVMVectorType(LLVMHalfTypeInContext(ctx->context), 2);
2519
2520 return ac_build_intrinsic(ctx, "llvm.amdgcn.cvt.pkrtz", v2f16,
2521 args, 2, AC_FUNC_ATTR_READNONE);
2522 }
2523
2524 LLVMValueRef ac_build_cvt_pknorm_i16(struct ac_llvm_context *ctx,
2525 LLVMValueRef args[2])
2526 {
2527 LLVMValueRef res =
2528 ac_build_intrinsic(ctx, "llvm.amdgcn.cvt.pknorm.i16",
2529 ctx->v2i16, args, 2,
2530 AC_FUNC_ATTR_READNONE);
2531 return LLVMBuildBitCast(ctx->builder, res, ctx->i32, "");
2532 }
2533
2534 LLVMValueRef ac_build_cvt_pknorm_u16(struct ac_llvm_context *ctx,
2535 LLVMValueRef args[2])
2536 {
2537 LLVMValueRef res =
2538 ac_build_intrinsic(ctx, "llvm.amdgcn.cvt.pknorm.u16",
2539 ctx->v2i16, args, 2,
2540 AC_FUNC_ATTR_READNONE);
2541 return LLVMBuildBitCast(ctx->builder, res, ctx->i32, "");
2542 }
2543
2544 /* The 8-bit and 10-bit clamping is for HW workarounds. */
2545 LLVMValueRef ac_build_cvt_pk_i16(struct ac_llvm_context *ctx,
2546 LLVMValueRef args[2], unsigned bits, bool hi)
2547 {
2548 assert(bits == 8 || bits == 10 || bits == 16);
2549
2550 LLVMValueRef max_rgb = LLVMConstInt(ctx->i32,
2551 bits == 8 ? 127 : bits == 10 ? 511 : 32767, 0);
2552 LLVMValueRef min_rgb = LLVMConstInt(ctx->i32,
2553 bits == 8 ? -128 : bits == 10 ? -512 : -32768, 0);
2554 LLVMValueRef max_alpha =
2555 bits != 10 ? max_rgb : ctx->i32_1;
2556 LLVMValueRef min_alpha =
2557 bits != 10 ? min_rgb : LLVMConstInt(ctx->i32, -2, 0);
2558
2559 /* Clamp. */
2560 if (bits != 16) {
2561 for (int i = 0; i < 2; i++) {
2562 bool alpha = hi && i == 1;
2563 args[i] = ac_build_imin(ctx, args[i],
2564 alpha ? max_alpha : max_rgb);
2565 args[i] = ac_build_imax(ctx, args[i],
2566 alpha ? min_alpha : min_rgb);
2567 }
2568 }
2569
2570 LLVMValueRef res =
2571 ac_build_intrinsic(ctx, "llvm.amdgcn.cvt.pk.i16",
2572 ctx->v2i16, args, 2,
2573 AC_FUNC_ATTR_READNONE);
2574 return LLVMBuildBitCast(ctx->builder, res, ctx->i32, "");
2575 }
2576
2577 /* The 8-bit and 10-bit clamping is for HW workarounds. */
2578 LLVMValueRef ac_build_cvt_pk_u16(struct ac_llvm_context *ctx,
2579 LLVMValueRef args[2], unsigned bits, bool hi)
2580 {
2581 assert(bits == 8 || bits == 10 || bits == 16);
2582
2583 LLVMValueRef max_rgb = LLVMConstInt(ctx->i32,
2584 bits == 8 ? 255 : bits == 10 ? 1023 : 65535, 0);
2585 LLVMValueRef max_alpha =
2586 bits != 10 ? max_rgb : LLVMConstInt(ctx->i32, 3, 0);
2587
2588 /* Clamp. */
2589 if (bits != 16) {
2590 for (int i = 0; i < 2; i++) {
2591 bool alpha = hi && i == 1;
2592 args[i] = ac_build_umin(ctx, args[i],
2593 alpha ? max_alpha : max_rgb);
2594 }
2595 }
2596
2597 LLVMValueRef res =
2598 ac_build_intrinsic(ctx, "llvm.amdgcn.cvt.pk.u16",
2599 ctx->v2i16, args, 2,
2600 AC_FUNC_ATTR_READNONE);
2601 return LLVMBuildBitCast(ctx->builder, res, ctx->i32, "");
2602 }
2603
2604 LLVMValueRef ac_build_wqm_vote(struct ac_llvm_context *ctx, LLVMValueRef i1)
2605 {
2606 return ac_build_intrinsic(ctx, "llvm.amdgcn.wqm.vote", ctx->i1,
2607 &i1, 1, AC_FUNC_ATTR_READNONE);
2608 }
2609
2610 void ac_build_kill_if_false(struct ac_llvm_context *ctx, LLVMValueRef i1)
2611 {
2612 ac_build_intrinsic(ctx, "llvm.amdgcn.kill", ctx->voidt,
2613 &i1, 1, 0);
2614 }
2615
2616 LLVMValueRef ac_build_bfe(struct ac_llvm_context *ctx, LLVMValueRef input,
2617 LLVMValueRef offset, LLVMValueRef width,
2618 bool is_signed)
2619 {
2620 LLVMValueRef args[] = {
2621 input,
2622 offset,
2623 width,
2624 };
2625
2626 return ac_build_intrinsic(ctx, is_signed ? "llvm.amdgcn.sbfe.i32" :
2627 "llvm.amdgcn.ubfe.i32",
2628 ctx->i32, args, 3, AC_FUNC_ATTR_READNONE);
2629
2630 }
2631
2632 LLVMValueRef ac_build_imad(struct ac_llvm_context *ctx, LLVMValueRef s0,
2633 LLVMValueRef s1, LLVMValueRef s2)
2634 {
2635 return LLVMBuildAdd(ctx->builder,
2636 LLVMBuildMul(ctx->builder, s0, s1, ""), s2, "");
2637 }
2638
2639 LLVMValueRef ac_build_fmad(struct ac_llvm_context *ctx, LLVMValueRef s0,
2640 LLVMValueRef s1, LLVMValueRef s2)
2641 {
2642 /* FMA is better on GFX10, because it has FMA units instead of MUL-ADD units. */
2643 if (ctx->chip_class >= GFX10) {
2644 return ac_build_intrinsic(ctx, "llvm.fma.f32", ctx->f32,
2645 (LLVMValueRef []) {s0, s1, s2}, 3,
2646 AC_FUNC_ATTR_READNONE);
2647 }
2648
2649 return LLVMBuildFAdd(ctx->builder,
2650 LLVMBuildFMul(ctx->builder, s0, s1, ""), s2, "");
2651 }
2652
2653 void ac_build_waitcnt(struct ac_llvm_context *ctx, unsigned wait_flags)
2654 {
2655 if (!wait_flags)
2656 return;
2657
2658 unsigned lgkmcnt = 63;
2659 unsigned vmcnt = ctx->chip_class >= GFX9 ? 63 : 15;
2660 unsigned vscnt = 63;
2661
2662 if (wait_flags & AC_WAIT_LGKM)
2663 lgkmcnt = 0;
2664 if (wait_flags & AC_WAIT_VLOAD)
2665 vmcnt = 0;
2666
2667 if (wait_flags & AC_WAIT_VSTORE) {
2668 if (ctx->chip_class >= GFX10)
2669 vscnt = 0;
2670 else
2671 vmcnt = 0;
2672 }
2673
2674 /* There is no intrinsic for vscnt(0), so use a fence. */
2675 if ((wait_flags & AC_WAIT_LGKM &&
2676 wait_flags & AC_WAIT_VLOAD &&
2677 wait_flags & AC_WAIT_VSTORE) ||
2678 vscnt == 0) {
2679 LLVMBuildFence(ctx->builder, LLVMAtomicOrderingRelease, false, "");
2680 return;
2681 }
2682
2683 unsigned simm16 = (lgkmcnt << 8) |
2684 (7 << 4) | /* expcnt */
2685 (vmcnt & 0xf) |
2686 ((vmcnt >> 4) << 14);
2687
2688 LLVMValueRef args[1] = {
2689 LLVMConstInt(ctx->i32, simm16, false),
2690 };
2691 ac_build_intrinsic(ctx, "llvm.amdgcn.s.waitcnt",
2692 ctx->voidt, args, 1, 0);
2693 }
2694
2695 LLVMValueRef ac_build_fmed3(struct ac_llvm_context *ctx, LLVMValueRef src0,
2696 LLVMValueRef src1, LLVMValueRef src2,
2697 unsigned bitsize)
2698 {
2699 LLVMTypeRef type;
2700 char *intr;
2701
2702 if (bitsize == 16) {
2703 intr = "llvm.amdgcn.fmed3.f16";
2704 type = ctx->f16;
2705 } else if (bitsize == 32) {
2706 intr = "llvm.amdgcn.fmed3.f32";
2707 type = ctx->f32;
2708 } else {
2709 intr = "llvm.amdgcn.fmed3.f64";
2710 type = ctx->f64;
2711 }
2712
2713 LLVMValueRef params[] = {
2714 src0,
2715 src1,
2716 src2,
2717 };
2718 return ac_build_intrinsic(ctx, intr, type, params, 3,
2719 AC_FUNC_ATTR_READNONE);
2720 }
2721
2722 LLVMValueRef ac_build_fract(struct ac_llvm_context *ctx, LLVMValueRef src0,
2723 unsigned bitsize)
2724 {
2725 LLVMTypeRef type;
2726 char *intr;
2727
2728 if (bitsize == 16) {
2729 intr = "llvm.amdgcn.fract.f16";
2730 type = ctx->f16;
2731 } else if (bitsize == 32) {
2732 intr = "llvm.amdgcn.fract.f32";
2733 type = ctx->f32;
2734 } else {
2735 intr = "llvm.amdgcn.fract.f64";
2736 type = ctx->f64;
2737 }
2738
2739 LLVMValueRef params[] = {
2740 src0,
2741 };
2742 return ac_build_intrinsic(ctx, intr, type, params, 1,
2743 AC_FUNC_ATTR_READNONE);
2744 }
2745
2746 LLVMValueRef ac_build_isign(struct ac_llvm_context *ctx, LLVMValueRef src0,
2747 unsigned bitsize)
2748 {
2749 LLVMTypeRef type = LLVMIntTypeInContext(ctx->context, bitsize);
2750 LLVMValueRef zero = LLVMConstInt(type, 0, false);
2751 LLVMValueRef one = LLVMConstInt(type, 1, false);
2752
2753 LLVMValueRef cmp, val;
2754 cmp = LLVMBuildICmp(ctx->builder, LLVMIntSGT, src0, zero, "");
2755 val = LLVMBuildSelect(ctx->builder, cmp, one, src0, "");
2756 cmp = LLVMBuildICmp(ctx->builder, LLVMIntSGE, val, zero, "");
2757 val = LLVMBuildSelect(ctx->builder, cmp, val, LLVMConstInt(type, -1, true), "");
2758 return val;
2759 }
2760
2761 LLVMValueRef ac_build_fsign(struct ac_llvm_context *ctx, LLVMValueRef src0,
2762 unsigned bitsize)
2763 {
2764 LLVMValueRef cmp, val, zero, one;
2765 LLVMTypeRef type;
2766
2767 if (bitsize == 16) {
2768 type = ctx->f16;
2769 zero = ctx->f16_0;
2770 one = ctx->f16_1;
2771 } else if (bitsize == 32) {
2772 type = ctx->f32;
2773 zero = ctx->f32_0;
2774 one = ctx->f32_1;
2775 } else {
2776 type = ctx->f64;
2777 zero = ctx->f64_0;
2778 one = ctx->f64_1;
2779 }
2780
2781 cmp = LLVMBuildFCmp(ctx->builder, LLVMRealOGT, src0, zero, "");
2782 val = LLVMBuildSelect(ctx->builder, cmp, one, src0, "");
2783 cmp = LLVMBuildFCmp(ctx->builder, LLVMRealOGE, val, zero, "");
2784 val = LLVMBuildSelect(ctx->builder, cmp, val, LLVMConstReal(type, -1.0), "");
2785 return val;
2786 }
2787
2788 LLVMValueRef ac_build_bit_count(struct ac_llvm_context *ctx, LLVMValueRef src0)
2789 {
2790 LLVMValueRef result;
2791 unsigned bitsize;
2792
2793 bitsize = ac_get_elem_bits(ctx, LLVMTypeOf(src0));
2794
2795 switch (bitsize) {
2796 case 64:
2797 result = ac_build_intrinsic(ctx, "llvm.ctpop.i64", ctx->i64,
2798 (LLVMValueRef []) { src0 }, 1,
2799 AC_FUNC_ATTR_READNONE);
2800
2801 result = LLVMBuildTrunc(ctx->builder, result, ctx->i32, "");
2802 break;
2803 case 32:
2804 result = ac_build_intrinsic(ctx, "llvm.ctpop.i32", ctx->i32,
2805 (LLVMValueRef []) { src0 }, 1,
2806 AC_FUNC_ATTR_READNONE);
2807 break;
2808 case 16:
2809 result = ac_build_intrinsic(ctx, "llvm.ctpop.i16", ctx->i16,
2810 (LLVMValueRef []) { src0 }, 1,
2811 AC_FUNC_ATTR_READNONE);
2812
2813 result = LLVMBuildZExt(ctx->builder, result, ctx->i32, "");
2814 break;
2815 case 8:
2816 result = ac_build_intrinsic(ctx, "llvm.ctpop.i8", ctx->i8,
2817 (LLVMValueRef []) { src0 }, 1,
2818 AC_FUNC_ATTR_READNONE);
2819
2820 result = LLVMBuildZExt(ctx->builder, result, ctx->i32, "");
2821 break;
2822 default:
2823 unreachable(!"invalid bitsize");
2824 break;
2825 }
2826
2827 return result;
2828 }
2829
2830 LLVMValueRef ac_build_bitfield_reverse(struct ac_llvm_context *ctx,
2831 LLVMValueRef src0)
2832 {
2833 LLVMValueRef result;
2834 unsigned bitsize;
2835
2836 bitsize = ac_get_elem_bits(ctx, LLVMTypeOf(src0));
2837
2838 switch (bitsize) {
2839 case 64:
2840 result = ac_build_intrinsic(ctx, "llvm.bitreverse.i64", ctx->i64,
2841 (LLVMValueRef []) { src0 }, 1,
2842 AC_FUNC_ATTR_READNONE);
2843
2844 result = LLVMBuildTrunc(ctx->builder, result, ctx->i32, "");
2845 break;
2846 case 32:
2847 result = ac_build_intrinsic(ctx, "llvm.bitreverse.i32", ctx->i32,
2848 (LLVMValueRef []) { src0 }, 1,
2849 AC_FUNC_ATTR_READNONE);
2850 break;
2851 case 16:
2852 result = ac_build_intrinsic(ctx, "llvm.bitreverse.i16", ctx->i16,
2853 (LLVMValueRef []) { src0 }, 1,
2854 AC_FUNC_ATTR_READNONE);
2855
2856 result = LLVMBuildZExt(ctx->builder, result, ctx->i32, "");
2857 break;
2858 case 8:
2859 result = ac_build_intrinsic(ctx, "llvm.bitreverse.i8", ctx->i8,
2860 (LLVMValueRef []) { src0 }, 1,
2861 AC_FUNC_ATTR_READNONE);
2862
2863 result = LLVMBuildZExt(ctx->builder, result, ctx->i32, "");
2864 break;
2865 default:
2866 unreachable(!"invalid bitsize");
2867 break;
2868 }
2869
2870 return result;
2871 }
2872
2873 #define AC_EXP_TARGET 0
2874 #define AC_EXP_ENABLED_CHANNELS 1
2875 #define AC_EXP_OUT0 2
2876
2877 enum ac_ir_type {
2878 AC_IR_UNDEF,
2879 AC_IR_CONST,
2880 AC_IR_VALUE,
2881 };
2882
2883 struct ac_vs_exp_chan
2884 {
2885 LLVMValueRef value;
2886 float const_float;
2887 enum ac_ir_type type;
2888 };
2889
2890 struct ac_vs_exp_inst {
2891 unsigned offset;
2892 LLVMValueRef inst;
2893 struct ac_vs_exp_chan chan[4];
2894 };
2895
2896 struct ac_vs_exports {
2897 unsigned num;
2898 struct ac_vs_exp_inst exp[VARYING_SLOT_MAX];
2899 };
2900
2901 /* Return true if the PARAM export has been eliminated. */
2902 static bool ac_eliminate_const_output(uint8_t *vs_output_param_offset,
2903 uint32_t num_outputs,
2904 struct ac_vs_exp_inst *exp)
2905 {
2906 unsigned i, default_val; /* SPI_PS_INPUT_CNTL_i.DEFAULT_VAL */
2907 bool is_zero[4] = {}, is_one[4] = {};
2908
2909 for (i = 0; i < 4; i++) {
2910 /* It's a constant expression. Undef outputs are eliminated too. */
2911 if (exp->chan[i].type == AC_IR_UNDEF) {
2912 is_zero[i] = true;
2913 is_one[i] = true;
2914 } else if (exp->chan[i].type == AC_IR_CONST) {
2915 if (exp->chan[i].const_float == 0)
2916 is_zero[i] = true;
2917 else if (exp->chan[i].const_float == 1)
2918 is_one[i] = true;
2919 else
2920 return false; /* other constant */
2921 } else
2922 return false;
2923 }
2924
2925 /* Only certain combinations of 0 and 1 can be eliminated. */
2926 if (is_zero[0] && is_zero[1] && is_zero[2])
2927 default_val = is_zero[3] ? 0 : 1;
2928 else if (is_one[0] && is_one[1] && is_one[2])
2929 default_val = is_zero[3] ? 2 : 3;
2930 else
2931 return false;
2932
2933 /* The PARAM export can be represented as DEFAULT_VAL. Kill it. */
2934 LLVMInstructionEraseFromParent(exp->inst);
2935
2936 /* Change OFFSET to DEFAULT_VAL. */
2937 for (i = 0; i < num_outputs; i++) {
2938 if (vs_output_param_offset[i] == exp->offset) {
2939 vs_output_param_offset[i] =
2940 AC_EXP_PARAM_DEFAULT_VAL_0000 + default_val;
2941 break;
2942 }
2943 }
2944 return true;
2945 }
2946
2947 static bool ac_eliminate_duplicated_output(struct ac_llvm_context *ctx,
2948 uint8_t *vs_output_param_offset,
2949 uint32_t num_outputs,
2950 struct ac_vs_exports *processed,
2951 struct ac_vs_exp_inst *exp)
2952 {
2953 unsigned p, copy_back_channels = 0;
2954
2955 /* See if the output is already in the list of processed outputs.
2956 * The LLVMValueRef comparison relies on SSA.
2957 */
2958 for (p = 0; p < processed->num; p++) {
2959 bool different = false;
2960
2961 for (unsigned j = 0; j < 4; j++) {
2962 struct ac_vs_exp_chan *c1 = &processed->exp[p].chan[j];
2963 struct ac_vs_exp_chan *c2 = &exp->chan[j];
2964
2965 /* Treat undef as a match. */
2966 if (c2->type == AC_IR_UNDEF)
2967 continue;
2968
2969 /* If c1 is undef but c2 isn't, we can copy c2 to c1
2970 * and consider the instruction duplicated.
2971 */
2972 if (c1->type == AC_IR_UNDEF) {
2973 copy_back_channels |= 1 << j;
2974 continue;
2975 }
2976
2977 /* Test whether the channels are not equal. */
2978 if (c1->type != c2->type ||
2979 (c1->type == AC_IR_CONST &&
2980 c1->const_float != c2->const_float) ||
2981 (c1->type == AC_IR_VALUE &&
2982 c1->value != c2->value)) {
2983 different = true;
2984 break;
2985 }
2986 }
2987 if (!different)
2988 break;
2989
2990 copy_back_channels = 0;
2991 }
2992 if (p == processed->num)
2993 return false;
2994
2995 /* If a match was found, but the matching export has undef where the new
2996 * one has a normal value, copy the normal value to the undef channel.
2997 */
2998 struct ac_vs_exp_inst *match = &processed->exp[p];
2999
3000 /* Get current enabled channels mask. */
3001 LLVMValueRef arg = LLVMGetOperand(match->inst, AC_EXP_ENABLED_CHANNELS);
3002 unsigned enabled_channels = LLVMConstIntGetZExtValue(arg);
3003
3004 while (copy_back_channels) {
3005 unsigned chan = u_bit_scan(&copy_back_channels);
3006
3007 assert(match->chan[chan].type == AC_IR_UNDEF);
3008 LLVMSetOperand(match->inst, AC_EXP_OUT0 + chan,
3009 exp->chan[chan].value);
3010 match->chan[chan] = exp->chan[chan];
3011
3012 /* Update number of enabled channels because the original mask
3013 * is not always 0xf.
3014 */
3015 enabled_channels |= (1 << chan);
3016 LLVMSetOperand(match->inst, AC_EXP_ENABLED_CHANNELS,
3017 LLVMConstInt(ctx->i32, enabled_channels, 0));
3018 }
3019
3020 /* The PARAM export is duplicated. Kill it. */
3021 LLVMInstructionEraseFromParent(exp->inst);
3022
3023 /* Change OFFSET to the matching export. */
3024 for (unsigned i = 0; i < num_outputs; i++) {
3025 if (vs_output_param_offset[i] == exp->offset) {
3026 vs_output_param_offset[i] = match->offset;
3027 break;
3028 }
3029 }
3030 return true;
3031 }
3032
3033 void ac_optimize_vs_outputs(struct ac_llvm_context *ctx,
3034 LLVMValueRef main_fn,
3035 uint8_t *vs_output_param_offset,
3036 uint32_t num_outputs,
3037 uint8_t *num_param_exports)
3038 {
3039 LLVMBasicBlockRef bb;
3040 bool removed_any = false;
3041 struct ac_vs_exports exports;
3042
3043 exports.num = 0;
3044
3045 /* Process all LLVM instructions. */
3046 bb = LLVMGetFirstBasicBlock(main_fn);
3047 while (bb) {
3048 LLVMValueRef inst = LLVMGetFirstInstruction(bb);
3049
3050 while (inst) {
3051 LLVMValueRef cur = inst;
3052 inst = LLVMGetNextInstruction(inst);
3053 struct ac_vs_exp_inst exp;
3054
3055 if (LLVMGetInstructionOpcode(cur) != LLVMCall)
3056 continue;
3057
3058 LLVMValueRef callee = ac_llvm_get_called_value(cur);
3059
3060 if (!ac_llvm_is_function(callee))
3061 continue;
3062
3063 const char *name = LLVMGetValueName(callee);
3064 unsigned num_args = LLVMCountParams(callee);
3065
3066 /* Check if this is an export instruction. */
3067 if ((num_args != 9 && num_args != 8) ||
3068 (strcmp(name, "llvm.SI.export") &&
3069 strcmp(name, "llvm.amdgcn.exp.f32")))
3070 continue;
3071
3072 LLVMValueRef arg = LLVMGetOperand(cur, AC_EXP_TARGET);
3073 unsigned target = LLVMConstIntGetZExtValue(arg);
3074
3075 if (target < V_008DFC_SQ_EXP_PARAM)
3076 continue;
3077
3078 target -= V_008DFC_SQ_EXP_PARAM;
3079
3080 /* Parse the instruction. */
3081 memset(&exp, 0, sizeof(exp));
3082 exp.offset = target;
3083 exp.inst = cur;
3084
3085 for (unsigned i = 0; i < 4; i++) {
3086 LLVMValueRef v = LLVMGetOperand(cur, AC_EXP_OUT0 + i);
3087
3088 exp.chan[i].value = v;
3089
3090 if (LLVMIsUndef(v)) {
3091 exp.chan[i].type = AC_IR_UNDEF;
3092 } else if (LLVMIsAConstantFP(v)) {
3093 LLVMBool loses_info;
3094 exp.chan[i].type = AC_IR_CONST;
3095 exp.chan[i].const_float =
3096 LLVMConstRealGetDouble(v, &loses_info);
3097 } else {
3098 exp.chan[i].type = AC_IR_VALUE;
3099 }
3100 }
3101
3102 /* Eliminate constant and duplicated PARAM exports. */
3103 if (ac_eliminate_const_output(vs_output_param_offset,
3104 num_outputs, &exp) ||
3105 ac_eliminate_duplicated_output(ctx,
3106 vs_output_param_offset,
3107 num_outputs, &exports,
3108 &exp)) {
3109 removed_any = true;
3110 } else {
3111 exports.exp[exports.num++] = exp;
3112 }
3113 }
3114 bb = LLVMGetNextBasicBlock(bb);
3115 }
3116
3117 /* Remove holes in export memory due to removed PARAM exports.
3118 * This is done by renumbering all PARAM exports.
3119 */
3120 if (removed_any) {
3121 uint8_t old_offset[VARYING_SLOT_MAX];
3122 unsigned out, i;
3123
3124 /* Make a copy of the offsets. We need the old version while
3125 * we are modifying some of them. */
3126 memcpy(old_offset, vs_output_param_offset,
3127 sizeof(old_offset));
3128
3129 for (i = 0; i < exports.num; i++) {
3130 unsigned offset = exports.exp[i].offset;
3131
3132 /* Update vs_output_param_offset. Multiple outputs can
3133 * have the same offset.
3134 */
3135 for (out = 0; out < num_outputs; out++) {
3136 if (old_offset[out] == offset)
3137 vs_output_param_offset[out] = i;
3138 }
3139
3140 /* Change the PARAM offset in the instruction. */
3141 LLVMSetOperand(exports.exp[i].inst, AC_EXP_TARGET,
3142 LLVMConstInt(ctx->i32,
3143 V_008DFC_SQ_EXP_PARAM + i, 0));
3144 }
3145 *num_param_exports = exports.num;
3146 }
3147 }
3148
3149 void ac_init_exec_full_mask(struct ac_llvm_context *ctx)
3150 {
3151 LLVMValueRef full_mask = LLVMConstInt(ctx->i64, ~0ull, 0);
3152 ac_build_intrinsic(ctx,
3153 "llvm.amdgcn.init.exec", ctx->voidt,
3154 &full_mask, 1, AC_FUNC_ATTR_CONVERGENT);
3155 }
3156
3157 void ac_declare_lds_as_pointer(struct ac_llvm_context *ctx)
3158 {
3159 unsigned lds_size = ctx->chip_class >= GFX7 ? 65536 : 32768;
3160 ctx->lds = LLVMBuildIntToPtr(ctx->builder, ctx->i32_0,
3161 LLVMPointerType(LLVMArrayType(ctx->i32, lds_size / 4), AC_ADDR_SPACE_LDS),
3162 "lds");
3163 }
3164
3165 LLVMValueRef ac_lds_load(struct ac_llvm_context *ctx,
3166 LLVMValueRef dw_addr)
3167 {
3168 return LLVMBuildLoad(ctx->builder, ac_build_gep0(ctx, ctx->lds, dw_addr), "");
3169 }
3170
3171 void ac_lds_store(struct ac_llvm_context *ctx,
3172 LLVMValueRef dw_addr,
3173 LLVMValueRef value)
3174 {
3175 value = ac_to_integer(ctx, value);
3176 ac_build_indexed_store(ctx, ctx->lds,
3177 dw_addr, value);
3178 }
3179
3180 LLVMValueRef ac_find_lsb(struct ac_llvm_context *ctx,
3181 LLVMTypeRef dst_type,
3182 LLVMValueRef src0)
3183 {
3184 unsigned src0_bitsize = ac_get_elem_bits(ctx, LLVMTypeOf(src0));
3185 const char *intrin_name;
3186 LLVMTypeRef type;
3187 LLVMValueRef zero;
3188
3189 switch (src0_bitsize) {
3190 case 64:
3191 intrin_name = "llvm.cttz.i64";
3192 type = ctx->i64;
3193 zero = ctx->i64_0;
3194 break;
3195 case 32:
3196 intrin_name = "llvm.cttz.i32";
3197 type = ctx->i32;
3198 zero = ctx->i32_0;
3199 break;
3200 case 16:
3201 intrin_name = "llvm.cttz.i16";
3202 type = ctx->i16;
3203 zero = ctx->i16_0;
3204 break;
3205 case 8:
3206 intrin_name = "llvm.cttz.i8";
3207 type = ctx->i8;
3208 zero = ctx->i8_0;
3209 break;
3210 default:
3211 unreachable(!"invalid bitsize");
3212 }
3213
3214 LLVMValueRef params[2] = {
3215 src0,
3216
3217 /* The value of 1 means that ffs(x=0) = undef, so LLVM won't
3218 * add special code to check for x=0. The reason is that
3219 * the LLVM behavior for x=0 is different from what we
3220 * need here. However, LLVM also assumes that ffs(x) is
3221 * in [0, 31], but GLSL expects that ffs(0) = -1, so
3222 * a conditional assignment to handle 0 is still required.
3223 *
3224 * The hardware already implements the correct behavior.
3225 */
3226 ctx->i1true,
3227 };
3228
3229 LLVMValueRef lsb = ac_build_intrinsic(ctx, intrin_name, type,
3230 params, 2,
3231 AC_FUNC_ATTR_READNONE);
3232
3233 if (src0_bitsize == 64) {
3234 lsb = LLVMBuildTrunc(ctx->builder, lsb, ctx->i32, "");
3235 } else if (src0_bitsize < 32) {
3236 lsb = LLVMBuildSExt(ctx->builder, lsb, ctx->i32, "");
3237 }
3238
3239 /* TODO: We need an intrinsic to skip this conditional. */
3240 /* Check for zero: */
3241 return LLVMBuildSelect(ctx->builder, LLVMBuildICmp(ctx->builder,
3242 LLVMIntEQ, src0,
3243 zero, ""),
3244 LLVMConstInt(ctx->i32, -1, 0), lsb, "");
3245 }
3246
3247 LLVMTypeRef ac_array_in_const_addr_space(LLVMTypeRef elem_type)
3248 {
3249 return LLVMPointerType(elem_type, AC_ADDR_SPACE_CONST);
3250 }
3251
3252 LLVMTypeRef ac_array_in_const32_addr_space(LLVMTypeRef elem_type)
3253 {
3254 return LLVMPointerType(elem_type, AC_ADDR_SPACE_CONST_32BIT);
3255 }
3256
3257 static struct ac_llvm_flow *
3258 get_current_flow(struct ac_llvm_context *ctx)
3259 {
3260 if (ctx->flow->depth > 0)
3261 return &ctx->flow->stack[ctx->flow->depth - 1];
3262 return NULL;
3263 }
3264
3265 static struct ac_llvm_flow *
3266 get_innermost_loop(struct ac_llvm_context *ctx)
3267 {
3268 for (unsigned i = ctx->flow->depth; i > 0; --i) {
3269 if (ctx->flow->stack[i - 1].loop_entry_block)
3270 return &ctx->flow->stack[i - 1];
3271 }
3272 return NULL;
3273 }
3274
3275 static struct ac_llvm_flow *
3276 push_flow(struct ac_llvm_context *ctx)
3277 {
3278 struct ac_llvm_flow *flow;
3279
3280 if (ctx->flow->depth >= ctx->flow->depth_max) {
3281 unsigned new_max = MAX2(ctx->flow->depth << 1,
3282 AC_LLVM_INITIAL_CF_DEPTH);
3283
3284 ctx->flow->stack = realloc(ctx->flow->stack, new_max * sizeof(*ctx->flow->stack));
3285 ctx->flow->depth_max = new_max;
3286 }
3287
3288 flow = &ctx->flow->stack[ctx->flow->depth];
3289 ctx->flow->depth++;
3290
3291 flow->next_block = NULL;
3292 flow->loop_entry_block = NULL;
3293 return flow;
3294 }
3295
3296 static void set_basicblock_name(LLVMBasicBlockRef bb, const char *base,
3297 int label_id)
3298 {
3299 char buf[32];
3300 snprintf(buf, sizeof(buf), "%s%d", base, label_id);
3301 LLVMSetValueName(LLVMBasicBlockAsValue(bb), buf);
3302 }
3303
3304 /* Append a basic block at the level of the parent flow.
3305 */
3306 static LLVMBasicBlockRef append_basic_block(struct ac_llvm_context *ctx,
3307 const char *name)
3308 {
3309 assert(ctx->flow->depth >= 1);
3310
3311 if (ctx->flow->depth >= 2) {
3312 struct ac_llvm_flow *flow = &ctx->flow->stack[ctx->flow->depth - 2];
3313
3314 return LLVMInsertBasicBlockInContext(ctx->context,
3315 flow->next_block, name);
3316 }
3317
3318 LLVMValueRef main_fn =
3319 LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx->builder));
3320 return LLVMAppendBasicBlockInContext(ctx->context, main_fn, name);
3321 }
3322
3323 /* Emit a branch to the given default target for the current block if
3324 * applicable -- that is, if the current block does not already contain a
3325 * branch from a break or continue.
3326 */
3327 static void emit_default_branch(LLVMBuilderRef builder,
3328 LLVMBasicBlockRef target)
3329 {
3330 if (!LLVMGetBasicBlockTerminator(LLVMGetInsertBlock(builder)))
3331 LLVMBuildBr(builder, target);
3332 }
3333
3334 void ac_build_bgnloop(struct ac_llvm_context *ctx, int label_id)
3335 {
3336 struct ac_llvm_flow *flow = push_flow(ctx);
3337 flow->loop_entry_block = append_basic_block(ctx, "LOOP");
3338 flow->next_block = append_basic_block(ctx, "ENDLOOP");
3339 set_basicblock_name(flow->loop_entry_block, "loop", label_id);
3340 LLVMBuildBr(ctx->builder, flow->loop_entry_block);
3341 LLVMPositionBuilderAtEnd(ctx->builder, flow->loop_entry_block);
3342 }
3343
3344 void ac_build_break(struct ac_llvm_context *ctx)
3345 {
3346 struct ac_llvm_flow *flow = get_innermost_loop(ctx);
3347 LLVMBuildBr(ctx->builder, flow->next_block);
3348 }
3349
3350 void ac_build_continue(struct ac_llvm_context *ctx)
3351 {
3352 struct ac_llvm_flow *flow = get_innermost_loop(ctx);
3353 LLVMBuildBr(ctx->builder, flow->loop_entry_block);
3354 }
3355
3356 void ac_build_else(struct ac_llvm_context *ctx, int label_id)
3357 {
3358 struct ac_llvm_flow *current_branch = get_current_flow(ctx);
3359 LLVMBasicBlockRef endif_block;
3360
3361 assert(!current_branch->loop_entry_block);
3362
3363 endif_block = append_basic_block(ctx, "ENDIF");
3364 emit_default_branch(ctx->builder, endif_block);
3365
3366 LLVMPositionBuilderAtEnd(ctx->builder, current_branch->next_block);
3367 set_basicblock_name(current_branch->next_block, "else", label_id);
3368
3369 current_branch->next_block = endif_block;
3370 }
3371
3372 void ac_build_endif(struct ac_llvm_context *ctx, int label_id)
3373 {
3374 struct ac_llvm_flow *current_branch = get_current_flow(ctx);
3375
3376 assert(!current_branch->loop_entry_block);
3377
3378 emit_default_branch(ctx->builder, current_branch->next_block);
3379 LLVMPositionBuilderAtEnd(ctx->builder, current_branch->next_block);
3380 set_basicblock_name(current_branch->next_block, "endif", label_id);
3381
3382 ctx->flow->depth--;
3383 }
3384
3385 void ac_build_endloop(struct ac_llvm_context *ctx, int label_id)
3386 {
3387 struct ac_llvm_flow *current_loop = get_current_flow(ctx);
3388
3389 assert(current_loop->loop_entry_block);
3390
3391 emit_default_branch(ctx->builder, current_loop->loop_entry_block);
3392
3393 LLVMPositionBuilderAtEnd(ctx->builder, current_loop->next_block);
3394 set_basicblock_name(current_loop->next_block, "endloop", label_id);
3395 ctx->flow->depth--;
3396 }
3397
3398 void ac_build_ifcc(struct ac_llvm_context *ctx, LLVMValueRef cond, int label_id)
3399 {
3400 struct ac_llvm_flow *flow = push_flow(ctx);
3401 LLVMBasicBlockRef if_block;
3402
3403 if_block = append_basic_block(ctx, "IF");
3404 flow->next_block = append_basic_block(ctx, "ELSE");
3405 set_basicblock_name(if_block, "if", label_id);
3406 LLVMBuildCondBr(ctx->builder, cond, if_block, flow->next_block);
3407 LLVMPositionBuilderAtEnd(ctx->builder, if_block);
3408 }
3409
3410 void ac_build_if(struct ac_llvm_context *ctx, LLVMValueRef value,
3411 int label_id)
3412 {
3413 LLVMValueRef cond = LLVMBuildFCmp(ctx->builder, LLVMRealUNE,
3414 value, ctx->f32_0, "");
3415 ac_build_ifcc(ctx, cond, label_id);
3416 }
3417
3418 void ac_build_uif(struct ac_llvm_context *ctx, LLVMValueRef value,
3419 int label_id)
3420 {
3421 LLVMValueRef cond = LLVMBuildICmp(ctx->builder, LLVMIntNE,
3422 ac_to_integer(ctx, value),
3423 ctx->i32_0, "");
3424 ac_build_ifcc(ctx, cond, label_id);
3425 }
3426
3427 LLVMValueRef ac_build_alloca_undef(struct ac_llvm_context *ac, LLVMTypeRef type,
3428 const char *name)
3429 {
3430 LLVMBuilderRef builder = ac->builder;
3431 LLVMBasicBlockRef current_block = LLVMGetInsertBlock(builder);
3432 LLVMValueRef function = LLVMGetBasicBlockParent(current_block);
3433 LLVMBasicBlockRef first_block = LLVMGetEntryBasicBlock(function);
3434 LLVMValueRef first_instr = LLVMGetFirstInstruction(first_block);
3435 LLVMBuilderRef first_builder = LLVMCreateBuilderInContext(ac->context);
3436 LLVMValueRef res;
3437
3438 if (first_instr) {
3439 LLVMPositionBuilderBefore(first_builder, first_instr);
3440 } else {
3441 LLVMPositionBuilderAtEnd(first_builder, first_block);
3442 }
3443
3444 res = LLVMBuildAlloca(first_builder, type, name);
3445 LLVMDisposeBuilder(first_builder);
3446 return res;
3447 }
3448
3449 LLVMValueRef ac_build_alloca(struct ac_llvm_context *ac,
3450 LLVMTypeRef type, const char *name)
3451 {
3452 LLVMValueRef ptr = ac_build_alloca_undef(ac, type, name);
3453 LLVMBuildStore(ac->builder, LLVMConstNull(type), ptr);
3454 return ptr;
3455 }
3456
3457 LLVMValueRef ac_cast_ptr(struct ac_llvm_context *ctx, LLVMValueRef ptr,
3458 LLVMTypeRef type)
3459 {
3460 int addr_space = LLVMGetPointerAddressSpace(LLVMTypeOf(ptr));
3461 return LLVMBuildBitCast(ctx->builder, ptr,
3462 LLVMPointerType(type, addr_space), "");
3463 }
3464
3465 LLVMValueRef ac_trim_vector(struct ac_llvm_context *ctx, LLVMValueRef value,
3466 unsigned count)
3467 {
3468 unsigned num_components = ac_get_llvm_num_components(value);
3469 if (count == num_components)
3470 return value;
3471
3472 LLVMValueRef masks[MAX2(count, 2)];
3473 masks[0] = ctx->i32_0;
3474 masks[1] = ctx->i32_1;
3475 for (unsigned i = 2; i < count; i++)
3476 masks[i] = LLVMConstInt(ctx->i32, i, false);
3477
3478 if (count == 1)
3479 return LLVMBuildExtractElement(ctx->builder, value, masks[0],
3480 "");
3481
3482 LLVMValueRef swizzle = LLVMConstVector(masks, count);
3483 return LLVMBuildShuffleVector(ctx->builder, value, value, swizzle, "");
3484 }
3485
3486 LLVMValueRef ac_unpack_param(struct ac_llvm_context *ctx, LLVMValueRef param,
3487 unsigned rshift, unsigned bitwidth)
3488 {
3489 LLVMValueRef value = param;
3490 if (rshift)
3491 value = LLVMBuildLShr(ctx->builder, value,
3492 LLVMConstInt(ctx->i32, rshift, false), "");
3493
3494 if (rshift + bitwidth < 32) {
3495 unsigned mask = (1 << bitwidth) - 1;
3496 value = LLVMBuildAnd(ctx->builder, value,
3497 LLVMConstInt(ctx->i32, mask, false), "");
3498 }
3499 return value;
3500 }
3501
3502 /* Adjust the sample index according to FMASK.
3503 *
3504 * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
3505 * which is the identity mapping. Each nibble says which physical sample
3506 * should be fetched to get that sample.
3507 *
3508 * For example, 0x11111100 means there are only 2 samples stored and
3509 * the second sample covers 3/4 of the pixel. When reading samples 0
3510 * and 1, return physical sample 0 (determined by the first two 0s
3511 * in FMASK), otherwise return physical sample 1.
3512 *
3513 * The sample index should be adjusted as follows:
3514 * addr[sample_index] = (fmask >> (addr[sample_index] * 4)) & 0xF;
3515 */
3516 void ac_apply_fmask_to_sample(struct ac_llvm_context *ac, LLVMValueRef fmask,
3517 LLVMValueRef *addr, bool is_array_tex)
3518 {
3519 struct ac_image_args fmask_load = {};
3520 fmask_load.opcode = ac_image_load;
3521 fmask_load.resource = fmask;
3522 fmask_load.dmask = 0xf;
3523 fmask_load.dim = is_array_tex ? ac_image_2darray : ac_image_2d;
3524 fmask_load.attributes = AC_FUNC_ATTR_READNONE;
3525
3526 fmask_load.coords[0] = addr[0];
3527 fmask_load.coords[1] = addr[1];
3528 if (is_array_tex)
3529 fmask_load.coords[2] = addr[2];
3530
3531 LLVMValueRef fmask_value = ac_build_image_opcode(ac, &fmask_load);
3532 fmask_value = LLVMBuildExtractElement(ac->builder, fmask_value,
3533 ac->i32_0, "");
3534
3535 /* Apply the formula. */
3536 unsigned sample_chan = is_array_tex ? 3 : 2;
3537 LLVMValueRef final_sample;
3538 final_sample = LLVMBuildMul(ac->builder, addr[sample_chan],
3539 LLVMConstInt(ac->i32, 4, 0), "");
3540 final_sample = LLVMBuildLShr(ac->builder, fmask_value, final_sample, "");
3541 /* Mask the sample index by 0x7, because 0x8 means an unknown value
3542 * with EQAA, so those will map to 0. */
3543 final_sample = LLVMBuildAnd(ac->builder, final_sample,
3544 LLVMConstInt(ac->i32, 0x7, 0), "");
3545
3546 /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
3547 * resource descriptor is 0 (invalid).
3548 */
3549 LLVMValueRef tmp;
3550 tmp = LLVMBuildBitCast(ac->builder, fmask, ac->v8i32, "");
3551 tmp = LLVMBuildExtractElement(ac->builder, tmp, ac->i32_1, "");
3552 tmp = LLVMBuildICmp(ac->builder, LLVMIntNE, tmp, ac->i32_0, "");
3553
3554 /* Replace the MSAA sample index. */
3555 addr[sample_chan] = LLVMBuildSelect(ac->builder, tmp, final_sample,
3556 addr[sample_chan], "");
3557 }
3558
3559 static LLVMValueRef
3560 _ac_build_readlane(struct ac_llvm_context *ctx, LLVMValueRef src, LLVMValueRef lane)
3561 {
3562 ac_build_optimization_barrier(ctx, &src);
3563 return ac_build_intrinsic(ctx,
3564 lane == NULL ? "llvm.amdgcn.readfirstlane" : "llvm.amdgcn.readlane",
3565 LLVMTypeOf(src), (LLVMValueRef []) {
3566 src, lane },
3567 lane == NULL ? 1 : 2,
3568 AC_FUNC_ATTR_READNONE |
3569 AC_FUNC_ATTR_CONVERGENT);
3570 }
3571
3572 /**
3573 * Builds the "llvm.amdgcn.readlane" or "llvm.amdgcn.readfirstlane" intrinsic.
3574 * @param ctx
3575 * @param src
3576 * @param lane - id of the lane or NULL for the first active lane
3577 * @return value of the lane
3578 */
3579 LLVMValueRef
3580 ac_build_readlane(struct ac_llvm_context *ctx, LLVMValueRef src, LLVMValueRef lane)
3581 {
3582 LLVMTypeRef src_type = LLVMTypeOf(src);
3583 src = ac_to_integer(ctx, src);
3584 unsigned bits = LLVMGetIntTypeWidth(LLVMTypeOf(src));
3585 LLVMValueRef ret;
3586
3587 if (bits == 32) {
3588 ret = _ac_build_readlane(ctx, src, lane);
3589 } else {
3590 assert(bits % 32 == 0);
3591 LLVMTypeRef vec_type = LLVMVectorType(ctx->i32, bits / 32);
3592 LLVMValueRef src_vector =
3593 LLVMBuildBitCast(ctx->builder, src, vec_type, "");
3594 ret = LLVMGetUndef(vec_type);
3595 for (unsigned i = 0; i < bits / 32; i++) {
3596 src = LLVMBuildExtractElement(ctx->builder, src_vector,
3597 LLVMConstInt(ctx->i32, i, 0), "");
3598 LLVMValueRef ret_comp = _ac_build_readlane(ctx, src, lane);
3599 ret = LLVMBuildInsertElement(ctx->builder, ret, ret_comp,
3600 LLVMConstInt(ctx->i32, i, 0), "");
3601 }
3602 }
3603 if (LLVMGetTypeKind(src_type) == LLVMPointerTypeKind)
3604 return LLVMBuildIntToPtr(ctx->builder, ret, src_type, "");
3605 return LLVMBuildBitCast(ctx->builder, ret, src_type, "");
3606 }
3607
3608 LLVMValueRef
3609 ac_build_writelane(struct ac_llvm_context *ctx, LLVMValueRef src, LLVMValueRef value, LLVMValueRef lane)
3610 {
3611 return ac_build_intrinsic(ctx, "llvm.amdgcn.writelane", ctx->i32,
3612 (LLVMValueRef []) {value, lane, src}, 3,
3613 AC_FUNC_ATTR_READNONE | AC_FUNC_ATTR_CONVERGENT);
3614 }
3615
3616 LLVMValueRef
3617 ac_build_mbcnt(struct ac_llvm_context *ctx, LLVMValueRef mask)
3618 {
3619 if (ctx->wave_size == 32) {
3620 return ac_build_intrinsic(ctx, "llvm.amdgcn.mbcnt.lo", ctx->i32,
3621 (LLVMValueRef []) { mask, ctx->i32_0 },
3622 2, AC_FUNC_ATTR_READNONE);
3623 }
3624 LLVMValueRef mask_vec = LLVMBuildBitCast(ctx->builder, mask,
3625 LLVMVectorType(ctx->i32, 2),
3626 "");
3627 LLVMValueRef mask_lo = LLVMBuildExtractElement(ctx->builder, mask_vec,
3628 ctx->i32_0, "");
3629 LLVMValueRef mask_hi = LLVMBuildExtractElement(ctx->builder, mask_vec,
3630 ctx->i32_1, "");
3631 LLVMValueRef val =
3632 ac_build_intrinsic(ctx, "llvm.amdgcn.mbcnt.lo", ctx->i32,
3633 (LLVMValueRef []) { mask_lo, ctx->i32_0 },
3634 2, AC_FUNC_ATTR_READNONE);
3635 val = ac_build_intrinsic(ctx, "llvm.amdgcn.mbcnt.hi", ctx->i32,
3636 (LLVMValueRef []) { mask_hi, val },
3637 2, AC_FUNC_ATTR_READNONE);
3638 return val;
3639 }
3640
3641 enum dpp_ctrl {
3642 _dpp_quad_perm = 0x000,
3643 _dpp_row_sl = 0x100,
3644 _dpp_row_sr = 0x110,
3645 _dpp_row_rr = 0x120,
3646 dpp_wf_sl1 = 0x130,
3647 dpp_wf_rl1 = 0x134,
3648 dpp_wf_sr1 = 0x138,
3649 dpp_wf_rr1 = 0x13C,
3650 dpp_row_mirror = 0x140,
3651 dpp_row_half_mirror = 0x141,
3652 dpp_row_bcast15 = 0x142,
3653 dpp_row_bcast31 = 0x143
3654 };
3655
3656 static inline enum dpp_ctrl
3657 dpp_quad_perm(unsigned lane0, unsigned lane1, unsigned lane2, unsigned lane3)
3658 {
3659 assert(lane0 < 4 && lane1 < 4 && lane2 < 4 && lane3 < 4);
3660 return _dpp_quad_perm | lane0 | (lane1 << 2) | (lane2 << 4) | (lane3 << 6);
3661 }
3662
3663 static inline enum dpp_ctrl
3664 dpp_row_sl(unsigned amount)
3665 {
3666 assert(amount > 0 && amount < 16);
3667 return _dpp_row_sl | amount;
3668 }
3669
3670 static inline enum dpp_ctrl
3671 dpp_row_sr(unsigned amount)
3672 {
3673 assert(amount > 0 && amount < 16);
3674 return _dpp_row_sr | amount;
3675 }
3676
3677 static LLVMValueRef
3678 _ac_build_dpp(struct ac_llvm_context *ctx, LLVMValueRef old, LLVMValueRef src,
3679 enum dpp_ctrl dpp_ctrl, unsigned row_mask, unsigned bank_mask,
3680 bool bound_ctrl)
3681 {
3682 return ac_build_intrinsic(ctx, "llvm.amdgcn.update.dpp.i32",
3683 LLVMTypeOf(old),
3684 (LLVMValueRef[]) {
3685 old, src,
3686 LLVMConstInt(ctx->i32, dpp_ctrl, 0),
3687 LLVMConstInt(ctx->i32, row_mask, 0),
3688 LLVMConstInt(ctx->i32, bank_mask, 0),
3689 LLVMConstInt(ctx->i1, bound_ctrl, 0) },
3690 6, AC_FUNC_ATTR_READNONE | AC_FUNC_ATTR_CONVERGENT);
3691 }
3692
3693 static LLVMValueRef
3694 ac_build_dpp(struct ac_llvm_context *ctx, LLVMValueRef old, LLVMValueRef src,
3695 enum dpp_ctrl dpp_ctrl, unsigned row_mask, unsigned bank_mask,
3696 bool bound_ctrl)
3697 {
3698 LLVMTypeRef src_type = LLVMTypeOf(src);
3699 src = ac_to_integer(ctx, src);
3700 old = ac_to_integer(ctx, old);
3701 unsigned bits = LLVMGetIntTypeWidth(LLVMTypeOf(src));
3702 LLVMValueRef ret;
3703 if (bits == 32) {
3704 ret = _ac_build_dpp(ctx, old, src, dpp_ctrl, row_mask,
3705 bank_mask, bound_ctrl);
3706 } else {
3707 assert(bits % 32 == 0);
3708 LLVMTypeRef vec_type = LLVMVectorType(ctx->i32, bits / 32);
3709 LLVMValueRef src_vector =
3710 LLVMBuildBitCast(ctx->builder, src, vec_type, "");
3711 LLVMValueRef old_vector =
3712 LLVMBuildBitCast(ctx->builder, old, vec_type, "");
3713 ret = LLVMGetUndef(vec_type);
3714 for (unsigned i = 0; i < bits / 32; i++) {
3715 src = LLVMBuildExtractElement(ctx->builder, src_vector,
3716 LLVMConstInt(ctx->i32, i,
3717 0), "");
3718 old = LLVMBuildExtractElement(ctx->builder, old_vector,
3719 LLVMConstInt(ctx->i32, i,
3720 0), "");
3721 LLVMValueRef ret_comp = _ac_build_dpp(ctx, old, src,
3722 dpp_ctrl,
3723 row_mask,
3724 bank_mask,
3725 bound_ctrl);
3726 ret = LLVMBuildInsertElement(ctx->builder, ret,
3727 ret_comp,
3728 LLVMConstInt(ctx->i32, i,
3729 0), "");
3730 }
3731 }
3732 return LLVMBuildBitCast(ctx->builder, ret, src_type, "");
3733 }
3734
3735 static LLVMValueRef
3736 _ac_build_permlane16(struct ac_llvm_context *ctx, LLVMValueRef src, uint64_t sel,
3737 bool exchange_rows, bool bound_ctrl)
3738 {
3739 LLVMValueRef args[6] = {
3740 src,
3741 src,
3742 LLVMConstInt(ctx->i32, sel, false),
3743 LLVMConstInt(ctx->i32, sel >> 32, false),
3744 ctx->i1true, /* fi */
3745 bound_ctrl ? ctx->i1true : ctx->i1false,
3746 };
3747 return ac_build_intrinsic(ctx, exchange_rows ? "llvm.amdgcn.permlanex16"
3748 : "llvm.amdgcn.permlane16",
3749 ctx->i32, args, 6,
3750 AC_FUNC_ATTR_READNONE | AC_FUNC_ATTR_CONVERGENT);
3751 }
3752
3753 static LLVMValueRef
3754 ac_build_permlane16(struct ac_llvm_context *ctx, LLVMValueRef src, uint64_t sel,
3755 bool exchange_rows, bool bound_ctrl)
3756 {
3757 LLVMTypeRef src_type = LLVMTypeOf(src);
3758 src = ac_to_integer(ctx, src);
3759 unsigned bits = LLVMGetIntTypeWidth(LLVMTypeOf(src));
3760 LLVMValueRef ret;
3761 if (bits == 32) {
3762 ret = _ac_build_permlane16(ctx, src, sel, exchange_rows,
3763 bound_ctrl);
3764 } else {
3765 assert(bits % 32 == 0);
3766 LLVMTypeRef vec_type = LLVMVectorType(ctx->i32, bits / 32);
3767 LLVMValueRef src_vector =
3768 LLVMBuildBitCast(ctx->builder, src, vec_type, "");
3769 ret = LLVMGetUndef(vec_type);
3770 for (unsigned i = 0; i < bits / 32; i++) {
3771 src = LLVMBuildExtractElement(ctx->builder, src_vector,
3772 LLVMConstInt(ctx->i32, i,
3773 0), "");
3774 LLVMValueRef ret_comp =
3775 _ac_build_permlane16(ctx, src, sel,
3776 exchange_rows,
3777 bound_ctrl);
3778 ret = LLVMBuildInsertElement(ctx->builder, ret,
3779 ret_comp,
3780 LLVMConstInt(ctx->i32, i,
3781 0), "");
3782 }
3783 }
3784 return LLVMBuildBitCast(ctx->builder, ret, src_type, "");
3785 }
3786
3787 static inline unsigned
3788 ds_pattern_bitmode(unsigned and_mask, unsigned or_mask, unsigned xor_mask)
3789 {
3790 assert(and_mask < 32 && or_mask < 32 && xor_mask < 32);
3791 return and_mask | (or_mask << 5) | (xor_mask << 10);
3792 }
3793
3794 static LLVMValueRef
3795 _ac_build_ds_swizzle(struct ac_llvm_context *ctx, LLVMValueRef src, unsigned mask)
3796 {
3797 return ac_build_intrinsic(ctx, "llvm.amdgcn.ds.swizzle",
3798 LLVMTypeOf(src), (LLVMValueRef []) {
3799 src, LLVMConstInt(ctx->i32, mask, 0) },
3800 2, AC_FUNC_ATTR_READNONE | AC_FUNC_ATTR_CONVERGENT);
3801 }
3802
3803 LLVMValueRef
3804 ac_build_ds_swizzle(struct ac_llvm_context *ctx, LLVMValueRef src, unsigned mask)
3805 {
3806 LLVMTypeRef src_type = LLVMTypeOf(src);
3807 src = ac_to_integer(ctx, src);
3808 unsigned bits = LLVMGetIntTypeWidth(LLVMTypeOf(src));
3809 LLVMValueRef ret;
3810 if (bits == 32) {
3811 ret = _ac_build_ds_swizzle(ctx, src, mask);
3812 } else {
3813 assert(bits % 32 == 0);
3814 LLVMTypeRef vec_type = LLVMVectorType(ctx->i32, bits / 32);
3815 LLVMValueRef src_vector =
3816 LLVMBuildBitCast(ctx->builder, src, vec_type, "");
3817 ret = LLVMGetUndef(vec_type);
3818 for (unsigned i = 0; i < bits / 32; i++) {
3819 src = LLVMBuildExtractElement(ctx->builder, src_vector,
3820 LLVMConstInt(ctx->i32, i,
3821 0), "");
3822 LLVMValueRef ret_comp = _ac_build_ds_swizzle(ctx, src,
3823 mask);
3824 ret = LLVMBuildInsertElement(ctx->builder, ret,
3825 ret_comp,
3826 LLVMConstInt(ctx->i32, i,
3827 0), "");
3828 }
3829 }
3830 return LLVMBuildBitCast(ctx->builder, ret, src_type, "");
3831 }
3832
3833 static LLVMValueRef
3834 ac_build_wwm(struct ac_llvm_context *ctx, LLVMValueRef src)
3835 {
3836 char name[32], type[8];
3837 ac_build_type_name_for_intr(LLVMTypeOf(src), type, sizeof(type));
3838 snprintf(name, sizeof(name), "llvm.amdgcn.wwm.%s", type);
3839 return ac_build_intrinsic(ctx, name, LLVMTypeOf(src),
3840 (LLVMValueRef []) { src }, 1,
3841 AC_FUNC_ATTR_READNONE);
3842 }
3843
3844 static LLVMValueRef
3845 ac_build_set_inactive(struct ac_llvm_context *ctx, LLVMValueRef src,
3846 LLVMValueRef inactive)
3847 {
3848 char name[33], type[8];
3849 LLVMTypeRef src_type = LLVMTypeOf(src);
3850 src = ac_to_integer(ctx, src);
3851 inactive = ac_to_integer(ctx, inactive);
3852 ac_build_type_name_for_intr(LLVMTypeOf(src), type, sizeof(type));
3853 snprintf(name, sizeof(name), "llvm.amdgcn.set.inactive.%s", type);
3854 LLVMValueRef ret =
3855 ac_build_intrinsic(ctx, name,
3856 LLVMTypeOf(src), (LLVMValueRef []) {
3857 src, inactive }, 2,
3858 AC_FUNC_ATTR_READNONE |
3859 AC_FUNC_ATTR_CONVERGENT);
3860 return LLVMBuildBitCast(ctx->builder, ret, src_type, "");
3861 }
3862
3863 static LLVMValueRef
3864 get_reduction_identity(struct ac_llvm_context *ctx, nir_op op, unsigned type_size)
3865 {
3866 if (type_size == 4) {
3867 switch (op) {
3868 case nir_op_iadd: return ctx->i32_0;
3869 case nir_op_fadd: return ctx->f32_0;
3870 case nir_op_imul: return ctx->i32_1;
3871 case nir_op_fmul: return ctx->f32_1;
3872 case nir_op_imin: return LLVMConstInt(ctx->i32, INT32_MAX, 0);
3873 case nir_op_umin: return LLVMConstInt(ctx->i32, UINT32_MAX, 0);
3874 case nir_op_fmin: return LLVMConstReal(ctx->f32, INFINITY);
3875 case nir_op_imax: return LLVMConstInt(ctx->i32, INT32_MIN, 0);
3876 case nir_op_umax: return ctx->i32_0;
3877 case nir_op_fmax: return LLVMConstReal(ctx->f32, -INFINITY);
3878 case nir_op_iand: return LLVMConstInt(ctx->i32, -1, 0);
3879 case nir_op_ior: return ctx->i32_0;
3880 case nir_op_ixor: return ctx->i32_0;
3881 default:
3882 unreachable("bad reduction intrinsic");
3883 }
3884 } else { /* type_size == 64bit */
3885 switch (op) {
3886 case nir_op_iadd: return ctx->i64_0;
3887 case nir_op_fadd: return ctx->f64_0;
3888 case nir_op_imul: return ctx->i64_1;
3889 case nir_op_fmul: return ctx->f64_1;
3890 case nir_op_imin: return LLVMConstInt(ctx->i64, INT64_MAX, 0);
3891 case nir_op_umin: return LLVMConstInt(ctx->i64, UINT64_MAX, 0);
3892 case nir_op_fmin: return LLVMConstReal(ctx->f64, INFINITY);
3893 case nir_op_imax: return LLVMConstInt(ctx->i64, INT64_MIN, 0);
3894 case nir_op_umax: return ctx->i64_0;
3895 case nir_op_fmax: return LLVMConstReal(ctx->f64, -INFINITY);
3896 case nir_op_iand: return LLVMConstInt(ctx->i64, -1, 0);
3897 case nir_op_ior: return ctx->i64_0;
3898 case nir_op_ixor: return ctx->i64_0;
3899 default:
3900 unreachable("bad reduction intrinsic");
3901 }
3902 }
3903 }
3904
3905 static LLVMValueRef
3906 ac_build_alu_op(struct ac_llvm_context *ctx, LLVMValueRef lhs, LLVMValueRef rhs, nir_op op)
3907 {
3908 bool _64bit = ac_get_type_size(LLVMTypeOf(lhs)) == 8;
3909 switch (op) {
3910 case nir_op_iadd: return LLVMBuildAdd(ctx->builder, lhs, rhs, "");
3911 case nir_op_fadd: return LLVMBuildFAdd(ctx->builder, lhs, rhs, "");
3912 case nir_op_imul: return LLVMBuildMul(ctx->builder, lhs, rhs, "");
3913 case nir_op_fmul: return LLVMBuildFMul(ctx->builder, lhs, rhs, "");
3914 case nir_op_imin: return LLVMBuildSelect(ctx->builder,
3915 LLVMBuildICmp(ctx->builder, LLVMIntSLT, lhs, rhs, ""),
3916 lhs, rhs, "");
3917 case nir_op_umin: return LLVMBuildSelect(ctx->builder,
3918 LLVMBuildICmp(ctx->builder, LLVMIntULT, lhs, rhs, ""),
3919 lhs, rhs, "");
3920 case nir_op_fmin: return ac_build_intrinsic(ctx,
3921 _64bit ? "llvm.minnum.f64" : "llvm.minnum.f32",
3922 _64bit ? ctx->f64 : ctx->f32,
3923 (LLVMValueRef[]){lhs, rhs}, 2, AC_FUNC_ATTR_READNONE);
3924 case nir_op_imax: return LLVMBuildSelect(ctx->builder,
3925 LLVMBuildICmp(ctx->builder, LLVMIntSGT, lhs, rhs, ""),
3926 lhs, rhs, "");
3927 case nir_op_umax: return LLVMBuildSelect(ctx->builder,
3928 LLVMBuildICmp(ctx->builder, LLVMIntUGT, lhs, rhs, ""),
3929 lhs, rhs, "");
3930 case nir_op_fmax: return ac_build_intrinsic(ctx,
3931 _64bit ? "llvm.maxnum.f64" : "llvm.maxnum.f32",
3932 _64bit ? ctx->f64 : ctx->f32,
3933 (LLVMValueRef[]){lhs, rhs}, 2, AC_FUNC_ATTR_READNONE);
3934 case nir_op_iand: return LLVMBuildAnd(ctx->builder, lhs, rhs, "");
3935 case nir_op_ior: return LLVMBuildOr(ctx->builder, lhs, rhs, "");
3936 case nir_op_ixor: return LLVMBuildXor(ctx->builder, lhs, rhs, "");
3937 default:
3938 unreachable("bad reduction intrinsic");
3939 }
3940 }
3941
3942 /**
3943 * \param maxprefix specifies that the result only needs to be correct for a
3944 * prefix of this many threads
3945 *
3946 * TODO: add inclusive and excluse scan functions for GFX6.
3947 */
3948 static LLVMValueRef
3949 ac_build_scan(struct ac_llvm_context *ctx, nir_op op, LLVMValueRef src, LLVMValueRef identity,
3950 unsigned maxprefix, bool inclusive)
3951 {
3952 LLVMValueRef result, tmp;
3953
3954 if (ctx->chip_class >= GFX10) {
3955 result = inclusive ? src : identity;
3956 } else {
3957 if (!inclusive)
3958 src = ac_build_dpp(ctx, identity, src, dpp_wf_sr1, 0xf, 0xf, false);
3959 result = src;
3960 }
3961 if (maxprefix <= 1)
3962 return result;
3963 tmp = ac_build_dpp(ctx, identity, src, dpp_row_sr(1), 0xf, 0xf, false);
3964 result = ac_build_alu_op(ctx, result, tmp, op);
3965 if (maxprefix <= 2)
3966 return result;
3967 tmp = ac_build_dpp(ctx, identity, src, dpp_row_sr(2), 0xf, 0xf, false);
3968 result = ac_build_alu_op(ctx, result, tmp, op);
3969 if (maxprefix <= 3)
3970 return result;
3971 tmp = ac_build_dpp(ctx, identity, src, dpp_row_sr(3), 0xf, 0xf, false);
3972 result = ac_build_alu_op(ctx, result, tmp, op);
3973 if (maxprefix <= 4)
3974 return result;
3975 tmp = ac_build_dpp(ctx, identity, result, dpp_row_sr(4), 0xf, 0xe, false);
3976 result = ac_build_alu_op(ctx, result, tmp, op);
3977 if (maxprefix <= 8)
3978 return result;
3979 tmp = ac_build_dpp(ctx, identity, result, dpp_row_sr(8), 0xf, 0xc, false);
3980 result = ac_build_alu_op(ctx, result, tmp, op);
3981 if (maxprefix <= 16)
3982 return result;
3983
3984 if (ctx->chip_class >= GFX10) {
3985 /* dpp_row_bcast{15,31} are not supported on gfx10. */
3986 LLVMBuilderRef builder = ctx->builder;
3987 LLVMValueRef tid = ac_get_thread_id(ctx);
3988 LLVMValueRef cc;
3989 /* TODO-GFX10: Can we get better code-gen by putting this into
3990 * a branch so that LLVM generates EXEC mask manipulations? */
3991 if (inclusive)
3992 tmp = result;
3993 else
3994 tmp = ac_build_alu_op(ctx, result, src, op);
3995 tmp = ac_build_permlane16(ctx, tmp, ~(uint64_t)0, true, false);
3996 tmp = ac_build_alu_op(ctx, result, tmp, op);
3997 cc = LLVMBuildAnd(builder, tid, LLVMConstInt(ctx->i32, 16, false), "");
3998 cc = LLVMBuildICmp(builder, LLVMIntNE, cc, ctx->i32_0, "");
3999 result = LLVMBuildSelect(builder, cc, tmp, result, "");
4000 if (maxprefix <= 32)
4001 return result;
4002
4003 if (inclusive)
4004 tmp = result;
4005 else
4006 tmp = ac_build_alu_op(ctx, result, src, op);
4007 tmp = ac_build_readlane(ctx, tmp, LLVMConstInt(ctx->i32, 31, false));
4008 tmp = ac_build_alu_op(ctx, result, tmp, op);
4009 cc = LLVMBuildICmp(builder, LLVMIntUGE, tid,
4010 LLVMConstInt(ctx->i32, 32, false), "");
4011 result = LLVMBuildSelect(builder, cc, tmp, result, "");
4012 return result;
4013 }
4014
4015 tmp = ac_build_dpp(ctx, identity, result, dpp_row_bcast15, 0xa, 0xf, false);
4016 result = ac_build_alu_op(ctx, result, tmp, op);
4017 if (maxprefix <= 32)
4018 return result;
4019 tmp = ac_build_dpp(ctx, identity, result, dpp_row_bcast31, 0xc, 0xf, false);
4020 result = ac_build_alu_op(ctx, result, tmp, op);
4021 return result;
4022 }
4023
4024 LLVMValueRef
4025 ac_build_inclusive_scan(struct ac_llvm_context *ctx, LLVMValueRef src, nir_op op)
4026 {
4027 LLVMValueRef result;
4028
4029 if (LLVMTypeOf(src) == ctx->i1 && op == nir_op_iadd) {
4030 LLVMBuilderRef builder = ctx->builder;
4031 src = LLVMBuildZExt(builder, src, ctx->i32, "");
4032 result = ac_build_ballot(ctx, src);
4033 result = ac_build_mbcnt(ctx, result);
4034 result = LLVMBuildAdd(builder, result, src, "");
4035 return result;
4036 }
4037
4038 ac_build_optimization_barrier(ctx, &src);
4039
4040 LLVMValueRef identity =
4041 get_reduction_identity(ctx, op, ac_get_type_size(LLVMTypeOf(src)));
4042 result = LLVMBuildBitCast(ctx->builder, ac_build_set_inactive(ctx, src, identity),
4043 LLVMTypeOf(identity), "");
4044 result = ac_build_scan(ctx, op, result, identity, ctx->wave_size, true);
4045
4046 return ac_build_wwm(ctx, result);
4047 }
4048
4049 LLVMValueRef
4050 ac_build_exclusive_scan(struct ac_llvm_context *ctx, LLVMValueRef src, nir_op op)
4051 {
4052 LLVMValueRef result;
4053
4054 if (LLVMTypeOf(src) == ctx->i1 && op == nir_op_iadd) {
4055 LLVMBuilderRef builder = ctx->builder;
4056 src = LLVMBuildZExt(builder, src, ctx->i32, "");
4057 result = ac_build_ballot(ctx, src);
4058 result = ac_build_mbcnt(ctx, result);
4059 return result;
4060 }
4061
4062 ac_build_optimization_barrier(ctx, &src);
4063
4064 LLVMValueRef identity =
4065 get_reduction_identity(ctx, op, ac_get_type_size(LLVMTypeOf(src)));
4066 result = LLVMBuildBitCast(ctx->builder, ac_build_set_inactive(ctx, src, identity),
4067 LLVMTypeOf(identity), "");
4068 result = ac_build_scan(ctx, op, result, identity, ctx->wave_size, false);
4069
4070 return ac_build_wwm(ctx, result);
4071 }
4072
4073 LLVMValueRef
4074 ac_build_reduce(struct ac_llvm_context *ctx, LLVMValueRef src, nir_op op, unsigned cluster_size)
4075 {
4076 if (cluster_size == 1) return src;
4077 ac_build_optimization_barrier(ctx, &src);
4078 LLVMValueRef result, swap;
4079 LLVMValueRef identity = get_reduction_identity(ctx, op,
4080 ac_get_type_size(LLVMTypeOf(src)));
4081 result = LLVMBuildBitCast(ctx->builder,
4082 ac_build_set_inactive(ctx, src, identity),
4083 LLVMTypeOf(identity), "");
4084 swap = ac_build_quad_swizzle(ctx, result, 1, 0, 3, 2);
4085 result = ac_build_alu_op(ctx, result, swap, op);
4086 if (cluster_size == 2) return ac_build_wwm(ctx, result);
4087
4088 swap = ac_build_quad_swizzle(ctx, result, 2, 3, 0, 1);
4089 result = ac_build_alu_op(ctx, result, swap, op);
4090 if (cluster_size == 4) return ac_build_wwm(ctx, result);
4091
4092 if (ctx->chip_class >= GFX8)
4093 swap = ac_build_dpp(ctx, identity, result, dpp_row_half_mirror, 0xf, 0xf, false);
4094 else
4095 swap = ac_build_ds_swizzle(ctx, result, ds_pattern_bitmode(0x1f, 0, 0x04));
4096 result = ac_build_alu_op(ctx, result, swap, op);
4097 if (cluster_size == 8) return ac_build_wwm(ctx, result);
4098
4099 if (ctx->chip_class >= GFX8)
4100 swap = ac_build_dpp(ctx, identity, result, dpp_row_mirror, 0xf, 0xf, false);
4101 else
4102 swap = ac_build_ds_swizzle(ctx, result, ds_pattern_bitmode(0x1f, 0, 0x08));
4103 result = ac_build_alu_op(ctx, result, swap, op);
4104 if (cluster_size == 16) return ac_build_wwm(ctx, result);
4105
4106 if (ctx->chip_class >= GFX10)
4107 swap = ac_build_permlane16(ctx, result, 0, true, false);
4108 else if (ctx->chip_class >= GFX8 && cluster_size != 32)
4109 swap = ac_build_dpp(ctx, identity, result, dpp_row_bcast15, 0xa, 0xf, false);
4110 else
4111 swap = ac_build_ds_swizzle(ctx, result, ds_pattern_bitmode(0x1f, 0, 0x10));
4112 result = ac_build_alu_op(ctx, result, swap, op);
4113 if (cluster_size == 32) return ac_build_wwm(ctx, result);
4114
4115 if (ctx->chip_class >= GFX8) {
4116 if (ctx->chip_class >= GFX10)
4117 swap = ac_build_readlane(ctx, result, LLVMConstInt(ctx->i32, 31, false));
4118 else
4119 swap = ac_build_dpp(ctx, identity, result, dpp_row_bcast31, 0xc, 0xf, false);
4120 result = ac_build_alu_op(ctx, result, swap, op);
4121 result = ac_build_readlane(ctx, result, LLVMConstInt(ctx->i32, 63, 0));
4122 return ac_build_wwm(ctx, result);
4123 } else {
4124 swap = ac_build_readlane(ctx, result, ctx->i32_0);
4125 result = ac_build_readlane(ctx, result, LLVMConstInt(ctx->i32, 32, 0));
4126 result = ac_build_alu_op(ctx, result, swap, op);
4127 return ac_build_wwm(ctx, result);
4128 }
4129 }
4130
4131 /**
4132 * "Top half" of a scan that reduces per-wave values across an entire
4133 * workgroup.
4134 *
4135 * The source value must be present in the highest lane of the wave, and the
4136 * highest lane must be live.
4137 */
4138 void
4139 ac_build_wg_wavescan_top(struct ac_llvm_context *ctx, struct ac_wg_scan *ws)
4140 {
4141 if (ws->maxwaves <= 1)
4142 return;
4143
4144 const LLVMValueRef last_lane = LLVMConstInt(ctx->i32, ctx->wave_size - 1, false);
4145 LLVMBuilderRef builder = ctx->builder;
4146 LLVMValueRef tid = ac_get_thread_id(ctx);
4147 LLVMValueRef tmp;
4148
4149 tmp = LLVMBuildICmp(builder, LLVMIntEQ, tid, last_lane, "");
4150 ac_build_ifcc(ctx, tmp, 1000);
4151 LLVMBuildStore(builder, ws->src, LLVMBuildGEP(builder, ws->scratch, &ws->waveidx, 1, ""));
4152 ac_build_endif(ctx, 1000);
4153 }
4154
4155 /**
4156 * "Bottom half" of a scan that reduces per-wave values across an entire
4157 * workgroup.
4158 *
4159 * The caller must place a barrier between the top and bottom halves.
4160 */
4161 void
4162 ac_build_wg_wavescan_bottom(struct ac_llvm_context *ctx, struct ac_wg_scan *ws)
4163 {
4164 const LLVMTypeRef type = LLVMTypeOf(ws->src);
4165 const LLVMValueRef identity =
4166 get_reduction_identity(ctx, ws->op, ac_get_type_size(type));
4167
4168 if (ws->maxwaves <= 1) {
4169 ws->result_reduce = ws->src;
4170 ws->result_inclusive = ws->src;
4171 ws->result_exclusive = identity;
4172 return;
4173 }
4174 assert(ws->maxwaves <= 32);
4175
4176 LLVMBuilderRef builder = ctx->builder;
4177 LLVMValueRef tid = ac_get_thread_id(ctx);
4178 LLVMBasicBlockRef bbs[2];
4179 LLVMValueRef phivalues_scan[2];
4180 LLVMValueRef tmp, tmp2;
4181
4182 bbs[0] = LLVMGetInsertBlock(builder);
4183 phivalues_scan[0] = LLVMGetUndef(type);
4184
4185 if (ws->enable_reduce)
4186 tmp = LLVMBuildICmp(builder, LLVMIntULT, tid, ws->numwaves, "");
4187 else if (ws->enable_inclusive)
4188 tmp = LLVMBuildICmp(builder, LLVMIntULE, tid, ws->waveidx, "");
4189 else
4190 tmp = LLVMBuildICmp(builder, LLVMIntULT, tid, ws->waveidx, "");
4191 ac_build_ifcc(ctx, tmp, 1001);
4192 {
4193 tmp = LLVMBuildLoad(builder, LLVMBuildGEP(builder, ws->scratch, &tid, 1, ""), "");
4194
4195 ac_build_optimization_barrier(ctx, &tmp);
4196
4197 bbs[1] = LLVMGetInsertBlock(builder);
4198 phivalues_scan[1] = ac_build_scan(ctx, ws->op, tmp, identity, ws->maxwaves, true);
4199 }
4200 ac_build_endif(ctx, 1001);
4201
4202 const LLVMValueRef scan = ac_build_phi(ctx, type, 2, phivalues_scan, bbs);
4203
4204 if (ws->enable_reduce) {
4205 tmp = LLVMBuildSub(builder, ws->numwaves, ctx->i32_1, "");
4206 ws->result_reduce = ac_build_readlane(ctx, scan, tmp);
4207 }
4208 if (ws->enable_inclusive)
4209 ws->result_inclusive = ac_build_readlane(ctx, scan, ws->waveidx);
4210 if (ws->enable_exclusive) {
4211 tmp = LLVMBuildSub(builder, ws->waveidx, ctx->i32_1, "");
4212 tmp = ac_build_readlane(ctx, scan, tmp);
4213 tmp2 = LLVMBuildICmp(builder, LLVMIntEQ, ws->waveidx, ctx->i32_0, "");
4214 ws->result_exclusive = LLVMBuildSelect(builder, tmp2, identity, tmp, "");
4215 }
4216 }
4217
4218 /**
4219 * Inclusive scan of a per-wave value across an entire workgroup.
4220 *
4221 * This implies an s_barrier instruction.
4222 *
4223 * Unlike ac_build_inclusive_scan, the caller \em must ensure that all threads
4224 * of the workgroup are live. (This requirement cannot easily be relaxed in a
4225 * useful manner because of the barrier in the algorithm.)
4226 */
4227 void
4228 ac_build_wg_wavescan(struct ac_llvm_context *ctx, struct ac_wg_scan *ws)
4229 {
4230 ac_build_wg_wavescan_top(ctx, ws);
4231 ac_build_s_barrier(ctx);
4232 ac_build_wg_wavescan_bottom(ctx, ws);
4233 }
4234
4235 /**
4236 * "Top half" of a scan that reduces per-thread values across an entire
4237 * workgroup.
4238 *
4239 * All lanes must be active when this code runs.
4240 */
4241 void
4242 ac_build_wg_scan_top(struct ac_llvm_context *ctx, struct ac_wg_scan *ws)
4243 {
4244 if (ws->enable_exclusive) {
4245 ws->extra = ac_build_exclusive_scan(ctx, ws->src, ws->op);
4246 if (LLVMTypeOf(ws->src) == ctx->i1 && ws->op == nir_op_iadd)
4247 ws->src = LLVMBuildZExt(ctx->builder, ws->src, ctx->i32, "");
4248 ws->src = ac_build_alu_op(ctx, ws->extra, ws->src, ws->op);
4249 } else {
4250 ws->src = ac_build_inclusive_scan(ctx, ws->src, ws->op);
4251 }
4252
4253 bool enable_inclusive = ws->enable_inclusive;
4254 bool enable_exclusive = ws->enable_exclusive;
4255 ws->enable_inclusive = false;
4256 ws->enable_exclusive = ws->enable_exclusive || enable_inclusive;
4257 ac_build_wg_wavescan_top(ctx, ws);
4258 ws->enable_inclusive = enable_inclusive;
4259 ws->enable_exclusive = enable_exclusive;
4260 }
4261
4262 /**
4263 * "Bottom half" of a scan that reduces per-thread values across an entire
4264 * workgroup.
4265 *
4266 * The caller must place a barrier between the top and bottom halves.
4267 */
4268 void
4269 ac_build_wg_scan_bottom(struct ac_llvm_context *ctx, struct ac_wg_scan *ws)
4270 {
4271 bool enable_inclusive = ws->enable_inclusive;
4272 bool enable_exclusive = ws->enable_exclusive;
4273 ws->enable_inclusive = false;
4274 ws->enable_exclusive = ws->enable_exclusive || enable_inclusive;
4275 ac_build_wg_wavescan_bottom(ctx, ws);
4276 ws->enable_inclusive = enable_inclusive;
4277 ws->enable_exclusive = enable_exclusive;
4278
4279 /* ws->result_reduce is already the correct value */
4280 if (ws->enable_inclusive)
4281 ws->result_inclusive = ac_build_alu_op(ctx, ws->result_inclusive, ws->src, ws->op);
4282 if (ws->enable_exclusive)
4283 ws->result_exclusive = ac_build_alu_op(ctx, ws->result_exclusive, ws->extra, ws->op);
4284 }
4285
4286 /**
4287 * A scan that reduces per-thread values across an entire workgroup.
4288 *
4289 * The caller must ensure that all lanes are active when this code runs
4290 * (WWM is insufficient!), because there is an implied barrier.
4291 */
4292 void
4293 ac_build_wg_scan(struct ac_llvm_context *ctx, struct ac_wg_scan *ws)
4294 {
4295 ac_build_wg_scan_top(ctx, ws);
4296 ac_build_s_barrier(ctx);
4297 ac_build_wg_scan_bottom(ctx, ws);
4298 }
4299
4300 LLVMValueRef
4301 ac_build_quad_swizzle(struct ac_llvm_context *ctx, LLVMValueRef src,
4302 unsigned lane0, unsigned lane1, unsigned lane2, unsigned lane3)
4303 {
4304 unsigned mask = dpp_quad_perm(lane0, lane1, lane2, lane3);
4305 if (ctx->chip_class >= GFX8) {
4306 return ac_build_dpp(ctx, src, src, mask, 0xf, 0xf, false);
4307 } else {
4308 return ac_build_ds_swizzle(ctx, src, (1 << 15) | mask);
4309 }
4310 }
4311
4312 LLVMValueRef
4313 ac_build_shuffle(struct ac_llvm_context *ctx, LLVMValueRef src, LLVMValueRef index)
4314 {
4315 index = LLVMBuildMul(ctx->builder, index, LLVMConstInt(ctx->i32, 4, 0), "");
4316 return ac_build_intrinsic(ctx,
4317 "llvm.amdgcn.ds.bpermute", ctx->i32,
4318 (LLVMValueRef []) {index, src}, 2,
4319 AC_FUNC_ATTR_READNONE |
4320 AC_FUNC_ATTR_CONVERGENT);
4321 }
4322
4323 LLVMValueRef
4324 ac_build_frexp_exp(struct ac_llvm_context *ctx, LLVMValueRef src0,
4325 unsigned bitsize)
4326 {
4327 LLVMTypeRef type;
4328 char *intr;
4329
4330 if (bitsize == 16) {
4331 intr = "llvm.amdgcn.frexp.exp.i16.f16";
4332 type = ctx->i16;
4333 } else if (bitsize == 32) {
4334 intr = "llvm.amdgcn.frexp.exp.i32.f32";
4335 type = ctx->i32;
4336 } else {
4337 intr = "llvm.amdgcn.frexp.exp.i32.f64";
4338 type = ctx->i32;
4339 }
4340
4341 LLVMValueRef params[] = {
4342 src0,
4343 };
4344 return ac_build_intrinsic(ctx, intr, type, params, 1,
4345 AC_FUNC_ATTR_READNONE);
4346 }
4347 LLVMValueRef
4348 ac_build_frexp_mant(struct ac_llvm_context *ctx, LLVMValueRef src0,
4349 unsigned bitsize)
4350 {
4351 LLVMTypeRef type;
4352 char *intr;
4353
4354 if (bitsize == 16) {
4355 intr = "llvm.amdgcn.frexp.mant.f16";
4356 type = ctx->f16;
4357 } else if (bitsize == 32) {
4358 intr = "llvm.amdgcn.frexp.mant.f32";
4359 type = ctx->f32;
4360 } else {
4361 intr = "llvm.amdgcn.frexp.mant.f64";
4362 type = ctx->f64;
4363 }
4364
4365 LLVMValueRef params[] = {
4366 src0,
4367 };
4368 return ac_build_intrinsic(ctx, intr, type, params, 1,
4369 AC_FUNC_ATTR_READNONE);
4370 }
4371
4372 /*
4373 * this takes an I,J coordinate pair,
4374 * and works out the X and Y derivatives.
4375 * it returns DDX(I), DDX(J), DDY(I), DDY(J).
4376 */
4377 LLVMValueRef
4378 ac_build_ddxy_interp(struct ac_llvm_context *ctx, LLVMValueRef interp_ij)
4379 {
4380 LLVMValueRef result[4], a;
4381 unsigned i;
4382
4383 for (i = 0; i < 2; i++) {
4384 a = LLVMBuildExtractElement(ctx->builder, interp_ij,
4385 LLVMConstInt(ctx->i32, i, false), "");
4386 result[i] = ac_build_ddxy(ctx, AC_TID_MASK_TOP_LEFT, 1, a);
4387 result[2+i] = ac_build_ddxy(ctx, AC_TID_MASK_TOP_LEFT, 2, a);
4388 }
4389 return ac_build_gather_values(ctx, result, 4);
4390 }
4391
4392 LLVMValueRef
4393 ac_build_load_helper_invocation(struct ac_llvm_context *ctx)
4394 {
4395 LLVMValueRef result = ac_build_intrinsic(ctx, "llvm.amdgcn.ps.live",
4396 ctx->i1, NULL, 0,
4397 AC_FUNC_ATTR_READNONE);
4398 result = LLVMBuildNot(ctx->builder, result, "");
4399 return LLVMBuildSExt(ctx->builder, result, ctx->i32, "");
4400 }
4401
4402 LLVMValueRef ac_build_call(struct ac_llvm_context *ctx, LLVMValueRef func,
4403 LLVMValueRef *args, unsigned num_args)
4404 {
4405 LLVMValueRef ret = LLVMBuildCall(ctx->builder, func, args, num_args, "");
4406 LLVMSetInstructionCallConv(ret, LLVMGetFunctionCallConv(func));
4407 return ret;
4408 }
4409
4410 void
4411 ac_export_mrt_z(struct ac_llvm_context *ctx, LLVMValueRef depth,
4412 LLVMValueRef stencil, LLVMValueRef samplemask,
4413 struct ac_export_args *args)
4414 {
4415 unsigned mask = 0;
4416 unsigned format = ac_get_spi_shader_z_format(depth != NULL,
4417 stencil != NULL,
4418 samplemask != NULL);
4419
4420 assert(depth || stencil || samplemask);
4421
4422 memset(args, 0, sizeof(*args));
4423
4424 args->valid_mask = 1; /* whether the EXEC mask is valid */
4425 args->done = 1; /* DONE bit */
4426
4427 /* Specify the target we are exporting */
4428 args->target = V_008DFC_SQ_EXP_MRTZ;
4429
4430 args->compr = 0; /* COMP flag */
4431 args->out[0] = LLVMGetUndef(ctx->f32); /* R, depth */
4432 args->out[1] = LLVMGetUndef(ctx->f32); /* G, stencil test val[0:7], stencil op val[8:15] */
4433 args->out[2] = LLVMGetUndef(ctx->f32); /* B, sample mask */
4434 args->out[3] = LLVMGetUndef(ctx->f32); /* A, alpha to mask */
4435
4436 if (format == V_028710_SPI_SHADER_UINT16_ABGR) {
4437 assert(!depth);
4438 args->compr = 1; /* COMPR flag */
4439
4440 if (stencil) {
4441 /* Stencil should be in X[23:16]. */
4442 stencil = ac_to_integer(ctx, stencil);
4443 stencil = LLVMBuildShl(ctx->builder, stencil,
4444 LLVMConstInt(ctx->i32, 16, 0), "");
4445 args->out[0] = ac_to_float(ctx, stencil);
4446 mask |= 0x3;
4447 }
4448 if (samplemask) {
4449 /* SampleMask should be in Y[15:0]. */
4450 args->out[1] = samplemask;
4451 mask |= 0xc;
4452 }
4453 } else {
4454 if (depth) {
4455 args->out[0] = depth;
4456 mask |= 0x1;
4457 }
4458 if (stencil) {
4459 args->out[1] = stencil;
4460 mask |= 0x2;
4461 }
4462 if (samplemask) {
4463 args->out[2] = samplemask;
4464 mask |= 0x4;
4465 }
4466 }
4467
4468 /* GFX6 (except OLAND and HAINAN) has a bug that it only looks
4469 * at the X writemask component. */
4470 if (ctx->chip_class == GFX6 &&
4471 ctx->family != CHIP_OLAND &&
4472 ctx->family != CHIP_HAINAN)
4473 mask |= 0x1;
4474
4475 /* Specify which components to enable */
4476 args->enabled_channels = mask;
4477 }
4478