1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
|
//===- MPIToLLVM.cpp - MPI to LLVM dialect conversion ---------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// Copyright (C) by Argonne National Laboratory
// See COPYRIGHT in top-level directory
// of MPICH source repository.
//
#include "mlir/Conversion/MPIToLLVM/MPIToLLVM.h"
#include "mlir/Conversion/ConvertToLLVM/ToLLVMInterface.h"
#include "mlir/Conversion/LLVMCommon/Pattern.h"
#include "mlir/Dialect/Arith/IR/Arith.h"
#include "mlir/Dialect/DLTI/DLTI.h"
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
#include "mlir/Dialect/LLVMIR/LLVMTypes.h"
#include "mlir/Dialect/MPI/IR/MPI.h"
#include "mlir/Transforms/DialectConversion.h"
#include <memory>
using namespace mlir;
namespace {
template <typename Op, typename... Args>
static Op getOrDefineGlobal(ModuleOp &moduleOp, const Location loc,
ConversionPatternRewriter &rewriter, StringRef name,
Args &&...args) {
Op ret;
if (!(ret = moduleOp.lookupSymbol<Op>(name))) {
ConversionPatternRewriter::InsertionGuard guard(rewriter);
rewriter.setInsertionPointToStart(moduleOp.getBody());
ret = Op::create(rewriter, loc, std::forward<Args>(args)...);
}
return ret;
}
static LLVM::LLVMFuncOp getOrDefineFunction(ModuleOp &moduleOp,
const Location loc,
ConversionPatternRewriter &rewriter,
StringRef name,
LLVM::LLVMFunctionType type) {
return getOrDefineGlobal<LLVM::LLVMFuncOp>(
moduleOp, loc, rewriter, name, name, type, LLVM::Linkage::External);
}
std::pair<Value, Value> getRawPtrAndSize(const Location loc,
ConversionPatternRewriter &rewriter,
Value memRef, Type elType) {
Type ptrType = LLVM::LLVMPointerType::get(rewriter.getContext());
Value dataPtr =
LLVM::ExtractValueOp::create(rewriter, loc, ptrType, memRef, 1);
Value offset = LLVM::ExtractValueOp::create(rewriter, loc,
rewriter.getI64Type(), memRef, 2);
Value resPtr =
LLVM::GEPOp::create(rewriter, loc, ptrType, elType, dataPtr, offset);
Value size;
if (cast<LLVM::LLVMStructType>(memRef.getType()).getBody().size() > 3) {
size = LLVM::ExtractValueOp::create(rewriter, loc, memRef,
ArrayRef<int64_t>{3, 0});
size = LLVM::TruncOp::create(rewriter, loc, rewriter.getI32Type(), size);
} else {
size = arith::ConstantIntOp::create(rewriter, loc, 1, 32);
}
return {resPtr, size};
}
/// When lowering the mpi dialect to functions calls certain details
/// differ between various MPI implementations. This class will provide
/// these in a generic way, depending on the MPI implementation that got
/// selected by the DLTI attribute on the module.
class MPIImplTraits {
ModuleOp &moduleOp;
public:
/// Instantiate a new MPIImplTraits object according to the DLTI attribute
/// on the given module. Default to MPICH if no attribute is present or
/// the value is unknown.
static std::unique_ptr<MPIImplTraits> get(ModuleOp &moduleOp);
explicit MPIImplTraits(ModuleOp &moduleOp) : moduleOp(moduleOp) {}
virtual ~MPIImplTraits() = default;
ModuleOp &getModuleOp() { return moduleOp; }
/// Gets or creates MPI_COMM_WORLD as a Value.
/// Different MPI implementations have different communicator types.
/// Using i64 as a portable, intermediate type.
/// Appropriate cast needs to take place before calling MPI functions.
virtual Value getCommWorld(const Location loc,
ConversionPatternRewriter &rewriter) = 0;
/// Type converter provides i64 type for communicator type.
/// Converts to native type, which might be ptr or int or whatever.
virtual Value castComm(const Location loc,
ConversionPatternRewriter &rewriter, Value comm) = 0;
/// Get the MPI_STATUS_IGNORE value (typically a pointer type).
virtual intptr_t getStatusIgnore() = 0;
/// Get the MPI_IN_PLACE value (void *).
virtual void *getInPlace() = 0;
/// Gets or creates an MPI datatype as a value which corresponds to the given
/// type.
virtual Value getDataType(const Location loc,
ConversionPatternRewriter &rewriter, Type type) = 0;
/// Gets or creates an MPI_Op value which corresponds to the given
/// enum value.
virtual Value getMPIOp(const Location loc,
ConversionPatternRewriter &rewriter,
mpi::MPI_ReductionOpEnum opAttr) = 0;
};
//===----------------------------------------------------------------------===//
// Implementation details for MPICH ABI compatible MPI implementations
//===----------------------------------------------------------------------===//
class MPICHImplTraits : public MPIImplTraits {
static constexpr int MPI_FLOAT = 0x4c00040a;
static constexpr int MPI_DOUBLE = 0x4c00080b;
static constexpr int MPI_INT8_T = 0x4c000137;
static constexpr int MPI_INT16_T = 0x4c000238;
static constexpr int MPI_INT32_T = 0x4c000439;
static constexpr int MPI_INT64_T = 0x4c00083a;
static constexpr int MPI_UINT8_T = 0x4c00013b;
static constexpr int MPI_UINT16_T = 0x4c00023c;
static constexpr int MPI_UINT32_T = 0x4c00043d;
static constexpr int MPI_UINT64_T = 0x4c00083e;
static constexpr int MPI_MAX = 0x58000001;
static constexpr int MPI_MIN = 0x58000002;
static constexpr int MPI_SUM = 0x58000003;
static constexpr int MPI_PROD = 0x58000004;
static constexpr int MPI_LAND = 0x58000005;
static constexpr int MPI_BAND = 0x58000006;
static constexpr int MPI_LOR = 0x58000007;
static constexpr int MPI_BOR = 0x58000008;
static constexpr int MPI_LXOR = 0x58000009;
static constexpr int MPI_BXOR = 0x5800000a;
static constexpr int MPI_MINLOC = 0x5800000b;
static constexpr int MPI_MAXLOC = 0x5800000c;
static constexpr int MPI_REPLACE = 0x5800000d;
static constexpr int MPI_NO_OP = 0x5800000e;
public:
using MPIImplTraits::MPIImplTraits;
~MPICHImplTraits() override = default;
Value getCommWorld(const Location loc,
ConversionPatternRewriter &rewriter) override {
static constexpr int MPI_COMM_WORLD = 0x44000000;
return LLVM::ConstantOp::create(rewriter, loc, rewriter.getI64Type(),
MPI_COMM_WORLD);
}
Value castComm(const Location loc, ConversionPatternRewriter &rewriter,
Value comm) override {
return LLVM::TruncOp::create(rewriter, loc, rewriter.getI32Type(), comm);
}
intptr_t getStatusIgnore() override { return 1; }
void *getInPlace() override { return reinterpret_cast<void *>(-1); }
Value getDataType(const Location loc, ConversionPatternRewriter &rewriter,
Type type) override {
int32_t mtype = 0;
if (type.isF32())
mtype = MPI_FLOAT;
else if (type.isF64())
mtype = MPI_DOUBLE;
else if (type.isInteger(64) && !type.isUnsignedInteger())
mtype = MPI_INT64_T;
else if (type.isInteger(64))
mtype = MPI_UINT64_T;
else if (type.isInteger(32) && !type.isUnsignedInteger())
mtype = MPI_INT32_T;
else if (type.isInteger(32))
mtype = MPI_UINT32_T;
else if (type.isInteger(16) && !type.isUnsignedInteger())
mtype = MPI_INT16_T;
else if (type.isInteger(16))
mtype = MPI_UINT16_T;
else if (type.isInteger(8) && !type.isUnsignedInteger())
mtype = MPI_INT8_T;
else if (type.isInteger(8))
mtype = MPI_UINT8_T;
else
assert(false && "unsupported type");
return LLVM::ConstantOp::create(rewriter, loc, rewriter.getI32Type(),
mtype);
}
Value getMPIOp(const Location loc, ConversionPatternRewriter &rewriter,
mpi::MPI_ReductionOpEnum opAttr) override {
int32_t op = MPI_NO_OP;
switch (opAttr) {
case mpi::MPI_ReductionOpEnum::MPI_OP_NULL:
op = MPI_NO_OP;
break;
case mpi::MPI_ReductionOpEnum::MPI_MAX:
op = MPI_MAX;
break;
case mpi::MPI_ReductionOpEnum::MPI_MIN:
op = MPI_MIN;
break;
case mpi::MPI_ReductionOpEnum::MPI_SUM:
op = MPI_SUM;
break;
case mpi::MPI_ReductionOpEnum::MPI_PROD:
op = MPI_PROD;
break;
case mpi::MPI_ReductionOpEnum::MPI_LAND:
op = MPI_LAND;
break;
case mpi::MPI_ReductionOpEnum::MPI_BAND:
op = MPI_BAND;
break;
case mpi::MPI_ReductionOpEnum::MPI_LOR:
op = MPI_LOR;
break;
case mpi::MPI_ReductionOpEnum::MPI_BOR:
op = MPI_BOR;
break;
case mpi::MPI_ReductionOpEnum::MPI_LXOR:
op = MPI_LXOR;
break;
case mpi::MPI_ReductionOpEnum::MPI_BXOR:
op = MPI_BXOR;
break;
case mpi::MPI_ReductionOpEnum::MPI_MINLOC:
op = MPI_MINLOC;
break;
case mpi::MPI_ReductionOpEnum::MPI_MAXLOC:
op = MPI_MAXLOC;
break;
case mpi::MPI_ReductionOpEnum::MPI_REPLACE:
op = MPI_REPLACE;
break;
}
return LLVM::ConstantOp::create(rewriter, loc, rewriter.getI32Type(), op);
}
};
//===----------------------------------------------------------------------===//
// Implementation details for OpenMPI
//===----------------------------------------------------------------------===//
class OMPIImplTraits : public MPIImplTraits {
LLVM::GlobalOp getOrDefineExternalStruct(const Location loc,
ConversionPatternRewriter &rewriter,
StringRef name,
LLVM::LLVMStructType type) {
return getOrDefineGlobal<LLVM::GlobalOp>(
getModuleOp(), loc, rewriter, name, type, /*isConstant=*/false,
LLVM::Linkage::External, name,
/*value=*/Attribute(), /*alignment=*/0, 0);
}
public:
using MPIImplTraits::MPIImplTraits;
~OMPIImplTraits() override = default;
Value getCommWorld(const Location loc,
ConversionPatternRewriter &rewriter) override {
auto context = rewriter.getContext();
// get external opaque struct pointer type
auto commStructT =
LLVM::LLVMStructType::getOpaque("ompi_communicator_t", context);
StringRef name = "ompi_mpi_comm_world";
// make sure global op definition exists
getOrDefineExternalStruct(loc, rewriter, name, commStructT);
// get address of symbol
auto comm = LLVM::AddressOfOp::create(rewriter, loc,
LLVM::LLVMPointerType::get(context),
SymbolRefAttr::get(context, name));
return LLVM::PtrToIntOp::create(rewriter, loc, rewriter.getI64Type(), comm);
}
Value castComm(const Location loc, ConversionPatternRewriter &rewriter,
Value comm) override {
return LLVM::IntToPtrOp::create(
rewriter, loc, LLVM::LLVMPointerType::get(rewriter.getContext()), comm);
}
intptr_t getStatusIgnore() override { return 0; }
void *getInPlace() override { return reinterpret_cast<void *>(1); }
Value getDataType(const Location loc, ConversionPatternRewriter &rewriter,
Type type) override {
StringRef mtype;
if (type.isF32())
mtype = "ompi_mpi_float";
else if (type.isF64())
mtype = "ompi_mpi_double";
else if (type.isInteger(64) && !type.isUnsignedInteger())
mtype = "ompi_mpi_int64_t";
else if (type.isInteger(64))
mtype = "ompi_mpi_uint64_t";
else if (type.isInteger(32) && !type.isUnsignedInteger())
mtype = "ompi_mpi_int32_t";
else if (type.isInteger(32))
mtype = "ompi_mpi_uint32_t";
else if (type.isInteger(16) && !type.isUnsignedInteger())
mtype = "ompi_mpi_int16_t";
else if (type.isInteger(16))
mtype = "ompi_mpi_uint16_t";
else if (type.isInteger(8) && !type.isUnsignedInteger())
mtype = "ompi_mpi_int8_t";
else if (type.isInteger(8))
mtype = "ompi_mpi_uint8_t";
else
assert(false && "unsupported type");
auto context = rewriter.getContext();
// get external opaque struct pointer type
auto typeStructT =
LLVM::LLVMStructType::getOpaque("ompi_predefined_datatype_t", context);
// make sure global op definition exists
getOrDefineExternalStruct(loc, rewriter, mtype, typeStructT);
// get address of symbol
return LLVM::AddressOfOp::create(rewriter, loc,
LLVM::LLVMPointerType::get(context),
SymbolRefAttr::get(context, mtype));
}
Value getMPIOp(const Location loc, ConversionPatternRewriter &rewriter,
mpi::MPI_ReductionOpEnum opAttr) override {
StringRef op;
switch (opAttr) {
case mpi::MPI_ReductionOpEnum::MPI_OP_NULL:
op = "ompi_mpi_no_op";
break;
case mpi::MPI_ReductionOpEnum::MPI_MAX:
op = "ompi_mpi_max";
break;
case mpi::MPI_ReductionOpEnum::MPI_MIN:
op = "ompi_mpi_min";
break;
case mpi::MPI_ReductionOpEnum::MPI_SUM:
op = "ompi_mpi_sum";
break;
case mpi::MPI_ReductionOpEnum::MPI_PROD:
op = "ompi_mpi_prod";
break;
case mpi::MPI_ReductionOpEnum::MPI_LAND:
op = "ompi_mpi_land";
break;
case mpi::MPI_ReductionOpEnum::MPI_BAND:
op = "ompi_mpi_band";
break;
case mpi::MPI_ReductionOpEnum::MPI_LOR:
op = "ompi_mpi_lor";
break;
case mpi::MPI_ReductionOpEnum::MPI_BOR:
op = "ompi_mpi_bor";
break;
case mpi::MPI_ReductionOpEnum::MPI_LXOR:
op = "ompi_mpi_lxor";
break;
case mpi::MPI_ReductionOpEnum::MPI_BXOR:
op = "ompi_mpi_bxor";
break;
case mpi::MPI_ReductionOpEnum::MPI_MINLOC:
op = "ompi_mpi_minloc";
break;
case mpi::MPI_ReductionOpEnum::MPI_MAXLOC:
op = "ompi_mpi_maxloc";
break;
case mpi::MPI_ReductionOpEnum::MPI_REPLACE:
op = "ompi_mpi_replace";
break;
}
auto context = rewriter.getContext();
// get external opaque struct pointer type
auto opStructT =
LLVM::LLVMStructType::getOpaque("ompi_predefined_op_t", context);
// make sure global op definition exists
getOrDefineExternalStruct(loc, rewriter, op, opStructT);
// get address of symbol
return LLVM::AddressOfOp::create(rewriter, loc,
LLVM::LLVMPointerType::get(context),
SymbolRefAttr::get(context, op));
}
};
std::unique_ptr<MPIImplTraits> MPIImplTraits::get(ModuleOp &moduleOp) {
auto attr = dlti::query(*&moduleOp, {"MPI:Implementation"}, true);
if (failed(attr))
return std::make_unique<MPICHImplTraits>(moduleOp);
auto strAttr = dyn_cast<StringAttr>(attr.value());
if (strAttr && strAttr.getValue() == "OpenMPI")
return std::make_unique<OMPIImplTraits>(moduleOp);
if (!strAttr || strAttr.getValue() != "MPICH")
moduleOp.emitWarning() << "Unknown \"MPI:Implementation\" value in DLTI ("
<< strAttr.getValue() << "), defaulting to MPICH";
return std::make_unique<MPICHImplTraits>(moduleOp);
}
//===----------------------------------------------------------------------===//
// InitOpLowering
//===----------------------------------------------------------------------===//
struct InitOpLowering : public ConvertOpToLLVMPattern<mpi::InitOp> {
using ConvertOpToLLVMPattern::ConvertOpToLLVMPattern;
LogicalResult
matchAndRewrite(mpi::InitOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
Location loc = op.getLoc();
// ptrType `!llvm.ptr`
Type ptrType = LLVM::LLVMPointerType::get(rewriter.getContext());
// instantiate nullptr `%nullptr = llvm.mlir.zero : !llvm.ptr`
auto nullPtrOp = LLVM::ZeroOp::create(rewriter, loc, ptrType);
Value llvmnull = nullPtrOp.getRes();
// grab a reference to the global module op:
auto moduleOp = op->getParentOfType<ModuleOp>();
// LLVM Function type representing `i32 MPI_Init(ptr, ptr)`
auto initFuncType =
LLVM::LLVMFunctionType::get(rewriter.getI32Type(), {ptrType, ptrType});
// get or create function declaration:
LLVM::LLVMFuncOp initDecl =
getOrDefineFunction(moduleOp, loc, rewriter, "MPI_Init", initFuncType);
// replace init with function call
rewriter.replaceOpWithNewOp<LLVM::CallOp>(op, initDecl,
ValueRange{llvmnull, llvmnull});
return success();
}
};
//===----------------------------------------------------------------------===//
// FinalizeOpLowering
//===----------------------------------------------------------------------===//
struct FinalizeOpLowering : public ConvertOpToLLVMPattern<mpi::FinalizeOp> {
using ConvertOpToLLVMPattern::ConvertOpToLLVMPattern;
LogicalResult
matchAndRewrite(mpi::FinalizeOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
// get loc
Location loc = op.getLoc();
// grab a reference to the global module op:
auto moduleOp = op->getParentOfType<ModuleOp>();
// LLVM Function type representing `i32 MPI_Finalize()`
auto initFuncType = LLVM::LLVMFunctionType::get(rewriter.getI32Type(), {});
// get or create function declaration:
LLVM::LLVMFuncOp initDecl = getOrDefineFunction(
moduleOp, loc, rewriter, "MPI_Finalize", initFuncType);
// replace init with function call
rewriter.replaceOpWithNewOp<LLVM::CallOp>(op, initDecl, ValueRange{});
return success();
}
};
//===----------------------------------------------------------------------===//
// CommWorldOpLowering
//===----------------------------------------------------------------------===//
struct CommWorldOpLowering : public ConvertOpToLLVMPattern<mpi::CommWorldOp> {
using ConvertOpToLLVMPattern::ConvertOpToLLVMPattern;
LogicalResult
matchAndRewrite(mpi::CommWorldOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
// grab a reference to the global module op:
auto moduleOp = op->getParentOfType<ModuleOp>();
auto mpiTraits = MPIImplTraits::get(moduleOp);
// get MPI_COMM_WORLD
rewriter.replaceOp(op, mpiTraits->getCommWorld(op.getLoc(), rewriter));
return success();
}
};
//===----------------------------------------------------------------------===//
// CommSplitOpLowering
//===----------------------------------------------------------------------===//
struct CommSplitOpLowering : public ConvertOpToLLVMPattern<mpi::CommSplitOp> {
using ConvertOpToLLVMPattern::ConvertOpToLLVMPattern;
LogicalResult
matchAndRewrite(mpi::CommSplitOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
// grab a reference to the global module op:
auto moduleOp = op->getParentOfType<ModuleOp>();
auto mpiTraits = MPIImplTraits::get(moduleOp);
Type i32 = rewriter.getI32Type();
Type ptrType = LLVM::LLVMPointerType::get(op->getContext());
Location loc = op.getLoc();
// get communicator
Value comm = mpiTraits->castComm(loc, rewriter, adaptor.getComm());
auto one = LLVM::ConstantOp::create(rewriter, loc, i32, 1);
auto outPtr =
LLVM::AllocaOp::create(rewriter, loc, ptrType, comm.getType(), one);
// int MPI_Comm_split(MPI_Comm comm, int color, int key, MPI_Comm * newcomm)
auto funcType =
LLVM::LLVMFunctionType::get(i32, {comm.getType(), i32, i32, ptrType});
// get or create function declaration:
LLVM::LLVMFuncOp funcDecl = getOrDefineFunction(moduleOp, loc, rewriter,
"MPI_Comm_split", funcType);
auto callOp =
LLVM::CallOp::create(rewriter, loc, funcDecl,
ValueRange{comm, adaptor.getColor(),
adaptor.getKey(), outPtr.getRes()});
// load the communicator into a register
Value res = LLVM::LoadOp::create(rewriter, loc, i32, outPtr.getResult());
res = LLVM::SExtOp::create(rewriter, loc, rewriter.getI64Type(), res);
// if retval is checked, replace uses of retval with the results from the
// call op
SmallVector<Value> replacements;
if (op.getRetval())
replacements.push_back(callOp.getResult());
// replace op
replacements.push_back(res);
rewriter.replaceOp(op, replacements);
return success();
}
};
//===----------------------------------------------------------------------===//
// CommRankOpLowering
//===----------------------------------------------------------------------===//
struct CommRankOpLowering : public ConvertOpToLLVMPattern<mpi::CommRankOp> {
using ConvertOpToLLVMPattern::ConvertOpToLLVMPattern;
LogicalResult
matchAndRewrite(mpi::CommRankOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
// get some helper vars
Location loc = op.getLoc();
MLIRContext *context = rewriter.getContext();
Type i32 = rewriter.getI32Type();
// ptrType `!llvm.ptr`
Type ptrType = LLVM::LLVMPointerType::get(context);
// grab a reference to the global module op:
auto moduleOp = op->getParentOfType<ModuleOp>();
auto mpiTraits = MPIImplTraits::get(moduleOp);
// get communicator
Value comm = mpiTraits->castComm(loc, rewriter, adaptor.getComm());
// LLVM Function type representing `i32 MPI_Comm_rank(ptr, ptr)`
auto rankFuncType =
LLVM::LLVMFunctionType::get(i32, {comm.getType(), ptrType});
// get or create function declaration:
LLVM::LLVMFuncOp initDecl = getOrDefineFunction(
moduleOp, loc, rewriter, "MPI_Comm_rank", rankFuncType);
// replace with function call
auto one = LLVM::ConstantOp::create(rewriter, loc, i32, 1);
auto rankptr = LLVM::AllocaOp::create(rewriter, loc, ptrType, i32, one);
auto callOp = LLVM::CallOp::create(rewriter, loc, initDecl,
ValueRange{comm, rankptr.getRes()});
// load the rank into a register
auto loadedRank =
LLVM::LoadOp::create(rewriter, loc, i32, rankptr.getResult());
// if retval is checked, replace uses of retval with the results from the
// call op
SmallVector<Value> replacements;
if (op.getRetval())
replacements.push_back(callOp.getResult());
// replace all uses, then erase op
replacements.push_back(loadedRank.getRes());
rewriter.replaceOp(op, replacements);
return success();
}
};
//===----------------------------------------------------------------------===//
// SendOpLowering
//===----------------------------------------------------------------------===//
struct SendOpLowering : public ConvertOpToLLVMPattern<mpi::SendOp> {
using ConvertOpToLLVMPattern::ConvertOpToLLVMPattern;
LogicalResult
matchAndRewrite(mpi::SendOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
// get some helper vars
Location loc = op.getLoc();
MLIRContext *context = rewriter.getContext();
Type i32 = rewriter.getI32Type();
Type elemType = op.getRef().getType().getElementType();
// ptrType `!llvm.ptr`
Type ptrType = LLVM::LLVMPointerType::get(context);
// grab a reference to the global module op:
auto moduleOp = op->getParentOfType<ModuleOp>();
// get MPI_COMM_WORLD, dataType and pointer
auto [dataPtr, size] =
getRawPtrAndSize(loc, rewriter, adaptor.getRef(), elemType);
auto mpiTraits = MPIImplTraits::get(moduleOp);
Value dataType = mpiTraits->getDataType(loc, rewriter, elemType);
Value comm = mpiTraits->castComm(loc, rewriter, adaptor.getComm());
// LLVM Function type representing `i32 MPI_send(data, count, datatype, dst,
// tag, comm)`
auto funcType = LLVM::LLVMFunctionType::get(
i32, {ptrType, i32, dataType.getType(), i32, i32, comm.getType()});
// get or create function declaration:
LLVM::LLVMFuncOp funcDecl =
getOrDefineFunction(moduleOp, loc, rewriter, "MPI_Send", funcType);
// replace op with function call
auto funcCall = LLVM::CallOp::create(rewriter, loc, funcDecl,
ValueRange{dataPtr, size, dataType,
adaptor.getDest(),
adaptor.getTag(), comm});
if (op.getRetval())
rewriter.replaceOp(op, funcCall.getResult());
else
rewriter.eraseOp(op);
return success();
}
};
//===----------------------------------------------------------------------===//
// RecvOpLowering
//===----------------------------------------------------------------------===//
struct RecvOpLowering : public ConvertOpToLLVMPattern<mpi::RecvOp> {
using ConvertOpToLLVMPattern::ConvertOpToLLVMPattern;
LogicalResult
matchAndRewrite(mpi::RecvOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
// get some helper vars
Location loc = op.getLoc();
MLIRContext *context = rewriter.getContext();
Type i32 = rewriter.getI32Type();
Type i64 = rewriter.getI64Type();
Type elemType = op.getRef().getType().getElementType();
// ptrType `!llvm.ptr`
Type ptrType = LLVM::LLVMPointerType::get(context);
// grab a reference to the global module op:
auto moduleOp = op->getParentOfType<ModuleOp>();
// get MPI_COMM_WORLD, dataType, status_ignore and pointer
auto [dataPtr, size] =
getRawPtrAndSize(loc, rewriter, adaptor.getRef(), elemType);
auto mpiTraits = MPIImplTraits::get(moduleOp);
Value dataType = mpiTraits->getDataType(loc, rewriter, elemType);
Value comm = mpiTraits->castComm(loc, rewriter, adaptor.getComm());
Value statusIgnore = LLVM::ConstantOp::create(rewriter, loc, i64,
mpiTraits->getStatusIgnore());
statusIgnore =
LLVM::IntToPtrOp::create(rewriter, loc, ptrType, statusIgnore);
// LLVM Function type representing `i32 MPI_Recv(data, count, datatype, dst,
// tag, comm)`
auto funcType =
LLVM::LLVMFunctionType::get(i32, {ptrType, i32, dataType.getType(), i32,
i32, comm.getType(), ptrType});
// get or create function declaration:
LLVM::LLVMFuncOp funcDecl =
getOrDefineFunction(moduleOp, loc, rewriter, "MPI_Recv", funcType);
// replace op with function call
auto funcCall = LLVM::CallOp::create(
rewriter, loc, funcDecl,
ValueRange{dataPtr, size, dataType, adaptor.getSource(),
adaptor.getTag(), comm, statusIgnore});
if (op.getRetval())
rewriter.replaceOp(op, funcCall.getResult());
else
rewriter.eraseOp(op);
return success();
}
};
//===----------------------------------------------------------------------===//
// AllReduceOpLowering
//===----------------------------------------------------------------------===//
struct AllReduceOpLowering : public ConvertOpToLLVMPattern<mpi::AllReduceOp> {
using ConvertOpToLLVMPattern::ConvertOpToLLVMPattern;
LogicalResult
matchAndRewrite(mpi::AllReduceOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
Location loc = op.getLoc();
MLIRContext *context = rewriter.getContext();
Type i32 = rewriter.getI32Type();
Type i64 = rewriter.getI64Type();
Type elemType = op.getSendbuf().getType().getElementType();
// ptrType `!llvm.ptr`
Type ptrType = LLVM::LLVMPointerType::get(context);
auto moduleOp = op->getParentOfType<ModuleOp>();
auto mpiTraits = MPIImplTraits::get(moduleOp);
auto [sendPtr, sendSize] =
getRawPtrAndSize(loc, rewriter, adaptor.getSendbuf(), elemType);
auto [recvPtr, recvSize] =
getRawPtrAndSize(loc, rewriter, adaptor.getRecvbuf(), elemType);
// If input and output are the same, request in-place operation.
if (adaptor.getSendbuf() == adaptor.getRecvbuf()) {
sendPtr = LLVM::ConstantOp::create(
rewriter, loc, i64,
reinterpret_cast<int64_t>(mpiTraits->getInPlace()));
sendPtr = LLVM::IntToPtrOp::create(rewriter, loc, ptrType, sendPtr);
}
Value dataType = mpiTraits->getDataType(loc, rewriter, elemType);
Value mpiOp = mpiTraits->getMPIOp(loc, rewriter, op.getOp());
Value commWorld = mpiTraits->castComm(loc, rewriter, adaptor.getComm());
// 'int MPI_Allreduce(const void *sendbuf, void *recvbuf, int count,
// MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)'
auto funcType = LLVM::LLVMFunctionType::get(
i32, {ptrType, ptrType, i32, dataType.getType(), mpiOp.getType(),
commWorld.getType()});
// get or create function declaration:
LLVM::LLVMFuncOp funcDecl =
getOrDefineFunction(moduleOp, loc, rewriter, "MPI_Allreduce", funcType);
// replace op with function call
auto funcCall = LLVM::CallOp::create(
rewriter, loc, funcDecl,
ValueRange{sendPtr, recvPtr, sendSize, dataType, mpiOp, commWorld});
if (op.getRetval())
rewriter.replaceOp(op, funcCall.getResult());
else
rewriter.eraseOp(op);
return success();
}
};
//===----------------------------------------------------------------------===//
// ConvertToLLVMPatternInterface implementation
//===----------------------------------------------------------------------===//
/// Implement the interface to convert Func to LLVM.
struct FuncToLLVMDialectInterface : public ConvertToLLVMPatternInterface {
using ConvertToLLVMPatternInterface::ConvertToLLVMPatternInterface;
/// Hook for derived dialect interface to provide conversion patterns
/// and mark dialect legal for the conversion target.
void populateConvertToLLVMConversionPatterns(
ConversionTarget &target, LLVMTypeConverter &typeConverter,
RewritePatternSet &patterns) const final {
mpi::populateMPIToLLVMConversionPatterns(typeConverter, patterns);
}
};
} // namespace
//===----------------------------------------------------------------------===//
// Pattern Population
//===----------------------------------------------------------------------===//
void mpi::populateMPIToLLVMConversionPatterns(LLVMTypeConverter &converter,
RewritePatternSet &patterns) {
// Using i64 as a portable, intermediate type for !mpi.comm.
// It would be nicer to somehow get the right type directly, but TLDI is not
// available here.
converter.addConversion([](mpi::CommType type) {
return IntegerType::get(type.getContext(), 64);
});
patterns.add<CommRankOpLowering, CommSplitOpLowering, CommWorldOpLowering,
FinalizeOpLowering, InitOpLowering, SendOpLowering,
RecvOpLowering, AllReduceOpLowering>(converter);
}
void mpi::registerConvertMPIToLLVMInterface(DialectRegistry ®istry) {
registry.addExtension(+[](MLIRContext *ctx, mpi::MPIDialect *dialect) {
dialect->addInterfaces<FuncToLLVMDialectInterface>();
});
}
|