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/Dialect/StandardOps/IR/Ops.h"
19 #include "mlir/IR/Builders.h"
20 #include "mlir/Pass/Pass.h"
21 #include "llvm/ADT/TypeSwitch.h"
22 #include "llvm/Support/Debug.h"
23 
24 #define DEBUG_TYPE "memref-bound-check"
25 
26 using namespace mlir;
27 
28 namespace {
29 
30 /// Checks for out of bound memref access subscripts..
31 struct TestMemRefBoundCheck
32     : public PassWrapper<TestMemRefBoundCheck, FunctionPass> {
33   void runOnFunction() override;
34 };
35 
36 } // end anonymous namespace
37 
runOnFunction()38 void TestMemRefBoundCheck::runOnFunction() {
39   getFunction().walk([](Operation *opInst) {
40     TypeSwitch<Operation *>(opInst)
41         .Case<AffineReadOpInterface, AffineWriteOpInterface>(
42             [](auto op) { boundCheckLoadOrStoreOp(op); });
43 
44     // TODO: do this for DMA ops as well.
45   });
46 }
47 
48 namespace mlir {
49 namespace test {
registerMemRefBoundCheck()50 void registerMemRefBoundCheck() {
51   PassRegistration<TestMemRefBoundCheck>(
52       "test-memref-bound-check", "Check memref access bounds in a Function");
53 }
54 } // namespace test
55 } // namespace mlir
56