| 1 | //===- MPIOps.cpp - MPI dialect ops implementation ------------------------===// |
| 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 | #include "mlir/Dialect/MPI/IR/MPI.h" |
| 10 | #include "mlir/Dialect/MPI/IR/Utils.h" |
| 11 | #include "mlir/Dialect/MemRef/IR/MemRef.h" |
| 12 | |
| 13 | using namespace mlir; |
| 14 | using namespace mlir::mpi; |
| 15 | |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | // Verifiers |
| 18 | //===----------------------------------------------------------------------===// |
| 19 | |
| 20 | LogicalResult mlir::mpi::ReduceScatterBlockOp::verify() { |
| 21 | if (getSendbuf().getType().getElementType() != |
| 22 | getRecvbuf().getType().getElementType()) |
| 23 | return emitOpError(message: "sendbuf and recvbuf must have the same element type" ); |
| 24 | return success(); |
| 25 | } |
| 26 | |
| 27 | namespace { |
| 28 | |
| 29 | //===----------------------------------------------------------------------===// |
| 30 | // Canonicalization patterns |
| 31 | //===----------------------------------------------------------------------===// |
| 32 | |
| 33 | // If input memref has dynamic shape and is a cast and if the cast's input has |
| 34 | // static shape, fold the cast's static input into the given operation. |
| 35 | template <typename OpT> |
| 36 | struct FoldCast final : public mlir::OpRewritePattern<OpT> { |
| 37 | using mlir::OpRewritePattern<OpT>::OpRewritePattern; |
| 38 | |
| 39 | LogicalResult matchAndRewrite(OpT op, |
| 40 | mlir::PatternRewriter &b) const override { |
| 41 | auto mRef = op.getRef(); |
| 42 | if (mRef.getType().hasStaticShape()) { |
| 43 | return mlir::failure(); |
| 44 | } |
| 45 | auto defOp = mRef.getDefiningOp(); |
| 46 | if (!defOp || !mlir::isa<mlir::memref::CastOp>(defOp)) { |
| 47 | return mlir::failure(); |
| 48 | } |
| 49 | auto src = mlir::cast<mlir::memref::CastOp>(defOp).getSource(); |
| 50 | if (!src.getType().hasStaticShape()) { |
| 51 | return mlir::failure(); |
| 52 | } |
| 53 | b.modifyOpInPlace(op, [&]() { op.getRefMutable().assign(src); }); |
| 54 | return mlir::success(); |
| 55 | } |
| 56 | }; |
| 57 | |
| 58 | struct FoldRank final : public mlir::OpRewritePattern<mlir::mpi::CommRankOp> { |
| 59 | using mlir::OpRewritePattern<mlir::mpi::CommRankOp>::OpRewritePattern; |
| 60 | LogicalResult matchAndRewrite(mlir::mpi::CommRankOp op, |
| 61 | mlir::PatternRewriter &b) const override { |
| 62 | return FoldToDLTIConst(op, key: "MPI:comm_world_rank" , b); |
| 63 | } |
| 64 | }; |
| 65 | |
| 66 | struct FoldSize final : public mlir::OpRewritePattern<mlir::mpi::CommSizeOp> { |
| 67 | using mlir::OpRewritePattern<mlir::mpi::CommSizeOp>::OpRewritePattern; |
| 68 | |
| 69 | LogicalResult matchAndRewrite(mlir::mpi::CommSizeOp op, |
| 70 | mlir::PatternRewriter &b) const override { |
| 71 | return FoldToDLTIConst(op, key: "MPI:comm_world_size" , b); |
| 72 | } |
| 73 | }; |
| 74 | } // namespace |
| 75 | |
| 76 | void mlir::mpi::SendOp::getCanonicalizationPatterns( |
| 77 | mlir::RewritePatternSet &results, mlir::MLIRContext *context) { |
| 78 | results.add<FoldCast<mlir::mpi::SendOp>>(arg&: context); |
| 79 | } |
| 80 | |
| 81 | void mlir::mpi::RecvOp::getCanonicalizationPatterns( |
| 82 | mlir::RewritePatternSet &results, mlir::MLIRContext *context) { |
| 83 | results.add<FoldCast<mlir::mpi::RecvOp>>(arg&: context); |
| 84 | } |
| 85 | |
| 86 | void mlir::mpi::ISendOp::getCanonicalizationPatterns( |
| 87 | mlir::RewritePatternSet &results, mlir::MLIRContext *context) { |
| 88 | results.add<FoldCast<mlir::mpi::ISendOp>>(arg&: context); |
| 89 | } |
| 90 | |
| 91 | void mlir::mpi::IRecvOp::getCanonicalizationPatterns( |
| 92 | mlir::RewritePatternSet &results, mlir::MLIRContext *context) { |
| 93 | results.add<FoldCast<mlir::mpi::IRecvOp>>(arg&: context); |
| 94 | } |
| 95 | |
| 96 | void mlir::mpi::CommRankOp::getCanonicalizationPatterns( |
| 97 | mlir::RewritePatternSet &results, mlir::MLIRContext *context) { |
| 98 | results.add<FoldRank>(arg&: context); |
| 99 | } |
| 100 | |
| 101 | void mlir::mpi::CommSizeOp::getCanonicalizationPatterns( |
| 102 | mlir::RewritePatternSet &results, mlir::MLIRContext *context) { |
| 103 | results.add<FoldSize>(arg&: context); |
| 104 | } |
| 105 | |
| 106 | //===----------------------------------------------------------------------===// |
| 107 | // TableGen'd op method definitions |
| 108 | //===----------------------------------------------------------------------===// |
| 109 | |
| 110 | #define GET_OP_CLASSES |
| 111 | #include "mlir/Dialect/MPI/IR/MPIOps.cpp.inc" |
| 112 | |