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/MemRef/IR/MemRef.h"
10 #include "mlir/IR/BuiltinTypes.h"
11 #include "mlir/Pass/Pass.h"
12 
13 using namespace mlir;
14 
15 namespace {
16 struct TestMemRefStrideCalculation
17     : public PassWrapper<TestMemRefStrideCalculation, FunctionPass> {
18   StringRef getArgument() const final {
19     return "test-memref-stride-calculation";
20   }
21   StringRef getDescription() const final {
22     return "Test operation constant folding";
23   }
24   void runOnFunction() override;
25 };
26 } // end anonymous namespace
27 
28 /// Traverse AllocOp and compute strides of each MemRefType independently.
29 void TestMemRefStrideCalculation::runOnFunction() {
30   llvm::outs() << "Testing: " << getFunction().getName() << "\n";
31   getFunction().walk([&](memref::AllocOp allocOp) {
32     auto memrefType = allocOp.getResult().getType().cast<MemRefType>();
33     int64_t offset;
34     SmallVector<int64_t, 4> strides;
35     if (failed(getStridesAndOffset(memrefType, strides, offset))) {
36       llvm::outs() << "MemRefType " << memrefType << " cannot be converted to "
37                    << "strided form\n";
38       return;
39     }
40     llvm::outs() << "MemRefType offset: ";
41     if (offset == MemRefType::getDynamicStrideOrOffset())
42       llvm::outs() << "?";
43     else
44       llvm::outs() << offset;
45     llvm::outs() << " strides: ";
46     llvm::interleaveComma(strides, llvm::outs(), [&](int64_t v) {
47       if (v == MemRefType::getDynamicStrideOrOffset())
48         llvm::outs() << "?";
49       else
50         llvm::outs() << v;
51     });
52     llvm::outs() << "\n";
53   });
54   llvm::outs().flush();
55 }
56 
57 namespace mlir {
58 namespace test {
59 void registerTestMemRefStrideCalculation() {
60   PassRegistration<TestMemRefStrideCalculation>();
61 }
62 } // namespace test
63 } // namespace mlir
64