1//===- ACCEmitRemarksData.cpp - Emit OpenACC data-mapping remarks ---------===//
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 pass emits optimization remarks describing OpenACC data-mapping clauses
10// associated with structured and unstructured data constructs.
11//
12//===----------------------------------------------------------------------===//
13
14#include "mlir/Dialect/Func/IR/FuncOps.h"
15#include "mlir/Dialect/OpenACC/Analysis/OpenACCSupport.h"
16#include "mlir/Dialect/OpenACC/Extensions/AllExtensions.h"
17#include "mlir/Dialect/OpenACC/OpenACC.h"
18#include "mlir/Dialect/OpenACC/Transforms/Passes.h"
19#include "llvm/ADT/STLExtras.h"
20#include "llvm/ADT/TypeSwitch.h"
21
22namespace mlir {
23namespace acc {
24#define GEN_PASS_DEF_ACCEMITREMARKSDATA
25#include "mlir/Dialect/OpenACC/Transforms/Passes.h.inc"
26} // namespace acc
27} // namespace mlir
28
29#define DEBUG_TYPE "acc-emit-remarks-data"
30
31using namespace mlir;
32
33namespace {
34static bool isStructuredDeclareEnter(acc::DeclareEnterOp op) {
35 return !op.getToken().getUsers().empty();
36}
37
38static StringRef getDataClauseRemarkPrefix(Operation *dataClauseOp) {
39 std::optional<acc::DataClause> clause = acc::getDataClause(accDataEntryOp: dataClauseOp);
40 if (!clause)
41 return {};
42
43 switch (*clause) {
44 case acc::DataClause::acc_copyin:
45 return "copyin(";
46 case acc::DataClause::acc_copyin_readonly:
47 return "copyin(readonly:";
48 case acc::DataClause::acc_copy:
49 return "copy(";
50 case acc::DataClause::acc_copyout:
51 return "copyout(";
52 case acc::DataClause::acc_copyout_zero:
53 return "copyout(zero:";
54 case acc::DataClause::acc_present:
55 return "present(";
56 case acc::DataClause::acc_create:
57 return "create(";
58 case acc::DataClause::acc_create_zero:
59 return "create(zero:";
60 case acc::DataClause::acc_delete:
61 return "delete(";
62 case acc::DataClause::acc_attach:
63 return "attach(";
64 case acc::DataClause::acc_detach:
65 return "detach(";
66 case acc::DataClause::acc_no_create:
67 return "no_create(";
68 case acc::DataClause::acc_private:
69 return "private(";
70 case acc::DataClause::acc_firstprivate:
71 return "firstprivate(";
72 case acc::DataClause::acc_deviceptr:
73 return "deviceptr(";
74 case acc::DataClause::acc_update_host:
75 return "update_host(";
76 case acc::DataClause::acc_update_self:
77 return "update_self(";
78 case acc::DataClause::acc_update_device:
79 return "update_device(";
80 case acc::DataClause::acc_use_device:
81 return "use_device(";
82 case acc::DataClause::acc_reduction:
83 return isa<acc::CopyinOp>(Val: dataClauseOp) ? "copy(" : "reduction(";
84 case acc::DataClause::acc_declare_device_resident:
85 return "device_resident(";
86 case acc::DataClause::acc_declare_link:
87 return "link(";
88 case acc::DataClause::acc_cache:
89 return "cache(";
90 case acc::DataClause::acc_cache_readonly:
91 return "cache(readonly:";
92 case acc::DataClause::acc_getdeviceptr:
93 return "";
94 }
95 llvm_unreachable("Unhandled data clause");
96}
97
98static bool shouldReport(Operation *op, acc::OpenACCSupport &accSupport,
99 std::string &varName) {
100 if (!isa_and_nonnull<ACC_DATA_CLAUSE_OPS>(Val: op))
101 return false;
102 // A synthetic operation exists only for the implementation's own
103 // bookkeeping, so it has nothing to tell the user about their code. It can
104 // even duplicate the report of the clause it was created to support.
105 if (acc::getSyntheticFlag(accDataClauseOp: op))
106 return false;
107 if (getDataClauseRemarkPrefix(dataClauseOp: op).empty())
108 return false;
109 if (op->getNumResults() == 0)
110 return false;
111 varName = accSupport.getVariableName(v: op->getResult(idx: 0));
112 // Not useful to report if the variable name is empty.
113 return !varName.empty();
114}
115
116static bool reportOnSameLine(Operation *lhs, Operation *rhs) {
117 return acc::getDataClause(accDataEntryOp: lhs) == acc::getDataClause(accDataEntryOp: rhs) &&
118 acc::getImplicitFlag(accDataEntryOp: lhs) == acc::getImplicitFlag(accDataEntryOp: rhs);
119}
120
121static bool reportIfNotPresent(Operation *op) {
122 switch (acc::getDataClause(accDataEntryOp: op).value()) {
123 case acc::DataClause::acc_copyin:
124 case acc::DataClause::acc_copyin_readonly:
125 case acc::DataClause::acc_copyout:
126 case acc::DataClause::acc_copyout_zero:
127 case acc::DataClause::acc_copy:
128 case acc::DataClause::acc_create:
129 case acc::DataClause::acc_create_zero:
130 case acc::DataClause::acc_no_create:
131 return true;
132 case acc::DataClause::acc_present:
133 case acc::DataClause::acc_delete:
134 case acc::DataClause::acc_attach:
135 case acc::DataClause::acc_detach:
136 case acc::DataClause::acc_private:
137 case acc::DataClause::acc_firstprivate:
138 case acc::DataClause::acc_deviceptr:
139 case acc::DataClause::acc_getdeviceptr:
140 case acc::DataClause::acc_update_host:
141 case acc::DataClause::acc_update_self:
142 case acc::DataClause::acc_update_device:
143 case acc::DataClause::acc_use_device:
144 case acc::DataClause::acc_declare_device_resident:
145 case acc::DataClause::acc_declare_link:
146 case acc::DataClause::acc_cache:
147 case acc::DataClause::acc_cache_readonly:
148 return false;
149 case acc::DataClause::acc_reduction:
150 return isa<acc::CopyinOp>(Val: op);
151 }
152 llvm_unreachable("Unhandled data clause");
153}
154
155static void emitDataMappingRemarks(ValueRange mappingOperands,
156 StringRef directivePrefix,
157 acc::OpenACCSupport &accSupport) {
158 if (mappingOperands.empty())
159 return;
160
161 // Collect reportable ops with their variable names once so sorting and
162 // remark formatting do not recompute them.
163 struct MappingInfo {
164 Operation *op;
165 std::string varName;
166 };
167 SmallVector<MappingInfo, 8> mappingOps;
168 mappingOps.reserve(N: mappingOperands.size());
169 for (Value operand : mappingOperands) {
170 Operation *defOp = operand.getDefiningOp();
171 std::string varName;
172 if (!shouldReport(op: defOp, accSupport, varName))
173 continue;
174 mappingOps.push_back(Elt: {.op: defOp, .varName: std::move(varName)});
175 }
176 if (mappingOps.empty())
177 return;
178
179 llvm::sort(C&: mappingOps, Comp: [](const MappingInfo &lhs, const MappingInfo &rhs) {
180 acc::DataClause lhsClause = acc::getDataClause(accDataEntryOp: lhs.op).value();
181 acc::DataClause rhsClause = acc::getDataClause(accDataEntryOp: rhs.op).value();
182 if (lhsClause == rhsClause) {
183 bool lhsImplicit = acc::getImplicitFlag(accDataEntryOp: lhs.op);
184 bool rhsImplicit = acc::getImplicitFlag(accDataEntryOp: rhs.op);
185 if (lhsImplicit == rhsImplicit)
186 return lhs.varName < rhs.varName;
187 return lhsImplicit < rhsImplicit;
188 }
189 return lhsClause < rhsClause;
190 });
191
192 for (auto *it = mappingOps.begin(); it != mappingOps.end(); ++it) {
193 Operation *op = it->op;
194
195 SmallVector<MappingInfo *, 4> groupedOps = {it};
196 while (std::next(x: it) != mappingOps.end() &&
197 reportOnSameLine(lhs: op, rhs: std::next(x: it)->op)) {
198 ++it;
199 groupedOps.push_back(Elt: it);
200 }
201
202 // Anchor the remark on the data-clause op so its source location is used.
203 accSupport.emitRemark(
204 op,
205 messageFn: [&]() {
206 std::string message = "Generating ";
207 message += directivePrefix.str();
208 if (op->getDiscardableAttr(name: acc::getFromDefaultClauseAttrName()))
209 message += "default ";
210 else if (acc::getImplicitFlag(accDataEntryOp: op))
211 message += "implicit ";
212 message += getDataClauseRemarkPrefix(dataClauseOp: op).str();
213 message += groupedOps.front()->varName;
214 for (MappingInfo *grouped : llvm::drop_begin(RangeOrContainer&: groupedOps)) {
215 message += ", ";
216 message += grouped->varName;
217 }
218 message += ")";
219 if (reportIfNotPresent(op))
220 message += " [if not already present]";
221 return message;
222 },
223 DEBUG_TYPE);
224 }
225}
226
227class ACCEmitRemarksData
228 : public acc::impl::ACCEmitRemarksDataBase<ACCEmitRemarksData> {
229public:
230 using ACCEmitRemarksDataBase<ACCEmitRemarksData>::ACCEmitRemarksDataBase;
231
232 void getDependentDialects(DialectRegistry &registry) const override {
233 ACCEmitRemarksDataBase<ACCEmitRemarksData>::getDependentDialects(registry);
234 acc::registerAllExtensions(registry);
235 }
236
237 void runOnOperation() override {
238 func::FuncOp func = getOperation();
239
240 auto cachedAnalysis = getCachedParentAnalysis<acc::OpenACCSupport>();
241 acc::OpenACCSupport &accSupport = cachedAnalysis
242 ? cachedAnalysis->get()
243 : getAnalysis<acc::OpenACCSupport>();
244
245 // Remark emission policy:
246 // - update / enter (structured or unstructured): report
247 // - exit: only unstructured (structured exit was covered at enter)
248 // - declare: only structured declare enter; unstructured declare
249 // registration does not emit mapping remarks.
250 func.walk(callback: [&](Operation *op) {
251 TypeSwitch<Operation *>(op)
252 .Case<acc::DataOp, acc::KernelEnvironmentOp>(caseFn: [&](auto dataOp) {
253 emitDataMappingRemarks(dataOp.getDataClauseOperands(), "",
254 accSupport);
255 })
256 .Case<acc::EnterDataOp>(caseFn: [&](acc::EnterDataOp enterOp) {
257 emitDataMappingRemarks(mappingOperands: enterOp.getDataClauseOperands(),
258 directivePrefix: "enter data ", accSupport);
259 })
260 .Case<acc::ExitDataOp>(caseFn: [&](acc::ExitDataOp exitOp) {
261 emitDataMappingRemarks(mappingOperands: exitOp.getDataClauseOperands(), directivePrefix: "exit data ",
262 accSupport);
263 })
264 .Case<acc::UpdateOp>(caseFn: [&](acc::UpdateOp updateOp) {
265 emitDataMappingRemarks(mappingOperands: updateOp.getDataClauseOperands(), directivePrefix: "",
266 accSupport);
267 })
268 .Case<acc::DeclareEnterOp>(caseFn: [&](acc::DeclareEnterOp declareEnterOp) {
269 if (isStructuredDeclareEnter(op: declareEnterOp))
270 emitDataMappingRemarks(mappingOperands: declareEnterOp.getDataClauseOperands(), directivePrefix: "",
271 accSupport);
272 });
273 });
274 }
275};
276} // namespace
277