MLIR 24.0.0git
MPIOps.cpp
Go to the documentation of this file.
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
12
13using namespace mlir;
14using namespace mlir::mpi;
15
16//===----------------------------------------------------------------------===//
17// Verifiers
18//===----------------------------------------------------------------------===//
19
20LogicalResult mlir::mpi::ReduceScatterBlockOp::verify() {
21 if (getSendbuf().getType().getElementType() !=
22 getRecvbuf().getType().getElementType())
23 return emitOpError("sendbuf and recvbuf must have the same element type");
24 return success();
25}
26
27namespace {
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.
35template <typename OpT>
36struct 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
58struct 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, "MPI:comm_world_rank", b);
63 }
64};
65
66struct 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, "MPI:comm_world_size", b);
72 }
73};
74} // namespace
75
76void mlir::mpi::SendOp::getCanonicalizationPatterns(
77 mlir::RewritePatternSet &results, mlir::MLIRContext *context) {
78 results.add<FoldCast<mlir::mpi::SendOp>>(context);
79}
80
81void mlir::mpi::RecvOp::getCanonicalizationPatterns(
82 mlir::RewritePatternSet &results, mlir::MLIRContext *context) {
83 results.add<FoldCast<mlir::mpi::RecvOp>>(context);
84}
85
86void mlir::mpi::ISendOp::getCanonicalizationPatterns(
87 mlir::RewritePatternSet &results, mlir::MLIRContext *context) {
88 results.add<FoldCast<mlir::mpi::ISendOp>>(context);
89}
90
91void mlir::mpi::IRecvOp::getCanonicalizationPatterns(
92 mlir::RewritePatternSet &results, mlir::MLIRContext *context) {
93 results.add<FoldCast<mlir::mpi::IRecvOp>>(context);
94}
95
96void mlir::mpi::CommRankOp::getCanonicalizationPatterns(
97 mlir::RewritePatternSet &results, mlir::MLIRContext *context) {
98 results.add<FoldRank>(context);
99}
100
101void mlir::mpi::CommSizeOp::getCanonicalizationPatterns(
102 mlir::RewritePatternSet &results, mlir::MLIRContext *context) {
103 results.add<FoldSize>(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"
return success()
b
Return true if permutation is a valid permutation of the outer_dims_perm (case OuterOrInnerPerm::Oute...
static Type getElementType(Type type, ArrayRef< int32_t > indices, function_ref< InFlightDiagnostic(StringRef)> emitErrorFn)
Walks the given type hierarchy with the given indices, potentially down to component granularity,...
Definition SPIRVOps.cpp:229
MLIRContext is the top-level object for a collection of MLIR operations.
Definition MLIRContext.h:71
RewritePatternSet & add(ConstructorArg &&arg, ConstructorArgs &&...args)
Add an instance of each of the pattern types 'Ts' to the pattern list with the given arguments.
LogicalResult FoldToDLTIConst(OpT op, const char *key, mlir::PatternRewriter &b)
Definition Utils.h:19
Include the generated interface declarations.
Type getType(OpFoldResult ofr)
Returns the int type of the integer in ofr.
Definition Utils.cpp:311
OpRewritePattern is a wrapper around RewritePattern that allows for matching and rewriting against an...