Skip to content

[SYCLomatic] Enhance the migration of __shared__ memory #2787

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 27 commits into
base: SYCLomatic
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
e2a868e
shared
zhiweij1 Apr 15, 2025
5a48a52
Add test
zhiweij1 Apr 15, 2025
d3e06b9
Merge remote-tracking branch 'origin/SYCLomatic' into shared
zhiweij1 Apr 15, 2025
9f0e4bf
Merge remote-tracking branch 'origin/SYCLomatic' into shared
zhiweij1 Apr 16, 2025
0bbb4b0
Fix
zhiweij1 Apr 16, 2025
78b8837
Merge remote-tracking branch 'origin/SYCLomatic' into shared
zhiweij1 Apr 17, 2025
e9bbff1
Merge remote-tracking branch 'origin/SYCLomatic' into shared
zhiweij1 Apr 28, 2025
173781d
WIP
zhiweij1 Apr 28, 2025
43a8124
wip
zhiweij1 Apr 28, 2025
c97541b
Merge remote-tracking branch 'origin/SYCLomatic' into shared
zhiweij1 Apr 29, 2025
d01ce43
WIP
zhiweij1 Apr 29, 2025
f2dde67
Fix
zhiweij1 Apr 29, 2025
63cd217
FIX
zhiweij1 Apr 29, 2025
5fd4cb6
fix
zhiweij1 Apr 30, 2025
929e86e
Update
zhiweij1 May 7, 2025
aa7aad6
Fix
zhiweij1 May 7, 2025
fc43abd
Fix
zhiweij1 May 7, 2025
ce1138a
Format
zhiweij1 May 8, 2025
60a397b
Merge remote-tracking branch 'origin/SYCLomatic' into shared
zhiweij1 May 12, 2025
0a5baa7
123
zhiweij1 May 12, 2025
ef9bd56
Merge remote-tracking branch 'origin/SYCLomatic' into shared
zhiweij1 May 12, 2025
106a0a6
Update
zhiweij1 May 12, 2025
46487f7
Merge remote-tracking branch 'origin/SYCLomatic' into shared
zhiweij1 May 16, 2025
7730bf1
Update
zhiweij1 May 19, 2025
701eb23
Update
zhiweij1 May 19, 2025
5f3b153
Merge remote-tracking branch 'origin/SYCLomatic' into shared
zhiweij1 May 22, 2025
c7a6dc4
Merge remote-tracking branch 'origin/SYCLomatic' into shared
zhiweij1 May 26, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions clang/lib/DPCT/AnalysisInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2811,6 +2811,7 @@ void CtTypeInfo::setArrayInfo(const DependentSizedArrayTypeLoc &TL,
bool NeedSizeFold) {
ContainSizeofType = containSizeOfType(TL.getSizeExpr());
ExprAnalysis EA;
EA.IsAnalyzingCtTypeInfo = true;
EA.analyze(TL.getSizeExpr());
auto TDSI = EA.getTemplateDependentStringInfo();
if (TDSI->containsTemplateDependentMacro())
Expand Down
11 changes: 10 additions & 1 deletion clang/lib/DPCT/RuleInfra/ExprAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,16 @@ void ExprAnalysis::analyzeExpr(const DeclRefExpr *DRE) {
}
if (auto TemplateDecl = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()))
addReplacement(DRE, TemplateDecl->getIndex());
else if (auto ECD = dyn_cast<EnumConstantDecl>(DRE->getDecl())) {
else if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl());
VD && VD->isConstexpr() &&
IsAnalyzingCtTypeInfo /*&& IsDependent*/) {
if (VD->getInit() && VD->getInit()->getBeginLoc().isValid()) {
ExprAnalysis EA(VD->getInit());
std::string VDInitStr = EA.getReplacedString();
std::string VDStr = VD->getNameAsString();
addReplacement(0, VDStr.size(), VDInitStr);
}
} else if (auto ECD = dyn_cast<EnumConstantDecl>(DRE->getDecl())) {
std::unordered_set<std::string> targetStr = {
"thread_scope_system", "thread_scope_device", "thread_scope_block",
"memory_order_relaxed", "memory_order_acq_rel", "memory_order_release",
Expand Down
27 changes: 27 additions & 0 deletions clang/lib/DPCT/RuleInfra/ExprAnalysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class TemplateDependentReplacement {
}
inline size_t getOffset() const { return Offset; }
inline size_t getLength() const { return Length; }
inline unsigned getTemplateIndex() const { return TemplateIndex; }
const TemplateArgumentInfo &
getTargetArgument(const std::vector<TemplateArgumentInfo> &TemplateList);
void replace(const std::vector<TemplateArgumentInfo> &TemplateList);
Expand Down Expand Up @@ -136,6 +137,15 @@ class StringReplacements {
std::make_pair(Offset, std::make_shared<TemplateDependentReplacement>(
SourceStr, Offset, Length, TemplateIndex)));
}

inline void addTemplateDependentReplacement(
size_t Offset, size_t Length,
std::shared_ptr<TemplateDependentStringInfo> TDSI) {
// TDRs.insert(
// std::make_pair(Offset, std::make_shared<TemplateDependentReplacement>(
// SourceStr, Offset, Length, TemplateIndex)));
}

// Add a string replacement
void addStringReplacement(size_t Offset, size_t Length, std::string Text) {
auto Result = ReplMap.insert(std::make_pair(
Expand Down Expand Up @@ -163,6 +173,10 @@ class StringReplacements {
replaceString();
return SourceStr;
}
inline std::map<size_t, std::shared_ptr<TemplateDependentReplacement>>
getTDRs() {
return TDRs;
}

private:
StringReplacements(const StringReplacements &) = delete;
Expand Down Expand Up @@ -230,6 +244,10 @@ class ExprAnalysis {
inline const std::string &getReplacedString() {
return ReplSet.getReplacedString();
}
inline std::map<size_t, std::shared_ptr<TemplateDependentReplacement>>
getReplSetTDRs() {
return ReplSet.getTDRs();
}
inline std::shared_ptr<TemplateDependentStringInfo>
getTemplateDependentStringInfo() {
auto Res = ReplSet.getTemplateDependentStringInfo();
Expand Down Expand Up @@ -586,6 +604,12 @@ class ExprAnalysis {
ReplSet.addTemplateDependentReplacement(Offset, Length, TemplateIndex);
}

inline void
addReplacement(size_t Offset, size_t Length,
std::shared_ptr<TemplateDependentStringInfo> TDSI) {
ReplSet.addTemplateDependentReplacement(Offset, Length, TDSI);
}

// Analyze the expression, jump to corresponding analysis function according
// to its class
// Precondition: Expression != nullptr
Expand Down Expand Up @@ -691,6 +715,9 @@ class ExprAnalysis {
std::string RewritePrefix;
std::string RewritePostfix;
std::set<HelperFeatureEnum> HelperFeatureSet;

public:
bool IsAnalyzingCtTypeInfo = false;
};

// Analyze pointer allocated by cudaMallocManaged.
Expand Down
15 changes: 14 additions & 1 deletion clang/test/dpct/sharedmem_var_static.cu
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
// RUN: dpct --format-range=none --usm-level=none -out-root %T/sharedmem_var_static %s --cuda-include-path="%cuda-path/include" --sycl-named-lambda -- -x cuda --cuda-host-only
// RUN: FileCheck %s --match-full-lines --input-file %T/sharedmem_var_static/sharedmem_var_static.dp.cpp
// RUN: %if build_lit %{icpx -c -fsycl -DNO_BUILD_TEST %T/sharedmem_var_static/sharedmem_var_static.dp.cpp -o %T/sharedmem_var_static/sharedmem_var_static.dp.o %}
#ifndef NO_BUILD_TEST

#include <stdio.h>
#include <complex>
#define SIZE 64

#ifndef NO_BUILD_TEST
class TestObject{
public:
// CHECK: static void run(int *in, int *out, int &a0) {
Expand Down Expand Up @@ -224,3 +225,15 @@ void fooh() {
fook<SZ><<<1, 1>>>();
}
#endif

constexpr int kWarpSize = 32;

template <int ThreadsPerBlock, int NumWarpQ> __global__ void kerfunc() {
constexpr int kNumWarps = ThreadsPerBlock / kWarpSize;
__shared__ int smem[kNumWarps * NumWarpQ];
}

void foo2() {
// CHECK: sycl::local_accessor<int, 1> smem_acc_ct1(sycl::range<1>(128 / kWarpSize * 8), cgh);
kerfunc<128, 8><<<32, 32>>>();
}
Loading