1 /**************************************************************************
3 * Copyright 2009 VMware, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
31 * Unit tests for type conversion.
33 * @author Jose Fonseca <jfonseca@vmware.com>
37 #include "util/u_pointer.h"
38 #include "gallivm/lp_bld_init.h"
39 #include "gallivm/lp_bld_type.h"
40 #include "gallivm/lp_bld_const.h"
41 #include "gallivm/lp_bld_conv.h"
42 #include "gallivm/lp_bld_debug.h"
46 typedef void (*conv_test_ptr_t
)(const void *src
, const void *dst
);
50 write_tsv_header(FILE *fp
)
54 "cycles_per_channel\t"
63 write_tsv_row(FILE *fp
,
64 struct lp_type src_type
,
65 struct lp_type dst_type
,
69 fprintf(fp
, "%s\t", success
? "pass" : "fail");
71 fprintf(fp
, "%.1f\t", cycles
/ MAX2(src_type
.length
, dst_type
.length
));
73 dump_type(fp
, src_type
);
76 dump_type(fp
, dst_type
);
84 dump_conv_types(FILE *fp
,
85 struct lp_type src_type
,
86 struct lp_type dst_type
)
88 fprintf(fp
, "src_type=");
89 dump_type(fp
, src_type
);
91 fprintf(fp
, " dst_type=");
92 dump_type(fp
, dst_type
);
94 fprintf(fp
, " ...\n");
100 add_conv_test(LLVMModuleRef module
,
101 struct lp_type src_type
, unsigned num_srcs
,
102 struct lp_type dst_type
, unsigned num_dsts
)
106 LLVMValueRef src_ptr
;
107 LLVMValueRef dst_ptr
;
108 LLVMBasicBlockRef block
;
109 LLVMBuilderRef builder
;
110 LLVMValueRef src
[LP_MAX_VECTOR_LENGTH
];
111 LLVMValueRef dst
[LP_MAX_VECTOR_LENGTH
];
114 args
[0] = LLVMPointerType(lp_build_vec_type(src_type
), 0);
115 args
[1] = LLVMPointerType(lp_build_vec_type(dst_type
), 0);
117 func
= LLVMAddFunction(module
, "test", LLVMFunctionType(LLVMVoidType(), args
, 2, 0));
118 LLVMSetFunctionCallConv(func
, LLVMCCallConv
);
119 src_ptr
= LLVMGetParam(func
, 0);
120 dst_ptr
= LLVMGetParam(func
, 1);
122 block
= LLVMAppendBasicBlock(func
, "entry");
123 builder
= LLVMCreateBuilder();
124 LLVMPositionBuilderAtEnd(builder
, block
);
126 for(i
= 0; i
< num_srcs
; ++i
) {
127 LLVMValueRef index
= LLVMConstInt(LLVMInt32Type(), i
, 0);
128 LLVMValueRef ptr
= LLVMBuildGEP(builder
, src_ptr
, &index
, 1, "");
129 src
[i
] = LLVMBuildLoad(builder
, ptr
, "");
132 lp_build_conv(builder
, src_type
, dst_type
, src
, num_srcs
, dst
, num_dsts
);
134 for(i
= 0; i
< num_dsts
; ++i
) {
135 LLVMValueRef index
= LLVMConstInt(LLVMInt32Type(), i
, 0);
136 LLVMValueRef ptr
= LLVMBuildGEP(builder
, dst_ptr
, &index
, 1, "");
137 LLVMBuildStore(builder
, dst
[i
], ptr
);
140 LLVMBuildRetVoid(builder
);;
142 LLVMDisposeBuilder(builder
);
149 test_one(unsigned verbose
,
151 struct lp_type src_type
,
152 struct lp_type dst_type
)
154 LLVMModuleRef module
= NULL
;
155 LLVMValueRef func
= NULL
;
156 LLVMExecutionEngineRef engine
= lp_build_engine
;
157 LLVMPassManagerRef pass
= NULL
;
159 conv_test_ptr_t conv_test_ptr
;
161 const unsigned n
= LP_TEST_NUM_SAMPLES
;
162 int64_t cycles
[LP_TEST_NUM_SAMPLES
];
163 double cycles_avg
= 0.0;
170 if (src_type
.width
* src_type
.length
!= dst_type
.width
* dst_type
.length
&&
171 src_type
.length
!= dst_type
.length
) {
176 dump_conv_types(stdout
, src_type
, dst_type
);
178 if (src_type
.length
> dst_type
.length
) {
180 num_dsts
= src_type
.length
/dst_type
.length
;
182 else if (src_type
.length
< dst_type
.length
) {
184 num_srcs
= dst_type
.length
/src_type
.length
;
191 /* We must not loose or gain channels. Only precision */
192 assert(src_type
.length
* num_srcs
== dst_type
.length
* num_dsts
);
194 eps
= MAX2(lp_const_eps(src_type
), lp_const_eps(dst_type
));
196 module
= LLVMModuleCreateWithName("test");
198 func
= add_conv_test(module
, src_type
, num_srcs
, dst_type
, num_dsts
);
200 if(LLVMVerifyModule(module
, LLVMPrintMessageAction
, &error
)) {
201 LLVMDumpModule(module
);
204 LLVMDisposeMessage(error
);
207 pass
= LLVMCreatePassManager();
208 LLVMAddTargetData(LLVMGetExecutionEngineTargetData(engine
), pass
);
209 /* These are the passes currently listed in llvm-c/Transforms/Scalar.h,
210 * but there are more on SVN. */
211 LLVMAddConstantPropagationPass(pass
);
212 LLVMAddInstructionCombiningPass(pass
);
213 LLVMAddPromoteMemoryToRegisterPass(pass
);
214 LLVMAddGVNPass(pass
);
215 LLVMAddCFGSimplificationPass(pass
);
216 LLVMRunPassManager(pass
, module
);
222 LLVMDumpModule(module
);
224 code
= LLVMGetPointerToGlobal(engine
, func
);
225 conv_test_ptr
= (conv_test_ptr_t
)pointer_to_func(code
);
228 lp_disassemble(code
);
231 for(i
= 0; i
< n
&& success
; ++i
) {
232 unsigned src_stride
= src_type
.length
*src_type
.width
/8;
233 unsigned dst_stride
= dst_type
.length
*dst_type
.width
/8;
234 PIPE_ALIGN_VAR(16) uint8_t src
[LP_MAX_VECTOR_LENGTH
*LP_MAX_VECTOR_LENGTH
];
235 PIPE_ALIGN_VAR(16) uint8_t dst
[LP_MAX_VECTOR_LENGTH
*LP_MAX_VECTOR_LENGTH
];
236 double fref
[LP_MAX_VECTOR_LENGTH
*LP_MAX_VECTOR_LENGTH
];
237 uint8_t ref
[LP_MAX_VECTOR_LENGTH
*LP_MAX_VECTOR_LENGTH
];
238 int64_t start_counter
= 0;
239 int64_t end_counter
= 0;
241 for(j
= 0; j
< num_srcs
; ++j
) {
242 random_vec(src_type
, src
+ j
*src_stride
);
243 read_vec(src_type
, src
+ j
*src_stride
, fref
+ j
*src_type
.length
);
246 for(j
= 0; j
< num_dsts
; ++j
) {
247 write_vec(dst_type
, ref
+ j
*dst_stride
, fref
+ j
*dst_type
.length
);
250 start_counter
= rdtsc();
251 conv_test_ptr(src
, dst
);
252 end_counter
= rdtsc();
254 cycles
[i
] = end_counter
- start_counter
;
256 for(j
= 0; j
< num_dsts
; ++j
) {
257 if(!compare_vec_with_eps(dst_type
, dst
+ j
*dst_stride
, ref
+ j
*dst_stride
, eps
))
261 if (!success
|| verbose
>= 3) {
263 dump_conv_types(stderr
, src_type
, dst_type
);
265 fprintf(stderr
, "PASS\n");
268 fprintf(stderr
, "MISMATCH\n");
271 for(j
= 0; j
< num_srcs
; ++j
) {
272 fprintf(stderr
, " Src%u: ", j
);
273 dump_vec(stderr
, src_type
, src
+ j
*src_stride
);
274 fprintf(stderr
, "\n");
278 fprintf(stderr
, " Ref: ");
279 for(j
= 0; j
< src_type
.length
*num_srcs
; ++j
)
280 fprintf(stderr
, " %f", fref
[j
]);
281 fprintf(stderr
, "\n");
284 for(j
= 0; j
< num_dsts
; ++j
) {
285 fprintf(stderr
, " Dst%u: ", j
);
286 dump_vec(stderr
, dst_type
, dst
+ j
*dst_stride
);
287 fprintf(stderr
, "\n");
289 fprintf(stderr
, " Ref%u: ", j
);
290 dump_vec(stderr
, dst_type
, ref
+ j
*dst_stride
);
291 fprintf(stderr
, "\n");
297 * Unfortunately the output of cycle counter is not very reliable as it comes
298 * -- sometimes we get outliers (due IRQs perhaps?) which are
299 * better removed to avoid random or biased data.
302 double sum
= 0.0, sum2
= 0.0;
306 for(i
= 0; i
< n
; ++i
) {
308 sum2
+= cycles
[i
]*cycles
[i
];
312 std
= sqrtf((sum2
- n
*avg
*avg
)/n
);
316 for(i
= 0; i
< n
; ++i
) {
317 if(fabs(cycles
[i
] - avg
) <= 4.0*std
) {
328 write_tsv_row(fp
, src_type
, dst_type
, cycles_avg
, success
);
331 static boolean firsttime
= TRUE
;
334 LLVMDumpModule(module
);
335 LLVMWriteBitcodeToFile(module
, "conv.bc");
336 fprintf(stderr
, "conv.bc written\n");
337 fprintf(stderr
, "Invoke as \"llc -o - conv.bc\"\n");
343 LLVMFreeMachineCodeForFunction(engine
, func
);
346 LLVMDisposePassManager(pass
);
352 const struct lp_type conv_types
[] = {
353 /* float, fixed, sign, norm, width, len */
355 { TRUE
, FALSE
, TRUE
, TRUE
, 32, 4 },
356 { TRUE
, FALSE
, TRUE
, FALSE
, 32, 4 },
357 { TRUE
, FALSE
, FALSE
, TRUE
, 32, 4 },
358 { TRUE
, FALSE
, FALSE
, FALSE
, 32, 4 },
360 /* TODO: test fixed formats too */
362 { FALSE
, FALSE
, TRUE
, TRUE
, 16, 8 },
363 { FALSE
, FALSE
, TRUE
, FALSE
, 16, 8 },
364 { FALSE
, FALSE
, FALSE
, TRUE
, 16, 8 },
365 { FALSE
, FALSE
, FALSE
, FALSE
, 16, 8 },
367 { FALSE
, FALSE
, TRUE
, TRUE
, 32, 4 },
368 { FALSE
, FALSE
, TRUE
, FALSE
, 32, 4 },
369 { FALSE
, FALSE
, FALSE
, TRUE
, 32, 4 },
370 { FALSE
, FALSE
, FALSE
, FALSE
, 32, 4 },
372 { FALSE
, FALSE
, TRUE
, TRUE
, 16, 8 },
373 { FALSE
, FALSE
, TRUE
, FALSE
, 16, 8 },
374 { FALSE
, FALSE
, FALSE
, TRUE
, 16, 8 },
375 { FALSE
, FALSE
, FALSE
, FALSE
, 16, 8 },
377 { FALSE
, FALSE
, TRUE
, TRUE
, 8, 16 },
378 { FALSE
, FALSE
, TRUE
, FALSE
, 8, 16 },
379 { FALSE
, FALSE
, FALSE
, TRUE
, 8, 16 },
380 { FALSE
, FALSE
, FALSE
, FALSE
, 8, 16 },
382 { FALSE
, FALSE
, TRUE
, TRUE
, 8, 4 },
383 { FALSE
, FALSE
, TRUE
, FALSE
, 8, 4 },
384 { FALSE
, FALSE
, FALSE
, TRUE
, 8, 4 },
385 { FALSE
, FALSE
, FALSE
, FALSE
, 8, 4 },
389 const unsigned num_types
= sizeof(conv_types
)/sizeof(conv_types
[0]);
393 test_all(unsigned verbose
, FILE *fp
)
395 const struct lp_type
*src_type
;
396 const struct lp_type
*dst_type
;
397 boolean success
= TRUE
;
399 for(src_type
= conv_types
; src_type
< &conv_types
[num_types
]; ++src_type
) {
400 for(dst_type
= conv_types
; dst_type
< &conv_types
[num_types
]; ++dst_type
) {
402 if(src_type
== dst_type
)
405 if(src_type
->norm
!= dst_type
->norm
)
408 if(!test_one(verbose
, fp
, *src_type
, *dst_type
))
419 test_some(unsigned verbose
, FILE *fp
, unsigned long n
)
421 const struct lp_type
*src_type
;
422 const struct lp_type
*dst_type
;
424 boolean success
= TRUE
;
426 for(i
= 0; i
< n
; ++i
) {
427 src_type
= &conv_types
[rand() % num_types
];
430 dst_type
= &conv_types
[rand() % num_types
];
431 } while (src_type
== dst_type
|| src_type
->norm
!= dst_type
->norm
);
433 if(!test_one(verbose
, fp
, *src_type
, *dst_type
))
442 test_single(unsigned verbose
, FILE *fp
)
444 /* float, fixed, sign, norm, width, len */
445 struct lp_type f32x4_type
=
446 { TRUE
, FALSE
, TRUE
, TRUE
, 32, 4 };
447 struct lp_type ub8x4_type
=
448 { FALSE
, FALSE
, FALSE
, TRUE
, 8, 16 };
452 success
= test_one(verbose
, fp
, f32x4_type
, ub8x4_type
);