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