Skip to content

Commit dfd506b

Browse files
authored
[SPIRV] Fix code quality issues. (#152005)
Fix code quality issues reported by static analysis tool, such as: - Rule of Three/Five. - Dereference after null check. - Unchecked return value. - Variable copied when it could be moved.
1 parent 8704ca0 commit dfd506b

15 files changed

+52
-33
lines changed

llvm/lib/Target/SPIRV/Analysis/SPIRVConvergenceRegionAnalysis.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,11 @@ class ConvergenceRegion {
7373
Entry(std::move(CR.Entry)), Exits(std::move(CR.Exits)),
7474
Blocks(std::move(CR.Blocks)) {}
7575

76+
~ConvergenceRegion() { releaseMemory(); }
77+
78+
ConvergenceRegion &operator=(ConvergenceRegion &&CR) = delete;
7679
ConvergenceRegion(const ConvergenceRegion &other) = delete;
80+
ConvergenceRegion &operator=(const ConvergenceRegion &other) = delete;
7781

7882
// Returns true if the given basic block belongs to this region, or to one of
7983
// its subregion.
@@ -101,6 +105,9 @@ class ConvergenceRegionInfo {
101105

102106
~ConvergenceRegionInfo() { releaseMemory(); }
103107

108+
ConvergenceRegionInfo(const ConvergenceRegionInfo &LHS) = delete;
109+
ConvergenceRegionInfo &operator=(const ConvergenceRegionInfo &LHS) = delete;
110+
104111
ConvergenceRegionInfo(ConvergenceRegionInfo &&LHS)
105112
: TopLevelRegion(LHS.TopLevelRegion) {
106113
if (TopLevelRegion != LHS.TopLevelRegion) {

llvm/lib/Target/SPIRV/MCTargetDesc/SPIRVInstPrinter.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ void SPIRVInstPrinter::printOpConstantVarOps(const MCInst *MI,
9696
void SPIRVInstPrinter::recordOpExtInstImport(const MCInst *MI) {
9797
MCRegister Reg = MI->getOperand(0).getReg();
9898
auto Name = getSPIRVStringOperand(*MI, 1);
99-
auto Set = getExtInstSetFromString(Name);
99+
auto Set = getExtInstSetFromString(std::move(Name));
100100
ExtInstSetIDs.insert({Reg, Set});
101101
}
102102

@@ -210,6 +210,7 @@ void SPIRVInstPrinter::printInst(const MCInst *MI, uint64_t Address,
210210
case SPIRV::OpConstantF:
211211
// The last fixed operand along with any variadic operands that follow
212212
// are part of the variable value.
213+
assert(NumFixedOps > 0 && "Expected at least one fixed operand");
213214
printOpConstantVarOps(MI, NumFixedOps - 1, OS);
214215
break;
215216
case SPIRV::OpCooperativeMatrixMulAddKHR: {

llvm/lib/Target/SPIRV/SPIRVAPI.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ SPIRVTranslateModule(Module *M, std::string &SpirvObj, std::string &ErrMsg,
156156
}
157157
}
158158
return SPIRVTranslate(M, SpirvObj, ErrMsg, AllowExtNames, OLevel,
159-
TargetTriple);
159+
std::move(TargetTriple));
160160
}
161161

162162
} // namespace llvm

llvm/lib/Target/SPIRV/SPIRVAsmPrinter.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ class SPIRVAsmPrinter : public AsmPrinter {
5050
public:
5151
explicit SPIRVAsmPrinter(TargetMachine &TM,
5252
std::unique_ptr<MCStreamer> Streamer)
53-
: AsmPrinter(TM, std::move(Streamer), ID), ST(nullptr), TII(nullptr) {}
53+
: AsmPrinter(TM, std::move(Streamer), ID), ModuleSectionsEmitted(false),
54+
ST(nullptr), TII(nullptr), MAI(nullptr) {}
5455
static char ID;
5556
bool ModuleSectionsEmitted;
5657
const SPIRVSubtarget *ST;
@@ -591,7 +592,9 @@ void SPIRVAsmPrinter::outputAnnotations(const Module &M) {
591592
cast<GlobalVariable>(CS->getOperand(1)->stripPointerCasts());
592593

593594
StringRef AnnotationString;
594-
getConstantStringInfo(GV, AnnotationString);
595+
[[maybe_unused]] bool Success =
596+
getConstantStringInfo(GV, AnnotationString);
597+
assert(Success && "Failed to get annotation string");
595598
MCInst Inst;
596599
Inst.setOpcode(SPIRV::OpDecorate);
597600
Inst.addOperand(MCOperand::createReg(Reg));

llvm/lib/Target/SPIRV/SPIRVBuiltins.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ struct IncomingCall {
5151
IncomingCall(const std::string BuiltinName, const DemangledBuiltin *Builtin,
5252
const Register ReturnRegister, const SPIRVType *ReturnType,
5353
const SmallVectorImpl<Register> &Arguments)
54-
: BuiltinName(BuiltinName), Builtin(Builtin),
54+
: BuiltinName(std::move(BuiltinName)), Builtin(Builtin),
5555
ReturnRegister(ReturnRegister), ReturnType(ReturnType),
5656
Arguments(Arguments) {}
5757

@@ -2619,6 +2619,7 @@ static bool generateConvertInst(const StringRef DemangledCall,
26192619
GR->getSPIRVTypeID(Call->ReturnType));
26202620
}
26212621

2622+
assert(Builtin && "Conversion builtin not found.");
26222623
if (Builtin->IsSaturated)
26232624
buildOpDecorate(Call->ReturnRegister, MIRBuilder,
26242625
SPIRV::Decoration::SaturatedConversion, {});

llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ void SPIRVEmitIntrinsics::propagateElemTypeRec(
499499
std::unordered_set<Value *> Visited;
500500
DenseMap<Function *, CallInst *> Ptrcasts;
501501
propagateElemTypeRec(Op, PtrElemTy, CastElemTy, VisitedSubst, Visited,
502-
Ptrcasts);
502+
std::move(Ptrcasts));
503503
}
504504

505505
void SPIRVEmitIntrinsics::propagateElemTypeRec(
@@ -897,17 +897,16 @@ Type *SPIRVEmitIntrinsics::deduceNestedTypeHelper(
897897
bool Change = false;
898898
for (unsigned i = 0; i < U->getNumOperands(); ++i) {
899899
Value *Op = U->getOperand(i);
900+
assert(Op && "Operands should not be null.");
900901
Type *OpTy = Op->getType();
901902
Type *Ty = OpTy;
902-
if (Op) {
903-
if (auto *PtrTy = dyn_cast<PointerType>(OpTy)) {
904-
if (Type *NestedTy =
905-
deduceElementTypeHelper(Op, Visited, UnknownElemTypeI8))
906-
Ty = getTypedPointerWrapper(NestedTy, PtrTy->getAddressSpace());
907-
} else {
908-
Ty = deduceNestedTypeHelper(dyn_cast<User>(Op), OpTy, Visited,
909-
UnknownElemTypeI8);
910-
}
903+
if (auto *PtrTy = dyn_cast<PointerType>(OpTy)) {
904+
if (Type *NestedTy =
905+
deduceElementTypeHelper(Op, Visited, UnknownElemTypeI8))
906+
Ty = getTypedPointerWrapper(NestedTy, PtrTy->getAddressSpace());
907+
} else {
908+
Ty = deduceNestedTypeHelper(dyn_cast<User>(Op), OpTy, Visited,
909+
UnknownElemTypeI8);
911910
}
912911
Tys.push_back(Ty);
913912
Change |= Ty != OpTy;

llvm/lib/Target/SPIRV/SPIRVEmitNonSemanticDI.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ bool SPIRVEmitNonSemanticDI::emitGlobalDI(MachineFunction &MF) {
116116
}
117117
}
118118
const NamedMDNode *ModuleFlags = M->getNamedMetadata("llvm.module.flags");
119+
assert(ModuleFlags && "Expected llvm.module.flags metadata to be present");
119120
for (const auto *Op : ModuleFlags->operands()) {
120121
const MDOperand &MaybeStrOp = Op->getOperand(1);
121122
if (MaybeStrOp.equalsStr("Dwarf Version"))

llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ storageClassRequiresExplictLayout(SPIRV::StorageClass::StorageClass SC) {
8787
}
8888

8989
SPIRVGlobalRegistry::SPIRVGlobalRegistry(unsigned PointerSize)
90-
: PointerSize(PointerSize), Bound(0) {}
90+
: PointerSize(PointerSize), Bound(0), CurMF(nullptr) {}
9191

9292
SPIRVType *SPIRVGlobalRegistry::assignIntTypeToVReg(unsigned BitWidth,
9393
Register VReg,
@@ -474,8 +474,8 @@ Register SPIRVGlobalRegistry::getOrCreateBaseRegister(
474474
}
475475
if (Type->getOpcode() == SPIRV::OpTypeFloat) {
476476
SPIRVType *SpvBaseType = getOrCreateSPIRVFloatType(BitWidth, I, TII);
477-
return getOrCreateConstFP(dyn_cast<ConstantFP>(Val)->getValue(), I,
478-
SpvBaseType, TII, ZeroAsNull);
477+
return getOrCreateConstFP(cast<ConstantFP>(Val)->getValue(), I, SpvBaseType,
478+
TII, ZeroAsNull);
479479
}
480480
assert(Type->getOpcode() == SPIRV::OpTypeInt);
481481
SPIRVType *SpvBaseType = getOrCreateSPIRVIntegerType(BitWidth, I, TII);
@@ -1069,7 +1069,8 @@ SPIRVType *SPIRVGlobalRegistry::createSPIRVType(
10691069
MIRBuilder);
10701070
};
10711071
}
1072-
return getOpTypeStruct(SType, MIRBuilder, AccQual, Decorator, EmitIR);
1072+
return getOpTypeStruct(SType, MIRBuilder, AccQual, std::move(Decorator),
1073+
EmitIR);
10731074
}
10741075
if (auto FType = dyn_cast<FunctionType>(Ty)) {
10751076
SPIRVType *RetTy = findSPIRVType(FType->getReturnType(), MIRBuilder,
@@ -1406,8 +1407,9 @@ SPIRVType *SPIRVGlobalRegistry::getOrCreateLayoutType(
14061407
// We need a new OpTypeStruct instruction because decorations will be
14071408
// different from a struct with an explicit layout created from a different
14081409
// entry point.
1409-
SPIRVType *SPIRVStructType = getOpTypeStruct(
1410-
ST, MIRBuilder, SPIRV::AccessQualifier::None, Decorator, EmitIr);
1410+
SPIRVType *SPIRVStructType =
1411+
getOpTypeStruct(ST, MIRBuilder, SPIRV::AccessQualifier::None,
1412+
std::move(Decorator), EmitIr);
14111413
add(Key, SPIRVStructType);
14121414
return SPIRVStructType;
14131415
}

llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ SPIRVInstructionSelector::SPIRVInstructionSelector(const SPIRVTargetMachine &TM,
362362
const RegisterBankInfo &RBI)
363363
: InstructionSelector(), STI(ST), TII(*ST.getInstrInfo()),
364364
TRI(*ST.getRegisterInfo()), RBI(RBI), GR(*ST.getSPIRVGlobalRegistry()),
365+
MRI(nullptr),
365366
#define GET_GLOBALISEL_PREDICATES_INIT
366367
#include "SPIRVGenGlobalISel.inc"
367368
#undef GET_GLOBALISEL_PREDICATES_INIT
@@ -3574,7 +3575,7 @@ bool SPIRVInstructionSelector::selectFirstBitSet64Overflow(
35743575

35753576
// Join all the resulting registers back into the return type in order
35763577
// (ie i32x2, i32x2, i32x1 -> i32x5)
3577-
return selectOpWithSrcs(ResVReg, ResType, I, PartialRegs,
3578+
return selectOpWithSrcs(ResVReg, ResType, I, std::move(PartialRegs),
35783579
SPIRV::OpCompositeConstruct);
35793580
}
35803581

llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ getSymbolicOperandRequirements(SPIRV::OperandCategory::OperandCategory Category,
9393
if (Reqs.isCapabilityAvailable(Cap)) {
9494
ReqExts.append(getSymbolicOperandExtensions(
9595
SPIRV::OperandCategory::CapabilityOperand, Cap));
96-
return {true, {Cap}, ReqExts, ReqMinVer, ReqMaxVer};
96+
return {true, {Cap}, std::move(ReqExts), ReqMinVer, ReqMaxVer};
9797
}
9898
} else {
9999
// By SPIR-V specification: "If an instruction, enumerant, or other
@@ -111,7 +111,7 @@ getSymbolicOperandRequirements(SPIRV::OperandCategory::OperandCategory Category,
111111
if (i == Sz - 1 || !AvoidCaps.S.contains(Cap)) {
112112
ReqExts.append(getSymbolicOperandExtensions(
113113
SPIRV::OperandCategory::CapabilityOperand, Cap));
114-
return {true, {Cap}, ReqExts, ReqMinVer, ReqMaxVer};
114+
return {true, {Cap}, std::move(ReqExts), ReqMinVer, ReqMaxVer};
115115
}
116116
}
117117
}
@@ -558,7 +558,7 @@ static void collectOtherInstr(MachineInstr &MI, SPIRV::ModuleAnalysisInfo &MAI,
558558
bool Append = true) {
559559
MAI.setSkipEmission(&MI);
560560
InstrSignature MISign = instrToSignature(MI, MAI, true);
561-
auto FoundMI = IS.insert(MISign);
561+
auto FoundMI = IS.insert(std::move(MISign));
562562
if (!FoundMI.second)
563563
return; // insert failed, so we found a duplicate; don't add it to MAI.MS
564564
// No duplicates, so add it.

0 commit comments

Comments
 (0)