MLIR  21.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 
13 #include "mlir/IR/Matchers.h"
15 
16 namespace mlir {
17 #define GEN_PASS_DEF_CONVERTOPENACCTOSCFPASS
18 #include "mlir/Conversion/Passes.h.inc"
19 } // namespace mlir
20 
21 using namespace mlir;
22 
23 //===----------------------------------------------------------------------===//
24 // Conversion patterns
25 //===----------------------------------------------------------------------===//
26 
27 namespace {
28 /// Pattern to transform the `getIfCond` on operation without region into a
29 /// scf.if and move the operation into the `then` region.
30 template <typename OpTy>
31 class ExpandIfCondition : public OpRewritePattern<OpTy> {
33 
34  LogicalResult matchAndRewrite(OpTy op,
35  PatternRewriter &rewriter) const override {
36  // Early exit if there is no condition.
37  if (!op.getIfCond())
38  return failure();
39 
40  IntegerAttr constAttr;
41  if (!matchPattern(op.getIfCond(), m_Constant(&constAttr))) {
42  auto ifOp = rewriter.create<scf::IfOp>(op.getLoc(), TypeRange(),
43  op.getIfCond(), false);
44  rewriter.modifyOpInPlace(op, [&]() { op.getIfCondMutable().erase(0); });
45  auto thenBodyBuilder = ifOp.getThenBodyBuilder(rewriter.getListener());
46  thenBodyBuilder.clone(*op.getOperation());
47  rewriter.eraseOp(op);
48  } else {
49  if (constAttr.getInt())
50  rewriter.modifyOpInPlace(op, [&]() { op.getIfCondMutable().erase(0); });
51  else
52  rewriter.eraseOp(op);
53  }
54  return success();
55  }
56 };
57 } // namespace
58 
60  patterns.add<ExpandIfCondition<acc::EnterDataOp>>(patterns.getContext());
61  patterns.add<ExpandIfCondition<acc::ExitDataOp>>(patterns.getContext());
62  patterns.add<ExpandIfCondition<acc::UpdateOp>>(patterns.getContext());
63 }
64 
65 namespace {
66 struct ConvertOpenACCToSCFPass
67  : public impl::ConvertOpenACCToSCFPassBase<ConvertOpenACCToSCFPass> {
68  void runOnOperation() override;
69 };
70 } // namespace
71 
72 void ConvertOpenACCToSCFPass::runOnOperation() {
73  auto op = getOperation();
74  auto *context = op.getContext();
75 
76  RewritePatternSet patterns(context);
77  ConversionTarget target(*context);
79 
80  target.addLegalDialect<scf::SCFDialect>();
81  target.addLegalDialect<acc::OpenACCDialect>();
82 
83  target.addDynamicallyLegalOp<acc::EnterDataOp>(
84  [](acc::EnterDataOp op) { return !op.getIfCond(); });
85 
86  target.addDynamicallyLegalOp<acc::ExitDataOp>(
87  [](acc::ExitDataOp op) { return !op.getIfCond(); });
88 
89  target.addDynamicallyLegalOp<acc::UpdateOp>(
90  [](acc::UpdateOp op) { return !op.getIfCond(); });
91 
92  if (failed(applyPartialConversion(op, target, std::move(patterns))))
93  signalPassFailure();
94 }
This class describes a specific conversion target.
Listener * getListener() const
Returns the current listener of this builder, or nullptr if this builder doesn't have a listener.
Definition: Builders.h:318
Operation * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
Definition: Builders.cpp:452
A special type of RewriterBase that coordinates the application of a rewrite pattern on the current I...
Definition: PatternMatch.h:748
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.
Definition: PatternMatch.h:593
This class provides an abstraction over the various different ranges of value types.
Definition: TypeRange.h:37
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.
const FrozenRewritePatternSet & patterns
detail::constant_op_matcher m_Constant()
Matches a constant foldable operation.
Definition: Matchers.h:369
LogicalResult applyPartialConversion(ArrayRef< Operation * > ops, const ConversionTarget &target, const FrozenRewritePatternSet &patterns, ConversionConfig config=ConversionConfig())
Below we define several entry points for operation conversion.
OpRewritePattern is a wrapper around RewritePattern that allows for matching and rewriting against an...
Definition: PatternMatch.h:314