Skip to content

Commit d45823a

Browse files
authored
[CIR] Use getDefiningOp<OpTy>() instead of dyn_cast<OpTy>(getDefiningOp()) (NFC) (#1765)
This applies similar changes to llvm/llvm-project#150428
1 parent e571d1c commit d45823a

File tree

14 files changed

+73
-81
lines changed

14 files changed

+73
-81
lines changed

clang/lib/CIR/CodeGen/Address.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,10 @@ class Address {
179179
return nullptr;
180180
return getPointer().getDefiningOp();
181181
}
182+
183+
template <typename T> T getDefiningOp() const {
184+
return mlir::dyn_cast_or_null<T>(getDefiningOp());
185+
}
182186
};
183187

184188
} // namespace clang::CIRGen

clang/lib/CIR/CodeGen/CIRGenDecl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ void CIRGenFunction::emitAutoVarInit(const AutoVarEmission &emission) {
331331
// out of it while trying to build the expression, mark it as such.
332332
auto addr = lv.getAddress().getPointer();
333333
assert(addr && "Should have an address");
334-
auto allocaOp = dyn_cast_or_null<cir::AllocaOp>(addr.getDefiningOp());
334+
auto allocaOp = addr.getDefiningOp<cir::AllocaOp>();
335335
assert(allocaOp && "Address should come straight out of the alloca");
336336

337337
if (!allocaOp.use_empty())
@@ -617,7 +617,7 @@ void CIRGenFunction::emitStaticVarDecl(const VarDecl &D,
617617
// TODO(cir): we should have a way to represent global ops as values without
618618
// having to emit a get global op. Sometimes these emissions are not used.
619619
auto addr = getBuilder().createGetGlobal(globalOp);
620-
auto getAddrOp = mlir::cast<cir::GetGlobalOp>(addr.getDefiningOp());
620+
auto getAddrOp = addr.getDefiningOp<cir::GetGlobalOp>();
621621

622622
CharUnits alignment = getContext().getDeclAlign(&D);
623623

clang/lib/CIR/CodeGen/CIRGenExpr.cpp

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -652,8 +652,7 @@ void CIRGenFunction::emitStoreOfScalar(mlir::Value value, Address addr,
652652

653653
// Update the alloca with more info on initialization.
654654
assert(addr.getPointer() && "expected pointer to exist");
655-
auto SrcAlloca =
656-
dyn_cast_or_null<cir::AllocaOp>(addr.getPointer().getDefiningOp());
655+
auto SrcAlloca = addr.getDefiningOp<cir::AllocaOp>();
657656
if (currVarDecl && SrcAlloca) {
658657
const VarDecl *VD = currVarDecl;
659658
assert(VD && "VarDecl expected");
@@ -1328,8 +1327,7 @@ LValue CIRGenFunction::emitUnaryOpLValue(const UnaryOperator *E) {
13281327
emitPointerWithAlignment(E->getSubExpr(), &BaseInfo, &TBAAInfo);
13291328

13301329
// Tag 'load' with deref attribute.
1331-
if (auto loadOp =
1332-
dyn_cast<cir::LoadOp>(Addr.getPointer().getDefiningOp())) {
1330+
if (auto loadOp = Addr.getDefiningOp<cir::LoadOp>()) {
13331331
loadOp.setIsDerefAttr(mlir::UnitAttr::get(&getMLIRContext()));
13341332
}
13351333

@@ -2221,8 +2219,7 @@ static Address createReferenceTemporary(CIRGenFunction &CGF,
22212219
if (const clang::ValueDecl *extDecl = M->getExtendingDecl()) {
22222220
auto extDeclAddrIter = CGF.LocalDeclMap.find(extDecl);
22232221
if (extDeclAddrIter != CGF.LocalDeclMap.end()) {
2224-
extDeclAlloca = dyn_cast_if_present<cir::AllocaOp>(
2225-
extDeclAddrIter->second.getDefiningOp());
2222+
extDeclAlloca = extDeclAddrIter->second.getDefiningOp<cir::AllocaOp>();
22262223
}
22272224
}
22282225
mlir::OpBuilder::InsertPoint ip;
@@ -2337,7 +2334,7 @@ LValue CIRGenFunction::emitMaterializeTemporaryExpr(
23372334
Address Alloca = Address::invalid();
23382335
Address Object = createReferenceTemporary(*this, M, E, &Alloca);
23392336

2340-
if (auto Var = dyn_cast<cir::GlobalOp>(Object.getPointer().getDefiningOp())) {
2337+
if (auto Var = Object.getDefiningOp<cir::GlobalOp>()) {
23412338
// TODO(cir): add something akin to stripPointerCasts() to ptr above
23422339
assert(0 && "NYI");
23432340
} else {
@@ -2867,7 +2864,7 @@ mlir::Value CIRGenFunction::emitAlloca(StringRef name, mlir::Type ty,
28672864
addr = builder.createAlloca(loc, /*addr type*/ localVarPtrTy,
28682865
/*var type*/ ty, name, alignIntAttr, arraySize);
28692866
if (currVarDecl) {
2870-
auto alloca = cast<cir::AllocaOp>(addr.getDefiningOp());
2867+
auto alloca = addr.getDefiningOp<cir::AllocaOp>();
28712868
alloca.setAstAttr(ASTVarDeclAttr::get(&getMLIRContext(), currVarDecl));
28722869
}
28732870
}
@@ -3097,9 +3094,9 @@ cir::AllocaOp CIRGenFunction::CreateTempAlloca(mlir::Type Ty,
30973094
const Twine &Name,
30983095
mlir::Value ArraySize,
30993096
bool insertIntoFnEntryBlock) {
3100-
return cast<cir::AllocaOp>(emitAlloca(Name.str(), Ty, Loc, CharUnits(),
3101-
insertIntoFnEntryBlock, ArraySize)
3102-
.getDefiningOp());
3097+
return emitAlloca(Name.str(), Ty, Loc, CharUnits(), insertIntoFnEntryBlock,
3098+
ArraySize)
3099+
.getDefiningOp<cir::AllocaOp>();
31033100
}
31043101

31053102
/// This creates an alloca and inserts it into the provided insertion point
@@ -3109,9 +3106,8 @@ cir::AllocaOp CIRGenFunction::CreateTempAlloca(mlir::Type Ty,
31093106
mlir::OpBuilder::InsertPoint ip,
31103107
mlir::Value ArraySize) {
31113108
assert(ip.isSet() && "Insertion point is not set");
3112-
return cast<cir::AllocaOp>(
3113-
emitAlloca(Name.str(), Ty, Loc, CharUnits(), ip, ArraySize)
3114-
.getDefiningOp());
3109+
return emitAlloca(Name.str(), Ty, Loc, CharUnits(), ip, ArraySize)
3110+
.getDefiningOp<cir::AllocaOp>();
31153111
}
31163112

31173113
/// Just like CreateTempAlloca above, but place the alloca into the function

clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
370370
// direclty in the parent scope removing the need to hoist it.
371371
assert(retAlloca.getDefiningOp() && "expected a alloca op");
372372
CGF.getBuilder().hoistAllocaToParentRegion(
373-
cast<cir::AllocaOp>(retAlloca.getDefiningOp()));
373+
retAlloca.getDefiningOp<cir::AllocaOp>());
374374

375375
return CGF.emitLoadOfScalar(CGF.makeAddrLValue(retAlloca, E->getType()),
376376
E->getExprLoc());

clang/lib/CIR/CodeGen/CIRGenFunction.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ mlir::LogicalResult CIRGenFunction::declare(const Decl *var, QualType ty,
293293
assert(!symbolTable.count(var) && "not supposed to be available just yet");
294294

295295
addr = emitAlloca(namedVar->getName(), ty, loc, alignment);
296-
auto allocaOp = cast<cir::AllocaOp>(addr.getDefiningOp());
296+
auto allocaOp = addr.getDefiningOp<cir::AllocaOp>();
297297
if (isParam)
298298
allocaOp.setInitAttr(mlir::UnitAttr::get(&getMLIRContext()));
299299
if (ty->isReferenceType() || ty.isConstQualified())
@@ -313,7 +313,7 @@ mlir::LogicalResult CIRGenFunction::declare(Address addr, const Decl *var,
313313
assert(!symbolTable.count(var) && "not supposed to be available just yet");
314314

315315
addrVal = addr.getPointer();
316-
auto allocaOp = cast<cir::AllocaOp>(addrVal.getDefiningOp());
316+
auto allocaOp = addrVal.getDefiningOp<cir::AllocaOp>();
317317
if (isParam)
318318
allocaOp.setInitAttr(mlir::UnitAttr::get(&getMLIRContext()));
319319
if (ty->isReferenceType() || ty.isConstQualified())
@@ -1986,7 +1986,7 @@ void CIRGenFunction::emitVarAnnotations(const VarDecl *decl, mlir::Value val) {
19861986
for (const auto *annot : decl->specific_attrs<AnnotateAttr>()) {
19871987
annotations.push_back(CGM.emitAnnotateAttr(annot));
19881988
}
1989-
auto allocaOp = dyn_cast_or_null<cir::AllocaOp>(val.getDefiningOp());
1989+
auto allocaOp = val.getDefiningOp<cir::AllocaOp>();
19901990
assert(allocaOp && "expects available alloca");
19911991
allocaOp.setAnnotationsAttr(builder.getArrayAttr(annotations));
19921992
}

clang/lib/CIR/CodeGen/CIRGenFunction.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2653,10 +2653,10 @@ inline mlir::Value DominatingCIRValue::restore(CIRGenFunction &CGF,
26532653
return value.getPointer();
26542654

26552655
// Otherwise, it should be an alloca instruction, as set up in save().
2656-
auto alloca = mlir::cast<cir::AllocaOp>(value.getPointer().getDefiningOp());
2656+
auto alloca = value.getPointer().getDefiningOp<cir::AllocaOp>();
26572657
mlir::Value val = CGF.getBuilder().createAlignedLoad(
26582658
alloca.getLoc(), alloca.getType(), alloca);
2659-
cir::LoadOp loadOp = mlir::cast<cir::LoadOp>(val.getDefiningOp());
2659+
cir::LoadOp loadOp = val.getDefiningOp<cir::LoadOp>();
26602660
loadOp.setAlignment(alloca.getAlignment());
26612661
return val;
26622662
}

clang/lib/CIR/Dialect/IR/CIRDialect.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,7 @@ Value tryFoldCastChain(cir::CastOp op) {
778778
if (!isIntOrBoolCast(op))
779779
break;
780780
head = op;
781-
op = dyn_cast_or_null<cir::CastOp>(head.getSrc().getDefiningOp());
781+
op = head.getSrc().getDefiningOp<cir::CastOp>();
782782
}
783783

784784
if (head == tail)
@@ -841,7 +841,7 @@ static bool isBoolNot(cir::UnaryOp op) {
841841
// and the argument of the first one (%0) will be used instead.
842842
OpFoldResult cir::UnaryOp::fold(FoldAdaptor adaptor) {
843843
if (isBoolNot(*this))
844-
if (auto previous = dyn_cast_or_null<UnaryOp>(getInput().getDefiningOp()))
844+
if (auto previous = getInput().getDefiningOp<UnaryOp>())
845845
if (isBoolNot(previous))
846846
return previous.getInput();
847847

clang/lib/CIR/Dialect/Transforms/LibOpt.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,8 @@ void LibOptPass::xformStdFindIntoMemchr(StdFindOp findOp) {
172172
// Build memchr op:
173173
// void *memchr(const void *s, int c, size_t n);
174174
auto memChr = [&] {
175-
if (auto iterBegin = dyn_cast<IterBeginOp>(first.getDefiningOp());
176-
iterBegin && isa<IterEndOp>(last.getDefiningOp())) {
175+
if (auto iterBegin = first.getDefiningOp<IterBeginOp>();
176+
iterBegin && last.getDefiningOp<IterEndOp>()) {
177177
// Both operands have the same type, use iterBegin.
178178

179179
// Look at this pointer to retrieve container information.

clang/lib/CIR/Dialect/Transforms/LifetimeCheck.cpp

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ static std::string getVarNameFromValue(mlir::Value v) {
477477
if (auto allocaOp = dyn_cast<AllocaOp>(srcOp))
478478
return allocaOp.getName().str();
479479
if (auto getElemOp = dyn_cast<GetMemberOp>(srcOp)) {
480-
auto parent = dyn_cast<AllocaOp>(getElemOp.getAddr().getDefiningOp());
480+
auto parent = getElemOp.getAddr().getDefiningOp<cir::AllocaOp>();
481481
if (parent) {
482482
llvm::SmallString<128> finalName;
483483
llvm::raw_svector_ostream Out(finalName);
@@ -760,9 +760,9 @@ void LifetimeCheckPass::checkReturn(ReturnOp retOp) {
760760

761761
// The return value is loaded from the return slot before
762762
// returning.
763-
auto loadOp = dyn_cast<LoadOp>(retOp.getOperand(0).getDefiningOp());
763+
auto loadOp = retOp.getOperand(0).getDefiningOp<cir::LoadOp>();
764764
assert(loadOp && "expected cir.load");
765-
if (!isa<AllocaOp>(loadOp.getAddr().getDefiningOp()))
765+
if (!loadOp.getAddr().getDefiningOp<cir::AllocaOp>())
766766
return;
767767

768768
// Keep track of interesting lambda.
@@ -1083,10 +1083,10 @@ void LifetimeCheckPass::checkCoroTaskStore(StoreOp storeOp) {
10831083
// Bind values that are coming from alloca's (like %arg0 above) to the
10841084
// pset of %task - this effectively leads to some invalidation of %task
10851085
// when %arg0 finishes its lifetime at the end of the enclosing cir.scope.
1086-
if (auto call = dyn_cast<cir::CallOp>(taskTmp.getDefiningOp())) {
1086+
if (auto call = taskTmp.getDefiningOp<cir::CallOp>()) {
10871087
bool potentialTaintedTask = false;
10881088
for (auto arg : call.getArgOperands()) {
1089-
auto alloca = dyn_cast<cir::AllocaOp>(arg.getDefiningOp());
1089+
auto alloca = arg.getDefiningOp<cir::AllocaOp>();
10901090
if (alloca && currScope->localValues.count(alloca)) {
10911091
getPmap()[taskAddr].insert(State::getLocalValue(alloca));
10921092
potentialTaintedTask = true;
@@ -1103,11 +1103,11 @@ void LifetimeCheckPass::checkCoroTaskStore(StoreOp storeOp) {
11031103
}
11041104

11051105
mlir::Value LifetimeCheckPass::getLambdaFromMemberAccess(mlir::Value addr) {
1106-
auto op = addr.getDefiningOp();
11071106
// FIXME: we likely want to consider more indirections here...
1108-
if (!isa<cir::GetMemberOp>(op))
1107+
auto op = addr.getDefiningOp<cir::GetMemberOp>();
1108+
if (!op)
11091109
return nullptr;
1110-
auto allocaOp = dyn_cast<cir::AllocaOp>(op->getOperand(0).getDefiningOp());
1110+
auto allocaOp = op->getOperand(0).getDefiningOp<cir::AllocaOp>();
11111111
if (!allocaOp || !isLambdaType(allocaOp.getAllocaType()))
11121112
return nullptr;
11131113
return allocaOp;
@@ -1117,7 +1117,7 @@ void LifetimeCheckPass::checkLambdaCaptureStore(StoreOp storeOp) {
11171117
auto localByRefAddr = storeOp.getValue();
11181118
auto lambdaCaptureAddr = storeOp.getAddr();
11191119

1120-
if (!isa_and_nonnull<cir::AllocaOp>(localByRefAddr.getDefiningOp()))
1120+
if (!localByRefAddr.getDefiningOp<cir::AllocaOp>())
11211121
return;
11221122
auto lambdaAddr = getLambdaFromMemberAccess(lambdaCaptureAddr);
11231123
if (!lambdaAddr)
@@ -1178,7 +1178,7 @@ void LifetimeCheckPass::updatePointsTo(mlir::Value addr, mlir::Value data,
11781178
mlir::Location loc) {
11791179

11801180
auto getArrayFromSubscript = [&](PtrStrideOp strideOp) -> mlir::Value {
1181-
auto castOp = dyn_cast<CastOp>(strideOp.getBase().getDefiningOp());
1181+
auto castOp = strideOp.getBase().getDefiningOp<cir::CastOp>();
11821182
if (!castOp)
11831183
return {};
11841184
if (castOp.getKind() != cir::CastKind::array_to_ptrdecay)
@@ -1284,11 +1284,11 @@ void LifetimeCheckPass::checkStore(StoreOp storeOp) {
12841284
// Decompose store's to aggregates into multiple updates to individual fields.
12851285
if (aggregates.count(addr)) {
12861286
auto data = storeOp.getValue();
1287-
auto dataSrcOp = data.getDefiningOp();
1288-
// Only interested in updating and tracking fields, anything besides
1289-
// constants isn't really relevant.
1290-
if (dataSrcOp && isa<ConstantOp>(dataSrcOp))
1287+
if (data.getDefiningOp<cir::ConstantOp>()) {
1288+
// Only interested in updating and tracking fields, anything besides
1289+
// constants isn't really relevant.
12911290
updatePointsTo(addr, data, data.getLoc());
1291+
}
12921292
return;
12931293
}
12941294

@@ -1343,7 +1343,7 @@ void LifetimeCheckPass::emitInvalidHistory(mlir::InFlightDiagnostic &D,
13431343
case InvalidStyle::EndOfScope: {
13441344
if (tasks.count(histKey)) {
13451345
llvm::StringRef resource = "resource";
1346-
if (auto allocaOp = dyn_cast<AllocaOp>(info.val->getDefiningOp())) {
1346+
if (auto allocaOp = info.val->getDefiningOp<cir::AllocaOp>()) {
13471347
if (isLambdaType(allocaOp.getAllocaType()))
13481348
resource = "lambda";
13491349
}
@@ -1419,7 +1419,7 @@ void LifetimeCheckPass::checkPointerDeref(mlir::Value addr, mlir::Location loc,
14191419
D << "returned lambda captures local variable";
14201420
else if (derefStyle == DerefStyle::CallParam ||
14211421
derefStyle == DerefStyle::IndirectCallParam) {
1422-
bool isAgg = isa_and_nonnull<GetMemberOp>(addr.getDefiningOp());
1422+
bool isAgg = addr.getDefiningOp<cir::GetMemberOp>();
14231423
D << "passing ";
14241424
if (!isAgg)
14251425
D << "invalid pointer";
@@ -1464,7 +1464,7 @@ mlir::Value LifetimeCheckPass::getThisParamPointerCategory(CallOp callOp) {
14641464
auto thisptr = callOp.getArgOperand(0);
14651465
if (ptrs.count(thisptr))
14661466
return thisptr;
1467-
if (auto loadOp = dyn_cast_or_null<LoadOp>(thisptr.getDefiningOp())) {
1467+
if (auto loadOp = thisptr.getDefiningOp<cir::LoadOp>()) {
14681468
if (ptrs.count(loadOp.getAddr()))
14691469
return loadOp.getAddr();
14701470
}
@@ -1476,7 +1476,7 @@ mlir::Value LifetimeCheckPass::getThisParamOwnerCategory(CallOp callOp) {
14761476
auto thisptr = callOp.getArgOperand(0);
14771477
if (owners.count(thisptr))
14781478
return thisptr;
1479-
if (auto loadOp = dyn_cast_or_null<LoadOp>(thisptr.getDefiningOp())) {
1479+
if (auto loadOp = thisptr.getDefiningOp<cir::LoadOp>()) {
14801480
if (owners.count(loadOp.getAddr()))
14811481
return loadOp.getAddr();
14821482
}
@@ -1580,7 +1580,7 @@ void LifetimeCheckPass::checkCtor(CallOp callOp, cir::CXXCtorAttr ctor) {
15801580

15811581
// Not interested in block/function arguments or any indirect
15821582
// provided alloca address.
1583-
if (!dyn_cast_or_null<AllocaOp>(addr.getDefiningOp()))
1583+
if (!addr.getDefiningOp<cir::AllocaOp>())
15841584
return;
15851585

15861586
markPsetNull(addr, callOp.getLoc());

clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1412,7 +1412,7 @@ static std::string getGlobalVarNameForConstString(cir::StoreOp op,
14121412
Out << "module.";
14131413
}
14141414

1415-
auto allocaOp = dyn_cast_or_null<cir::AllocaOp>(op.getAddr().getDefiningOp());
1415+
auto allocaOp = op.getAddr().getDefiningOp<cir::AllocaOp>();
14161416
if (allocaOp && !allocaOp.getName().empty())
14171417
Out << allocaOp.getName();
14181418
else

0 commit comments

Comments
 (0)