ac: add ac_build_export_null() helper
[mesa.git] / src / amd / common / ac_llvm_build.c
1 /*
2 * Copyright 2014 Advanced Micro Devices, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sub license, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
15 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
17 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
18 * USE OR OTHER DEALINGS IN THE SOFTWARE.
19 *
20 * The above copyright notice and this permission notice (including the
21 * next paragraph) shall be included in all copies or substantial portions
22 * of the Software.
23 *
24 */
25 /* based on pieces from si_pipe.c and radeon_llvm_emit.c */
26 #include "ac_llvm_build.h"
27
28 #include <llvm-c/Core.h>
29
30 #include "c11/threads.h"
31
32 #include <assert.h>
33 #include <stdio.h>
34
35 #include "ac_llvm_util.h"
36 #include "ac_exp_param.h"
37 #include "util/bitscan.h"
38 #include "util/macros.h"
39 #include "util/u_atomic.h"
40 #include "sid.h"
41
42 #include "shader_enums.h"
43
44 /* Initialize module-independent parts of the context.
45 *
46 * The caller is responsible for initializing ctx::module and ctx::builder.
47 */
48 void
49 ac_llvm_context_init(struct ac_llvm_context *ctx, LLVMContextRef context,
50 enum chip_class chip_class, enum radeon_family family)
51 {
52 LLVMValueRef args[1];
53
54 ctx->chip_class = chip_class;
55 ctx->family = family;
56
57 ctx->context = context;
58 ctx->module = NULL;
59 ctx->builder = NULL;
60
61 ctx->voidt = LLVMVoidTypeInContext(ctx->context);
62 ctx->i1 = LLVMInt1TypeInContext(ctx->context);
63 ctx->i8 = LLVMInt8TypeInContext(ctx->context);
64 ctx->i16 = LLVMIntTypeInContext(ctx->context, 16);
65 ctx->i32 = LLVMIntTypeInContext(ctx->context, 32);
66 ctx->i64 = LLVMIntTypeInContext(ctx->context, 64);
67 ctx->f16 = LLVMHalfTypeInContext(ctx->context);
68 ctx->f32 = LLVMFloatTypeInContext(ctx->context);
69 ctx->f64 = LLVMDoubleTypeInContext(ctx->context);
70 ctx->v2i16 = LLVMVectorType(ctx->i16, 2);
71 ctx->v2i32 = LLVMVectorType(ctx->i32, 2);
72 ctx->v3i32 = LLVMVectorType(ctx->i32, 3);
73 ctx->v4i32 = LLVMVectorType(ctx->i32, 4);
74 ctx->v2f32 = LLVMVectorType(ctx->f32, 2);
75 ctx->v4f32 = LLVMVectorType(ctx->f32, 4);
76 ctx->v8i32 = LLVMVectorType(ctx->i32, 8);
77
78 ctx->i32_0 = LLVMConstInt(ctx->i32, 0, false);
79 ctx->i32_1 = LLVMConstInt(ctx->i32, 1, false);
80 ctx->i64_0 = LLVMConstInt(ctx->i64, 0, false);
81 ctx->i64_1 = LLVMConstInt(ctx->i64, 1, false);
82 ctx->f32_0 = LLVMConstReal(ctx->f32, 0.0);
83 ctx->f32_1 = LLVMConstReal(ctx->f32, 1.0);
84 ctx->f64_0 = LLVMConstReal(ctx->f64, 0.0);
85 ctx->f64_1 = LLVMConstReal(ctx->f64, 1.0);
86
87 ctx->i1false = LLVMConstInt(ctx->i1, 0, false);
88 ctx->i1true = LLVMConstInt(ctx->i1, 1, false);
89
90 ctx->range_md_kind = LLVMGetMDKindIDInContext(ctx->context,
91 "range", 5);
92
93 ctx->invariant_load_md_kind = LLVMGetMDKindIDInContext(ctx->context,
94 "invariant.load", 14);
95
96 ctx->fpmath_md_kind = LLVMGetMDKindIDInContext(ctx->context, "fpmath", 6);
97
98 args[0] = LLVMConstReal(ctx->f32, 2.5);
99 ctx->fpmath_md_2p5_ulp = LLVMMDNodeInContext(ctx->context, args, 1);
100
101 ctx->uniform_md_kind = LLVMGetMDKindIDInContext(ctx->context,
102 "amdgpu.uniform", 14);
103
104 ctx->empty_md = LLVMMDNodeInContext(ctx->context, NULL, 0);
105 }
106
107 int
108 ac_get_llvm_num_components(LLVMValueRef value)
109 {
110 LLVMTypeRef type = LLVMTypeOf(value);
111 unsigned num_components = LLVMGetTypeKind(type) == LLVMVectorTypeKind
112 ? LLVMGetVectorSize(type)
113 : 1;
114 return num_components;
115 }
116
117 LLVMValueRef
118 ac_llvm_extract_elem(struct ac_llvm_context *ac,
119 LLVMValueRef value,
120 int index)
121 {
122 if (LLVMGetTypeKind(LLVMTypeOf(value)) != LLVMVectorTypeKind) {
123 assert(index == 0);
124 return value;
125 }
126
127 return LLVMBuildExtractElement(ac->builder, value,
128 LLVMConstInt(ac->i32, index, false), "");
129 }
130
131 unsigned
132 ac_get_type_size(LLVMTypeRef type)
133 {
134 LLVMTypeKind kind = LLVMGetTypeKind(type);
135
136 switch (kind) {
137 case LLVMIntegerTypeKind:
138 return LLVMGetIntTypeWidth(type) / 8;
139 case LLVMFloatTypeKind:
140 return 4;
141 case LLVMDoubleTypeKind:
142 case LLVMPointerTypeKind:
143 return 8;
144 case LLVMVectorTypeKind:
145 return LLVMGetVectorSize(type) *
146 ac_get_type_size(LLVMGetElementType(type));
147 case LLVMArrayTypeKind:
148 return LLVMGetArrayLength(type) *
149 ac_get_type_size(LLVMGetElementType(type));
150 default:
151 assert(0);
152 return 0;
153 }
154 }
155
156 static LLVMTypeRef to_integer_type_scalar(struct ac_llvm_context *ctx, LLVMTypeRef t)
157 {
158 if (t == ctx->f16 || t == ctx->i16)
159 return ctx->i16;
160 else if (t == ctx->f32 || t == ctx->i32)
161 return ctx->i32;
162 else if (t == ctx->f64 || t == ctx->i64)
163 return ctx->i64;
164 else
165 unreachable("Unhandled integer size");
166 }
167
168 LLVMTypeRef
169 ac_to_integer_type(struct ac_llvm_context *ctx, LLVMTypeRef t)
170 {
171 if (LLVMGetTypeKind(t) == LLVMVectorTypeKind) {
172 LLVMTypeRef elem_type = LLVMGetElementType(t);
173 return LLVMVectorType(to_integer_type_scalar(ctx, elem_type),
174 LLVMGetVectorSize(t));
175 }
176 return to_integer_type_scalar(ctx, t);
177 }
178
179 LLVMValueRef
180 ac_to_integer(struct ac_llvm_context *ctx, LLVMValueRef v)
181 {
182 LLVMTypeRef type = LLVMTypeOf(v);
183 return LLVMBuildBitCast(ctx->builder, v, ac_to_integer_type(ctx, type), "");
184 }
185
186 static LLVMTypeRef to_float_type_scalar(struct ac_llvm_context *ctx, LLVMTypeRef t)
187 {
188 if (t == ctx->i16 || t == ctx->f16)
189 return ctx->f16;
190 else if (t == ctx->i32 || t == ctx->f32)
191 return ctx->f32;
192 else if (t == ctx->i64 || t == ctx->f64)
193 return ctx->f64;
194 else
195 unreachable("Unhandled float size");
196 }
197
198 LLVMTypeRef
199 ac_to_float_type(struct ac_llvm_context *ctx, LLVMTypeRef t)
200 {
201 if (LLVMGetTypeKind(t) == LLVMVectorTypeKind) {
202 LLVMTypeRef elem_type = LLVMGetElementType(t);
203 return LLVMVectorType(to_float_type_scalar(ctx, elem_type),
204 LLVMGetVectorSize(t));
205 }
206 return to_float_type_scalar(ctx, t);
207 }
208
209 LLVMValueRef
210 ac_to_float(struct ac_llvm_context *ctx, LLVMValueRef v)
211 {
212 LLVMTypeRef type = LLVMTypeOf(v);
213 return LLVMBuildBitCast(ctx->builder, v, ac_to_float_type(ctx, type), "");
214 }
215
216
217 LLVMValueRef
218 ac_build_intrinsic(struct ac_llvm_context *ctx, const char *name,
219 LLVMTypeRef return_type, LLVMValueRef *params,
220 unsigned param_count, unsigned attrib_mask)
221 {
222 LLVMValueRef function, call;
223 bool set_callsite_attrs = !(attrib_mask & AC_FUNC_ATTR_LEGACY);
224
225 function = LLVMGetNamedFunction(ctx->module, name);
226 if (!function) {
227 LLVMTypeRef param_types[32], function_type;
228 unsigned i;
229
230 assert(param_count <= 32);
231
232 for (i = 0; i < param_count; ++i) {
233 assert(params[i]);
234 param_types[i] = LLVMTypeOf(params[i]);
235 }
236 function_type =
237 LLVMFunctionType(return_type, param_types, param_count, 0);
238 function = LLVMAddFunction(ctx->module, name, function_type);
239
240 LLVMSetFunctionCallConv(function, LLVMCCallConv);
241 LLVMSetLinkage(function, LLVMExternalLinkage);
242
243 if (!set_callsite_attrs)
244 ac_add_func_attributes(ctx->context, function, attrib_mask);
245 }
246
247 call = LLVMBuildCall(ctx->builder, function, params, param_count, "");
248 if (set_callsite_attrs)
249 ac_add_func_attributes(ctx->context, call, attrib_mask);
250 return call;
251 }
252
253 /**
254 * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
255 * intrinsic names).
256 */
257 void ac_build_type_name_for_intr(LLVMTypeRef type, char *buf, unsigned bufsize)
258 {
259 LLVMTypeRef elem_type = type;
260
261 assert(bufsize >= 8);
262
263 if (LLVMGetTypeKind(type) == LLVMVectorTypeKind) {
264 int ret = snprintf(buf, bufsize, "v%u",
265 LLVMGetVectorSize(type));
266 if (ret < 0) {
267 char *type_name = LLVMPrintTypeToString(type);
268 fprintf(stderr, "Error building type name for: %s\n",
269 type_name);
270 return;
271 }
272 elem_type = LLVMGetElementType(type);
273 buf += ret;
274 bufsize -= ret;
275 }
276 switch (LLVMGetTypeKind(elem_type)) {
277 default: break;
278 case LLVMIntegerTypeKind:
279 snprintf(buf, bufsize, "i%d", LLVMGetIntTypeWidth(elem_type));
280 break;
281 case LLVMFloatTypeKind:
282 snprintf(buf, bufsize, "f32");
283 break;
284 case LLVMDoubleTypeKind:
285 snprintf(buf, bufsize, "f64");
286 break;
287 }
288 }
289
290 /**
291 * Helper function that builds an LLVM IR PHI node and immediately adds
292 * incoming edges.
293 */
294 LLVMValueRef
295 ac_build_phi(struct ac_llvm_context *ctx, LLVMTypeRef type,
296 unsigned count_incoming, LLVMValueRef *values,
297 LLVMBasicBlockRef *blocks)
298 {
299 LLVMValueRef phi = LLVMBuildPhi(ctx->builder, type, "");
300 LLVMAddIncoming(phi, values, blocks, count_incoming);
301 return phi;
302 }
303
304 /* Prevent optimizations (at least of memory accesses) across the current
305 * point in the program by emitting empty inline assembly that is marked as
306 * having side effects.
307 *
308 * Optionally, a value can be passed through the inline assembly to prevent
309 * LLVM from hoisting calls to ReadNone functions.
310 */
311 void
312 ac_build_optimization_barrier(struct ac_llvm_context *ctx,
313 LLVMValueRef *pvgpr)
314 {
315 static int counter = 0;
316
317 LLVMBuilderRef builder = ctx->builder;
318 char code[16];
319
320 snprintf(code, sizeof(code), "; %d", p_atomic_inc_return(&counter));
321
322 if (!pvgpr) {
323 LLVMTypeRef ftype = LLVMFunctionType(ctx->voidt, NULL, 0, false);
324 LLVMValueRef inlineasm = LLVMConstInlineAsm(ftype, code, "", true, false);
325 LLVMBuildCall(builder, inlineasm, NULL, 0, "");
326 } else {
327 LLVMTypeRef ftype = LLVMFunctionType(ctx->i32, &ctx->i32, 1, false);
328 LLVMValueRef inlineasm = LLVMConstInlineAsm(ftype, code, "=v,0", true, false);
329 LLVMValueRef vgpr = *pvgpr;
330 LLVMTypeRef vgpr_type = LLVMTypeOf(vgpr);
331 unsigned vgpr_size = ac_get_type_size(vgpr_type);
332 LLVMValueRef vgpr0;
333
334 assert(vgpr_size % 4 == 0);
335
336 vgpr = LLVMBuildBitCast(builder, vgpr, LLVMVectorType(ctx->i32, vgpr_size / 4), "");
337 vgpr0 = LLVMBuildExtractElement(builder, vgpr, ctx->i32_0, "");
338 vgpr0 = LLVMBuildCall(builder, inlineasm, &vgpr0, 1, "");
339 vgpr = LLVMBuildInsertElement(builder, vgpr, vgpr0, ctx->i32_0, "");
340 vgpr = LLVMBuildBitCast(builder, vgpr, vgpr_type, "");
341
342 *pvgpr = vgpr;
343 }
344 }
345
346 LLVMValueRef
347 ac_build_shader_clock(struct ac_llvm_context *ctx)
348 {
349 LLVMValueRef tmp = ac_build_intrinsic(ctx, "llvm.readcyclecounter",
350 ctx->i64, NULL, 0, 0);
351 return LLVMBuildBitCast(ctx->builder, tmp, ctx->v2i32, "");
352 }
353
354 LLVMValueRef
355 ac_build_ballot(struct ac_llvm_context *ctx,
356 LLVMValueRef value)
357 {
358 LLVMValueRef args[3] = {
359 value,
360 ctx->i32_0,
361 LLVMConstInt(ctx->i32, LLVMIntNE, 0)
362 };
363
364 /* We currently have no other way to prevent LLVM from lifting the icmp
365 * calls to a dominating basic block.
366 */
367 ac_build_optimization_barrier(ctx, &args[0]);
368
369 if (LLVMTypeOf(args[0]) != ctx->i32)
370 args[0] = LLVMBuildBitCast(ctx->builder, args[0], ctx->i32, "");
371
372 return ac_build_intrinsic(ctx,
373 "llvm.amdgcn.icmp.i32",
374 ctx->i64, args, 3,
375 AC_FUNC_ATTR_NOUNWIND |
376 AC_FUNC_ATTR_READNONE |
377 AC_FUNC_ATTR_CONVERGENT);
378 }
379
380 LLVMValueRef
381 ac_build_vote_all(struct ac_llvm_context *ctx, LLVMValueRef value)
382 {
383 LLVMValueRef active_set = ac_build_ballot(ctx, ctx->i32_1);
384 LLVMValueRef vote_set = ac_build_ballot(ctx, value);
385 return LLVMBuildICmp(ctx->builder, LLVMIntEQ, vote_set, active_set, "");
386 }
387
388 LLVMValueRef
389 ac_build_vote_any(struct ac_llvm_context *ctx, LLVMValueRef value)
390 {
391 LLVMValueRef vote_set = ac_build_ballot(ctx, value);
392 return LLVMBuildICmp(ctx->builder, LLVMIntNE, vote_set,
393 LLVMConstInt(ctx->i64, 0, 0), "");
394 }
395
396 LLVMValueRef
397 ac_build_vote_eq(struct ac_llvm_context *ctx, LLVMValueRef value)
398 {
399 LLVMValueRef active_set = ac_build_ballot(ctx, ctx->i32_1);
400 LLVMValueRef vote_set = ac_build_ballot(ctx, value);
401
402 LLVMValueRef all = LLVMBuildICmp(ctx->builder, LLVMIntEQ,
403 vote_set, active_set, "");
404 LLVMValueRef none = LLVMBuildICmp(ctx->builder, LLVMIntEQ,
405 vote_set,
406 LLVMConstInt(ctx->i64, 0, 0), "");
407 return LLVMBuildOr(ctx->builder, all, none, "");
408 }
409
410 LLVMValueRef
411 ac_build_varying_gather_values(struct ac_llvm_context *ctx, LLVMValueRef *values,
412 unsigned value_count, unsigned component)
413 {
414 LLVMValueRef vec = NULL;
415
416 if (value_count == 1) {
417 return values[component];
418 } else if (!value_count)
419 unreachable("value_count is 0");
420
421 for (unsigned i = component; i < value_count + component; i++) {
422 LLVMValueRef value = values[i];
423
424 if (i == component)
425 vec = LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value), value_count));
426 LLVMValueRef index = LLVMConstInt(ctx->i32, i - component, false);
427 vec = LLVMBuildInsertElement(ctx->builder, vec, value, index, "");
428 }
429 return vec;
430 }
431
432 LLVMValueRef
433 ac_build_gather_values_extended(struct ac_llvm_context *ctx,
434 LLVMValueRef *values,
435 unsigned value_count,
436 unsigned value_stride,
437 bool load,
438 bool always_vector)
439 {
440 LLVMBuilderRef builder = ctx->builder;
441 LLVMValueRef vec = NULL;
442 unsigned i;
443
444 if (value_count == 1 && !always_vector) {
445 if (load)
446 return LLVMBuildLoad(builder, values[0], "");
447 return values[0];
448 } else if (!value_count)
449 unreachable("value_count is 0");
450
451 for (i = 0; i < value_count; i++) {
452 LLVMValueRef value = values[i * value_stride];
453 if (load)
454 value = LLVMBuildLoad(builder, value, "");
455
456 if (!i)
457 vec = LLVMGetUndef( LLVMVectorType(LLVMTypeOf(value), value_count));
458 LLVMValueRef index = LLVMConstInt(ctx->i32, i, false);
459 vec = LLVMBuildInsertElement(builder, vec, value, index, "");
460 }
461 return vec;
462 }
463
464 LLVMValueRef
465 ac_build_gather_values(struct ac_llvm_context *ctx,
466 LLVMValueRef *values,
467 unsigned value_count)
468 {
469 return ac_build_gather_values_extended(ctx, values, value_count, 1, false, false);
470 }
471
472 /* Expand a scalar or vector to <4 x type> by filling the remaining channels
473 * with undef. Extract at most num_channels components from the input.
474 */
475 LLVMValueRef ac_build_expand_to_vec4(struct ac_llvm_context *ctx,
476 LLVMValueRef value,
477 unsigned num_channels)
478 {
479 LLVMTypeRef elemtype;
480 LLVMValueRef chan[4];
481
482 if (LLVMGetTypeKind(LLVMTypeOf(value)) == LLVMVectorTypeKind) {
483 unsigned vec_size = LLVMGetVectorSize(LLVMTypeOf(value));
484 num_channels = MIN2(num_channels, vec_size);
485
486 if (num_channels >= 4)
487 return value;
488
489 for (unsigned i = 0; i < num_channels; i++)
490 chan[i] = ac_llvm_extract_elem(ctx, value, i);
491
492 elemtype = LLVMGetElementType(LLVMTypeOf(value));
493 } else {
494 if (num_channels) {
495 assert(num_channels == 1);
496 chan[0] = value;
497 }
498 elemtype = LLVMTypeOf(value);
499 }
500
501 while (num_channels < 4)
502 chan[num_channels++] = LLVMGetUndef(elemtype);
503
504 return ac_build_gather_values(ctx, chan, 4);
505 }
506
507 LLVMValueRef
508 ac_build_fdiv(struct ac_llvm_context *ctx,
509 LLVMValueRef num,
510 LLVMValueRef den)
511 {
512 LLVMValueRef ret = LLVMBuildFDiv(ctx->builder, num, den, "");
513
514 /* Use v_rcp_f32 instead of precise division. */
515 if (!LLVMIsConstant(ret))
516 LLVMSetMetadata(ret, ctx->fpmath_md_kind, ctx->fpmath_md_2p5_ulp);
517 return ret;
518 }
519
520 /* Coordinates for cube map selection. sc, tc, and ma are as in Table 8.27
521 * of the OpenGL 4.5 (Compatibility Profile) specification, except ma is
522 * already multiplied by two. id is the cube face number.
523 */
524 struct cube_selection_coords {
525 LLVMValueRef stc[2];
526 LLVMValueRef ma;
527 LLVMValueRef id;
528 };
529
530 static void
531 build_cube_intrinsic(struct ac_llvm_context *ctx,
532 LLVMValueRef in[3],
533 struct cube_selection_coords *out)
534 {
535 LLVMTypeRef f32 = ctx->f32;
536
537 out->stc[1] = ac_build_intrinsic(ctx, "llvm.amdgcn.cubetc",
538 f32, in, 3, AC_FUNC_ATTR_READNONE);
539 out->stc[0] = ac_build_intrinsic(ctx, "llvm.amdgcn.cubesc",
540 f32, in, 3, AC_FUNC_ATTR_READNONE);
541 out->ma = ac_build_intrinsic(ctx, "llvm.amdgcn.cubema",
542 f32, in, 3, AC_FUNC_ATTR_READNONE);
543 out->id = ac_build_intrinsic(ctx, "llvm.amdgcn.cubeid",
544 f32, in, 3, AC_FUNC_ATTR_READNONE);
545 }
546
547 /**
548 * Build a manual selection sequence for cube face sc/tc coordinates and
549 * major axis vector (multiplied by 2 for consistency) for the given
550 * vec3 \p coords, for the face implied by \p selcoords.
551 *
552 * For the major axis, we always adjust the sign to be in the direction of
553 * selcoords.ma; i.e., a positive out_ma means that coords is pointed towards
554 * the selcoords major axis.
555 */
556 static void build_cube_select(struct ac_llvm_context *ctx,
557 const struct cube_selection_coords *selcoords,
558 const LLVMValueRef *coords,
559 LLVMValueRef *out_st,
560 LLVMValueRef *out_ma)
561 {
562 LLVMBuilderRef builder = ctx->builder;
563 LLVMTypeRef f32 = LLVMTypeOf(coords[0]);
564 LLVMValueRef is_ma_positive;
565 LLVMValueRef sgn_ma;
566 LLVMValueRef is_ma_z, is_not_ma_z;
567 LLVMValueRef is_ma_y;
568 LLVMValueRef is_ma_x;
569 LLVMValueRef sgn;
570 LLVMValueRef tmp;
571
572 is_ma_positive = LLVMBuildFCmp(builder, LLVMRealUGE,
573 selcoords->ma, LLVMConstReal(f32, 0.0), "");
574 sgn_ma = LLVMBuildSelect(builder, is_ma_positive,
575 LLVMConstReal(f32, 1.0), LLVMConstReal(f32, -1.0), "");
576
577 is_ma_z = LLVMBuildFCmp(builder, LLVMRealUGE, selcoords->id, LLVMConstReal(f32, 4.0), "");
578 is_not_ma_z = LLVMBuildNot(builder, is_ma_z, "");
579 is_ma_y = LLVMBuildAnd(builder, is_not_ma_z,
580 LLVMBuildFCmp(builder, LLVMRealUGE, selcoords->id, LLVMConstReal(f32, 2.0), ""), "");
581 is_ma_x = LLVMBuildAnd(builder, is_not_ma_z, LLVMBuildNot(builder, is_ma_y, ""), "");
582
583 /* Select sc */
584 tmp = LLVMBuildSelect(builder, is_ma_x, coords[2], coords[0], "");
585 sgn = LLVMBuildSelect(builder, is_ma_y, LLVMConstReal(f32, 1.0),
586 LLVMBuildSelect(builder, is_ma_z, sgn_ma,
587 LLVMBuildFNeg(builder, sgn_ma, ""), ""), "");
588 out_st[0] = LLVMBuildFMul(builder, tmp, sgn, "");
589
590 /* Select tc */
591 tmp = LLVMBuildSelect(builder, is_ma_y, coords[2], coords[1], "");
592 sgn = LLVMBuildSelect(builder, is_ma_y, sgn_ma,
593 LLVMConstReal(f32, -1.0), "");
594 out_st[1] = LLVMBuildFMul(builder, tmp, sgn, "");
595
596 /* Select ma */
597 tmp = LLVMBuildSelect(builder, is_ma_z, coords[2],
598 LLVMBuildSelect(builder, is_ma_y, coords[1], coords[0], ""), "");
599 tmp = ac_build_intrinsic(ctx, "llvm.fabs.f32",
600 ctx->f32, &tmp, 1, AC_FUNC_ATTR_READNONE);
601 *out_ma = LLVMBuildFMul(builder, tmp, LLVMConstReal(f32, 2.0), "");
602 }
603
604 void
605 ac_prepare_cube_coords(struct ac_llvm_context *ctx,
606 bool is_deriv, bool is_array, bool is_lod,
607 LLVMValueRef *coords_arg,
608 LLVMValueRef *derivs_arg)
609 {
610
611 LLVMBuilderRef builder = ctx->builder;
612 struct cube_selection_coords selcoords;
613 LLVMValueRef coords[3];
614 LLVMValueRef invma;
615
616 if (is_array && !is_lod) {
617 LLVMValueRef tmp = coords_arg[3];
618 tmp = ac_build_intrinsic(ctx, "llvm.rint.f32", ctx->f32, &tmp, 1, 0);
619
620 /* Section 8.9 (Texture Functions) of the GLSL 4.50 spec says:
621 *
622 * "For Array forms, the array layer used will be
623 *
624 * max(0, min(d−1, floor(layer+0.5)))
625 *
626 * where d is the depth of the texture array and layer
627 * comes from the component indicated in the tables below.
628 * Workaroudn for an issue where the layer is taken from a
629 * helper invocation which happens to fall on a different
630 * layer due to extrapolation."
631 *
632 * VI and earlier attempt to implement this in hardware by
633 * clamping the value of coords[2] = (8 * layer) + face.
634 * Unfortunately, this means that the we end up with the wrong
635 * face when clamping occurs.
636 *
637 * Clamp the layer earlier to work around the issue.
638 */
639 if (ctx->chip_class <= VI) {
640 LLVMValueRef ge0;
641 ge0 = LLVMBuildFCmp(builder, LLVMRealOGE, tmp, ctx->f32_0, "");
642 tmp = LLVMBuildSelect(builder, ge0, tmp, ctx->f32_0, "");
643 }
644
645 coords_arg[3] = tmp;
646 }
647
648 build_cube_intrinsic(ctx, coords_arg, &selcoords);
649
650 invma = ac_build_intrinsic(ctx, "llvm.fabs.f32",
651 ctx->f32, &selcoords.ma, 1, AC_FUNC_ATTR_READNONE);
652 invma = ac_build_fdiv(ctx, LLVMConstReal(ctx->f32, 1.0), invma);
653
654 for (int i = 0; i < 2; ++i)
655 coords[i] = LLVMBuildFMul(builder, selcoords.stc[i], invma, "");
656
657 coords[2] = selcoords.id;
658
659 if (is_deriv && derivs_arg) {
660 LLVMValueRef derivs[4];
661 int axis;
662
663 /* Convert cube derivatives to 2D derivatives. */
664 for (axis = 0; axis < 2; axis++) {
665 LLVMValueRef deriv_st[2];
666 LLVMValueRef deriv_ma;
667
668 /* Transform the derivative alongside the texture
669 * coordinate. Mathematically, the correct formula is
670 * as follows. Assume we're projecting onto the +Z face
671 * and denote by dx/dh the derivative of the (original)
672 * X texture coordinate with respect to horizontal
673 * window coordinates. The projection onto the +Z face
674 * plane is:
675 *
676 * f(x,z) = x/z
677 *
678 * Then df/dh = df/dx * dx/dh + df/dz * dz/dh
679 * = 1/z * dx/dh - x/z * 1/z * dz/dh.
680 *
681 * This motivatives the implementation below.
682 *
683 * Whether this actually gives the expected results for
684 * apps that might feed in derivatives obtained via
685 * finite differences is anyone's guess. The OpenGL spec
686 * seems awfully quiet about how textureGrad for cube
687 * maps should be handled.
688 */
689 build_cube_select(ctx, &selcoords, &derivs_arg[axis * 3],
690 deriv_st, &deriv_ma);
691
692 deriv_ma = LLVMBuildFMul(builder, deriv_ma, invma, "");
693
694 for (int i = 0; i < 2; ++i)
695 derivs[axis * 2 + i] =
696 LLVMBuildFSub(builder,
697 LLVMBuildFMul(builder, deriv_st[i], invma, ""),
698 LLVMBuildFMul(builder, deriv_ma, coords[i], ""), "");
699 }
700
701 memcpy(derivs_arg, derivs, sizeof(derivs));
702 }
703
704 /* Shift the texture coordinate. This must be applied after the
705 * derivative calculation.
706 */
707 for (int i = 0; i < 2; ++i)
708 coords[i] = LLVMBuildFAdd(builder, coords[i], LLVMConstReal(ctx->f32, 1.5), "");
709
710 if (is_array) {
711 /* for cube arrays coord.z = coord.w(array_index) * 8 + face */
712 /* coords_arg.w component - array_index for cube arrays */
713 LLVMValueRef tmp = LLVMBuildFMul(ctx->builder, coords_arg[3], LLVMConstReal(ctx->f32, 8.0), "");
714 coords[2] = LLVMBuildFAdd(ctx->builder, tmp, coords[2], "");
715 }
716
717 memcpy(coords_arg, coords, sizeof(coords));
718 }
719
720
721 LLVMValueRef
722 ac_build_fs_interp(struct ac_llvm_context *ctx,
723 LLVMValueRef llvm_chan,
724 LLVMValueRef attr_number,
725 LLVMValueRef params,
726 LLVMValueRef i,
727 LLVMValueRef j)
728 {
729 LLVMValueRef args[5];
730 LLVMValueRef p1;
731
732 args[0] = i;
733 args[1] = llvm_chan;
734 args[2] = attr_number;
735 args[3] = params;
736
737 p1 = ac_build_intrinsic(ctx, "llvm.amdgcn.interp.p1",
738 ctx->f32, args, 4, AC_FUNC_ATTR_READNONE);
739
740 args[0] = p1;
741 args[1] = j;
742 args[2] = llvm_chan;
743 args[3] = attr_number;
744 args[4] = params;
745
746 return ac_build_intrinsic(ctx, "llvm.amdgcn.interp.p2",
747 ctx->f32, args, 5, AC_FUNC_ATTR_READNONE);
748 }
749
750 LLVMValueRef
751 ac_build_fs_interp_mov(struct ac_llvm_context *ctx,
752 LLVMValueRef parameter,
753 LLVMValueRef llvm_chan,
754 LLVMValueRef attr_number,
755 LLVMValueRef params)
756 {
757 LLVMValueRef args[4];
758
759 args[0] = parameter;
760 args[1] = llvm_chan;
761 args[2] = attr_number;
762 args[3] = params;
763
764 return ac_build_intrinsic(ctx, "llvm.amdgcn.interp.mov",
765 ctx->f32, args, 4, AC_FUNC_ATTR_READNONE);
766 }
767
768 LLVMValueRef
769 ac_build_gep0(struct ac_llvm_context *ctx,
770 LLVMValueRef base_ptr,
771 LLVMValueRef index)
772 {
773 LLVMValueRef indices[2] = {
774 LLVMConstInt(ctx->i32, 0, 0),
775 index,
776 };
777 return LLVMBuildGEP(ctx->builder, base_ptr,
778 indices, 2, "");
779 }
780
781 void
782 ac_build_indexed_store(struct ac_llvm_context *ctx,
783 LLVMValueRef base_ptr, LLVMValueRef index,
784 LLVMValueRef value)
785 {
786 LLVMBuildStore(ctx->builder, value,
787 ac_build_gep0(ctx, base_ptr, index));
788 }
789
790 /**
791 * Build an LLVM bytecode indexed load using LLVMBuildGEP + LLVMBuildLoad.
792 * It's equivalent to doing a load from &base_ptr[index].
793 *
794 * \param base_ptr Where the array starts.
795 * \param index The element index into the array.
796 * \param uniform Whether the base_ptr and index can be assumed to be
797 * dynamically uniform (i.e. load to an SGPR)
798 * \param invariant Whether the load is invariant (no other opcodes affect it)
799 */
800 static LLVMValueRef
801 ac_build_load_custom(struct ac_llvm_context *ctx, LLVMValueRef base_ptr,
802 LLVMValueRef index, bool uniform, bool invariant)
803 {
804 LLVMValueRef pointer, result;
805
806 pointer = ac_build_gep0(ctx, base_ptr, index);
807 if (uniform)
808 LLVMSetMetadata(pointer, ctx->uniform_md_kind, ctx->empty_md);
809 result = LLVMBuildLoad(ctx->builder, pointer, "");
810 if (invariant)
811 LLVMSetMetadata(result, ctx->invariant_load_md_kind, ctx->empty_md);
812 return result;
813 }
814
815 LLVMValueRef ac_build_load(struct ac_llvm_context *ctx, LLVMValueRef base_ptr,
816 LLVMValueRef index)
817 {
818 return ac_build_load_custom(ctx, base_ptr, index, false, false);
819 }
820
821 LLVMValueRef ac_build_load_invariant(struct ac_llvm_context *ctx,
822 LLVMValueRef base_ptr, LLVMValueRef index)
823 {
824 return ac_build_load_custom(ctx, base_ptr, index, false, true);
825 }
826
827 LLVMValueRef ac_build_load_to_sgpr(struct ac_llvm_context *ctx,
828 LLVMValueRef base_ptr, LLVMValueRef index)
829 {
830 return ac_build_load_custom(ctx, base_ptr, index, true, true);
831 }
832
833 /* TBUFFER_STORE_FORMAT_{X,XY,XYZ,XYZW} <- the suffix is selected by num_channels=1..4.
834 * The type of vdata must be one of i32 (num_channels=1), v2i32 (num_channels=2),
835 * or v4i32 (num_channels=3,4).
836 */
837 void
838 ac_build_buffer_store_dword(struct ac_llvm_context *ctx,
839 LLVMValueRef rsrc,
840 LLVMValueRef vdata,
841 unsigned num_channels,
842 LLVMValueRef voffset,
843 LLVMValueRef soffset,
844 unsigned inst_offset,
845 bool glc,
846 bool slc,
847 bool writeonly_memory,
848 bool swizzle_enable_hint)
849 {
850 /* SWIZZLE_ENABLE requires that soffset isn't folded into voffset
851 * (voffset is swizzled, but soffset isn't swizzled).
852 * llvm.amdgcn.buffer.store doesn't have a separate soffset parameter.
853 */
854 if (!swizzle_enable_hint) {
855 /* Split 3 channel stores, becase LLVM doesn't support 3-channel
856 * intrinsics. */
857 if (num_channels == 3) {
858 LLVMValueRef v[3], v01;
859
860 for (int i = 0; i < 3; i++) {
861 v[i] = LLVMBuildExtractElement(ctx->builder, vdata,
862 LLVMConstInt(ctx->i32, i, 0), "");
863 }
864 v01 = ac_build_gather_values(ctx, v, 2);
865
866 ac_build_buffer_store_dword(ctx, rsrc, v01, 2, voffset,
867 soffset, inst_offset, glc, slc,
868 writeonly_memory, swizzle_enable_hint);
869 ac_build_buffer_store_dword(ctx, rsrc, v[2], 1, voffset,
870 soffset, inst_offset + 8,
871 glc, slc,
872 writeonly_memory, swizzle_enable_hint);
873 return;
874 }
875
876 unsigned func = CLAMP(num_channels, 1, 3) - 1;
877 static const char *types[] = {"f32", "v2f32", "v4f32"};
878 char name[256];
879 LLVMValueRef offset = soffset;
880
881 if (inst_offset)
882 offset = LLVMBuildAdd(ctx->builder, offset,
883 LLVMConstInt(ctx->i32, inst_offset, 0), "");
884 if (voffset)
885 offset = LLVMBuildAdd(ctx->builder, offset, voffset, "");
886
887 LLVMValueRef args[] = {
888 ac_to_float(ctx, vdata),
889 LLVMBuildBitCast(ctx->builder, rsrc, ctx->v4i32, ""),
890 LLVMConstInt(ctx->i32, 0, 0),
891 offset,
892 LLVMConstInt(ctx->i1, glc, 0),
893 LLVMConstInt(ctx->i1, slc, 0),
894 };
895
896 snprintf(name, sizeof(name), "llvm.amdgcn.buffer.store.%s",
897 types[func]);
898
899 ac_build_intrinsic(ctx, name, ctx->voidt,
900 args, ARRAY_SIZE(args),
901 writeonly_memory ?
902 AC_FUNC_ATTR_INACCESSIBLE_MEM_ONLY :
903 AC_FUNC_ATTR_WRITEONLY);
904 return;
905 }
906
907 static unsigned dfmt[] = {
908 V_008F0C_BUF_DATA_FORMAT_32,
909 V_008F0C_BUF_DATA_FORMAT_32_32,
910 V_008F0C_BUF_DATA_FORMAT_32_32_32,
911 V_008F0C_BUF_DATA_FORMAT_32_32_32_32
912 };
913 assert(num_channels >= 1 && num_channels <= 4);
914
915 LLVMValueRef args[] = {
916 rsrc,
917 vdata,
918 LLVMConstInt(ctx->i32, num_channels, 0),
919 voffset ? voffset : LLVMGetUndef(ctx->i32),
920 soffset,
921 LLVMConstInt(ctx->i32, inst_offset, 0),
922 LLVMConstInt(ctx->i32, dfmt[num_channels - 1], 0),
923 LLVMConstInt(ctx->i32, V_008F0C_BUF_NUM_FORMAT_UINT, 0),
924 LLVMConstInt(ctx->i32, voffset != NULL, 0),
925 LLVMConstInt(ctx->i32, 0, 0), /* idxen */
926 LLVMConstInt(ctx->i32, glc, 0),
927 LLVMConstInt(ctx->i32, slc, 0),
928 LLVMConstInt(ctx->i32, 0, 0), /* tfe*/
929 };
930
931 /* The instruction offset field has 12 bits */
932 assert(voffset || inst_offset < (1 << 12));
933
934 /* The intrinsic is overloaded, we need to add a type suffix for overloading to work. */
935 unsigned func = CLAMP(num_channels, 1, 3) - 1;
936 const char *types[] = {"i32", "v2i32", "v4i32"};
937 char name[256];
938 snprintf(name, sizeof(name), "llvm.SI.tbuffer.store.%s", types[func]);
939
940 ac_build_intrinsic(ctx, name, ctx->voidt,
941 args, ARRAY_SIZE(args),
942 AC_FUNC_ATTR_LEGACY);
943 }
944
945 static LLVMValueRef
946 ac_build_buffer_load_common(struct ac_llvm_context *ctx,
947 LLVMValueRef rsrc,
948 LLVMValueRef vindex,
949 LLVMValueRef voffset,
950 unsigned num_channels,
951 bool glc,
952 bool slc,
953 bool can_speculate,
954 bool use_format)
955 {
956 LLVMValueRef args[] = {
957 LLVMBuildBitCast(ctx->builder, rsrc, ctx->v4i32, ""),
958 vindex ? vindex : LLVMConstInt(ctx->i32, 0, 0),
959 voffset,
960 LLVMConstInt(ctx->i1, glc, 0),
961 LLVMConstInt(ctx->i1, slc, 0)
962 };
963 unsigned func = CLAMP(num_channels, 1, 3) - 1;
964
965 LLVMTypeRef types[] = {ctx->f32, ctx->v2f32, ctx->v4f32};
966 const char *type_names[] = {"f32", "v2f32", "v4f32"};
967 char name[256];
968
969 if (use_format) {
970 snprintf(name, sizeof(name), "llvm.amdgcn.buffer.load.format.%s",
971 type_names[func]);
972 } else {
973 snprintf(name, sizeof(name), "llvm.amdgcn.buffer.load.%s",
974 type_names[func]);
975 }
976
977 return ac_build_intrinsic(ctx, name, types[func], args,
978 ARRAY_SIZE(args),
979 ac_get_load_intr_attribs(can_speculate));
980 }
981
982 LLVMValueRef
983 ac_build_buffer_load(struct ac_llvm_context *ctx,
984 LLVMValueRef rsrc,
985 int num_channels,
986 LLVMValueRef vindex,
987 LLVMValueRef voffset,
988 LLVMValueRef soffset,
989 unsigned inst_offset,
990 unsigned glc,
991 unsigned slc,
992 bool can_speculate,
993 bool allow_smem)
994 {
995 LLVMValueRef offset = LLVMConstInt(ctx->i32, inst_offset, 0);
996 if (voffset)
997 offset = LLVMBuildAdd(ctx->builder, offset, voffset, "");
998 if (soffset)
999 offset = LLVMBuildAdd(ctx->builder, offset, soffset, "");
1000
1001 /* TODO: VI and later generations can use SMEM with GLC=1.*/
1002 if (allow_smem && !glc && !slc) {
1003 assert(vindex == NULL);
1004
1005 LLVMValueRef result[8];
1006
1007 for (int i = 0; i < num_channels; i++) {
1008 if (i) {
1009 offset = LLVMBuildAdd(ctx->builder, offset,
1010 LLVMConstInt(ctx->i32, 4, 0), "");
1011 }
1012 LLVMValueRef args[2] = {rsrc, offset};
1013 result[i] = ac_build_intrinsic(ctx, "llvm.SI.load.const.v4i32",
1014 ctx->f32, args, 2,
1015 AC_FUNC_ATTR_READNONE |
1016 AC_FUNC_ATTR_LEGACY);
1017 }
1018 if (num_channels == 1)
1019 return result[0];
1020
1021 if (num_channels == 3)
1022 result[num_channels++] = LLVMGetUndef(ctx->f32);
1023 return ac_build_gather_values(ctx, result, num_channels);
1024 }
1025
1026 return ac_build_buffer_load_common(ctx, rsrc, vindex, offset,
1027 num_channels, glc, slc,
1028 can_speculate, false);
1029 }
1030
1031 LLVMValueRef ac_build_buffer_load_format(struct ac_llvm_context *ctx,
1032 LLVMValueRef rsrc,
1033 LLVMValueRef vindex,
1034 LLVMValueRef voffset,
1035 unsigned num_channels,
1036 bool glc,
1037 bool can_speculate)
1038 {
1039 return ac_build_buffer_load_common(ctx, rsrc, vindex, voffset,
1040 num_channels, glc, false,
1041 can_speculate, true);
1042 }
1043
1044 /**
1045 * Set range metadata on an instruction. This can only be used on load and
1046 * call instructions. If you know an instruction can only produce the values
1047 * 0, 1, 2, you would do set_range_metadata(value, 0, 3);
1048 * \p lo is the minimum value inclusive.
1049 * \p hi is the maximum value exclusive.
1050 */
1051 static void set_range_metadata(struct ac_llvm_context *ctx,
1052 LLVMValueRef value, unsigned lo, unsigned hi)
1053 {
1054 LLVMValueRef range_md, md_args[2];
1055 LLVMTypeRef type = LLVMTypeOf(value);
1056 LLVMContextRef context = LLVMGetTypeContext(type);
1057
1058 md_args[0] = LLVMConstInt(type, lo, false);
1059 md_args[1] = LLVMConstInt(type, hi, false);
1060 range_md = LLVMMDNodeInContext(context, md_args, 2);
1061 LLVMSetMetadata(value, ctx->range_md_kind, range_md);
1062 }
1063
1064 LLVMValueRef
1065 ac_get_thread_id(struct ac_llvm_context *ctx)
1066 {
1067 LLVMValueRef tid;
1068
1069 LLVMValueRef tid_args[2];
1070 tid_args[0] = LLVMConstInt(ctx->i32, 0xffffffff, false);
1071 tid_args[1] = LLVMConstInt(ctx->i32, 0, false);
1072 tid_args[1] = ac_build_intrinsic(ctx,
1073 "llvm.amdgcn.mbcnt.lo", ctx->i32,
1074 tid_args, 2, AC_FUNC_ATTR_READNONE);
1075
1076 tid = ac_build_intrinsic(ctx, "llvm.amdgcn.mbcnt.hi",
1077 ctx->i32, tid_args,
1078 2, AC_FUNC_ATTR_READNONE);
1079 set_range_metadata(ctx, tid, 0, 64);
1080 return tid;
1081 }
1082
1083 /*
1084 * SI implements derivatives using the local data store (LDS)
1085 * All writes to the LDS happen in all executing threads at
1086 * the same time. TID is the Thread ID for the current
1087 * thread and is a value between 0 and 63, representing
1088 * the thread's position in the wavefront.
1089 *
1090 * For the pixel shader threads are grouped into quads of four pixels.
1091 * The TIDs of the pixels of a quad are:
1092 *
1093 * +------+------+
1094 * |4n + 0|4n + 1|
1095 * +------+------+
1096 * |4n + 2|4n + 3|
1097 * +------+------+
1098 *
1099 * So, masking the TID with 0xfffffffc yields the TID of the top left pixel
1100 * of the quad, masking with 0xfffffffd yields the TID of the top pixel of
1101 * the current pixel's column, and masking with 0xfffffffe yields the TID
1102 * of the left pixel of the current pixel's row.
1103 *
1104 * Adding 1 yields the TID of the pixel to the right of the left pixel, and
1105 * adding 2 yields the TID of the pixel below the top pixel.
1106 */
1107 LLVMValueRef
1108 ac_build_ddxy(struct ac_llvm_context *ctx,
1109 uint32_t mask,
1110 int idx,
1111 LLVMValueRef val)
1112 {
1113 LLVMValueRef tl, trbl, args[2];
1114 LLVMValueRef result;
1115
1116 if (ctx->chip_class >= VI) {
1117 LLVMValueRef thread_id, tl_tid, trbl_tid;
1118 thread_id = ac_get_thread_id(ctx);
1119
1120 tl_tid = LLVMBuildAnd(ctx->builder, thread_id,
1121 LLVMConstInt(ctx->i32, mask, false), "");
1122
1123 trbl_tid = LLVMBuildAdd(ctx->builder, tl_tid,
1124 LLVMConstInt(ctx->i32, idx, false), "");
1125
1126 args[0] = LLVMBuildMul(ctx->builder, tl_tid,
1127 LLVMConstInt(ctx->i32, 4, false), "");
1128 args[1] = val;
1129 tl = ac_build_intrinsic(ctx,
1130 "llvm.amdgcn.ds.bpermute", ctx->i32,
1131 args, 2,
1132 AC_FUNC_ATTR_READNONE |
1133 AC_FUNC_ATTR_CONVERGENT);
1134
1135 args[0] = LLVMBuildMul(ctx->builder, trbl_tid,
1136 LLVMConstInt(ctx->i32, 4, false), "");
1137 trbl = ac_build_intrinsic(ctx,
1138 "llvm.amdgcn.ds.bpermute", ctx->i32,
1139 args, 2,
1140 AC_FUNC_ATTR_READNONE |
1141 AC_FUNC_ATTR_CONVERGENT);
1142 } else {
1143 uint32_t masks[2] = {};
1144
1145 switch (mask) {
1146 case AC_TID_MASK_TOP_LEFT:
1147 masks[0] = 0x8000;
1148 if (idx == 1)
1149 masks[1] = 0x8055;
1150 else
1151 masks[1] = 0x80aa;
1152
1153 break;
1154 case AC_TID_MASK_TOP:
1155 masks[0] = 0x8044;
1156 masks[1] = 0x80ee;
1157 break;
1158 case AC_TID_MASK_LEFT:
1159 masks[0] = 0x80a0;
1160 masks[1] = 0x80f5;
1161 break;
1162 default:
1163 assert(0);
1164 }
1165
1166 args[0] = val;
1167 args[1] = LLVMConstInt(ctx->i32, masks[0], false);
1168
1169 tl = ac_build_intrinsic(ctx,
1170 "llvm.amdgcn.ds.swizzle", ctx->i32,
1171 args, 2,
1172 AC_FUNC_ATTR_READNONE |
1173 AC_FUNC_ATTR_CONVERGENT);
1174
1175 args[1] = LLVMConstInt(ctx->i32, masks[1], false);
1176 trbl = ac_build_intrinsic(ctx,
1177 "llvm.amdgcn.ds.swizzle", ctx->i32,
1178 args, 2,
1179 AC_FUNC_ATTR_READNONE |
1180 AC_FUNC_ATTR_CONVERGENT);
1181 }
1182
1183 tl = LLVMBuildBitCast(ctx->builder, tl, ctx->f32, "");
1184 trbl = LLVMBuildBitCast(ctx->builder, trbl, ctx->f32, "");
1185 result = LLVMBuildFSub(ctx->builder, trbl, tl, "");
1186 return result;
1187 }
1188
1189 void
1190 ac_build_sendmsg(struct ac_llvm_context *ctx,
1191 uint32_t msg,
1192 LLVMValueRef wave_id)
1193 {
1194 LLVMValueRef args[2];
1195 args[0] = LLVMConstInt(ctx->i32, msg, false);
1196 args[1] = wave_id;
1197 ac_build_intrinsic(ctx, "llvm.amdgcn.s.sendmsg", ctx->voidt, args, 2, 0);
1198 }
1199
1200 LLVMValueRef
1201 ac_build_imsb(struct ac_llvm_context *ctx,
1202 LLVMValueRef arg,
1203 LLVMTypeRef dst_type)
1204 {
1205 LLVMValueRef msb = ac_build_intrinsic(ctx, "llvm.amdgcn.sffbh.i32",
1206 dst_type, &arg, 1,
1207 AC_FUNC_ATTR_READNONE);
1208
1209 /* The HW returns the last bit index from MSB, but NIR/TGSI wants
1210 * the index from LSB. Invert it by doing "31 - msb". */
1211 msb = LLVMBuildSub(ctx->builder, LLVMConstInt(ctx->i32, 31, false),
1212 msb, "");
1213
1214 LLVMValueRef all_ones = LLVMConstInt(ctx->i32, -1, true);
1215 LLVMValueRef cond = LLVMBuildOr(ctx->builder,
1216 LLVMBuildICmp(ctx->builder, LLVMIntEQ,
1217 arg, LLVMConstInt(ctx->i32, 0, 0), ""),
1218 LLVMBuildICmp(ctx->builder, LLVMIntEQ,
1219 arg, all_ones, ""), "");
1220
1221 return LLVMBuildSelect(ctx->builder, cond, all_ones, msb, "");
1222 }
1223
1224 LLVMValueRef
1225 ac_build_umsb(struct ac_llvm_context *ctx,
1226 LLVMValueRef arg,
1227 LLVMTypeRef dst_type)
1228 {
1229 LLVMValueRef args[2] = {
1230 arg,
1231 ctx->i1true,
1232 };
1233 LLVMValueRef msb = ac_build_intrinsic(ctx, "llvm.ctlz.i32",
1234 dst_type, args, ARRAY_SIZE(args),
1235 AC_FUNC_ATTR_READNONE);
1236
1237 /* The HW returns the last bit index from MSB, but TGSI/NIR wants
1238 * the index from LSB. Invert it by doing "31 - msb". */
1239 msb = LLVMBuildSub(ctx->builder, LLVMConstInt(ctx->i32, 31, false),
1240 msb, "");
1241
1242 /* check for zero */
1243 return LLVMBuildSelect(ctx->builder,
1244 LLVMBuildICmp(ctx->builder, LLVMIntEQ, arg,
1245 LLVMConstInt(ctx->i32, 0, 0), ""),
1246 LLVMConstInt(ctx->i32, -1, true), msb, "");
1247 }
1248
1249 LLVMValueRef ac_build_fmin(struct ac_llvm_context *ctx, LLVMValueRef a,
1250 LLVMValueRef b)
1251 {
1252 LLVMValueRef args[2] = {a, b};
1253 return ac_build_intrinsic(ctx, "llvm.minnum.f32", ctx->f32, args, 2,
1254 AC_FUNC_ATTR_READNONE);
1255 }
1256
1257 LLVMValueRef ac_build_fmax(struct ac_llvm_context *ctx, LLVMValueRef a,
1258 LLVMValueRef b)
1259 {
1260 LLVMValueRef args[2] = {a, b};
1261 return ac_build_intrinsic(ctx, "llvm.maxnum.f32", ctx->f32, args, 2,
1262 AC_FUNC_ATTR_READNONE);
1263 }
1264
1265 LLVMValueRef ac_build_imin(struct ac_llvm_context *ctx, LLVMValueRef a,
1266 LLVMValueRef b)
1267 {
1268 LLVMValueRef cmp = LLVMBuildICmp(ctx->builder, LLVMIntSLE, a, b, "");
1269 return LLVMBuildSelect(ctx->builder, cmp, a, b, "");
1270 }
1271
1272 LLVMValueRef ac_build_imax(struct ac_llvm_context *ctx, LLVMValueRef a,
1273 LLVMValueRef b)
1274 {
1275 LLVMValueRef cmp = LLVMBuildICmp(ctx->builder, LLVMIntSGT, a, b, "");
1276 return LLVMBuildSelect(ctx->builder, cmp, a, b, "");
1277 }
1278
1279 LLVMValueRef ac_build_umin(struct ac_llvm_context *ctx, LLVMValueRef a,
1280 LLVMValueRef b)
1281 {
1282 LLVMValueRef cmp = LLVMBuildICmp(ctx->builder, LLVMIntULE, a, b, "");
1283 return LLVMBuildSelect(ctx->builder, cmp, a, b, "");
1284 }
1285
1286 LLVMValueRef ac_build_clamp(struct ac_llvm_context *ctx, LLVMValueRef value)
1287 {
1288 if (HAVE_LLVM >= 0x0500) {
1289 return ac_build_fmin(ctx, ac_build_fmax(ctx, value, ctx->f32_0),
1290 ctx->f32_1);
1291 }
1292
1293 LLVMValueRef args[3] = {
1294 value,
1295 LLVMConstReal(ctx->f32, 0),
1296 LLVMConstReal(ctx->f32, 1),
1297 };
1298
1299 return ac_build_intrinsic(ctx, "llvm.AMDGPU.clamp.", ctx->f32, args, 3,
1300 AC_FUNC_ATTR_READNONE |
1301 AC_FUNC_ATTR_LEGACY);
1302 }
1303
1304 void ac_build_export(struct ac_llvm_context *ctx, struct ac_export_args *a)
1305 {
1306 LLVMValueRef args[9];
1307
1308 if (HAVE_LLVM >= 0x0500) {
1309 args[0] = LLVMConstInt(ctx->i32, a->target, 0);
1310 args[1] = LLVMConstInt(ctx->i32, a->enabled_channels, 0);
1311
1312 if (a->compr) {
1313 LLVMTypeRef i16 = LLVMInt16TypeInContext(ctx->context);
1314 LLVMTypeRef v2i16 = LLVMVectorType(i16, 2);
1315
1316 args[2] = LLVMBuildBitCast(ctx->builder, a->out[0],
1317 v2i16, "");
1318 args[3] = LLVMBuildBitCast(ctx->builder, a->out[1],
1319 v2i16, "");
1320 args[4] = LLVMConstInt(ctx->i1, a->done, 0);
1321 args[5] = LLVMConstInt(ctx->i1, a->valid_mask, 0);
1322
1323 ac_build_intrinsic(ctx, "llvm.amdgcn.exp.compr.v2i16",
1324 ctx->voidt, args, 6, 0);
1325 } else {
1326 args[2] = a->out[0];
1327 args[3] = a->out[1];
1328 args[4] = a->out[2];
1329 args[5] = a->out[3];
1330 args[6] = LLVMConstInt(ctx->i1, a->done, 0);
1331 args[7] = LLVMConstInt(ctx->i1, a->valid_mask, 0);
1332
1333 ac_build_intrinsic(ctx, "llvm.amdgcn.exp.f32",
1334 ctx->voidt, args, 8, 0);
1335 }
1336 return;
1337 }
1338
1339 args[0] = LLVMConstInt(ctx->i32, a->enabled_channels, 0);
1340 args[1] = LLVMConstInt(ctx->i32, a->valid_mask, 0);
1341 args[2] = LLVMConstInt(ctx->i32, a->done, 0);
1342 args[3] = LLVMConstInt(ctx->i32, a->target, 0);
1343 args[4] = LLVMConstInt(ctx->i32, a->compr, 0);
1344 memcpy(args + 5, a->out, sizeof(a->out[0]) * 4);
1345
1346 ac_build_intrinsic(ctx, "llvm.SI.export", ctx->voidt, args, 9,
1347 AC_FUNC_ATTR_LEGACY);
1348 }
1349
1350 void ac_build_export_null(struct ac_llvm_context *ctx)
1351 {
1352 struct ac_export_args args;
1353
1354 args.enabled_channels = 0x0; /* enabled channels */
1355 args.valid_mask = 1; /* whether the EXEC mask is valid */
1356 args.done = 1; /* DONE bit */
1357 args.target = V_008DFC_SQ_EXP_NULL;
1358 args.compr = 0; /* COMPR flag (0 = 32-bit export) */
1359 args.out[0] = LLVMGetUndef(ctx->f32); /* R */
1360 args.out[1] = LLVMGetUndef(ctx->f32); /* G */
1361 args.out[2] = LLVMGetUndef(ctx->f32); /* B */
1362 args.out[3] = LLVMGetUndef(ctx->f32); /* A */
1363
1364 ac_build_export(ctx, &args);
1365 }
1366
1367 LLVMValueRef ac_build_image_opcode(struct ac_llvm_context *ctx,
1368 struct ac_image_args *a)
1369 {
1370 LLVMValueRef args[11];
1371 unsigned num_args = 0;
1372 const char *name = NULL;
1373 char intr_name[128], type[64];
1374
1375 bool sample = a->opcode == ac_image_sample ||
1376 a->opcode == ac_image_gather4 ||
1377 a->opcode == ac_image_get_lod;
1378
1379 if (sample)
1380 args[num_args++] = ac_to_float(ctx, a->addr);
1381 else
1382 args[num_args++] = a->addr;
1383
1384 args[num_args++] = a->resource;
1385 if (sample)
1386 args[num_args++] = a->sampler;
1387 args[num_args++] = LLVMConstInt(ctx->i32, a->dmask, 0);
1388 if (sample)
1389 args[num_args++] = LLVMConstInt(ctx->i1, a->unorm, 0);
1390 args[num_args++] = ctx->i1false; /* glc */
1391 args[num_args++] = ctx->i1false; /* slc */
1392 args[num_args++] = ctx->i1false; /* lwe */
1393 args[num_args++] = LLVMConstInt(ctx->i1, a->da, 0);
1394
1395 switch (a->opcode) {
1396 case ac_image_sample:
1397 name = "llvm.amdgcn.image.sample";
1398 break;
1399 case ac_image_gather4:
1400 name = "llvm.amdgcn.image.gather4";
1401 break;
1402 case ac_image_load:
1403 name = "llvm.amdgcn.image.load";
1404 break;
1405 case ac_image_load_mip:
1406 name = "llvm.amdgcn.image.load.mip";
1407 break;
1408 case ac_image_get_lod:
1409 name = "llvm.amdgcn.image.getlod";
1410 break;
1411 case ac_image_get_resinfo:
1412 name = "llvm.amdgcn.image.getresinfo";
1413 break;
1414 default:
1415 unreachable("invalid image opcode");
1416 }
1417
1418 ac_build_type_name_for_intr(LLVMTypeOf(args[0]), type,
1419 sizeof(type));
1420
1421 snprintf(intr_name, sizeof(intr_name), "%s%s%s%s.v4f32.%s.v8i32",
1422 name,
1423 a->compare ? ".c" : "",
1424 a->bias ? ".b" :
1425 a->lod ? ".l" :
1426 a->deriv ? ".d" :
1427 a->level_zero ? ".lz" : "",
1428 a->offset ? ".o" : "",
1429 type);
1430
1431 LLVMValueRef result =
1432 ac_build_intrinsic(ctx, intr_name,
1433 ctx->v4f32, args, num_args,
1434 AC_FUNC_ATTR_READNONE);
1435 if (!sample) {
1436 result = LLVMBuildBitCast(ctx->builder, result,
1437 ctx->v4i32, "");
1438 }
1439 return result;
1440 }
1441
1442 LLVMValueRef ac_build_cvt_pkrtz_f16(struct ac_llvm_context *ctx,
1443 LLVMValueRef args[2])
1444 {
1445 if (HAVE_LLVM >= 0x0500) {
1446 LLVMTypeRef v2f16 =
1447 LLVMVectorType(LLVMHalfTypeInContext(ctx->context), 2);
1448 LLVMValueRef res =
1449 ac_build_intrinsic(ctx, "llvm.amdgcn.cvt.pkrtz",
1450 v2f16, args, 2,
1451 AC_FUNC_ATTR_READNONE);
1452 return LLVMBuildBitCast(ctx->builder, res, ctx->i32, "");
1453 }
1454
1455 return ac_build_intrinsic(ctx, "llvm.SI.packf16", ctx->i32, args, 2,
1456 AC_FUNC_ATTR_READNONE |
1457 AC_FUNC_ATTR_LEGACY);
1458 }
1459
1460 /* Upper 16 bits must be zero. */
1461 static LLVMValueRef ac_llvm_pack_two_int16(struct ac_llvm_context *ctx,
1462 LLVMValueRef val[2])
1463 {
1464 return LLVMBuildOr(ctx->builder, val[0],
1465 LLVMBuildShl(ctx->builder, val[1],
1466 LLVMConstInt(ctx->i32, 16, 0),
1467 ""), "");
1468 }
1469
1470 /* Upper 16 bits are ignored and will be dropped. */
1471 static LLVMValueRef ac_llvm_pack_two_int32_as_int16(struct ac_llvm_context *ctx,
1472 LLVMValueRef val[2])
1473 {
1474 LLVMValueRef v[2] = {
1475 LLVMBuildAnd(ctx->builder, val[0],
1476 LLVMConstInt(ctx->i32, 0xffff, 0), ""),
1477 val[1],
1478 };
1479 return ac_llvm_pack_two_int16(ctx, v);
1480 }
1481
1482 LLVMValueRef ac_build_cvt_pknorm_i16(struct ac_llvm_context *ctx,
1483 LLVMValueRef args[2])
1484 {
1485 if (HAVE_LLVM >= 0x0600) {
1486 LLVMValueRef res =
1487 ac_build_intrinsic(ctx, "llvm.amdgcn.cvt.pknorm.i16",
1488 ctx->v2i16, args, 2,
1489 AC_FUNC_ATTR_READNONE);
1490 return LLVMBuildBitCast(ctx->builder, res, ctx->i32, "");
1491 }
1492
1493 LLVMValueRef val[2];
1494
1495 for (int chan = 0; chan < 2; chan++) {
1496 /* Clamp between [-1, 1]. */
1497 val[chan] = ac_build_fmin(ctx, args[chan], ctx->f32_1);
1498 val[chan] = ac_build_fmax(ctx, val[chan], LLVMConstReal(ctx->f32, -1));
1499 /* Convert to a signed integer in [-32767, 32767]. */
1500 val[chan] = LLVMBuildFMul(ctx->builder, val[chan],
1501 LLVMConstReal(ctx->f32, 32767), "");
1502 /* If positive, add 0.5, else add -0.5. */
1503 val[chan] = LLVMBuildFAdd(ctx->builder, val[chan],
1504 LLVMBuildSelect(ctx->builder,
1505 LLVMBuildFCmp(ctx->builder, LLVMRealOGE,
1506 val[chan], ctx->f32_0, ""),
1507 LLVMConstReal(ctx->f32, 0.5),
1508 LLVMConstReal(ctx->f32, -0.5), ""), "");
1509 val[chan] = LLVMBuildFPToSI(ctx->builder, val[chan], ctx->i32, "");
1510 }
1511 return ac_llvm_pack_two_int32_as_int16(ctx, val);
1512 }
1513
1514 LLVMValueRef ac_build_cvt_pknorm_u16(struct ac_llvm_context *ctx,
1515 LLVMValueRef args[2])
1516 {
1517 if (HAVE_LLVM >= 0x0600) {
1518 LLVMValueRef res =
1519 ac_build_intrinsic(ctx, "llvm.amdgcn.cvt.pknorm.u16",
1520 ctx->v2i16, args, 2,
1521 AC_FUNC_ATTR_READNONE);
1522 return LLVMBuildBitCast(ctx->builder, res, ctx->i32, "");
1523 }
1524
1525 LLVMValueRef val[2];
1526
1527 for (int chan = 0; chan < 2; chan++) {
1528 val[chan] = ac_build_clamp(ctx, args[chan]);
1529 val[chan] = LLVMBuildFMul(ctx->builder, val[chan],
1530 LLVMConstReal(ctx->f32, 65535), "");
1531 val[chan] = LLVMBuildFAdd(ctx->builder, val[chan],
1532 LLVMConstReal(ctx->f32, 0.5), "");
1533 val[chan] = LLVMBuildFPToUI(ctx->builder, val[chan],
1534 ctx->i32, "");
1535 }
1536 return ac_llvm_pack_two_int32_as_int16(ctx, val);
1537 }
1538
1539 /* The 8-bit and 10-bit clamping is for HW workarounds. */
1540 LLVMValueRef ac_build_cvt_pk_i16(struct ac_llvm_context *ctx,
1541 LLVMValueRef args[2], unsigned bits, bool hi)
1542 {
1543 assert(bits == 8 || bits == 10 || bits == 16);
1544
1545 LLVMValueRef max_rgb = LLVMConstInt(ctx->i32,
1546 bits == 8 ? 127 : bits == 10 ? 511 : 32767, 0);
1547 LLVMValueRef min_rgb = LLVMConstInt(ctx->i32,
1548 bits == 8 ? -128 : bits == 10 ? -512 : -32768, 0);
1549 LLVMValueRef max_alpha =
1550 bits != 10 ? max_rgb : ctx->i32_1;
1551 LLVMValueRef min_alpha =
1552 bits != 10 ? min_rgb : LLVMConstInt(ctx->i32, -2, 0);
1553 bool has_intrinsic = HAVE_LLVM >= 0x0600;
1554
1555 /* Clamp. */
1556 if (!has_intrinsic || bits != 16) {
1557 for (int i = 0; i < 2; i++) {
1558 bool alpha = hi && i == 1;
1559 args[i] = ac_build_imin(ctx, args[i],
1560 alpha ? max_alpha : max_rgb);
1561 args[i] = ac_build_imax(ctx, args[i],
1562 alpha ? min_alpha : min_rgb);
1563 }
1564 }
1565
1566 if (has_intrinsic) {
1567 LLVMValueRef res =
1568 ac_build_intrinsic(ctx, "llvm.amdgcn.cvt.pk.i16",
1569 ctx->v2i16, args, 2,
1570 AC_FUNC_ATTR_READNONE);
1571 return LLVMBuildBitCast(ctx->builder, res, ctx->i32, "");
1572 }
1573
1574 return ac_llvm_pack_two_int32_as_int16(ctx, args);
1575 }
1576
1577 /* The 8-bit and 10-bit clamping is for HW workarounds. */
1578 LLVMValueRef ac_build_cvt_pk_u16(struct ac_llvm_context *ctx,
1579 LLVMValueRef args[2], unsigned bits, bool hi)
1580 {
1581 assert(bits == 8 || bits == 10 || bits == 16);
1582
1583 LLVMValueRef max_rgb = LLVMConstInt(ctx->i32,
1584 bits == 8 ? 255 : bits == 10 ? 1023 : 65535, 0);
1585 LLVMValueRef max_alpha =
1586 bits != 10 ? max_rgb : LLVMConstInt(ctx->i32, 3, 0);
1587 bool has_intrinsic = HAVE_LLVM >= 0x0600;
1588
1589 /* Clamp. */
1590 if (!has_intrinsic || bits != 16) {
1591 for (int i = 0; i < 2; i++) {
1592 bool alpha = hi && i == 1;
1593 args[i] = ac_build_umin(ctx, args[i],
1594 alpha ? max_alpha : max_rgb);
1595 }
1596 }
1597
1598 if (has_intrinsic) {
1599 LLVMValueRef res =
1600 ac_build_intrinsic(ctx, "llvm.amdgcn.cvt.pk.u16",
1601 ctx->v2i16, args, 2,
1602 AC_FUNC_ATTR_READNONE);
1603 return LLVMBuildBitCast(ctx->builder, res, ctx->i32, "");
1604 }
1605
1606 return ac_llvm_pack_two_int16(ctx, args);
1607 }
1608
1609 LLVMValueRef ac_build_wqm_vote(struct ac_llvm_context *ctx, LLVMValueRef i1)
1610 {
1611 assert(HAVE_LLVM >= 0x0600);
1612 return ac_build_intrinsic(ctx, "llvm.amdgcn.wqm.vote", ctx->i1,
1613 &i1, 1, AC_FUNC_ATTR_READNONE);
1614 }
1615
1616 void ac_build_kill_if_false(struct ac_llvm_context *ctx, LLVMValueRef i1)
1617 {
1618 if (HAVE_LLVM >= 0x0600) {
1619 ac_build_intrinsic(ctx, "llvm.amdgcn.kill", ctx->voidt,
1620 &i1, 1, 0);
1621 return;
1622 }
1623
1624 LLVMValueRef value = LLVMBuildSelect(ctx->builder, i1,
1625 LLVMConstReal(ctx->f32, 1),
1626 LLVMConstReal(ctx->f32, -1), "");
1627 ac_build_intrinsic(ctx, "llvm.AMDGPU.kill", ctx->voidt,
1628 &value, 1, AC_FUNC_ATTR_LEGACY);
1629 }
1630
1631 LLVMValueRef ac_build_bfe(struct ac_llvm_context *ctx, LLVMValueRef input,
1632 LLVMValueRef offset, LLVMValueRef width,
1633 bool is_signed)
1634 {
1635 LLVMValueRef args[] = {
1636 input,
1637 offset,
1638 width,
1639 };
1640
1641 if (HAVE_LLVM >= 0x0500) {
1642 return ac_build_intrinsic(ctx,
1643 is_signed ? "llvm.amdgcn.sbfe.i32" :
1644 "llvm.amdgcn.ubfe.i32",
1645 ctx->i32, args, 3,
1646 AC_FUNC_ATTR_READNONE);
1647 }
1648
1649 return ac_build_intrinsic(ctx,
1650 is_signed ? "llvm.AMDGPU.bfe.i32" :
1651 "llvm.AMDGPU.bfe.u32",
1652 ctx->i32, args, 3,
1653 AC_FUNC_ATTR_READNONE |
1654 AC_FUNC_ATTR_LEGACY);
1655 }
1656
1657 void ac_build_waitcnt(struct ac_llvm_context *ctx, unsigned simm16)
1658 {
1659 LLVMValueRef args[1] = {
1660 LLVMConstInt(ctx->i32, simm16, false),
1661 };
1662 ac_build_intrinsic(ctx, "llvm.amdgcn.s.waitcnt",
1663 ctx->voidt, args, 1, 0);
1664 }
1665
1666 void ac_get_image_intr_name(const char *base_name,
1667 LLVMTypeRef data_type,
1668 LLVMTypeRef coords_type,
1669 LLVMTypeRef rsrc_type,
1670 char *out_name, unsigned out_len)
1671 {
1672 char coords_type_name[8];
1673
1674 ac_build_type_name_for_intr(coords_type, coords_type_name,
1675 sizeof(coords_type_name));
1676
1677 char data_type_name[8];
1678 char rsrc_type_name[8];
1679
1680 ac_build_type_name_for_intr(data_type, data_type_name,
1681 sizeof(data_type_name));
1682 ac_build_type_name_for_intr(rsrc_type, rsrc_type_name,
1683 sizeof(rsrc_type_name));
1684 snprintf(out_name, out_len, "%s.%s.%s.%s", base_name,
1685 data_type_name, coords_type_name, rsrc_type_name);
1686 }
1687
1688 #define AC_EXP_TARGET (HAVE_LLVM >= 0x0500 ? 0 : 3)
1689 #define AC_EXP_OUT0 (HAVE_LLVM >= 0x0500 ? 2 : 5)
1690
1691 enum ac_ir_type {
1692 AC_IR_UNDEF,
1693 AC_IR_CONST,
1694 AC_IR_VALUE,
1695 };
1696
1697 struct ac_vs_exp_chan
1698 {
1699 LLVMValueRef value;
1700 float const_float;
1701 enum ac_ir_type type;
1702 };
1703
1704 struct ac_vs_exp_inst {
1705 unsigned offset;
1706 LLVMValueRef inst;
1707 struct ac_vs_exp_chan chan[4];
1708 };
1709
1710 struct ac_vs_exports {
1711 unsigned num;
1712 struct ac_vs_exp_inst exp[VARYING_SLOT_MAX];
1713 };
1714
1715 /* Return true if the PARAM export has been eliminated. */
1716 static bool ac_eliminate_const_output(uint8_t *vs_output_param_offset,
1717 uint32_t num_outputs,
1718 struct ac_vs_exp_inst *exp)
1719 {
1720 unsigned i, default_val; /* SPI_PS_INPUT_CNTL_i.DEFAULT_VAL */
1721 bool is_zero[4] = {}, is_one[4] = {};
1722
1723 for (i = 0; i < 4; i++) {
1724 /* It's a constant expression. Undef outputs are eliminated too. */
1725 if (exp->chan[i].type == AC_IR_UNDEF) {
1726 is_zero[i] = true;
1727 is_one[i] = true;
1728 } else if (exp->chan[i].type == AC_IR_CONST) {
1729 if (exp->chan[i].const_float == 0)
1730 is_zero[i] = true;
1731 else if (exp->chan[i].const_float == 1)
1732 is_one[i] = true;
1733 else
1734 return false; /* other constant */
1735 } else
1736 return false;
1737 }
1738
1739 /* Only certain combinations of 0 and 1 can be eliminated. */
1740 if (is_zero[0] && is_zero[1] && is_zero[2])
1741 default_val = is_zero[3] ? 0 : 1;
1742 else if (is_one[0] && is_one[1] && is_one[2])
1743 default_val = is_zero[3] ? 2 : 3;
1744 else
1745 return false;
1746
1747 /* The PARAM export can be represented as DEFAULT_VAL. Kill it. */
1748 LLVMInstructionEraseFromParent(exp->inst);
1749
1750 /* Change OFFSET to DEFAULT_VAL. */
1751 for (i = 0; i < num_outputs; i++) {
1752 if (vs_output_param_offset[i] == exp->offset) {
1753 vs_output_param_offset[i] =
1754 AC_EXP_PARAM_DEFAULT_VAL_0000 + default_val;
1755 break;
1756 }
1757 }
1758 return true;
1759 }
1760
1761 static bool ac_eliminate_duplicated_output(uint8_t *vs_output_param_offset,
1762 uint32_t num_outputs,
1763 struct ac_vs_exports *processed,
1764 struct ac_vs_exp_inst *exp)
1765 {
1766 unsigned p, copy_back_channels = 0;
1767
1768 /* See if the output is already in the list of processed outputs.
1769 * The LLVMValueRef comparison relies on SSA.
1770 */
1771 for (p = 0; p < processed->num; p++) {
1772 bool different = false;
1773
1774 for (unsigned j = 0; j < 4; j++) {
1775 struct ac_vs_exp_chan *c1 = &processed->exp[p].chan[j];
1776 struct ac_vs_exp_chan *c2 = &exp->chan[j];
1777
1778 /* Treat undef as a match. */
1779 if (c2->type == AC_IR_UNDEF)
1780 continue;
1781
1782 /* If c1 is undef but c2 isn't, we can copy c2 to c1
1783 * and consider the instruction duplicated.
1784 */
1785 if (c1->type == AC_IR_UNDEF) {
1786 copy_back_channels |= 1 << j;
1787 continue;
1788 }
1789
1790 /* Test whether the channels are not equal. */
1791 if (c1->type != c2->type ||
1792 (c1->type == AC_IR_CONST &&
1793 c1->const_float != c2->const_float) ||
1794 (c1->type == AC_IR_VALUE &&
1795 c1->value != c2->value)) {
1796 different = true;
1797 break;
1798 }
1799 }
1800 if (!different)
1801 break;
1802
1803 copy_back_channels = 0;
1804 }
1805 if (p == processed->num)
1806 return false;
1807
1808 /* If a match was found, but the matching export has undef where the new
1809 * one has a normal value, copy the normal value to the undef channel.
1810 */
1811 struct ac_vs_exp_inst *match = &processed->exp[p];
1812
1813 while (copy_back_channels) {
1814 unsigned chan = u_bit_scan(&copy_back_channels);
1815
1816 assert(match->chan[chan].type == AC_IR_UNDEF);
1817 LLVMSetOperand(match->inst, AC_EXP_OUT0 + chan,
1818 exp->chan[chan].value);
1819 match->chan[chan] = exp->chan[chan];
1820 }
1821
1822 /* The PARAM export is duplicated. Kill it. */
1823 LLVMInstructionEraseFromParent(exp->inst);
1824
1825 /* Change OFFSET to the matching export. */
1826 for (unsigned i = 0; i < num_outputs; i++) {
1827 if (vs_output_param_offset[i] == exp->offset) {
1828 vs_output_param_offset[i] = match->offset;
1829 break;
1830 }
1831 }
1832 return true;
1833 }
1834
1835 void ac_optimize_vs_outputs(struct ac_llvm_context *ctx,
1836 LLVMValueRef main_fn,
1837 uint8_t *vs_output_param_offset,
1838 uint32_t num_outputs,
1839 uint8_t *num_param_exports)
1840 {
1841 LLVMBasicBlockRef bb;
1842 bool removed_any = false;
1843 struct ac_vs_exports exports;
1844
1845 exports.num = 0;
1846
1847 /* Process all LLVM instructions. */
1848 bb = LLVMGetFirstBasicBlock(main_fn);
1849 while (bb) {
1850 LLVMValueRef inst = LLVMGetFirstInstruction(bb);
1851
1852 while (inst) {
1853 LLVMValueRef cur = inst;
1854 inst = LLVMGetNextInstruction(inst);
1855 struct ac_vs_exp_inst exp;
1856
1857 if (LLVMGetInstructionOpcode(cur) != LLVMCall)
1858 continue;
1859
1860 LLVMValueRef callee = ac_llvm_get_called_value(cur);
1861
1862 if (!ac_llvm_is_function(callee))
1863 continue;
1864
1865 const char *name = LLVMGetValueName(callee);
1866 unsigned num_args = LLVMCountParams(callee);
1867
1868 /* Check if this is an export instruction. */
1869 if ((num_args != 9 && num_args != 8) ||
1870 (strcmp(name, "llvm.SI.export") &&
1871 strcmp(name, "llvm.amdgcn.exp.f32")))
1872 continue;
1873
1874 LLVMValueRef arg = LLVMGetOperand(cur, AC_EXP_TARGET);
1875 unsigned target = LLVMConstIntGetZExtValue(arg);
1876
1877 if (target < V_008DFC_SQ_EXP_PARAM)
1878 continue;
1879
1880 target -= V_008DFC_SQ_EXP_PARAM;
1881
1882 /* Parse the instruction. */
1883 memset(&exp, 0, sizeof(exp));
1884 exp.offset = target;
1885 exp.inst = cur;
1886
1887 for (unsigned i = 0; i < 4; i++) {
1888 LLVMValueRef v = LLVMGetOperand(cur, AC_EXP_OUT0 + i);
1889
1890 exp.chan[i].value = v;
1891
1892 if (LLVMIsUndef(v)) {
1893 exp.chan[i].type = AC_IR_UNDEF;
1894 } else if (LLVMIsAConstantFP(v)) {
1895 LLVMBool loses_info;
1896 exp.chan[i].type = AC_IR_CONST;
1897 exp.chan[i].const_float =
1898 LLVMConstRealGetDouble(v, &loses_info);
1899 } else {
1900 exp.chan[i].type = AC_IR_VALUE;
1901 }
1902 }
1903
1904 /* Eliminate constant and duplicated PARAM exports. */
1905 if (ac_eliminate_const_output(vs_output_param_offset,
1906 num_outputs, &exp) ||
1907 ac_eliminate_duplicated_output(vs_output_param_offset,
1908 num_outputs, &exports,
1909 &exp)) {
1910 removed_any = true;
1911 } else {
1912 exports.exp[exports.num++] = exp;
1913 }
1914 }
1915 bb = LLVMGetNextBasicBlock(bb);
1916 }
1917
1918 /* Remove holes in export memory due to removed PARAM exports.
1919 * This is done by renumbering all PARAM exports.
1920 */
1921 if (removed_any) {
1922 uint8_t old_offset[VARYING_SLOT_MAX];
1923 unsigned out, i;
1924
1925 /* Make a copy of the offsets. We need the old version while
1926 * we are modifying some of them. */
1927 memcpy(old_offset, vs_output_param_offset,
1928 sizeof(old_offset));
1929
1930 for (i = 0; i < exports.num; i++) {
1931 unsigned offset = exports.exp[i].offset;
1932
1933 /* Update vs_output_param_offset. Multiple outputs can
1934 * have the same offset.
1935 */
1936 for (out = 0; out < num_outputs; out++) {
1937 if (old_offset[out] == offset)
1938 vs_output_param_offset[out] = i;
1939 }
1940
1941 /* Change the PARAM offset in the instruction. */
1942 LLVMSetOperand(exports.exp[i].inst, AC_EXP_TARGET,
1943 LLVMConstInt(ctx->i32,
1944 V_008DFC_SQ_EXP_PARAM + i, 0));
1945 }
1946 *num_param_exports = exports.num;
1947 }
1948 }
1949
1950 void ac_init_exec_full_mask(struct ac_llvm_context *ctx)
1951 {
1952 LLVMValueRef full_mask = LLVMConstInt(ctx->i64, ~0ull, 0);
1953 ac_build_intrinsic(ctx,
1954 "llvm.amdgcn.init.exec", ctx->voidt,
1955 &full_mask, 1, AC_FUNC_ATTR_CONVERGENT);
1956 }
1957
1958 void ac_declare_lds_as_pointer(struct ac_llvm_context *ctx)
1959 {
1960 unsigned lds_size = ctx->chip_class >= CIK ? 65536 : 32768;
1961 ctx->lds = LLVMBuildIntToPtr(ctx->builder, ctx->i32_0,
1962 LLVMPointerType(LLVMArrayType(ctx->i32, lds_size / 4), AC_LOCAL_ADDR_SPACE),
1963 "lds");
1964 }
1965
1966 LLVMValueRef ac_lds_load(struct ac_llvm_context *ctx,
1967 LLVMValueRef dw_addr)
1968 {
1969 return ac_build_load(ctx, ctx->lds, dw_addr);
1970 }
1971
1972 void ac_lds_store(struct ac_llvm_context *ctx,
1973 LLVMValueRef dw_addr,
1974 LLVMValueRef value)
1975 {
1976 value = ac_to_integer(ctx, value);
1977 ac_build_indexed_store(ctx, ctx->lds,
1978 dw_addr, value);
1979 }
1980
1981 LLVMValueRef ac_find_lsb(struct ac_llvm_context *ctx,
1982 LLVMTypeRef dst_type,
1983 LLVMValueRef src0)
1984 {
1985 LLVMValueRef params[2] = {
1986 src0,
1987
1988 /* The value of 1 means that ffs(x=0) = undef, so LLVM won't
1989 * add special code to check for x=0. The reason is that
1990 * the LLVM behavior for x=0 is different from what we
1991 * need here. However, LLVM also assumes that ffs(x) is
1992 * in [0, 31], but GLSL expects that ffs(0) = -1, so
1993 * a conditional assignment to handle 0 is still required.
1994 *
1995 * The hardware already implements the correct behavior.
1996 */
1997 LLVMConstInt(ctx->i1, 1, false),
1998 };
1999
2000 LLVMValueRef lsb = ac_build_intrinsic(ctx, "llvm.cttz.i32", ctx->i32,
2001 params, 2,
2002 AC_FUNC_ATTR_READNONE);
2003
2004 /* TODO: We need an intrinsic to skip this conditional. */
2005 /* Check for zero: */
2006 return LLVMBuildSelect(ctx->builder, LLVMBuildICmp(ctx->builder,
2007 LLVMIntEQ, src0,
2008 ctx->i32_0, ""),
2009 LLVMConstInt(ctx->i32, -1, 0), lsb, "");
2010 }
2011
2012 LLVMTypeRef ac_array_in_const_addr_space(LLVMTypeRef elem_type)
2013 {
2014 return LLVMPointerType(LLVMArrayType(elem_type, 0),
2015 AC_CONST_ADDR_SPACE);
2016 }