xref: /llvm-project/mlir/test/lib/Analysis/TestMemRefBoundCheck.cpp (revision 4c48f016effde67d500fc95290096aec9f3bdb70)
1 //===- TestMemRefBoundCheck.cpp - Test out of bound access checks ---------===//
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 // This file implements a pass to check memref accesses for out of bound
10 // accesses.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "mlir/Dialect/Affine/Analysis/AffineAnalysis.h"
15 #include "mlir/Dialect/Affine/Analysis/AffineStructures.h"
16 #include "mlir/Dialect/Affine/Analysis/Utils.h"
17 #include "mlir/Dialect/Affine/IR/AffineOps.h"
18 #include "mlir/IR/Builders.h"
19 #include "mlir/Pass/Pass.h"
20 #include "llvm/ADT/TypeSwitch.h"
21 #include "llvm/Support/Debug.h"
22 
23 #define DEBUG_TYPE "memref-bound-check"
24 
25 using namespace mlir;
26 using namespace mlir::affine;
27 
28 namespace {
29 
30 /// Checks for out of bound memref access subscripts..
31 struct TestMemRefBoundCheck
32     : public PassWrapper<TestMemRefBoundCheck, OperationPass<>> {
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID__anon8c0578d00111::TestMemRefBoundCheck33   MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestMemRefBoundCheck)
34 
35   StringRef getArgument() const final { return "test-memref-bound-check"; }
getDescription__anon8c0578d00111::TestMemRefBoundCheck36   StringRef getDescription() const final {
37     return "Check memref access bounds";
38   }
39   void runOnOperation() override;
40 };
41 
42 } // namespace
43 
runOnOperation()44 void TestMemRefBoundCheck::runOnOperation() {
45   getOperation()->walk([](Operation *opInst) {
46     TypeSwitch<Operation *>(opInst)
47         .Case<AffineReadOpInterface, AffineWriteOpInterface>(
48             [](auto op) { (void)boundCheckLoadOrStoreOp(op); });
49 
50     // TODO: do this for DMA ops as well.
51   });
52 }
53 
54 namespace mlir {
55 namespace test {
registerMemRefBoundCheck()56 void registerMemRefBoundCheck() { PassRegistration<TestMemRefBoundCheck>(); }
57 } // namespace test
58 } // namespace mlir
59