Skip to content

[SYCLomatic] Fix insert duplicate dpct_operator_overloading #2898

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

Merged
merged 11 commits into from
Jul 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
38 changes: 38 additions & 0 deletions clang/lib/DPCT/ExtReplacements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,44 @@ void ExtReplacements::markAsAlive(std::shared_ptr<ExtReplacement> Repl) {
}
}

void ExtReplacements::markAsDead(std::shared_ptr<ExtReplacement> Repl) {
if (auto PairID = Repl->getPairID()) {
if (auto &R = PairReplsMap[PairID]) {
R->Status = PairReplsStatus::Dead;
auto BeginIter = ReplMap.lower_bound(R->Repl->getOffset());
auto EndIter = ReplMap.upper_bound(R->Repl->getOffset());
for (auto Start = BeginIter; Start != EndIter; Start++) {
if (*Start->second == *R->Repl) {
ReplMap.erase(Start);
break;
}
}
} else {
PairReplsMap[PairID] =
std::make_shared<PairReplsStatus>(Repl, PairReplsStatus::Dead);
}
}
}

bool ExtReplacements::checkLiveness(std::shared_ptr<ExtReplacement> Repl) {
if (isAlive(Repl))
// If a replacement in the same pair is alive, merge it anyway.
return true;
// Check if it is duplicate replacement.
return !isDuplicated(Repl, ReplMap.lower_bound(Repl->getOffset()),
ReplMap.upper_bound(Repl->getOffset()));
}
// Check if its pair has a replacement inserted.
bool ExtReplacements::isAlive(std::shared_ptr<ExtReplacement> Repl) {
if (auto PairID = Repl->getPairID()) {
if (auto &R = PairReplsMap[PairID]) {
if (!R->Repl->getReplacementText().contains(
"namespace dpct_operator_overloading {"))
return R->Status == PairReplsStatus::Alive;
}
}
return false;
}
bool ExtReplacements::isDuplicated(std::shared_ptr<ExtReplacement> Repl,
ReplIterator Begin, ReplIterator End) {
while (Begin != End) {
Expand Down
23 changes: 3 additions & 20 deletions clang/lib/DPCT/ExtReplacements.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,7 @@ class ExtReplacements {
// Check if the Repl is same as source code
bool isReplRedundant(std::shared_ptr<ExtReplacement> Repl,
std::shared_ptr<DpctFileInfo> FileInfo);
inline bool checkLiveness(std::shared_ptr<ExtReplacement> Repl) {
if (isAlive(Repl))
// If a replacement in the same pair is alive, merge it anyway.
return true;
// Check if it is duplicate replacement.
return !isDuplicated(Repl, ReplMap.lower_bound(Repl->getOffset()),
ReplMap.upper_bound(Repl->getOffset()));
}
bool checkLiveness(std::shared_ptr<ExtReplacement> Repl);

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

// Mark a replacement as dead.
void markAsDead(std::shared_ptr<ExtReplacement> Repl) {
if (auto PairID = Repl->getPairID())
PairReplsMap[PairID] =
std::make_shared<PairReplsStatus>(Repl, PairReplsStatus::Dead);
}
void markAsDead(std::shared_ptr<ExtReplacement> Repl);

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

// Check if its pair has a replacement inserted.
bool isAlive(std::shared_ptr<ExtReplacement> Repl) {
if (auto PairID = Repl->getPairID()) {
if (auto &R = PairReplsMap[PairID])
return R->Status == PairReplsStatus::Alive;
}
return false;
}
bool isAlive(std::shared_ptr<ExtReplacement> Repl);

clang::tooling::UnifiedPath FilePath;
///< Offset, ExtReplacement>
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/DPCT/RulesLang/RulesLang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1727,8 +1727,8 @@ void VectorTypeOperatorRule::MigrateOverloadedOperatorDecl(
SR = GetFunctionSourceRange(SM, FD->getBeginLoc(), FD->getEndLoc());
}
report(SR.getBegin(), Diagnostics::TRNA_WARNING_OVERLOADED_API_FOUND, false);
emplaceTransformation(new InsertText(SR.getBegin(), Prologue.str()));
emplaceTransformation(new InsertText(SR.getEnd(), Epilogue.str()));
insertAroundRange(SR.getBegin(), SR.getEnd(), Prologue.str(),
Epilogue.str());
}

void VectorTypeOperatorRule::MigrateOverloadedOperatorCall(
Expand Down
12 changes: 12 additions & 0 deletions clang/test/dpct/insert_around/compile_commands.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"command": "nvcc -c -o float_vector.o ./float_vector.cpp",
"directory": ".",
"file": "./float_vector.cpp"
},
{
"command": "c++ -c -o float_vector.o ./float_vector.cpp",
"directory": ".",
"file": "./float_vector.cpp"
}
]
21 changes: 21 additions & 0 deletions clang/test/dpct/insert_around/float_vector.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// UNSUPPORTED: system-windows
// RUN: cd %T
// RUN: cat %S/compile_commands.json > %T/compile_commands.json
// RUN: cat %S/float_vector.cpp > %T/float_vector.cpp
// RUN: dpct -p %T -out-root %T/float_vector --cuda-include-path="%cuda-path/include"
// RUN: FileCheck %s --match-full-lines --input-file %T/float_vector/float_vector.cpp.dp.cpp


#include <cuda_runtime.h>
namespace quda {
// CHECK: namespace dpct_operator_overloading {
// CHECK-EMPTY:
// CHECK-NEXT: inline sycl::double2 operator+(const sycl::double2 &x, const sycl::double2 &y)
__host__ __device__ inline double2 operator+(const double2 &x, const double2 &y)
{
}
// CHECK:} // namespace dpct_operator_overloading
}
int main() {
return 0;
}
Loading