Skip to content

Commit f5a4969

Browse files
authored
[SYCLomatic] Fix the parse on the const memory through parameter of function (#2922)
Signed-off-by: Chen, Sheng S <sheng.s.chen@intel.com>
1 parent 0f2367f commit f5a4969

File tree

17 files changed

+108
-43
lines changed

17 files changed

+108
-43
lines changed

clang/examples/DPCT/Runtime/cudaGetSymbolAddress.cu

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
void test(void **pDev, const void *symbol) {
1+
#define MAX_CONST_SIZE 1024
2+
__constant__ char symbol[MAX_CONST_SIZE];
3+
4+
void test(void **pDev) {
25
// Start
36
cudaGetSymbolAddress(pDev /*void ***/, symbol /*const void **/);
47
// End

clang/include/clang/DPCT/DPCTOptions.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,7 @@ DPCT_ENUM_OPTION(
807807
DPCT_OPTION_ENUM_VALUE(
808808
"device_global", int(ExperimentalFeatures::Exp_DeviceGlobal),
809809
"Experimental extension that allows device scoped memory "
810-
"allocations into SYCL that can\n"
810+
"allocations into SYCL that can "
811811
"be accessed within a kernel using syntax similar to C++ global "
812812
"variables.\n",
813813
false),

clang/lib/DPCT/RulesLang/Math/CallExprRewriterMath.cpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,12 @@ std::string MathFuncNameRewriter::getNewFuncName() {
6363

6464
auto ContextFD = getImmediateOuterFuncDecl(Call);
6565
if (NamespaceStr == "std" && ContextFD &&
66-
!ContextFD->hasAttr<CUDADeviceAttr>() &&
67-
!ContextFD->hasAttr<CUDAGlobalAttr>()) {
66+
!isGlobalOrDeviceFuncDecl(ContextFD)) {
6867
return "";
6968
}
7069
// For device functions
7170
else if ((FD->hasAttr<CUDADeviceAttr>() && !FD->hasAttr<CUDAHostAttr>()) ||
72-
(ContextFD && (ContextFD->hasAttr<CUDADeviceAttr>() ||
73-
ContextFD->hasAttr<CUDAGlobalAttr>()))) {
71+
(ContextFD && isGlobalOrDeviceFuncDecl(ContextFD))) {
7472
if (SourceCalleeName == "abs") {
7573
// further check the type of the args.
7674
if (!Call->getArg(0)->getType()->isIntegerType()) {
@@ -333,15 +331,12 @@ std::optional<std::string> MathSimulatedRewriter::rewrite() {
333331
}
334332

335333
auto ContextFD = getImmediateOuterFuncDecl(Call);
336-
if (NamespaceStr == "std" && ContextFD &&
337-
!ContextFD->hasAttr<CUDADeviceAttr>() &&
338-
!ContextFD->hasAttr<CUDAGlobalAttr>()) {
334+
if (NamespaceStr == "std" && ContextFD && !isGlobalOrDeviceFuncDecl(ContextFD)) {
339335
return {};
340336
}
341337

342338
if (!FD->hasAttr<CUDADeviceAttr>() && ContextFD &&
343-
!ContextFD->hasAttr<CUDADeviceAttr>() &&
344-
!ContextFD->hasAttr<CUDAGlobalAttr>())
339+
!isGlobalOrDeviceFuncDecl(ContextFD))
345340
return Base::rewrite();
346341

347342
// Do not need to report warnings for pow, funnelshift, or drcp migrations

clang/lib/DPCT/RulesLang/Math/CallExprRewriterMath.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,7 @@ inline auto IsDirectCallerPureHost = [](const CallExpr *C) -> bool {
213213
}
214214
if (!ContextFD)
215215
return false;
216-
if (!ContextFD->getAttr<CUDADeviceAttr>() &&
217-
!ContextFD->getAttr<CUDAGlobalAttr>()) {
216+
if (!isGlobalOrDeviceFuncDecl(ContextFD)) {
218217
return true;
219218
}
220219
return false;

clang/lib/DPCT/RulesLang/RulesLang.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5020,8 +5020,7 @@ void DeviceFunctionDeclRule::runRule(
50205020

50215021
// We need skip lambda in host code, but cannot skip lambda in device code.
50225022
if (const FunctionDecl *OuterMostFD = findTheOuterMostFunctionDecl(FD);
5023-
OuterMostFD && (!OuterMostFD->hasAttr<CUDADeviceAttr>() &&
5024-
!OuterMostFD->hasAttr<CUDAGlobalAttr>()))
5023+
OuterMostFD && !isGlobalOrDeviceFuncDecl(OuterMostFD))
50255024
return;
50265025

50275026
if (FD->isVariadic()) {
@@ -6813,9 +6812,17 @@ void MemoryMigrationRule::getSymbolAddressMigration(
68136812
ExprAnalysis EA;
68146813
EA.analyze(C->getArg(0));
68156814
auto StmtStrArg0 = EA.getReplacedString();
6815+
const DeclRefExpr *Arg =
6816+
dyn_cast<DeclRefExpr>(C->getArg(1)->IgnoreImplicitAsWritten());
6817+
const VarDecl *VD = dyn_cast<VarDecl>(Arg->getDecl());
68166818
EA.analyze(C->getArg(1));
68176819
auto StmtStrArg1 = EA.getReplacedString();
6818-
Replacement = "*(" + StmtStrArg0 + ")" + " = " + StmtStrArg1 + ".get_ptr()";
6820+
if (VD && VD->isLocalVarDeclOrParm()) {
6821+
StmtStrArg1 = "const_cast<void *>(" + StmtStrArg1 + ")";
6822+
} else {
6823+
StmtStrArg1 += ".get_ptr()";
6824+
}
6825+
Replacement = "*(" + StmtStrArg0 + ")" + " = " + StmtStrArg1;
68196826
requestFeature(HelperFeatureEnum::device_ext);
68206827
emplaceTransformation(new ReplaceStmt(C, std::move(Replacement)));
68216828
}

clang/lib/DPCT/RulesLang/RulesLangNoneAPIAndType.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,17 @@ void MemVarRefMigrationRule::runRule(const MatchFinder::MatchResult &Result) {
216216
}
217217
}
218218
}
219-
if (!HasTypeCasted && Decl->hasAttr<CUDAConstantAttr>() &&
220-
(MemVarRef->getType()->getTypeClass() ==
221-
Type::TypeClass::ConstantArray)) {
219+
auto FD = dpct::DpctGlobalInfo::findAncestor<FunctionDecl>(MemVarRef);
220+
auto CE = dpct::DpctGlobalInfo::findAncestor<CallExpr>(MemVarRef);
221+
if (auto VD =dyn_cast<VarDecl>(MemVarRef->getDecl()); FD && VD &&
222+
!VD->isLocalVarDeclOrParm() &&
223+
!isGlobalOrDeviceFuncDecl(FD)) {
224+
if (CE &&
225+
!DpctGlobalInfo::isInCudaPath(CE->getCalleeDecl()->getBeginLoc()))
226+
emplaceTransformation(new InsertAfterStmt(MemVarRef, ".get_ptr()"));
227+
} else if (!HasTypeCasted && Decl->hasAttr<CUDAConstantAttr>() &&
228+
(MemVarRef->getType()->getTypeClass() ==
229+
Type::TypeClass::ConstantArray)) {
222230
const Expr *RHS = getRHSOfTheNonConstAssignedVar(MemVarRef);
223231
if (RHS) {
224232
auto Range = GetReplRange(RHS);
@@ -235,7 +243,7 @@ void MemVarRefMigrationRule::runRule(const MatchFinder::MatchResult &Result) {
235243
if (VD == nullptr)
236244
return;
237245
auto Var = Global.findMemVarInfo(VD);
238-
if (Func->hasAttr<CUDAGlobalAttr>() || Func->hasAttr<CUDADeviceAttr>()) {
246+
if (isGlobalOrDeviceFuncDecl(Func)) {
239247
if (DpctGlobalInfo::useGroupLocalMemory() &&
240248
VD->hasAttr<CUDASharedAttr>() && VD->getStorageClass() != SC_Extern) {
241249
if (!Var)
@@ -829,7 +837,7 @@ void MemVarAnalysisRule::runRule(const MatchFinder::MatchResult &Result) {
829837
return;
830838
}
831839
auto Var = MemVarInfo::buildMemVarInfo(VD);
832-
if (Func->hasAttr<CUDAGlobalAttr>() || Func->hasAttr<CUDADeviceAttr>()) {
840+
if (isGlobalOrDeviceFuncDecl(Func)) {
833841
if (!(DpctGlobalInfo::useGroupLocalMemory() &&
834842
VD->hasAttr<CUDASharedAttr>() &&
835843
VD->getStorageClass() != SC_Extern)) {
@@ -1025,7 +1033,7 @@ void ZeroLengthArrayRule::runRule(const MatchFinder::MatchResult &Result) {
10251033
const clang::FunctionDecl *FD = DpctGlobalInfo::getParentFunction(TL);
10261034
if (FD) {
10271035
// Check if the array is in device code
1028-
if (!(FD->getAttr<CUDADeviceAttr>()) && !(FD->getAttr<CUDAGlobalAttr>()))
1036+
if (!isGlobalOrDeviceFuncDecl(FD))
10291037
return;
10301038
}
10311039
}

clang/lib/DPCT/RulesLang/RulesLangTexture.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@ const Expr *TextureRule::getAssignedBO(const Expr *E, ASTContext &Context) {
762762
bool TextureRule::processTexVarDeclInDevice(const VarDecl *VD) {
763763
if (auto FD =
764764
dyn_cast_or_null<FunctionDecl>(VD->getParentFunctionOrMethod())) {
765-
if (FD->hasAttr<CUDAGlobalAttr>() || FD->hasAttr<CUDADeviceAttr>()) {
765+
if (isGlobalOrDeviceFuncDecl(FD)) {
766766
auto Tex = DpctGlobalInfo::getInstance().insertTextureInfo(VD);
767767

768768
auto DataType = Tex->getType()->getDataType();
@@ -1009,8 +1009,7 @@ void TextureRule::runRule(const MatchFinder::MatchResult &Result) {
10091009
return;
10101010
}
10111011
if (auto FD = DpctGlobalInfo::getParentFunction(TL)) {
1012-
if ((FD->hasAttr<CUDAGlobalAttr>() || FD->hasAttr<CUDADeviceAttr>()) &&
1013-
!DpctGlobalInfo::useExtBindlessImages()) {
1012+
if (isGlobalOrDeviceFuncDecl(FD) && !DpctGlobalInfo::useExtBindlessImages()) {
10141013
return;
10151014
}
10161015
}

clang/lib/DPCT/RulesLangLib/CUBAPIMigration.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,8 +1054,7 @@ void CubRule::processCubTypeDefOrUsing(const TypedefNameDecl *TD) {
10541054
MapNames::getClNamespace() + "sub_group", SM));
10551055
} else if (CanonicalTypeStr.find("Block") != std::string::npos) {
10561056
auto DeviceFuncDecl = DpctGlobalInfo::findAncestor<FunctionDecl>(TD);
1057-
if (DeviceFuncDecl && (DeviceFuncDecl->hasAttr<CUDADeviceAttr>() ||
1058-
DeviceFuncDecl->hasAttr<CUDAGlobalAttr>())) {
1057+
if (DeviceFuncDecl && isGlobalOrDeviceFuncDecl(DeviceFuncDecl)) {
10591058
if (auto DI = DeviceFunctionDecl::LinkRedecls(DeviceFuncDecl)) {
10601059
auto &Map = DpctGlobalInfo::getInstance().getCubPlaceholderIndexMap();
10611060
Map.insert({PlaceholderIndex, DI});
@@ -1692,8 +1691,7 @@ void CubRule::processTypeLoc(const TypeLoc *TL) {
16921691
} else if (TypeName.find("class cub::BlockScan") == 0 ||
16931692
TypeName.find("class cub::BlockReduce") == 0) {
16941693
auto DeviceFuncDecl = DpctGlobalInfo::findAncestor<FunctionDecl>(TL);
1695-
if (DeviceFuncDecl && (DeviceFuncDecl->hasAttr<CUDADeviceAttr>() ||
1696-
DeviceFuncDecl->hasAttr<CUDAGlobalAttr>())) {
1694+
if (DeviceFuncDecl && isGlobalOrDeviceFuncDecl(DeviceFuncDecl)) {
16971695
if (auto DI = DeviceFunctionDecl::LinkRedecls(DeviceFuncDecl)) {
16981696
auto &Map = DpctGlobalInfo::getInstance().getCubPlaceholderIndexMap();
16991697
Map.insert({PlaceholderIndex, DI});

clang/lib/DPCT/RulesLangLib/ThrustAPIMigration.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ void ThrustAPIRule::thrustFuncMigration(const MatchFinder::MatchResult &Result,
188188
// thrust::count, thrust::equal) called in device function , should be
189189
// migrated to oneapi::dpl APIs without a policy on the SYCL side
190190
if (auto FD = DpctGlobalInfo::getParentFunction(CE)) {
191-
if (FD->hasAttr<CUDAGlobalAttr>() || FD->hasAttr<CUDADeviceAttr>()) {
191+
if (isGlobalOrDeviceFuncDecl(FD)) {
192192
if (hasExecutionPolicy) {
193193
emplaceTransformation(removeArg(CE, 0, *Result.SourceManager));
194194
}

clang/lib/DPCT/Utility.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,11 @@ bool isCudaMemoryAllocation(const DeclRefExpr *Arg, const CallExpr *CE) {
783783
return false;
784784
}
785785

786+
bool isGlobalOrDeviceFuncDecl(const FunctionDecl *FD) {
787+
if (FD->hasAttr<CUDADeviceAttr>() || FD->hasAttr<CUDAGlobalAttr>())
788+
return true;
789+
return false;
790+
}
786791
/// This function traverses all the nodes in the AST represented by \param Root
787792
/// in a depth-first manner, until the node \param Sentinal is reached, to check
788793
/// if the pointer \param Arg to a piece of memory is used as lvalue after the

0 commit comments

Comments
 (0)