2020-05-07 09:41:54 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "npcomp/Conversion/TCFToTCP/TCFToTCP.h"
|
|
|
|
|
|
|
|
#include "../PassDetail.h"
|
|
|
|
#include "mlir/Dialect/Shape/IR/Shape.h"
|
|
|
|
#include "mlir/Dialect/Traits.h"
|
|
|
|
#include "mlir/Transforms/DialectConversion.h"
|
|
|
|
#include "npcomp/Dialect/TCF/IR/TCFOps.h"
|
|
|
|
#include "npcomp/Dialect/TCP/IR/TCPOps.h"
|
|
|
|
|
|
|
|
using namespace mlir;
|
|
|
|
using namespace mlir::NPCOMP;
|
|
|
|
|
|
|
|
namespace {
|
2020-08-03 13:06:12 +08:00
|
|
|
|
|
|
|
RankedTensorType getExtentTensorType(Builder &builder) {
|
|
|
|
return RankedTensorType::get({ShapedType::kDynamicSize},
|
|
|
|
builder.getIndexType());
|
|
|
|
}
|
|
|
|
|
2020-05-07 09:41:54 +08:00
|
|
|
class ConvertAdd : public OpRewritePattern<tcf::AddOp> {
|
|
|
|
public:
|
|
|
|
using OpRewritePattern::OpRewritePattern;
|
|
|
|
LogicalResult matchAndRewrite(tcf::AddOp op,
|
|
|
|
PatternRewriter &rewriter) const override {
|
|
|
|
auto lhsType = op.lhs().getType().dyn_cast<RankedTensorType>();
|
|
|
|
auto rhsType = op.rhs().getType().dyn_cast<RankedTensorType>();
|
|
|
|
if (!lhsType || !rhsType) {
|
|
|
|
return rewriter.notifyMatchFailure(op, "requires ranked tensors");
|
|
|
|
}
|
|
|
|
Value lhsShape = rewriter.create<shape::ShapeOfOp>(op.getLoc(), op.lhs());
|
|
|
|
Value rhsShape = rewriter.create<shape::ShapeOfOp>(op.getLoc(), op.rhs());
|
|
|
|
Value broadcastedShape = rewriter.create<shape::BroadcastOp>(
|
|
|
|
op.getLoc(), lhsShape, rhsShape, /*error=*/nullptr);
|
2020-05-19 04:35:25 +08:00
|
|
|
rewriter.create<tcp::ShapeObserveErrorOp>(op.getLoc(), broadcastedShape);
|
2020-08-03 13:06:12 +08:00
|
|
|
Value broadcastedExtents = rewriter.create<shape::ToExtentTensorOp>(
|
|
|
|
op.getLoc(), getExtentTensorType(rewriter), broadcastedShape);
|
|
|
|
|
2020-05-07 09:41:54 +08:00
|
|
|
// TODO: It's annoying to do the dynamic broadcast above then
|
|
|
|
// do the static transfer function here. Would be nice if they could
|
|
|
|
// somehow be unified.
|
|
|
|
SmallVector<int64_t, 6> broadcastedStaticShape;
|
|
|
|
OpTrait::util::getBroadcastedShape(lhsType.getShape(), rhsType.getShape(),
|
|
|
|
broadcastedStaticShape);
|
|
|
|
auto resultType =
|
|
|
|
RankedTensorType::get(broadcastedStaticShape, lhsType.getElementType());
|
|
|
|
Value lhsBroadcasted = rewriter.create<tcp::BroadcastToOp>(
|
2020-08-03 13:06:12 +08:00
|
|
|
op.getLoc(), resultType, op.lhs(), broadcastedExtents);
|
2020-05-07 09:41:54 +08:00
|
|
|
Value rhsBroadcasted = rewriter.create<tcp::BroadcastToOp>(
|
2020-08-03 13:06:12 +08:00
|
|
|
op.getLoc(), resultType, op.rhs(), broadcastedExtents);
|
2020-05-07 09:41:54 +08:00
|
|
|
Value add = rewriter.create<tcp::AddOp>(op.getLoc(), op.getType(),
|
|
|
|
lhsBroadcasted, rhsBroadcasted);
|
2020-05-15 06:19:37 +08:00
|
|
|
rewriter.replaceOp(op, add);
|
2020-05-07 09:41:54 +08:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
};
|
2020-06-14 05:53:54 +08:00
|
|
|
} // namespace
|
2020-05-07 09:41:54 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
class ConvertTCFToTCP : public ConvertTCFToTCPBase<ConvertTCFToTCP> {
|
|
|
|
public:
|
|
|
|
void runOnOperation() {
|
|
|
|
ModuleOp module = getOperation();
|
|
|
|
MLIRContext *context = &getContext();
|
|
|
|
|
|
|
|
OwningRewritePatternList patterns;
|
|
|
|
patterns.insert<ConvertAdd>(context);
|
|
|
|
(void)applyPatternsAndFoldGreedily(module, patterns);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
std::unique_ptr<OperationPass<ModuleOp>>
|
|
|
|
mlir::NPCOMP::createConvertTCFToTCPPass() {
|
|
|
|
return std::make_unique<ConvertTCFToTCP>();
|
|
|
|
}
|