MLIR 24.0.0git
MPIToLLVM.cpp
Go to the documentation of this file.
1//===- MPIToLLVM.cpp - MPI to LLVM dialect conversion ---------------------===//
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//
10// Copyright (C) by Argonne National Laboratory
11// See COPYRIGHT in top-level directory
12// of MPICH source repository.
13//
14
31#include <memory>
32
33using namespace mlir;
34
35namespace {
36
37template <typename Op, typename... Args>
38static Op getOrDefineGlobal(ModuleOp &moduleOp, const Location loc,
39 ConversionPatternRewriter &rewriter, StringRef name,
40 Args &&...args) {
41 Op ret;
42 if (!(ret = moduleOp.lookupSymbol<Op>(name))) {
43 ConversionPatternRewriter::InsertionGuard guard(rewriter);
44 rewriter.setInsertionPointToStart(moduleOp.getBody());
45 ret = Op::create(rewriter, loc, std::forward<Args>(args)...);
46 }
47 return ret;
48}
49
50static LLVM::LLVMFuncOp getOrDefineFunction(ModuleOp &moduleOp,
51 const Location loc,
52 ConversionPatternRewriter &rewriter,
53 StringRef name,
54 LLVM::LLVMFunctionType type) {
55 return getOrDefineGlobal<LLVM::LLVMFuncOp>(
56 moduleOp, loc, rewriter, name, name, type, LLVM::Linkage::External);
57}
58
59std::pair<Value, Value> getRawPtrAndSize(const Location loc,
60 ConversionPatternRewriter &rewriter,
61 Value memRef, int64_t rank,
62 Type elType) {
63 Type ptrType = LLVM::LLVMPointerType::get(rewriter.getContext());
64 Type i32Type = rewriter.getI32Type();
65 auto descriptorType = cast<LLVM::LLVMStructType>(memRef.getType());
66 // The offset and the sizes of a memref descriptor have the converted index
67 // type, which is not necessarily `i64`. Take it from the descriptor itself.
68 auto indexType = cast<IntegerType>(descriptorType.getBody()[2]);
69
70 Value dataPtr =
71 LLVM::ExtractValueOp::create(rewriter, loc, ptrType, memRef, 1);
72 Value offset =
73 LLVM::ExtractValueOp::create(rewriter, loc, indexType, memRef, 2);
74 Value resPtr =
75 LLVM::GEPOp::create(rewriter, loc, ptrType, elType, dataPtr, offset);
76 Value size = LLVM::ConstantOp::create(rewriter, loc, i32Type,
77 rewriter.getI32IntegerAttr(1));
78 if (descriptorType.getBody().size() > 3) {
79 for (int64_t i = 0; i < rank; ++i) {
80 Value dim = LLVM::ExtractValueOp::create(rewriter, loc, memRef,
81 ArrayRef<int64_t>{3, i});
82 // The MPI interface counts elements in an `i32`, so adjust the
83 // index-typed extent to that width. Extents are non-negative, hence the
84 // zero extension.
85 if (indexType.getWidth() > 32)
86 dim = LLVM::TruncOp::create(rewriter, loc, i32Type, dim);
87 else if (indexType.getWidth() < 32)
88 dim = LLVM::ZExtOp::create(rewriter, loc, i32Type, dim);
89 size = LLVM::MulOp::create(rewriter, loc, i32Type, dim, size);
90 }
91 }
92 return {resPtr, size};
93}
94
95/// When lowering the mpi dialect to functions calls certain details
96/// differ between various MPI implementations. This class will provide
97/// these in a generic way, depending on the MPI implementation that got
98/// selected by the DLTI attribute on the module.
99class MPIImplTraits {
100 ModuleOp &moduleOp;
101
102public:
103 /// Instantiate a new MPIImplTraits object according to the DLTI attribute
104 /// on the given module. Default to MPICH if no attribute is present or
105 /// the value is unknown.
106 static std::unique_ptr<MPIImplTraits> get(ModuleOp &moduleOp);
107
108 explicit MPIImplTraits(ModuleOp &moduleOp) : moduleOp(moduleOp) {}
109
110 virtual ~MPIImplTraits() = default;
111
112 ModuleOp &getModuleOp() { return moduleOp; }
113
114 /// Gets or creates MPI_COMM_WORLD as a Value.
115 /// Different MPI implementations have different communicator types.
116 /// Using i64 as a portable, intermediate type.
117 /// Appropriate cast needs to take place before calling MPI functions.
118 virtual Value getCommWorld(Location loc,
119 ConversionPatternRewriter &rewriter) = 0;
120
121 /// Type converter provides i64 type for communicator type.
122 /// Converts to native type, which might be ptr or int or whatever.
123 virtual Value castComm(Location loc, ConversionPatternRewriter &rewriter,
124 Value comm) = 0;
125
126 /// Get the MPI_STATUS_IGNORE value (typically a pointer type).
127 virtual intptr_t getStatusIgnore() = 0;
128
129 /// Get the MPI_IN_PLACE value (void *).
130 virtual void *getInPlace() = 0;
131
132 /// Gets or creates an MPI datatype as a value which corresponds to the given
133 /// type.
134 virtual Value getDataType(Location loc, ConversionPatternRewriter &rewriter,
135 Type type) = 0;
136
137 /// Gets or creates an MPI_Op value which corresponds to the given
138 /// enum value.
139 virtual Value getMPIOp(Location loc, ConversionPatternRewriter &rewriter,
140 mpi::MPI_ReductionOpEnum opAttr) = 0;
141};
142
143//===----------------------------------------------------------------------===//
144// Implementation details for MPICH ABI compatible MPI implementations
145//===----------------------------------------------------------------------===//
146
147class MPICHImplTraits : public MPIImplTraits {
148 static constexpr int MPI_FLOAT = 0x4c00040a;
149 static constexpr int MPI_DOUBLE = 0x4c00080b;
150 static constexpr int MPI_INT8_T = 0x4c000137;
151 static constexpr int MPI_INT16_T = 0x4c000238;
152 static constexpr int MPI_INT32_T = 0x4c000439;
153 static constexpr int MPI_INT64_T = 0x4c00083a;
154 static constexpr int MPI_UINT8_T = 0x4c00013b;
155 static constexpr int MPI_UINT16_T = 0x4c00023c;
156 static constexpr int MPI_UINT32_T = 0x4c00043d;
157 static constexpr int MPI_UINT64_T = 0x4c00083e;
158 static constexpr int MPI_MAX = 0x58000001;
159 static constexpr int MPI_MIN = 0x58000002;
160 static constexpr int MPI_SUM = 0x58000003;
161 static constexpr int MPI_PROD = 0x58000004;
162 static constexpr int MPI_LAND = 0x58000005;
163 static constexpr int MPI_BAND = 0x58000006;
164 static constexpr int MPI_LOR = 0x58000007;
165 static constexpr int MPI_BOR = 0x58000008;
166 static constexpr int MPI_LXOR = 0x58000009;
167 static constexpr int MPI_BXOR = 0x5800000a;
168 static constexpr int MPI_MINLOC = 0x5800000b;
169 static constexpr int MPI_MAXLOC = 0x5800000c;
170 static constexpr int MPI_REPLACE = 0x5800000d;
171 static constexpr int MPI_NO_OP = 0x5800000e;
172
173public:
174 using MPIImplTraits::MPIImplTraits;
175
176 ~MPICHImplTraits() override = default;
177
178 Value getCommWorld(const Location loc,
179 ConversionPatternRewriter &rewriter) override {
180 static constexpr int MPI_COMM_WORLD = 0x44000000;
181 return LLVM::ConstantOp::create(rewriter, loc, rewriter.getI64Type(),
182 MPI_COMM_WORLD);
183 }
184
185 Value castComm(const Location loc, ConversionPatternRewriter &rewriter,
186 Value comm) override {
187 return LLVM::TruncOp::create(rewriter, loc, rewriter.getI32Type(), comm);
188 }
189
190 intptr_t getStatusIgnore() override { return 1; }
191
192 void *getInPlace() override { return reinterpret_cast<void *>(-1); }
193
194 Value getDataType(const Location loc, ConversionPatternRewriter &rewriter,
195 Type type) override {
196 int32_t mtype = 0;
197 if (type.isF32())
198 mtype = MPI_FLOAT;
199 else if (type.isF64())
200 mtype = MPI_DOUBLE;
201 else if (type.isInteger(64) && !type.isUnsignedInteger())
202 mtype = MPI_INT64_T;
203 else if (type.isInteger(64))
204 mtype = MPI_UINT64_T;
205 else if (type.isInteger(32) && !type.isUnsignedInteger())
206 mtype = MPI_INT32_T;
207 else if (type.isInteger(32))
208 mtype = MPI_UINT32_T;
209 else if (type.isInteger(16) && !type.isUnsignedInteger())
210 mtype = MPI_INT16_T;
211 else if (type.isInteger(16))
212 mtype = MPI_UINT16_T;
213 else if (type.isInteger(8) && !type.isUnsignedInteger())
214 mtype = MPI_INT8_T;
215 else if (type.isInteger(8))
216 mtype = MPI_UINT8_T;
217 else
218 assert(false && "unsupported type");
219 return LLVM::ConstantOp::create(rewriter, loc, rewriter.getI32Type(),
220 mtype);
221 }
222
223 Value getMPIOp(const Location loc, ConversionPatternRewriter &rewriter,
224 mpi::MPI_ReductionOpEnum opAttr) override {
225 int32_t op = MPI_NO_OP;
226 switch (opAttr) {
227 case mpi::MPI_ReductionOpEnum::MPI_OP_NULL:
228 op = MPI_NO_OP;
229 break;
230 case mpi::MPI_ReductionOpEnum::MPI_MAX:
231 op = MPI_MAX;
232 break;
233 case mpi::MPI_ReductionOpEnum::MPI_MIN:
234 op = MPI_MIN;
235 break;
236 case mpi::MPI_ReductionOpEnum::MPI_SUM:
237 op = MPI_SUM;
238 break;
239 case mpi::MPI_ReductionOpEnum::MPI_PROD:
240 op = MPI_PROD;
241 break;
242 case mpi::MPI_ReductionOpEnum::MPI_LAND:
243 op = MPI_LAND;
244 break;
245 case mpi::MPI_ReductionOpEnum::MPI_BAND:
246 op = MPI_BAND;
247 break;
248 case mpi::MPI_ReductionOpEnum::MPI_LOR:
249 op = MPI_LOR;
250 break;
251 case mpi::MPI_ReductionOpEnum::MPI_BOR:
252 op = MPI_BOR;
253 break;
254 case mpi::MPI_ReductionOpEnum::MPI_LXOR:
255 op = MPI_LXOR;
256 break;
257 case mpi::MPI_ReductionOpEnum::MPI_BXOR:
258 op = MPI_BXOR;
259 break;
260 case mpi::MPI_ReductionOpEnum::MPI_MINLOC:
261 op = MPI_MINLOC;
262 break;
263 case mpi::MPI_ReductionOpEnum::MPI_MAXLOC:
264 op = MPI_MAXLOC;
265 break;
266 case mpi::MPI_ReductionOpEnum::MPI_REPLACE:
267 op = MPI_REPLACE;
268 break;
269 }
270 return LLVM::ConstantOp::create(rewriter, loc, rewriter.getI32Type(), op);
271 }
272};
273
274//===----------------------------------------------------------------------===//
275// Implementation details for OpenMPI
276//===----------------------------------------------------------------------===//
277class OMPIImplTraits : public MPIImplTraits {
278 LLVM::GlobalOp getOrDefineExternalStruct(const Location loc,
279 ConversionPatternRewriter &rewriter,
280 StringRef name,
281 LLVM::LLVMStructType type) {
282
283 return getOrDefineGlobal<LLVM::GlobalOp>(
284 getModuleOp(), loc, rewriter, name, type, /*isConstant=*/false,
285 LLVM::Linkage::External, name,
286 /*value=*/Attribute(), /*alignment=*/0, 0);
287 }
288
289public:
290 using MPIImplTraits::MPIImplTraits;
291
292 ~OMPIImplTraits() override = default;
293
294 Value getCommWorld(const Location loc,
295 ConversionPatternRewriter &rewriter) override {
296 auto *context = rewriter.getContext();
297 // get external opaque struct pointer type
298 auto commStructT =
299 LLVM::LLVMStructType::getOpaque("ompi_communicator_t", context);
300 StringRef name = "ompi_mpi_comm_world";
301
302 // make sure global op definition exists
303 getOrDefineExternalStruct(loc, rewriter, name, commStructT);
304
305 // get address of symbol
306 auto comm = LLVM::AddressOfOp::create(rewriter, loc,
307 LLVM::LLVMPointerType::get(context),
308 SymbolRefAttr::get(context, name));
309 return LLVM::PtrToIntOp::create(rewriter, loc, rewriter.getI64Type(), comm);
310 }
311
312 Value castComm(const Location loc, ConversionPatternRewriter &rewriter,
313 Value comm) override {
314 return LLVM::IntToPtrOp::create(
315 rewriter, loc, LLVM::LLVMPointerType::get(rewriter.getContext()), comm);
316 }
317
318 intptr_t getStatusIgnore() override { return 0; }
319
320 void *getInPlace() override { return reinterpret_cast<void *>(1); }
321
322 Value getDataType(const Location loc, ConversionPatternRewriter &rewriter,
323 Type type) override {
324 StringRef mtype;
325 if (type.isF32())
326 mtype = "ompi_mpi_float";
327 else if (type.isF64())
328 mtype = "ompi_mpi_double";
329 else if (type.isInteger(64) && !type.isUnsignedInteger())
330 mtype = "ompi_mpi_int64_t";
331 else if (type.isInteger(64))
332 mtype = "ompi_mpi_uint64_t";
333 else if (type.isInteger(32) && !type.isUnsignedInteger())
334 mtype = "ompi_mpi_int32_t";
335 else if (type.isInteger(32))
336 mtype = "ompi_mpi_uint32_t";
337 else if (type.isInteger(16) && !type.isUnsignedInteger())
338 mtype = "ompi_mpi_int16_t";
339 else if (type.isInteger(16))
340 mtype = "ompi_mpi_uint16_t";
341 else if (type.isInteger(8) && !type.isUnsignedInteger())
342 mtype = "ompi_mpi_int8_t";
343 else if (type.isInteger(8))
344 mtype = "ompi_mpi_uint8_t";
345 else
346 assert(false && "unsupported type");
347
348 auto *context = rewriter.getContext();
349 // get external opaque struct pointer type
350 auto typeStructT =
351 LLVM::LLVMStructType::getOpaque("ompi_predefined_datatype_t", context);
352 // make sure global op definition exists
353 getOrDefineExternalStruct(loc, rewriter, mtype, typeStructT);
354 // get address of symbol
355 return LLVM::AddressOfOp::create(rewriter, loc,
356 LLVM::LLVMPointerType::get(context),
357 SymbolRefAttr::get(context, mtype));
358 }
359
360 Value getMPIOp(const Location loc, ConversionPatternRewriter &rewriter,
361 mpi::MPI_ReductionOpEnum opAttr) override {
362 StringRef op;
363 switch (opAttr) {
364 case mpi::MPI_ReductionOpEnum::MPI_OP_NULL:
365 op = "ompi_mpi_no_op";
366 break;
367 case mpi::MPI_ReductionOpEnum::MPI_MAX:
368 op = "ompi_mpi_max";
369 break;
370 case mpi::MPI_ReductionOpEnum::MPI_MIN:
371 op = "ompi_mpi_min";
372 break;
373 case mpi::MPI_ReductionOpEnum::MPI_SUM:
374 op = "ompi_mpi_sum";
375 break;
376 case mpi::MPI_ReductionOpEnum::MPI_PROD:
377 op = "ompi_mpi_prod";
378 break;
379 case mpi::MPI_ReductionOpEnum::MPI_LAND:
380 op = "ompi_mpi_land";
381 break;
382 case mpi::MPI_ReductionOpEnum::MPI_BAND:
383 op = "ompi_mpi_band";
384 break;
385 case mpi::MPI_ReductionOpEnum::MPI_LOR:
386 op = "ompi_mpi_lor";
387 break;
388 case mpi::MPI_ReductionOpEnum::MPI_BOR:
389 op = "ompi_mpi_bor";
390 break;
391 case mpi::MPI_ReductionOpEnum::MPI_LXOR:
392 op = "ompi_mpi_lxor";
393 break;
394 case mpi::MPI_ReductionOpEnum::MPI_BXOR:
395 op = "ompi_mpi_bxor";
396 break;
397 case mpi::MPI_ReductionOpEnum::MPI_MINLOC:
398 op = "ompi_mpi_minloc";
399 break;
400 case mpi::MPI_ReductionOpEnum::MPI_MAXLOC:
401 op = "ompi_mpi_maxloc";
402 break;
403 case mpi::MPI_ReductionOpEnum::MPI_REPLACE:
404 op = "ompi_mpi_replace";
405 break;
406 }
407 auto *context = rewriter.getContext();
408 // get external opaque struct pointer type
409 auto opStructT =
410 LLVM::LLVMStructType::getOpaque("ompi_predefined_op_t", context);
411 // make sure global op definition exists
412 getOrDefineExternalStruct(loc, rewriter, op, opStructT);
413 // get address of symbol
414 return LLVM::AddressOfOp::create(rewriter, loc,
415 LLVM::LLVMPointerType::get(context),
416 SymbolRefAttr::get(context, op));
417 }
418};
419
420std::unique_ptr<MPIImplTraits> MPIImplTraits::get(ModuleOp &moduleOp) {
421 auto attr = dlti::query(moduleOp, {"MPI:Implementation"}, false);
422 if (failed(attr))
423 return std::make_unique<MPICHImplTraits>(moduleOp);
424 auto strAttr = dyn_cast<StringAttr>(attr.value());
425 if (strAttr && strAttr.getValue() == "OpenMPI")
426 return std::make_unique<OMPIImplTraits>(moduleOp);
427 if (!strAttr || strAttr.getValue() != "MPICH")
428 moduleOp.emitWarning() << "Unknown \"MPI:Implementation\" value in DLTI ("
429 << (strAttr ? strAttr.getValue() : "<NULL>")
430 << "), defaulting to MPICH";
431 return std::make_unique<MPICHImplTraits>(moduleOp);
432}
433
434//===----------------------------------------------------------------------===//
435// InitOpLowering
436//===----------------------------------------------------------------------===//
437
438struct InitOpLowering : public ConvertOpToLLVMPattern<mpi::InitOp> {
440
441 LogicalResult
442 matchAndRewrite(mpi::InitOp op, OpAdaptor adaptor,
443 ConversionPatternRewriter &rewriter) const override {
444 Location loc = op.getLoc();
445
446 // ptrType `!llvm.ptr`
447 Type ptrType = LLVM::LLVMPointerType::get(rewriter.getContext());
448
449 // instantiate nullptr `%nullptr = llvm.mlir.zero : !llvm.ptr`
450 auto nullPtrOp = LLVM::ZeroOp::create(rewriter, loc, ptrType);
451 Value llvmnull = nullPtrOp.getRes();
452
453 // grab a reference to the global module op:
454 auto moduleOp = op->getParentOfType<ModuleOp>();
455
456 // LLVM Function type representing `i32 MPI_Init(ptr, ptr)`
457 auto initFuncType =
458 LLVM::LLVMFunctionType::get(rewriter.getI32Type(), {ptrType, ptrType});
459 // get or create function declaration:
460 LLVM::LLVMFuncOp initDecl =
461 getOrDefineFunction(moduleOp, loc, rewriter, "MPI_Init", initFuncType);
462
463 // replace init with function call
464 rewriter.replaceOpWithNewOp<LLVM::CallOp>(op, initDecl,
465 ValueRange{llvmnull, llvmnull});
466
467 return success();
468 }
469};
470
471//===----------------------------------------------------------------------===//
472// FinalizeOpLowering
473//===----------------------------------------------------------------------===//
474
475struct FinalizeOpLowering : public ConvertOpToLLVMPattern<mpi::FinalizeOp> {
477
478 LogicalResult
479 matchAndRewrite(mpi::FinalizeOp op, OpAdaptor adaptor,
480 ConversionPatternRewriter &rewriter) const override {
481 // get loc
482 Location loc = op.getLoc();
483
484 // grab a reference to the global module op:
485 auto moduleOp = op->getParentOfType<ModuleOp>();
486
487 // LLVM Function type representing `i32 MPI_Finalize()`
488 auto initFuncType = LLVM::LLVMFunctionType::get(rewriter.getI32Type(), {});
489 // get or create function declaration:
490 LLVM::LLVMFuncOp initDecl = getOrDefineFunction(
491 moduleOp, loc, rewriter, "MPI_Finalize", initFuncType);
492
493 // replace init with function call
494 rewriter.replaceOpWithNewOp<LLVM::CallOp>(op, initDecl, ValueRange{});
495
496 return success();
497 }
498};
499
500//===----------------------------------------------------------------------===//
501// CommWorldOpLowering
502//===----------------------------------------------------------------------===//
503
504struct CommWorldOpLowering : public ConvertOpToLLVMPattern<mpi::CommWorldOp> {
506
507 LogicalResult
508 matchAndRewrite(mpi::CommWorldOp op, OpAdaptor adaptor,
509 ConversionPatternRewriter &rewriter) const override {
510 // grab a reference to the global module op:
511 auto moduleOp = op->getParentOfType<ModuleOp>();
512 auto mpiTraits = MPIImplTraits::get(moduleOp);
513 // get MPI_COMM_WORLD
514 rewriter.replaceOp(op, mpiTraits->getCommWorld(op.getLoc(), rewriter));
515
516 return success();
517 }
518};
519
520//===----------------------------------------------------------------------===//
521// CommSplitOpLowering
522//===----------------------------------------------------------------------===//
523
524struct CommSplitOpLowering : public ConvertOpToLLVMPattern<mpi::CommSplitOp> {
526
527 LogicalResult
528 matchAndRewrite(mpi::CommSplitOp op, OpAdaptor adaptor,
529 ConversionPatternRewriter &rewriter) const override {
530 // grab a reference to the global module op:
531 auto moduleOp = op->getParentOfType<ModuleOp>();
532 auto mpiTraits = MPIImplTraits::get(moduleOp);
533 Type i32 = rewriter.getI32Type();
534 Type ptrType = LLVM::LLVMPointerType::get(op->getContext());
535 Location loc = op.getLoc();
536
537 // get communicator
538 Value comm = mpiTraits->castComm(loc, rewriter, adaptor.getComm());
539 auto one = LLVM::ConstantOp::create(rewriter, loc, i32, 1);
540 auto outPtr =
541 LLVM::AllocaOp::create(rewriter, loc, ptrType, comm.getType(), one);
542
543 // int MPI_Comm_split(MPI_Comm comm, int color, int key, MPI_Comm * newcomm)
544 auto funcType =
545 LLVM::LLVMFunctionType::get(i32, {comm.getType(), i32, i32, ptrType});
546 // get or create function declaration:
547 LLVM::LLVMFuncOp funcDecl = getOrDefineFunction(moduleOp, loc, rewriter,
548 "MPI_Comm_split", funcType);
549
550 auto callOp =
551 LLVM::CallOp::create(rewriter, loc, funcDecl,
552 ValueRange{comm, adaptor.getColor(),
553 adaptor.getKey(), outPtr.getRes()});
554
555 // load the communicator into a register
556 Value res = LLVM::LoadOp::create(rewriter, loc, i32, outPtr.getResult());
557 res = LLVM::SExtOp::create(rewriter, loc, rewriter.getI64Type(), res);
558
559 // if retval is checked, replace uses of retval with the results from the
560 // call op
561 SmallVector<Value> replacements;
562 if (op.getRetval())
563 replacements.push_back(callOp.getResult());
564
565 // replace op
566 replacements.push_back(res);
567 rewriter.replaceOp(op, replacements);
568
569 return success();
570 }
571};
572
573//===----------------------------------------------------------------------===//
574// CommRankOpLowering
575//===----------------------------------------------------------------------===//
576
577struct CommRankOpLowering : public ConvertOpToLLVMPattern<mpi::CommRankOp> {
579
580 LogicalResult
581 matchAndRewrite(mpi::CommRankOp op, OpAdaptor adaptor,
582 ConversionPatternRewriter &rewriter) const override {
583 // get some helper vars
584 Location loc = op.getLoc();
585 MLIRContext *context = rewriter.getContext();
586 Type i32 = rewriter.getI32Type();
587
588 // ptrType `!llvm.ptr`
589 Type ptrType = LLVM::LLVMPointerType::get(context);
590
591 // grab a reference to the global module op:
592 auto moduleOp = op->getParentOfType<ModuleOp>();
593
594 auto mpiTraits = MPIImplTraits::get(moduleOp);
595 // get communicator
596 Value comm = mpiTraits->castComm(loc, rewriter, adaptor.getComm());
597
598 // LLVM Function type representing `i32 MPI_Comm_rank(ptr, ptr)`
599 auto rankFuncType =
600 LLVM::LLVMFunctionType::get(i32, {comm.getType(), ptrType});
601 // get or create function declaration:
602 LLVM::LLVMFuncOp initDecl = getOrDefineFunction(
603 moduleOp, loc, rewriter, "MPI_Comm_rank", rankFuncType);
604
605 // replace with function call
606 auto one = LLVM::ConstantOp::create(rewriter, loc, i32, 1);
607 auto rankptr = LLVM::AllocaOp::create(rewriter, loc, ptrType, i32, one);
608 auto callOp = LLVM::CallOp::create(rewriter, loc, initDecl,
609 ValueRange{comm, rankptr.getRes()});
610
611 // load the rank into a register
612 auto loadedRank =
613 LLVM::LoadOp::create(rewriter, loc, i32, rankptr.getResult());
614
615 // if retval is checked, replace uses of retval with the results from the
616 // call op
617 SmallVector<Value> replacements;
618 if (op.getRetval())
619 replacements.push_back(callOp.getResult());
620
621 // replace all uses, then erase op
622 replacements.push_back(loadedRank.getRes());
623 rewriter.replaceOp(op, replacements);
624
625 return success();
626 }
627};
628
629//===----------------------------------------------------------------------===//
630// CommSizeOpLowering
631//===----------------------------------------------------------------------===//
632
633static Value createOrFoldCommSize(ConversionPatternRewriter &rewriter,
634 Location loc, Value commOrg,
635 Value commAdapt) {
636 auto i32 = rewriter.getI32Type();
637 auto nRanksOp = mpi::CommSizeOp::create(rewriter, loc, i32, commOrg);
638 if (succeeded(FoldToDLTIConst(nRanksOp, "MPI:comm_world_size", rewriter)))
639 return nRanksOp.getSize();
640 rewriter.eraseOp(nRanksOp);
641 return mpi::CommSizeOp::create(rewriter, loc, i32, commAdapt).getSize();
642}
643
644struct CommSizeOpLowering : public ConvertOpToLLVMPattern<mpi::CommSizeOp> {
646
647 LogicalResult
648 matchAndRewrite(mpi::CommSizeOp op, OpAdaptor adaptor,
649 ConversionPatternRewriter &rewriter) const override {
650 // get some helper vars
651 Location loc = op.getLoc();
652 MLIRContext *context = rewriter.getContext();
653 Type i32 = rewriter.getI32Type();
654
655 // ptrType `!llvm.ptr`
656 Type ptrType = LLVM::LLVMPointerType::get(context);
657
658 // grab a reference to the global module op:
659 auto moduleOp = op->getParentOfType<ModuleOp>();
660
661 auto mpiTraits = MPIImplTraits::get(moduleOp);
662 // get communicator
663 Value comm = mpiTraits->castComm(loc, rewriter, adaptor.getComm());
664
665 // LLVM Function type representing `i32 MPI_Comm_rank(ptr, ptr)`
666 auto SizeFuncType =
667 LLVM::LLVMFunctionType::get(i32, {comm.getType(), ptrType});
668 // get or create function declaration:
669 LLVM::LLVMFuncOp initDecl = getOrDefineFunction(
670 moduleOp, loc, rewriter, "MPI_Comm_size", SizeFuncType);
671
672 // replace with function call
673 auto one = LLVM::ConstantOp::create(rewriter, loc, i32, 1);
674 auto sizeptr = LLVM::AllocaOp::create(rewriter, loc, ptrType, i32, one);
675 auto callOp = LLVM::CallOp::create(rewriter, loc, initDecl,
676 ValueRange{comm, sizeptr.getRes()});
677
678 // load the Size into a register
679 auto loadedSize =
680 LLVM::LoadOp::create(rewriter, loc, i32, sizeptr.getResult());
681
682 // if retval is checked, replace uses of retval with the results from the
683 // call op
684 SmallVector<Value> replacements;
685 if (op.getRetval())
686 replacements.push_back(callOp.getResult());
687
688 // replace all uses, then erase op
689 replacements.push_back(loadedSize.getRes());
690 rewriter.replaceOp(op, replacements);
691
692 return success();
693 }
694};
695
696//===----------------------------------------------------------------------===//
697// SendOpLowering
698//===----------------------------------------------------------------------===//
699
700struct SendOpLowering : public ConvertOpToLLVMPattern<mpi::SendOp> {
702
703 LogicalResult
704 matchAndRewrite(mpi::SendOp op, OpAdaptor adaptor,
705 ConversionPatternRewriter &rewriter) const override {
706 // get some helper vars
707 Location loc = op.getLoc();
708 MLIRContext *context = rewriter.getContext();
709 Type i32 = rewriter.getI32Type();
710 Type elemType = op.getRef().getType().getElementType();
711 int64_t rank = op.getRef().getType().getRank();
712
713 // ptrType `!llvm.ptr`
714 Type ptrType = LLVM::LLVMPointerType::get(context);
715
716 // grab a reference to the global module op:
717 auto moduleOp = op->getParentOfType<ModuleOp>();
718
719 // get MPI_COMM_WORLD, dataType and pointer
720 auto [dataPtr, size] =
721 getRawPtrAndSize(loc, rewriter, adaptor.getRef(), rank, elemType);
722 auto mpiTraits = MPIImplTraits::get(moduleOp);
723 Value dataType = mpiTraits->getDataType(loc, rewriter, elemType);
724 Value comm = mpiTraits->castComm(loc, rewriter, adaptor.getComm());
725
726 // LLVM Function type representing `i32 MPI_send(data, count, datatype, dst,
727 // tag, comm)`
728 auto funcType = LLVM::LLVMFunctionType::get(
729 i32, {ptrType, i32, dataType.getType(), i32, i32, comm.getType()});
730 // get or create function declaration:
731 LLVM::LLVMFuncOp funcDecl =
732 getOrDefineFunction(moduleOp, loc, rewriter, "MPI_Send", funcType);
733
734 // replace op with function call
735 auto funcCall = LLVM::CallOp::create(rewriter, loc, funcDecl,
736 ValueRange{dataPtr, size, dataType,
737 adaptor.getDest(),
738 adaptor.getTag(), comm});
739 if (op.getRetval())
740 rewriter.replaceOp(op, funcCall.getResult());
741 else
742 rewriter.eraseOp(op);
743
744 return success();
745 }
746};
747
748//===----------------------------------------------------------------------===//
749// RecvOpLowering
750//===----------------------------------------------------------------------===//
751
752struct RecvOpLowering : public ConvertOpToLLVMPattern<mpi::RecvOp> {
754
755 LogicalResult
756 matchAndRewrite(mpi::RecvOp op, OpAdaptor adaptor,
757 ConversionPatternRewriter &rewriter) const override {
758 // get some helper vars
759 Location loc = op.getLoc();
760 MLIRContext *context = rewriter.getContext();
761 Type i32 = rewriter.getI32Type();
762 Type i64 = rewriter.getI64Type();
763 Type elemType = op.getRef().getType().getElementType();
764 int64_t rank = op.getRef().getType().getRank();
765
766 // ptrType `!llvm.ptr`
767 Type ptrType = LLVM::LLVMPointerType::get(context);
768
769 // grab a reference to the global module op:
770 auto moduleOp = op->getParentOfType<ModuleOp>();
771
772 // get MPI_COMM_WORLD, dataType, status_ignore and pointer
773 auto [dataPtr, size] =
774 getRawPtrAndSize(loc, rewriter, adaptor.getRef(), rank, elemType);
775 auto mpiTraits = MPIImplTraits::get(moduleOp);
776 Value dataType = mpiTraits->getDataType(loc, rewriter, elemType);
777 Value comm = mpiTraits->castComm(loc, rewriter, adaptor.getComm());
778 Value statusIgnore = LLVM::ConstantOp::create(rewriter, loc, i64,
779 mpiTraits->getStatusIgnore());
780 statusIgnore =
781 LLVM::IntToPtrOp::create(rewriter, loc, ptrType, statusIgnore);
782
783 // LLVM Function type representing `i32 MPI_Recv(data, count, datatype, dst,
784 // tag, comm)`
785 auto funcType =
786 LLVM::LLVMFunctionType::get(i32, {ptrType, i32, dataType.getType(), i32,
787 i32, comm.getType(), ptrType});
788 // get or create function declaration:
789 LLVM::LLVMFuncOp funcDecl =
790 getOrDefineFunction(moduleOp, loc, rewriter, "MPI_Recv", funcType);
791
792 // replace op with function call
793 auto funcCall = LLVM::CallOp::create(
794 rewriter, loc, funcDecl,
795 ValueRange{dataPtr, size, dataType, adaptor.getSource(),
796 adaptor.getTag(), comm, statusIgnore});
797 if (op.getRetval())
798 rewriter.replaceOp(op, funcCall.getResult());
799 else
800 rewriter.eraseOp(op);
801
802 return success();
803 }
804};
805
806//===----------------------------------------------------------------------===//
807// AllGatherOpLowering
808//===----------------------------------------------------------------------===//
809
810struct AllGatherOpLowering : public ConvertOpToLLVMPattern<mpi::AllGatherOp> {
812
813 LogicalResult
814 matchAndRewrite(mpi::AllGatherOp op, OpAdaptor adaptor,
815 ConversionPatternRewriter &rewriter) const override {
816 Location loc = op.getLoc();
817 MLIRContext *context = rewriter.getContext();
818 Type sElemType = op.getSendbuf().getType().getElementType();
819 Type rElemType = op.getRecvbuf().getType().getElementType();
820 int64_t sRank = op.getSendbuf().getType().getRank();
821 int64_t rRank = op.getRecvbuf().getType().getRank();
822 auto [sendPtr, sendSize] =
823 getRawPtrAndSize(loc, rewriter, adaptor.getSendbuf(), sRank, sElemType);
824 auto [recvPtr, recvSize] =
825 getRawPtrAndSize(loc, rewriter, adaptor.getRecvbuf(), rRank, rElemType);
826
827 auto moduleOp = op->getParentOfType<ModuleOp>();
828 auto mpiTraits = MPIImplTraits::get(moduleOp);
829 Value sDataType = mpiTraits->getDataType(loc, rewriter, sElemType);
830 Value rDataType = mpiTraits->getDataType(loc, rewriter, rElemType);
831 Value comm = mpiTraits->castComm(loc, rewriter, adaptor.getComm());
832
833 Type ptrType = LLVM::LLVMPointerType::get(context);
834 Type i32 = rewriter.getI32Type();
835 // int MPI_Allgather(
836 // const void* buffer_send, int count_send, MPI_Datatype datatype_send,
837 // void* buffer_recv, int count_recv, MPI_Datatype datatype_recv,
838 // MPI_Comm communicator);
839 auto funcType = LLVM::LLVMFunctionType::get(
840 i32, {ptrType, i32, sDataType.getType(), ptrType, i32,
841 rDataType.getType(), comm.getType()});
842 // get or create function declaration:
843 LLVM::LLVMFuncOp funcDecl =
844 getOrDefineFunction(moduleOp, loc, rewriter, "MPI_Allgather", funcType);
845
846 // count_recv is the number of elements received from each rank, not total
847 Value nRanks =
848 createOrFoldCommSize(rewriter, loc, op.getComm(), adaptor.getComm());
849 Value recvCountPerRank =
850 LLVM::UDivOp::create(rewriter, loc, i32, recvSize, nRanks);
851
852 // replace op with function call
853 auto funcCall =
854 LLVM::CallOp::create(rewriter, loc, funcDecl,
855 ValueRange{sendPtr, sendSize, sDataType, recvPtr,
856 recvCountPerRank, rDataType, comm});
857
858 if (op.getRetval())
859 rewriter.replaceOp(op, funcCall.getResult());
860 else
861 rewriter.eraseOp(op);
862
863 return success();
864 }
865};
866
867//===----------------------------------------------------------------------===//
868// AllReduceOpLowering
869//===----------------------------------------------------------------------===//
870
871struct AllReduceOpLowering : public ConvertOpToLLVMPattern<mpi::AllReduceOp> {
873
874 LogicalResult
875 matchAndRewrite(mpi::AllReduceOp op, OpAdaptor adaptor,
876 ConversionPatternRewriter &rewriter) const override {
877 Location loc = op.getLoc();
878 MLIRContext *context = rewriter.getContext();
879 Type i32 = rewriter.getI32Type();
880 Type i64 = rewriter.getI64Type();
881 Type elemType = op.getSendbuf().getType().getElementType();
882 int64_t sRank = op.getSendbuf().getType().getRank();
883 int64_t rRank = op.getRecvbuf().getType().getRank();
884
885 // ptrType `!llvm.ptr`
886 Type ptrType = LLVM::LLVMPointerType::get(context);
887 auto moduleOp = op->getParentOfType<ModuleOp>();
888 auto mpiTraits = MPIImplTraits::get(moduleOp);
889 auto [sendPtr, sendSize] =
890 getRawPtrAndSize(loc, rewriter, adaptor.getSendbuf(), sRank, elemType);
891 auto [recvPtr, recvSize] =
892 getRawPtrAndSize(loc, rewriter, adaptor.getRecvbuf(), rRank, elemType);
893
894 // If input and output are the same, request in-place operation.
895 if (adaptor.getSendbuf() == adaptor.getRecvbuf()) {
896 sendPtr = LLVM::ConstantOp::create(
897 rewriter, loc, i64,
898 reinterpret_cast<int64_t>(mpiTraits->getInPlace()));
899 sendPtr = LLVM::IntToPtrOp::create(rewriter, loc, ptrType, sendPtr);
900 }
901
902 Value dataType = mpiTraits->getDataType(loc, rewriter, elemType);
903 Value mpiOp = mpiTraits->getMPIOp(loc, rewriter, op.getOp());
904 Value commWorld = mpiTraits->castComm(loc, rewriter, adaptor.getComm());
905
906 // 'int MPI_Allreduce(const void *sendbuf, void *recvbuf, int count,
907 // MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)'
908 auto funcType = LLVM::LLVMFunctionType::get(
909 i32, {ptrType, ptrType, i32, dataType.getType(), mpiOp.getType(),
910 commWorld.getType()});
911 // get or create function declaration:
912 LLVM::LLVMFuncOp funcDecl =
913 getOrDefineFunction(moduleOp, loc, rewriter, "MPI_Allreduce", funcType);
914
915 // replace op with function call
916 auto funcCall = LLVM::CallOp::create(
917 rewriter, loc, funcDecl,
918 ValueRange{sendPtr, recvPtr, sendSize, dataType, mpiOp, commWorld});
919
920 if (op.getRetval())
921 rewriter.replaceOp(op, funcCall.getResult());
922 else
923 rewriter.eraseOp(op);
924
925 return success();
926 }
927};
928
929//===----------------------------------------------------------------------===//
930// ReduceScatterBlockOpLowering
931//===----------------------------------------------------------------------===//
932
933struct ReduceScatterBlockOpLowering
934 : public ConvertOpToLLVMPattern<mpi::ReduceScatterBlockOp> {
936
937 LogicalResult
938 matchAndRewrite(mpi::ReduceScatterBlockOp op, OpAdaptor adaptor,
939 ConversionPatternRewriter &rewriter) const override {
940 Location loc = op.getLoc();
941 MLIRContext *context = rewriter.getContext();
942 Type i32 = rewriter.getI32Type();
943 Type i64 = rewriter.getI64Type();
944 Type elemType = op.getSendbuf().getType().getElementType();
945 int64_t sRank = op.getSendbuf().getType().getRank();
946 int64_t rRank = op.getRecvbuf().getType().getRank();
947
948 // ptrType `!llvm.ptr`
949 Type ptrType = LLVM::LLVMPointerType::get(context);
950 auto moduleOp = op->getParentOfType<ModuleOp>();
951 auto mpiTraits = MPIImplTraits::get(moduleOp);
952 auto [sendPtr, sendSize] =
953 getRawPtrAndSize(loc, rewriter, adaptor.getSendbuf(), sRank, elemType);
954 auto [recvPtr, recvSize] =
955 getRawPtrAndSize(loc, rewriter, adaptor.getRecvbuf(), rRank, elemType);
956
957 // If input and output are the same, request in-place operation.
958 if (adaptor.getSendbuf() == adaptor.getRecvbuf()) {
959 sendPtr = LLVM::ConstantOp::create(
960 rewriter, loc, i64,
961 reinterpret_cast<int64_t>(mpiTraits->getInPlace()));
962 sendPtr = LLVM::IntToPtrOp::create(rewriter, loc, ptrType, sendPtr);
963 }
964
965 Value dataType = mpiTraits->getDataType(loc, rewriter, elemType);
966 Value mpiOp = mpiTraits->getMPIOp(loc, rewriter, op.getOp());
967 Value comm = mpiTraits->castComm(loc, rewriter, adaptor.getComm());
968
969 Value nRanks =
970 createOrFoldCommSize(rewriter, loc, op.getComm(), adaptor.getComm());
971 Value totalExpected =
972 LLVM::MulOp::create(rewriter, loc, i32, recvSize, nRanks);
973 Value sizeIsValid = LLVM::ICmpOp::create(
974 rewriter, loc, LLVM::ICmpPredicate::eq, sendSize, totalExpected);
975 cf::AssertOp::create(rewriter, loc, sizeIsValid,
976 "Send buffer's size must be the receive buffer's size "
977 "times the number of ranks");
978
979 // 'int MPI_Reduce_scatter_block(const void *sendbuf, void *recvbuf,
980 // int recvcount, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)'
981 auto funcType = LLVM::LLVMFunctionType::get(
982 i32, {ptrType, ptrType, i32, dataType.getType(), mpiOp.getType(),
983 comm.getType()});
984 // get or create function declaration:
985 LLVM::LLVMFuncOp funcDecl = getOrDefineFunction(
986 moduleOp, loc, rewriter, "MPI_Reduce_scatter_block", funcType);
987
988 // replace op with function call
989 auto funcCall = LLVM::CallOp::create(
990 rewriter, loc, funcDecl,
991 ValueRange{sendPtr, recvPtr, recvSize, dataType, mpiOp, comm});
992
993 if (op.getRetval())
994 rewriter.replaceOp(op, funcCall.getResult());
995 else
996 rewriter.eraseOp(op);
997
998 return success();
999 }
1000};
1001
1002//===----------------------------------------------------------------------===//
1003// ConvertToLLVMPatternInterface implementation
1004//===----------------------------------------------------------------------===//
1005
1006/// Implement the interface to convert Func to LLVM.
1007struct FuncToLLVMDialectInterface : public ConvertToLLVMPatternInterface {
1008 FuncToLLVMDialectInterface(Dialect *dialect)
1009 : ConvertToLLVMPatternInterface(dialect) {}
1010
1011 /// Hook for derived dialect interface to provide conversion patterns
1012 /// and mark dialect legal for the conversion target.
1013 void populateConvertToLLVMConversionPatterns(
1014 ConversionTarget &target, LLVMTypeConverter &typeConverter,
1015 RewritePatternSet &patterns) const final {
1016 mpi::populateMPIToLLVMConversionPatterns(typeConverter, patterns);
1017 }
1018};
1019} // namespace
1020
1021//===----------------------------------------------------------------------===//
1022// Pattern Population
1023//===----------------------------------------------------------------------===//
1024
1026 RewritePatternSet &patterns) {
1027 // Using i64 as a portable, intermediate type for !mpi.comm.
1028 // It would be nicer to somehow get the right type directly, but TLDI is not
1029 // available here.
1030 converter.addConversion([](mpi::CommType type) {
1031 return IntegerType::get(type.getContext(), 64);
1032 });
1033 patterns.add<CommRankOpLowering, CommSizeOpLowering, CommSplitOpLowering,
1034 CommWorldOpLowering, FinalizeOpLowering, InitOpLowering,
1035 SendOpLowering, RecvOpLowering, AllGatherOpLowering,
1036 AllReduceOpLowering, ReduceScatterBlockOpLowering>(converter);
1037}
1038
1040 registry.addExtension(+[](MLIRContext *ctx, mpi::MPIDialect *dialect) {
1041 dialect->addInterfaces<FuncToLLVMDialectInterface>();
1042 });
1043}
return success()
ConvertOpToLLVMPattern(const LLVMTypeConverter &typeConverter, PatternBenefit benefit=1)
Definition Pattern.h:239
The DialectRegistry maps a dialect namespace to a constructor for the matching dialect.
bool addExtension(TypeID extensionID, std::unique_ptr< DialectExtensionBase > extension)
Add the given extension to the registry.
Conversion from types to the LLVM IR dialect.
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition Location.h:76
MLIRContext is the top-level object for a collection of MLIR operations.
Definition MLIRContext.h:71
This provides public APIs that all operations should have.
RewritePatternSet & add(ConstructorArg &&arg, ConstructorArgs &&...args)
Add an instance of each of the pattern types 'Ts' to the pattern list with the given arguments.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition Types.h:74
bool isF64() const
Definition Types.cpp:41
bool isF32() const
Definition Types.cpp:40
bool isUnsignedInteger() const
Return true if this is an unsigned integer type (with the specified width).
Definition Types.cpp:90
bool isInteger() const
Return true if this is an integer type (with the specified width).
Definition Types.cpp:58
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition Value.h:96
Type getType() const
Return the type of this value.
Definition Value.h:105
FailureOr< Attribute > query(Operation *op, ArrayRef< DataLayoutEntryKey > keys, bool emitError=false)
Query the first key at op or the nearest ancestor that can answer it.
Definition DLTI.cpp:579
LogicalResult FoldToDLTIConst(OpT op, const char *key, mlir::PatternRewriter &b)
Definition Utils.h:19
void populateMPIToLLVMConversionPatterns(LLVMTypeConverter &converter, RewritePatternSet &patterns)
void registerConvertMPIToLLVMInterface(DialectRegistry &registry)
detail::InFlightRemark failed(Location loc, RemarkOpts opts)
Report an optimization remark that failed.
Definition Remarks.h:886
Include the generated interface declarations.
LLVM::LLVMFuncOp getOrDefineFunction(Operation *moduleOp, Location loc, OpBuilder &b, StringRef name, LLVM::LLVMFunctionType type)
Note that these functions don't take a SymbolTable because GPU module lowerings can have name collisi...
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...