[llvm-branch-commits] [clang] [CIR] Use getDefiningOp<OpTy>() instead of dyn_cast<OpTy>(getDefiningOp()) (NFC) (PR #151217)
Henrich Lauko via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Fri Aug 1 10:43:28 PDT 2025
https://github.com/xlauko updated https://github.com/llvm/llvm-project/pull/151217
>From 2128e981991aa3b7098795bdf7d9a5cf7d0db011 Mon Sep 17 00:00:00 2001
From: xlauko <xlauko at mail.muni.cz>
Date: Tue, 29 Jul 2025 21:49:22 +0200
Subject: [PATCH] [CIR] Use getDefiningOp<OpTy>() instead of
dyn_cast<OpTy>(getDefiningOp()) (NFC)
This applies similar changes to llvm/llvm-project#150428
---
clang/lib/CIR/CodeGen/Address.h | 11 +++++++++++
clang/lib/CIR/CodeGen/CIRGenDecl.cpp | 5 +++--
clang/lib/CIR/CodeGen/CIRGenExpr.cpp | 15 ++++++---------
clang/lib/CIR/CodeGen/CIRGenFunction.cpp | 4 +++-
clang/lib/CIR/Dialect/IR/CIRDialect.cpp | 10 ++++------
.../lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 8 +++-----
6 files changed, 30 insertions(+), 23 deletions(-)
diff --git a/clang/lib/CIR/CodeGen/Address.h b/clang/lib/CIR/CodeGen/Address.h
index 6f76c3ebb1c5e..affacaf92bf84 100644
--- a/clang/lib/CIR/CodeGen/Address.h
+++ b/clang/lib/CIR/CodeGen/Address.h
@@ -101,6 +101,17 @@ class Address {
}
clang::CharUnits getAlignment() const { return alignment; }
+
+ /// Get the operation which defines this address.
+ mlir::Operation *getDefiningOp() const {
+ if (!isValid())
+ return nullptr;
+ return getPointer().getDefiningOp();
+ }
+
+ template <typename T> T getDefiningOp() const {
+ return mlir::dyn_cast_or_null<T>(getDefiningOp());
+ }
};
} // namespace clang::CIRGen
diff --git a/clang/lib/CIR/CodeGen/CIRGenDecl.cpp b/clang/lib/CIR/CodeGen/CIRGenDecl.cpp
index fa0d8a2a13432..4c0b0ec117444 100644
--- a/clang/lib/CIR/CodeGen/CIRGenDecl.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenDecl.cpp
@@ -158,7 +158,7 @@ void CIRGenFunction::emitAutoVarInit(
// out of it while trying to build the expression, mark it as such.
mlir::Value val = lv.getAddress().getPointer();
assert(val && "Should have an address");
- auto allocaOp = dyn_cast_or_null<cir::AllocaOp>(val.getDefiningOp());
+ auto allocaOp = val.getDefiningOp<cir::AllocaOp>();
assert(allocaOp && "Address should come straight out of the alloca");
if (!allocaOp.use_empty())
@@ -412,7 +412,8 @@ void CIRGenFunction::emitStaticVarDecl(const VarDecl &d,
// TODO(cir): we should have a way to represent global ops as values without
// having to emit a get global op. Sometimes these emissions are not used.
mlir::Value addr = builder.createGetGlobal(globalOp);
- auto getAddrOp = mlir::cast<cir::GetGlobalOp>(addr.getDefiningOp());
+ auto getAddrOp = addr.getDefiningOp<cir::GetGlobalOp>();
+ assert(getAddrOp && "expected cir::GetGlobalOp");
CharUnits alignment = getContext().getDeclAlign(&d);
diff --git a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
index a375386433866..29e9ddcea0c4b 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
@@ -303,8 +303,7 @@ void CIRGenFunction::emitStoreOfScalar(mlir::Value value, Address addr,
// Update the alloca with more info on initialization.
assert(addr.getPointer() && "expected pointer to exist");
- auto srcAlloca =
- dyn_cast_or_null<cir::AllocaOp>(addr.getPointer().getDefiningOp());
+ auto srcAlloca = addr.getDefiningOp<cir::AllocaOp>();
if (currVarDecl && srcAlloca) {
const VarDecl *vd = currVarDecl;
assert(vd && "VarDecl expected");
@@ -635,10 +634,8 @@ LValue CIRGenFunction::emitUnaryOpLValue(const UnaryOperator *e) {
// Tag 'load' with deref attribute.
// FIXME: This misses some derefence cases and has problematic interactions
// with other operators.
- if (auto loadOp =
- dyn_cast<cir::LoadOp>(addr.getPointer().getDefiningOp())) {
+ if (auto loadOp = addr.getDefiningOp<cir::LoadOp>())
loadOp.setIsDerefAttr(mlir::UnitAttr::get(&getMLIRContext()));
- }
LValue lv = makeAddrLValue(addr, t, baseInfo);
assert(!cir::MissingFeatures::addressSpace());
@@ -1861,9 +1858,9 @@ cir::AllocaOp CIRGenFunction::createTempAlloca(mlir::Type ty,
const Twine &name,
mlir::Value arraySize,
bool insertIntoFnEntryBlock) {
- return cast<cir::AllocaOp>(emitAlloca(name.str(), ty, loc, CharUnits(),
- insertIntoFnEntryBlock, arraySize)
- .getDefiningOp());
+ return mlir::cast<cir::AllocaOp>(emitAlloca(name.str(), ty, loc, CharUnits(),
+ insertIntoFnEntryBlock, arraySize)
+ .getDefiningOp());
}
/// This creates an alloca and inserts it into the provided insertion point
@@ -1873,7 +1870,7 @@ cir::AllocaOp CIRGenFunction::createTempAlloca(mlir::Type ty,
mlir::OpBuilder::InsertPoint ip,
mlir::Value arraySize) {
assert(ip.isSet() && "Insertion point is not set");
- return cast<cir::AllocaOp>(
+ return mlir::cast<cir::AllocaOp>(
emitAlloca(name.str(), ty, loc, CharUnits(), ip, arraySize)
.getDefiningOp());
}
diff --git a/clang/lib/CIR/CodeGen/CIRGenFunction.cpp b/clang/lib/CIR/CodeGen/CIRGenFunction.cpp
index 0c9bc3823f2f5..544cb9cf98c45 100644
--- a/clang/lib/CIR/CodeGen/CIRGenFunction.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenFunction.cpp
@@ -219,7 +219,9 @@ void CIRGenFunction::declare(mlir::Value addrVal, const Decl *var, QualType ty,
assert(isa<NamedDecl>(var) && "Needs a named decl");
assert(!cir::MissingFeatures::cgfSymbolTable());
- auto allocaOp = cast<cir::AllocaOp>(addrVal.getDefiningOp());
+ auto allocaOp = addrVal.getDefiningOp<cir::AllocaOp>();
+ assert(allocaOp && "expected cir::AllocaOp");
+
if (isParam)
allocaOp.setInitAttr(mlir::UnitAttr::get(&getMLIRContext()));
if (ty->isReferenceType() || ty.isConstQualified())
diff --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
index 263ff15d9e005..d3fcac1edeb5a 100644
--- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
@@ -606,7 +606,7 @@ static Value tryFoldCastChain(cir::CastOp op) {
if (!isIntOrBoolCast(op))
break;
head = op;
- op = dyn_cast_or_null<cir::CastOp>(head.getSrc().getDefiningOp());
+ op = head.getSrc().getDefiningOp<cir::CastOp>();
}
if (head == tail)
@@ -1802,7 +1802,7 @@ OpFoldResult cir::UnaryOp::fold(FoldAdaptor adaptor) {
}
if (isBoolNot(*this))
- if (auto previous = dyn_cast_or_null<UnaryOp>(getInput().getDefiningOp()))
+ if (auto previous = getInput().getDefiningOp<cir::UnaryOp>())
if (isBoolNot(previous))
return previous.getInput();
@@ -2184,8 +2184,7 @@ LogicalResult cir::ComplexRealOp::verify() {
}
OpFoldResult cir::ComplexRealOp::fold(FoldAdaptor adaptor) {
- if (auto complexCreateOp =
- dyn_cast_or_null<cir::ComplexCreateOp>(getOperand().getDefiningOp()))
+ if (auto complexCreateOp = getOperand().getDefiningOp<cir::ComplexCreateOp>())
return complexCreateOp.getOperand(0);
auto complex =
@@ -2206,8 +2205,7 @@ LogicalResult cir::ComplexImagOp::verify() {
}
OpFoldResult cir::ComplexImagOp::fold(FoldAdaptor adaptor) {
- if (auto complexCreateOp =
- dyn_cast_or_null<cir::ComplexCreateOp>(getOperand().getDefiningOp()))
+ if (auto complexCreateOp = getOperand().getDefiningOp<cir::ComplexCreateOp>())
return complexCreateOp.getOperand(1);
auto complex =
diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
index 895872b6a14db..dc6e1b7715c67 100644
--- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
+++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
@@ -821,8 +821,7 @@ mlir::LogicalResult CIRToLLVMPtrStrideOpLowering::matchAndRewrite(
// before it. To achieve that, look at unary minus, which already got
// lowered to "sub 0, x".
const auto sub = dyn_cast<mlir::LLVM::SubOp>(indexOp);
- auto unary = dyn_cast_if_present<cir::UnaryOp>(
- ptrStrideOp.getStride().getDefiningOp());
+ auto unary = ptrStrideOp.getStride().getDefiningOp<cir::UnaryOp>();
bool rewriteSub =
unary && unary.getKind() == cir::UnaryOpKind::Minus && sub;
if (rewriteSub)
@@ -2378,15 +2377,14 @@ mlir::LogicalResult CIRToLLVMVecSplatOpLowering::matchAndRewrite(
mlir::Value poison = rewriter.create<mlir::LLVM::PoisonOp>(loc, llvmTy);
mlir::Value elementValue = adaptor.getValue();
- if (mlir::isa<mlir::LLVM::PoisonOp>(elementValue.getDefiningOp())) {
+ if (elementValue.getDefiningOp<mlir::LLVM::PoisonOp>()) {
// If the splat value is poison, then we can just use poison value
// for the entire vector.
rewriter.replaceOp(op, poison);
return mlir::success();
}
- if (auto constValue =
- dyn_cast<mlir::LLVM::ConstantOp>(elementValue.getDefiningOp())) {
+ if (auto constValue = elementValue.getDefiningOp<mlir::LLVM::ConstantOp>()) {
if (auto intAttr = dyn_cast<mlir::IntegerAttr>(constValue.getValue())) {
mlir::DenseIntElementsAttr denseVec = mlir::DenseIntElementsAttr::get(
mlir::cast<mlir::ShapedType>(llvmTy), intAttr.getValue());
More information about the llvm-branch-commits
mailing list