Skip to content

[mlir][mesh, MPI] Mesh2mpi #104566

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions mlir/include/mlir/Conversion/MeshToMPI/MeshToMPI.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//===- MeshToMPI.h - Convert Mesh to MPI dialect ----------------*- C++ -*-===//
//
// 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
//
//===----------------------------------------------------------------------===//

#ifndef MLIR_CONVERSION_MESHTOMPI_MESHTOMPI_H
#define MLIR_CONVERSION_MESHTOMPI_MESHTOMPI_H

#include "mlir/Pass/Pass.h"
#include "mlir/Support/LLVM.h"

namespace mlir {
class Pass;

#define GEN_PASS_DECL_CONVERTMESHTOMPIPASS
#include "mlir/Conversion/Passes.h.inc"

/// Lowers Mesh communication operations (updateHalo, AllGater, ...)
/// to MPI primitives.
std::unique_ptr<::mlir::Pass> createConvertMeshToMPIPass();

} // namespace mlir

#endif // MLIR_CONVERSION_MESHTOMPI_MESHTOMPI_H
1 change: 1 addition & 0 deletions mlir/include/mlir/Conversion/Passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
#include "mlir/Conversion/MemRefToEmitC/MemRefToEmitCPass.h"
#include "mlir/Conversion/MemRefToLLVM/MemRefToLLVM.h"
#include "mlir/Conversion/MemRefToSPIRV/MemRefToSPIRVPass.h"
#include "mlir/Conversion/MeshToMPI/MeshToMPI.h"
#include "mlir/Conversion/NVGPUToNVVM/NVGPUToNVVM.h"
#include "mlir/Conversion/NVVMToLLVM/NVVMToLLVM.h"
#include "mlir/Conversion/OpenACCToSCF/ConvertOpenACCToSCF.h"
Expand Down
23 changes: 23 additions & 0 deletions mlir/include/mlir/Conversion/Passes.td
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,29 @@ def ConvertMemRefToSPIRV : Pass<"convert-memref-to-spirv"> {
];
}

//===----------------------------------------------------------------------===//
// MeshToMPI
//===----------------------------------------------------------------------===//

def ConvertMeshToMPIPass : Pass<"convert-mesh-to-mpi"> {
let summary = "Convert Mesh dialect to MPI dialect.";
let constructor = "mlir::createConvertMeshToMPIPass()";
let description = [{
This pass converts communication operations from the Mesh dialect to the
MPI dialect.
If it finds a global named "static_mpi_rank" it will use that splat value
instead of calling MPI_Comm_rank. This allows optimizations like constant
shape propagation and fusion because shard/partition sizes depend on the
rank.
}];
let dependentDialects = [
"memref::MemRefDialect",
"mpi::MPIDialect",
"scf::SCFDialect",
"bufferization::BufferizationDialect"
];
}

//===----------------------------------------------------------------------===//
// NVVMToLLVM
//===----------------------------------------------------------------------===//
Expand Down
2 changes: 2 additions & 0 deletions mlir/include/mlir/Dialect/MPI/IR/MPIOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def MPI_SendOp : MPI_Op<"send", []> {
let assemblyFormat = "`(` $ref `,` $tag `,` $rank `)` attr-dict `:` "
"type($ref) `,` type($tag) `,` type($rank)"
"(`->` type($retval)^)?";
let hasCanonicalizer = 1;
}

//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -114,6 +115,7 @@ def MPI_RecvOp : MPI_Op<"recv", []> {
let assemblyFormat = "`(` $ref `,` $tag `,` $rank `)` attr-dict `:` "
"type($ref) `,` type($tag) `,` type($rank)"
"(`->` type($retval)^)?";
let hasCanonicalizer = 1;
}


Expand Down
54 changes: 42 additions & 12 deletions mlir/include/mlir/Dialect/Mesh/IR/MeshOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,40 @@ def Mesh_ProcessLinearIndexOp : Mesh_Op<"process_linear_index", [
];
}

def Mesh_NeighborsLinearIndicesOp : Mesh_Op<"neighbors_linear_indices", [
Pure,
DeclareOpInterfaceMethods<SymbolUserOpInterface>,
DeclareOpInterfaceMethods<OpAsmOpInterface, ["getAsmResultNames"]>
]> {
let summary =
"For given mesh index get the linear indices of the direct neighbor processes along the given split.";
let description = [{
Example:
```
mesh.mesh @mesh0(shape = 10x20x30)
%c1 = arith.constant 1 : index
%c2 = arith.constant 2 : index
%c3 = arith.constant 3 : index
%idx = mesh.neighbors_linear_indices on @mesh[%c1, %c2, %c3] split_axes = [1] : index
```
The above returns two indices, `633` and `693`, which correspond to the
index of the previous process `(1, 1, 3)`, and the next process
`(1, 3, 3) along the split axis `1`.

A negative value is returned if there is no neighbor in the respective
direction along the given `split_axes`.
}];
let arguments = (ins FlatSymbolRefAttr:$mesh,
Variadic<Index>:$device,
Mesh_MeshAxesAttr:$split_axes);
let results = (outs Index:$neighbor_down, Index:$neighbor_up);
let assemblyFormat = [{
`on` $mesh `[` $device `]`
`split_axes` `=` $split_axes
attr-dict `:` type(results)
}];
}

//===----------------------------------------------------------------------===//
// Sharding operations.
//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -1058,12 +1092,12 @@ def Mesh_ShiftOp : Mesh_CollectiveCommunicationOpBase<"shift", [
}

def Mesh_UpdateHaloOp : Mesh_Op<"update_halo", [
Pure,
DestinationStyleOpInterface,
TypesMatchWith<
"result has same type as destination",
"result", "destination", "$_self">,
DeclareOpInterfaceMethods<SymbolUserOpInterface>,
AttrSizedOperandSegments
DeclareOpInterfaceMethods<SymbolUserOpInterface>
]> {
let summary = "Update halo data.";
let description = [{
Expand All @@ -1072,7 +1106,7 @@ def Mesh_UpdateHaloOp : Mesh_Op<"update_halo", [
on the remote devices. Changes might be caused by mutating operations
and/or if the new halo regions are larger than the existing ones.

Source and destination might have different halo sizes.
Destination is supposed to be initialized with the local data (not halos).

Assumes all devices hold tensors with same-sized halo data as specified
by `source_halo_sizes/static_source_halo_sizes` and
Expand All @@ -1084,25 +1118,21 @@ def Mesh_UpdateHaloOp : Mesh_Op<"update_halo", [

}];
let arguments = (ins
AnyTypeOf<[AnyNon0RankedMemRef, AnyNon0RankedTensor]>:$source,
AnyTypeOf<[AnyNon0RankedMemRef, AnyNon0RankedTensor]>:$destination,
FlatSymbolRefAttr:$mesh,
Mesh_MeshAxesArrayAttr:$split_axes,
Variadic<I64>:$source_halo_sizes,
DefaultValuedAttr<DenseI64ArrayAttr, "{}">:$static_source_halo_sizes,
Variadic<I64>:$destination_halo_sizes,
DefaultValuedAttr<DenseI64ArrayAttr, "{}">:$static_destination_halo_sizes
Variadic<I64>:$halo_sizes,
DefaultValuedAttr<DenseI64ArrayAttr, "{}">:$static_halo_sizes
);
let results = (outs
AnyTypeOf<[AnyNon0RankedMemRef, AnyNon0RankedTensor]>:$result
);
let assemblyFormat = [{
$source `into` $destination
$destination
`on` $mesh
`split_axes` `=` $split_axes
(`source_halo_sizes` `=` custom<DynamicIndexList>($source_halo_sizes, $static_source_halo_sizes)^)?
(`destination_halo_sizes` `=` custom<DynamicIndexList>($destination_halo_sizes, $static_destination_halo_sizes)^)?
attr-dict `:` type($source) `->` type($result)
(`halo_sizes` `=` custom<DynamicIndexList>($halo_sizes, $static_halo_sizes)^)?
attr-dict `:` type($result)
}];
let extraClassDeclaration = [{
MutableOperandRange getDpsInitsMutable() { return getDestinationMutable(); }
Expand Down
1 change: 1 addition & 0 deletions mlir/lib/Conversion/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ add_subdirectory(MathToSPIRV)
add_subdirectory(MemRefToEmitC)
add_subdirectory(MemRefToLLVM)
add_subdirectory(MemRefToSPIRV)
add_subdirectory(MeshToMPI)
add_subdirectory(NVGPUToNVVM)
add_subdirectory(NVVMToLLVM)
add_subdirectory(OpenACCToSCF)
Expand Down
22 changes: 22 additions & 0 deletions mlir/lib/Conversion/MeshToMPI/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
add_mlir_conversion_library(MLIRMeshToMPI
MeshToMPI.cpp

ADDITIONAL_HEADER_DIRS
${MLIR_MAIN_INCLUDE_DIR}/mlir/Conversion/MeshToMPI

DEPENDS
MLIRConversionPassIncGen

LINK_COMPONENTS
Core

LINK_LIBS PUBLIC
MLIRFuncDialect
MLIRIR
MLIRLinalgTransforms
MLIRMemRefDialect
MLIRPass
MLIRMeshDialect
MLIRMPIDialect
MLIRTransforms
)
Loading