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
, unsigned reg_index
,
116 const struct tgsi_ind_register
*reg
)
118 struct radeon_llvm_context
*ctx
= radeon_llvm_context(bld_base
);
122 unsigned num_arrays
= bld_base
->info
->array_max
[TGSI_FILE_TEMPORARY
];
123 for (i
= 0; i
< num_arrays
; i
++) {
124 const struct tgsi_declaration_range
*range
=
125 &ctx
->arrays
[i
].range
;
127 if (reg_index
>= range
->First
&& reg_index
<= range
->Last
) {
128 return ctx
->arrays
[i
].range
;
133 if (File
!= TGSI_FILE_TEMPORARY
|| !reg
|| reg
->ArrayID
== 0 ||
134 reg
->ArrayID
> bld_base
->info
->array_max
[TGSI_FILE_TEMPORARY
]) {
135 struct tgsi_declaration_range range
;
137 range
.Last
= bld_base
->info
->file_max
[File
];
141 return ctx
->arrays
[reg
->ArrayID
- 1].range
;
144 static LLVMValueRef
get_alloca_for_array(struct lp_build_tgsi_context
*bld_base
,
150 struct radeon_llvm_context
*ctx
= radeon_llvm_context(bld_base
);
152 if (file
!= TGSI_FILE_TEMPORARY
)
155 num_arrays
= bld_base
->info
->array_max
[TGSI_FILE_TEMPORARY
];
156 for (i
= 0; i
< num_arrays
; i
++) {
157 const struct tgsi_declaration_range
*range
=
158 &ctx
->arrays
[i
].range
;
160 if (index
>= range
->First
&& index
<= range
->Last
) {
161 return ctx
->arrays
[i
].alloca
;
168 emit_array_index(struct lp_build_tgsi_soa_context
*bld
,
169 const struct tgsi_ind_register
*reg
,
172 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
175 return lp_build_const_int32(gallivm
, offset
);
177 LLVMValueRef addr
= LLVMBuildLoad(gallivm
->builder
, bld
->addr
[reg
->Index
][reg
->Swizzle
], "");
178 return LLVMBuildAdd(gallivm
->builder
, addr
, lp_build_const_int32(gallivm
, offset
), "");
182 radeon_llvm_emit_fetch_64bit(struct lp_build_tgsi_context
*bld_base
,
183 enum tgsi_opcode_type type
,
187 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
190 result
= LLVMGetUndef(LLVMVectorType(LLVMIntTypeInContext(bld_base
->base
.gallivm
->context
, 32), bld_base
->base
.type
.length
* 2));
192 result
= LLVMBuildInsertElement(builder
,
194 bitcast(bld_base
, TGSI_TYPE_UNSIGNED
, ptr
),
195 bld_base
->int_bld
.zero
, "");
196 result
= LLVMBuildInsertElement(builder
,
198 bitcast(bld_base
, TGSI_TYPE_UNSIGNED
, ptr2
),
199 bld_base
->int_bld
.one
, "");
200 return bitcast(bld_base
, type
, result
);
204 emit_array_fetch(struct lp_build_tgsi_context
*bld_base
,
205 unsigned File
, enum tgsi_opcode_type type
,
206 struct tgsi_declaration_range range
,
209 struct lp_build_tgsi_soa_context
*bld
= lp_soa_context(bld_base
);
210 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
211 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
213 unsigned i
, size
= range
.Last
- range
.First
+ 1;
214 LLVMTypeRef vec
= LLVMVectorType(tgsi2llvmtype(bld_base
, type
), size
);
215 LLVMValueRef result
= LLVMGetUndef(vec
);
217 struct tgsi_full_src_register tmp_reg
= {};
218 tmp_reg
.Register
.File
= File
;
220 for (i
= 0; i
< size
; ++i
) {
221 tmp_reg
.Register
.Index
= i
+ range
.First
;
222 LLVMValueRef temp
= radeon_llvm_emit_fetch(bld_base
, &tmp_reg
, type
, swizzle
);
223 result
= LLVMBuildInsertElement(builder
, result
, temp
,
224 lp_build_const_int32(gallivm
, i
), "array_vector");
230 load_value_from_array(struct lp_build_tgsi_context
*bld_base
,
232 enum tgsi_opcode_type type
,
235 const struct tgsi_ind_register
*reg_indirect
)
237 struct lp_build_tgsi_soa_context
*bld
= lp_soa_context(bld_base
);
238 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
239 LLVMBuilderRef builder
= gallivm
->builder
;
240 struct tgsi_declaration_range range
= get_array_range(bld_base
, file
, reg_index
, reg_indirect
);
241 LLVMValueRef index
= emit_array_index(bld
, reg_indirect
, reg_index
- range
.First
);
242 LLVMValueRef array
= get_alloca_for_array(bld_base
, file
, reg_index
);
243 LLVMValueRef ptr
, val
, indices
[2];
246 /* Handle the case where the array is stored as a vector. */
247 return LLVMBuildExtractElement(builder
,
248 emit_array_fetch(bld_base
, file
, type
, range
, swizzle
),
252 index
= LLVMBuildMul(builder
, index
, lp_build_const_int32(gallivm
, TGSI_NUM_CHANNELS
), "");
253 index
= LLVMBuildAdd(builder
, index
, lp_build_const_int32(gallivm
, swizzle
), "");
254 indices
[0] = bld_base
->uint_bld
.zero
;
256 ptr
= LLVMBuildGEP(builder
, array
, indices
, 2, "");
257 val
= LLVMBuildLoad(builder
, ptr
, "");
258 if (tgsi_type_is_64bit(type
)) {
259 LLVMValueRef ptr_hi
, val_hi
;
260 indices
[0] = lp_build_const_int32(gallivm
, 1);
261 ptr_hi
= LLVMBuildGEP(builder
, ptr
, indices
, 1, "");
262 val_hi
= LLVMBuildLoad(builder
, ptr_hi
, "");
263 val
= radeon_llvm_emit_fetch_64bit(bld_base
, type
, val
, val_hi
);
270 store_value_to_array(struct lp_build_tgsi_context
*bld_base
,
275 const struct tgsi_ind_register
*reg_indirect
)
277 struct lp_build_tgsi_soa_context
*bld
= lp_soa_context(bld_base
);
278 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
279 LLVMBuilderRef builder
= gallivm
->builder
;
280 struct tgsi_declaration_range range
= get_array_range(bld_base
, file
, reg_index
, reg_indirect
);
281 LLVMValueRef index
= emit_array_index(bld
, reg_indirect
, reg_index
- range
.First
);
282 LLVMValueRef array
= get_alloca_for_array(bld_base
, file
, reg_index
);
285 LLVMValueRef indices
[2];
286 index
= LLVMBuildMul(builder
, index
, lp_build_const_int32(gallivm
, TGSI_NUM_CHANNELS
), "");
287 index
= LLVMBuildAdd(builder
, index
, lp_build_const_int32(gallivm
, chan_index
), "");
288 indices
[0] = bld_base
->uint_bld
.zero
;
290 LLVMValueRef pointer
= LLVMBuildGEP(builder
, array
, indices
, 2, "");
291 LLVMBuildStore(builder
, value
, pointer
);
294 return LLVMBuildInsertElement(builder
,
295 emit_array_fetch(bld_base
, file
, TGSI_TYPE_FLOAT
, range
, chan_index
),
300 LLVMValueRef
radeon_llvm_emit_fetch(struct lp_build_tgsi_context
*bld_base
,
301 const struct tgsi_full_src_register
*reg
,
302 enum tgsi_opcode_type type
,
305 struct radeon_llvm_context
*ctx
= radeon_llvm_context(bld_base
);
306 struct lp_build_tgsi_soa_context
*bld
= lp_soa_context(bld_base
);
307 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
308 LLVMValueRef result
= NULL
, ptr
, ptr2
;
311 LLVMValueRef values
[TGSI_NUM_CHANNELS
];
313 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
314 values
[chan
] = radeon_llvm_emit_fetch(bld_base
, reg
, type
, chan
);
316 return lp_build_gather_values(bld_base
->base
.gallivm
, values
,
320 if (reg
->Register
.Indirect
) {
321 LLVMValueRef load
= load_value_from_array(bld_base
, reg
->Register
.File
, type
,
322 swizzle
, reg
->Register
.Index
, ®
->Indirect
);
323 return bitcast(bld_base
, type
, load
);
326 switch(reg
->Register
.File
) {
327 case TGSI_FILE_IMMEDIATE
: {
328 LLVMTypeRef ctype
= tgsi2llvmtype(bld_base
, type
);
329 if (tgsi_type_is_64bit(type
)) {
330 result
= LLVMGetUndef(LLVMVectorType(LLVMIntTypeInContext(bld_base
->base
.gallivm
->context
, 32), bld_base
->base
.type
.length
* 2));
331 result
= LLVMConstInsertElement(result
,
332 bld
->immediates
[reg
->Register
.Index
][swizzle
],
333 bld_base
->int_bld
.zero
);
334 result
= LLVMConstInsertElement(result
,
335 bld
->immediates
[reg
->Register
.Index
][swizzle
+ 1],
336 bld_base
->int_bld
.one
);
337 return LLVMConstBitCast(result
, ctype
);
339 return LLVMConstBitCast(bld
->immediates
[reg
->Register
.Index
][swizzle
], ctype
);
343 case TGSI_FILE_INPUT
:
344 result
= ctx
->inputs
[radeon_llvm_reg_index_soa(reg
->Register
.Index
, swizzle
)];
345 if (tgsi_type_is_64bit(type
)) {
347 ptr2
= ctx
->inputs
[radeon_llvm_reg_index_soa(reg
->Register
.Index
, swizzle
+ 1)];
348 return radeon_llvm_emit_fetch_64bit(bld_base
, type
, ptr
, ptr2
);
352 case TGSI_FILE_TEMPORARY
:
353 if (reg
->Register
.Index
>= ctx
->temps_count
)
354 return LLVMGetUndef(tgsi2llvmtype(bld_base
, type
));
355 ptr
= ctx
->temps
[reg
->Register
.Index
* TGSI_NUM_CHANNELS
+ swizzle
];
356 if (tgsi_type_is_64bit(type
)) {
357 ptr2
= ctx
->temps
[reg
->Register
.Index
* TGSI_NUM_CHANNELS
+ swizzle
+ 1];
358 return radeon_llvm_emit_fetch_64bit(bld_base
, type
,
359 LLVMBuildLoad(builder
, ptr
, ""),
360 LLVMBuildLoad(builder
, ptr2
, ""));
362 LLVMValueRef array
= get_alloca_for_array(bld_base
, reg
->Register
.File
, reg
->Register
.Index
);
364 return bitcast(bld_base
, type
, load_value_from_array(bld_base
, reg
->Register
.File
, type
,
365 swizzle
, reg
->Register
.Index
, NULL
));
367 result
= LLVMBuildLoad(builder
, ptr
, "");
370 case TGSI_FILE_OUTPUT
:
371 ptr
= lp_get_output_ptr(bld
, reg
->Register
.Index
, swizzle
);
372 if (tgsi_type_is_64bit(type
)) {
373 ptr2
= lp_get_output_ptr(bld
, reg
->Register
.Index
, swizzle
+ 1);
374 return radeon_llvm_emit_fetch_64bit(bld_base
, type
,
375 LLVMBuildLoad(builder
, ptr
, ""),
376 LLVMBuildLoad(builder
, ptr2
, ""));
378 result
= LLVMBuildLoad(builder
, ptr
, "");
382 return LLVMGetUndef(tgsi2llvmtype(bld_base
, type
));
385 return bitcast(bld_base
, type
, result
);
388 static LLVMValueRef
fetch_system_value(struct lp_build_tgsi_context
*bld_base
,
389 const struct tgsi_full_src_register
*reg
,
390 enum tgsi_opcode_type type
,
393 struct radeon_llvm_context
*ctx
= radeon_llvm_context(bld_base
);
394 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
396 LLVMValueRef cval
= ctx
->system_values
[reg
->Register
.Index
];
397 if (LLVMGetTypeKind(LLVMTypeOf(cval
)) == LLVMVectorTypeKind
) {
398 cval
= LLVMBuildExtractElement(gallivm
->builder
, cval
,
399 lp_build_const_int32(gallivm
, swizzle
), "");
401 return bitcast(bld_base
, type
, cval
);
404 static LLVMValueRef
si_build_alloca_undef(struct gallivm_state
*gallivm
,
408 LLVMValueRef ptr
= lp_build_alloca(gallivm
, type
, name
);
409 LLVMBuildStore(gallivm
->builder
, LLVMGetUndef(type
), ptr
);
413 static void emit_declaration(struct lp_build_tgsi_context
*bld_base
,
414 const struct tgsi_full_declaration
*decl
)
416 struct radeon_llvm_context
*ctx
= radeon_llvm_context(bld_base
);
417 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
418 unsigned first
, last
, i
, idx
;
419 switch(decl
->Declaration
.File
) {
420 case TGSI_FILE_ADDRESS
:
423 for (idx
= decl
->Range
.First
; idx
<= decl
->Range
.Last
; idx
++) {
425 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
426 ctx
->soa
.addr
[idx
][chan
] = si_build_alloca_undef(
428 ctx
->soa
.bld_base
.uint_bld
.elem_type
, "");
434 case TGSI_FILE_TEMPORARY
:
437 first
= decl
->Range
.First
;
438 last
= decl
->Range
.Last
;
439 decl_size
= 4 * ((last
- first
) + 1);
440 if (decl
->Declaration
.Array
) {
441 unsigned id
= decl
->Array
.ArrayID
- 1;
443 int size
= bld_base
->info
->array_max
[TGSI_FILE_TEMPORARY
];
444 ctx
->arrays
= CALLOC(size
, sizeof(ctx
->arrays
[0]));
445 for (i
= 0; i
< size
; ++i
) {
446 assert(!ctx
->arrays
[i
].alloca
);}
449 ctx
->arrays
[id
].range
= decl
->Range
;
451 /* If the array is more than 16 elements (each element
452 * is 32-bits), then store it in a vector. Storing the
453 * array in a vector will causes the compiler to store
454 * the array in registers and access it using indirect
455 * addressing. 16 is number of vector elements that
456 * LLVM will store in a register.
457 * FIXME: We shouldn't need to do this. LLVM should be
458 * smart enough to promote allocas int registers when
461 if (decl_size
> 16) {
462 ctx
->arrays
[id
].alloca
= LLVMBuildAlloca(builder
,
463 LLVMArrayType(bld_base
->base
.vec_type
, decl_size
),"array");
466 first
= decl
->Range
.First
;
467 last
= decl
->Range
.Last
;
468 if (!ctx
->temps_count
) {
469 ctx
->temps_count
= bld_base
->info
->file_max
[TGSI_FILE_TEMPORARY
] + 1;
470 ctx
->temps
= MALLOC(TGSI_NUM_CHANNELS
* ctx
->temps_count
* sizeof(LLVMValueRef
));
472 for (idx
= first
; idx
<= last
; idx
++) {
473 for (i
= 0; i
< TGSI_NUM_CHANNELS
; i
++) {
474 ctx
->temps
[idx
* TGSI_NUM_CHANNELS
+ i
] =
475 si_build_alloca_undef(bld_base
->base
.gallivm
,
476 bld_base
->base
.vec_type
,
482 case TGSI_FILE_INPUT
:
485 for (idx
= decl
->Range
.First
; idx
<= decl
->Range
.Last
; idx
++) {
487 ctx
->load_input(ctx
, idx
, decl
);
492 case TGSI_FILE_SYSTEM_VALUE
:
495 for (idx
= decl
->Range
.First
; idx
<= decl
->Range
.Last
; idx
++) {
496 ctx
->load_system_value(ctx
, idx
, decl
);
501 case TGSI_FILE_OUTPUT
:
504 for (idx
= decl
->Range
.First
; idx
<= decl
->Range
.Last
; idx
++) {
506 assert(idx
< RADEON_LLVM_MAX_OUTPUTS
);
507 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; chan
++) {
508 ctx
->soa
.outputs
[idx
][chan
] = si_build_alloca_undef(
510 ctx
->soa
.bld_base
.base
.elem_type
, "");
516 case TGSI_FILE_MEMORY
:
517 ctx
->declare_memory_region(ctx
, decl
);
525 LLVMValueRef
radeon_llvm_saturate(struct lp_build_tgsi_context
*bld_base
,
528 struct lp_build_emit_data clamp_emit_data
;
530 memset(&clamp_emit_data
, 0, sizeof(clamp_emit_data
));
531 clamp_emit_data
.arg_count
= 3;
532 clamp_emit_data
.args
[0] = value
;
533 clamp_emit_data
.args
[2] = bld_base
->base
.one
;
534 clamp_emit_data
.args
[1] = bld_base
->base
.zero
;
536 return lp_build_emit_llvm(bld_base
, TGSI_OPCODE_CLAMP
,
540 void radeon_llvm_emit_store(struct lp_build_tgsi_context
*bld_base
,
541 const struct tgsi_full_instruction
*inst
,
542 const struct tgsi_opcode_info
*info
,
545 struct radeon_llvm_context
*ctx
= radeon_llvm_context(bld_base
);
546 struct lp_build_tgsi_soa_context
*bld
= lp_soa_context(bld_base
);
547 struct gallivm_state
*gallivm
= bld
->bld_base
.base
.gallivm
;
548 const struct tgsi_full_dst_register
*reg
= &inst
->Dst
[0];
549 LLVMBuilderRef builder
= bld
->bld_base
.base
.gallivm
->builder
;
550 LLVMValueRef temp_ptr
, temp_ptr2
= NULL
;
551 unsigned chan
, chan_index
;
552 bool is_vec_store
= false;
553 enum tgsi_opcode_type dtype
= tgsi_opcode_infer_dst_type(inst
->Instruction
.Opcode
);
556 LLVMTypeKind k
= LLVMGetTypeKind(LLVMTypeOf(dst
[0]));
557 is_vec_store
= (k
== LLVMVectorTypeKind
);
561 LLVMValueRef values
[4] = {};
562 TGSI_FOR_EACH_DST0_ENABLED_CHANNEL(inst
, chan
) {
563 LLVMValueRef index
= lp_build_const_int32(gallivm
, chan
);
564 values
[chan
] = LLVMBuildExtractElement(gallivm
->builder
,
567 bld_base
->emit_store(bld_base
, inst
, info
, values
);
571 TGSI_FOR_EACH_DST0_ENABLED_CHANNEL( inst
, chan_index
) {
572 LLVMValueRef value
= dst
[chan_index
];
574 if (tgsi_type_is_64bit(dtype
) && (chan_index
== 1 || chan_index
== 3))
576 if (inst
->Instruction
.Saturate
)
577 value
= radeon_llvm_saturate(bld_base
, value
);
579 if (reg
->Register
.File
== TGSI_FILE_ADDRESS
) {
580 temp_ptr
= bld
->addr
[reg
->Register
.Index
][chan_index
];
581 LLVMBuildStore(builder
, value
, temp_ptr
);
585 if (!tgsi_type_is_64bit(dtype
))
586 value
= bitcast(bld_base
, TGSI_TYPE_FLOAT
, value
);
588 if (reg
->Register
.Indirect
) {
589 struct tgsi_declaration_range range
= get_array_range(bld_base
,
590 reg
->Register
.File
, reg
->Register
.Index
, ®
->Indirect
);
592 unsigned i
, size
= range
.Last
- range
.First
+ 1;
593 unsigned file
= reg
->Register
.File
;
594 unsigned reg_index
= reg
->Register
.Index
;
595 LLVMValueRef array
= store_value_to_array(bld_base
, value
, file
, chan_index
,
596 reg_index
, ®
->Indirect
);
597 if (get_alloca_for_array(bld_base
, file
, reg_index
)) {
600 for (i
= 0; i
< size
; ++i
) {
601 switch(reg
->Register
.File
) {
602 case TGSI_FILE_OUTPUT
:
603 temp_ptr
= bld
->outputs
[i
+ range
.First
][chan_index
];
606 case TGSI_FILE_TEMPORARY
:
607 if (range
.First
+ i
>= ctx
->temps_count
)
609 temp_ptr
= ctx
->temps
[(i
+ range
.First
) * TGSI_NUM_CHANNELS
+ chan_index
];
615 value
= LLVMBuildExtractElement(builder
, array
,
616 lp_build_const_int32(gallivm
, i
), "");
617 LLVMBuildStore(builder
, value
, temp_ptr
);
621 switch(reg
->Register
.File
) {
622 case TGSI_FILE_OUTPUT
:
623 temp_ptr
= bld
->outputs
[reg
->Register
.Index
][chan_index
];
624 if (tgsi_type_is_64bit(dtype
))
625 temp_ptr2
= bld
->outputs
[reg
->Register
.Index
][chan_index
+ 1];
628 case TGSI_FILE_TEMPORARY
:
631 if (reg
->Register
.Index
>= ctx
->temps_count
)
633 array
= get_alloca_for_array(bld_base
, reg
->Register
.File
, reg
->Register
.Index
);
636 store_value_to_array(bld_base
, value
, reg
->Register
.File
, chan_index
, reg
->Register
.Index
,
640 temp_ptr
= ctx
->temps
[ TGSI_NUM_CHANNELS
* reg
->Register
.Index
+ chan_index
];
641 if (tgsi_type_is_64bit(dtype
))
642 temp_ptr2
= ctx
->temps
[ TGSI_NUM_CHANNELS
* reg
->Register
.Index
+ chan_index
+ 1];
649 if (!tgsi_type_is_64bit(dtype
))
650 LLVMBuildStore(builder
, value
, temp_ptr
);
652 LLVMValueRef ptr
= LLVMBuildBitCast(builder
, value
,
653 LLVMVectorType(LLVMIntTypeInContext(bld_base
->base
.gallivm
->context
, 32), 2), "");
655 value
= LLVMBuildExtractElement(builder
, ptr
,
656 bld_base
->uint_bld
.zero
, "");
657 val2
= LLVMBuildExtractElement(builder
, ptr
,
658 bld_base
->uint_bld
.one
, "");
660 LLVMBuildStore(builder
, bitcast(bld_base
, TGSI_TYPE_FLOAT
, value
), temp_ptr
);
661 LLVMBuildStore(builder
, bitcast(bld_base
, TGSI_TYPE_FLOAT
, val2
), temp_ptr2
);
667 static void bgnloop_emit(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 radeon_llvm_context
*ctx
= radeon_llvm_context(bld_base
);
672 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
673 LLVMBasicBlockRef loop_block
;
674 LLVMBasicBlockRef endloop_block
;
675 endloop_block
= LLVMAppendBasicBlockInContext(gallivm
->context
,
676 ctx
->main_fn
, "ENDLOOP");
677 loop_block
= LLVMInsertBasicBlockInContext(gallivm
->context
,
678 endloop_block
, "LOOP");
679 LLVMBuildBr(gallivm
->builder
, loop_block
);
680 LLVMPositionBuilderAtEnd(gallivm
->builder
, loop_block
);
682 if (++ctx
->loop_depth
> ctx
->loop_depth_max
) {
683 unsigned new_max
= ctx
->loop_depth_max
<< 1;
686 new_max
= RADEON_LLVM_INITIAL_CF_DEPTH
;
688 ctx
->loop
= REALLOC(ctx
->loop
, ctx
->loop_depth_max
*
689 sizeof(ctx
->loop
[0]),
690 new_max
* sizeof(ctx
->loop
[0]));
691 ctx
->loop_depth_max
= new_max
;
694 ctx
->loop
[ctx
->loop_depth
- 1].loop_block
= loop_block
;
695 ctx
->loop
[ctx
->loop_depth
- 1].endloop_block
= endloop_block
;
698 static void brk_emit(const struct lp_build_tgsi_action
*action
,
699 struct lp_build_tgsi_context
*bld_base
,
700 struct lp_build_emit_data
*emit_data
)
702 struct radeon_llvm_context
*ctx
= radeon_llvm_context(bld_base
);
703 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
704 struct radeon_llvm_loop
*current_loop
= get_current_loop(ctx
);
706 LLVMBuildBr(gallivm
->builder
, current_loop
->endloop_block
);
709 static void cont_emit(const struct lp_build_tgsi_action
*action
,
710 struct lp_build_tgsi_context
*bld_base
,
711 struct lp_build_emit_data
*emit_data
)
713 struct radeon_llvm_context
*ctx
= radeon_llvm_context(bld_base
);
714 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
715 struct radeon_llvm_loop
*current_loop
= get_current_loop(ctx
);
717 LLVMBuildBr(gallivm
->builder
, current_loop
->loop_block
);
720 static void else_emit(const struct lp_build_tgsi_action
*action
,
721 struct lp_build_tgsi_context
*bld_base
,
722 struct lp_build_emit_data
*emit_data
)
724 struct radeon_llvm_context
*ctx
= radeon_llvm_context(bld_base
);
725 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
726 struct radeon_llvm_branch
*current_branch
= get_current_branch(ctx
);
727 LLVMBasicBlockRef current_block
= LLVMGetInsertBlock(gallivm
->builder
);
729 /* We need to add a terminator to the current block if the previous
730 * instruction was an ENDIF.Example:
743 if (current_block
!= current_branch
->if_block
) {
744 LLVMBuildBr(gallivm
->builder
, current_branch
->endif_block
);
746 if (!LLVMGetBasicBlockTerminator(current_branch
->if_block
)) {
747 LLVMBuildBr(gallivm
->builder
, current_branch
->endif_block
);
749 current_branch
->has_else
= 1;
750 LLVMPositionBuilderAtEnd(gallivm
->builder
, current_branch
->else_block
);
753 static void endif_emit(const struct lp_build_tgsi_action
*action
,
754 struct lp_build_tgsi_context
*bld_base
,
755 struct lp_build_emit_data
*emit_data
)
757 struct radeon_llvm_context
*ctx
= radeon_llvm_context(bld_base
);
758 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
759 struct radeon_llvm_branch
*current_branch
= get_current_branch(ctx
);
760 LLVMBasicBlockRef current_block
= LLVMGetInsertBlock(gallivm
->builder
);
762 /* If we have consecutive ENDIF instructions, then the first ENDIF
763 * will not have a terminator, so we need to add one. */
764 if (current_block
!= current_branch
->if_block
765 && current_block
!= current_branch
->else_block
766 && !LLVMGetBasicBlockTerminator(current_block
)) {
768 LLVMBuildBr(gallivm
->builder
, current_branch
->endif_block
);
770 if (!LLVMGetBasicBlockTerminator(current_branch
->else_block
)) {
771 LLVMPositionBuilderAtEnd(gallivm
->builder
, current_branch
->else_block
);
772 LLVMBuildBr(gallivm
->builder
, current_branch
->endif_block
);
775 if (!LLVMGetBasicBlockTerminator(current_branch
->if_block
)) {
776 LLVMPositionBuilderAtEnd(gallivm
->builder
, current_branch
->if_block
);
777 LLVMBuildBr(gallivm
->builder
, current_branch
->endif_block
);
780 LLVMPositionBuilderAtEnd(gallivm
->builder
, current_branch
->endif_block
);
784 static void endloop_emit(const struct lp_build_tgsi_action
*action
,
785 struct lp_build_tgsi_context
*bld_base
,
786 struct lp_build_emit_data
*emit_data
)
788 struct radeon_llvm_context
*ctx
= radeon_llvm_context(bld_base
);
789 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
790 struct radeon_llvm_loop
*current_loop
= get_current_loop(ctx
);
792 if (!LLVMGetBasicBlockTerminator(LLVMGetInsertBlock(gallivm
->builder
))) {
793 LLVMBuildBr(gallivm
->builder
, current_loop
->loop_block
);
796 LLVMPositionBuilderAtEnd(gallivm
->builder
, current_loop
->endloop_block
);
800 static void if_cond_emit(const struct lp_build_tgsi_action
*action
,
801 struct lp_build_tgsi_context
*bld_base
,
802 struct lp_build_emit_data
*emit_data
,
805 struct radeon_llvm_context
*ctx
= radeon_llvm_context(bld_base
);
806 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
807 LLVMBasicBlockRef if_block
, else_block
, endif_block
;
809 endif_block
= LLVMAppendBasicBlockInContext(gallivm
->context
,
810 ctx
->main_fn
, "ENDIF");
811 if_block
= LLVMInsertBasicBlockInContext(gallivm
->context
,
813 else_block
= LLVMInsertBasicBlockInContext(gallivm
->context
,
814 endif_block
, "ELSE");
815 LLVMBuildCondBr(gallivm
->builder
, cond
, if_block
, else_block
);
816 LLVMPositionBuilderAtEnd(gallivm
->builder
, if_block
);
818 if (++ctx
->branch_depth
> ctx
->branch_depth_max
) {
819 unsigned new_max
= ctx
->branch_depth_max
<< 1;
822 new_max
= RADEON_LLVM_INITIAL_CF_DEPTH
;
824 ctx
->branch
= REALLOC(ctx
->branch
, ctx
->branch_depth_max
*
825 sizeof(ctx
->branch
[0]),
826 new_max
* sizeof(ctx
->branch
[0]));
827 ctx
->branch_depth_max
= new_max
;
830 ctx
->branch
[ctx
->branch_depth
- 1].endif_block
= endif_block
;
831 ctx
->branch
[ctx
->branch_depth
- 1].if_block
= if_block
;
832 ctx
->branch
[ctx
->branch_depth
- 1].else_block
= else_block
;
833 ctx
->branch
[ctx
->branch_depth
- 1].has_else
= 0;
836 static void if_emit(const struct lp_build_tgsi_action
*action
,
837 struct lp_build_tgsi_context
*bld_base
,
838 struct lp_build_emit_data
*emit_data
)
840 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
843 cond
= LLVMBuildFCmp(gallivm
->builder
, LLVMRealUNE
,
845 bld_base
->base
.zero
, "");
847 if_cond_emit(action
, bld_base
, emit_data
, cond
);
850 static void uif_emit(const struct lp_build_tgsi_action
*action
,
851 struct lp_build_tgsi_context
*bld_base
,
852 struct lp_build_emit_data
*emit_data
)
854 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
857 cond
= LLVMBuildICmp(gallivm
->builder
, LLVMIntNE
,
858 bitcast(bld_base
, TGSI_TYPE_UNSIGNED
, emit_data
->args
[0]),
859 bld_base
->int_bld
.zero
, "");
861 if_cond_emit(action
, bld_base
, emit_data
, cond
);
864 static void kill_if_fetch_args(struct lp_build_tgsi_context
*bld_base
,
865 struct lp_build_emit_data
*emit_data
)
867 const struct tgsi_full_instruction
*inst
= emit_data
->inst
;
868 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
869 LLVMBuilderRef builder
= gallivm
->builder
;
871 LLVMValueRef conds
[TGSI_NUM_CHANNELS
];
873 for (i
= 0; i
< TGSI_NUM_CHANNELS
; i
++) {
874 LLVMValueRef value
= lp_build_emit_fetch(bld_base
, inst
, 0, i
);
875 conds
[i
] = LLVMBuildFCmp(builder
, LLVMRealOLT
, value
,
876 bld_base
->base
.zero
, "");
879 /* Or the conditions together */
880 for (i
= TGSI_NUM_CHANNELS
- 1; i
> 0; i
--) {
881 conds
[i
- 1] = LLVMBuildOr(builder
, conds
[i
], conds
[i
- 1], "");
884 emit_data
->dst_type
= LLVMVoidTypeInContext(gallivm
->context
);
885 emit_data
->arg_count
= 1;
886 emit_data
->args
[0] = LLVMBuildSelect(builder
, conds
[0],
887 lp_build_const_float(gallivm
, -1.0f
),
888 bld_base
->base
.zero
, "");
891 static void kil_emit(const struct lp_build_tgsi_action
*action
,
892 struct lp_build_tgsi_context
*bld_base
,
893 struct lp_build_emit_data
*emit_data
)
896 for (i
= 0; i
< emit_data
->arg_count
; i
++) {
897 emit_data
->output
[i
] = lp_build_intrinsic_unary(
898 bld_base
->base
.gallivm
->builder
,
900 emit_data
->dst_type
, emit_data
->args
[i
]);
904 static void radeon_llvm_cube_to_2d_coords(struct lp_build_tgsi_context
*bld_base
,
905 LLVMValueRef
*in
, LLVMValueRef
*out
)
907 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
908 LLVMBuilderRef builder
= gallivm
->builder
;
909 LLVMTypeRef type
= bld_base
->base
.elem_type
;
910 LLVMValueRef coords
[4];
911 LLVMValueRef mad_args
[3];
912 LLVMValueRef v
, cube_vec
;
915 cube_vec
= lp_build_gather_values(bld_base
->base
.gallivm
, in
, 4);
916 v
= lp_build_intrinsic(builder
, "llvm.AMDGPU.cube", LLVMVectorType(type
, 4),
917 &cube_vec
, 1, LLVMReadNoneAttribute
);
919 for (i
= 0; i
< 4; ++i
)
920 coords
[i
] = LLVMBuildExtractElement(builder
, v
,
921 lp_build_const_int32(gallivm
, i
), "");
923 coords
[2] = lp_build_intrinsic(builder
, "llvm.fabs.f32",
924 type
, &coords
[2], 1, LLVMReadNoneAttribute
);
925 coords
[2] = lp_build_emit_llvm_unary(bld_base
, TGSI_OPCODE_RCP
, coords
[2]);
927 mad_args
[1] = coords
[2];
928 mad_args
[2] = LLVMConstReal(type
, 1.5);
930 mad_args
[0] = coords
[0];
931 coords
[0] = lp_build_emit_llvm_ternary(bld_base
, TGSI_OPCODE_MAD
,
932 mad_args
[0], mad_args
[1], mad_args
[2]);
934 mad_args
[0] = coords
[1];
935 coords
[1] = lp_build_emit_llvm_ternary(bld_base
, TGSI_OPCODE_MAD
,
936 mad_args
[0], mad_args
[1], mad_args
[2]);
938 /* apply xyz = yxw swizzle to cooords */
944 void radeon_llvm_emit_prepare_cube_coords(struct lp_build_tgsi_context
*bld_base
,
945 struct lp_build_emit_data
*emit_data
,
946 LLVMValueRef
*coords_arg
,
947 LLVMValueRef
*derivs_arg
)
950 unsigned target
= emit_data
->inst
->Texture
.Texture
;
951 unsigned opcode
= emit_data
->inst
->Instruction
.Opcode
;
952 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
953 LLVMBuilderRef builder
= gallivm
->builder
;
954 LLVMValueRef coords
[4];
957 radeon_llvm_cube_to_2d_coords(bld_base
, coords_arg
, coords
);
959 if (opcode
== TGSI_OPCODE_TXD
&& derivs_arg
) {
960 LLVMValueRef derivs
[4];
963 /* Convert cube derivatives to 2D derivatives. */
964 for (axis
= 0; axis
< 2; axis
++) {
965 LLVMValueRef shifted_cube_coords
[4], shifted_coords
[4];
967 /* Shift the cube coordinates by the derivatives to get
968 * the cube coordinates of the "neighboring pixel".
970 for (i
= 0; i
< 3; i
++)
971 shifted_cube_coords
[i
] =
972 LLVMBuildFAdd(builder
, coords_arg
[i
],
973 derivs_arg
[axis
*3+i
], "");
974 shifted_cube_coords
[3] = LLVMGetUndef(bld_base
->base
.elem_type
);
976 /* Project the shifted cube coordinates onto the face. */
977 radeon_llvm_cube_to_2d_coords(bld_base
, shifted_cube_coords
,
980 /* Subtract both sets of 2D coordinates to get 2D derivatives.
981 * This won't work if the shifted coordinates ended up
982 * in a different face.
984 for (i
= 0; i
< 2; i
++)
985 derivs
[axis
* 2 + i
] =
986 LLVMBuildFSub(builder
, shifted_coords
[i
],
990 memcpy(derivs_arg
, derivs
, sizeof(derivs
));
993 if (target
== TGSI_TEXTURE_CUBE_ARRAY
||
994 target
== TGSI_TEXTURE_SHADOWCUBE_ARRAY
) {
995 /* for cube arrays coord.z = coord.w(array_index) * 8 + face */
996 /* coords_arg.w component - array_index for cube arrays */
997 coords
[2] = lp_build_emit_llvm_ternary(bld_base
, TGSI_OPCODE_MAD
,
998 coords_arg
[3], lp_build_const_float(gallivm
, 8.0), coords
[2]);
1001 /* Preserve compare/lod/bias. Put it in coords.w. */
1002 if (opcode
== TGSI_OPCODE_TEX2
||
1003 opcode
== TGSI_OPCODE_TXB2
||
1004 opcode
== TGSI_OPCODE_TXL2
) {
1005 coords
[3] = coords_arg
[4];
1006 } else if (opcode
== TGSI_OPCODE_TXB
||
1007 opcode
== TGSI_OPCODE_TXL
||
1008 target
== TGSI_TEXTURE_SHADOWCUBE
) {
1009 coords
[3] = coords_arg
[3];
1012 memcpy(coords_arg
, coords
, sizeof(coords
));
1015 static void emit_icmp(const struct lp_build_tgsi_action
*action
,
1016 struct lp_build_tgsi_context
*bld_base
,
1017 struct lp_build_emit_data
*emit_data
)
1020 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1021 LLVMContextRef context
= bld_base
->base
.gallivm
->context
;
1023 switch (emit_data
->inst
->Instruction
.Opcode
) {
1024 case TGSI_OPCODE_USEQ
: pred
= LLVMIntEQ
; break;
1025 case TGSI_OPCODE_USNE
: pred
= LLVMIntNE
; break;
1026 case TGSI_OPCODE_USGE
: pred
= LLVMIntUGE
; break;
1027 case TGSI_OPCODE_USLT
: pred
= LLVMIntULT
; break;
1028 case TGSI_OPCODE_ISGE
: pred
= LLVMIntSGE
; break;
1029 case TGSI_OPCODE_ISLT
: pred
= LLVMIntSLT
; break;
1031 assert(!"unknown instruction");
1036 LLVMValueRef v
= LLVMBuildICmp(builder
, pred
,
1037 emit_data
->args
[0], emit_data
->args
[1],"");
1039 v
= LLVMBuildSExtOrBitCast(builder
, v
,
1040 LLVMInt32TypeInContext(context
), "");
1042 emit_data
->output
[emit_data
->chan
] = v
;
1045 static void emit_ucmp(const struct lp_build_tgsi_action
*action
,
1046 struct lp_build_tgsi_context
*bld_base
,
1047 struct lp_build_emit_data
*emit_data
)
1049 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1051 LLVMValueRef arg0
= LLVMBuildBitCast(builder
, emit_data
->args
[0],
1052 bld_base
->uint_bld
.elem_type
, "");
1054 LLVMValueRef v
= LLVMBuildICmp(builder
, LLVMIntNE
, arg0
,
1055 bld_base
->uint_bld
.zero
, "");
1057 emit_data
->output
[emit_data
->chan
] =
1058 LLVMBuildSelect(builder
, v
, emit_data
->args
[1], emit_data
->args
[2], "");
1061 static void emit_cmp(const struct lp_build_tgsi_action
*action
,
1062 struct lp_build_tgsi_context
*bld_base
,
1063 struct lp_build_emit_data
*emit_data
)
1065 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1066 LLVMValueRef cond
, *args
= emit_data
->args
;
1068 cond
= LLVMBuildFCmp(builder
, LLVMRealOLT
, args
[0],
1069 bld_base
->base
.zero
, "");
1071 emit_data
->output
[emit_data
->chan
] =
1072 LLVMBuildSelect(builder
, cond
, args
[1], args
[2], "");
1075 static void emit_set_cond(const struct lp_build_tgsi_action
*action
,
1076 struct lp_build_tgsi_context
*bld_base
,
1077 struct lp_build_emit_data
*emit_data
)
1079 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1080 LLVMRealPredicate pred
;
1083 /* Use ordered for everything but NE (which is usual for
1084 * float comparisons)
1086 switch (emit_data
->inst
->Instruction
.Opcode
) {
1087 case TGSI_OPCODE_SGE
: pred
= LLVMRealOGE
; break;
1088 case TGSI_OPCODE_SEQ
: pred
= LLVMRealOEQ
; break;
1089 case TGSI_OPCODE_SLE
: pred
= LLVMRealOLE
; break;
1090 case TGSI_OPCODE_SLT
: pred
= LLVMRealOLT
; break;
1091 case TGSI_OPCODE_SNE
: pred
= LLVMRealUNE
; break;
1092 case TGSI_OPCODE_SGT
: pred
= LLVMRealOGT
; break;
1093 default: assert(!"unknown instruction"); pred
= 0; break;
1096 cond
= LLVMBuildFCmp(builder
,
1097 pred
, emit_data
->args
[0], emit_data
->args
[1], "");
1099 emit_data
->output
[emit_data
->chan
] = LLVMBuildSelect(builder
,
1100 cond
, bld_base
->base
.one
, bld_base
->base
.zero
, "");
1103 static void emit_fcmp(const struct lp_build_tgsi_action
*action
,
1104 struct lp_build_tgsi_context
*bld_base
,
1105 struct lp_build_emit_data
*emit_data
)
1107 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1108 LLVMContextRef context
= bld_base
->base
.gallivm
->context
;
1109 LLVMRealPredicate pred
;
1111 /* Use ordered for everything but NE (which is usual for
1112 * float comparisons)
1114 switch (emit_data
->inst
->Instruction
.Opcode
) {
1115 case TGSI_OPCODE_FSEQ
: pred
= LLVMRealOEQ
; break;
1116 case TGSI_OPCODE_FSGE
: pred
= LLVMRealOGE
; break;
1117 case TGSI_OPCODE_FSLT
: pred
= LLVMRealOLT
; break;
1118 case TGSI_OPCODE_FSNE
: pred
= LLVMRealUNE
; break;
1119 default: assert(!"unknown instruction"); pred
= 0; break;
1122 LLVMValueRef v
= LLVMBuildFCmp(builder
, pred
,
1123 emit_data
->args
[0], emit_data
->args
[1],"");
1125 v
= LLVMBuildSExtOrBitCast(builder
, v
,
1126 LLVMInt32TypeInContext(context
), "");
1128 emit_data
->output
[emit_data
->chan
] = v
;
1131 static void emit_dcmp(const struct lp_build_tgsi_action
*action
,
1132 struct lp_build_tgsi_context
*bld_base
,
1133 struct lp_build_emit_data
*emit_data
)
1135 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1136 LLVMContextRef context
= bld_base
->base
.gallivm
->context
;
1137 LLVMRealPredicate pred
;
1139 /* Use ordered for everything but NE (which is usual for
1140 * float comparisons)
1142 switch (emit_data
->inst
->Instruction
.Opcode
) {
1143 case TGSI_OPCODE_DSEQ
: pred
= LLVMRealOEQ
; break;
1144 case TGSI_OPCODE_DSGE
: pred
= LLVMRealOGE
; break;
1145 case TGSI_OPCODE_DSLT
: pred
= LLVMRealOLT
; break;
1146 case TGSI_OPCODE_DSNE
: pred
= LLVMRealUNE
; break;
1147 default: assert(!"unknown instruction"); pred
= 0; break;
1150 LLVMValueRef v
= LLVMBuildFCmp(builder
, pred
,
1151 emit_data
->args
[0], emit_data
->args
[1],"");
1153 v
= LLVMBuildSExtOrBitCast(builder
, v
,
1154 LLVMInt32TypeInContext(context
), "");
1156 emit_data
->output
[emit_data
->chan
] = v
;
1159 static void emit_not(const struct lp_build_tgsi_action
*action
,
1160 struct lp_build_tgsi_context
*bld_base
,
1161 struct lp_build_emit_data
*emit_data
)
1163 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1164 LLVMValueRef v
= bitcast(bld_base
, TGSI_TYPE_UNSIGNED
,
1165 emit_data
->args
[0]);
1166 emit_data
->output
[emit_data
->chan
] = LLVMBuildNot(builder
, v
, "");
1169 static void emit_arl(const struct lp_build_tgsi_action
*action
,
1170 struct lp_build_tgsi_context
*bld_base
,
1171 struct lp_build_emit_data
*emit_data
)
1173 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1174 LLVMValueRef floor_index
= lp_build_emit_llvm_unary(bld_base
, TGSI_OPCODE_FLR
, emit_data
->args
[0]);
1175 emit_data
->output
[emit_data
->chan
] = LLVMBuildFPToSI(builder
,
1176 floor_index
, bld_base
->base
.int_elem_type
, "");
1179 static void emit_and(const struct lp_build_tgsi_action
*action
,
1180 struct lp_build_tgsi_context
*bld_base
,
1181 struct lp_build_emit_data
*emit_data
)
1183 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1184 emit_data
->output
[emit_data
->chan
] = LLVMBuildAnd(builder
,
1185 emit_data
->args
[0], emit_data
->args
[1], "");
1188 static void emit_or(const struct lp_build_tgsi_action
*action
,
1189 struct lp_build_tgsi_context
*bld_base
,
1190 struct lp_build_emit_data
*emit_data
)
1192 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1193 emit_data
->output
[emit_data
->chan
] = LLVMBuildOr(builder
,
1194 emit_data
->args
[0], emit_data
->args
[1], "");
1197 static void emit_uadd(const struct lp_build_tgsi_action
*action
,
1198 struct lp_build_tgsi_context
*bld_base
,
1199 struct lp_build_emit_data
*emit_data
)
1201 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1202 emit_data
->output
[emit_data
->chan
] = LLVMBuildAdd(builder
,
1203 emit_data
->args
[0], emit_data
->args
[1], "");
1206 static void emit_udiv(const struct lp_build_tgsi_action
*action
,
1207 struct lp_build_tgsi_context
*bld_base
,
1208 struct lp_build_emit_data
*emit_data
)
1210 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1211 emit_data
->output
[emit_data
->chan
] = LLVMBuildUDiv(builder
,
1212 emit_data
->args
[0], emit_data
->args
[1], "");
1215 static void emit_idiv(const struct lp_build_tgsi_action
*action
,
1216 struct lp_build_tgsi_context
*bld_base
,
1217 struct lp_build_emit_data
*emit_data
)
1219 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1220 emit_data
->output
[emit_data
->chan
] = LLVMBuildSDiv(builder
,
1221 emit_data
->args
[0], emit_data
->args
[1], "");
1224 static void emit_mod(const struct lp_build_tgsi_action
*action
,
1225 struct lp_build_tgsi_context
*bld_base
,
1226 struct lp_build_emit_data
*emit_data
)
1228 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1229 emit_data
->output
[emit_data
->chan
] = LLVMBuildSRem(builder
,
1230 emit_data
->args
[0], emit_data
->args
[1], "");
1233 static void emit_umod(const struct lp_build_tgsi_action
*action
,
1234 struct lp_build_tgsi_context
*bld_base
,
1235 struct lp_build_emit_data
*emit_data
)
1237 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1238 emit_data
->output
[emit_data
->chan
] = LLVMBuildURem(builder
,
1239 emit_data
->args
[0], emit_data
->args
[1], "");
1242 static void emit_shl(const struct lp_build_tgsi_action
*action
,
1243 struct lp_build_tgsi_context
*bld_base
,
1244 struct lp_build_emit_data
*emit_data
)
1246 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1247 emit_data
->output
[emit_data
->chan
] = LLVMBuildShl(builder
,
1248 emit_data
->args
[0], emit_data
->args
[1], "");
1251 static void emit_ushr(const struct lp_build_tgsi_action
*action
,
1252 struct lp_build_tgsi_context
*bld_base
,
1253 struct lp_build_emit_data
*emit_data
)
1255 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1256 emit_data
->output
[emit_data
->chan
] = LLVMBuildLShr(builder
,
1257 emit_data
->args
[0], emit_data
->args
[1], "");
1259 static void emit_ishr(const struct lp_build_tgsi_action
*action
,
1260 struct lp_build_tgsi_context
*bld_base
,
1261 struct lp_build_emit_data
*emit_data
)
1263 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1264 emit_data
->output
[emit_data
->chan
] = LLVMBuildAShr(builder
,
1265 emit_data
->args
[0], emit_data
->args
[1], "");
1268 static void emit_xor(const struct lp_build_tgsi_action
*action
,
1269 struct lp_build_tgsi_context
*bld_base
,
1270 struct lp_build_emit_data
*emit_data
)
1272 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1273 emit_data
->output
[emit_data
->chan
] = LLVMBuildXor(builder
,
1274 emit_data
->args
[0], emit_data
->args
[1], "");
1277 static void emit_ssg(const struct lp_build_tgsi_action
*action
,
1278 struct lp_build_tgsi_context
*bld_base
,
1279 struct lp_build_emit_data
*emit_data
)
1281 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1283 LLVMValueRef cmp
, val
;
1285 if (emit_data
->inst
->Instruction
.Opcode
== TGSI_OPCODE_ISSG
) {
1286 cmp
= LLVMBuildICmp(builder
, LLVMIntSGT
, emit_data
->args
[0], bld_base
->int_bld
.zero
, "");
1287 val
= LLVMBuildSelect(builder
, cmp
, bld_base
->int_bld
.one
, emit_data
->args
[0], "");
1288 cmp
= LLVMBuildICmp(builder
, LLVMIntSGE
, val
, bld_base
->int_bld
.zero
, "");
1289 val
= LLVMBuildSelect(builder
, cmp
, val
, LLVMConstInt(bld_base
->int_bld
.elem_type
, -1, true), "");
1290 } else { // float SSG
1291 cmp
= LLVMBuildFCmp(builder
, LLVMRealOGT
, emit_data
->args
[0], bld_base
->base
.zero
, "");
1292 val
= LLVMBuildSelect(builder
, cmp
, bld_base
->base
.one
, emit_data
->args
[0], "");
1293 cmp
= LLVMBuildFCmp(builder
, LLVMRealOGE
, val
, bld_base
->base
.zero
, "");
1294 val
= LLVMBuildSelect(builder
, cmp
, val
, LLVMConstReal(bld_base
->base
.elem_type
, -1), "");
1297 emit_data
->output
[emit_data
->chan
] = val
;
1300 static void emit_ineg(const struct lp_build_tgsi_action
*action
,
1301 struct lp_build_tgsi_context
*bld_base
,
1302 struct lp_build_emit_data
*emit_data
)
1304 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1305 emit_data
->output
[emit_data
->chan
] = LLVMBuildNeg(builder
,
1306 emit_data
->args
[0], "");
1309 static void emit_dneg(const struct lp_build_tgsi_action
*action
,
1310 struct lp_build_tgsi_context
*bld_base
,
1311 struct lp_build_emit_data
*emit_data
)
1313 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1314 emit_data
->output
[emit_data
->chan
] = LLVMBuildFNeg(builder
,
1315 emit_data
->args
[0], "");
1318 static void emit_frac(const struct lp_build_tgsi_action
*action
,
1319 struct lp_build_tgsi_context
*bld_base
,
1320 struct lp_build_emit_data
*emit_data
)
1322 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1325 if (emit_data
->info
->opcode
== TGSI_OPCODE_FRC
)
1326 intr
= "llvm.floor.f32";
1327 else if (emit_data
->info
->opcode
== TGSI_OPCODE_DFRAC
)
1328 intr
= "llvm.floor.f64";
1334 LLVMValueRef floor
= lp_build_intrinsic(builder
, intr
, emit_data
->dst_type
,
1335 &emit_data
->args
[0], 1,
1336 LLVMReadNoneAttribute
);
1337 emit_data
->output
[emit_data
->chan
] = LLVMBuildFSub(builder
,
1338 emit_data
->args
[0], floor
, "");
1341 static void emit_f2i(const struct lp_build_tgsi_action
*action
,
1342 struct lp_build_tgsi_context
*bld_base
,
1343 struct lp_build_emit_data
*emit_data
)
1345 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1346 emit_data
->output
[emit_data
->chan
] = LLVMBuildFPToSI(builder
,
1347 emit_data
->args
[0], bld_base
->int_bld
.elem_type
, "");
1350 static void emit_f2u(const struct lp_build_tgsi_action
*action
,
1351 struct lp_build_tgsi_context
*bld_base
,
1352 struct lp_build_emit_data
*emit_data
)
1354 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1355 emit_data
->output
[emit_data
->chan
] = LLVMBuildFPToUI(builder
,
1356 emit_data
->args
[0], bld_base
->uint_bld
.elem_type
, "");
1359 static void emit_i2f(const struct lp_build_tgsi_action
*action
,
1360 struct lp_build_tgsi_context
*bld_base
,
1361 struct lp_build_emit_data
*emit_data
)
1363 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1364 emit_data
->output
[emit_data
->chan
] = LLVMBuildSIToFP(builder
,
1365 emit_data
->args
[0], bld_base
->base
.elem_type
, "");
1368 static void emit_u2f(const struct lp_build_tgsi_action
*action
,
1369 struct lp_build_tgsi_context
*bld_base
,
1370 struct lp_build_emit_data
*emit_data
)
1372 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1373 emit_data
->output
[emit_data
->chan
] = LLVMBuildUIToFP(builder
,
1374 emit_data
->args
[0], bld_base
->base
.elem_type
, "");
1377 static void emit_immediate(struct lp_build_tgsi_context
*bld_base
,
1378 const struct tgsi_full_immediate
*imm
)
1381 struct radeon_llvm_context
*ctx
= radeon_llvm_context(bld_base
);
1383 for (i
= 0; i
< 4; ++i
) {
1384 ctx
->soa
.immediates
[ctx
->soa
.num_immediates
][i
] =
1385 LLVMConstInt(bld_base
->uint_bld
.elem_type
, imm
->u
[i
].Uint
, false );
1388 ctx
->soa
.num_immediates
++;
1392 build_tgsi_intrinsic_nomem(const struct lp_build_tgsi_action
*action
,
1393 struct lp_build_tgsi_context
*bld_base
,
1394 struct lp_build_emit_data
*emit_data
)
1396 struct lp_build_context
*base
= &bld_base
->base
;
1397 emit_data
->output
[emit_data
->chan
] =
1398 lp_build_intrinsic(base
->gallivm
->builder
, action
->intr_name
,
1399 emit_data
->dst_type
, emit_data
->args
,
1400 emit_data
->arg_count
, LLVMReadNoneAttribute
);
1403 static void emit_bfi(const struct lp_build_tgsi_action
*action
,
1404 struct lp_build_tgsi_context
*bld_base
,
1405 struct lp_build_emit_data
*emit_data
)
1407 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1408 LLVMBuilderRef builder
= gallivm
->builder
;
1409 LLVMValueRef bfi_args
[3];
1411 // Calculate the bitmask: (((1 << src3) - 1) << src2
1412 bfi_args
[0] = LLVMBuildShl(builder
,
1413 LLVMBuildSub(builder
,
1414 LLVMBuildShl(builder
,
1415 bld_base
->int_bld
.one
,
1416 emit_data
->args
[3], ""),
1417 bld_base
->int_bld
.one
, ""),
1418 emit_data
->args
[2], "");
1420 bfi_args
[1] = LLVMBuildShl(builder
, emit_data
->args
[1],
1421 emit_data
->args
[2], "");
1423 bfi_args
[2] = emit_data
->args
[0];
1426 * (arg0 & arg1) | (~arg0 & arg2) = arg2 ^ (arg0 & (arg1 ^ arg2)
1427 * Use the right-hand side, which the LLVM backend can convert to V_BFI.
1429 emit_data
->output
[emit_data
->chan
] =
1430 LLVMBuildXor(builder
, bfi_args
[2],
1431 LLVMBuildAnd(builder
, bfi_args
[0],
1432 LLVMBuildXor(builder
, bfi_args
[1], bfi_args
[2],
1436 /* this is ffs in C */
1437 static void emit_lsb(const struct lp_build_tgsi_action
*action
,
1438 struct lp_build_tgsi_context
*bld_base
,
1439 struct lp_build_emit_data
*emit_data
)
1441 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1442 LLVMValueRef args
[2] = {
1445 /* The value of 1 means that ffs(x=0) = undef, so LLVM won't
1446 * add special code to check for x=0. The reason is that
1447 * the LLVM behavior for x=0 is different from what we
1450 * The hardware already implements the correct behavior.
1452 lp_build_const_int32(gallivm
, 1)
1455 emit_data
->output
[emit_data
->chan
] =
1456 lp_build_intrinsic(gallivm
->builder
, "llvm.cttz.i32",
1457 emit_data
->dst_type
, args
, ARRAY_SIZE(args
),
1458 LLVMReadNoneAttribute
);
1461 /* Find the last bit set. */
1462 static void emit_umsb(const struct lp_build_tgsi_action
*action
,
1463 struct lp_build_tgsi_context
*bld_base
,
1464 struct lp_build_emit_data
*emit_data
)
1466 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1467 LLVMBuilderRef builder
= gallivm
->builder
;
1468 LLVMValueRef args
[2] = {
1470 /* Don't generate code for handling zero: */
1471 lp_build_const_int32(gallivm
, 1)
1475 lp_build_intrinsic(builder
, "llvm.ctlz.i32",
1476 emit_data
->dst_type
, args
, ARRAY_SIZE(args
),
1477 LLVMReadNoneAttribute
);
1479 /* The HW returns the last bit index from MSB, but TGSI wants
1480 * the index from LSB. Invert it by doing "31 - msb". */
1481 msb
= LLVMBuildSub(builder
, lp_build_const_int32(gallivm
, 31),
1484 /* Check for zero: */
1485 emit_data
->output
[emit_data
->chan
] =
1486 LLVMBuildSelect(builder
,
1487 LLVMBuildICmp(builder
, LLVMIntEQ
, args
[0],
1488 bld_base
->uint_bld
.zero
, ""),
1489 lp_build_const_int32(gallivm
, -1), msb
, "");
1492 /* Find the last bit opposite of the sign bit. */
1493 static void emit_imsb(const struct lp_build_tgsi_action
*action
,
1494 struct lp_build_tgsi_context
*bld_base
,
1495 struct lp_build_emit_data
*emit_data
)
1497 struct gallivm_state
*gallivm
= bld_base
->base
.gallivm
;
1498 LLVMBuilderRef builder
= gallivm
->builder
;
1499 LLVMValueRef arg
= emit_data
->args
[0];
1502 lp_build_intrinsic(builder
, "llvm.AMDGPU.flbit.i32",
1503 emit_data
->dst_type
, &arg
, 1,
1504 LLVMReadNoneAttribute
);
1506 /* The HW returns the last bit index from MSB, but TGSI wants
1507 * the index from LSB. Invert it by doing "31 - msb". */
1508 msb
= LLVMBuildSub(builder
, lp_build_const_int32(gallivm
, 31),
1511 /* If arg == 0 || arg == -1 (0xffffffff), return -1. */
1512 LLVMValueRef all_ones
= lp_build_const_int32(gallivm
, -1);
1515 LLVMBuildOr(builder
,
1516 LLVMBuildICmp(builder
, LLVMIntEQ
, arg
,
1517 bld_base
->uint_bld
.zero
, ""),
1518 LLVMBuildICmp(builder
, LLVMIntEQ
, arg
,
1521 emit_data
->output
[emit_data
->chan
] =
1522 LLVMBuildSelect(builder
, cond
, all_ones
, msb
, "");
1525 static void emit_iabs(const struct lp_build_tgsi_action
*action
,
1526 struct lp_build_tgsi_context
*bld_base
,
1527 struct lp_build_emit_data
*emit_data
)
1529 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1531 emit_data
->output
[emit_data
->chan
] =
1532 lp_build_emit_llvm_binary(bld_base
, TGSI_OPCODE_IMAX
,
1534 LLVMBuildNeg(builder
,
1535 emit_data
->args
[0], ""));
1538 static void emit_minmax_int(const struct lp_build_tgsi_action
*action
,
1539 struct lp_build_tgsi_context
*bld_base
,
1540 struct lp_build_emit_data
*emit_data
)
1542 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1543 LLVMIntPredicate op
;
1545 switch (emit_data
->info
->opcode
) {
1548 case TGSI_OPCODE_IMAX
:
1551 case TGSI_OPCODE_IMIN
:
1554 case TGSI_OPCODE_UMAX
:
1557 case TGSI_OPCODE_UMIN
:
1562 emit_data
->output
[emit_data
->chan
] =
1563 LLVMBuildSelect(builder
,
1564 LLVMBuildICmp(builder
, op
, emit_data
->args
[0],
1565 emit_data
->args
[1], ""),
1567 emit_data
->args
[1], "");
1570 static void pk2h_fetch_args(struct lp_build_tgsi_context
*bld_base
,
1571 struct lp_build_emit_data
*emit_data
)
1573 emit_data
->args
[0] = lp_build_emit_fetch(bld_base
, emit_data
->inst
,
1575 emit_data
->args
[1] = lp_build_emit_fetch(bld_base
, emit_data
->inst
,
1579 static void emit_pk2h(const struct lp_build_tgsi_action
*action
,
1580 struct lp_build_tgsi_context
*bld_base
,
1581 struct lp_build_emit_data
*emit_data
)
1583 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1584 LLVMContextRef context
= bld_base
->base
.gallivm
->context
;
1585 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
1586 LLVMTypeRef fp16
, i16
;
1587 LLVMValueRef const16
, comp
[2];
1590 fp16
= LLVMHalfTypeInContext(context
);
1591 i16
= LLVMInt16TypeInContext(context
);
1592 const16
= lp_build_const_int32(uint_bld
->gallivm
, 16);
1594 for (i
= 0; i
< 2; i
++) {
1595 comp
[i
] = LLVMBuildFPTrunc(builder
, emit_data
->args
[i
], fp16
, "");
1596 comp
[i
] = LLVMBuildBitCast(builder
, comp
[i
], i16
, "");
1597 comp
[i
] = LLVMBuildZExt(builder
, comp
[i
], uint_bld
->elem_type
, "");
1600 comp
[1] = LLVMBuildShl(builder
, comp
[1], const16
, "");
1601 comp
[0] = LLVMBuildOr(builder
, comp
[0], comp
[1], "");
1603 emit_data
->output
[emit_data
->chan
] = comp
[0];
1606 static void up2h_fetch_args(struct lp_build_tgsi_context
*bld_base
,
1607 struct lp_build_emit_data
*emit_data
)
1609 emit_data
->args
[0] = lp_build_emit_fetch(bld_base
, emit_data
->inst
,
1613 static void emit_up2h(const struct lp_build_tgsi_action
*action
,
1614 struct lp_build_tgsi_context
*bld_base
,
1615 struct lp_build_emit_data
*emit_data
)
1617 LLVMBuilderRef builder
= bld_base
->base
.gallivm
->builder
;
1618 LLVMContextRef context
= bld_base
->base
.gallivm
->context
;
1619 struct lp_build_context
*uint_bld
= &bld_base
->uint_bld
;
1620 LLVMTypeRef fp16
, i16
;
1621 LLVMValueRef const16
, input
, val
;
1624 fp16
= LLVMHalfTypeInContext(context
);
1625 i16
= LLVMInt16TypeInContext(context
);
1626 const16
= lp_build_const_int32(uint_bld
->gallivm
, 16);
1627 input
= emit_data
->args
[0];
1629 for (i
= 0; i
< 2; i
++) {
1630 val
= i
== 1 ? LLVMBuildLShr(builder
, input
, const16
, "") : input
;
1631 val
= LLVMBuildTrunc(builder
, val
, i16
, "");
1632 val
= LLVMBuildBitCast(builder
, val
, fp16
, "");
1633 emit_data
->output
[i
] =
1634 LLVMBuildFPExt(builder
, val
, bld_base
->base
.elem_type
, "");
1638 static void emit_fdiv(const struct lp_build_tgsi_action
*action
,
1639 struct lp_build_tgsi_context
*bld_base
,
1640 struct lp_build_emit_data
*emit_data
)
1642 struct radeon_llvm_context
*ctx
= radeon_llvm_context(bld_base
);
1644 emit_data
->output
[emit_data
->chan
] =
1645 LLVMBuildFDiv(bld_base
->base
.gallivm
->builder
,
1646 emit_data
->args
[0], emit_data
->args
[1], "");
1648 /* Use v_rcp_f32 instead of precise division. */
1649 if (HAVE_LLVM
>= 0x0309 &&
1650 !LLVMIsConstant(emit_data
->output
[emit_data
->chan
]))
1651 LLVMSetMetadata(emit_data
->output
[emit_data
->chan
],
1652 ctx
->fpmath_md_kind
, ctx
->fpmath_md_2p5_ulp
);
1655 /* 1/sqrt is translated to rsq for f32 if fp32 denormals are not enabled in
1656 * the target machine. f64 needs global unsafe math flags to get rsq. */
1657 static void emit_rsq(const struct lp_build_tgsi_action
*action
,
1658 struct lp_build_tgsi_context
*bld_base
,
1659 struct lp_build_emit_data
*emit_data
)
1662 lp_build_emit_llvm_unary(bld_base
, TGSI_OPCODE_SQRT
,
1663 emit_data
->args
[0]);
1665 emit_data
->output
[emit_data
->chan
] =
1666 lp_build_emit_llvm_binary(bld_base
, TGSI_OPCODE_DIV
,
1667 bld_base
->base
.one
, sqrt
);
1670 void radeon_llvm_context_init(struct radeon_llvm_context
*ctx
, const char *triple
)
1672 struct lp_type type
;
1674 /* Initialize the gallivm object:
1675 * We are only using the module, context, and builder fields of this struct.
1676 * This should be enough for us to be able to pass our gallivm struct to the
1677 * helper functions in the gallivm module.
1679 memset(&ctx
->gallivm
, 0, sizeof (ctx
->gallivm
));
1680 memset(&ctx
->soa
, 0, sizeof(ctx
->soa
));
1681 ctx
->gallivm
.context
= LLVMContextCreate();
1682 ctx
->gallivm
.module
= LLVMModuleCreateWithNameInContext("tgsi",
1683 ctx
->gallivm
.context
);
1684 LLVMSetTarget(ctx
->gallivm
.module
, triple
);
1685 ctx
->gallivm
.builder
= LLVMCreateBuilderInContext(ctx
->gallivm
.context
);
1687 struct lp_build_tgsi_context
*bld_base
= &ctx
->soa
.bld_base
;
1689 type
.floating
= true;
1696 lp_build_context_init(&bld_base
->base
, &ctx
->gallivm
, type
);
1697 lp_build_context_init(&ctx
->soa
.bld_base
.uint_bld
, &ctx
->gallivm
, lp_uint_type(type
));
1698 lp_build_context_init(&ctx
->soa
.bld_base
.int_bld
, &ctx
->gallivm
, lp_int_type(type
));
1700 struct lp_type dbl_type
;
1702 dbl_type
.width
*= 2;
1703 lp_build_context_init(&ctx
->soa
.bld_base
.dbl_bld
, &ctx
->gallivm
, dbl_type
);
1707 bld_base
->emit_store
= radeon_llvm_emit_store
;
1708 bld_base
->emit_swizzle
= emit_swizzle
;
1709 bld_base
->emit_declaration
= emit_declaration
;
1710 bld_base
->emit_immediate
= emit_immediate
;
1712 bld_base
->emit_fetch_funcs
[TGSI_FILE_IMMEDIATE
] = radeon_llvm_emit_fetch
;
1713 bld_base
->emit_fetch_funcs
[TGSI_FILE_INPUT
] = radeon_llvm_emit_fetch
;
1714 bld_base
->emit_fetch_funcs
[TGSI_FILE_TEMPORARY
] = radeon_llvm_emit_fetch
;
1715 bld_base
->emit_fetch_funcs
[TGSI_FILE_OUTPUT
] = radeon_llvm_emit_fetch
;
1716 bld_base
->emit_fetch_funcs
[TGSI_FILE_SYSTEM_VALUE
] = fetch_system_value
;
1718 /* metadata allowing 2.5 ULP */
1719 ctx
->fpmath_md_kind
= LLVMGetMDKindIDInContext(ctx
->gallivm
.context
,
1721 LLVMValueRef arg
= lp_build_const_float(&ctx
->gallivm
, 2.5);
1722 ctx
->fpmath_md_2p5_ulp
= LLVMMDNodeInContext(ctx
->gallivm
.context
,
1725 /* Allocate outputs */
1726 ctx
->soa
.outputs
= ctx
->outputs
;
1728 lp_set_default_actions(bld_base
);
1730 bld_base
->op_actions
[TGSI_OPCODE_ABS
].emit
= build_tgsi_intrinsic_nomem
;
1731 bld_base
->op_actions
[TGSI_OPCODE_ABS
].intr_name
= "llvm.fabs.f32";
1732 bld_base
->op_actions
[TGSI_OPCODE_AND
].emit
= emit_and
;
1733 bld_base
->op_actions
[TGSI_OPCODE_ARL
].emit
= emit_arl
;
1734 bld_base
->op_actions
[TGSI_OPCODE_BFI
].emit
= emit_bfi
;
1735 bld_base
->op_actions
[TGSI_OPCODE_BGNLOOP
].emit
= bgnloop_emit
;
1736 bld_base
->op_actions
[TGSI_OPCODE_BREV
].emit
= build_tgsi_intrinsic_nomem
;
1737 bld_base
->op_actions
[TGSI_OPCODE_BREV
].intr_name
=
1738 HAVE_LLVM
>= 0x0308 ? "llvm.bitreverse.i32" : "llvm.AMDGPU.brev";
1739 bld_base
->op_actions
[TGSI_OPCODE_BRK
].emit
= brk_emit
;
1740 bld_base
->op_actions
[TGSI_OPCODE_CEIL
].emit
= build_tgsi_intrinsic_nomem
;
1741 bld_base
->op_actions
[TGSI_OPCODE_CEIL
].intr_name
= "llvm.ceil.f32";
1742 bld_base
->op_actions
[TGSI_OPCODE_CLAMP
].emit
= build_tgsi_intrinsic_nomem
;
1743 bld_base
->op_actions
[TGSI_OPCODE_CLAMP
].intr_name
=
1744 HAVE_LLVM
>= 0x0308 ? "llvm.AMDGPU.clamp." : "llvm.AMDIL.clamp.";
1745 bld_base
->op_actions
[TGSI_OPCODE_CMP
].emit
= emit_cmp
;
1746 bld_base
->op_actions
[TGSI_OPCODE_CONT
].emit
= cont_emit
;
1747 bld_base
->op_actions
[TGSI_OPCODE_COS
].emit
= build_tgsi_intrinsic_nomem
;
1748 bld_base
->op_actions
[TGSI_OPCODE_COS
].intr_name
= "llvm.cos.f32";
1749 bld_base
->op_actions
[TGSI_OPCODE_DABS
].emit
= build_tgsi_intrinsic_nomem
;
1750 bld_base
->op_actions
[TGSI_OPCODE_DABS
].intr_name
= "llvm.fabs.f64";
1751 bld_base
->op_actions
[TGSI_OPCODE_DFMA
].emit
= build_tgsi_intrinsic_nomem
;
1752 bld_base
->op_actions
[TGSI_OPCODE_DFMA
].intr_name
= "llvm.fma.f64";
1753 bld_base
->op_actions
[TGSI_OPCODE_DFRAC
].emit
= emit_frac
;
1754 bld_base
->op_actions
[TGSI_OPCODE_DIV
].emit
= emit_fdiv
;
1755 bld_base
->op_actions
[TGSI_OPCODE_DNEG
].emit
= emit_dneg
;
1756 bld_base
->op_actions
[TGSI_OPCODE_DSEQ
].emit
= emit_dcmp
;
1757 bld_base
->op_actions
[TGSI_OPCODE_DSGE
].emit
= emit_dcmp
;
1758 bld_base
->op_actions
[TGSI_OPCODE_DSLT
].emit
= emit_dcmp
;
1759 bld_base
->op_actions
[TGSI_OPCODE_DSNE
].emit
= emit_dcmp
;
1760 bld_base
->op_actions
[TGSI_OPCODE_DRSQ
].emit
= build_tgsi_intrinsic_nomem
;
1761 bld_base
->op_actions
[TGSI_OPCODE_DRSQ
].intr_name
=
1762 HAVE_LLVM
>= 0x0309 ? "llvm.amdgcn.rsq.f64" : "llvm.AMDGPU.rsq.f64";
1763 bld_base
->op_actions
[TGSI_OPCODE_DSQRT
].emit
= build_tgsi_intrinsic_nomem
;
1764 bld_base
->op_actions
[TGSI_OPCODE_DSQRT
].intr_name
= "llvm.sqrt.f64";
1765 bld_base
->op_actions
[TGSI_OPCODE_ELSE
].emit
= else_emit
;
1766 bld_base
->op_actions
[TGSI_OPCODE_ENDIF
].emit
= endif_emit
;
1767 bld_base
->op_actions
[TGSI_OPCODE_ENDLOOP
].emit
= endloop_emit
;
1768 bld_base
->op_actions
[TGSI_OPCODE_EX2
].emit
= build_tgsi_intrinsic_nomem
;
1769 bld_base
->op_actions
[TGSI_OPCODE_EX2
].intr_name
=
1770 HAVE_LLVM
>= 0x0308 ? "llvm.exp2.f32" : "llvm.AMDIL.exp.";
1771 bld_base
->op_actions
[TGSI_OPCODE_FLR
].emit
= build_tgsi_intrinsic_nomem
;
1772 bld_base
->op_actions
[TGSI_OPCODE_FLR
].intr_name
= "llvm.floor.f32";
1773 bld_base
->op_actions
[TGSI_OPCODE_FMA
].emit
=
1774 bld_base
->op_actions
[TGSI_OPCODE_MAD
].emit
;
1775 bld_base
->op_actions
[TGSI_OPCODE_FRC
].emit
= emit_frac
;
1776 bld_base
->op_actions
[TGSI_OPCODE_F2I
].emit
= emit_f2i
;
1777 bld_base
->op_actions
[TGSI_OPCODE_F2U
].emit
= emit_f2u
;
1778 bld_base
->op_actions
[TGSI_OPCODE_FSEQ
].emit
= emit_fcmp
;
1779 bld_base
->op_actions
[TGSI_OPCODE_FSGE
].emit
= emit_fcmp
;
1780 bld_base
->op_actions
[TGSI_OPCODE_FSLT
].emit
= emit_fcmp
;
1781 bld_base
->op_actions
[TGSI_OPCODE_FSNE
].emit
= emit_fcmp
;
1782 bld_base
->op_actions
[TGSI_OPCODE_IABS
].emit
= emit_iabs
;
1783 bld_base
->op_actions
[TGSI_OPCODE_IBFE
].emit
= build_tgsi_intrinsic_nomem
;
1784 bld_base
->op_actions
[TGSI_OPCODE_IBFE
].intr_name
= "llvm.AMDGPU.bfe.i32";
1785 bld_base
->op_actions
[TGSI_OPCODE_IDIV
].emit
= emit_idiv
;
1786 bld_base
->op_actions
[TGSI_OPCODE_IF
].emit
= if_emit
;
1787 bld_base
->op_actions
[TGSI_OPCODE_UIF
].emit
= uif_emit
;
1788 bld_base
->op_actions
[TGSI_OPCODE_IMAX
].emit
= emit_minmax_int
;
1789 bld_base
->op_actions
[TGSI_OPCODE_IMIN
].emit
= emit_minmax_int
;
1790 bld_base
->op_actions
[TGSI_OPCODE_IMSB
].emit
= emit_imsb
;
1791 bld_base
->op_actions
[TGSI_OPCODE_INEG
].emit
= emit_ineg
;
1792 bld_base
->op_actions
[TGSI_OPCODE_ISHR
].emit
= emit_ishr
;
1793 bld_base
->op_actions
[TGSI_OPCODE_ISGE
].emit
= emit_icmp
;
1794 bld_base
->op_actions
[TGSI_OPCODE_ISLT
].emit
= emit_icmp
;
1795 bld_base
->op_actions
[TGSI_OPCODE_ISSG
].emit
= emit_ssg
;
1796 bld_base
->op_actions
[TGSI_OPCODE_I2F
].emit
= emit_i2f
;
1797 bld_base
->op_actions
[TGSI_OPCODE_KILL_IF
].fetch_args
= kill_if_fetch_args
;
1798 bld_base
->op_actions
[TGSI_OPCODE_KILL_IF
].emit
= kil_emit
;
1799 bld_base
->op_actions
[TGSI_OPCODE_KILL_IF
].intr_name
= "llvm.AMDGPU.kill";
1800 bld_base
->op_actions
[TGSI_OPCODE_KILL
].emit
= lp_build_tgsi_intrinsic
;
1801 bld_base
->op_actions
[TGSI_OPCODE_KILL
].intr_name
= "llvm.AMDGPU.kilp";
1802 bld_base
->op_actions
[TGSI_OPCODE_LSB
].emit
= emit_lsb
;
1803 bld_base
->op_actions
[TGSI_OPCODE_LG2
].emit
= build_tgsi_intrinsic_nomem
;
1804 bld_base
->op_actions
[TGSI_OPCODE_LG2
].intr_name
= "llvm.log2.f32";
1805 bld_base
->op_actions
[TGSI_OPCODE_MOD
].emit
= emit_mod
;
1806 bld_base
->op_actions
[TGSI_OPCODE_UMSB
].emit
= emit_umsb
;
1807 bld_base
->op_actions
[TGSI_OPCODE_NOT
].emit
= emit_not
;
1808 bld_base
->op_actions
[TGSI_OPCODE_OR
].emit
= emit_or
;
1809 bld_base
->op_actions
[TGSI_OPCODE_PK2H
].fetch_args
= pk2h_fetch_args
;
1810 bld_base
->op_actions
[TGSI_OPCODE_PK2H
].emit
= emit_pk2h
;
1811 bld_base
->op_actions
[TGSI_OPCODE_POPC
].emit
= build_tgsi_intrinsic_nomem
;
1812 bld_base
->op_actions
[TGSI_OPCODE_POPC
].intr_name
= "llvm.ctpop.i32";
1813 bld_base
->op_actions
[TGSI_OPCODE_POW
].emit
= build_tgsi_intrinsic_nomem
;
1814 bld_base
->op_actions
[TGSI_OPCODE_POW
].intr_name
= "llvm.pow.f32";
1815 bld_base
->op_actions
[TGSI_OPCODE_ROUND
].emit
= build_tgsi_intrinsic_nomem
;
1816 bld_base
->op_actions
[TGSI_OPCODE_ROUND
].intr_name
= "llvm.rint.f32";
1817 bld_base
->op_actions
[TGSI_OPCODE_RSQ
].emit
= emit_rsq
;
1818 bld_base
->op_actions
[TGSI_OPCODE_SGE
].emit
= emit_set_cond
;
1819 bld_base
->op_actions
[TGSI_OPCODE_SEQ
].emit
= emit_set_cond
;
1820 bld_base
->op_actions
[TGSI_OPCODE_SHL
].emit
= emit_shl
;
1821 bld_base
->op_actions
[TGSI_OPCODE_SLE
].emit
= emit_set_cond
;
1822 bld_base
->op_actions
[TGSI_OPCODE_SLT
].emit
= emit_set_cond
;
1823 bld_base
->op_actions
[TGSI_OPCODE_SNE
].emit
= emit_set_cond
;
1824 bld_base
->op_actions
[TGSI_OPCODE_SGT
].emit
= emit_set_cond
;
1825 bld_base
->op_actions
[TGSI_OPCODE_SIN
].emit
= build_tgsi_intrinsic_nomem
;
1826 bld_base
->op_actions
[TGSI_OPCODE_SIN
].intr_name
= "llvm.sin.f32";
1827 bld_base
->op_actions
[TGSI_OPCODE_SQRT
].emit
= build_tgsi_intrinsic_nomem
;
1828 bld_base
->op_actions
[TGSI_OPCODE_SQRT
].intr_name
= "llvm.sqrt.f32";
1829 bld_base
->op_actions
[TGSI_OPCODE_SSG
].emit
= emit_ssg
;
1830 bld_base
->op_actions
[TGSI_OPCODE_TRUNC
].emit
= build_tgsi_intrinsic_nomem
;
1831 bld_base
->op_actions
[TGSI_OPCODE_TRUNC
].intr_name
= "llvm.trunc.f32";
1832 bld_base
->op_actions
[TGSI_OPCODE_UADD
].emit
= emit_uadd
;
1833 bld_base
->op_actions
[TGSI_OPCODE_UBFE
].emit
= build_tgsi_intrinsic_nomem
;
1834 bld_base
->op_actions
[TGSI_OPCODE_UBFE
].intr_name
= "llvm.AMDGPU.bfe.u32";
1835 bld_base
->op_actions
[TGSI_OPCODE_UDIV
].emit
= emit_udiv
;
1836 bld_base
->op_actions
[TGSI_OPCODE_UMAX
].emit
= emit_minmax_int
;
1837 bld_base
->op_actions
[TGSI_OPCODE_UMIN
].emit
= emit_minmax_int
;
1838 bld_base
->op_actions
[TGSI_OPCODE_UMOD
].emit
= emit_umod
;
1839 bld_base
->op_actions
[TGSI_OPCODE_USEQ
].emit
= emit_icmp
;
1840 bld_base
->op_actions
[TGSI_OPCODE_USGE
].emit
= emit_icmp
;
1841 bld_base
->op_actions
[TGSI_OPCODE_USHR
].emit
= emit_ushr
;
1842 bld_base
->op_actions
[TGSI_OPCODE_USLT
].emit
= emit_icmp
;
1843 bld_base
->op_actions
[TGSI_OPCODE_USNE
].emit
= emit_icmp
;
1844 bld_base
->op_actions
[TGSI_OPCODE_U2F
].emit
= emit_u2f
;
1845 bld_base
->op_actions
[TGSI_OPCODE_XOR
].emit
= emit_xor
;
1846 bld_base
->op_actions
[TGSI_OPCODE_UCMP
].emit
= emit_ucmp
;
1847 bld_base
->op_actions
[TGSI_OPCODE_UP2H
].fetch_args
= up2h_fetch_args
;
1848 bld_base
->op_actions
[TGSI_OPCODE_UP2H
].emit
= emit_up2h
;
1851 void radeon_llvm_create_func(struct radeon_llvm_context
*ctx
,
1852 LLVMTypeRef
*return_types
, unsigned num_return_elems
,
1853 LLVMTypeRef
*ParamTypes
, unsigned ParamCount
)
1855 LLVMTypeRef main_fn_type
, ret_type
;
1856 LLVMBasicBlockRef main_fn_body
;
1858 if (num_return_elems
)
1859 ret_type
= LLVMStructTypeInContext(ctx
->gallivm
.context
,
1861 num_return_elems
, true);
1863 ret_type
= LLVMVoidTypeInContext(ctx
->gallivm
.context
);
1865 /* Setup the function */
1866 ctx
->return_type
= ret_type
;
1867 main_fn_type
= LLVMFunctionType(ret_type
, ParamTypes
, ParamCount
, 0);
1868 ctx
->main_fn
= LLVMAddFunction(ctx
->gallivm
.module
, "main", main_fn_type
);
1869 main_fn_body
= LLVMAppendBasicBlockInContext(ctx
->gallivm
.context
,
1870 ctx
->main_fn
, "main_body");
1871 LLVMPositionBuilderAtEnd(ctx
->gallivm
.builder
, main_fn_body
);
1874 void radeon_llvm_finalize_module(struct radeon_llvm_context
*ctx
)
1876 struct gallivm_state
*gallivm
= ctx
->soa
.bld_base
.base
.gallivm
;
1877 const char *triple
= LLVMGetTarget(gallivm
->module
);
1878 LLVMTargetLibraryInfoRef target_library_info
;
1880 /* Create the pass manager */
1881 gallivm
->passmgr
= LLVMCreateFunctionPassManagerForModule(
1884 target_library_info
= gallivm_create_target_library_info(triple
);
1885 LLVMAddTargetLibraryInfo(target_library_info
, gallivm
->passmgr
);
1887 /* This pass should eliminate all the load and store instructions */
1888 LLVMAddPromoteMemoryToRegisterPass(gallivm
->passmgr
);
1890 /* Add some optimization passes */
1891 LLVMAddScalarReplAggregatesPass(gallivm
->passmgr
);
1892 LLVMAddLICMPass(gallivm
->passmgr
);
1893 LLVMAddAggressiveDCEPass(gallivm
->passmgr
);
1894 LLVMAddCFGSimplificationPass(gallivm
->passmgr
);
1895 LLVMAddInstructionCombiningPass(gallivm
->passmgr
);
1898 LLVMRunFunctionPassManager(gallivm
->passmgr
, ctx
->main_fn
);
1900 LLVMDisposeBuilder(gallivm
->builder
);
1901 LLVMDisposePassManager(gallivm
->passmgr
);
1902 gallivm_dispose_target_library_info(target_library_info
);
1905 void radeon_llvm_dispose(struct radeon_llvm_context
*ctx
)
1907 LLVMDisposeModule(ctx
->soa
.bld_base
.base
.gallivm
->module
);
1908 LLVMContextDispose(ctx
->soa
.bld_base
.base
.gallivm
->context
);
1913 ctx
->temps_count
= 0;
1916 ctx
->loop_depth_max
= 0;
1919 ctx
->branch_depth_max
= 0;