2018-09-13 01:21:23 +08:00
|
|
|
//===- LoopUnrollAndJam.cpp - Code to perform loop unroll and jam ---------===//
|
2018-08-29 09:24:27 +08:00
|
|
|
//
|
2020-01-26 11:58:30 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
2019-12-24 01:35:36 +08:00
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-08-29 09:24:27 +08:00
|
|
|
//
|
2019-12-24 01:35:36 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2018-08-29 09:24:27 +08:00
|
|
|
//
|
2019-01-15 11:28:04 +08:00
|
|
|
// This file implements loop unroll and jam. Unroll and jam is a transformation
|
|
|
|
// that improves locality, in particular, register reuse, while also improving
|
2019-03-28 05:02:02 +08:00
|
|
|
// operation level parallelism. The example below shows what it does in nearly
|
2019-01-15 11:28:04 +08:00
|
|
|
// the general case. Loop unroll and jam currently works if the bounds of the
|
|
|
|
// loops inner to the loop being unroll-jammed do not depend on the latter.
|
2018-08-29 09:24:27 +08:00
|
|
|
//
|
2018-09-08 05:47:21 +08:00
|
|
|
// Before After unroll and jam of i by factor 2:
|
2018-08-29 09:24:27 +08:00
|
|
|
//
|
|
|
|
// for i, step = 2
|
|
|
|
// for i S1(i);
|
|
|
|
// S1; S2(i);
|
|
|
|
// S2; S1(i+1);
|
|
|
|
// for j S2(i+1);
|
|
|
|
// S3; for j
|
|
|
|
// S4; S3(i, j);
|
|
|
|
// S5; S4(i, j);
|
|
|
|
// S6; S3(i+1, j)
|
|
|
|
// S4(i+1, j)
|
|
|
|
// S5(i);
|
|
|
|
// S6(i);
|
|
|
|
// S5(i+1);
|
|
|
|
// S6(i+1);
|
|
|
|
//
|
|
|
|
// Note: 'if/else' blocks are not jammed. So, if there are loops inside if
|
2019-03-28 05:02:02 +08:00
|
|
|
// op's, bodies of those loops will not be jammed.
|
2018-08-29 09:24:27 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2020-04-08 04:58:12 +08:00
|
|
|
|
|
|
|
#include "PassDetail.h"
|
2018-09-13 01:21:23 +08:00
|
|
|
#include "mlir/Analysis/LoopAnalysis.h"
|
2020-03-21 05:18:47 +08:00
|
|
|
#include "mlir/Dialect/Affine/IR/AffineOps.h"
|
2020-03-22 19:25:36 +08:00
|
|
|
#include "mlir/Dialect/Affine/Passes.h"
|
2018-08-29 09:24:27 +08:00
|
|
|
#include "mlir/IR/AffineExpr.h"
|
2018-09-19 01:22:03 +08:00
|
|
|
#include "mlir/IR/AffineMap.h"
|
2019-01-25 04:25:30 +08:00
|
|
|
#include "mlir/IR/BlockAndValueMapping.h"
|
2018-08-29 09:24:27 +08:00
|
|
|
#include "mlir/IR/Builders.h"
|
2018-09-19 01:22:03 +08:00
|
|
|
#include "mlir/Transforms/LoopUtils.h"
|
2018-08-29 09:24:27 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
|
|
|
|
using namespace mlir;
|
|
|
|
|
2019-05-04 02:07:37 +08:00
|
|
|
#define DEBUG_TYPE "affine-loop-unroll-jam"
|
2019-01-26 14:14:04 +08:00
|
|
|
|
2018-08-29 09:24:27 +08:00
|
|
|
namespace {
|
2018-09-13 01:21:23 +08:00
|
|
|
/// Loop unroll jam pass. Currently, this just unroll jams the first
|
2018-12-29 00:48:09 +08:00
|
|
|
/// outer loop in a Function.
|
2020-04-08 04:58:12 +08:00
|
|
|
struct LoopUnrollAndJam : public AffineLoopUnrollAndJamBase<LoopUnrollAndJam> {
|
2020-04-09 03:57:02 +08:00
|
|
|
explicit LoopUnrollAndJam(Optional<unsigned> unrollJamFactor = None) {
|
|
|
|
if (unrollJamFactor)
|
|
|
|
this->unrollJamFactor = *unrollJamFactor;
|
|
|
|
}
|
2018-08-29 09:24:27 +08:00
|
|
|
|
2019-03-01 06:50:42 +08:00
|
|
|
void runOnFunction() override;
|
2018-08-29 09:24:27 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2020-04-08 04:56:16 +08:00
|
|
|
std::unique_ptr<OperationPass<FuncOp>>
|
2019-08-13 10:12:42 +08:00
|
|
|
mlir::createLoopUnrollAndJamPass(int unrollJamFactor) {
|
2019-08-18 02:05:35 +08:00
|
|
|
return std::make_unique<LoopUnrollAndJam>(
|
2018-08-29 09:24:27 +08:00
|
|
|
unrollJamFactor == -1 ? None : Optional<unsigned>(unrollJamFactor));
|
|
|
|
}
|
|
|
|
|
2019-03-01 06:50:42 +08:00
|
|
|
void LoopUnrollAndJam::runOnFunction() {
|
2018-08-29 09:24:27 +08:00
|
|
|
// Currently, just the outermost loop from the first loop nest is
|
2019-02-02 08:42:18 +08:00
|
|
|
// unroll-and-jammed by this pass. However, runOnAffineForOp can be called on
|
2019-02-28 02:59:29 +08:00
|
|
|
// any for operation.
|
2019-03-26 09:02:49 +08:00
|
|
|
auto &entryBlock = getFunction().front();
|
2019-05-12 06:56:50 +08:00
|
|
|
if (auto forOp = dyn_cast<AffineForOp>(entryBlock.front()))
|
2020-04-09 03:57:02 +08:00
|
|
|
loopUnrollJamByFactor(forOp, unrollJamFactor);
|
2018-08-29 09:24:27 +08:00
|
|
|
}
|