[Beignet] [PATCH 2/2] utests: Added unit test to test LLVM and ASM dump generation in a two step build process with clCompile and clLink APIs.

Manasi Navare manasi.d.navare at intel.com
Thu Oct 1 04:35:26 PDT 2015


    This patch adds a new test to the unit tests. It uses the existing
    framework and data structures and tests the llvm/asm dump generation
    when these flags (-dump-opt-llvm, -dump-opt-asm) are passed as compile and link
    options to clCompileProgram and clLinkProgram APIs along with the dump file names.

    Method added:
    1) get_compile_link_llvm_asm_info() tests LLVM dump generation after clCompileProgram() stage
       and Gen ASM dump generation after clLinkProgram stage

Signed-off-by: Manasi Navare <manasi.d.navare at intel.com>
---
 utests/get_cl_info.cpp  | 61 +++++++++++++++++++++++++++++++++++++++++++++++++
 utests/utest_helper.cpp | 61 +++++++++++++++++++++++++++++++++++++++++++++++++
 utests/utest_helper.hpp |  2 ++
 3 files changed, 124 insertions(+)

diff --git a/utests/get_cl_info.cpp b/utests/get_cl_info.cpp
index 7c03d95..4513b33 100644
--- a/utests/get_cl_info.cpp
+++ b/utests/get_cl_info.cpp
@@ -470,6 +470,67 @@ void get_build_asm_info(void)
 
 MAKE_UTEST_FROM_FUNCTION(get_build_asm_info);
 
+void get_compile_link_llvm_asm_info(void)
+{
+    map<cl_program_info, void *> maps;
+    cl_build_status expect_status;
+    char llvm_file[] = "test_llvm_dump.txt";
+    char asm_file[] = "test_asm_dump.txt";
+    char compile_opt[] = "-dump-opt-llvm=test_llvm_dump.txt";
+    char link_opt[] = "-dump-opt-asm=test_asm_dump.txt";
+    FILE *fp = NULL;
+    int sz;
+
+    //Remove any pre-existing file
+    if( (fp = fopen(llvm_file, "r")) != NULL) {
+        fclose(fp);
+        std::remove(llvm_file);
+    }
+    if( (fp = fopen(asm_file, "r")) != NULL) {
+        fclose(fp);
+        std::remove(asm_file);
+    }
+
+    OCL_CALL (cl_kernel_compile_link, "compiler_if_else.cl", "compiler_if_else", compile_opt, link_opt);
+
+    /* Do our test.*/
+    expect_status = CL_BUILD_SUCCESS;
+    maps.insert(make_pair(CL_PROGRAM_BUILD_STATUS,
+                          (void *)(new Info_Result<cl_build_status>(expect_status))));
+  
+
+    for (map<cl_program_info, void *>::iterator x = maps.begin(); x != maps.end(); ++x) {
+        switch (x->first) {
+        case CL_PROGRAM_BUILD_STATUS:
+            CALL_PROG_BUILD_INFO_AND_RET(cl_build_status);
+            break;
+        case CL_PROGRAM_BUILD_OPTIONS:
+            CALL_PROG_BUILD_INFO_AND_RET(char *);
+            break;
+        default:
+            break;
+        }
+    }
+
+    //Test is successful if the backend created the file
+    if( (fp = fopen(llvm_file, "r")) == NULL) {
+        std::cout << "LLVM file creation.. FAILED";
+        OCL_ASSERT(0);
+    } else {
+        fclose(fp);
+        std::cout << "LLVM file created.. SUCCESS";
+    }
+    if( (fp = fopen(asm_file, "r")) == NULL) {
+        std::cout << "ASM file creation.. FAILED";
+        OCL_ASSERT(0);
+    } else {
+        fclose(fp);
+        std::cout << "ASM file created.. SUCCESS";
+    }
+}
+
+MAKE_UTEST_FROM_FUNCTION(get_compile_link_llvm_asm_info);
+
 
 /* ***************************************************** *
  * clGetContextInfo                                      *
diff --git a/utests/utest_helper.cpp b/utests/utest_helper.cpp
index 8f772fd..5a7396f 100644
--- a/utests/utest_helper.cpp
+++ b/utests/utest_helper.cpp
@@ -286,6 +286,67 @@ error:
   goto exit;
 }
 
+int
+cl_kernel_compile_link(const char *file_name, const char *kernel_name, const char * compile_opt, const char * link_opt)
+{
+  cl_file_map_t *fm = NULL;
+  char *ker_path = NULL;
+  cl_int status = CL_SUCCESS;
+  static const char *prevFileName = NULL;
+  cl_int err;
+
+  /* Load the program and build it */
+  if (!program || (program && (!prevFileName || strcmp(prevFileName, file_name)))) {
+    if (program) clReleaseProgram(program);
+    ker_path = cl_do_kiss_path(file_name, device);
+    cl_file_map_t *fm = cl_file_map_new();
+    FATAL_IF (cl_file_map_open(fm, ker_path) != CL_FILE_MAP_SUCCESS,
+                "Failed to open file \"%s\" with kernel \"%s\". Did you properly set OCL_KERNEL_PATH variable?",
+                file_name, kernel_name);
+    const char *src = cl_file_map_begin(fm);
+    const size_t sz = cl_file_map_size(fm);
+    program = clCreateProgramWithSource(ctx, 1, &src, &sz, &status);
+    cl_file_map_delete(fm);
+    
+    if (status != CL_SUCCESS) {
+      fprintf(stderr, "error calling clCreateProgramWithBinary\n");
+      goto error;
+    }
+    prevFileName = file_name;
+
+    OCL_CALL (clCompileProgram, program,
+                                1, &device, // num_devices & device_list
+                                compile_opt, // compile_options
+                                0, // num_input_headers
+                                NULL,
+                                NULL,
+                                NULL, NULL);
+   OCL_ASSERT(err==CL_SUCCESS);
+  cl_program input_programs[1] = {program};
+  program = clLinkProgram(ctx, 1, &device, link_opt, 1, input_programs, NULL, NULL, &err);
+  OCL_ASSERT(program != NULL);
+  OCL_ASSERT(err == CL_SUCCESS);
+  }
+  
+  /* Create a kernel from the program */
+  if (kernel)
+    clReleaseKernel(kernel);
+  kernel = clCreateKernel(program, kernel_name, &status);
+  if (status != CL_SUCCESS) {
+    fprintf(stderr, "error calling clCreateKernel\n");
+    goto error;
+  }
+
+exit:
+  free(ker_path);
+  cl_file_map_delete(fm);
+  return status;
+error:
+  prevFileName = NULL;
+  goto exit;
+}
+
+
 #define GET_PLATFORM_STR_INFO(LOWER_NAME, NAME) \
   { \
     size_t param_value_size; \
diff --git a/utests/utest_helper.hpp b/utests/utest_helper.hpp
index 3b17606..1a9eb73 100644
--- a/utests/utest_helper.hpp
+++ b/utests/utest_helper.hpp
@@ -194,6 +194,8 @@ extern int cl_ocl_init(void);
 /* Init program and kernel for the test */
 extern int cl_kernel_init(const char *file_name,
                 const char *kernel_name, int format, const char * build_opt);
+extern int cl_kernel_compile_link(const char *file_name, const char *kernel_name, 
+                const char * compile_opt, const char * link_opt);
 
 /* Get the file path */
 extern char* cl_do_kiss_path(const char *file, cl_device_id device);
-- 
1.9.1



More information about the Beignet mailing list