2 * Copyright 2011 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 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * 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 NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * Authors: Tom Stellard <thomas.stellard@amd.com>
26 #include "radeon_llvm.h"
28 #include "gallivm/lp_bld_const.h"
29 #include "gallivm/lp_bld_gather.h"
30 #include "gallivm/lp_bld_flow.h"
31 #include "gallivm/lp_bld_init.h"
32 #include "gallivm/lp_bld_intr.h"
33 #include "gallivm/lp_bld_misc.h"
34 #include "gallivm/lp_bld_swizzle.h"
35 #include "tgsi/tgsi_info.h"
36 #include "tgsi/tgsi_parse.h"
37 #include "util/u_math.h"
38 #include "util/u_memory.h"
39 #include "util/u_debug.h"
41 #include <llvm-c/Core.h>
42 #include <llvm-c/Transforms/Scalar.h>
44 LLVMTypeRef
tgsi2llvmtype(struct lp_build_tgsi_context
*bld_base
,
45 enum tgsi_opcode_type type
)
47 LLVMContextRef ctx
= bld_base
->base
.gallivm
->context
;
50 case TGSI_TYPE_UNSIGNED
:
51 case TGSI_TYPE_SIGNED
:
52 return LLVMInt32TypeInContext(ctx
);
53 case TGSI_TYPE_DOUBLE
:
54 return LLVMDoubleTypeInContext(ctx
);
55 case TGSI_TYPE_UNTYPED
:
57 return LLVMFloatTypeInContext(ctx
);
63 LLVMValueRef
bitcast(struct lp_build_tgsi_context
*bld_base
,
64 enum tgsi_opcode_type type
, LLVMValueRef value
)
66 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
67 LLVMTypeRef dst_type
= tgsi2llvmtype(bld_base
, type
);
70 return LLVMBuildBitCast(builder
, value
, dst_type
, "");
75 static struct radeon_llvm_loop
*get_current_loop(struct radeon_llvm_context
*ctx
)
77 return ctx
->loop_depth
> 0 ? ctx
->loop
+ (ctx
->loop_depth
- 1) : NULL
;
80 static struct radeon_llvm_branch
*get_current_branch(struct radeon_llvm_context
*ctx
)
82 return ctx
->branch_depth
> 0 ?
83 ctx
->branch
+ (ctx
->branch_depth
- 1) : NULL
;
86 unsigned radeon_llvm_reg_index_soa(unsigned index
, unsigned chan
)
88 return (index
* 4) + chan
;
91 static LLVMValueRef
emit_swizzle(struct lp_build_tgsi_context
*bld_base
,
98 LLVMValueRef swizzles
[4];
100 LLVMInt32TypeInContext(bld_base
->base
.gallivm
->context
);
102 swizzles
[0] = LLVMConstInt(i32t
, swizzle_x
, 0);
103 swizzles
[1] = LLVMConstInt(i32t
, swizzle_y
, 0);
104 swizzles
[2] = LLVMConstInt(i32t
, swizzle_z
, 0);
105 swizzles
[3] = LLVMConstInt(i32t
, swizzle_w
, 0);
107 return LLVMBuildShuffleVector(bld_base
->base
.gallivm
->builder
,
109 LLVMGetUndef(LLVMTypeOf(value
)),
110 LLVMConstVector(swizzles
, 4), "");
113 static struct tgsi_declaration_range
114 get_array_range(struct lp_build_tgsi_context
*bld_base
,
115 unsigned File
, const struct tgsi_ind_register
*reg
)
117 struct radeon_llvm_context
*ctx
= radeon_llvm_context(bld_base
);
119 if (File
!= TGSI_FILE_TEMPORARY
|| reg
->ArrayID
== 0 ||
120 reg
->ArrayID
> bld_base
->info
->array_max
[TGSI_FILE_TEMPORARY
]) {
121 struct tgsi_declaration_range range
;
123 range
.Last
= bld_base
->info
->file_max
[File
];
127 return ctx
->arrays
[reg
->ArrayID
- 1];
131 emit_array_index(struct lp_build_tgsi_soa_context
*bld
,
132 const struct tgsi_ind_register
*reg
,
135 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
137 LLVMValueRef addr
= LLVMBuildLoad(gallivm
->builder
, bld
->addr
[reg
->Index
][reg
->Swizzle
], "");
138 return LLVMBuildAdd(gallivm
->builder
, addr
, lp_build_const_int32(gallivm
, offset
), "");
142 radeon_llvm_emit_fetch_64bit(struct lp_build_tgsi_context
*bld_base
,
143 enum tgsi_opcode_type type
,
147 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
150 result
= LLVMGetUndef(LLVMVectorType(LLVMIntTypeInContext(bld_base
->base
.gallivm
->context
, 32), bld_base
->base
.type
.length
* 2));
152 result
= LLVMBuildInsertElement(builder
,
154 bitcast(bld_base
, TGSI_TYPE_UNSIGNED
, ptr
),
155 bld_base
->int_bld
.zero
, "");
156 result
= LLVMBuildInsertElement(builder
,
158 bitcast(bld_base
, TGSI_TYPE_UNSIGNED
, ptr2
),
159 bld_base
->int_bld
.one
, "");
160 return bitcast(bld_base
, type
, result
);
164 emit_array_fetch(struct lp_build_tgsi_context
*bld_base
,
165 unsigned File
, enum tgsi_opcode_type type
,
166 struct tgsi_declaration_range range
,
169 struct lp_build_tgsi_soa_context
*bld
= lp_soa_context(bld_base
);
170 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
171 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
173 unsigned i
, size
= range
.Last
- range
.First
+ 1;
174 LLVMTypeRef vec
= LLVMVectorType(tgsi2llvmtype(bld_base
, type
), size
);
175 LLVMValueRef result
= LLVMGetUndef(vec
);
177 struct tgsi_full_src_register tmp_reg
= {};
178 tmp_reg
.Register
.File
= File
;
180 for (i
= 0; i
< size
; ++i
) {
181 tmp_reg
.Register
.Index
= i
+ range
.First
;
182 LLVMValueRef temp
= radeon_llvm_emit_fetch(bld_base
, &tmp_reg
, type
, swizzle
);
183 result
= LLVMBuildInsertElement(builder
, result
, temp
,
184 lp_build_const_int32(gallivm
, i
), "");
190 load_value_from_array(struct lp_build_tgsi_context
*bld_base
,
192 enum tgsi_opcode_type type
,
195 const struct tgsi_ind_register
*reg_indirect
)
197 struct lp_build_tgsi_soa_context
*bld
= lp_soa_context(bld_base
);
198 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
199 struct tgsi_declaration_range range
= get_array_range(bld_base
, file
, reg_indirect
);
201 return LLVMBuildExtractElement(builder
,
202 emit_array_fetch(bld_base
, file
, type
, range
, swizzle
),
203 emit_array_index(bld
, reg_indirect
, reg_index
- range
.First
), "");
208 store_value_to_array(struct lp_build_tgsi_context
*bld_base
,
213 const struct tgsi_ind_register
*reg_indirect
)
215 struct lp_build_tgsi_soa_context
*bld
= lp_soa_context(bld_base
);
216 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
217 struct tgsi_declaration_range range
= get_array_range(bld_base
, file
, reg_indirect
);
219 return LLVMBuildInsertElement(builder
,
220 emit_array_fetch(bld_base
, file
, TGSI_TYPE_FLOAT
, range
, chan_index
),
221 value
, emit_array_index(bld
, reg_indirect
, reg_index
- range
.First
), "");
225 LLVMValueRef
radeon_llvm_emit_fetch(struct lp_build_tgsi_context
*bld_base
,
226 const struct tgsi_full_src_register
*reg
,
227 enum tgsi_opcode_type type
,
230 struct radeon_llvm_context
*ctx
= radeon_llvm_context(bld_base
);
231 struct lp_build_tgsi_soa_context
*bld
= lp_soa_context(bld_base
);
232 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
233 LLVMValueRef result
= NULL
, ptr
, ptr2
;
236 LLVMValueRef values
[TGSI_NUM_CHANNELS
];
238 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
239 values
[chan
] = radeon_llvm_emit_fetch(bld_base
, reg
, type
, chan
);
241 return lp_build_gather_values(bld_base
->base
.gallivm
, values
,
245 if (reg
->Register
.Indirect
) {
246 return load_value_from_array(bld_base
, reg
->Register
.File
, type
,
247 swizzle
, reg
->Register
.Index
, ®
->Indirect
);
250 switch(reg
->Register
.File
) {
251 case TGSI_FILE_IMMEDIATE
: {
252 LLVMTypeRef ctype
= tgsi2llvmtype(bld_base
, type
);
253 if (tgsi_type_is_64bit(type
)) {
254 result
= LLVMGetUndef(LLVMVectorType(LLVMIntTypeInContext(bld_base
->base
.gallivm
->context
, 32), bld_base
->base
.type
.length
* 2));
255 result
= LLVMConstInsertElement(result
,
256 bld
->immediates
[reg
->Register
.Index
][swizzle
],
257 bld_base
->int_bld
.zero
);
258 result
= LLVMConstInsertElement(result
,
259 bld
->immediates
[reg
->Register
.Index
][swizzle
+ 1],
260 bld_base
->int_bld
.one
);
261 return LLVMConstBitCast(result
, ctype
);
263 return LLVMConstBitCast(bld
->immediates
[reg
->Register
.Index
][swizzle
], ctype
);
267 case TGSI_FILE_INPUT
:
268 result
= ctx
->inputs
[radeon_llvm_reg_index_soa(reg
->Register
.Index
, swizzle
)];
269 if (tgsi_type_is_64bit(type
)) {
271 ptr2
= ctx
->inputs
[radeon_llvm_reg_index_soa(reg
->Register
.Index
, swizzle
+ 1)];
272 return radeon_llvm_emit_fetch_64bit(bld_base
, type
, ptr
, ptr2
);
276 case TGSI_FILE_TEMPORARY
:
277 if (reg
->Register
.Index
>= ctx
->temps_count
)
278 return LLVMGetUndef(tgsi2llvmtype(bld_base
, type
));
279 ptr
= ctx
->temps
[reg
->Register
.Index
* TGSI_NUM_CHANNELS
+ swizzle
];
280 if (tgsi_type_is_64bit(type
)) {
281 ptr2
= ctx
->temps
[reg
->Register
.Index
* TGSI_NUM_CHANNELS
+ swizzle
+ 1];
282 return radeon_llvm_emit_fetch_64bit(bld_base
, type
,
283 LLVMBuildLoad(builder
, ptr
, ""),
284 LLVMBuildLoad(builder
, ptr2
, ""));
286 result
= LLVMBuildLoad(builder
, ptr
, "");
289 case TGSI_FILE_OUTPUT
:
290 ptr
= lp_get_output_ptr(bld
, reg
->Register
.Index
, swizzle
);
291 if (tgsi_type_is_64bit(type
)) {
292 ptr2
= lp_get_output_ptr(bld
, reg
->Register
.Index
, swizzle
+ 1);
293 return radeon_llvm_emit_fetch_64bit(bld_base
, type
,
294 LLVMBuildLoad(builder
, ptr
, ""),
295 LLVMBuildLoad(builder
, ptr2
, ""));
297 result
= LLVMBuildLoad(builder
, ptr
, "");
301 return LLVMGetUndef(tgsi2llvmtype(bld_base
, type
));
304 return bitcast(bld_base
, type
, result
);
307 static LLVMValueRef
fetch_system_value(struct lp_build_tgsi_context
*bld_base
,
308 const struct tgsi_full_src_register
*reg
,
309 enum tgsi_opcode_type type
,
312 struct radeon_llvm_context
*ctx
= radeon_llvm_context(bld_base
);
313 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
315 LLVMValueRef cval
= ctx
->system_values
[reg
->Register
.Index
];
316 if (LLVMGetTypeKind(LLVMTypeOf(cval
)) == LLVMVectorTypeKind
) {
317 cval
= LLVMBuildExtractElement(gallivm
->builder
, cval
,
318 lp_build_const_int32(gallivm
, swizzle
), "");
320 return bitcast(bld_base
, type
, cval
);
323 static LLVMValueRef
si_build_alloca_undef(struct gallivm_state
*gallivm
,
327 LLVMValueRef ptr
= lp_build_alloca(gallivm
, type
, name
);
328 LLVMBuildStore(gallivm
->builder
, LLVMGetUndef(type
), ptr
);
332 static void emit_declaration(struct lp_build_tgsi_context
*bld_base
,
333 const struct tgsi_full_declaration
*decl
)
335 struct radeon_llvm_context
*ctx
= radeon_llvm_context(bld_base
);
336 unsigned first
, last
, i
, idx
;
337 switch(decl
->Declaration
.File
) {
338 case TGSI_FILE_ADDRESS
:
341 for (idx
= decl
->Range
.First
; idx
<= decl
->Range
.Last
; idx
++) {
343 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
344 ctx
->soa
.addr
[idx
][chan
] = si_build_alloca_undef(
346 ctx
->soa
.bld_base
.uint_bld
.elem_type
, "");
352 case TGSI_FILE_TEMPORARY
:
353 if (decl
->Declaration
.Array
) {
355 int size
= bld_base
->info
->array_max
[TGSI_FILE_TEMPORARY
];
356 ctx
->arrays
= MALLOC(sizeof(ctx
->arrays
[0]) * size
);
359 ctx
->arrays
[decl
->Array
.ArrayID
- 1] = decl
->Range
;
361 first
= decl
->Range
.First
;
362 last
= decl
->Range
.Last
;
363 if (!ctx
->temps_count
) {
364 ctx
->temps_count
= bld_base
->info
->file_max
[TGSI_FILE_TEMPORARY
] + 1;
365 ctx
->temps
= MALLOC(TGSI_NUM_CHANNELS
* ctx
->temps_count
* sizeof(LLVMValueRef
));
367 for (idx
= first
; idx
<= last
; idx
++) {
368 for (i
= 0; i
< TGSI_NUM_CHANNELS
; i
++) {
369 ctx
->temps
[idx
* TGSI_NUM_CHANNELS
+ i
] =
370 si_build_alloca_undef(bld_base
->base
.gallivm
,
371 bld_base
->base
.vec_type
,
377 case TGSI_FILE_INPUT
:
380 for (idx
= decl
->Range
.First
; idx
<= decl
->Range
.Last
; idx
++) {
382 ctx
->load_input(ctx
, idx
, decl
);
387 case TGSI_FILE_SYSTEM_VALUE
:
390 for (idx
= decl
->Range
.First
; idx
<= decl
->Range
.Last
; idx
++) {
391 ctx
->load_system_value(ctx
, idx
, decl
);
396 case TGSI_FILE_OUTPUT
:
399 for (idx
= decl
->Range
.First
; idx
<= decl
->Range
.Last
; idx
++) {
401 assert(idx
< RADEON_LLVM_MAX_OUTPUTS
);
402 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
403 ctx
->soa
.outputs
[idx
][chan
] = si_build_alloca_undef(
405 ctx
->soa
.bld_base
.base
.elem_type
, "");
411 case TGSI_FILE_MEMORY
:
412 ctx
->declare_memory_region(ctx
, decl
);
420 LLVMValueRef
radeon_llvm_saturate(struct lp_build_tgsi_context
*bld_base
,
423 struct lp_build_emit_data clamp_emit_data
;
425 memset(&clamp_emit_data
, 0, sizeof(clamp_emit_data
));
426 clamp_emit_data
.arg_count
= 3;
427 clamp_emit_data
.args
[0] = value
;
428 clamp_emit_data
.args
[2] = bld_base
->base
.one
;
429 clamp_emit_data
.args
[1] = bld_base
->base
.zero
;
431 return lp_build_emit_llvm(bld_base
, TGSI_OPCODE_CLAMP
,
435 void radeon_llvm_emit_store(struct lp_build_tgsi_context
*bld_base
,
436 const struct tgsi_full_instruction
*inst
,
437 const struct tgsi_opcode_info
*info
,
440 struct radeon_llvm_context
*ctx
= radeon_llvm_context(bld_base
);
441 struct lp_build_tgsi_soa_context
*bld
= lp_soa_context(bld_base
);
442 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
443 const struct tgsi_full_dst_register
*reg
= &inst
->Dst
[0];
444 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
445 LLVMValueRef temp_ptr
, temp_ptr2
= NULL
;
446 unsigned chan
, chan_index
;
447 bool is_vec_store
= false;
448 enum tgsi_opcode_type dtype
= tgsi_opcode_infer_dst_type(inst
->Instruction
.Opcode
);
451 LLVMTypeKind k
= LLVMGetTypeKind(LLVMTypeOf(dst
[0]));
452 is_vec_store
= (k
== LLVMVectorTypeKind
);
456 LLVMValueRef values
[4] = {};
457 TGSI_FOR_EACH_DST0_ENABLED_CHANNEL(inst
, chan
) {
458 LLVMValueRef index
= lp_build_const_int32(gallivm
, chan
);
459 values
[chan
] = LLVMBuildExtractElement(gallivm
->builder
,
462 bld_base
->emit_store(bld_base
, inst
, info
, values
);
466 TGSI_FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
467 LLVMValueRef value
= dst
[chan_index
];
469 if (tgsi_type_is_64bit(dtype
) && (chan_index
== 1 || chan_index
== 3))
471 if (inst
->Instruction
.Saturate
)
472 value
= radeon_llvm_saturate(bld_base
, value
);
474 if (reg
->Register
.File
== TGSI_FILE_ADDRESS
) {
475 temp_ptr
= bld
->addr
[reg
->Register
.Index
][chan_index
];
476 LLVMBuildStore(builder
, value
, temp_ptr
);
480 if (!tgsi_type_is_64bit(dtype
))
481 value
= bitcast(bld_base
, TGSI_TYPE_FLOAT
, value
);
483 if (reg
->Register
.Indirect
) {
484 struct tgsi_declaration_range range
= get_array_range(bld_base
,
485 reg
->Register
.File
, ®
->Indirect
);
487 unsigned i
, size
= range
.Last
- range
.First
+ 1;
488 LLVMValueRef array
= store_value_to_array(bld_base
, value
, reg
->Register
.File
, chan_index
,
489 reg
->Register
.Index
, ®
->Indirect
);
490 for (i
= 0; i
< size
; ++i
) {
491 switch(reg
->Register
.File
) {
492 case TGSI_FILE_OUTPUT
:
493 temp_ptr
= bld
->outputs
[i
+ range
.First
][chan_index
];
496 case TGSI_FILE_TEMPORARY
:
497 if (range
.First
+ i
>= ctx
->temps_count
)
499 temp_ptr
= ctx
->temps
[(i
+ range
.First
) * TGSI_NUM_CHANNELS
+ chan_index
];
505 value
= LLVMBuildExtractElement(builder
, array
,
506 lp_build_const_int32(gallivm
, i
), "");
507 LLVMBuildStore(builder
, value
, temp_ptr
);
511 switch(reg
->Register
.File
) {
512 case TGSI_FILE_OUTPUT
:
513 temp_ptr
= bld
->outputs
[reg
->Register
.Index
][chan_index
];
514 if (tgsi_type_is_64bit(dtype
))
515 temp_ptr2
= bld
->outputs
[reg
->Register
.Index
][chan_index
+ 1];
518 case TGSI_FILE_TEMPORARY
:
519 if (reg
->Register
.Index
>= ctx
->temps_count
)
521 temp_ptr
= ctx
->temps
[ TGSI_NUM_CHANNELS
* reg
->Register
.Index
+ chan_index
];
522 if (tgsi_type_is_64bit(dtype
))
523 temp_ptr2
= ctx
->temps
[ TGSI_NUM_CHANNELS
* reg
->Register
.Index
+ chan_index
+ 1];
530 if (!tgsi_type_is_64bit(dtype
))
531 LLVMBuildStore(builder
, value
, temp_ptr
);
533 LLVMValueRef ptr
= LLVMBuildBitCast(builder
, value
,
534 LLVMVectorType(LLVMIntTypeInContext(bld_base
->base
.gallivm
->context
, 32), 2), "");
536 value
= LLVMBuildExtractElement(builder
, ptr
,
537 bld_base
->uint_bld
.zero
, "");
538 val2
= LLVMBuildExtractElement(builder
, ptr
,
539 bld_base
->uint_bld
.one
, "");
541 LLVMBuildStore(builder
, bitcast(bld_base
, TGSI_TYPE_FLOAT
, value
), temp_ptr
);
542 LLVMBuildStore(builder
, bitcast(bld_base
, TGSI_TYPE_FLOAT
, val2
), temp_ptr2
);
548 static void bgnloop_emit(const struct lp_build_tgsi_action
*action
,
549 struct lp_build_tgsi_context
*bld_base
,
550 struct lp_build_emit_data
*emit_data
)
552 struct radeon_llvm_context
*ctx
= radeon_llvm_context(bld_base
);
553 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
554 LLVMBasicBlockRef loop_block
;
555 LLVMBasicBlockRef endloop_block
;
556 endloop_block
= LLVMAppendBasicBlockInContext(gallivm
->context
,
557 ctx
->main_fn
, "ENDLOOP");
558 loop_block
= LLVMInsertBasicBlockInContext(gallivm
->context
,
559 endloop_block
, "LOOP");
560 LLVMBuildBr(gallivm
->builder
, loop_block
);
561 LLVMPositionBuilderAtEnd(gallivm
->builder
, loop_block
);
563 if (++ctx
->loop_depth
> ctx
->loop_depth_max
) {
564 unsigned new_max
= ctx
->loop_depth_max
<< 1;
567 new_max
= RADEON_LLVM_INITIAL_CF_DEPTH
;
569 ctx
->loop
= REALLOC(ctx
->loop
, ctx
->loop_depth_max
*
570 sizeof(ctx
->loop
[0]),
571 new_max
* sizeof(ctx
->loop
[0]));
572 ctx
->loop_depth_max
= new_max
;
575 ctx
->loop
[ctx
->loop_depth
- 1].loop_block
= loop_block
;
576 ctx
->loop
[ctx
->loop_depth
- 1].endloop_block
= endloop_block
;
579 static void brk_emit(const struct lp_build_tgsi_action
*action
,
580 struct lp_build_tgsi_context
*bld_base
,
581 struct lp_build_emit_data
*emit_data
)
583 struct radeon_llvm_context
*ctx
= radeon_llvm_context(bld_base
);
584 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
585 struct radeon_llvm_loop
*current_loop
= get_current_loop(ctx
);
587 LLVMBuildBr(gallivm
->builder
, current_loop
->endloop_block
);
590 static void cont_emit(const struct lp_build_tgsi_action
*action
,
591 struct lp_build_tgsi_context
*bld_base
,
592 struct lp_build_emit_data
*emit_data
)
594 struct radeon_llvm_context
*ctx
= radeon_llvm_context(bld_base
);
595 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
596 struct radeon_llvm_loop
*current_loop
= get_current_loop(ctx
);
598 LLVMBuildBr(gallivm
->builder
, current_loop
->loop_block
);
601 static void else_emit(const struct lp_build_tgsi_action
*action
,
602 struct lp_build_tgsi_context
*bld_base
,
603 struct lp_build_emit_data
*emit_data
)
605 struct radeon_llvm_context
*ctx
= radeon_llvm_context(bld_base
);
606 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
607 struct radeon_llvm_branch
*current_branch
= get_current_branch(ctx
);
608 LLVMBasicBlockRef current_block
= LLVMGetInsertBlock(gallivm
->builder
);
610 /* We need to add a terminator to the current block if the previous
611 * instruction was an ENDIF.Example:
624 if (current_block
!= current_branch
->if_block
) {
625 LLVMBuildBr(gallivm
->builder
, current_branch
->endif_block
);
627 if (!LLVMGetBasicBlockTerminator(current_branch
->if_block
)) {
628 LLVMBuildBr(gallivm
->builder
, current_branch
->endif_block
);
630 current_branch
->has_else
= 1;
631 LLVMPositionBuilderAtEnd(gallivm
->builder
, current_branch
->else_block
);
634 static void endif_emit(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 radeon_llvm_context
*ctx
= radeon_llvm_context(bld_base
);
639 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
640 struct radeon_llvm_branch
*current_branch
= get_current_branch(ctx
);
641 LLVMBasicBlockRef current_block
= LLVMGetInsertBlock(gallivm
->builder
);
643 /* If we have consecutive ENDIF instructions, then the first ENDIF
644 * will not have a terminator, so we need to add one. */
645 if (current_block
!= current_branch
->if_block
646 && current_block
!= current_branch
->else_block
647 && !LLVMGetBasicBlockTerminator(current_block
)) {
649 LLVMBuildBr(gallivm
->builder
, current_branch
->endif_block
);
651 if (!LLVMGetBasicBlockTerminator(current_branch
->else_block
)) {
652 LLVMPositionBuilderAtEnd(gallivm
->builder
, current_branch
->else_block
);
653 LLVMBuildBr(gallivm
->builder
, current_branch
->endif_block
);
656 if (!LLVMGetBasicBlockTerminator(current_branch
->if_block
)) {
657 LLVMPositionBuilderAtEnd(gallivm
->builder
, current_branch
->if_block
);
658 LLVMBuildBr(gallivm
->builder
, current_branch
->endif_block
);
661 LLVMPositionBuilderAtEnd(gallivm
->builder
, current_branch
->endif_block
);
665 static void endloop_emit(const struct lp_build_tgsi_action
*action
,
666 struct lp_build_tgsi_context
*bld_base
,
667 struct lp_build_emit_data
*emit_data
)
669 struct radeon_llvm_context
*ctx
= radeon_llvm_context(bld_base
);
670 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
671 struct radeon_llvm_loop
*current_loop
= get_current_loop(ctx
);
673 if (!LLVMGetBasicBlockTerminator(LLVMGetInsertBlock(gallivm
->builder
))) {
674 LLVMBuildBr(gallivm
->builder
, current_loop
->loop_block
);
677 LLVMPositionBuilderAtEnd(gallivm
->builder
, current_loop
->endloop_block
);
681 static void if_cond_emit(const struct lp_build_tgsi_action
*action
,
682 struct lp_build_tgsi_context
*bld_base
,
683 struct lp_build_emit_data
*emit_data
,
686 struct radeon_llvm_context
*ctx
= radeon_llvm_context(bld_base
);
687 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
688 LLVMBasicBlockRef if_block
, else_block
, endif_block
;
690 endif_block
= LLVMAppendBasicBlockInContext(gallivm
->context
,
691 ctx
->main_fn
, "ENDIF");
692 if_block
= LLVMInsertBasicBlockInContext(gallivm
->context
,
694 else_block
= LLVMInsertBasicBlockInContext(gallivm
->context
,
695 endif_block
, "ELSE");
696 LLVMBuildCondBr(gallivm
->builder
, cond
, if_block
, else_block
);
697 LLVMPositionBuilderAtEnd(gallivm
->builder
, if_block
);
699 if (++ctx
->branch_depth
> ctx
->branch_depth_max
) {
700 unsigned new_max
= ctx
->branch_depth_max
<< 1;
703 new_max
= RADEON_LLVM_INITIAL_CF_DEPTH
;
705 ctx
->branch
= REALLOC(ctx
->branch
, ctx
->branch_depth_max
*
706 sizeof(ctx
->branch
[0]),
707 new_max
* sizeof(ctx
->branch
[0]));
708 ctx
->branch_depth_max
= new_max
;
711 ctx
->branch
[ctx
->branch_depth
- 1].endif_block
= endif_block
;
712 ctx
->branch
[ctx
->branch_depth
- 1].if_block
= if_block
;
713 ctx
->branch
[ctx
->branch_depth
- 1].else_block
= else_block
;
714 ctx
->branch
[ctx
->branch_depth
- 1].has_else
= 0;
717 static void if_emit(const struct lp_build_tgsi_action
*action
,
718 struct lp_build_tgsi_context
*bld_base
,
719 struct lp_build_emit_data
*emit_data
)
721 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
724 cond
= LLVMBuildFCmp(gallivm
->builder
, LLVMRealUNE
,
726 bld_base
->base
.zero
, "");
728 if_cond_emit(action
, bld_base
, emit_data
, cond
);
731 static void uif_emit(const struct lp_build_tgsi_action
*action
,
732 struct lp_build_tgsi_context
*bld_base
,
733 struct lp_build_emit_data
*emit_data
)
735 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
738 cond
= LLVMBuildICmp(gallivm
->builder
, LLVMIntNE
,
739 bitcast(bld_base
, TGSI_TYPE_UNSIGNED
, emit_data
->args
[0]),
740 bld_base
->int_bld
.zero
, "");
742 if_cond_emit(action
, bld_base
, emit_data
, cond
);
745 static void kill_if_fetch_args(struct lp_build_tgsi_context
*bld_base
,
746 struct lp_build_emit_data
*emit_data
)
748 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
749 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
750 LLVMBuilderRef builder
= gallivm
->builder
;
752 LLVMValueRef conds
[TGSI_NUM_CHANNELS
];
754 for (i
= 0; i
< TGSI_NUM_CHANNELS
; i
++) {
755 LLVMValueRef value
= lp_build_emit_fetch(bld_base
, inst
, 0, i
);
756 conds
[i
] = LLVMBuildFCmp(builder
, LLVMRealOLT
, value
,
757 bld_base
->base
.zero
, "");
760 /* Or the conditions together */
761 for (i
= TGSI_NUM_CHANNELS
- 1; i
> 0; i
--) {
762 conds
[i
- 1] = LLVMBuildOr(builder
, conds
[i
], conds
[i
- 1], "");
765 emit_data
->dst_type
= LLVMVoidTypeInContext(gallivm
->context
);
766 emit_data
->arg_count
= 1;
767 emit_data
->args
[0] = LLVMBuildSelect(builder
, conds
[0],
768 lp_build_const_float(gallivm
, -1.0f
),
769 bld_base
->base
.zero
, "");
772 static void kil_emit(const struct lp_build_tgsi_action
*action
,
773 struct lp_build_tgsi_context
*bld_base
,
774 struct lp_build_emit_data
*emit_data
)
777 for (i
= 0; i
< emit_data
->arg_count
; i
++) {
778 emit_data
->output
[i
] = lp_build_intrinsic_unary(
779 bld_base
->base
.gallivm
->builder
,
781 emit_data
->dst_type
, emit_data
->args
[i
]);
785 static void radeon_llvm_cube_to_2d_coords(struct lp_build_tgsi_context
*bld_base
,
786 LLVMValueRef
*in
, LLVMValueRef
*out
)
788 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
789 LLVMBuilderRef builder
= gallivm
->builder
;
790 LLVMTypeRef type
= bld_base
->base
.elem_type
;
791 LLVMValueRef coords
[4];
792 LLVMValueRef mad_args
[3];
793 LLVMValueRef v
, cube_vec
;
796 cube_vec
= lp_build_gather_values(bld_base
->base
.gallivm
, in
, 4);
797 v
= lp_build_intrinsic(builder
, "llvm.AMDGPU.cube", LLVMVectorType(type
, 4),
798 &cube_vec
, 1, LLVMReadNoneAttribute
);
800 for (i
= 0; i
< 4; ++i
)
801 coords
[i
] = LLVMBuildExtractElement(builder
, v
,
802 lp_build_const_int32(gallivm
, i
), "");
804 coords
[2] = lp_build_intrinsic(builder
, "llvm.fabs.f32",
805 type
, &coords
[2], 1, LLVMReadNoneAttribute
);
806 coords
[2] = lp_build_emit_llvm_unary(bld_base
, TGSI_OPCODE_RCP
, coords
[2]);
808 mad_args
[1] = coords
[2];
809 mad_args
[2] = LLVMConstReal(type
, 1.5);
811 mad_args
[0] = coords
[0];
812 coords
[0] = lp_build_emit_llvm_ternary(bld_base
, TGSI_OPCODE_MAD
,
813 mad_args
[0], mad_args
[1], mad_args
[2]);
815 mad_args
[0] = coords
[1];
816 coords
[1] = lp_build_emit_llvm_ternary(bld_base
, TGSI_OPCODE_MAD
,
817 mad_args
[0], mad_args
[1], mad_args
[2]);
819 /* apply xyz = yxw swizzle to cooords */
825 void radeon_llvm_emit_prepare_cube_coords(struct lp_build_tgsi_context
*bld_base
,
826 struct lp_build_emit_data
*emit_data
,
827 LLVMValueRef
*coords_arg
,
828 LLVMValueRef
*derivs_arg
)
831 unsigned target
= emit_data
->inst
->Texture
.Texture
;
832 unsigned opcode
= emit_data
->inst
->Instruction
.Opcode
;
833 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
834 LLVMBuilderRef builder
= gallivm
->builder
;
835 LLVMValueRef coords
[4];
838 radeon_llvm_cube_to_2d_coords(bld_base
, coords_arg
, coords
);
840 if (opcode
== TGSI_OPCODE_TXD
&& derivs_arg
) {
841 LLVMValueRef derivs
[4];
844 /* Convert cube derivatives to 2D derivatives. */
845 for (axis
= 0; axis
< 2; axis
++) {
846 LLVMValueRef shifted_cube_coords
[4], shifted_coords
[4];
848 /* Shift the cube coordinates by the derivatives to get
849 * the cube coordinates of the "neighboring pixel".
851 for (i
= 0; i
< 3; i
++)
852 shifted_cube_coords
[i
] =
853 LLVMBuildFAdd(builder
, coords_arg
[i
],
854 derivs_arg
[axis
*3+i
], "");
855 shifted_cube_coords
[3] = LLVMGetUndef(bld_base
->base
.elem_type
);
857 /* Project the shifted cube coordinates onto the face. */
858 radeon_llvm_cube_to_2d_coords(bld_base
, shifted_cube_coords
,
861 /* Subtract both sets of 2D coordinates to get 2D derivatives.
862 * This won't work if the shifted coordinates ended up
863 * in a different face.
865 for (i
= 0; i
< 2; i
++)
866 derivs
[axis
* 2 + i
] =
867 LLVMBuildFSub(builder
, shifted_coords
[i
],
871 memcpy(derivs_arg
, derivs
, sizeof(derivs
));
874 if (target
== TGSI_TEXTURE_CUBE_ARRAY
||
875 target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
) {
876 /* for cube arrays coord.z = coord.w(array_index) * 8 + face */
877 /* coords_arg.w component - array_index for cube arrays */
878 coords
[2] = lp_build_emit_llvm_ternary(bld_base
, TGSI_OPCODE_MAD
,
879 coords_arg
[3], lp_build_const_float(gallivm
, 8.0), coords
[2]);
882 /* Preserve compare/lod/bias. Put it in coords.w. */
883 if (opcode
== TGSI_OPCODE_TEX2
||
884 opcode
== TGSI_OPCODE_TXB2
||
885 opcode
== TGSI_OPCODE_TXL2
) {
886 coords
[3] = coords_arg
[4];
887 } else if (opcode
== TGSI_OPCODE_TXB
||
888 opcode
== TGSI_OPCODE_TXL
||
889 target
== TGSI_TEXTURE_SHADOWCUBE
) {
890 coords
[3] = coords_arg
[3];
893 memcpy(coords_arg
, coords
, sizeof(coords
));
896 static void emit_icmp(const struct lp_build_tgsi_action
*action
,
897 struct lp_build_tgsi_context
*bld_base
,
898 struct lp_build_emit_data
*emit_data
)
901 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
902 LLVMContextRef context
= bld_base
->base
.gallivm
->context
;
904 switch (emit_data
->inst
->Instruction
.Opcode
) {
905 case TGSI_OPCODE_USEQ
: pred
= LLVMIntEQ
; break;
906 case TGSI_OPCODE_USNE
: pred
= LLVMIntNE
; break;
907 case TGSI_OPCODE_USGE
: pred
= LLVMIntUGE
; break;
908 case TGSI_OPCODE_USLT
: pred
= LLVMIntULT
; break;
909 case TGSI_OPCODE_ISGE
: pred
= LLVMIntSGE
; break;
910 case TGSI_OPCODE_ISLT
: pred
= LLVMIntSLT
; break;
912 assert(!"unknown instruction");
917 LLVMValueRef v
= LLVMBuildICmp(builder
, pred
,
918 emit_data
->args
[0], emit_data
->args
[1],"");
920 v
= LLVMBuildSExtOrBitCast(builder
, v
,
921 LLVMInt32TypeInContext(context
), "");
923 emit_data
->output
[emit_data
->chan
] = v
;
926 static void emit_ucmp(const struct lp_build_tgsi_action
*action
,
927 struct lp_build_tgsi_context
*bld_base
,
928 struct lp_build_emit_data
*emit_data
)
930 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
932 LLVMValueRef arg0
= LLVMBuildBitCast(builder
, emit_data
->args
[0],
933 bld_base
->uint_bld
.elem_type
, "");
935 LLVMValueRef v
= LLVMBuildICmp(builder
, LLVMIntNE
, arg0
,
936 bld_base
->uint_bld
.zero
, "");
938 emit_data
->output
[emit_data
->chan
] =
939 LLVMBuildSelect(builder
, v
, emit_data
->args
[1], emit_data
->args
[2], "");
942 static void emit_cmp(const struct lp_build_tgsi_action
*action
,
943 struct lp_build_tgsi_context
*bld_base
,
944 struct lp_build_emit_data
*emit_data
)
946 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
947 LLVMValueRef cond
, *args
= emit_data
->args
;
949 cond
= LLVMBuildFCmp(builder
, LLVMRealOLT
, args
[0],
950 bld_base
->base
.zero
, "");
952 emit_data
->output
[emit_data
->chan
] =
953 LLVMBuildSelect(builder
, cond
, args
[1], args
[2], "");
956 static void emit_set_cond(const struct lp_build_tgsi_action
*action
,
957 struct lp_build_tgsi_context
*bld_base
,
958 struct lp_build_emit_data
*emit_data
)
960 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
961 LLVMRealPredicate pred
;
964 /* Use ordered for everything but NE (which is usual for
967 switch (emit_data
->inst
->Instruction
.Opcode
) {
968 case TGSI_OPCODE_SGE
: pred
= LLVMRealOGE
; break;
969 case TGSI_OPCODE_SEQ
: pred
= LLVMRealOEQ
; break;
970 case TGSI_OPCODE_SLE
: pred
= LLVMRealOLE
; break;
971 case TGSI_OPCODE_SLT
: pred
= LLVMRealOLT
; break;
972 case TGSI_OPCODE_SNE
: pred
= LLVMRealUNE
; break;
973 case TGSI_OPCODE_SGT
: pred
= LLVMRealOGT
; break;
974 default: assert(!"unknown instruction"); pred
= 0; break;
977 cond
= LLVMBuildFCmp(builder
,
978 pred
, emit_data
->args
[0], emit_data
->args
[1], "");
980 emit_data
->output
[emit_data
->chan
] = LLVMBuildSelect(builder
,
981 cond
, bld_base
->base
.one
, bld_base
->base
.zero
, "");
984 static void emit_fcmp(const struct lp_build_tgsi_action
*action
,
985 struct lp_build_tgsi_context
*bld_base
,
986 struct lp_build_emit_data
*emit_data
)
988 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
989 LLVMContextRef context
= bld_base
->base
.gallivm
->context
;
990 LLVMRealPredicate pred
;
992 /* Use ordered for everything but NE (which is usual for
995 switch (emit_data
->inst
->Instruction
.Opcode
) {
996 case TGSI_OPCODE_FSEQ
: pred
= LLVMRealOEQ
; break;
997 case TGSI_OPCODE_FSGE
: pred
= LLVMRealOGE
; break;
998 case TGSI_OPCODE_FSLT
: pred
= LLVMRealOLT
; break;
999 case TGSI_OPCODE_FSNE
: pred
= LLVMRealUNE
; break;
1000 default: assert(!"unknown instruction"); pred
= 0; break;
1003 LLVMValueRef v
= LLVMBuildFCmp(builder
, pred
,
1004 emit_data
->args
[0], emit_data
->args
[1],"");
1006 v
= LLVMBuildSExtOrBitCast(builder
, v
,
1007 LLVMInt32TypeInContext(context
), "");
1009 emit_data
->output
[emit_data
->chan
] = v
;
1012 static void emit_dcmp(const struct lp_build_tgsi_action
*action
,
1013 struct lp_build_tgsi_context
*bld_base
,
1014 struct lp_build_emit_data
*emit_data
)
1016 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1017 LLVMContextRef context
= bld_base
->base
.gallivm
->context
;
1018 LLVMRealPredicate pred
;
1020 /* Use ordered for everything but NE (which is usual for
1021 * float comparisons)
1023 switch (emit_data
->inst
->Instruction
.Opcode
) {
1024 case TGSI_OPCODE_DSEQ
: pred
= LLVMRealOEQ
; break;
1025 case TGSI_OPCODE_DSGE
: pred
= LLVMRealOGE
; break;
1026 case TGSI_OPCODE_DSLT
: pred
= LLVMRealOLT
; break;
1027 case TGSI_OPCODE_DSNE
: pred
= LLVMRealUNE
; break;
1028 default: assert(!"unknown instruction"); pred
= 0; break;
1031 LLVMValueRef v
= LLVMBuildFCmp(builder
, pred
,
1032 emit_data
->args
[0], emit_data
->args
[1],"");
1034 v
= LLVMBuildSExtOrBitCast(builder
, v
,
1035 LLVMInt32TypeInContext(context
), "");
1037 emit_data
->output
[emit_data
->chan
] = v
;
1040 static void emit_not(const struct lp_build_tgsi_action
*action
,
1041 struct lp_build_tgsi_context
*bld_base
,
1042 struct lp_build_emit_data
*emit_data
)
1044 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1045 LLVMValueRef v
= bitcast(bld_base
, TGSI_TYPE_UNSIGNED
,
1046 emit_data
->args
[0]);
1047 emit_data
->output
[emit_data
->chan
] = LLVMBuildNot(builder
, v
, "");
1050 static void emit_arl(const struct lp_build_tgsi_action
*action
,
1051 struct lp_build_tgsi_context
*bld_base
,
1052 struct lp_build_emit_data
*emit_data
)
1054 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1055 LLVMValueRef floor_index
= lp_build_emit_llvm_unary(bld_base
, TGSI_OPCODE_FLR
, emit_data
->args
[0]);
1056 emit_data
->output
[emit_data
->chan
] = LLVMBuildFPToSI(builder
,
1057 floor_index
, bld_base
->base
.int_elem_type
, "");
1060 static void emit_and(const struct lp_build_tgsi_action
*action
,
1061 struct lp_build_tgsi_context
*bld_base
,
1062 struct lp_build_emit_data
*emit_data
)
1064 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1065 emit_data
->output
[emit_data
->chan
] = LLVMBuildAnd(builder
,
1066 emit_data
->args
[0], emit_data
->args
[1], "");
1069 static void emit_or(const struct lp_build_tgsi_action
*action
,
1070 struct lp_build_tgsi_context
*bld_base
,
1071 struct lp_build_emit_data
*emit_data
)
1073 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1074 emit_data
->output
[emit_data
->chan
] = LLVMBuildOr(builder
,
1075 emit_data
->args
[0], emit_data
->args
[1], "");
1078 static void emit_uadd(const struct lp_build_tgsi_action
*action
,
1079 struct lp_build_tgsi_context
*bld_base
,
1080 struct lp_build_emit_data
*emit_data
)
1082 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1083 emit_data
->output
[emit_data
->chan
] = LLVMBuildAdd(builder
,
1084 emit_data
->args
[0], emit_data
->args
[1], "");
1087 static void emit_udiv(const struct lp_build_tgsi_action
*action
,
1088 struct lp_build_tgsi_context
*bld_base
,
1089 struct lp_build_emit_data
*emit_data
)
1091 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1092 emit_data
->output
[emit_data
->chan
] = LLVMBuildUDiv(builder
,
1093 emit_data
->args
[0], emit_data
->args
[1], "");
1096 static void emit_idiv(const struct lp_build_tgsi_action
*action
,
1097 struct lp_build_tgsi_context
*bld_base
,
1098 struct lp_build_emit_data
*emit_data
)
1100 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1101 emit_data
->output
[emit_data
->chan
] = LLVMBuildSDiv(builder
,
1102 emit_data
->args
[0], emit_data
->args
[1], "");
1105 static void emit_mod(const struct lp_build_tgsi_action
*action
,
1106 struct lp_build_tgsi_context
*bld_base
,
1107 struct lp_build_emit_data
*emit_data
)
1109 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1110 emit_data
->output
[emit_data
->chan
] = LLVMBuildSRem(builder
,
1111 emit_data
->args
[0], emit_data
->args
[1], "");
1114 static void emit_umod(const struct lp_build_tgsi_action
*action
,
1115 struct lp_build_tgsi_context
*bld_base
,
1116 struct lp_build_emit_data
*emit_data
)
1118 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1119 emit_data
->output
[emit_data
->chan
] = LLVMBuildURem(builder
,
1120 emit_data
->args
[0], emit_data
->args
[1], "");
1123 static void emit_shl(const struct lp_build_tgsi_action
*action
,
1124 struct lp_build_tgsi_context
*bld_base
,
1125 struct lp_build_emit_data
*emit_data
)
1127 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1128 emit_data
->output
[emit_data
->chan
] = LLVMBuildShl(builder
,
1129 emit_data
->args
[0], emit_data
->args
[1], "");
1132 static void emit_ushr(const struct lp_build_tgsi_action
*action
,
1133 struct lp_build_tgsi_context
*bld_base
,
1134 struct lp_build_emit_data
*emit_data
)
1136 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1137 emit_data
->output
[emit_data
->chan
] = LLVMBuildLShr(builder
,
1138 emit_data
->args
[0], emit_data
->args
[1], "");
1140 static void emit_ishr(const struct lp_build_tgsi_action
*action
,
1141 struct lp_build_tgsi_context
*bld_base
,
1142 struct lp_build_emit_data
*emit_data
)
1144 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1145 emit_data
->output
[emit_data
->chan
] = LLVMBuildAShr(builder
,
1146 emit_data
->args
[0], emit_data
->args
[1], "");
1149 static void emit_xor(const struct lp_build_tgsi_action
*action
,
1150 struct lp_build_tgsi_context
*bld_base
,
1151 struct lp_build_emit_data
*emit_data
)
1153 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1154 emit_data
->output
[emit_data
->chan
] = LLVMBuildXor(builder
,
1155 emit_data
->args
[0], emit_data
->args
[1], "");
1158 static void emit_ssg(const struct lp_build_tgsi_action
*action
,
1159 struct lp_build_tgsi_context
*bld_base
,
1160 struct lp_build_emit_data
*emit_data
)
1162 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1164 LLVMValueRef cmp
, val
;
1166 if (emit_data
->inst
->Instruction
.Opcode
== TGSI_OPCODE_ISSG
) {
1167 cmp
= LLVMBuildICmp(builder
, LLVMIntSGT
, emit_data
->args
[0], bld_base
->int_bld
.zero
, "");
1168 val
= LLVMBuildSelect(builder
, cmp
, bld_base
->int_bld
.one
, emit_data
->args
[0], "");
1169 cmp
= LLVMBuildICmp(builder
, LLVMIntSGE
, val
, bld_base
->int_bld
.zero
, "");
1170 val
= LLVMBuildSelect(builder
, cmp
, val
, LLVMConstInt(bld_base
->int_bld
.elem_type
, -1, true), "");
1171 } else { // float SSG
1172 cmp
= LLVMBuildFCmp(builder
, LLVMRealOGT
, emit_data
->args
[0], bld_base
->base
.zero
, "");
1173 val
= LLVMBuildSelect(builder
, cmp
, bld_base
->base
.one
, emit_data
->args
[0], "");
1174 cmp
= LLVMBuildFCmp(builder
, LLVMRealOGE
, val
, bld_base
->base
.zero
, "");
1175 val
= LLVMBuildSelect(builder
, cmp
, val
, LLVMConstReal(bld_base
->base
.elem_type
, -1), "");
1178 emit_data
->output
[emit_data
->chan
] = val
;
1181 static void emit_ineg(const struct lp_build_tgsi_action
*action
,
1182 struct lp_build_tgsi_context
*bld_base
,
1183 struct lp_build_emit_data
*emit_data
)
1185 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1186 emit_data
->output
[emit_data
->chan
] = LLVMBuildNeg(builder
,
1187 emit_data
->args
[0], "");
1190 static void emit_dneg(const struct lp_build_tgsi_action
*action
,
1191 struct lp_build_tgsi_context
*bld_base
,
1192 struct lp_build_emit_data
*emit_data
)
1194 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1195 emit_data
->output
[emit_data
->chan
] = LLVMBuildFNeg(builder
,
1196 emit_data
->args
[0], "");
1199 static void emit_frac(const struct lp_build_tgsi_action
*action
,
1200 struct lp_build_tgsi_context
*bld_base
,
1201 struct lp_build_emit_data
*emit_data
)
1203 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1206 if (emit_data
->info
->opcode
== TGSI_OPCODE_FRC
)
1207 intr
= "llvm.floor.f32";
1208 else if (emit_data
->info
->opcode
== TGSI_OPCODE_DFRAC
)
1209 intr
= "llvm.floor.f64";
1215 LLVMValueRef floor
= lp_build_intrinsic(builder
, intr
, emit_data
->dst_type
,
1216 &emit_data
->args
[0], 1,
1217 LLVMReadNoneAttribute
);
1218 emit_data
->output
[emit_data
->chan
] = LLVMBuildFSub(builder
,
1219 emit_data
->args
[0], floor
, "");
1222 static void emit_f2i(const struct lp_build_tgsi_action
*action
,
1223 struct lp_build_tgsi_context
*bld_base
,
1224 struct lp_build_emit_data
*emit_data
)
1226 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1227 emit_data
->output
[emit_data
->chan
] = LLVMBuildFPToSI(builder
,
1228 emit_data
->args
[0], bld_base
->int_bld
.elem_type
, "");
1231 static void emit_f2u(const struct lp_build_tgsi_action
*action
,
1232 struct lp_build_tgsi_context
*bld_base
,
1233 struct lp_build_emit_data
*emit_data
)
1235 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1236 emit_data
->output
[emit_data
->chan
] = LLVMBuildFPToUI(builder
,
1237 emit_data
->args
[0], bld_base
->uint_bld
.elem_type
, "");
1240 static void emit_i2f(const struct lp_build_tgsi_action
*action
,
1241 struct lp_build_tgsi_context
*bld_base
,
1242 struct lp_build_emit_data
*emit_data
)
1244 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1245 emit_data
->output
[emit_data
->chan
] = LLVMBuildSIToFP(builder
,
1246 emit_data
->args
[0], bld_base
->base
.elem_type
, "");
1249 static void emit_u2f(const struct lp_build_tgsi_action
*action
,
1250 struct lp_build_tgsi_context
*bld_base
,
1251 struct lp_build_emit_data
*emit_data
)
1253 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1254 emit_data
->output
[emit_data
->chan
] = LLVMBuildUIToFP(builder
,
1255 emit_data
->args
[0], bld_base
->base
.elem_type
, "");
1258 static void emit_immediate(struct lp_build_tgsi_context
*bld_base
,
1259 const struct tgsi_full_immediate
*imm
)
1262 struct radeon_llvm_context
*ctx
= radeon_llvm_context(bld_base
);
1264 for (i
= 0; i
< 4; ++i
) {
1265 ctx
->soa
.immediates
[ctx
->soa
.num_immediates
][i
] =
1266 LLVMConstInt(bld_base
->uint_bld
.elem_type
, imm
->u
[i
].Uint
, false );
1269 ctx
->soa
.num_immediates
++;
1273 build_tgsi_intrinsic_nomem(const struct lp_build_tgsi_action
*action
,
1274 struct lp_build_tgsi_context
*bld_base
,
1275 struct lp_build_emit_data
*emit_data
)
1277 struct lp_build_context
*base
= &bld_base
->base
;
1278 emit_data
->output
[emit_data
->chan
] =
1279 lp_build_intrinsic(base
->gallivm
->builder
, action
->intr_name
,
1280 emit_data
->dst_type
, emit_data
->args
,
1281 emit_data
->arg_count
, LLVMReadNoneAttribute
);
1284 static void emit_bfi(const struct lp_build_tgsi_action
*action
,
1285 struct lp_build_tgsi_context
*bld_base
,
1286 struct lp_build_emit_data
*emit_data
)
1288 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1289 LLVMBuilderRef builder
= gallivm
->builder
;
1290 LLVMValueRef bfi_args
[3];
1292 // Calculate the bitmask: (((1 << src3) - 1) << src2
1293 bfi_args
[0] = LLVMBuildShl(builder
,
1294 LLVMBuildSub(builder
,
1295 LLVMBuildShl(builder
,
1296 bld_base
->int_bld
.one
,
1297 emit_data
->args
[3], ""),
1298 bld_base
->int_bld
.one
, ""),
1299 emit_data
->args
[2], "");
1301 bfi_args
[1] = LLVMBuildShl(builder
, emit_data
->args
[1],
1302 emit_data
->args
[2], "");
1304 bfi_args
[2] = emit_data
->args
[0];
1307 * (arg0 & arg1) | (~arg0 & arg2) = arg2 ^ (arg0 & (arg1 ^ arg2)
1308 * Use the right-hand side, which the LLVM backend can convert to V_BFI.
1310 emit_data
->output
[emit_data
->chan
] =
1311 LLVMBuildXor(builder
, bfi_args
[2],
1312 LLVMBuildAnd(builder
, bfi_args
[0],
1313 LLVMBuildXor(builder
, bfi_args
[1], bfi_args
[2],
1317 /* this is ffs in C */
1318 static void emit_lsb(const struct lp_build_tgsi_action
*action
,
1319 struct lp_build_tgsi_context
*bld_base
,
1320 struct lp_build_emit_data
*emit_data
)
1322 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1323 LLVMValueRef args
[2] = {
1326 /* The value of 1 means that ffs(x=0) = undef, so LLVM won't
1327 * add special code to check for x=0. The reason is that
1328 * the LLVM behavior for x=0 is different from what we
1331 * The hardware already implements the correct behavior.
1333 lp_build_const_int32(gallivm
, 1)
1336 emit_data
->output
[emit_data
->chan
] =
1337 lp_build_intrinsic(gallivm
->builder
, "llvm.cttz.i32",
1338 emit_data
->dst_type
, args
, ARRAY_SIZE(args
),
1339 LLVMReadNoneAttribute
);
1342 /* Find the last bit set. */
1343 static void emit_umsb(const struct lp_build_tgsi_action
*action
,
1344 struct lp_build_tgsi_context
*bld_base
,
1345 struct lp_build_emit_data
*emit_data
)
1347 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1348 LLVMBuilderRef builder
= gallivm
->builder
;
1349 LLVMValueRef args
[2] = {
1351 /* Don't generate code for handling zero: */
1352 lp_build_const_int32(gallivm
, 1)
1356 lp_build_intrinsic(builder
, "llvm.ctlz.i32",
1357 emit_data
->dst_type
, args
, ARRAY_SIZE(args
),
1358 LLVMReadNoneAttribute
);
1360 /* The HW returns the last bit index from MSB, but TGSI wants
1361 * the index from LSB. Invert it by doing "31 - msb". */
1362 msb
= LLVMBuildSub(builder
, lp_build_const_int32(gallivm
, 31),
1365 /* Check for zero: */
1366 emit_data
->output
[emit_data
->chan
] =
1367 LLVMBuildSelect(builder
,
1368 LLVMBuildICmp(builder
, LLVMIntEQ
, args
[0],
1369 bld_base
->uint_bld
.zero
, ""),
1370 lp_build_const_int32(gallivm
, -1), msb
, "");
1373 /* Find the last bit opposite of the sign bit. */
1374 static void emit_imsb(const struct lp_build_tgsi_action
*action
,
1375 struct lp_build_tgsi_context
*bld_base
,
1376 struct lp_build_emit_data
*emit_data
)
1378 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1379 LLVMBuilderRef builder
= gallivm
->builder
;
1380 LLVMValueRef arg
= emit_data
->args
[0];
1383 lp_build_intrinsic(builder
, "llvm.AMDGPU.flbit.i32",
1384 emit_data
->dst_type
, &arg
, 1,
1385 LLVMReadNoneAttribute
);
1387 /* The HW returns the last bit index from MSB, but TGSI wants
1388 * the index from LSB. Invert it by doing "31 - msb". */
1389 msb
= LLVMBuildSub(builder
, lp_build_const_int32(gallivm
, 31),
1392 /* If arg == 0 || arg == -1 (0xffffffff), return -1. */
1393 LLVMValueRef all_ones
= lp_build_const_int32(gallivm
, -1);
1396 LLVMBuildOr(builder
,
1397 LLVMBuildICmp(builder
, LLVMIntEQ
, arg
,
1398 bld_base
->uint_bld
.zero
, ""),
1399 LLVMBuildICmp(builder
, LLVMIntEQ
, arg
,
1402 emit_data
->output
[emit_data
->chan
] =
1403 LLVMBuildSelect(builder
, cond
, all_ones
, msb
, "");
1406 static void emit_iabs(const struct lp_build_tgsi_action
*action
,
1407 struct lp_build_tgsi_context
*bld_base
,
1408 struct lp_build_emit_data
*emit_data
)
1410 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1412 emit_data
->output
[emit_data
->chan
] =
1413 lp_build_emit_llvm_binary(bld_base
, TGSI_OPCODE_IMAX
,
1415 LLVMBuildNeg(builder
,
1416 emit_data
->args
[0], ""));
1419 static void emit_minmax_int(const struct lp_build_tgsi_action
*action
,
1420 struct lp_build_tgsi_context
*bld_base
,
1421 struct lp_build_emit_data
*emit_data
)
1423 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1424 LLVMIntPredicate op
;
1426 switch (emit_data
->info
->opcode
) {
1429 case TGSI_OPCODE_IMAX
:
1432 case TGSI_OPCODE_IMIN
:
1435 case TGSI_OPCODE_UMAX
:
1438 case TGSI_OPCODE_UMIN
:
1443 emit_data
->output
[emit_data
->chan
] =
1444 LLVMBuildSelect(builder
,
1445 LLVMBuildICmp(builder
, op
, emit_data
->args
[0],
1446 emit_data
->args
[1], ""),
1448 emit_data
->args
[1], "");
1451 static void pk2h_fetch_args(struct lp_build_tgsi_context
*bld_base
,
1452 struct lp_build_emit_data
*emit_data
)
1454 emit_data
->args
[0] = lp_build_emit_fetch(bld_base
, emit_data
->inst
,
1456 emit_data
->args
[1] = lp_build_emit_fetch(bld_base
, emit_data
->inst
,
1460 static void emit_pk2h(const struct lp_build_tgsi_action
*action
,
1461 struct lp_build_tgsi_context
*bld_base
,
1462 struct lp_build_emit_data
*emit_data
)
1464 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1465 LLVMContextRef context
= bld_base
->base
.gallivm
->context
;
1466 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
1467 LLVMTypeRef fp16
, i16
;
1468 LLVMValueRef const16
, comp
[2];
1471 fp16
= LLVMHalfTypeInContext(context
);
1472 i16
= LLVMInt16TypeInContext(context
);
1473 const16
= lp_build_const_int32(uint_bld
->gallivm
, 16);
1475 for (i
= 0; i
< 2; i
++) {
1476 comp
[i
] = LLVMBuildFPTrunc(builder
, emit_data
->args
[i
], fp16
, "");
1477 comp
[i
] = LLVMBuildBitCast(builder
, comp
[i
], i16
, "");
1478 comp
[i
] = LLVMBuildZExt(builder
, comp
[i
], uint_bld
->elem_type
, "");
1481 comp
[1] = LLVMBuildShl(builder
, comp
[1], const16
, "");
1482 comp
[0] = LLVMBuildOr(builder
, comp
[0], comp
[1], "");
1484 emit_data
->output
[emit_data
->chan
] = comp
[0];
1487 static void up2h_fetch_args(struct lp_build_tgsi_context
*bld_base
,
1488 struct lp_build_emit_data
*emit_data
)
1490 emit_data
->args
[0] = lp_build_emit_fetch(bld_base
, emit_data
->inst
,
1494 static void emit_up2h(const struct lp_build_tgsi_action
*action
,
1495 struct lp_build_tgsi_context
*bld_base
,
1496 struct lp_build_emit_data
*emit_data
)
1498 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1499 LLVMContextRef context
= bld_base
->base
.gallivm
->context
;
1500 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
1501 LLVMTypeRef fp16
, i16
;
1502 LLVMValueRef const16
, input
, val
;
1505 fp16
= LLVMHalfTypeInContext(context
);
1506 i16
= LLVMInt16TypeInContext(context
);
1507 const16
= lp_build_const_int32(uint_bld
->gallivm
, 16);
1508 input
= emit_data
->args
[0];
1510 for (i
= 0; i
< 2; i
++) {
1511 val
= i
== 1 ? LLVMBuildLShr(builder
, input
, const16
, "") : input
;
1512 val
= LLVMBuildTrunc(builder
, val
, i16
, "");
1513 val
= LLVMBuildBitCast(builder
, val
, fp16
, "");
1514 emit_data
->output
[i
] =
1515 LLVMBuildFPExt(builder
, val
, bld_base
->base
.elem_type
, "");
1519 static void emit_fdiv(const struct lp_build_tgsi_action
*action
,
1520 struct lp_build_tgsi_context
*bld_base
,
1521 struct lp_build_emit_data
*emit_data
)
1523 struct radeon_llvm_context
*ctx
= radeon_llvm_context(bld_base
);
1525 emit_data
->output
[emit_data
->chan
] =
1526 LLVMBuildFDiv(bld_base
->base
.gallivm
->builder
,
1527 emit_data
->args
[0], emit_data
->args
[1], "");
1529 /* Use v_rcp_f32 instead of precise division. */
1530 if (HAVE_LLVM
>= 0x0309 &&
1531 !LLVMIsConstant(emit_data
->output
[emit_data
->chan
]))
1532 LLVMSetMetadata(emit_data
->output
[emit_data
->chan
],
1533 ctx
->fpmath_md_kind
, ctx
->fpmath_md_2p5_ulp
);
1536 /* 1/sqrt is translated to rsq for f32 if fp32 denormals are not enabled in
1537 * the target machine. f64 needs global unsafe math flags to get rsq. */
1538 static void emit_rsq(const struct lp_build_tgsi_action
*action
,
1539 struct lp_build_tgsi_context
*bld_base
,
1540 struct lp_build_emit_data
*emit_data
)
1543 lp_build_emit_llvm_unary(bld_base
, TGSI_OPCODE_SQRT
,
1544 emit_data
->args
[0]);
1546 emit_data
->output
[emit_data
->chan
] =
1547 lp_build_emit_llvm_binary(bld_base
, TGSI_OPCODE_DIV
,
1548 bld_base
->base
.one
, sqrt
);
1551 void radeon_llvm_context_init(struct radeon_llvm_context
*ctx
, const char *triple
)
1553 struct lp_type type
;
1555 /* Initialize the gallivm object:
1556 * We are only using the module, context, and builder fields of this struct.
1557 * This should be enough for us to be able to pass our gallivm struct to the
1558 * helper functions in the gallivm module.
1560 memset(&ctx
->gallivm
, 0, sizeof (ctx
->gallivm
));
1561 memset(&ctx
->soa
, 0, sizeof(ctx
->soa
));
1562 ctx
->gallivm
.context
= LLVMContextCreate();
1563 ctx
->gallivm
.module
= LLVMModuleCreateWithNameInContext("tgsi",
1564 ctx
->gallivm
.context
);
1565 LLVMSetTarget(ctx
->gallivm
.module
, triple
);
1566 ctx
->gallivm
.builder
= LLVMCreateBuilderInContext(ctx
->gallivm
.context
);
1568 struct lp_build_tgsi_context
*bld_base
= &ctx
->soa
.bld_base
;
1570 type
.floating
= true;
1577 lp_build_context_init(&bld_base
->base
, &ctx
->gallivm
, type
);
1578 lp_build_context_init(&ctx
->soa
.bld_base
.uint_bld
, &ctx
->gallivm
, lp_uint_type(type
));
1579 lp_build_context_init(&ctx
->soa
.bld_base
.int_bld
, &ctx
->gallivm
, lp_int_type(type
));
1581 struct lp_type dbl_type
;
1583 dbl_type
.width
*= 2;
1584 lp_build_context_init(&ctx
->soa
.bld_base
.dbl_bld
, &ctx
->gallivm
, dbl_type
);
1588 bld_base
->emit_store
= radeon_llvm_emit_store
;
1589 bld_base
->emit_swizzle
= emit_swizzle
;
1590 bld_base
->emit_declaration
= emit_declaration
;
1591 bld_base
->emit_immediate
= emit_immediate
;
1593 bld_base
->emit_fetch_funcs
[TGSI_FILE_IMMEDIATE
] = radeon_llvm_emit_fetch
;
1594 bld_base
->emit_fetch_funcs
[TGSI_FILE_INPUT
] = radeon_llvm_emit_fetch
;
1595 bld_base
->emit_fetch_funcs
[TGSI_FILE_TEMPORARY
] = radeon_llvm_emit_fetch
;
1596 bld_base
->emit_fetch_funcs
[TGSI_FILE_OUTPUT
] = radeon_llvm_emit_fetch
;
1597 bld_base
->emit_fetch_funcs
[TGSI_FILE_SYSTEM_VALUE
] = fetch_system_value
;
1599 /* metadata allowing 2.5 ULP */
1600 ctx
->fpmath_md_kind
= LLVMGetMDKindIDInContext(ctx
->gallivm
.context
,
1602 LLVMValueRef arg
= lp_build_const_float(&ctx
->gallivm
, 2.5);
1603 ctx
->fpmath_md_2p5_ulp
= LLVMMDNodeInContext(ctx
->gallivm
.context
,
1606 /* Allocate outputs */
1607 ctx
->soa
.outputs
= ctx
->outputs
;
1609 lp_set_default_actions(bld_base
);
1611 bld_base
->op_actions
[TGSI_OPCODE_ABS
].emit
= build_tgsi_intrinsic_nomem
;
1612 bld_base
->op_actions
[TGSI_OPCODE_ABS
].intr_name
= "llvm.fabs.f32";
1613 bld_base
->op_actions
[TGSI_OPCODE_AND
].emit
= emit_and
;
1614 bld_base
->op_actions
[TGSI_OPCODE_ARL
].emit
= emit_arl
;
1615 bld_base
->op_actions
[TGSI_OPCODE_BFI
].emit
= emit_bfi
;
1616 bld_base
->op_actions
[TGSI_OPCODE_BGNLOOP
].emit
= bgnloop_emit
;
1617 bld_base
->op_actions
[TGSI_OPCODE_BREV
].emit
= build_tgsi_intrinsic_nomem
;
1618 bld_base
->op_actions
[TGSI_OPCODE_BREV
].intr_name
=
1619 HAVE_LLVM
>= 0x0308 ? "llvm.bitreverse.i32" : "llvm.AMDGPU.brev";
1620 bld_base
->op_actions
[TGSI_OPCODE_BRK
].emit
= brk_emit
;
1621 bld_base
->op_actions
[TGSI_OPCODE_CEIL
].emit
= build_tgsi_intrinsic_nomem
;
1622 bld_base
->op_actions
[TGSI_OPCODE_CEIL
].intr_name
= "llvm.ceil.f32";
1623 bld_base
->op_actions
[TGSI_OPCODE_CLAMP
].emit
= build_tgsi_intrinsic_nomem
;
1624 bld_base
->op_actions
[TGSI_OPCODE_CLAMP
].intr_name
=
1625 HAVE_LLVM
>= 0x0308 ? "llvm.AMDGPU.clamp." : "llvm.AMDIL.clamp.";
1626 bld_base
->op_actions
[TGSI_OPCODE_CMP
].emit
= emit_cmp
;
1627 bld_base
->op_actions
[TGSI_OPCODE_CONT
].emit
= cont_emit
;
1628 bld_base
->op_actions
[TGSI_OPCODE_COS
].emit
= build_tgsi_intrinsic_nomem
;
1629 bld_base
->op_actions
[TGSI_OPCODE_COS
].intr_name
= "llvm.cos.f32";
1630 bld_base
->op_actions
[TGSI_OPCODE_DABS
].emit
= build_tgsi_intrinsic_nomem
;
1631 bld_base
->op_actions
[TGSI_OPCODE_DABS
].intr_name
= "llvm.fabs.f64";
1632 bld_base
->op_actions
[TGSI_OPCODE_DFMA
].emit
= build_tgsi_intrinsic_nomem
;
1633 bld_base
->op_actions
[TGSI_OPCODE_DFMA
].intr_name
= "llvm.fma.f64";
1634 bld_base
->op_actions
[TGSI_OPCODE_DFRAC
].emit
= emit_frac
;
1635 bld_base
->op_actions
[TGSI_OPCODE_DIV
].emit
= emit_fdiv
;
1636 bld_base
->op_actions
[TGSI_OPCODE_DNEG
].emit
= emit_dneg
;
1637 bld_base
->op_actions
[TGSI_OPCODE_DSEQ
].emit
= emit_dcmp
;
1638 bld_base
->op_actions
[TGSI_OPCODE_DSGE
].emit
= emit_dcmp
;
1639 bld_base
->op_actions
[TGSI_OPCODE_DSLT
].emit
= emit_dcmp
;
1640 bld_base
->op_actions
[TGSI_OPCODE_DSNE
].emit
= emit_dcmp
;
1641 bld_base
->op_actions
[TGSI_OPCODE_DRSQ
].emit
= build_tgsi_intrinsic_nomem
;
1642 bld_base
->op_actions
[TGSI_OPCODE_DRSQ
].intr_name
= "llvm.AMDGPU.rsq.f64";
1643 bld_base
->op_actions
[TGSI_OPCODE_DSQRT
].emit
= build_tgsi_intrinsic_nomem
;
1644 bld_base
->op_actions
[TGSI_OPCODE_DSQRT
].intr_name
= "llvm.sqrt.f64";
1645 bld_base
->op_actions
[TGSI_OPCODE_ELSE
].emit
= else_emit
;
1646 bld_base
->op_actions
[TGSI_OPCODE_ENDIF
].emit
= endif_emit
;
1647 bld_base
->op_actions
[TGSI_OPCODE_ENDLOOP
].emit
= endloop_emit
;
1648 bld_base
->op_actions
[TGSI_OPCODE_EX2
].emit
= build_tgsi_intrinsic_nomem
;
1649 bld_base
->op_actions
[TGSI_OPCODE_EX2
].intr_name
=
1650 HAVE_LLVM
>= 0x0308 ? "llvm.exp2.f32" : "llvm.AMDIL.exp.";
1651 bld_base
->op_actions
[TGSI_OPCODE_FLR
].emit
= build_tgsi_intrinsic_nomem
;
1652 bld_base
->op_actions
[TGSI_OPCODE_FLR
].intr_name
= "llvm.floor.f32";
1653 bld_base
->op_actions
[TGSI_OPCODE_FMA
].emit
= build_tgsi_intrinsic_nomem
;
1654 bld_base
->op_actions
[TGSI_OPCODE_FMA
].intr_name
= "llvm.fma.f32";
1655 bld_base
->op_actions
[TGSI_OPCODE_FRC
].emit
= emit_frac
;
1656 bld_base
->op_actions
[TGSI_OPCODE_F2I
].emit
= emit_f2i
;
1657 bld_base
->op_actions
[TGSI_OPCODE_F2U
].emit
= emit_f2u
;
1658 bld_base
->op_actions
[TGSI_OPCODE_FSEQ
].emit
= emit_fcmp
;
1659 bld_base
->op_actions
[TGSI_OPCODE_FSGE
].emit
= emit_fcmp
;
1660 bld_base
->op_actions
[TGSI_OPCODE_FSLT
].emit
= emit_fcmp
;
1661 bld_base
->op_actions
[TGSI_OPCODE_FSNE
].emit
= emit_fcmp
;
1662 bld_base
->op_actions
[TGSI_OPCODE_IABS
].emit
= emit_iabs
;
1663 bld_base
->op_actions
[TGSI_OPCODE_IBFE
].emit
= build_tgsi_intrinsic_nomem
;
1664 bld_base
->op_actions
[TGSI_OPCODE_IBFE
].intr_name
= "llvm.AMDGPU.bfe.i32";
1665 bld_base
->op_actions
[TGSI_OPCODE_IDIV
].emit
= emit_idiv
;
1666 bld_base
->op_actions
[TGSI_OPCODE_IF
].emit
= if_emit
;
1667 bld_base
->op_actions
[TGSI_OPCODE_UIF
].emit
= uif_emit
;
1668 bld_base
->op_actions
[TGSI_OPCODE_IMAX
].emit
= emit_minmax_int
;
1669 bld_base
->op_actions
[TGSI_OPCODE_IMIN
].emit
= emit_minmax_int
;
1670 bld_base
->op_actions
[TGSI_OPCODE_IMSB
].emit
= emit_imsb
;
1671 bld_base
->op_actions
[TGSI_OPCODE_INEG
].emit
= emit_ineg
;
1672 bld_base
->op_actions
[TGSI_OPCODE_ISHR
].emit
= emit_ishr
;
1673 bld_base
->op_actions
[TGSI_OPCODE_ISGE
].emit
= emit_icmp
;
1674 bld_base
->op_actions
[TGSI_OPCODE_ISLT
].emit
= emit_icmp
;
1675 bld_base
->op_actions
[TGSI_OPCODE_ISSG
].emit
= emit_ssg
;
1676 bld_base
->op_actions
[TGSI_OPCODE_I2F
].emit
= emit_i2f
;
1677 bld_base
->op_actions
[TGSI_OPCODE_KILL_IF
].fetch_args
= kill_if_fetch_args
;
1678 bld_base
->op_actions
[TGSI_OPCODE_KILL_IF
].emit
= kil_emit
;
1679 bld_base
->op_actions
[TGSI_OPCODE_KILL_IF
].intr_name
= "llvm.AMDGPU.kill";
1680 bld_base
->op_actions
[TGSI_OPCODE_KILL
].emit
= lp_build_tgsi_intrinsic
;
1681 bld_base
->op_actions
[TGSI_OPCODE_KILL
].intr_name
= "llvm.AMDGPU.kilp";
1682 bld_base
->op_actions
[TGSI_OPCODE_LSB
].emit
= emit_lsb
;
1683 bld_base
->op_actions
[TGSI_OPCODE_LG2
].emit
= build_tgsi_intrinsic_nomem
;
1684 bld_base
->op_actions
[TGSI_OPCODE_LG2
].intr_name
= "llvm.log2.f32";
1685 bld_base
->op_actions
[TGSI_OPCODE_MOD
].emit
= emit_mod
;
1686 bld_base
->op_actions
[TGSI_OPCODE_UMSB
].emit
= emit_umsb
;
1687 bld_base
->op_actions
[TGSI_OPCODE_NOT
].emit
= emit_not
;
1688 bld_base
->op_actions
[TGSI_OPCODE_OR
].emit
= emit_or
;
1689 bld_base
->op_actions
[TGSI_OPCODE_PK2H
].fetch_args
= pk2h_fetch_args
;
1690 bld_base
->op_actions
[TGSI_OPCODE_PK2H
].emit
= emit_pk2h
;
1691 bld_base
->op_actions
[TGSI_OPCODE_POPC
].emit
= build_tgsi_intrinsic_nomem
;
1692 bld_base
->op_actions
[TGSI_OPCODE_POPC
].intr_name
= "llvm.ctpop.i32";
1693 bld_base
->op_actions
[TGSI_OPCODE_POW
].emit
= build_tgsi_intrinsic_nomem
;
1694 bld_base
->op_actions
[TGSI_OPCODE_POW
].intr_name
= "llvm.pow.f32";
1695 bld_base
->op_actions
[TGSI_OPCODE_ROUND
].emit
= build_tgsi_intrinsic_nomem
;
1696 bld_base
->op_actions
[TGSI_OPCODE_ROUND
].intr_name
= "llvm.rint.f32";
1697 bld_base
->op_actions
[TGSI_OPCODE_RSQ
].emit
= emit_rsq
;
1698 bld_base
->op_actions
[TGSI_OPCODE_SGE
].emit
= emit_set_cond
;
1699 bld_base
->op_actions
[TGSI_OPCODE_SEQ
].emit
= emit_set_cond
;
1700 bld_base
->op_actions
[TGSI_OPCODE_SHL
].emit
= emit_shl
;
1701 bld_base
->op_actions
[TGSI_OPCODE_SLE
].emit
= emit_set_cond
;
1702 bld_base
->op_actions
[TGSI_OPCODE_SLT
].emit
= emit_set_cond
;
1703 bld_base
->op_actions
[TGSI_OPCODE_SNE
].emit
= emit_set_cond
;
1704 bld_base
->op_actions
[TGSI_OPCODE_SGT
].emit
= emit_set_cond
;
1705 bld_base
->op_actions
[TGSI_OPCODE_SIN
].emit
= build_tgsi_intrinsic_nomem
;
1706 bld_base
->op_actions
[TGSI_OPCODE_SIN
].intr_name
= "llvm.sin.f32";
1707 bld_base
->op_actions
[TGSI_OPCODE_SQRT
].emit
= build_tgsi_intrinsic_nomem
;
1708 bld_base
->op_actions
[TGSI_OPCODE_SQRT
].intr_name
= "llvm.sqrt.f32";
1709 bld_base
->op_actions
[TGSI_OPCODE_SSG
].emit
= emit_ssg
;
1710 bld_base
->op_actions
[TGSI_OPCODE_TRUNC
].emit
= build_tgsi_intrinsic_nomem
;
1711 bld_base
->op_actions
[TGSI_OPCODE_TRUNC
].intr_name
= "llvm.trunc.f32";
1712 bld_base
->op_actions
[TGSI_OPCODE_UADD
].emit
= emit_uadd
;
1713 bld_base
->op_actions
[TGSI_OPCODE_UBFE
].emit
= build_tgsi_intrinsic_nomem
;
1714 bld_base
->op_actions
[TGSI_OPCODE_UBFE
].intr_name
= "llvm.AMDGPU.bfe.u32";
1715 bld_base
->op_actions
[TGSI_OPCODE_UDIV
].emit
= emit_udiv
;
1716 bld_base
->op_actions
[TGSI_OPCODE_UMAX
].emit
= emit_minmax_int
;
1717 bld_base
->op_actions
[TGSI_OPCODE_UMIN
].emit
= emit_minmax_int
;
1718 bld_base
->op_actions
[TGSI_OPCODE_UMOD
].emit
= emit_umod
;
1719 bld_base
->op_actions
[TGSI_OPCODE_USEQ
].emit
= emit_icmp
;
1720 bld_base
->op_actions
[TGSI_OPCODE_USGE
].emit
= emit_icmp
;
1721 bld_base
->op_actions
[TGSI_OPCODE_USHR
].emit
= emit_ushr
;
1722 bld_base
->op_actions
[TGSI_OPCODE_USLT
].emit
= emit_icmp
;
1723 bld_base
->op_actions
[TGSI_OPCODE_USNE
].emit
= emit_icmp
;
1724 bld_base
->op_actions
[TGSI_OPCODE_U2F
].emit
= emit_u2f
;
1725 bld_base
->op_actions
[TGSI_OPCODE_XOR
].emit
= emit_xor
;
1726 bld_base
->op_actions
[TGSI_OPCODE_UCMP
].emit
= emit_ucmp
;
1727 bld_base
->op_actions
[TGSI_OPCODE_UP2H
].fetch_args
= up2h_fetch_args
;
1728 bld_base
->op_actions
[TGSI_OPCODE_UP2H
].emit
= emit_up2h
;
1731 void radeon_llvm_create_func(struct radeon_llvm_context
*ctx
,
1732 LLVMTypeRef
*return_types
, unsigned num_return_elems
,
1733 LLVMTypeRef
*ParamTypes
, unsigned ParamCount
)
1735 LLVMTypeRef main_fn_type
, ret_type
;
1736 LLVMBasicBlockRef main_fn_body
;
1738 if (num_return_elems
)
1739 ret_type
= LLVMStructTypeInContext(ctx
->gallivm
.context
,
1741 num_return_elems
, true);
1743 ret_type
= LLVMVoidTypeInContext(ctx
->gallivm
.context
);
1745 /* Setup the function */
1746 ctx
->return_type
= ret_type
;
1747 main_fn_type
= LLVMFunctionType(ret_type
, ParamTypes
, ParamCount
, 0);
1748 ctx
->main_fn
= LLVMAddFunction(ctx
->gallivm
.module
, "main", main_fn_type
);
1749 main_fn_body
= LLVMAppendBasicBlockInContext(ctx
->gallivm
.context
,
1750 ctx
->main_fn
, "main_body");
1751 LLVMPositionBuilderAtEnd(ctx
->gallivm
.builder
, main_fn_body
);
1754 void radeon_llvm_finalize_module(struct radeon_llvm_context
*ctx
)
1756 struct gallivm_state
*gallivm
= ctx
->soa
.bld_base
.base
.gallivm
;
1757 const char *triple
= LLVMGetTarget(gallivm
->module
);
1758 LLVMTargetLibraryInfoRef target_library_info
;
1760 /* Create the pass manager */
1761 gallivm
->passmgr
= LLVMCreateFunctionPassManagerForModule(
1764 target_library_info
= gallivm_create_target_library_info(triple
);
1765 LLVMAddTargetLibraryInfo(target_library_info
, gallivm
->passmgr
);
1767 /* This pass should eliminate all the load and store instructions */
1768 LLVMAddPromoteMemoryToRegisterPass(gallivm
->passmgr
);
1770 /* Add some optimization passes */
1771 LLVMAddScalarReplAggregatesPass(gallivm
->passmgr
);
1772 LLVMAddLICMPass(gallivm
->passmgr
);
1773 LLVMAddAggressiveDCEPass(gallivm
->passmgr
);
1774 LLVMAddCFGSimplificationPass(gallivm
->passmgr
);
1775 LLVMAddInstructionCombiningPass(gallivm
->passmgr
);
1778 LLVMRunFunctionPassManager(gallivm
->passmgr
, ctx
->main_fn
);
1780 LLVMDisposeBuilder(gallivm
->builder
);
1781 LLVMDisposePassManager(gallivm
->passmgr
);
1782 gallivm_dispose_target_library_info(target_library_info
);
1785 void radeon_llvm_dispose(struct radeon_llvm_context
*ctx
)
1787 LLVMDisposeModule(ctx
->soa
.bld_base
.base
.gallivm
->module
);
1788 LLVMContextDispose(ctx
->soa
.bld_base
.base
.gallivm
->context
);
1793 ctx
->temps_count
= 0;
1796 ctx
->loop_depth_max
= 0;
1799 ctx
->branch_depth_max
= 0;