Skip to content

Commit cc7008c

Browse files
[mlir] Migrate away from PointerUnion::dyn_cast (NFC)
Note that PointerUnion::dyn_cast has been soft deprecated in PointerUnion.h: // FIXME: Replace the uses of is(), get() and dyn_cast() with // isa<T>, cast<T> and the llvm::dyn_cast<T>
1 parent 79231a8 commit cc7008c

File tree

9 files changed

+21
-16
lines changed

9 files changed

+21
-16
lines changed

mlir/examples/transform-opt/mlir-transform-opt.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,17 @@ class DiagnosticHandlerWrapper {
120120
/// Verifies the captured "expected-*" diagnostics if required.
121121
llvm::LogicalResult verify() const {
122122
if (auto *ptr =
123-
handler.dyn_cast<mlir::SourceMgrDiagnosticVerifierHandler *>()) {
123+
dyn_cast_if_present<mlir::SourceMgrDiagnosticVerifierHandler *>(
124+
handler)) {
124125
return ptr->verify();
125126
}
126127
return mlir::success();
127128
}
128129

129130
/// Destructs the object of the same type as allocated.
130131
~DiagnosticHandlerWrapper() {
131-
if (auto *ptr = handler.dyn_cast<mlir::SourceMgrDiagnosticHandler *>()) {
132+
if (auto *ptr =
133+
dyn_cast_if_present<mlir::SourceMgrDiagnosticHandler *>(handler)) {
132134
delete ptr;
133135
} else {
134136
delete cast<mlir::SourceMgrDiagnosticVerifierHandler *>(handler);

mlir/lib/Conversion/VectorToLLVM/ConvertVectorToLLVM.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ static Value getIndexedPtrs(ConversionPatternRewriter &rewriter, Location loc,
119119
/// an LLVM constant op.
120120
static Value getAsLLVMValue(OpBuilder &builder, Location loc,
121121
OpFoldResult foldResult) {
122-
if (auto attr = foldResult.dyn_cast<Attribute>()) {
122+
if (auto attr = dyn_cast_if_present<Attribute>(foldResult)) {
123123
auto intAttr = cast<IntegerAttr>(attr);
124124
return builder.create<LLVM::ConstantOp>(loc, intAttr).getResult();
125125
}

mlir/lib/Dialect/Transform/Transforms/TransformInterpreterUtils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ LogicalResult transform::applyTransformNamedSequence(
210210
<< "expected one payload to be bound to the first argument, got "
211211
<< bindings.at(0).size();
212212
}
213-
auto *payloadRoot = bindings.at(0).front().dyn_cast<Operation *>();
213+
auto *payloadRoot = dyn_cast_if_present<Operation *>(bindings.at(0).front());
214214
if (!payloadRoot) {
215215
return transformRoot->emitError() << "expected the object bound to the "
216216
"first argument to be an operation";

mlir/lib/Dialect/Vector/IR/VectorOps.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ SmallVector<Value> vector::getAsValues(OpBuilder &builder, Location loc,
340340
SmallVector<Value> values;
341341
llvm::transform(foldResults, std::back_inserter(values),
342342
[&](OpFoldResult foldResult) {
343-
if (auto attr = foldResult.dyn_cast<Attribute>())
343+
if (auto attr = dyn_cast_if_present<Attribute>(foldResult))
344344
return builder
345345
.create<arith::ConstantIndexOp>(
346346
loc, cast<IntegerAttr>(attr).getInt())
@@ -2880,7 +2880,7 @@ LogicalResult InsertOp::verify() {
28802880
return emitOpError(
28812881
"expected position attribute rank to match the dest vector rank");
28822882
for (auto [idx, pos] : llvm::enumerate(position)) {
2883-
if (auto attr = pos.dyn_cast<Attribute>()) {
2883+
if (auto attr = dyn_cast_if_present<Attribute>(pos)) {
28842884
int64_t constIdx = cast<IntegerAttr>(attr).getInt();
28852885
if (constIdx < 0 || constIdx >= destVectorType.getDimSize(idx)) {
28862886
return emitOpError("expected position attribute #")

mlir/lib/Dialect/Vector/Transforms/VectorEmulateNarrowType.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,10 @@ static Value dynamicallyExtractSubVector(OpBuilder &rewriter, Location loc,
242242
int64_t numElementsToExtract) {
243243
for (int i = 0; i < numElementsToExtract; ++i) {
244244
Value extractLoc =
245-
(i == 0) ? offset.dyn_cast<Value>()
245+
(i == 0) ? dyn_cast_if_present<Value>(offset)
246246
: rewriter.create<arith::AddIOp>(
247-
loc, rewriter.getIndexType(), offset.dyn_cast<Value>(),
247+
loc, rewriter.getIndexType(),
248+
dyn_cast_if_present<Value>(offset),
248249
rewriter.create<arith::ConstantIndexOp>(loc, i));
249250
auto extractOp =
250251
rewriter.create<vector::ExtractOp>(loc, source, extractLoc);

mlir/lib/IR/AffineMap.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -748,7 +748,7 @@ AffineMap mlir::foldAttributesIntoMap(Builder &b, AffineMap map,
748748
SmallVector<AffineExpr> dimReplacements, symReplacements;
749749
int64_t numDims = 0;
750750
for (int64_t i = 0; i < map.getNumDims(); ++i) {
751-
if (auto attr = operands[i].dyn_cast<Attribute>()) {
751+
if (auto attr = dyn_cast_if_present<Attribute>(operands[i])) {
752752
dimReplacements.push_back(
753753
b.getAffineConstantExpr(cast<IntegerAttr>(attr).getInt()));
754754
} else {
@@ -758,7 +758,8 @@ AffineMap mlir::foldAttributesIntoMap(Builder &b, AffineMap map,
758758
}
759759
int64_t numSymbols = 0;
760760
for (int64_t i = 0; i < map.getNumSymbols(); ++i) {
761-
if (auto attr = operands[i + map.getNumDims()].dyn_cast<Attribute>()) {
761+
if (auto attr =
762+
dyn_cast_if_present<Attribute>(operands[i + map.getNumDims()])) {
762763
symReplacements.push_back(
763764
b.getAffineConstantExpr(cast<IntegerAttr>(attr).getInt()));
764765
} else {

mlir/lib/Transforms/Utils/GreedyPatternRewriteDriver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ bool GreedyPatternRewriteDriver::processWorklist() {
515515
bool materializationSucceeded = true;
516516
for (auto [ofr, resultType] :
517517
llvm::zip_equal(foldResults, op->getResultTypes())) {
518-
if (auto value = ofr.dyn_cast<Value>()) {
518+
if (auto value = dyn_cast_if_present<Value>(ofr)) {
519519
assert(value.getType() == resultType &&
520520
"folder produced value of incorrect type");
521521
replacements.push_back(value);

mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1781,7 +1781,7 @@ void OpEmitter::genPropertiesSupportForBytecode(
17811781
writePropertiesMethod << tgfmt(writeBytecodeSegmentSizeLegacy, &fmtCtxt);
17821782
}
17831783
if (const auto *namedProperty =
1784-
attrOrProp.dyn_cast<const NamedProperty *>()) {
1784+
dyn_cast_if_present<const NamedProperty *>(attrOrProp)) {
17851785
StringRef name = namedProperty->name;
17861786
readPropertiesMethod << formatv(
17871787
R"(
@@ -1807,7 +1807,8 @@ void OpEmitter::genPropertiesSupportForBytecode(
18071807
name, tgfmt(namedProperty->prop.getWriteToMlirBytecodeCall(), &fctx));
18081808
continue;
18091809
}
1810-
const auto *namedAttr = attrOrProp.dyn_cast<const AttributeMetadata *>();
1810+
const auto *namedAttr =
1811+
dyn_cast_if_present<const AttributeMetadata *>(attrOrProp);
18111812
StringRef name = namedAttr->attrName;
18121813
if (namedAttr->isRequired) {
18131814
readPropertiesMethod << formatv(R"(

mlir/unittests/IR/SymbolTableTest.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ class ReplaceAllSymbolUsesTest : public ::testing::Test {
4949
// Check that it got renamed.
5050
bool calleeFound = false;
5151
fooOp->walk([&](CallOpInterface callOp) {
52-
StringAttr callee = callOp.getCallableForCallee()
53-
.dyn_cast<SymbolRefAttr>()
54-
.getLeafReference();
52+
StringAttr callee =
53+
dyn_cast_if_present<SymbolRefAttr>(callOp.getCallableForCallee())
54+
.getLeafReference();
5555
EXPECT_EQ(callee, "baz");
5656
calleeFound = true;
5757
});

0 commit comments

Comments
 (0)