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/Analysis/AffineAnalysis.h"
15 #include "mlir/Analysis/AffineStructures.h"
16 #include "mlir/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 
27 namespace {
28 
29 /// Checks for out of bound memref access subscripts..
30 struct TestMemRefBoundCheck
31     : public PassWrapper<TestMemRefBoundCheck, FunctionPass> {
getArgument__anon93c1f3960111::TestMemRefBoundCheck32   StringRef getArgument() const final { return "test-memref-bound-check"; }
getDescription__anon93c1f3960111::TestMemRefBoundCheck33   StringRef getDescription() const final {
34     return "Check memref access bounds in a Function";
35   }
36   void runOnFunction() override;
37 };
38 
39 } // end anonymous namespace
40 
runOnFunction()41 void TestMemRefBoundCheck::runOnFunction() {
42   getFunction().walk([](Operation *opInst) {
43     TypeSwitch<Operation *>(opInst)
44         .Case<AffineReadOpInterface, AffineWriteOpInterface>(
45             [](auto op) { (void)boundCheckLoadOrStoreOp(op); });
46 
47     // TODO: do this for DMA ops as well.
48   });
49 }
50 
51 namespace mlir {
52 namespace test {
registerMemRefBoundCheck()53 void registerMemRefBoundCheck() { PassRegistration<TestMemRefBoundCheck>(); }
54 } // namespace test
55 } // namespace mlir
56