2 * Copyright 2016 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
24 #include "si_shader_internal.h"
25 #include "gallivm/lp_bld_const.h"
26 #include "gallivm/lp_bld_intr.h"
27 #include "gallivm/lp_bld_gather.h"
28 #include "tgsi/tgsi_parse.h"
29 #include "amd/common/ac_llvm_build.h"
31 static void kill_if_fetch_args(struct lp_build_tgsi_context
*bld_base
,
32 struct lp_build_emit_data
*emit_data
)
34 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
35 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
36 LLVMBuilderRef builder
= ctx
->ac
.builder
;
38 LLVMValueRef conds
[TGSI_NUM_CHANNELS
];
40 for (i
= 0; i
< TGSI_NUM_CHANNELS
; i
++) {
41 LLVMValueRef value
= lp_build_emit_fetch(bld_base
, inst
, 0, i
);
42 conds
[i
] = LLVMBuildFCmp(builder
, LLVMRealOGE
, value
,
46 /* And the conditions together */
47 for (i
= TGSI_NUM_CHANNELS
- 1; i
> 0; i
--) {
48 conds
[i
- 1] = LLVMBuildAnd(builder
, conds
[i
], conds
[i
- 1], "");
51 emit_data
->dst_type
= ctx
->voidt
;
52 emit_data
->arg_count
= 1;
53 emit_data
->args
[0] = conds
[0];
56 static void kil_emit(const struct lp_build_tgsi_action
*action
,
57 struct lp_build_tgsi_context
*bld_base
,
58 struct lp_build_emit_data
*emit_data
)
60 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
61 LLVMBuilderRef builder
= ctx
->ac
.builder
;
64 if (emit_data
->inst
->Instruction
.Opcode
== TGSI_OPCODE_KILL_IF
) {
65 visible
= emit_data
->args
[0];
67 assert(emit_data
->inst
->Instruction
.Opcode
== TGSI_OPCODE_KILL
);
68 visible
= LLVMConstInt(ctx
->i1
, false, 0);
71 if (ctx
->shader
->selector
->force_correct_derivs_after_kill
) {
72 /* LLVM 6.0 can kill immediately while maintaining WQM. */
73 if (HAVE_LLVM
>= 0x0600) {
74 ac_build_kill_if_false(&ctx
->ac
,
75 ac_build_wqm_vote(&ctx
->ac
, visible
));
78 LLVMValueRef mask
= LLVMBuildLoad(builder
, ctx
->postponed_kill
, "");
79 mask
= LLVMBuildAnd(builder
, mask
, visible
, "");
80 LLVMBuildStore(builder
, mask
, ctx
->postponed_kill
);
84 ac_build_kill_if_false(&ctx
->ac
, visible
);
87 static void emit_icmp(const struct lp_build_tgsi_action
*action
,
88 struct lp_build_tgsi_context
*bld_base
,
89 struct lp_build_emit_data
*emit_data
)
92 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
94 switch (emit_data
->inst
->Instruction
.Opcode
) {
95 case TGSI_OPCODE_USEQ
:
96 case TGSI_OPCODE_U64SEQ
: pred
= LLVMIntEQ
; break;
97 case TGSI_OPCODE_USNE
:
98 case TGSI_OPCODE_U64SNE
: pred
= LLVMIntNE
; break;
99 case TGSI_OPCODE_USGE
:
100 case TGSI_OPCODE_U64SGE
: pred
= LLVMIntUGE
; break;
101 case TGSI_OPCODE_USLT
:
102 case TGSI_OPCODE_U64SLT
: pred
= LLVMIntULT
; break;
103 case TGSI_OPCODE_ISGE
:
104 case TGSI_OPCODE_I64SGE
: pred
= LLVMIntSGE
; break;
105 case TGSI_OPCODE_ISLT
:
106 case TGSI_OPCODE_I64SLT
: pred
= LLVMIntSLT
; break;
108 assert(!"unknown instruction");
113 LLVMValueRef v
= LLVMBuildICmp(ctx
->ac
.builder
, pred
,
114 emit_data
->args
[0], emit_data
->args
[1],"");
116 v
= LLVMBuildSExtOrBitCast(ctx
->ac
.builder
, v
, ctx
->i32
, "");
118 emit_data
->output
[emit_data
->chan
] = v
;
121 static void emit_ucmp(const struct lp_build_tgsi_action
*action
,
122 struct lp_build_tgsi_context
*bld_base
,
123 struct lp_build_emit_data
*emit_data
)
125 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
126 LLVMValueRef arg0
= ac_to_integer(&ctx
->ac
, emit_data
->args
[0]);
128 LLVMValueRef v
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntNE
, arg0
,
131 emit_data
->output
[emit_data
->chan
] =
132 LLVMBuildSelect(ctx
->ac
.builder
, v
, emit_data
->args
[1], emit_data
->args
[2], "");
135 static void emit_cmp(const struct lp_build_tgsi_action
*action
,
136 struct lp_build_tgsi_context
*bld_base
,
137 struct lp_build_emit_data
*emit_data
)
139 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
140 LLVMValueRef cond
, *args
= emit_data
->args
;
142 cond
= LLVMBuildFCmp(ctx
->ac
.builder
, LLVMRealOLT
, args
[0],
145 emit_data
->output
[emit_data
->chan
] =
146 LLVMBuildSelect(ctx
->ac
.builder
, cond
, args
[1], args
[2], "");
149 static void emit_set_cond(const struct lp_build_tgsi_action
*action
,
150 struct lp_build_tgsi_context
*bld_base
,
151 struct lp_build_emit_data
*emit_data
)
153 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
154 LLVMRealPredicate pred
;
157 /* Use ordered for everything but NE (which is usual for
160 switch (emit_data
->inst
->Instruction
.Opcode
) {
161 case TGSI_OPCODE_SGE
: pred
= LLVMRealOGE
; break;
162 case TGSI_OPCODE_SEQ
: pred
= LLVMRealOEQ
; break;
163 case TGSI_OPCODE_SLE
: pred
= LLVMRealOLE
; break;
164 case TGSI_OPCODE_SLT
: pred
= LLVMRealOLT
; break;
165 case TGSI_OPCODE_SNE
: pred
= LLVMRealUNE
; break;
166 case TGSI_OPCODE_SGT
: pred
= LLVMRealOGT
; break;
167 default: assert(!"unknown instruction"); pred
= 0; break;
170 cond
= LLVMBuildFCmp(ctx
->ac
.builder
,
171 pred
, emit_data
->args
[0], emit_data
->args
[1], "");
173 emit_data
->output
[emit_data
->chan
] = LLVMBuildSelect(ctx
->ac
.builder
,
174 cond
, ctx
->ac
.f32_1
, ctx
->ac
.f32_0
, "");
177 static void emit_fcmp(const struct lp_build_tgsi_action
*action
,
178 struct lp_build_tgsi_context
*bld_base
,
179 struct lp_build_emit_data
*emit_data
)
181 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
182 LLVMRealPredicate pred
;
184 /* Use ordered for everything but NE (which is usual for
187 switch (emit_data
->inst
->Instruction
.Opcode
) {
188 case TGSI_OPCODE_FSEQ
: pred
= LLVMRealOEQ
; break;
189 case TGSI_OPCODE_FSGE
: pred
= LLVMRealOGE
; break;
190 case TGSI_OPCODE_FSLT
: pred
= LLVMRealOLT
; break;
191 case TGSI_OPCODE_FSNE
: pred
= LLVMRealUNE
; break;
192 default: assert(!"unknown instruction"); pred
= 0; break;
195 LLVMValueRef v
= LLVMBuildFCmp(ctx
->ac
.builder
, pred
,
196 emit_data
->args
[0], emit_data
->args
[1],"");
198 v
= LLVMBuildSExtOrBitCast(ctx
->ac
.builder
, v
, ctx
->i32
, "");
200 emit_data
->output
[emit_data
->chan
] = v
;
203 static void emit_dcmp(const struct lp_build_tgsi_action
*action
,
204 struct lp_build_tgsi_context
*bld_base
,
205 struct lp_build_emit_data
*emit_data
)
207 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
208 LLVMRealPredicate pred
;
210 /* Use ordered for everything but NE (which is usual for
213 switch (emit_data
->inst
->Instruction
.Opcode
) {
214 case TGSI_OPCODE_DSEQ
: pred
= LLVMRealOEQ
; break;
215 case TGSI_OPCODE_DSGE
: pred
= LLVMRealOGE
; break;
216 case TGSI_OPCODE_DSLT
: pred
= LLVMRealOLT
; break;
217 case TGSI_OPCODE_DSNE
: pred
= LLVMRealUNE
; break;
218 default: assert(!"unknown instruction"); pred
= 0; break;
221 LLVMValueRef v
= LLVMBuildFCmp(ctx
->ac
.builder
, pred
,
222 emit_data
->args
[0], emit_data
->args
[1],"");
224 v
= LLVMBuildSExtOrBitCast(ctx
->ac
.builder
, v
, ctx
->i32
, "");
226 emit_data
->output
[emit_data
->chan
] = v
;
229 static void emit_not(const struct lp_build_tgsi_action
*action
,
230 struct lp_build_tgsi_context
*bld_base
,
231 struct lp_build_emit_data
*emit_data
)
233 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
234 LLVMValueRef v
= ac_to_integer(&ctx
->ac
, emit_data
->args
[0]);
235 emit_data
->output
[emit_data
->chan
] = LLVMBuildNot(ctx
->ac
.builder
, v
, "");
238 static void emit_arl(const struct lp_build_tgsi_action
*action
,
239 struct lp_build_tgsi_context
*bld_base
,
240 struct lp_build_emit_data
*emit_data
)
242 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
243 LLVMValueRef floor_index
= lp_build_emit_llvm_unary(bld_base
, TGSI_OPCODE_FLR
, emit_data
->args
[0]);
244 emit_data
->output
[emit_data
->chan
] = LLVMBuildFPToSI(ctx
->ac
.builder
,
245 floor_index
, ctx
->i32
, "");
248 static void emit_and(const struct lp_build_tgsi_action
*action
,
249 struct lp_build_tgsi_context
*bld_base
,
250 struct lp_build_emit_data
*emit_data
)
252 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
253 emit_data
->output
[emit_data
->chan
] = LLVMBuildAnd(ctx
->ac
.builder
,
254 emit_data
->args
[0], emit_data
->args
[1], "");
257 static void emit_or(const struct lp_build_tgsi_action
*action
,
258 struct lp_build_tgsi_context
*bld_base
,
259 struct lp_build_emit_data
*emit_data
)
261 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
262 emit_data
->output
[emit_data
->chan
] = LLVMBuildOr(ctx
->ac
.builder
,
263 emit_data
->args
[0], emit_data
->args
[1], "");
266 static void emit_uadd(const struct lp_build_tgsi_action
*action
,
267 struct lp_build_tgsi_context
*bld_base
,
268 struct lp_build_emit_data
*emit_data
)
270 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
271 emit_data
->output
[emit_data
->chan
] = LLVMBuildAdd(ctx
->ac
.builder
,
272 emit_data
->args
[0], emit_data
->args
[1], "");
275 static void emit_udiv(const struct lp_build_tgsi_action
*action
,
276 struct lp_build_tgsi_context
*bld_base
,
277 struct lp_build_emit_data
*emit_data
)
279 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
280 emit_data
->output
[emit_data
->chan
] = LLVMBuildUDiv(ctx
->ac
.builder
,
281 emit_data
->args
[0], emit_data
->args
[1], "");
284 static void emit_idiv(const struct lp_build_tgsi_action
*action
,
285 struct lp_build_tgsi_context
*bld_base
,
286 struct lp_build_emit_data
*emit_data
)
288 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
289 emit_data
->output
[emit_data
->chan
] = LLVMBuildSDiv(ctx
->ac
.builder
,
290 emit_data
->args
[0], emit_data
->args
[1], "");
293 static void emit_mod(const struct lp_build_tgsi_action
*action
,
294 struct lp_build_tgsi_context
*bld_base
,
295 struct lp_build_emit_data
*emit_data
)
297 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
298 emit_data
->output
[emit_data
->chan
] = LLVMBuildSRem(ctx
->ac
.builder
,
299 emit_data
->args
[0], emit_data
->args
[1], "");
302 static void emit_umod(const struct lp_build_tgsi_action
*action
,
303 struct lp_build_tgsi_context
*bld_base
,
304 struct lp_build_emit_data
*emit_data
)
306 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
307 emit_data
->output
[emit_data
->chan
] = LLVMBuildURem(ctx
->ac
.builder
,
308 emit_data
->args
[0], emit_data
->args
[1], "");
311 static void emit_shl(const struct lp_build_tgsi_action
*action
,
312 struct lp_build_tgsi_context
*bld_base
,
313 struct lp_build_emit_data
*emit_data
)
315 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
316 emit_data
->output
[emit_data
->chan
] = LLVMBuildShl(ctx
->ac
.builder
,
317 emit_data
->args
[0], emit_data
->args
[1], "");
320 static void emit_ushr(const struct lp_build_tgsi_action
*action
,
321 struct lp_build_tgsi_context
*bld_base
,
322 struct lp_build_emit_data
*emit_data
)
324 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
325 emit_data
->output
[emit_data
->chan
] = LLVMBuildLShr(ctx
->ac
.builder
,
326 emit_data
->args
[0], emit_data
->args
[1], "");
328 static void emit_ishr(const struct lp_build_tgsi_action
*action
,
329 struct lp_build_tgsi_context
*bld_base
,
330 struct lp_build_emit_data
*emit_data
)
332 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
333 emit_data
->output
[emit_data
->chan
] = LLVMBuildAShr(ctx
->ac
.builder
,
334 emit_data
->args
[0], emit_data
->args
[1], "");
337 static void emit_xor(const struct lp_build_tgsi_action
*action
,
338 struct lp_build_tgsi_context
*bld_base
,
339 struct lp_build_emit_data
*emit_data
)
341 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
342 emit_data
->output
[emit_data
->chan
] = LLVMBuildXor(ctx
->ac
.builder
,
343 emit_data
->args
[0], emit_data
->args
[1], "");
346 static void emit_ssg(const struct lp_build_tgsi_action
*action
,
347 struct lp_build_tgsi_context
*bld_base
,
348 struct lp_build_emit_data
*emit_data
)
350 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
351 LLVMBuilderRef builder
= ctx
->ac
.builder
;
353 LLVMValueRef cmp
, val
;
355 if (emit_data
->inst
->Instruction
.Opcode
== TGSI_OPCODE_I64SSG
) {
356 val
= ac_build_isign(&ctx
->ac
, emit_data
->args
[0], 64);
357 } else if (emit_data
->inst
->Instruction
.Opcode
== TGSI_OPCODE_ISSG
) {
358 val
= ac_build_isign(&ctx
->ac
, emit_data
->args
[0], 32);
359 } else if (emit_data
->inst
->Instruction
.Opcode
== TGSI_OPCODE_DSSG
) {
360 cmp
= LLVMBuildFCmp(builder
, LLVMRealOGT
, emit_data
->args
[0], bld_base
->dbl_bld
.zero
, "");
361 val
= LLVMBuildSelect(builder
, cmp
, bld_base
->dbl_bld
.one
, emit_data
->args
[0], "");
362 cmp
= LLVMBuildFCmp(builder
, LLVMRealOGE
, val
, bld_base
->dbl_bld
.zero
, "");
363 val
= LLVMBuildSelect(builder
, cmp
, val
, LLVMConstReal(bld_base
->dbl_bld
.elem_type
, -1), "");
364 } else { // float SSG
365 cmp
= LLVMBuildFCmp(builder
, LLVMRealOGT
, emit_data
->args
[0], ctx
->ac
.f32_0
, "");
366 val
= LLVMBuildSelect(builder
, cmp
, ctx
->ac
.f32_1
, emit_data
->args
[0], "");
367 cmp
= LLVMBuildFCmp(builder
, LLVMRealOGE
, val
, ctx
->ac
.f32_0
, "");
368 val
= LLVMBuildSelect(builder
, cmp
, val
, LLVMConstReal(ctx
->f32
, -1), "");
371 emit_data
->output
[emit_data
->chan
] = val
;
374 static void emit_ineg(const struct lp_build_tgsi_action
*action
,
375 struct lp_build_tgsi_context
*bld_base
,
376 struct lp_build_emit_data
*emit_data
)
378 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
379 emit_data
->output
[emit_data
->chan
] = LLVMBuildNeg(ctx
->ac
.builder
,
380 emit_data
->args
[0], "");
383 static void emit_dneg(const struct lp_build_tgsi_action
*action
,
384 struct lp_build_tgsi_context
*bld_base
,
385 struct lp_build_emit_data
*emit_data
)
387 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
388 emit_data
->output
[emit_data
->chan
] = LLVMBuildFNeg(ctx
->ac
.builder
,
389 emit_data
->args
[0], "");
392 static void emit_frac(const struct lp_build_tgsi_action
*action
,
393 struct lp_build_tgsi_context
*bld_base
,
394 struct lp_build_emit_data
*emit_data
)
396 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
399 if (emit_data
->info
->opcode
== TGSI_OPCODE_FRC
)
401 else if (emit_data
->info
->opcode
== TGSI_OPCODE_DFRAC
)
408 emit_data
->output
[emit_data
->chan
] =
409 ac_build_fract(&ctx
->ac
, emit_data
->args
[0], bitsize
);
412 static void emit_f2i(const struct lp_build_tgsi_action
*action
,
413 struct lp_build_tgsi_context
*bld_base
,
414 struct lp_build_emit_data
*emit_data
)
416 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
417 emit_data
->output
[emit_data
->chan
] = LLVMBuildFPToSI(ctx
->ac
.builder
,
418 emit_data
->args
[0], ctx
->i32
, "");
421 static void emit_f2u(const struct lp_build_tgsi_action
*action
,
422 struct lp_build_tgsi_context
*bld_base
,
423 struct lp_build_emit_data
*emit_data
)
425 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
426 emit_data
->output
[emit_data
->chan
] = LLVMBuildFPToUI(ctx
->ac
.builder
,
427 emit_data
->args
[0], ctx
->i32
, "");
430 static void emit_i2f(const struct lp_build_tgsi_action
*action
,
431 struct lp_build_tgsi_context
*bld_base
,
432 struct lp_build_emit_data
*emit_data
)
434 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
435 emit_data
->output
[emit_data
->chan
] = LLVMBuildSIToFP(ctx
->ac
.builder
,
436 emit_data
->args
[0], ctx
->f32
, "");
439 static void emit_u2f(const struct lp_build_tgsi_action
*action
,
440 struct lp_build_tgsi_context
*bld_base
,
441 struct lp_build_emit_data
*emit_data
)
443 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
444 emit_data
->output
[emit_data
->chan
] = LLVMBuildUIToFP(ctx
->ac
.builder
,
445 emit_data
->args
[0], ctx
->f32
, "");
449 build_tgsi_intrinsic_nomem(const struct lp_build_tgsi_action
*action
,
450 struct lp_build_tgsi_context
*bld_base
,
451 struct lp_build_emit_data
*emit_data
)
453 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
454 emit_data
->output
[emit_data
->chan
] =
455 lp_build_intrinsic(ctx
->ac
.builder
, action
->intr_name
,
456 emit_data
->dst_type
, emit_data
->args
,
457 emit_data
->arg_count
, LP_FUNC_ATTR_READNONE
);
460 static void emit_bfi(const struct lp_build_tgsi_action
*action
,
461 struct lp_build_tgsi_context
*bld_base
,
462 struct lp_build_emit_data
*emit_data
)
464 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
465 LLVMBuilderRef builder
= ctx
->ac
.builder
;
466 LLVMValueRef bfi_args
[3];
467 LLVMValueRef bfi_sm5
;
470 // Calculate the bitmask: (((1 << src3) - 1) << src2
471 bfi_args
[0] = LLVMBuildShl(builder
,
472 LLVMBuildSub(builder
,
473 LLVMBuildShl(builder
,
475 emit_data
->args
[3], ""),
477 emit_data
->args
[2], "");
479 bfi_args
[1] = LLVMBuildShl(builder
, emit_data
->args
[1],
480 emit_data
->args
[2], "");
482 bfi_args
[2] = emit_data
->args
[0];
485 * (arg0 & arg1) | (~arg0 & arg2) = arg2 ^ (arg0 & (arg1 ^ arg2)
486 * Use the right-hand side, which the LLVM backend can convert to V_BFI.
489 LLVMBuildXor(builder
, bfi_args
[2],
490 LLVMBuildAnd(builder
, bfi_args
[0],
491 LLVMBuildXor(builder
, bfi_args
[1], bfi_args
[2],
494 /* Since shifts of >= 32 bits are undefined in LLVM IR, the backend
495 * uses the convenient V_BFI lowering for the above, which follows SM5
496 * and disagrees with GLSL semantics when bits (src3) is 32.
498 cond
= LLVMBuildICmp(builder
, LLVMIntUGE
, emit_data
->args
[3],
499 LLVMConstInt(ctx
->i32
, 32, 0), "");
500 emit_data
->output
[emit_data
->chan
] =
501 LLVMBuildSelect(builder
, cond
, emit_data
->args
[1], bfi_sm5
, "");
504 static void emit_bfe(const struct lp_build_tgsi_action
*action
,
505 struct lp_build_tgsi_context
*bld_base
,
506 struct lp_build_emit_data
*emit_data
)
508 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
509 LLVMValueRef bfe_sm5
;
512 bfe_sm5
= ac_build_bfe(&ctx
->ac
, emit_data
->args
[0],
513 emit_data
->args
[1], emit_data
->args
[2],
514 emit_data
->info
->opcode
== TGSI_OPCODE_IBFE
);
516 /* Correct for GLSL semantics. */
517 cond
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntUGE
, emit_data
->args
[2],
518 LLVMConstInt(ctx
->i32
, 32, 0), "");
519 emit_data
->output
[emit_data
->chan
] =
520 LLVMBuildSelect(ctx
->ac
.builder
, cond
, emit_data
->args
[0], bfe_sm5
, "");
523 /* this is ffs in C */
524 static void emit_lsb(const struct lp_build_tgsi_action
*action
,
525 struct lp_build_tgsi_context
*bld_base
,
526 struct lp_build_emit_data
*emit_data
)
528 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
530 emit_data
->output
[emit_data
->chan
] = ac_find_lsb(&ctx
->ac
, emit_data
->dst_type
, emit_data
->args
[0]);
533 /* Find the last bit set. */
534 static void emit_umsb(const struct lp_build_tgsi_action
*action
,
535 struct lp_build_tgsi_context
*bld_base
,
536 struct lp_build_emit_data
*emit_data
)
538 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
540 emit_data
->output
[emit_data
->chan
] =
541 ac_build_umsb(&ctx
->ac
, emit_data
->args
[0], emit_data
->dst_type
);
544 /* Find the last bit opposite of the sign bit. */
545 static void emit_imsb(const struct lp_build_tgsi_action
*action
,
546 struct lp_build_tgsi_context
*bld_base
,
547 struct lp_build_emit_data
*emit_data
)
549 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
550 emit_data
->output
[emit_data
->chan
] =
551 ac_build_imsb(&ctx
->ac
, emit_data
->args
[0],
552 emit_data
->dst_type
);
555 static void emit_iabs(const struct lp_build_tgsi_action
*action
,
556 struct lp_build_tgsi_context
*bld_base
,
557 struct lp_build_emit_data
*emit_data
)
559 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
561 emit_data
->output
[emit_data
->chan
] =
562 lp_build_emit_llvm_binary(bld_base
, TGSI_OPCODE_IMAX
,
564 LLVMBuildNeg(ctx
->ac
.builder
,
565 emit_data
->args
[0], ""));
568 static void emit_minmax_int(const struct lp_build_tgsi_action
*action
,
569 struct lp_build_tgsi_context
*bld_base
,
570 struct lp_build_emit_data
*emit_data
)
572 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
575 switch (emit_data
->info
->opcode
) {
578 case TGSI_OPCODE_IMAX
:
579 case TGSI_OPCODE_I64MAX
:
582 case TGSI_OPCODE_IMIN
:
583 case TGSI_OPCODE_I64MIN
:
586 case TGSI_OPCODE_UMAX
:
587 case TGSI_OPCODE_U64MAX
:
590 case TGSI_OPCODE_UMIN
:
591 case TGSI_OPCODE_U64MIN
:
596 emit_data
->output
[emit_data
->chan
] =
597 LLVMBuildSelect(ctx
->ac
.builder
,
598 LLVMBuildICmp(ctx
->ac
.builder
, op
, emit_data
->args
[0],
599 emit_data
->args
[1], ""),
601 emit_data
->args
[1], "");
604 static void pk2h_fetch_args(struct lp_build_tgsi_context
*bld_base
,
605 struct lp_build_emit_data
*emit_data
)
607 emit_data
->args
[0] = lp_build_emit_fetch(bld_base
, emit_data
->inst
,
609 emit_data
->args
[1] = lp_build_emit_fetch(bld_base
, emit_data
->inst
,
613 static void emit_pk2h(const struct lp_build_tgsi_action
*action
,
614 struct lp_build_tgsi_context
*bld_base
,
615 struct lp_build_emit_data
*emit_data
)
617 /* From the GLSL 4.50 spec:
618 * "The rounding mode cannot be set and is undefined."
620 * v_cvt_pkrtz_f16 rounds to zero, but it's fastest.
622 emit_data
->output
[emit_data
->chan
] =
623 ac_build_cvt_pkrtz_f16(&si_shader_context(bld_base
)->ac
,
627 static void up2h_fetch_args(struct lp_build_tgsi_context
*bld_base
,
628 struct lp_build_emit_data
*emit_data
)
630 emit_data
->args
[0] = lp_build_emit_fetch(bld_base
, emit_data
->inst
,
634 static void emit_up2h(const struct lp_build_tgsi_action
*action
,
635 struct lp_build_tgsi_context
*bld_base
,
636 struct lp_build_emit_data
*emit_data
)
638 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
640 LLVMValueRef const16
, input
, val
;
643 i16
= LLVMInt16TypeInContext(ctx
->ac
.context
);
644 const16
= LLVMConstInt(ctx
->i32
, 16, 0);
645 input
= emit_data
->args
[0];
647 for (i
= 0; i
< 2; i
++) {
648 val
= i
== 1 ? LLVMBuildLShr(ctx
->ac
.builder
, input
, const16
, "") : input
;
649 val
= LLVMBuildTrunc(ctx
->ac
.builder
, val
, i16
, "");
650 val
= ac_to_float(&ctx
->ac
, val
);
651 emit_data
->output
[i
] = LLVMBuildFPExt(ctx
->ac
.builder
, val
, ctx
->f32
, "");
655 static void emit_fdiv(const struct lp_build_tgsi_action
*action
,
656 struct lp_build_tgsi_context
*bld_base
,
657 struct lp_build_emit_data
*emit_data
)
659 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
661 emit_data
->output
[emit_data
->chan
] =
662 ac_build_fdiv(&ctx
->ac
, emit_data
->args
[0], emit_data
->args
[1]);
665 /* 1/sqrt is translated to rsq for f32 if fp32 denormals are not enabled in
666 * the target machine. f64 needs global unsafe math flags to get rsq. */
667 static void emit_rsq(const struct lp_build_tgsi_action
*action
,
668 struct lp_build_tgsi_context
*bld_base
,
669 struct lp_build_emit_data
*emit_data
)
671 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
674 lp_build_emit_llvm_unary(bld_base
, TGSI_OPCODE_SQRT
,
677 emit_data
->output
[emit_data
->chan
] =
678 lp_build_emit_llvm_binary(bld_base
, TGSI_OPCODE_DIV
,
679 ctx
->ac
.f32_1
, sqrt
);
682 static void dfracexp_fetch_args(struct lp_build_tgsi_context
*bld_base
,
683 struct lp_build_emit_data
*emit_data
)
685 emit_data
->args
[0] = lp_build_emit_fetch(bld_base
, emit_data
->inst
, 0, TGSI_CHAN_X
);
686 emit_data
->arg_count
= 1;
689 static void dfracexp_emit(const struct lp_build_tgsi_action
*action
,
690 struct lp_build_tgsi_context
*bld_base
,
691 struct lp_build_emit_data
*emit_data
)
693 struct si_shader_context
*ctx
= si_shader_context(bld_base
);
695 emit_data
->output
[emit_data
->chan
] =
696 lp_build_intrinsic(ctx
->ac
.builder
, "llvm.amdgcn.frexp.mant.f64",
697 ctx
->ac
.f64
, &emit_data
->args
[0], 1, 0);
698 emit_data
->output1
[emit_data
->chan
] =
699 lp_build_intrinsic(ctx
->ac
.builder
, "llvm.amdgcn.frexp.exp.i32.f64",
700 ctx
->ac
.i32
, &emit_data
->args
[0], 1, 0);
703 void si_shader_context_init_alu(struct lp_build_tgsi_context
*bld_base
)
705 lp_set_default_actions(bld_base
);
707 bld_base
->op_actions
[TGSI_OPCODE_AND
].emit
= emit_and
;
708 bld_base
->op_actions
[TGSI_OPCODE_ARL
].emit
= emit_arl
;
709 bld_base
->op_actions
[TGSI_OPCODE_BFI
].emit
= emit_bfi
;
710 bld_base
->op_actions
[TGSI_OPCODE_BREV
].emit
= build_tgsi_intrinsic_nomem
;
711 bld_base
->op_actions
[TGSI_OPCODE_BREV
].intr_name
= "llvm.bitreverse.i32";
712 bld_base
->op_actions
[TGSI_OPCODE_CEIL
].emit
= build_tgsi_intrinsic_nomem
;
713 bld_base
->op_actions
[TGSI_OPCODE_CEIL
].intr_name
= "llvm.ceil.f32";
714 bld_base
->op_actions
[TGSI_OPCODE_CMP
].emit
= emit_cmp
;
715 bld_base
->op_actions
[TGSI_OPCODE_COS
].emit
= build_tgsi_intrinsic_nomem
;
716 bld_base
->op_actions
[TGSI_OPCODE_COS
].intr_name
= "llvm.cos.f32";
717 bld_base
->op_actions
[TGSI_OPCODE_DABS
].emit
= build_tgsi_intrinsic_nomem
;
718 bld_base
->op_actions
[TGSI_OPCODE_DABS
].intr_name
= "llvm.fabs.f64";
719 bld_base
->op_actions
[TGSI_OPCODE_DCEIL
].emit
= build_tgsi_intrinsic_nomem
;
720 bld_base
->op_actions
[TGSI_OPCODE_DCEIL
].intr_name
= "llvm.ceil.f64";
721 bld_base
->op_actions
[TGSI_OPCODE_DFLR
].emit
= build_tgsi_intrinsic_nomem
;
722 bld_base
->op_actions
[TGSI_OPCODE_DFLR
].intr_name
= "llvm.floor.f64";
723 bld_base
->op_actions
[TGSI_OPCODE_DFMA
].emit
= build_tgsi_intrinsic_nomem
;
724 bld_base
->op_actions
[TGSI_OPCODE_DFMA
].intr_name
= "llvm.fma.f64";
725 bld_base
->op_actions
[TGSI_OPCODE_DFRAC
].emit
= emit_frac
;
726 bld_base
->op_actions
[TGSI_OPCODE_DIV
].emit
= emit_fdiv
;
727 bld_base
->op_actions
[TGSI_OPCODE_DNEG
].emit
= emit_dneg
;
728 bld_base
->op_actions
[TGSI_OPCODE_DROUND
].emit
= build_tgsi_intrinsic_nomem
;
729 bld_base
->op_actions
[TGSI_OPCODE_DROUND
].intr_name
= "llvm.rint.f64";
730 bld_base
->op_actions
[TGSI_OPCODE_DSEQ
].emit
= emit_dcmp
;
731 bld_base
->op_actions
[TGSI_OPCODE_DSGE
].emit
= emit_dcmp
;
732 bld_base
->op_actions
[TGSI_OPCODE_DSLT
].emit
= emit_dcmp
;
733 bld_base
->op_actions
[TGSI_OPCODE_DSNE
].emit
= emit_dcmp
;
734 bld_base
->op_actions
[TGSI_OPCODE_DSSG
].emit
= emit_ssg
;
735 bld_base
->op_actions
[TGSI_OPCODE_DRSQ
].emit
= build_tgsi_intrinsic_nomem
;
736 bld_base
->op_actions
[TGSI_OPCODE_DRSQ
].intr_name
= "llvm.amdgcn.rsq.f64";
737 bld_base
->op_actions
[TGSI_OPCODE_DSQRT
].emit
= build_tgsi_intrinsic_nomem
;
738 bld_base
->op_actions
[TGSI_OPCODE_DSQRT
].intr_name
= "llvm.sqrt.f64";
739 bld_base
->op_actions
[TGSI_OPCODE_DTRUNC
].emit
= build_tgsi_intrinsic_nomem
;
740 bld_base
->op_actions
[TGSI_OPCODE_DTRUNC
].intr_name
= "llvm.trunc.f64";
741 bld_base
->op_actions
[TGSI_OPCODE_DFRACEXP
].fetch_args
= dfracexp_fetch_args
;
742 bld_base
->op_actions
[TGSI_OPCODE_DFRACEXP
].emit
= dfracexp_emit
;
743 bld_base
->op_actions
[TGSI_OPCODE_DLDEXP
].emit
= build_tgsi_intrinsic_nomem
;
744 bld_base
->op_actions
[TGSI_OPCODE_DLDEXP
].intr_name
= "llvm.amdgcn.ldexp.f64";
745 bld_base
->op_actions
[TGSI_OPCODE_EX2
].emit
= build_tgsi_intrinsic_nomem
;
746 bld_base
->op_actions
[TGSI_OPCODE_EX2
].intr_name
= "llvm.exp2.f32";
747 bld_base
->op_actions
[TGSI_OPCODE_FLR
].emit
= build_tgsi_intrinsic_nomem
;
748 bld_base
->op_actions
[TGSI_OPCODE_FLR
].intr_name
= "llvm.floor.f32";
749 bld_base
->op_actions
[TGSI_OPCODE_FMA
].emit
=
750 bld_base
->op_actions
[TGSI_OPCODE_MAD
].emit
;
751 bld_base
->op_actions
[TGSI_OPCODE_FRC
].emit
= emit_frac
;
752 bld_base
->op_actions
[TGSI_OPCODE_F2I
].emit
= emit_f2i
;
753 bld_base
->op_actions
[TGSI_OPCODE_F2U
].emit
= emit_f2u
;
754 bld_base
->op_actions
[TGSI_OPCODE_FSEQ
].emit
= emit_fcmp
;
755 bld_base
->op_actions
[TGSI_OPCODE_FSGE
].emit
= emit_fcmp
;
756 bld_base
->op_actions
[TGSI_OPCODE_FSLT
].emit
= emit_fcmp
;
757 bld_base
->op_actions
[TGSI_OPCODE_FSNE
].emit
= emit_fcmp
;
758 bld_base
->op_actions
[TGSI_OPCODE_IABS
].emit
= emit_iabs
;
759 bld_base
->op_actions
[TGSI_OPCODE_IBFE
].emit
= emit_bfe
;
760 bld_base
->op_actions
[TGSI_OPCODE_IDIV
].emit
= emit_idiv
;
761 bld_base
->op_actions
[TGSI_OPCODE_IMAX
].emit
= emit_minmax_int
;
762 bld_base
->op_actions
[TGSI_OPCODE_IMIN
].emit
= emit_minmax_int
;
763 bld_base
->op_actions
[TGSI_OPCODE_IMSB
].emit
= emit_imsb
;
764 bld_base
->op_actions
[TGSI_OPCODE_INEG
].emit
= emit_ineg
;
765 bld_base
->op_actions
[TGSI_OPCODE_ISHR
].emit
= emit_ishr
;
766 bld_base
->op_actions
[TGSI_OPCODE_ISGE
].emit
= emit_icmp
;
767 bld_base
->op_actions
[TGSI_OPCODE_ISLT
].emit
= emit_icmp
;
768 bld_base
->op_actions
[TGSI_OPCODE_ISSG
].emit
= emit_ssg
;
769 bld_base
->op_actions
[TGSI_OPCODE_I2F
].emit
= emit_i2f
;
770 bld_base
->op_actions
[TGSI_OPCODE_KILL_IF
].fetch_args
= kill_if_fetch_args
;
771 bld_base
->op_actions
[TGSI_OPCODE_KILL_IF
].emit
= kil_emit
;
772 bld_base
->op_actions
[TGSI_OPCODE_KILL
].emit
= kil_emit
;
773 bld_base
->op_actions
[TGSI_OPCODE_LDEXP
].emit
= build_tgsi_intrinsic_nomem
;
774 bld_base
->op_actions
[TGSI_OPCODE_LDEXP
].intr_name
= "llvm.amdgcn.ldexp.f32";
775 bld_base
->op_actions
[TGSI_OPCODE_LSB
].emit
= emit_lsb
;
776 bld_base
->op_actions
[TGSI_OPCODE_LG2
].emit
= build_tgsi_intrinsic_nomem
;
777 bld_base
->op_actions
[TGSI_OPCODE_LG2
].intr_name
= "llvm.log2.f32";
778 bld_base
->op_actions
[TGSI_OPCODE_MAX
].emit
= build_tgsi_intrinsic_nomem
;
779 bld_base
->op_actions
[TGSI_OPCODE_MAX
].intr_name
= "llvm.maxnum.f32";
780 bld_base
->op_actions
[TGSI_OPCODE_MIN
].emit
= build_tgsi_intrinsic_nomem
;
781 bld_base
->op_actions
[TGSI_OPCODE_MIN
].intr_name
= "llvm.minnum.f32";
782 bld_base
->op_actions
[TGSI_OPCODE_MOD
].emit
= emit_mod
;
783 bld_base
->op_actions
[TGSI_OPCODE_UMSB
].emit
= emit_umsb
;
784 bld_base
->op_actions
[TGSI_OPCODE_NOT
].emit
= emit_not
;
785 bld_base
->op_actions
[TGSI_OPCODE_OR
].emit
= emit_or
;
786 bld_base
->op_actions
[TGSI_OPCODE_PK2H
].fetch_args
= pk2h_fetch_args
;
787 bld_base
->op_actions
[TGSI_OPCODE_PK2H
].emit
= emit_pk2h
;
788 bld_base
->op_actions
[TGSI_OPCODE_POPC
].emit
= build_tgsi_intrinsic_nomem
;
789 bld_base
->op_actions
[TGSI_OPCODE_POPC
].intr_name
= "llvm.ctpop.i32";
790 bld_base
->op_actions
[TGSI_OPCODE_POW
].emit
= build_tgsi_intrinsic_nomem
;
791 bld_base
->op_actions
[TGSI_OPCODE_POW
].intr_name
= "llvm.pow.f32";
792 bld_base
->op_actions
[TGSI_OPCODE_ROUND
].emit
= build_tgsi_intrinsic_nomem
;
793 bld_base
->op_actions
[TGSI_OPCODE_ROUND
].intr_name
= "llvm.rint.f32";
794 bld_base
->op_actions
[TGSI_OPCODE_RSQ
].emit
= emit_rsq
;
795 bld_base
->op_actions
[TGSI_OPCODE_SGE
].emit
= emit_set_cond
;
796 bld_base
->op_actions
[TGSI_OPCODE_SEQ
].emit
= emit_set_cond
;
797 bld_base
->op_actions
[TGSI_OPCODE_SHL
].emit
= emit_shl
;
798 bld_base
->op_actions
[TGSI_OPCODE_SLE
].emit
= emit_set_cond
;
799 bld_base
->op_actions
[TGSI_OPCODE_SLT
].emit
= emit_set_cond
;
800 bld_base
->op_actions
[TGSI_OPCODE_SNE
].emit
= emit_set_cond
;
801 bld_base
->op_actions
[TGSI_OPCODE_SGT
].emit
= emit_set_cond
;
802 bld_base
->op_actions
[TGSI_OPCODE_SIN
].emit
= build_tgsi_intrinsic_nomem
;
803 bld_base
->op_actions
[TGSI_OPCODE_SIN
].intr_name
= "llvm.sin.f32";
804 bld_base
->op_actions
[TGSI_OPCODE_SQRT
].emit
= build_tgsi_intrinsic_nomem
;
805 bld_base
->op_actions
[TGSI_OPCODE_SQRT
].intr_name
= "llvm.sqrt.f32";
806 bld_base
->op_actions
[TGSI_OPCODE_SSG
].emit
= emit_ssg
;
807 bld_base
->op_actions
[TGSI_OPCODE_TRUNC
].emit
= build_tgsi_intrinsic_nomem
;
808 bld_base
->op_actions
[TGSI_OPCODE_TRUNC
].intr_name
= "llvm.trunc.f32";
809 bld_base
->op_actions
[TGSI_OPCODE_UADD
].emit
= emit_uadd
;
810 bld_base
->op_actions
[TGSI_OPCODE_UBFE
].emit
= emit_bfe
;
811 bld_base
->op_actions
[TGSI_OPCODE_UDIV
].emit
= emit_udiv
;
812 bld_base
->op_actions
[TGSI_OPCODE_UMAX
].emit
= emit_minmax_int
;
813 bld_base
->op_actions
[TGSI_OPCODE_UMIN
].emit
= emit_minmax_int
;
814 bld_base
->op_actions
[TGSI_OPCODE_UMOD
].emit
= emit_umod
;
815 bld_base
->op_actions
[TGSI_OPCODE_USEQ
].emit
= emit_icmp
;
816 bld_base
->op_actions
[TGSI_OPCODE_USGE
].emit
= emit_icmp
;
817 bld_base
->op_actions
[TGSI_OPCODE_USHR
].emit
= emit_ushr
;
818 bld_base
->op_actions
[TGSI_OPCODE_USLT
].emit
= emit_icmp
;
819 bld_base
->op_actions
[TGSI_OPCODE_USNE
].emit
= emit_icmp
;
820 bld_base
->op_actions
[TGSI_OPCODE_U2F
].emit
= emit_u2f
;
821 bld_base
->op_actions
[TGSI_OPCODE_XOR
].emit
= emit_xor
;
822 bld_base
->op_actions
[TGSI_OPCODE_UCMP
].emit
= emit_ucmp
;
823 bld_base
->op_actions
[TGSI_OPCODE_UP2H
].fetch_args
= up2h_fetch_args
;
824 bld_base
->op_actions
[TGSI_OPCODE_UP2H
].emit
= emit_up2h
;
826 bld_base
->op_actions
[TGSI_OPCODE_I64MAX
].emit
= emit_minmax_int
;
827 bld_base
->op_actions
[TGSI_OPCODE_I64MIN
].emit
= emit_minmax_int
;
828 bld_base
->op_actions
[TGSI_OPCODE_U64MAX
].emit
= emit_minmax_int
;
829 bld_base
->op_actions
[TGSI_OPCODE_U64MIN
].emit
= emit_minmax_int
;
830 bld_base
->op_actions
[TGSI_OPCODE_I64ABS
].emit
= emit_iabs
;
831 bld_base
->op_actions
[TGSI_OPCODE_I64SSG
].emit
= emit_ssg
;
832 bld_base
->op_actions
[TGSI_OPCODE_I64NEG
].emit
= emit_ineg
;
834 bld_base
->op_actions
[TGSI_OPCODE_U64SEQ
].emit
= emit_icmp
;
835 bld_base
->op_actions
[TGSI_OPCODE_U64SNE
].emit
= emit_icmp
;
836 bld_base
->op_actions
[TGSI_OPCODE_U64SGE
].emit
= emit_icmp
;
837 bld_base
->op_actions
[TGSI_OPCODE_U64SLT
].emit
= emit_icmp
;
838 bld_base
->op_actions
[TGSI_OPCODE_I64SGE
].emit
= emit_icmp
;
839 bld_base
->op_actions
[TGSI_OPCODE_I64SLT
].emit
= emit_icmp
;
841 bld_base
->op_actions
[TGSI_OPCODE_U64ADD
].emit
= emit_uadd
;
842 bld_base
->op_actions
[TGSI_OPCODE_U64SHL
].emit
= emit_shl
;
843 bld_base
->op_actions
[TGSI_OPCODE_U64SHR
].emit
= emit_ushr
;
844 bld_base
->op_actions
[TGSI_OPCODE_I64SHR
].emit
= emit_ishr
;
846 bld_base
->op_actions
[TGSI_OPCODE_U64MOD
].emit
= emit_umod
;
847 bld_base
->op_actions
[TGSI_OPCODE_I64MOD
].emit
= emit_mod
;
848 bld_base
->op_actions
[TGSI_OPCODE_U64DIV
].emit
= emit_udiv
;
849 bld_base
->op_actions
[TGSI_OPCODE_I64DIV
].emit
= emit_idiv
;