1//===- ACCEmitRemarksPrivate.cpp - Emit OpenACC privatization 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 remarks describing the private and firstprivate variables
10// associated with OpenACC compute and loop 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/StringExtras.h"
20#include "llvm/ADT/Twine.h"
21#include "llvm/ADT/TypeSwitch.h"
22
23namespace mlir {
24namespace acc {
25#define GEN_PASS_DEF_ACCEMITREMARKSPRIVATE
26#include "mlir/Dialect/OpenACC/Transforms/Passes.h.inc"
27} // namespace acc
28} // namespace mlir
29
30#define DEBUG_TYPE "acc-emit-remarks-private"
31
32using namespace mlir;
33
34namespace {
35template <typename OpTy>
36static void reportPrivatization(Operation *accOp, ValueRange operands,
37 acc::OpenACCSupport &accSupport,
38 StringRef clause) {
39 SmallVector<std::string> implicitNames;
40 SmallVector<std::string> explicitNames;
41 for (Value operand : operands) {
42 auto op = cast<OpTy>(operand.getDefiningOp());
43 std::string varName = accSupport.getVariableName(v: op.getAccVar());
44 (op.getImplicit() ? implicitNames : explicitNames)
45 .push_back(varName.empty() ? "<unknown>" : varName);
46 }
47
48 if (!implicitNames.empty())
49 accSupport.emitRemark(
50 accOp,
51 [clause, names = std::move(implicitNames)]() {
52 return (Twine("Generating implicit ") + clause + "(" +
53 llvm::join(R: names, Separator: ",") + ")")
54 .str();
55 },
56 DEBUG_TYPE);
57
58 if (!explicitNames.empty())
59 accSupport.emitRemark(
60 accOp,
61 [clause, names = std::move(explicitNames)]() {
62 return (Twine("Generating ") + clause + "(" + llvm::join(R: names, Separator: ",") +
63 ")")
64 .str();
65 },
66 DEBUG_TYPE);
67}
68
69template <typename OpTy>
70static void emitRemarksForACCOp(OpTy accOp, acc::OpenACCSupport &accSupport) {
71 reportPrivatization<acc::FirstprivateOp>(
72 accOp, accOp.getFirstprivateOperands(), accSupport, "firstprivate");
73 reportPrivatization<acc::PrivateOp>(accOp, accOp.getPrivateOperands(),
74 accSupport, "private");
75}
76
77class ACCEmitRemarksPrivate
78 : public acc::impl::ACCEmitRemarksPrivateBase<ACCEmitRemarksPrivate> {
79public:
80 using ACCEmitRemarksPrivateBase<
81 ACCEmitRemarksPrivate>::ACCEmitRemarksPrivateBase;
82
83 void getDependentDialects(DialectRegistry &registry) const override {
84 ACCEmitRemarksPrivateBase<ACCEmitRemarksPrivate>::getDependentDialects(
85 registry);
86 acc::registerAllExtensions(registry);
87 }
88
89 void runOnOperation() override {
90 func::FuncOp func = getOperation();
91
92 auto cachedAnalysis = getCachedParentAnalysis<acc::OpenACCSupport>();
93 acc::OpenACCSupport &accSupport = cachedAnalysis
94 ? cachedAnalysis->get()
95 : getAnalysis<acc::OpenACCSupport>();
96
97 func.walk(callback: [&](Operation *op) {
98 TypeSwitch<Operation *>(op).Case<ACC_COMPUTE_CONSTRUCT_AND_LOOP_OPS>(
99 caseFn: [&](auto constructOp) {
100 emitRemarksForACCOp(constructOp, accSupport);
101 });
102 });
103 }
104};
105} // namespace
106