MLIR 24.0.0git
OpenACCToSCF.cpp
Go to the documentation of this file.
1//===- OpenACCToSCF.cpp - OpenACC condition to SCF if 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
10
14#include "mlir/IR/Matchers.h"
16
17namespace mlir {
18#define GEN_PASS_DEF_CONVERTOPENACCTOSCFPASS
19#include "mlir/Conversion/Passes.h.inc"
20} // namespace mlir
21
22using namespace mlir;
23
24//===----------------------------------------------------------------------===//
25// Conversion patterns
26//===----------------------------------------------------------------------===//
27
28namespace {
29/// Pattern to transform the `getIfCond` on operation without region into a
30/// scf.if and move the operation into the `then` region.
31template <typename OpTy>
32class ExpandIfCondition : public OpRewritePattern<OpTy> {
33 using OpRewritePattern<OpTy>::OpRewritePattern;
34
35 LogicalResult matchAndRewrite(OpTy op,
36 PatternRewriter &rewriter) const override {
37 // Early exit if there is no condition.
38 if (!op.getIfCond())
39 return failure();
40
41 IntegerAttr constAttr;
42 if (!matchPattern(op.getIfCond(), m_Constant(&constAttr))) {
43 auto ifOp = scf::IfOp::create(rewriter, op.getLoc(), TypeRange(),
44 op.getIfCond(), false);
45 rewriter.modifyOpInPlace(op, [&]() { op.getIfCondMutable().erase(0); });
46 auto thenBodyBuilder = ifOp.getThenBodyBuilder(rewriter.getListener());
47 thenBodyBuilder.clone(*op.getOperation());
48 rewriter.eraseOp(op);
49 } else {
50 if (constAttr.getInt())
51 rewriter.modifyOpInPlace(op, [&]() { op.getIfCondMutable().erase(0); });
52 else
53 rewriter.eraseOp(op);
54 }
55 return success();
56 }
57};
58} // namespace
59
61 patterns.add<ExpandIfCondition<acc::EnterDataOp>>(patterns.getContext());
62 patterns.add<ExpandIfCondition<acc::ExitDataOp>>(patterns.getContext());
63 patterns.add<ExpandIfCondition<acc::UpdateOp>>(patterns.getContext());
64}
65
66namespace {
67struct ConvertOpenACCToSCFPass
68 : public impl::ConvertOpenACCToSCFPassBase<ConvertOpenACCToSCFPass> {
69 void getDependentDialects(DialectRegistry &registry) const override {
70 Base::getDependentDialects(registry);
72 }
73
74 void runOnOperation() override;
75};
76} // namespace
77
78void ConvertOpenACCToSCFPass::runOnOperation() {
79 auto op = getOperation();
80 auto *context = op.getContext();
81
82 RewritePatternSet patterns(context);
83 ConversionTarget target(*context);
85
86 target.addLegalDialect<scf::SCFDialect>();
87 target.addLegalDialect<acc::OpenACCDialect>();
88
89 target.addDynamicallyLegalOp<acc::EnterDataOp>(
90 [](acc::EnterDataOp op) { return !op.getIfCond(); });
91
92 target.addDynamicallyLegalOp<acc::ExitDataOp>(
93 [](acc::ExitDataOp op) { return !op.getIfCond(); });
94
95 target.addDynamicallyLegalOp<acc::UpdateOp>(
96 [](acc::UpdateOp op) { return !op.getIfCond(); });
97
98 if (failed(applyPartialConversion(op, target, std::move(patterns))))
99 signalPassFailure();
100}
return success()
The DialectRegistry maps a dialect namespace to a constructor for the matching dialect.
Listener * getListener() const
Returns the current listener of this builder, or nullptr if this builder doesn't have a listener.
Definition Builders.h:323
MLIRContext * getContext() const
RewritePatternSet & add(ConstructorArg &&arg, ConstructorArgs &&...args)
Add an instance of each of the pattern types 'Ts' to the pattern list with the given arguments.
virtual void eraseOp(Operation *op)
This method erases an operation that is known to have no uses.
void modifyOpInPlace(Operation *root, CallableT &&callable)
This method is a utility wrapper around an in-place modification of an operation.
void registerAllExtensions(DialectRegistry &registry)
Register the standard OpenACC models for GPU, LLVM and MemRef IR.
detail::InFlightRemark failed(Location loc, RemarkOpts opts)
Report an optimization remark that failed.
Definition Remarks.h:734
Include the generated interface declarations.
bool matchPattern(Value value, const Pattern &pattern)
Entry point for matching a pattern over a Value.
Definition Matchers.h:490
void populateOpenACCToSCFConversionPatterns(RewritePatternSet &patterns)
Collect the patterns to convert from the OpenACC dialect to OpenACC with SCF dialect.
detail::constant_op_matcher m_Constant()
Matches a constant foldable operation.
Definition Matchers.h:369
OpRewritePattern is a wrapper around RewritePattern that allows for matching and rewriting against an...