MLIR 23.0.0git
ACCSpecializePatterns.h
Go to the documentation of this file.
1//===- ACCSpecializePatterns.h - Common ACC Specialization Patterns ------===//
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//
9// This file contains common rewrite pattern templates used by both
10// ACCSpecializeForHost and ACCSpecializeForDevice passes.
11//
12// The patterns provide the following transformations:
13//
14// - ACCOpReplaceWithVarConversion<OpTy>: Replaces a data entry operation
15// with its var operand. Used for ops like acc.copyin, acc.create, etc.
16//
17// - ACCOpEraseConversion<OpTy>: Simply erases an operation. Used for
18// data exit ops like acc.copyout, acc.delete, and runtime ops.
19//
20// - ACCRegionUnwrapConversion<OpTy>: Inlines the region of an operation
21// and erases the wrapper. Used for structured data constructs
22// (acc.data, acc.host_data) and compute constructs (acc.parallel, etc.)
23//
24// - ACCDeclareEnterOpConversion: Erases acc.declare_enter and its
25// associated acc.declare_exit operation.
26//
27//===----------------------------------------------------------------------===//
28
29#ifndef MLIR_DIALECT_OPENACC_TRANSFORMS_ACCSPECIALIZEPATTERNS_H
30#define MLIR_DIALECT_OPENACC_TRANSFORMS_ACCSPECIALIZEPATTERNS_H
31
34
35namespace mlir {
36namespace acc {
37
38//===----------------------------------------------------------------------===//
39// Generic pattern templates for ACC specialization
40//===----------------------------------------------------------------------===//
41
42/// Pattern to replace an ACC op with its var operand.
43/// Used for data entry ops like acc.copyin, acc.create, acc.attach, etc.
44template <typename OpTy>
47
48public:
49 LogicalResult matchAndRewrite(OpTy op,
50 PatternRewriter &rewriter) const override {
51 // Defer if any user validates its operands as data entry ops (e.g.
52 // acc.data, acc.parallel, acc.declare_enter). Replacing the data entry
53 // op before these are processed would leave them with an invalid operand
54 // and fail IR verification.
55 for (Operation *user : op->getUsers()) {
56 if (isa<acc::DataOp, acc::HostDataOp, acc::KernelEnvironmentOp,
57 acc::ParallelOp, acc::SerialOp, acc::KernelsOp,
58 acc::DeclareEnterOp, acc::EnterDataOp>(user))
59 return failure();
60 }
61 // Replace this op with its var operand; it's possible the op has no uses
62 // if the op that had previously used it was already converted.
63 if (op->use_empty())
64 rewriter.eraseOp(op);
65 else
66 rewriter.replaceOp(op, op.getVar());
67 return success();
68 }
69};
70
71/// Pattern to simply erase an ACC op (for ops with no results).
72/// Used for data exit ops like acc.copyout, acc.delete, acc.detach, etc.
73template <typename OpTy>
76
77public:
78 LogicalResult matchAndRewrite(OpTy op,
79 PatternRewriter &rewriter) const override {
80 assert(op->getNumResults() == 0 && "expected op with no results");
81 rewriter.eraseOp(op);
82 return success();
83 }
84};
85
86/// Pattern to unwrap a region from an ACC op and erase the wrapper.
87/// Moves the region's contents to the parent block and removes the wrapper op.
88/// Used for structured data constructs (acc.data, acc.host_data,
89/// acc.kernel_environment, acc.declare) and compute constructs (acc.parallel,
90/// acc.serial, acc.kernels).
91template <typename OpTy>
94
95public:
96 LogicalResult matchAndRewrite(OpTy op,
97 PatternRewriter &rewriter) const override {
98 assert(op.getRegion().hasOneBlock() && "expected one block");
99 Block *block = &op.getRegion().front();
100 // Erase the terminator (acc.yield or acc.terminator) before unwrapping
101 rewriter.eraseOp(block->getTerminator());
102 rewriter.inlineBlockBefore(block, op);
103 rewriter.eraseOp(op);
104 return success();
105 }
106};
107
108/// Pattern to erase acc.declare_enter and its associated acc.declare_exit.
109/// The declare_enter produces a token that is consumed by declare_exit.
111 : public OpRewritePattern<acc::DeclareEnterOp> {
112 using OpRewritePattern<acc::DeclareEnterOp>::OpRewritePattern;
113
114public:
115 LogicalResult matchAndRewrite(acc::DeclareEnterOp op,
116 PatternRewriter &rewriter) const override {
117 // If the enter token is used by an exit, erase exit first.
118 if (!op->use_empty()) {
119 assert(op->hasOneUse() && "expected one use");
120 auto exitOp = dyn_cast<acc::DeclareExitOp>(*op->getUsers().begin());
121 assert(exitOp && "expected declare exit op");
122 rewriter.eraseOp(exitOp);
123 }
124 rewriter.eraseOp(op);
125 return success();
126 }
127};
128
129} // namespace acc
130} // namespace mlir
131
132#endif // MLIR_DIALECT_OPENACC_TRANSFORMS_ACCSPECIALIZEPATTERNS_H
return success()
Block represents an ordered list of Operations.
Definition Block.h:33
Operation * getTerminator()
Get the terminator operation of this block.
Definition Block.cpp:249
Operation is the basic unit of execution within MLIR.
Definition Operation.h:88
A special type of RewriterBase that coordinates the application of a rewrite pattern on the current I...
virtual void replaceOp(Operation *op, ValueRange newValues)
Replace the results of the given (original) operation with the specified list of values (replacements...
virtual void eraseOp(Operation *op)
This method erases an operation that is known to have no uses.
virtual void inlineBlockBefore(Block *source, Block *dest, Block::iterator before, ValueRange argValues={})
Inline the operations of block 'source' into block 'dest' before the given position.
Pattern to erase acc.declare_enter and its associated acc.declare_exit.
LogicalResult matchAndRewrite(acc::DeclareEnterOp op, PatternRewriter &rewriter) const override
Pattern to simply erase an ACC op (for ops with no results).
LogicalResult matchAndRewrite(OpTy op, PatternRewriter &rewriter) const override
Pattern to replace an ACC op with its var operand.
LogicalResult matchAndRewrite(OpTy op, PatternRewriter &rewriter) const override
Pattern to unwrap a region from an ACC op and erase the wrapper.
LogicalResult matchAndRewrite(OpTy op, PatternRewriter &rewriter) const override
Include the generated interface declarations.
OpRewritePattern(MLIRContext *context, PatternBenefit benefit=1, ArrayRef< StringRef > generatedNames={})