Skip to content

Commit f149cb8

Browse files
committed
[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
1 parent fa6965f commit f149cb8

File tree

7 files changed

+41
-33
lines changed

7 files changed

+41
-33
lines changed

clang/include/clang/CIR/Dialect/IR/CIROps.td

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,21 @@ def CIR_ConstantOp : CIR_Op<"const", [
291291
return ptrAttr.isNullValue();
292292
return false;
293293
}
294+
295+
template <typename T>
296+
T getValueAttr() { return mlir::dyn_cast<T>(getValue()); }
297+
298+
llvm::APInt getIntValue() {
299+
if (const auto intAttr = getValueAttr<cir::IntAttr>())
300+
return intAttr.getValue();
301+
llvm_unreachable("Expected an IntAttr in ConstantOp");
302+
}
303+
304+
bool getBoolValue() {
305+
if (const auto boolAttr = getValueAttr<cir::BoolAttr>())
306+
return boolAttr.getValue();
307+
llvm_unreachable("Expected a BoolAttr in ConstantOp");
308+
}
294309
}];
295310

296311
let hasFolder = 1;

clang/lib/CIR/CodeGen/CIRGenClass.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -349,12 +349,16 @@ void CIRGenFunction::emitCXXAggrConstructorCall(
349349
// doesn't happen, but it's not clear that it's worth it.
350350

351351
// Optimize for a constant count.
352-
auto constantCount = dyn_cast<cir::ConstantOp>(numElements.getDefiningOp());
353-
if (constantCount) {
354-
auto constIntAttr = mlir::dyn_cast<cir::IntAttr>(constantCount.getValue());
355-
// Just skip out if the constant count is zero.
356-
if (constIntAttr && constIntAttr.getUInt() == 0)
357-
return;
352+
if (auto constantCount = numElements.getDefiningOp<cir::ConstantOp>()) {
353+
if (auto constIntAttr = constantCount.getValueAttr<cir::IntAttr>()) {
354+
// Just skip out if the constant count is zero.
355+
if (constIntAttr.getUInt() == 0)
356+
return;
357+
// Otherwise, emit the check.
358+
}
359+
360+
if (constantCount.use_empty())
361+
constantCount.erase();
358362
} else {
359363
// Otherwise, emit the check.
360364
cgm.errorNYI(e->getSourceRange(), "dynamic-length array expression");
@@ -417,9 +421,6 @@ void CIRGenFunction::emitCXXAggrConstructorCall(
417421
builder.create<cir::YieldOp>(loc);
418422
});
419423
}
420-
421-
if (constantCount.use_empty())
422-
constantCount.erase();
423424
}
424425

425426
void CIRGenFunction::emitDelegateCXXConstructorCall(

clang/lib/CIR/CodeGen/CIRGenExpr.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -712,17 +712,16 @@ static const Expr *getSimpleArrayDecayOperand(const Expr *e) {
712712

713713
static cir::IntAttr getConstantIndexOrNull(mlir::Value idx) {
714714
// TODO(cir): should we consider using MLIRs IndexType instead of IntegerAttr?
715-
if (auto constantOp = dyn_cast<cir::ConstantOp>(idx.getDefiningOp()))
716-
return mlir::dyn_cast<cir::IntAttr>(constantOp.getValue());
715+
if (auto constantOp = idx.getDefiningOp<cir::ConstantOp>())
716+
return constantOp.getValueAttr<cir::IntAttr>();
717717
return {};
718718
}
719719

720720
static CharUnits getArrayElementAlign(CharUnits arrayAlign, mlir::Value idx,
721721
CharUnits eltSize) {
722722
// If we have a constant index, we can use the exact offset of the
723723
// element we're accessing.
724-
const cir::IntAttr constantIdx = getConstantIndexOrNull(idx);
725-
if (constantIdx) {
724+
if (const cir::IntAttr constantIdx = getConstantIndexOrNull(idx)) {
726725
const CharUnits offset = constantIdx.getValue().getZExtValue() * eltSize;
727726
return arrayAlign.alignmentAtOffset(offset);
728727
}

clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ struct BinOpInfo {
4848
/// Check if the binop can result in integer overflow.
4949
bool mayHaveIntegerOverflow() const {
5050
// Without constant input, we can't rule out overflow.
51-
auto lhsci = dyn_cast<cir::ConstantOp>(lhs.getDefiningOp());
52-
auto rhsci = dyn_cast<cir::ConstantOp>(rhs.getDefiningOp());
51+
auto lhsci = lhs.getDefiningOp<cir::ConstantOp>();
52+
auto rhsci = rhs.getDefiningOp<cir::ConstantOp>();
5353
if (!lhsci || !rhsci)
5454
return true;
5555

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1826,7 +1826,7 @@ LogicalResult cir::GetMemberOp::verify() {
18261826

18271827
OpFoldResult cir::VecCreateOp::fold(FoldAdaptor adaptor) {
18281828
if (llvm::any_of(getElements(), [](mlir::Value value) {
1829-
return !mlir::isa<cir::ConstantOp>(value.getDefiningOp());
1829+
return !value.getDefiningOp<cir::ConstantOp>();
18301830
}))
18311831
return {};
18321832

clang/lib/CIR/Dialect/Transforms/CIRSimplify.cpp

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ struct SimplifyTernary final : public OpRewritePattern<TernaryOp> {
9797
// Check whether the region/block contains a cir.const followed by a
9898
// cir.yield that yields the value.
9999
auto yieldOp = mlir::cast<cir::YieldOp>(onlyBlock.getTerminator());
100-
auto yieldValueDefOp = mlir::dyn_cast_if_present<cir::ConstantOp>(
101-
yieldOp.getArgs()[0].getDefiningOp());
100+
auto yieldValueDefOp =
101+
yieldOp.getArgs()[0].getDefiningOp<cir::ConstantOp>();
102102
return yieldValueDefOp && yieldValueDefOp->getBlock() == &onlyBlock;
103103
}
104104
};
@@ -126,18 +126,13 @@ struct SimplifySelect : public OpRewritePattern<SelectOp> {
126126

127127
LogicalResult matchAndRewrite(SelectOp op,
128128
PatternRewriter &rewriter) const final {
129-
mlir::Operation *trueValueOp = op.getTrueValue().getDefiningOp();
130-
mlir::Operation *falseValueOp = op.getFalseValue().getDefiningOp();
131-
auto trueValueConstOp =
132-
mlir::dyn_cast_if_present<cir::ConstantOp>(trueValueOp);
133-
auto falseValueConstOp =
134-
mlir::dyn_cast_if_present<cir::ConstantOp>(falseValueOp);
135-
if (!trueValueConstOp || !falseValueConstOp)
129+
auto trueValueOp = op.getTrueValue().getDefiningOp<cir::ConstantOp>();
130+
auto falseValueOp = op.getFalseValue().getDefiningOp<cir::ConstantOp>();
131+
if (!trueValueOp || !falseValueOp)
136132
return mlir::failure();
137133

138-
auto trueValue = mlir::dyn_cast<cir::BoolAttr>(trueValueConstOp.getValue());
139-
auto falseValue =
140-
mlir::dyn_cast<cir::BoolAttr>(falseValueConstOp.getValue());
134+
auto trueValue = trueValueOp.getValueAttr<cir::BoolAttr>();
135+
auto falseValue = falseValueOp.getValueAttr<cir::BoolAttr>();
141136
if (!trueValue || !falseValue)
142137
return mlir::failure();
143138

@@ -265,8 +260,7 @@ struct SimplifyVecSplat : public OpRewritePattern<VecSplatOp> {
265260
LogicalResult matchAndRewrite(VecSplatOp op,
266261
PatternRewriter &rewriter) const override {
267262
mlir::Value splatValue = op.getValue();
268-
auto constant =
269-
mlir::dyn_cast_if_present<cir::ConstantOp>(splatValue.getDefiningOp());
263+
auto constant = splatValue.getDefiningOp<cir::ConstantOp>();
270264
if (!constant)
271265
return mlir::failure();
272266

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1874,12 +1874,11 @@ mlir::LogicalResult CIRToLLVMSelectOpLowering::matchAndRewrite(
18741874
cir::SelectOp op, OpAdaptor adaptor,
18751875
mlir::ConversionPatternRewriter &rewriter) const {
18761876
auto getConstantBool = [](mlir::Value value) -> cir::BoolAttr {
1877-
auto definingOp =
1878-
mlir::dyn_cast_if_present<cir::ConstantOp>(value.getDefiningOp());
1877+
auto definingOp = value.getDefiningOp<cir::ConstantOp>();
18791878
if (!definingOp)
18801879
return {};
18811880

1882-
auto constValue = mlir::dyn_cast<cir::BoolAttr>(definingOp.getValue());
1881+
auto constValue = definingOp.getValueAttr<cir::BoolAttr>();
18831882
if (!constValue)
18841883
return {};
18851884

0 commit comments

Comments
 (0)