2020-07-07 04:57:00 +08:00
|
|
|
/* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
|
|
|
|
|
|
|
|
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.
|
|
|
|
==============================================================================*/
|
|
|
|
|
|
|
|
// This file implements logic for fusing linalg ops obtained after LHLO
|
|
|
|
// lowering.
|
|
|
|
|
2020-07-29 07:12:08 +08:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "mlir-hlo/Dialect/mhlo/transforms/passes.h"
|
2020-08-26 11:30:05 +08:00
|
|
|
#include "mlir/Dialect/Affine/IR/AffineOps.h"
|
2020-07-07 04:57:00 +08:00
|
|
|
#include "mlir/Dialect/Linalg/Analysis/DependenceAnalysis.h"
|
2020-07-29 07:12:08 +08:00
|
|
|
#include "mlir/Dialect/Linalg/Transforms/Transforms.h"
|
2020-08-26 11:30:05 +08:00
|
|
|
#include "mlir/Dialect/SCF/SCF.h"
|
2020-07-29 07:12:08 +08:00
|
|
|
#include "mlir/Dialect/StandardOps/IR/Ops.h"
|
2020-10-16 19:03:33 +08:00
|
|
|
#include "mlir/Interfaces/ViewLikeInterface.h"
|
2020-07-29 07:12:08 +08:00
|
|
|
#include "mlir/Pass/Pass.h"
|
|
|
|
#include "mlir/Transforms/FoldUtils.h"
|
2020-10-27 21:55:28 +08:00
|
|
|
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
|
2020-07-07 04:57:00 +08:00
|
|
|
|
|
|
|
namespace mlir {
|
2020-07-09 01:05:32 +08:00
|
|
|
namespace lmhlo {
|
2020-07-07 04:57:00 +08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
using linalg::LinalgOp;
|
|
|
|
|
2020-07-29 07:12:08 +08:00
|
|
|
class LhloFuseLinalgPass
|
|
|
|
: public PassWrapper<LhloFuseLinalgPass, FunctionPass> {
|
2020-08-26 11:30:05 +08:00
|
|
|
void getDependentDialects(DialectRegistry& registry) const override {
|
|
|
|
registry.insert<AffineDialect, linalg::LinalgDialect, scf::SCFDialect>();
|
|
|
|
}
|
|
|
|
|
2020-07-07 04:57:00 +08:00
|
|
|
public:
|
2020-07-29 07:12:08 +08:00
|
|
|
LhloFuseLinalgPass() = default;
|
|
|
|
LhloFuseLinalgPass(const LhloFuseLinalgPass&) {}
|
|
|
|
LhloFuseLinalgPass(bool use_parallel_loops,
|
|
|
|
llvm::ArrayRef<unsigned> tile_sizes) {
|
2020-07-07 04:57:00 +08:00
|
|
|
tile_sizes_ = tile_sizes;
|
|
|
|
use_parallel_loops_.setValue(use_parallel_loops);
|
|
|
|
}
|
|
|
|
|
|
|
|
void runOnFunction() override {
|
|
|
|
auto func = getFunction();
|
|
|
|
|
|
|
|
// TODO(pifon): Remove assumption that the function has a single block.
|
|
|
|
if (!llvm::hasSingleElement(func)) {
|
|
|
|
emitError(func.getLoc(), "The function needs to have a single block.");
|
|
|
|
signalPassFailure();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The fusion in Linalg is currently possible only when the consumer op is
|
|
|
|
// tiled. In order to greedily fuse the ops, we have to start from the tiled
|
|
|
|
// root linalg ops, i.e. linalg ops that write to output buffers of the
|
|
|
|
// function or are returned in case of escaping allocations.
|
|
|
|
llvm::SmallDenseSet<Value> result_buffers;
|
|
|
|
for (auto func_arg : func.getArguments()) {
|
|
|
|
result_buffers.insert(func_arg);
|
|
|
|
}
|
|
|
|
for (auto& block : func) {
|
|
|
|
auto returnOp = mlir::dyn_cast<mlir::ReturnOp>(block.getTerminator());
|
|
|
|
if (!returnOp) continue;
|
|
|
|
for (auto operand : returnOp.getOperands()) {
|
|
|
|
result_buffers.insert(operand);
|
|
|
|
}
|
|
|
|
}
|
2020-10-16 19:03:33 +08:00
|
|
|
// Resolve aliasing operations (like casts) on the result to identify
|
|
|
|
// results. This only handles escaping results.
|
|
|
|
// TODO(herhut): Use BufferizeAliasAnalysis for this.
|
|
|
|
llvm::SmallVector<Value, 4> worklist(result_buffers.begin(),
|
|
|
|
result_buffers.end());
|
|
|
|
while (!worklist.empty()) {
|
|
|
|
Value result = worklist.pop_back_val();
|
|
|
|
auto definingOp = result.getDefiningOp();
|
|
|
|
if (!definingOp) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (auto viewLike = dyn_cast<ViewLikeOpInterface>(definingOp)) {
|
|
|
|
auto alias = viewLike.getViewSource();
|
|
|
|
if (result_buffers.insert(alias).second) {
|
|
|
|
worklist.push_back(alias);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-07 04:57:00 +08:00
|
|
|
MLIRContext* ctx = func.getContext();
|
|
|
|
OpBuilder b(func);
|
|
|
|
OperationFolder folder(ctx);
|
|
|
|
func.walk([&](linalg::GenericOp generic_op) {
|
|
|
|
SmallVector<int64_t, 2> tile_sizes(tile_sizes_.begin(),
|
|
|
|
tile_sizes_.end());
|
|
|
|
if (tile_sizes.empty()) {
|
|
|
|
tile_sizes = SmallVector<int64_t, 2>(generic_op.getNumLoops(), 1);
|
|
|
|
}
|
|
|
|
auto op = cast<LinalgOp>(generic_op.getOperation());
|
|
|
|
for (const Value result : op.getOutputBuffers()) {
|
|
|
|
if (!result_buffers.count(result)) continue;
|
|
|
|
if (tileGenericOp(op, tile_sizes, &b)) {
|
|
|
|
generic_op.erase();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
auto patterns = linalg::getLinalgTilingCanonicalizationPatterns(ctx);
|
2020-10-27 21:55:28 +08:00
|
|
|
applyPatternsAndFoldGreedily(func, std::move(patterns));
|
2020-07-07 04:57:00 +08:00
|
|
|
|
|
|
|
// Fuse producers of tiled linalg ops.
|
|
|
|
llvm::SmallDenseSet<Operation*> erase_set;
|
|
|
|
SmallVector<Operation*, 8> linalg_ops;
|
|
|
|
func.walk([&](LinalgOp op) { linalg_ops.push_back(op); });
|
|
|
|
for (auto* op : llvm::reverse(linalg_ops)) {
|
|
|
|
for (unsigned id = 0, e = LinalgOp(op).getNumInputs(); id < e; ++id) {
|
|
|
|
linalg::Aliases aliases;
|
|
|
|
linalg::LinalgDependenceGraph graph(aliases, linalg_ops);
|
2020-10-27 06:10:29 +08:00
|
|
|
if (auto info = fuseProducerOfBuffer(b, op, id, graph, &folder)) {
|
2020-07-07 04:57:00 +08:00
|
|
|
auto originalOp = info->originalProducer.getOperation();
|
|
|
|
erase_set.insert(originalOp);
|
|
|
|
auto originalOpInLinalgOpsVector = std::find_if(
|
|
|
|
linalg_ops.begin(), linalg_ops.end(),
|
|
|
|
[&](const Operation* op) { return op == originalOp; });
|
|
|
|
*originalOpInLinalgOpsVector = info->fusedProducer.getOperation();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto patterns = linalg::getLinalgTilingCanonicalizationPatterns(ctx);
|
2020-10-27 21:55:28 +08:00
|
|
|
applyPatternsAndFoldGreedily(func, std::move(patterns));
|
2020-07-07 04:57:00 +08:00
|
|
|
}
|
|
|
|
for (auto* e : erase_set) e->erase();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool tileGenericOp(LinalgOp op, ArrayRef<int64_t> tile_sizes, OpBuilder* b) {
|
|
|
|
auto loopType = use_parallel_loops_
|
|
|
|
? linalg::LinalgTilingLoopType::ParallelLoops
|
|
|
|
: linalg::LinalgTilingLoopType::Loops;
|
|
|
|
auto tiled_generic_op = linalg::tileLinalgOp(*b, op,
|
|
|
|
linalg::LinalgTilingOptions()
|
|
|
|
.setTileSizes(tile_sizes)
|
|
|
|
.setLoopType(loopType));
|
|
|
|
return tiled_generic_op.hasValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
Option<bool> use_parallel_loops_{
|
|
|
|
*this, "use-parallel-loops",
|
|
|
|
llvm::cl::desc(
|
|
|
|
"Tiles GenericOp consumer to parallel loops before linalg fusion"),
|
|
|
|
llvm::cl::init(false)};
|
|
|
|
|
|
|
|
ListOption<unsigned> tile_sizes_{
|
|
|
|
*this, "tile-sizes",
|
|
|
|
llvm::cl::desc(
|
|
|
|
"Tile sizes by which to tile linalg generic before linalg fusion"),
|
|
|
|
llvm::cl::ZeroOrMore, llvm::cl::MiscFlags::CommaSeparated};
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2020-07-29 07:12:08 +08:00
|
|
|
std::unique_ptr<FunctionPass> createLhloFuseLinalgPass(
|
2020-07-07 04:57:00 +08:00
|
|
|
bool use_parallel_loops, ArrayRef<unsigned> tile_sizes) {
|
2020-07-29 07:12:08 +08:00
|
|
|
return std::make_unique<LhloFuseLinalgPass>(use_parallel_loops, tile_sizes);
|
2020-07-07 04:57:00 +08:00
|
|
|
}
|
|
|
|
|
2020-07-09 01:05:32 +08:00
|
|
|
} // namespace lmhlo
|
2020-07-07 04:57:00 +08:00
|
|
|
} // namespace mlir
|