#include "llvm/llvm_to_gen.hpp"
#include <cstring>
+#include <sstream>
#include <memory>
#include <iostream>
#include <fstream>
}
static gbe_program genProgramNewFromBinary(const char *binary, size_t size) {
- NOT_IMPLEMENTED;
- return NULL;
+ using namespace gbe;
+ std::string binary_content;
+ binary_content.assign(binary, size);
+ GenProgram *program = GBE_NEW_NO_ARG(GenProgram);
+ std::istringstream ifs(binary_content, std::ostringstream::binary);
+
+ if (!program->deserializeFromBin(ifs)) {
+ delete program;
+ return NULL;
+ }
+
+ //program->printStatus(0, std::cout);
+ return reinterpret_cast<gbe_program>(program);
}
static gbe_program genProgramNewFromLLVM(const char *fileName,
/* TODO support create program from binary */
assert(program->source_type == FROM_LLVM ||
- program->source_type == FROM_SOURCE);
+ program->source_type == FROM_SOURCE ||
+ program->source_type == FROM_BINARY);
if((err = cl_program_build(program, options)) != CL_SUCCESS) {
goto error;
}
}
}
+static void
+cl_program_release_binary(cl_program p)
+{
+ if (p->binary) {
+ cl_free(p->binary);
+ p->binary = NULL;
+ }
+}
+
LOCAL void
cl_program_delete(cl_program p)
{
/* We are not done with it yet */
if ((ref = atomic_dec(&p->ref_n)) > 1) return;
- /* Destroy the sources if still allocated */
+ /* Destroy the sources and binary if still allocated */
cl_program_release_sources(p);
+ cl_program_release_binary(p);
/* Release the build options. */
if (p->build_opts) {
cl_int * binary_status,
cl_int * errcode_ret)
{
-#if 0
cl_program program = NULL;
cl_int err = CL_SUCCESS;
goto error;
}
- // TRY_ALLOC (program, cl_program_new(ctx, (const char *) binaries[0], lengths[0]));
+ program = cl_program_new(ctx);
+
+ // TODO: Need to check the binary format here to return CL_INVALID_BINARY.
+ TRY_ALLOC(program->binary, cl_calloc(lengths[0], sizeof(char)));
+ memcpy(program->binary, binaries[0], lengths[0]);
+ program->binary_sz = lengths[0];
+ program->source_type = FROM_BINARY;
+
+ if (binary_status)
+ binary_status[0] = CL_SUCCESS;
exit:
if (errcode_ret)
cl_program_delete(program);
program = NULL;
goto exit;
-#endif
- NOT_IMPLEMENTED;
+
return CL_SUCCESS;
}
/* Create all the kernels */
TRY (cl_program_load_gen_program, p);
p->source_type = FROM_LLVM;
+ } else if (p->source_type == FROM_BINARY) {
+ p->opaque = gbe_program_new_from_binary(p->binary, p->binary_sz);
+ if (UNLIKELY(p->opaque == NULL)) {
+ err = CL_INVALID_PROGRAM;
+ goto error;
+ }
+
+ /* Create all the kernels */
+ TRY (cl_program_load_gen_program, p);
+ p->source_type = FROM_LLVM;
}
for (i = 0; i < p->ker_n; i ++) {
char *bin; /* The program copied verbatim */
size_t bin_sz; /* Its size in memory */
char *source; /* Program sources */
+ char *binary; /* Program binary. */
+ size_t binary_sz; /* The binary size. */
uint32_t ker_n; /* Number of declared kernels */
uint32_t source_type:2; /* Built from binary, source or LLVM */
uint32_t is_built:1; /* Did we call clBuildProgram on it? */