Skip to content

Commit 74681f6

Browse files
authored
Fix issues reported by static analyzer tool (#1787)
1 parent c18de44 commit 74681f6

File tree

5 files changed

+16
-13
lines changed

5 files changed

+16
-13
lines changed

lib/SPIRV/SPIRVLowerBitCastToNonStandardType.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ SPIRVLowerBitCastToNonStandardTypePass::run(Function &F,
203203
// Some vector-valued instructions were replaced with undef values, so if
204204
// that's what we got, it's still a dead instruction.
205205
if (VH.pointsToAliveValue() && !isa<UndefValue>(VH)) {
206-
auto *VT = dyn_cast<VectorType>(VH->getType());
206+
auto *VT = cast<VectorType>(VH->getType());
207207
report_fatal_error(Twine("Unsupported vector type with ") +
208208
Twine(VT->getElementCount().getFixedValue()) +
209209
Twine(" elements"),

lib/SPIRV/SPIRVReader.cpp

+6-8
Original file line numberDiff line numberDiff line change
@@ -1425,12 +1425,12 @@ Value *SPIRVToLLVM::transValueWithoutDecoration(SPIRVValue *BV, Function *F,
14251425
case OpTypeVector:
14261426
return mapValue(BV, ConstantVector::get(CV));
14271427
case OpTypeMatrix:
1428-
case OpTypeArray:
1429-
return mapValue(
1430-
BV, ConstantArray::get(dyn_cast<ArrayType>(transType(BCC->getType())),
1431-
CV));
1428+
case OpTypeArray: {
1429+
auto *AT = cast<ArrayType>(transType(BCC->getType()));
1430+
return mapValue(BV, ConstantArray::get(AT, CV));
1431+
}
14321432
case OpTypeStruct: {
1433-
auto BCCTy = dyn_cast<StructType>(transType(BCC->getType()));
1433+
auto *BCCTy = cast<StructType>(transType(BCC->getType()));
14341434
auto Members = BCCTy->getNumElements();
14351435
auto Constants = CV.size();
14361436
// if we try to initialize constant TypeStruct, add bitcasts
@@ -1447,9 +1447,7 @@ Value *SPIRVToLLVM::transValueWithoutDecoration(SPIRVValue *BV, Function *F,
14471447
}
14481448
}
14491449

1450-
return mapValue(BV,
1451-
ConstantStruct::get(
1452-
dyn_cast<StructType>(transType(BCC->getType())), CV));
1450+
return mapValue(BV, ConstantStruct::get(BCCTy, CV));
14531451
}
14541452
default:
14551453
llvm_unreachable("not implemented");

lib/SPIRV/SPIRVToOCL.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ void SPIRVToOCLBase::visitCallInst(CallInst &CI) {
7373
case OpenCLLIB::Printf: {
7474
// TODO: Lower the printf instruction with the non-constant address space
7575
// format string to suitable for OpenCL representation
76-
if (dyn_cast<PointerType>(CI.getOperand(0)->getType())
77-
->getAddressSpace() == SPIR::TypeAttributeEnum::ATTR_CONST)
76+
auto *PT = dyn_cast<PointerType>(CI.getOperand(0)->getType());
77+
if (PT && PT->getAddressSpace() == SPIR::TypeAttributeEnum::ATTR_CONST)
7878
visitCallSPIRVPrintf(&CI, ExtOp);
7979
break;
8080
}

lib/SPIRV/SPIRVUtil.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -1381,8 +1381,10 @@ Value *getScalarOrArray(Value *V, unsigned Size, Instruction *Pos) {
13811381
auto GEP = cast<GEPOperator>(V);
13821382
assert(GEP->getNumOperands() == 3 && "must be a GEP from an array");
13831383
assert(GEP->getSourceElementType()->getArrayNumElements() == Size);
1384-
assert(dyn_cast<ConstantInt>(GEP->getOperand(1))->getZExtValue() == 0);
1385-
assert(dyn_cast<ConstantInt>(GEP->getOperand(2))->getZExtValue() == 0);
1384+
[[maybe_unused]] auto *OP1 = cast<ConstantInt>(GEP->getOperand(1));
1385+
[[maybe_unused]] auto *OP2 = cast<ConstantInt>(GEP->getOperand(2));
1386+
assert(OP1->getZExtValue() == 0);
1387+
assert(OP2->getZExtValue() == 0);
13861388
return new LoadInst(GEP->getSourceElementType(), GEP->getOperand(0), "", Pos);
13871389
}
13881390

lib/SPIRV/libSPIRV/SPIRVError.h

+3
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,9 @@ inline bool SPIRVErrorLog::checkError(bool Cond, SPIRVErrorCode ErrCode,
164164
break;
165165
case SPIRVDbgErrorHandlingKinds::Ignore:
166166
// Still print info about the error into debug output stream
167+
// TODO: The value Ignore is not currently used but if it would be used
168+
// then places where this routine is called must be checked as just
169+
// ignoring the error may lead to NULL pointer dereferences
167170
spvdbgs() << SS.str() << '\n';
168171
spvdbgs().flush();
169172
break;

0 commit comments

Comments
 (0)