1 //===- TestMemRefDependenceCheck.cpp - Test dep analysis ------------------===//
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 run pair-wise memref access dependence checks.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "mlir/Analysis/AffineAnalysis.h"
14 #include "mlir/Analysis/AffineStructures.h"
15 #include "mlir/Analysis/Utils.h"
16 #include "mlir/Dialect/Affine/IR/AffineOps.h"
17 #include "mlir/IR/Builders.h"
18 #include "mlir/Pass/Pass.h"
19 #include "llvm/Support/Debug.h"
20 
21 #define DEBUG_TYPE "test-memref-dependence-check"
22 
23 using namespace mlir;
24 
25 namespace {
26 
27 // TODO: Add common surrounding loop depth-wise dependence checks.
28 /// Checks dependences between all pairs of memref accesses in a Function.
29 struct TestMemRefDependenceCheck
30     : public PassWrapper<TestMemRefDependenceCheck, FunctionPass> {
31   StringRef getArgument() const final { return "test-memref-dependence-check"; }
32   StringRef getDescription() const final {
33     return "Checks dependences between all pairs of memref accesses.";
34   }
35   SmallVector<Operation *, 4> loadsAndStores;
36   void runOnFunction() override;
37 };
38 
39 } // end anonymous namespace
40 
41 // Returns a result string which represents the direction vector (if there was
42 // a dependence), returns the string "false" otherwise.
43 static std::string
44 getDirectionVectorStr(bool ret, unsigned numCommonLoops, unsigned loopNestDepth,
45                       ArrayRef<DependenceComponent> dependenceComponents) {
46   if (!ret)
47     return "false";
48   if (dependenceComponents.empty() || loopNestDepth > numCommonLoops)
49     return "true";
50   std::string result;
51   for (unsigned i = 0, e = dependenceComponents.size(); i < e; ++i) {
52     std::string lbStr = "-inf";
53     if (dependenceComponents[i].lb.hasValue() &&
54         dependenceComponents[i].lb.getValue() !=
55             std::numeric_limits<int64_t>::min())
56       lbStr = std::to_string(dependenceComponents[i].lb.getValue());
57 
58     std::string ubStr = "+inf";
59     if (dependenceComponents[i].ub.hasValue() &&
60         dependenceComponents[i].ub.getValue() !=
61             std::numeric_limits<int64_t>::max())
62       ubStr = std::to_string(dependenceComponents[i].ub.getValue());
63 
64     result += "[" + lbStr + ", " + ubStr + "]";
65   }
66   return result;
67 }
68 
69 // For each access in 'loadsAndStores', runs a dependence check between this
70 // "source" access and all subsequent "destination" accesses in
71 // 'loadsAndStores'. Emits the result of the dependence check as a note with
72 // the source access.
73 static void checkDependences(ArrayRef<Operation *> loadsAndStores) {
74   for (unsigned i = 0, e = loadsAndStores.size(); i < e; ++i) {
75     auto *srcOpInst = loadsAndStores[i];
76     MemRefAccess srcAccess(srcOpInst);
77     for (unsigned j = 0; j < e; ++j) {
78       auto *dstOpInst = loadsAndStores[j];
79       MemRefAccess dstAccess(dstOpInst);
80 
81       unsigned numCommonLoops =
82           getNumCommonSurroundingLoops(*srcOpInst, *dstOpInst);
83       for (unsigned d = 1; d <= numCommonLoops + 1; ++d) {
84         FlatAffineValueConstraints dependenceConstraints;
85         SmallVector<DependenceComponent, 2> dependenceComponents;
86         DependenceResult result = checkMemrefAccessDependence(
87             srcAccess, dstAccess, d, &dependenceConstraints,
88             &dependenceComponents);
89         assert(result.value != DependenceResult::Failure);
90         bool ret = hasDependence(result);
91         // TODO: Print dependence type (i.e. RAW, etc) and print
92         // distance vectors as: ([2, 3], [0, 10]). Also, shorten distance
93         // vectors from ([1, 1], [3, 3]) to (1, 3).
94         srcOpInst->emitRemark("dependence from ")
95             << i << " to " << j << " at depth " << d << " = "
96             << getDirectionVectorStr(ret, numCommonLoops, d,
97                                      dependenceComponents);
98       }
99     }
100   }
101 }
102 
103 // Walks the Function 'f' adding load and store ops to 'loadsAndStores'.
104 // Runs pair-wise dependence checks.
105 void TestMemRefDependenceCheck::runOnFunction() {
106   // Collect the loads and stores within the function.
107   loadsAndStores.clear();
108   getFunction().walk([&](Operation *op) {
109     if (isa<AffineLoadOp, AffineStoreOp>(op))
110       loadsAndStores.push_back(op);
111   });
112 
113   checkDependences(loadsAndStores);
114 }
115 
116 namespace mlir {
117 namespace test {
118 void registerTestMemRefDependenceCheck() {
119   PassRegistration<TestMemRefDependenceCheck>();
120 }
121 } // namespace test
122 } // namespace mlir
123