MLIR 24.0.0git
LegalizeDataValues.cpp
Go to the documentation of this file.
1//===- LegalizeDataValues.cpp - -------------------------------------------===//
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
11
14#include "mlir/IR/Dominance.h"
16#include "mlir/Pass/Pass.h"
18#include "llvm/Support/ErrorHandling.h"
19
20namespace mlir {
21namespace acc {
22#define GEN_PASS_DEF_LEGALIZEDATAVALUESINREGION
23#include "mlir/Dialect/OpenACC/Transforms/Passes.h.inc"
24} // namespace acc
25} // namespace mlir
26
27using namespace mlir;
28
29namespace {
30static bool insideAccComputeRegion(mlir::Operation *op) {
32}
33
34static void collectVars(mlir::ValueRange operands,
35 llvm::SmallVector<std::pair<Value, Value>> &values,
36 bool hostToDevice) {
37 for (auto operand : operands) {
38 Value var = acc::getVar(operand.getDefiningOp());
39 Value accVar = acc::getAccVar(operand.getDefiningOp());
40 if (var && accVar) {
41 if (hostToDevice)
42 values.push_back({var, accVar});
43 else
44 values.push_back({accVar, var});
45 }
46 }
47}
48
49template <typename Op>
50static void replaceAllUsesInAccComputeRegionsWith(Value orig, Value replacement,
51 Region &outerRegion) {
52 for (auto &use : llvm::make_early_inc_range(orig.getUses())) {
53 if (outerRegion.isAncestor(use.getOwner()->getParentRegion())) {
54 if constexpr (std::is_same_v<Op, acc::DataOp> ||
55 std::is_same_v<Op, acc::DeclareOp>) {
56 // For data construct regions, only replace uses in contained compute
57 // regions.
58 if (insideAccComputeRegion(use.getOwner())) {
59 use.set(replacement);
60 }
61 } else {
62 use.set(replacement);
63 }
64 }
65 }
66}
67
68template <typename Op>
69static void replaceAllUsesInUnstructuredComputeRegionWith(
70 Op &op, llvm::SmallVector<std::pair<Value, Value>> &values,
71 DominanceInfo &domInfo, PostDominanceInfo &postDomInfo) {
72
74 if constexpr (std::is_same_v<Op, acc::DeclareEnterOp>) {
75 // For declare enter/exit pairs, collect all exit ops
76 for (auto *user : op.getToken().getUsers()) {
77 if (auto declareExit = dyn_cast<acc::DeclareExitOp>(user))
78 exitOps.push_back(declareExit);
79 }
80 if (exitOps.empty())
81 return;
82 }
83
84 for (auto p : values) {
85 Value hostVal = std::get<0>(p);
86 Value deviceVal = std::get<1>(p);
87 for (auto &use : llvm::make_early_inc_range(hostVal.getUses())) {
88 Operation *owner = use.getOwner();
89
90 // Check It's the case that the acc entry operation dominates the use.
91 if (!domInfo.dominates(op.getOperation(), owner))
92 continue;
93
94 // Check It's the case that at least one of the acc exit operations
95 // post-dominates the use
96 bool hasPostDominatingExit = false;
97 for (auto *exit : exitOps) {
98 if (postDomInfo.postDominates(exit, owner)) {
99 hasPostDominatingExit = true;
100 break;
101 }
102 }
103
104 if (!hasPostDominatingExit)
105 continue;
106
107 if (insideAccComputeRegion(owner))
108 use.set(deviceVal);
109 }
110 }
111}
112
113template <typename Op>
114static void
115collectAndReplaceInRegion(Op &op, bool hostToDevice,
116 DominanceInfo *domInfo = nullptr,
117 PostDominanceInfo *postDomInfo = nullptr) {
119
120 if constexpr (std::is_same_v<Op, acc::LoopOp>) {
121 collectVars(op.getReductionOperands(), values, hostToDevice);
122 collectVars(op.getPrivateOperands(), values, hostToDevice);
123 } else {
124 collectVars(op.getDataClauseOperands(), values, hostToDevice);
125 if constexpr (!std::is_same_v<Op, acc::KernelsOp> &&
126 !std::is_same_v<Op, acc::DataOp> &&
127 !std::is_same_v<Op, acc::DeclareOp> &&
128 !std::is_same_v<Op, acc::HostDataOp> &&
129 !std::is_same_v<Op, acc::DeclareEnterOp>) {
130 collectVars(op.getReductionOperands(), values, hostToDevice);
131 collectVars(op.getPrivateOperands(), values, hostToDevice);
132 collectVars(op.getFirstprivateOperands(), values, hostToDevice);
133 }
134 }
135
136 if constexpr (std::is_same_v<Op, acc::DeclareEnterOp>) {
137 assert(domInfo && postDomInfo &&
138 "Dominance info required for DeclareEnterOp");
139 replaceAllUsesInUnstructuredComputeRegionWith<Op>(op, values, *domInfo,
140 *postDomInfo);
141 } else {
142 for (auto p : values) {
143 replaceAllUsesInAccComputeRegionsWith<Op>(std::get<0>(p), std::get<1>(p),
144 op.getRegion());
145 }
146 }
147}
148
149class LegalizeDataValuesInRegion
151 LegalizeDataValuesInRegion> {
152public:
153 using LegalizeDataValuesInRegionBase<
154 LegalizeDataValuesInRegion>::LegalizeDataValuesInRegionBase;
155
156 void getDependentDialects(DialectRegistry &registry) const override {
157 LegalizeDataValuesInRegionBase<
158 LegalizeDataValuesInRegion>::getDependentDialects(registry);
160 }
161
162 void runOnOperation() override {
163 FunctionOpInterface funcOp = getOperation();
164 bool replaceHostVsDevice = this->hostToDevice.getValue();
165
166 // Initialize dominance info
167 DominanceInfo domInfo;
168 PostDominanceInfo postDomInfo;
169 bool computedDomInfo = false;
170
171 funcOp.walk([&](Operation *op) {
172 if (!isa<ACC_COMPUTE_CONSTRUCT_AND_LOOP_OPS>(*op) &&
173 !(isa<ACC_DATA_CONSTRUCT_STRUCTURED_OPS>(*op) &&
174 applyToAccDataConstruct) &&
175 !isa<acc::DeclareEnterOp>(*op))
176 return;
177
178 if (auto parallelOp = dyn_cast<acc::ParallelOp>(*op)) {
179 collectAndReplaceInRegion(parallelOp, replaceHostVsDevice);
180 } else if (auto serialOp = dyn_cast<acc::SerialOp>(*op)) {
181 collectAndReplaceInRegion(serialOp, replaceHostVsDevice);
182 } else if (auto kernelsOp = dyn_cast<acc::KernelsOp>(*op)) {
183 collectAndReplaceInRegion(kernelsOp, replaceHostVsDevice);
184 } else if (auto loopOp = dyn_cast<acc::LoopOp>(*op)) {
185 collectAndReplaceInRegion(loopOp, replaceHostVsDevice);
186 } else if (auto dataOp = dyn_cast<acc::DataOp>(*op)) {
187 collectAndReplaceInRegion(dataOp, replaceHostVsDevice);
188 } else if (auto declareOp = dyn_cast<acc::DeclareOp>(*op)) {
189 collectAndReplaceInRegion(declareOp, replaceHostVsDevice);
190 } else if (auto hostDataOp = dyn_cast<acc::HostDataOp>(*op)) {
191 collectAndReplaceInRegion(hostDataOp, replaceHostVsDevice);
192 } else if (auto declareEnterOp = dyn_cast<acc::DeclareEnterOp>(*op)) {
193 if (!computedDomInfo) {
194 domInfo = DominanceInfo(funcOp);
195 postDomInfo = PostDominanceInfo(funcOp);
196 computedDomInfo = true;
197 }
198 collectAndReplaceInRegion(declareEnterOp, replaceHostVsDevice, &domInfo,
199 &postDomInfo);
200 } else {
201 llvm_unreachable("unsupported acc region op");
202 }
203 });
204 }
205};
206} // end anonymous namespace
*if copies could not be generated due to yet unimplemented cases *copyInPlacementStart and copyOutPlacementStart in copyPlacementBlock *specify the insertion points where the incoming copies and outgoing should be the output argument nBegin is set to its * replacement(set to `begin` if no invalidation happens). Since outgoing *copies could have been inserted at `end`
A class for computing basic dominance information.
Definition Dominance.h:143
bool dominates(Operation *a, Operation *b) const
Return true if operation A dominates operation B, i.e.
Definition Dominance.h:161
This provides public APIs that all operations should have.
Operation * getOperation()
Inherit getOperation from OpState.
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
OpTy getParentOfType()
Return the closest surrounding parent operation that is of type 'OpTy'.
Definition Operation.h:255
A class for computing basic postdominance information.
Definition Dominance.h:207
bool postDominates(Operation *a, Operation *b) const
Return true if operation A postdominates operation B.
Definition Dominance.h:216
This class contains a list of basic blocks and a link to the parent operation it is attached to.
Definition Region.h:26
bool isAncestor(Region *other)
Return true if this region is ancestor of the other region.
Definition Region.h:234
This class provides an abstraction over the different types of ranges over Values.
Definition ValueRange.h:392
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition Value.h:96
use_range getUses() const
Returns a range of all uses, which is useful for iterating over all uses.
Definition Value.h:188
#define ACC_COMPUTE_CONSTRUCT_OPS
Definition OpenACC.h:59
mlir::Value getAccVar(mlir::Operation *accDataClauseOp)
Used to obtain the accVar from a data clause operation.
Definition OpenACC.cpp:5073
mlir::Value getVar(mlir::Operation *accDataClauseOp)
Used to obtain the var from a data clause operation.
Definition OpenACC.cpp:5041
void registerAllExtensions(DialectRegistry &registry)
Register the standard OpenACC models for GPU, LLVM and MemRef IR.
Include the generated interface declarations.