diff options
author | Tung D. Le <tung@jp.ibm.com> | 2021-07-30 15:22:21 +0530 |
---|---|---|
committer | Uday Bondhugula <uday@polymagelabs.com> | 2021-07-30 15:22:46 +0530 |
commit | a2186277be1c97ea5c2da890b06cc22b82ffb1a4 (patch) | |
tree | 5df19601217128f0260cc59b1ce664ca0c676f5b /mlir/lib/Transforms/LoopFusion.cpp | |
parent | 817f942a287725e758798f5b639e7ca1ccf0e83f (diff) | |
download | llvm-a2186277be1c97ea5c2da890b06cc22b82ffb1a4.zip llvm-a2186277be1c97ea5c2da890b06cc22b82ffb1a4.tar.gz llvm-a2186277be1c97ea5c2da890b06cc22b82ffb1a4.tar.bz2 |
[mlir][affine-loop-fusion] Fix a bug that AffineIfOp prevents fusion of the other loops
The presence of AffineIfOp inside AffineFor prevents fusion of the other loops to happen. For example:
```
affine.for %i0 = 0 to 10 {
affine.store %cf7, %a[%i0] : memref<10xf32>
}
affine.for %i1 = 0 to 10 {
%v0 = affine.load %a[%i1] : memref<10xf32>
affine.store %v0, %b[%i1] : memref<10xf32>
}
affine.for %i2 = 0 to 10 {
affine.if #set(%i2) {
%v0 = affine.load %b[%i2] : memref<10xf32>
}
}
```
The first two loops were not be fused because of `affine.if` inside the last `affine.for`.
The issue seems to come from a conservative constraint that does not allow fusion if there are ops whose number of regions != 0 (affine.if is one of them).
This patch just removes such a constraint when`affine.if` is inside `affine.for`. The existing `canFuseLoops` method is able to handle `affine.if` correctly.
Reviewed By: bondhugula, vinayaka-polymage
Differential Revision: https://reviews.llvm.org/D105963
Diffstat (limited to 'mlir/lib/Transforms/LoopFusion.cpp')
-rw-r--r-- | mlir/lib/Transforms/LoopFusion.cpp | 15 |
1 files changed, 8 insertions, 7 deletions
diff --git a/mlir/lib/Transforms/LoopFusion.cpp b/mlir/lib/Transforms/LoopFusion.cpp index 49bd52d..955230d 100644 --- a/mlir/lib/Transforms/LoopFusion.cpp +++ b/mlir/lib/Transforms/LoopFusion.cpp @@ -70,19 +70,20 @@ mlir::createLoopFusionPass(unsigned fastMemorySpace, namespace { // LoopNestStateCollector walks loop nests and collects load and store -// operations, and whether or not an IfInst was encountered in the loop nest. +// operations, and whether or not a region holding op other than ForOp and IfOp +// was encountered in the loop nest. struct LoopNestStateCollector { SmallVector<AffineForOp, 4> forOps; SmallVector<Operation *, 4> loadOpInsts; SmallVector<Operation *, 4> storeOpInsts; - bool hasNonForRegion = false; + bool hasNonAffineRegionOp = false; void collect(Operation *opToWalk) { opToWalk->walk([&](Operation *op) { if (isa<AffineForOp>(op)) forOps.push_back(cast<AffineForOp>(op)); - else if (op->getNumRegions() != 0) - hasNonForRegion = true; + else if (op->getNumRegions() != 0 && !isa<AffineIfOp>(op)) + hasNonAffineRegionOp = true; else if (isa<AffineReadOpInterface>(op)) loadOpInsts.push_back(op); else if (isa<AffineWriteOpInterface>(op)) @@ -744,9 +745,9 @@ bool MemRefDependenceGraph::init(FuncOp f) { // all loads and store accesses it contains. LoopNestStateCollector collector; collector.collect(&op); - // Return false if a non 'affine.for' region was found (not currently - // supported). - if (collector.hasNonForRegion) + // Return false if a region holding op other than 'affine.for' and + // 'affine.if' was found (not currently supported). + if (collector.hasNonAffineRegionOp) return false; Node node(nextNodeId++, &op); for (auto *opInst : collector.loadOpInsts) { |