[Beignet] [PATCH 5/9 newRT] Add compiler API functions.

junyan.he at inbox.com junyan.he at inbox.com
Sat Apr 1 09:43:30 UTC 2017


From: Junyan He <junyan.he at intel.com>

We will split the compiler with runtime. The runtime will
call the compiler using standard Build, Compile, and Link
API to generate ELF, IR Bitcode. The file implements all
these APIs

Signed-off-by: Junyan He <junyan.he at intel.com>
---
 CMakeLists.txt                       |  2 +-
 backend/src/backend/compiler_api.cpp | 34 ++++++++++++++++++++++++----------
 2 files changed, 25 insertions(+), 11 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index e6babe4..fe895d0 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -239,7 +239,7 @@ IF (EXPERIMENTAL_DOUBLE)
   ADD_DEFINITIONS(-DENABLE_FP64)
 ENDIF(EXPERIMENTAL_DOUBLE)
 
-SET(CAN_OPENCL_20 ON)
+SET(CAN_OPENCL_20 OFF)
 IF (CMAKE_SIZEOF_VOID_P EQUAL 4)
   SET(CAN_OPENCL_20 OFF)
 ENDIF (CMAKE_SIZEOF_VOID_P EQUAL 4)
diff --git a/backend/src/backend/compiler_api.cpp b/backend/src/backend/compiler_api.cpp
index 98f5d0b..a9aac9d 100644
--- a/backend/src/backend/compiler_api.cpp
+++ b/backend/src/backend/compiler_api.cpp
@@ -29,8 +29,9 @@
 #include "clang/Frontend/CompilerInstance.h"
 #include "clang/CodeGen/CodeGenAction.h"
 
-#include "GBEConfig.h"
+#include "src/GBEConfig.h"
 #include "backend/gen_program.hpp"
+#include "llvm/llvm_to_gen.hpp"
 #include "sys/cvar.hpp"
 
 #include <sstream>
@@ -52,7 +53,16 @@ loadProgramFromLLVMIRBinary(uint32_t deviceID, const char *binary, size_t size)
 {
   std::string binary_content;
   //the first byte stands for binary_type.
-  binary_content.assign(binary, size);
+  if (binary[0] == 'L' && binary[1] == 'I' && binary[2] == 'B' &&
+      binary[3] == 'B' && binary[4] == 'C' &&
+      binary[5] == (char)0xC0 && binary[6] == (char)0xDE) {
+    binary_content.assign(binary + 3, size - 3);
+  } else if (binary[0] == 'B' && binary[1] == 'C' &&
+             binary[2] == (char)0xC0 && binary[3] == (char)0xDE) {
+    binary_content.assign(binary, size);
+  } else
+    return NULL;
+
   llvm::StringRef llvm_bin_str(binary_content);
 #if LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 9
   llvm::LLVMContext &c = GBEGetLLVMContext();
@@ -735,17 +745,11 @@ GenLinkProgram(uint32_t deviceID, int binary_num, const char **binaries, size_t
     }
 
     if (link_ret == true) { //error happened
-      if (mod) {
-        delete mod;
-        mod = NULL;
-      }
       ret = false;
       break;
     }
 
     assert(mod != NULL);
-    delete mod;
-    mod = NULL;
   }
 
   if (ret == true) {
@@ -754,9 +758,12 @@ GenLinkProgram(uint32_t deviceID, int binary_num, const char **binaries, size_t
       llvm::raw_string_ostream ostream(irBuf);
       llvm::WriteBitcodeToFile(target_module, ostream);
       ostream.flush();
-      *retBinarySize = irBuf.capacity();
+      *retBinarySize = irBuf.capacity() + 3; // For add 'L' 'I' 'B'
       *retBinary = static_cast<char *>(::malloc(*retBinarySize));
-      ::memcpy(*retBinary, irBuf.c_str(), *retBinarySize);
+      (*retBinary)[0] = 'L';
+      (*retBinary)[1] = 'I';
+      (*retBinary)[2] = 'B';
+      ::memcpy(*retBinary + 3, irBuf.c_str(), *retBinarySize - 3);
     } else {
       size_t clangErrSize = *errRetSize;
 
@@ -828,6 +835,13 @@ GenCheckCompilerOption(const char *option)
     else
       s.erase(pos, pos2 - pos);
   }
+
+  // -cl-no-signed-zeros is not supported, and some verion can not recognize it
+  pos = s.find("-cl-no-signed-zeros");
+  if (pos != std::string::npos) {
+    s.erase(pos, strlen("-cl-no-signed-zeros"));
+  }
+
   args.push_back(s.c_str());
 
   // The compiler invocation needs a DiagnosticsEngine so it can report problems
-- 
2.7.4



More information about the Beignet mailing list