|
| 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 | +#include "mlir/Conversion/MPIToLLVM/MPIToLLVM.h" |
| 10 | + |
| 11 | +#include "mlir/Conversion/LLVMCommon/ConversionTarget.h" |
| 12 | +#include "mlir/Conversion/LLVMCommon/Pattern.h" |
| 13 | +#include "mlir/Dialect/LLVMIR/LLVMDialect.h" |
| 14 | +#include "mlir/Dialect/MPI/IR/MPI.h" |
| 15 | +#include "mlir/Pass/Pass.h" |
| 16 | + |
| 17 | +namespace mlir { |
| 18 | +#define GEN_PASS_DEF_MPITOLLVMCONVERSIONPASS |
| 19 | +#include "mlir/Conversion/Passes.h.inc" |
| 20 | +} // namespace mlir |
| 21 | + |
| 22 | +using namespace mlir; |
| 23 | + |
| 24 | +namespace { |
| 25 | + |
| 26 | +struct InitOpLowering : ConvertOpToLLVMPattern<mpi::InitOp> { |
| 27 | + using ConvertOpToLLVMPattern::ConvertOpToLLVMPattern; |
| 28 | + |
| 29 | + LogicalResult |
| 30 | + matchAndRewrite(mpi::InitOp op, OpAdaptor adaptor, |
| 31 | + ConversionPatternRewriter &rewriter) const override; |
| 32 | +}; |
| 33 | + |
| 34 | +struct CommRankOpLowering : ConvertOpToLLVMPattern<mpi::CommRankOp> { |
| 35 | + using ConvertOpToLLVMPattern::ConvertOpToLLVMPattern; |
| 36 | + |
| 37 | + LogicalResult |
| 38 | + matchAndRewrite(mpi::CommRankOp op, OpAdaptor adaptor, |
| 39 | + ConversionPatternRewriter &rewriter) const override; |
| 40 | +}; |
| 41 | + |
| 42 | +struct FinalizeOpLowering : ConvertOpToLLVMPattern<mpi::FinalizeOp> { |
| 43 | + using ConvertOpToLLVMPattern::ConvertOpToLLVMPattern; |
| 44 | + |
| 45 | + LogicalResult |
| 46 | + matchAndRewrite(mpi::FinalizeOp op, OpAdaptor adaptor, |
| 47 | + ConversionPatternRewriter &rewriter) const override; |
| 48 | +}; |
| 49 | + |
| 50 | +// TODO: this was copied from GPUOpsLowering.cpp:288 |
| 51 | +// is this okay, or should this be moved to some common file? |
| 52 | +LLVM::LLVMFuncOp getOrDefineFunction(ModuleOp &moduleOp, const Location loc, |
| 53 | + ConversionPatternRewriter &rewriter, |
| 54 | + StringRef name, |
| 55 | + LLVM::LLVMFunctionType type) { |
| 56 | + LLVM::LLVMFuncOp ret; |
| 57 | + if (!(ret = moduleOp.lookupSymbol<LLVM::LLVMFuncOp>(name))) { |
| 58 | + ConversionPatternRewriter::InsertionGuard guard(rewriter); |
| 59 | + rewriter.setInsertionPointToStart(moduleOp.getBody()); |
| 60 | + ret = rewriter.create<LLVM::LLVMFuncOp>(loc, name, type, |
| 61 | + LLVM::Linkage::External); |
| 62 | + } |
| 63 | + return ret; |
| 64 | +} |
| 65 | + |
| 66 | +// TODO: this is pretty close to getOrDefineFunction, can probably be factored |
| 67 | +LLVM::GlobalOp getOrDefineExternalStruct(ModuleOp &moduleOp, const Location loc, |
| 68 | + ConversionPatternRewriter &rewriter, |
| 69 | + StringRef name, |
| 70 | + LLVM::LLVMStructType type) { |
| 71 | + LLVM::GlobalOp ret; |
| 72 | + if (!(ret = moduleOp.lookupSymbol<LLVM::GlobalOp>(name))) { |
| 73 | + ConversionPatternRewriter::InsertionGuard guard(rewriter); |
| 74 | + rewriter.setInsertionPointToStart(moduleOp.getBody()); |
| 75 | + ret = rewriter.create<LLVM::GlobalOp>( |
| 76 | + loc, type, /*isConstant=*/false, LLVM::Linkage::External, name, |
| 77 | + /*value=*/Attribute(), /*alignment=*/0, 0); |
| 78 | + } |
| 79 | + return ret; |
| 80 | +} |
| 81 | + |
| 82 | +} // namespace |
| 83 | + |
| 84 | +//===----------------------------------------------------------------------===// |
| 85 | +// InitOpLowering |
| 86 | +//===----------------------------------------------------------------------===// |
| 87 | + |
| 88 | +LogicalResult |
| 89 | +InitOpLowering::matchAndRewrite(mpi::InitOp op, OpAdaptor adaptor, |
| 90 | + ConversionPatternRewriter &rewriter) const { |
| 91 | + // get loc |
| 92 | + auto loc = op.getLoc(); |
| 93 | + |
| 94 | + // ptrType `!llvm.ptr` |
| 95 | + Type ptrType = LLVM::LLVMPointerType::get(rewriter.getContext()); |
| 96 | + |
| 97 | + // instantiate nullptr `%nullptr = llvm.mlir.zero : !llvm.ptr` |
| 98 | + auto nullPtrOp = rewriter.create<LLVM::ZeroOp>(loc, ptrType); |
| 99 | + Value llvmnull = nullPtrOp.getRes(); |
| 100 | + |
| 101 | + // grab a reference to the global module op: |
| 102 | + auto moduleOp = op->getParentOfType<ModuleOp>(); |
| 103 | + |
| 104 | + // LLVM Function type representing `i32 MPI_Init(ptr, ptr)` |
| 105 | + auto initFuncType = |
| 106 | + LLVM::LLVMFunctionType::get(rewriter.getI32Type(), {ptrType, ptrType}); |
| 107 | + // get or create function declaration: |
| 108 | + LLVM::LLVMFuncOp initDecl = |
| 109 | + getOrDefineFunction(moduleOp, loc, rewriter, "MPI_Init", initFuncType); |
| 110 | + |
| 111 | + // replace init with function call |
| 112 | + rewriter.replaceOpWithNewOp<LLVM::CallOp>(op, initDecl, |
| 113 | + ValueRange{llvmnull, llvmnull}); |
| 114 | + |
| 115 | + return success(); |
| 116 | +} |
| 117 | + |
| 118 | +//===----------------------------------------------------------------------===// |
| 119 | +// FinalizeOpLowering |
| 120 | +//===----------------------------------------------------------------------===// |
| 121 | + |
| 122 | +LogicalResult |
| 123 | +FinalizeOpLowering::matchAndRewrite(mpi::FinalizeOp op, OpAdaptor adaptor, |
| 124 | + ConversionPatternRewriter &rewriter) const { |
| 125 | + // get loc |
| 126 | + auto loc = op.getLoc(); |
| 127 | + |
| 128 | + // grab a reference to the global module op: |
| 129 | + auto moduleOp = op->getParentOfType<ModuleOp>(); |
| 130 | + |
| 131 | + // LLVM Function type representing `i32 MPI_Finalize()` |
| 132 | + auto initFuncType = LLVM::LLVMFunctionType::get(rewriter.getI32Type(), {}); |
| 133 | + // get or create function declaration: |
| 134 | + LLVM::LLVMFuncOp initDecl = getOrDefineFunction(moduleOp, loc, rewriter, |
| 135 | + "MPI_Finalize", initFuncType); |
| 136 | + |
| 137 | + // replace init with function call |
| 138 | + rewriter.replaceOpWithNewOp<LLVM::CallOp>(op, initDecl, ValueRange{}); |
| 139 | + |
| 140 | + return success(); |
| 141 | +} |
| 142 | + |
| 143 | +//===----------------------------------------------------------------------===// |
| 144 | +// CommRankLowering |
| 145 | +//===----------------------------------------------------------------------===// |
| 146 | + |
| 147 | +LogicalResult |
| 148 | +CommRankOpLowering::matchAndRewrite(mpi::CommRankOp op, OpAdaptor adaptor, |
| 149 | + ConversionPatternRewriter &rewriter) const { |
| 150 | + // get some helper vars |
| 151 | + auto loc = op.getLoc(); |
| 152 | + auto context = rewriter.getContext(); |
| 153 | + auto i32 = rewriter.getI32Type(); |
| 154 | + |
| 155 | + // ptrType `!llvm.ptr` |
| 156 | + Type ptrType = LLVM::LLVMPointerType::get(context); |
| 157 | + |
| 158 | + // get external opaque struct pointer type |
| 159 | + auto commStructT = LLVM::LLVMStructType::getOpaque("MPI_ABI_Comm", context); |
| 160 | + |
| 161 | + // grab a reference to the global module op: |
| 162 | + auto moduleOp = op->getParentOfType<ModuleOp>(); |
| 163 | + |
| 164 | + // make sure global op definition exists |
| 165 | + getOrDefineExternalStruct(moduleOp, loc, rewriter, "MPI_COMM_WORLD", |
| 166 | + commStructT); |
| 167 | + |
| 168 | + // get address of @MPI_COMM_WORLD |
| 169 | + auto one = rewriter.create<LLVM::ConstantOp>(loc, i32, 1); |
| 170 | + auto rankptr = rewriter.create<LLVM::AllocaOp>(loc, ptrType, i32, one); |
| 171 | + auto commWorld = rewriter.create<LLVM::AddressOfOp>( |
| 172 | + loc, ptrType, SymbolRefAttr::get(context, "MPI_COMM_WORLD")); |
| 173 | + |
| 174 | + // LLVM Function type representing `i32 MPI_Comm_rank(ptr, ptr)` |
| 175 | + auto rankFuncType = LLVM::LLVMFunctionType::get(i32, {ptrType, ptrType}); |
| 176 | + // get or create function declaration: |
| 177 | + LLVM::LLVMFuncOp initDecl = getOrDefineFunction( |
| 178 | + moduleOp, loc, rewriter, "MPI_Comm_rank", rankFuncType); |
| 179 | + |
| 180 | + // replace init with function call |
| 181 | + auto callOp = rewriter.create<LLVM::CallOp>( |
| 182 | + loc, initDecl, ValueRange{commWorld.getRes(), rankptr.getRes()}); |
| 183 | + |
| 184 | + // load the rank into a register |
| 185 | + auto loadedRank = |
| 186 | + rewriter.create<LLVM::LoadOp>(loc, i32, rankptr.getResult()); |
| 187 | + |
| 188 | + // if retval is checked, replace uses of retval with the results from the call |
| 189 | + // op |
| 190 | + SmallVector<Value> replacements; |
| 191 | + if (op.getRetval()) { |
| 192 | + replacements.push_back(callOp.getResult()); |
| 193 | + } |
| 194 | + // replace all uses, then erase op |
| 195 | + replacements.push_back(loadedRank.getRes()); |
| 196 | + rewriter.replaceOp(op, replacements); |
| 197 | + |
| 198 | + return success(); |
| 199 | +} |
| 200 | + |
| 201 | +//===----------------------------------------------------------------------===// |
| 202 | +// Pass Definition |
| 203 | +//===----------------------------------------------------------------------===// |
| 204 | + |
| 205 | +namespace { |
| 206 | +struct MPIToLLVMConversionPass |
| 207 | + : public impl::MPIToLLVMConversionPassBase<MPIToLLVMConversionPass> { |
| 208 | + using Base::Base; |
| 209 | + |
| 210 | + void runOnOperation() override { |
| 211 | + LLVMConversionTarget target(getContext()); |
| 212 | + RewritePatternSet patterns(&getContext()); |
| 213 | + |
| 214 | + LowerToLLVMOptions options(&getContext()); |
| 215 | + if (indexBitwidth != kDeriveIndexBitwidthFromDataLayout) |
| 216 | + options.overrideIndexBitwidth(indexBitwidth); |
| 217 | + |
| 218 | + target.addIllegalDialect<mpi::MPIDialect>(); |
| 219 | + |
| 220 | + // not yet implemented, will be added in future patches: |
| 221 | + target.addLegalOp<mpi::RecvOp>(); |
| 222 | + target.addLegalOp<mpi::SendOp>(); |
| 223 | + target.addLegalOp<mpi::ErrorClassOp>(); |
| 224 | + target.addLegalOp<mpi::RetvalCheckOp>(); |
| 225 | + |
| 226 | + LLVMTypeConverter converter(&getContext(), options); |
| 227 | + |
| 228 | + converter.addConversion( |
| 229 | + [&](mpi::RetvalType) { return IntegerType::get(&getContext(), 32); }); |
| 230 | + |
| 231 | + mpi::populateMPIToLLVMConversionPatterns(converter, patterns); |
| 232 | + |
| 233 | + if (failed(applyPartialConversion(getOperation(), target, |
| 234 | + std::move(patterns)))) |
| 235 | + signalPassFailure(); |
| 236 | + } |
| 237 | +}; |
| 238 | +} // namespace |
| 239 | + |
| 240 | +//===----------------------------------------------------------------------===// |
| 241 | +// Pattern Population |
| 242 | +//===----------------------------------------------------------------------===// |
| 243 | + |
| 244 | +void mpi::populateMPIToLLVMConversionPatterns(LLVMTypeConverter &converter, |
| 245 | + RewritePatternSet &patterns) { |
| 246 | + patterns.add<InitOpLowering>(converter); |
| 247 | + patterns.add<CommRankOpLowering>(converter); |
| 248 | + patterns.add<FinalizeOpLowering>(converter); |
| 249 | +} |
0 commit comments