| 1 | //===- OpenMPToLLVM.cpp - conversion from OpenMP to LLVM dialect ----------===// |
| 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/OpenMPToLLVM/ConvertOpenMPToLLVM.h" |
| 10 | |
| 11 | #include "mlir/Conversion/ConvertToLLVM/ToLLVMInterface.h" |
| 12 | #include "mlir/Conversion/LLVMCommon/Pattern.h" |
| 13 | #include "mlir/Dialect/LLVMIR/LLVMDialectDecl.h" |
| 14 | #include "mlir/Dialect/OpenMP/OpenMPDialect.h" |
| 15 | |
| 16 | using namespace mlir; |
| 17 | |
| 18 | namespace { |
| 19 | |
| 20 | static LogicalResult convertTypeAttr(Attribute &attr, |
| 21 | const TypeConverter &typeConverter) { |
| 22 | auto typeAttr = dyn_cast<TypeAttr>(Val&: attr); |
| 23 | if (!typeAttr) |
| 24 | return success(); |
| 25 | Type convertedType = typeConverter.convertType(t: typeAttr.getValue()); |
| 26 | if (!convertedType) |
| 27 | return failure(); |
| 28 | attr = TypeAttr::get(type: convertedType); |
| 29 | return success(); |
| 30 | } |
| 31 | |
| 32 | static bool areTypeAttrsLegal(Operation *op, |
| 33 | const TypeConverter &typeConverter) { |
| 34 | bool inherentAttrsLegal = true; |
| 35 | op->getName().walkInherentAttrs(op, visitor: [&](StringRef, Attribute &attr) { |
| 36 | if (auto typeAttr = dyn_cast<TypeAttr>(Val&: attr)) |
| 37 | inherentAttrsLegal &= typeConverter.isLegal(type: typeAttr.getValue()); |
| 38 | }); |
| 39 | return inherentAttrsLegal && |
| 40 | llvm::all_of(Range: op->getDiscardableAttrs(), P: [&](NamedAttribute attr) { |
| 41 | auto typeAttr = dyn_cast<TypeAttr>(Val: attr.getValue()); |
| 42 | return !typeAttr || typeConverter.isLegal(type: typeAttr.getValue()); |
| 43 | }); |
| 44 | } |
| 45 | |
| 46 | /// A pattern that converts the result and operand types, attributes, and region |
| 47 | /// arguments of an OpenMP operation to the LLVM dialect. |
| 48 | /// |
| 49 | /// Attributes are copied verbatim by default, and only translated if they are |
| 50 | /// type attributes. |
| 51 | /// |
| 52 | /// Region bodies, if any, are not modified and expected to either be processed |
| 53 | /// by the conversion infrastructure or already contain ops compatible with LLVM |
| 54 | /// dialect types. |
| 55 | template <typename T> |
| 56 | struct OpenMPOpConversion : public ConvertOpToLLVMPattern<T> { |
| 57 | using ConvertOpToLLVMPattern<T>::ConvertOpToLLVMPattern; |
| 58 | |
| 59 | OpenMPOpConversion(LLVMTypeConverter &typeConverter, |
| 60 | PatternBenefit benefit = 1) |
| 61 | : ConvertOpToLLVMPattern<T>(typeConverter, benefit) { |
| 62 | // Operations using CanonicalLoopInfoType are lowered only by |
| 63 | // mlir::translateModuleToLLVMIR() using the OpenMPIRBuilder. Until then, |
| 64 | // the type and operations using it must be preserved. |
| 65 | typeConverter.addConversion( |
| 66 | [&](::mlir::omp::CanonicalLoopInfoType type) { return type; }); |
| 67 | } |
| 68 | |
| 69 | LogicalResult |
| 70 | matchAndRewrite(T op, typename T::Adaptor adaptor, |
| 71 | ConversionPatternRewriter &rewriter) const override { |
| 72 | // Translate result types. |
| 73 | const TypeConverter *converter = ConvertToLLVMPattern::getTypeConverter(); |
| 74 | SmallVector<Type> resTypes; |
| 75 | if (failed(converter->convertTypes(op->getResultTypes(), resTypes))) |
| 76 | return failure(); |
| 77 | |
| 78 | // Translate type attributes in the properties and discardable attributes. |
| 79 | // They are kept unmodified except if they are type attributes. |
| 80 | typename T::Properties convertedProperties = op.getProperties(); |
| 81 | LogicalResult attrConversionResult = success(); |
| 82 | T::walkInherentAttrs( |
| 83 | op.getContext(), convertedProperties, [&](StringRef, Attribute &attr) { |
| 84 | if (succeeded(Result: attrConversionResult)) |
| 85 | attrConversionResult = convertTypeAttr(attr, typeConverter: *converter); |
| 86 | }); |
| 87 | if (failed(Result: attrConversionResult)) |
| 88 | return rewriter.notifyMatchFailure(op, |
| 89 | "failed to convert type in attribute" ); |
| 90 | |
| 91 | SmallVector<NamedAttribute> convertedDiscardableAttrs; |
| 92 | for (NamedAttribute attr : op->getDiscardableAttrs()) { |
| 93 | Attribute convertedAttr = attr.getValue(); |
| 94 | if (failed(Result: convertTypeAttr(attr&: convertedAttr, typeConverter: *converter))) |
| 95 | return rewriter.notifyMatchFailure( |
| 96 | op, "failed to convert type in attribute" ); |
| 97 | convertedDiscardableAttrs.emplace_back(Args: attr.getName(), Args&: convertedAttr); |
| 98 | } |
| 99 | |
| 100 | // Translate operands. |
| 101 | SmallVector<Value> convertedOperands; |
| 102 | convertedOperands.reserve(N: op->getNumOperands()); |
| 103 | for (auto [originalOperand, convertedOperand] : |
| 104 | llvm::zip_equal(op->getOperands(), adaptor.getOperands())) { |
| 105 | if (!originalOperand) |
| 106 | return failure(); |
| 107 | |
| 108 | // TODO: Revisit whether we need to trigger an error specifically for this |
| 109 | // set of operations. Consider removing this check or updating the list. |
| 110 | if constexpr (llvm::is_one_of<T, omp::AtomicUpdateOp, omp::AtomicWriteOp, |
| 111 | omp::FlushOp, omp::MapBoundsOp, |
| 112 | omp::ThreadprivateOp>::value) { |
| 113 | if (isa<MemRefType>(originalOperand.getType())) { |
| 114 | // TODO: Support memref type in variable operands |
| 115 | return rewriter.notifyMatchFailure(op, "memref is not supported yet" ); |
| 116 | } |
| 117 | } |
| 118 | convertedOperands.push_back(Elt: convertedOperand); |
| 119 | } |
| 120 | |
| 121 | // Create new operation. |
| 122 | auto newOp = T::create(rewriter, op.getLoc(), resTypes, convertedOperands, |
| 123 | convertedProperties, convertedDiscardableAttrs); |
| 124 | |
| 125 | // Translate regions. |
| 126 | for (auto [originalRegion, convertedRegion] : |
| 127 | llvm::zip_equal(op->getRegions(), newOp->getRegions())) { |
| 128 | rewriter.inlineRegionBefore(originalRegion, convertedRegion, |
| 129 | convertedRegion.end()); |
| 130 | if (failed(rewriter.convertRegionTypes(region: &convertedRegion, |
| 131 | converter: *this->getTypeConverter()))) |
| 132 | return failure(); |
| 133 | } |
| 134 | |
| 135 | // Delete old operation and replace result uses with those of the new one. |
| 136 | rewriter.replaceOp(op, newOp->getResults()); |
| 137 | return success(); |
| 138 | } |
| 139 | }; |
| 140 | |
| 141 | } // namespace |
| 142 | |
| 143 | void mlir::configureOpenMPToLLVMConversionLegality( |
| 144 | ConversionTarget &target, const LLVMTypeConverter &typeConverter) { |
| 145 | target.addDynamicallyLegalOp< |
| 146 | #define GET_OP_LIST |
| 147 | #include "mlir/Dialect/OpenMP/OpenMPOps.cpp.inc" |
| 148 | >(callback: [&](Operation *op) { |
| 149 | return typeConverter.isLegal(range: op->getOperandTypes()) && |
| 150 | typeConverter.isLegal(range: op->getResultTypes()) && |
| 151 | llvm::all_of(Range: op->getRegions(), |
| 152 | P: [&](Region ®ion) { |
| 153 | return typeConverter.isLegal(region: ®ion); |
| 154 | }) && |
| 155 | areTypeAttrsLegal(op, typeConverter); |
| 156 | }); |
| 157 | } |
| 158 | |
| 159 | /// Add an `OpenMPOpConversion<T>` conversion pattern for each operation type |
| 160 | /// passed as template argument. |
| 161 | template <typename... Ts> |
| 162 | static inline RewritePatternSet & |
| 163 | addOpenMPOpConversions(LLVMTypeConverter &converter, |
| 164 | RewritePatternSet &patterns) { |
| 165 | return patterns.add<OpenMPOpConversion<Ts>...>(converter); |
| 166 | } |
| 167 | |
| 168 | void mlir::populateOpenMPToLLVMConversionPatterns(LLVMTypeConverter &converter, |
| 169 | RewritePatternSet &patterns) { |
| 170 | // This type is allowed when converting OpenMP to LLVM Dialect, it carries |
| 171 | // bounds information for map clauses and the operation and type are |
| 172 | // discarded on lowering to LLVM-IR from the OpenMP dialect. |
| 173 | converter.addConversion( |
| 174 | callback: [&](omp::MapBoundsType type) -> Type { return type; }); |
| 175 | converter.addConversion( |
| 176 | callback: [&](omp::AffinityEntryType type) -> Type { return type; }); |
| 177 | converter.addConversion(callback: [&](omp::IteratedType type) -> Type { return type; }); |
| 178 | |
| 179 | // Add conversions for all OpenMP operations. |
| 180 | addOpenMPOpConversions< |
| 181 | #define GET_OP_LIST |
| 182 | #include "mlir/Dialect/OpenMP/OpenMPOps.cpp.inc" |
| 183 | >(converter, patterns); |
| 184 | } |
| 185 | |
| 186 | //===----------------------------------------------------------------------===// |
| 187 | // ConvertToLLVMPatternInterface implementation |
| 188 | //===----------------------------------------------------------------------===// |
| 189 | namespace { |
| 190 | /// Implement the interface to convert OpenMP to LLVM. |
| 191 | struct OpenMPToLLVMDialectInterface : public ConvertToLLVMPatternInterface { |
| 192 | OpenMPToLLVMDialectInterface(Dialect *dialect) |
| 193 | : ConvertToLLVMPatternInterface(dialect) {} |
| 194 | |
| 195 | void loadDependentDialects(MLIRContext *context) const final { |
| 196 | context->loadDialect<LLVM::LLVMDialect>(); |
| 197 | } |
| 198 | |
| 199 | /// Hook for derived dialect interface to provide conversion patterns |
| 200 | /// and mark dialect legal for the conversion target. |
| 201 | void populateConvertToLLVMConversionPatterns( |
| 202 | ConversionTarget &target, LLVMTypeConverter &typeConverter, |
| 203 | RewritePatternSet &patterns) const final { |
| 204 | configureOpenMPToLLVMConversionLegality(target, typeConverter); |
| 205 | populateOpenMPToLLVMConversionPatterns(converter&: typeConverter, patterns); |
| 206 | } |
| 207 | }; |
| 208 | } // namespace |
| 209 | |
| 210 | void mlir::registerConvertOpenMPToLLVMInterface(DialectRegistry ®istry) { |
| 211 | registry.addExtension(extensionFn: +[](MLIRContext *ctx, omp::OpenMPDialect *dialect) { |
| 212 | dialect->addInterfaces<OpenMPToLLVMDialectInterface>(); |
| 213 | }); |
| 214 | } |
| 215 | |