Skip to content

Commit 6c29c0c

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

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
@@ -649,8 +649,7 @@ void CIRGenFunction::emitStoreOfScalar(mlir::Value value, Address addr,
649649

650650
// Update the alloca with more info on initialization.
651651
assert(addr.getPointer() && "expected pointer to exist");
652-
auto SrcAlloca =
653-
dyn_cast_or_null<cir::AllocaOp>(addr.getPointer().getDefiningOp());
652+
auto SrcAlloca = addr.getDefiningOp<cir::AllocaOp>();
654653
if (currVarDecl && SrcAlloca) {
655654
const VarDecl *VD = currVarDecl;
656655
assert(VD && "VarDecl expected");
@@ -1326,8 +1325,7 @@ LValue CIRGenFunction::emitUnaryOpLValue(const UnaryOperator *E) {
13261325
emitPointerWithAlignment(E->getSubExpr(), &BaseInfo, &TBAAInfo);
13271326

13281327
// Tag 'load' with deref attribute.
1329-
if (auto loadOp =
1330-
dyn_cast<cir::LoadOp>(Addr.getPointer().getDefiningOp())) {
1328+
if (auto loadOp = Addr.getDefiningOp<cir::LoadOp>()) {
13311329
loadOp.setIsDerefAttr(mlir::UnitAttr::get(&getMLIRContext()));
13321330
}
13331331

@@ -2217,8 +2215,7 @@ static Address createReferenceTemporary(CIRGenFunction &CGF,
22172215
if (const clang::ValueDecl *extDecl = M->getExtendingDecl()) {
22182216
auto extDeclAddrIter = CGF.LocalDeclMap.find(extDecl);
22192217
if (extDeclAddrIter != CGF.LocalDeclMap.end()) {
2220-
extDeclAlloca = dyn_cast_if_present<cir::AllocaOp>(
2221-
extDeclAddrIter->second.getDefiningOp());
2218+
extDeclAlloca = extDeclAddrIter->second.getDefiningOp<cir::AllocaOp>();
22222219
}
22232220
}
22242221
mlir::OpBuilder::InsertPoint ip;
@@ -2334,7 +2331,7 @@ LValue CIRGenFunction::emitMaterializeTemporaryExpr(
23342331
Address Alloca = Address::invalid();
23352332
Address Object = createReferenceTemporary(*this, M, E, &Alloca);
23362333

2337-
if (auto Var = dyn_cast<cir::GlobalOp>(Object.getPointer().getDefiningOp())) {
2334+
if (auto Var = Object.getDefiningOp<cir::GlobalOp>()) {
23382335
// TODO(cir): add something akin to stripPointerCasts() to ptr above
23392336
assert(0 && "NYI");
23402337
} else {
@@ -2864,7 +2861,7 @@ mlir::Value CIRGenFunction::emitAlloca(StringRef name, mlir::Type ty,
28642861
addr = builder.createAlloca(loc, /*addr type*/ localVarPtrTy,
28652862
/*var type*/ ty, name, alignIntAttr, arraySize);
28662863
if (currVarDecl) {
2867-
auto alloca = cast<cir::AllocaOp>(addr.getDefiningOp());
2864+
auto alloca = addr.getDefiningOp<cir::AllocaOp>();
28682865
alloca.setAstAttr(ASTVarDeclAttr::get(&getMLIRContext(), currVarDecl));
28692866
}
28702867
}
@@ -3094,9 +3091,9 @@ cir::AllocaOp CIRGenFunction::CreateTempAlloca(mlir::Type Ty,
30943091
const Twine &Name,
30953092
mlir::Value ArraySize,
30963093
bool insertIntoFnEntryBlock) {
3097-
return cast<cir::AllocaOp>(emitAlloca(Name.str(), Ty, Loc, CharUnits(),
3098-
insertIntoFnEntryBlock, ArraySize)
3099-
.getDefiningOp());
3094+
return emitAlloca(Name.str(), Ty, Loc, CharUnits(), insertIntoFnEntryBlock,
3095+
ArraySize)
3096+
.getDefiningOp<cir::AllocaOp>();
31003097
}
31013098

31023099
/// This creates an alloca and inserts it into the provided insertion point
@@ -3106,9 +3103,8 @@ cir::AllocaOp CIRGenFunction::CreateTempAlloca(mlir::Type Ty,
31063103
mlir::OpBuilder::InsertPoint ip,
31073104
mlir::Value ArraySize) {
31083105
assert(ip.isSet() && "Insertion point is not set");
3109-
return cast<cir::AllocaOp>(
3110-
emitAlloca(Name.str(), Ty, Loc, CharUnits(), ip, ArraySize)
3111-
.getDefiningOp());
3106+
return emitAlloca(Name.str(), Ty, Loc, CharUnits(), ip, ArraySize)
3107+
.getDefiningOp<cir::AllocaOp>();
31123108
}
31133109

31143110
/// 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
@@ -294,7 +294,7 @@ mlir::LogicalResult CIRGenFunction::declare(const Decl *var, QualType ty,
294294
assert(!symbolTable.count(var) && "not supposed to be available just yet");
295295

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

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

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);
@@ -759,9 +759,9 @@ void LifetimeCheckPass::checkReturn(ReturnOp retOp) {
759759

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

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

11041104
mlir::Value LifetimeCheckPass::getLambdaFromMemberAccess(mlir::Value addr) {
1105-
auto op = addr.getDefiningOp();
11061105
// FIXME: we likely want to consider more indirections here...
1107-
if (!isa<cir::GetMemberOp>(op))
1106+
auto op = addr.getDefiningOp<cir::GetMemberOp>();
1107+
if (!op)
11081108
return nullptr;
1109-
auto allocaOp = dyn_cast<cir::AllocaOp>(op->getOperand(0).getDefiningOp());
1109+
auto allocaOp = op->getOperand(0).getDefiningOp<cir::AllocaOp>();
11101110
if (!allocaOp || !isLambdaType(allocaOp.getAllocaType()))
11111111
return nullptr;
11121112
return allocaOp;
@@ -1116,7 +1116,7 @@ void LifetimeCheckPass::checkLambdaCaptureStore(StoreOp storeOp) {
11161116
auto localByRefAddr = storeOp.getValue();
11171117
auto lambdaCaptureAddr = storeOp.getAddr();
11181118

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

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

@@ -1342,7 +1342,7 @@ void LifetimeCheckPass::emitInvalidHistory(mlir::InFlightDiagnostic &D,
13421342
case InvalidStyle::EndOfScope: {
13431343
if (tasks.count(histKey)) {
13441344
llvm::StringRef resource = "resource";
1345-
if (auto allocaOp = dyn_cast<AllocaOp>(info.val->getDefiningOp())) {
1345+
if (auto allocaOp = info.val->getDefiningOp<cir::AllocaOp>()) {
13461346
if (isLambdaType(allocaOp.getAllocaType()))
13471347
resource = "lambda";
13481348
}
@@ -1418,7 +1418,7 @@ void LifetimeCheckPass::checkPointerDeref(mlir::Value addr, mlir::Location loc,
14181418
D << "returned lambda captures local variable";
14191419
else if (derefStyle == DerefStyle::CallParam ||
14201420
derefStyle == DerefStyle::IndirectCallParam) {
1421-
bool isAgg = isa_and_nonnull<GetMemberOp>(addr.getDefiningOp());
1421+
bool isAgg = addr.getDefiningOp<cir::GetMemberOp>();
14221422
D << "passing ";
14231423
if (!isAgg)
14241424
D << "invalid pointer";
@@ -1463,7 +1463,7 @@ mlir::Value LifetimeCheckPass::getThisParamPointerCategory(CallOp callOp) {
14631463
auto thisptr = callOp.getArgOperand(0);
14641464
if (ptrs.count(thisptr))
14651465
return thisptr;
1466-
if (auto loadOp = dyn_cast_or_null<LoadOp>(thisptr.getDefiningOp())) {
1466+
if (auto loadOp = thisptr.getDefiningOp<cir::LoadOp>()) {
14671467
if (ptrs.count(loadOp.getAddr()))
14681468
return loadOp.getAddr();
14691469
}
@@ -1475,7 +1475,7 @@ mlir::Value LifetimeCheckPass::getThisParamOwnerCategory(CallOp callOp) {
14751475
auto thisptr = callOp.getArgOperand(0);
14761476
if (owners.count(thisptr))
14771477
return thisptr;
1478-
if (auto loadOp = dyn_cast_or_null<LoadOp>(thisptr.getDefiningOp())) {
1478+
if (auto loadOp = thisptr.getDefiningOp<cir::LoadOp>()) {
14791479
if (owners.count(loadOp.getAddr()))
14801480
return loadOp.getAddr();
14811481
}
@@ -1579,7 +1579,7 @@ void LifetimeCheckPass::checkCtor(CallOp callOp, cir::CXXCtorAttr ctor) {
15791579

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

15851585
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)