Skip to content

Commit

Permalink
Refactor null checks and dereferences
Browse files Browse the repository at this point in the history
Add asserts to check for nulls.
Remove unnecessary nullptr checks.
  • Loading branch information
fda0 authored and igcbot committed Sep 21, 2023
1 parent 27557fb commit 5db0dac
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 24 deletions.
39 changes: 30 additions & 9 deletions IGC/AdaptorOCL/OCL/sp/spp_g8.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,10 @@ static std::string getTempFileName(const IGC::COpenCLKernel* kernel)
EC = sys::fs::getPotentiallyUniqueTempFileName("ze", "elf", FileName);
if (EC) {
IGC::CodeGenContext* ctx = kernel->GetContext();
ctx->EmitError((std::string("unable to create temporary file for kernel: ") + kernel->m_kernelInfo.m_kernelName).c_str(), nullptr);
if (ctx)
{
ctx->EmitError((std::string("unable to create temporary file for kernel: ") + kernel->m_kernelInfo.m_kernelName).c_str(), nullptr);
}
return "";
}
return FileName.c_str();
Expand Down Expand Up @@ -623,7 +626,10 @@ bool CGen8OpenCLProgram::GetZEBinary(
}
else
{
ctx->EmitError((std::string("failed to open ELF file: ") + elfFileNameStr).c_str(), nullptr);
if (ctx)
{
ctx->EmitError((std::string("failed to open ELF file: ") + elfFileNameStr).c_str(), nullptr);
}
elfTmpFilesError = true; // Handle this error at the end of this function
break;
}
Expand Down Expand Up @@ -722,23 +728,32 @@ bool CGen8OpenCLProgram::GetZEBinary(
else
{
ECfileOpen = MBOrErr.getError();
ctx->EmitError("ELF linked file cannot be buffered", nullptr);
ctx->EmitError(ECfileOpen.message().c_str(), nullptr);
if (ctx)
{
ctx->EmitError("ELF linked file cannot be buffered", nullptr);
ctx->EmitError(ECfileOpen.message().c_str(), nullptr);
}
elfTmpFilesError = true; // Handle this error also below
}
}
else
{
ECfileOpen = errorToErrorCode(FDOrErr.takeError());
ctx->EmitError("ELF linked file cannot be opened", nullptr);
ctx->EmitError(ECfileOpen.message().c_str(), nullptr);
if (ctx)
{
ctx->EmitError("ELF linked file cannot be opened", nullptr);
ctx->EmitError(ECfileOpen.message().c_str(), nullptr);
}
elfTmpFilesError = true; // Handle this error also below

}
}
else
{
ctx->EmitError("ELF linked file size error", nullptr);
if (ctx)
{
ctx->EmitError("ELF linked file size error", nullptr);
}
elfTmpFilesError = true; // Handle this error also below
}
}
Expand All @@ -751,7 +766,10 @@ bool CGen8OpenCLProgram::GetZEBinary(
#endif // LLVM_VERSION_MAJOR
if (!linkErrStr.empty())
{
ctx->EmitError(linkErrStr.c_str(), nullptr);
if (ctx)
{
ctx->EmitError(linkErrStr.c_str(), nullptr);
}
elfTmpFilesError = true; // Handle this error also below
}
}
Expand All @@ -761,7 +779,10 @@ bool CGen8OpenCLProgram::GetZEBinary(
if (elfTmpFilesError)
{
// Nothing to do with the linker when any error with temporary files occured.
ctx->EmitError("ZeBinary will not contain correct debug info due to an ELF temporary files error", nullptr);
if (ctx)
{
ctx->EmitError("ZeBinary will not contain correct debug info due to an ELF temporary files error", nullptr);
}
retValue = false;
}

Expand Down
10 changes: 5 additions & 5 deletions IGC/Compiler/CISACodeGen/EmitVISAPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20299,11 +20299,11 @@ void EmitPass::emitDpas(GenIntrinsicInst* GII, const SSource* Sources, const Dst
}

// float dpas uses short as bfloat16 for either input or dst.
ConstantInt* pa = dyn_cast<ConstantInt>(GII->getOperand(3)); // Activation's precision
ConstantInt* pb = dyn_cast<ConstantInt>(GII->getOperand(4)); // Weight's precision
ConstantInt* sdepth = dyn_cast<ConstantInt>(GII->getOperand(5));
ConstantInt* rcount = dyn_cast<ConstantInt>(GII->getOperand(6));
ConstantInt* dpasw = dyn_cast<ConstantInt>(GII->getOperand(7));
ConstantInt* pa = cast<ConstantInt>(GII->getOperand(3)); // Activation's precision
ConstantInt* pb = cast<ConstantInt>(GII->getOperand(4)); // Weight's precision
ConstantInt* sdepth = cast<ConstantInt>(GII->getOperand(5));
ConstantInt* rcount = cast<ConstantInt>(GII->getOperand(6));
ConstantInt* dpasw = cast<ConstantInt>(GII->getOperand(7));
int PA = (int)pa->getSExtValue();
int PB = (int)pb->getSExtValue();
int SD = (int)sdepth->getSExtValue();
Expand Down
11 changes: 4 additions & 7 deletions IGC/Metrics/IGCMetricImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -954,16 +954,13 @@ namespace IGCMetrics
llvm::MetadataAsValue* MDValue = makeMDasVal(New);

llvm::Instruction* insertAfter = nullptr;
if (llvm::isa<llvm::Argument>(New))
if (auto arg = llvm::dyn_cast<llvm::Argument>(New))
{
auto firstInstr = &*llvm::dyn_cast<llvm::Argument>(New)
->getParent()->getEntryBlock().begin();

insertAfter = firstInstr;
insertAfter = &*arg->getParent()->getEntryBlock().begin();
}
else if (llvm::isa<llvm::Instruction>(New))
else if (auto instruction = llvm::dyn_cast<llvm::Instruction>(New))
{
insertAfter = llvm::dyn_cast<llvm::Instruction>(New);
insertAfter = instruction;
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion visa/GraphColor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3298,7 +3298,7 @@ void Augmentation::markNonDefaultDstRgn(G4_INST *inst, G4_Operand *opnd) {
}

// Handle dst
if (inst->isCall() || inst->isCallerSave()) {
if (dst && (inst->isCall() || inst->isCallerSave())) {
const G4_Declare *dcl = dst->getBase()->asRegVar()->getDeclare();
if (dcl && liveAnalysis.livenessClass(dcl->getRegFile())) {
gra.setAugmentationMask(dcl->getRootDeclare(),
Expand Down
1 change: 1 addition & 0 deletions visa/PointsToAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ void PointsToAnalysis::doPointsToAnalysis(FlowGraph &fg) {
G4_VarBase *srcPtr = src->isSrcRegRegion()
? src->asSrcRegRegion()->getBase()
: nullptr;
vASSERT(srcPtr);
// case: arithmetic-op A0 A1 src1
// merge the two addr's points-to set together
G4_RegVar *ptrVar = ptr->asRegVar();
Expand Down
2 changes: 1 addition & 1 deletion visa/RADebug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -809,7 +809,7 @@ bool VerifyAugmentation::isClobbered(LiveRange *lr, std::string &msg) {
if (oiDst && oiDst->isDstRegRegion() && oiDst->getTopDcl()) {
unsigned oilb = 0, oirb = 0;
auto oiLR = DclLRMap[oiDst->getTopDcl()];
if (oiLR && !oiLR->getPhyReg())
if (!oiLR->getPhyReg())
continue;

oilb = oiLR->getPhyReg()->asGreg()->getRegNum() *
Expand Down
4 changes: 3 additions & 1 deletion visa/VisaToG4/TranslateSendLdStLegacy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1235,6 +1235,8 @@ int IR_Builder::translateVISADwordAtomicInst(
((atomicOp != ATOMIC_FADD) && (atomicOp != ATOMIC_FSUB)),
"FADD/FSUB atomic operations are only supported on this devices");

vASSERT(surface);

VISA_Exec_Size instExecSize = execSize;
execSize = roundUpExecSize(execSize);

Expand All @@ -1256,7 +1258,7 @@ int IR_Builder::translateVISADwordAtomicInst(
unsigned len = 0;

bool useHeader =
needsA32MsgHeader() && surface && isStatelessSurface(surface);
needsA32MsgHeader() && isStatelessSurface(surface);
if (useHeader) {
G4_Declare *dcl = createSendPayloadDcl(getGenxDataportIOSize(), Type_UD);

Expand Down

0 comments on commit 5db0dac

Please sign in to comment.