blob: 958cc40538e92d6420e30432927136a43cc359fe [file] [log] [blame]
José Fonseca89884242009-08-07 09:51:48 +01001/**************************************************************************
2 *
3 * Copyright 2009 VMware, Inc.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28
29/**
30 * @file
31 * Unit tests for type conversion.
32 *
33 * @author Jose Fonseca <jfonseca@vmware.com>
34 */
35
36
Zack Rusinc61bf362010-02-08 18:05:22 -050037#include "gallivm/lp_bld_type.h"
38#include "gallivm/lp_bld_const.h"
39#include "gallivm/lp_bld_conv.h"
40#include "gallivm/lp_bld_debug.h"
José Fonseca89884242009-08-07 09:51:48 +010041#include "lp_test.h"
42
43
44typedef void (*conv_test_ptr_t)(const void *src, const void *dst);
45
46
47void
48write_tsv_header(FILE *fp)
49{
50 fprintf(fp,
51 "result\t"
José Fonsecab07d19a2009-08-08 20:38:01 +010052 "cycles_per_channel\t"
José Fonseca89884242009-08-07 09:51:48 +010053 "src_type\t"
54 "dst_type\n");
55
56 fflush(fp);
57}
58
59
60static void
61write_tsv_row(FILE *fp,
José Fonsecab4835ea2009-09-14 11:05:06 +010062 struct lp_type src_type,
63 struct lp_type dst_type,
José Fonseca89884242009-08-07 09:51:48 +010064 double cycles,
65 boolean success)
66{
67 fprintf(fp, "%s\t", success ? "pass" : "fail");
68
José Fonsecab07d19a2009-08-08 20:38:01 +010069 fprintf(fp, "%.1f\t", cycles / MAX2(src_type.length, dst_type.length));
José Fonseca89884242009-08-07 09:51:48 +010070
71 dump_type(fp, src_type);
72 fprintf(fp, "\t");
73
74 dump_type(fp, dst_type);
José Fonsecab07d19a2009-08-08 20:38:01 +010075 fprintf(fp, "\n");
José Fonseca89884242009-08-07 09:51:48 +010076
77 fflush(fp);
78}
79
80
81static void
82dump_conv_types(FILE *fp,
José Fonsecab4835ea2009-09-14 11:05:06 +010083 struct lp_type src_type,
84 struct lp_type dst_type)
José Fonseca89884242009-08-07 09:51:48 +010085{
86 fprintf(fp, "src_type=");
87 dump_type(fp, src_type);
88
89 fprintf(fp, " dst_type=");
90 dump_type(fp, dst_type);
91
José Fonseca60584af2009-08-08 23:10:59 +010092 fprintf(fp, " ...\n");
José Fonseca89884242009-08-07 09:51:48 +010093 fflush(fp);
94}
95
96
97static LLVMValueRef
98add_conv_test(LLVMModuleRef module,
José Fonsecab4835ea2009-09-14 11:05:06 +010099 struct lp_type src_type, unsigned num_srcs,
100 struct lp_type dst_type, unsigned num_dsts)
José Fonseca89884242009-08-07 09:51:48 +0100101{
102 LLVMTypeRef args[2];
103 LLVMValueRef func;
104 LLVMValueRef src_ptr;
105 LLVMValueRef dst_ptr;
106 LLVMBasicBlockRef block;
107 LLVMBuilderRef builder;
108 LLVMValueRef src[LP_MAX_VECTOR_LENGTH];
109 LLVMValueRef dst[LP_MAX_VECTOR_LENGTH];
110 unsigned i;
111
112 args[0] = LLVMPointerType(lp_build_vec_type(src_type), 0);
113 args[1] = LLVMPointerType(lp_build_vec_type(dst_type), 0);
114
115 func = LLVMAddFunction(module, "test", LLVMFunctionType(LLVMVoidType(), args, 2, 0));
116 LLVMSetFunctionCallConv(func, LLVMCCallConv);
117 src_ptr = LLVMGetParam(func, 0);
118 dst_ptr = LLVMGetParam(func, 1);
119
120 block = LLVMAppendBasicBlock(func, "entry");
121 builder = LLVMCreateBuilder();
122 LLVMPositionBuilderAtEnd(builder, block);
123
124 for(i = 0; i < num_srcs; ++i) {
125 LLVMValueRef index = LLVMConstInt(LLVMInt32Type(), i, 0);
126 LLVMValueRef ptr = LLVMBuildGEP(builder, src_ptr, &index, 1, "");
127 src[i] = LLVMBuildLoad(builder, ptr, "");
128 }
129
130 lp_build_conv(builder, src_type, dst_type, src, num_srcs, dst, num_dsts);
131
132 for(i = 0; i < num_dsts; ++i) {
133 LLVMValueRef index = LLVMConstInt(LLVMInt32Type(), i, 0);
134 LLVMValueRef ptr = LLVMBuildGEP(builder, dst_ptr, &index, 1, "");
135 LLVMBuildStore(builder, dst[i], ptr);
136 }
137
138 LLVMBuildRetVoid(builder);;
139
140 LLVMDisposeBuilder(builder);
141 return func;
142}
143
144
José Fonseca26c78a42010-01-12 12:15:24 +0000145PIPE_ALIGN_STACK
José Fonseca89884242009-08-07 09:51:48 +0100146static boolean
147test_one(unsigned verbose,
148 FILE *fp,
José Fonsecab4835ea2009-09-14 11:05:06 +0100149 struct lp_type src_type,
150 struct lp_type dst_type)
José Fonseca89884242009-08-07 09:51:48 +0100151{
152 LLVMModuleRef module = NULL;
153 LLVMValueRef func = NULL;
154 LLVMExecutionEngineRef engine = NULL;
155 LLVMModuleProviderRef provider = NULL;
156 LLVMPassManagerRef pass = NULL;
157 char *error = NULL;
158 conv_test_ptr_t conv_test_ptr;
159 boolean success;
José Fonseca9aafa1f2009-10-22 19:02:04 +0100160 const unsigned n = LP_TEST_NUM_SAMPLES;
161 int64_t cycles[LP_TEST_NUM_SAMPLES];
José Fonseca89884242009-08-07 09:51:48 +0100162 double cycles_avg = 0.0;
163 unsigned num_srcs;
164 unsigned num_dsts;
José Fonseca33ce51b2009-08-21 07:34:15 +0100165 double eps;
José Fonseca89884242009-08-07 09:51:48 +0100166 unsigned i, j;
167
168 if(verbose >= 1)
169 dump_conv_types(stdout, src_type, dst_type);
170
171 if(src_type.length > dst_type.length) {
172 num_srcs = 1;
173 num_dsts = src_type.length/dst_type.length;
174 }
175 else {
176 num_dsts = 1;
177 num_srcs = dst_type.length/src_type.length;
178 }
179
180 assert(src_type.width * src_type.length == dst_type.width * dst_type.length);
181
182 /* We must not loose or gain channels. Only precision */
183 assert(src_type.length * num_srcs == dst_type.length * num_dsts);
184
José Fonseca33ce51b2009-08-21 07:34:15 +0100185 eps = MAX2(lp_const_eps(src_type), lp_const_eps(dst_type));
186
José Fonseca89884242009-08-07 09:51:48 +0100187 module = LLVMModuleCreateWithName("test");
188
189 func = add_conv_test(module, src_type, num_srcs, dst_type, num_dsts);
190
191 if(LLVMVerifyModule(module, LLVMPrintMessageAction, &error)) {
192 LLVMDumpModule(module);
193 abort();
194 }
195 LLVMDisposeMessage(error);
196
197 provider = LLVMCreateModuleProviderForExistingModule(module);
198 if (LLVMCreateJITCompiler(&engine, provider, 1, &error)) {
José Fonseca60584af2009-08-08 23:10:59 +0100199 if(verbose < 1)
200 dump_conv_types(stderr, src_type, dst_type);
José Fonseca89884242009-08-07 09:51:48 +0100201 fprintf(stderr, "%s\n", error);
202 LLVMDisposeMessage(error);
203 abort();
204 }
205
206#if 0
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);
217#else
218 (void)pass;
219#endif
220
221 if(verbose >= 2)
222 LLVMDumpModule(module);
223
224 conv_test_ptr = (conv_test_ptr_t)LLVMGetPointerToGlobal(engine, func);
225
José Fonseca818d4442009-08-16 11:50:17 +0100226 if(verbose >= 2)
227 lp_disassemble(conv_test_ptr);
228
José Fonseca89884242009-08-07 09:51:48 +0100229 success = TRUE;
230 for(i = 0; i < n && success; ++i) {
231 unsigned src_stride = src_type.length*src_type.width/8;
232 unsigned dst_stride = dst_type.length*dst_type.width/8;
José Fonseca5dfd5ed2010-01-12 11:47:37 +0000233 PIPE_ALIGN_VAR(16) uint8_t src[LP_MAX_VECTOR_LENGTH*LP_MAX_VECTOR_LENGTH];
234 PIPE_ALIGN_VAR(16) uint8_t dst[LP_MAX_VECTOR_LENGTH*LP_MAX_VECTOR_LENGTH];
José Fonseca89884242009-08-07 09:51:48 +0100235 double fref[LP_MAX_VECTOR_LENGTH*LP_MAX_VECTOR_LENGTH];
236 uint8_t ref[LP_MAX_VECTOR_LENGTH*LP_MAX_VECTOR_LENGTH];
237 int64_t start_counter = 0;
238 int64_t end_counter = 0;
239
240 for(j = 0; j < num_srcs; ++j) {
241 random_vec(src_type, src + j*src_stride);
242 read_vec(src_type, src + j*src_stride, fref + j*src_type.length);
243 }
244
245 for(j = 0; j < num_dsts; ++j) {
246 write_vec(dst_type, ref + j*dst_stride, fref + j*dst_type.length);
247 }
248
249 start_counter = rdtsc();
250 conv_test_ptr(src, dst);
251 end_counter = rdtsc();
252
253 cycles[i] = end_counter - start_counter;
254
255 for(j = 0; j < num_dsts; ++j) {
José Fonseca33ce51b2009-08-21 07:34:15 +0100256 if(!compare_vec_with_eps(dst_type, dst + j*dst_stride, ref + j*dst_stride, eps))
José Fonseca89884242009-08-07 09:51:48 +0100257 success = FALSE;
258 }
259
260 if (!success) {
José Fonseca60584af2009-08-08 23:10:59 +0100261 if(verbose < 1)
262 dump_conv_types(stderr, src_type, dst_type);
José Fonseca89884242009-08-07 09:51:48 +0100263 fprintf(stderr, "MISMATCH\n");
264
265 for(j = 0; j < num_srcs; ++j) {
266 fprintf(stderr, " Src%u: ", j);
267 dump_vec(stderr, src_type, src + j*src_stride);
268 fprintf(stderr, "\n");
269 }
270
José Fonseca33ce51b2009-08-21 07:34:15 +0100271#if 1
272 fprintf(stderr, " Ref: ");
José Fonseca89884242009-08-07 09:51:48 +0100273 for(j = 0; j < src_type.length*num_srcs; ++j)
274 fprintf(stderr, " %f", fref[j]);
275 fprintf(stderr, "\n");
José Fonseca60584af2009-08-08 23:10:59 +0100276#endif
José Fonseca89884242009-08-07 09:51:48 +0100277
278 for(j = 0; j < num_dsts; ++j) {
279 fprintf(stderr, " Dst%u: ", j);
280 dump_vec(stderr, dst_type, dst + j*dst_stride);
281 fprintf(stderr, "\n");
282
283 fprintf(stderr, " Ref%u: ", j);
284 dump_vec(stderr, dst_type, ref + j*dst_stride);
285 fprintf(stderr, "\n");
286 }
287 }
288 }
289
290 /*
291 * Unfortunately the output of cycle counter is not very reliable as it comes
292 * -- sometimes we get outliers (due IRQs perhaps?) which are
293 * better removed to avoid random or biased data.
294 */
295 {
296 double sum = 0.0, sum2 = 0.0;
297 double avg, std;
298 unsigned m;
299
300 for(i = 0; i < n; ++i) {
301 sum += cycles[i];
302 sum2 += cycles[i]*cycles[i];
303 }
304
305 avg = sum/n;
306 std = sqrtf((sum2 - n*avg*avg)/n);
307
308 m = 0;
309 sum = 0.0;
310 for(i = 0; i < n; ++i) {
311 if(fabs(cycles[i] - avg) <= 4.0*std) {
312 sum += cycles[i];
313 ++m;
314 }
315 }
316
317 cycles_avg = sum/m;
318
319 }
320
José Fonseca89884242009-08-07 09:51:48 +0100321 if(fp)
322 write_tsv_row(fp, src_type, dst_type, cycles_avg, success);
323
324 if (!success) {
José Fonseca60584af2009-08-08 23:10:59 +0100325 static boolean firsttime = TRUE;
326 if(firsttime) {
327 if(verbose < 2)
328 LLVMDumpModule(module);
329 LLVMWriteBitcodeToFile(module, "conv.bc");
330 fprintf(stderr, "conv.bc written\n");
331 fprintf(stderr, "Invoke as \"llc -o - conv.bc\"\n");
332 firsttime = FALSE;
Vinson Leefd237a82010-01-01 15:38:19 -0800333 /* abort(); */
José Fonseca60584af2009-08-08 23:10:59 +0100334 }
José Fonseca89884242009-08-07 09:51:48 +0100335 }
336
337 LLVMFreeMachineCodeForFunction(engine, func);
338
339 LLVMDisposeExecutionEngine(engine);
340 if(pass)
341 LLVMDisposePassManager(pass);
342
343 return success;
344}
345
346
José Fonsecab4835ea2009-09-14 11:05:06 +0100347const struct lp_type conv_types[] = {
José Fonseca89884242009-08-07 09:51:48 +0100348 /* float, fixed, sign, norm, width, len */
José Fonseca60584af2009-08-08 23:10:59 +0100349
José Fonsecab4835ea2009-09-14 11:05:06 +0100350 { TRUE, FALSE, TRUE, TRUE, 32, 4 },
351 { TRUE, FALSE, TRUE, FALSE, 32, 4 },
352 { TRUE, FALSE, FALSE, TRUE, 32, 4 },
353 { TRUE, FALSE, FALSE, FALSE, 32, 4 },
José Fonseca60584af2009-08-08 23:10:59 +0100354
José Fonseca64cc7112009-08-22 12:37:12 +0100355 /* TODO: test fixed formats too */
356
José Fonsecab4835ea2009-09-14 11:05:06 +0100357 { FALSE, FALSE, TRUE, TRUE, 16, 8 },
358 { FALSE, FALSE, TRUE, FALSE, 16, 8 },
359 { FALSE, FALSE, FALSE, TRUE, 16, 8 },
360 { FALSE, FALSE, FALSE, FALSE, 16, 8 },
José Fonseca64cc7112009-08-22 12:37:12 +0100361
José Fonsecab4835ea2009-09-14 11:05:06 +0100362 { FALSE, FALSE, TRUE, TRUE, 32, 4 },
363 { FALSE, FALSE, TRUE, FALSE, 32, 4 },
364 { FALSE, FALSE, FALSE, TRUE, 32, 4 },
365 { FALSE, FALSE, FALSE, FALSE, 32, 4 },
José Fonseca60584af2009-08-08 23:10:59 +0100366
José Fonsecab4835ea2009-09-14 11:05:06 +0100367 { FALSE, FALSE, TRUE, TRUE, 16, 8 },
368 { FALSE, FALSE, TRUE, FALSE, 16, 8 },
369 { FALSE, FALSE, FALSE, TRUE, 16, 8 },
370 { FALSE, FALSE, FALSE, FALSE, 16, 8 },
José Fonseca60584af2009-08-08 23:10:59 +0100371
José Fonsecab4835ea2009-09-14 11:05:06 +0100372 { FALSE, FALSE, TRUE, TRUE, 8, 16 },
373 { FALSE, FALSE, TRUE, FALSE, 8, 16 },
374 { FALSE, FALSE, FALSE, TRUE, 8, 16 },
375 { FALSE, FALSE, FALSE, FALSE, 8, 16 },
José Fonseca89884242009-08-07 09:51:48 +0100376};
377
378
379const unsigned num_types = sizeof(conv_types)/sizeof(conv_types[0]);
380
381
382boolean
383test_all(unsigned verbose, FILE *fp)
384{
José Fonsecab4835ea2009-09-14 11:05:06 +0100385 const struct lp_type *src_type;
386 const struct lp_type *dst_type;
José Fonseca89884242009-08-07 09:51:48 +0100387 bool success = TRUE;
388
José Fonsecab874a7b2009-08-07 14:34:13 +0100389 for(src_type = conv_types; src_type < &conv_types[num_types]; ++src_type) {
José Fonseca89884242009-08-07 09:51:48 +0100390 for(dst_type = conv_types; dst_type < &conv_types[num_types]; ++dst_type) {
391
392 if(src_type == dst_type)
393 continue;
394
José Fonseca60584af2009-08-08 23:10:59 +0100395 if(src_type->norm != dst_type->norm)
396 continue;
397
José Fonseca89884242009-08-07 09:51:48 +0100398 if(!test_one(verbose, fp, *src_type, *dst_type))
399 success = FALSE;
400
401 }
402 }
403
404 return success;
405}
406
407
408boolean
409test_some(unsigned verbose, FILE *fp, unsigned long n)
410{
José Fonsecab4835ea2009-09-14 11:05:06 +0100411 const struct lp_type *src_type;
412 const struct lp_type *dst_type;
José Fonseca89884242009-08-07 09:51:48 +0100413 unsigned long i;
414 bool success = TRUE;
415
416 for(i = 0; i < n; ++i) {
José Fonseca459ea002009-09-16 10:39:06 +0100417 src_type = &conv_types[rand() % num_types];
José Fonseca89884242009-08-07 09:51:48 +0100418
419 do {
José Fonseca459ea002009-09-16 10:39:06 +0100420 dst_type = &conv_types[rand() % num_types];
José Fonseca60584af2009-08-08 23:10:59 +0100421 } while (src_type == dst_type || src_type->norm != dst_type->norm);
José Fonseca89884242009-08-07 09:51:48 +0100422
423 if(!test_one(verbose, fp, *src_type, *dst_type))
424 success = FALSE;
425 }
426
427 return success;
428}