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