MLIR  19.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"
15 #include "mlir/Pass/Pass.h"
17 
18 namespace mlir {
19 #define GEN_PASS_DEF_CONVERTOPENACCTOSCF
20 #include "mlir/Conversion/Passes.h.inc"
21 } // namespace mlir
22 
23 using namespace mlir;
24 
25 //===----------------------------------------------------------------------===//
26 // Conversion patterns
27 //===----------------------------------------------------------------------===//
28 
29 namespace {
30 /// Pattern to transform the `getIfCond` on operation without region into a
31 /// scf.if and move the operation into the `then` region.
32 template <typename OpTy>
33 class ExpandIfCondition : public OpRewritePattern<OpTy> {
35 
36  LogicalResult matchAndRewrite(OpTy op,
37  PatternRewriter &rewriter) const override {
38  // Early exit if there is no condition.
39  if (!op.getIfCond())
40  return failure();
41 
42  IntegerAttr constAttr;
43  if (!matchPattern(op.getIfCond(), m_Constant(&constAttr))) {
44  auto ifOp = rewriter.create<scf::IfOp>(op.getLoc(), TypeRange(),
45  op.getIfCond(), false);
46  rewriter.modifyOpInPlace(op, [&]() { op.getIfCondMutable().erase(0); });
47  auto thenBodyBuilder = ifOp.getThenBodyBuilder(rewriter.getListener());
48  thenBodyBuilder.clone(*op.getOperation());
49  rewriter.eraseOp(op);
50  } else {
51  if (constAttr.getInt())
52  rewriter.modifyOpInPlace(op, [&]() { op.getIfCondMutable().erase(0); });
53  else
54  rewriter.eraseOp(op);
55  }
56  return success();
57  }
58 };
59 } // namespace
60 
62  patterns.add<ExpandIfCondition<acc::EnterDataOp>>(patterns.getContext());
63  patterns.add<ExpandIfCondition<acc::ExitDataOp>>(patterns.getContext());
64  patterns.add<ExpandIfCondition<acc::UpdateOp>>(patterns.getContext());
65 }
66 
67 namespace {
68 struct ConvertOpenACCToSCFPass
69  : public impl::ConvertOpenACCToSCFBase<ConvertOpenACCToSCFPass> {
70  void runOnOperation() override;
71 };
72 } // namespace
73 
74 void ConvertOpenACCToSCFPass::runOnOperation() {
75  auto op = getOperation();
76  auto *context = op.getContext();
77 
78  RewritePatternSet patterns(context);
79  ConversionTarget target(*context);
81 
82  target.addLegalDialect<scf::SCFDialect>();
83  target.addLegalDialect<acc::OpenACCDialect>();
84 
85  target.addDynamicallyLegalOp<acc::EnterDataOp>(
86  [](acc::EnterDataOp op) { return !op.getIfCond(); });
87 
88  target.addDynamicallyLegalOp<acc::ExitDataOp>(
89  [](acc::ExitDataOp op) { return !op.getIfCond(); });
90 
91  target.addDynamicallyLegalOp<acc::UpdateOp>(
92  [](acc::UpdateOp op) { return !op.getIfCond(); });
93 
94  if (failed(applyPartialConversion(op, target, std::move(patterns))))
95  signalPassFailure();
96 }
97 
98 std::unique_ptr<OperationPass<ModuleOp>> mlir::createConvertOpenACCToSCFPass() {
99  return std::make_unique<ConvertOpenACCToSCFPass>();
100 }
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:322
Operation * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
Definition: Builders.cpp:464
MLIRContext * getContext()
Return the context this operation is associated with.
Definition: Operation.h:216
Location getLoc()
The source location the operation was defined or derived from.
Definition: Operation.h:223
void erase()
Remove this operation from its parent block and delete it.
Definition: Operation.cpp:539
A special type of RewriterBase that coordinates the application of a rewrite pattern on the current I...
Definition: PatternMatch.h:785
MLIRContext * getContext() const
Definition: PatternMatch.h:822
RewritePatternSet & add(ConstructorArg &&arg, ConstructorArgs &&...args)
Add an instance of each of the pattern types 'Ts' to the pattern list with the given arguments.
Definition: PatternMatch.h:846
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:630
This class provides an abstraction over the various different ranges of value types.
Definition: TypeRange.h:36
Include the generated interface declarations.
bool matchPattern(Value value, const Pattern &pattern)
Entry point for matching a pattern over a Value.
Definition: Matchers.h:401
LogicalResult failure(bool isFailure=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:62
std::unique_ptr< OperationPass< ModuleOp > > createConvertOpenACCToSCFPass()
Create a pass to convert the OpenACC dialect into the LLVMIR dialect.
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:56
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:310
LogicalResult applyPartialConversion(ArrayRef< Operation * > ops, const ConversionTarget &target, const FrozenRewritePatternSet &patterns, ConversionConfig config=ConversionConfig())
Below we define several entry points for operation conversion.
bool failed(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a failure value.
Definition: LogicalResult.h:72
This class represents an efficient way to signal success or failure.
Definition: LogicalResult.h:26
OpRewritePattern is a wrapper around RewritePattern that allows for matching and rewriting against an...
Definition: PatternMatch.h:358