-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathCallExprRewriter.cpp
More file actions
170 lines (151 loc) · 5.19 KB
/
CallExprRewriter.cpp
File metadata and controls
170 lines (151 loc) · 5.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//===--------------- CallExprRewriter.cpp ---------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "RuleInfra/CallExprRewriter.h"
#include "RuleInfra/CallExprRewriterCommon.h"
#include <memory>
namespace clang {
namespace dpct {
std::shared_ptr<CallExprRewriterFactoryBase>
createUserDefinedRewriterFactory(const std::string &Source, MetaRuleObject &R) {
return std::make_shared<UserDefinedRewriterFactory>(R);
}
std::shared_ptr<CallExprRewriterFactoryBase>
createUserDefinedMethodRewriterFactory(
const std::string &Source, MetaRuleObject &R,
std::shared_ptr<MetaRuleObject::ClassMethod> MethodPtr) {
return std::make_shared<UserDefinedRewriterFactory>(R, MethodPtr);
}
std::function<bool(const CallExpr *C)> hasManagedAttr(int Idx) {
return [=](const CallExpr *C) -> bool {
const Expr *Arg = C->getArg(Idx)->IgnoreImpCasts();
if (auto CSCE = dyn_cast_or_null<CStyleCastExpr>(Arg)) {
Arg = CSCE->getSubExpr();
}
if (auto UO = dyn_cast_or_null<UnaryOperator>(Arg)) {
Arg = UO->getSubExpr();
}
if (auto ArgDRE = dyn_cast_or_null<DeclRefExpr>(Arg)) {
auto D = ArgDRE->getDecl();
if (D->hasAttr<HIPManagedAttr>()) {
return true;
}
}
return false;
};
}
AddrOfExpr::AddrOfExpr(const Expr *E, const CallExpr *C) {
this->C = C;
this->E = getAddrOfedExpr(E);
if (this->E) {
this->E = this->E->IgnoreParens();
this->DerefRemoved = true;
} else {
this->E = E;
}
this->NeedParens = needExtraParens(E);
if (const auto UO = dyn_cast_or_null<UnaryOperator>(E->IgnoreImpCasts())) {
if (UO->getOpcode() == UnaryOperatorKind::UO_AddrOf) {
this->NeedParens = false;
}
}
}
DerefExpr::DerefExpr(const Expr *E, const CallExpr *C) {
this->C = C;
// If E is UnaryOperator or CXXOperatorCallExpr D.E will has value
this->E = getDereferencedExpr(E);
if (this->E) {
this->E = this->E->IgnoreParens();
this->AddrOfRemoved = true;
} else {
this->E = E;
}
this->NeedParens = needExtraParens(E);
if (const auto UO = dyn_cast_or_null<UnaryOperator>(E->IgnoreImpCasts())) {
if (UO->getOpcode() == UnaryOperatorKind::UO_Deref) {
this->NeedParens = false;
}
}
}
std::string CallExprRewriter::getMigratedArg(unsigned Idx) {
Analyzer.analyze(Call->getArg(Idx));
return Analyzer.getRewritePrefix() + Analyzer.getRewriteString() +
Analyzer.getRewritePostfix();
}
std::string CallExprRewriter::getMigratedArgWithExtraParens(unsigned Idx) {
if (needExtraParens(Call->getArg(Idx)))
return "(" + getMigratedArg(Idx) + ")";
return getMigratedArg(Idx);
}
std::vector<std::string> CallExprRewriter::getMigratedArgs() {
std::vector<std::string> ArgList;
for (unsigned i = 0; i < Call->getNumArgs(); ++i)
ArgList.emplace_back(getMigratedArg(i));
return ArgList;
}
std::optional<std::string>
FuncCallExprRewriter::rewrite(ExprAnalysis *Analysis) {
ParentAnalysisGuard Guard(Analysis);
RewriteArgList = getMigratedArgs();
return buildRewriteString();
}
std::optional<std::string> FuncCallExprRewriter::buildRewriteString() {
std::string Result;
llvm::raw_string_ostream OS(Result);
OS << TargetCalleeName << "(";
for (auto &Arg : RewriteArgList)
OS << Arg << ", ";
OS.flush();
return RewriteArgList.empty() ? Result.append(")")
: Result.replace(Result.length() - 2, 2, ")");
}
std::unique_ptr<std::unordered_map<
std::string, std::shared_ptr<CallExprRewriterFactoryBase>>>
CallExprRewriterFactoryBase::RewriterMap = std::make_unique<std::unordered_map<
std::string, std::shared_ptr<CallExprRewriterFactoryBase>>>();
std::unique_ptr<std::unordered_map<
std::string, std::shared_ptr<CallExprRewriterFactoryBase>>>
CallExprRewriterFactoryBase::MethodRewriterMap = std::make_unique<std::unordered_map<
std::string, std::shared_ptr<CallExprRewriterFactoryBase>>>();
ExprAnalysis *CallExprRewriter::ParentAnalysis = nullptr;
void CallExprRewriterFactoryBase::initRewriterMap() {
if (DpctGlobalInfo::useSYCLCompat()) {
initRewriterMapSYCLcompat(*RewriterMap);
initRewriterMethodMapSYCLcompat(*MethodRewriterMap);
}
initRewriterMapAtomic();
initRewriterMapCUB();
initRewriterMapCUFFT();
initRewriterMapCUBLAS();
initRewriterMapCURAND();
initRewriterMapCUSOLVER();
initRewriterMapCUSPARSE();
initRewriterMapComplex();
initRewriterMapDriver();
initRewriterMapGraph();
initRewriterMapGraphicsInterop();
initRewriterMapMemory();
initRewriterMapMisc();
initRewriterMapNccl();
initRewriterMapStream();
initRewriterMapTexture();
initRewriterMapThrust();
initRewriterMapWarp();
initRewriterMapCUDNN();
initRewriterMapErrorHandling();
initRewriterMapLIBCU();
initRewriterMapNvtx();
initRewriterMapEvent();
initRewriterMapMath();
initRewriterMapCooperativeGroups();
initRewriterMapWmma();
initMethodRewriterMapCUB();
initMethodRewriterMapCooperativeGroups();
initMethodRewriterMapLIBCU();
}
} // namespace dpct
} // namespace clang