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/lib/Conversion/GPUCommon/GPUOpsLowering.cpp | |
parent | 5c8ce6d5761ed6a9a39ef5a77aa45d8b6095e0f5 (diff) | |
download | llvm-5550c821897ab77e664977121a0e90ad5be1ff59.zip llvm-5550c821897ab77e664977121a0e90ad5be1ff59.tar.gz llvm-5550c821897ab77e664977121a0e90ad5be1ff59.tar.bz2 |
[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/lib/Conversion/GPUCommon/GPUOpsLowering.cpp')
-rw-r--r-- | mlir/lib/Conversion/GPUCommon/GPUOpsLowering.cpp | 34 |
1 files changed, 15 insertions, 19 deletions
diff --git a/mlir/lib/Conversion/GPUCommon/GPUOpsLowering.cpp b/mlir/lib/Conversion/GPUCommon/GPUOpsLowering.cpp index a13acc6..664d077 100644 --- a/mlir/lib/Conversion/GPUCommon/GPUOpsLowering.cpp +++ b/mlir/lib/Conversion/GPUCommon/GPUOpsLowering.cpp @@ -26,22 +26,20 @@ GPUFuncOpLowering::matchAndRewrite(gpu::GPUFuncOp gpuFuncOp, OpAdaptor adaptor, for (const auto &en : llvm::enumerate(gpuFuncOp.getWorkgroupAttributions())) { BlockArgument attribution = en.value(); - auto type = attribution.getType().dyn_cast<MemRefType>(); + auto type = dyn_cast<MemRefType>(attribution.getType()); assert(type && type.hasStaticShape() && "unexpected type in attribution"); uint64_t numElements = type.getNumElements(); auto elementType = - typeConverter->convertType(type.getElementType()).template cast<Type>(); + cast<Type>(typeConverter->convertType(type.getElementType())); auto arrayType = LLVM::LLVMArrayType::get(elementType, numElements); std::string name = std::string( llvm::formatv("__wg_{0}_{1}", gpuFuncOp.getName(), en.index())); uint64_t alignment = 0; if (auto alignAttr = - gpuFuncOp - .getWorkgroupAttributionAttr( - en.index(), LLVM::LLVMDialect::getAlignAttrName()) - .dyn_cast_or_null<IntegerAttr>()) + dyn_cast_or_null<IntegerAttr>(gpuFuncOp.getWorkgroupAttributionAttr( + en.index(), LLVM::LLVMDialect::getAlignAttrName()))) alignment = alignAttr.getInt(); auto globalOp = rewriter.create<LLVM::GlobalOp>( gpuFuncOp.getLoc(), arrayType, /*isConstant=*/false, @@ -100,7 +98,7 @@ GPUFuncOpLowering::matchAndRewrite(gpu::GPUFuncOp gpuFuncOp, OpAdaptor adaptor, global.getAddrSpace()), global.getSymNameAttr()); auto elementType = - global.getType().cast<LLVM::LLVMArrayType>().getElementType(); + cast<LLVM::LLVMArrayType>(global.getType()).getElementType(); Value memory = rewriter.create<LLVM::GEPOp>( loc, getTypeConverter()->getPointerType(elementType, @@ -112,7 +110,7 @@ GPUFuncOpLowering::matchAndRewrite(gpu::GPUFuncOp gpuFuncOp, OpAdaptor adaptor, // otherwise necessary given that memref sizes are fixed, but we can try // and canonicalize that away later. Value attribution = gpuFuncOp.getWorkgroupAttributions()[en.index()]; - auto type = attribution.getType().cast<MemRefType>(); + auto type = cast<MemRefType>(attribution.getType()); auto descr = MemRefDescriptor::fromStaticShape( rewriter, loc, *getTypeConverter(), type, memory); signatureConversion.remapInput(numProperArguments + en.index(), descr); @@ -123,7 +121,7 @@ GPUFuncOpLowering::matchAndRewrite(gpu::GPUFuncOp gpuFuncOp, OpAdaptor adaptor, auto int64Ty = IntegerType::get(rewriter.getContext(), 64); for (const auto &en : llvm::enumerate(gpuFuncOp.getPrivateAttributions())) { Value attribution = en.value(); - auto type = attribution.getType().cast<MemRefType>(); + auto type = cast<MemRefType>(attribution.getType()); assert(type && type.hasStaticShape() && "unexpected type in attribution"); // Explicitly drop memory space when lowering private memory @@ -136,10 +134,8 @@ GPUFuncOpLowering::matchAndRewrite(gpu::GPUFuncOp gpuFuncOp, OpAdaptor adaptor, gpuFuncOp.getLoc(), int64Ty, type.getNumElements()); uint64_t alignment = 0; if (auto alignAttr = - gpuFuncOp - .getPrivateAttributionAttr( - en.index(), LLVM::LLVMDialect::getAlignAttrName()) - .dyn_cast_or_null<IntegerAttr>()) + dyn_cast_or_null<IntegerAttr>(gpuFuncOp.getPrivateAttributionAttr( + en.index(), LLVM::LLVMDialect::getAlignAttrName()))) alignment = alignAttr.getInt(); Value allocated = rewriter.create<LLVM::AllocaOp>( gpuFuncOp.getLoc(), ptrType, elementType, numElements, alignment); @@ -164,7 +160,7 @@ GPUFuncOpLowering::matchAndRewrite(gpu::GPUFuncOp gpuFuncOp, OpAdaptor adaptor, OpBuilder::InsertionGuard guard(rewriter); rewriter.setInsertionPointToStart(&llvmFuncOp.getBody().front()); for (const auto &en : llvm::enumerate(gpuFuncOp.getArgumentTypes())) { - auto memrefTy = en.value().dyn_cast<MemRefType>(); + auto memrefTy = dyn_cast<MemRefType>(en.value()); if (!memrefTy) continue; assert(memrefTy.hasStaticShape() && @@ -302,7 +298,7 @@ LogicalResult GPUPrintfOpToHIPLowering::matchAndRewrite( rewriter.create<LLVM::ConstantOp>(loc, llvmI32, numArgsThisCall)); for (size_t i = group; i < bound; ++i) { Value arg = adaptor.getArgs()[i]; - if (auto floatType = arg.getType().dyn_cast<FloatType>()) { + if (auto floatType = dyn_cast<FloatType>(arg.getType())) { if (!floatType.isF64()) arg = rewriter.create<LLVM::FPExtOp>( loc, typeConverter->convertType(rewriter.getF64Type()), arg); @@ -428,7 +424,7 @@ LogicalResult GPUPrintfOpToVPrintfLowering::matchAndRewrite( Type type = arg.getType(); Value promotedArg = arg; assert(type.isIntOrFloat()); - if (type.isa<FloatType>()) { + if (isa<FloatType>(type)) { type = rewriter.getF64Type(); promotedArg = rewriter.create<LLVM::FPExtOp>(loc, type, arg); } @@ -462,14 +458,14 @@ LogicalResult impl::scalarizeVectorOp(Operation *op, ValueRange operands, LLVMTypeConverter &converter) { TypeRange operandTypes(operands); if (llvm::none_of(operandTypes, - [](Type type) { return type.isa<VectorType>(); })) { + [](Type type) { return isa<VectorType>(type); })) { return rewriter.notifyMatchFailure(op, "expected vector operand"); } if (op->getNumRegions() != 0 || op->getNumSuccessors() != 0) return rewriter.notifyMatchFailure(op, "expected no region/successor"); if (op->getNumResults() != 1) return rewriter.notifyMatchFailure(op, "expected single result"); - VectorType vectorType = op->getResult(0).getType().dyn_cast<VectorType>(); + VectorType vectorType = dyn_cast<VectorType>(op->getResult(0).getType()); if (!vectorType) return rewriter.notifyMatchFailure(op, "expected vector result"); @@ -482,7 +478,7 @@ LogicalResult impl::scalarizeVectorOp(Operation *op, ValueRange operands, for (int64_t i = 0; i < vectorType.getNumElements(); ++i) { Value index = rewriter.create<LLVM::ConstantOp>(loc, indexType, i); auto extractElement = [&](Value operand) -> Value { - if (!operand.getType().isa<VectorType>()) + if (!isa<VectorType>(operand.getType())) return operand; return rewriter.create<LLVM::ExtractElementOp>(loc, operand, index); }; |