Skip to content

[NFC] Use more isa and isa_and_nonnull instead dyn_cast for predicates #137393

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions clang/lib/Sema/SemaChecking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10052,9 +10052,10 @@ void CheckFreeArgumentsAddressof(Sema &S, const std::string &CalleeName,
const UnaryOperator *UnaryExpr) {
if (const auto *Lvalue = dyn_cast<DeclRefExpr>(UnaryExpr->getSubExpr())) {
const Decl *D = Lvalue->getDecl();
if (isa<DeclaratorDecl>(D))
if (!dyn_cast<DeclaratorDecl>(D)->getType()->isReferenceType())
if (auto *DD = dyn_cast<DeclaratorDecl>(D)) {
if (!DD->getType()->isReferenceType())
return CheckFreeArgumentsOnLvalue(S, CalleeName, UnaryExpr, D);
}
}

if (const auto *Lvalue = dyn_cast<MemberExpr>(UnaryExpr->getSubExpr()))
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Sema/SemaModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -942,7 +942,7 @@ static bool checkExportedDecl(Sema &S, Decl *D, SourceLocation BlockStart) {
// HLSL: export declaration is valid only on functions
if (S.getLangOpts().HLSL) {
// Export-within-export was already diagnosed in ActOnStartExportDecl
if (!dyn_cast<FunctionDecl>(D) && !dyn_cast<ExportDecl>(D)) {
if (!isa<FunctionDecl, ExportDecl>(D)) {
S.Diag(D->getBeginLoc(), diag::err_hlsl_export_not_on_function);
D->setInvalidDecl();
return false;
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Sema/SemaOpenMP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2307,7 +2307,7 @@ bool SemaOpenMP::isInOpenMPTargetExecutionDirective() const {

bool SemaOpenMP::isOpenMPRebuildMemberExpr(ValueDecl *D) {
// Only rebuild for Field.
if (!dyn_cast<FieldDecl>(D))
if (!isa<FieldDecl>(D))
return false;
DSAStackTy::DSAVarData DVarPrivate = DSAStack->hasDSA(
D,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ class RawPtrRefLocalVarsChecker
SmallString<100> Buf;
llvm::raw_svector_ostream Os(Buf);

if (dyn_cast<ParmVarDecl>(V)) {
if (isa<ParmVarDecl>(V)) {
Os << "Assignment to an " << ptrKind() << " parameter ";
printQuotedQualifiedName(Os, V);
Os << " is unsafe.";
Expand Down
5 changes: 2 additions & 3 deletions lld/MachO/SymbolTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ static const Symbol *getAlternativeSpelling(const Undefined &sym,

// If in the symbol table and not undefined.
if (const Symbol *s = symtab->find(newName))
if (dyn_cast<Undefined>(s) == nullptr)
if (!isa<Undefined>(s))
return s;

return nullptr;
Expand Down Expand Up @@ -567,8 +567,7 @@ static const Symbol *getAlternativeSpelling(const Undefined &sym,
if (name.equals_insensitive(it.first))
return it.second;
for (Symbol *sym : symtab->getSymbols())
if (dyn_cast<Undefined>(sym) == nullptr &&
name.equals_insensitive(sym->getName()))
if (!isa<Undefined>(sym) && name.equals_insensitive(sym->getName()))
return sym;

// The reference may be a mangled name while the definition is not. Suggest a
Expand Down
2 changes: 1 addition & 1 deletion llvm/include/llvm/Analysis/LoopInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ class LoopInfo : public LoopInfoBase<BasicBlock, Loop> {
}

// Return true if a new use of V added in ExitBB would require an LCSSA PHI
// to be inserted at the begining of the block. Note that V is assumed to
// to be inserted at the beginning of the block. Note that V is assumed to
// dominate ExitBB, and ExitBB must be the exit block of some loop. The
// IR is assumed to be in LCSSA form before the planned insertion.
bool wouldBeOutOfLoopUseRequiringLCSSA(const Value *V,
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Bitcode/Reader/MetadataLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ class MetadataLoader::MetadataLoaderImpl {
SetVector<Metadata *> EntitiesToRemove;
for (Metadata *Op : CU->getImportedEntities()->operands()) {
auto *IE = cast<DIImportedEntity>(Op);
if (dyn_cast_or_null<DILocalScope>(IE->getScope())) {
if (isa_and_nonnull<DILocalScope>(IE->getScope())) {
EntitiesToRemove.insert(IE);
}
}
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/IR/Verifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6584,7 +6584,7 @@ void Verifier::visitIntrinsicCall(Intrinsic::ID ID, CallBase &Call) {
for (BasicBlock *ColorFirstBB : CV)
if (auto It = ColorFirstBB->getFirstNonPHIIt();
It != ColorFirstBB->end())
if (dyn_cast_or_null<FuncletPadInst>(&*It))
if (isa_and_nonnull<FuncletPadInst>(&*It))
InEHFunclet = true;

// Check for funclet operand bundle
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ bool WebAssemblyTTIImpl::isProfitableToSinkOperands(

Value *V = I->getOperand(1);
// We dont need to sink constant splat.
if (dyn_cast<Constant>(V))
if (isa<Constant>(V))
return false;

if (match(V, m_Shuffle(m_InsertElt(m_Value(), m_Value(), m_ZeroInt()),
Expand Down
2 changes: 1 addition & 1 deletion mlir/lib/Conversion/LLVMCommon/TypeConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ LLVMTypeConverter::promoteOperands(Location loc, ValueRange opOperands,
if (useBarePtrCallConv) {
// For the bare-ptr calling convention, we only have to extract the
// aligned pointer of a memref.
if (dyn_cast<MemRefType>(operand.getType())) {
if (isa<MemRefType>(operand.getType())) {
MemRefDescriptor desc(llvmOperand);
llvmOperand = desc.alignedPtr(builder, loc);
} else if (isa<UnrankedMemRefType>(operand.getType())) {
Expand Down
4 changes: 2 additions & 2 deletions mlir/lib/Conversion/MathToFuncs/MathToFuncs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ static func::FuncOp createElementFPowIFunc(ModuleOp *module,
LogicalResult
FPowIOpLowering::matchAndRewrite(math::FPowIOp op,
PatternRewriter &rewriter) const {
if (dyn_cast<VectorType>(op.getType()))
if (isa<VectorType>(op.getType()))
return rewriter.notifyMatchFailure(op, "non-scalar operation");

FunctionType funcType = getElementalFuncTypeForOp(op);
Expand Down Expand Up @@ -751,7 +751,7 @@ static func::FuncOp createCtlzFunc(ModuleOp *module, Type elementType) {
/// operation.
LogicalResult CtlzOpLowering::matchAndRewrite(math::CountLeadingZerosOp op,
PatternRewriter &rewriter) const {
if (dyn_cast<VectorType>(op.getType()))
if (isa<VectorType>(op.getType()))
return rewriter.notifyMatchFailure(op, "non-scalar operation");

Type type = getElementTypeOrSelf(op.getResult().getType());
Expand Down
2 changes: 1 addition & 1 deletion mlir/lib/Conversion/MemRefToLLVM/MemRefToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -890,7 +890,7 @@ struct RankOpLowering : public ConvertOpToLLVMPattern<memref::RankOp> {
ConversionPatternRewriter &rewriter) const override {
Location loc = op.getLoc();
Type operandType = op.getMemref().getType();
if (dyn_cast<UnrankedMemRefType>(operandType)) {
if (isa<UnrankedMemRefType>(operandType)) {
UnrankedMemRefDescriptor desc(adaptor.getMemref());
rewriter.replaceOp(op, {desc.rank(rewriter, loc)});
return success();
Expand Down
2 changes: 1 addition & 1 deletion mlir/lib/Dialect/AMDGPU/Transforms/TransferReadToLoad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ static LogicalResult transferPreconditions(
return rewriter.notifyMatchFailure(xferOp, "not a memref source");

Attribute addrSpace = memRefType.getMemorySpace();
if (!addrSpace || !dyn_cast<amdgpu::AddressSpaceAttr>(addrSpace))
if (!isa_and_nonnull<amdgpu::AddressSpaceAttr>(addrSpace))
return rewriter.notifyMatchFailure(xferOp, "no address space");

if (dyn_cast<amdgpu::AddressSpaceAttr>(addrSpace).getValue() !=
Expand Down
2 changes: 1 addition & 1 deletion mlir/lib/Dialect/Affine/IR/AffineOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2194,7 +2194,7 @@ static void printBound(AffineMapAttr boundMap,
// Print bound that consists of a single SSA symbol if the map is over a
// single symbol.
if (map.getNumDims() == 0 && map.getNumSymbols() == 1) {
if (dyn_cast<AffineSymbolExpr>(expr)) {
if (isa<AffineSymbolExpr>(expr)) {
p.printOperand(*boundOperands.begin());
return;
}
Expand Down
4 changes: 2 additions & 2 deletions mlir/lib/Dialect/Arith/Utils/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ static Value convertScalarToComplexDtype(ImplicitLocOpBuilder &b, Value operand,
}
}

if (dyn_cast<FloatType>(operand.getType())) {
if (isa<FloatType>(operand.getType())) {
FloatType toFpTy = cast<FloatType>(targetType.getElementType());
auto toBitwidth = toFpTy.getIntOrFloatBitWidth();
Value from = operand;
Expand All @@ -220,7 +220,7 @@ static Value convertScalarToComplexDtype(ImplicitLocOpBuilder &b, Value operand,
return b.create<complex::CreateOp>(targetType, from, zero);
}

if (dyn_cast<IntegerType>(operand.getType())) {
if (isa<IntegerType>(operand.getType())) {
FloatType toFpTy = cast<FloatType>(targetType.getElementType());
Value from = operand;
if (isUnsigned) {
Expand Down
6 changes: 3 additions & 3 deletions mlir/lib/Dialect/Async/Transforms/AsyncRuntimeRefCounting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -534,15 +534,15 @@ void AsyncRuntimePolicyBasedRefCountingPass::initializeDefaultPolicy() {
bool isValue = isa<ValueType>(type);

// Drop reference after async token or group error check (coro await).
if (dyn_cast<RuntimeIsErrorOp>(op))
if (isa<RuntimeIsErrorOp>(op))
return (isToken || isGroup) ? -1 : 0;

// Drop reference after async value load.
if (dyn_cast<RuntimeLoadOp>(op))
if (isa<RuntimeLoadOp>(op))
return isValue ? -1 : 0;

// Drop reference after async token added to the group.
if (dyn_cast<RuntimeAddToGroupOp>(op))
if (isa<RuntimeAddToGroupOp>(op))
return isToken ? -1 : 0;

return 0;
Expand Down
2 changes: 1 addition & 1 deletion mlir/lib/Dialect/Linalg/IR/LinalgInterfaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1296,7 +1296,7 @@ LogicalResult mlir::linalg::detail::verifyStructuredOpInterface(Operation *op) {
"unexpected result less than 0 at expression #")
<< dim << " in " << mapStr;
}
if (dyn_cast<AffineDimExpr>(indexingMap.getResult(dim))) {
if (isa<AffineDimExpr>(indexingMap.getResult(dim))) {
if (inferredDimSize != shape[dim]) {
return op->emitOpError("inferred input/output operand #")
<< opOperand.getOperandNumber() << " has shape's dimension #"
Expand Down
4 changes: 2 additions & 2 deletions mlir/lib/Dialect/Linalg/TransformOps/LinalgTransformOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1211,7 +1211,7 @@ DiagnosedSilenceableFailure transform::LinalgCopyToMemrefOp::applyToOne(
// linalg.copy supports different element types on source/dest whereas
// memref.copy does not, so we must check that the source and dest types can
// be handled by memref.copy and otherwise reject the transformation.
if (!dyn_cast<ShapedType>(input.getType())) {
if (!isa<ShapedType>(input.getType())) {
DiagnosedSilenceableFailure diag =
emitSilenceableError()
<< "cannot transform a linalg.copy which input has no shape";
Expand All @@ -1220,7 +1220,7 @@ DiagnosedSilenceableFailure transform::LinalgCopyToMemrefOp::applyToOne(
}

// linalg.copy destination must be a shaped type.
assert(dyn_cast<ShapedType>(output.getType()));
assert(isa<ShapedType>(output.getType()));

if (cast<ShapedType>(input.getType()).getElementType() !=
cast<ShapedType>(output.getType()).getElementType()) {
Expand Down
7 changes: 3 additions & 4 deletions mlir/lib/Dialect/Linalg/Transforms/Detensorize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,8 @@ struct LinalgDetensorize
// * Add the argument to blockArgsToDetensor.
// * Walk the use-def chain backwards to add each predecessor's
// terminator-operands corresponding to currentItem to workList.
if (dyn_cast<BlockArgument>(currentItem)) {
BlockArgument currentItemBlockArgument =
cast<BlockArgument>(currentItem);
if (auto currentItemBlockArgument =
dyn_cast<BlockArgument>(currentItem)) {
Block *ownerBlock = currentItemBlockArgument.getOwner();

// Function arguments are not detensored/converted.
Expand Down Expand Up @@ -414,7 +413,7 @@ struct LinalgDetensorize
Block *block = blockArg.getParentBlock();

// For the potentially detensorable block argument, find the
// correpsonding operands in predecessor blocks.
// corresponding operands in predecessor blocks.
for (PredecessorIterator pred = block->pred_begin();
pred != block->pred_end(); ++pred) {
BranchOpInterface terminator =
Expand Down
2 changes: 1 addition & 1 deletion mlir/lib/Dialect/Linalg/Transforms/Loops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ struct FoldAffineOp : public RewritePattern {
}
return failure();
}
if (dyn_cast<AffineDimExpr>(expr) || dyn_cast<AffineSymbolExpr>(expr)) {
if (isa<AffineDimExpr, AffineSymbolExpr>(expr)) {
rewriter.replaceOp(op, op->getOperand(0));
return success();
}
Expand Down
2 changes: 1 addition & 1 deletion mlir/lib/Dialect/Quant/Utils/UniformSupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Type ExpressedToQuantizedConverter::convert(QuantizedType elementalType) const {
assert(expressedType && "convert() on unsupported conversion");
if (auto tensorType = dyn_cast<RankedTensorType>(inputType))
return RankedTensorType::get(tensorType.getShape(), elementalType);
if (dyn_cast<UnrankedTensorType>(inputType))
if (isa<UnrankedTensorType>(inputType))
return UnrankedTensorType::get(elementalType);
if (auto vectorType = dyn_cast<VectorType>(inputType))
return VectorType::get(vectorType.getShape(), elementalType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1284,7 +1284,7 @@ struct DirectConvertRewriter : public OpRewritePattern<ConvertOp> {

bool fromSparseConst = false;
if (auto constOp = op.getSource().getDefiningOp<arith::ConstantOp>())
if (dyn_cast<SparseElementsAttr>(constOp.getValue()))
if (isa<SparseElementsAttr>(constOp.getValue()))
fromSparseConst = true;

const AffineMapAttr foreachOrder =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ Value mlir::sparse_tensor::genIsNonzero(OpBuilder &builder, mlir::Location loc,
if (tp.isIntOrIndex())
return builder.create<arith::CmpIOp>(loc, arith::CmpIPredicate::ne, v,
zero);
if (dyn_cast<ComplexType>(tp))
if (isa<ComplexType>(tp))
return builder.create<complex::NotEqualOp>(loc, v, zero);
llvm_unreachable("Non-numeric type");
}
Expand Down
2 changes: 1 addition & 1 deletion mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ struct TransposeIsReshape : public OpRewritePattern<tosa::TransposeOp> {

Value result = op.getResult();
for (Operation *subop : result.getUsers()) {
if (dyn_cast_or_null<tosa::TransposeOp>(subop))
if (isa_and_nonnull<tosa::TransposeOp>(subop))
return rewriter.notifyMatchFailure(
op, "Dest is used by transpose, can compose transposes");
}
Expand Down
4 changes: 2 additions & 2 deletions mlir/lib/IR/Diagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -392,11 +392,11 @@ struct SourceMgrDiagnosticHandlerImpl {

/// Return a processable CallSiteLoc from the given location.
static std::optional<CallSiteLoc> getCallSiteLoc(Location loc) {
if (dyn_cast<NameLoc>(loc))
if (isa<NameLoc>(loc))
return getCallSiteLoc(cast<NameLoc>(loc).getChildLoc());
if (auto callLoc = dyn_cast<CallSiteLoc>(loc))
return callLoc;
if (dyn_cast<FusedLoc>(loc)) {
if (isa<FusedLoc>(loc)) {
for (auto subLoc : cast<FusedLoc>(loc).getLocations()) {
if (auto callLoc = getCallSiteLoc(subLoc)) {
return callLoc;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,15 +344,15 @@ class NVVMDialectLLVMIRTranslationInterface
llvm::Function *llvmFunc = moduleTranslation.lookupFunction(func.getName());

if (attribute.getName() == NVVM::NVVMDialect::getMaxntidAttrName()) {
if (!dyn_cast<DenseI32ArrayAttr>(attribute.getValue()))
if (!isa<DenseI32ArrayAttr>(attribute.getValue()))
return failure();
auto values = cast<DenseI32ArrayAttr>(attribute.getValue());
const std::string attr = llvm::formatv(
"{0:$[,]}", llvm::make_range(values.asArrayRef().begin(),
values.asArrayRef().end()));
llvmFunc->addFnAttr("nvvm.maxntid", attr);
} else if (attribute.getName() == NVVM::NVVMDialect::getReqntidAttrName()) {
if (!dyn_cast<DenseI32ArrayAttr>(attribute.getValue()))
if (!isa<DenseI32ArrayAttr>(attribute.getValue()))
return failure();
auto values = cast<DenseI32ArrayAttr>(attribute.getValue());
const std::string attr = llvm::formatv(
Expand All @@ -361,7 +361,7 @@ class NVVMDialectLLVMIRTranslationInterface
llvmFunc->addFnAttr("nvvm.reqntid", attr);
} else if (attribute.getName() ==
NVVM::NVVMDialect::getClusterDimAttrName()) {
if (!dyn_cast<DenseI32ArrayAttr>(attribute.getValue()))
if (!isa<DenseI32ArrayAttr>(attribute.getValue()))
return failure();
auto values = cast<DenseI32ArrayAttr>(attribute.getValue());
const std::string attr = llvm::formatv(
Expand Down