24 #define GEN_PASS_DEF_CONVERTOPENMPTOLLVMPASS
25 #include "mlir/Conversion/Passes.h.inc"
35 template <
typename OpType>
40 matchAndRewrite(OpType curOp,
typename OpType::Adaptor adaptor,
42 auto newOp = rewriter.
create<OpType>(
43 curOp.getLoc(),
TypeRange(), adaptor.getOperands(), curOp->getAttrs());
45 newOp.getRegion().end());
47 *this->getTypeConverter())))
56 struct RegionLessOpWithVarOperandsConversion
60 matchAndRewrite(T curOp,
typename T::Adaptor adaptor,
64 if (failed(converter->
convertTypes(curOp->getResultTypes(), resTypes)))
67 assert(curOp.getNumVariableOperands() ==
68 curOp.getOperation()->getNumOperands() &&
69 "unexpected non-variable operands");
70 for (
unsigned idx = 0; idx < curOp.getNumVariableOperands(); ++idx) {
71 Value originalVariableOperand = curOp.getVariableOperand(idx);
72 if (!originalVariableOperand)
74 if (isa<MemRefType>(originalVariableOperand.
getType())) {
77 "memref is not supported yet");
79 convertedOperands.emplace_back(adaptor.getOperands()[idx]);
92 matchAndRewrite(T curOp,
typename T::Adaptor adaptor,
96 if (failed(converter->
convertTypes(curOp->getResultTypes(), resTypes)))
99 assert(curOp.getNumVariableOperands() ==
100 curOp.getOperation()->getNumOperands() &&
101 "unexpected non-variable operands");
102 for (
unsigned idx = 0; idx < curOp.getNumVariableOperands(); ++idx) {
103 Value originalVariableOperand = curOp.getVariableOperand(idx);
104 if (!originalVariableOperand)
106 if (isa<MemRefType>(originalVariableOperand.
getType())) {
109 "memref is not supported yet");
111 convertedOperands.emplace_back(adaptor.getOperands()[idx]);
113 auto newOp = rewriter.
create<T>(curOp.getLoc(), resTypes, convertedOperands,
116 newOp.getRegion().end());
118 *this->getTypeConverter())))
126 template <
typename T>
130 matchAndRewrite(T curOp,
typename T::Adaptor adaptor,
134 if (failed(converter->
convertTypes(curOp->getResultTypes(), resTypes)))
143 struct AtomicReadOpConversion
147 matchAndRewrite(omp::AtomicReadOp curOp, OpAdaptor adaptor,
150 Type curElementType = curOp.getElementType();
151 auto newOp = rewriter.
create<omp::AtomicReadOp>(
152 curOp.getLoc(),
TypeRange(), adaptor.getOperands(), curOp->getAttrs());
154 newOp.setElementTypeAttr(typeAttr);
163 matchAndRewrite(omp::MapInfoOp curOp, OpAdaptor adaptor,
168 if (failed(converter->
convertTypes(curOp->getResultTypes(), resTypes)))
175 if (
auto typeAttr = dyn_cast<TypeAttr>(attr.getValue())) {
177 newAttrs.emplace_back(attr.getName(),
TypeAttr::get(newAttr));
179 newAttrs.push_back(attr);
184 curOp, resTypes, adaptor.getOperands(), newAttrs);
189 template <
typename OpType>
193 void forwardOpAttrs(OpType curOp, OpType newOp)
const {}
196 matchAndRewrite(OpType curOp,
typename OpType::Adaptor adaptor,
198 auto newOp = rewriter.
create<OpType>(
199 curOp.getLoc(),
TypeRange(), curOp.getSymNameAttr(),
201 curOp.getTypeAttr().getValue())));
202 forwardOpAttrs(curOp, newOp);
204 for (
unsigned idx = 0; idx < curOp.getNumRegions(); idx++) {
206 newOp.getRegion(idx).end());
208 *this->getTypeConverter())))
218 void MultiRegionOpConversion<omp::PrivateClauseOp>::forwardOpAttrs(
219 omp::PrivateClauseOp curOp, omp::PrivateClauseOp newOp)
const {
220 newOp.setDataSharingType(curOp.getDataSharingType());
227 omp::AtomicReadOp, omp::AtomicWriteOp, omp::CancellationPointOp,
228 omp::CancelOp, omp::CriticalDeclareOp, omp::FlushOp, omp::MapBoundsOp,
229 omp::MapInfoOp, omp::OrderedOp, omp::TargetEnterDataOp,
230 omp::TargetExitDataOp, omp::TargetUpdateOp, omp::ThreadprivateOp,
232 return typeConverter.
isLegal(op->getOperandTypes()) &&
233 typeConverter.
isLegal(op->getResultTypes());
236 omp::AtomicUpdateOp, omp::CriticalOp, omp::DeclareReductionOp,
237 omp::DistributeOp, omp::LoopNestOp, omp::LoopOp, omp::MasterOp,
238 omp::OrderedRegionOp, omp::ParallelOp, omp::PrivateClauseOp,
239 omp::SectionOp, omp::SectionsOp, omp::SimdOp, omp::SingleOp,
240 omp::TargetDataOp, omp::TargetOp, omp::TaskgroupOp, omp::TaskloopOp,
241 omp::TaskOp, omp::TeamsOp, omp::WsloopOp>([&](
Operation *op) {
242 return std::all_of(op->getRegions().begin(), op->getRegions().end(),
244 return typeConverter.isLegal(®ion);
246 typeConverter.
isLegal(op->getOperandTypes()) &&
247 typeConverter.
isLegal(op->getResultTypes());
257 [&](omp::MapBoundsType type) ->
Type {
return type; });
260 AtomicReadOpConversion, MapInfoOpConversion,
261 MultiRegionOpConversion<omp::DeclareReductionOp>,
262 MultiRegionOpConversion<omp::PrivateClauseOp>,
263 RegionLessOpConversion<omp::CancellationPointOp>,
264 RegionLessOpConversion<omp::CancelOp>,
265 RegionLessOpConversion<omp::CriticalDeclareOp>,
266 RegionLessOpConversion<omp::OrderedOp>,
267 RegionLessOpConversion<omp::TargetEnterDataOp>,
268 RegionLessOpConversion<omp::TargetExitDataOp>,
269 RegionLessOpConversion<omp::TargetUpdateOp>,
270 RegionLessOpConversion<omp::YieldOp>,
271 RegionLessOpWithVarOperandsConversion<omp::AtomicWriteOp>,
272 RegionLessOpWithVarOperandsConversion<omp::FlushOp>,
273 RegionLessOpWithVarOperandsConversion<omp::MapBoundsOp>,
274 RegionLessOpWithVarOperandsConversion<omp::ThreadprivateOp>,
275 RegionOpConversion<omp::AtomicCaptureOp>,
276 RegionOpConversion<omp::CriticalOp>,
277 RegionOpConversion<omp::DistributeOp>,
278 RegionOpConversion<omp::LoopNestOp>, RegionOpConversion<omp::LoopOp>,
279 RegionOpConversion<omp::MaskedOp>, RegionOpConversion<omp::MasterOp>,
280 RegionOpConversion<omp::OrderedRegionOp>,
281 RegionOpConversion<omp::ParallelOp>, RegionOpConversion<omp::SectionOp>,
282 RegionOpConversion<omp::SectionsOp>, RegionOpConversion<omp::SimdOp>,
283 RegionOpConversion<omp::SingleOp>, RegionOpConversion<omp::TargetDataOp>,
284 RegionOpConversion<omp::TargetOp>, RegionOpConversion<omp::TaskgroupOp>,
285 RegionOpConversion<omp::TaskloopOp>, RegionOpConversion<omp::TaskOp>,
286 RegionOpConversion<omp::TeamsOp>, RegionOpConversion<omp::WsloopOp>,
287 RegionOpWithVarOperandsConversion<omp::AtomicUpdateOp>>(converter);
291 struct ConvertOpenMPToLLVMPass
292 :
public impl::ConvertOpenMPToLLVMPassBase<ConvertOpenMPToLLVMPass> {
295 void runOnOperation()
override;
299 void ConvertOpenMPToLLVMPass::runOnOperation() {
300 auto module = getOperation();
312 target.addLegalOp<omp::BarrierOp, omp::FlushOp, omp::TaskwaitOp,
313 omp::TaskyieldOp, omp::TerminatorOp>();
326 void loadDependentDialects(
MLIRContext *context)
const final {
327 context->loadDialect<LLVM::LLVMDialect>();
332 void populateConvertToLLVMConversionPatterns(
343 dialect->addInterfaces<OpenMPToLLVMDialectInterface>();
static MLIRContext * getContext(OpFoldResult val)
This class implements a pattern rewriter for use with ConversionPatterns.
FailureOr< Block * > convertRegionTypes(Region *region, const TypeConverter &converter, TypeConverter::SignatureConversion *entryConversion=nullptr)
Apply a signature conversion to each block in the given region.
void eraseOp(Operation *op) override
PatternRewriter hook for erasing a dead operation.
This class describes a specific conversion target.
void addDynamicallyLegalOp(OperationName op, const DynamicLegalityCallbackFn &callback)
Register the given operation as dynamically legal and set the dynamic legalization callback to the on...
Utility class for operation conversions targeting the LLVM dialect that match exactly one source oper...
Base class for dialect interfaces providing translation to LLVM IR.
ConvertToLLVMPatternInterface(Dialect *dialect)
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.
Derived class that automatically populates legalization information for different LLVM ops.
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.
Operation * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
Operation is the basic unit of execution within MLIR.
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.
std::enable_if_t<!std::is_convertible< CallbackT, Twine >::value, LogicalResult > notifyMatchFailure(Location loc, CallbackT &&reasonCallback)
Used to notify the listener that the IR failed to be rewritten because of a match failure,...
void inlineRegionBefore(Region ®ion, Region &parent, Region::iterator before)
Move the blocks that belong to "region" before the given position in another region "parent".
OpTy replaceOpWithNewOp(Operation *op, Args &&...args)
Replace the results of the given (original) op with a new op that is created without verification (re...
void addConversion(FnT &&callback)
Register a conversion function.
bool isLegal(Type type) const
Return true if the given type is legal for this type converter, i.e.
LogicalResult convertType(Type t, SmallVectorImpl< Type > &results) const
Convert the given type.
LogicalResult convertTypes(TypeRange types, SmallVectorImpl< Type > &results) const
Convert the given set of types, filling 'results' as necessary.
This class provides an abstraction over the various different ranges of value types.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Type getType() const
Return the type of this value.
void populateArithToLLVMConversionPatterns(const LLVMTypeConverter &converter, RewritePatternSet &patterns)
void populateControlFlowToLLVMConversionPatterns(const LLVMTypeConverter &converter, RewritePatternSet &patterns)
Collect the patterns to convert from the ControlFlow dialect to LLVM.
Include the generated interface declarations.
void populateOpenMPToLLVMConversionPatterns(LLVMTypeConverter &converter, RewritePatternSet &patterns)
Populate the given list with patterns that convert from OpenMP to LLVM.
void populateFinalizeMemRefToLLVMConversionPatterns(const LLVMTypeConverter &converter, RewritePatternSet &patterns)
Collect a set of patterns to convert memory-related operations from the MemRef dialect to the LLVM di...
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.
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
void populateFuncToLLVMConversionPatterns(const LLVMTypeConverter &converter, RewritePatternSet &patterns, const SymbolTable *symbolTable=nullptr)
Collect the patterns to convert from the Func dialect to LLVM.
LogicalResult applyPartialConversion(ArrayRef< Operation * > ops, const ConversionTarget &target, const FrozenRewritePatternSet &patterns, ConversionConfig config=ConversionConfig())
Below we define several entry points for operation conversion.