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_type.h"
39 #include "gallivm/lp_bld_const.h"
40 #include "gallivm/lp_bld_conv.h"
41 #include "gallivm/lp_bld_debug.h"
45 typedef void (*conv_test_ptr_t
)(const void *src
, const void *dst
);
49 write_tsv_header(FILE *fp
)
53 "cycles_per_channel\t"
62 write_tsv_row(FILE *fp
,
63 struct lp_type src_type
,
64 struct lp_type dst_type
,
68 fprintf(fp
, "%s\t", success
? "pass" : "fail");
70 fprintf(fp
, "%.1f\t", cycles
/ MAX2(src_type
.length
, dst_type
.length
));
72 dump_type(fp
, src_type
);
75 dump_type(fp
, dst_type
);
83 dump_conv_types(FILE *fp
,
84 struct lp_type src_type
,
85 struct lp_type dst_type
)
87 fprintf(fp
, "src_type=");
88 dump_type(fp
, src_type
);
90 fprintf(fp
, " dst_type=");
91 dump_type(fp
, dst_type
);
93 fprintf(fp
, " ...\n");
99 add_conv_test(LLVMModuleRef module
,
100 struct lp_type src_type
, unsigned num_srcs
,
101 struct lp_type dst_type
, unsigned num_dsts
)
105 LLVMValueRef src_ptr
;
106 LLVMValueRef dst_ptr
;
107 LLVMBasicBlockRef block
;
108 LLVMBuilderRef builder
;
109 LLVMValueRef src
[LP_MAX_VECTOR_LENGTH
];
110 LLVMValueRef dst
[LP_MAX_VECTOR_LENGTH
];
113 args
[0] = LLVMPointerType(lp_build_vec_type(src_type
), 0);
114 args
[1] = LLVMPointerType(lp_build_vec_type(dst_type
), 0);
116 func
= LLVMAddFunction(module
, "test", LLVMFunctionType(LLVMVoidType(), args
, 2, 0));
117 LLVMSetFunctionCallConv(func
, LLVMCCallConv
);
118 src_ptr
= LLVMGetParam(func
, 0);
119 dst_ptr
= LLVMGetParam(func
, 1);
121 block
= LLVMAppendBasicBlock(func
, "entry");
122 builder
= LLVMCreateBuilder();
123 LLVMPositionBuilderAtEnd(builder
, block
);
125 for(i
= 0; i
< num_srcs
; ++i
) {
126 LLVMValueRef index
= LLVMConstInt(LLVMInt32Type(), i
, 0);
127 LLVMValueRef ptr
= LLVMBuildGEP(builder
, src_ptr
, &index
, 1, "");
128 src
[i
] = LLVMBuildLoad(builder
, ptr
, "");
131 lp_build_conv(builder
, src_type
, dst_type
, src
, num_srcs
, dst
, num_dsts
);
133 for(i
= 0; i
< num_dsts
; ++i
) {
134 LLVMValueRef index
= LLVMConstInt(LLVMInt32Type(), i
, 0);
135 LLVMValueRef ptr
= LLVMBuildGEP(builder
, dst_ptr
, &index
, 1, "");
136 LLVMBuildStore(builder
, dst
[i
], ptr
);
139 LLVMBuildRetVoid(builder
);;
141 LLVMDisposeBuilder(builder
);
148 test_one(unsigned verbose
,
150 struct lp_type src_type
,
151 struct lp_type dst_type
)
153 LLVMModuleRef module
= NULL
;
154 LLVMValueRef func
= NULL
;
155 LLVMExecutionEngineRef engine
= NULL
;
156 LLVMModuleProviderRef provider
= NULL
;
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
);
206 provider
= LLVMCreateModuleProviderForExistingModule(module
);
207 if (LLVMCreateJITCompiler(&engine
, provider
, 1, &error
)) {
209 dump_conv_types(stderr
, src_type
, dst_type
);
210 fprintf(stderr
, "%s\n", error
);
211 LLVMDisposeMessage(error
);
216 pass
= LLVMCreatePassManager();
217 LLVMAddTargetData(LLVMGetExecutionEngineTargetData(engine
), pass
);
218 /* These are the passes currently listed in llvm-c/Transforms/Scalar.h,
219 * but there are more on SVN. */
220 LLVMAddConstantPropagationPass(pass
);
221 LLVMAddInstructionCombiningPass(pass
);
222 LLVMAddPromoteMemoryToRegisterPass(pass
);
223 LLVMAddGVNPass(pass
);
224 LLVMAddCFGSimplificationPass(pass
);
225 LLVMRunPassManager(pass
, module
);
231 LLVMDumpModule(module
);
233 code
= LLVMGetPointerToGlobal(engine
, func
);
234 conv_test_ptr
= (conv_test_ptr_t
)pointer_to_func(code
);
237 lp_disassemble(code
);
240 for(i
= 0; i
< n
&& success
; ++i
) {
241 unsigned src_stride
= src_type
.length
*src_type
.width
/8;
242 unsigned dst_stride
= dst_type
.length
*dst_type
.width
/8;
243 PIPE_ALIGN_VAR(16) uint8_t src
[LP_MAX_VECTOR_LENGTH
*LP_MAX_VECTOR_LENGTH
];
244 PIPE_ALIGN_VAR(16) uint8_t dst
[LP_MAX_VECTOR_LENGTH
*LP_MAX_VECTOR_LENGTH
];
245 double fref
[LP_MAX_VECTOR_LENGTH
*LP_MAX_VECTOR_LENGTH
];
246 uint8_t ref
[LP_MAX_VECTOR_LENGTH
*LP_MAX_VECTOR_LENGTH
];
247 int64_t start_counter
= 0;
248 int64_t end_counter
= 0;
250 for(j
= 0; j
< num_srcs
; ++j
) {
251 random_vec(src_type
, src
+ j
*src_stride
);
252 read_vec(src_type
, src
+ j
*src_stride
, fref
+ j
*src_type
.length
);
255 for(j
= 0; j
< num_dsts
; ++j
) {
256 write_vec(dst_type
, ref
+ j
*dst_stride
, fref
+ j
*dst_type
.length
);
259 start_counter
= rdtsc();
260 conv_test_ptr(src
, dst
);
261 end_counter
= rdtsc();
263 cycles
[i
] = end_counter
- start_counter
;
265 for(j
= 0; j
< num_dsts
; ++j
) {
266 if(!compare_vec_with_eps(dst_type
, dst
+ j
*dst_stride
, ref
+ j
*dst_stride
, eps
))
270 if (!success
|| verbose
>= 3) {
272 dump_conv_types(stderr
, src_type
, dst_type
);
274 fprintf(stderr
, "PASS\n");
277 fprintf(stderr
, "MISMATCH\n");
280 for(j
= 0; j
< num_srcs
; ++j
) {
281 fprintf(stderr
, " Src%u: ", j
);
282 dump_vec(stderr
, src_type
, src
+ j
*src_stride
);
283 fprintf(stderr
, "\n");
287 fprintf(stderr
, " Ref: ");
288 for(j
= 0; j
< src_type
.length
*num_srcs
; ++j
)
289 fprintf(stderr
, " %f", fref
[j
]);
290 fprintf(stderr
, "\n");
293 for(j
= 0; j
< num_dsts
; ++j
) {
294 fprintf(stderr
, " Dst%u: ", j
);
295 dump_vec(stderr
, dst_type
, dst
+ j
*dst_stride
);
296 fprintf(stderr
, "\n");
298 fprintf(stderr
, " Ref%u: ", j
);
299 dump_vec(stderr
, dst_type
, ref
+ j
*dst_stride
);
300 fprintf(stderr
, "\n");
306 * Unfortunately the output of cycle counter is not very reliable as it comes
307 * -- sometimes we get outliers (due IRQs perhaps?) which are
308 * better removed to avoid random or biased data.
311 double sum
= 0.0, sum2
= 0.0;
315 for(i
= 0; i
< n
; ++i
) {
317 sum2
+= cycles
[i
]*cycles
[i
];
321 std
= sqrtf((sum2
- n
*avg
*avg
)/n
);
325 for(i
= 0; i
< n
; ++i
) {
326 if(fabs(cycles
[i
] - avg
) <= 4.0*std
) {
337 write_tsv_row(fp
, src_type
, dst_type
, cycles_avg
, success
);
340 static boolean firsttime
= TRUE
;
343 LLVMDumpModule(module
);
344 LLVMWriteBitcodeToFile(module
, "conv.bc");
345 fprintf(stderr
, "conv.bc written\n");
346 fprintf(stderr
, "Invoke as \"llc -o - conv.bc\"\n");
352 LLVMFreeMachineCodeForFunction(engine
, func
);
354 LLVMDisposeExecutionEngine(engine
);
356 LLVMDisposePassManager(pass
);
362 const struct lp_type conv_types
[] = {
363 /* float, fixed, sign, norm, width, len */
365 { TRUE
, FALSE
, TRUE
, TRUE
, 32, 4 },
366 { TRUE
, FALSE
, TRUE
, FALSE
, 32, 4 },
367 { TRUE
, FALSE
, FALSE
, TRUE
, 32, 4 },
368 { TRUE
, FALSE
, FALSE
, FALSE
, 32, 4 },
370 /* TODO: test fixed formats too */
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
, 32, 4 },
378 { FALSE
, FALSE
, TRUE
, FALSE
, 32, 4 },
379 { FALSE
, FALSE
, FALSE
, TRUE
, 32, 4 },
380 { FALSE
, FALSE
, FALSE
, FALSE
, 32, 4 },
382 { FALSE
, FALSE
, TRUE
, TRUE
, 16, 8 },
383 { FALSE
, FALSE
, TRUE
, FALSE
, 16, 8 },
384 { FALSE
, FALSE
, FALSE
, TRUE
, 16, 8 },
385 { FALSE
, FALSE
, FALSE
, FALSE
, 16, 8 },
387 { FALSE
, FALSE
, TRUE
, TRUE
, 8, 16 },
388 { FALSE
, FALSE
, TRUE
, FALSE
, 8, 16 },
389 { FALSE
, FALSE
, FALSE
, TRUE
, 8, 16 },
390 { FALSE
, FALSE
, FALSE
, FALSE
, 8, 16 },
392 { FALSE
, FALSE
, TRUE
, TRUE
, 8, 4 },
393 { FALSE
, FALSE
, TRUE
, FALSE
, 8, 4 },
394 { FALSE
, FALSE
, FALSE
, TRUE
, 8, 4 },
395 { FALSE
, FALSE
, FALSE
, FALSE
, 8, 4 },
399 const unsigned num_types
= sizeof(conv_types
)/sizeof(conv_types
[0]);
403 test_all(unsigned verbose
, FILE *fp
)
405 const struct lp_type
*src_type
;
406 const struct lp_type
*dst_type
;
407 boolean success
= TRUE
;
409 for(src_type
= conv_types
; src_type
< &conv_types
[num_types
]; ++src_type
) {
410 for(dst_type
= conv_types
; dst_type
< &conv_types
[num_types
]; ++dst_type
) {
412 if(src_type
== dst_type
)
415 if(src_type
->norm
!= dst_type
->norm
)
418 if(!test_one(verbose
, fp
, *src_type
, *dst_type
))
429 test_some(unsigned verbose
, FILE *fp
, unsigned long n
)
431 const struct lp_type
*src_type
;
432 const struct lp_type
*dst_type
;
434 boolean success
= TRUE
;
436 for(i
= 0; i
< n
; ++i
) {
437 src_type
= &conv_types
[rand() % num_types
];
440 dst_type
= &conv_types
[rand() % num_types
];
441 } while (src_type
== dst_type
|| src_type
->norm
!= dst_type
->norm
);
443 if(!test_one(verbose
, fp
, *src_type
, *dst_type
))
452 test_single(unsigned verbose
, FILE *fp
)
454 /* float, fixed, sign, norm, width, len */
455 struct lp_type f32x4_type
=
456 { TRUE
, FALSE
, TRUE
, TRUE
, 32, 4 };
457 struct lp_type ub8x4_type
=
458 { FALSE
, FALSE
, FALSE
, TRUE
, 8, 16 };
462 success
= test_one(verbose
, fp
, f32x4_type
, ub8x4_type
);