llvmpipe: raise dirty flag on transfers to bound constbuf
[mesa.git] / src / gallium / drivers / llvmpipe / lp_test_conv.c
1 /**************************************************************************
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
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"
43 #include "lp_test.h"
44
45
46 typedef void (*conv_test_ptr_t)(const void *src, const void *dst);
47
48
49 void
50 write_tsv_header(FILE *fp)
51 {
52 fprintf(fp,
53 "result\t"
54 "cycles_per_channel\t"
55 "src_type\t"
56 "dst_type\n");
57
58 fflush(fp);
59 }
60
61
62 static void
63 write_tsv_row(FILE *fp,
64 struct lp_type src_type,
65 struct lp_type dst_type,
66 double cycles,
67 boolean success)
68 {
69 fprintf(fp, "%s\t", success ? "pass" : "fail");
70
71 fprintf(fp, "%.1f\t", cycles / MAX2(src_type.length, dst_type.length));
72
73 dump_type(fp, src_type);
74 fprintf(fp, "\t");
75
76 dump_type(fp, dst_type);
77 fprintf(fp, "\n");
78
79 fflush(fp);
80 }
81
82
83 static void
84 dump_conv_types(FILE *fp,
85 struct lp_type src_type,
86 struct lp_type dst_type)
87 {
88 fprintf(fp, "src_type=");
89 dump_type(fp, src_type);
90
91 fprintf(fp, " dst_type=");
92 dump_type(fp, dst_type);
93
94 fprintf(fp, " ...\n");
95 fflush(fp);
96 }
97
98
99 static LLVMValueRef
100 add_conv_test(LLVMModuleRef module,
101 struct lp_type src_type, unsigned num_srcs,
102 struct lp_type dst_type, unsigned num_dsts)
103 {
104 LLVMTypeRef args[2];
105 LLVMValueRef func;
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];
112 unsigned i;
113
114 args[0] = LLVMPointerType(lp_build_vec_type(src_type), 0);
115 args[1] = LLVMPointerType(lp_build_vec_type(dst_type), 0);
116
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);
121
122 block = LLVMAppendBasicBlock(func, "entry");
123 builder = LLVMCreateBuilder();
124 LLVMPositionBuilderAtEnd(builder, block);
125
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, "");
130 }
131
132 lp_build_conv(builder, src_type, dst_type, src, num_srcs, dst, num_dsts);
133
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);
138 }
139
140 LLVMBuildRetVoid(builder);;
141
142 LLVMDisposeBuilder(builder);
143 return func;
144 }
145
146
147 PIPE_ALIGN_STACK
148 static boolean
149 test_one(unsigned verbose,
150 FILE *fp,
151 struct lp_type src_type,
152 struct lp_type dst_type)
153 {
154 LLVMModuleRef module = NULL;
155 LLVMValueRef func = NULL;
156 LLVMExecutionEngineRef engine = lp_build_engine;
157 LLVMPassManagerRef pass = NULL;
158 char *error = NULL;
159 conv_test_ptr_t conv_test_ptr;
160 boolean success;
161 const unsigned n = LP_TEST_NUM_SAMPLES;
162 int64_t cycles[LP_TEST_NUM_SAMPLES];
163 double cycles_avg = 0.0;
164 unsigned num_srcs;
165 unsigned num_dsts;
166 double eps;
167 unsigned i, j;
168 void *code;
169
170 if (src_type.width * src_type.length != dst_type.width * dst_type.length &&
171 src_type.length != dst_type.length) {
172 return TRUE;
173 }
174
175 if(verbose >= 1)
176 dump_conv_types(stdout, src_type, dst_type);
177
178 if (src_type.length > dst_type.length) {
179 num_srcs = 1;
180 num_dsts = src_type.length/dst_type.length;
181 }
182 else if (src_type.length < dst_type.length) {
183 num_dsts = 1;
184 num_srcs = dst_type.length/src_type.length;
185 }
186 else {
187 num_dsts = 1;
188 num_srcs = 1;
189 }
190
191 /* We must not loose or gain channels. Only precision */
192 assert(src_type.length * num_srcs == dst_type.length * num_dsts);
193
194 eps = MAX2(lp_const_eps(src_type), lp_const_eps(dst_type));
195
196 module = LLVMModuleCreateWithName("test");
197
198 func = add_conv_test(module, src_type, num_srcs, dst_type, num_dsts);
199
200 if(LLVMVerifyModule(module, LLVMPrintMessageAction, &error)) {
201 LLVMDumpModule(module);
202 abort();
203 }
204 LLVMDisposeMessage(error);
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 code = LLVMGetPointerToGlobal(engine, func);
225 conv_test_ptr = (conv_test_ptr_t)pointer_to_func(code);
226
227 if(verbose >= 2)
228 lp_disassemble(code);
229
230 success = TRUE;
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;
240
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);
244 }
245
246 for(j = 0; j < num_dsts; ++j) {
247 write_vec(dst_type, ref + j*dst_stride, fref + j*dst_type.length);
248 }
249
250 start_counter = rdtsc();
251 conv_test_ptr(src, dst);
252 end_counter = rdtsc();
253
254 cycles[i] = end_counter - start_counter;
255
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))
258 success = FALSE;
259 }
260
261 if (!success || verbose >= 3) {
262 if(verbose < 1)
263 dump_conv_types(stderr, src_type, dst_type);
264 if (success) {
265 fprintf(stderr, "PASS\n");
266 }
267 else {
268 fprintf(stderr, "MISMATCH\n");
269 }
270
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");
275 }
276
277 #if 1
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");
282 #endif
283
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");
288
289 fprintf(stderr, " Ref%u: ", j);
290 dump_vec(stderr, dst_type, ref + j*dst_stride);
291 fprintf(stderr, "\n");
292 }
293 }
294 }
295
296 /*
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.
300 */
301 {
302 double sum = 0.0, sum2 = 0.0;
303 double avg, std;
304 unsigned m;
305
306 for(i = 0; i < n; ++i) {
307 sum += cycles[i];
308 sum2 += cycles[i]*cycles[i];
309 }
310
311 avg = sum/n;
312 std = sqrtf((sum2 - n*avg*avg)/n);
313
314 m = 0;
315 sum = 0.0;
316 for(i = 0; i < n; ++i) {
317 if(fabs(cycles[i] - avg) <= 4.0*std) {
318 sum += cycles[i];
319 ++m;
320 }
321 }
322
323 cycles_avg = sum/m;
324
325 }
326
327 if(fp)
328 write_tsv_row(fp, src_type, dst_type, cycles_avg, success);
329
330 if (!success) {
331 static boolean firsttime = TRUE;
332 if(firsttime) {
333 if(verbose < 2)
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");
338 firsttime = FALSE;
339 /* abort(); */
340 }
341 }
342
343 LLVMFreeMachineCodeForFunction(engine, func);
344
345 if(pass)
346 LLVMDisposePassManager(pass);
347
348 return success;
349 }
350
351
352 const struct lp_type conv_types[] = {
353 /* float, fixed, sign, norm, width, len */
354
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 },
359
360 /* TODO: test fixed formats too */
361
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 },
366
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 },
371
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 },
376
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 },
381
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 },
386 };
387
388
389 const unsigned num_types = sizeof(conv_types)/sizeof(conv_types[0]);
390
391
392 boolean
393 test_all(unsigned verbose, FILE *fp)
394 {
395 const struct lp_type *src_type;
396 const struct lp_type *dst_type;
397 boolean success = TRUE;
398
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) {
401
402 if(src_type == dst_type)
403 continue;
404
405 if(src_type->norm != dst_type->norm)
406 continue;
407
408 if(!test_one(verbose, fp, *src_type, *dst_type))
409 success = FALSE;
410
411 }
412 }
413
414 return success;
415 }
416
417
418 boolean
419 test_some(unsigned verbose, FILE *fp, unsigned long n)
420 {
421 const struct lp_type *src_type;
422 const struct lp_type *dst_type;
423 unsigned long i;
424 boolean success = TRUE;
425
426 for(i = 0; i < n; ++i) {
427 src_type = &conv_types[rand() % num_types];
428
429 do {
430 dst_type = &conv_types[rand() % num_types];
431 } while (src_type == dst_type || src_type->norm != dst_type->norm);
432
433 if(!test_one(verbose, fp, *src_type, *dst_type))
434 success = FALSE;
435 }
436
437 return success;
438 }
439
440
441 boolean
442 test_single(unsigned verbose, FILE *fp)
443 {
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 };
449
450 boolean success;
451
452 success = test_one(verbose, fp, f32x4_type, ub8x4_type);
453
454 return success;
455 }