2019-05-11 11:50:29 -07:00
|
|
|
//===- TestMemRefDependenceCheck.cpp - Test dep analysis ------------------===//
|
2018-11-02 09:55:04 -07:00
|
|
|
//
|
2020-01-26 03:58:30 +00:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
2019-12-23 09:35:36 -08:00
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-11-02 09:55:04 -07:00
|
|
|
//
|
2019-12-23 09:35:36 -08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2018-11-02 09:55:04 -07:00
|
|
|
//
|
|
|
|
|
// This file implements a pass to run pair-wise memref access dependence checks.
|
|
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
|
|
#include "mlir/Analysis/AffineAnalysis.h"
|
2019-02-22 16:51:08 -08:00
|
|
|
#include "mlir/Analysis/AffineStructures.h"
|
2018-12-05 15:14:25 -08:00
|
|
|
#include "mlir/Analysis/Utils.h"
|
2020-03-20 14:18:47 -07:00
|
|
|
#include "mlir/Dialect/Affine/IR/AffineOps.h"
|
2020-02-21 11:54:49 -08:00
|
|
|
#include "mlir/Dialect/StandardOps/IR/Ops.h"
|
2018-11-02 09:55:04 -07:00
|
|
|
#include "mlir/IR/Builders.h"
|
2019-02-19 17:17:46 -08:00
|
|
|
#include "mlir/Pass/Pass.h"
|
2018-11-02 09:55:04 -07:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
|
|
2019-05-11 11:50:29 -07:00
|
|
|
#define DEBUG_TYPE "test-memref-dependence-check"
|
2018-11-02 09:55:04 -07:00
|
|
|
|
|
|
|
|
using namespace mlir;
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
2020-07-07 01:35:23 -07:00
|
|
|
// TODO: Add common surrounding loop depth-wise dependence checks.
|
2018-12-28 08:48:09 -08:00
|
|
|
/// Checks dependences between all pairs of memref accesses in a Function.
|
2019-05-11 11:50:29 -07:00
|
|
|
struct TestMemRefDependenceCheck
|
2020-04-07 13:56:16 -07:00
|
|
|
: public PassWrapper<TestMemRefDependenceCheck, FunctionPass> {
|
2019-03-27 08:55:17 -07:00
|
|
|
SmallVector<Operation *, 4> loadsAndStores;
|
2019-02-28 14:50:42 -08:00
|
|
|
void runOnFunction() override;
|
2018-11-02 09:55:04 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
2018-11-09 09:42:24 -08:00
|
|
|
// Returns a result string which represents the direction vector (if there was
|
|
|
|
|
// a dependence), returns the string "false" otherwise.
|
2019-03-19 08:45:06 -07:00
|
|
|
static std::string
|
2018-11-09 09:42:24 -08:00
|
|
|
getDirectionVectorStr(bool ret, unsigned numCommonLoops, unsigned loopNestDepth,
|
|
|
|
|
ArrayRef<DependenceComponent> dependenceComponents) {
|
|
|
|
|
if (!ret)
|
|
|
|
|
return "false";
|
|
|
|
|
if (dependenceComponents.empty() || loopNestDepth > numCommonLoops)
|
|
|
|
|
return "true";
|
2019-03-19 08:45:06 -07:00
|
|
|
std::string result;
|
2018-11-09 09:42:24 -08:00
|
|
|
for (unsigned i = 0, e = dependenceComponents.size(); i < e; ++i) {
|
2019-03-19 08:45:06 -07:00
|
|
|
std::string lbStr = "-inf";
|
2019-02-14 13:25:46 -08:00
|
|
|
if (dependenceComponents[i].lb.hasValue() &&
|
|
|
|
|
dependenceComponents[i].lb.getValue() !=
|
|
|
|
|
std::numeric_limits<int64_t>::min())
|
|
|
|
|
lbStr = std::to_string(dependenceComponents[i].lb.getValue());
|
|
|
|
|
|
2019-03-19 08:45:06 -07:00
|
|
|
std::string ubStr = "+inf";
|
2019-02-14 13:25:46 -08:00
|
|
|
if (dependenceComponents[i].ub.hasValue() &&
|
|
|
|
|
dependenceComponents[i].ub.getValue() !=
|
|
|
|
|
std::numeric_limits<int64_t>::max())
|
|
|
|
|
ubStr = std::to_string(dependenceComponents[i].ub.getValue());
|
|
|
|
|
|
2018-11-09 09:42:24 -08:00
|
|
|
result += "[" + lbStr + ", " + ubStr + "]";
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-20 00:11:03 -07:00
|
|
|
// For each access in 'loadsAndStores', runs a dependence check between this
|
2018-11-02 09:55:04 -07:00
|
|
|
// "source" access and all subsequent "destination" accesses in
|
|
|
|
|
// 'loadsAndStores'. Emits the result of the dependence check as a note with
|
|
|
|
|
// the source access.
|
2019-03-27 08:55:17 -07:00
|
|
|
static void checkDependences(ArrayRef<Operation *> loadsAndStores) {
|
2018-11-02 09:55:04 -07:00
|
|
|
for (unsigned i = 0, e = loadsAndStores.size(); i < e; ++i) {
|
2018-12-28 16:05:35 -08:00
|
|
|
auto *srcOpInst = loadsAndStores[i];
|
2019-01-07 15:06:32 -08:00
|
|
|
MemRefAccess srcAccess(srcOpInst);
|
2018-11-09 09:42:24 -08:00
|
|
|
for (unsigned j = 0; j < e; ++j) {
|
2018-12-28 16:05:35 -08:00
|
|
|
auto *dstOpInst = loadsAndStores[j];
|
2019-01-07 15:06:32 -08:00
|
|
|
MemRefAccess dstAccess(dstOpInst);
|
2018-11-09 09:42:24 -08:00
|
|
|
|
|
|
|
|
unsigned numCommonLoops =
|
2018-12-29 19:16:55 -08:00
|
|
|
getNumCommonSurroundingLoops(*srcOpInst, *dstOpInst);
|
2018-11-09 09:42:24 -08:00
|
|
|
for (unsigned d = 1; d <= numCommonLoops + 1; ++d) {
|
2018-12-17 09:57:14 -08:00
|
|
|
FlatAffineConstraints dependenceConstraints;
|
2019-12-18 09:28:48 -08:00
|
|
|
SmallVector<DependenceComponent, 2> dependenceComponents;
|
2019-06-10 10:50:08 -07:00
|
|
|
DependenceResult result = checkMemrefAccessDependence(
|
|
|
|
|
srcAccess, dstAccess, d, &dependenceConstraints,
|
|
|
|
|
&dependenceComponents);
|
|
|
|
|
assert(result.value != DependenceResult::Failure);
|
|
|
|
|
bool ret = hasDependence(result);
|
2020-07-07 01:35:23 -07:00
|
|
|
// TODO: Print dependence type (i.e. RAW, etc) and print
|
2018-11-09 09:42:24 -08:00
|
|
|
// distance vectors as: ([2, 3], [0, 10]). Also, shorten distance
|
|
|
|
|
// vectors from ([1, 1], [3, 3]) to (1, 3).
|
2019-05-11 11:50:29 -07:00
|
|
|
srcOpInst->emitRemark("dependence from ")
|
|
|
|
|
<< i << " to " << j << " at depth " << d << " = "
|
|
|
|
|
<< getDirectionVectorStr(ret, numCommonLoops, d,
|
|
|
|
|
dependenceComponents);
|
2018-11-09 09:42:24 -08:00
|
|
|
}
|
2018-11-02 09:55:04 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-28 08:48:09 -08:00
|
|
|
// Walks the Function 'f' adding load and store ops to 'loadsAndStores'.
|
2018-11-02 09:55:04 -07:00
|
|
|
// Runs pair-wise dependence checks.
|
2019-05-11 11:50:29 -07:00
|
|
|
void TestMemRefDependenceCheck::runOnFunction() {
|
2019-02-04 16:24:44 -08:00
|
|
|
// Collect the loads and stores within the function.
|
2018-11-02 09:55:04 -07:00
|
|
|
loadsAndStores.clear();
|
2019-03-27 08:55:17 -07:00
|
|
|
getFunction().walk([&](Operation *op) {
|
2020-06-29 07:31:48 -07:00
|
|
|
if (isa<AffineLoadOp, AffineStoreOp>(op))
|
2019-03-27 08:55:17 -07:00
|
|
|
loadsAndStores.push_back(op);
|
2019-02-04 16:24:44 -08:00
|
|
|
});
|
|
|
|
|
|
2018-11-02 09:55:04 -07:00
|
|
|
checkDependences(loadsAndStores);
|
|
|
|
|
}
|
2018-11-06 18:34:18 -08:00
|
|
|
|
2020-02-12 09:03:40 +00:00
|
|
|
namespace mlir {
|
|
|
|
|
void registerTestMemRefDependenceCheck() {
|
|
|
|
|
PassRegistration<TestMemRefDependenceCheck> pass(
|
|
|
|
|
"test-memref-dependence-check",
|
|
|
|
|
"Checks dependences between all pairs of memref accesses.");
|
|
|
|
|
}
|
|
|
|
|
} // namespace mlir
|