From dc7dbdad528074451b499dd5192be346f0fee432 Mon Sep 17 00:00:00 2001 From: "Chen, Sheng S" Date: Mon, 16 Jun 2025 15:29:31 +0800 Subject: [PATCH 1/8] [SYCLomatic] Fix insert duplicate opertor Signed-off-by: Chen, Sheng S --- clang/lib/DPCT/ExtReplacements.h | 24 ++++++++++++++----- clang/lib/DPCT/RulesLang/RulesLang.cpp | 4 ++-- .../dpct/insert_around/compile_commands.json | 12 ++++++++++ clang/test/dpct/insert_around/test.cu | 17 +++++++++++++ 4 files changed, 49 insertions(+), 8 deletions(-) create mode 100644 clang/test/dpct/insert_around/compile_commands.json create mode 100644 clang/test/dpct/insert_around/test.cu diff --git a/clang/lib/DPCT/ExtReplacements.h b/clang/lib/DPCT/ExtReplacements.h index c24e9a04de44..62fb46d24512 100644 --- a/clang/lib/DPCT/ExtReplacements.h +++ b/clang/lib/DPCT/ExtReplacements.h @@ -67,12 +67,18 @@ class ExtReplacements { bool isReplRedundant(std::shared_ptr Repl, std::shared_ptr FileInfo); inline bool checkLiveness(std::shared_ptr Repl) { + bool ret = false; if (isAlive(Repl)) // If a replacement in the same pair is alive, merge it anyway. - return true; + ret = true; // Check if it is duplicate replacement. - return !isDuplicated(Repl, ReplMap.lower_bound(Repl->getOffset()), - ReplMap.upper_bound(Repl->getOffset())); + if (!isDuplicated(Repl, ReplMap.lower_bound(Repl->getOffset()), + ReplMap.upper_bound(Repl->getOffset()))) { + ret = true; + } else { + ret = false; + } + return ret; } bool isDuplicated(std::shared_ptr Repl, ReplIterator Begin, @@ -137,9 +143,15 @@ class ExtReplacements { // Mark a replacement as dead. void markAsDead(std::shared_ptr Repl) { - if (auto PairID = Repl->getPairID()) - PairReplsMap[PairID] = - std::make_shared(Repl, PairReplsStatus::Dead); + if (auto PairID = Repl->getPairID()) { + if (auto &R = PairReplsMap[PairID]) { + R->Status = PairReplsStatus::Dead; + ReplMap.erase(ReplMap.lower_bound(R->Repl->getOffset())); + } else { + PairReplsMap[PairID] = + std::make_shared(Repl, PairReplsStatus::Dead); + } + } } // Mark a replacement as alive and insert into ReplMap diff --git a/clang/lib/DPCT/RulesLang/RulesLang.cpp b/clang/lib/DPCT/RulesLang/RulesLang.cpp index e4cefbbc1c85..8d72a04aeb28 100644 --- a/clang/lib/DPCT/RulesLang/RulesLang.cpp +++ b/clang/lib/DPCT/RulesLang/RulesLang.cpp @@ -1724,8 +1724,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( diff --git a/clang/test/dpct/insert_around/compile_commands.json b/clang/test/dpct/insert_around/compile_commands.json new file mode 100644 index 000000000000..acf9c03295c8 --- /dev/null +++ b/clang/test/dpct/insert_around/compile_commands.json @@ -0,0 +1,12 @@ +[ + { + "command": "nvcc -c -o test.o ./test.cu", + "directory": ".", + "file": "./test.cu" + }, + { + "command": "c++ -c -o test.o ./test.cu", + "directory": ".", + "file": "./test.cu" + } +] \ No newline at end of file diff --git a/clang/test/dpct/insert_around/test.cu b/clang/test/dpct/insert_around/test.cu new file mode 100644 index 000000000000..ef5eb6d28eb5 --- /dev/null +++ b/clang/test/dpct/insert_around/test.cu @@ -0,0 +1,17 @@ +// RUN: cd %T +// RUN: cat %S/compile_commands.json > %T/compile_commands.json +// RUN: cat %S/test.cu > %T/test.cu +// RUN: dpct -p %S -out-root %T/insert_around --cuda-include-path="%cuda-path/include" + +#include +namespace quda { + // CHECK: return int8_t(a + b); + __host__ __device__ inline double2 operator+(const double2 &x, const double2 &y) + { + } + + +} +int main() { + return 0; +} From 4cea3375f46a403bb677719ede5e5dc0c2c957d0 Mon Sep 17 00:00:00 2001 From: "Chen, Sheng S" Date: Fri, 20 Jun 2025 11:12:48 +0800 Subject: [PATCH 2/8] up Signed-off-by: Chen, Sheng S --- clang/lib/DPCT/ExtReplacements.h | 11 +++------- .../test/dpct/insert_around/insert_around.cu | 20 +++++++++++++++++++ clang/test/dpct/insert_around/test.cu | 17 ---------------- 3 files changed, 23 insertions(+), 25 deletions(-) create mode 100644 clang/test/dpct/insert_around/insert_around.cu delete mode 100644 clang/test/dpct/insert_around/test.cu diff --git a/clang/lib/DPCT/ExtReplacements.h b/clang/lib/DPCT/ExtReplacements.h index 62fb46d24512..1071ca85fd97 100644 --- a/clang/lib/DPCT/ExtReplacements.h +++ b/clang/lib/DPCT/ExtReplacements.h @@ -70,15 +70,10 @@ class ExtReplacements { bool ret = false; if (isAlive(Repl)) // If a replacement in the same pair is alive, merge it anyway. - ret = true; + return true; // Check if it is duplicate replacement. - if (!isDuplicated(Repl, ReplMap.lower_bound(Repl->getOffset()), - ReplMap.upper_bound(Repl->getOffset()))) { - ret = true; - } else { - ret = false; - } - return ret; + return !isDuplicated(Repl, ReplMap.lower_bound(Repl->getOffset()), + ReplMap.upper_bound(Repl->getOffset())); } bool isDuplicated(std::shared_ptr Repl, ReplIterator Begin, diff --git a/clang/test/dpct/insert_around/insert_around.cu b/clang/test/dpct/insert_around/insert_around.cu new file mode 100644 index 000000000000..7e6a50f88a2d --- /dev/null +++ b/clang/test/dpct/insert_around/insert_around.cu @@ -0,0 +1,20 @@ +// RUN: cd %T +// RUN: cat %S/compile_commands.json > %T/compile_commands.json +// RUN: cat %S/insert_around.cu > %T/insert_around.cu +// RUN: dpct -p %T %T/insert_around.cu -out-root %T/insert_around --cuda-include-path="%cuda-path/include" +// RUN: FileCheck %s --match-full-lines --input-file %T/insert_around/insert_around.dp.cpp + + +#include +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; +} diff --git a/clang/test/dpct/insert_around/test.cu b/clang/test/dpct/insert_around/test.cu deleted file mode 100644 index ef5eb6d28eb5..000000000000 --- a/clang/test/dpct/insert_around/test.cu +++ /dev/null @@ -1,17 +0,0 @@ -// RUN: cd %T -// RUN: cat %S/compile_commands.json > %T/compile_commands.json -// RUN: cat %S/test.cu > %T/test.cu -// RUN: dpct -p %S -out-root %T/insert_around --cuda-include-path="%cuda-path/include" - -#include -namespace quda { - // CHECK: return int8_t(a + b); - __host__ __device__ inline double2 operator+(const double2 &x, const double2 &y) - { - } - - -} -int main() { - return 0; -} From a88fdec1b864110fc3774177df05f03bbce41994 Mon Sep 17 00:00:00 2001 From: "Chen, Sheng S" Date: Fri, 20 Jun 2025 11:13:52 +0800 Subject: [PATCH 3/8] up Signed-off-by: Chen, Sheng S --- clang/lib/DPCT/ExtReplacements.h | 1 - 1 file changed, 1 deletion(-) diff --git a/clang/lib/DPCT/ExtReplacements.h b/clang/lib/DPCT/ExtReplacements.h index 1071ca85fd97..7b5d16adf35f 100644 --- a/clang/lib/DPCT/ExtReplacements.h +++ b/clang/lib/DPCT/ExtReplacements.h @@ -67,7 +67,6 @@ class ExtReplacements { bool isReplRedundant(std::shared_ptr Repl, std::shared_ptr FileInfo); inline bool checkLiveness(std::shared_ptr Repl) { - bool ret = false; if (isAlive(Repl)) // If a replacement in the same pair is alive, merge it anyway. return true; From 5b61caa66e2b3ddc7831ef613fd1c9f5a5bfd74a Mon Sep 17 00:00:00 2001 From: "Chen, Sheng S" Date: Mon, 23 Jun 2025 11:18:27 +0800 Subject: [PATCH 4/8] update Signed-off-by: Chen, Sheng S --- clang/lib/DPCT/ExtReplacements.cpp | 38 +++++++++++++++++++ clang/lib/DPCT/ExtReplacements.h | 29 ++------------ .../dpct/insert_around/compile_commands.json | 8 ++-- .../{insert_around.cu => float_vector.cpp} | 2 +- 4 files changed, 46 insertions(+), 31 deletions(-) rename clang/test/dpct/insert_around/{insert_around.cu => float_vector.cpp} (84%) diff --git a/clang/lib/DPCT/ExtReplacements.cpp b/clang/lib/DPCT/ExtReplacements.cpp index 2d4e0eef7a96..486e1758304c 100644 --- a/clang/lib/DPCT/ExtReplacements.cpp +++ b/clang/lib/DPCT/ExtReplacements.cpp @@ -305,6 +305,44 @@ void ExtReplacements::markAsAlive(std::shared_ptr Repl) { } } +void ExtReplacements::markAsDead(std::shared_ptr 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 (*beginIter->second == *R->Repl) { + ReplMap.erase(beginIter); + break; + } + } + } else { + PairReplsMap[PairID] = + std::make_shared(Repl, PairReplsStatus::Dead); + } + } +} + +bool ExtReplacements::checkLiveness(std::shared_ptr 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 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 Repl, ReplIterator Begin, ReplIterator End) { while (Begin != End) { diff --git a/clang/lib/DPCT/ExtReplacements.h b/clang/lib/DPCT/ExtReplacements.h index 7b5d16adf35f..3307689873de 100644 --- a/clang/lib/DPCT/ExtReplacements.h +++ b/clang/lib/DPCT/ExtReplacements.h @@ -66,14 +66,7 @@ class ExtReplacements { // Check if the Repl is same as source code bool isReplRedundant(std::shared_ptr Repl, std::shared_ptr FileInfo); - inline bool checkLiveness(std::shared_ptr 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 Repl); bool isDuplicated(std::shared_ptr Repl, ReplIterator Begin, ReplIterator End); @@ -136,17 +129,7 @@ class ExtReplacements { size_t findCR(StringRef Line); // Mark a replacement as dead. - void markAsDead(std::shared_ptr Repl) { - if (auto PairID = Repl->getPairID()) { - if (auto &R = PairReplsMap[PairID]) { - R->Status = PairReplsStatus::Dead; - ReplMap.erase(ReplMap.lower_bound(R->Repl->getOffset())); - } else { - PairReplsMap[PairID] = - std::make_shared(Repl, PairReplsStatus::Dead); - } - } - } + void markAsDead(std::shared_ptr Repl); // Mark a replacement as alive and insert into ReplMap // If it is not the first encountered replacement and the first one is @@ -154,13 +137,7 @@ class ExtReplacements { void markAsAlive(std::shared_ptr Repl); // Check if its pair has a replacement inserted. - bool isAlive(std::shared_ptr Repl) { - if (auto PairID = Repl->getPairID()) { - if (auto &R = PairReplsMap[PairID]) - return R->Status == PairReplsStatus::Alive; - } - return false; - } + bool isAlive(std::shared_ptr Repl); clang::tooling::UnifiedPath FilePath; ///< Offset, ExtReplacement> diff --git a/clang/test/dpct/insert_around/compile_commands.json b/clang/test/dpct/insert_around/compile_commands.json index acf9c03295c8..c0235aac2e61 100644 --- a/clang/test/dpct/insert_around/compile_commands.json +++ b/clang/test/dpct/insert_around/compile_commands.json @@ -1,12 +1,12 @@ [ { - "command": "nvcc -c -o test.o ./test.cu", + "command": "nvcc -c -o float_vector.o ./float_vector.cpp", "directory": ".", - "file": "./test.cu" + "file": "./float_vector.cpp" }, { - "command": "c++ -c -o test.o ./test.cu", + "command": "c++ -c -o float_vector.o ./float_vector.cpp", "directory": ".", - "file": "./test.cu" + "file": "./float_vector.cpp" } ] \ No newline at end of file diff --git a/clang/test/dpct/insert_around/insert_around.cu b/clang/test/dpct/insert_around/float_vector.cpp similarity index 84% rename from clang/test/dpct/insert_around/insert_around.cu rename to clang/test/dpct/insert_around/float_vector.cpp index 7e6a50f88a2d..e6cdba70db66 100644 --- a/clang/test/dpct/insert_around/insert_around.cu +++ b/clang/test/dpct/insert_around/float_vector.cpp @@ -1,7 +1,7 @@ // RUN: cd %T // RUN: cat %S/compile_commands.json > %T/compile_commands.json // RUN: cat %S/insert_around.cu > %T/insert_around.cu -// RUN: dpct -p %T %T/insert_around.cu -out-root %T/insert_around --cuda-include-path="%cuda-path/include" +// RUN: dpct -p %T -out-root %T/insert_around --cuda-include-path="%cuda-path/include" // RUN: FileCheck %s --match-full-lines --input-file %T/insert_around/insert_around.dp.cpp From 6fde99f0c6a8d8f8793efd8e6f7c2c6bfc1eae10 Mon Sep 17 00:00:00 2001 From: "Chen, Sheng S" Date: Mon, 23 Jun 2025 11:25:46 +0800 Subject: [PATCH 5/8] update lit Signed-off-by: Chen, Sheng S --- clang/test/dpct/insert_around/float_vector.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/clang/test/dpct/insert_around/float_vector.cpp b/clang/test/dpct/insert_around/float_vector.cpp index e6cdba70db66..902c706e6853 100644 --- a/clang/test/dpct/insert_around/float_vector.cpp +++ b/clang/test/dpct/insert_around/float_vector.cpp @@ -1,8 +1,8 @@ // RUN: cd %T // RUN: cat %S/compile_commands.json > %T/compile_commands.json -// RUN: cat %S/insert_around.cu > %T/insert_around.cu -// RUN: dpct -p %T -out-root %T/insert_around --cuda-include-path="%cuda-path/include" -// RUN: FileCheck %s --match-full-lines --input-file %T/insert_around/insert_around.dp.cpp +// 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 From 876ec8704e7adc826bd1753514f68cbec445b549 Mon Sep 17 00:00:00 2001 From: "Chen, Sheng S" Date: Fri, 27 Jun 2025 09:43:20 +0800 Subject: [PATCH 6/8] update lit Signed-off-by: Chen, Sheng S --- clang/test/dpct/insert_around/float_vector.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/clang/test/dpct/insert_around/float_vector.cpp b/clang/test/dpct/insert_around/float_vector.cpp index 902c706e6853..bf3b66e0b975 100644 --- a/clang/test/dpct/insert_around/float_vector.cpp +++ b/clang/test/dpct/insert_around/float_vector.cpp @@ -1,3 +1,4 @@ +// 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 From 535e062fc93f81d3bca186d7bfe859f69b6fa3aa Mon Sep 17 00:00:00 2001 From: "Chen, Sheng S" Date: Mon, 7 Jul 2025 08:53:42 +0800 Subject: [PATCH 7/8] up Signed-off-by: Chen, Sheng S --- clang/lib/DPCT/ExtReplacements.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/clang/lib/DPCT/ExtReplacements.cpp b/clang/lib/DPCT/ExtReplacements.cpp index 486e1758304c..53a192ae78ba 100644 --- a/clang/lib/DPCT/ExtReplacements.cpp +++ b/clang/lib/DPCT/ExtReplacements.cpp @@ -309,11 +309,11 @@ void ExtReplacements::markAsDead(std::shared_ptr 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 (*beginIter->second == *R->Repl) { - ReplMap.erase(beginIter); + auto BeginIter = ReplMap.lower_bound(R->Repl->getOffset()); + auto EndIter = ReplMap.upper_bound(R->Repl->getOffset()); + for (auto start = BeginIter; start != EndIter; start++) { + if (*BeginIter->second == *R->Repl) { + ReplMap.erase(BeginIter); break; } } From dd6490bc535d25b3dd8db5f9d048ff04fcccdb99 Mon Sep 17 00:00:00 2001 From: "Chen, Sheng S" Date: Mon, 7 Jul 2025 16:17:27 +0800 Subject: [PATCH 8/8] up Signed-off-by: Chen, Sheng S --- clang/lib/DPCT/ExtReplacements.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/clang/lib/DPCT/ExtReplacements.cpp b/clang/lib/DPCT/ExtReplacements.cpp index 53a192ae78ba..ffeadf5b6295 100644 --- a/clang/lib/DPCT/ExtReplacements.cpp +++ b/clang/lib/DPCT/ExtReplacements.cpp @@ -311,9 +311,9 @@ void ExtReplacements::markAsDead(std::shared_ptr Repl) { 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 (*BeginIter->second == *R->Repl) { - ReplMap.erase(BeginIter); + for (auto Start = BeginIter; Start != EndIter; Start++) { + if (*Start->second == *R->Repl) { + ReplMap.erase(Start); break; } }