diff options
author | Tres Popp <tpopp@google.com> | 2023-05-08 16:33:54 +0200 |
---|---|---|
committer | Tres Popp <tpopp@google.com> | 2023-05-12 11:21:25 +0200 |
commit | 5550c821897ab77e664977121a0e90ad5be1ff59 (patch) | |
tree | 1947e879997b2fccdb629789362c42310d1f1f84 /mlir/unittests | |
parent | 5c8ce6d5761ed6a9a39ef5a77aa45d8b6095e0f5 (diff) | |
download | llvm-5550c821897ab77e664977121a0e90ad5be1ff59.tar.gz |
[mlir] Move casting calls from methods to function calls
The MLIR classes Type/Attribute/Operation/Op/Value support
cast/dyn_cast/isa/dyn_cast_or_null functionality through llvm's doCast
functionality in addition to defining methods with the same name.
This change begins the migration of uses of the method to the
corresponding function call as has been decided as more consistent.
Note that there still exist classes that only define methods directly,
such as AffineExpr, and this does not include work currently to support
a functional cast/isa call.
Caveats include:
- This clang-tidy script probably has more problems.
- This only touches C++ code, so nothing that is being generated.
Context:
- https://mlir.llvm.org/deprecation/ at "Use the free function variants
for dyn_cast/cast/isa/…"
- Original discussion at https://discourse.llvm.org/t/preferred-casting-style-going-forward/68443
Implementation:
This first patch was created with the following steps. The intention is
to only do automated changes at first, so I waste less time if it's
reverted, and so the first mass change is more clear as an example to
other teams that will need to follow similar steps.
Steps are described per line, as comments are removed by git:
0. Retrieve the change from the following to build clang-tidy with an
additional check:
https://github.com/llvm/llvm-project/compare/main...tpopp:llvm-project:tidy-cast-check
1. Build clang-tidy
2. Run clang-tidy over your entire codebase while disabling all checks
and enabling the one relevant one. Run on all header files also.
3. Delete .inc files that were also modified, so the next build rebuilds
them to a pure state.
4. Some changes have been deleted for the following reasons:
- Some files had a variable also named cast
- Some files had not included a header file that defines the cast
functions
- Some files are definitions of the classes that have the casting
methods, so the code still refers to the method instead of the
function without adding a prefix or removing the method declaration
at the same time.
```
ninja -C $BUILD_DIR clang-tidy
run-clang-tidy -clang-tidy-binary=$BUILD_DIR/bin/clang-tidy -checks='-*,misc-cast-functions'\
-header-filter=mlir/ mlir/* -fix
rm -rf $BUILD_DIR/tools/mlir/**/*.inc
git restore mlir/lib/IR mlir/lib/Dialect/DLTI/DLTI.cpp\
mlir/lib/Dialect/Complex/IR/ComplexDialect.cpp\
mlir/lib/**/IR/\
mlir/lib/Dialect/SparseTensor/Transforms/SparseVectorization.cpp\
mlir/lib/Dialect/Vector/Transforms/LowerVectorMultiReduction.cpp\
mlir/test/lib/Dialect/Test/TestTypes.cpp\
mlir/test/lib/Dialect/Transform/TestTransformDialectExtension.cpp\
mlir/test/lib/Dialect/Test/TestAttributes.cpp\
mlir/unittests/TableGen/EnumsGenTest.cpp\
mlir/test/python/lib/PythonTestCAPI.cpp\
mlir/include/mlir/IR/
```
Differential Revision: https://reviews.llvm.org/D150123
Diffstat (limited to 'mlir/unittests')
-rw-r--r-- | mlir/unittests/Dialect/LLVMIR/LLVMTypeTest.cpp | 8 | ||||
-rw-r--r-- | mlir/unittests/IR/AttributeTest.cpp | 14 | ||||
-rw-r--r-- | mlir/unittests/IR/InterfaceAttachmentTest.cpp | 49 | ||||
-rw-r--r-- | mlir/unittests/Interfaces/DataLayoutInterfacesTest.cpp | 10 | ||||
-rw-r--r-- | mlir/unittests/Pass/PassManagerTest.cpp | 8 |
5 files changed, 44 insertions, 45 deletions
diff --git a/mlir/unittests/Dialect/LLVMIR/LLVMTypeTest.cpp b/mlir/unittests/Dialect/LLVMIR/LLVMTypeTest.cpp index f39093498e09..aa19b5c651f5 100644 --- a/mlir/unittests/Dialect/LLVMIR/LLVMTypeTest.cpp +++ b/mlir/unittests/Dialect/LLVMIR/LLVMTypeTest.cpp @@ -36,18 +36,18 @@ TEST_F(LLVMIRTest, MutualReferencedSubElementTypes) { ASSERT_EQ(subElementTypes.size(), 4U); // !llvm.ptr<struct<"foo",...>> - ASSERT_TRUE(subElementTypes[0].isa<LLVMPointerType>()); + ASSERT_TRUE(isa<LLVMPointerType>(subElementTypes[0])); // !llvm.struct<"bar",...> - auto structType = subElementTypes[1].dyn_cast<LLVMStructType>(); + auto structType = dyn_cast<LLVMStructType>(subElementTypes[1]); ASSERT_TRUE(bool(structType)); ASSERT_TRUE(structType.getName().equals("bar")); // !llvm.ptr<struct<"bar",...>> - ASSERT_TRUE(subElementTypes[2].isa<LLVMPointerType>()); + ASSERT_TRUE(isa<LLVMPointerType>(subElementTypes[2])); // !llvm.struct<"foo",...> - structType = subElementTypes[3].dyn_cast<LLVMStructType>(); + structType = dyn_cast<LLVMStructType>(subElementTypes[3]); ASSERT_TRUE(bool(structType)); ASSERT_TRUE(structType.getName().equals("foo")); } diff --git a/mlir/unittests/IR/AttributeTest.cpp b/mlir/unittests/IR/AttributeTest.cpp index 94345d00f113..f01cc026b72c 100644 --- a/mlir/unittests/IR/AttributeTest.cpp +++ b/mlir/unittests/IR/AttributeTest.cpp @@ -278,7 +278,7 @@ static void checkNativeAccess(MLIRContext *ctx, ArrayRef<T> data, // Check that we cast to this attribute when possible. Attribute genericAttr = attr; - EXPECT_TRUE(genericAttr.template isa<AttrT>()); + EXPECT_TRUE(isa<AttrT>(genericAttr)); } template <typename AttrT, typename T> static void checkNativeIntAccess(Builder &builder, size_t intWidth) { @@ -330,9 +330,9 @@ TEST(DenseResourceElementsAttrTest, CheckNoCast) { Attribute i32ResourceAttr = DenseI32ResourceElementsAttr::get( type, "resource", UnmanagedAsmResourceBlob::allocateInferAlign(data)); - EXPECT_TRUE(i32ResourceAttr.isa<DenseI32ResourceElementsAttr>()); - EXPECT_FALSE(i32ResourceAttr.isa<DenseF32ResourceElementsAttr>()); - EXPECT_FALSE(i32ResourceAttr.isa<DenseBoolResourceElementsAttr>()); + EXPECT_TRUE(isa<DenseI32ResourceElementsAttr>(i32ResourceAttr)); + EXPECT_FALSE(isa<DenseF32ResourceElementsAttr>(i32ResourceAttr)); + EXPECT_FALSE(isa<DenseBoolResourceElementsAttr>(i32ResourceAttr)); } TEST(DenseResourceElementsAttrTest, CheckInvalidData) { @@ -407,17 +407,17 @@ TEST(SparseElementsAttrTest, GetZero) { // Only index (0, 0) contains an element, others are supposed to return // the zero/empty value. auto zeroIntValue = - sparseInt.getValues<Attribute>()[{1, 1}].cast<IntegerAttr>(); + cast<IntegerAttr>(sparseInt.getValues<Attribute>()[{1, 1}]); EXPECT_EQ(zeroIntValue.getInt(), 0); EXPECT_TRUE(zeroIntValue.getType() == intTy); auto zeroFloatValue = - sparseFloat.getValues<Attribute>()[{1, 1}].cast<FloatAttr>(); + cast<FloatAttr>(sparseFloat.getValues<Attribute>()[{1, 1}]); EXPECT_EQ(zeroFloatValue.getValueAsDouble(), 0.0f); EXPECT_TRUE(zeroFloatValue.getType() == floatTy); auto zeroStringValue = - sparseString.getValues<Attribute>()[{1, 1}].cast<StringAttr>(); + cast<StringAttr>(sparseString.getValues<Attribute>()[{1, 1}]); EXPECT_TRUE(zeroStringValue.getValue().empty()); EXPECT_TRUE(zeroStringValue.getType() == stringTy); } diff --git a/mlir/unittests/IR/InterfaceAttachmentTest.cpp b/mlir/unittests/IR/InterfaceAttachmentTest.cpp index d5e19d27f8eb..fe855164f874 100644 --- a/mlir/unittests/IR/InterfaceAttachmentTest.cpp +++ b/mlir/unittests/IR/InterfaceAttachmentTest.cpp @@ -61,11 +61,11 @@ TEST(InterfaceAttachment, Type) { // Check that the type has no interface. IntegerType i8 = IntegerType::get(&context, 8); - ASSERT_FALSE(i8.isa<TestExternalTypeInterface>()); + ASSERT_FALSE(isa<TestExternalTypeInterface>(i8)); // Attach an interface and check that the type now has the interface. IntegerType::attachInterface<Model>(context); - TestExternalTypeInterface iface = i8.dyn_cast<TestExternalTypeInterface>(); + TestExternalTypeInterface iface = dyn_cast<TestExternalTypeInterface>(i8); ASSERT_TRUE(iface != nullptr); EXPECT_EQ(iface.getBitwidthPlusArg(10), 18u); EXPECT_EQ(iface.staticGetSomeValuePlusArg(0), 42u); @@ -74,9 +74,9 @@ TEST(InterfaceAttachment, Type) { // Same, but with the default implementation overridden. FloatType flt = Float32Type::get(&context); - ASSERT_FALSE(flt.isa<TestExternalTypeInterface>()); + ASSERT_FALSE(isa<TestExternalTypeInterface>(flt)); Float32Type::attachInterface<OverridingModel>(context); - iface = flt.dyn_cast<TestExternalTypeInterface>(); + iface = dyn_cast<TestExternalTypeInterface>(flt); ASSERT_TRUE(iface != nullptr); EXPECT_EQ(iface.getBitwidthPlusArg(10), 42u); EXPECT_EQ(iface.staticGetSomeValuePlusArg(10), 52u); @@ -86,7 +86,7 @@ TEST(InterfaceAttachment, Type) { // Other contexts shouldn't have the attribute attached. MLIRContext other; IntegerType i8other = IntegerType::get(&other, 8); - EXPECT_FALSE(i8other.isa<TestExternalTypeInterface>()); + EXPECT_FALSE(isa<TestExternalTypeInterface>(i8other)); } /// External interface model for the test type from the test dialect. @@ -111,7 +111,7 @@ TEST(InterfaceAttachment, TypeDelayedContextConstruct) { MLIRContext context(registry); context.loadDialect<test::TestDialect>(); test::TestType testType = test::TestType::get(&context); - auto iface = testType.dyn_cast<TestExternalTypeInterface>(); + auto iface = dyn_cast<TestExternalTypeInterface>(testType); ASSERT_TRUE(iface != nullptr); EXPECT_EQ(iface.getBitwidthPlusArg(42), 42u); EXPECT_EQ(iface.staticGetSomeValuePlusArg(10), 20u); @@ -130,9 +130,9 @@ TEST(InterfaceAttachment, TypeDelayedContextAppend) { MLIRContext context; context.loadDialect<test::TestDialect>(); test::TestType testType = test::TestType::get(&context); - EXPECT_FALSE(testType.isa<TestExternalTypeInterface>()); + EXPECT_FALSE(isa<TestExternalTypeInterface>(testType)); context.appendDialectRegistry(registry); - EXPECT_TRUE(testType.isa<TestExternalTypeInterface>()); + EXPECT_TRUE(isa<TestExternalTypeInterface>(testType)); } TEST(InterfaceAttachment, RepeatedRegistration) { @@ -156,13 +156,13 @@ TEST(InterfaceAttachment, TypeBuiltinDelayed) { MLIRContext context(registry); IntegerType i16 = IntegerType::get(&context, 16); - EXPECT_TRUE(i16.isa<TestExternalTypeInterface>()); + EXPECT_TRUE(isa<TestExternalTypeInterface>(i16)); MLIRContext initiallyEmpty; IntegerType i32 = IntegerType::get(&initiallyEmpty, 32); - EXPECT_FALSE(i32.isa<TestExternalTypeInterface>()); + EXPECT_FALSE(isa<TestExternalTypeInterface>(i32)); initiallyEmpty.appendDialectRegistry(registry); - EXPECT_TRUE(i32.isa<TestExternalTypeInterface>()); + EXPECT_TRUE(isa<TestExternalTypeInterface>(i32)); } /// The interface provides a default implementation that expects @@ -181,9 +181,8 @@ struct TestExternalFallbackTypeVectorModel : public TestExternalFallbackTypeInterface::FallbackModel< TestExternalFallbackTypeVectorModel> { unsigned getBitwidth(Type type) const { - IntegerType elementType = type.cast<VectorType>() - .getElementType() - .dyn_cast_or_null<IntegerType>(); + IntegerType elementType = + dyn_cast_or_null<IntegerType>(cast<VectorType>(type).getElementType()); return elementType ? elementType.getWidth() : 0; } }; @@ -193,16 +192,16 @@ TEST(InterfaceAttachment, Fallback) { // Just check that we can attach the interface. IntegerType i8 = IntegerType::get(&context, 8); - ASSERT_FALSE(i8.isa<TestExternalFallbackTypeInterface>()); + ASSERT_FALSE(isa<TestExternalFallbackTypeInterface>(i8)); IntegerType::attachInterface<TestExternalFallbackTypeIntegerModel>(context); - ASSERT_TRUE(i8.isa<TestExternalFallbackTypeInterface>()); + ASSERT_TRUE(isa<TestExternalFallbackTypeInterface>(i8)); // Call the method so it is guaranteed not to be instantiated. VectorType vec = VectorType::get({42}, i8); - ASSERT_FALSE(vec.isa<TestExternalFallbackTypeInterface>()); + ASSERT_FALSE(isa<TestExternalFallbackTypeInterface>(vec)); VectorType::attachInterface<TestExternalFallbackTypeVectorModel>(context); - ASSERT_TRUE(vec.isa<TestExternalFallbackTypeInterface>()); - EXPECT_EQ(vec.cast<TestExternalFallbackTypeInterface>().getBitwidth(), 8u); + ASSERT_TRUE(isa<TestExternalFallbackTypeInterface>(vec)); + EXPECT_EQ(cast<TestExternalFallbackTypeInterface>(vec).getBitwidth(), 8u); } /// External model for attribute interfaces. @@ -210,7 +209,7 @@ struct TestExternalIntegerAttrModel : public TestExternalAttrInterface::ExternalModel< TestExternalIntegerAttrModel, IntegerAttr> { const Dialect *getDialectPtr(Attribute attr) const { - return &attr.cast<IntegerAttr>().getDialect(); + return &cast<IntegerAttr>(attr).getDialect(); } static int getSomeNumber() { return 42; } @@ -222,9 +221,9 @@ TEST(InterfaceAttachment, Attribute) { // Attribute interfaces use the exact same mechanism as types, so just check // that the basics work for attributes. IntegerAttr attr = IntegerAttr::get(IntegerType::get(&context, 32), 42); - ASSERT_FALSE(attr.isa<TestExternalAttrInterface>()); + ASSERT_FALSE(isa<TestExternalAttrInterface>(attr)); IntegerAttr::attachInterface<TestExternalIntegerAttrModel>(context); - auto iface = attr.dyn_cast<TestExternalAttrInterface>(); + auto iface = dyn_cast<TestExternalAttrInterface>(attr); ASSERT_TRUE(iface != nullptr); EXPECT_EQ(iface.getDialectPtr(), &attr.getDialect()); EXPECT_EQ(iface.getSomeNumber(), 42); @@ -253,14 +252,14 @@ TEST(InterfaceAttachmentTest, AttributeDelayed) { MLIRContext context(registry); context.loadDialect<test::TestDialect>(); auto attr = test::SimpleAAttr::get(&context); - EXPECT_TRUE(attr.isa<TestExternalAttrInterface>()); + EXPECT_TRUE(isa<TestExternalAttrInterface>(attr)); MLIRContext initiallyEmpty; initiallyEmpty.loadDialect<test::TestDialect>(); attr = test::SimpleAAttr::get(&initiallyEmpty); - EXPECT_FALSE(attr.isa<TestExternalAttrInterface>()); + EXPECT_FALSE(isa<TestExternalAttrInterface>(attr)); initiallyEmpty.appendDialectRegistry(registry); - EXPECT_TRUE(attr.isa<TestExternalAttrInterface>()); + EXPECT_TRUE(isa<TestExternalAttrInterface>(attr)); } /// External interface model for the module operation. Only provides non-default diff --git a/mlir/unittests/Interfaces/DataLayoutInterfacesTest.cpp b/mlir/unittests/Interfaces/DataLayoutInterfacesTest.cpp index 7e0a8f55a552..6601f32f3288 100644 --- a/mlir/unittests/Interfaces/DataLayoutInterfacesTest.cpp +++ b/mlir/unittests/Interfaces/DataLayoutInterfacesTest.cpp @@ -152,16 +152,16 @@ struct OpWithLayout : public Op<OpWithLayout, DataLayoutOpInterface::Trait> { static unsigned getTypeSizeInBits(Type type, const DataLayout &dataLayout, DataLayoutEntryListRef params) { // Make a recursive query. - if (type.isa<FloatType>()) + if (isa<FloatType>(type)) return dataLayout.getTypeSizeInBits( IntegerType::get(type.getContext(), type.getIntOrFloatBitWidth())); // Handle built-in types that are not handled by the default process. - if (auto iType = type.dyn_cast<IntegerType>()) { + if (auto iType = dyn_cast<IntegerType>(type)) { for (DataLayoutEntryInterface entry : params) if (entry.getKey().dyn_cast<Type>() == type) return 8 * - entry.getValue().cast<IntegerAttr>().getValue().getZExtValue(); + cast<IntegerAttr>(entry.getValue()).getValue().getZExtValue(); return 8 * iType.getIntOrFloatBitWidth(); } @@ -217,7 +217,7 @@ struct DLTestDialect : Dialect { void printAttribute(Attribute attr, DialectAsmPrinter &printer) const override { printer << "spec<"; - llvm::interleaveComma(attr.cast<CustomDataLayoutSpec>().getEntries(), + llvm::interleaveComma(cast<CustomDataLayoutSpec>(attr).getEntries(), printer); printer << ">"; } @@ -244,7 +244,7 @@ struct DLTestDialect : Dialect { } void printType(Type type, DialectAsmPrinter &printer) const override { - if (type.isa<SingleQueryType>()) + if (isa<SingleQueryType>(type)) printer << "single_query"; else printer << "no_layout"; diff --git a/mlir/unittests/Pass/PassManagerTest.cpp b/mlir/unittests/Pass/PassManagerTest.cpp index 24e87020c932..97349d681c3a 100644 --- a/mlir/unittests/Pass/PassManagerTest.cpp +++ b/mlir/unittests/Pass/PassManagerTest.cpp @@ -75,12 +75,12 @@ TEST(PassManagerTest, OpSpecificAnalysis) { // Verify that each function got annotated with expected attributes. for (func::FuncOp func : module->getOps<func::FuncOp>()) { - ASSERT_TRUE(func->getAttr("isFunc").isa<BoolAttr>()); - EXPECT_TRUE(func->getAttr("isFunc").cast<BoolAttr>().getValue()); + ASSERT_TRUE(isa<BoolAttr>(func->getAttr("isFunc"))); + EXPECT_TRUE(cast<BoolAttr>(func->getAttr("isFunc")).getValue()); bool isSecret = func.getName() == "secret"; - ASSERT_TRUE(func->getAttr("isSecret").isa<BoolAttr>()); - EXPECT_EQ(func->getAttr("isSecret").cast<BoolAttr>().getValue(), isSecret); + ASSERT_TRUE(isa<BoolAttr>(func->getAttr("isSecret"))); + EXPECT_EQ(cast<BoolAttr>(func->getAttr("isSecret")).getValue(), isSecret); } } |