1 //===- TestMemRefStrideCalculation.cpp - Pass to test strides computation--===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "mlir/Dialect/StandardOps/IR/Ops.h"
10 #include "mlir/IR/BuiltinTypes.h"
11 #include "mlir/Pass/Pass.h"
12 #include "mlir/Transforms/Passes.h"
13 
14 using namespace mlir;
15 
16 namespace {
17 struct TestMemRefStrideCalculation
18     : public PassWrapper<TestMemRefStrideCalculation, FunctionPass> {
19   void runOnFunction() override;
20 };
21 } // end anonymous namespace
22 
23 /// Traverse AllocOp and compute strides of each MemRefType independently.
runOnFunction()24 void TestMemRefStrideCalculation::runOnFunction() {
25   llvm::outs() << "Testing: " << getFunction().getName() << "\n";
26   getFunction().walk([&](AllocOp allocOp) {
27     auto memrefType = allocOp.getResult().getType().cast<MemRefType>();
28     int64_t offset;
29     SmallVector<int64_t, 4> strides;
30     if (failed(getStridesAndOffset(memrefType, strides, offset))) {
31       llvm::outs() << "MemRefType " << memrefType << " cannot be converted to "
32                    << "strided form\n";
33       return;
34     }
35     llvm::outs() << "MemRefType offset: ";
36     if (offset == MemRefType::getDynamicStrideOrOffset())
37       llvm::outs() << "?";
38     else
39       llvm::outs() << offset;
40     llvm::outs() << " strides: ";
41     llvm::interleaveComma(strides, llvm::outs(), [&](int64_t v) {
42       if (v == MemRefType::getDynamicStrideOrOffset())
43         llvm::outs() << "?";
44       else
45         llvm::outs() << v;
46     });
47     llvm::outs() << "\n";
48   });
49   llvm::outs().flush();
50 }
51 
52 namespace mlir {
53 namespace test {
registerTestMemRefStrideCalculation()54 void registerTestMemRefStrideCalculation() {
55   PassRegistration<TestMemRefStrideCalculation> pass(
56       "test-memref-stride-calculation", "Test operation constant folding");
57 }
58 } // namespace test
59 } // namespace mlir
60