|
| 1 | +//===- TrivialUse.cpp - Remove trivial use instruction ---------------- -*-===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +// |
| 9 | +// This file implements a pass to lower gpu kernels in NVVM/gpu dialects into |
| 10 | +// a generic parallel for representation |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | +#include "PassDetails.h" |
| 13 | + |
| 14 | +#include "mlir/Dialect/Arithmetic/IR/Arithmetic.h" |
| 15 | +#include "mlir/Dialect/MemRef/IR/MemRef.h" |
| 16 | +#include "mlir/Dialect/StandardOps/IR/Ops.h" |
| 17 | +#include "mlir/Dialect/StandardOps/Transforms/Passes.h" |
| 18 | +#include "mlir/Rewrite/FrozenRewritePatternSet.h" |
| 19 | +#include "mlir/Transforms/DialectConversion.h" |
| 20 | +#include "polygeist/Dialect.h" |
| 21 | +#include "polygeist/Ops.h" |
| 22 | + |
| 23 | +using namespace mlir; |
| 24 | +using namespace polygeist; |
| 25 | +using namespace mlir::arith; |
| 26 | + |
| 27 | +namespace { |
| 28 | + |
| 29 | +struct SubIndexToReinterpretCast |
| 30 | + : public OpConversionPattern<polygeist::SubIndexOp> { |
| 31 | + using OpConversionPattern::OpConversionPattern; |
| 32 | + |
| 33 | + LogicalResult |
| 34 | + matchAndRewrite(polygeist::SubIndexOp op, OpAdaptor adaptor, |
| 35 | + ConversionPatternRewriter &rewriter) const override { |
| 36 | + auto srcMemRefType = op.source().getType().cast<MemRefType>(); |
| 37 | + auto resMemRefType = op.result().getType().cast<MemRefType>(); |
| 38 | + auto shape = srcMemRefType.getShape(); |
| 39 | + |
| 40 | + if (!resMemRefType.hasStaticShape()) |
| 41 | + return failure(); |
| 42 | + |
| 43 | + int64_t innerSize = resMemRefType.getNumElements(); |
| 44 | + auto offset = rewriter.create<arith::MulIOp>( |
| 45 | + op.getLoc(), op.index(), |
| 46 | + rewriter.create<ConstantIndexOp>(op.getLoc(), innerSize)); |
| 47 | + |
| 48 | + llvm::SmallVector<OpFoldResult> sizes, strides; |
| 49 | + for (auto dim : shape.drop_front()) { |
| 50 | + sizes.push_back(rewriter.getIndexAttr(dim)); |
| 51 | + strides.push_back(rewriter.getIndexAttr(1)); |
| 52 | + } |
| 53 | + |
| 54 | + rewriter.replaceOpWithNewOp<memref::ReinterpretCastOp>( |
| 55 | + op, resMemRefType, op.source(), offset.getResult(), sizes, strides); |
| 56 | + |
| 57 | + return success(); |
| 58 | + } |
| 59 | +}; |
| 60 | + |
| 61 | +struct LowerPolygeistOpsPass |
| 62 | + : public LowerPolygeistOpsBase<LowerPolygeistOpsPass> { |
| 63 | + |
| 64 | + void runOnFunction() override { |
| 65 | + auto op = getOperation(); |
| 66 | + auto ctx = op.getContext(); |
| 67 | + RewritePatternSet patterns(ctx); |
| 68 | + patterns.insert<SubIndexToReinterpretCast>(ctx); |
| 69 | + |
| 70 | + ConversionTarget target(*ctx); |
| 71 | + target.addIllegalDialect<polygeist::PolygeistDialect>(); |
| 72 | + target.addLegalDialect<arith::ArithmeticDialect, mlir::StandardOpsDialect, |
| 73 | + memref::MemRefDialect>(); |
| 74 | + |
| 75 | + if (failed(applyPartialConversion(op, target, std::move(patterns)))) |
| 76 | + return signalPassFailure(); |
| 77 | + } |
| 78 | +}; |
| 79 | +} // namespace |
| 80 | + |
| 81 | +namespace mlir { |
| 82 | +namespace polygeist { |
| 83 | +std::unique_ptr<Pass> createLowerPolygeistOpsPass() { |
| 84 | + return std::make_unique<LowerPolygeistOpsPass>(); |
| 85 | +} |
| 86 | + |
| 87 | +} // namespace polygeist |
| 88 | +} // namespace mlir |
0 commit comments