Files
clang-p2996/mlir/test/lib/Dialect/Affine/TestVectorizationUtils.cpp
Tres Popp 5550c82189 [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
2023-05-12 11:21:25 +02:00

294 lines
9.9 KiB
C++

//===- VectorizerTestPass.cpp - VectorizerTestPass Pass Impl --------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file implements a simple testing pass for vectorization functionality.
//
//===----------------------------------------------------------------------===//
#include "mlir/Analysis/SliceAnalysis.h"
#include "mlir/Dialect/Affine/Analysis/AffineAnalysis.h"
#include "mlir/Dialect/Affine/Analysis/NestedMatcher.h"
#include "mlir/Dialect/Affine/IR/AffineOps.h"
#include "mlir/Dialect/Affine/LoopUtils.h"
#include "mlir/Dialect/Affine/Utils.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/Dialect/Utils/IndexingUtils.h"
#include "mlir/Dialect/Vector/IR/VectorOps.h"
#include "mlir/Dialect/Vector/Utils/VectorUtils.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/BuiltinTypes.h"
#include "mlir/IR/Diagnostics.h"
#include "mlir/Pass/Pass.h"
#include "mlir/Transforms/Passes.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#define DEBUG_TYPE "affine-super-vectorizer-test"
using namespace mlir;
using namespace mlir::affine;
static llvm::cl::OptionCategory clOptionsCategory(DEBUG_TYPE " options");
static llvm::cl::list<int> clTestVectorShapeRatio(
"vector-shape-ratio",
llvm::cl::desc("Specify the HW vector size for vectorization"),
llvm::cl::cat(clOptionsCategory));
static llvm::cl::opt<bool> clTestForwardSlicingAnalysis(
"forward-slicing",
llvm::cl::desc("Enable testing forward static slicing and topological sort "
"functionalities"),
llvm::cl::cat(clOptionsCategory));
static llvm::cl::opt<bool> clTestBackwardSlicingAnalysis(
"backward-slicing",
llvm::cl::desc("Enable testing backward static slicing and "
"topological sort functionalities"),
llvm::cl::cat(clOptionsCategory));
static llvm::cl::opt<bool> clTestSlicingAnalysis(
"slicing",
llvm::cl::desc("Enable testing static slicing and topological sort "
"functionalities"),
llvm::cl::cat(clOptionsCategory));
static llvm::cl::opt<bool> clTestComposeMaps(
"compose-maps",
llvm::cl::desc(
"Enable testing the composition of AffineMap where each "
"AffineMap in the composition is specified as the affine_map attribute "
"in a constant op."),
llvm::cl::cat(clOptionsCategory));
static llvm::cl::opt<bool> clTestVecAffineLoopNest(
"vectorize-affine-loop-nest",
llvm::cl::desc(
"Enable testing for the 'vectorizeAffineLoopNest' utility by "
"vectorizing the outermost loops found"),
llvm::cl::cat(clOptionsCategory));
namespace {
struct VectorizerTestPass
: public PassWrapper<VectorizerTestPass, OperationPass<func::FuncOp>> {
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(VectorizerTestPass)
static constexpr auto kTestAffineMapOpName = "test_affine_map";
static constexpr auto kTestAffineMapAttrName = "affine_map";
void getDependentDialects(DialectRegistry &registry) const override {
registry.insert<vector::VectorDialect>();
}
StringRef getArgument() const final { return "affine-super-vectorizer-test"; }
StringRef getDescription() const final {
return "Tests vectorizer standalone functionality.";
}
void runOnOperation() override;
void testVectorShapeRatio(llvm::raw_ostream &outs);
void testForwardSlicing(llvm::raw_ostream &outs);
void testBackwardSlicing(llvm::raw_ostream &outs);
void testSlicing(llvm::raw_ostream &outs);
void testComposeMaps(llvm::raw_ostream &outs);
/// Test for 'vectorizeAffineLoopNest' utility.
void testVecAffineLoopNest(llvm::raw_ostream &outs);
};
} // namespace
void VectorizerTestPass::testVectorShapeRatio(llvm::raw_ostream &outs) {
auto f = getOperation();
using affine::matcher::Op;
SmallVector<int64_t, 8> shape(clTestVectorShapeRatio.begin(),
clTestVectorShapeRatio.end());
auto subVectorType =
VectorType::get(shape, FloatType::getF32(f.getContext()));
// Only filter operations that operate on a strict super-vector and have one
// return. This makes testing easier.
auto filter = [&](Operation &op) {
assert(subVectorType.getElementType().isF32() &&
"Only f32 supported for now");
if (!mlir::matcher::operatesOnSuperVectorsOf(op, subVectorType)) {
return false;
}
if (op.getNumResults() != 1) {
return false;
}
return true;
};
auto pat = Op(filter);
SmallVector<NestedMatch, 8> matches;
pat.match(f, &matches);
for (auto m : matches) {
auto *opInst = m.getMatchedOperation();
// This is a unit test that only checks and prints shape ratio.
// As a consequence we write only Ops with a single return type for the
// purpose of this test. If we need to test more intricate behavior in the
// future we can always extend.
auto superVectorType = cast<VectorType>(opInst->getResult(0).getType());
auto ratio =
computeShapeRatio(superVectorType.getShape(), subVectorType.getShape());
if (!ratio) {
opInst->emitRemark("NOT MATCHED");
} else {
outs << "\nmatched: " << *opInst << " with shape ratio: ";
llvm::interleaveComma(MutableArrayRef<int64_t>(*ratio), outs);
}
}
}
static NestedPattern patternTestSlicingOps() {
using affine::matcher::Op;
// Match all operations with the kTestSlicingOpName name.
auto filter = [](Operation &op) {
// Just use a custom op name for this test, it makes life easier.
return op.getName().getStringRef() == "slicing-test-op";
};
return Op(filter);
}
void VectorizerTestPass::testBackwardSlicing(llvm::raw_ostream &outs) {
auto f = getOperation();
outs << "\n" << f.getName();
SmallVector<NestedMatch, 8> matches;
patternTestSlicingOps().match(f, &matches);
for (auto m : matches) {
SetVector<Operation *> backwardSlice;
getBackwardSlice(m.getMatchedOperation(), &backwardSlice);
outs << "\nmatched: " << *m.getMatchedOperation()
<< " backward static slice: ";
for (auto *op : backwardSlice)
outs << "\n" << *op;
}
}
void VectorizerTestPass::testForwardSlicing(llvm::raw_ostream &outs) {
auto f = getOperation();
outs << "\n" << f.getName();
SmallVector<NestedMatch, 8> matches;
patternTestSlicingOps().match(f, &matches);
for (auto m : matches) {
SetVector<Operation *> forwardSlice;
getForwardSlice(m.getMatchedOperation(), &forwardSlice);
outs << "\nmatched: " << *m.getMatchedOperation()
<< " forward static slice: ";
for (auto *op : forwardSlice)
outs << "\n" << *op;
}
}
void VectorizerTestPass::testSlicing(llvm::raw_ostream &outs) {
auto f = getOperation();
outs << "\n" << f.getName();
SmallVector<NestedMatch, 8> matches;
patternTestSlicingOps().match(f, &matches);
for (auto m : matches) {
SetVector<Operation *> staticSlice = getSlice(m.getMatchedOperation());
outs << "\nmatched: " << *m.getMatchedOperation() << " static slice: ";
for (auto *op : staticSlice)
outs << "\n" << *op;
}
}
static bool customOpWithAffineMapAttribute(Operation &op) {
return op.getName().getStringRef() ==
VectorizerTestPass::kTestAffineMapOpName;
}
void VectorizerTestPass::testComposeMaps(llvm::raw_ostream &outs) {
auto f = getOperation();
using affine::matcher::Op;
auto pattern = Op(customOpWithAffineMapAttribute);
SmallVector<NestedMatch, 8> matches;
pattern.match(f, &matches);
SmallVector<AffineMap, 4> maps;
maps.reserve(matches.size());
for (auto m : llvm::reverse(matches)) {
auto *opInst = m.getMatchedOperation();
auto map = cast<AffineMapAttr>(
opInst->getAttr(VectorizerTestPass::kTestAffineMapAttrName))
.getValue();
maps.push_back(map);
}
if (maps.empty())
// Nothing to compose
return;
AffineMap res;
for (auto m : maps) {
res = res ? res.compose(m) : m;
}
simplifyAffineMap(res).print(outs << "\nComposed map: ");
}
/// Test for 'vectorizeAffineLoopNest' utility.
void VectorizerTestPass::testVecAffineLoopNest(llvm::raw_ostream &outs) {
std::vector<SmallVector<AffineForOp, 2>> loops;
gatherLoops(getOperation(), loops);
// Expected only one loop nest.
if (loops.empty() || loops[0].size() != 1)
return;
// We vectorize the outermost loop found with VF=4.
AffineForOp outermostLoop = loops[0][0];
VectorizationStrategy strategy;
strategy.vectorSizes.push_back(4 /*vectorization factor*/);
strategy.loopToVectorDim[outermostLoop] = 0;
ReductionLoopMap reductionLoops;
SmallVector<LoopReduction, 2> reductions;
if (!isLoopParallel(outermostLoop, &reductions)) {
outs << "Outermost loop cannot be parallel\n";
return;
}
std::vector<SmallVector<AffineForOp, 2>> loopsToVectorize;
loopsToVectorize.push_back({outermostLoop});
(void)vectorizeAffineLoopNest(loopsToVectorize, strategy);
}
void VectorizerTestPass::runOnOperation() {
// Only support single block functions at this point.
func::FuncOp f = getOperation();
if (!llvm::hasSingleElement(f))
return;
std::string str;
llvm::raw_string_ostream outs(str);
{ // Tests that expect a NestedPatternContext to be allocated externally.
NestedPatternContext mlContext;
if (!clTestVectorShapeRatio.empty())
testVectorShapeRatio(outs);
if (clTestForwardSlicingAnalysis)
testForwardSlicing(outs);
if (clTestBackwardSlicingAnalysis)
testBackwardSlicing(outs);
if (clTestSlicingAnalysis)
testSlicing(outs);
if (clTestComposeMaps)
testComposeMaps(outs);
}
if (clTestVecAffineLoopNest)
testVecAffineLoopNest(outs);
if (!outs.str().empty()) {
emitRemark(UnknownLoc::get(&getContext()), outs.str());
}
}
namespace mlir {
void registerVectorizerTestPass() { PassRegistration<VectorizerTestPass>(); }
} // namespace mlir