Skip to content

Commit a85be23

Browse files
authored
[SYCLomatic] Fix the issue that auto type is used in template scenario after migration for auto type global device memory (#2763)
Signed-off-by: intwanghao <hao3.wang@intel.com>
1 parent bd4c37e commit a85be23

File tree

4 files changed

+29
-1
lines changed

4 files changed

+29
-1
lines changed

clang/lib/DPCT/AnalysisInfo.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2625,6 +2625,9 @@ CtTypeInfo::CtTypeInfo(const VarDecl *D, bool NeedSizeFold) : CtTypeInfo() {
26252625
auto TL = D->getTypeSourceInfo()->getTypeLoc();
26262626
IsConstantQualified = D->hasAttr<CUDAConstantAttr>();
26272627
setTypeInfo(TL, NeedSizeFold);
2628+
if (IsAutoSpecified) {
2629+
DeducedTypeStr = DpctGlobalInfo::getReplacedTypeName(D->getType());
2630+
}
26282631
if (TL.getTypeLocClass() == TypeLoc::IncompleteArray) {
26292632
if (auto CAT = dyn_cast<ConstantArrayType>(D->getType())) {
26302633
Range[0] = std::to_string(CAT->getSize().getZExtValue());
@@ -2822,6 +2825,12 @@ void CtTypeInfo::setArrayInfo(const IncompleteArrayTypeLoc &TL,
28222825
}
28232826
void CtTypeInfo::setName(const TypeLoc &TL) {
28242827
ExprAnalysis EA;
2828+
QualType QT = TL.getType();
2829+
if (const Type *TP = QT.getTypePtr()) {
2830+
if (isa<AutoType>(TP)) {
2831+
IsAutoSpecified = true;
2832+
}
2833+
}
28252834
EA.analyze(TL);
28262835
TDSI = EA.getTemplateDependentStringInfo();
28272836
auto SetFromTL = EA.getHelperFeatureSet();
@@ -3019,7 +3028,11 @@ void MemVarInfo::migrateToDeviceGlobal(const VarDecl *MemVar) {
30193028
}
30203029
}
30213030
if (BaseTypeStr.empty()) {
3022-
BaseTypeStr = getType()->getBaseNameWithoutQualifiers();
3031+
if (getType()->isAutoSpecified()) {
3032+
BaseTypeStr = getType()->getDeducedTypeStr();
3033+
} else {
3034+
BaseTypeStr = getType()->getBaseNameWithoutQualifiers();
3035+
}
30233036
TypeReplLoc = TL.getBeginLoc();
30243037
TypeReplLen =
30253038
SM.getFileOffset(TL.getEndLoc()) - SM.getFileOffset(TL.getBeginLoc()) +

clang/lib/DPCT/AnalysisInfo.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1799,6 +1799,8 @@ class CtTypeInfo {
17991799
}
18001800
bool containsTemplateDependentMacro() const { return TemplateDependentMacro; }
18011801
bool isConstantQualified() const { return IsConstantQualified; }
1802+
bool isAutoSpecified() { return IsAutoSpecified; }
1803+
std::string getDeducedTypeStr() { return DeducedTypeStr; }
18021804

18031805
private:
18041806
// For ConstantArrayType, size in generated code is folded as an integer.
@@ -1858,6 +1860,8 @@ class CtTypeInfo {
18581860
std::vector<std::string> ArraySizeOriginExprs{};
18591861
std::set<HelperFeatureEnum> HelperFeatureSet;
18601862
std::shared_ptr<TemplateDependentStringInfo> TDSI;
1863+
bool IsAutoSpecified = false;
1864+
std::string DeducedTypeStr;
18611865
};
18621866

18631867
// variable info includes name, type and location.

clang/lib/DPCT/RuleInfra/ExprAnalysis.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,11 @@ void ExprAnalysis::analyzeExpr(const DeclRefExpr *DRE) {
611611
}
612612
}
613613
} else if (auto VD = dyn_cast<VarDecl>(DRE->getDecl())) {
614+
DpctGlobalInfo &Global = DpctGlobalInfo::getInstance();
615+
auto Info = Global.findMemVarInfo(VD);
616+
if (Info && Info->isUseDeviceGlobal()) {
617+
addReplacement(DRE, getStmtSpelling(DRE) + ".get()");
618+
}
614619
if (RefString == "warpSize" &&
615620
!DpctGlobalInfo::isInAnalysisScope(VD->getLocation())) {
616621
addReplacement(DRE, DpctGlobalInfo::getSubGroup(DRE) +

clang/test/dpct/device_global/device_global.cu

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ struct B{
1414
int data = 1;
1515
};
1616

17+
const float init_val = 0;
1718

1819
// CHECK: static sycl::ext::oneapi::experimental::device_global<int> var_a;
1920
// CHECK: /*
@@ -31,13 +32,16 @@ struct B{
3132
// CHECK: static sycl::ext::oneapi::experimental::device_global<A> var_e;
3233
// CHECK: static sycl::ext::oneapi::experimental::device_global<B> var_f;
3334
// CHECK: inline dpct::global_memory<int, 0> var_g;
35+
// CHECK: static sycl::ext::oneapi::experimental::device_global<float> var_h{init_val};
3436
__device__ int var_a;
3537
__device__ int var_b = 0;
3638
__constant__ float var_c = 2.f;
3739
__constant__ float var_d = 1.f;
3840
__device__ A var_e;
3941
__constant__ B var_f;
4042
__device__ int var_g;
43+
__device__ auto var_h = init_val;
44+
4145
// CHECK: static sycl::ext::oneapi::experimental::device_global<float[10]> arr_a;
4246
// CHECK: /*
4347
// CHECK: DPCT1127:{{[0-9]+}}: The constant compile-time initialization for device_global is supported when compiling with C++20. You may need to adjust the compile commands.
@@ -82,6 +86,7 @@ TABLE_END()
8286
// CHECK: var_a.get() = 0;
8387
// CHECK: var_e.get().data = 1;
8488
// CHECK: *ptr = var_a.get() + var_b.get() + var_c.get() + var_d.get() + var_e.get().data + var_f.get().data + device_func();
89+
// CHECK: sycl::fmax(var_h.get(), (double)0);
8590
// CHECK: }
8691
__device__ int device_func() {
8792
float *p = arr_a;
@@ -94,6 +99,7 @@ __global__ void kernel(int *ptr) {
9499
var_a = 0;
95100
var_e.data = 1;
96101
*ptr = var_a + var_b + var_c + var_d + var_e.data + var_f.data + device_func();
102+
fmax(var_h, 0);
97103
}
98104

99105
// CHECK: void kernel2(int *ptr,

0 commit comments

Comments
 (0)