Skip to content

Commit 0fe9d81

Browse files
[SYCLomatic] Fix insert duplicate dpct_operator_overloading (#2898)
Signed-off-by: Chen, Sheng S <sheng.s.chen@intel.com>
2 parents 3d1ed39 + dd6490b commit 0fe9d81

File tree

5 files changed

+76
-22
lines changed

5 files changed

+76
-22
lines changed

clang/lib/DPCT/ExtReplacements.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,44 @@ void ExtReplacements::markAsAlive(std::shared_ptr<ExtReplacement> Repl) {
305305
}
306306
}
307307

308+
void ExtReplacements::markAsDead(std::shared_ptr<ExtReplacement> Repl) {
309+
if (auto PairID = Repl->getPairID()) {
310+
if (auto &R = PairReplsMap[PairID]) {
311+
R->Status = PairReplsStatus::Dead;
312+
auto BeginIter = ReplMap.lower_bound(R->Repl->getOffset());
313+
auto EndIter = ReplMap.upper_bound(R->Repl->getOffset());
314+
for (auto Start = BeginIter; Start != EndIter; Start++) {
315+
if (*Start->second == *R->Repl) {
316+
ReplMap.erase(Start);
317+
break;
318+
}
319+
}
320+
} else {
321+
PairReplsMap[PairID] =
322+
std::make_shared<PairReplsStatus>(Repl, PairReplsStatus::Dead);
323+
}
324+
}
325+
}
326+
327+
bool ExtReplacements::checkLiveness(std::shared_ptr<ExtReplacement> Repl) {
328+
if (isAlive(Repl))
329+
// If a replacement in the same pair is alive, merge it anyway.
330+
return true;
331+
// Check if it is duplicate replacement.
332+
return !isDuplicated(Repl, ReplMap.lower_bound(Repl->getOffset()),
333+
ReplMap.upper_bound(Repl->getOffset()));
334+
}
335+
// Check if its pair has a replacement inserted.
336+
bool ExtReplacements::isAlive(std::shared_ptr<ExtReplacement> Repl) {
337+
if (auto PairID = Repl->getPairID()) {
338+
if (auto &R = PairReplsMap[PairID]) {
339+
if (!R->Repl->getReplacementText().contains(
340+
"namespace dpct_operator_overloading {"))
341+
return R->Status == PairReplsStatus::Alive;
342+
}
343+
}
344+
return false;
345+
}
308346
bool ExtReplacements::isDuplicated(std::shared_ptr<ExtReplacement> Repl,
309347
ReplIterator Begin, ReplIterator End) {
310348
while (Begin != End) {

clang/lib/DPCT/ExtReplacements.h

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,7 @@ class ExtReplacements {
6666
// Check if the Repl is same as source code
6767
bool isReplRedundant(std::shared_ptr<ExtReplacement> Repl,
6868
std::shared_ptr<DpctFileInfo> FileInfo);
69-
inline bool checkLiveness(std::shared_ptr<ExtReplacement> Repl) {
70-
if (isAlive(Repl))
71-
// If a replacement in the same pair is alive, merge it anyway.
72-
return true;
73-
// Check if it is duplicate replacement.
74-
return !isDuplicated(Repl, ReplMap.lower_bound(Repl->getOffset()),
75-
ReplMap.upper_bound(Repl->getOffset()));
76-
}
69+
bool checkLiveness(std::shared_ptr<ExtReplacement> Repl);
7770

7871
bool isDuplicated(std::shared_ptr<ExtReplacement> Repl, ReplIterator Begin,
7972
ReplIterator End);
@@ -136,25 +129,15 @@ class ExtReplacements {
136129
size_t findCR(StringRef Line);
137130

138131
// Mark a replacement as dead.
139-
void markAsDead(std::shared_ptr<ExtReplacement> Repl) {
140-
if (auto PairID = Repl->getPairID())
141-
PairReplsMap[PairID] =
142-
std::make_shared<PairReplsStatus>(Repl, PairReplsStatus::Dead);
143-
}
132+
void markAsDead(std::shared_ptr<ExtReplacement> Repl);
144133

145134
// Mark a replacement as alive and insert into ReplMap
146135
// If it is not the first encountered replacement and the first one is
147136
// dead, insert the first one into ReplMap, too.
148137
void markAsAlive(std::shared_ptr<ExtReplacement> Repl);
149138

150139
// Check if its pair has a replacement inserted.
151-
bool isAlive(std::shared_ptr<ExtReplacement> Repl) {
152-
if (auto PairID = Repl->getPairID()) {
153-
if (auto &R = PairReplsMap[PairID])
154-
return R->Status == PairReplsStatus::Alive;
155-
}
156-
return false;
157-
}
140+
bool isAlive(std::shared_ptr<ExtReplacement> Repl);
158141

159142
clang::tooling::UnifiedPath FilePath;
160143
///< Offset, ExtReplacement>

clang/lib/DPCT/RulesLang/RulesLang.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1727,8 +1727,8 @@ void VectorTypeOperatorRule::MigrateOverloadedOperatorDecl(
17271727
SR = GetFunctionSourceRange(SM, FD->getBeginLoc(), FD->getEndLoc());
17281728
}
17291729
report(SR.getBegin(), Diagnostics::TRNA_WARNING_OVERLOADED_API_FOUND, false);
1730-
emplaceTransformation(new InsertText(SR.getBegin(), Prologue.str()));
1731-
emplaceTransformation(new InsertText(SR.getEnd(), Epilogue.str()));
1730+
insertAroundRange(SR.getBegin(), SR.getEnd(), Prologue.str(),
1731+
Epilogue.str());
17321732
}
17331733

17341734
void VectorTypeOperatorRule::MigrateOverloadedOperatorCall(
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[
2+
{
3+
"command": "nvcc -c -o float_vector.o ./float_vector.cpp",
4+
"directory": ".",
5+
"file": "./float_vector.cpp"
6+
},
7+
{
8+
"command": "c++ -c -o float_vector.o ./float_vector.cpp",
9+
"directory": ".",
10+
"file": "./float_vector.cpp"
11+
}
12+
]
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// UNSUPPORTED: system-windows
2+
// RUN: cd %T
3+
// RUN: cat %S/compile_commands.json > %T/compile_commands.json
4+
// RUN: cat %S/float_vector.cpp > %T/float_vector.cpp
5+
// RUN: dpct -p %T -out-root %T/float_vector --cuda-include-path="%cuda-path/include"
6+
// RUN: FileCheck %s --match-full-lines --input-file %T/float_vector/float_vector.cpp.dp.cpp
7+
8+
9+
#include <cuda_runtime.h>
10+
namespace quda {
11+
// CHECK: namespace dpct_operator_overloading {
12+
// CHECK-EMPTY:
13+
// CHECK-NEXT: inline sycl::double2 operator+(const sycl::double2 &x, const sycl::double2 &y)
14+
__host__ __device__ inline double2 operator+(const double2 &x, const double2 &y)
15+
{
16+
}
17+
// CHECK:} // namespace dpct_operator_overloading
18+
}
19+
int main() {
20+
return 0;
21+
}

0 commit comments

Comments
 (0)