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 all 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
3 changes: 2 additions & 1 deletion clang/lib/DPCT/AnalysisInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2821,11 +2821,12 @@ 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())
TemplateDependentMacro = true;
Range.emplace_back(EA.getTemplateDependentStringInfo());
Range.emplace_back(TDSI);
setTypeInfo(TL.getElementLoc(), NeedSizeFold);
}
void CtTypeInfo::setArrayInfo(const IncompleteArrayTypeLoc &TL,
Expand Down
33 changes: 31 additions & 2 deletions clang/lib/DPCT/RuleInfra/ExprAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,27 @@ void ExprAnalysis::initSourceRange(const SourceRange &Range) {
}

void StringReplacements::replaceString() {
SourceStr.reserve(SourceStr.length() + ShiftLength);
for (auto &TDR : TDRs2) {
// Find items in ReplMap whose offset <= TDR.first.
// Then check length, is there is overlap, ignore this insertion.
// Finally calculate the shift length.
int Shift = 0;
auto UpperBound = ReplMap.upper_bound(TDR.first);
for (auto It = ReplMap.begin(); It != UpperBound; ++It) {
if ((It->first + It->second->getLength()) > TDR.first) {
// overlap
continue;
}
Shift +=
(It->second->getReplacedText().length() - It->second->getLength());
}
auto NewTDR = std::make_shared<TemplateDependentReplacement>(
TDR.second->getSourceStr(), TDR.first, TDR.second->getLength(),
TDR.second->getTemplateIndex());
NewTDR->shift(Shift);
TDRs.insert(std::make_pair(TDR.first + Shift, NewTDR));
}

auto Itr = ReplMap.rbegin();
while (Itr != ReplMap.rend()) {
Itr->second->replaceString();
Expand Down Expand Up @@ -571,7 +591,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) {
if (VD->getInit() && VD->getInit()->getBeginLoc().isValid() &&
(VD->getInit()->getDependence() != ExprDependence::None)) {
ExprAnalysis EA(VD->getInit());
auto TDSI = EA.getTemplateDependentStringInfo();
auto LocInfo = getOffsetAndLength(DRE);
addReplacement(LocInfo.first, LocInfo.second, TDSI);
}
} 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
60 changes: 41 additions & 19 deletions clang/lib/DPCT/RuleInfra/ExprAnalysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class StringReplacement {
}

inline const std::string &getReplacedText() { return Text; }
inline size_t getLength() { return Length; }

private:
// SourceStr is the string which need replaced.
Expand All @@ -56,27 +57,29 @@ class TemplateArgumentInfo;

/// Store replacement dependent on template args
class TemplateDependentReplacement {
std::string &SourceStr;
std::string SourceStr;
size_t Offset;
size_t Length;
unsigned TemplateIndex;

public:
TemplateDependentReplacement(std::string &SrcStr, size_t Offset,
size_t Length, unsigned TemplateIndex)
TemplateDependentReplacement(std::string SrcStr, size_t Offset, size_t Length,
unsigned TemplateIndex)
: SourceStr(SrcStr), Offset(Offset), Length(Length),
TemplateIndex(TemplateIndex) {}
TemplateDependentReplacement(const TemplateDependentReplacement &rhs)
: TemplateDependentReplacement(rhs.SourceStr, rhs.Offset, rhs.Length,
rhs.TemplateIndex) {}

inline std::shared_ptr<TemplateDependentReplacement>
alterSource(std::string &SrcStr) {
alterSource(std::string SrcStr) {
return std::make_shared<TemplateDependentReplacement>(
SrcStr, Offset, Length, TemplateIndex);
}
inline const std::string &getSourceStr() const { return SourceStr; }
inline size_t getOffset() const { return Offset; }
inline size_t getLength() const { return Length; }
inline size_t getTemplateIndex() const { return TemplateIndex; }
const TemplateArgumentInfo &
getTargetArgument(const std::vector<TemplateArgumentInfo> &TemplateList);
void replace(const std::vector<TemplateArgumentInfo> &TemplateList);
Expand Down Expand Up @@ -117,39 +120,49 @@ class TemplateDependentStringInfo {
HelperFeatureSet = Set;
}
bool containsTemplateDependentMacro() const { return ContainsTemplateDependentMacro; }
const std::vector<std::shared_ptr<TemplateDependentReplacement>> &
getTDRs() const {
return TDRs;
}
};

/// Store an expr source string which may need replaced and its replacements
class StringReplacements {
public:
StringReplacements() : ShiftLength(0) {}
StringReplacements() {}
inline void init(std::string &&SrcStr) {
SourceStr = std::move(SrcStr);
ReplMap.clear();
}
inline void reset() { ReplMap.clear(); }

// Add a template dependent replacement
inline void addTemplateDependentReplacement(size_t Offset, size_t Length,
unsigned TemplateIndex) {
TDRs.insert(
void addTemplateDependentReplacement(size_t Offset, size_t Length,
unsigned TemplateIndex) {
TDRs2.insert(
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) {
addStringReplacement(Offset, Length, TDSI->getSourceString());
for (const auto &Item : TDSI->getTDRs()) {
std::string String =
TDSI->getSourceString().substr(Item->getOffset(), Item->getLength());
size_t NewOffset = Offset + Item->getOffset();
auto TDR = std::make_shared<TemplateDependentReplacement>(
String, NewOffset, String.size(), Item->getTemplateIndex());
TDRs.insert(std::make_pair(NewOffset, TDR));
}
}

// Add a string replacement
void addStringReplacement(size_t Offset, size_t Length, std::string Text) {
auto Result = ReplMap.insert(std::make_pair(
ReplMap.insert(std::make_pair(
Offset,
std::make_shared<StringReplacement>(SourceStr, Offset, Length, Text)));
if (Result.second) {
auto Shift = Result.first->second->getReplacedText().length() - Length;
ShiftLength += Shift;
auto TDRItr = TDRs.upper_bound(Result.first->first);
while (TDRItr != TDRs.end()) {
TDRItr->second->shift(Shift);
++TDRItr;
}
}
}

// Generate replacement text info which dependent on template args.
Expand All @@ -171,10 +184,10 @@ class StringReplacements {

void replaceString();

unsigned ShiftLength;
std::string SourceStr;
std::map<size_t, std::shared_ptr<StringReplacement>> ReplMap;
std::map<size_t, std::shared_ptr<TemplateDependentReplacement>> TDRs;
std::map<size_t, std::shared_ptr<TemplateDependentReplacement>> TDRs2;
};

/// Analyze expression and generate its migrated string
Expand Down Expand Up @@ -586,6 +599,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 +710,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, int ccc> __global__ void kerfunc() {
constexpr int kNumWarps = (2 * ThreadsPerBlock / kWarpSize * ccc);
__shared__ int smem[kNumWarps * NumWarpQ];
}

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