[Beignet] [PATCH] Backend: Fix one bug of printf because of ir reorder.

2015-01-30 Thread junyan . he
From: Junyan He junyan...@linux.intel.com

The llvm will generate ir which has if.else block before
if.then block. We parse the printf statement before llvm_to_gen.
The later if-else analysis will reorder the if-else blocks.
This cause when we print out the result, we get the wrong message
from another printf statement.
Add printf index to the index buffer to record which one the result
belongs to, and so this bug is fixed.

Signed-off-by: Junyan He junyan...@linux.intel.com
---
 backend/src/ir/printf.cpp   | 25 -
 backend/src/ir/printf.hpp   |  2 +-
 backend/src/llvm/llvm_printf_parser.cpp | 19 ---
 src/cl_command_queue_gen7.c |  2 +-
 4 files changed, 34 insertions(+), 14 deletions(-)

diff --git a/backend/src/ir/printf.cpp b/backend/src/ir/printf.cpp
index fa108dc..3d9b2fd 100644
--- a/backend/src/ir/printf.cpp
+++ b/backend/src/ir/printf.cpp
@@ -34,8 +34,9 @@ namespace gbe
 uint32_t PrintfSet::append(PrintfFmt* fmt, Unit unit)
 {
   fmts.push_back(*fmt);
+  vectorPrintfSlot vp = fmts.back().first;
 
-  for (PrintfFmt::iterator f = fmts.back().begin(); f != 
fmts.back().end(); ++f) {
+  for (vectorPrintfSlot::iterator f = vp.begin(); f !=  vp.end(); ++f) {
 if (f-type == PRINTF_SLOT_TYPE_STRING)
   continue;
 
@@ -126,14 +127,27 @@ namespace gbe
   int stmt = 0;
 
   for (size_t count = 0; count  fmts.size(); ++count) {
-PrintfFmt pf = fmts[count];
 for (i = 0; i  global_wk_sz0; i++) {
   for (j = 0; j  global_wk_sz1; j++) {
 for (k = 0; k  global_wk_sz2; k++) {
-  int loop_num = ((int 
*)index_addr)[stmt*global_wk_sz0*global_wk_sz1*global_wk_sz2
- + 
k*global_wk_sz0*global_wk_sz1 + j*global_wk_sz0 + i];
+  int loop_num = ((int 
*)index_addr)[(stmt*global_wk_sz0*global_wk_sz1*global_wk_sz2
+ + 
k*global_wk_sz0*global_wk_sz1 + j*global_wk_sz0 + i)*2];
+  int printf_num = ((int 
*)index_addr)[(stmt*global_wk_sz0*global_wk_sz1*global_wk_sz2
+ + 
k*global_wk_sz0*global_wk_sz1 + j*global_wk_sz0 + i)*2 + 1];
+  if (!loop_num) continue;
+
+  PrintfFmt* ppf = NULL;
+  for (auto f : fmts) {
+if (f.second == printf_num) {
+  ppf = f;
+  break;
+}
+  }
+
+  PrintfFmt pf = *ppf;
+
   for (int n = 0; n  loop_num; n++) {
-for (PrintfFmt::iterator pfit = pf.begin(); pfit != pf.end(); 
++pfit) {
+for (vectorPrintfSlot::iterator pfit = pf.first.begin(); 
pfit != pf.first.end(); ++pfit) {
   PrintfSlot slot = *pfit;
   pf_str = ;
   int vec_num;
@@ -146,6 +160,7 @@ namespace gbe
 
   generatePrintfFmtString(*slot.state, pf_str);
 
+
   vec_num = slot.state-vector_n  0 ? slot.state-vector_n : 
1;
 
   for (int vec_i = 0; vec_i  vec_num; vec_i++) {
diff --git a/backend/src/ir/printf.hpp b/backend/src/ir/printf.hpp
index f6c6bcf..cbab759 100644
--- a/backend/src/ir/printf.hpp
+++ b/backend/src/ir/printf.hpp
@@ -198,7 +198,7 @@ namespace gbe
 }
   };
 
-  typedef vectorPrintfSlot PrintfFmt;
+  typedef std::pairvectorPrintfSlot, int PrintfFmt;
   uint32_t append(PrintfFmt* fmt, Unit unit);
 
   uint32_t getPrintfNum(void) const {
diff --git a/backend/src/llvm/llvm_printf_parser.cpp 
b/backend/src/llvm/llvm_printf_parser.cpp
index 8fec683..52da2e5 100644
--- a/backend/src/llvm/llvm_printf_parser.cpp
+++ b/backend/src/llvm/llvm_printf_parser.cpp
@@ -268,7 +268,7 @@ again:
 
   if (p != begin) {
 std::string s = std::string(begin, size_t(p - begin));
-printf_fmt-push_back(PrintfSlot(s.c_str()));
+printf_fmt-first.push_back(PrintfSlot(s.c_str()));
   }
 
   if (p == end) // finish
@@ -279,7 +279,7 @@ again:
   if (ret_char  0)
 goto error;
 
-  printf_fmt-push_back(state);
+  printf_fmt-first.push_back(state);
   num++;
 
   if (rend == end)
@@ -385,14 +385,14 @@ error:
 
 /
 /* calculate index address.
-   index_addr = (index_offset + wg_offset )* sizeof(int) + index_buf_ptr
+   index_addr = (index_offset + wg_offset )* sizeof(int) * 2 + 
index_buf_ptr
index_offset = global_size2 * global_size1 * global_size0 * printf_num 
*/
 
 Value* index_offset = builder-CreateMul(g1Xg2Xg3, ConstantInt::get(intTy, 
printf_num));
 // index_offset + offset
 op0 = builder-CreateAdd(index_offset, wg_offset);
-// (index_offset + offset)* sizeof(int)
-op0 = builder-CreateMul(op0, ConstantInt::get(intTy, sizeof(int)));
+// (index_offset + offset)* sizeof(int) * 2
+   

[Beignet] runtime_alloc_host_ptr_buffer intermittent failure

2015-01-30 Thread Rebecca N. Palmer
In beignet 1.0.1 on i5-3230M Debian 8 (libdrm 2.4.58, kernel 
3.16.7-ckt2-1 with the whitelist OpenCL registers patch), the recently 
added runtime_alloc_host_ptr_buffer (CL_MEM_ALLOC_HOST_PTR) test often 
fails when run as part of the whole set, but not when run by itself.


The backtrace points to this being a CL_MEM_OBJECT_ALLOCATION_FAILURE. 
(The error handling then calls cl_mem_delete on the incompletely-set-up 
buffer, which fails at assert(mem-ctx).)


$ gdb --args ./utest_run -a
[...]
runtime_use_host_ptr_buffer()[SUCCESS]
runtime_alloc_host_ptr_buffer()utest_run: 
/home/rnpalmer/Debian/builds/stackbuild/beignet/src/cl_mem.c:1073: 
cl_mem_delete: Assertion `mem-ctx' failed.


Program received signal SIGABRT, Aborted.
0x75560107 in raise () from /lib/x86_64-linux-gnu/libc.so.6
(gdb) bt full
#0  0x75560107 in raise () from /lib/x86_64-linux-gnu/libc.so.6
No symbol table info available.
#1  0x755614e8 in abort () from /lib/x86_64-linux-gnu/libc.so.6
No symbol table info available.
#2  0x75559226 in ?? () from /lib/x86_64-linux-gnu/libc.so.6
No symbol table info available.
#3  0x755592d2 in __assert_fail () from 
/lib/x86_64-linux-gnu/libc.so.6

No symbol table info available.
#4  0x77354be1 in cl_mem_delete (mem=0xf915a0)
at /home/rnpalmer/Debian/builds/stackbuild/beignet/src/cl_mem.c:1073
i = optimized out
mem = 0xf915a0
#5  0x77354e0b in cl_mem_allocate (type=optimized out,
ctx=optimized out, flags=optimized out, sz=1638400,
is_tiled=optimized out, host_ptr=optimized out, 
errcode=0x7fffd754)

at /home/rnpalmer/Debian/builds/stackbuild/beignet/src/cl_mem.c:339
bufmgr = optimized out
mem = optimized out
err = -4
alignment = optimized out
__PRETTY_FUNCTION__ = cl_mem_allocate
#6  0x77355189 in cl_mem_new_buffer (ctx=0x642d70, flags=16,
sz=1638400, data=0x0, errcode_ret=0x7fffd79c)
---Type return to continue, or q return to quit---
at /home/rnpalmer/Debian/builds/stackbuild/beignet/src/cl_mem.c:427
err = 0
mem = 0x642d70
max_mem_size = 1073741824
#7  0x773446cf in clCreateBuffer (context=optimized out,
flags=optimized out, size=optimized out, host_ptr=optimized out,
errcode_ret=0x7fffd7bc)
at /home/rnpalmer/Debian/builds/stackbuild/beignet/src/cl_api.c:489
mem = 0x0
err = 0
#8  0x7776f24c in runtime_alloc_host_ptr_buffer ()
at 
/home/rnpalmer/Debian/builds/stackbuild/beignet/utests/runtime_alloc_host_ptr_buffer.cpp:10

status = 0
__FUNCTION__ = runtime_alloc_host_ptr_buffer
mapptr = optimized out
#9  0x7776f60d in __ANON__runtime_alloc_host_ptr_buffer__ ()
at 
/home/rnpalmer/Debian/builds/stackbuild/beignet/utests/runtime_alloc_host_ptr_buffer.cpp:25

No locals.
#10 0x775a64e1 in UTest::runAll ()

___
Beignet mailing list
Beignet@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/beignet