Skip to content

Commit 643323c

Browse files
authored
[SYCLomatic] Fix a bug that kernel arg name may be incorrect when defined in macro (#2425)
Signed-off-by: Huang, Andy <[email protected]>
1 parent c3db834 commit 643323c

File tree

6 files changed

+50
-6
lines changed

6 files changed

+50
-6
lines changed

clang/lib/DPCT/AnalysisInfo.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5351,7 +5351,7 @@ KernelCallExpr::ArgInfo::ArgInfo(const ParmVarDecl *PVD, KernelCallExpr *Kernel)
53515351
IsRedeclareRequired = false;
53525352
}
53535353
KernelCallExpr::ArgInfo::ArgInfo(std::shared_ptr<TextureObjectInfo> Obj,
5354-
KernelCallExpr *BASE)
5354+
KernelCallExpr *BASE, std::string ArgStr)
53555355
: IsUsedAsLvalueAfterMalloc(false), Texture(Obj) {
53565356
IsPointer = false;
53575357
IsRedeclareRequired = false;
@@ -5360,7 +5360,7 @@ KernelCallExpr::ArgInfo::ArgInfo(std::shared_ptr<TextureObjectInfo> Obj,
53605360
if (auto S = std::dynamic_pointer_cast<StructureTextureObjectInfo>(Obj)) {
53615361
IsDoublePointer = S->containsVirtualPointer();
53625362
}
5363-
ArgString = Obj->getName();
5363+
ArgString = ArgStr;
53645364
IdString = ArgString + "_";
53655365
ArgSize = MapNames::KernelArgTypeSizeMap.at(KernelArgType::KAT_Texture);
53665366
}
@@ -5869,14 +5869,16 @@ void KernelCallExpr::buildArgsInfo(const CallExpr *CE) {
58695869
getTheLastCompleteImmediateRange(CE->getBeginLoc(), CE->getEndLoc());
58705870
Analysis.setCallSpelling(KCallSpellingRange.first, KCallSpellingRange.second);
58715871
auto &TexList = getTextureObjectList();
5872-
58735872
const auto *FD = CE->getDirectCallee();
58745873
const auto *FTD = FD ? FD->getPrimaryTemplate() : nullptr;
58755874
for (unsigned Idx = 0; Idx < CE->getNumArgs(); ++Idx) {
5875+
auto Arg = CE->getArg(Idx);
5876+
auto CallDefRange = getDefinitionRange(CE->getBeginLoc(), CE->getEndLoc());
5877+
auto ArgString = getStringInRange(
5878+
Arg->getSourceRange(), CallDefRange.getBegin(), CallDefRange.getEnd());
58765879
if (auto Obj = TexList[Idx]) {
5877-
ArgsInfo.emplace_back(Obj, this);
5880+
ArgsInfo.emplace_back(Obj, this, ArgString);
58785881
} else {
5879-
auto Arg = CE->getArg(Idx);
58805882
bool Used = true;
58815883
if (auto *ArgDRE = dyn_cast<DeclRefExpr>(Arg->IgnoreImpCasts()))
58825884
Used = isArgUsedAsLvalueUntil(ArgDRE, CE);

clang/lib/DPCT/AnalysisInfo.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2741,7 +2741,8 @@ class KernelCallExpr : public CallFunctionExpr {
27412741
ArgInfo(const ParmVarDecl *PVD, const std::string &ArgsArrayName,
27422742
KernelCallExpr *Kernel);
27432743
ArgInfo(const ParmVarDecl *PVD, KernelCallExpr *Kernel);
2744-
ArgInfo(std::shared_ptr<TextureObjectInfo> Obj, KernelCallExpr *BASE);
2744+
ArgInfo(std::shared_ptr<TextureObjectInfo> Obj, KernelCallExpr *BASE,
2745+
std::string ArgStr);
27452746
inline const std::string &getArgString() const;
27462747
inline const std::string &getTypeString() const;
27472748
inline std::string getIdStringWithIndex() const {

clang/lib/DPCT/CallExprRewriterTexture.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "CallExprRewriter.h"
1010
#include "CallExprRewriterCommon.h"
11+
#include "Utility.h"
1112

1213
namespace clang {
1314
namespace dpct {
@@ -198,6 +199,11 @@ class TextureReadRewriterFactory : public CallExprRewriterFactoryBase {
198199
return createRewriter(Call, RetAssign, SourceName);
199200
}
200201
} else if (auto DRE = dyn_cast<DeclRefExpr>(SourceExpr)) {
202+
auto CallDefRange =
203+
getDefinitionRange(Call->getBeginLoc(), Call->getEndLoc());
204+
auto DREString =
205+
getStringInRange(DRE->getSourceRange(), CallDefRange.getBegin(),
206+
CallDefRange.getEnd());
201207
auto TexInfo = CallInfo->addTextureObjectArg(SourceIdx, DRE, false);
202208
if (TexInfo) {
203209
TexInfo->setType(DpctGlobalInfo::getUnqualifiedTypeName(TargetType),

clang/lib/DPCT/Utility.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2385,6 +2385,17 @@ getRangeInRange(SourceRange Range, SourceLocation SearchRangeBegin,
23852385
Range.getEnd());
23862386
}
23872387

2388+
std::string getStringInRange(clang::SourceRange Range,
2389+
clang::SourceLocation RangeBegin,
2390+
clang::SourceLocation RangeEnd,
2391+
bool IncludeLastToken) {
2392+
auto ResultRange =
2393+
getRangeInRange(Range, RangeBegin, RangeEnd, IncludeLastToken);
2394+
auto &SM = dpct::DpctGlobalInfo::getSourceManager();
2395+
return std::string(SM.getCharacterData(ResultRange.first),
2396+
SM.getCharacterData(ResultRange.second));
2397+
}
2398+
23882399
unsigned int calculateIndentWidth(const CUDAKernelCallExpr *Node,
23892400
clang::SourceLocation SL, bool &Flag) {
23902401
Flag = true;

clang/lib/DPCT/Utility.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,10 @@ getRangeInRange(const clang::Stmt *E, clang::SourceLocation RangeBegin,
446446
std::pair<clang::SourceLocation, clang::SourceLocation>
447447
getRangeInRange(clang::SourceRange Range, clang::SourceLocation RangeBegin,
448448
clang::SourceLocation RangeEnd, bool IncludeLastToken = true);
449+
std::string getStringInRange(clang::SourceRange Range,
450+
clang::SourceLocation RangeBegin,
451+
clang::SourceLocation RangeEnd,
452+
bool IncludeLastToken = true);
449453
unsigned int calculateIndentWidth(const clang::CUDAKernelCallExpr *Node,
450454
clang::SourceLocation SL, bool &Flag);
451455
bool isIncludedFile(const clang::tooling::UnifiedPath &CurrentFile,

clang/test/dpct/macro_test.cu

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,4 +1394,24 @@ __global__ void foo40_kernel() {
13941394
void foo40() {
13951395
foo40_kernel<<<1, 1>>>();
13961396
}
1397+
1398+
1399+
template <class T> class MyClass {};
1400+
1401+
__global__ void foo41(MyClass<float> m) {}
1402+
1403+
void foo42(MyClass<float> &vecs) {
1404+
#define RUN_APPEND2(DATA) foo41<<<1, 1, 0>>>(DATA);
1405+
//CHECK: #define RUN_APPEND2(DATA) \
1406+
//CHECK-NEXT: dpct::get_in_order_queue().submit([&](sycl::handler &cgh) { \
1407+
//CHECK-NEXT: auto DATA_ct0 = DATA; \
1408+
//CHECK-NEXT: \
1409+
//CHECK-NEXT: cgh.parallel_for( \
1410+
//CHECK-NEXT: sycl::nd_range<3>(sycl::range<3>(1, 1, 1), sycl::range<3>(1, 1, 1)), \
1411+
//CHECK-NEXT: [=](sycl::nd_item<3> item_ct1) { foo41(DATA_ct0); }); \
1412+
//CHECK-NEXT: });
1413+
RUN_APPEND2(vecs);
1414+
}
1415+
1416+
13971417
#endif

0 commit comments

Comments
 (0)