[clang] [CIR] Simplify ConstantOp accesses and its getDefiningOp (PR #151216)
Henrich Lauko via cfe-commits
cfe-commits at lists.llvm.org
Fri Aug 1 11:21:03 PDT 2025
https://github.com/xlauko updated https://github.com/llvm/llvm-project/pull/151216
>From 376046571e736227c8ce8bb39b90d4570429bdc0 Mon Sep 17 00:00:00 2001
From: xlauko <xlauko at mail.muni.cz>
Date: Tue, 29 Jul 2025 21:38:48 +0200
Subject: [PATCH] [CIR] Simplify ConstantOp accesses and its getDefiningOp
- Replaces dyn_cast<cir::ConstantOp>(v.getDefiningOp()) and similar with v.getDefiningOp<cir::ConstantOp>()
- Adds `getValueAttr`, `getIntValue` and `getBoolValue` methods to ConstantOp
---
clang/include/clang/CIR/Dialect/IR/CIROps.td | 3 +++
clang/lib/CIR/CodeGen/CIRGenClass.cpp | 19 ++++++++--------
clang/lib/CIR/CodeGen/CIRGenExpr.cpp | 7 +++---
clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp | 4 ++--
clang/lib/CIR/Dialect/IR/CIRDialect.cpp | 2 +-
.../CIR/Dialect/Transforms/CIRSimplify.cpp | 22 +++++++------------
.../CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 5 ++---
7 files changed, 29 insertions(+), 33 deletions(-)
diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td
index 5ef5b60ed5a52..72841a1c08441 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIROps.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td
@@ -291,6 +291,9 @@ def CIR_ConstantOp : CIR_Op<"const", [
return ptrAttr.isNullValue();
return false;
}
+
+ template <typename T>
+ T getValueAttr() { return mlir::dyn_cast<T>(getValue()); }
}];
let hasFolder = 1;
diff --git a/clang/lib/CIR/CodeGen/CIRGenClass.cpp b/clang/lib/CIR/CodeGen/CIRGenClass.cpp
index 50cca0e63611e..72b9d177e4c63 100644
--- a/clang/lib/CIR/CodeGen/CIRGenClass.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenClass.cpp
@@ -349,12 +349,16 @@ void CIRGenFunction::emitCXXAggrConstructorCall(
// doesn't happen, but it's not clear that it's worth it.
// Optimize for a constant count.
- auto constantCount = dyn_cast<cir::ConstantOp>(numElements.getDefiningOp());
- if (constantCount) {
- auto constIntAttr = mlir::dyn_cast<cir::IntAttr>(constantCount.getValue());
- // Just skip out if the constant count is zero.
- if (constIntAttr && constIntAttr.getUInt() == 0)
- return;
+ if (auto constantCount = numElements.getDefiningOp<cir::ConstantOp>()) {
+ if (auto constIntAttr = constantCount.getValueAttr<cir::IntAttr>()) {
+ // Just skip out if the constant count is zero.
+ if (constIntAttr.getUInt() == 0)
+ return;
+ // Otherwise, emit the check.
+ }
+
+ if (constantCount.use_empty())
+ constantCount.erase();
} else {
// Otherwise, emit the check.
cgm.errorNYI(e->getSourceRange(), "dynamic-length array expression");
@@ -417,9 +421,6 @@ void CIRGenFunction::emitCXXAggrConstructorCall(
builder.create<cir::YieldOp>(loc);
});
}
-
- if (constantCount.use_empty())
- constantCount.erase();
}
void CIRGenFunction::emitDelegateCXXConstructorCall(
diff --git a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
index d267504dedd85..a375386433866 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
@@ -721,8 +721,8 @@ static const Expr *getSimpleArrayDecayOperand(const Expr *e) {
static cir::IntAttr getConstantIndexOrNull(mlir::Value idx) {
// TODO(cir): should we consider using MLIRs IndexType instead of IntegerAttr?
- if (auto constantOp = dyn_cast<cir::ConstantOp>(idx.getDefiningOp()))
- return mlir::dyn_cast<cir::IntAttr>(constantOp.getValue());
+ if (auto constantOp = idx.getDefiningOp<cir::ConstantOp>())
+ return constantOp.getValueAttr<cir::IntAttr>();
return {};
}
@@ -730,8 +730,7 @@ static CharUnits getArrayElementAlign(CharUnits arrayAlign, mlir::Value idx,
CharUnits eltSize) {
// If we have a constant index, we can use the exact offset of the
// element we're accessing.
- const cir::IntAttr constantIdx = getConstantIndexOrNull(idx);
- if (constantIdx) {
+ if (const cir::IntAttr constantIdx = getConstantIndexOrNull(idx)) {
const CharUnits offset = constantIdx.getValue().getZExtValue() * eltSize;
return arrayAlign.alignmentAtOffset(offset);
}
diff --git a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
index f62be49ee7af3..ccdec96e239dc 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
@@ -48,8 +48,8 @@ struct BinOpInfo {
/// Check if the binop can result in integer overflow.
bool mayHaveIntegerOverflow() const {
// Without constant input, we can't rule out overflow.
- auto lhsci = dyn_cast<cir::ConstantOp>(lhs.getDefiningOp());
- auto rhsci = dyn_cast<cir::ConstantOp>(rhs.getDefiningOp());
+ auto lhsci = lhs.getDefiningOp<cir::ConstantOp>();
+ auto rhsci = rhs.getDefiningOp<cir::ConstantOp>();
if (!lhsci || !rhsci)
return true;
diff --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
index 1c3a31091aa8d..263ff15d9e005 100644
--- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
@@ -1833,7 +1833,7 @@ LogicalResult cir::GetMemberOp::verify() {
OpFoldResult cir::VecCreateOp::fold(FoldAdaptor adaptor) {
if (llvm::any_of(getElements(), [](mlir::Value value) {
- return !mlir::isa<cir::ConstantOp>(value.getDefiningOp());
+ return !value.getDefiningOp<cir::ConstantOp>();
}))
return {};
diff --git a/clang/lib/CIR/Dialect/Transforms/CIRSimplify.cpp b/clang/lib/CIR/Dialect/Transforms/CIRSimplify.cpp
index 3b7f08c441405..3c6f76892d5cb 100644
--- a/clang/lib/CIR/Dialect/Transforms/CIRSimplify.cpp
+++ b/clang/lib/CIR/Dialect/Transforms/CIRSimplify.cpp
@@ -97,8 +97,8 @@ struct SimplifyTernary final : public OpRewritePattern<TernaryOp> {
// Check whether the region/block contains a cir.const followed by a
// cir.yield that yields the value.
auto yieldOp = mlir::cast<cir::YieldOp>(onlyBlock.getTerminator());
- auto yieldValueDefOp = mlir::dyn_cast_if_present<cir::ConstantOp>(
- yieldOp.getArgs()[0].getDefiningOp());
+ auto yieldValueDefOp =
+ yieldOp.getArgs()[0].getDefiningOp<cir::ConstantOp>();
return yieldValueDefOp && yieldValueDefOp->getBlock() == &onlyBlock;
}
};
@@ -126,18 +126,13 @@ struct SimplifySelect : public OpRewritePattern<SelectOp> {
LogicalResult matchAndRewrite(SelectOp op,
PatternRewriter &rewriter) const final {
- mlir::Operation *trueValueOp = op.getTrueValue().getDefiningOp();
- mlir::Operation *falseValueOp = op.getFalseValue().getDefiningOp();
- auto trueValueConstOp =
- mlir::dyn_cast_if_present<cir::ConstantOp>(trueValueOp);
- auto falseValueConstOp =
- mlir::dyn_cast_if_present<cir::ConstantOp>(falseValueOp);
- if (!trueValueConstOp || !falseValueConstOp)
+ auto trueValueOp = op.getTrueValue().getDefiningOp<cir::ConstantOp>();
+ auto falseValueOp = op.getFalseValue().getDefiningOp<cir::ConstantOp>();
+ if (!trueValueOp || !falseValueOp)
return mlir::failure();
- auto trueValue = mlir::dyn_cast<cir::BoolAttr>(trueValueConstOp.getValue());
- auto falseValue =
- mlir::dyn_cast<cir::BoolAttr>(falseValueConstOp.getValue());
+ auto trueValue = trueValueOp.getValueAttr<cir::BoolAttr>();
+ auto falseValue = falseValueOp.getValueAttr<cir::BoolAttr>();
if (!trueValue || !falseValue)
return mlir::failure();
@@ -265,8 +260,7 @@ struct SimplifyVecSplat : public OpRewritePattern<VecSplatOp> {
LogicalResult matchAndRewrite(VecSplatOp op,
PatternRewriter &rewriter) const override {
mlir::Value splatValue = op.getValue();
- auto constant =
- mlir::dyn_cast_if_present<cir::ConstantOp>(splatValue.getDefiningOp());
+ auto constant = splatValue.getDefiningOp<cir::ConstantOp>();
if (!constant)
return mlir::failure();
diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
index 957a51ab334aa..895872b6a14db 100644
--- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
+++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
@@ -1935,12 +1935,11 @@ mlir::LogicalResult CIRToLLVMSelectOpLowering::matchAndRewrite(
cir::SelectOp op, OpAdaptor adaptor,
mlir::ConversionPatternRewriter &rewriter) const {
auto getConstantBool = [](mlir::Value value) -> cir::BoolAttr {
- auto definingOp =
- mlir::dyn_cast_if_present<cir::ConstantOp>(value.getDefiningOp());
+ auto definingOp = value.getDefiningOp<cir::ConstantOp>();
if (!definingOp)
return {};
- auto constValue = mlir::dyn_cast<cir::BoolAttr>(definingOp.getValue());
+ auto constValue = definingOp.getValueAttr<cir::BoolAttr>();
if (!constValue)
return {};
More information about the cfe-commits
mailing list