2018-09-28 12:17:26 -07:00
|
|
|
//===- LoopUtils.cpp ---- Misc utilities for loop transformation ----------===//
|
2018-09-07 14:47:21 -07:00
|
|
|
//
|
|
|
|
|
// Copyright 2019 The MLIR Authors.
|
|
|
|
|
//
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
|
// limitations under the License.
|
|
|
|
|
// =============================================================================
|
|
|
|
|
//
|
2018-09-28 12:17:26 -07:00
|
|
|
// This file implements miscellaneous loop transformation routines.
|
2018-09-07 14:47:21 -07:00
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
2018-09-18 10:22:03 -07:00
|
|
|
#include "mlir/Transforms/LoopUtils.h"
|
2018-09-12 10:21:23 -07:00
|
|
|
|
2019-02-01 16:42:18 -08:00
|
|
|
#include "mlir/AffineOps/AffineOps.h"
|
2019-02-15 09:32:18 -08:00
|
|
|
#include "mlir/Analysis/AffineAnalysis.h"
|
2019-02-22 16:51:08 -08:00
|
|
|
#include "mlir/Analysis/AffineStructures.h"
|
2018-09-12 10:21:23 -07:00
|
|
|
#include "mlir/Analysis/LoopAnalysis.h"
|
2018-09-18 10:22:03 -07:00
|
|
|
#include "mlir/IR/AffineExpr.h"
|
|
|
|
|
#include "mlir/IR/AffineMap.h"
|
2019-01-24 12:25:30 -08:00
|
|
|
#include "mlir/IR/BlockAndValueMapping.h"
|
2018-09-07 14:47:21 -07:00
|
|
|
#include "mlir/IR/Builders.h"
|
2019-03-26 14:45:38 -07:00
|
|
|
#include "mlir/IR/Operation.h"
|
2019-03-01 13:48:24 -08:00
|
|
|
#include "mlir/StandardOps/Ops.h"
|
2018-09-28 12:17:26 -07:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2018-10-22 13:44:31 -07:00
|
|
|
#include "llvm/Support/Debug.h"
|
Add a stripmineSink and imperfectly nested tiling primitives.
This CL adds a primitive to perform stripmining of a loop by a given factor and
sinking it under multiple target loops.
In turn this is used to implement imperfectly nested loop tiling (with interchange) by repeatedly calling the stripmineSink primitive.
The API returns the point loops and allows repeated invocations of tiling to achieve declarative, multi-level, imperfectly-nested tiling.
Note that this CL is only concerned with the mechanical aspects and does not worry about analysis and legality.
The API is demonstrated in an example which creates an EDSC block, emits the corresponding MLIR and applies imperfectly-nested tiling:
```cpp
auto block = edsc::block({
For(ArrayRef<edsc::Expr>{i, j}, {zero, zero}, {M, N}, {one, one}, {
For(k1, zero, O, one, {
C({i, j, k1}) = A({i, j, k1}) + B({i, j, k1})
}),
For(k2, zero, O, one, {
C({i, j, k2}) = A({i, j, k2}) + B({i, j, k2})
}),
}),
});
// clang-format on
emitter.emitStmts(block.getBody());
auto l_i = emitter.getAffineForOp(i), l_j = emitter.getAffineForOp(j),
l_k1 = emitter.getAffineForOp(k1), l_k2 = emitter.getAffineForOp(k2);
auto indicesL1 = mlir::tile({l_i, l_j}, {512, 1024}, {l_k1, l_k2});
auto l_ii1 = indicesL1[0][0], l_jj1 = indicesL1[1][0];
mlir::tile({l_jj1, l_ii1}, {32, 16}, l_jj1);
```
The edsc::Expr for the induction variables (i, j, k_1, k_2) provide the programmatic hooks from which tiling can be applied declaratively.
PiperOrigin-RevId: 235548228
2019-02-25 09:53:05 -08:00
|
|
|
|
2018-10-22 13:44:31 -07:00
|
|
|
#define DEBUG_TYPE "LoopUtils"
|
2018-09-07 14:47:21 -07:00
|
|
|
|
2018-09-18 10:22:03 -07:00
|
|
|
using namespace mlir;
|
|
|
|
|
|
2019-03-12 08:00:52 -07:00
|
|
|
/// Computes the cleanup loop lower bound of the loop being unrolled with
|
|
|
|
|
/// the specified unroll factor; this bound will also be upper bound of the main
|
|
|
|
|
/// part of the unrolled loop. Computes the bound as an AffineMap with its
|
|
|
|
|
/// operands or a null map when the trip count can't be expressed as an affine
|
|
|
|
|
/// expression.
|
2019-03-24 19:53:05 -07:00
|
|
|
void mlir::getCleanupLoopLowerBound(AffineForOp forOp, unsigned unrollFactor,
|
|
|
|
|
AffineMap *map,
|
2019-03-12 08:00:52 -07:00
|
|
|
SmallVectorImpl<Value *> *operands,
|
|
|
|
|
FuncBuilder *b) {
|
2019-03-25 11:13:31 -07:00
|
|
|
auto lbMap = forOp.getLowerBoundMap();
|
2018-09-18 10:22:03 -07:00
|
|
|
|
|
|
|
|
// Single result lower bound map only.
|
2019-03-12 08:00:52 -07:00
|
|
|
if (lbMap.getNumResults() != 1) {
|
|
|
|
|
*map = AffineMap();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-09-18 10:22:03 -07:00
|
|
|
|
2019-03-12 08:00:52 -07:00
|
|
|
AffineMap tripCountMap;
|
|
|
|
|
SmallVector<Value *, 4> tripCountOperands;
|
|
|
|
|
buildTripCountMapAndOperands(forOp, &tripCountMap, &tripCountOperands);
|
2018-09-18 10:22:03 -07:00
|
|
|
|
|
|
|
|
// Sometimes the trip count cannot be expressed as an affine expression.
|
2019-03-12 08:00:52 -07:00
|
|
|
if (!tripCountMap) {
|
|
|
|
|
*map = AffineMap();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-09-18 10:22:03 -07:00
|
|
|
|
2019-03-25 11:13:31 -07:00
|
|
|
unsigned step = forOp.getStep();
|
2019-03-12 08:00:52 -07:00
|
|
|
|
2019-03-25 11:13:31 -07:00
|
|
|
SmallVector<Value *, 4> lbOperands(forOp.getLowerBoundOperands());
|
|
|
|
|
auto lb = b->create<AffineApplyOp>(forOp.getLoc(), lbMap, lbOperands);
|
2019-03-12 08:00:52 -07:00
|
|
|
|
|
|
|
|
// For each upper bound expr, get the range.
|
2019-03-25 10:14:34 -07:00
|
|
|
// Eg: affine.for %i = lb to min (ub1, ub2),
|
2019-03-12 08:00:52 -07:00
|
|
|
// where tripCountExprs yield (tr1, tr2), we create affine.apply's:
|
|
|
|
|
// lb + tr1 - tr1 % ufactor, lb + tr2 - tr2 % ufactor; the results of all
|
|
|
|
|
// these affine.apply's make up the cleanup loop lower bound.
|
|
|
|
|
SmallVector<AffineExpr, 4> bumpExprs(tripCountMap.getNumResults());
|
|
|
|
|
SmallVector<Value *, 4> bumpValues(tripCountMap.getNumResults());
|
|
|
|
|
for (unsigned i = 0, e = tripCountMap.getNumResults(); i < e; i++) {
|
|
|
|
|
auto tripCountExpr = tripCountMap.getResult(i);
|
|
|
|
|
bumpExprs[i] = (tripCountExpr - tripCountExpr % unrollFactor) * step;
|
|
|
|
|
auto bumpMap =
|
|
|
|
|
b->getAffineMap(tripCountMap.getNumDims(), tripCountMap.getNumSymbols(),
|
|
|
|
|
bumpExprs[i], {});
|
|
|
|
|
bumpValues[i] =
|
2019-03-25 11:13:31 -07:00
|
|
|
b->create<AffineApplyOp>(forOp.getLoc(), bumpMap, tripCountOperands);
|
2019-03-12 08:00:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SmallVector<AffineExpr, 4> newUbExprs(tripCountMap.getNumResults());
|
|
|
|
|
for (unsigned i = 0, e = bumpExprs.size(); i < e; i++)
|
|
|
|
|
newUbExprs[i] = b->getAffineDimExpr(0) + b->getAffineDimExpr(i + 1);
|
|
|
|
|
|
|
|
|
|
operands->clear();
|
|
|
|
|
operands->push_back(lb);
|
|
|
|
|
operands->append(bumpValues.begin(), bumpValues.end());
|
|
|
|
|
*map = b->getAffineMap(1 + tripCountMap.getNumResults(), 0, newUbExprs, {});
|
|
|
|
|
// Simplify the map + operands.
|
|
|
|
|
fullyComposeAffineMapAndOperands(map, operands);
|
|
|
|
|
*map = simplifyAffineMap(*map);
|
|
|
|
|
canonicalizeMapAndOperands(map, operands);
|
|
|
|
|
// Remove any affine.apply's that became dead from the simplification above.
|
|
|
|
|
for (auto *v : bumpValues) {
|
|
|
|
|
if (v->use_empty()) {
|
2019-03-26 17:05:09 -07:00
|
|
|
v->getDefiningOp()->erase();
|
2019-03-12 08:00:52 -07:00
|
|
|
}
|
|
|
|
|
}
|
2019-03-25 11:13:31 -07:00
|
|
|
if (lb.use_empty())
|
|
|
|
|
lb.erase();
|
2018-09-18 10:22:03 -07:00
|
|
|
}
|
|
|
|
|
|
2019-02-01 16:42:18 -08:00
|
|
|
/// Promotes the loop body of a forOp to its containing block if the forOp
|
2019-03-06 17:37:14 -08:00
|
|
|
/// was known to have a single iteration.
|
2018-09-12 10:21:23 -07:00
|
|
|
// TODO(bondhugula): extend this for arbitrary affine bounds.
|
2019-03-24 19:53:05 -07:00
|
|
|
LogicalResult mlir::promoteIfSingleIteration(AffineForOp forOp) {
|
2019-02-01 16:42:18 -08:00
|
|
|
Optional<uint64_t> tripCount = getConstantTripCount(forOp);
|
2018-09-18 10:22:03 -07:00
|
|
|
if (!tripCount.hasValue() || tripCount.getValue() != 1)
|
2019-03-10 15:32:54 -07:00
|
|
|
return failure();
|
2018-09-07 14:47:21 -07:00
|
|
|
|
2018-09-18 10:22:03 -07:00
|
|
|
// TODO(mlir-team): there is no builder for a max.
|
2019-03-25 11:13:31 -07:00
|
|
|
if (forOp.getLowerBoundMap().getNumResults() != 1)
|
2019-03-10 15:32:54 -07:00
|
|
|
return failure();
|
2018-09-07 14:47:21 -07:00
|
|
|
|
|
|
|
|
// Replaces all IV uses to its single iteration value.
|
2019-03-25 11:13:31 -07:00
|
|
|
auto *iv = forOp.getInductionVar();
|
2019-03-27 14:02:02 -07:00
|
|
|
Operation *op = forOp.getOperation();
|
2019-01-26 12:40:12 -08:00
|
|
|
if (!iv->use_empty()) {
|
2019-03-25 11:13:31 -07:00
|
|
|
if (forOp.hasConstantLowerBound()) {
|
2019-03-27 14:02:02 -07:00
|
|
|
auto *mlFunc = op->getFunction();
|
2018-12-29 15:33:43 -08:00
|
|
|
FuncBuilder topBuilder(mlFunc);
|
2018-10-06 17:21:53 -07:00
|
|
|
auto constOp = topBuilder.create<ConstantIndexOp>(
|
2019-03-25 11:13:31 -07:00
|
|
|
forOp.getLoc(), forOp.getConstantLowerBound());
|
2019-01-26 12:40:12 -08:00
|
|
|
iv->replaceAllUsesWith(constOp);
|
2018-09-18 10:22:03 -07:00
|
|
|
} else {
|
2019-03-25 11:13:31 -07:00
|
|
|
AffineBound lb = forOp.getLowerBound();
|
2018-12-27 14:35:10 -08:00
|
|
|
SmallVector<Value *, 4> lbOperands(lb.operand_begin(), lb.operand_end());
|
2019-03-27 14:02:02 -07:00
|
|
|
FuncBuilder builder(op->getBlock(), Block::iterator(op));
|
2019-01-22 13:58:52 -08:00
|
|
|
if (lb.getMap() == builder.getDimIdentityMap()) {
|
2019-02-06 11:08:18 -08:00
|
|
|
// No need of generating an affine.apply.
|
2019-01-26 12:40:12 -08:00
|
|
|
iv->replaceAllUsesWith(lbOperands[0]);
|
2019-01-22 13:58:52 -08:00
|
|
|
} else {
|
|
|
|
|
auto affineApplyOp = builder.create<AffineApplyOp>(
|
2019-03-27 14:02:02 -07:00
|
|
|
op->getLoc(), lb.getMap(), lbOperands);
|
2019-01-27 09:33:19 -08:00
|
|
|
iv->replaceAllUsesWith(affineApplyOp);
|
2019-01-22 13:58:52 -08:00
|
|
|
}
|
2018-09-18 10:22:03 -07:00
|
|
|
}
|
|
|
|
|
}
|
2019-03-27 14:02:02 -07:00
|
|
|
// Move the loop body operations, except for terminator, to the loop's
|
2019-03-27 05:11:58 -07:00
|
|
|
// containing block.
|
2019-03-27 14:02:02 -07:00
|
|
|
auto *block = op->getBlock();
|
2019-03-27 05:11:58 -07:00
|
|
|
forOp.getBody()->getOperations().back().erase();
|
2019-03-27 14:02:02 -07:00
|
|
|
block->getOperations().splice(Block::iterator(op),
|
2019-03-26 17:05:09 -07:00
|
|
|
forOp.getBody()->getOperations());
|
2019-03-25 11:13:31 -07:00
|
|
|
forOp.erase();
|
2019-03-10 15:32:54 -07:00
|
|
|
return success();
|
2018-09-07 14:47:21 -07:00
|
|
|
}
|
|
|
|
|
|
2019-03-27 14:02:02 -07:00
|
|
|
/// Promotes all single iteration for op's in the Function, i.e., moves
|
2018-12-28 13:07:39 -08:00
|
|
|
/// their body into the containing Block.
|
2018-12-28 08:48:09 -08:00
|
|
|
void mlir::promoteSingleIterationLoops(Function *f) {
|
2018-09-07 14:47:21 -07:00
|
|
|
// Gathers all innermost loops through a post order pruned walk.
|
2019-04-04 11:13:02 -07:00
|
|
|
f->walk<AffineForOp>(
|
2019-03-24 19:53:05 -07:00
|
|
|
[](AffineForOp forOp) { promoteIfSingleIteration(forOp); });
|
2018-09-07 14:47:21 -07:00
|
|
|
}
|
2018-09-28 12:17:26 -07:00
|
|
|
|
2019-03-27 14:02:02 -07:00
|
|
|
/// Generates a 'affine.for' op with the specified lower and upper bounds
|
|
|
|
|
/// while generating the right IV remappings for the shifted operations. The
|
|
|
|
|
/// operation blocks that go into the loop are specified in instGroupQueue
|
2018-09-28 12:17:26 -07:00
|
|
|
/// starting from the specified offset, and in that order; the first element of
|
2019-03-27 14:02:02 -07:00
|
|
|
/// the pair specifies the shift applied to that group of operations; note
|
2018-12-28 16:05:35 -08:00
|
|
|
/// that the shift is multiplied by the loop step before being applied. Returns
|
2018-09-28 12:17:26 -07:00
|
|
|
/// nullptr if the generated loop simplifies to a single iteration one.
|
2019-03-24 19:53:05 -07:00
|
|
|
static AffineForOp
|
2018-12-10 15:17:25 -08:00
|
|
|
generateLoop(AffineMap lbMap, AffineMap ubMap,
|
2019-03-27 14:02:02 -07:00
|
|
|
const std::vector<std::pair<uint64_t, ArrayRef<Operation *>>>
|
2018-12-28 16:05:35 -08:00
|
|
|
&instGroupQueue,
|
2019-03-24 19:53:05 -07:00
|
|
|
unsigned offset, AffineForOp srcForInst, FuncBuilder *b) {
|
2019-03-25 11:13:31 -07:00
|
|
|
SmallVector<Value *, 4> lbOperands(srcForInst.getLowerBoundOperands());
|
|
|
|
|
SmallVector<Value *, 4> ubOperands(srcForInst.getUpperBoundOperands());
|
2018-09-28 12:17:26 -07:00
|
|
|
|
2018-12-10 15:17:25 -08:00
|
|
|
assert(lbMap.getNumInputs() == lbOperands.size());
|
|
|
|
|
assert(ubMap.getNumInputs() == ubOperands.size());
|
|
|
|
|
|
2019-02-01 16:42:18 -08:00
|
|
|
auto loopChunk =
|
2019-03-25 11:13:31 -07:00
|
|
|
b->create<AffineForOp>(srcForInst.getLoc(), lbOperands, lbMap, ubOperands,
|
|
|
|
|
ubMap, srcForInst.getStep());
|
|
|
|
|
auto *loopChunkIV = loopChunk.getInductionVar();
|
|
|
|
|
auto *srcIV = srcForInst.getInductionVar();
|
2018-12-10 15:17:25 -08:00
|
|
|
|
2019-01-24 12:25:30 -08:00
|
|
|
BlockAndValueMapping operandMap;
|
2018-09-28 12:17:26 -07:00
|
|
|
|
2019-03-27 05:11:58 -07:00
|
|
|
FuncBuilder bodyBuilder = loopChunk.getBodyBuilder();
|
2018-12-28 16:05:35 -08:00
|
|
|
for (auto it = instGroupQueue.begin() + offset, e = instGroupQueue.end();
|
2018-09-28 12:17:26 -07:00
|
|
|
it != e; ++it) {
|
2018-12-10 15:17:25 -08:00
|
|
|
uint64_t shift = it->first;
|
2018-12-28 16:05:35 -08:00
|
|
|
auto insts = it->second;
|
2019-03-27 14:02:02 -07:00
|
|
|
// All 'same shift' operations get added with their operands being
|
|
|
|
|
// remapped to results of cloned operations, and their IV used remapped.
|
2018-12-10 15:17:25 -08:00
|
|
|
// Generate the remapping if the shift is not zero: remappedIV = newIV -
|
|
|
|
|
// shift.
|
2019-01-26 12:40:12 -08:00
|
|
|
if (!srcIV->use_empty() && shift != 0) {
|
2019-03-27 05:11:58 -07:00
|
|
|
auto ivRemap = bodyBuilder.create<AffineApplyOp>(
|
2019-03-25 11:13:31 -07:00
|
|
|
srcForInst.getLoc(),
|
2019-03-27 05:11:58 -07:00
|
|
|
bodyBuilder.getSingleDimShiftAffineMap(
|
2019-03-25 11:13:31 -07:00
|
|
|
-static_cast<int64_t>(srcForInst.getStep() * shift)),
|
2019-01-27 09:33:19 -08:00
|
|
|
loopChunkIV);
|
2019-01-26 12:40:12 -08:00
|
|
|
operandMap.map(srcIV, ivRemap);
|
2018-09-28 12:17:26 -07:00
|
|
|
} else {
|
2019-01-26 12:40:12 -08:00
|
|
|
operandMap.map(srcIV, loopChunkIV);
|
2018-09-28 12:17:26 -07:00
|
|
|
}
|
2019-03-27 14:02:02 -07:00
|
|
|
for (auto *op : insts) {
|
|
|
|
|
if (!op->isa<AffineTerminatorOp>())
|
|
|
|
|
bodyBuilder.clone(*op, operandMap);
|
2018-09-28 12:17:26 -07:00
|
|
|
}
|
2019-03-27 05:11:58 -07:00
|
|
|
};
|
2019-03-06 17:37:14 -08:00
|
|
|
if (succeeded(promoteIfSingleIteration(loopChunk)))
|
2019-03-24 19:53:05 -07:00
|
|
|
return AffineForOp();
|
2018-09-28 12:17:26 -07:00
|
|
|
return loopChunk;
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-27 14:02:02 -07:00
|
|
|
/// Skew the operations in the body of a 'affine.for' operation with the
|
|
|
|
|
/// specified operation-wise shifts. The shifts are with respect to the
|
2019-03-25 10:14:34 -07:00
|
|
|
/// original execution order, and are multiplied by the loop 'step' before being
|
2019-03-27 14:02:02 -07:00
|
|
|
/// applied. A shift of zero for each operation will lead to no change.
|
|
|
|
|
// The skewing of operations with respect to one another can be used for
|
2018-12-28 16:05:35 -08:00
|
|
|
// example to allow overlap of asynchronous operations (such as DMA
|
2019-03-27 14:02:02 -07:00
|
|
|
// communication) with computation, or just relative shifting of operations
|
2018-12-28 16:05:35 -08:00
|
|
|
// for better register reuse, locality or parallelism. As such, the shifts are
|
2019-03-27 14:02:02 -07:00
|
|
|
// typically expected to be at most of the order of the number of operations.
|
2018-12-28 16:05:35 -08:00
|
|
|
// This method should not be used as a substitute for loop distribution/fission.
|
2019-03-27 14:02:02 -07:00
|
|
|
// This method uses an algorithm// in time linear in the number of operations
|
2018-12-28 16:05:35 -08:00
|
|
|
// in the body of the for loop - (using the 'sweep line' paradigm). This method
|
2018-09-28 12:17:26 -07:00
|
|
|
// asserts preservation of SSA dominance. A check for that as well as that for
|
|
|
|
|
// memory-based depedence preservation check rests with the users of this
|
|
|
|
|
// method.
|
2019-03-24 19:53:05 -07:00
|
|
|
LogicalResult mlir::instBodySkew(AffineForOp forOp, ArrayRef<uint64_t> shifts,
|
2019-03-08 16:04:42 -08:00
|
|
|
bool unrollPrologueEpilogue) {
|
2019-03-27 05:11:58 -07:00
|
|
|
if (forOp.getBody()->begin() == std::prev(forOp.getBody()->end()))
|
2019-03-10 15:32:54 -07:00
|
|
|
return success();
|
2018-09-28 12:17:26 -07:00
|
|
|
|
|
|
|
|
// If the trip counts aren't constant, we would need versioning and
|
|
|
|
|
// conditional guards (or context information to prevent such versioning). The
|
|
|
|
|
// better way to pipeline for such loops is to first tile them and extract
|
|
|
|
|
// constant trip count "full tiles" before applying this.
|
2019-02-01 16:42:18 -08:00
|
|
|
auto mayBeConstTripCount = getConstantTripCount(forOp);
|
2018-10-22 13:44:31 -07:00
|
|
|
if (!mayBeConstTripCount.hasValue()) {
|
2019-03-25 11:13:31 -07:00
|
|
|
LLVM_DEBUG(forOp.emitNote("non-constant trip count loop not handled"));
|
2019-03-10 15:32:54 -07:00
|
|
|
return success();
|
2018-10-22 13:44:31 -07:00
|
|
|
}
|
2018-09-28 12:17:26 -07:00
|
|
|
uint64_t tripCount = mayBeConstTripCount.getValue();
|
|
|
|
|
|
2019-02-01 16:42:18 -08:00
|
|
|
assert(isInstwiseShiftValid(forOp, shifts) &&
|
2018-10-22 13:44:31 -07:00
|
|
|
"shifts will lead to an invalid transformation\n");
|
2018-09-28 12:17:26 -07:00
|
|
|
|
2019-03-25 11:13:31 -07:00
|
|
|
int64_t step = forOp.getStep();
|
2018-12-10 15:17:25 -08:00
|
|
|
|
2019-03-26 17:05:09 -07:00
|
|
|
unsigned numChildInsts = forOp.getBody()->getOperations().size();
|
2018-09-28 12:17:26 -07:00
|
|
|
|
2018-12-10 15:17:25 -08:00
|
|
|
// Do a linear time (counting) sort for the shifts.
|
|
|
|
|
uint64_t maxShift = 0;
|
2018-12-28 16:05:35 -08:00
|
|
|
for (unsigned i = 0; i < numChildInsts; i++) {
|
2018-12-10 15:17:25 -08:00
|
|
|
maxShift = std::max(maxShift, shifts[i]);
|
2018-09-28 12:17:26 -07:00
|
|
|
}
|
2018-12-10 15:17:25 -08:00
|
|
|
// Such large shifts are not the typical use case.
|
2018-12-28 16:05:35 -08:00
|
|
|
if (maxShift >= numChildInsts) {
|
2019-03-25 11:13:31 -07:00
|
|
|
forOp.emitWarning("not shifting because shifts are unrealistically large");
|
2019-03-10 15:32:54 -07:00
|
|
|
return success();
|
2018-10-22 13:44:31 -07:00
|
|
|
}
|
2018-09-28 12:17:26 -07:00
|
|
|
|
2019-03-27 14:02:02 -07:00
|
|
|
// An array of operation groups sorted by shift amount; each group has all
|
|
|
|
|
// operations with the same shift in the order in which they appear in the
|
|
|
|
|
// body of the 'affine.for' op.
|
|
|
|
|
std::vector<std::vector<Operation *>> sortedInstGroups(maxShift + 1);
|
2018-09-28 12:17:26 -07:00
|
|
|
unsigned pos = 0;
|
2019-03-27 14:02:02 -07:00
|
|
|
for (auto &op : *forOp.getBody()) {
|
2018-12-10 15:17:25 -08:00
|
|
|
auto shift = shifts[pos++];
|
2019-03-27 14:02:02 -07:00
|
|
|
sortedInstGroups[shift].push_back(&op);
|
2018-09-28 12:17:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Unless the shifts have a specific pattern (which actually would be the
|
|
|
|
|
// common use case), prologue and epilogue are not meaningfully defined.
|
|
|
|
|
// Nevertheless, if 'unrollPrologueEpilogue' is set, we will treat the first
|
|
|
|
|
// loop generated as the prologue and the last as epilogue and unroll these
|
|
|
|
|
// fully.
|
2019-03-24 19:53:05 -07:00
|
|
|
AffineForOp prologue;
|
|
|
|
|
AffineForOp epilogue;
|
2018-09-28 12:17:26 -07:00
|
|
|
|
2018-12-10 15:17:25 -08:00
|
|
|
// Do a sweep over the sorted shifts while storing open groups in a
|
2018-09-28 12:17:26 -07:00
|
|
|
// vector, and generating loop portions as necessary during the sweep. A block
|
2019-03-27 14:02:02 -07:00
|
|
|
// of operations is paired with its shift.
|
|
|
|
|
std::vector<std::pair<uint64_t, ArrayRef<Operation *>>> instGroupQueue;
|
2018-09-28 12:17:26 -07:00
|
|
|
|
2019-03-25 11:13:31 -07:00
|
|
|
auto origLbMap = forOp.getLowerBoundMap();
|
2018-12-10 15:17:25 -08:00
|
|
|
uint64_t lbShift = 0;
|
2019-03-26 17:05:09 -07:00
|
|
|
FuncBuilder b(forOp.getOperation());
|
2018-12-28 16:05:35 -08:00
|
|
|
for (uint64_t d = 0, e = sortedInstGroups.size(); d < e; ++d) {
|
2018-12-10 15:17:25 -08:00
|
|
|
// If nothing is shifted by d, continue.
|
2018-12-28 16:05:35 -08:00
|
|
|
if (sortedInstGroups[d].empty())
|
2018-09-28 12:17:26 -07:00
|
|
|
continue;
|
2018-12-28 16:05:35 -08:00
|
|
|
if (!instGroupQueue.empty()) {
|
2018-09-28 12:17:26 -07:00
|
|
|
assert(d >= 1 &&
|
|
|
|
|
"Queue expected to be empty when the first block is found");
|
|
|
|
|
// The interval for which the loop needs to be generated here is:
|
2018-12-10 15:17:25 -08:00
|
|
|
// [lbShift, min(lbShift + tripCount, d)) and the body of the
|
2019-03-27 14:02:02 -07:00
|
|
|
// loop needs to have all operations in instQueue in that order.
|
2019-03-24 19:53:05 -07:00
|
|
|
AffineForOp res;
|
2018-12-10 15:17:25 -08:00
|
|
|
if (lbShift + tripCount * step < d * step) {
|
|
|
|
|
res = generateLoop(
|
|
|
|
|
b.getShiftedAffineMap(origLbMap, lbShift),
|
|
|
|
|
b.getShiftedAffineMap(origLbMap, lbShift + tripCount * step),
|
2019-02-01 16:42:18 -08:00
|
|
|
instGroupQueue, 0, forOp, &b);
|
2019-03-27 14:02:02 -07:00
|
|
|
// Entire loop for the queued op groups generated, empty it.
|
2018-12-28 16:05:35 -08:00
|
|
|
instGroupQueue.clear();
|
2018-12-10 15:17:25 -08:00
|
|
|
lbShift += tripCount * step;
|
2018-09-28 12:17:26 -07:00
|
|
|
} else {
|
2018-12-10 15:17:25 -08:00
|
|
|
res = generateLoop(b.getShiftedAffineMap(origLbMap, lbShift),
|
2018-12-28 16:05:35 -08:00
|
|
|
b.getShiftedAffineMap(origLbMap, d), instGroupQueue,
|
2019-02-01 16:42:18 -08:00
|
|
|
0, forOp, &b);
|
2018-12-10 15:17:25 -08:00
|
|
|
lbShift = d * step;
|
2018-09-28 12:17:26 -07:00
|
|
|
}
|
|
|
|
|
if (!prologue && res)
|
|
|
|
|
prologue = res;
|
|
|
|
|
epilogue = res;
|
|
|
|
|
} else {
|
|
|
|
|
// Start of first interval.
|
2018-12-10 15:17:25 -08:00
|
|
|
lbShift = d * step;
|
2018-09-28 12:17:26 -07:00
|
|
|
}
|
2019-03-27 14:02:02 -07:00
|
|
|
// Augment the list of operations that get into the current open interval.
|
2018-12-28 16:05:35 -08:00
|
|
|
instGroupQueue.push_back({d, sortedInstGroups[d]});
|
2018-09-28 12:17:26 -07:00
|
|
|
}
|
|
|
|
|
|
2019-03-27 14:02:02 -07:00
|
|
|
// Those operations groups left in the queue now need to be processed (FIFO)
|
2018-09-28 12:17:26 -07:00
|
|
|
// and their loops completed.
|
2018-12-28 16:05:35 -08:00
|
|
|
for (unsigned i = 0, e = instGroupQueue.size(); i < e; ++i) {
|
|
|
|
|
uint64_t ubShift = (instGroupQueue[i].first + tripCount) * step;
|
2018-12-10 15:17:25 -08:00
|
|
|
epilogue = generateLoop(b.getShiftedAffineMap(origLbMap, lbShift),
|
|
|
|
|
b.getShiftedAffineMap(origLbMap, ubShift),
|
2019-02-01 16:42:18 -08:00
|
|
|
instGroupQueue, i, forOp, &b);
|
2018-12-10 15:17:25 -08:00
|
|
|
lbShift = ubShift;
|
2018-09-28 12:17:26 -07:00
|
|
|
if (!prologue)
|
|
|
|
|
prologue = epilogue;
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-27 14:02:02 -07:00
|
|
|
// Erase the original for op.
|
2019-03-25 11:13:31 -07:00
|
|
|
forOp.erase();
|
2018-09-28 12:17:26 -07:00
|
|
|
|
|
|
|
|
if (unrollPrologueEpilogue && prologue)
|
|
|
|
|
loopUnrollFull(prologue);
|
2019-02-01 16:42:18 -08:00
|
|
|
if (unrollPrologueEpilogue && !epilogue &&
|
2019-03-26 17:05:09 -07:00
|
|
|
epilogue.getOperation() != prologue.getOperation())
|
2018-09-28 12:17:26 -07:00
|
|
|
loopUnrollFull(epilogue);
|
|
|
|
|
|
2019-03-10 15:32:54 -07:00
|
|
|
return success();
|
2018-09-28 12:17:26 -07:00
|
|
|
}
|
2018-11-14 14:15:48 -08:00
|
|
|
|
2019-04-04 15:19:17 -07:00
|
|
|
/// Get perfectly nested sequence of loops starting at root of loop nest
|
|
|
|
|
/// (the first op being another AffineFor, and the second op - a terminator).
|
|
|
|
|
/// A loop is perfectly nested iff: the first op in the loop's body is another
|
|
|
|
|
/// AffineForOp, and the second op is a terminator).
|
|
|
|
|
void mlir::getPerfectlyNestedLoops(SmallVectorImpl<AffineForOp> &nestedLoops,
|
|
|
|
|
AffineForOp root) {
|
|
|
|
|
AffineForOp curr = root;
|
|
|
|
|
nestedLoops.push_back(curr);
|
|
|
|
|
auto *currBody = curr.getBody();
|
|
|
|
|
while (currBody->begin() == std::prev(currBody->end(), 2) &&
|
|
|
|
|
(curr = curr.getBody()->front().dyn_cast<AffineForOp>())) {
|
|
|
|
|
nestedLoops.push_back(curr);
|
|
|
|
|
currBody = curr.getBody();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-14 14:15:48 -08:00
|
|
|
/// Unrolls this loop completely.
|
2019-03-24 19:53:05 -07:00
|
|
|
LogicalResult mlir::loopUnrollFull(AffineForOp forOp) {
|
2019-02-01 16:42:18 -08:00
|
|
|
Optional<uint64_t> mayBeConstantTripCount = getConstantTripCount(forOp);
|
2018-11-14 14:15:48 -08:00
|
|
|
if (mayBeConstantTripCount.hasValue()) {
|
|
|
|
|
uint64_t tripCount = mayBeConstantTripCount.getValue();
|
|
|
|
|
if (tripCount == 1) {
|
2019-02-01 16:42:18 -08:00
|
|
|
return promoteIfSingleIteration(forOp);
|
2018-11-14 14:15:48 -08:00
|
|
|
}
|
2019-02-01 16:42:18 -08:00
|
|
|
return loopUnrollByFactor(forOp, tripCount);
|
2018-11-14 14:15:48 -08:00
|
|
|
}
|
2019-03-10 15:32:54 -07:00
|
|
|
return failure();
|
2018-11-14 14:15:48 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Unrolls and jams this loop by the specified factor or by the trip count (if
|
|
|
|
|
/// constant) whichever is lower.
|
2019-03-24 19:53:05 -07:00
|
|
|
LogicalResult mlir::loopUnrollUpToFactor(AffineForOp forOp,
|
2019-03-08 16:04:42 -08:00
|
|
|
uint64_t unrollFactor) {
|
2019-02-01 16:42:18 -08:00
|
|
|
Optional<uint64_t> mayBeConstantTripCount = getConstantTripCount(forOp);
|
2018-11-14 14:15:48 -08:00
|
|
|
|
|
|
|
|
if (mayBeConstantTripCount.hasValue() &&
|
|
|
|
|
mayBeConstantTripCount.getValue() < unrollFactor)
|
2019-02-01 16:42:18 -08:00
|
|
|
return loopUnrollByFactor(forOp, mayBeConstantTripCount.getValue());
|
|
|
|
|
return loopUnrollByFactor(forOp, unrollFactor);
|
2018-11-14 14:15:48 -08:00
|
|
|
}
|
|
|
|
|
|
2019-03-06 17:37:14 -08:00
|
|
|
/// Unrolls this loop by the specified factor. Returns success if the loop
|
2018-11-14 14:15:48 -08:00
|
|
|
/// is successfully unrolled.
|
2019-03-24 19:53:05 -07:00
|
|
|
LogicalResult mlir::loopUnrollByFactor(AffineForOp forOp,
|
2019-03-08 16:04:42 -08:00
|
|
|
uint64_t unrollFactor) {
|
2018-11-14 14:15:48 -08:00
|
|
|
assert(unrollFactor >= 1 && "unroll factor should be >= 1");
|
|
|
|
|
|
2019-01-22 15:48:07 -08:00
|
|
|
if (unrollFactor == 1)
|
2019-02-01 16:42:18 -08:00
|
|
|
return promoteIfSingleIteration(forOp);
|
2019-01-22 15:48:07 -08:00
|
|
|
|
2019-03-27 05:11:58 -07:00
|
|
|
if (forOp.getBody()->empty() ||
|
|
|
|
|
forOp.getBody()->begin() == std::prev(forOp.getBody()->end()))
|
2019-03-10 15:32:54 -07:00
|
|
|
return failure();
|
2018-11-14 14:15:48 -08:00
|
|
|
|
2019-03-12 08:00:52 -07:00
|
|
|
// Loops where the lower bound is a max expression isn't supported for
|
|
|
|
|
// unrolling since the trip count can be expressed as an affine function when
|
|
|
|
|
// both the lower bound and the upper bound are multi-result maps. However,
|
|
|
|
|
// one meaningful way to do such unrolling would be to specialize the loop for
|
|
|
|
|
// the 'hotspot' case and unroll that hotspot.
|
2019-03-25 11:13:31 -07:00
|
|
|
if (forOp.getLowerBoundMap().getNumResults() != 1)
|
2019-03-10 15:32:54 -07:00
|
|
|
return failure();
|
2018-11-14 14:15:48 -08:00
|
|
|
|
|
|
|
|
// If the trip count is lower than the unroll factor, no unrolled body.
|
|
|
|
|
// TODO(bondhugula): option to specify cleanup loop unrolling.
|
2019-03-12 08:00:52 -07:00
|
|
|
Optional<uint64_t> mayBeConstantTripCount = getConstantTripCount(forOp);
|
2018-11-14 14:15:48 -08:00
|
|
|
if (mayBeConstantTripCount.hasValue() &&
|
|
|
|
|
mayBeConstantTripCount.getValue() < unrollFactor)
|
2019-03-10 15:32:54 -07:00
|
|
|
return failure();
|
2018-11-14 14:15:48 -08:00
|
|
|
|
|
|
|
|
// Generate the cleanup loop if trip count isn't a multiple of unrollFactor.
|
2019-03-27 14:02:02 -07:00
|
|
|
Operation *op = forOp.getOperation();
|
2019-02-01 16:42:18 -08:00
|
|
|
if (getLargestDivisorOfTripCount(forOp) % unrollFactor != 0) {
|
2019-03-27 14:02:02 -07:00
|
|
|
FuncBuilder builder(op->getBlock(), ++Block::iterator(op));
|
|
|
|
|
auto cleanupForInst = builder.clone(*op)->cast<AffineForOp>();
|
2019-03-12 08:00:52 -07:00
|
|
|
AffineMap cleanupMap;
|
|
|
|
|
SmallVector<Value *, 4> cleanupOperands;
|
|
|
|
|
getCleanupLoopLowerBound(forOp, unrollFactor, &cleanupMap, &cleanupOperands,
|
|
|
|
|
&builder);
|
|
|
|
|
assert(cleanupMap &&
|
|
|
|
|
"cleanup loop lower bound map for single result lower bound maps "
|
|
|
|
|
"can always be determined");
|
2019-03-25 11:13:31 -07:00
|
|
|
cleanupForInst.setLowerBound(cleanupOperands, cleanupMap);
|
2018-11-14 14:15:48 -08:00
|
|
|
// Promote the loop body up if this has turned into a single iteration loop.
|
2018-12-28 16:05:35 -08:00
|
|
|
promoteIfSingleIteration(cleanupForInst);
|
2018-11-14 14:15:48 -08:00
|
|
|
|
2019-03-12 08:00:52 -07:00
|
|
|
// Adjust upper bound of the original loop; this is the same as the lower
|
|
|
|
|
// bound of the cleanup loop.
|
2019-03-25 11:13:31 -07:00
|
|
|
forOp.setUpperBound(cleanupOperands, cleanupMap);
|
2018-11-14 14:15:48 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Scale the step of loop being unrolled by unroll factor.
|
2019-03-25 11:13:31 -07:00
|
|
|
int64_t step = forOp.getStep();
|
|
|
|
|
forOp.setStep(step * unrollFactor);
|
2018-11-14 14:15:48 -08:00
|
|
|
|
2019-03-27 05:11:58 -07:00
|
|
|
// Builder to insert unrolled bodies just before the terminator of the body of
|
|
|
|
|
// 'forOp'.
|
|
|
|
|
FuncBuilder builder = forOp.getBodyBuilder();
|
2018-11-14 14:15:48 -08:00
|
|
|
|
2019-03-27 14:02:02 -07:00
|
|
|
// Keep a pointer to the last non-terminator operation in the original block
|
2019-03-27 05:11:58 -07:00
|
|
|
// so that we know what to clone (since we are doing this in-place).
|
|
|
|
|
Block::iterator srcBlockEnd = std::prev(forOp.getBody()->end(), 2);
|
2018-11-14 14:15:48 -08:00
|
|
|
|
2019-02-01 16:42:18 -08:00
|
|
|
// Unroll the contents of 'forOp' (append unrollFactor-1 additional copies).
|
2019-03-25 11:13:31 -07:00
|
|
|
auto *forOpIV = forOp.getInductionVar();
|
2018-11-14 14:15:48 -08:00
|
|
|
for (unsigned i = 1; i < unrollFactor; i++) {
|
2019-01-24 12:25:30 -08:00
|
|
|
BlockAndValueMapping operandMap;
|
2018-11-14 14:15:48 -08:00
|
|
|
|
|
|
|
|
// If the induction variable is used, create a remapping to the value for
|
|
|
|
|
// this unrolled instance.
|
2019-02-01 16:42:18 -08:00
|
|
|
if (!forOpIV->use_empty()) {
|
2018-11-14 14:15:48 -08:00
|
|
|
// iv' = iv + 1/2/3...unrollFactor-1;
|
|
|
|
|
auto d0 = builder.getAffineDimExpr(0);
|
|
|
|
|
auto bumpMap = builder.getAffineMap(1, 0, {d0 + i * step}, {});
|
2019-01-27 09:33:19 -08:00
|
|
|
auto ivUnroll =
|
2019-03-25 11:13:31 -07:00
|
|
|
builder.create<AffineApplyOp>(forOp.getLoc(), bumpMap, forOpIV);
|
2019-02-01 16:42:18 -08:00
|
|
|
operandMap.map(forOpIV, ivUnroll);
|
2018-11-14 14:15:48 -08:00
|
|
|
}
|
|
|
|
|
|
2019-02-01 16:42:18 -08:00
|
|
|
// Clone the original body of 'forOp'.
|
2019-03-25 11:13:31 -07:00
|
|
|
for (auto it = forOp.getBody()->begin(); it != std::next(srcBlockEnd);
|
2018-12-23 08:17:48 -08:00
|
|
|
it++) {
|
2018-11-14 14:15:48 -08:00
|
|
|
builder.clone(*it, operandMap);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Promote the loop body up if this has turned into a single iteration loop.
|
2019-02-01 16:42:18 -08:00
|
|
|
promoteIfSingleIteration(forOp);
|
2019-03-10 15:32:54 -07:00
|
|
|
return success();
|
2018-11-14 14:15:48 -08:00
|
|
|
}
|
2019-02-15 09:32:18 -08:00
|
|
|
|
|
|
|
|
/// Performs loop interchange on 'forOpA' and 'forOpB', where 'forOpB' is
|
2019-03-27 05:11:58 -07:00
|
|
|
/// nested within 'forOpA' as the only non-terminator operation in its block.
|
2019-03-24 19:53:05 -07:00
|
|
|
void mlir::interchangeLoops(AffineForOp forOpA, AffineForOp forOpB) {
|
2019-03-26 17:05:09 -07:00
|
|
|
auto *forOpAInst = forOpA.getOperation();
|
2019-03-27 05:11:58 -07:00
|
|
|
|
2019-03-26 17:05:09 -07:00
|
|
|
assert(&*forOpA.getBody()->begin() == forOpB.getOperation());
|
2019-03-27 05:11:58 -07:00
|
|
|
auto &forOpABody = forOpA.getBody()->getOperations();
|
|
|
|
|
auto &forOpBBody = forOpB.getBody()->getOperations();
|
|
|
|
|
|
|
|
|
|
// 1) Splice forOpA's non-terminator operations (which is just forOpB) just
|
|
|
|
|
// before forOpA (in ForOpA's parent's block) this should leave 'forOpA's
|
|
|
|
|
// body containing only the terminator.
|
|
|
|
|
forOpAInst->getBlock()->getOperations().splice(Block::iterator(forOpAInst),
|
|
|
|
|
forOpABody, forOpABody.begin(),
|
|
|
|
|
std::prev(forOpABody.end()));
|
|
|
|
|
// 2) Splice forOpB's non-terminator operations into the beginning of forOpA's
|
|
|
|
|
// body (this leaves forOpB's body containing only the terminator).
|
|
|
|
|
forOpABody.splice(forOpABody.begin(), forOpBBody, forOpBBody.begin(),
|
|
|
|
|
std::prev(forOpBBody.end()));
|
|
|
|
|
// 3) Splice forOpA into the beginning of forOpB's body.
|
|
|
|
|
forOpBBody.splice(forOpBBody.begin(), forOpAInst->getBlock()->getOperations(),
|
|
|
|
|
Block::iterator(forOpAInst));
|
2019-02-15 09:32:18 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Performs a series of loop interchanges to sink 'forOp' 'loopDepth' levels
|
|
|
|
|
/// deeper in the loop nest.
|
2019-03-24 19:53:05 -07:00
|
|
|
void mlir::sinkLoop(AffineForOp forOp, unsigned loopDepth) {
|
2019-02-15 09:32:18 -08:00
|
|
|
for (unsigned i = 0; i < loopDepth; ++i) {
|
2019-03-25 11:13:31 -07:00
|
|
|
assert(forOp.getBody()->front().isa<AffineForOp>());
|
|
|
|
|
AffineForOp nextForOp = forOp.getBody()->front().cast<AffineForOp>();
|
2019-02-15 09:32:18 -08:00
|
|
|
interchangeLoops(forOp, nextForOp);
|
|
|
|
|
}
|
|
|
|
|
}
|
Add a stripmineSink and imperfectly nested tiling primitives.
This CL adds a primitive to perform stripmining of a loop by a given factor and
sinking it under multiple target loops.
In turn this is used to implement imperfectly nested loop tiling (with interchange) by repeatedly calling the stripmineSink primitive.
The API returns the point loops and allows repeated invocations of tiling to achieve declarative, multi-level, imperfectly-nested tiling.
Note that this CL is only concerned with the mechanical aspects and does not worry about analysis and legality.
The API is demonstrated in an example which creates an EDSC block, emits the corresponding MLIR and applies imperfectly-nested tiling:
```cpp
auto block = edsc::block({
For(ArrayRef<edsc::Expr>{i, j}, {zero, zero}, {M, N}, {one, one}, {
For(k1, zero, O, one, {
C({i, j, k1}) = A({i, j, k1}) + B({i, j, k1})
}),
For(k2, zero, O, one, {
C({i, j, k2}) = A({i, j, k2}) + B({i, j, k2})
}),
}),
});
// clang-format on
emitter.emitStmts(block.getBody());
auto l_i = emitter.getAffineForOp(i), l_j = emitter.getAffineForOp(j),
l_k1 = emitter.getAffineForOp(k1), l_k2 = emitter.getAffineForOp(k2);
auto indicesL1 = mlir::tile({l_i, l_j}, {512, 1024}, {l_k1, l_k2});
auto l_ii1 = indicesL1[0][0], l_jj1 = indicesL1[1][0];
mlir::tile({l_jj1, l_ii1}, {32, 16}, l_jj1);
```
The edsc::Expr for the induction variables (i, j, k_1, k_2) provide the programmatic hooks from which tiling can be applied declaratively.
PiperOrigin-RevId: 235548228
2019-02-25 09:53:05 -08:00
|
|
|
|
2019-03-20 10:23:04 -07:00
|
|
|
// Factors out common behavior to add a new `iv` (resp. `iv` + `offset`) to the
|
2019-03-21 07:32:51 -07:00
|
|
|
// lower (resp. upper) loop bound. When called for both the lower and upper
|
|
|
|
|
// bounds, the resulting IR resembles:
|
2019-03-20 10:23:04 -07:00
|
|
|
//
|
|
|
|
|
// ```mlir
|
2019-03-25 10:14:34 -07:00
|
|
|
// affine.for %i = max (`iv, ...) to min (`iv` + `offset`) {
|
2019-03-21 07:32:51 -07:00
|
|
|
// ...
|
|
|
|
|
// }
|
|
|
|
|
// ```
|
Add a stripmineSink and imperfectly nested tiling primitives.
This CL adds a primitive to perform stripmining of a loop by a given factor and
sinking it under multiple target loops.
In turn this is used to implement imperfectly nested loop tiling (with interchange) by repeatedly calling the stripmineSink primitive.
The API returns the point loops and allows repeated invocations of tiling to achieve declarative, multi-level, imperfectly-nested tiling.
Note that this CL is only concerned with the mechanical aspects and does not worry about analysis and legality.
The API is demonstrated in an example which creates an EDSC block, emits the corresponding MLIR and applies imperfectly-nested tiling:
```cpp
auto block = edsc::block({
For(ArrayRef<edsc::Expr>{i, j}, {zero, zero}, {M, N}, {one, one}, {
For(k1, zero, O, one, {
C({i, j, k1}) = A({i, j, k1}) + B({i, j, k1})
}),
For(k2, zero, O, one, {
C({i, j, k2}) = A({i, j, k2}) + B({i, j, k2})
}),
}),
});
// clang-format on
emitter.emitStmts(block.getBody());
auto l_i = emitter.getAffineForOp(i), l_j = emitter.getAffineForOp(j),
l_k1 = emitter.getAffineForOp(k1), l_k2 = emitter.getAffineForOp(k2);
auto indicesL1 = mlir::tile({l_i, l_j}, {512, 1024}, {l_k1, l_k2});
auto l_ii1 = indicesL1[0][0], l_jj1 = indicesL1[1][0];
mlir::tile({l_jj1, l_ii1}, {32, 16}, l_jj1);
```
The edsc::Expr for the induction variables (i, j, k_1, k_2) provide the programmatic hooks from which tiling can be applied declaratively.
PiperOrigin-RevId: 235548228
2019-02-25 09:53:05 -08:00
|
|
|
static void augmentMapAndBounds(FuncBuilder *b, Value *iv, AffineMap *map,
|
|
|
|
|
SmallVector<Value *, 4> *operands,
|
|
|
|
|
int64_t offset = 0) {
|
|
|
|
|
auto bounds = llvm::to_vector<4>(map->getResults());
|
2019-03-20 10:23:04 -07:00
|
|
|
bounds.push_back(b->getAffineDimExpr(map->getNumDims()) + offset);
|
|
|
|
|
operands->insert(operands->begin() + map->getNumDims(), iv);
|
|
|
|
|
*map =
|
|
|
|
|
b->getAffineMap(map->getNumDims() + 1, map->getNumSymbols(), bounds, {});
|
Add a stripmineSink and imperfectly nested tiling primitives.
This CL adds a primitive to perform stripmining of a loop by a given factor and
sinking it under multiple target loops.
In turn this is used to implement imperfectly nested loop tiling (with interchange) by repeatedly calling the stripmineSink primitive.
The API returns the point loops and allows repeated invocations of tiling to achieve declarative, multi-level, imperfectly-nested tiling.
Note that this CL is only concerned with the mechanical aspects and does not worry about analysis and legality.
The API is demonstrated in an example which creates an EDSC block, emits the corresponding MLIR and applies imperfectly-nested tiling:
```cpp
auto block = edsc::block({
For(ArrayRef<edsc::Expr>{i, j}, {zero, zero}, {M, N}, {one, one}, {
For(k1, zero, O, one, {
C({i, j, k1}) = A({i, j, k1}) + B({i, j, k1})
}),
For(k2, zero, O, one, {
C({i, j, k2}) = A({i, j, k2}) + B({i, j, k2})
}),
}),
});
// clang-format on
emitter.emitStmts(block.getBody());
auto l_i = emitter.getAffineForOp(i), l_j = emitter.getAffineForOp(j),
l_k1 = emitter.getAffineForOp(k1), l_k2 = emitter.getAffineForOp(k2);
auto indicesL1 = mlir::tile({l_i, l_j}, {512, 1024}, {l_k1, l_k2});
auto l_ii1 = indicesL1[0][0], l_jj1 = indicesL1[1][0];
mlir::tile({l_jj1, l_ii1}, {32, 16}, l_jj1);
```
The edsc::Expr for the induction variables (i, j, k_1, k_2) provide the programmatic hooks from which tiling can be applied declaratively.
PiperOrigin-RevId: 235548228
2019-02-25 09:53:05 -08:00
|
|
|
canonicalizeMapAndOperands(map, operands);
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-20 10:23:04 -07:00
|
|
|
// Clone the original body of `forOp` into the body of `newForOp` while
|
|
|
|
|
// substituting `oldIv` in place of
|
2019-03-27 05:11:58 -07:00
|
|
|
// `forOp.getInductionVariable()` and ignoring the terminator.
|
2019-03-20 10:23:04 -07:00
|
|
|
// Note: `newForOp` may be nested under `forOp`.
|
2019-03-24 19:53:05 -07:00
|
|
|
static void cloneLoopBodyInto(AffineForOp forOp, Value *oldIv,
|
|
|
|
|
AffineForOp newForOp) {
|
2019-03-20 10:23:04 -07:00
|
|
|
BlockAndValueMapping map;
|
2019-03-25 11:13:31 -07:00
|
|
|
map.map(oldIv, newForOp.getInductionVar());
|
2019-03-27 05:11:58 -07:00
|
|
|
FuncBuilder b = newForOp.getBodyBuilder();
|
2019-03-27 14:02:02 -07:00
|
|
|
for (auto &op : *forOp.getBody()) {
|
2019-03-20 10:23:04 -07:00
|
|
|
// Step over newForOp in case it is nested under forOp.
|
2019-03-27 14:02:02 -07:00
|
|
|
if (&op == newForOp.getOperation()) {
|
2019-03-27 05:11:58 -07:00
|
|
|
continue;
|
|
|
|
|
}
|
2019-03-27 14:02:02 -07:00
|
|
|
if (op.isa<AffineTerminatorOp>()) {
|
2019-03-20 10:23:04 -07:00
|
|
|
continue;
|
|
|
|
|
}
|
2019-03-27 14:02:02 -07:00
|
|
|
auto *instClone = b.clone(op, map);
|
2019-03-20 10:23:04 -07:00
|
|
|
unsigned idx = 0;
|
2019-03-27 14:02:02 -07:00
|
|
|
for (auto r : op.getResults()) {
|
2019-03-20 10:23:04 -07:00
|
|
|
// Since we do a forward pass over the body, we iteratively augment
|
|
|
|
|
// the `map` with everything we clone.
|
2019-03-27 05:11:58 -07:00
|
|
|
map.map(r, instClone->getResult(idx++));
|
2019-03-20 10:23:04 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
Add a stripmineSink and imperfectly nested tiling primitives.
This CL adds a primitive to perform stripmining of a loop by a given factor and
sinking it under multiple target loops.
In turn this is used to implement imperfectly nested loop tiling (with interchange) by repeatedly calling the stripmineSink primitive.
The API returns the point loops and allows repeated invocations of tiling to achieve declarative, multi-level, imperfectly-nested tiling.
Note that this CL is only concerned with the mechanical aspects and does not worry about analysis and legality.
The API is demonstrated in an example which creates an EDSC block, emits the corresponding MLIR and applies imperfectly-nested tiling:
```cpp
auto block = edsc::block({
For(ArrayRef<edsc::Expr>{i, j}, {zero, zero}, {M, N}, {one, one}, {
For(k1, zero, O, one, {
C({i, j, k1}) = A({i, j, k1}) + B({i, j, k1})
}),
For(k2, zero, O, one, {
C({i, j, k2}) = A({i, j, k2}) + B({i, j, k2})
}),
}),
});
// clang-format on
emitter.emitStmts(block.getBody());
auto l_i = emitter.getAffineForOp(i), l_j = emitter.getAffineForOp(j),
l_k1 = emitter.getAffineForOp(k1), l_k2 = emitter.getAffineForOp(k2);
auto indicesL1 = mlir::tile({l_i, l_j}, {512, 1024}, {l_k1, l_k2});
auto l_ii1 = indicesL1[0][0], l_jj1 = indicesL1[1][0];
mlir::tile({l_jj1, l_ii1}, {32, 16}, l_jj1);
```
The edsc::Expr for the induction variables (i, j, k_1, k_2) provide the programmatic hooks from which tiling can be applied declaratively.
PiperOrigin-RevId: 235548228
2019-02-25 09:53:05 -08:00
|
|
|
// Stripmines `forOp` by `factor` and sinks it under each of the `targets`.
|
|
|
|
|
// Stripmine-sink is a primitive building block for generalized tiling of
|
|
|
|
|
// imperfectly nested loops.
|
|
|
|
|
// This transformation is purely mechanical and does not check legality,
|
|
|
|
|
// profitability or even structural correctness. It is the user's
|
|
|
|
|
// responsibility to specify `targets` that are dominated by `forOp`.
|
|
|
|
|
// Returns the new AffineForOps, one per `targets`, nested immediately under
|
|
|
|
|
// each of the `targets`.
|
2019-03-24 19:53:05 -07:00
|
|
|
static SmallVector<AffineForOp, 8>
|
|
|
|
|
stripmineSink(AffineForOp forOp, uint64_t factor,
|
|
|
|
|
ArrayRef<AffineForOp> targets) {
|
Add a stripmineSink and imperfectly nested tiling primitives.
This CL adds a primitive to perform stripmining of a loop by a given factor and
sinking it under multiple target loops.
In turn this is used to implement imperfectly nested loop tiling (with interchange) by repeatedly calling the stripmineSink primitive.
The API returns the point loops and allows repeated invocations of tiling to achieve declarative, multi-level, imperfectly-nested tiling.
Note that this CL is only concerned with the mechanical aspects and does not worry about analysis and legality.
The API is demonstrated in an example which creates an EDSC block, emits the corresponding MLIR and applies imperfectly-nested tiling:
```cpp
auto block = edsc::block({
For(ArrayRef<edsc::Expr>{i, j}, {zero, zero}, {M, N}, {one, one}, {
For(k1, zero, O, one, {
C({i, j, k1}) = A({i, j, k1}) + B({i, j, k1})
}),
For(k2, zero, O, one, {
C({i, j, k2}) = A({i, j, k2}) + B({i, j, k2})
}),
}),
});
// clang-format on
emitter.emitStmts(block.getBody());
auto l_i = emitter.getAffineForOp(i), l_j = emitter.getAffineForOp(j),
l_k1 = emitter.getAffineForOp(k1), l_k2 = emitter.getAffineForOp(k2);
auto indicesL1 = mlir::tile({l_i, l_j}, {512, 1024}, {l_k1, l_k2});
auto l_ii1 = indicesL1[0][0], l_jj1 = indicesL1[1][0];
mlir::tile({l_jj1, l_ii1}, {32, 16}, l_jj1);
```
The edsc::Expr for the induction variables (i, j, k_1, k_2) provide the programmatic hooks from which tiling can be applied declaratively.
PiperOrigin-RevId: 235548228
2019-02-25 09:53:05 -08:00
|
|
|
// TODO(ntv): Use cheap structural assertions that targets are nested under
|
|
|
|
|
// forOp and that targets are not nested under each other when DominanceInfo
|
|
|
|
|
// exposes the capability. It seems overkill to construct a whole function
|
|
|
|
|
// dominance tree at this point.
|
2019-03-25 11:13:31 -07:00
|
|
|
auto originalStep = forOp.getStep();
|
Add a stripmineSink and imperfectly nested tiling primitives.
This CL adds a primitive to perform stripmining of a loop by a given factor and
sinking it under multiple target loops.
In turn this is used to implement imperfectly nested loop tiling (with interchange) by repeatedly calling the stripmineSink primitive.
The API returns the point loops and allows repeated invocations of tiling to achieve declarative, multi-level, imperfectly-nested tiling.
Note that this CL is only concerned with the mechanical aspects and does not worry about analysis and legality.
The API is demonstrated in an example which creates an EDSC block, emits the corresponding MLIR and applies imperfectly-nested tiling:
```cpp
auto block = edsc::block({
For(ArrayRef<edsc::Expr>{i, j}, {zero, zero}, {M, N}, {one, one}, {
For(k1, zero, O, one, {
C({i, j, k1}) = A({i, j, k1}) + B({i, j, k1})
}),
For(k2, zero, O, one, {
C({i, j, k2}) = A({i, j, k2}) + B({i, j, k2})
}),
}),
});
// clang-format on
emitter.emitStmts(block.getBody());
auto l_i = emitter.getAffineForOp(i), l_j = emitter.getAffineForOp(j),
l_k1 = emitter.getAffineForOp(k1), l_k2 = emitter.getAffineForOp(k2);
auto indicesL1 = mlir::tile({l_i, l_j}, {512, 1024}, {l_k1, l_k2});
auto l_ii1 = indicesL1[0][0], l_jj1 = indicesL1[1][0];
mlir::tile({l_jj1, l_ii1}, {32, 16}, l_jj1);
```
The edsc::Expr for the induction variables (i, j, k_1, k_2) provide the programmatic hooks from which tiling can be applied declaratively.
PiperOrigin-RevId: 235548228
2019-02-25 09:53:05 -08:00
|
|
|
auto scaledStep = originalStep * factor;
|
2019-03-25 11:13:31 -07:00
|
|
|
forOp.setStep(scaledStep);
|
Add a stripmineSink and imperfectly nested tiling primitives.
This CL adds a primitive to perform stripmining of a loop by a given factor and
sinking it under multiple target loops.
In turn this is used to implement imperfectly nested loop tiling (with interchange) by repeatedly calling the stripmineSink primitive.
The API returns the point loops and allows repeated invocations of tiling to achieve declarative, multi-level, imperfectly-nested tiling.
Note that this CL is only concerned with the mechanical aspects and does not worry about analysis and legality.
The API is demonstrated in an example which creates an EDSC block, emits the corresponding MLIR and applies imperfectly-nested tiling:
```cpp
auto block = edsc::block({
For(ArrayRef<edsc::Expr>{i, j}, {zero, zero}, {M, N}, {one, one}, {
For(k1, zero, O, one, {
C({i, j, k1}) = A({i, j, k1}) + B({i, j, k1})
}),
For(k2, zero, O, one, {
C({i, j, k2}) = A({i, j, k2}) + B({i, j, k2})
}),
}),
});
// clang-format on
emitter.emitStmts(block.getBody());
auto l_i = emitter.getAffineForOp(i), l_j = emitter.getAffineForOp(j),
l_k1 = emitter.getAffineForOp(k1), l_k2 = emitter.getAffineForOp(k2);
auto indicesL1 = mlir::tile({l_i, l_j}, {512, 1024}, {l_k1, l_k2});
auto l_ii1 = indicesL1[0][0], l_jj1 = indicesL1[1][0];
mlir::tile({l_jj1, l_ii1}, {32, 16}, l_jj1);
```
The edsc::Expr for the induction variables (i, j, k_1, k_2) provide the programmatic hooks from which tiling can be applied declaratively.
PiperOrigin-RevId: 235548228
2019-02-25 09:53:05 -08:00
|
|
|
|
2019-03-27 14:02:02 -07:00
|
|
|
auto *op = forOp.getOperation();
|
|
|
|
|
FuncBuilder b(op->getBlock(), ++Block::iterator(op));
|
Add a stripmineSink and imperfectly nested tiling primitives.
This CL adds a primitive to perform stripmining of a loop by a given factor and
sinking it under multiple target loops.
In turn this is used to implement imperfectly nested loop tiling (with interchange) by repeatedly calling the stripmineSink primitive.
The API returns the point loops and allows repeated invocations of tiling to achieve declarative, multi-level, imperfectly-nested tiling.
Note that this CL is only concerned with the mechanical aspects and does not worry about analysis and legality.
The API is demonstrated in an example which creates an EDSC block, emits the corresponding MLIR and applies imperfectly-nested tiling:
```cpp
auto block = edsc::block({
For(ArrayRef<edsc::Expr>{i, j}, {zero, zero}, {M, N}, {one, one}, {
For(k1, zero, O, one, {
C({i, j, k1}) = A({i, j, k1}) + B({i, j, k1})
}),
For(k2, zero, O, one, {
C({i, j, k2}) = A({i, j, k2}) + B({i, j, k2})
}),
}),
});
// clang-format on
emitter.emitStmts(block.getBody());
auto l_i = emitter.getAffineForOp(i), l_j = emitter.getAffineForOp(j),
l_k1 = emitter.getAffineForOp(k1), l_k2 = emitter.getAffineForOp(k2);
auto indicesL1 = mlir::tile({l_i, l_j}, {512, 1024}, {l_k1, l_k2});
auto l_ii1 = indicesL1[0][0], l_jj1 = indicesL1[1][0];
mlir::tile({l_jj1, l_ii1}, {32, 16}, l_jj1);
```
The edsc::Expr for the induction variables (i, j, k_1, k_2) provide the programmatic hooks from which tiling can be applied declaratively.
PiperOrigin-RevId: 235548228
2019-02-25 09:53:05 -08:00
|
|
|
|
|
|
|
|
// Lower-bound map creation.
|
2019-03-25 11:13:31 -07:00
|
|
|
auto lbMap = forOp.getLowerBoundMap();
|
|
|
|
|
SmallVector<Value *, 4> lbOperands(forOp.getLowerBoundOperands());
|
|
|
|
|
augmentMapAndBounds(&b, forOp.getInductionVar(), &lbMap, &lbOperands);
|
Add a stripmineSink and imperfectly nested tiling primitives.
This CL adds a primitive to perform stripmining of a loop by a given factor and
sinking it under multiple target loops.
In turn this is used to implement imperfectly nested loop tiling (with interchange) by repeatedly calling the stripmineSink primitive.
The API returns the point loops and allows repeated invocations of tiling to achieve declarative, multi-level, imperfectly-nested tiling.
Note that this CL is only concerned with the mechanical aspects and does not worry about analysis and legality.
The API is demonstrated in an example which creates an EDSC block, emits the corresponding MLIR and applies imperfectly-nested tiling:
```cpp
auto block = edsc::block({
For(ArrayRef<edsc::Expr>{i, j}, {zero, zero}, {M, N}, {one, one}, {
For(k1, zero, O, one, {
C({i, j, k1}) = A({i, j, k1}) + B({i, j, k1})
}),
For(k2, zero, O, one, {
C({i, j, k2}) = A({i, j, k2}) + B({i, j, k2})
}),
}),
});
// clang-format on
emitter.emitStmts(block.getBody());
auto l_i = emitter.getAffineForOp(i), l_j = emitter.getAffineForOp(j),
l_k1 = emitter.getAffineForOp(k1), l_k2 = emitter.getAffineForOp(k2);
auto indicesL1 = mlir::tile({l_i, l_j}, {512, 1024}, {l_k1, l_k2});
auto l_ii1 = indicesL1[0][0], l_jj1 = indicesL1[1][0];
mlir::tile({l_jj1, l_ii1}, {32, 16}, l_jj1);
```
The edsc::Expr for the induction variables (i, j, k_1, k_2) provide the programmatic hooks from which tiling can be applied declaratively.
PiperOrigin-RevId: 235548228
2019-02-25 09:53:05 -08:00
|
|
|
|
|
|
|
|
// Upper-bound map creation.
|
2019-03-25 11:13:31 -07:00
|
|
|
auto ubMap = forOp.getUpperBoundMap();
|
|
|
|
|
SmallVector<Value *, 4> ubOperands(forOp.getUpperBoundOperands());
|
|
|
|
|
augmentMapAndBounds(&b, forOp.getInductionVar(), &ubMap, &ubOperands,
|
Add a stripmineSink and imperfectly nested tiling primitives.
This CL adds a primitive to perform stripmining of a loop by a given factor and
sinking it under multiple target loops.
In turn this is used to implement imperfectly nested loop tiling (with interchange) by repeatedly calling the stripmineSink primitive.
The API returns the point loops and allows repeated invocations of tiling to achieve declarative, multi-level, imperfectly-nested tiling.
Note that this CL is only concerned with the mechanical aspects and does not worry about analysis and legality.
The API is demonstrated in an example which creates an EDSC block, emits the corresponding MLIR and applies imperfectly-nested tiling:
```cpp
auto block = edsc::block({
For(ArrayRef<edsc::Expr>{i, j}, {zero, zero}, {M, N}, {one, one}, {
For(k1, zero, O, one, {
C({i, j, k1}) = A({i, j, k1}) + B({i, j, k1})
}),
For(k2, zero, O, one, {
C({i, j, k2}) = A({i, j, k2}) + B({i, j, k2})
}),
}),
});
// clang-format on
emitter.emitStmts(block.getBody());
auto l_i = emitter.getAffineForOp(i), l_j = emitter.getAffineForOp(j),
l_k1 = emitter.getAffineForOp(k1), l_k2 = emitter.getAffineForOp(k2);
auto indicesL1 = mlir::tile({l_i, l_j}, {512, 1024}, {l_k1, l_k2});
auto l_ii1 = indicesL1[0][0], l_jj1 = indicesL1[1][0];
mlir::tile({l_jj1, l_ii1}, {32, 16}, l_jj1);
```
The edsc::Expr for the induction variables (i, j, k_1, k_2) provide the programmatic hooks from which tiling can be applied declaratively.
PiperOrigin-RevId: 235548228
2019-02-25 09:53:05 -08:00
|
|
|
/*offset=*/scaledStep);
|
|
|
|
|
|
2019-03-24 19:53:05 -07:00
|
|
|
SmallVector<AffineForOp, 8> innerLoops;
|
Add a stripmineSink and imperfectly nested tiling primitives.
This CL adds a primitive to perform stripmining of a loop by a given factor and
sinking it under multiple target loops.
In turn this is used to implement imperfectly nested loop tiling (with interchange) by repeatedly calling the stripmineSink primitive.
The API returns the point loops and allows repeated invocations of tiling to achieve declarative, multi-level, imperfectly-nested tiling.
Note that this CL is only concerned with the mechanical aspects and does not worry about analysis and legality.
The API is demonstrated in an example which creates an EDSC block, emits the corresponding MLIR and applies imperfectly-nested tiling:
```cpp
auto block = edsc::block({
For(ArrayRef<edsc::Expr>{i, j}, {zero, zero}, {M, N}, {one, one}, {
For(k1, zero, O, one, {
C({i, j, k1}) = A({i, j, k1}) + B({i, j, k1})
}),
For(k2, zero, O, one, {
C({i, j, k2}) = A({i, j, k2}) + B({i, j, k2})
}),
}),
});
// clang-format on
emitter.emitStmts(block.getBody());
auto l_i = emitter.getAffineForOp(i), l_j = emitter.getAffineForOp(j),
l_k1 = emitter.getAffineForOp(k1), l_k2 = emitter.getAffineForOp(k2);
auto indicesL1 = mlir::tile({l_i, l_j}, {512, 1024}, {l_k1, l_k2});
auto l_ii1 = indicesL1[0][0], l_jj1 = indicesL1[1][0];
mlir::tile({l_jj1, l_ii1}, {32, 16}, l_jj1);
```
The edsc::Expr for the induction variables (i, j, k_1, k_2) provide the programmatic hooks from which tiling can be applied declaratively.
PiperOrigin-RevId: 235548228
2019-02-25 09:53:05 -08:00
|
|
|
for (auto t : targets) {
|
2019-03-27 05:11:58 -07:00
|
|
|
// Insert newForOp before the terminator of `t`.
|
|
|
|
|
FuncBuilder b = t.getBodyBuilder();
|
2019-03-25 11:13:31 -07:00
|
|
|
auto newForOp = b.create<AffineForOp>(t.getLoc(), lbOperands, lbMap,
|
2019-03-20 10:23:04 -07:00
|
|
|
ubOperands, ubMap, originalStep);
|
2019-03-25 11:13:31 -07:00
|
|
|
cloneLoopBodyInto(t, forOp.getInductionVar(), newForOp);
|
2019-03-27 14:02:02 -07:00
|
|
|
// Remove all operations from `t` except `newForOp`.
|
2019-03-26 17:05:09 -07:00
|
|
|
auto rit = ++newForOp.getOperation()->getReverseIterator();
|
2019-03-25 11:13:31 -07:00
|
|
|
auto re = t.getBody()->rend();
|
2019-03-27 14:02:02 -07:00
|
|
|
for (auto &op : llvm::make_early_inc_range(llvm::make_range(rit, re))) {
|
|
|
|
|
op.erase();
|
2019-03-20 10:23:04 -07:00
|
|
|
}
|
|
|
|
|
innerLoops.push_back(newForOp);
|
Add a stripmineSink and imperfectly nested tiling primitives.
This CL adds a primitive to perform stripmining of a loop by a given factor and
sinking it under multiple target loops.
In turn this is used to implement imperfectly nested loop tiling (with interchange) by repeatedly calling the stripmineSink primitive.
The API returns the point loops and allows repeated invocations of tiling to achieve declarative, multi-level, imperfectly-nested tiling.
Note that this CL is only concerned with the mechanical aspects and does not worry about analysis and legality.
The API is demonstrated in an example which creates an EDSC block, emits the corresponding MLIR and applies imperfectly-nested tiling:
```cpp
auto block = edsc::block({
For(ArrayRef<edsc::Expr>{i, j}, {zero, zero}, {M, N}, {one, one}, {
For(k1, zero, O, one, {
C({i, j, k1}) = A({i, j, k1}) + B({i, j, k1})
}),
For(k2, zero, O, one, {
C({i, j, k2}) = A({i, j, k2}) + B({i, j, k2})
}),
}),
});
// clang-format on
emitter.emitStmts(block.getBody());
auto l_i = emitter.getAffineForOp(i), l_j = emitter.getAffineForOp(j),
l_k1 = emitter.getAffineForOp(k1), l_k2 = emitter.getAffineForOp(k2);
auto indicesL1 = mlir::tile({l_i, l_j}, {512, 1024}, {l_k1, l_k2});
auto l_ii1 = indicesL1[0][0], l_jj1 = indicesL1[1][0];
mlir::tile({l_jj1, l_ii1}, {32, 16}, l_jj1);
```
The edsc::Expr for the induction variables (i, j, k_1, k_2) provide the programmatic hooks from which tiling can be applied declaratively.
PiperOrigin-RevId: 235548228
2019-02-25 09:53:05 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return innerLoops;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Stripmines a `forOp` by `factor` and sinks it under a single `target`.
|
|
|
|
|
// Returns the new AffineForOps, nested immediately under `target`.
|
2019-03-24 19:53:05 -07:00
|
|
|
AffineForOp stripmineSink(AffineForOp forOp, uint64_t factor,
|
|
|
|
|
AffineForOp target) {
|
|
|
|
|
auto res = stripmineSink(forOp, factor, ArrayRef<AffineForOp>{target});
|
Add a stripmineSink and imperfectly nested tiling primitives.
This CL adds a primitive to perform stripmining of a loop by a given factor and
sinking it under multiple target loops.
In turn this is used to implement imperfectly nested loop tiling (with interchange) by repeatedly calling the stripmineSink primitive.
The API returns the point loops and allows repeated invocations of tiling to achieve declarative, multi-level, imperfectly-nested tiling.
Note that this CL is only concerned with the mechanical aspects and does not worry about analysis and legality.
The API is demonstrated in an example which creates an EDSC block, emits the corresponding MLIR and applies imperfectly-nested tiling:
```cpp
auto block = edsc::block({
For(ArrayRef<edsc::Expr>{i, j}, {zero, zero}, {M, N}, {one, one}, {
For(k1, zero, O, one, {
C({i, j, k1}) = A({i, j, k1}) + B({i, j, k1})
}),
For(k2, zero, O, one, {
C({i, j, k2}) = A({i, j, k2}) + B({i, j, k2})
}),
}),
});
// clang-format on
emitter.emitStmts(block.getBody());
auto l_i = emitter.getAffineForOp(i), l_j = emitter.getAffineForOp(j),
l_k1 = emitter.getAffineForOp(k1), l_k2 = emitter.getAffineForOp(k2);
auto indicesL1 = mlir::tile({l_i, l_j}, {512, 1024}, {l_k1, l_k2});
auto l_ii1 = indicesL1[0][0], l_jj1 = indicesL1[1][0];
mlir::tile({l_jj1, l_ii1}, {32, 16}, l_jj1);
```
The edsc::Expr for the induction variables (i, j, k_1, k_2) provide the programmatic hooks from which tiling can be applied declaratively.
PiperOrigin-RevId: 235548228
2019-02-25 09:53:05 -08:00
|
|
|
assert(res.size() == 1 && "Expected 1 inner forOp");
|
|
|
|
|
return res[0];
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-24 19:53:05 -07:00
|
|
|
SmallVector<SmallVector<AffineForOp, 8>, 8>
|
|
|
|
|
mlir::tile(ArrayRef<AffineForOp> forOps, ArrayRef<uint64_t> sizes,
|
|
|
|
|
ArrayRef<AffineForOp> targets) {
|
|
|
|
|
SmallVector<SmallVector<AffineForOp, 8>, 8> res;
|
|
|
|
|
SmallVector<AffineForOp, 8> currentTargets(targets.begin(), targets.end());
|
Add a stripmineSink and imperfectly nested tiling primitives.
This CL adds a primitive to perform stripmining of a loop by a given factor and
sinking it under multiple target loops.
In turn this is used to implement imperfectly nested loop tiling (with interchange) by repeatedly calling the stripmineSink primitive.
The API returns the point loops and allows repeated invocations of tiling to achieve declarative, multi-level, imperfectly-nested tiling.
Note that this CL is only concerned with the mechanical aspects and does not worry about analysis and legality.
The API is demonstrated in an example which creates an EDSC block, emits the corresponding MLIR and applies imperfectly-nested tiling:
```cpp
auto block = edsc::block({
For(ArrayRef<edsc::Expr>{i, j}, {zero, zero}, {M, N}, {one, one}, {
For(k1, zero, O, one, {
C({i, j, k1}) = A({i, j, k1}) + B({i, j, k1})
}),
For(k2, zero, O, one, {
C({i, j, k2}) = A({i, j, k2}) + B({i, j, k2})
}),
}),
});
// clang-format on
emitter.emitStmts(block.getBody());
auto l_i = emitter.getAffineForOp(i), l_j = emitter.getAffineForOp(j),
l_k1 = emitter.getAffineForOp(k1), l_k2 = emitter.getAffineForOp(k2);
auto indicesL1 = mlir::tile({l_i, l_j}, {512, 1024}, {l_k1, l_k2});
auto l_ii1 = indicesL1[0][0], l_jj1 = indicesL1[1][0];
mlir::tile({l_jj1, l_ii1}, {32, 16}, l_jj1);
```
The edsc::Expr for the induction variables (i, j, k_1, k_2) provide the programmatic hooks from which tiling can be applied declaratively.
PiperOrigin-RevId: 235548228
2019-02-25 09:53:05 -08:00
|
|
|
for (auto it : llvm::zip(forOps, sizes)) {
|
|
|
|
|
auto step = stripmineSink(std::get<0>(it), std::get<1>(it), currentTargets);
|
|
|
|
|
res.push_back(step);
|
|
|
|
|
currentTargets = step;
|
|
|
|
|
}
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-24 19:53:05 -07:00
|
|
|
SmallVector<AffineForOp, 8> mlir::tile(ArrayRef<AffineForOp> forOps,
|
|
|
|
|
ArrayRef<uint64_t> sizes,
|
|
|
|
|
AffineForOp target) {
|
|
|
|
|
return tile(forOps, sizes, ArrayRef<AffineForOp>{target})[0];
|
Add a stripmineSink and imperfectly nested tiling primitives.
This CL adds a primitive to perform stripmining of a loop by a given factor and
sinking it under multiple target loops.
In turn this is used to implement imperfectly nested loop tiling (with interchange) by repeatedly calling the stripmineSink primitive.
The API returns the point loops and allows repeated invocations of tiling to achieve declarative, multi-level, imperfectly-nested tiling.
Note that this CL is only concerned with the mechanical aspects and does not worry about analysis and legality.
The API is demonstrated in an example which creates an EDSC block, emits the corresponding MLIR and applies imperfectly-nested tiling:
```cpp
auto block = edsc::block({
For(ArrayRef<edsc::Expr>{i, j}, {zero, zero}, {M, N}, {one, one}, {
For(k1, zero, O, one, {
C({i, j, k1}) = A({i, j, k1}) + B({i, j, k1})
}),
For(k2, zero, O, one, {
C({i, j, k2}) = A({i, j, k2}) + B({i, j, k2})
}),
}),
});
// clang-format on
emitter.emitStmts(block.getBody());
auto l_i = emitter.getAffineForOp(i), l_j = emitter.getAffineForOp(j),
l_k1 = emitter.getAffineForOp(k1), l_k2 = emitter.getAffineForOp(k2);
auto indicesL1 = mlir::tile({l_i, l_j}, {512, 1024}, {l_k1, l_k2});
auto l_ii1 = indicesL1[0][0], l_jj1 = indicesL1[1][0];
mlir::tile({l_jj1, l_ii1}, {32, 16}, l_jj1);
```
The edsc::Expr for the induction variables (i, j, k_1, k_2) provide the programmatic hooks from which tiling can be applied declaratively.
PiperOrigin-RevId: 235548228
2019-02-25 09:53:05 -08:00
|
|
|
}
|