From ff4eaa52340ab666c0fb5f8b2476aaf855422a96 Mon Sep 17 00:00:00 2001 From: Luo Date: Fri, 27 Jun 2014 08:27:03 +0800 Subject: add the usage of link program from llvm binary. user A could compile and link kernel source to llvm binary first, then query the binary to save to file; With the binary, user B can call clCreateProgramWithBinary without compile the source again. this usage could protect those who need to protect the kernel source. Signed-off-by: Luo Reviewed-by: Zhigang Gong --- utests/runtime_compile_link.cpp | 41 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) (limited to 'utests/runtime_compile_link.cpp') diff --git a/utests/runtime_compile_link.cpp b/utests/runtime_compile_link.cpp index 8aeea31a..4a39b6a7 100644 --- a/utests/runtime_compile_link.cpp +++ b/utests/runtime_compile_link.cpp @@ -67,12 +67,47 @@ void runtime_compile_link(void) OCL_ASSERT(err==CL_SUCCESS); cl_program input_programs[2] = { program_A, program_B}; - cl_program linked_program = clLinkProgram(ctx, 0, NULL, NULL, 2, input_programs, NULL, NULL, &err); - + cl_program linked_program = clLinkProgram(ctx, 0, NULL, "-create-library", 2, input_programs, NULL, NULL, &err); OCL_ASSERT(linked_program != NULL); OCL_ASSERT(err == CL_SUCCESS); + size_t binarySize; + unsigned char *binary; + + // Get the size of the resulting binary (only one device) + err= clGetProgramInfo( linked_program, CL_PROGRAM_BINARY_SIZES, sizeof( binarySize ), &binarySize, NULL ); + OCL_ASSERT(err==CL_SUCCESS); + + // Create a buffer and get the actual binary + binary = (unsigned char*)malloc(sizeof(unsigned char)*binarySize); + if (binary == NULL) { + OCL_ASSERT(0); + return ; + } + + unsigned char *buffers[ 1 ] = { binary }; + // Do another sanity check here first + size_t size; + cl_int loadErrors[ 1 ]; + err = clGetProgramInfo( linked_program, CL_PROGRAM_BINARIES, 0, NULL, &size ); + OCL_ASSERT(err==CL_SUCCESS); + if( size != sizeof( buffers ) ){ + free(binary); + return ; + } + + err = clGetProgramInfo( linked_program, CL_PROGRAM_BINARIES, sizeof( buffers ), &buffers, NULL ); + OCL_ASSERT(err==CL_SUCCESS); + + cl_device_id deviceID; + err = clGetProgramInfo( linked_program, CL_PROGRAM_DEVICES, sizeof( deviceID), &deviceID, NULL ); + OCL_ASSERT(err==CL_SUCCESS); + + cl_program program_with_binary = clCreateProgramWithBinary(ctx, 1, &deviceID, &binarySize, (const unsigned char**)buffers, loadErrors, &err); + OCL_ASSERT(err==CL_SUCCESS); + cl_program new_linked_program = clLinkProgram(ctx, 1, &deviceID, NULL, 1, &program_with_binary, NULL, NULL, &err); + OCL_ASSERT(err==CL_SUCCESS); // link success, run this kernel. const size_t n = 16; @@ -104,7 +139,7 @@ void runtime_compile_link(void) OCL_UNMAP_BUFFER(0); OCL_UNMAP_BUFFER(1); - kernel = clCreateKernel(linked_program, "runtime_compile_link_a", &err); + kernel = clCreateKernel(new_linked_program, "runtime_compile_link_a", &err); OCL_ASSERT(err == CL_SUCCESS); -- cgit v1.2.1