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;
171 dump_conv_types(stdout
, src_type
, dst_type
);
173 if(src_type
.length
> dst_type
.length
) {
175 num_dsts
= src_type
.length
/dst_type
.length
;
179 num_srcs
= dst_type
.length
/src_type
.length
;
182 assert(src_type
.width
* src_type
.length
== dst_type
.width
* dst_type
.length
);
184 /* We must not loose or gain channels. Only precision */
185 assert(src_type
.length
* num_srcs
== dst_type
.length
* num_dsts
);
187 eps
= MAX2(lp_const_eps(src_type
), lp_const_eps(dst_type
));
189 module
= LLVMModuleCreateWithName("test");
191 func
= add_conv_test(module
, src_type
, num_srcs
, dst_type
, num_dsts
);
193 if(LLVMVerifyModule(module
, LLVMPrintMessageAction
, &error
)) {
194 LLVMDumpModule(module
);
197 LLVMDisposeMessage(error
);
199 provider
= LLVMCreateModuleProviderForExistingModule(module
);
200 if (LLVMCreateJITCompiler(&engine
, provider
, 1, &error
)) {
202 dump_conv_types(stderr
, src_type
, dst_type
);
203 fprintf(stderr
, "%s\n", error
);
204 LLVMDisposeMessage(error
);
209 pass
= LLVMCreatePassManager();
210 LLVMAddTargetData(LLVMGetExecutionEngineTargetData(engine
), pass
);
211 /* These are the passes currently listed in llvm-c/Transforms/Scalar.h,
212 * but there are more on SVN. */
213 LLVMAddConstantPropagationPass(pass
);
214 LLVMAddInstructionCombiningPass(pass
);
215 LLVMAddPromoteMemoryToRegisterPass(pass
);
216 LLVMAddGVNPass(pass
);
217 LLVMAddCFGSimplificationPass(pass
);
218 LLVMRunPassManager(pass
, module
);
224 LLVMDumpModule(module
);
226 code
= LLVMGetPointerToGlobal(engine
, func
);
227 conv_test_ptr
= (conv_test_ptr_t
)pointer_to_func(code
);
230 lp_disassemble(code
);
233 for(i
= 0; i
< n
&& success
; ++i
) {
234 unsigned src_stride
= src_type
.length
*src_type
.width
/8;
235 unsigned dst_stride
= dst_type
.length
*dst_type
.width
/8;
236 PIPE_ALIGN_VAR(16) uint8_t src
[LP_MAX_VECTOR_LENGTH
*LP_MAX_VECTOR_LENGTH
];
237 PIPE_ALIGN_VAR(16) uint8_t dst
[LP_MAX_VECTOR_LENGTH
*LP_MAX_VECTOR_LENGTH
];
238 double fref
[LP_MAX_VECTOR_LENGTH
*LP_MAX_VECTOR_LENGTH
];
239 uint8_t ref
[LP_MAX_VECTOR_LENGTH
*LP_MAX_VECTOR_LENGTH
];
240 int64_t start_counter
= 0;
241 int64_t end_counter
= 0;
243 for(j
= 0; j
< num_srcs
; ++j
) {
244 random_vec(src_type
, src
+ j
*src_stride
);
245 read_vec(src_type
, src
+ j
*src_stride
, fref
+ j
*src_type
.length
);
248 for(j
= 0; j
< num_dsts
; ++j
) {
249 write_vec(dst_type
, ref
+ j
*dst_stride
, fref
+ j
*dst_type
.length
);
252 start_counter
= rdtsc();
253 conv_test_ptr(src
, dst
);
254 end_counter
= rdtsc();
256 cycles
[i
] = end_counter
- start_counter
;
258 for(j
= 0; j
< num_dsts
; ++j
) {
259 if(!compare_vec_with_eps(dst_type
, dst
+ j
*dst_stride
, ref
+ j
*dst_stride
, eps
))
263 if (!success
|| verbose
>= 3) {
265 dump_conv_types(stderr
, src_type
, dst_type
);
267 fprintf(stderr
, "PASS\n");
270 fprintf(stderr
, "MISMATCH\n");
273 for(j
= 0; j
< num_srcs
; ++j
) {
274 fprintf(stderr
, " Src%u: ", j
);
275 dump_vec(stderr
, src_type
, src
+ j
*src_stride
);
276 fprintf(stderr
, "\n");
280 fprintf(stderr
, " Ref: ");
281 for(j
= 0; j
< src_type
.length
*num_srcs
; ++j
)
282 fprintf(stderr
, " %f", fref
[j
]);
283 fprintf(stderr
, "\n");
286 for(j
= 0; j
< num_dsts
; ++j
) {
287 fprintf(stderr
, " Dst%u: ", j
);
288 dump_vec(stderr
, dst_type
, dst
+ j
*dst_stride
);
289 fprintf(stderr
, "\n");
291 fprintf(stderr
, " Ref%u: ", j
);
292 dump_vec(stderr
, dst_type
, ref
+ j
*dst_stride
);
293 fprintf(stderr
, "\n");
299 * Unfortunately the output of cycle counter is not very reliable as it comes
300 * -- sometimes we get outliers (due IRQs perhaps?) which are
301 * better removed to avoid random or biased data.
304 double sum
= 0.0, sum2
= 0.0;
308 for(i
= 0; i
< n
; ++i
) {
310 sum2
+= cycles
[i
]*cycles
[i
];
314 std
= sqrtf((sum2
- n
*avg
*avg
)/n
);
318 for(i
= 0; i
< n
; ++i
) {
319 if(fabs(cycles
[i
] - avg
) <= 4.0*std
) {
330 write_tsv_row(fp
, src_type
, dst_type
, cycles_avg
, success
);
333 static boolean firsttime
= TRUE
;
336 LLVMDumpModule(module
);
337 LLVMWriteBitcodeToFile(module
, "conv.bc");
338 fprintf(stderr
, "conv.bc written\n");
339 fprintf(stderr
, "Invoke as \"llc -o - conv.bc\"\n");
345 LLVMFreeMachineCodeForFunction(engine
, func
);
347 LLVMDisposeExecutionEngine(engine
);
349 LLVMDisposePassManager(pass
);
355 const struct lp_type conv_types
[] = {
356 /* float, fixed, sign, norm, width, len */
358 { TRUE
, FALSE
, TRUE
, TRUE
, 32, 4 },
359 { TRUE
, FALSE
, TRUE
, FALSE
, 32, 4 },
360 { TRUE
, FALSE
, FALSE
, TRUE
, 32, 4 },
361 { TRUE
, FALSE
, FALSE
, FALSE
, 32, 4 },
363 /* TODO: test fixed formats too */
365 { FALSE
, FALSE
, TRUE
, TRUE
, 16, 8 },
366 { FALSE
, FALSE
, TRUE
, FALSE
, 16, 8 },
367 { FALSE
, FALSE
, FALSE
, TRUE
, 16, 8 },
368 { FALSE
, FALSE
, FALSE
, FALSE
, 16, 8 },
370 { FALSE
, FALSE
, TRUE
, TRUE
, 32, 4 },
371 { FALSE
, FALSE
, TRUE
, FALSE
, 32, 4 },
372 { FALSE
, FALSE
, FALSE
, TRUE
, 32, 4 },
373 { FALSE
, FALSE
, FALSE
, FALSE
, 32, 4 },
375 { FALSE
, FALSE
, TRUE
, TRUE
, 16, 8 },
376 { FALSE
, FALSE
, TRUE
, FALSE
, 16, 8 },
377 { FALSE
, FALSE
, FALSE
, TRUE
, 16, 8 },
378 { FALSE
, FALSE
, FALSE
, FALSE
, 16, 8 },
380 { FALSE
, FALSE
, TRUE
, TRUE
, 8, 16 },
381 { FALSE
, FALSE
, TRUE
, FALSE
, 8, 16 },
382 { FALSE
, FALSE
, FALSE
, TRUE
, 8, 16 },
383 { FALSE
, FALSE
, FALSE
, FALSE
, 8, 16 },
387 const unsigned num_types
= sizeof(conv_types
)/sizeof(conv_types
[0]);
391 test_all(unsigned verbose
, FILE *fp
)
393 const struct lp_type
*src_type
;
394 const struct lp_type
*dst_type
;
395 boolean success
= TRUE
;
397 for(src_type
= conv_types
; src_type
< &conv_types
[num_types
]; ++src_type
) {
398 for(dst_type
= conv_types
; dst_type
< &conv_types
[num_types
]; ++dst_type
) {
400 if(src_type
== dst_type
)
403 if(src_type
->norm
!= dst_type
->norm
)
406 if(!test_one(verbose
, fp
, *src_type
, *dst_type
))
417 test_some(unsigned verbose
, FILE *fp
, unsigned long n
)
419 const struct lp_type
*src_type
;
420 const struct lp_type
*dst_type
;
422 boolean success
= TRUE
;
424 for(i
= 0; i
< n
; ++i
) {
425 src_type
= &conv_types
[rand() % num_types
];
428 dst_type
= &conv_types
[rand() % num_types
];
429 } while (src_type
== dst_type
|| src_type
->norm
!= dst_type
->norm
);
431 if(!test_one(verbose
, fp
, *src_type
, *dst_type
))
440 test_single(unsigned verbose
, FILE *fp
)
442 /* float, fixed, sign, norm, width, len */
443 struct lp_type f32x4_type
=
444 { TRUE
, FALSE
, TRUE
, TRUE
, 32, 4 };
445 struct lp_type ub8x4_type
=
446 { FALSE
, FALSE
, FALSE
, TRUE
, 8, 16 };
450 success
= test_one(verbose
, fp
, f32x4_type
, ub8x4_type
);