20static LogicalResult convertTypeAttr(
Attribute &attr,
22 auto typeAttr = dyn_cast<TypeAttr>(attr);
25 Type convertedType = typeConverter.convertType(typeAttr.getValue());
28 attr = TypeAttr::get(convertedType);
32static bool areTypeAttrsLegal(
Operation *op,
34 bool inherentAttrsLegal =
true;
36 if (
auto typeAttr = dyn_cast<TypeAttr>(attr))
37 inherentAttrsLegal &= typeConverter.isLegal(typeAttr.getValue());
39 return inherentAttrsLegal &&
41 auto typeAttr = dyn_cast<TypeAttr>(attr.getValue());
42 return !typeAttr || typeConverter.isLegal(typeAttr.getValue());
57 using ConvertOpToLLVMPattern<T>::ConvertOpToLLVMPattern;
59 OpenMPOpConversion(LLVMTypeConverter &typeConverter,
60 PatternBenefit benefit = 1)
61 : ConvertOpToLLVMPattern<T>(typeConverter, benefit) {
65 typeConverter.addConversion(
66 [&](::mlir::omp::CanonicalLoopInfoType type) {
return type; });
70 matchAndRewrite(T op,
typename T::Adaptor adaptor,
71 ConversionPatternRewriter &rewriter)
const override {
74 SmallVector<Type> resTypes;
75 if (
failed(converter->convertTypes(op->getResultTypes(), resTypes)))
80 typename T::Properties convertedProperties = op.getProperties();
81 LogicalResult attrConversionResult =
success();
83 op.getContext(), convertedProperties, [&](StringRef, Attribute &attr) {
84 if (succeeded(attrConversionResult))
85 attrConversionResult = convertTypeAttr(attr, *converter);
87 if (
failed(attrConversionResult))
88 return rewriter.notifyMatchFailure(op,
89 "failed to convert type in attribute");
91 SmallVector<NamedAttribute> convertedDiscardableAttrs;
92 for (NamedAttribute attr : op->getDiscardableAttrs()) {
93 Attribute convertedAttr = attr.getValue();
94 if (
failed(convertTypeAttr(convertedAttr, *converter)))
95 return rewriter.notifyMatchFailure(
96 op,
"failed to convert type in attribute");
97 convertedDiscardableAttrs.emplace_back(attr.getName(), convertedAttr);
101 SmallVector<Value> convertedOperands;
102 convertedOperands.reserve(op->getNumOperands());
103 for (
auto [originalOperand, convertedOperand] :
104 llvm::zip_equal(op->getOperands(), adaptor.getOperands())) {
105 if (!originalOperand)
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())) {
115 return rewriter.notifyMatchFailure(op,
"memref is not supported yet");
118 convertedOperands.push_back(convertedOperand);
122 auto newOp = T::create(rewriter, op.getLoc(), resTypes, convertedOperands,
123 convertedProperties, convertedDiscardableAttrs);
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(&convertedRegion,
131 *this->getTypeConverter())))
136 rewriter.replaceOp(op, newOp->getResults());
145 target.addDynamicallyLegalOp<
147#include "mlir/Dialect/OpenMP/OpenMPOps.cpp.inc"
149 return typeConverter.isLegal(op->getOperandTypes()) &&
150 typeConverter.isLegal(op->getResultTypes()) &&
151 llvm::all_of(op->getRegions(),
153 return typeConverter.isLegal(®ion);
155 areTypeAttrsLegal(op, typeConverter);
161template <
typename... Ts>
165 return patterns.
add<OpenMPOpConversion<Ts>...>(converter);
173 converter.addConversion(
174 [&](omp::MapBoundsType type) ->
Type {
return type; });
175 converter.addConversion(
176 [&](omp::AffinityEntryType type) ->
Type {
return type; });
177 converter.addConversion([&](omp::IteratedType type) ->
Type {
return type; });
182#include "mlir/Dialect/OpenMP/OpenMPOps.cpp.inc"
183 >(converter, patterns);
191struct OpenMPToLLVMDialectInterface :
public ConvertToLLVMPatternInterface {
192 OpenMPToLLVMDialectInterface(
Dialect *dialect)
193 : ConvertToLLVMPatternInterface(dialect) {}
195 void loadDependentDialects(MLIRContext *context)
const final {
196 context->loadDialect<LLVM::LLVMDialect>();
201 void populateConvertToLLVMConversionPatterns(
202 ConversionTarget &
target, LLVMTypeConverter &typeConverter,
203 RewritePatternSet &patterns)
const final {
212 dialect->addInterfaces<OpenMPToLLVMDialectInterface>();
static RewritePatternSet & addOpenMPOpConversions(LLVMTypeConverter &converter, RewritePatternSet &patterns)
Add an OpenMPOpConversion<T> conversion pattern for each operation type passed as template argument.
Attributes are known-constant values of operations.
Utility class for operation conversions targeting the LLVM dialect that match exactly one source oper...
const LLVMTypeConverter * getTypeConverter() const
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.
Dialects are groups of MLIR operations, types and attributes, as well as behavior associated with the...
Conversion from types to the LLVM IR dialect.
MLIRContext is the top-level object for a collection of MLIR operations.
NamedAttribute represents a combination of a name and an Attribute value.
void walkInherentAttrs(Operation *op, InherentAttrVisitor visitor) const
Visit the inherent attributes stored in the properties of op.
Operation is the basic unit of execution within MLIR.
auto getDiscardableAttrs()
Return a range of all of discardable attributes on this operation.
OperationName getName()
The name of an operation is the key identifier for it.
This class contains a list of basic blocks and a link to the parent operation it is attached to.
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...
Include the generated interface declarations.
void populateOpenMPToLLVMConversionPatterns(LLVMTypeConverter &converter, RewritePatternSet &patterns)
Populate the given list with patterns that convert from OpenMP to LLVM.
void registerConvertOpenMPToLLVMInterface(DialectRegistry ®istry)
Registers the ConvertToLLVMPatternInterface interface in the OpenMP dialect.
void configureOpenMPToLLVMConversionLegality(ConversionTarget &target, const LLVMTypeConverter &typeConverter)
Configure dynamic conversion legality of regionless operations from OpenMP to LLVM.