-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[CIR] Simplify ConstantOp accesses and its getDefiningOp #151216
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Contributor
Author
This stack of pull requests is managed by Graphite. Learn more about stacking. |
Member
|
@llvm/pr-subscribers-clang @llvm/pr-subscribers-clangir Author: Henrich Lauko (xlauko) Changes
Full diff: https://github.com/llvm/llvm-project/pull/151216.diff 7 Files Affected:
diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td
index e5a0ba707e0e2..ae842e988b3bb 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIROps.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td
@@ -291,6 +291,21 @@ def CIR_ConstantOp : CIR_Op<"const", [
return ptrAttr.isNullValue();
return false;
}
+
+ template <typename T>
+ T getValueAttr() { return mlir::dyn_cast<T>(getValue()); }
+
+ llvm::APInt getIntValue() {
+ if (const auto intAttr = getValueAttr<cir::IntAttr>())
+ return intAttr.getValue();
+ llvm_unreachable("Expected an IntAttr in ConstantOp");
+ }
+
+ bool getBoolValue() {
+ if (const auto boolAttr = getValueAttr<cir::BoolAttr>())
+ return boolAttr.getValue();
+ llvm_unreachable("Expected a BoolAttr in ConstantOp");
+ }
}];
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 64dc1ce1d1e20..3a6732394f1cb 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
@@ -712,8 +712,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 {};
}
@@ -721,8 +721,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 2523b0ff33787..41243a77d9df6 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 35408bfcb8ba7..6cde1fc2d300e 100644
--- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
@@ -1826,7 +1826,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 0ed632fa09c05..b15205f95bc95 100644
--- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
+++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
@@ -1874,12 +1874,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 {};
|
f149cb8 to
1770a46
Compare
Contributor
Author
|
@andykaylor ping |
andykaylor
approved these changes
Aug 1, 2025
Contributor
andykaylor
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great
3f27255 to
2312172
Compare
Contributor
Author
Merge activity
|
3760465 to
05b0240
Compare
- Replaces dyn_cast<cir::ConstantOp>(v.getDefiningOp()) and similar with v.getDefiningOp<cir::ConstantOp>() - Adds `getValueAttr`, `getIntValue` and `getBoolValue` methods to ConstantOp
05b0240 to
88e5e8b
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.

getValueAttrmethod to ConstantOp