2020-09-17 08:31:40 +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 "../PassDetail.h"
|
2020-10-07 07:14:37 +08:00
|
|
|
#include "npcomp/RefBackend/RefBackend.h"
|
2020-09-17 08:31:40 +08:00
|
|
|
|
|
|
|
#include "mlir/Dialect/StandardOps/IR/Ops.h"
|
|
|
|
#include "mlir/Pass/Pass.h"
|
|
|
|
#include "mlir/Pass/PassRegistry.h"
|
2020-10-15 10:28:43 +08:00
|
|
|
#include "mlir/Transforms/Bufferize.h"
|
2020-09-17 08:31:40 +08:00
|
|
|
#include "mlir/Transforms/DialectConversion.h"
|
2020-10-08 08:30:10 +08:00
|
|
|
#include "npcomp/Dialect/Refback/IR/RefbackDialect.h"
|
|
|
|
#include "npcomp/Dialect/Refback/IR/RefbackOps.h"
|
2020-09-17 08:31:40 +08:00
|
|
|
|
|
|
|
using namespace mlir;
|
|
|
|
using namespace mlir::NPCOMP;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class LowerExtractElementOp : public OpConversionPattern<ExtractElementOp> {
|
|
|
|
public:
|
|
|
|
using OpConversionPattern::OpConversionPattern;
|
|
|
|
LogicalResult
|
|
|
|
matchAndRewrite(ExtractElementOp op, ArrayRef<Value> operands,
|
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
|
|
|
ExtractElementOp::Adaptor adaptor(operands);
|
|
|
|
rewriter.replaceOpWithNewOp<LoadOp>(op, adaptor.aggregate(),
|
|
|
|
adaptor.indices());
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class LowerTensorFromElementsOp
|
|
|
|
: public OpConversionPattern<TensorFromElementsOp> {
|
|
|
|
public:
|
|
|
|
using OpConversionPattern::OpConversionPattern;
|
|
|
|
LogicalResult
|
|
|
|
matchAndRewrite(TensorFromElementsOp op, ArrayRef<Value> operands,
|
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
|
|
|
int numberOfElements = op.elements().size();
|
|
|
|
auto resultType = MemRefType::get(
|
|
|
|
{numberOfElements}, op.getType().cast<TensorType>().getElementType());
|
|
|
|
Value result = rewriter.create<AllocOp>(op.getLoc(), resultType);
|
|
|
|
for (auto element : llvm::enumerate(op.elements())) {
|
|
|
|
Value index =
|
|
|
|
rewriter.create<ConstantIndexOp>(op.getLoc(), element.index());
|
|
|
|
rewriter.create<StoreOp>(op.getLoc(), element.value(), result, index);
|
|
|
|
}
|
|
|
|
rewriter.replaceOp(op, {result});
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class LowerTensorCastOp : public OpConversionPattern<TensorCastOp> {
|
|
|
|
public:
|
|
|
|
using OpConversionPattern::OpConversionPattern;
|
|
|
|
LogicalResult
|
|
|
|
matchAndRewrite(TensorCastOp op, ArrayRef<Value> operands,
|
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
|
|
|
auto resultType = typeConverter->convertType(op.getType());
|
|
|
|
rewriter.replaceOpWithNewOp<MemRefCastOp>(op, resultType, operands[0]);
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
// TODO: Upstream this.
|
|
|
|
class LowerStdToMemref : public LowerStdToMemrefBase<LowerStdToMemref> {
|
2020-09-22 05:48:44 +08:00
|
|
|
void getDependentDialects(DialectRegistry ®istry) const override {
|
2020-10-08 08:30:10 +08:00
|
|
|
registry.insert<refback::RefbackDialect>();
|
2020-09-22 05:48:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void runOnOperation() override {
|
2020-09-17 08:31:40 +08:00
|
|
|
auto func = getOperation();
|
|
|
|
auto *context = &getContext();
|
|
|
|
|
2020-10-15 10:28:43 +08:00
|
|
|
BufferizeTypeConverter typeConverter;
|
2020-09-17 08:31:40 +08:00
|
|
|
|
|
|
|
OwningRewritePatternList patterns;
|
|
|
|
|
|
|
|
ConversionTarget target(*context);
|
|
|
|
|
|
|
|
target.addLegalDialect<StandardOpsDialect>();
|
|
|
|
|
|
|
|
patterns.insert<LowerExtractElementOp>(typeConverter, context);
|
|
|
|
target.addIllegalOp<ExtractElementOp>();
|
|
|
|
patterns.insert<LowerTensorFromElementsOp>(typeConverter, context);
|
|
|
|
target.addIllegalOp<TensorFromElementsOp>();
|
|
|
|
patterns.insert<LowerTensorCastOp>(typeConverter, context);
|
|
|
|
target.addIllegalOp<TensorCastOp>();
|
|
|
|
|
|
|
|
if (failed(applyPartialConversion(func, target, patterns)))
|
|
|
|
return signalPassFailure();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
std::unique_ptr<OperationPass<FuncOp>>
|
|
|
|
mlir::NPCOMP::createLowerStdToMemrefPass() {
|
|
|
|
return std::make_unique<LowerStdToMemref>();
|
|
|
|
}
|