Skip to content

Commit 44500ae

Browse files
authored
[CIR] Use getDefiningOp<OpTy>() instead of dyn_cast<OpTy>(getDefiningOp()) (NFC) (#151217)
This applies similar changes to #150428
1 parent a1c3c65 commit 44500ae

File tree

5 files changed

+19
-23
lines changed

5 files changed

+19
-23
lines changed

clang/lib/CIR/CodeGen/CIRGenDecl.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ void CIRGenFunction::emitAutoVarInit(
158158
// out of it while trying to build the expression, mark it as such.
159159
mlir::Value val = lv.getAddress().getPointer();
160160
assert(val && "Should have an address");
161-
auto allocaOp = dyn_cast_or_null<cir::AllocaOp>(val.getDefiningOp());
161+
auto allocaOp = val.getDefiningOp<cir::AllocaOp>();
162162
assert(allocaOp && "Address should come straight out of the alloca");
163163

164164
if (!allocaOp.use_empty())
@@ -412,7 +412,8 @@ void CIRGenFunction::emitStaticVarDecl(const VarDecl &d,
412412
// TODO(cir): we should have a way to represent global ops as values without
413413
// having to emit a get global op. Sometimes these emissions are not used.
414414
mlir::Value addr = builder.createGetGlobal(globalOp);
415-
auto getAddrOp = mlir::cast<cir::GetGlobalOp>(addr.getDefiningOp());
415+
auto getAddrOp = addr.getDefiningOp<cir::GetGlobalOp>();
416+
assert(getAddrOp && "expected cir::GetGlobalOp");
416417

417418
CharUnits alignment = getContext().getDeclAlign(&d);
418419

clang/lib/CIR/CodeGen/CIRGenExpr.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,7 @@ void CIRGenFunction::emitStoreOfScalar(mlir::Value value, Address addr,
303303

304304
// Update the alloca with more info on initialization.
305305
assert(addr.getPointer() && "expected pointer to exist");
306-
auto srcAlloca =
307-
dyn_cast_or_null<cir::AllocaOp>(addr.getPointer().getDefiningOp());
306+
auto srcAlloca = addr.getDefiningOp<cir::AllocaOp>();
308307
if (currVarDecl && srcAlloca) {
309308
const VarDecl *vd = currVarDecl;
310309
assert(vd && "VarDecl expected");
@@ -635,10 +634,8 @@ LValue CIRGenFunction::emitUnaryOpLValue(const UnaryOperator *e) {
635634
// Tag 'load' with deref attribute.
636635
// FIXME: This misses some derefence cases and has problematic interactions
637636
// with other operators.
638-
if (auto loadOp =
639-
dyn_cast<cir::LoadOp>(addr.getPointer().getDefiningOp())) {
637+
if (auto loadOp = addr.getDefiningOp<cir::LoadOp>())
640638
loadOp.setIsDerefAttr(mlir::UnitAttr::get(&getMLIRContext()));
641-
}
642639

643640
LValue lv = makeAddrLValue(addr, t, baseInfo);
644641
assert(!cir::MissingFeatures::addressSpace());
@@ -2006,9 +2003,9 @@ cir::AllocaOp CIRGenFunction::createTempAlloca(mlir::Type ty,
20062003
const Twine &name,
20072004
mlir::Value arraySize,
20082005
bool insertIntoFnEntryBlock) {
2009-
return cast<cir::AllocaOp>(emitAlloca(name.str(), ty, loc, CharUnits(),
2010-
insertIntoFnEntryBlock, arraySize)
2011-
.getDefiningOp());
2006+
return mlir::cast<cir::AllocaOp>(emitAlloca(name.str(), ty, loc, CharUnits(),
2007+
insertIntoFnEntryBlock, arraySize)
2008+
.getDefiningOp());
20122009
}
20132010

20142011
/// This creates an alloca and inserts it into the provided insertion point
@@ -2018,7 +2015,7 @@ cir::AllocaOp CIRGenFunction::createTempAlloca(mlir::Type ty,
20182015
mlir::OpBuilder::InsertPoint ip,
20192016
mlir::Value arraySize) {
20202017
assert(ip.isSet() && "Insertion point is not set");
2021-
return cast<cir::AllocaOp>(
2018+
return mlir::cast<cir::AllocaOp>(
20222019
emitAlloca(name.str(), ty, loc, CharUnits(), ip, arraySize)
20232020
.getDefiningOp());
20242021
}

clang/lib/CIR/CodeGen/CIRGenFunction.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,9 @@ void CIRGenFunction::declare(mlir::Value addrVal, const Decl *var, QualType ty,
219219
assert(isa<NamedDecl>(var) && "Needs a named decl");
220220
assert(!cir::MissingFeatures::cgfSymbolTable());
221221

222-
auto allocaOp = cast<cir::AllocaOp>(addrVal.getDefiningOp());
222+
auto allocaOp = addrVal.getDefiningOp<cir::AllocaOp>();
223+
assert(allocaOp && "expected cir::AllocaOp");
224+
223225
if (isParam)
224226
allocaOp.setInitAttr(mlir::UnitAttr::get(&getMLIRContext()));
225227
if (ty->isReferenceType() || ty.isConstQualified())

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,7 @@ static Value tryFoldCastChain(cir::CastOp op) {
606606
if (!isIntOrBoolCast(op))
607607
break;
608608
head = op;
609-
op = dyn_cast_or_null<cir::CastOp>(head.getSrc().getDefiningOp());
609+
op = head.getSrc().getDefiningOp<cir::CastOp>();
610610
}
611611

612612
if (head == tail)
@@ -1802,7 +1802,7 @@ OpFoldResult cir::UnaryOp::fold(FoldAdaptor adaptor) {
18021802
}
18031803

18041804
if (isBoolNot(*this))
1805-
if (auto previous = dyn_cast_or_null<UnaryOp>(getInput().getDefiningOp()))
1805+
if (auto previous = getInput().getDefiningOp<cir::UnaryOp>())
18061806
if (isBoolNot(previous))
18071807
return previous.getInput();
18081808

@@ -2184,8 +2184,7 @@ LogicalResult cir::ComplexRealOp::verify() {
21842184
}
21852185

21862186
OpFoldResult cir::ComplexRealOp::fold(FoldAdaptor adaptor) {
2187-
if (auto complexCreateOp =
2188-
dyn_cast_or_null<cir::ComplexCreateOp>(getOperand().getDefiningOp()))
2187+
if (auto complexCreateOp = getOperand().getDefiningOp<cir::ComplexCreateOp>())
21892188
return complexCreateOp.getOperand(0);
21902189

21912190
auto complex =
@@ -2206,8 +2205,7 @@ LogicalResult cir::ComplexImagOp::verify() {
22062205
}
22072206

22082207
OpFoldResult cir::ComplexImagOp::fold(FoldAdaptor adaptor) {
2209-
if (auto complexCreateOp =
2210-
dyn_cast_or_null<cir::ComplexCreateOp>(getOperand().getDefiningOp()))
2208+
if (auto complexCreateOp = getOperand().getDefiningOp<cir::ComplexCreateOp>())
22112209
return complexCreateOp.getOperand(1);
22122210

22132211
auto complex =

clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -821,8 +821,7 @@ mlir::LogicalResult CIRToLLVMPtrStrideOpLowering::matchAndRewrite(
821821
// before it. To achieve that, look at unary minus, which already got
822822
// lowered to "sub 0, x".
823823
const auto sub = dyn_cast<mlir::LLVM::SubOp>(indexOp);
824-
auto unary = dyn_cast_if_present<cir::UnaryOp>(
825-
ptrStrideOp.getStride().getDefiningOp());
824+
auto unary = ptrStrideOp.getStride().getDefiningOp<cir::UnaryOp>();
826825
bool rewriteSub =
827826
unary && unary.getKind() == cir::UnaryOpKind::Minus && sub;
828827
if (rewriteSub)
@@ -2378,15 +2377,14 @@ mlir::LogicalResult CIRToLLVMVecSplatOpLowering::matchAndRewrite(
23782377
mlir::Value poison = rewriter.create<mlir::LLVM::PoisonOp>(loc, llvmTy);
23792378

23802379
mlir::Value elementValue = adaptor.getValue();
2381-
if (mlir::isa<mlir::LLVM::PoisonOp>(elementValue.getDefiningOp())) {
2380+
if (elementValue.getDefiningOp<mlir::LLVM::PoisonOp>()) {
23822381
// If the splat value is poison, then we can just use poison value
23832382
// for the entire vector.
23842383
rewriter.replaceOp(op, poison);
23852384
return mlir::success();
23862385
}
23872386

2388-
if (auto constValue =
2389-
dyn_cast<mlir::LLVM::ConstantOp>(elementValue.getDefiningOp())) {
2387+
if (auto constValue = elementValue.getDefiningOp<mlir::LLVM::ConstantOp>()) {
23902388
if (auto intAttr = dyn_cast<mlir::IntegerAttr>(constValue.getValue())) {
23912389
mlir::DenseIntElementsAttr denseVec = mlir::DenseIntElementsAttr::get(
23922390
mlir::cast<mlir::ShapedType>(llvmTy), intAttr.getValue());

0 commit comments

Comments
 (0)