[Beignet] [PATCH 2/3] Add llvm3.6 build support.

Yang Rong rong.r.yang at intel.com
Thu Feb 12 00:29:40 PST 2015


There are some changes from llvm3.5:
1. Some functions return std::unique_ptr instead of pointer.
2. MetaNode to Value and Value to MetaNode.

V2: Fix llvm3.5 build error.
V3: Print link and function materialize message.
Signed-off-by: Yang Rong <rong.r.yang at intel.com>
---
 backend/src/backend/gen_program.cpp    | 10 ++++++++++
 backend/src/backend/program.cpp        |  4 ++++
 backend/src/llvm/llvm_bitcode_link.cpp | 25 ++++++++++++++++++++++++-
 backend/src/llvm/llvm_gen_backend.cpp  | 22 +++++++++++++++++++++-
 backend/src/llvm/llvm_passes.cpp       |  4 ++++
 backend/src/llvm/llvm_to_gen.cpp       | 16 +++++++++++++---
 backend/src/llvm/llvm_unroll.cpp       | 14 ++++++++++++++
 7 files changed, 90 insertions(+), 5 deletions(-)

diff --git a/backend/src/backend/gen_program.cpp b/backend/src/backend/gen_program.cpp
index a4019fe..65a7ba2 100644
--- a/backend/src/backend/gen_program.cpp
+++ b/backend/src/backend/gen_program.cpp
@@ -252,9 +252,15 @@ namespace gbe {
     llvm::StringRef llvm_bin_str(binary_content);
     llvm::LLVMContext& c = llvm::getGlobalContext();
     llvm::SMDiagnostic Err;
+#if LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 6
+    std::unique_ptr<llvm::MemoryBuffer> memory_buffer = llvm::MemoryBuffer::getMemBuffer(llvm_bin_str, "llvm_bin_str");
+    acquireLLVMContextLock();
+    llvm::Module* module = llvm::parseIR(memory_buffer->getMemBufferRef(), Err, c).release();
+#else
     llvm::MemoryBuffer* memory_buffer = llvm::MemoryBuffer::getMemBuffer(llvm_bin_str, "llvm_bin_str");
     acquireLLVMContextLock();
     llvm::Module* module = llvm::ParseIR(memory_buffer, Err, c);
+#endif
     releaseLLVMContextLock();
     if(module == NULL){
       GBE_ASSERT(0);
@@ -382,7 +388,11 @@ namespace gbe {
       llvm::Module* src = (llvm::Module*)((GenProgram*)src_program)->module;
       llvm::Module* dst = (llvm::Module*)((GenProgram*)dst_program)->module;
 
+#if LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR <= 5
       if (LLVMLinkModules(wrap(dst), wrap(src), LLVMLinkerPreserveSource, &errMsg)) {
+#else
+      if (LLVMLinkModules(wrap(dst), wrap(src), 0, &errMsg)) {
+#endif
         if (err != NULL && errSize != NULL && stringSize > 0u) {
           if(strlen(errMsg) < stringSize )
             stringSize = strlen(errMsg);
diff --git a/backend/src/backend/program.cpp b/backend/src/backend/program.cpp
index 38ce9c8..06810bd 100644
--- a/backend/src/backend/program.cpp
+++ b/backend/src/backend/program.cpp
@@ -621,7 +621,11 @@ namespace gbe {
     if (!retVal)
       return false;
 
+#if LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR <= 5
     llvm::Module *module = Act->takeModule();
+#else
+    llvm::Module *module = Act->takeModule().release();
+#endif
 
     *out_module = module;
     return true;
diff --git a/backend/src/llvm/llvm_bitcode_link.cpp b/backend/src/llvm/llvm_bitcode_link.cpp
index 8eb6dd5..229e3bb 100644
--- a/backend/src/llvm/llvm_bitcode_link.cpp
+++ b/backend/src/llvm/llvm_bitcode_link.cpp
@@ -63,7 +63,11 @@ namespace gbe
     }
     assert(findBC);
 
+#if LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR <= 5
     oclLib = getLazyIRFileModule(FilePath, Err, ctx);
+#else
+    oclLib = getLazyIRFileModule(FilePath, Err, ctx).release();
+#endif
     if (!oclLib) {
       printf("Fatal Error: ocl lib can not be opened\n");
       return NULL;
@@ -114,12 +118,18 @@ namespace gbe
 
         std::string ErrInfo;// = "Not Materializable";
         if (!fromSrc && newMF->isMaterializable()) {
+#if LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR <= 5
           if (newMF->Materialize(&ErrInfo)) {
             printf("Can not materialize the function: %s, because %s\n", fnName.c_str(), ErrInfo.c_str());
             return false;
           }
+#else
+          if (std::error_code EC = newMF->materialize()) {
+            printf("Can not materialize the function: %s, because %s\n", fnName.c_str(), EC.message().c_str());
+            return false;
+          }
+#endif
         }
-
         if (!materializedFuncCall(src, lib, *newMF, MFS))
           return false;
 
@@ -205,12 +215,21 @@ namespace gbe
       }
       std::string ErrInfo;// = "Not Materializable";
       if (newMF->isMaterializable()) {
+#if LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR <= 5
         if (newMF->Materialize(&ErrInfo)) {
           printf("Can not materialize the function: %s, because %s\n", fnName.c_str(), ErrInfo.c_str());
           delete clonedLib;
           return NULL;
         }
       }
+#else
+        if (std::error_code EC = newMF->materialize()) {
+          printf("Can not materialize the function: %s, because %s\n", fnName.c_str(), EC.message().c_str();
+          delete clonedLib;
+          return NULL;
+        }
+      }
+#endif
 
       if (!materializedFuncCall(*mod, *clonedLib, *newMF, materializedFuncs)) {
         delete clonedLib;
@@ -223,7 +242,11 @@ namespace gbe
     /* We use beignet's bitcode as dst because it will have a lot of
        lazy functions which will not be loaded. */
     char* errorMsg;
+#if LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR <= 5
     if(LLVMLinkModules(wrap(clonedLib), wrap(mod), LLVMLinkerDestroySource, &errorMsg)) {
+#else
+    if(LLVMLinkModules(wrap(clonedLib), wrap(mod), 0, &errorMsg)) {
+#endif
       delete clonedLib;
       printf("Fatal Error: link the bitcode error:\n%s\n", errorMsg);
       return NULL;
diff --git a/backend/src/llvm/llvm_gen_backend.cpp b/backend/src/llvm/llvm_gen_backend.cpp
index d47721a..c67a880 100644
--- a/backend/src/llvm/llvm_gen_backend.cpp
+++ b/backend/src/llvm/llvm_gen_backend.cpp
@@ -1467,7 +1467,12 @@ error:
     /* First find the meta data belong to this function. */
     for(uint i = 0; i < clKernelMetaDatas->getNumOperands(); i++) {
       node = clKernelMetaDatas->getOperand(i);
+#if LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR <= 5
       if (node->getOperand(0) == &F) break;
+#else
+      auto *V = cast<ValueAsMetadata>(node->getOperand(0));
+      if (V && V->getValue() == &F) break;
+#endif
       node = NULL;
     }
 
@@ -1484,9 +1489,15 @@ error:
 
       if (attrName->getString() == "reqd_work_group_size") {
         GBE_ASSERT(attrNode->getNumOperands() == 4);
+#if LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR <= 5
         ConstantInt *x = dyn_cast<ConstantInt>(attrNode->getOperand(1));
         ConstantInt *y = dyn_cast<ConstantInt>(attrNode->getOperand(2));
         ConstantInt *z = dyn_cast<ConstantInt>(attrNode->getOperand(3));
+#else
+        ConstantInt *x = mdconst::extract<ConstantInt>(attrNode->getOperand(1));
+        ConstantInt *y = mdconst::extract<ConstantInt>(attrNode->getOperand(2));
+        ConstantInt *z = mdconst::extract<ConstantInt>(attrNode->getOperand(3));
+#endif
         GBE_ASSERT(x && y && z);
         reqd_wg_sz[0] = x->getZExtValue();
         reqd_wg_sz[1] = y->getZExtValue();
@@ -1521,9 +1532,15 @@ error:
         functionAttributes += " ";
       } else if (attrName->getString() == "work_group_size_hint") {
         GBE_ASSERT(attrNode->getNumOperands() == 4);
+#if LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR <= 5
         ConstantInt *x = dyn_cast<ConstantInt>(attrNode->getOperand(1));
         ConstantInt *y = dyn_cast<ConstantInt>(attrNode->getOperand(2));
         ConstantInt *z = dyn_cast<ConstantInt>(attrNode->getOperand(3));
+#else
+        ConstantInt *x = mdconst::extract<ConstantInt>(attrNode->getOperand(1));
+        ConstantInt *y = mdconst::extract<ConstantInt>(attrNode->getOperand(2));
+        ConstantInt *z = mdconst::extract<ConstantInt>(attrNode->getOperand(3));
+#endif
         GBE_ASSERT(x && y && z);
         hint_wg_sz[0] = x->getZExtValue();
         hint_wg_sz[1] = y->getZExtValue();
@@ -1561,8 +1578,11 @@ error:
       for (; I != E; ++I, ++argID) {
         const std::string &argName = I->getName().str();
         Type *type = I->getType();
-
+#if LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR <= 5
         llvmInfo.addrSpace = (cast<ConstantInt>(addrSpaceNode->getOperand(1 + argID)))->getZExtValue();
+#else
+        llvmInfo.addrSpace = (mdconst::extract<ConstantInt>(addrSpaceNode->getOperand(1 + argID)))->getZExtValue();
+#endif
         llvmInfo.typeName = (cast<MDString>(typeNameNode->getOperand(1 + argID)))->getString();
         llvmInfo.accessQual = (cast<MDString>(accessQualNode->getOperand(1 + argID)))->getString();
         llvmInfo.typeQual = (cast<MDString>(typeQualNode->getOperand(1 + argID)))->getString();
diff --git a/backend/src/llvm/llvm_passes.cpp b/backend/src/llvm/llvm_passes.cpp
index 5c0a2e0..1b0e4f8 100644
--- a/backend/src/llvm/llvm_passes.cpp
+++ b/backend/src/llvm/llvm_passes.cpp
@@ -119,7 +119,11 @@ namespace gbe
       uint32_t ops = md.getNumOperands();
       for(uint32_t x = 0; x < ops; x++) {
         MDNode* node = md.getOperand(x);
+#if LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR <= 5
         Value * op = node->getOperand(0);
+#else
+        Value * op = cast<ValueAsMetadata>(node->getOperand(0))->getValue();
+#endif
         if(op == &F) bKernel = true;
       }
     }
diff --git a/backend/src/llvm/llvm_to_gen.cpp b/backend/src/llvm/llvm_to_gen.cpp
index c2c015a..b1dc686 100644
--- a/backend/src/llvm/llvm_to_gen.cpp
+++ b/backend/src/llvm/llvm_to_gen.cpp
@@ -81,7 +81,9 @@ namespace gbe
   {
     FunctionPassManager FPM(&mod);
 
-#if LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 5
+#if LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 6
+    FPM.add(new DataLayoutPass());
+#elif LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR == 5
     FPM.add(new DataLayoutPass(DL));
 #else
     FPM.add(new DataLayout(DL));
@@ -112,7 +114,9 @@ namespace gbe
   {
     llvm::PassManager MPM;
 
-#if LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 5
+#if LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 6
+    MPM.add(new DataLayoutPass());
+#elif LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR == 5
     MPM.add(new DataLayoutPass(DL));
 #else
     MPM.add(new DataLayout(DL));
@@ -231,7 +235,11 @@ namespace gbe
       cl_mod = reinterpret_cast<Module*>(const_cast<void*>(module));
     } else if (fileName){
       llvm::LLVMContext& c = llvm::getGlobalContext();
+#if LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 6
+      cl_mod = parseIRFile(fileName, Err, c).release();
+#else
       cl_mod = ParseIRFile(fileName, Err, c);
+#endif
     }
 
     if (!cl_mod) return false;
@@ -259,7 +267,9 @@ namespace gbe
     runFuntionPass(mod, libraryInfo, DL);
     runModulePass(mod, libraryInfo, DL, optLevel, strictMath);
     llvm::PassManager passes;
-#if LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 5
+#if LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 6
+    passes.add(new DataLayoutPass());
+#elif LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR == 5
     passes.add(new DataLayoutPass(DL));
 #else
     passes.add(new DataLayout(DL));
diff --git a/backend/src/llvm/llvm_unroll.cpp b/backend/src/llvm/llvm_unroll.cpp
index 172e724..5d3fad8 100644
--- a/backend/src/llvm/llvm_unroll.cpp
+++ b/backend/src/llvm/llvm_unroll.cpp
@@ -95,7 +95,11 @@ namespace gbe {
           if (Name.equals(S->getString())) {
             assert(MD->getNumOperands() == 2 &&
                    "Unroll hint metadata should have two operands.");
+#if LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 6
+            return mdconst::extract<ConstantInt>(MD->getOperand(1));
+#else
             return cast<ConstantInt>(MD->getOperand(1));
+#endif
           }
         }
         return nullptr;
@@ -105,6 +109,15 @@ namespace gbe {
         if (!enable && disabledLoops.find(L) != disabledLoops.end())
            return;
         LLVMContext &Context = L->getHeader()->getContext();
+#if LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 6
+        SmallVector<Metadata *, 2> forceUnroll;
+        forceUnroll.push_back(MDString::get(Context, "llvm.loop.unroll.enable"));
+        forceUnroll.push_back(ConstantAsMetadata::get(ConstantInt::get(Type::getInt1Ty(Context), enable)));
+        MDNode *forceUnrollNode = MDNode::get(Context, forceUnroll);
+        SmallVector<Metadata *, 4> Vals;
+        Vals.push_back(NULL);
+        Vals.push_back(forceUnrollNode);
+#else
         SmallVector<Value *, 2> forceUnroll;
         forceUnroll.push_back(MDString::get(Context, "llvm.loop.unroll.enable"));
         forceUnroll.push_back(ConstantInt::get(Type::getInt1Ty(Context), enable));
@@ -112,6 +125,7 @@ namespace gbe {
         SmallVector<Value *, 4> Vals;
         Vals.push_back(NULL);
         Vals.push_back(forceUnrollNode);
+#endif
         MDNode *NewLoopID = MDNode::get(Context, Vals);
         // Set operand 0 to refer to the loop id itself.
         NewLoopID->replaceOperandWith(0, NewLoopID);
-- 
1.8.3.2



More information about the Beignet mailing list