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