Skip to content

Commit 586c5e3

Browse files
authored
[mlir][mpi] fixing in-place and 0d mpi.all_reduce (llvm#134225)
* inplace allreduce needs special MPI token MPI_IN_PLACE as send buffer * 0d tensors have no sizes/strides in LLVM memref struct
1 parent 6bbdc70 commit 586c5e3

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

mlir/lib/Conversion/MPIToLLVM/MPIToLLVM.cpp

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
#include "mlir/Conversion/MPIToLLVM/MPIToLLVM.h"
1616
#include "mlir/Conversion/ConvertToLLVM/ToLLVMInterface.h"
1717
#include "mlir/Conversion/LLVMCommon/Pattern.h"
18+
#include "mlir/Dialect/Arith/IR/Arith.h"
1819
#include "mlir/Dialect/DLTI/DLTI.h"
1920
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
21+
#include "mlir/Dialect/LLVMIR/LLVMTypes.h"
2022
#include "mlir/Dialect/MPI/IR/MPI.h"
2123
#include "mlir/Transforms/DialectConversion.h"
2224
#include <memory>
@@ -57,9 +59,14 @@ std::pair<Value, Value> getRawPtrAndSize(const Location loc,
5759
loc, rewriter.getI64Type(), memRef, 2);
5860
Value resPtr =
5961
rewriter.create<LLVM::GEPOp>(loc, ptrType, elType, dataPtr, offset);
60-
Value size = rewriter.create<LLVM::ExtractValueOp>(loc, memRef,
61-
ArrayRef<int64_t>{3, 0});
62-
size = rewriter.create<LLVM::TruncOp>(loc, rewriter.getI32Type(), size);
62+
Value size;
63+
if (cast<LLVM::LLVMStructType>(memRef.getType()).getBody().size() > 3) {
64+
size = rewriter.create<LLVM::ExtractValueOp>(loc, memRef,
65+
ArrayRef<int64_t>{3, 0});
66+
size = rewriter.create<LLVM::TruncOp>(loc, rewriter.getI32Type(), size);
67+
} else {
68+
size = rewriter.create<arith::ConstantIntOp>(loc, 1, 32);
69+
}
6370
return {resPtr, size};
6471
}
6572

@@ -97,6 +104,9 @@ class MPIImplTraits {
97104
/// Get the MPI_STATUS_IGNORE value (typically a pointer type).
98105
virtual intptr_t getStatusIgnore() = 0;
99106

107+
/// Get the MPI_IN_PLACE value (void *).
108+
virtual void *getInPlace() = 0;
109+
100110
/// Gets or creates an MPI datatype as a value which corresponds to the given
101111
/// type.
102112
virtual Value getDataType(const Location loc,
@@ -158,6 +168,8 @@ class MPICHImplTraits : public MPIImplTraits {
158168

159169
intptr_t getStatusIgnore() override { return 1; }
160170

171+
void *getInPlace() override { return reinterpret_cast<void *>(-1); }
172+
161173
Value getDataType(const Location loc, ConversionPatternRewriter &rewriter,
162174
Type type) override {
163175
int32_t mtype = 0;
@@ -283,6 +295,8 @@ class OMPIImplTraits : public MPIImplTraits {
283295

284296
intptr_t getStatusIgnore() override { return 0; }
285297

298+
void *getInPlace() override { return reinterpret_cast<void *>(1); }
299+
286300
Value getDataType(const Location loc, ConversionPatternRewriter &rewriter,
287301
Type type) override {
288302
StringRef mtype;
@@ -516,7 +530,8 @@ struct CommSplitOpLowering : public ConvertOpToLLVMPattern<mpi::CommSplitOp> {
516530
outPtr.getRes()});
517531

518532
// load the communicator into a register
519-
auto res = rewriter.create<LLVM::LoadOp>(loc, i32, outPtr.getResult());
533+
Value res = rewriter.create<LLVM::LoadOp>(loc, i32, outPtr.getResult());
534+
res = rewriter.create<LLVM::SExtOp>(loc, rewriter.getI64Type(), res);
520535

521536
// if retval is checked, replace uses of retval with the results from the
522537
// call op
@@ -525,7 +540,7 @@ struct CommSplitOpLowering : public ConvertOpToLLVMPattern<mpi::CommSplitOp> {
525540
replacements.push_back(callOp.getResult());
526541

527542
// replace op
528-
replacements.push_back(res.getRes());
543+
replacements.push_back(res);
529544
rewriter.replaceOp(op, replacements);
530545

531546
return success();
@@ -709,6 +724,7 @@ struct AllReduceOpLowering : public ConvertOpToLLVMPattern<mpi::AllReduceOp> {
709724
Location loc = op.getLoc();
710725
MLIRContext *context = rewriter.getContext();
711726
Type i32 = rewriter.getI32Type();
727+
Type i64 = rewriter.getI64Type();
712728
Type elemType = op.getSendbuf().getType().getElementType();
713729

714730
// ptrType `!llvm.ptr`
@@ -719,6 +735,14 @@ struct AllReduceOpLowering : public ConvertOpToLLVMPattern<mpi::AllReduceOp> {
719735
getRawPtrAndSize(loc, rewriter, adaptor.getSendbuf(), elemType);
720736
auto [recvPtr, recvSize] =
721737
getRawPtrAndSize(loc, rewriter, adaptor.getRecvbuf(), elemType);
738+
739+
// If input and output are the same, request in-place operation.
740+
if (adaptor.getSendbuf() == adaptor.getRecvbuf()) {
741+
sendPtr = rewriter.create<LLVM::ConstantOp>(
742+
loc, i64, reinterpret_cast<int64_t>(mpiTraits->getInPlace()));
743+
sendPtr = rewriter.create<LLVM::IntToPtrOp>(loc, ptrType, sendPtr);
744+
}
745+
722746
Value dataType = mpiTraits->getDataType(loc, rewriter, elemType);
723747
Value mpiOp = mpiTraits->getMPIOp(loc, rewriter, op.getOp());
724748
Value commWorld = mpiTraits->castComm(loc, rewriter, adaptor.getComm());

mlir/test/Conversion/MPIToLLVM/mpitollvm.mlir

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,12 @@ module attributes {dlti.map = #dlti.map<"MPI:Implementation" = "MPICH">} {
9898
// CHECK: [[v66:%.*]] = llvm.getelementptr [[v64]][[[v65]]] : (!llvm.ptr, i64) -> !llvm.ptr, f32
9999
// CHECK: [[v67:%.*]] = llvm.extractvalue [[v5]][3, 0] : !llvm.struct<(ptr, ptr, i64, array<1 x i64>, array<1 x i64>)>
100100
// CHECK: [[v68:%.*]] = llvm.trunc [[v67]] : i64 to i32
101+
// CHECK: [[ip:%.*]] = llvm.mlir.constant(-1 : i64) : i64
102+
// CHECK: [[ipp:%.*]] = llvm.inttoptr [[ip]] : i64 to !llvm.ptr
101103
// CHECK: [[v69:%.*]] = llvm.mlir.constant(1275069450 : i32) : i32
102104
// CHECK: [[v70:%.*]] = llvm.mlir.constant(1476395011 : i32) : i32
103105
// CHECK: [[v71:%.*]] = llvm.trunc [[comm]] : i64 to i32
104-
// CHECK: [[v72:%.*]] = llvm.call @MPI_Allreduce([[v61]], [[v66]], [[v63]], [[v69]], [[v70]], [[v71]]) : (!llvm.ptr, !llvm.ptr, i32, i32, i32, i32) -> i32
106+
// CHECK: [[v72:%.*]] = llvm.call @MPI_Allreduce([[ipp]], [[v66]], [[v63]], [[v69]], [[v70]], [[v71]]) : (!llvm.ptr, !llvm.ptr, i32, i32, i32, i32) -> i32
105107
mpi.allreduce(%arg0, %arg0, MPI_SUM, %comm) : memref<100xf32>, memref<100xf32>
106108

107109
// CHECK: llvm.call @MPI_Finalize() : () -> i32
@@ -202,10 +204,12 @@ module attributes { dlti.map = #dlti.map<"MPI:Implementation" = "OpenMPI"> } {
202204
// CHECK: [[v56:%.*]] = llvm.getelementptr [[v54]][[[v55]]] : (!llvm.ptr, i64) -> !llvm.ptr, f32
203205
// CHECK: [[v57:%.*]] = llvm.extractvalue [[v5]][3, 0] : !llvm.struct<(ptr, ptr, i64, array<1 x i64>, array<1 x i64>)>
204206
// CHECK: [[v58:%.*]] = llvm.trunc [[v57]] : i64 to i32
207+
// CHECK: [[ip:%.*]] = llvm.mlir.constant(1 : i64) : i64
208+
// CHECK: [[ipp:%.*]] = llvm.inttoptr [[ip]] : i64 to !llvm.ptr
205209
// CHECK: [[v59:%.*]] = llvm.mlir.addressof @ompi_mpi_float : !llvm.ptr
206210
// CHECK: [[v60:%.*]] = llvm.mlir.addressof @ompi_mpi_sum : !llvm.ptr
207211
// CHECK: [[v61:%.*]] = llvm.inttoptr [[comm]] : i64 to !llvm.ptr
208-
// CHECK: [[v62:%.*]] = llvm.call @MPI_Allreduce([[v51]], [[v56]], [[v53]], [[v59]], [[v60]], [[v61]]) : (!llvm.ptr, !llvm.ptr, i32, !llvm.ptr, !llvm.ptr, !llvm.ptr) -> i32
212+
// CHECK: [[v62:%.*]] = llvm.call @MPI_Allreduce([[ipp]], [[v56]], [[v53]], [[v59]], [[v60]], [[v61]]) : (!llvm.ptr, !llvm.ptr, i32, !llvm.ptr, !llvm.ptr, !llvm.ptr) -> i32
209213
mpi.allreduce(%arg0, %arg0, MPI_SUM, %comm) : memref<100xf32>, memref<100xf32>
210214

211215
// CHECK: [[v71:%.*]] = llvm.mlir.constant(10 : i32) : i32

0 commit comments

Comments
 (0)