[01/12] gbe/llvm: fix potential null pointer dereference.

Submitted by Luo, Xionghu on May 5, 2016, 4:11 p.m.

Details

Message ID 1462464714-15444-1-git-send-email-xionghu.luo@intel.com
State New
Headers show
Series "Series without cover letter" ( rev: 1 ) in Beignet

Not browsing as part of any series.

Commit Message

Luo, Xionghu May 5, 2016, 4:11 p.m.
From: Luo Xionghu <xionghu.luo@intel.com>

Signed-off-by: Luo Xionghu <xionghu.luo@intel.com>
---
 backend/src/backend/gen_program.cpp              | 14 ++++++--
 backend/src/ir/function.cpp                      |  2 +-
 backend/src/ir/lowering.cpp                      | 11 ++++--
 backend/src/llvm/ExpandLargeIntegers.cpp         |  4 ++-
 backend/src/llvm/PromoteIntegers.cpp             |  5 ++-
 backend/src/llvm/llvm_bitcode_link.cpp           |  8 +++--
 backend/src/llvm/llvm_gen_backend.cpp            | 46 +++++++++++++++---------
 backend/src/llvm/llvm_loadstore_optimization.cpp |  9 ++++-
 backend/src/llvm/llvm_passes.cpp                 |  7 +++-
 backend/src/llvm/llvm_printf_parser.cpp          | 13 ++++++-
 backend/src/llvm/llvm_sampler_fix.cpp            |  2 ++
 backend/src/llvm/llvm_scalarize.cpp              | 26 +++++++++-----
 12 files changed, 111 insertions(+), 36 deletions(-)

Patch hide | download patch | download mbox

diff --git a/backend/src/backend/gen_program.cpp b/backend/src/backend/gen_program.cpp
index 099116c..0abe54e 100644
--- a/backend/src/backend/gen_program.cpp
+++ b/backend/src/backend/gen_program.cpp
@@ -87,6 +87,11 @@  namespace gbe {
     Kernel::printStatus(indent, outs);
 
     FILE *f = fopen("/dev/null", "w");
+    if(!f) {
+      outs << "could not open /dev/null !";
+      return;
+    }
+
     char *buf = new char[4096];
     setbuffer(f, buf, 4096);
     GenCompactInstruction * pCom = NULL;
@@ -147,6 +152,8 @@  namespace gbe {
     // when the function already provides the simd width we need to use (i.e.
     // non zero)
     const ir::Function *fn = unit.getFunction(name);
+    if(fn == NULL)
+      GBE_ASSERT(0);
     uint32_t codeGenNum = sizeof(codeGenStrategy) / sizeof(codeGenStrategy[0]);
     uint32_t codeGen = 0;
     GenContext *ctx = NULL;
@@ -189,14 +196,17 @@  namespace gbe {
       const uint32_t reservedSpillRegs = codeGenStrategy[codeGen].reservedSpillRegs;
 
       // Force the SIMD width now and try to compile
-      unit.getFunction(name)->setSimdWidth(simdWidth);
+      ir::Function *simdFn = unit.getFunction(name);
+      if(simdFn == NULL)
+        GBE_ASSERT(0);
+      simdFn->setSimdWidth(simdWidth);
       ctx->startNewCG(simdWidth, reservedSpillRegs, limitRegisterPressure);
       kernel = ctx->compileKernel();
       if (kernel != NULL) {
         GBE_ASSERT(ctx->getErrCode() == NO_ERROR);
         break;
       }
-      fn->getImageSet()->clearInfo();
+      simdFn->getImageSet()->clearInfo();
       // If we get a out of range if/endif error.
       // We need to set the context to if endif fix mode and restart the previous compile.
       if ( ctx->getErrCode() == OUT_OF_RANGE_IF_ENDIF && !ctx->getIFENDIFFix() ) {
diff --git a/backend/src/ir/function.cpp b/backend/src/ir/function.cpp
index 00fe97c..3b7891b 100644
--- a/backend/src/ir/function.cpp
+++ b/backend/src/ir/function.cpp
@@ -393,7 +393,7 @@  namespace ir {
   LabelIndex BasicBlock::getLabelIndex(void) const {
     const Instruction *first = this->getFirstInstruction();
     const LabelInstruction *label = cast<LabelInstruction>(first);
-    return label->getLabelIndex();
+    return label?label->getLabelIndex():LabelIndex(-1);
   }
 
 } /* namespace ir */
diff --git a/backend/src/ir/lowering.cpp b/backend/src/ir/lowering.cpp
index 535f7e6..654a3bb 100644
--- a/backend/src/ir/lowering.cpp
+++ b/backend/src/ir/lowering.cpp
@@ -170,6 +170,8 @@  namespace ir {
 
     if (opcode == OP_LOAD) {
       LoadInstruction *load = cast<LoadInstruction>(insn);
+      if(!load)
+        return false;
       if (load->getAddressSpace() != MEM_PRIVATE)
         return false;
       loadAddImm.load = insn;
@@ -250,6 +252,7 @@  namespace ir {
     set<PushLocation> inserted;
     for (const auto &loadAddImm : seq) {
       LoadInstruction *load = cast<LoadInstruction>(loadAddImm.load);
+      if(!load) continue;
       const uint32_t valueNum = load->getValueNum();
       bool replaced = false;
       Instruction *ins_after = load; // the instruction to insert after.
@@ -323,6 +326,8 @@  namespace ir {
           derivedRegs.push_back(dst);
         } else if(opcode == OP_LOAD) {
           LoadInstruction *load = cast<LoadInstruction>(insn);
+          if(!load)
+            continue;
           if (load->getAddressSpace() != MEM_PRIVATE)
             continue;
 
@@ -364,7 +369,7 @@  namespace ir {
       const Register arg = fn->getArg(indirectLoad.argID).reg;
       if(dead.contains(indirectLoad.load)) continue;  //repetitive load in the indirectSeq, skip.
       LoadInstruction *load = cast<LoadInstruction>(indirectLoad.load);
-      const uint32_t valueNum = load->getValueNum();
+      const uint32_t valueNum = load ? load->getValueNum() : 0;
       bool replaced = false;
       Instruction *ins_after = load; // the instruction to insert after.
       for (uint32_t valueID = 0; valueID < valueNum; ++valueID) {
@@ -388,7 +393,7 @@  namespace ir {
       vector<Instruction *> adds = indirectLoad.adds;
       for (uint32_t i=0; i<adds.size(); i++) {
         BinaryInstruction *add = cast<BinaryInstruction>(adds[i]);
-        if (!dead.contains(add)) {
+        if (add && !dead.contains(add)) {
           Register dst = add->getDst();
           const Register src0 = add->getSrc(0);
           const Register src1 = add->getSrc(1);
@@ -451,6 +456,7 @@  namespace ir {
       // add.ptr_type dst ptr other
       if (opcode != OP_ADD) return false;
       BinaryInstruction *add = cast<BinaryInstruction>(insn);
+      if(!add) return false;
       const Type addType = add->getType();
       const RegisterFamily family = getFamily(addType);
       if (family != unit.getPointerFamily()) return false;
@@ -467,6 +473,7 @@  namespace ir {
       Instruction *otherInsn = const_cast<Instruction*>(otherDef->getInstruction());
       if (otherInsn->getOpcode() != OP_LOADI) return false;
       LoadImmInstruction *loadImm = cast<LoadImmInstruction>(otherInsn);
+      if(!loadImm) return false;
       const Immediate imm = loadImm->getImmediate();
       const uint64_t offset = getOffsetFromImm(imm);
 
diff --git a/backend/src/llvm/ExpandLargeIntegers.cpp b/backend/src/llvm/ExpandLargeIntegers.cpp
index 00987cb..1ee294f 100644
--- a/backend/src/llvm/ExpandLargeIntegers.cpp
+++ b/backend/src/llvm/ExpandLargeIntegers.cpp
@@ -156,7 +156,7 @@  static TypePair getExpandedIntTypes(Type *Ty) {
 
 // Return true if Val is an int which should be converted.
 static bool shouldConvert(const Value *Val) {
-  Type *Ty = Val->getType();
+  Type *Ty = Val ? Val->getType() : NULL;
   if (IntegerType *ITy = dyn_cast<IntegerType>(Ty))
     return !isLegalBitSize(ITy->getBitWidth());
   return false;
@@ -398,6 +398,8 @@  static void convertInstruction(Instruction *Inst, ConversionState &State,
     PHINode *Hi = IRB.CreatePHI(OpTys.Hi, N, Twine(Name + ".hi"));
     for (unsigned I = 0; I != N; ++I) {
       Value *InVal = Phi->getIncomingValue(I);
+      if(!InVal)
+        continue;
       BasicBlock *InBB = Phi->getIncomingBlock(I);
       // If the value hasn't already been converted then this is a
       // forward-reference PHI which needs to be patched up after RPO traversal.
diff --git a/backend/src/llvm/PromoteIntegers.cpp b/backend/src/llvm/PromoteIntegers.cpp
index 8759287..adba004 100644
--- a/backend/src/llvm/PromoteIntegers.cpp
+++ b/backend/src/llvm/PromoteIntegers.cpp
@@ -129,7 +129,8 @@  static Type *getPromotedType(Type *Ty) {
 
 // Return true if Val is an int which should be converted.
 static bool shouldConvert(Value *Val) {
-  if (IntegerType *ITy = dyn_cast<IntegerType>(Val->getType())) {
+  Type *Ty = Val ? Val->getType() : NULL;
+  if (IntegerType *ITy = dyn_cast<IntegerType>(Ty)) {
     if (!isLegalSize(ITy->getBitWidth())) {
       return true;
     }
@@ -338,6 +339,8 @@  static Value *splitStore(StoreInst *Inst, ConversionState &State) {
 // original type cleared.
 static Value *getClearConverted(Value *Operand, Instruction *InsertPt,
                                 ConversionState &State) {
+  if(!Operand)
+    return Operand;
   Type *OrigType = Operand->getType();
   Instruction *OrigInst = dyn_cast<Instruction>(Operand);
   Operand = State.getConverted(Operand);
diff --git a/backend/src/llvm/llvm_bitcode_link.cpp b/backend/src/llvm/llvm_bitcode_link.cpp
index 5f087f8..990babd 100644
--- a/backend/src/llvm/llvm_bitcode_link.cpp
+++ b/backend/src/llvm/llvm_bitcode_link.cpp
@@ -82,8 +82,12 @@  namespace gbe
           continue;
         }
 
-        if (call->getCalledFunction() &&
-            call->getCalledFunction()->getIntrinsicID() != 0)
+        llvm::Function * callFunc = call->getCalledFunction();
+        if(!callFunc) {
+          continue;
+        }
+
+        if (callFunc->getIntrinsicID() != 0)
           continue;
 
         std::string fnName = call->getCalledValue()->stripPointerCasts()->getName();
diff --git a/backend/src/llvm/llvm_gen_backend.cpp b/backend/src/llvm/llvm_gen_backend.cpp
index 51a1dab..3753756 100644
--- a/backend/src/llvm/llvm_gen_backend.cpp
+++ b/backend/src/llvm/llvm_gen_backend.cpp
@@ -743,13 +743,13 @@  namespace gbe
   static void updatePointerSource(Value *parent, Value *theUser, Value *source, SmallVector<Value *, 4> &pointers) {
     if (isa<SelectInst>(theUser)) {
       SelectInst *si = dyn_cast<SelectInst>(theUser);
-      if (si->getTrueValue() == parent)
+      if (si && si->getTrueValue() == parent)
         pointers[0] = source;
       else
         pointers[1] = source;
     } else if (isa<PHINode>(theUser)) {
       PHINode *phi = dyn_cast<PHINode>(theUser);
-      unsigned opNum = phi->getNumIncomingValues();
+      unsigned opNum = phi ? phi->getNumIncomingValues() : 0;
       for (unsigned j = 0; j < opNum; j++) {
         if (phi->getIncomingValue(j) == parent) {
           pointers[j] = source;
@@ -829,7 +829,7 @@  namespace gbe
             if (isa<SelectInst>(theUser)) capacity = 2;
             if (isa<PHINode>(theUser)) {
               PHINode *phi = dyn_cast<PHINode>(theUser);
-              capacity = phi->getNumIncomingValues();
+              capacity = phi ? phi->getNumIncomingValues() : 1;
             }
 
             SmallVector<Value *, 4> pointers;
@@ -915,7 +915,7 @@  namespace gbe
           } else if (isa<CallInst>(theUser)) {
             // atomic/read(write)image
             CallInst *ci = dyn_cast<CallInst>(theUser);
-            pointer = ci->getArgOperand(0);
+            pointer = ci ? ci->getArgOperand(0) : NULL;
           } else {
             theUser->dump();
             GBE_ASSERT(0 && "Unknown instruction operating on pointers\n");
@@ -1203,7 +1203,7 @@  namespace gbe
     MDNode *typeNameNode = NULL;
     MDNode *typeBaseNameNode = NULL;
     MDNode *node = getKernelFunctionMetadata(&F);
-    for(uint j = 0; j < node->getNumOperands() - 1; j++) {
+    for(uint j = 0;node && j < node->getNumOperands() - 1; j++) {
       MDNode *attrNode = dyn_cast_or_null<MDNode>(node->getOperand(1 + j));
       if (attrNode == NULL) break;
       MDString *attrName = dyn_cast_or_null<MDString>(attrNode->getOperand(0));
@@ -1219,7 +1219,9 @@  namespace gbe
     unsigned argID = 0;
     ir::FunctionArgument::InfoFromLLVM llvmInfo;
     for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I, argID++) {
-      llvmInfo.typeName= (cast<MDString>(typeNameNode->getOperand(1 + argID)))->getString();
+      if(typeNameNode) {
+        llvmInfo.typeName= (cast<MDString>(typeNameNode->getOperand(1 + argID)))->getString();
+      }
       if(typeBaseNameNode) {
         llvmInfo.typeBaseName= (cast<MDString>(typeBaseNameNode->getOperand(1 + argID)))->getString();
       }
@@ -1427,7 +1429,7 @@  namespace gbe
           const StructType * strTy = cast<StructType>(c->getType());
           uint32_t size = 0;
 
-          for(uint32_t op=0; op < strTy->getNumElements(); op++)
+          for(uint32_t op=0; strTy && op < strTy->getNumElements(); op++)
           {
             Type* elementType = strTy->getElementType(op);
             uint32_t align = 8 * getAlignmentByte(unit, elementType);
@@ -1449,6 +1451,8 @@  namespace gbe
             getSequentialData(cds, mem, offset);
           else {
             const ConstantArray *ca = dyn_cast<ConstantArray>(c);
+            if(!ca)
+              return;
             const ArrayType *arrTy = ca->getType();
             Type* elemTy = arrTy->getElementType();
             uint32_t elemSize = getTypeBitSize(unit, elemTy);
@@ -1950,7 +1954,7 @@  namespace gbe
       assert(node);
 
 
-    for(uint j = 0; j < node->getNumOperands() - 1; j++) {
+    for(uint j = 0; node && j < node->getNumOperands() - 1; j++) {
       MDNode *attrNode = dyn_cast_or_null<MDNode>(node->getOperand(1 + j));
       if (attrNode == NULL) break;
       MDString *attrName = dyn_cast_or_null<MDString>(attrNode->getOperand(0));
@@ -1973,7 +1977,7 @@  namespace gbe
         reqd_wg_sz[2] = z->getZExtValue();
         functionAttributes += attrName->getString();
         std::stringstream param;
-        char buffer[100];
+        char buffer[100] = {0};
         param <<"(";
         param << reqd_wg_sz[0];
         param << ",";
@@ -2024,7 +2028,7 @@  namespace gbe
         std::string typeName = getTypeName(ctx, stype, signValue);
 
         std::stringstream param;
-        char buffer[100];
+        char buffer[100] = {0};
         param <<"(";
         param << typeName;
         if(vtype->isVectorTy())
@@ -2050,7 +2054,7 @@  namespace gbe
         hint_wg_sz[2] = z->getZExtValue();
         functionAttributes += attrName->getString();
         std::stringstream param;
-        char buffer[100];
+        char buffer[100] = {0};
         param <<"(";
         param << hint_wg_sz[0];
         param << ",";
@@ -2080,17 +2084,25 @@  namespace gbe
       for (; I != E; ++I, ++argID) {
         const std::string &argName = I->getName().str();
         Type *type = I->getType();
+        if(addrSpaceNode) {
 #if LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR <= 5
-        llvmInfo.addrSpace = (cast<ConstantInt>(addrSpaceNode->getOperand(1 + argID)))->getZExtValue();
+          llvmInfo.addrSpace = (cast<ConstantInt>(addrSpaceNode->getOperand(1 + argID)))->getZExtValue();
 #else
-        llvmInfo.addrSpace = (mdconst::extract<ConstantInt>(addrSpaceNode->getOperand(1 + argID)))->getZExtValue();
+          llvmInfo.addrSpace = (mdconst::extract<ConstantInt>(addrSpaceNode->getOperand(1 + argID)))->getZExtValue();
 #endif
-        llvmInfo.typeName = (cast<MDString>(typeNameNode->getOperand(1 + argID)))->getString();
+        }
+        if(typeNameNode) {
+          llvmInfo.typeName = (cast<MDString>(typeNameNode->getOperand(1 + argID)))->getString();
+        }
         if(typeBaseNameNode){
           llvmInfo.typeBaseName = (cast<MDString>(typeBaseNameNode->getOperand(1 + argID)))->getString();
         }
-        llvmInfo.accessQual = (cast<MDString>(accessQualNode->getOperand(1 + argID)))->getString();
-        llvmInfo.typeQual = (cast<MDString>(typeQualNode->getOperand(1 + argID)))->getString();
+        if(accessQualNode) {
+          llvmInfo.accessQual = (cast<MDString>(accessQualNode->getOperand(1 + argID)))->getString();
+        }
+        if(typeQualNode) {
+          llvmInfo.typeQual = (cast<MDString>(typeQualNode->getOperand(1 + argID)))->getString();
+        }
         if(argNameNode){
           llvmInfo.argName = (cast<MDString>(argNameNode->getOperand(1 + argID)))->getString();
         }
@@ -2137,6 +2149,8 @@  namespace gbe
           ctx.input(argName, ir::FunctionArgument::VALUE, reg, llvmInfo, getTypeByteSize(unit, type), getAlignmentByte(unit, type), 0);
         else {
           PointerType *pointerType = dyn_cast<PointerType>(type);
+          if(!pointerType)
+            continue;
           Type *pointed = pointerType->getElementType();
           // By value structure
 #if LLVM_VERSION_MINOR <= 1
diff --git a/backend/src/llvm/llvm_loadstore_optimization.cpp b/backend/src/llvm/llvm_loadstore_optimization.cpp
index 121f53d..c0ac6ed 100644
--- a/backend/src/llvm/llvm_loadstore_optimization.cpp
+++ b/backend/src/llvm/llvm_loadstore_optimization.cpp
@@ -202,6 +202,9 @@  namespace gbe {
       values.push_back(cast<StoreInst>(merged[i])->getValueOperand());
     }
     StoreInst *st = cast<StoreInst>(merged[0]);
+    if(!st)
+      return;
+
     unsigned addrSpace = st->getPointerAddressSpace();
 
     unsigned align = st->getAlignment();
@@ -215,7 +218,10 @@  namespace gbe {
       parent = Builder.CreateInsertElement(parent, values[i], ConstantInt::get(IntegerType::get(st->getContext(), 32), i));
     }
 
-    Value *newPtr = Builder.CreateBitCast(st->getPointerOperand(), PointerType::get(vecTy, addrSpace));
+    Value * stPointer = st->getPointerOperand();
+    if(!stPointer)
+      return;
+    Value *newPtr = Builder.CreateBitCast(stPointer, PointerType::get(vecTy, addrSpace));
     StoreInst *newST = Builder.CreateStore(parent, newPtr);
     newST->setAlignment(align);
   }
@@ -227,6 +233,7 @@  namespace gbe {
       if(isa<LoadInst>(*BBI) || isa<StoreInst>(*BBI)) {
         bool isLoad = isa<LoadInst>(*BBI) ? true: false;
         Type *ty = getValueType(&*BBI);
+        if(!ty) continue;
         if(ty->isVectorTy()) continue;
         // TODO Support DWORD/WORD/BYTE LOAD for store support DWORD only now.
         if (!(ty->isFloatTy() || ty->isIntegerTy(32) ||
diff --git a/backend/src/llvm/llvm_passes.cpp b/backend/src/llvm/llvm_passes.cpp
index d5d965b..b925e5f 100644
--- a/backend/src/llvm/llvm_passes.cpp
+++ b/backend/src/llvm/llvm_passes.cpp
@@ -222,7 +222,9 @@  namespace gbe
   {
     const uint32_t ptrSize = unit.getPointerSize();
     Value* parentPointer = GEPInst->getOperand(0);
-    CompositeType* CompTy = cast<CompositeType>(parentPointer->getType());
+    CompositeType* CompTy = parentPointer ? cast<CompositeType>(parentPointer->getType()) : NULL;
+    if(!CompTy)
+      return false;
 
     Value* currentAddrInst = 
       new PtrToIntInst(parentPointer, IntegerType::get(GEPInst->getContext(), ptrSize), "", GEPInst);
@@ -254,6 +256,9 @@  namespace gbe
 
         Value *operand = GEPInst->getOperand(op); 
 
+        if(!operand)
+          continue;
+
         //HACK TODO: Inserted by type replacement.. this code could break something????
         if(getTypeByteSize(unit, operand->getType())>4)
         {
diff --git a/backend/src/llvm/llvm_printf_parser.cpp b/backend/src/llvm/llvm_printf_parser.cpp
index c9ec85f..a92c46d 100644
--- a/backend/src/llvm/llvm_printf_parser.cpp
+++ b/backend/src/llvm/llvm_printf_parser.cpp
@@ -324,6 +324,9 @@  error:
     int param_num = 0;
 
     llvm::Constant* arg0 = dyn_cast<llvm::ConstantExpr>(*CI_FMT);
+    if(!arg0) {
+      return false;
+    }
     llvm::Constant* arg0_ptr = dyn_cast<llvm::Constant>(arg0->getOperand(0));
     if (!arg0_ptr) {
       return false;
@@ -428,7 +431,12 @@  error:
           continue;
         }
 
-        if (call->getCalledFunction() && call->getCalledFunction()->getIntrinsicID() != 0)
+        llvm::Function * callFunc = call->getCalledFunction();
+        if(!callFunc) {
+          continue;
+        }
+
+        if ( callFunc->getIntrinsicID() != 0)
           continue;
 
         Value *Callee = call->getCalledValue();
@@ -560,6 +568,9 @@  error:
         switch (slot.state.conversion_specifier) {
           case PRINTF_CONVERSION_S: {
             llvm::Constant* arg0 = dyn_cast<llvm::ConstantExpr>(arg);
+            if(!arg0) {
+              return false;
+            }
             llvm::Constant* arg0_ptr = dyn_cast<llvm::Constant>(arg0->getOperand(0));
             if (!arg0_ptr) {
               return false;
diff --git a/backend/src/llvm/llvm_sampler_fix.cpp b/backend/src/llvm/llvm_sampler_fix.cpp
index 01db8fe..de7ebdb 100644
--- a/backend/src/llvm/llvm_sampler_fix.cpp
+++ b/backend/src/llvm/llvm_sampler_fix.cpp
@@ -41,6 +41,8 @@  namespace gbe {
     }
 
     bool visitCallInst(CallInst *I) {
+      if(!I)
+        return false;
       Value *Callee = I->getCalledValue();
       const std::string fnName = Callee->getName();
       bool changed = false;
diff --git a/backend/src/llvm/llvm_scalarize.cpp b/backend/src/llvm/llvm_scalarize.cpp
index 3984218..53fd320 100644
--- a/backend/src/llvm/llvm_scalarize.cpp
+++ b/backend/src/llvm/llvm_scalarize.cpp
@@ -173,6 +173,8 @@  namespace gbe {
     }
 
     Type* GetBasicType(Type* type) {
+      if(!type)
+        return type;
       switch(type->getTypeID()) {
       case Type::VectorTyID:
       case Type::ArrayTyID:
@@ -184,14 +186,14 @@  namespace gbe {
     }
 
     int GetComponentCount(const Type* type)  {
-      if (type->getTypeID() == Type::VectorTyID)
+      if (type && type->getTypeID() == Type::VectorTyID)
         return llvm::dyn_cast<VectorType>(type)->getNumElements();
       else
         return 1;
     }
 
     int GetComponentCount(const Value* value) {
-      return GetComponentCount(value->getType());
+      return GetComponentCount(value ? value->getType() : NULL);
     }
 
     /* set to insert new instructions after the specified instruction.*/
@@ -236,7 +238,7 @@  namespace gbe {
   {
     assert(canGetComponent(v) && "getComponent called on unhandled vector");
 
-    if (v->getType()->isVectorTy()) {
+    if (v && v->getType() && v->getType()->isVectorTy()) {
       if (ConstantDataVector* c = dyn_cast<ConstantDataVector>(v)) {
         return c->getElementAsConstant(component);
       } else if (ConstantVector* c = dyn_cast<ConstantVector>(v)) {
@@ -337,7 +339,7 @@  namespace gbe {
   }
   bool Scalarize::canGetComponent(Value* v)
   {
-    if (v->getType()->isVectorTy()) {
+    if (v && v->getType() && v->getType()->isVectorTy()) {
       if (isa<ConstantDataVector>(v) || isa<ConstantVector>(v) || isa<ConstantAggregateZero>(v) || isa<UndefValue>(v)) {
         return true;
       } else {
@@ -541,13 +543,18 @@  namespace gbe {
     VectorValues& vVals = vectorVals[sv];
 
     int size = GetComponentCount(sv);
-    int srcSize = GetComponentCount(sv->getOperand(0)->getType());
+
+    Value* Op0 = sv->getOperand(0);
+    if(!Op0)
+      return false;
+
+    int srcSize = GetComponentCount(Op0->getType());
 
     for (int i = 0; i < size; ++i) {
       int select = sv->getMaskValue(i);
 
       if (select < 0) {
-        setComponent(vVals, i, UndefValue::get(GetBasicType(sv->getOperand(0))));
+        setComponent(vVals, i, UndefValue::get(GetBasicType(Op0)));
         continue;
       }
 
@@ -736,7 +743,7 @@  namespace gbe {
         //TODO: This is a implement for the non-constant index, we use an allocated new vector
         //to store the need vector elements.
         Value* foo = extr->getOperand(0);
-        Type* fooTy = foo->getType();
+        Type* fooTy = foo ? foo->getType() : NULL;
 
         Value* Alloc;
         if(vectorAlloca.find(foo) == vectorAlloca.end())
@@ -807,7 +814,10 @@  namespace gbe {
       return;
     ReversePostOrderTraversal<Function*> rpot(&F);
     BasicBlock::iterator instI = (*rpot.begin())->begin();
-    builder->SetInsertPoint(&*instI);
+    Instruction* instVal = &*instI;
+    if(instVal == nullptr)
+      return;
+    builder->SetInsertPoint(instVal);
 
     Function::arg_iterator I = F.arg_begin(), E = F.arg_end();
 

Comments

The patchset LGTM, pushed, thanks.

> -----Original Message-----

> From: Beignet [mailto:beignet-bounces@lists.freedesktop.org] On Behalf Of

> xionghu.luo@intel.com

> Sent: Friday, May 6, 2016 0:12

> To: beignet@lists.freedesktop.org

> Cc: Luo, Xionghu <xionghu.luo@intel.com>

> Subject: [Beignet] [PATCH 01/12] gbe/llvm: fix potential null pointer

> dereference.

> 

> From: Luo Xionghu <xionghu.luo@intel.com>

> 

> Signed-off-by: Luo Xionghu <xionghu.luo@intel.com>

> ---

>  backend/src/backend/gen_program.cpp              | 14 ++++++--

>  backend/src/ir/function.cpp                      |  2 +-

>  backend/src/ir/lowering.cpp                      | 11 ++++--

>  backend/src/llvm/ExpandLargeIntegers.cpp         |  4 ++-

>  backend/src/llvm/PromoteIntegers.cpp             |  5 ++-

>  backend/src/llvm/llvm_bitcode_link.cpp           |  8 +++--

>  backend/src/llvm/llvm_gen_backend.cpp            | 46 +++++++++++++++------

> ---

>  backend/src/llvm/llvm_loadstore_optimization.cpp |  9 ++++-

>  backend/src/llvm/llvm_passes.cpp                 |  7 +++-

>  backend/src/llvm/llvm_printf_parser.cpp          | 13 ++++++-

>  backend/src/llvm/llvm_sampler_fix.cpp            |  2 ++

>  backend/src/llvm/llvm_scalarize.cpp              | 26 +++++++++-----

>  12 files changed, 111 insertions(+), 36 deletions(-)

> 

> diff --git a/backend/src/backend/gen_program.cpp

> b/backend/src/backend/gen_program.cpp

> index 099116c..0abe54e 100644

> --- a/backend/src/backend/gen_program.cpp

> +++ b/backend/src/backend/gen_program.cpp

> @@ -87,6 +87,11 @@ namespace gbe {

>      Kernel::printStatus(indent, outs);

> 

>      FILE *f = fopen("/dev/null", "w");

> +    if(!f) {

> +      outs << "could not open /dev/null !";

> +      return;

> +    }

> +

>      char *buf = new char[4096];

>      setbuffer(f, buf, 4096);

>      GenCompactInstruction * pCom = NULL; @@ -147,6 +152,8 @@

> namespace gbe {

>      // when the function already provides the simd width we need to use (i.e.

>      // non zero)

>      const ir::Function *fn = unit.getFunction(name);

> +    if(fn == NULL)

> +      GBE_ASSERT(0);

>      uint32_t codeGenNum = sizeof(codeGenStrategy) /

> sizeof(codeGenStrategy[0]);

>      uint32_t codeGen = 0;

>      GenContext *ctx = NULL;

> @@ -189,14 +196,17 @@ namespace gbe {

>        const uint32_t reservedSpillRegs =

> codeGenStrategy[codeGen].reservedSpillRegs;

> 

>        // Force the SIMD width now and try to compile

> -      unit.getFunction(name)->setSimdWidth(simdWidth);

> +      ir::Function *simdFn = unit.getFunction(name);

> +      if(simdFn == NULL)

> +        GBE_ASSERT(0);

> +      simdFn->setSimdWidth(simdWidth);

>        ctx->startNewCG(simdWidth, reservedSpillRegs, limitRegisterPressure);

>        kernel = ctx->compileKernel();

>        if (kernel != NULL) {

>          GBE_ASSERT(ctx->getErrCode() == NO_ERROR);

>          break;

>        }

> -      fn->getImageSet()->clearInfo();

> +      simdFn->getImageSet()->clearInfo();

>        // If we get a out of range if/endif error.

>        // We need to set the context to if endif fix mode and restart the

> previous compile.

>        if ( ctx->getErrCode() == OUT_OF_RANGE_IF_ENDIF && !ctx-

> >getIFENDIFFix() ) { diff --git a/backend/src/ir/function.cpp

> b/backend/src/ir/function.cpp index 00fe97c..3b7891b 100644

> --- a/backend/src/ir/function.cpp

> +++ b/backend/src/ir/function.cpp

> @@ -393,7 +393,7 @@ namespace ir {

>    LabelIndex BasicBlock::getLabelIndex(void) const {

>      const Instruction *first = this->getFirstInstruction();

>      const LabelInstruction *label = cast<LabelInstruction>(first);

> -    return label->getLabelIndex();

> +    return label?label->getLabelIndex():LabelIndex(-1);

>    }

> 

>  } /* namespace ir */

> diff --git a/backend/src/ir/lowering.cpp b/backend/src/ir/lowering.cpp index

> 535f7e6..654a3bb 100644

> --- a/backend/src/ir/lowering.cpp

> +++ b/backend/src/ir/lowering.cpp

> @@ -170,6 +170,8 @@ namespace ir {

> 

>      if (opcode == OP_LOAD) {

>        LoadInstruction *load = cast<LoadInstruction>(insn);

> +      if(!load)

> +        return false;

>        if (load->getAddressSpace() != MEM_PRIVATE)

>          return false;

>        loadAddImm.load = insn;

> @@ -250,6 +252,7 @@ namespace ir {

>      set<PushLocation> inserted;

>      for (const auto &loadAddImm : seq) {

>        LoadInstruction *load = cast<LoadInstruction>(loadAddImm.load);

> +      if(!load) continue;

>        const uint32_t valueNum = load->getValueNum();

>        bool replaced = false;

>        Instruction *ins_after = load; // the instruction to insert after.

> @@ -323,6 +326,8 @@ namespace ir {

>            derivedRegs.push_back(dst);

>          } else if(opcode == OP_LOAD) {

>            LoadInstruction *load = cast<LoadInstruction>(insn);

> +          if(!load)

> +            continue;

>            if (load->getAddressSpace() != MEM_PRIVATE)

>              continue;

> 

> @@ -364,7 +369,7 @@ namespace ir {

>        const Register arg = fn->getArg(indirectLoad.argID).reg;

>        if(dead.contains(indirectLoad.load)) continue;  //repetitive load in the

> indirectSeq, skip.

>        LoadInstruction *load = cast<LoadInstruction>(indirectLoad.load);

> -      const uint32_t valueNum = load->getValueNum();

> +      const uint32_t valueNum = load ? load->getValueNum() : 0;

>        bool replaced = false;

>        Instruction *ins_after = load; // the instruction to insert after.

>        for (uint32_t valueID = 0; valueID < valueNum; ++valueID) { @@ -388,7

> +393,7 @@ namespace ir {

>        vector<Instruction *> adds = indirectLoad.adds;

>        for (uint32_t i=0; i<adds.size(); i++) {

>          BinaryInstruction *add = cast<BinaryInstruction>(adds[i]);

> -        if (!dead.contains(add)) {

> +        if (add && !dead.contains(add)) {

>            Register dst = add->getDst();

>            const Register src0 = add->getSrc(0);

>            const Register src1 = add->getSrc(1); @@ -451,6 +456,7 @@ namespace

> ir {

>        // add.ptr_type dst ptr other

>        if (opcode != OP_ADD) return false;

>        BinaryInstruction *add = cast<BinaryInstruction>(insn);

> +      if(!add) return false;

>        const Type addType = add->getType();

>        const RegisterFamily family = getFamily(addType);

>        if (family != unit.getPointerFamily()) return false; @@ -467,6 +473,7 @@

> namespace ir {

>        Instruction *otherInsn = const_cast<Instruction*>(otherDef-

> >getInstruction());

>        if (otherInsn->getOpcode() != OP_LOADI) return false;

>        LoadImmInstruction *loadImm = cast<LoadImmInstruction>(otherInsn);

> +      if(!loadImm) return false;

>        const Immediate imm = loadImm->getImmediate();

>        const uint64_t offset = getOffsetFromImm(imm);

> 

> diff --git a/backend/src/llvm/ExpandLargeIntegers.cpp

> b/backend/src/llvm/ExpandLargeIntegers.cpp

> index 00987cb..1ee294f 100644

> --- a/backend/src/llvm/ExpandLargeIntegers.cpp

> +++ b/backend/src/llvm/ExpandLargeIntegers.cpp

> @@ -156,7 +156,7 @@ static TypePair getExpandedIntTypes(Type *Ty) {

> 

>  // Return true if Val is an int which should be converted.

>  static bool shouldConvert(const Value *Val) {

> -  Type *Ty = Val->getType();

> +  Type *Ty = Val ? Val->getType() : NULL;

>    if (IntegerType *ITy = dyn_cast<IntegerType>(Ty))

>      return !isLegalBitSize(ITy->getBitWidth());

>    return false;

> @@ -398,6 +398,8 @@ static void convertInstruction(Instruction *Inst,

> ConversionState &State,

>      PHINode *Hi = IRB.CreatePHI(OpTys.Hi, N, Twine(Name + ".hi"));

>      for (unsigned I = 0; I != N; ++I) {

>        Value *InVal = Phi->getIncomingValue(I);

> +      if(!InVal)

> +        continue;

>        BasicBlock *InBB = Phi->getIncomingBlock(I);

>        // If the value hasn't already been converted then this is a

>        // forward-reference PHI which needs to be patched up after RPO

> traversal.

> diff --git a/backend/src/llvm/PromoteIntegers.cpp

> b/backend/src/llvm/PromoteIntegers.cpp

> index 8759287..adba004 100644

> --- a/backend/src/llvm/PromoteIntegers.cpp

> +++ b/backend/src/llvm/PromoteIntegers.cpp

> @@ -129,7 +129,8 @@ static Type *getPromotedType(Type *Ty) {

> 

>  // Return true if Val is an int which should be converted.

>  static bool shouldConvert(Value *Val) {

> -  if (IntegerType *ITy = dyn_cast<IntegerType>(Val->getType())) {

> +  Type *Ty = Val ? Val->getType() : NULL;  if (IntegerType *ITy =

> + dyn_cast<IntegerType>(Ty)) {

>      if (!isLegalSize(ITy->getBitWidth())) {

>        return true;

>      }

> @@ -338,6 +339,8 @@ static Value *splitStore(StoreInst *Inst,

> ConversionState &State) {  // original type cleared.

>  static Value *getClearConverted(Value *Operand, Instruction *InsertPt,

>                                  ConversionState &State) {

> +  if(!Operand)

> +    return Operand;

>    Type *OrigType = Operand->getType();

>    Instruction *OrigInst = dyn_cast<Instruction>(Operand);

>    Operand = State.getConverted(Operand); diff --git

> a/backend/src/llvm/llvm_bitcode_link.cpp

> b/backend/src/llvm/llvm_bitcode_link.cpp

> index 5f087f8..990babd 100644

> --- a/backend/src/llvm/llvm_bitcode_link.cpp

> +++ b/backend/src/llvm/llvm_bitcode_link.cpp

> @@ -82,8 +82,12 @@ namespace gbe

>            continue;

>          }

> 

> -        if (call->getCalledFunction() &&

> -            call->getCalledFunction()->getIntrinsicID() != 0)

> +        llvm::Function * callFunc = call->getCalledFunction();

> +        if(!callFunc) {

> +          continue;

> +        }

> +

> +        if (callFunc->getIntrinsicID() != 0)

>            continue;

> 

>          std::string fnName = call->getCalledValue()->stripPointerCasts()-

> >getName();

> diff --git a/backend/src/llvm/llvm_gen_backend.cpp

> b/backend/src/llvm/llvm_gen_backend.cpp

> index 51a1dab..3753756 100644

> --- a/backend/src/llvm/llvm_gen_backend.cpp

> +++ b/backend/src/llvm/llvm_gen_backend.cpp

> @@ -743,13 +743,13 @@ namespace gbe

>    static void updatePointerSource(Value *parent, Value *theUser, Value

> *source, SmallVector<Value *, 4> &pointers) {

>      if (isa<SelectInst>(theUser)) {

>        SelectInst *si = dyn_cast<SelectInst>(theUser);

> -      if (si->getTrueValue() == parent)

> +      if (si && si->getTrueValue() == parent)

>          pointers[0] = source;

>        else

>          pointers[1] = source;

>      } else if (isa<PHINode>(theUser)) {

>        PHINode *phi = dyn_cast<PHINode>(theUser);

> -      unsigned opNum = phi->getNumIncomingValues();

> +      unsigned opNum = phi ? phi->getNumIncomingValues() : 0;

>        for (unsigned j = 0; j < opNum; j++) {

>          if (phi->getIncomingValue(j) == parent) {

>            pointers[j] = source;

> @@ -829,7 +829,7 @@ namespace gbe

>              if (isa<SelectInst>(theUser)) capacity = 2;

>              if (isa<PHINode>(theUser)) {

>                PHINode *phi = dyn_cast<PHINode>(theUser);

> -              capacity = phi->getNumIncomingValues();

> +              capacity = phi ? phi->getNumIncomingValues() : 1;

>              }

> 

>              SmallVector<Value *, 4> pointers; @@ -915,7 +915,7 @@ namespace

> gbe

>            } else if (isa<CallInst>(theUser)) {

>              // atomic/read(write)image

>              CallInst *ci = dyn_cast<CallInst>(theUser);

> -            pointer = ci->getArgOperand(0);

> +            pointer = ci ? ci->getArgOperand(0) : NULL;

>            } else {

>              theUser->dump();

>              GBE_ASSERT(0 && "Unknown instruction operating on pointers\n");

> @@ -1203,7 +1203,7 @@ namespace gbe

>      MDNode *typeNameNode = NULL;

>      MDNode *typeBaseNameNode = NULL;

>      MDNode *node = getKernelFunctionMetadata(&F);

> -    for(uint j = 0; j < node->getNumOperands() - 1; j++) {

> +    for(uint j = 0;node && j < node->getNumOperands() - 1; j++) {

>        MDNode *attrNode = dyn_cast_or_null<MDNode>(node-

> >getOperand(1 + j));

>        if (attrNode == NULL) break;

>        MDString *attrName = dyn_cast_or_null<MDString>(attrNode-

> >getOperand(0));

> @@ -1219,7 +1219,9 @@ namespace gbe

>      unsigned argID = 0;

>      ir::FunctionArgument::InfoFromLLVM llvmInfo;

>      for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I,

> argID++) {

> -      llvmInfo.typeName= (cast<MDString>(typeNameNode->getOperand(1 +

> argID)))->getString();

> +      if(typeNameNode) {

> +        llvmInfo.typeName= (cast<MDString>(typeNameNode->getOperand(1

> + argID)))->getString();

> +      }

>        if(typeBaseNameNode) {

>          llvmInfo.typeBaseName= (cast<MDString>(typeBaseNameNode-

> >getOperand(1 + argID)))->getString();

>        }

> @@ -1427,7 +1429,7 @@ namespace gbe

>            const StructType * strTy = cast<StructType>(c->getType());

>            uint32_t size = 0;

> 

> -          for(uint32_t op=0; op < strTy->getNumElements(); op++)

> +          for(uint32_t op=0; strTy && op < strTy->getNumElements();

> + op++)

>            {

>              Type* elementType = strTy->getElementType(op);

>              uint32_t align = 8 * getAlignmentByte(unit, elementType); @@ -1449,6

> +1451,8 @@ namespace gbe

>              getSequentialData(cds, mem, offset);

>            else {

>              const ConstantArray *ca = dyn_cast<ConstantArray>(c);

> +            if(!ca)

> +              return;

>              const ArrayType *arrTy = ca->getType();

>              Type* elemTy = arrTy->getElementType();

>              uint32_t elemSize = getTypeBitSize(unit, elemTy); @@ -1950,7 +1954,7

> @@ namespace gbe

>        assert(node);

> 

> 

> -    for(uint j = 0; j < node->getNumOperands() - 1; j++) {

> +    for(uint j = 0; node && j < node->getNumOperands() - 1; j++) {

>        MDNode *attrNode = dyn_cast_or_null<MDNode>(node-

> >getOperand(1 + j));

>        if (attrNode == NULL) break;

>        MDString *attrName = dyn_cast_or_null<MDString>(attrNode-

> >getOperand(0));

> @@ -1973,7 +1977,7 @@ namespace gbe

>          reqd_wg_sz[2] = z->getZExtValue();

>          functionAttributes += attrName->getString();

>          std::stringstream param;

> -        char buffer[100];

> +        char buffer[100] = {0};

>          param <<"(";

>          param << reqd_wg_sz[0];

>          param << ",";

> @@ -2024,7 +2028,7 @@ namespace gbe

>          std::string typeName = getTypeName(ctx, stype, signValue);

> 

>          std::stringstream param;

> -        char buffer[100];

> +        char buffer[100] = {0};

>          param <<"(";

>          param << typeName;

>          if(vtype->isVectorTy())

> @@ -2050,7 +2054,7 @@ namespace gbe

>          hint_wg_sz[2] = z->getZExtValue();

>          functionAttributes += attrName->getString();

>          std::stringstream param;

> -        char buffer[100];

> +        char buffer[100] = {0};

>          param <<"(";

>          param << hint_wg_sz[0];

>          param << ",";

> @@ -2080,17 +2084,25 @@ namespace gbe

>        for (; I != E; ++I, ++argID) {

>          const std::string &argName = I->getName().str();

>          Type *type = I->getType();

> +        if(addrSpaceNode) {

>  #if LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR <= 5

> -        llvmInfo.addrSpace = (cast<ConstantInt>(addrSpaceNode-

> >getOperand(1 + argID)))->getZExtValue();

> +          llvmInfo.addrSpace =

> + (cast<ConstantInt>(addrSpaceNode->getOperand(1 +

> + argID)))->getZExtValue();

>  #else

> -        llvmInfo.addrSpace = (mdconst::extract<ConstantInt>(addrSpaceNode-

> >getOperand(1 + argID)))->getZExtValue();

> +          llvmInfo.addrSpace =

> + (mdconst::extract<ConstantInt>(addrSpaceNode->getOperand(1 +

> + argID)))->getZExtValue();

>  #endif

> -        llvmInfo.typeName = (cast<MDString>(typeNameNode->getOperand(1

> + argID)))->getString();

> +        }

> +        if(typeNameNode) {

> +          llvmInfo.typeName = (cast<MDString>(typeNameNode-

> >getOperand(1 + argID)))->getString();

> +        }

>          if(typeBaseNameNode){

>            llvmInfo.typeBaseName = (cast<MDString>(typeBaseNameNode-

> >getOperand(1 + argID)))->getString();

>          }

> -        llvmInfo.accessQual = (cast<MDString>(accessQualNode-

> >getOperand(1 + argID)))->getString();

> -        llvmInfo.typeQual = (cast<MDString>(typeQualNode->getOperand(1 +

> argID)))->getString();

> +        if(accessQualNode) {

> +          llvmInfo.accessQual = (cast<MDString>(accessQualNode-

> >getOperand(1 + argID)))->getString();

> +        }

> +        if(typeQualNode) {

> +          llvmInfo.typeQual = (cast<MDString>(typeQualNode->getOperand(1 +

> argID)))->getString();

> +        }

>          if(argNameNode){

>            llvmInfo.argName = (cast<MDString>(argNameNode->getOperand(1 +

> argID)))->getString();

>          }

> @@ -2137,6 +2149,8 @@ namespace gbe

>            ctx.input(argName, ir::FunctionArgument::VALUE, reg, llvmInfo,

> getTypeByteSize(unit, type), getAlignmentByte(unit, type), 0);

>          else {

>            PointerType *pointerType = dyn_cast<PointerType>(type);

> +          if(!pointerType)

> +            continue;

>            Type *pointed = pointerType->getElementType();

>            // By value structure

>  #if LLVM_VERSION_MINOR <= 1

> diff --git a/backend/src/llvm/llvm_loadstore_optimization.cpp

> b/backend/src/llvm/llvm_loadstore_optimization.cpp

> index 121f53d..c0ac6ed 100644

> --- a/backend/src/llvm/llvm_loadstore_optimization.cpp

> +++ b/backend/src/llvm/llvm_loadstore_optimization.cpp

> @@ -202,6 +202,9 @@ namespace gbe {

>        values.push_back(cast<StoreInst>(merged[i])->getValueOperand());

>      }

>      StoreInst *st = cast<StoreInst>(merged[0]);

> +    if(!st)

> +      return;

> +

>      unsigned addrSpace = st->getPointerAddressSpace();

> 

>      unsigned align = st->getAlignment(); @@ -215,7 +218,10 @@ namespace

> gbe {

>        parent = Builder.CreateInsertElement(parent, values[i],

> ConstantInt::get(IntegerType::get(st->getContext(), 32), i));

>      }

> 

> -    Value *newPtr = Builder.CreateBitCast(st->getPointerOperand(),

> PointerType::get(vecTy, addrSpace));

> +    Value * stPointer = st->getPointerOperand();

> +    if(!stPointer)

> +      return;

> +    Value *newPtr = Builder.CreateBitCast(stPointer,

> + PointerType::get(vecTy, addrSpace));

>      StoreInst *newST = Builder.CreateStore(parent, newPtr);

>      newST->setAlignment(align);

>    }

> @@ -227,6 +233,7 @@ namespace gbe {

>        if(isa<LoadInst>(*BBI) || isa<StoreInst>(*BBI)) {

>          bool isLoad = isa<LoadInst>(*BBI) ? true: false;

>          Type *ty = getValueType(&*BBI);

> +        if(!ty) continue;

>          if(ty->isVectorTy()) continue;

>          // TODO Support DWORD/WORD/BYTE LOAD for store support DWORD

> only now.

>          if (!(ty->isFloatTy() || ty->isIntegerTy(32) || diff --git

> a/backend/src/llvm/llvm_passes.cpp b/backend/src/llvm/llvm_passes.cpp

> index d5d965b..b925e5f 100644

> --- a/backend/src/llvm/llvm_passes.cpp

> +++ b/backend/src/llvm/llvm_passes.cpp

> @@ -222,7 +222,9 @@ namespace gbe

>    {

>      const uint32_t ptrSize = unit.getPointerSize();

>      Value* parentPointer = GEPInst->getOperand(0);

> -    CompositeType* CompTy = cast<CompositeType>(parentPointer-

> >getType());

> +    CompositeType* CompTy = parentPointer ?

> cast<CompositeType>(parentPointer->getType()) : NULL;

> +    if(!CompTy)

> +      return false;

> 

>      Value* currentAddrInst =

>        new PtrToIntInst(parentPointer, IntegerType::get(GEPInst->getContext(),

> ptrSize), "", GEPInst); @@ -254,6 +256,9 @@ namespace gbe

> 

>          Value *operand = GEPInst->getOperand(op);

> 

> +        if(!operand)

> +          continue;

> +

>          //HACK TODO: Inserted by type replacement.. this code could break

> something????

>          if(getTypeByteSize(unit, operand->getType())>4)

>          {

> diff --git a/backend/src/llvm/llvm_printf_parser.cpp

> b/backend/src/llvm/llvm_printf_parser.cpp

> index c9ec85f..a92c46d 100644

> --- a/backend/src/llvm/llvm_printf_parser.cpp

> +++ b/backend/src/llvm/llvm_printf_parser.cpp

> @@ -324,6 +324,9 @@ error:

>      int param_num = 0;

> 

>      llvm::Constant* arg0 = dyn_cast<llvm::ConstantExpr>(*CI_FMT);

> +    if(!arg0) {

> +      return false;

> +    }

>      llvm::Constant* arg0_ptr = dyn_cast<llvm::Constant>(arg0-

> >getOperand(0));

>      if (!arg0_ptr) {

>        return false;

> @@ -428,7 +431,12 @@ error:

>            continue;

>          }

> 

> -        if (call->getCalledFunction() && call->getCalledFunction()-

> >getIntrinsicID() != 0)

> +        llvm::Function * callFunc = call->getCalledFunction();

> +        if(!callFunc) {

> +          continue;

> +        }

> +

> +        if ( callFunc->getIntrinsicID() != 0)

>            continue;

> 

>          Value *Callee = call->getCalledValue(); @@ -560,6 +568,9 @@ error:

>          switch (slot.state.conversion_specifier) {

>            case PRINTF_CONVERSION_S: {

>              llvm::Constant* arg0 = dyn_cast<llvm::ConstantExpr>(arg);

> +            if(!arg0) {

> +              return false;

> +            }

>              llvm::Constant* arg0_ptr = dyn_cast<llvm::Constant>(arg0-

> >getOperand(0));

>              if (!arg0_ptr) {

>                return false;

> diff --git a/backend/src/llvm/llvm_sampler_fix.cpp

> b/backend/src/llvm/llvm_sampler_fix.cpp

> index 01db8fe..de7ebdb 100644

> --- a/backend/src/llvm/llvm_sampler_fix.cpp

> +++ b/backend/src/llvm/llvm_sampler_fix.cpp

> @@ -41,6 +41,8 @@ namespace gbe {

>      }

> 

>      bool visitCallInst(CallInst *I) {

> +      if(!I)

> +        return false;

>        Value *Callee = I->getCalledValue();

>        const std::string fnName = Callee->getName();

>        bool changed = false;

> diff --git a/backend/src/llvm/llvm_scalarize.cpp

> b/backend/src/llvm/llvm_scalarize.cpp

> index 3984218..53fd320 100644

> --- a/backend/src/llvm/llvm_scalarize.cpp

> +++ b/backend/src/llvm/llvm_scalarize.cpp

> @@ -173,6 +173,8 @@ namespace gbe {

>      }

> 

>      Type* GetBasicType(Type* type) {

> +      if(!type)

> +        return type;

>        switch(type->getTypeID()) {

>        case Type::VectorTyID:

>        case Type::ArrayTyID:

> @@ -184,14 +186,14 @@ namespace gbe {

>      }

> 

>      int GetComponentCount(const Type* type)  {

> -      if (type->getTypeID() == Type::VectorTyID)

> +      if (type && type->getTypeID() == Type::VectorTyID)

>          return llvm::dyn_cast<VectorType>(type)->getNumElements();

>        else

>          return 1;

>      }

> 

>      int GetComponentCount(const Value* value) {

> -      return GetComponentCount(value->getType());

> +      return GetComponentCount(value ? value->getType() : NULL);

>      }

> 

>      /* set to insert new instructions after the specified instruction.*/ @@ -

> 236,7 +238,7 @@ namespace gbe {

>    {

>      assert(canGetComponent(v) && "getComponent called on unhandled

> vector");

> 

> -    if (v->getType()->isVectorTy()) {

> +    if (v && v->getType() && v->getType()->isVectorTy()) {

>        if (ConstantDataVector* c = dyn_cast<ConstantDataVector>(v)) {

>          return c->getElementAsConstant(component);

>        } else if (ConstantVector* c = dyn_cast<ConstantVector>(v)) { @@ -337,7

> +339,7 @@ namespace gbe {

>    }

>    bool Scalarize::canGetComponent(Value* v)

>    {

> -    if (v->getType()->isVectorTy()) {

> +    if (v && v->getType() && v->getType()->isVectorTy()) {

>        if (isa<ConstantDataVector>(v) || isa<ConstantVector>(v) ||

> isa<ConstantAggregateZero>(v) || isa<UndefValue>(v)) {

>          return true;

>        } else {

> @@ -541,13 +543,18 @@ namespace gbe {

>      VectorValues& vVals = vectorVals[sv];

> 

>      int size = GetComponentCount(sv);

> -    int srcSize = GetComponentCount(sv->getOperand(0)->getType());

> +

> +    Value* Op0 = sv->getOperand(0);

> +    if(!Op0)

> +      return false;

> +

> +    int srcSize = GetComponentCount(Op0->getType());

> 

>      for (int i = 0; i < size; ++i) {

>        int select = sv->getMaskValue(i);

> 

>        if (select < 0) {

> -        setComponent(vVals, i, UndefValue::get(GetBasicType(sv-

> >getOperand(0))));

> +        setComponent(vVals, i, UndefValue::get(GetBasicType(Op0)));

>          continue;

>        }

> 

> @@ -736,7 +743,7 @@ namespace gbe {

>          //TODO: This is a implement for the non-constant index, we use an

> allocated new vector

>          //to store the need vector elements.

>          Value* foo = extr->getOperand(0);

> -        Type* fooTy = foo->getType();

> +        Type* fooTy = foo ? foo->getType() : NULL;

> 

>          Value* Alloc;

>          if(vectorAlloca.find(foo) == vectorAlloca.end()) @@ -807,7 +814,10 @@

> namespace gbe {

>        return;

>      ReversePostOrderTraversal<Function*> rpot(&F);

>      BasicBlock::iterator instI = (*rpot.begin())->begin();

> -    builder->SetInsertPoint(&*instI);

> +    Instruction* instVal = &*instI;

> +    if(instVal == nullptr)

> +      return;

> +    builder->SetInsertPoint(instVal);

> 

>      Function::arg_iterator I = F.arg_begin(), E = F.arg_end();

> 

> --

> 2.1.4

> 

> _______________________________________________

> Beignet mailing list

> Beignet@lists.freedesktop.org

> https://lists.freedesktop.org/mailman/listinfo/beignet