Skip to content

[mlir][bufferization] Use a cache when checking whether ops are in mutually exclusive regions #123516

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,11 @@ class AnalysisState {

virtual void resetCache();

/// Checks whether `op0` and `op1` are inside mutually exclusive regions.
/// The logic defers to `mlir::insideMutuallyExclusiveRegions`, but the
/// result is cached.
bool insideMutuallyExclusiveRegions(Operation *op0, Operation *op1);

protected:
AnalysisState(const BufferizationOptions &options, TypeID type);

Expand All @@ -576,6 +581,11 @@ class AnalysisState {
/// Cache containing closest ancestor repetitive Region.
DenseMap<std::variant<Operation *, Block *, Region *, Value>, Region *>
enclosingRepetitiveRegionCache;

/// Cache that specifies whether the two operations are in mutually exclusive
/// regions.
DenseMap<std::pair<Operation *, Operation *>, bool>
insideMutuallyExclusiveRegionsCache;
};

/// Create an AllocTensorOp for the given shaped value (memref or tensor).
Expand Down
18 changes: 17 additions & 1 deletion mlir/lib/Dialect/Bufferization/IR/BufferizableOpInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,23 @@ Region *AnalysisState::getEnclosingRepetitiveRegion(
return region;
}

void AnalysisState::resetCache() { enclosingRepetitiveRegionCache.clear(); }
bool AnalysisState::insideMutuallyExclusiveRegions(Operation *op0,
Operation *op1) {
auto key = std::make_pair(op0, op1);
if (auto iter = insideMutuallyExclusiveRegionsCache.find(key);
iter != insideMutuallyExclusiveRegionsCache.end())
return iter->second;
bool result = ::mlir::insideMutuallyExclusiveRegions(op0, op1);
// Populate results for both orderings of the ops.
insideMutuallyExclusiveRegionsCache[key] = result;
insideMutuallyExclusiveRegionsCache[std::make_pair(op1, op0)] = result;
return result;
}

void AnalysisState::resetCache() {
enclosingRepetitiveRegionCache.clear();
insideMutuallyExclusiveRegionsCache.clear();
}

Region *bufferization::getNextEnclosingRepetitiveRegion(
Region *region, const BufferizationOptions &options) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,8 @@ hasReadAfterWriteInterference(const DenseSet<OpOperand *> &usesRead,
// Note: If ops are executed multiple times (e.g., because they are
// inside a loop), mutually exclusive regions may be executed
// multiple times.
if (insideMutuallyExclusiveRegions(readingOp, conflictingWritingOp)) {
if (state.insideMutuallyExclusiveRegions(readingOp,
conflictingWritingOp)) {
LLVM_DEBUG(llvm::dbgs() << " no conflict: read and write are in "
"mutually exclusive regions\n");
continue;
Expand Down