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

Yang Rong rong.r.yang at intel.com
Tue Feb 10 22:57:00 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.

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 | 29 ++++++++++++++++++++++++++++-
 backend/src/llvm/llvm_gen_backend.cpp  | 23 +++++++++++++++++++++--
 backend/src/llvm/llvm_passes.cpp       |  5 ++++-
 backend/src/llvm/llvm_to_gen.cpp       | 18 ++++++++++++++----
 backend/src/llvm/llvm_unroll.cpp       | 14 ++++++++++++++
 7 files changed, 95 insertions(+), 8 deletions(-)

diff --git a/backend/src/backend/gen_program.cpp b/backend/src/backend/gen_program.cpp
index 4cfb703..5d36975 100644
--- a/backend/src/backend/gen_program.cpp
+++ b/backend/src/backend/gen_program.cpp
@@ -257,9 +257,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);
@@ -386,10 +392,14 @@ namespace gbe {
     }else{
       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 >= 6
+      llvm::Linker::LinkModules(dst, src);
+#else
       llvm::Linker::LinkModules( dst,
                                  src,
                                  llvm::Linker::PreserveSource,
                                  &errMsg);
+#endif
       if (errMsg.c_str() != NULL) {
         if (err != NULL && errSize != NULL && stringSize > 0u) {
           if(errMsg.length() < stringSize )
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 d3058d6..61900ef 100644
--- a/backend/src/llvm/llvm_bitcode_link.cpp
+++ b/backend/src/llvm/llvm_bitcode_link.cpp
@@ -67,7 +67,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;
@@ -118,12 +122,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 (newMF->materialize()) {
+            printf("Can not materialize the function: %s\n", fnName.c_str());
+            return false;
+          }
         }
-
+#endif
         if (!materializedFuncCall(src, lib, *newMF, MFS))
           return false;
 
@@ -209,12 +219,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 (newMF->materialize()) {
+          printf("Can not materialize the function: %s\n", fnName.c_str());
+          delete clonedLib;
+          return NULL;
+        }
+      }
+#endif
 
       if (!materializedFuncCall(*mod, *clonedLib, *newMF, materializedFuncs)) {
         delete clonedLib;
@@ -227,11 +246,19 @@ namespace gbe
     /* We use beignet's bitcode as dst because it will have a lot of
        lazy functions which will not be loaded. */
     std::string errorMsg;
+#if LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR <= 5
     if(Linker::LinkModules(clonedLib, mod, Linker::DestroySource, &errorMsg)) {
       delete clonedLib;
       printf("Fatal Error: link the bitcode error:\n%s\n", errorMsg.c_str());
       return NULL;
     }
+#else
+    if(Linker::LinkModules(clonedLib, mod)) {
+      delete clonedLib;
+      printf("Fatal Error: link the bitcode error:\n");
+      return NULL;
+    }
+#endif
 
     llvm::PassManager passes;
 
diff --git a/backend/src/llvm/llvm_gen_backend.cpp b/backend/src/llvm/llvm_gen_backend.cpp
index d47721a..de846cb 100644
--- a/backend/src/llvm/llvm_gen_backend.cpp
+++ b/backend/src/llvm/llvm_gen_backend.cpp
@@ -100,7 +100,6 @@
 #include "llvm/ADT/SmallString.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/Analysis/ConstantsScanner.h"
-#include "llvm/Analysis/FindUsedTypes.h"
 #include "llvm/Analysis/LoopInfo.h"
 #include "llvm/Analysis/ValueTracking.h"
 #include "llvm/CodeGen/Passes.h"
@@ -1467,7 +1466,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 +1488,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 +1531,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 +1577,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..1b40389 100644
--- a/backend/src/llvm/llvm_passes.cpp
+++ b/backend/src/llvm/llvm_passes.cpp
@@ -59,7 +59,6 @@
 #include "llvm/ADT/SmallString.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/Analysis/ConstantsScanner.h"
-#include "llvm/Analysis/FindUsedTypes.h"
 #include "llvm/Analysis/LoopInfo.h"
 #include "llvm/Analysis/ValueTracking.h"
 #include "llvm/CodeGen/Passes.h"
@@ -119,7 +118,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 5701563..cfbcc6f 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();
-      cl_mod = ParseIRFile(fileName, Err, c);
+#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 7cd7c35..f0ad4b4 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