MLIR 22.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 // Replace this op with its var operand; it's possible the op has no uses
52 // if the op that had previously used it was already converted.
53 if (op->use_empty())
54 rewriter.eraseOp(op);
55 else
56 rewriter.replaceOp(op, op.getVar());
57 return success();
58 }
59};
60
61/// Pattern to simply erase an ACC op (for ops with no results).
62/// Used for data exit ops like acc.copyout, acc.delete, acc.detach, etc.
63template <typename OpTy>
66
67public:
68 LogicalResult matchAndRewrite(OpTy op,
69 PatternRewriter &rewriter) const override {
70 assert(op->getNumResults() == 0 && "expected op with no results");
71 rewriter.eraseOp(op);
72 return success();
73 }
74};
75
76/// Pattern to unwrap a region from an ACC op and erase the wrapper.
77/// Moves the region's contents to the parent block and removes the wrapper op.
78/// Used for structured data constructs (acc.data, acc.host_data,
79/// acc.kernel_environment, acc.declare) and compute constructs (acc.parallel,
80/// acc.serial, acc.kernels).
81template <typename OpTy>
84
85public:
86 LogicalResult matchAndRewrite(OpTy op,
87 PatternRewriter &rewriter) const override {
88 assert(op.getRegion().hasOneBlock() && "expected one block");
89 Block *block = &op.getRegion().front();
90 // Erase the terminator (acc.yield or acc.terminator) before unwrapping
91 rewriter.eraseOp(block->getTerminator());
92 rewriter.inlineBlockBefore(block, op);
93 rewriter.eraseOp(op);
94 return success();
95 }
96};
97
98/// Pattern to erase acc.declare_enter and its associated acc.declare_exit.
99/// The declare_enter produces a token that is consumed by declare_exit.
101 : public OpRewritePattern<acc::DeclareEnterOp> {
102 using OpRewritePattern<acc::DeclareEnterOp>::OpRewritePattern;
103
104public:
105 LogicalResult matchAndRewrite(acc::DeclareEnterOp op,
106 PatternRewriter &rewriter) const override {
107 // If the enter token is used by an exit, erase exit first.
108 if (!op->use_empty()) {
109 assert(op->hasOneUse() && "expected one use");
110 auto exitOp = dyn_cast<acc::DeclareExitOp>(*op->getUsers().begin());
111 assert(exitOp && "expected declare exit op");
112 rewriter.eraseOp(exitOp);
113 }
114 rewriter.eraseOp(op);
115 return success();
116 }
117};
118
119} // namespace acc
120} // namespace mlir
121
122#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:244
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={})