remove lspci, gbe_bin_genenrater would generator llvm binary by default.
authorLuo <xionghu.luo@intel.com>
Tue, 8 Jul 2014 01:07:54 +0000 (09:07 +0800)
committerZhigang Gong <zhigang.gong@intel.com>
Tue, 8 Jul 2014 06:36:52 +0000 (14:36 +0800)
driver can get chipset id by ioctl instead of calling lspci in cmake;
user could generator gen binary by configuring cmake option
-DGEN_PCI_ID=xxxx or calling the gbe_bin_generater with option -t
GEN_PCI_ID.

v2: add "\0GENC" magic code for gen binary, fix typo.

Signed-off-by: Luo <xionghu.luo@intel.com>
Reviewed-by: Zhigang Gong <zhigang.gong@linux.intel.com>
backend/src/gbe_bin_generater.cpp
src/CMakeLists.txt
src/GetGenID.sh [deleted file]
src/cl_program.c
utests/CMakeLists.txt
utests/enqueue_built_in_kernels.cpp

index 925ba93..d9ae946 100644 (file)
@@ -156,21 +156,23 @@ void program_build_instance::serialize_program(void) throw(int)
 {
     ofstream ofs;
     ostringstream oss;
-    size_t sz, header_sz = 0;
+    size_t sz = 0, header_sz = 0;
     ofs.open(bin_path, ofstream::out | ofstream::trunc | ofstream::binary);
 
-    //add header to differeciate from llvm bitcode binary.
-    // (5 bytes: 1 byte for binary type, 4 byte for bc code.)
-    char header = '\0';
-
     if (str_fmt_out) {
-      OUTS_UPDATE_SZ(header);
-      OUTS_UPDATE_SZ(header);
-      OUTS_UPDATE_SZ(header);
-      OUTS_UPDATE_SZ(header);
-      OUTS_UPDATE_SZ(header);
 
-      string array_name = "Unkown_name_array";
+      if(gen_pci_id){
+        //add header to differeciate from llvm bitcode binary.
+        // (5 bytes: 1 byte for binary type, 4 byte for bc code, 'GENC' is for gen binary.)
+        char gen_header[6] = "\0GENC";
+        OUTS_UPDATE_SZ(gen_header[0]);
+        OUTS_UPDATE_SZ(gen_header[1]);
+        OUTS_UPDATE_SZ(gen_header[2]);
+        OUTS_UPDATE_SZ(gen_header[3]);
+        OUTS_UPDATE_SZ(gen_header[4]);
+      }
+
+      string array_name = "Unknown_name_array";
       unsigned long last_slash = bin_path.rfind("/");
       unsigned long last_dot = bin_path.rfind(".");
 
@@ -180,9 +182,15 @@ void program_build_instance::serialize_program(void) throw(int)
       ofs << "#include <stddef.h>" << "\n";
       ofs << "char " << array_name << "[] = {" << "\n";
 
-      sz = gbe_prog->serializeToBin(oss);
-
-      sz+=5;
+      if(gen_pci_id){
+        sz = gbe_prog->serializeToBin(oss);
+        sz += header_sz;
+      }else{
+        char *llvm_binary;
+        size_t bin_length = gbe_program_serialize_to_binary((gbe_program)gbe_prog, &llvm_binary, 1);
+        oss.write(llvm_binary, bin_length);
+        sz += bin_length;
+      }
 
       for (size_t i = 0; i < sz; i++) {
         unsigned char c = oss.str().c_str()[i];
@@ -191,18 +199,27 @@ void program_build_instance::serialize_program(void) throw(int)
         ofs << "0x";
         ofs << asic_str << ((i == sz - 1) ? "" : ", ");
       }
-
       ofs << "};\n";
 
       string array_size = array_name + "_size";
       ofs << "size_t " << array_size << " = " << sz << ";" << "\n";
     } else {
-      OUTF_UPDATE_SZ(header);
-      OUTF_UPDATE_SZ(header);
-      OUTF_UPDATE_SZ(header);
-      OUTF_UPDATE_SZ(header);
-      OUTF_UPDATE_SZ(header);
-      sz = gbe_prog->serializeToBin(ofs);
+      if(gen_pci_id){
+        //add header to differeciate from llvm bitcode binary.
+        // (5 bytes: 1 byte for binary type, 4 byte for bc code, 'GENC' is for gen binary.)
+        char gen_header[6] = "\0GENC";
+        OUTF_UPDATE_SZ(gen_header[0]);
+        OUTF_UPDATE_SZ(gen_header[1]);
+        OUTF_UPDATE_SZ(gen_header[2]);
+        OUTF_UPDATE_SZ(gen_header[3]);
+        OUTF_UPDATE_SZ(gen_header[4]);
+        sz = gbe_prog->serializeToBin(ofs);
+      }else{
+        char *llvm_binary;
+        size_t bin_length = gbe_program_serialize_to_binary((gbe_program)gbe_prog, &llvm_binary, 1);
+        ofs.write(llvm_binary, bin_length);
+        sz+=bin_length;
+      }
     }
 
     ofs.close();
@@ -215,15 +232,20 @@ void program_build_instance::serialize_program(void) throw(int)
 
 void program_build_instance::build_program(void) throw(int)
 {
-    // FIXME, we need to find a graceful way to generate internal binaries for difference
-    // devices.
-    gbe_program opaque = gbe_program_new_from_source(gen_pci_id, code, 0, build_opt.c_str(), NULL, NULL);
+    gbe_program  opaque = NULL;
+    if(gen_pci_id){
+      opaque = gbe_program_new_from_source(gen_pci_id, code, 0, build_opt.c_str(), NULL, NULL);
+    }else{
+      opaque = gbe_program_compile_from_source(0, code, NULL, 0, build_opt.c_str(), NULL, NULL);
+    }
     if (!opaque)
         throw FILE_BUILD_FAILED;
 
     gbe_prog = reinterpret_cast<gbe::Program*>(opaque);
 
-    assert(gbe_program_get_kernel_num(opaque));
+    if(gen_pci_id){
+      assert(gbe_program_get_kernel_num(opaque));
+    }
 }
 
 const char* program_build_instance::file_map_open(void) throw(int)
index 9b41932..45c83d4 100644 (file)
@@ -5,30 +5,24 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR}
                     ${CMAKE_CURRENT_SOURCE_DIR}/../include
                     ${MESA_SOURCE_INCLUDES})
 
-if (NOT GEN_PCI_ID)
-# lspci
-  Find_Program(LSPCI lspci)
-  IF(LSPCI)
-    MESSAGE(STATUS "Looking for lspci - found")
-  ELSE(LSPCI)
-    MESSAGE(FATAL_ERROR "Looking for lspci - not found")
-  ENDIF(LSPCI)
-  execute_process(COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/GetGenID.sh"
-    OUTPUT_VARIABLE GEN_PCI_ID)
-  message(STATUS "Platform Gen PCI id is " ${GEN_PCI_ID})
-  set(GEN_PCI_ID ${GEN_PCI_ID} PARENT_SCOPE)
-endif (NOT GEN_PCI_ID)
-
 macro (MakeKernelBinStr KERNEL_PATH KERNEL_FILES)
 foreach (KF ${KERNEL_FILES})
   set (input_file ${KERNEL_PATH}/${KF}.cl)
   set (output_file ${KERNEL_PATH}/${KF}_str.c)
   list (APPEND KERNEL_STR_FILES ${output_file})
-  add_custom_command(
-    OUTPUT ${output_file}
-    COMMAND rm -rf ${output_file}
-    COMMAND ${GBE_BIN_GENERATER} -s ${input_file} -o${output_file} -t${GEN_PCI_ID}
-    DEPENDS ${input_file} ${CMAKE_CURRENT_BINARY_DIR}/../backend/src/gbe_bin_generater)
+  if(GEN_PCI_ID)
+    add_custom_command(
+      OUTPUT ${output_file}
+      COMMAND rm -rf ${output_file}
+      COMMAND ${GBE_BIN_GENERATER} -s ${input_file} -o${output_file} -t${GEN_PCI_ID}
+      DEPENDS ${input_file} ${CMAKE_CURRENT_BINARY_DIR}/../backend/src/gbe_bin_generater)
+  else(GEN_PCI_ID)
+    add_custom_command(
+      OUTPUT ${output_file}
+      COMMAND rm -rf ${output_file}
+      COMMAND ${GBE_BIN_GENERATER} -s ${input_file} -o${output_file}
+      DEPENDS ${input_file} ${CMAKE_CURRENT_BINARY_DIR}/../backend/src/gbe_bin_generater)
+  endif(GEN_PCI_ID)
 endforeach (KF)
 endmacro (MakeKernelBinStr)
 
diff --git a/src/GetGenID.sh b/src/GetGenID.sh
deleted file mode 100755 (executable)
index 7acf9bd..0000000
+++ /dev/null
@@ -1,26 +0,0 @@
-#!/bin/bash
-genpciid=(0152 0162 0156 0166 015a 016a 0f31 0402 0412 0422 040a 041a 042a 0406 0416 0426 0c02 0c12 0c22 0c0a 0c1a 0c2a 0c06 0c16 0c26 0a02 0a12 0a22 0a0a 0a1a 0a2a 0a06 0a16 0a26 0d02 0d12 0d22 0d0a 0d1a 0d2a 0d06 0d16 0d26)
-pciid=($(lspci -nn | grep "\[8086:.*\]" -o | awk -F : '{print $2}' | awk -F ] '{print $1}'))
-n=${#pciid[*]}
-i=0
-m=${#genpciid[*]}
-j=0
-while [ $i -lt $n ]
-do
-    id1=${pciid[$i]}
-    let j=0
-
-    while [ $j -lt $m ]
-    do
-       id2=${genpciid[$j]}
-
-       if [ ${id1} == ${id2} ]
-       then
-           echo ${id1}
-           exit 0
-       fi
-       let j=j+1
-    done
-
-    let i=i+1
-done
index d7e9ae7..3fc2212 100644 (file)
@@ -236,6 +236,7 @@ cl_program_create_from_binary(cl_context             ctx,
       err = CL_INVALID_PROGRAM;
       goto error;
     }
+    program->source_type = FROM_LLVM;
   }
 
   if (binary_status)
@@ -267,17 +268,16 @@ cl_program_create_with_built_in_kernles(cl_context     ctx,
   INVALID_DEVICE_IF (devices == NULL);
   INVALID_DEVICE_IF (devices[0] != ctx->device);
 
+  cl_int binary_status = CL_SUCCESS;
   extern char cl_internal_built_in_kernel_str[];
   extern size_t cl_internal_built_in_kernel_str_size;
   char* p_built_in_kernel_str =cl_internal_built_in_kernel_str;
-  cl_int binary_status = CL_SUCCESS;
 
   ctx->built_in_prgs = cl_program_create_from_binary(ctx, 1,
                                                           &ctx->device,
                                                           (size_t*)&cl_internal_built_in_kernel_str_size,
                                                           (const unsigned char **)&p_built_in_kernel_str,
                                                           &binary_status, &err);
-
   if (!ctx->built_in_prgs)
     return NULL;
 
@@ -462,7 +462,22 @@ cl_program_build(cl_program p, const char *options)
 
     /* Create all the kernels */
     TRY (cl_program_load_gen_program, p);
-    p->source_type = FROM_LLVM;
+  } else if (p->source_type == FROM_LLVM) {
+    if (!CompilerSupported()) {
+      err = CL_COMPILER_NOT_AVAILABLE;
+      goto error;
+    }
+
+    compiler_program_build_from_llvm(p->opaque, p->build_log_max_sz, p->build_log, &p->build_log_sz, options);
+    if (UNLIKELY(p->opaque == NULL)) {
+      if (p->build_log_sz > 0 && strstr(p->build_log, "error: error reading 'options'"))
+        err = CL_INVALID_BUILD_OPTIONS;
+      else
+        err = CL_BUILD_PROGRAM_FAILURE;
+      goto error;
+    }
+    /* Create all the kernels */
+    TRY (cl_program_load_gen_program, p);
   } else if (p->source_type == FROM_BINARY) {
     p->opaque = interp_program_new_from_binary(p->ctx->device->vendor_id, p->binary, p->binary_sz);
     if (UNLIKELY(p->opaque == NULL)) {
@@ -472,7 +487,6 @@ cl_program_build(cl_program p, const char *options)
 
     /* Create all the kernels */
     TRY (cl_program_load_gen_program, p);
-    p->source_type = FROM_LLVM;
   }
   p->binary_type = CL_PROGRAM_BINARY_TYPE_EXECUTABLE;
 
index d786a45..3614c57 100644 (file)
@@ -187,11 +187,18 @@ set (utests_sources
   utest_helper.cpp)
 
 SET (kernel_bin ${CMAKE_CURRENT_SOURCE_DIR}/../kernels/compiler_ceil)
-ADD_CUSTOM_COMMAND(
-    OUTPUT ${kernel_bin}.bin
-    COMMAND ${GBE_BIN_GENERATER} ${kernel_bin}.cl -o${kernel_bin}.bin -t${GEN_PCI_ID}
-    DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/../backend/src/gbe_bin_generater ${kernel_bin}.cl
-    )
+
+if(GEN_PCI_ID)
+  ADD_CUSTOM_COMMAND(
+  OUTPUT ${kernel_bin}.bin
+  COMMAND ${GBE_BIN_GENERATER} ${kernel_bin}.cl -o${kernel_bin}.bin -t${GEN_PCI_ID}
+  DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/../backend/src/gbe_bin_generater ${kernel_bin}.cl)
+else(GEN_PCI_ID)
+  ADD_CUSTOM_COMMAND(
+  OUTPUT ${kernel_bin}.bin
+  COMMAND ${GBE_BIN_GENERATER} ${kernel_bin}.cl -o${kernel_bin}.bin
+  DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/../backend/src/gbe_bin_generater ${kernel_bin}.cl)
+endif(GEN_PCI_ID)
 
 ADD_CUSTOM_TARGET(kernel_bin.bin
     DEPENDS ${kernel_bin}.bin)
index 8b47bca..52b8848 100644 (file)
@@ -14,7 +14,6 @@ void enqueue_built_in_kernels(void)
   OCL_ASSERT(ret_sz == built_in_kernels_size);
   cl_program built_in_prog = clCreateProgramWithBuiltInKernels(ctx, 1, &device, built_in_kernel_names, &err);
   OCL_ASSERT(built_in_prog != NULL);
-
 }
 
 MAKE_UTEST_FROM_FUNCTION(enqueue_built_in_kernels);