2019-12-13 13:26:00 -08:00
|
|
|
//===- Builders.cpp - MLIR Declarative Linalg Builders --------------------===//
|
|
|
|
//
|
2020-01-26 03:58:30 +00:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
2019-12-23 09:35:36 -08:00
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2019-12-13 13:26:00 -08:00
|
|
|
//
|
2019-12-23 09:35:36 -08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2019-12-13 13:26:00 -08:00
|
|
|
|
2020-02-10 11:55:02 -05:00
|
|
|
#include "mlir/IR/Builders.h"
|
|
|
|
#include "mlir/Dialect/AffineOps/EDSC/Intrinsics.h"
|
2019-12-16 13:32:02 -08:00
|
|
|
#include "mlir/Dialect/Linalg/EDSC/Intrinsics.h"
|
2020-02-10 11:55:02 -05:00
|
|
|
#include "mlir/Dialect/LoopOps/EDSC/Builders.h"
|
|
|
|
#include "mlir/Dialect/StandardOps/EDSC/Intrinsics.h"
|
[mlir][EDSC] NFC - Move StructuredIndexed and IteratorType out of Linalg
Summary:
This NFC revision will allow those classes to be reused to allow
building structured vector operations.
Reviewers: aartbik, ftynse
Subscribers: arphaman, mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, arpith-jacob, mgester, lucyrfox, liufengdb, Joonsoo, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D74279
2020-02-08 11:16:22 -05:00
|
|
|
#include "mlir/Dialect/Utils/StructuredOpsUtils.h"
|
2019-12-13 13:26:00 -08:00
|
|
|
#include "mlir/IR/AffineExpr.h"
|
2019-12-13 16:35:49 -08:00
|
|
|
#include "mlir/Support/Functional.h"
|
2019-12-13 13:26:00 -08:00
|
|
|
|
|
|
|
using namespace mlir;
|
|
|
|
using namespace mlir::edsc;
|
2019-12-13 16:35:49 -08:00
|
|
|
using namespace mlir::edsc::intrinsics;
|
[mlir][EDSC] Refactor dependencies involving EDSCs.
Summary: This diff removes the dependency of LinalgOps and VectorOps on EDSCs.
Reviewers: jpienaar, ftynse
Reviewed By: ftynse
Subscribers: merge_guards_bot, mgorny, mehdi_amini, rriddle, burmako, shauheen, antiagainst, csigg, arpith-jacob, mgester, lucyrfox, herhut, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D72481
2020-01-15 09:28:12 -05:00
|
|
|
using namespace mlir::linalg;
|
|
|
|
using namespace mlir::loop;
|
|
|
|
|
|
|
|
mlir::edsc::LoopRangeBuilder::LoopRangeBuilder(ValueHandle *iv,
|
|
|
|
ValueHandle range) {
|
|
|
|
assert(range.getType() && "expected !linalg.range type");
|
|
|
|
assert(range.getValue().getDefiningOp() &&
|
|
|
|
"need operations to extract range parts");
|
|
|
|
auto rangeOp = cast<RangeOp>(range.getValue().getDefiningOp());
|
|
|
|
auto lb = rangeOp.min();
|
|
|
|
auto ub = rangeOp.max();
|
|
|
|
auto step = rangeOp.step();
|
|
|
|
auto forOp = OperationHandle::createOp<ForOp>(lb, ub, step);
|
|
|
|
*iv = ValueHandle(forOp.getInductionVar());
|
|
|
|
auto *body = forOp.getBody();
|
|
|
|
enter(body, /*prev=*/1);
|
|
|
|
}
|
|
|
|
|
|
|
|
mlir::edsc::LoopRangeBuilder::LoopRangeBuilder(ValueHandle *iv,
|
|
|
|
SubViewOp::Range range) {
|
|
|
|
auto forOp =
|
|
|
|
OperationHandle::createOp<ForOp>(range.offset, range.size, range.stride);
|
|
|
|
*iv = ValueHandle(forOp.getInductionVar());
|
|
|
|
auto *body = forOp.getBody();
|
|
|
|
enter(body, /*prev=*/1);
|
|
|
|
}
|
|
|
|
|
[mlir][Linalg] Adding support for linalg_matmul with tensors.
Summary:
This revision provides 2 versions of matmul with tensors to account for the differences in buffer vs value semantics:
1. `C(i, j) = sum_{r_k} A(i, r_k) * B(r_k, j)`
2. `D(i, j) = C(i, j) + sum_{r_k} A(i, r_k) * B(r_k, j)`
Reviewers: ftynse
Subscribers: mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, arpith-jacob, mgester, lucyrfox, aartbik, liufengdb, Joonsoo, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D73796
2020-01-31 14:42:34 -05:00
|
|
|
ValueHandle
|
|
|
|
mlir::edsc::LoopRangeBuilder::operator()(std::function<void(void)> fun) {
|
[mlir][EDSC] Refactor dependencies involving EDSCs.
Summary: This diff removes the dependency of LinalgOps and VectorOps on EDSCs.
Reviewers: jpienaar, ftynse
Reviewed By: ftynse
Subscribers: merge_guards_bot, mgorny, mehdi_amini, rriddle, burmako, shauheen, antiagainst, csigg, arpith-jacob, mgester, lucyrfox, herhut, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D72481
2020-01-15 09:28:12 -05:00
|
|
|
if (fun)
|
|
|
|
fun();
|
|
|
|
exit();
|
|
|
|
return ValueHandle::null();
|
|
|
|
}
|
|
|
|
|
|
|
|
mlir::edsc::LoopNestRangeBuilder::LoopNestRangeBuilder(
|
|
|
|
ArrayRef<ValueHandle *> ivs, ArrayRef<SubViewOp::Range> ranges) {
|
|
|
|
loops.reserve(ranges.size());
|
|
|
|
for (unsigned i = 0, e = ranges.size(); i < e; ++i) {
|
|
|
|
loops.emplace_back(ivs[i], ranges[i]);
|
|
|
|
}
|
|
|
|
assert(loops.size() == ivs.size() && "Mismatch loops vs ivs size");
|
|
|
|
}
|
|
|
|
|
|
|
|
mlir::edsc::LoopNestRangeBuilder::LoopNestRangeBuilder(
|
|
|
|
ArrayRef<ValueHandle *> ivs, ArrayRef<ValueHandle> ranges) {
|
|
|
|
loops.reserve(ranges.size());
|
|
|
|
for (unsigned i = 0, e = ranges.size(); i < e; ++i) {
|
|
|
|
loops.emplace_back(ivs[i], ranges[i]);
|
|
|
|
}
|
|
|
|
assert(loops.size() == ivs.size() && "Mismatch loops vs ivs size");
|
|
|
|
}
|
|
|
|
|
|
|
|
mlir::edsc::LoopNestRangeBuilder::LoopNestRangeBuilder(
|
|
|
|
ArrayRef<ValueHandle *> ivs, ArrayRef<Value> ranges)
|
|
|
|
: LoopNestRangeBuilder(
|
|
|
|
ivs, SmallVector<ValueHandle, 4>(ranges.begin(), ranges.end())) {}
|
|
|
|
|
[mlir][Linalg] Adding support for linalg_matmul with tensors.
Summary:
This revision provides 2 versions of matmul with tensors to account for the differences in buffer vs value semantics:
1. `C(i, j) = sum_{r_k} A(i, r_k) * B(r_k, j)`
2. `D(i, j) = C(i, j) + sum_{r_k} A(i, r_k) * B(r_k, j)`
Reviewers: ftynse
Subscribers: mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, arpith-jacob, mgester, lucyrfox, aartbik, liufengdb, Joonsoo, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D73796
2020-01-31 14:42:34 -05:00
|
|
|
ValueHandle LoopNestRangeBuilder::LoopNestRangeBuilder::operator()(
|
|
|
|
std::function<void(void)> fun) {
|
[mlir][EDSC] Refactor dependencies involving EDSCs.
Summary: This diff removes the dependency of LinalgOps and VectorOps on EDSCs.
Reviewers: jpienaar, ftynse
Reviewed By: ftynse
Subscribers: merge_guards_bot, mgorny, mehdi_amini, rriddle, burmako, shauheen, antiagainst, csigg, arpith-jacob, mgester, lucyrfox, herhut, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D72481
2020-01-15 09:28:12 -05:00
|
|
|
if (fun)
|
|
|
|
fun();
|
|
|
|
for (auto &lit : reverse(loops)) {
|
|
|
|
lit({});
|
|
|
|
}
|
|
|
|
return ValueHandle::null();
|
|
|
|
}
|
|
|
|
|
2020-01-15 11:12:53 -05:00
|
|
|
namespace mlir {
|
|
|
|
namespace edsc {
|
2020-01-31 11:34:15 +01:00
|
|
|
|
[mlir][EDSC] Refactor dependencies involving EDSCs.
Summary: This diff removes the dependency of LinalgOps and VectorOps on EDSCs.
Reviewers: jpienaar, ftynse
Reviewed By: ftynse
Subscribers: merge_guards_bot, mgorny, mehdi_amini, rriddle, burmako, shauheen, antiagainst, csigg, arpith-jacob, mgester, lucyrfox, herhut, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D72481
2020-01-15 09:28:12 -05:00
|
|
|
template <>
|
2020-01-15 11:12:53 -05:00
|
|
|
GenericLoopNestRangeBuilder<loop::ForOp>::GenericLoopNestRangeBuilder(
|
|
|
|
ArrayRef<edsc::ValueHandle *> ivs, ArrayRef<Value> ranges) {
|
[mlir][EDSC] Refactor dependencies involving EDSCs.
Summary: This diff removes the dependency of LinalgOps and VectorOps on EDSCs.
Reviewers: jpienaar, ftynse
Reviewed By: ftynse
Subscribers: merge_guards_bot, mgorny, mehdi_amini, rriddle, burmako, shauheen, antiagainst, csigg, arpith-jacob, mgester, lucyrfox, herhut, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D72481
2020-01-15 09:28:12 -05:00
|
|
|
builder = std::make_unique<LoopNestRangeBuilder>(ivs, ranges);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
2020-01-15 11:12:53 -05:00
|
|
|
GenericLoopNestRangeBuilder<AffineForOp>::GenericLoopNestRangeBuilder(
|
|
|
|
ArrayRef<ValueHandle *> ivs, ArrayRef<Value> ranges) {
|
[mlir][EDSC] Refactor dependencies involving EDSCs.
Summary: This diff removes the dependency of LinalgOps and VectorOps on EDSCs.
Reviewers: jpienaar, ftynse
Reviewed By: ftynse
Subscribers: merge_guards_bot, mgorny, mehdi_amini, rriddle, burmako, shauheen, antiagainst, csigg, arpith-jacob, mgester, lucyrfox, herhut, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D72481
2020-01-15 09:28:12 -05:00
|
|
|
SmallVector<ValueHandle, 4> lbs;
|
|
|
|
SmallVector<ValueHandle, 4> ubs;
|
|
|
|
SmallVector<int64_t, 4> steps;
|
|
|
|
for (Value range : ranges) {
|
|
|
|
assert(range.getType() && "expected linalg.range type");
|
|
|
|
assert(range.getDefiningOp() && "need operations to extract range parts");
|
|
|
|
RangeOp rangeOp = cast<RangeOp>(range.getDefiningOp());
|
2020-01-31 11:34:15 +01:00
|
|
|
lbs.emplace_back(rangeOp.min());
|
|
|
|
ubs.emplace_back(rangeOp.max());
|
|
|
|
steps.emplace_back(rangeOp.step());
|
[mlir][EDSC] Refactor dependencies involving EDSCs.
Summary: This diff removes the dependency of LinalgOps and VectorOps on EDSCs.
Reviewers: jpienaar, ftynse
Reviewed By: ftynse
Subscribers: merge_guards_bot, mgorny, mehdi_amini, rriddle, burmako, shauheen, antiagainst, csigg, arpith-jacob, mgester, lucyrfox, herhut, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D72481
2020-01-15 09:28:12 -05:00
|
|
|
}
|
|
|
|
builder = std::make_unique<AffineLoopNestBuilder>(ivs, lbs, ubs, steps);
|
|
|
|
}
|
2020-01-31 11:34:15 +01:00
|
|
|
|
|
|
|
template <>
|
|
|
|
GenericLoopNestRangeBuilder<loop::ParallelOp>::GenericLoopNestRangeBuilder(
|
|
|
|
ArrayRef<ValueHandle *> ivs, ArrayRef<Value> ranges) {
|
|
|
|
SmallVector<ValueHandle, 4> lbs, ubs, steps;
|
|
|
|
for (Value range : ranges) {
|
|
|
|
assert(range.getType() && "expected linalg.range type");
|
|
|
|
assert(range.getDefiningOp() && "need operations to extract range parts");
|
|
|
|
RangeOp rangeOp = cast<RangeOp>(range.getDefiningOp());
|
|
|
|
lbs.emplace_back(rangeOp.min());
|
|
|
|
ubs.emplace_back(rangeOp.max());
|
|
|
|
steps.emplace_back(rangeOp.step());
|
|
|
|
}
|
|
|
|
builder = std::make_unique<ParallelLoopNestBuilder>(ivs, lbs, ubs, steps);
|
|
|
|
}
|
|
|
|
|
2020-01-15 11:12:53 -05:00
|
|
|
} // namespace edsc
|
|
|
|
} // namespace mlir
|
2019-12-13 16:35:49 -08:00
|
|
|
|
|
|
|
static void getMaxDimIndex(ArrayRef<StructuredIndexed> structuredIndices,
|
|
|
|
unsigned &pos) {
|
|
|
|
for (auto sidx : structuredIndices) {
|
|
|
|
for (auto expr : sidx.getExprs()) {
|
|
|
|
expr.walk([&pos](AffineExpr e) {
|
|
|
|
if (auto d = e.dyn_cast<AffineDimExpr>())
|
|
|
|
pos = std::max(pos, d.getPosition());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-13 13:26:00 -08:00
|
|
|
|
2020-01-02 09:14:23 -05:00
|
|
|
Operation *mlir::edsc::makeGenericLinalgOp(
|
[mlir][EDSC] NFC - Move StructuredIndexed and IteratorType out of Linalg
Summary:
This NFC revision will allow those classes to be reused to allow
building structured vector operations.
Reviewers: aartbik, ftynse
Subscribers: arphaman, mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, arpith-jacob, mgester, lucyrfox, liufengdb, Joonsoo, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D74279
2020-02-08 11:16:22 -05:00
|
|
|
ArrayRef<IteratorType> iteratorTypes, ArrayRef<StructuredIndexed> inputs,
|
2020-01-21 19:43:27 -05:00
|
|
|
ArrayRef<StructuredIndexed> outputs,
|
2019-12-23 14:45:01 -08:00
|
|
|
function_ref<void(ArrayRef<BlockArgument>)> regionBuilder,
|
|
|
|
ArrayRef<Value> otherValues, ArrayRef<Attribute> otherAttributes) {
|
[mlir][Linalg] Add tensor support to Linalg EDSC Builders
Summary:
This diff extends the Linalg EDSC builders so we can easily create mixed
tensor/buffer linalg.generic ops. This is expected to be useful for
HLO -> Linalg lowering.
The StructuredIndexed struct is made to derive from ValueHandle and can
now capture a type + indexing expressions. This is used to represent return
tensors.
Pointwise unary and binary builders are extended to allow both output buffers
and return tensors. This has implications on the number of region arguments.
Reviewers: ftynse, hanchung, asaadaldien
Subscribers: mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, arpith-jacob, mgester, lucyrfox, aartbik, liufengdb, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D73149
2020-01-21 19:48:31 -05:00
|
|
|
for (unsigned i = 0, e = outputs.size(); i + 1 < e; ++i)
|
|
|
|
assert(!(outputs[i].getType().isa<RankedTensorType>() &&
|
|
|
|
outputs[i + 1].getType().isa<MemRefType>()) &&
|
|
|
|
"output tensors must be passed after output buffers");
|
2019-12-13 13:26:00 -08:00
|
|
|
auto &builder = edsc::ScopedContext::getBuilder();
|
|
|
|
auto *ctx = builder.getContext();
|
2019-12-13 16:35:49 -08:00
|
|
|
unsigned nInputs = inputs.size();
|
2020-01-21 19:43:27 -05:00
|
|
|
unsigned nOutputs = outputs.size();
|
2019-12-16 13:32:02 -08:00
|
|
|
unsigned maxPos = 0;
|
|
|
|
getMaxDimIndex(inputs, maxPos);
|
2020-01-21 19:43:27 -05:00
|
|
|
getMaxDimIndex(outputs, maxPos);
|
2019-12-16 13:32:02 -08:00
|
|
|
// maxPos is 0 indexed, need to turn this into a count (i.e. +1)
|
|
|
|
unsigned nDims = maxPos + 1;
|
2019-12-13 13:26:00 -08:00
|
|
|
|
|
|
|
SmallVector<AffineMap, 4> maps;
|
2019-12-13 16:35:49 -08:00
|
|
|
maps.reserve(nInputs + nOutputs);
|
|
|
|
for (auto in : inputs)
|
|
|
|
maps.push_back(
|
2019-12-16 13:32:02 -08:00
|
|
|
AffineMap::get(/*dimCount=*/nDims, /*symbolCount=*/0, in.getExprs()));
|
2020-01-21 19:43:27 -05:00
|
|
|
for (auto out : outputs)
|
2019-12-13 16:35:49 -08:00
|
|
|
maps.push_back(
|
2019-12-16 13:32:02 -08:00
|
|
|
AffineMap::get(/*dimCount=*/nDims, /*symbolCount=*/0, out.getExprs()));
|
2019-12-13 13:26:00 -08:00
|
|
|
|
2019-12-13 16:35:49 -08:00
|
|
|
unsigned nViews = nInputs + nOutputs;
|
2019-12-23 14:45:01 -08:00
|
|
|
SmallVector<Value, 4> values;
|
2019-12-13 16:35:49 -08:00
|
|
|
values.reserve(nViews);
|
|
|
|
values.append(inputs.begin(), inputs.end());
|
[mlir][Linalg] Add tensor support to Linalg EDSC Builders
Summary:
This diff extends the Linalg EDSC builders so we can easily create mixed
tensor/buffer linalg.generic ops. This is expected to be useful for
HLO -> Linalg lowering.
The StructuredIndexed struct is made to derive from ValueHandle and can
now capture a type + indexing expressions. This is used to represent return
tensors.
Pointwise unary and binary builders are extended to allow both output buffers
and return tensors. This has implications on the number of region arguments.
Reviewers: ftynse, hanchung, asaadaldien
Subscribers: mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, arpith-jacob, mgester, lucyrfox, aartbik, liufengdb, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D73149
2020-01-21 19:48:31 -05:00
|
|
|
std::copy_if(outputs.begin(), outputs.end(), std::back_inserter(values),
|
|
|
|
[](StructuredIndexed s) { return s.hasValue(); });
|
|
|
|
SmallVector<Type, 4> types;
|
|
|
|
std::copy_if(outputs.begin(), outputs.end(), std::back_inserter(types),
|
|
|
|
[](StructuredIndexed s) { return !s.hasValue(); });
|
2019-12-13 13:26:00 -08:00
|
|
|
|
2019-12-13 16:35:49 -08:00
|
|
|
auto iteratorStrTypes = functional::map(toString, iteratorTypes);
|
|
|
|
// clang-format off
|
2019-12-13 13:26:00 -08:00
|
|
|
auto *op =
|
|
|
|
edsc::ScopedContext::getBuilder()
|
|
|
|
.create<linalg::GenericOp>(
|
2019-12-13 16:35:49 -08:00
|
|
|
edsc::ScopedContext::getLocation(),
|
[mlir][Linalg] Add tensor support to Linalg EDSC Builders
Summary:
This diff extends the Linalg EDSC builders so we can easily create mixed
tensor/buffer linalg.generic ops. This is expected to be useful for
HLO -> Linalg lowering.
The StructuredIndexed struct is made to derive from ValueHandle and can
now capture a type + indexing expressions. This is used to represent return
tensors.
Pointwise unary and binary builders are extended to allow both output buffers
and return tensors. This has implications on the number of region arguments.
Reviewers: ftynse, hanchung, asaadaldien
Subscribers: mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, arpith-jacob, mgester, lucyrfox, aartbik, liufengdb, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D73149
2020-01-21 19:48:31 -05:00
|
|
|
types,
|
2019-12-13 16:35:49 -08:00
|
|
|
values,
|
|
|
|
IntegerAttr::get(IntegerType::get(64, ctx), nInputs),
|
|
|
|
IntegerAttr::get(IntegerType::get(64, ctx), nOutputs),
|
2019-12-13 13:26:00 -08:00
|
|
|
builder.getAffineMapArrayAttr(maps),
|
2019-12-13 16:35:49 -08:00
|
|
|
builder.getStrArrayAttr(iteratorStrTypes),
|
|
|
|
StringAttr() /*doc*/,
|
|
|
|
FlatSymbolRefAttr() /*fun*/,
|
|
|
|
StringAttr() /*library_call*/
|
|
|
|
/* TODO: other attributes in op */
|
2019-12-13 13:26:00 -08:00
|
|
|
)
|
|
|
|
.getOperation();
|
2019-12-13 16:35:49 -08:00
|
|
|
// clang-format on
|
2019-12-13 13:26:00 -08:00
|
|
|
|
|
|
|
using namespace edsc;
|
|
|
|
SmallVector<Type, 4> blockTypes;
|
2019-12-13 16:35:49 -08:00
|
|
|
blockTypes.reserve(values.size());
|
|
|
|
for (auto it : llvm::enumerate(values))
|
|
|
|
blockTypes.push_back((it.index() < nViews)
|
|
|
|
? getElementTypeOrSelf(it.value())
|
2020-01-11 08:54:04 -08:00
|
|
|
: it.value().getType());
|
2019-12-13 13:26:00 -08:00
|
|
|
|
[mlir][Linalg] Fix Linalg EDSC builders
Summary:
This diff fixes the fact that the method `mlir::edsc::makeGenericLinalgOp`
incorrectly adds 2 blocks to Linalg ops.
Tests are updated accordingly.
Reviewers: ftynse, hanchung, herhut, pifon2a, asaadaldien
Reviewed By: asaadaldien
Subscribers: merge_guards_bot, mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, arpith-jacob, mgester, lucyrfox, liufengdb, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D72780
2020-01-16 09:30:17 -05:00
|
|
|
assert(op->getNumRegions() == 1);
|
|
|
|
assert(op->getRegion(0).empty());
|
|
|
|
OpBuilder opBuilder(op);
|
|
|
|
ScopedContext scope(opBuilder, op->getLoc());
|
2019-12-13 13:26:00 -08:00
|
|
|
BlockHandle b;
|
|
|
|
auto handles = makeValueHandles(blockTypes);
|
[mlir][Linalg] Fix Linalg EDSC builders
Summary:
This diff fixes the fact that the method `mlir::edsc::makeGenericLinalgOp`
incorrectly adds 2 blocks to Linalg ops.
Tests are updated accordingly.
Reviewers: ftynse, hanchung, herhut, pifon2a, asaadaldien
Reviewed By: asaadaldien
Subscribers: merge_guards_bot, mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, arpith-jacob, mgester, lucyrfox, liufengdb, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D72780
2020-01-16 09:30:17 -05:00
|
|
|
BlockBuilder(&b, op->getRegion(0),
|
|
|
|
makeHandlePointers(MutableArrayRef<ValueHandle>(handles)))(
|
2019-12-13 13:26:00 -08:00
|
|
|
[&] { regionBuilder(b.getBlock()->getArguments()); });
|
[mlir][Linalg] Fix Linalg EDSC builders
Summary:
This diff fixes the fact that the method `mlir::edsc::makeGenericLinalgOp`
incorrectly adds 2 blocks to Linalg ops.
Tests are updated accordingly.
Reviewers: ftynse, hanchung, herhut, pifon2a, asaadaldien
Reviewed By: asaadaldien
Subscribers: merge_guards_bot, mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, arpith-jacob, mgester, lucyrfox, liufengdb, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D72780
2020-01-16 09:30:17 -05:00
|
|
|
assert(op->getRegion(0).getBlocks().size() == 1);
|
2019-12-13 13:26:00 -08:00
|
|
|
return op;
|
|
|
|
}
|
2019-12-13 16:35:49 -08:00
|
|
|
|
[mlir][Linalg] Adding support for linalg_matmul with tensors.
Summary:
This revision provides 2 versions of matmul with tensors to account for the differences in buffer vs value semantics:
1. `C(i, j) = sum_{r_k} A(i, r_k) * B(r_k, j)`
2. `D(i, j) = C(i, j) + sum_{r_k} A(i, r_k) * B(r_k, j)`
Reviewers: ftynse
Subscribers: mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, arpith-jacob, mgester, lucyrfox, aartbik, liufengdb, Joonsoo, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D73796
2020-01-31 14:42:34 -05:00
|
|
|
static void mulRegionBuilder(ArrayRef<BlockArgument> args) {
|
|
|
|
using edsc::op::operator*;
|
|
|
|
assert(args.size() == 2 && "expected 2 block arguments");
|
|
|
|
ValueHandle a(args[0]), b(args[1]);
|
|
|
|
linalg_yield((a * b).getValue());
|
|
|
|
}
|
|
|
|
|
2019-12-23 14:45:01 -08:00
|
|
|
void mlir::edsc::ops::macRegionBuilder(ArrayRef<BlockArgument> args) {
|
2019-12-16 13:32:02 -08:00
|
|
|
using edsc::op::operator+;
|
|
|
|
using edsc::op::operator*;
|
|
|
|
assert(args.size() == 3 && "expected 3 block arguments");
|
|
|
|
ValueHandle a(args[0]), b(args[1]), c(args[2]);
|
|
|
|
linalg_yield((c + a * b).getValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
Operation *mlir::edsc::ops::linalg_pointwise(UnaryPointwiseOpBuilder unaryOp,
|
|
|
|
StructuredIndexed I,
|
2020-01-21 19:43:27 -05:00
|
|
|
StructuredIndexed O) {
|
[mlir][EDSC] NFC - Move StructuredIndexed and IteratorType out of Linalg
Summary:
This NFC revision will allow those classes to be reused to allow
building structured vector operations.
Reviewers: aartbik, ftynse
Subscribers: arphaman, mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, arpith-jacob, mgester, lucyrfox, liufengdb, Joonsoo, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D74279
2020-02-08 11:16:22 -05:00
|
|
|
SmallVector<IteratorType, 4> iterTypes(O.getExprs().size(),
|
|
|
|
IteratorType::Parallel);
|
[mlir][Linalg] Add tensor support to Linalg EDSC Builders
Summary:
This diff extends the Linalg EDSC builders so we can easily create mixed
tensor/buffer linalg.generic ops. This is expected to be useful for
HLO -> Linalg lowering.
The StructuredIndexed struct is made to derive from ValueHandle and can
now capture a type + indexing expressions. This is used to represent return
tensors.
Pointwise unary and binary builders are extended to allow both output buffers
and return tensors. This has implications on the number of region arguments.
Reviewers: ftynse, hanchung, asaadaldien
Subscribers: mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, arpith-jacob, mgester, lucyrfox, aartbik, liufengdb, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D73149
2020-01-21 19:48:31 -05:00
|
|
|
if (O.getType().isa<RankedTensorType>()) {
|
|
|
|
auto fun = [&unaryOp](ArrayRef<BlockArgument> args) {
|
|
|
|
assert(args.size() == 1 && "expected 1 block arguments");
|
|
|
|
ValueHandle a(args[0]);
|
|
|
|
linalg_yield(unaryOp(a));
|
|
|
|
};
|
|
|
|
return makeGenericLinalgOp(iterTypes, {I}, {O}, fun);
|
|
|
|
}
|
2019-12-23 14:45:01 -08:00
|
|
|
auto fun = [&unaryOp](ArrayRef<BlockArgument> args) {
|
2019-12-16 13:32:02 -08:00
|
|
|
assert(args.size() == 2 && "expected 2 block arguments");
|
|
|
|
ValueHandle a(args[0]);
|
|
|
|
linalg_yield(unaryOp(a));
|
|
|
|
};
|
2020-01-21 19:43:27 -05:00
|
|
|
return makeGenericLinalgOp(iterTypes, {I}, {O}, fun);
|
2019-12-16 13:32:02 -08:00
|
|
|
}
|
|
|
|
|
2020-01-21 19:43:27 -05:00
|
|
|
Operation *mlir::edsc::ops::linalg_pointwise_tanh(StructuredIndexed I,
|
|
|
|
StructuredIndexed O) {
|
2020-02-10 11:55:02 -05:00
|
|
|
UnaryPointwiseOpBuilder unOp(
|
|
|
|
[](ValueHandle a) -> Value { return std_tanh(a); });
|
2020-01-21 19:43:27 -05:00
|
|
|
return linalg_pointwise(unOp, I, O);
|
2019-12-16 13:32:02 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Binary pointwise operation (with broadcast) entry point.
|
|
|
|
Operation *mlir::edsc::ops::linalg_pointwise(BinaryPointwiseOpBuilder binaryOp,
|
|
|
|
StructuredIndexed I1,
|
|
|
|
StructuredIndexed I2,
|
2020-01-21 19:43:27 -05:00
|
|
|
StructuredIndexed O) {
|
[mlir][EDSC] NFC - Move StructuredIndexed and IteratorType out of Linalg
Summary:
This NFC revision will allow those classes to be reused to allow
building structured vector operations.
Reviewers: aartbik, ftynse
Subscribers: arphaman, mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, arpith-jacob, mgester, lucyrfox, liufengdb, Joonsoo, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D74279
2020-02-08 11:16:22 -05:00
|
|
|
SmallVector<IteratorType, 4> iterTypes(O.getExprs().size(),
|
|
|
|
IteratorType::Parallel);
|
[mlir][Linalg] Add tensor support to Linalg EDSC Builders
Summary:
This diff extends the Linalg EDSC builders so we can easily create mixed
tensor/buffer linalg.generic ops. This is expected to be useful for
HLO -> Linalg lowering.
The StructuredIndexed struct is made to derive from ValueHandle and can
now capture a type + indexing expressions. This is used to represent return
tensors.
Pointwise unary and binary builders are extended to allow both output buffers
and return tensors. This has implications on the number of region arguments.
Reviewers: ftynse, hanchung, asaadaldien
Subscribers: mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, arpith-jacob, mgester, lucyrfox, aartbik, liufengdb, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D73149
2020-01-21 19:48:31 -05:00
|
|
|
if (O.getType().isa<RankedTensorType>()) {
|
|
|
|
auto fun = [&binaryOp](ArrayRef<BlockArgument> args) {
|
|
|
|
assert(args.size() == 2 && "expected 2 block arguments");
|
|
|
|
ValueHandle a(args[0]), b(args[1]);
|
|
|
|
linalg_yield(binaryOp(a, b));
|
|
|
|
};
|
|
|
|
return makeGenericLinalgOp(iterTypes, {I1, I2}, {O}, fun);
|
|
|
|
}
|
2019-12-23 14:45:01 -08:00
|
|
|
auto fun = [&binaryOp](ArrayRef<BlockArgument> args) {
|
2019-12-16 13:32:02 -08:00
|
|
|
assert(args.size() == 3 && "expected 3 block arguments");
|
|
|
|
ValueHandle a(args[0]), b(args[1]);
|
|
|
|
linalg_yield(binaryOp(a, b));
|
|
|
|
};
|
2020-01-21 19:43:27 -05:00
|
|
|
return makeGenericLinalgOp(iterTypes, {I1, I2}, {O}, fun);
|
2019-12-16 13:32:02 -08:00
|
|
|
}
|
2019-12-13 16:35:49 -08:00
|
|
|
|
2020-01-21 19:43:27 -05:00
|
|
|
Operation *mlir::edsc::ops::linalg_pointwise_add(StructuredIndexed I1,
|
|
|
|
StructuredIndexed I2,
|
|
|
|
StructuredIndexed O) {
|
2019-12-16 13:32:02 -08:00
|
|
|
using edsc::op::operator+;
|
|
|
|
BinaryPointwiseOpBuilder binOp(
|
2019-12-23 14:45:01 -08:00
|
|
|
[](ValueHandle a, ValueHandle b) -> Value { return a + b; });
|
2020-01-21 19:43:27 -05:00
|
|
|
return linalg_pointwise(binOp, I1, I2, O);
|
2019-12-16 13:32:02 -08:00
|
|
|
}
|
|
|
|
|
2020-01-21 19:43:27 -05:00
|
|
|
Operation *mlir::edsc::ops::linalg_pointwise_max(StructuredIndexed I1,
|
|
|
|
StructuredIndexed I2,
|
|
|
|
StructuredIndexed O) {
|
2019-12-23 14:45:01 -08:00
|
|
|
BinaryPointwiseOpBuilder binOp([](ValueHandle a, ValueHandle b) -> Value {
|
2019-12-16 13:32:02 -08:00
|
|
|
using edsc::op::operator>;
|
2020-02-10 11:55:02 -05:00
|
|
|
return std_select(a > b, a, b).getValue();
|
2019-12-16 13:32:02 -08:00
|
|
|
});
|
2020-01-21 19:43:27 -05:00
|
|
|
return linalg_pointwise(binOp, I1, I2, O);
|
2019-12-16 13:32:02 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
Operation *mlir::edsc::ops::linalg_matmul(ValueHandle vA, ValueHandle vB,
|
2020-01-21 19:43:27 -05:00
|
|
|
ValueHandle vC) {
|
|
|
|
// clang-format off
|
2019-12-13 16:35:49 -08:00
|
|
|
AffineExpr m, n, k;
|
|
|
|
bindDims(ScopedContext::getContext(), m, n, k);
|
|
|
|
StructuredIndexed A(vA), B(vB), C(vC);
|
2020-01-02 09:14:23 -05:00
|
|
|
return makeGenericLinalgOp(
|
[mlir][EDSC] NFC - Move StructuredIndexed and IteratorType out of Linalg
Summary:
This NFC revision will allow those classes to be reused to allow
building structured vector operations.
Reviewers: aartbik, ftynse
Subscribers: arphaman, mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, arpith-jacob, mgester, lucyrfox, liufengdb, Joonsoo, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D74279
2020-02-08 11:16:22 -05:00
|
|
|
{IteratorType::Parallel, IteratorType::Parallel, IteratorType::Reduction},
|
2020-01-21 19:43:27 -05:00
|
|
|
{A({m, k}), B({k, n})},
|
|
|
|
{C({m, n})},
|
|
|
|
macRegionBuilder);
|
|
|
|
// clang-format on
|
2019-12-16 13:32:02 -08:00
|
|
|
}
|
|
|
|
|
[mlir][Linalg] Adding support for linalg_matmul with tensors.
Summary:
This revision provides 2 versions of matmul with tensors to account for the differences in buffer vs value semantics:
1. `C(i, j) = sum_{r_k} A(i, r_k) * B(r_k, j)`
2. `D(i, j) = C(i, j) + sum_{r_k} A(i, r_k) * B(r_k, j)`
Reviewers: ftynse
Subscribers: mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, arpith-jacob, mgester, lucyrfox, aartbik, liufengdb, Joonsoo, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D73796
2020-01-31 14:42:34 -05:00
|
|
|
Operation *mlir::edsc::ops::linalg_matmul(ValueHandle vA, ValueHandle vB,
|
|
|
|
RankedTensorType tC) {
|
|
|
|
// clang-format off
|
|
|
|
AffineExpr m, n, k;
|
|
|
|
bindDims(ScopedContext::getContext(), m, n, k);
|
|
|
|
StructuredIndexed A(vA), B(vB), C(tC);
|
|
|
|
return makeGenericLinalgOp(
|
[mlir][EDSC] NFC - Move StructuredIndexed and IteratorType out of Linalg
Summary:
This NFC revision will allow those classes to be reused to allow
building structured vector operations.
Reviewers: aartbik, ftynse
Subscribers: arphaman, mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, arpith-jacob, mgester, lucyrfox, liufengdb, Joonsoo, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D74279
2020-02-08 11:16:22 -05:00
|
|
|
{IteratorType::Parallel, IteratorType::Parallel, IteratorType::Reduction},
|
[mlir][Linalg] Adding support for linalg_matmul with tensors.
Summary:
This revision provides 2 versions of matmul with tensors to account for the differences in buffer vs value semantics:
1. `C(i, j) = sum_{r_k} A(i, r_k) * B(r_k, j)`
2. `D(i, j) = C(i, j) + sum_{r_k} A(i, r_k) * B(r_k, j)`
Reviewers: ftynse
Subscribers: mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, arpith-jacob, mgester, lucyrfox, aartbik, liufengdb, Joonsoo, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D73796
2020-01-31 14:42:34 -05:00
|
|
|
{A({m, k}), B({k, n})},
|
|
|
|
{C({m, n})},
|
|
|
|
mulRegionBuilder);
|
|
|
|
// clang-format on
|
|
|
|
}
|
|
|
|
|
|
|
|
Operation *mlir::edsc::ops::linalg_matmul(ValueHandle vA, ValueHandle vB,
|
|
|
|
ValueHandle vC, RankedTensorType tD) {
|
|
|
|
// clang-format off
|
|
|
|
AffineExpr m, n, k;
|
|
|
|
bindDims(ScopedContext::getContext(), m, n, k);
|
|
|
|
StructuredIndexed A(vA), B(vB), C(vC), D(tD);
|
|
|
|
return makeGenericLinalgOp(
|
[mlir][EDSC] NFC - Move StructuredIndexed and IteratorType out of Linalg
Summary:
This NFC revision will allow those classes to be reused to allow
building structured vector operations.
Reviewers: aartbik, ftynse
Subscribers: arphaman, mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, arpith-jacob, mgester, lucyrfox, liufengdb, Joonsoo, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D74279
2020-02-08 11:16:22 -05:00
|
|
|
{IteratorType::Parallel, IteratorType::Parallel, IteratorType::Reduction},
|
[mlir][Linalg] Adding support for linalg_matmul with tensors.
Summary:
This revision provides 2 versions of matmul with tensors to account for the differences in buffer vs value semantics:
1. `C(i, j) = sum_{r_k} A(i, r_k) * B(r_k, j)`
2. `D(i, j) = C(i, j) + sum_{r_k} A(i, r_k) * B(r_k, j)`
Reviewers: ftynse
Subscribers: mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, arpith-jacob, mgester, lucyrfox, aartbik, liufengdb, Joonsoo, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D73796
2020-01-31 14:42:34 -05:00
|
|
|
{A({m, k}), B({k, n}), C({m, n})},
|
|
|
|
{D({m, n})},
|
|
|
|
macRegionBuilder);
|
|
|
|
// clang-format on
|
|
|
|
}
|
|
|
|
|
2019-12-16 13:32:02 -08:00
|
|
|
Operation *mlir::edsc::ops::linalg_conv_nhwc(ValueHandle vI, ValueHandle vW,
|
|
|
|
ValueHandle vO,
|
|
|
|
ArrayRef<int> strides,
|
|
|
|
ArrayRef<int> dilations) {
|
|
|
|
MLIRContext *ctx = ScopedContext::getContext();
|
|
|
|
// TODO(ntv) some template magic to make everything rank-polymorphic.
|
|
|
|
assert((dilations.empty() || dilations.size() == 2) && "only 2-D conv atm");
|
|
|
|
assert((strides.empty() || strides.size() == 2) && "only 2-D conv atm");
|
|
|
|
|
|
|
|
// Some short names.
|
[mlir][EDSC] NFC - Move StructuredIndexed and IteratorType out of Linalg
Summary:
This NFC revision will allow those classes to be reused to allow
building structured vector operations.
Reviewers: aartbik, ftynse
Subscribers: arphaman, mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, arpith-jacob, mgester, lucyrfox, liufengdb, Joonsoo, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D74279
2020-02-08 11:16:22 -05:00
|
|
|
auto par = IteratorType::Parallel;
|
|
|
|
auto red = IteratorType::Reduction;
|
2019-12-16 13:32:02 -08:00
|
|
|
auto s = strides;
|
|
|
|
auto d = dilations;
|
|
|
|
|
|
|
|
AffineExpr b, f, h, w, kh, kw, c;
|
|
|
|
bindDims(ctx, b, f, h, w, kh, kw, c);
|
|
|
|
unsigned numDims = c.cast<AffineDimExpr>().getPosition() + 1;
|
|
|
|
StructuredIndexed I(vI), W(vW), O(vO);
|
|
|
|
// clang-format off
|
2020-01-21 19:43:27 -05:00
|
|
|
return makeGenericLinalgOp(
|
|
|
|
{par, par, par, par, red, red, red}, {
|
2019-12-16 13:32:02 -08:00
|
|
|
I({b,
|
2020-01-21 19:43:27 -05:00
|
|
|
// Roundtrip to flattened form to serve as canonicalization and ensure
|
|
|
|
// consistent ordering of subexpressions.
|
2019-12-16 13:32:02 -08:00
|
|
|
simplifyAffineExpr(s[0] * h + d[0] * kh, numDims, 0),
|
|
|
|
simplifyAffineExpr(s[1] * w + d[1] * kw, numDims, 0),
|
|
|
|
c}),
|
2020-01-21 19:43:27 -05:00
|
|
|
W({kh, kw, c, f})}, {
|
|
|
|
O({b, h, w, f})},
|
|
|
|
macRegionBuilder);
|
2019-12-16 13:32:02 -08:00
|
|
|
// clang-format on
|
|
|
|
}
|
|
|
|
|
|
|
|
Operation *mlir::edsc::ops::linalg_dilated_conv_nhwc(
|
2020-01-21 19:43:27 -05:00
|
|
|
ValueHandle vI, ValueHandle vW, ValueHandle vO, int depth_multiplier,
|
2019-12-16 13:32:02 -08:00
|
|
|
ArrayRef<int> strides, ArrayRef<int> dilations) {
|
|
|
|
MLIRContext *ctx = ScopedContext::getContext();
|
|
|
|
// TODO(ntv) some template magic to make everything rank-polymorphic.
|
|
|
|
assert((dilations.empty() || dilations.size() == 2) && "only 2-D conv atm");
|
|
|
|
assert((strides.empty() || strides.size() == 2) && "only 2-D conv atm");
|
|
|
|
|
|
|
|
// Some short names.
|
[mlir][EDSC] NFC - Move StructuredIndexed and IteratorType out of Linalg
Summary:
This NFC revision will allow those classes to be reused to allow
building structured vector operations.
Reviewers: aartbik, ftynse
Subscribers: arphaman, mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, arpith-jacob, mgester, lucyrfox, liufengdb, Joonsoo, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D74279
2020-02-08 11:16:22 -05:00
|
|
|
auto par = IteratorType::Parallel;
|
|
|
|
auto red = IteratorType::Reduction;
|
2019-12-16 13:32:02 -08:00
|
|
|
auto s = strides;
|
|
|
|
auto d = dilations;
|
|
|
|
|
|
|
|
// clang-format off
|
|
|
|
AffineExpr b, dm, c, h, w, kh, kw;
|
|
|
|
bindDims(ctx, b, dm, c, h, w, kh, kw);
|
|
|
|
unsigned numDims = kw.cast<AffineDimExpr>().getPosition() + 1;
|
|
|
|
StructuredIndexed I(vI), W(vW), O(vO);
|
2020-01-21 19:43:27 -05:00
|
|
|
return makeGenericLinalgOp(
|
|
|
|
{par, par, par, par, par, red, red}, {
|
2019-12-16 13:32:02 -08:00
|
|
|
I({b,
|
|
|
|
// Roundtrip to flattened form to serve as canonicalization and ensure
|
|
|
|
// consistent ordering of subexpressions.
|
|
|
|
simplifyAffineExpr(s[0] * h + d[0] * kh, numDims, 0),
|
|
|
|
simplifyAffineExpr(s[1] * w + d[1] * kw, numDims, 0),
|
|
|
|
c}),
|
2020-01-21 19:43:27 -05:00
|
|
|
W({kh, kw, c, dm})}, {
|
|
|
|
O({b, h, w, simplifyAffineExpr(c * depth_multiplier + dm, numDims, 0)})},
|
|
|
|
macRegionBuilder);
|
2019-12-13 16:35:49 -08:00
|
|
|
// clang-format on
|
|
|
|
}
|