5483b3146c01c284eb7153f4cb67e5f1720a319b
[mesa.git] / src / amd / llvm / 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 ctx->context = LLVMContextCreate();
69
70 ctx->chip_class = chip_class;
71 ctx->family = family;
72 ctx->wave_size = wave_size;
73 ctx->ballot_mask_bits = ballot_mask_bits;
74 ctx->float_mode = float_mode;
75 ctx->module = ac_create_module(wave_size == 32 ? compiler->tm_wave32
76 : compiler->tm,
77 ctx->context);
78 ctx->builder = ac_create_builder(ctx->context, float_mode);
79
80 ctx->voidt = LLVMVoidTypeInContext(ctx->context);
81 ctx->i1 = LLVMInt1TypeInContext(ctx->context);
82 ctx->i8 = LLVMInt8TypeInContext(ctx->context);
83 ctx->i16 = LLVMIntTypeInContext(ctx->context, 16);
84 ctx->i32 = LLVMIntTypeInContext(ctx->context, 32);
85 ctx->i64 = LLVMIntTypeInContext(ctx->context, 64);
86 ctx->i128 = LLVMIntTypeInContext(ctx->context, 128);
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->v4i16 = LLVMVectorType(ctx->i16, 4);
93 ctx->v2f16 = LLVMVectorType(ctx->f16, 2);
94 ctx->v4f16 = LLVMVectorType(ctx->f16, 4);
95 ctx->v2i32 = LLVMVectorType(ctx->i32, 2);
96 ctx->v3i32 = LLVMVectorType(ctx->i32, 3);
97 ctx->v4i32 = LLVMVectorType(ctx->i32, 4);
98 ctx->v2f32 = LLVMVectorType(ctx->f32, 2);
99 ctx->v3f32 = LLVMVectorType(ctx->f32, 3);
100 ctx->v4f32 = LLVMVectorType(ctx->f32, 4);
101 ctx->v8i32 = LLVMVectorType(ctx->i32, 8);
102 ctx->iN_wavemask = LLVMIntTypeInContext(ctx->context, ctx->wave_size);
103 ctx->iN_ballotmask = LLVMIntTypeInContext(ctx->context, ballot_mask_bits);
104
105 ctx->i8_0 = LLVMConstInt(ctx->i8, 0, false);
106 ctx->i8_1 = LLVMConstInt(ctx->i8, 1, false);
107 ctx->i16_0 = LLVMConstInt(ctx->i16, 0, false);
108 ctx->i16_1 = LLVMConstInt(ctx->i16, 1, false);
109 ctx->i32_0 = LLVMConstInt(ctx->i32, 0, false);
110 ctx->i32_1 = LLVMConstInt(ctx->i32, 1, false);
111 ctx->i64_0 = LLVMConstInt(ctx->i64, 0, false);
112 ctx->i64_1 = LLVMConstInt(ctx->i64, 1, false);
113 ctx->i128_0 = LLVMConstInt(ctx->i128, 0, false);
114 ctx->i128_1 = LLVMConstInt(ctx->i128, 1, false);
115 ctx->f16_0 = LLVMConstReal(ctx->f16, 0.0);
116 ctx->f16_1 = LLVMConstReal(ctx->f16, 1.0);
117 ctx->f32_0 = LLVMConstReal(ctx->f32, 0.0);
118 ctx->f32_1 = LLVMConstReal(ctx->f32, 1.0);
119 ctx->f64_0 = LLVMConstReal(ctx->f64, 0.0);
120 ctx->f64_1 = LLVMConstReal(ctx->f64, 1.0);
121
122 ctx->i1false = LLVMConstInt(ctx->i1, 0, false);
123 ctx->i1true = LLVMConstInt(ctx->i1, 1, false);
124
125 ctx->range_md_kind = LLVMGetMDKindIDInContext(ctx->context,
126 "range", 5);
127
128 ctx->invariant_load_md_kind = LLVMGetMDKindIDInContext(ctx->context,
129 "invariant.load", 14);
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 (LLVMGetTypeKind(type) == LLVMPointerTypeKind) {
180 if (LLVMGetPointerAddressSpace(type) == AC_ADDR_SPACE_LDS)
181 return 32;
182 }
183
184 if (type == ctx->f16)
185 return 16;
186 if (type == ctx->f32)
187 return 32;
188 if (type == ctx->f64)
189 return 64;
190
191 unreachable("Unhandled type kind in get_elem_bits");
192 }
193
194 unsigned
195 ac_get_type_size(LLVMTypeRef type)
196 {
197 LLVMTypeKind kind = LLVMGetTypeKind(type);
198
199 switch (kind) {
200 case LLVMIntegerTypeKind:
201 return LLVMGetIntTypeWidth(type) / 8;
202 case LLVMHalfTypeKind:
203 return 2;
204 case LLVMFloatTypeKind:
205 return 4;
206 case LLVMDoubleTypeKind:
207 return 8;
208 case LLVMPointerTypeKind:
209 if (LLVMGetPointerAddressSpace(type) == AC_ADDR_SPACE_CONST_32BIT)
210 return 4;
211 return 8;
212 case LLVMVectorTypeKind:
213 return LLVMGetVectorSize(type) *
214 ac_get_type_size(LLVMGetElementType(type));
215 case LLVMArrayTypeKind:
216 return LLVMGetArrayLength(type) *
217 ac_get_type_size(LLVMGetElementType(type));
218 default:
219 assert(0);
220 return 0;
221 }
222 }
223
224 static LLVMTypeRef to_integer_type_scalar(struct ac_llvm_context *ctx, LLVMTypeRef t)
225 {
226 if (t == ctx->i8)
227 return ctx->i8;
228 else if (t == ctx->f16 || t == ctx->i16)
229 return ctx->i16;
230 else if (t == ctx->f32 || t == ctx->i32)
231 return ctx->i32;
232 else if (t == ctx->f64 || t == ctx->i64)
233 return ctx->i64;
234 else
235 unreachable("Unhandled integer size");
236 }
237
238 LLVMTypeRef
239 ac_to_integer_type(struct ac_llvm_context *ctx, LLVMTypeRef t)
240 {
241 if (LLVMGetTypeKind(t) == LLVMVectorTypeKind) {
242 LLVMTypeRef elem_type = LLVMGetElementType(t);
243 return LLVMVectorType(to_integer_type_scalar(ctx, elem_type),
244 LLVMGetVectorSize(t));
245 }
246 if (LLVMGetTypeKind(t) == LLVMPointerTypeKind) {
247 switch (LLVMGetPointerAddressSpace(t)) {
248 case AC_ADDR_SPACE_GLOBAL:
249 return ctx->i64;
250 case AC_ADDR_SPACE_CONST_32BIT:
251 case AC_ADDR_SPACE_LDS:
252 return ctx->i32;
253 default:
254 unreachable("unhandled address space");
255 }
256 }
257 return to_integer_type_scalar(ctx, t);
258 }
259
260 LLVMValueRef
261 ac_to_integer(struct ac_llvm_context *ctx, LLVMValueRef v)
262 {
263 LLVMTypeRef type = LLVMTypeOf(v);
264 if (LLVMGetTypeKind(type) == LLVMPointerTypeKind) {
265 return LLVMBuildPtrToInt(ctx->builder, v, ac_to_integer_type(ctx, type), "");
266 }
267 return LLVMBuildBitCast(ctx->builder, v, ac_to_integer_type(ctx, type), "");
268 }
269
270 LLVMValueRef
271 ac_to_integer_or_pointer(struct ac_llvm_context *ctx, LLVMValueRef v)
272 {
273 LLVMTypeRef type = LLVMTypeOf(v);
274 if (LLVMGetTypeKind(type) == LLVMPointerTypeKind)
275 return v;
276 return ac_to_integer(ctx, v);
277 }
278
279 static LLVMTypeRef to_float_type_scalar(struct ac_llvm_context *ctx, LLVMTypeRef t)
280 {
281 if (t == ctx->i8)
282 return ctx->i8;
283 else if (t == ctx->i16 || t == ctx->f16)
284 return ctx->f16;
285 else if (t == ctx->i32 || t == ctx->f32)
286 return ctx->f32;
287 else if (t == ctx->i64 || t == ctx->f64)
288 return ctx->f64;
289 else
290 unreachable("Unhandled float size");
291 }
292
293 LLVMTypeRef
294 ac_to_float_type(struct ac_llvm_context *ctx, LLVMTypeRef t)
295 {
296 if (LLVMGetTypeKind(t) == LLVMVectorTypeKind) {
297 LLVMTypeRef elem_type = LLVMGetElementType(t);
298 return LLVMVectorType(to_float_type_scalar(ctx, elem_type),
299 LLVMGetVectorSize(t));
300 }
301 return to_float_type_scalar(ctx, t);
302 }
303
304 LLVMValueRef
305 ac_to_float(struct ac_llvm_context *ctx, LLVMValueRef v)
306 {
307 LLVMTypeRef type = LLVMTypeOf(v);
308 return LLVMBuildBitCast(ctx->builder, v, ac_to_float_type(ctx, type), "");
309 }
310
311
312 LLVMValueRef
313 ac_build_intrinsic(struct ac_llvm_context *ctx, const char *name,
314 LLVMTypeRef return_type, LLVMValueRef *params,
315 unsigned param_count, unsigned attrib_mask)
316 {
317 LLVMValueRef function, call;
318 bool set_callsite_attrs = !(attrib_mask & AC_FUNC_ATTR_LEGACY);
319
320 function = LLVMGetNamedFunction(ctx->module, name);
321 if (!function) {
322 LLVMTypeRef param_types[32], function_type;
323 unsigned i;
324
325 assert(param_count <= 32);
326
327 for (i = 0; i < param_count; ++i) {
328 assert(params[i]);
329 param_types[i] = LLVMTypeOf(params[i]);
330 }
331 function_type =
332 LLVMFunctionType(return_type, param_types, param_count, 0);
333 function = LLVMAddFunction(ctx->module, name, function_type);
334
335 LLVMSetFunctionCallConv(function, LLVMCCallConv);
336 LLVMSetLinkage(function, LLVMExternalLinkage);
337
338 if (!set_callsite_attrs)
339 ac_add_func_attributes(ctx->context, function, attrib_mask);
340 }
341
342 call = LLVMBuildCall(ctx->builder, function, params, param_count, "");
343 if (set_callsite_attrs)
344 ac_add_func_attributes(ctx->context, call, attrib_mask);
345 return call;
346 }
347
348 /**
349 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
350 * intrinsic names).
351 */
352 void ac_build_type_name_for_intr(LLVMTypeRef type, char *buf, unsigned bufsize)
353 {
354 LLVMTypeRef elem_type = type;
355
356 assert(bufsize >= 8);
357
358 if (LLVMGetTypeKind(type) == LLVMVectorTypeKind) {
359 int ret = snprintf(buf, bufsize, "v%u",
360 LLVMGetVectorSize(type));
361 if (ret < 0) {
362 char *type_name = LLVMPrintTypeToString(type);
363 fprintf(stderr, "Error building type name for: %s\n",
364 type_name);
365 LLVMDisposeMessage(type_name);
366 return;
367 }
368 elem_type = LLVMGetElementType(type);
369 buf += ret;
370 bufsize -= ret;
371 }
372 switch (LLVMGetTypeKind(elem_type)) {
373 default: break;
374 case LLVMIntegerTypeKind:
375 snprintf(buf, bufsize, "i%d", LLVMGetIntTypeWidth(elem_type));
376 break;
377 case LLVMHalfTypeKind:
378 snprintf(buf, bufsize, "f16");
379 break;
380 case LLVMFloatTypeKind:
381 snprintf(buf, bufsize, "f32");
382 break;
383 case LLVMDoubleTypeKind:
384 snprintf(buf, bufsize, "f64");
385 break;
386 }
387 }
388
389 /**
390 * Helper function that builds an LLVM IR PHI node and immediately adds
391 * incoming edges.
392 */
393 LLVMValueRef
394 ac_build_phi(struct ac_llvm_context *ctx, LLVMTypeRef type,
395 unsigned count_incoming, LLVMValueRef *values,
396 LLVMBasicBlockRef *blocks)
397 {
398 LLVMValueRef phi = LLVMBuildPhi(ctx->builder, type, "");
399 LLVMAddIncoming(phi, values, blocks, count_incoming);
400 return phi;
401 }
402
403 void ac_build_s_barrier(struct ac_llvm_context *ctx)
404 {
405 ac_build_intrinsic(ctx, "llvm.amdgcn.s.barrier", ctx->voidt, NULL,
406 0, AC_FUNC_ATTR_CONVERGENT);
407 }
408
409 /* Prevent optimizations (at least of memory accesses) across the current
410 * point in the program by emitting empty inline assembly that is marked as
411 * having side effects.
412 *
413 * Optionally, a value can be passed through the inline assembly to prevent
414 * LLVM from hoisting calls to ReadNone functions.
415 */
416 void
417 ac_build_optimization_barrier(struct ac_llvm_context *ctx,
418 LLVMValueRef *pvgpr)
419 {
420 static int counter = 0;
421
422 LLVMBuilderRef builder = ctx->builder;
423 char code[16];
424
425 snprintf(code, sizeof(code), "; %d", p_atomic_inc_return(&counter));
426
427 if (!pvgpr) {
428 LLVMTypeRef ftype = LLVMFunctionType(ctx->voidt, NULL, 0, false);
429 LLVMValueRef inlineasm = LLVMConstInlineAsm(ftype, code, "", true, false);
430 LLVMBuildCall(builder, inlineasm, NULL, 0, "");
431 } else {
432 LLVMTypeRef ftype = LLVMFunctionType(ctx->i32, &ctx->i32, 1, false);
433 LLVMValueRef inlineasm = LLVMConstInlineAsm(ftype, code, "=v,0", true, false);
434 LLVMTypeRef type = LLVMTypeOf(*pvgpr);
435 unsigned bitsize = ac_get_elem_bits(ctx, type);
436 LLVMValueRef vgpr = *pvgpr;
437 LLVMTypeRef vgpr_type;
438 unsigned vgpr_size;
439 LLVMValueRef vgpr0;
440
441 if (bitsize < 32)
442 vgpr = LLVMBuildZExt(ctx->builder, vgpr, ctx->i32, "");
443
444 vgpr_type = LLVMTypeOf(vgpr);
445 vgpr_size = ac_get_type_size(vgpr_type);
446
447 assert(vgpr_size % 4 == 0);
448
449 vgpr = LLVMBuildBitCast(builder, vgpr, LLVMVectorType(ctx->i32, vgpr_size / 4), "");
450 vgpr0 = LLVMBuildExtractElement(builder, vgpr, ctx->i32_0, "");
451 vgpr0 = LLVMBuildCall(builder, inlineasm, &vgpr0, 1, "");
452 vgpr = LLVMBuildInsertElement(builder, vgpr, vgpr0, ctx->i32_0, "");
453 vgpr = LLVMBuildBitCast(builder, vgpr, vgpr_type, "");
454
455 if (bitsize < 32)
456 vgpr = LLVMBuildTrunc(builder, vgpr, type, "");
457
458 *pvgpr = vgpr;
459 }
460 }
461
462 LLVMValueRef
463 ac_build_shader_clock(struct ac_llvm_context *ctx, nir_scope scope)
464 {
465 const char *name = scope == NIR_SCOPE_DEVICE ? "llvm.amdgcn.s.memrealtime" : "llvm.amdgcn.s.memtime";
466 LLVMValueRef tmp = ac_build_intrinsic(ctx, name, ctx->i64, NULL, 0, 0);
467 return LLVMBuildBitCast(ctx->builder, tmp, ctx->v2i32, "");
468 }
469
470 LLVMValueRef
471 ac_build_ballot(struct ac_llvm_context *ctx,
472 LLVMValueRef value)
473 {
474 const char *name;
475
476 if (LLVM_VERSION_MAJOR >= 9) {
477 if (ctx->wave_size == 64)
478 name = "llvm.amdgcn.icmp.i64.i32";
479 else
480 name = "llvm.amdgcn.icmp.i32.i32";
481 } else {
482 name = "llvm.amdgcn.icmp.i32";
483 }
484 LLVMValueRef args[3] = {
485 value,
486 ctx->i32_0,
487 LLVMConstInt(ctx->i32, LLVMIntNE, 0)
488 };
489
490 /* We currently have no other way to prevent LLVM from lifting the icmp
491 * calls to a dominating basic block.
492 */
493 ac_build_optimization_barrier(ctx, &args[0]);
494
495 args[0] = ac_to_integer(ctx, args[0]);
496
497 return ac_build_intrinsic(ctx, name, ctx->iN_wavemask, args, 3,
498 AC_FUNC_ATTR_NOUNWIND |
499 AC_FUNC_ATTR_READNONE |
500 AC_FUNC_ATTR_CONVERGENT);
501 }
502
503 LLVMValueRef ac_get_i1_sgpr_mask(struct ac_llvm_context *ctx,
504 LLVMValueRef value)
505 {
506 const char *name;
507
508 if (LLVM_VERSION_MAJOR >= 9) {
509 if (ctx->wave_size == 64)
510 name = "llvm.amdgcn.icmp.i64.i1";
511 else
512 name = "llvm.amdgcn.icmp.i32.i1";
513 } else {
514 name = "llvm.amdgcn.icmp.i1";
515 }
516 LLVMValueRef args[3] = {
517 value,
518 ctx->i1false,
519 LLVMConstInt(ctx->i32, LLVMIntNE, 0),
520 };
521
522 return ac_build_intrinsic(ctx, name, ctx->iN_wavemask, args, 3,
523 AC_FUNC_ATTR_NOUNWIND |
524 AC_FUNC_ATTR_READNONE |
525 AC_FUNC_ATTR_CONVERGENT);
526 }
527
528 LLVMValueRef
529 ac_build_vote_all(struct ac_llvm_context *ctx, LLVMValueRef value)
530 {
531 LLVMValueRef active_set = ac_build_ballot(ctx, ctx->i32_1);
532 LLVMValueRef vote_set = ac_build_ballot(ctx, value);
533 return LLVMBuildICmp(ctx->builder, LLVMIntEQ, vote_set, active_set, "");
534 }
535
536 LLVMValueRef
537 ac_build_vote_any(struct ac_llvm_context *ctx, LLVMValueRef value)
538 {
539 LLVMValueRef vote_set = ac_build_ballot(ctx, value);
540 return LLVMBuildICmp(ctx->builder, LLVMIntNE, vote_set,
541 LLVMConstInt(ctx->iN_wavemask, 0, 0), "");
542 }
543
544 LLVMValueRef
545 ac_build_vote_eq(struct ac_llvm_context *ctx, LLVMValueRef value)
546 {
547 LLVMValueRef active_set = ac_build_ballot(ctx, ctx->i32_1);
548 LLVMValueRef vote_set = ac_build_ballot(ctx, value);
549
550 LLVMValueRef all = LLVMBuildICmp(ctx->builder, LLVMIntEQ,
551 vote_set, active_set, "");
552 LLVMValueRef none = LLVMBuildICmp(ctx->builder, LLVMIntEQ,
553 vote_set,
554 LLVMConstInt(ctx->iN_wavemask, 0, 0), "");
555 return LLVMBuildOr(ctx->builder, all, none, "");
556 }
557
558 LLVMValueRef
559 ac_build_varying_gather_values(struct ac_llvm_context *ctx, LLVMValueRef *values,
560 unsigned value_count, unsigned component)
561 {
562 LLVMValueRef vec = NULL;
563
564 if (value_count == 1) {
565 return values[component];
566 } else if (!value_count)
567 unreachable("value_count is 0");
568
569 for (unsigned i = component; i < value_count + component; i++) {
570 LLVMValueRef value = values[i];
571
572 if (i == component)
573 vec = LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value), value_count));
574 LLVMValueRef index = LLVMConstInt(ctx->i32, i - component, false);
575 vec = LLVMBuildInsertElement(ctx->builder, vec, value, index, "");
576 }
577 return vec;
578 }
579
580 LLVMValueRef
581 ac_build_gather_values_extended(struct ac_llvm_context *ctx,
582 LLVMValueRef *values,
583 unsigned value_count,
584 unsigned value_stride,
585 bool load,
586 bool always_vector)
587 {
588 LLVMBuilderRef builder = ctx->builder;
589 LLVMValueRef vec = NULL;
590 unsigned i;
591
592 if (value_count == 1 && !always_vector) {
593 if (load)
594 return LLVMBuildLoad(builder, values[0], "");
595 return values[0];
596 } else if (!value_count)
597 unreachable("value_count is 0");
598
599 for (i = 0; i < value_count; i++) {
600 LLVMValueRef value = values[i * value_stride];
601 if (load)
602 value = LLVMBuildLoad(builder, value, "");
603
604 if (!i)
605 vec = LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value), value_count));
606 LLVMValueRef index = LLVMConstInt(ctx->i32, i, false);
607 vec = LLVMBuildInsertElement(builder, vec, value, index, "");
608 }
609 return vec;
610 }
611
612 LLVMValueRef
613 ac_build_gather_values(struct ac_llvm_context *ctx,
614 LLVMValueRef *values,
615 unsigned value_count)
616 {
617 return ac_build_gather_values_extended(ctx, values, value_count, 1, false, false);
618 }
619
620 /* Expand a scalar or vector to <dst_channels x type> by filling the remaining
621 * channels with undef. Extract at most src_channels components from the input.
622 */
623 static LLVMValueRef
624 ac_build_expand(struct ac_llvm_context *ctx,
625 LLVMValueRef value,
626 unsigned src_channels,
627 unsigned dst_channels)
628 {
629 LLVMTypeRef elemtype;
630 LLVMValueRef chan[dst_channels];
631
632 if (LLVMGetTypeKind(LLVMTypeOf(value)) == LLVMVectorTypeKind) {
633 unsigned vec_size = LLVMGetVectorSize(LLVMTypeOf(value));
634
635 if (src_channels == dst_channels && vec_size == dst_channels)
636 return value;
637
638 src_channels = MIN2(src_channels, vec_size);
639
640 for (unsigned i = 0; i < src_channels; i++)
641 chan[i] = ac_llvm_extract_elem(ctx, value, i);
642
643 elemtype = LLVMGetElementType(LLVMTypeOf(value));
644 } else {
645 if (src_channels) {
646 assert(src_channels == 1);
647 chan[0] = value;
648 }
649 elemtype = LLVMTypeOf(value);
650 }
651
652 for (unsigned i = src_channels; i < dst_channels; i++)
653 chan[i] = LLVMGetUndef(elemtype);
654
655 return ac_build_gather_values(ctx, chan, dst_channels);
656 }
657
658 /* Extract components [start, start + channels) from a vector.
659 */
660 LLVMValueRef
661 ac_extract_components(struct ac_llvm_context *ctx,
662 LLVMValueRef value,
663 unsigned start,
664 unsigned channels)
665 {
666 LLVMValueRef chan[channels];
667
668 for (unsigned i = 0; i < channels; i++)
669 chan[i] = ac_llvm_extract_elem(ctx, value, i + start);
670
671 return ac_build_gather_values(ctx, chan, channels);
672 }
673
674 /* Expand a scalar or vector to <4 x type> by filling the remaining channels
675 * with undef. Extract at most num_channels components from the input.
676 */
677 LLVMValueRef ac_build_expand_to_vec4(struct ac_llvm_context *ctx,
678 LLVMValueRef value,
679 unsigned num_channels)
680 {
681 return ac_build_expand(ctx, value, num_channels, 4);
682 }
683
684 LLVMValueRef ac_build_round(struct ac_llvm_context *ctx, LLVMValueRef value)
685 {
686 unsigned type_size = ac_get_type_size(LLVMTypeOf(value));
687 const char *name;
688
689 if (type_size == 2)
690 name = "llvm.rint.f16";
691 else if (type_size == 4)
692 name = "llvm.rint.f32";
693 else
694 name = "llvm.rint.f64";
695
696 return ac_build_intrinsic(ctx, name, LLVMTypeOf(value), &value, 1,
697 AC_FUNC_ATTR_READNONE);
698 }
699
700 LLVMValueRef
701 ac_build_fdiv(struct ac_llvm_context *ctx,
702 LLVMValueRef num,
703 LLVMValueRef den)
704 {
705 unsigned type_size = ac_get_type_size(LLVMTypeOf(den));
706 const char *name;
707
708 if (type_size == 2)
709 name = "llvm.amdgcn.rcp.f16";
710 else if (type_size == 4)
711 name = "llvm.amdgcn.rcp.f32";
712 else
713 name = "llvm.amdgcn.rcp.f64";
714
715 LLVMValueRef rcp = ac_build_intrinsic(ctx, name, LLVMTypeOf(den),
716 &den, 1, AC_FUNC_ATTR_READNONE);
717
718 return LLVMBuildFMul(ctx->builder, num, rcp, "");
719 }
720
721 /* See fast_idiv_by_const.h. */
722 /* Set: increment = util_fast_udiv_info::increment ? multiplier : 0; */
723 LLVMValueRef ac_build_fast_udiv(struct ac_llvm_context *ctx,
724 LLVMValueRef num,
725 LLVMValueRef multiplier,
726 LLVMValueRef pre_shift,
727 LLVMValueRef post_shift,
728 LLVMValueRef increment)
729 {
730 LLVMBuilderRef builder = ctx->builder;
731
732 num = LLVMBuildLShr(builder, num, pre_shift, "");
733 num = LLVMBuildMul(builder,
734 LLVMBuildZExt(builder, num, ctx->i64, ""),
735 LLVMBuildZExt(builder, multiplier, ctx->i64, ""), "");
736 num = LLVMBuildAdd(builder, num,
737 LLVMBuildZExt(builder, increment, ctx->i64, ""), "");
738 num = LLVMBuildLShr(builder, num, LLVMConstInt(ctx->i64, 32, 0), "");
739 num = LLVMBuildTrunc(builder, num, ctx->i32, "");
740 return LLVMBuildLShr(builder, num, post_shift, "");
741 }
742
743 /* See fast_idiv_by_const.h. */
744 /* If num != UINT_MAX, this more efficient version can be used. */
745 /* Set: increment = util_fast_udiv_info::increment; */
746 LLVMValueRef ac_build_fast_udiv_nuw(struct ac_llvm_context *ctx,
747 LLVMValueRef num,
748 LLVMValueRef multiplier,
749 LLVMValueRef pre_shift,
750 LLVMValueRef post_shift,
751 LLVMValueRef increment)
752 {
753 LLVMBuilderRef builder = ctx->builder;
754
755 num = LLVMBuildLShr(builder, num, pre_shift, "");
756 num = LLVMBuildNUWAdd(builder, num, increment, "");
757 num = LLVMBuildMul(builder,
758 LLVMBuildZExt(builder, num, ctx->i64, ""),
759 LLVMBuildZExt(builder, multiplier, ctx->i64, ""), "");
760 num = LLVMBuildLShr(builder, num, LLVMConstInt(ctx->i64, 32, 0), "");
761 num = LLVMBuildTrunc(builder, num, ctx->i32, "");
762 return LLVMBuildLShr(builder, num, post_shift, "");
763 }
764
765 /* See fast_idiv_by_const.h. */
766 /* Both operands must fit in 31 bits and the divisor must not be 1. */
767 LLVMValueRef ac_build_fast_udiv_u31_d_not_one(struct ac_llvm_context *ctx,
768 LLVMValueRef num,
769 LLVMValueRef multiplier,
770 LLVMValueRef post_shift)
771 {
772 LLVMBuilderRef builder = ctx->builder;
773
774 num = LLVMBuildMul(builder,
775 LLVMBuildZExt(builder, num, ctx->i64, ""),
776 LLVMBuildZExt(builder, multiplier, ctx->i64, ""), "");
777 num = LLVMBuildLShr(builder, num, LLVMConstInt(ctx->i64, 32, 0), "");
778 num = LLVMBuildTrunc(builder, num, ctx->i32, "");
779 return LLVMBuildLShr(builder, num, post_shift, "");
780 }
781
782 /* Coordinates for cube map selection. sc, tc, and ma are as in Table 8.27
783 * of the OpenGL 4.5 (Compatibility Profile) specification, except ma is
784 * already multiplied by two. id is the cube face number.
785 */
786 struct cube_selection_coords {
787 LLVMValueRef stc[2];
788 LLVMValueRef ma;
789 LLVMValueRef id;
790 };
791
792 static void
793 build_cube_intrinsic(struct ac_llvm_context *ctx,
794 LLVMValueRef in[3],
795 struct cube_selection_coords *out)
796 {
797 LLVMTypeRef f32 = ctx->f32;
798
799 out->stc[1] = ac_build_intrinsic(ctx, "llvm.amdgcn.cubetc",
800 f32, in, 3, AC_FUNC_ATTR_READNONE);
801 out->stc[0] = ac_build_intrinsic(ctx, "llvm.amdgcn.cubesc",
802 f32, in, 3, AC_FUNC_ATTR_READNONE);
803 out->ma = ac_build_intrinsic(ctx, "llvm.amdgcn.cubema",
804 f32, in, 3, AC_FUNC_ATTR_READNONE);
805 out->id = ac_build_intrinsic(ctx, "llvm.amdgcn.cubeid",
806 f32, in, 3, AC_FUNC_ATTR_READNONE);
807 }
808
809 /**
810 * Build a manual selection sequence for cube face sc/tc coordinates and
811 * major axis vector (multiplied by 2 for consistency) for the given
812 * vec3 \p coords, for the face implied by \p selcoords.
813 *
814 * For the major axis, we always adjust the sign to be in the direction of
815 * selcoords.ma; i.e., a positive out_ma means that coords is pointed towards
816 * the selcoords major axis.
817 */
818 static void build_cube_select(struct ac_llvm_context *ctx,
819 const struct cube_selection_coords *selcoords,
820 const LLVMValueRef *coords,
821 LLVMValueRef *out_st,
822 LLVMValueRef *out_ma)
823 {
824 LLVMBuilderRef builder = ctx->builder;
825 LLVMTypeRef f32 = LLVMTypeOf(coords[0]);
826 LLVMValueRef is_ma_positive;
827 LLVMValueRef sgn_ma;
828 LLVMValueRef is_ma_z, is_not_ma_z;
829 LLVMValueRef is_ma_y;
830 LLVMValueRef is_ma_x;
831 LLVMValueRef sgn;
832 LLVMValueRef tmp;
833
834 is_ma_positive = LLVMBuildFCmp(builder, LLVMRealUGE,
835 selcoords->ma, LLVMConstReal(f32, 0.0), "");
836 sgn_ma = LLVMBuildSelect(builder, is_ma_positive,
837 LLVMConstReal(f32, 1.0), LLVMConstReal(f32, -1.0), "");
838
839 is_ma_z = LLVMBuildFCmp(builder, LLVMRealUGE, selcoords->id, LLVMConstReal(f32, 4.0), "");
840 is_not_ma_z = LLVMBuildNot(builder, is_ma_z, "");
841 is_ma_y = LLVMBuildAnd(builder, is_not_ma_z,
842 LLVMBuildFCmp(builder, LLVMRealUGE, selcoords->id, LLVMConstReal(f32, 2.0), ""), "");
843 is_ma_x = LLVMBuildAnd(builder, is_not_ma_z, LLVMBuildNot(builder, is_ma_y, ""), "");
844
845 /* Select sc */
846 tmp = LLVMBuildSelect(builder, is_ma_x, coords[2], coords[0], "");
847 sgn = LLVMBuildSelect(builder, is_ma_y, LLVMConstReal(f32, 1.0),
848 LLVMBuildSelect(builder, is_ma_z, sgn_ma,
849 LLVMBuildFNeg(builder, sgn_ma, ""), ""), "");
850 out_st[0] = LLVMBuildFMul(builder, tmp, sgn, "");
851
852 /* Select tc */
853 tmp = LLVMBuildSelect(builder, is_ma_y, coords[2], coords[1], "");
854 sgn = LLVMBuildSelect(builder, is_ma_y, sgn_ma,
855 LLVMConstReal(f32, -1.0), "");
856 out_st[1] = LLVMBuildFMul(builder, tmp, sgn, "");
857
858 /* Select ma */
859 tmp = LLVMBuildSelect(builder, is_ma_z, coords[2],
860 LLVMBuildSelect(builder, is_ma_y, coords[1], coords[0], ""), "");
861 tmp = ac_build_intrinsic(ctx, "llvm.fabs.f32",
862 ctx->f32, &tmp, 1, AC_FUNC_ATTR_READNONE);
863 *out_ma = LLVMBuildFMul(builder, tmp, LLVMConstReal(f32, 2.0), "");
864 }
865
866 void
867 ac_prepare_cube_coords(struct ac_llvm_context *ctx,
868 bool is_deriv, bool is_array, bool is_lod,
869 LLVMValueRef *coords_arg,
870 LLVMValueRef *derivs_arg)
871 {
872
873 LLVMBuilderRef builder = ctx->builder;
874 struct cube_selection_coords selcoords;
875 LLVMValueRef coords[3];
876 LLVMValueRef invma;
877
878 if (is_array && !is_lod) {
879 LLVMValueRef tmp = ac_build_round(ctx, coords_arg[3]);
880
881 /* Section 8.9 (Texture Functions) of the GLSL 4.50 spec says:
882 *
883 * "For Array forms, the array layer used will be
884 *
885 * max(0, min(d−1, floor(layer+0.5)))
886 *
887 * where d is the depth of the texture array and layer
888 * comes from the component indicated in the tables below.
889 * Workaroudn for an issue where the layer is taken from a
890 * helper invocation which happens to fall on a different
891 * layer due to extrapolation."
892 *
893 * GFX8 and earlier attempt to implement this in hardware by
894 * clamping the value of coords[2] = (8 * layer) + face.
895 * Unfortunately, this means that the we end up with the wrong
896 * face when clamping occurs.
897 *
898 * Clamp the layer earlier to work around the issue.
899 */
900 if (ctx->chip_class <= GFX8) {
901 LLVMValueRef ge0;
902 ge0 = LLVMBuildFCmp(builder, LLVMRealOGE, tmp, ctx->f32_0, "");
903 tmp = LLVMBuildSelect(builder, ge0, tmp, ctx->f32_0, "");
904 }
905
906 coords_arg[3] = tmp;
907 }
908
909 build_cube_intrinsic(ctx, coords_arg, &selcoords);
910
911 invma = ac_build_intrinsic(ctx, "llvm.fabs.f32",
912 ctx->f32, &selcoords.ma, 1, AC_FUNC_ATTR_READNONE);
913 invma = ac_build_fdiv(ctx, LLVMConstReal(ctx->f32, 1.0), invma);
914
915 for (int i = 0; i < 2; ++i)
916 coords[i] = LLVMBuildFMul(builder, selcoords.stc[i], invma, "");
917
918 coords[2] = selcoords.id;
919
920 if (is_deriv && derivs_arg) {
921 LLVMValueRef derivs[4];
922 int axis;
923
924 /* Convert cube derivatives to 2D derivatives. */
925 for (axis = 0; axis < 2; axis++) {
926 LLVMValueRef deriv_st[2];
927 LLVMValueRef deriv_ma;
928
929 /* Transform the derivative alongside the texture
930 * coordinate. Mathematically, the correct formula is
931 * as follows. Assume we're projecting onto the +Z face
932 * and denote by dx/dh the derivative of the (original)
933 * X texture coordinate with respect to horizontal
934 * window coordinates. The projection onto the +Z face
935 * plane is:
936 *
937 * f(x,z) = x/z
938 *
939 * Then df/dh = df/dx * dx/dh + df/dz * dz/dh
940 * = 1/z * dx/dh - x/z * 1/z * dz/dh.
941 *
942 * This motivatives the implementation below.
943 *
944 * Whether this actually gives the expected results for
945 * apps that might feed in derivatives obtained via
946 * finite differences is anyone's guess. The OpenGL spec
947 * seems awfully quiet about how textureGrad for cube
948 * maps should be handled.
949 */
950 build_cube_select(ctx, &selcoords, &derivs_arg[axis * 3],
951 deriv_st, &deriv_ma);
952
953 deriv_ma = LLVMBuildFMul(builder, deriv_ma, invma, "");
954
955 for (int i = 0; i < 2; ++i)
956 derivs[axis * 2 + i] =
957 LLVMBuildFSub(builder,
958 LLVMBuildFMul(builder, deriv_st[i], invma, ""),
959 LLVMBuildFMul(builder, deriv_ma, coords[i], ""), "");
960 }
961
962 memcpy(derivs_arg, derivs, sizeof(derivs));
963 }
964
965 /* Shift the texture coordinate. This must be applied after the
966 * derivative calculation.
967 */
968 for (int i = 0; i < 2; ++i)
969 coords[i] = LLVMBuildFAdd(builder, coords[i], LLVMConstReal(ctx->f32, 1.5), "");
970
971 if (is_array) {
972 /* for cube arrays coord.z = coord.w(array_index) * 8 + face */
973 /* coords_arg.w component - array_index for cube arrays */
974 coords[2] = ac_build_fmad(ctx, coords_arg[3], LLVMConstReal(ctx->f32, 8.0), coords[2]);
975 }
976
977 memcpy(coords_arg, coords, sizeof(coords));
978 }
979
980
981 LLVMValueRef
982 ac_build_fs_interp(struct ac_llvm_context *ctx,
983 LLVMValueRef llvm_chan,
984 LLVMValueRef attr_number,
985 LLVMValueRef params,
986 LLVMValueRef i,
987 LLVMValueRef j)
988 {
989 LLVMValueRef args[5];
990 LLVMValueRef p1;
991
992 args[0] = i;
993 args[1] = llvm_chan;
994 args[2] = attr_number;
995 args[3] = params;
996
997 p1 = ac_build_intrinsic(ctx, "llvm.amdgcn.interp.p1",
998 ctx->f32, args, 4, AC_FUNC_ATTR_READNONE);
999
1000 args[0] = p1;
1001 args[1] = j;
1002 args[2] = llvm_chan;
1003 args[3] = attr_number;
1004 args[4] = params;
1005
1006 return ac_build_intrinsic(ctx, "llvm.amdgcn.interp.p2",
1007 ctx->f32, args, 5, AC_FUNC_ATTR_READNONE);
1008 }
1009
1010 LLVMValueRef
1011 ac_build_fs_interp_f16(struct ac_llvm_context *ctx,
1012 LLVMValueRef llvm_chan,
1013 LLVMValueRef attr_number,
1014 LLVMValueRef params,
1015 LLVMValueRef i,
1016 LLVMValueRef j)
1017 {
1018 LLVMValueRef args[6];
1019 LLVMValueRef p1;
1020
1021 args[0] = i;
1022 args[1] = llvm_chan;
1023 args[2] = attr_number;
1024 args[3] = ctx->i1false;
1025 args[4] = params;
1026
1027 p1 = ac_build_intrinsic(ctx, "llvm.amdgcn.interp.p1.f16",
1028 ctx->f32, args, 5, AC_FUNC_ATTR_READNONE);
1029
1030 args[0] = p1;
1031 args[1] = j;
1032 args[2] = llvm_chan;
1033 args[3] = attr_number;
1034 args[4] = ctx->i1false;
1035 args[5] = params;
1036
1037 return ac_build_intrinsic(ctx, "llvm.amdgcn.interp.p2.f16",
1038 ctx->f16, args, 6, AC_FUNC_ATTR_READNONE);
1039 }
1040
1041 LLVMValueRef
1042 ac_build_fs_interp_mov(struct ac_llvm_context *ctx,
1043 LLVMValueRef parameter,
1044 LLVMValueRef llvm_chan,
1045 LLVMValueRef attr_number,
1046 LLVMValueRef params)
1047 {
1048 LLVMValueRef args[4];
1049
1050 args[0] = parameter;
1051 args[1] = llvm_chan;
1052 args[2] = attr_number;
1053 args[3] = params;
1054
1055 return ac_build_intrinsic(ctx, "llvm.amdgcn.interp.mov",
1056 ctx->f32, args, 4, AC_FUNC_ATTR_READNONE);
1057 }
1058
1059 LLVMValueRef
1060 ac_build_gep_ptr(struct ac_llvm_context *ctx,
1061 LLVMValueRef base_ptr,
1062 LLVMValueRef index)
1063 {
1064 return LLVMBuildGEP(ctx->builder, base_ptr, &index, 1, "");
1065 }
1066
1067 LLVMValueRef
1068 ac_build_gep0(struct ac_llvm_context *ctx,
1069 LLVMValueRef base_ptr,
1070 LLVMValueRef index)
1071 {
1072 LLVMValueRef indices[2] = {
1073 ctx->i32_0,
1074 index,
1075 };
1076 return LLVMBuildGEP(ctx->builder, base_ptr, indices, 2, "");
1077 }
1078
1079 LLVMValueRef ac_build_pointer_add(struct ac_llvm_context *ctx, LLVMValueRef ptr,
1080 LLVMValueRef index)
1081 {
1082 return LLVMBuildPointerCast(ctx->builder,
1083 LLVMBuildGEP(ctx->builder, ptr, &index, 1, ""),
1084 LLVMTypeOf(ptr), "");
1085 }
1086
1087 void
1088 ac_build_indexed_store(struct ac_llvm_context *ctx,
1089 LLVMValueRef base_ptr, LLVMValueRef index,
1090 LLVMValueRef value)
1091 {
1092 LLVMBuildStore(ctx->builder, value,
1093 ac_build_gep0(ctx, base_ptr, index));
1094 }
1095
1096 /**
1097 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad.
1098 * It's equivalent to doing a load from &base_ptr[index].
1099 *
1100 * \param base_ptr Where the array starts.
1101 * \param index The element index into the array.
1102 * \param uniform Whether the base_ptr and index can be assumed to be
1103 * dynamically uniform (i.e. load to an SGPR)
1104 * \param invariant Whether the load is invariant (no other opcodes affect it)
1105 * \param no_unsigned_wraparound
1106 * For all possible re-associations and re-distributions of an expression
1107 * "base_ptr + index * elemsize" into "addr + offset" (excluding GEPs
1108 * without inbounds in base_ptr), this parameter is true if "addr + offset"
1109 * does not result in an unsigned integer wraparound. This is used for
1110 * optimal code generation of 32-bit pointer arithmetic.
1111 *
1112 * For example, a 32-bit immediate offset that causes a 32-bit unsigned
1113 * integer wraparound can't be an imm offset in s_load_dword, because
1114 * the instruction performs "addr + offset" in 64 bits.
1115 *
1116 * Expected usage for bindless textures by chaining GEPs:
1117 * // possible unsigned wraparound, don't use InBounds:
1118 * ptr1 = LLVMBuildGEP(base_ptr, index);
1119 * image = load(ptr1); // becomes "s_load ptr1, 0"
1120 *
1121 * ptr2 = LLVMBuildInBoundsGEP(ptr1, 32 / elemsize);
1122 * sampler = load(ptr2); // becomes "s_load ptr1, 32" thanks to InBounds
1123 */
1124 static LLVMValueRef
1125 ac_build_load_custom(struct ac_llvm_context *ctx, LLVMValueRef base_ptr,
1126 LLVMValueRef index, bool uniform, bool invariant,
1127 bool no_unsigned_wraparound)
1128 {
1129 LLVMValueRef pointer, result;
1130
1131 if (no_unsigned_wraparound &&
1132 LLVMGetPointerAddressSpace(LLVMTypeOf(base_ptr)) == AC_ADDR_SPACE_CONST_32BIT)
1133 pointer = LLVMBuildInBoundsGEP(ctx->builder, base_ptr, &index, 1, "");
1134 else
1135 pointer = LLVMBuildGEP(ctx->builder, base_ptr, &index, 1, "");
1136
1137 if (uniform)
1138 LLVMSetMetadata(pointer, ctx->uniform_md_kind, ctx->empty_md);
1139 result = LLVMBuildLoad(ctx->builder, pointer, "");
1140 if (invariant)
1141 LLVMSetMetadata(result, ctx->invariant_load_md_kind, ctx->empty_md);
1142 return result;
1143 }
1144
1145 LLVMValueRef ac_build_load(struct ac_llvm_context *ctx, LLVMValueRef base_ptr,
1146 LLVMValueRef index)
1147 {
1148 return ac_build_load_custom(ctx, base_ptr, index, false, false, false);
1149 }
1150
1151 LLVMValueRef ac_build_load_invariant(struct ac_llvm_context *ctx,
1152 LLVMValueRef base_ptr, LLVMValueRef index)
1153 {
1154 return ac_build_load_custom(ctx, base_ptr, index, false, true, false);
1155 }
1156
1157 /* This assumes that there is no unsigned integer wraparound during the address
1158 * computation, excluding all GEPs within base_ptr. */
1159 LLVMValueRef ac_build_load_to_sgpr(struct ac_llvm_context *ctx,
1160 LLVMValueRef base_ptr, LLVMValueRef index)
1161 {
1162 return ac_build_load_custom(ctx, base_ptr, index, true, true, true);
1163 }
1164
1165 /* See ac_build_load_custom() documentation. */
1166 LLVMValueRef ac_build_load_to_sgpr_uint_wraparound(struct ac_llvm_context *ctx,
1167 LLVMValueRef base_ptr, LLVMValueRef index)
1168 {
1169 return ac_build_load_custom(ctx, base_ptr, index, true, true, false);
1170 }
1171
1172 static unsigned get_load_cache_policy(struct ac_llvm_context *ctx,
1173 unsigned cache_policy)
1174 {
1175 return cache_policy |
1176 (ctx->chip_class >= GFX10 && cache_policy & ac_glc ? ac_dlc : 0);
1177 }
1178
1179 static void
1180 ac_build_buffer_store_common(struct ac_llvm_context *ctx,
1181 LLVMValueRef rsrc,
1182 LLVMValueRef data,
1183 LLVMValueRef vindex,
1184 LLVMValueRef voffset,
1185 LLVMValueRef soffset,
1186 unsigned cache_policy,
1187 bool use_format,
1188 bool structurized)
1189 {
1190 LLVMValueRef args[6];
1191 int idx = 0;
1192 args[idx++] = data;
1193 args[idx++] = LLVMBuildBitCast(ctx->builder, rsrc, ctx->v4i32, "");
1194 if (structurized)
1195 args[idx++] = vindex ? vindex : ctx->i32_0;
1196 args[idx++] = voffset ? voffset : ctx->i32_0;
1197 args[idx++] = soffset ? soffset : ctx->i32_0;
1198 args[idx++] = LLVMConstInt(ctx->i32, cache_policy, 0);
1199 const char *indexing_kind = structurized ? "struct" : "raw";
1200 char name[256], type_name[8];
1201
1202 ac_build_type_name_for_intr(LLVMTypeOf(data), type_name, sizeof(type_name));
1203
1204 if (use_format) {
1205 snprintf(name, sizeof(name), "llvm.amdgcn.%s.buffer.store.format.%s",
1206 indexing_kind, type_name);
1207 } else {
1208 snprintf(name, sizeof(name), "llvm.amdgcn.%s.buffer.store.%s",
1209 indexing_kind, type_name);
1210 }
1211
1212 ac_build_intrinsic(ctx, name, ctx->voidt, args, idx,
1213 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY);
1214 }
1215
1216 void
1217 ac_build_buffer_store_format(struct ac_llvm_context *ctx,
1218 LLVMValueRef rsrc,
1219 LLVMValueRef data,
1220 LLVMValueRef vindex,
1221 LLVMValueRef voffset,
1222 unsigned cache_policy)
1223 {
1224 ac_build_buffer_store_common(ctx, rsrc, data, vindex, voffset, NULL,
1225 cache_policy, true, true);
1226 }
1227
1228 /* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
1229 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
1230 * or v4i32 (num_channels=3,4).
1231 */
1232 void
1233 ac_build_buffer_store_dword(struct ac_llvm_context *ctx,
1234 LLVMValueRef rsrc,
1235 LLVMValueRef vdata,
1236 unsigned num_channels,
1237 LLVMValueRef voffset,
1238 LLVMValueRef soffset,
1239 unsigned inst_offset,
1240 unsigned cache_policy)
1241 {
1242 /* Split 3 channel stores, because only LLVM 9+ support 3-channel
1243 * intrinsics. */
1244 if (num_channels == 3 && !ac_has_vec3_support(ctx->chip_class, false)) {
1245 LLVMValueRef v[3], v01;
1246
1247 for (int i = 0; i < 3; i++) {
1248 v[i] = LLVMBuildExtractElement(ctx->builder, vdata,
1249 LLVMConstInt(ctx->i32, i, 0), "");
1250 }
1251 v01 = ac_build_gather_values(ctx, v, 2);
1252
1253 ac_build_buffer_store_dword(ctx, rsrc, v01, 2, voffset,
1254 soffset, inst_offset, cache_policy);
1255 ac_build_buffer_store_dword(ctx, rsrc, v[2], 1, voffset,
1256 soffset, inst_offset + 8,
1257 cache_policy);
1258 return;
1259 }
1260
1261 /* SWIZZLE_ENABLE requires that soffset isn't folded into voffset
1262 * (voffset is swizzled, but soffset isn't swizzled).
1263 * llvm.amdgcn.buffer.store doesn't have a separate soffset parameter.
1264 */
1265 if (!(cache_policy & ac_swizzled)) {
1266 LLVMValueRef offset = soffset;
1267
1268 if (inst_offset)
1269 offset = LLVMBuildAdd(ctx->builder, offset,
1270 LLVMConstInt(ctx->i32, inst_offset, 0), "");
1271
1272 ac_build_buffer_store_common(ctx, rsrc, ac_to_float(ctx, vdata),
1273 ctx->i32_0, voffset, offset,
1274 cache_policy, false, false);
1275 return;
1276 }
1277
1278 static const unsigned dfmts[] = {
1279 V_008F0C_BUF_DATA_FORMAT_32,
1280 V_008F0C_BUF_DATA_FORMAT_32_32,
1281 V_008F0C_BUF_DATA_FORMAT_32_32_32,
1282 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
1283 };
1284 unsigned dfmt = dfmts[num_channels - 1];
1285 unsigned nfmt = V_008F0C_BUF_NUM_FORMAT_UINT;
1286 LLVMValueRef immoffset = LLVMConstInt(ctx->i32, inst_offset, 0);
1287
1288 ac_build_raw_tbuffer_store(ctx, rsrc, vdata, voffset, soffset,
1289 immoffset, num_channels, dfmt, nfmt, cache_policy);
1290 }
1291
1292 static LLVMValueRef
1293 ac_build_buffer_load_common(struct ac_llvm_context *ctx,
1294 LLVMValueRef rsrc,
1295 LLVMValueRef vindex,
1296 LLVMValueRef voffset,
1297 LLVMValueRef soffset,
1298 unsigned num_channels,
1299 LLVMTypeRef channel_type,
1300 unsigned cache_policy,
1301 bool can_speculate,
1302 bool use_format,
1303 bool structurized)
1304 {
1305 LLVMValueRef args[5];
1306 int idx = 0;
1307 args[idx++] = LLVMBuildBitCast(ctx->builder, rsrc, ctx->v4i32, "");
1308 if (structurized)
1309 args[idx++] = vindex ? vindex : ctx->i32_0;
1310 args[idx++] = voffset ? voffset : ctx->i32_0;
1311 args[idx++] = soffset ? soffset : ctx->i32_0;
1312 args[idx++] = LLVMConstInt(ctx->i32, get_load_cache_policy(ctx, cache_policy), 0);
1313 unsigned func = !ac_has_vec3_support(ctx->chip_class, use_format) && num_channels == 3 ? 4 : num_channels;
1314 const char *indexing_kind = structurized ? "struct" : "raw";
1315 char name[256], type_name[8];
1316
1317 LLVMTypeRef type = func > 1 ? LLVMVectorType(channel_type, func) : channel_type;
1318 ac_build_type_name_for_intr(type, type_name, sizeof(type_name));
1319
1320 if (use_format) {
1321 snprintf(name, sizeof(name), "llvm.amdgcn.%s.buffer.load.format.%s",
1322 indexing_kind, type_name);
1323 } else {
1324 snprintf(name, sizeof(name), "llvm.amdgcn.%s.buffer.load.%s",
1325 indexing_kind, type_name);
1326 }
1327
1328 return ac_build_intrinsic(ctx, name, type, args, idx,
1329 ac_get_load_intr_attribs(can_speculate));
1330 }
1331
1332 LLVMValueRef
1333 ac_build_buffer_load(struct ac_llvm_context *ctx,
1334 LLVMValueRef rsrc,
1335 int num_channels,
1336 LLVMValueRef vindex,
1337 LLVMValueRef voffset,
1338 LLVMValueRef soffset,
1339 unsigned inst_offset,
1340 unsigned cache_policy,
1341 bool can_speculate,
1342 bool allow_smem)
1343 {
1344 LLVMValueRef offset = LLVMConstInt(ctx->i32, inst_offset, 0);
1345 if (voffset)
1346 offset = LLVMBuildAdd(ctx->builder, offset, voffset, "");
1347 if (soffset)
1348 offset = LLVMBuildAdd(ctx->builder, offset, soffset, "");
1349
1350 if (allow_smem && !(cache_policy & ac_slc) &&
1351 (!(cache_policy & ac_glc) || ctx->chip_class >= GFX8)) {
1352 assert(vindex == NULL);
1353
1354 LLVMValueRef result[8];
1355
1356 for (int i = 0; i < num_channels; i++) {
1357 if (i) {
1358 offset = LLVMBuildAdd(ctx->builder, offset,
1359 LLVMConstInt(ctx->i32, 4, 0), "");
1360 }
1361 LLVMValueRef args[3] = {
1362 rsrc,
1363 offset,
1364 LLVMConstInt(ctx->i32, get_load_cache_policy(ctx, cache_policy), 0),
1365 };
1366 result[i] = ac_build_intrinsic(ctx,
1367 "llvm.amdgcn.s.buffer.load.f32",
1368 ctx->f32, args, 3,
1369 AC_FUNC_ATTR_READNONE);
1370 }
1371 if (num_channels == 1)
1372 return result[0];
1373
1374 if (num_channels == 3 && !ac_has_vec3_support(ctx->chip_class, false))
1375 result[num_channels++] = LLVMGetUndef(ctx->f32);
1376 return ac_build_gather_values(ctx, result, num_channels);
1377 }
1378
1379 return ac_build_buffer_load_common(ctx, rsrc, vindex,
1380 offset, ctx->i32_0,
1381 num_channels, ctx->f32,
1382 cache_policy,
1383 can_speculate, false, false);
1384 }
1385
1386 LLVMValueRef ac_build_buffer_load_format(struct ac_llvm_context *ctx,
1387 LLVMValueRef rsrc,
1388 LLVMValueRef vindex,
1389 LLVMValueRef voffset,
1390 unsigned num_channels,
1391 unsigned cache_policy,
1392 bool can_speculate)
1393 {
1394 return ac_build_buffer_load_common(ctx, rsrc, vindex, voffset,
1395 ctx->i32_0, num_channels, ctx->f32,
1396 cache_policy, can_speculate,
1397 true, true);
1398 }
1399
1400 static LLVMValueRef
1401 ac_build_tbuffer_load(struct ac_llvm_context *ctx,
1402 LLVMValueRef rsrc,
1403 LLVMValueRef vindex,
1404 LLVMValueRef voffset,
1405 LLVMValueRef soffset,
1406 LLVMValueRef immoffset,
1407 unsigned num_channels,
1408 unsigned dfmt,
1409 unsigned nfmt,
1410 unsigned cache_policy,
1411 bool can_speculate,
1412 bool structurized)
1413 {
1414 voffset = LLVMBuildAdd(ctx->builder, voffset, immoffset, "");
1415
1416 LLVMValueRef args[6];
1417 int idx = 0;
1418 args[idx++] = LLVMBuildBitCast(ctx->builder, rsrc, ctx->v4i32, "");
1419 if (structurized)
1420 args[idx++] = vindex ? vindex : ctx->i32_0;
1421 args[idx++] = voffset ? voffset : ctx->i32_0;
1422 args[idx++] = soffset ? soffset : ctx->i32_0;
1423 args[idx++] = LLVMConstInt(ctx->i32, ac_get_tbuffer_format(ctx->chip_class, dfmt, nfmt), 0);
1424 args[idx++] = LLVMConstInt(ctx->i32, get_load_cache_policy(ctx, cache_policy), 0);
1425 unsigned func = !ac_has_vec3_support(ctx->chip_class, true) && num_channels == 3 ? 4 : num_channels;
1426 const char *indexing_kind = structurized ? "struct" : "raw";
1427 char name[256], type_name[8];
1428
1429 LLVMTypeRef type = func > 1 ? LLVMVectorType(ctx->i32, func) : ctx->i32;
1430 ac_build_type_name_for_intr(type, type_name, sizeof(type_name));
1431
1432 snprintf(name, sizeof(name), "llvm.amdgcn.%s.tbuffer.load.%s",
1433 indexing_kind, type_name);
1434
1435 return ac_build_intrinsic(ctx, name, type, args, idx,
1436 ac_get_load_intr_attribs(can_speculate));
1437 }
1438
1439 LLVMValueRef
1440 ac_build_struct_tbuffer_load(struct ac_llvm_context *ctx,
1441 LLVMValueRef rsrc,
1442 LLVMValueRef vindex,
1443 LLVMValueRef voffset,
1444 LLVMValueRef soffset,
1445 LLVMValueRef immoffset,
1446 unsigned num_channels,
1447 unsigned dfmt,
1448 unsigned nfmt,
1449 unsigned cache_policy,
1450 bool can_speculate)
1451 {
1452 return ac_build_tbuffer_load(ctx, rsrc, vindex, voffset, soffset,
1453 immoffset, num_channels, dfmt, nfmt,
1454 cache_policy, can_speculate, true);
1455 }
1456
1457 LLVMValueRef
1458 ac_build_raw_tbuffer_load(struct ac_llvm_context *ctx,
1459 LLVMValueRef rsrc,
1460 LLVMValueRef voffset,
1461 LLVMValueRef soffset,
1462 LLVMValueRef immoffset,
1463 unsigned num_channels,
1464 unsigned dfmt,
1465 unsigned nfmt,
1466 unsigned cache_policy,
1467 bool can_speculate)
1468 {
1469 return ac_build_tbuffer_load(ctx, rsrc, NULL, voffset, soffset,
1470 immoffset, num_channels, dfmt, nfmt,
1471 cache_policy, can_speculate, false);
1472 }
1473
1474 LLVMValueRef
1475 ac_build_tbuffer_load_short(struct ac_llvm_context *ctx,
1476 LLVMValueRef rsrc,
1477 LLVMValueRef voffset,
1478 LLVMValueRef soffset,
1479 LLVMValueRef immoffset,
1480 unsigned cache_policy)
1481 {
1482 LLVMValueRef res;
1483
1484 if (LLVM_VERSION_MAJOR >= 9) {
1485 voffset = LLVMBuildAdd(ctx->builder, voffset, immoffset, "");
1486
1487 /* LLVM 9+ supports i8/i16 with struct/raw intrinsics. */
1488 res = ac_build_buffer_load_common(ctx, rsrc, NULL,
1489 voffset, soffset,
1490 1, ctx->i16, cache_policy,
1491 false, false, false);
1492 } else {
1493 unsigned dfmt = V_008F0C_BUF_DATA_FORMAT_16;
1494 unsigned nfmt = V_008F0C_BUF_NUM_FORMAT_UINT;
1495
1496 res = ac_build_raw_tbuffer_load(ctx, rsrc, voffset, soffset,
1497 immoffset, 1, dfmt, nfmt, cache_policy,
1498 false);
1499
1500 res = LLVMBuildTrunc(ctx->builder, res, ctx->i16, "");
1501 }
1502
1503 return res;
1504 }
1505
1506 LLVMValueRef
1507 ac_build_tbuffer_load_byte(struct ac_llvm_context *ctx,
1508 LLVMValueRef rsrc,
1509 LLVMValueRef voffset,
1510 LLVMValueRef soffset,
1511 LLVMValueRef immoffset,
1512 unsigned cache_policy)
1513 {
1514 LLVMValueRef res;
1515
1516 if (LLVM_VERSION_MAJOR >= 9) {
1517 voffset = LLVMBuildAdd(ctx->builder, voffset, immoffset, "");
1518
1519 /* LLVM 9+ supports i8/i16 with struct/raw intrinsics. */
1520 res = ac_build_buffer_load_common(ctx, rsrc, NULL,
1521 voffset, soffset,
1522 1, ctx->i8, cache_policy,
1523 false, false, false);
1524 } else {
1525 unsigned dfmt = V_008F0C_BUF_DATA_FORMAT_8;
1526 unsigned nfmt = V_008F0C_BUF_NUM_FORMAT_UINT;
1527
1528 res = ac_build_raw_tbuffer_load(ctx, rsrc, voffset, soffset,
1529 immoffset, 1, dfmt, nfmt, cache_policy,
1530 false);
1531
1532 res = LLVMBuildTrunc(ctx->builder, res, ctx->i8, "");
1533 }
1534
1535 return res;
1536 }
1537
1538 /**
1539 * Convert an 11- or 10-bit unsigned floating point number to an f32.
1540 *
1541 * The input exponent is expected to be biased analogous to IEEE-754, i.e. by
1542 * 2^(exp_bits-1) - 1 (as defined in OpenGL and other graphics APIs).
1543 */
1544 static LLVMValueRef
1545 ac_ufN_to_float(struct ac_llvm_context *ctx, LLVMValueRef src, unsigned exp_bits, unsigned mant_bits)
1546 {
1547 assert(LLVMTypeOf(src) == ctx->i32);
1548
1549 LLVMValueRef tmp;
1550 LLVMValueRef mantissa;
1551 mantissa = LLVMBuildAnd(ctx->builder, src, LLVMConstInt(ctx->i32, (1 << mant_bits) - 1, false), "");
1552
1553 /* Converting normal numbers is just a shift + correcting the exponent bias */
1554 unsigned normal_shift = 23 - mant_bits;
1555 unsigned bias_shift = 127 - ((1 << (exp_bits - 1)) - 1);
1556 LLVMValueRef shifted, normal;
1557
1558 shifted = LLVMBuildShl(ctx->builder, src, LLVMConstInt(ctx->i32, normal_shift, false), "");
1559 normal = LLVMBuildAdd(ctx->builder, shifted, LLVMConstInt(ctx->i32, bias_shift << 23, false), "");
1560
1561 /* Converting nan/inf numbers is the same, but with a different exponent update */
1562 LLVMValueRef naninf;
1563 naninf = LLVMBuildOr(ctx->builder, normal, LLVMConstInt(ctx->i32, 0xff << 23, false), "");
1564
1565 /* Converting denormals is the complex case: determine the leading zeros of the
1566 * mantissa to obtain the correct shift for the mantissa and exponent correction.
1567 */
1568 LLVMValueRef denormal;
1569 LLVMValueRef params[2] = {
1570 mantissa,
1571 ctx->i1true, /* result can be undef when arg is 0 */
1572 };
1573 LLVMValueRef ctlz = ac_build_intrinsic(ctx, "llvm.ctlz.i32", ctx->i32,
1574 params, 2, AC_FUNC_ATTR_READNONE);
1575
1576 /* Shift such that the leading 1 ends up as the LSB of the exponent field. */
1577 tmp = LLVMBuildSub(ctx->builder, ctlz, LLVMConstInt(ctx->i32, 8, false), "");
1578 denormal = LLVMBuildShl(ctx->builder, mantissa, tmp, "");
1579
1580 unsigned denormal_exp = bias_shift + (32 - mant_bits) - 1;
1581 tmp = LLVMBuildSub(ctx->builder, LLVMConstInt(ctx->i32, denormal_exp, false), ctlz, "");
1582 tmp = LLVMBuildShl(ctx->builder, tmp, LLVMConstInt(ctx->i32, 23, false), "");
1583 denormal = LLVMBuildAdd(ctx->builder, denormal, tmp, "");
1584
1585 /* Select the final result. */
1586 LLVMValueRef result;
1587
1588 tmp = LLVMBuildICmp(ctx->builder, LLVMIntUGE, src,
1589 LLVMConstInt(ctx->i32, ((1 << exp_bits) - 1) << mant_bits, false), "");
1590 result = LLVMBuildSelect(ctx->builder, tmp, naninf, normal, "");
1591
1592 tmp = LLVMBuildICmp(ctx->builder, LLVMIntUGE, src,
1593 LLVMConstInt(ctx->i32, 1 << mant_bits, false), "");
1594 result = LLVMBuildSelect(ctx->builder, tmp, result, denormal, "");
1595
1596 tmp = LLVMBuildICmp(ctx->builder, LLVMIntNE, src, ctx->i32_0, "");
1597 result = LLVMBuildSelect(ctx->builder, tmp, result, ctx->i32_0, "");
1598
1599 return ac_to_float(ctx, result);
1600 }
1601
1602 /**
1603 * Generate a fully general open coded buffer format fetch with all required
1604 * fixups suitable for vertex fetch, using non-format buffer loads.
1605 *
1606 * Some combinations of argument values have special interpretations:
1607 * - size = 8 bytes, format = fixed indicates PIPE_FORMAT_R11G11B10_FLOAT
1608 * - size = 8 bytes, format != {float,fixed} indicates a 2_10_10_10 data format
1609 *
1610 * \param log_size log(size of channel in bytes)
1611 * \param num_channels number of channels (1 to 4)
1612 * \param format AC_FETCH_FORMAT_xxx value
1613 * \param reverse whether XYZ channels are reversed
1614 * \param known_aligned whether the source is known to be aligned to hardware's
1615 * effective element size for loading the given format
1616 * (note: this means dword alignment for 8_8_8_8, 16_16, etc.)
1617 * \param rsrc buffer resource descriptor
1618 * \return the resulting vector of floats or integers bitcast to <4 x i32>
1619 */
1620 LLVMValueRef
1621 ac_build_opencoded_load_format(struct ac_llvm_context *ctx,
1622 unsigned log_size,
1623 unsigned num_channels,
1624 unsigned format,
1625 bool reverse,
1626 bool known_aligned,
1627 LLVMValueRef rsrc,
1628 LLVMValueRef vindex,
1629 LLVMValueRef voffset,
1630 LLVMValueRef soffset,
1631 unsigned cache_policy,
1632 bool can_speculate)
1633 {
1634 LLVMValueRef tmp;
1635 unsigned load_log_size = log_size;
1636 unsigned load_num_channels = num_channels;
1637 if (log_size == 3) {
1638 load_log_size = 2;
1639 if (format == AC_FETCH_FORMAT_FLOAT) {
1640 load_num_channels = 2 * num_channels;
1641 } else {
1642 load_num_channels = 1; /* 10_11_11 or 2_10_10_10 */
1643 }
1644 }
1645
1646 int log_recombine = 0;
1647 if (ctx->chip_class == GFX6 && !known_aligned) {
1648 /* Avoid alignment restrictions by loading one byte at a time. */
1649 load_num_channels <<= load_log_size;
1650 log_recombine = load_log_size;
1651 load_log_size = 0;
1652 } else if (load_num_channels == 2 || load_num_channels == 4) {
1653 log_recombine = -util_logbase2(load_num_channels);
1654 load_num_channels = 1;
1655 load_log_size += -log_recombine;
1656 }
1657
1658 assert(load_log_size >= 2 || LLVM_VERSION_MAJOR >= 9);
1659
1660 LLVMValueRef loads[32]; /* up to 32 bytes */
1661 for (unsigned i = 0; i < load_num_channels; ++i) {
1662 tmp = LLVMBuildAdd(ctx->builder, soffset,
1663 LLVMConstInt(ctx->i32, i << load_log_size, false), "");
1664 LLVMTypeRef channel_type = load_log_size == 0 ? ctx->i8 :
1665 load_log_size == 1 ? ctx->i16 : ctx->i32;
1666 unsigned num_channels = 1 << (MAX2(load_log_size, 2) - 2);
1667 loads[i] = ac_build_buffer_load_common(
1668 ctx, rsrc, vindex, voffset, tmp,
1669 num_channels, channel_type, cache_policy,
1670 can_speculate, false, true);
1671 if (load_log_size >= 2)
1672 loads[i] = ac_to_integer(ctx, loads[i]);
1673 }
1674
1675 if (log_recombine > 0) {
1676 /* Recombine bytes if necessary (GFX6 only) */
1677 LLVMTypeRef dst_type = log_recombine == 2 ? ctx->i32 : ctx->i16;
1678
1679 for (unsigned src = 0, dst = 0; src < load_num_channels; ++dst) {
1680 LLVMValueRef accum = NULL;
1681 for (unsigned i = 0; i < (1 << log_recombine); ++i, ++src) {
1682 tmp = LLVMBuildZExt(ctx->builder, loads[src], dst_type, "");
1683 if (i == 0) {
1684 accum = tmp;
1685 } else {
1686 tmp = LLVMBuildShl(ctx->builder, tmp,
1687 LLVMConstInt(dst_type, 8 * i, false), "");
1688 accum = LLVMBuildOr(ctx->builder, accum, tmp, "");
1689 }
1690 }
1691 loads[dst] = accum;
1692 }
1693 } else if (log_recombine < 0) {
1694 /* Split vectors of dwords */
1695 if (load_log_size > 2) {
1696 assert(load_num_channels == 1);
1697 LLVMValueRef loaded = loads[0];
1698 unsigned log_split = load_log_size - 2;
1699 log_recombine += log_split;
1700 load_num_channels = 1 << log_split;
1701 load_log_size = 2;
1702 for (unsigned i = 0; i < load_num_channels; ++i) {
1703 tmp = LLVMConstInt(ctx->i32, i, false);
1704 loads[i] = LLVMBuildExtractElement(ctx->builder, loaded, tmp, "");
1705 }
1706 }
1707
1708 /* Further split dwords and shorts if required */
1709 if (log_recombine < 0) {
1710 for (unsigned src = load_num_channels,
1711 dst = load_num_channels << -log_recombine;
1712 src > 0; --src) {
1713 unsigned dst_bits = 1 << (3 + load_log_size + log_recombine);
1714 LLVMTypeRef dst_type = LLVMIntTypeInContext(ctx->context, dst_bits);
1715 LLVMValueRef loaded = loads[src - 1];
1716 LLVMTypeRef loaded_type = LLVMTypeOf(loaded);
1717 for (unsigned i = 1 << -log_recombine; i > 0; --i, --dst) {
1718 tmp = LLVMConstInt(loaded_type, dst_bits * (i - 1), false);
1719 tmp = LLVMBuildLShr(ctx->builder, loaded, tmp, "");
1720 loads[dst - 1] = LLVMBuildTrunc(ctx->builder, tmp, dst_type, "");
1721 }
1722 }
1723 }
1724 }
1725
1726 if (log_size == 3) {
1727 if (format == AC_FETCH_FORMAT_FLOAT) {
1728 for (unsigned i = 0; i < num_channels; ++i) {
1729 tmp = ac_build_gather_values(ctx, &loads[2 * i], 2);
1730 loads[i] = LLVMBuildBitCast(ctx->builder, tmp, ctx->f64, "");
1731 }
1732 } else if (format == AC_FETCH_FORMAT_FIXED) {
1733 /* 10_11_11_FLOAT */
1734 LLVMValueRef data = loads[0];
1735 LLVMValueRef i32_2047 = LLVMConstInt(ctx->i32, 2047, false);
1736 LLVMValueRef r = LLVMBuildAnd(ctx->builder, data, i32_2047, "");
1737 tmp = LLVMBuildLShr(ctx->builder, data, LLVMConstInt(ctx->i32, 11, false), "");
1738 LLVMValueRef g = LLVMBuildAnd(ctx->builder, tmp, i32_2047, "");
1739 LLVMValueRef b = LLVMBuildLShr(ctx->builder, data, LLVMConstInt(ctx->i32, 22, false), "");
1740
1741 loads[0] = ac_to_integer(ctx, ac_ufN_to_float(ctx, r, 5, 6));
1742 loads[1] = ac_to_integer(ctx, ac_ufN_to_float(ctx, g, 5, 6));
1743 loads[2] = ac_to_integer(ctx, ac_ufN_to_float(ctx, b, 5, 5));
1744
1745 num_channels = 3;
1746 log_size = 2;
1747 format = AC_FETCH_FORMAT_FLOAT;
1748 } else {
1749 /* 2_10_10_10 data formats */
1750 LLVMValueRef data = loads[0];
1751 LLVMTypeRef i10 = LLVMIntTypeInContext(ctx->context, 10);
1752 LLVMTypeRef i2 = LLVMIntTypeInContext(ctx->context, 2);
1753 loads[0] = LLVMBuildTrunc(ctx->builder, data, i10, "");
1754 tmp = LLVMBuildLShr(ctx->builder, data, LLVMConstInt(ctx->i32, 10, false), "");
1755 loads[1] = LLVMBuildTrunc(ctx->builder, tmp, i10, "");
1756 tmp = LLVMBuildLShr(ctx->builder, data, LLVMConstInt(ctx->i32, 20, false), "");
1757 loads[2] = LLVMBuildTrunc(ctx->builder, tmp, i10, "");
1758 tmp = LLVMBuildLShr(ctx->builder, data, LLVMConstInt(ctx->i32, 30, false), "");
1759 loads[3] = LLVMBuildTrunc(ctx->builder, tmp, i2, "");
1760
1761 num_channels = 4;
1762 }
1763 }
1764
1765 if (format == AC_FETCH_FORMAT_FLOAT) {
1766 if (log_size != 2) {
1767 for (unsigned chan = 0; chan < num_channels; ++chan) {
1768 tmp = ac_to_float(ctx, loads[chan]);
1769 if (log_size == 3)
1770 tmp = LLVMBuildFPTrunc(ctx->builder, tmp, ctx->f32, "");
1771 else if (log_size == 1)
1772 tmp = LLVMBuildFPExt(ctx->builder, tmp, ctx->f32, "");
1773 loads[chan] = ac_to_integer(ctx, tmp);
1774 }
1775 }
1776 } else if (format == AC_FETCH_FORMAT_UINT) {
1777 if (log_size != 2) {
1778 for (unsigned chan = 0; chan < num_channels; ++chan)
1779 loads[chan] = LLVMBuildZExt(ctx->builder, loads[chan], ctx->i32, "");
1780 }
1781 } else if (format == AC_FETCH_FORMAT_SINT) {
1782 if (log_size != 2) {
1783 for (unsigned chan = 0; chan < num_channels; ++chan)
1784 loads[chan] = LLVMBuildSExt(ctx->builder, loads[chan], ctx->i32, "");
1785 }
1786 } else {
1787 bool unsign = format == AC_FETCH_FORMAT_UNORM ||
1788 format == AC_FETCH_FORMAT_USCALED ||
1789 format == AC_FETCH_FORMAT_UINT;
1790
1791 for (unsigned chan = 0; chan < num_channels; ++chan) {
1792 if (unsign) {
1793 tmp = LLVMBuildUIToFP(ctx->builder, loads[chan], ctx->f32, "");
1794 } else {
1795 tmp = LLVMBuildSIToFP(ctx->builder, loads[chan], ctx->f32, "");
1796 }
1797
1798 LLVMValueRef scale = NULL;
1799 if (format == AC_FETCH_FORMAT_FIXED) {
1800 assert(log_size == 2);
1801 scale = LLVMConstReal(ctx->f32, 1.0 / 0x10000);
1802 } else if (format == AC_FETCH_FORMAT_UNORM) {
1803 unsigned bits = LLVMGetIntTypeWidth(LLVMTypeOf(loads[chan]));
1804 scale = LLVMConstReal(ctx->f32, 1.0 / (((uint64_t)1 << bits) - 1));
1805 } else if (format == AC_FETCH_FORMAT_SNORM) {
1806 unsigned bits = LLVMGetIntTypeWidth(LLVMTypeOf(loads[chan]));
1807 scale = LLVMConstReal(ctx->f32, 1.0 / (((uint64_t)1 << (bits - 1)) - 1));
1808 }
1809 if (scale)
1810 tmp = LLVMBuildFMul(ctx->builder, tmp, scale, "");
1811
1812 if (format == AC_FETCH_FORMAT_SNORM) {
1813 /* Clamp to [-1, 1] */
1814 LLVMValueRef neg_one = LLVMConstReal(ctx->f32, -1.0);
1815 LLVMValueRef clamp =
1816 LLVMBuildFCmp(ctx->builder, LLVMRealULT, tmp, neg_one, "");
1817 tmp = LLVMBuildSelect(ctx->builder, clamp, neg_one, tmp, "");
1818 }
1819
1820 loads[chan] = ac_to_integer(ctx, tmp);
1821 }
1822 }
1823
1824 while (num_channels < 4) {
1825 if (format == AC_FETCH_FORMAT_UINT || format == AC_FETCH_FORMAT_SINT) {
1826 loads[num_channels] = num_channels == 3 ? ctx->i32_1 : ctx->i32_0;
1827 } else {
1828 loads[num_channels] = ac_to_integer(ctx, num_channels == 3 ? ctx->f32_1 : ctx->f32_0);
1829 }
1830 num_channels++;
1831 }
1832
1833 if (reverse) {
1834 tmp = loads[0];
1835 loads[0] = loads[2];
1836 loads[2] = tmp;
1837 }
1838
1839 return ac_build_gather_values(ctx, loads, 4);
1840 }
1841
1842 static void
1843 ac_build_tbuffer_store(struct ac_llvm_context *ctx,
1844 LLVMValueRef rsrc,
1845 LLVMValueRef vdata,
1846 LLVMValueRef vindex,
1847 LLVMValueRef voffset,
1848 LLVMValueRef soffset,
1849 LLVMValueRef immoffset,
1850 unsigned num_channels,
1851 unsigned dfmt,
1852 unsigned nfmt,
1853 unsigned cache_policy,
1854 bool structurized)
1855 {
1856 voffset = LLVMBuildAdd(ctx->builder, voffset ? voffset : ctx->i32_0,
1857 immoffset, "");
1858
1859 LLVMValueRef args[7];
1860 int idx = 0;
1861 args[idx++] = vdata;
1862 args[idx++] = LLVMBuildBitCast(ctx->builder, rsrc, ctx->v4i32, "");
1863 if (structurized)
1864 args[idx++] = vindex ? vindex : ctx->i32_0;
1865 args[idx++] = voffset ? voffset : ctx->i32_0;
1866 args[idx++] = soffset ? soffset : ctx->i32_0;
1867 args[idx++] = LLVMConstInt(ctx->i32, ac_get_tbuffer_format(ctx->chip_class, dfmt, nfmt), 0);
1868 args[idx++] = LLVMConstInt(ctx->i32, cache_policy, 0);
1869 unsigned func = !ac_has_vec3_support(ctx->chip_class, true) && num_channels == 3 ? 4 : num_channels;
1870 const char *indexing_kind = structurized ? "struct" : "raw";
1871 char name[256], type_name[8];
1872
1873 LLVMTypeRef type = func > 1 ? LLVMVectorType(ctx->i32, func) : ctx->i32;
1874 ac_build_type_name_for_intr(type, type_name, sizeof(type_name));
1875
1876 snprintf(name, sizeof(name), "llvm.amdgcn.%s.tbuffer.store.%s",
1877 indexing_kind, type_name);
1878
1879 ac_build_intrinsic(ctx, name, ctx->voidt, args, idx,
1880 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY);
1881 }
1882
1883 void
1884 ac_build_struct_tbuffer_store(struct ac_llvm_context *ctx,
1885 LLVMValueRef rsrc,
1886 LLVMValueRef vdata,
1887 LLVMValueRef vindex,
1888 LLVMValueRef voffset,
1889 LLVMValueRef soffset,
1890 LLVMValueRef immoffset,
1891 unsigned num_channels,
1892 unsigned dfmt,
1893 unsigned nfmt,
1894 unsigned cache_policy)
1895 {
1896 ac_build_tbuffer_store(ctx, rsrc, vdata, vindex, voffset, soffset,
1897 immoffset, num_channels, dfmt, nfmt, cache_policy,
1898 true);
1899 }
1900
1901 void
1902 ac_build_raw_tbuffer_store(struct ac_llvm_context *ctx,
1903 LLVMValueRef rsrc,
1904 LLVMValueRef vdata,
1905 LLVMValueRef voffset,
1906 LLVMValueRef soffset,
1907 LLVMValueRef immoffset,
1908 unsigned num_channels,
1909 unsigned dfmt,
1910 unsigned nfmt,
1911 unsigned cache_policy)
1912 {
1913 ac_build_tbuffer_store(ctx, rsrc, vdata, NULL, voffset, soffset,
1914 immoffset, num_channels, dfmt, nfmt, cache_policy,
1915 false);
1916 }
1917
1918 void
1919 ac_build_tbuffer_store_short(struct ac_llvm_context *ctx,
1920 LLVMValueRef rsrc,
1921 LLVMValueRef vdata,
1922 LLVMValueRef voffset,
1923 LLVMValueRef soffset,
1924 unsigned cache_policy)
1925 {
1926 vdata = LLVMBuildBitCast(ctx->builder, vdata, ctx->i16, "");
1927
1928 if (LLVM_VERSION_MAJOR >= 9) {
1929 /* LLVM 9+ supports i8/i16 with struct/raw intrinsics. */
1930 ac_build_buffer_store_common(ctx, rsrc, vdata, NULL,
1931 voffset, soffset, cache_policy,
1932 false, false);
1933 } else {
1934 unsigned dfmt = V_008F0C_BUF_DATA_FORMAT_16;
1935 unsigned nfmt = V_008F0C_BUF_NUM_FORMAT_UINT;
1936
1937 vdata = LLVMBuildZExt(ctx->builder, vdata, ctx->i32, "");
1938
1939 ac_build_raw_tbuffer_store(ctx, rsrc, vdata, voffset, soffset,
1940 ctx->i32_0, 1, dfmt, nfmt, cache_policy);
1941 }
1942 }
1943
1944 void
1945 ac_build_tbuffer_store_byte(struct ac_llvm_context *ctx,
1946 LLVMValueRef rsrc,
1947 LLVMValueRef vdata,
1948 LLVMValueRef voffset,
1949 LLVMValueRef soffset,
1950 unsigned cache_policy)
1951 {
1952 vdata = LLVMBuildBitCast(ctx->builder, vdata, ctx->i8, "");
1953
1954 if (LLVM_VERSION_MAJOR >= 9) {
1955 /* LLVM 9+ supports i8/i16 with struct/raw intrinsics. */
1956 ac_build_buffer_store_common(ctx, rsrc, vdata, NULL,
1957 voffset, soffset, cache_policy,
1958 false, false);
1959 } else {
1960 unsigned dfmt = V_008F0C_BUF_DATA_FORMAT_8;
1961 unsigned nfmt = V_008F0C_BUF_NUM_FORMAT_UINT;
1962
1963 vdata = LLVMBuildZExt(ctx->builder, vdata, ctx->i32, "");
1964
1965 ac_build_raw_tbuffer_store(ctx, rsrc, vdata, voffset, soffset,
1966 ctx->i32_0, 1, dfmt, nfmt, cache_policy);
1967 }
1968 }
1969 /**
1970 * Set range metadata on an instruction. This can only be used on load and
1971 * call instructions. If you know an instruction can only produce the values
1972 * 0, 1, 2, you would do set_range_metadata(value, 0, 3);
1973 * \p lo is the minimum value inclusive.
1974 * \p hi is the maximum value exclusive.
1975 */
1976 static void set_range_metadata(struct ac_llvm_context *ctx,
1977 LLVMValueRef value, unsigned lo, unsigned hi)
1978 {
1979 LLVMValueRef range_md, md_args[2];
1980 LLVMTypeRef type = LLVMTypeOf(value);
1981 LLVMContextRef context = LLVMGetTypeContext(type);
1982
1983 md_args[0] = LLVMConstInt(type, lo, false);
1984 md_args[1] = LLVMConstInt(type, hi, false);
1985 range_md = LLVMMDNodeInContext(context, md_args, 2);
1986 LLVMSetMetadata(value, ctx->range_md_kind, range_md);
1987 }
1988
1989 LLVMValueRef
1990 ac_get_thread_id(struct ac_llvm_context *ctx)
1991 {
1992 LLVMValueRef tid;
1993
1994 LLVMValueRef tid_args[2];
1995 tid_args[0] = LLVMConstInt(ctx->i32, 0xffffffff, false);
1996 tid_args[1] = ctx->i32_0;
1997 tid_args[1] = ac_build_intrinsic(ctx,
1998 "llvm.amdgcn.mbcnt.lo", ctx->i32,
1999 tid_args, 2, AC_FUNC_ATTR_READNONE);
2000
2001 if (ctx->wave_size == 32) {
2002 tid = tid_args[1];
2003 } else {
2004 tid = ac_build_intrinsic(ctx, "llvm.amdgcn.mbcnt.hi",
2005 ctx->i32, tid_args,
2006 2, AC_FUNC_ATTR_READNONE);
2007 }
2008 set_range_metadata(ctx, tid, 0, ctx->wave_size);
2009 return tid;
2010 }
2011
2012 /*
2013 * AMD GCN implements derivatives using the local data store (LDS)
2014 * All writes to the LDS happen in all executing threads at
2015 * the same time. TID is the Thread ID for the current
2016 * thread and is a value between 0 and 63, representing
2017 * the thread's position in the wavefront.
2018 *
2019 * For the pixel shader threads are grouped into quads of four pixels.
2020 * The TIDs of the pixels of a quad are:
2021 *
2022 * +------+------+
2023 * |4n + 0|4n + 1|
2024 * +------+------+
2025 * |4n + 2|4n + 3|
2026 * +------+------+
2027 *
2028 * So, masking the TID with 0xfffffffc yields the TID of the top left pixel
2029 * of the quad, masking with 0xfffffffd yields the TID of the top pixel of
2030 * the current pixel's column, and masking with 0xfffffffe yields the TID
2031 * of the left pixel of the current pixel's row.
2032 *
2033 * Adding 1 yields the TID of the pixel to the right of the left pixel, and
2034 * adding 2 yields the TID of the pixel below the top pixel.
2035 */
2036 LLVMValueRef
2037 ac_build_ddxy(struct ac_llvm_context *ctx,
2038 uint32_t mask,
2039 int idx,
2040 LLVMValueRef val)
2041 {
2042 unsigned tl_lanes[4], trbl_lanes[4];
2043 char name[32], type[8];
2044 LLVMValueRef tl, trbl;
2045 LLVMTypeRef result_type;
2046 LLVMValueRef result;
2047
2048 result_type = ac_to_float_type(ctx, LLVMTypeOf(val));
2049
2050 if (result_type == ctx->f16)
2051 val = LLVMBuildZExt(ctx->builder, val, ctx->i32, "");
2052
2053 for (unsigned i = 0; i < 4; ++i) {
2054 tl_lanes[i] = i & mask;
2055 trbl_lanes[i] = (i & mask) + idx;
2056 }
2057
2058 tl = ac_build_quad_swizzle(ctx, val,
2059 tl_lanes[0], tl_lanes[1],
2060 tl_lanes[2], tl_lanes[3]);
2061 trbl = ac_build_quad_swizzle(ctx, val,
2062 trbl_lanes[0], trbl_lanes[1],
2063 trbl_lanes[2], trbl_lanes[3]);
2064
2065 if (result_type == ctx->f16) {
2066 tl = LLVMBuildTrunc(ctx->builder, tl, ctx->i16, "");
2067 trbl = LLVMBuildTrunc(ctx->builder, trbl, ctx->i16, "");
2068 }
2069
2070 tl = LLVMBuildBitCast(ctx->builder, tl, result_type, "");
2071 trbl = LLVMBuildBitCast(ctx->builder, trbl, result_type, "");
2072 result = LLVMBuildFSub(ctx->builder, trbl, tl, "");
2073
2074 ac_build_type_name_for_intr(result_type, type, sizeof(type));
2075 snprintf(name, sizeof(name), "llvm.amdgcn.wqm.%s", type);
2076
2077 return ac_build_intrinsic(ctx, name, result_type, &result, 1, 0);
2078 }
2079
2080 void
2081 ac_build_sendmsg(struct ac_llvm_context *ctx,
2082 uint32_t msg,
2083 LLVMValueRef wave_id)
2084 {
2085 LLVMValueRef args[2];
2086 args[0] = LLVMConstInt(ctx->i32, msg, false);
2087 args[1] = wave_id;
2088 ac_build_intrinsic(ctx, "llvm.amdgcn.s.sendmsg", ctx->voidt, args, 2, 0);
2089 }
2090
2091 LLVMValueRef
2092 ac_build_imsb(struct ac_llvm_context *ctx,
2093 LLVMValueRef arg,
2094 LLVMTypeRef dst_type)
2095 {
2096 LLVMValueRef msb = ac_build_intrinsic(ctx, "llvm.amdgcn.sffbh.i32",
2097 dst_type, &arg, 1,
2098 AC_FUNC_ATTR_READNONE);
2099
2100 /* The HW returns the last bit index from MSB, but NIR/TGSI wants
2101 * the index from LSB. Invert it by doing "31 - msb". */
2102 msb = LLVMBuildSub(ctx->builder, LLVMConstInt(ctx->i32, 31, false),
2103 msb, "");
2104
2105 LLVMValueRef all_ones = LLVMConstInt(ctx->i32, -1, true);
2106 LLVMValueRef cond = LLVMBuildOr(ctx->builder,
2107 LLVMBuildICmp(ctx->builder, LLVMIntEQ,
2108 arg, ctx->i32_0, ""),
2109 LLVMBuildICmp(ctx->builder, LLVMIntEQ,
2110 arg, all_ones, ""), "");
2111
2112 return LLVMBuildSelect(ctx->builder, cond, all_ones, msb, "");
2113 }
2114
2115 LLVMValueRef
2116 ac_build_umsb(struct ac_llvm_context *ctx,
2117 LLVMValueRef arg,
2118 LLVMTypeRef dst_type)
2119 {
2120 const char *intrin_name;
2121 LLVMTypeRef type;
2122 LLVMValueRef highest_bit;
2123 LLVMValueRef zero;
2124 unsigned bitsize;
2125
2126 bitsize = ac_get_elem_bits(ctx, LLVMTypeOf(arg));
2127 switch (bitsize) {
2128 case 64:
2129 intrin_name = "llvm.ctlz.i64";
2130 type = ctx->i64;
2131 highest_bit = LLVMConstInt(ctx->i64, 63, false);
2132 zero = ctx->i64_0;
2133 break;
2134 case 32:
2135 intrin_name = "llvm.ctlz.i32";
2136 type = ctx->i32;
2137 highest_bit = LLVMConstInt(ctx->i32, 31, false);
2138 zero = ctx->i32_0;
2139 break;
2140 case 16:
2141 intrin_name = "llvm.ctlz.i16";
2142 type = ctx->i16;
2143 highest_bit = LLVMConstInt(ctx->i16, 15, false);
2144 zero = ctx->i16_0;
2145 break;
2146 case 8:
2147 intrin_name = "llvm.ctlz.i8";
2148 type = ctx->i8;
2149 highest_bit = LLVMConstInt(ctx->i8, 7, false);
2150 zero = ctx->i8_0;
2151 break;
2152 default:
2153 unreachable(!"invalid bitsize");
2154 break;
2155 }
2156
2157 LLVMValueRef params[2] = {
2158 arg,
2159 ctx->i1true,
2160 };
2161
2162 LLVMValueRef msb = ac_build_intrinsic(ctx, intrin_name, type,
2163 params, 2,
2164 AC_FUNC_ATTR_READNONE);
2165
2166 /* The HW returns the last bit index from MSB, but TGSI/NIR wants
2167 * the index from LSB. Invert it by doing "31 - msb". */
2168 msb = LLVMBuildSub(ctx->builder, highest_bit, msb, "");
2169
2170 if (bitsize == 64) {
2171 msb = LLVMBuildTrunc(ctx->builder, msb, ctx->i32, "");
2172 } else if (bitsize < 32) {
2173 msb = LLVMBuildSExt(ctx->builder, msb, ctx->i32, "");
2174 }
2175
2176 /* check for zero */
2177 return LLVMBuildSelect(ctx->builder,
2178 LLVMBuildICmp(ctx->builder, LLVMIntEQ, arg, zero, ""),
2179 LLVMConstInt(ctx->i32, -1, true), msb, "");
2180 }
2181
2182 LLVMValueRef ac_build_fmin(struct ac_llvm_context *ctx, LLVMValueRef a,
2183 LLVMValueRef b)
2184 {
2185 char name[64], type[64];
2186
2187 ac_build_type_name_for_intr(LLVMTypeOf(a), type, sizeof(type));
2188 snprintf(name, sizeof(name), "llvm.minnum.%s", type);
2189 LLVMValueRef args[2] = {a, b};
2190 return ac_build_intrinsic(ctx, name, LLVMTypeOf(a), args, 2,
2191 AC_FUNC_ATTR_READNONE);
2192 }
2193
2194 LLVMValueRef ac_build_fmax(struct ac_llvm_context *ctx, LLVMValueRef a,
2195 LLVMValueRef b)
2196 {
2197 char name[64], type[64];
2198
2199 ac_build_type_name_for_intr(LLVMTypeOf(a), type, sizeof(type));
2200 snprintf(name, sizeof(name), "llvm.maxnum.%s", type);
2201 LLVMValueRef args[2] = {a, b};
2202 return ac_build_intrinsic(ctx, name, LLVMTypeOf(a), args, 2,
2203 AC_FUNC_ATTR_READNONE);
2204 }
2205
2206 LLVMValueRef ac_build_imin(struct ac_llvm_context *ctx, LLVMValueRef a,
2207 LLVMValueRef b)
2208 {
2209 LLVMValueRef cmp = LLVMBuildICmp(ctx->builder, LLVMIntSLE, a, b, "");
2210 return LLVMBuildSelect(ctx->builder, cmp, a, b, "");
2211 }
2212
2213 LLVMValueRef ac_build_imax(struct ac_llvm_context *ctx, LLVMValueRef a,
2214 LLVMValueRef b)
2215 {
2216 LLVMValueRef cmp = LLVMBuildICmp(ctx->builder, LLVMIntSGT, a, b, "");
2217 return LLVMBuildSelect(ctx->builder, cmp, a, b, "");
2218 }
2219
2220 LLVMValueRef ac_build_umin(struct ac_llvm_context *ctx, LLVMValueRef a,
2221 LLVMValueRef b)
2222 {
2223 LLVMValueRef cmp = LLVMBuildICmp(ctx->builder, LLVMIntULE, a, b, "");
2224 return LLVMBuildSelect(ctx->builder, cmp, a, b, "");
2225 }
2226
2227 LLVMValueRef ac_build_umax(struct ac_llvm_context *ctx, LLVMValueRef a,
2228 LLVMValueRef b)
2229 {
2230 LLVMValueRef cmp = LLVMBuildICmp(ctx->builder, LLVMIntUGE, a, b, "");
2231 return LLVMBuildSelect(ctx->builder, cmp, a, b, "");
2232 }
2233
2234 LLVMValueRef ac_build_clamp(struct ac_llvm_context *ctx, LLVMValueRef value)
2235 {
2236 LLVMTypeRef t = LLVMTypeOf(value);
2237 return ac_build_fmin(ctx, ac_build_fmax(ctx, value, LLVMConstReal(t, 0.0)),
2238 LLVMConstReal(t, 1.0));
2239 }
2240
2241 void ac_build_export(struct ac_llvm_context *ctx, struct ac_export_args *a)
2242 {
2243 LLVMValueRef args[9];
2244
2245 args[0] = LLVMConstInt(ctx->i32, a->target, 0);
2246 args[1] = LLVMConstInt(ctx->i32, a->enabled_channels, 0);
2247
2248 if (a->compr) {
2249 args[2] = LLVMBuildBitCast(ctx->builder, a->out[0],
2250 ctx->v2i16, "");
2251 args[3] = LLVMBuildBitCast(ctx->builder, a->out[1],
2252 ctx->v2i16, "");
2253 args[4] = LLVMConstInt(ctx->i1, a->done, 0);
2254 args[5] = LLVMConstInt(ctx->i1, a->valid_mask, 0);
2255
2256 ac_build_intrinsic(ctx, "llvm.amdgcn.exp.compr.v2i16",
2257 ctx->voidt, args, 6, 0);
2258 } else {
2259 args[2] = a->out[0];
2260 args[3] = a->out[1];
2261 args[4] = a->out[2];
2262 args[5] = a->out[3];
2263 args[6] = LLVMConstInt(ctx->i1, a->done, 0);
2264 args[7] = LLVMConstInt(ctx->i1, a->valid_mask, 0);
2265
2266 ac_build_intrinsic(ctx, "llvm.amdgcn.exp.f32",
2267 ctx->voidt, args, 8, 0);
2268 }
2269 }
2270
2271 void ac_build_export_null(struct ac_llvm_context *ctx)
2272 {
2273 struct ac_export_args args;
2274
2275 args.enabled_channels = 0x0; /* enabled channels */
2276 args.valid_mask = 1; /* whether the EXEC mask is valid */
2277 args.done = 1; /* DONE bit */
2278 args.target = V_008DFC_SQ_EXP_NULL;
2279 args.compr = 0; /* COMPR flag (0 = 32-bit export) */
2280 args.out[0] = LLVMGetUndef(ctx->f32); /* R */
2281 args.out[1] = LLVMGetUndef(ctx->f32); /* G */
2282 args.out[2] = LLVMGetUndef(ctx->f32); /* B */
2283 args.out[3] = LLVMGetUndef(ctx->f32); /* A */
2284
2285 ac_build_export(ctx, &args);
2286 }
2287
2288 static unsigned ac_num_coords(enum ac_image_dim dim)
2289 {
2290 switch (dim) {
2291 case ac_image_1d:
2292 return 1;
2293 case ac_image_2d:
2294 case ac_image_1darray:
2295 return 2;
2296 case ac_image_3d:
2297 case ac_image_cube:
2298 case ac_image_2darray:
2299 case ac_image_2dmsaa:
2300 return 3;
2301 case ac_image_2darraymsaa:
2302 return 4;
2303 default:
2304 unreachable("ac_num_coords: bad dim");
2305 }
2306 }
2307
2308 static unsigned ac_num_derivs(enum ac_image_dim dim)
2309 {
2310 switch (dim) {
2311 case ac_image_1d:
2312 case ac_image_1darray:
2313 return 2;
2314 case ac_image_2d:
2315 case ac_image_2darray:
2316 case ac_image_cube:
2317 return 4;
2318 case ac_image_3d:
2319 return 6;
2320 case ac_image_2dmsaa:
2321 case ac_image_2darraymsaa:
2322 default:
2323 unreachable("derivatives not supported");
2324 }
2325 }
2326
2327 static const char *get_atomic_name(enum ac_atomic_op op)
2328 {
2329 switch (op) {
2330 case ac_atomic_swap: return "swap";
2331 case ac_atomic_add: return "add";
2332 case ac_atomic_sub: return "sub";
2333 case ac_atomic_smin: return "smin";
2334 case ac_atomic_umin: return "umin";
2335 case ac_atomic_smax: return "smax";
2336 case ac_atomic_umax: return "umax";
2337 case ac_atomic_and: return "and";
2338 case ac_atomic_or: return "or";
2339 case ac_atomic_xor: return "xor";
2340 case ac_atomic_inc_wrap: return "inc";
2341 case ac_atomic_dec_wrap: return "dec";
2342 }
2343 unreachable("bad atomic op");
2344 }
2345
2346 LLVMValueRef ac_build_image_opcode(struct ac_llvm_context *ctx,
2347 struct ac_image_args *a)
2348 {
2349 const char *overload[3] = { "", "", "" };
2350 unsigned num_overloads = 0;
2351 LLVMValueRef args[18];
2352 unsigned num_args = 0;
2353 enum ac_image_dim dim = a->dim;
2354
2355 assert(!a->lod || a->lod == ctx->i32_0 || a->lod == ctx->f32_0 ||
2356 !a->level_zero);
2357 assert((a->opcode != ac_image_get_resinfo && a->opcode != ac_image_load_mip &&
2358 a->opcode != ac_image_store_mip) ||
2359 a->lod);
2360 assert(a->opcode == ac_image_sample || a->opcode == ac_image_gather4 ||
2361 (!a->compare && !a->offset));
2362 assert((a->opcode == ac_image_sample || a->opcode == ac_image_gather4 ||
2363 a->opcode == ac_image_get_lod) ||
2364 !a->bias);
2365 assert((a->bias ? 1 : 0) +
2366 (a->lod ? 1 : 0) +
2367 (a->level_zero ? 1 : 0) +
2368 (a->derivs[0] ? 1 : 0) <= 1);
2369 assert((a->min_lod ? 1 : 0) +
2370 (a->lod ? 1 : 0) +
2371 (a->level_zero ? 1 : 0) <= 1);
2372
2373 if (a->opcode == ac_image_get_lod) {
2374 switch (dim) {
2375 case ac_image_1darray:
2376 dim = ac_image_1d;
2377 break;
2378 case ac_image_2darray:
2379 case ac_image_cube:
2380 dim = ac_image_2d;
2381 break;
2382 default:
2383 break;
2384 }
2385 }
2386
2387 bool sample = a->opcode == ac_image_sample ||
2388 a->opcode == ac_image_gather4 ||
2389 a->opcode == ac_image_get_lod;
2390 bool atomic = a->opcode == ac_image_atomic ||
2391 a->opcode == ac_image_atomic_cmpswap;
2392 bool load = a->opcode == ac_image_sample ||
2393 a->opcode == ac_image_gather4 ||
2394 a->opcode == ac_image_load ||
2395 a->opcode == ac_image_load_mip;
2396 LLVMTypeRef coord_type = sample ? ctx->f32 : ctx->i32;
2397
2398 if (atomic || a->opcode == ac_image_store || a->opcode == ac_image_store_mip) {
2399 args[num_args++] = a->data[0];
2400 if (a->opcode == ac_image_atomic_cmpswap)
2401 args[num_args++] = a->data[1];
2402 }
2403
2404 if (!atomic)
2405 args[num_args++] = LLVMConstInt(ctx->i32, a->dmask, false);
2406
2407 if (a->offset)
2408 args[num_args++] = ac_to_integer(ctx, a->offset);
2409 if (a->bias) {
2410 args[num_args++] = ac_to_float(ctx, a->bias);
2411 overload[num_overloads++] = ".f32";
2412 }
2413 if (a->compare)
2414 args[num_args++] = ac_to_float(ctx, a->compare);
2415 if (a->derivs[0]) {
2416 unsigned count = ac_num_derivs(dim);
2417 for (unsigned i = 0; i < count; ++i)
2418 args[num_args++] = ac_to_float(ctx, a->derivs[i]);
2419 overload[num_overloads++] = ".f32";
2420 }
2421 unsigned num_coords =
2422 a->opcode != ac_image_get_resinfo ? ac_num_coords(dim) : 0;
2423 for (unsigned i = 0; i < num_coords; ++i)
2424 args[num_args++] = LLVMBuildBitCast(ctx->builder, a->coords[i], coord_type, "");
2425 if (a->lod)
2426 args[num_args++] = LLVMBuildBitCast(ctx->builder, a->lod, coord_type, "");
2427 if (a->min_lod)
2428 args[num_args++] = LLVMBuildBitCast(ctx->builder, a->min_lod, coord_type, "");
2429
2430 overload[num_overloads++] = sample ? ".f32" : ".i32";
2431
2432 args[num_args++] = a->resource;
2433 if (sample) {
2434 args[num_args++] = a->sampler;
2435 args[num_args++] = LLVMConstInt(ctx->i1, a->unorm, false);
2436 }
2437
2438 args[num_args++] = ctx->i32_0; /* texfailctrl */
2439 args[num_args++] = LLVMConstInt(ctx->i32,
2440 load ? get_load_cache_policy(ctx, a->cache_policy) :
2441 a->cache_policy, false);
2442
2443 const char *name;
2444 const char *atomic_subop = "";
2445 switch (a->opcode) {
2446 case ac_image_sample: name = "sample"; break;
2447 case ac_image_gather4: name = "gather4"; break;
2448 case ac_image_load: name = "load"; break;
2449 case ac_image_load_mip: name = "load.mip"; break;
2450 case ac_image_store: name = "store"; break;
2451 case ac_image_store_mip: name = "store.mip"; break;
2452 case ac_image_atomic:
2453 name = "atomic.";
2454 atomic_subop = get_atomic_name(a->atomic);
2455 break;
2456 case ac_image_atomic_cmpswap:
2457 name = "atomic.";
2458 atomic_subop = "cmpswap";
2459 break;
2460 case ac_image_get_lod: name = "getlod"; break;
2461 case ac_image_get_resinfo: name = "getresinfo"; break;
2462 default: unreachable("invalid image opcode");
2463 }
2464
2465 const char *dimname;
2466 switch (dim) {
2467 case ac_image_1d: dimname = "1d"; break;
2468 case ac_image_2d: dimname = "2d"; break;
2469 case ac_image_3d: dimname = "3d"; break;
2470 case ac_image_cube: dimname = "cube"; break;
2471 case ac_image_1darray: dimname = "1darray"; break;
2472 case ac_image_2darray: dimname = "2darray"; break;
2473 case ac_image_2dmsaa: dimname = "2dmsaa"; break;
2474 case ac_image_2darraymsaa: dimname = "2darraymsaa"; break;
2475 default: unreachable("invalid dim");
2476 }
2477
2478 bool lod_suffix =
2479 a->lod && (a->opcode == ac_image_sample || a->opcode == ac_image_gather4);
2480 char intr_name[96];
2481 snprintf(intr_name, sizeof(intr_name),
2482 "llvm.amdgcn.image.%s%s" /* base name */
2483 "%s%s%s%s" /* sample/gather modifiers */
2484 ".%s.%s%s%s%s", /* dimension and type overloads */
2485 name, atomic_subop,
2486 a->compare ? ".c" : "",
2487 a->bias ? ".b" :
2488 lod_suffix ? ".l" :
2489 a->derivs[0] ? ".d" :
2490 a->level_zero ? ".lz" : "",
2491 a->min_lod ? ".cl" : "",
2492 a->offset ? ".o" : "",
2493 dimname,
2494 atomic ? "i32" : "v4f32",
2495 overload[0], overload[1], overload[2]);
2496
2497 LLVMTypeRef retty;
2498 if (atomic)
2499 retty = ctx->i32;
2500 else if (a->opcode == ac_image_store || a->opcode == ac_image_store_mip)
2501 retty = ctx->voidt;
2502 else
2503 retty = ctx->v4f32;
2504
2505 LLVMValueRef result =
2506 ac_build_intrinsic(ctx, intr_name, retty, args, num_args,
2507 a->attributes);
2508 if (!sample && retty == ctx->v4f32) {
2509 result = LLVMBuildBitCast(ctx->builder, result,
2510 ctx->v4i32, "");
2511 }
2512 return result;
2513 }
2514
2515 LLVMValueRef ac_build_image_get_sample_count(struct ac_llvm_context *ctx,
2516 LLVMValueRef rsrc)
2517 {
2518 LLVMValueRef samples;
2519
2520 /* Read the samples from the descriptor directly.
2521 * Hardware doesn't have any instruction for this.
2522 */
2523 samples = LLVMBuildExtractElement(ctx->builder, rsrc,
2524 LLVMConstInt(ctx->i32, 3, 0), "");
2525 samples = LLVMBuildLShr(ctx->builder, samples,
2526 LLVMConstInt(ctx->i32, 16, 0), "");
2527 samples = LLVMBuildAnd(ctx->builder, samples,
2528 LLVMConstInt(ctx->i32, 0xf, 0), "");
2529 samples = LLVMBuildShl(ctx->builder, ctx->i32_1,
2530 samples, "");
2531 return samples;
2532 }
2533
2534 LLVMValueRef ac_build_cvt_pkrtz_f16(struct ac_llvm_context *ctx,
2535 LLVMValueRef args[2])
2536 {
2537 return ac_build_intrinsic(ctx, "llvm.amdgcn.cvt.pkrtz", ctx->v2f16,
2538 args, 2, AC_FUNC_ATTR_READNONE);
2539 }
2540
2541 LLVMValueRef ac_build_cvt_pknorm_i16(struct ac_llvm_context *ctx,
2542 LLVMValueRef args[2])
2543 {
2544 LLVMValueRef res =
2545 ac_build_intrinsic(ctx, "llvm.amdgcn.cvt.pknorm.i16",
2546 ctx->v2i16, args, 2,
2547 AC_FUNC_ATTR_READNONE);
2548 return LLVMBuildBitCast(ctx->builder, res, ctx->i32, "");
2549 }
2550
2551 LLVMValueRef ac_build_cvt_pknorm_u16(struct ac_llvm_context *ctx,
2552 LLVMValueRef args[2])
2553 {
2554 LLVMValueRef res =
2555 ac_build_intrinsic(ctx, "llvm.amdgcn.cvt.pknorm.u16",
2556 ctx->v2i16, args, 2,
2557 AC_FUNC_ATTR_READNONE);
2558 return LLVMBuildBitCast(ctx->builder, res, ctx->i32, "");
2559 }
2560
2561 /* The 8-bit and 10-bit clamping is for HW workarounds. */
2562 LLVMValueRef ac_build_cvt_pk_i16(struct ac_llvm_context *ctx,
2563 LLVMValueRef args[2], unsigned bits, bool hi)
2564 {
2565 assert(bits == 8 || bits == 10 || bits == 16);
2566
2567 LLVMValueRef max_rgb = LLVMConstInt(ctx->i32,
2568 bits == 8 ? 127 : bits == 10 ? 511 : 32767, 0);
2569 LLVMValueRef min_rgb = LLVMConstInt(ctx->i32,
2570 bits == 8 ? -128 : bits == 10 ? -512 : -32768, 0);
2571 LLVMValueRef max_alpha =
2572 bits != 10 ? max_rgb : ctx->i32_1;
2573 LLVMValueRef min_alpha =
2574 bits != 10 ? min_rgb : LLVMConstInt(ctx->i32, -2, 0);
2575
2576 /* Clamp. */
2577 if (bits != 16) {
2578 for (int i = 0; i < 2; i++) {
2579 bool alpha = hi && i == 1;
2580 args[i] = ac_build_imin(ctx, args[i],
2581 alpha ? max_alpha : max_rgb);
2582 args[i] = ac_build_imax(ctx, args[i],
2583 alpha ? min_alpha : min_rgb);
2584 }
2585 }
2586
2587 LLVMValueRef res =
2588 ac_build_intrinsic(ctx, "llvm.amdgcn.cvt.pk.i16",
2589 ctx->v2i16, args, 2,
2590 AC_FUNC_ATTR_READNONE);
2591 return LLVMBuildBitCast(ctx->builder, res, ctx->i32, "");
2592 }
2593
2594 /* The 8-bit and 10-bit clamping is for HW workarounds. */
2595 LLVMValueRef ac_build_cvt_pk_u16(struct ac_llvm_context *ctx,
2596 LLVMValueRef args[2], unsigned bits, bool hi)
2597 {
2598 assert(bits == 8 || bits == 10 || bits == 16);
2599
2600 LLVMValueRef max_rgb = LLVMConstInt(ctx->i32,
2601 bits == 8 ? 255 : bits == 10 ? 1023 : 65535, 0);
2602 LLVMValueRef max_alpha =
2603 bits != 10 ? max_rgb : LLVMConstInt(ctx->i32, 3, 0);
2604
2605 /* Clamp. */
2606 if (bits != 16) {
2607 for (int i = 0; i < 2; i++) {
2608 bool alpha = hi && i == 1;
2609 args[i] = ac_build_umin(ctx, args[i],
2610 alpha ? max_alpha : max_rgb);
2611 }
2612 }
2613
2614 LLVMValueRef res =
2615 ac_build_intrinsic(ctx, "llvm.amdgcn.cvt.pk.u16",
2616 ctx->v2i16, args, 2,
2617 AC_FUNC_ATTR_READNONE);
2618 return LLVMBuildBitCast(ctx->builder, res, ctx->i32, "");
2619 }
2620
2621 LLVMValueRef ac_build_wqm_vote(struct ac_llvm_context *ctx, LLVMValueRef i1)
2622 {
2623 return ac_build_intrinsic(ctx, "llvm.amdgcn.wqm.vote", ctx->i1,
2624 &i1, 1, AC_FUNC_ATTR_READNONE);
2625 }
2626
2627 void ac_build_kill_if_false(struct ac_llvm_context *ctx, LLVMValueRef i1)
2628 {
2629 ac_build_intrinsic(ctx, "llvm.amdgcn.kill", ctx->voidt,
2630 &i1, 1, 0);
2631 }
2632
2633 LLVMValueRef ac_build_bfe(struct ac_llvm_context *ctx, LLVMValueRef input,
2634 LLVMValueRef offset, LLVMValueRef width,
2635 bool is_signed)
2636 {
2637 LLVMValueRef args[] = {
2638 input,
2639 offset,
2640 width,
2641 };
2642
2643 return ac_build_intrinsic(ctx, is_signed ? "llvm.amdgcn.sbfe.i32" :
2644 "llvm.amdgcn.ubfe.i32",
2645 ctx->i32, args, 3, AC_FUNC_ATTR_READNONE);
2646
2647 }
2648
2649 LLVMValueRef ac_build_imad(struct ac_llvm_context *ctx, LLVMValueRef s0,
2650 LLVMValueRef s1, LLVMValueRef s2)
2651 {
2652 return LLVMBuildAdd(ctx->builder,
2653 LLVMBuildMul(ctx->builder, s0, s1, ""), s2, "");
2654 }
2655
2656 LLVMValueRef ac_build_fmad(struct ac_llvm_context *ctx, LLVMValueRef s0,
2657 LLVMValueRef s1, LLVMValueRef s2)
2658 {
2659 /* FMA is better on GFX10, because it has FMA units instead of MUL-ADD units. */
2660 if (ctx->chip_class >= GFX10) {
2661 return ac_build_intrinsic(ctx, "llvm.fma.f32", ctx->f32,
2662 (LLVMValueRef []) {s0, s1, s2}, 3,
2663 AC_FUNC_ATTR_READNONE);
2664 }
2665
2666 return LLVMBuildFAdd(ctx->builder,
2667 LLVMBuildFMul(ctx->builder, s0, s1, ""), s2, "");
2668 }
2669
2670 void ac_build_waitcnt(struct ac_llvm_context *ctx, unsigned wait_flags)
2671 {
2672 if (!wait_flags)
2673 return;
2674
2675 unsigned lgkmcnt = 63;
2676 unsigned vmcnt = ctx->chip_class >= GFX9 ? 63 : 15;
2677 unsigned vscnt = 63;
2678
2679 if (wait_flags & AC_WAIT_LGKM)
2680 lgkmcnt = 0;
2681 if (wait_flags & AC_WAIT_VLOAD)
2682 vmcnt = 0;
2683
2684 if (wait_flags & AC_WAIT_VSTORE) {
2685 if (ctx->chip_class >= GFX10)
2686 vscnt = 0;
2687 else
2688 vmcnt = 0;
2689 }
2690
2691 /* There is no intrinsic for vscnt(0), so use a fence. */
2692 if ((wait_flags & AC_WAIT_LGKM &&
2693 wait_flags & AC_WAIT_VLOAD &&
2694 wait_flags & AC_WAIT_VSTORE) ||
2695 vscnt == 0) {
2696 LLVMBuildFence(ctx->builder, LLVMAtomicOrderingRelease, false, "");
2697 return;
2698 }
2699
2700 unsigned simm16 = (lgkmcnt << 8) |
2701 (7 << 4) | /* expcnt */
2702 (vmcnt & 0xf) |
2703 ((vmcnt >> 4) << 14);
2704
2705 LLVMValueRef args[1] = {
2706 LLVMConstInt(ctx->i32, simm16, false),
2707 };
2708 ac_build_intrinsic(ctx, "llvm.amdgcn.s.waitcnt",
2709 ctx->voidt, args, 1, 0);
2710 }
2711
2712 LLVMValueRef ac_build_fmed3(struct ac_llvm_context *ctx, LLVMValueRef src0,
2713 LLVMValueRef src1, LLVMValueRef src2,
2714 unsigned bitsize)
2715 {
2716 LLVMValueRef result;
2717
2718 if (bitsize == 64 || (bitsize == 16 && ctx->chip_class <= GFX8)) {
2719 /* Lower 64-bit fmed because LLVM doesn't expose an intrinsic,
2720 * or lower 16-bit fmed because it's only supported on GFX9+.
2721 */
2722 LLVMValueRef min1, min2, max1;
2723
2724 min1 = ac_build_fmin(ctx, src0, src1);
2725 max1 = ac_build_fmax(ctx, src0, src1);
2726 min2 = ac_build_fmin(ctx, max1, src2);
2727
2728 result = ac_build_fmax(ctx, min2, min1);
2729 } else {
2730 LLVMTypeRef type;
2731 char *intr;
2732
2733 if (bitsize == 16) {
2734 intr = "llvm.amdgcn.fmed3.f16";
2735 type = ctx->f16;
2736 } else {
2737 assert(bitsize == 32);
2738 intr = "llvm.amdgcn.fmed3.f32";
2739 type = ctx->f32;
2740 }
2741
2742 LLVMValueRef params[] = {
2743 src0,
2744 src1,
2745 src2,
2746 };
2747
2748 result = ac_build_intrinsic(ctx, intr, type, params, 3,
2749 AC_FUNC_ATTR_READNONE);
2750 }
2751
2752 if (ctx->chip_class < GFX9 && bitsize == 32) {
2753 /* Only pre-GFX9 chips do not flush denorms. */
2754 result = ac_build_canonicalize(ctx, result, bitsize);
2755 }
2756
2757 return result;
2758 }
2759
2760 LLVMValueRef ac_build_fract(struct ac_llvm_context *ctx, LLVMValueRef src0,
2761 unsigned bitsize)
2762 {
2763 LLVMTypeRef type;
2764 char *intr;
2765
2766 if (bitsize == 16) {
2767 intr = "llvm.amdgcn.fract.f16";
2768 type = ctx->f16;
2769 } else if (bitsize == 32) {
2770 intr = "llvm.amdgcn.fract.f32";
2771 type = ctx->f32;
2772 } else {
2773 intr = "llvm.amdgcn.fract.f64";
2774 type = ctx->f64;
2775 }
2776
2777 LLVMValueRef params[] = {
2778 src0,
2779 };
2780 return ac_build_intrinsic(ctx, intr, type, params, 1,
2781 AC_FUNC_ATTR_READNONE);
2782 }
2783
2784 LLVMValueRef ac_build_isign(struct ac_llvm_context *ctx, LLVMValueRef src0,
2785 unsigned bitsize)
2786 {
2787 LLVMTypeRef type = LLVMIntTypeInContext(ctx->context, bitsize);
2788 LLVMValueRef zero = LLVMConstInt(type, 0, false);
2789 LLVMValueRef one = LLVMConstInt(type, 1, false);
2790
2791 LLVMValueRef cmp, val;
2792 cmp = LLVMBuildICmp(ctx->builder, LLVMIntSGT, src0, zero, "");
2793 val = LLVMBuildSelect(ctx->builder, cmp, one, src0, "");
2794 cmp = LLVMBuildICmp(ctx->builder, LLVMIntSGE, val, zero, "");
2795 val = LLVMBuildSelect(ctx->builder, cmp, val, LLVMConstInt(type, -1, true), "");
2796 return val;
2797 }
2798
2799 LLVMValueRef ac_build_fsign(struct ac_llvm_context *ctx, LLVMValueRef src0,
2800 unsigned bitsize)
2801 {
2802 LLVMValueRef cmp, val, zero, one;
2803 LLVMTypeRef type;
2804
2805 if (bitsize == 16) {
2806 type = ctx->f16;
2807 zero = ctx->f16_0;
2808 one = ctx->f16_1;
2809 } else if (bitsize == 32) {
2810 type = ctx->f32;
2811 zero = ctx->f32_0;
2812 one = ctx->f32_1;
2813 } else {
2814 type = ctx->f64;
2815 zero = ctx->f64_0;
2816 one = ctx->f64_1;
2817 }
2818
2819 cmp = LLVMBuildFCmp(ctx->builder, LLVMRealOGT, src0, zero, "");
2820 val = LLVMBuildSelect(ctx->builder, cmp, one, src0, "");
2821 cmp = LLVMBuildFCmp(ctx->builder, LLVMRealOGE, val, zero, "");
2822 val = LLVMBuildSelect(ctx->builder, cmp, val, LLVMConstReal(type, -1.0), "");
2823 return val;
2824 }
2825
2826 LLVMValueRef ac_build_bit_count(struct ac_llvm_context *ctx, LLVMValueRef src0)
2827 {
2828 LLVMValueRef result;
2829 unsigned bitsize;
2830
2831 bitsize = ac_get_elem_bits(ctx, LLVMTypeOf(src0));
2832
2833 switch (bitsize) {
2834 case 128:
2835 result = ac_build_intrinsic(ctx, "llvm.ctpop.i128", ctx->i128,
2836 (LLVMValueRef []) { src0 }, 1,
2837 AC_FUNC_ATTR_READNONE);
2838 result = LLVMBuildTrunc(ctx->builder, result, ctx->i32, "");
2839 break;
2840 case 64:
2841 result = ac_build_intrinsic(ctx, "llvm.ctpop.i64", ctx->i64,
2842 (LLVMValueRef []) { src0 }, 1,
2843 AC_FUNC_ATTR_READNONE);
2844
2845 result = LLVMBuildTrunc(ctx->builder, result, ctx->i32, "");
2846 break;
2847 case 32:
2848 result = ac_build_intrinsic(ctx, "llvm.ctpop.i32", ctx->i32,
2849 (LLVMValueRef []) { src0 }, 1,
2850 AC_FUNC_ATTR_READNONE);
2851 break;
2852 case 16:
2853 result = ac_build_intrinsic(ctx, "llvm.ctpop.i16", ctx->i16,
2854 (LLVMValueRef []) { src0 }, 1,
2855 AC_FUNC_ATTR_READNONE);
2856
2857 result = LLVMBuildZExt(ctx->builder, result, ctx->i32, "");
2858 break;
2859 case 8:
2860 result = ac_build_intrinsic(ctx, "llvm.ctpop.i8", ctx->i8,
2861 (LLVMValueRef []) { src0 }, 1,
2862 AC_FUNC_ATTR_READNONE);
2863
2864 result = LLVMBuildZExt(ctx->builder, result, ctx->i32, "");
2865 break;
2866 default:
2867 unreachable(!"invalid bitsize");
2868 break;
2869 }
2870
2871 return result;
2872 }
2873
2874 LLVMValueRef ac_build_bitfield_reverse(struct ac_llvm_context *ctx,
2875 LLVMValueRef src0)
2876 {
2877 LLVMValueRef result;
2878 unsigned bitsize;
2879
2880 bitsize = ac_get_elem_bits(ctx, LLVMTypeOf(src0));
2881
2882 switch (bitsize) {
2883 case 64:
2884 result = ac_build_intrinsic(ctx, "llvm.bitreverse.i64", ctx->i64,
2885 (LLVMValueRef []) { src0 }, 1,
2886 AC_FUNC_ATTR_READNONE);
2887
2888 result = LLVMBuildTrunc(ctx->builder, result, ctx->i32, "");
2889 break;
2890 case 32:
2891 result = ac_build_intrinsic(ctx, "llvm.bitreverse.i32", ctx->i32,
2892 (LLVMValueRef []) { src0 }, 1,
2893 AC_FUNC_ATTR_READNONE);
2894 break;
2895 case 16:
2896 result = ac_build_intrinsic(ctx, "llvm.bitreverse.i16", ctx->i16,
2897 (LLVMValueRef []) { src0 }, 1,
2898 AC_FUNC_ATTR_READNONE);
2899
2900 result = LLVMBuildZExt(ctx->builder, result, ctx->i32, "");
2901 break;
2902 case 8:
2903 result = ac_build_intrinsic(ctx, "llvm.bitreverse.i8", ctx->i8,
2904 (LLVMValueRef []) { src0 }, 1,
2905 AC_FUNC_ATTR_READNONE);
2906
2907 result = LLVMBuildZExt(ctx->builder, result, ctx->i32, "");
2908 break;
2909 default:
2910 unreachable(!"invalid bitsize");
2911 break;
2912 }
2913
2914 return result;
2915 }
2916
2917 #define AC_EXP_TARGET 0
2918 #define AC_EXP_ENABLED_CHANNELS 1
2919 #define AC_EXP_OUT0 2
2920
2921 enum ac_ir_type {
2922 AC_IR_UNDEF,
2923 AC_IR_CONST,
2924 AC_IR_VALUE,
2925 };
2926
2927 struct ac_vs_exp_chan
2928 {
2929 LLVMValueRef value;
2930 float const_float;
2931 enum ac_ir_type type;
2932 };
2933
2934 struct ac_vs_exp_inst {
2935 unsigned offset;
2936 LLVMValueRef inst;
2937 struct ac_vs_exp_chan chan[4];
2938 };
2939
2940 struct ac_vs_exports {
2941 unsigned num;
2942 struct ac_vs_exp_inst exp[VARYING_SLOT_MAX];
2943 };
2944
2945 /* Return true if the PARAM export has been eliminated. */
2946 static bool ac_eliminate_const_output(uint8_t *vs_output_param_offset,
2947 uint32_t num_outputs,
2948 struct ac_vs_exp_inst *exp)
2949 {
2950 unsigned i, default_val; /* SPI_PS_INPUT_CNTL_i.DEFAULT_VAL */
2951 bool is_zero[4] = {}, is_one[4] = {};
2952
2953 for (i = 0; i < 4; i++) {
2954 /* It's a constant expression. Undef outputs are eliminated too. */
2955 if (exp->chan[i].type == AC_IR_UNDEF) {
2956 is_zero[i] = true;
2957 is_one[i] = true;
2958 } else if (exp->chan[i].type == AC_IR_CONST) {
2959 if (exp->chan[i].const_float == 0)
2960 is_zero[i] = true;
2961 else if (exp->chan[i].const_float == 1)
2962 is_one[i] = true;
2963 else
2964 return false; /* other constant */
2965 } else
2966 return false;
2967 }
2968
2969 /* Only certain combinations of 0 and 1 can be eliminated. */
2970 if (is_zero[0] && is_zero[1] && is_zero[2])
2971 default_val = is_zero[3] ? 0 : 1;
2972 else if (is_one[0] && is_one[1] && is_one[2])
2973 default_val = is_zero[3] ? 2 : 3;
2974 else
2975 return false;
2976
2977 /* The PARAM export can be represented as DEFAULT_VAL. Kill it. */
2978 LLVMInstructionEraseFromParent(exp->inst);
2979
2980 /* Change OFFSET to DEFAULT_VAL. */
2981 for (i = 0; i < num_outputs; i++) {
2982 if (vs_output_param_offset[i] == exp->offset) {
2983 vs_output_param_offset[i] =
2984 AC_EXP_PARAM_DEFAULT_VAL_0000 + default_val;
2985 break;
2986 }
2987 }
2988 return true;
2989 }
2990
2991 static bool ac_eliminate_duplicated_output(struct ac_llvm_context *ctx,
2992 uint8_t *vs_output_param_offset,
2993 uint32_t num_outputs,
2994 struct ac_vs_exports *processed,
2995 struct ac_vs_exp_inst *exp)
2996 {
2997 unsigned p, copy_back_channels = 0;
2998
2999 /* See if the output is already in the list of processed outputs.
3000 * The LLVMValueRef comparison relies on SSA.
3001 */
3002 for (p = 0; p < processed->num; p++) {
3003 bool different = false;
3004
3005 for (unsigned j = 0; j < 4; j++) {
3006 struct ac_vs_exp_chan *c1 = &processed->exp[p].chan[j];
3007 struct ac_vs_exp_chan *c2 = &exp->chan[j];
3008
3009 /* Treat undef as a match. */
3010 if (c2->type == AC_IR_UNDEF)
3011 continue;
3012
3013 /* If c1 is undef but c2 isn't, we can copy c2 to c1
3014 * and consider the instruction duplicated.
3015 */
3016 if (c1->type == AC_IR_UNDEF) {
3017 copy_back_channels |= 1 << j;
3018 continue;
3019 }
3020
3021 /* Test whether the channels are not equal. */
3022 if (c1->type != c2->type ||
3023 (c1->type == AC_IR_CONST &&
3024 c1->const_float != c2->const_float) ||
3025 (c1->type == AC_IR_VALUE &&
3026 c1->value != c2->value)) {
3027 different = true;
3028 break;
3029 }
3030 }
3031 if (!different)
3032 break;
3033
3034 copy_back_channels = 0;
3035 }
3036 if (p == processed->num)
3037 return false;
3038
3039 /* If a match was found, but the matching export has undef where the new
3040 * one has a normal value, copy the normal value to the undef channel.
3041 */
3042 struct ac_vs_exp_inst *match = &processed->exp[p];
3043
3044 /* Get current enabled channels mask. */
3045 LLVMValueRef arg = LLVMGetOperand(match->inst, AC_EXP_ENABLED_CHANNELS);
3046 unsigned enabled_channels = LLVMConstIntGetZExtValue(arg);
3047
3048 while (copy_back_channels) {
3049 unsigned chan = u_bit_scan(&copy_back_channels);
3050
3051 assert(match->chan[chan].type == AC_IR_UNDEF);
3052 LLVMSetOperand(match->inst, AC_EXP_OUT0 + chan,
3053 exp->chan[chan].value);
3054 match->chan[chan] = exp->chan[chan];
3055
3056 /* Update number of enabled channels because the original mask
3057 * is not always 0xf.
3058 */
3059 enabled_channels |= (1 << chan);
3060 LLVMSetOperand(match->inst, AC_EXP_ENABLED_CHANNELS,
3061 LLVMConstInt(ctx->i32, enabled_channels, 0));
3062 }
3063
3064 /* The PARAM export is duplicated. Kill it. */
3065 LLVMInstructionEraseFromParent(exp->inst);
3066
3067 /* Change OFFSET to the matching export. */
3068 for (unsigned i = 0; i < num_outputs; i++) {
3069 if (vs_output_param_offset[i] == exp->offset) {
3070 vs_output_param_offset[i] = match->offset;
3071 break;
3072 }
3073 }
3074 return true;
3075 }
3076
3077 void ac_optimize_vs_outputs(struct ac_llvm_context *ctx,
3078 LLVMValueRef main_fn,
3079 uint8_t *vs_output_param_offset,
3080 uint32_t num_outputs,
3081 uint32_t skip_output_mask,
3082 uint8_t *num_param_exports)
3083 {
3084 LLVMBasicBlockRef bb;
3085 bool removed_any = false;
3086 struct ac_vs_exports exports;
3087
3088 exports.num = 0;
3089
3090 /* Process all LLVM instructions. */
3091 bb = LLVMGetFirstBasicBlock(main_fn);
3092 while (bb) {
3093 LLVMValueRef inst = LLVMGetFirstInstruction(bb);
3094
3095 while (inst) {
3096 LLVMValueRef cur = inst;
3097 inst = LLVMGetNextInstruction(inst);
3098 struct ac_vs_exp_inst exp;
3099
3100 if (LLVMGetInstructionOpcode(cur) != LLVMCall)
3101 continue;
3102
3103 LLVMValueRef callee = ac_llvm_get_called_value(cur);
3104
3105 if (!ac_llvm_is_function(callee))
3106 continue;
3107
3108 const char *name = LLVMGetValueName(callee);
3109 unsigned num_args = LLVMCountParams(callee);
3110
3111 /* Check if this is an export instruction. */
3112 if ((num_args != 9 && num_args != 8) ||
3113 (strcmp(name, "llvm.SI.export") &&
3114 strcmp(name, "llvm.amdgcn.exp.f32")))
3115 continue;
3116
3117 LLVMValueRef arg = LLVMGetOperand(cur, AC_EXP_TARGET);
3118 unsigned target = LLVMConstIntGetZExtValue(arg);
3119
3120 if (target < V_008DFC_SQ_EXP_PARAM)
3121 continue;
3122
3123 target -= V_008DFC_SQ_EXP_PARAM;
3124
3125 /* Parse the instruction. */
3126 memset(&exp, 0, sizeof(exp));
3127 exp.offset = target;
3128 exp.inst = cur;
3129
3130 for (unsigned i = 0; i < 4; i++) {
3131 LLVMValueRef v = LLVMGetOperand(cur, AC_EXP_OUT0 + i);
3132
3133 exp.chan[i].value = v;
3134
3135 if (LLVMIsUndef(v)) {
3136 exp.chan[i].type = AC_IR_UNDEF;
3137 } else if (LLVMIsAConstantFP(v)) {
3138 LLVMBool loses_info;
3139 exp.chan[i].type = AC_IR_CONST;
3140 exp.chan[i].const_float =
3141 LLVMConstRealGetDouble(v, &loses_info);
3142 } else {
3143 exp.chan[i].type = AC_IR_VALUE;
3144 }
3145 }
3146
3147 /* Eliminate constant and duplicated PARAM exports. */
3148 if (!((1u << target) & skip_output_mask) &&
3149 (ac_eliminate_const_output(vs_output_param_offset,
3150 num_outputs, &exp) ||
3151 ac_eliminate_duplicated_output(ctx,
3152 vs_output_param_offset,
3153 num_outputs, &exports,
3154 &exp))) {
3155 removed_any = true;
3156 } else {
3157 exports.exp[exports.num++] = exp;
3158 }
3159 }
3160 bb = LLVMGetNextBasicBlock(bb);
3161 }
3162
3163 /* Remove holes in export memory due to removed PARAM exports.
3164 * This is done by renumbering all PARAM exports.
3165 */
3166 if (removed_any) {
3167 uint8_t old_offset[VARYING_SLOT_MAX];
3168 unsigned out, i;
3169
3170 /* Make a copy of the offsets. We need the old version while
3171 * we are modifying some of them. */
3172 memcpy(old_offset, vs_output_param_offset,
3173 sizeof(old_offset));
3174
3175 for (i = 0; i < exports.num; i++) {
3176 unsigned offset = exports.exp[i].offset;
3177
3178 /* Update vs_output_param_offset. Multiple outputs can
3179 * have the same offset.
3180 */
3181 for (out = 0; out < num_outputs; out++) {
3182 if (old_offset[out] == offset)
3183 vs_output_param_offset[out] = i;
3184 }
3185
3186 /* Change the PARAM offset in the instruction. */
3187 LLVMSetOperand(exports.exp[i].inst, AC_EXP_TARGET,
3188 LLVMConstInt(ctx->i32,
3189 V_008DFC_SQ_EXP_PARAM + i, 0));
3190 }
3191 *num_param_exports = exports.num;
3192 }
3193 }
3194
3195 void ac_init_exec_full_mask(struct ac_llvm_context *ctx)
3196 {
3197 LLVMValueRef full_mask = LLVMConstInt(ctx->i64, ~0ull, 0);
3198 ac_build_intrinsic(ctx,
3199 "llvm.amdgcn.init.exec", ctx->voidt,
3200 &full_mask, 1, AC_FUNC_ATTR_CONVERGENT);
3201 }
3202
3203 void ac_declare_lds_as_pointer(struct ac_llvm_context *ctx)
3204 {
3205 unsigned lds_size = ctx->chip_class >= GFX7 ? 65536 : 32768;
3206 ctx->lds = LLVMBuildIntToPtr(ctx->builder, ctx->i32_0,
3207 LLVMPointerType(LLVMArrayType(ctx->i32, lds_size / 4), AC_ADDR_SPACE_LDS),
3208 "lds");
3209 }
3210
3211 LLVMValueRef ac_lds_load(struct ac_llvm_context *ctx,
3212 LLVMValueRef dw_addr)
3213 {
3214 return LLVMBuildLoad(ctx->builder, ac_build_gep0(ctx, ctx->lds, dw_addr), "");
3215 }
3216
3217 void ac_lds_store(struct ac_llvm_context *ctx,
3218 LLVMValueRef dw_addr,
3219 LLVMValueRef value)
3220 {
3221 value = ac_to_integer(ctx, value);
3222 ac_build_indexed_store(ctx, ctx->lds,
3223 dw_addr, value);
3224 }
3225
3226 LLVMValueRef ac_find_lsb(struct ac_llvm_context *ctx,
3227 LLVMTypeRef dst_type,
3228 LLVMValueRef src0)
3229 {
3230 unsigned src0_bitsize = ac_get_elem_bits(ctx, LLVMTypeOf(src0));
3231 const char *intrin_name;
3232 LLVMTypeRef type;
3233 LLVMValueRef zero;
3234
3235 switch (src0_bitsize) {
3236 case 64:
3237 intrin_name = "llvm.cttz.i64";
3238 type = ctx->i64;
3239 zero = ctx->i64_0;
3240 break;
3241 case 32:
3242 intrin_name = "llvm.cttz.i32";
3243 type = ctx->i32;
3244 zero = ctx->i32_0;
3245 break;
3246 case 16:
3247 intrin_name = "llvm.cttz.i16";
3248 type = ctx->i16;
3249 zero = ctx->i16_0;
3250 break;
3251 case 8:
3252 intrin_name = "llvm.cttz.i8";
3253 type = ctx->i8;
3254 zero = ctx->i8_0;
3255 break;
3256 default:
3257 unreachable(!"invalid bitsize");
3258 }
3259
3260 LLVMValueRef params[2] = {
3261 src0,
3262
3263 /* The value of 1 means that ffs(x=0) = undef, so LLVM won't
3264 * add special code to check for x=0. The reason is that
3265 * the LLVM behavior for x=0 is different from what we
3266 * need here. However, LLVM also assumes that ffs(x) is
3267 * in [0, 31], but GLSL expects that ffs(0) = -1, so
3268 * a conditional assignment to handle 0 is still required.
3269 *
3270 * The hardware already implements the correct behavior.
3271 */
3272 ctx->i1true,
3273 };
3274
3275 LLVMValueRef lsb = ac_build_intrinsic(ctx, intrin_name, type,
3276 params, 2,
3277 AC_FUNC_ATTR_READNONE);
3278
3279 if (src0_bitsize == 64) {
3280 lsb = LLVMBuildTrunc(ctx->builder, lsb, ctx->i32, "");
3281 } else if (src0_bitsize < 32) {
3282 lsb = LLVMBuildSExt(ctx->builder, lsb, ctx->i32, "");
3283 }
3284
3285 /* TODO: We need an intrinsic to skip this conditional. */
3286 /* Check for zero: */
3287 return LLVMBuildSelect(ctx->builder, LLVMBuildICmp(ctx->builder,
3288 LLVMIntEQ, src0,
3289 zero, ""),
3290 LLVMConstInt(ctx->i32, -1, 0), lsb, "");
3291 }
3292
3293 LLVMTypeRef ac_array_in_const_addr_space(LLVMTypeRef elem_type)
3294 {
3295 return LLVMPointerType(elem_type, AC_ADDR_SPACE_CONST);
3296 }
3297
3298 LLVMTypeRef ac_array_in_const32_addr_space(LLVMTypeRef elem_type)
3299 {
3300 return LLVMPointerType(elem_type, AC_ADDR_SPACE_CONST_32BIT);
3301 }
3302
3303 static struct ac_llvm_flow *
3304 get_current_flow(struct ac_llvm_context *ctx)
3305 {
3306 if (ctx->flow->depth > 0)
3307 return &ctx->flow->stack[ctx->flow->depth - 1];
3308 return NULL;
3309 }
3310
3311 static struct ac_llvm_flow *
3312 get_innermost_loop(struct ac_llvm_context *ctx)
3313 {
3314 for (unsigned i = ctx->flow->depth; i > 0; --i) {
3315 if (ctx->flow->stack[i - 1].loop_entry_block)
3316 return &ctx->flow->stack[i - 1];
3317 }
3318 return NULL;
3319 }
3320
3321 static struct ac_llvm_flow *
3322 push_flow(struct ac_llvm_context *ctx)
3323 {
3324 struct ac_llvm_flow *flow;
3325
3326 if (ctx->flow->depth >= ctx->flow->depth_max) {
3327 unsigned new_max = MAX2(ctx->flow->depth << 1,
3328 AC_LLVM_INITIAL_CF_DEPTH);
3329
3330 ctx->flow->stack = realloc(ctx->flow->stack, new_max * sizeof(*ctx->flow->stack));
3331 ctx->flow->depth_max = new_max;
3332 }
3333
3334 flow = &ctx->flow->stack[ctx->flow->depth];
3335 ctx->flow->depth++;
3336
3337 flow->next_block = NULL;
3338 flow->loop_entry_block = NULL;
3339 return flow;
3340 }
3341
3342 static void set_basicblock_name(LLVMBasicBlockRef bb, const char *base,
3343 int label_id)
3344 {
3345 char buf[32];
3346 snprintf(buf, sizeof(buf), "%s%d", base, label_id);
3347 LLVMSetValueName(LLVMBasicBlockAsValue(bb), buf);
3348 }
3349
3350 /* Append a basic block at the level of the parent flow.
3351 */
3352 static LLVMBasicBlockRef append_basic_block(struct ac_llvm_context *ctx,
3353 const char *name)
3354 {
3355 assert(ctx->flow->depth >= 1);
3356
3357 if (ctx->flow->depth >= 2) {
3358 struct ac_llvm_flow *flow = &ctx->flow->stack[ctx->flow->depth - 2];
3359
3360 return LLVMInsertBasicBlockInContext(ctx->context,
3361 flow->next_block, name);
3362 }
3363
3364 LLVMValueRef main_fn =
3365 LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx->builder));
3366 return LLVMAppendBasicBlockInContext(ctx->context, main_fn, name);
3367 }
3368
3369 /* Emit a branch to the given default target for the current block if
3370 * applicable -- that is, if the current block does not already contain a
3371 * branch from a break or continue.
3372 */
3373 static void emit_default_branch(LLVMBuilderRef builder,
3374 LLVMBasicBlockRef target)
3375 {
3376 if (!LLVMGetBasicBlockTerminator(LLVMGetInsertBlock(builder)))
3377 LLVMBuildBr(builder, target);
3378 }
3379
3380 void ac_build_bgnloop(struct ac_llvm_context *ctx, int label_id)
3381 {
3382 struct ac_llvm_flow *flow = push_flow(ctx);
3383 flow->loop_entry_block = append_basic_block(ctx, "LOOP");
3384 flow->next_block = append_basic_block(ctx, "ENDLOOP");
3385 set_basicblock_name(flow->loop_entry_block, "loop", label_id);
3386 LLVMBuildBr(ctx->builder, flow->loop_entry_block);
3387 LLVMPositionBuilderAtEnd(ctx->builder, flow->loop_entry_block);
3388 }
3389
3390 void ac_build_break(struct ac_llvm_context *ctx)
3391 {
3392 struct ac_llvm_flow *flow = get_innermost_loop(ctx);
3393 LLVMBuildBr(ctx->builder, flow->next_block);
3394 }
3395
3396 void ac_build_continue(struct ac_llvm_context *ctx)
3397 {
3398 struct ac_llvm_flow *flow = get_innermost_loop(ctx);
3399 LLVMBuildBr(ctx->builder, flow->loop_entry_block);
3400 }
3401
3402 void ac_build_else(struct ac_llvm_context *ctx, int label_id)
3403 {
3404 struct ac_llvm_flow *current_branch = get_current_flow(ctx);
3405 LLVMBasicBlockRef endif_block;
3406
3407 assert(!current_branch->loop_entry_block);
3408
3409 endif_block = append_basic_block(ctx, "ENDIF");
3410 emit_default_branch(ctx->builder, endif_block);
3411
3412 LLVMPositionBuilderAtEnd(ctx->builder, current_branch->next_block);
3413 set_basicblock_name(current_branch->next_block, "else", label_id);
3414
3415 current_branch->next_block = endif_block;
3416 }
3417
3418 void ac_build_endif(struct ac_llvm_context *ctx, int label_id)
3419 {
3420 struct ac_llvm_flow *current_branch = get_current_flow(ctx);
3421
3422 assert(!current_branch->loop_entry_block);
3423
3424 emit_default_branch(ctx->builder, current_branch->next_block);
3425 LLVMPositionBuilderAtEnd(ctx->builder, current_branch->next_block);
3426 set_basicblock_name(current_branch->next_block, "endif", label_id);
3427
3428 ctx->flow->depth--;
3429 }
3430
3431 void ac_build_endloop(struct ac_llvm_context *ctx, int label_id)
3432 {
3433 struct ac_llvm_flow *current_loop = get_current_flow(ctx);
3434
3435 assert(current_loop->loop_entry_block);
3436
3437 emit_default_branch(ctx->builder