From e04f79c7d367cc5dbc131e63f6cc5df36152368c Mon Sep 17 00:00:00 2001 From: "chenwei.sun" Date: Wed, 25 Jun 2025 09:12:28 +0800 Subject: [PATCH 1/8] [SYCLomatic] Refine file renaming migration logic for `#include stmt` consistent with migrated file name Signed-off-by: chenwei.sun --- clang/lib/DPCT/AnalysisInfo.cpp | 18 +++++++++++++++--- clang/lib/DPCT/FileGenerator/GenFiles.cpp | 18 ++++++++++++------ .../lib/DPCT/RulesInclude/InclusionHeaders.cpp | 6 +++++- 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/clang/lib/DPCT/AnalysisInfo.cpp b/clang/lib/DPCT/AnalysisInfo.cpp index 36cd3cfe5405..c52b99bbc096 100644 --- a/clang/lib/DPCT/AnalysisInfo.cpp +++ b/clang/lib/DPCT/AnalysisInfo.cpp @@ -1487,9 +1487,21 @@ std::string DpctGlobalInfo::getStringForRegexReplacement(StringRef MatchedStr) { HelperFuncType::HFT_DefaultQueuePtr, Index); case 'E': { auto &Vec = DpctGlobalInfo::getInstance().getCSourceFileList(); - return DpctGlobalInfo::hasCUDASyntax(Vec[Index]) - ? ("c" + DpctGlobalInfo::getSYCLSourceExtension()) - : "c"; + SourceProcessType FileType = GetSourceFileType(Vec[Index]); + if (Vec[Index + 1] == ".h") + return DpctGlobalInfo::hasCUDASyntax(Vec[Index]) && + (FileType & SPT_CppSource) + ? ("h" + DpctGlobalInfo::getSYCLSourceExtension()) + : "h"; + if (Vec[Index + 1] == ".cc") + return DpctGlobalInfo::hasCUDASyntax(Vec[Index])&& + (FileType & SPT_CppSource) + ? ("cc" + DpctGlobalInfo::getSYCLSourceExtension()) + : "cc"; + + return DpctGlobalInfo::hasCUDASyntax(Vec[Index]) + ? ("c" + DpctGlobalInfo::getSYCLSourceExtension()) + : "c"; } case 'P': { std::string ReplStr; diff --git a/clang/lib/DPCT/FileGenerator/GenFiles.cpp b/clang/lib/DPCT/FileGenerator/GenFiles.cpp index a2d95abc735b..2bc1747a49ac 100644 --- a/clang/lib/DPCT/FileGenerator/GenFiles.cpp +++ b/clang/lib/DPCT/FileGenerator/GenFiles.cpp @@ -204,6 +204,8 @@ void rewriteFileName(std::string &FileName, const std::string &FullPathName) { SmallString<512> CanonicalPathStr(FullPathName); const auto Extension = path::extension(CanonicalPathStr); SourceProcessType FileType = GetSourceFileType(FullPathName); + + printf("FileType:%d %s\n", FileType, FullPathName.c_str()); // If user does not specify which extension need be changed, we change all the // SPT_CudaSource, SPT_CppSource and SPT_CudaHeader files. if (DpctGlobalInfo::getChangeExtensions().empty() || @@ -211,17 +213,21 @@ void rewriteFileName(std::string &FileName, const std::string &FullPathName) { if (FileType & SPT_CudaSource) { path::replace_extension(CanonicalPathStr, DpctGlobalInfo::getSYCLSourceExtension()); - } else if (FileType & SPT_CppSource) { + } else if ((FileType & SPT_CppSource) && + DpctGlobalInfo::hasCUDASyntax(FileName)) { if (Extension == ".c") { - if (DpctGlobalInfo::hasCUDASyntax(FileName)) { - path::replace_extension(CanonicalPathStr, - Extension + - DpctGlobalInfo::getSYCLSourceExtension()); - } + path::replace_extension(CanonicalPathStr, + Extension + + DpctGlobalInfo::getSYCLSourceExtension()); + } else { + printf("######################## %s\n", FileName.c_str()); path::replace_extension(CanonicalPathStr, Extension + DpctGlobalInfo::getSYCLSourceExtension()); + printf("CanonicalPathStr:%s\n", CanonicalPathStr.c_str()); + printf("########################0 %s\n", Extension.str().c_str()); + printf("########################1 %s\n", DpctGlobalInfo::getSYCLSourceExtension().c_str()); } } else if (FileType & SPT_CudaHeader) { path::replace_extension(CanonicalPathStr, diff --git a/clang/lib/DPCT/RulesInclude/InclusionHeaders.cpp b/clang/lib/DPCT/RulesInclude/InclusionHeaders.cpp index 67375ef33a80..d27af6c4328e 100644 --- a/clang/lib/DPCT/RulesInclude/InclusionHeaders.cpp +++ b/clang/lib/DPCT/RulesInclude/InclusionHeaders.cpp @@ -175,14 +175,18 @@ void IncludesCallbacks::InclusionDirective( const auto Extension = path::extension(FileName); SmallString<512> NewFileName(FileName.str()); - if (Extension == ".c") { + SourceProcessType FileType = GetSourceFileType(FileName); + if (Extension == ".c" || Extension == ".h" || Extension == ".cc") { auto &Vec = DpctGlobalInfo::getInstance().getCSourceFileList(); path::replace_extension( NewFileName, "{{NEEDREPLACEE" + std::to_string(Vec.size()) + "}}"); Vec.push_back(IncludedFile); + Vec.push_back(Extension); } else { clang::tooling::UnifiedPath NewFilePath = FileName; + printf("IncludedFile:%s\n", IncludedFile.getCanonicalPath().str().c_str()); rewriteFileName(NewFilePath, IncludedFile); + printf("NewFilePath:%s\n", NewFilePath.getCanonicalPath().str().c_str()); path::remove_filename(NewFileName); path::append(NewFileName, path::filename(NewFilePath.getCanonicalPath())); NewFileName = path::convert_to_slash(NewFileName, path::Style::native); From ea2a5f6d95553192c7d5d6b8b61cb6c9900571df Mon Sep 17 00:00:00 2001 From: "chenwei.sun" Date: Wed, 25 Jun 2025 09:30:27 +0800 Subject: [PATCH 2/8] remove print statements for debug Signed-off-by: chenwei.sun --- clang/lib/DPCT/AnalysisInfo.cpp | 32 ++++++++++--------- clang/lib/DPCT/FileGenerator/GenFiles.cpp | 17 ++-------- .../DPCT/RulesInclude/InclusionHeaders.cpp | 3 -- 3 files changed, 20 insertions(+), 32 deletions(-) diff --git a/clang/lib/DPCT/AnalysisInfo.cpp b/clang/lib/DPCT/AnalysisInfo.cpp index c52b99bbc096..547df4a45667 100644 --- a/clang/lib/DPCT/AnalysisInfo.cpp +++ b/clang/lib/DPCT/AnalysisInfo.cpp @@ -1487,21 +1487,23 @@ std::string DpctGlobalInfo::getStringForRegexReplacement(StringRef MatchedStr) { HelperFuncType::HFT_DefaultQueuePtr, Index); case 'E': { auto &Vec = DpctGlobalInfo::getInstance().getCSourceFileList(); - SourceProcessType FileType = GetSourceFileType(Vec[Index]); - if (Vec[Index + 1] == ".h") - return DpctGlobalInfo::hasCUDASyntax(Vec[Index]) && - (FileType & SPT_CppSource) - ? ("h" + DpctGlobalInfo::getSYCLSourceExtension()) - : "h"; - if (Vec[Index + 1] == ".cc") - return DpctGlobalInfo::hasCUDASyntax(Vec[Index])&& - (FileType & SPT_CppSource) - ? ("cc" + DpctGlobalInfo::getSYCLSourceExtension()) - : "cc"; - - return DpctGlobalInfo::hasCUDASyntax(Vec[Index]) - ? ("c" + DpctGlobalInfo::getSYCLSourceExtension()) - : "c"; + SourceProcessType FileType = GetSourceFileType(Vec[Index]); + + const bool HasCUDASyntax = DpctGlobalInfo::hasCUDASyntax(Vec[Index]); + const bool IsCppSource = (FileType & SPT_CppSource); + const auto Extention = Vec[Index + 1]; + + if (Extention == ".h") { + return (HasCUDASyntax && IsCppSource) + ? "h" + DpctGlobalInfo::getSYCLSourceExtension() + : "h"; + } + if (Extention == ".cc") { + return (HasCUDASyntax && IsCppSource) + ? "cc" + DpctGlobalInfo::getSYCLSourceExtension() + : "cc"; + } + return HasCUDASyntax ? "c" + DpctGlobalInfo::getSYCLSourceExtension() : "c"; } case 'P': { std::string ReplStr; diff --git a/clang/lib/DPCT/FileGenerator/GenFiles.cpp b/clang/lib/DPCT/FileGenerator/GenFiles.cpp index 2bc1747a49ac..1a15196b4f81 100644 --- a/clang/lib/DPCT/FileGenerator/GenFiles.cpp +++ b/clang/lib/DPCT/FileGenerator/GenFiles.cpp @@ -205,7 +205,6 @@ void rewriteFileName(std::string &FileName, const std::string &FullPathName) { const auto Extension = path::extension(CanonicalPathStr); SourceProcessType FileType = GetSourceFileType(FullPathName); - printf("FileType:%d %s\n", FileType, FullPathName.c_str()); // If user does not specify which extension need be changed, we change all the // SPT_CudaSource, SPT_CppSource and SPT_CudaHeader files. if (DpctGlobalInfo::getChangeExtensions().empty() || @@ -215,20 +214,10 @@ void rewriteFileName(std::string &FileName, const std::string &FullPathName) { DpctGlobalInfo::getSYCLSourceExtension()); } else if ((FileType & SPT_CppSource) && DpctGlobalInfo::hasCUDASyntax(FileName)) { - if (Extension == ".c") { - path::replace_extension(CanonicalPathStr, - Extension + - DpctGlobalInfo::getSYCLSourceExtension()); + path::replace_extension(CanonicalPathStr, + Extension + + DpctGlobalInfo::getSYCLSourceExtension()); - } else { - printf("######################## %s\n", FileName.c_str()); - path::replace_extension(CanonicalPathStr, - Extension + - DpctGlobalInfo::getSYCLSourceExtension()); - printf("CanonicalPathStr:%s\n", CanonicalPathStr.c_str()); - printf("########################0 %s\n", Extension.str().c_str()); - printf("########################1 %s\n", DpctGlobalInfo::getSYCLSourceExtension().c_str()); - } } else if (FileType & SPT_CudaHeader) { path::replace_extension(CanonicalPathStr, DpctGlobalInfo::getSYCLHeaderExtension()); diff --git a/clang/lib/DPCT/RulesInclude/InclusionHeaders.cpp b/clang/lib/DPCT/RulesInclude/InclusionHeaders.cpp index d27af6c4328e..bdc12d122b7a 100644 --- a/clang/lib/DPCT/RulesInclude/InclusionHeaders.cpp +++ b/clang/lib/DPCT/RulesInclude/InclusionHeaders.cpp @@ -175,7 +175,6 @@ void IncludesCallbacks::InclusionDirective( const auto Extension = path::extension(FileName); SmallString<512> NewFileName(FileName.str()); - SourceProcessType FileType = GetSourceFileType(FileName); if (Extension == ".c" || Extension == ".h" || Extension == ".cc") { auto &Vec = DpctGlobalInfo::getInstance().getCSourceFileList(); path::replace_extension( @@ -184,9 +183,7 @@ void IncludesCallbacks::InclusionDirective( Vec.push_back(Extension); } else { clang::tooling::UnifiedPath NewFilePath = FileName; - printf("IncludedFile:%s\n", IncludedFile.getCanonicalPath().str().c_str()); rewriteFileName(NewFilePath, IncludedFile); - printf("NewFilePath:%s\n", NewFilePath.getCanonicalPath().str().c_str()); path::remove_filename(NewFileName); path::append(NewFileName, path::filename(NewFilePath.getCanonicalPath())); NewFileName = path::convert_to_slash(NewFileName, path::Style::native); From f99f7432f64e7f491046bfafe02b5b84ae76d60d Mon Sep 17 00:00:00 2001 From: "chenwei.sun" Date: Wed, 25 Jun 2025 15:29:41 +0800 Subject: [PATCH 3/8] fix failure in CI test Signed-off-by: chenwei.sun --- clang/lib/DPCT/AnalysisInfo.cpp | 5 ----- clang/lib/DPCT/FileGenerator/GenFiles.cpp | 1 - clang/lib/DPCT/RulesInclude/InclusionHeaders.cpp | 2 +- clang/test/dpct/include/main.cu | 3 --- 4 files changed, 1 insertion(+), 10 deletions(-) diff --git a/clang/lib/DPCT/AnalysisInfo.cpp b/clang/lib/DPCT/AnalysisInfo.cpp index 547df4a45667..400cf252b00a 100644 --- a/clang/lib/DPCT/AnalysisInfo.cpp +++ b/clang/lib/DPCT/AnalysisInfo.cpp @@ -1493,11 +1493,6 @@ std::string DpctGlobalInfo::getStringForRegexReplacement(StringRef MatchedStr) { const bool IsCppSource = (FileType & SPT_CppSource); const auto Extention = Vec[Index + 1]; - if (Extention == ".h") { - return (HasCUDASyntax && IsCppSource) - ? "h" + DpctGlobalInfo::getSYCLSourceExtension() - : "h"; - } if (Extention == ".cc") { return (HasCUDASyntax && IsCppSource) ? "cc" + DpctGlobalInfo::getSYCLSourceExtension() diff --git a/clang/lib/DPCT/FileGenerator/GenFiles.cpp b/clang/lib/DPCT/FileGenerator/GenFiles.cpp index 1a15196b4f81..c00cc2d7019b 100644 --- a/clang/lib/DPCT/FileGenerator/GenFiles.cpp +++ b/clang/lib/DPCT/FileGenerator/GenFiles.cpp @@ -204,7 +204,6 @@ void rewriteFileName(std::string &FileName, const std::string &FullPathName) { SmallString<512> CanonicalPathStr(FullPathName); const auto Extension = path::extension(CanonicalPathStr); SourceProcessType FileType = GetSourceFileType(FullPathName); - // If user does not specify which extension need be changed, we change all the // SPT_CudaSource, SPT_CppSource and SPT_CudaHeader files. if (DpctGlobalInfo::getChangeExtensions().empty() || diff --git a/clang/lib/DPCT/RulesInclude/InclusionHeaders.cpp b/clang/lib/DPCT/RulesInclude/InclusionHeaders.cpp index bdc12d122b7a..1898a7d7656d 100644 --- a/clang/lib/DPCT/RulesInclude/InclusionHeaders.cpp +++ b/clang/lib/DPCT/RulesInclude/InclusionHeaders.cpp @@ -175,7 +175,7 @@ void IncludesCallbacks::InclusionDirective( const auto Extension = path::extension(FileName); SmallString<512> NewFileName(FileName.str()); - if (Extension == ".c" || Extension == ".h" || Extension == ".cc") { + if (Extension == ".c" || Extension == ".cc") { auto &Vec = DpctGlobalInfo::getInstance().getCSourceFileList(); path::replace_extension( NewFileName, "{{NEEDREPLACEE" + std::to_string(Vec.size()) + "}}"); diff --git a/clang/test/dpct/include/main.cu b/clang/test/dpct/include/main.cu index 86e91f5ec612..5336a352726c 100644 --- a/clang/test/dpct/include/main.cu +++ b/clang/test/dpct/include/main.cu @@ -53,9 +53,6 @@ // case 4: cuh file not in database. // CHECK: #include "test4.dp.hpp" #include "test4.cuh" -// case 5: header file has CUDA syntax, in database. -// CHECK: #include "test5.h.dp.cpp" -#include "test5.h" // case 6: header file has CUDA syntax, not in database. // CHECK: #include "test6.h" #include "test6.h" From f3d68cf489486c3c37fa8ba546a50651f58fef1c Mon Sep 17 00:00:00 2001 From: "chenwei.sun" Date: Thu, 26 Jun 2025 10:18:40 +0800 Subject: [PATCH 4/8] add lit test Signed-off-by: chenwei.sun --- .../yaml_has_cuda_syntax/MainSourceFiles.ref | 7 ++++ .../test/dpct/yaml_has_cuda_syntax/common.cpp | 32 +++++++++++++++++++ .../test/dpct/yaml_has_cuda_syntax/utils.cpp | 10 ++++++ 3 files changed, 49 insertions(+) create mode 100644 clang/test/dpct/yaml_has_cuda_syntax/MainSourceFiles.ref create mode 100644 clang/test/dpct/yaml_has_cuda_syntax/common.cpp create mode 100644 clang/test/dpct/yaml_has_cuda_syntax/utils.cpp diff --git a/clang/test/dpct/yaml_has_cuda_syntax/MainSourceFiles.ref b/clang/test/dpct/yaml_has_cuda_syntax/MainSourceFiles.ref new file mode 100644 index 000000000000..6475f14f58a0 --- /dev/null +++ b/clang/test/dpct/yaml_has_cuda_syntax/MainSourceFiles.ref @@ -0,0 +1,7 @@ +// CHECK: MainSourceFilesDigest: +// CHECK-NEXT: - MainSourceFile: '{{(.+)}}common.cpp' +// CHECK-NEXT: Digest: {{(.+)}} +// CHECK-NEXT: HasCUDASyntax: false +// CHECK-NEXT: - MainSourceFile: '{{(.+)}}yaml_has_cuda_syntax/utils.cpp' +// CHECK-NEXT: Digest: {{(.+)}} +// CHECK-NEXT: HasCUDASyntax: true diff --git a/clang/test/dpct/yaml_has_cuda_syntax/common.cpp b/clang/test/dpct/yaml_has_cuda_syntax/common.cpp new file mode 100644 index 000000000000..14a83921ed30 --- /dev/null +++ b/clang/test/dpct/yaml_has_cuda_syntax/common.cpp @@ -0,0 +1,32 @@ +// UNSUPPORTED: system-windows +// RUN: echo "[" > %T/compile_commands.json +// RUN: echo " {" >> %T/compile_commands.json +// RUN: echo " \"command\": \"nvcc %S/common.cpp\"," >> %T/compile_commands.json +// RUN: echo " \"directory\": \"%T\"," >> %T/compile_commands.json +// RUN: echo " \"file\": \"%S/common.cpp\"" >> %T/compile_commands.json +// RUN: echo " }," >> %T/compile_commands.json +// RUN: echo " {" >> %T/compile_commands.json +// RUN: echo " \"command\": \"nvcc %S/utils.cpp\"," >> %T/compile_commands.json +// RUN: echo " \"directory\": \"%T\"," >> %T/compile_commands.json +// RUN: echo " \"file\": \"%S/utils.cpp\"" >> %T/compile_commands.json +// RUN: echo " }" >> %T/compile_commands.json +// RUN: echo "]" >> %T/compile_commands.json + +// RUN: dpct -in-root=%S -p=%T --out-root=%T/out --cuda-include-path="%cuda-path/include" +// RUN: FileCheck %s --match-full-lines --input-file %T/out/common.cpp +// RUN: %if build_lit %{icpx -c -fsycl %T/out/common.cpp -o %T/out/common.o %} + +// RUN: FileCheck %S/utils.cpp --match-full-lines --input-file %T/out/utils.cpp.dp.cpp +// RUN: %if build_lit %{icpx -c -fsycl %T/out/utils.cpp.dp.cpp -o %T/out/ utils.cpp.dp.o %} + +// RUN: cat %S/MainSourceFiles.ref > %T/out/check_MainSourceFiles.yaml +// RUN: cat %T/out/MainSourceFiles.yaml >>%T/out/check_MainSourceFiles.yaml +// RUN: FileCheck --match-full-lines --input-file %T/out/check_MainSourceFiles.yaml %T/out/check_MainSourceFiles.yaml + +// CHECK: #include + +void test() { + int a = -1; + // CHECK: abs(a); + abs(a); +} diff --git a/clang/test/dpct/yaml_has_cuda_syntax/utils.cpp b/clang/test/dpct/yaml_has_cuda_syntax/utils.cpp new file mode 100644 index 000000000000..3c4f5417846a --- /dev/null +++ b/clang/test/dpct/yaml_has_cuda_syntax/utils.cpp @@ -0,0 +1,10 @@ +// UNSUPPORTED: system-windows +// RUN: echo "empty command" + + +#include +#include + +// CHECK: void test_foo(){ +__device__ void test_foo(void){ +} From 5ea41a8f5bc56e6f1dbb9639ce7bf4bad84decad Mon Sep 17 00:00:00 2001 From: "chenwei.sun" Date: Mon, 30 Jun 2025 11:12:23 +0800 Subject: [PATCH 5/8] fix CI failure Signed-off-by: chenwei.sun --- clang/test/dpct/yaml_has_cuda_syntax/common.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang/test/dpct/yaml_has_cuda_syntax/common.cpp b/clang/test/dpct/yaml_has_cuda_syntax/common.cpp index 14a83921ed30..bb661f733952 100644 --- a/clang/test/dpct/yaml_has_cuda_syntax/common.cpp +++ b/clang/test/dpct/yaml_has_cuda_syntax/common.cpp @@ -17,7 +17,7 @@ // RUN: %if build_lit %{icpx -c -fsycl %T/out/common.cpp -o %T/out/common.o %} // RUN: FileCheck %S/utils.cpp --match-full-lines --input-file %T/out/utils.cpp.dp.cpp -// RUN: %if build_lit %{icpx -c -fsycl %T/out/utils.cpp.dp.cpp -o %T/out/ utils.cpp.dp.o %} +// RUN: %if build_lit %{icpx -c -fsycl %T/out/utils.cpp.dp.cpp -o %T/out/utils.cpp.dp.o %} // RUN: cat %S/MainSourceFiles.ref > %T/out/check_MainSourceFiles.yaml // RUN: cat %T/out/MainSourceFiles.yaml >>%T/out/check_MainSourceFiles.yaml From 4a181f0d206125610ef5c4ce3e22ac81994858e0 Mon Sep 17 00:00:00 2001 From: "chenwei.sun" Date: Mon, 30 Jun 2025 14:28:59 +0800 Subject: [PATCH 6/8] refine code Signed-off-by: chenwei.sun --- clang/lib/DPCT/AnalysisInfo.cpp | 10 ++++++---- clang/lib/DPCT/AnalysisInfo.h | 8 ++++++-- clang/lib/DPCT/RulesInclude/InclusionHeaders.cpp | 5 +++-- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/clang/lib/DPCT/AnalysisInfo.cpp b/clang/lib/DPCT/AnalysisInfo.cpp index 400cf252b00a..f74e12e72902 100644 --- a/clang/lib/DPCT/AnalysisInfo.cpp +++ b/clang/lib/DPCT/AnalysisInfo.cpp @@ -1487,11 +1487,11 @@ std::string DpctGlobalInfo::getStringForRegexReplacement(StringRef MatchedStr) { HelperFuncType::HFT_DefaultQueuePtr, Index); case 'E': { auto &Vec = DpctGlobalInfo::getInstance().getCSourceFileList(); - SourceProcessType FileType = GetSourceFileType(Vec[Index]); + SourceProcessType FileType = GetSourceFileType(Vec[Index].first); - const bool HasCUDASyntax = DpctGlobalInfo::hasCUDASyntax(Vec[Index]); + const bool HasCUDASyntax = DpctGlobalInfo::hasCUDASyntax(Vec[Index].first); const bool IsCppSource = (FileType & SPT_CppSource); - const auto Extention = Vec[Index + 1]; + const auto Extention = Vec[Index].second; if (Extention == ".cc") { return (HasCUDASyntax && IsCppSource) @@ -2532,7 +2532,9 @@ bool DpctGlobalInfo::CVersionCUDALaunchUsedFlag = false; unsigned int DpctGlobalInfo::ColorOption = 1; std::unordered_map> DpctGlobalInfo::CubPlaceholderIndexMap; -std::vector DpctGlobalInfo::CSourceFileList; +std::vector> + DpctGlobalInfo::CSourceFileList; bool DpctGlobalInfo::OptimizeMigrationFlag = false; std::unordered_map> DpctGlobalInfo::PriorityReplInfoMap; diff --git a/clang/lib/DPCT/AnalysisInfo.h b/clang/lib/DPCT/AnalysisInfo.h index 06958a3785ec..dfbc65bd9734 100644 --- a/clang/lib/DPCT/AnalysisInfo.h +++ b/clang/lib/DPCT/AnalysisInfo.h @@ -1450,7 +1450,9 @@ class DpctGlobalInfo { getCubPlaceholderIndexMap() { return CubPlaceholderIndexMap; } - std::vector &getCSourceFileList() { + std::vector> & + getCSourceFileList() { return CSourceFileList; } static std::unordered_map> & @@ -1703,7 +1705,9 @@ class DpctGlobalInfo { static unsigned int ColorOption; static std::unordered_map> CubPlaceholderIndexMap; - static std::vector CSourceFileList; + static std::vector> + CSourceFileList; static bool OptimizeMigrationFlag; static std::unordered_map> PriorityReplInfoMap; diff --git a/clang/lib/DPCT/RulesInclude/InclusionHeaders.cpp b/clang/lib/DPCT/RulesInclude/InclusionHeaders.cpp index 1898a7d7656d..f283b19def3c 100644 --- a/clang/lib/DPCT/RulesInclude/InclusionHeaders.cpp +++ b/clang/lib/DPCT/RulesInclude/InclusionHeaders.cpp @@ -10,6 +10,7 @@ #include "PreProcessor.h" #include "UserDefinedRules/UserDefinedRules.h" #include +#include namespace clang { namespace dpct { @@ -179,8 +180,8 @@ void IncludesCallbacks::InclusionDirective( auto &Vec = DpctGlobalInfo::getInstance().getCSourceFileList(); path::replace_extension( NewFileName, "{{NEEDREPLACEE" + std::to_string(Vec.size()) + "}}"); - Vec.push_back(IncludedFile); - Vec.push_back(Extension); + Vec.push_back(std::pair(IncludedFile, + Extension)); } else { clang::tooling::UnifiedPath NewFilePath = FileName; rewriteFileName(NewFilePath, IncludedFile); From e95e17beb2b871a019684bd75265248a5a490fb1 Mon Sep 17 00:00:00 2001 From: "chenwei.sun" Date: Tue, 1 Jul 2025 14:56:51 +0800 Subject: [PATCH 7/8] Address review comments Signed-off-by: chenwei.sun --- clang/lib/DPCT/AnalysisInfo.cpp | 11 +---------- clang/lib/DPCT/RulesInclude/InclusionHeaders.cpp | 4 ++-- clang/test/dpct/include/main.cu | 2 +- 3 files changed, 4 insertions(+), 13 deletions(-) diff --git a/clang/lib/DPCT/AnalysisInfo.cpp b/clang/lib/DPCT/AnalysisInfo.cpp index f74e12e72902..81f7e6092804 100644 --- a/clang/lib/DPCT/AnalysisInfo.cpp +++ b/clang/lib/DPCT/AnalysisInfo.cpp @@ -1487,18 +1487,9 @@ std::string DpctGlobalInfo::getStringForRegexReplacement(StringRef MatchedStr) { HelperFuncType::HFT_DefaultQueuePtr, Index); case 'E': { auto &Vec = DpctGlobalInfo::getInstance().getCSourceFileList(); - SourceProcessType FileType = GetSourceFileType(Vec[Index].first); - const bool HasCUDASyntax = DpctGlobalInfo::hasCUDASyntax(Vec[Index].first); - const bool IsCppSource = (FileType & SPT_CppSource); const auto Extention = Vec[Index].second; - - if (Extention == ".cc") { - return (HasCUDASyntax && IsCppSource) - ? "cc" + DpctGlobalInfo::getSYCLSourceExtension() - : "cc"; - } - return HasCUDASyntax ? "c" + DpctGlobalInfo::getSYCLSourceExtension() : "c"; + return HasCUDASyntax ? Extention + DpctGlobalInfo::getSYCLSourceExtension() : Extention; } case 'P': { std::string ReplStr; diff --git a/clang/lib/DPCT/RulesInclude/InclusionHeaders.cpp b/clang/lib/DPCT/RulesInclude/InclusionHeaders.cpp index f283b19def3c..5fab5d6184ff 100644 --- a/clang/lib/DPCT/RulesInclude/InclusionHeaders.cpp +++ b/clang/lib/DPCT/RulesInclude/InclusionHeaders.cpp @@ -180,8 +180,8 @@ void IncludesCallbacks::InclusionDirective( auto &Vec = DpctGlobalInfo::getInstance().getCSourceFileList(); path::replace_extension( NewFileName, "{{NEEDREPLACEE" + std::to_string(Vec.size()) + "}}"); - Vec.push_back(std::pair(IncludedFile, - Extension)); + Vec.push_back(std::pair( + IncludedFile, Extension.substr(1))); } else { clang::tooling::UnifiedPath NewFilePath = FileName; rewriteFileName(NewFilePath, IncludedFile); diff --git a/clang/test/dpct/include/main.cu b/clang/test/dpct/include/main.cu index 5336a352726c..614e09483e05 100644 --- a/clang/test/dpct/include/main.cu +++ b/clang/test/dpct/include/main.cu @@ -66,7 +66,7 @@ // CHECK: #include "test9.cc.dp.cpp" #include "test9.cc" // case 10: source file has CUDA syntax, not in database. -// CHECK: #include "test10.cc" +// CHECK: #include "test10.cc.dp.cpp" #include "test10.cc" // case 11: source file does not have CUDA syntax, in database. // CHECK: #include "test11.cc" From 9289a4915eba6b46cf29a4db41fc30ff44e84e62 Mon Sep 17 00:00:00 2001 From: "chenwei.sun" Date: Thu, 3 Jul 2025 13:32:13 +0800 Subject: [PATCH 8/8] fix CI lit test failure Signed-off-by: chenwei.sun --- clang/lib/DPCT/AnalysisInfo.cpp | 5 ++++- clang/lib/DPCT/RulesInclude/InclusionHeaders.cpp | 1 - clang/test/dpct/include/main.cu | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/clang/lib/DPCT/AnalysisInfo.cpp b/clang/lib/DPCT/AnalysisInfo.cpp index 81f7e6092804..d8df52c64eb4 100644 --- a/clang/lib/DPCT/AnalysisInfo.cpp +++ b/clang/lib/DPCT/AnalysisInfo.cpp @@ -1489,7 +1489,10 @@ std::string DpctGlobalInfo::getStringForRegexReplacement(StringRef MatchedStr) { auto &Vec = DpctGlobalInfo::getInstance().getCSourceFileList(); const bool HasCUDASyntax = DpctGlobalInfo::hasCUDASyntax(Vec[Index].first); const auto Extention = Vec[Index].second; - return HasCUDASyntax ? Extention + DpctGlobalInfo::getSYCLSourceExtension() : Extention; + SourceProcessType FileType = GetSourceFileType(Vec[Index].first); + return HasCUDASyntax && (FileType & SPT_CppSource) + ? Extention + DpctGlobalInfo::getSYCLSourceExtension() + : Extention; } case 'P': { std::string ReplStr; diff --git a/clang/lib/DPCT/RulesInclude/InclusionHeaders.cpp b/clang/lib/DPCT/RulesInclude/InclusionHeaders.cpp index 5fab5d6184ff..79dc6d5d9bb4 100644 --- a/clang/lib/DPCT/RulesInclude/InclusionHeaders.cpp +++ b/clang/lib/DPCT/RulesInclude/InclusionHeaders.cpp @@ -10,7 +10,6 @@ #include "PreProcessor.h" #include "UserDefinedRules/UserDefinedRules.h" #include -#include namespace clang { namespace dpct { diff --git a/clang/test/dpct/include/main.cu b/clang/test/dpct/include/main.cu index 614e09483e05..5336a352726c 100644 --- a/clang/test/dpct/include/main.cu +++ b/clang/test/dpct/include/main.cu @@ -66,7 +66,7 @@ // CHECK: #include "test9.cc.dp.cpp" #include "test9.cc" // case 10: source file has CUDA syntax, not in database. -// CHECK: #include "test10.cc.dp.cpp" +// CHECK: #include "test10.cc" #include "test10.cc" // case 11: source file does not have CUDA syntax, in database. // CHECK: #include "test11.cc"