Skip to content

Commit cc5c93a

Browse files
authored
[SYCLomatic] Refine file renaming migration logic for #include stmt consistent with migrated file name (#2913)
Signed-off-by: chenwei.sun <chenwei.sun@intel.com>
1 parent 1185217 commit cc5c93a

File tree

8 files changed

+73
-23
lines changed

8 files changed

+73
-23
lines changed

clang/lib/DPCT/AnalysisInfo.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1487,9 +1487,12 @@ std::string DpctGlobalInfo::getStringForRegexReplacement(StringRef MatchedStr) {
14871487
HelperFuncType::HFT_DefaultQueuePtr, Index);
14881488
case 'E': {
14891489
auto &Vec = DpctGlobalInfo::getInstance().getCSourceFileList();
1490-
return DpctGlobalInfo::hasCUDASyntax(Vec[Index])
1491-
? ("c" + DpctGlobalInfo::getSYCLSourceExtension())
1492-
: "c";
1490+
const bool HasCUDASyntax = DpctGlobalInfo::hasCUDASyntax(Vec[Index].first);
1491+
const auto Extention = Vec[Index].second;
1492+
SourceProcessType FileType = GetSourceFileType(Vec[Index].first);
1493+
return HasCUDASyntax && (FileType & SPT_CppSource)
1494+
? Extention + DpctGlobalInfo::getSYCLSourceExtension()
1495+
: Extention;
14931496
}
14941497
case 'P': {
14951498
std::string ReplStr;
@@ -2523,7 +2526,9 @@ bool DpctGlobalInfo::CVersionCUDALaunchUsedFlag = false;
25232526
unsigned int DpctGlobalInfo::ColorOption = 1;
25242527
std::unordered_map<int, std::shared_ptr<DeviceFunctionInfo>>
25252528
DpctGlobalInfo::CubPlaceholderIndexMap;
2526-
std::vector<tooling::UnifiedPath> DpctGlobalInfo::CSourceFileList;
2529+
std::vector<std::pair<tooling::UnifiedPath /*including filename*/,
2530+
std::string /*extention name*/>>
2531+
DpctGlobalInfo::CSourceFileList;
25272532
bool DpctGlobalInfo::OptimizeMigrationFlag = false;
25282533
std::unordered_map<std::string, std::shared_ptr<PriorityReplInfo>>
25292534
DpctGlobalInfo::PriorityReplInfoMap;

clang/lib/DPCT/AnalysisInfo.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1450,7 +1450,9 @@ class DpctGlobalInfo {
14501450
getCubPlaceholderIndexMap() {
14511451
return CubPlaceholderIndexMap;
14521452
}
1453-
std::vector<tooling::UnifiedPath> &getCSourceFileList() {
1453+
std::vector<std::pair<tooling::UnifiedPath /*including filename*/,
1454+
std::string /*extention name*/>> &
1455+
getCSourceFileList() {
14541456
return CSourceFileList;
14551457
}
14561458
static std::unordered_map<std::string, std::shared_ptr<PriorityReplInfo>> &
@@ -1703,7 +1705,9 @@ class DpctGlobalInfo {
17031705
static unsigned int ColorOption;
17041706
static std::unordered_map<int, std::shared_ptr<DeviceFunctionInfo>>
17051707
CubPlaceholderIndexMap;
1706-
static std::vector<tooling::UnifiedPath> CSourceFileList;
1708+
static std::vector<std::pair<tooling::UnifiedPath /*including filename*/,
1709+
std::string /*extention name*/>>
1710+
CSourceFileList;
17071711
static bool OptimizeMigrationFlag;
17081712
static std::unordered_map<std::string, std::shared_ptr<PriorityReplInfo>>
17091713
PriorityReplInfoMap;

clang/lib/DPCT/FileGenerator/GenFiles.cpp

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -211,18 +211,12 @@ void rewriteFileName(std::string &FileName, const std::string &FullPathName) {
211211
if (FileType & SPT_CudaSource) {
212212
path::replace_extension(CanonicalPathStr,
213213
DpctGlobalInfo::getSYCLSourceExtension());
214-
} else if (FileType & SPT_CppSource) {
215-
if (Extension == ".c") {
216-
if (DpctGlobalInfo::hasCUDASyntax(FileName)) {
217-
path::replace_extension(CanonicalPathStr,
218-
Extension +
219-
DpctGlobalInfo::getSYCLSourceExtension());
220-
}
221-
} else {
222-
path::replace_extension(CanonicalPathStr,
223-
Extension +
224-
DpctGlobalInfo::getSYCLSourceExtension());
225-
}
214+
} else if ((FileType & SPT_CppSource) &&
215+
DpctGlobalInfo::hasCUDASyntax(FileName)) {
216+
path::replace_extension(CanonicalPathStr,
217+
Extension +
218+
DpctGlobalInfo::getSYCLSourceExtension());
219+
226220
} else if (FileType & SPT_CudaHeader) {
227221
path::replace_extension(CanonicalPathStr,
228222
DpctGlobalInfo::getSYCLHeaderExtension());

clang/lib/DPCT/RulesInclude/InclusionHeaders.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,12 @@ void IncludesCallbacks::InclusionDirective(
175175
const auto Extension = path::extension(FileName);
176176
SmallString<512> NewFileName(FileName.str());
177177

178-
if (Extension == ".c") {
178+
if (Extension == ".c" || Extension == ".cc") {
179179
auto &Vec = DpctGlobalInfo::getInstance().getCSourceFileList();
180180
path::replace_extension(
181181
NewFileName, "{{NEEDREPLACEE" + std::to_string(Vec.size()) + "}}");
182-
Vec.push_back(IncludedFile);
182+
Vec.push_back(std::pair<tooling::UnifiedPath, std::string>(
183+
IncludedFile, Extension.substr(1)));
183184
} else {
184185
clang::tooling::UnifiedPath NewFilePath = FileName;
185186
rewriteFileName(NewFilePath, IncludedFile);

clang/test/dpct/include/main.cu

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,6 @@
5353
// case 4: cuh file not in database.
5454
// CHECK: #include "test4.dp.hpp"
5555
#include "test4.cuh"
56-
// case 5: header file has CUDA syntax, in database.
57-
// CHECK: #include "test5.h.dp.cpp"
58-
#include "test5.h"
5956
// case 6: header file has CUDA syntax, not in database.
6057
// CHECK: #include "test6.h"
6158
#include "test6.h"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// CHECK: MainSourceFilesDigest:
2+
// CHECK-NEXT: - MainSourceFile: '{{(.+)}}common.cpp'
3+
// CHECK-NEXT: Digest: {{(.+)}}
4+
// CHECK-NEXT: HasCUDASyntax: false
5+
// CHECK-NEXT: - MainSourceFile: '{{(.+)}}yaml_has_cuda_syntax/utils.cpp'
6+
// CHECK-NEXT: Digest: {{(.+)}}
7+
// CHECK-NEXT: HasCUDASyntax: true
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// UNSUPPORTED: system-windows
2+
// RUN: echo "[" > %T/compile_commands.json
3+
// RUN: echo " {" >> %T/compile_commands.json
4+
// RUN: echo " \"command\": \"nvcc %S/common.cpp\"," >> %T/compile_commands.json
5+
// RUN: echo " \"directory\": \"%T\"," >> %T/compile_commands.json
6+
// RUN: echo " \"file\": \"%S/common.cpp\"" >> %T/compile_commands.json
7+
// RUN: echo " }," >> %T/compile_commands.json
8+
// RUN: echo " {" >> %T/compile_commands.json
9+
// RUN: echo " \"command\": \"nvcc %S/utils.cpp\"," >> %T/compile_commands.json
10+
// RUN: echo " \"directory\": \"%T\"," >> %T/compile_commands.json
11+
// RUN: echo " \"file\": \"%S/utils.cpp\"" >> %T/compile_commands.json
12+
// RUN: echo " }" >> %T/compile_commands.json
13+
// RUN: echo "]" >> %T/compile_commands.json
14+
15+
// RUN: dpct -in-root=%S -p=%T --out-root=%T/out --cuda-include-path="%cuda-path/include"
16+
// RUN: FileCheck %s --match-full-lines --input-file %T/out/common.cpp
17+
// RUN: %if build_lit %{icpx -c -fsycl %T/out/common.cpp -o %T/out/common.o %}
18+
19+
// RUN: FileCheck %S/utils.cpp --match-full-lines --input-file %T/out/utils.cpp.dp.cpp
20+
// RUN: %if build_lit %{icpx -c -fsycl %T/out/utils.cpp.dp.cpp -o %T/out/utils.cpp.dp.o %}
21+
22+
// RUN: cat %S/MainSourceFiles.ref > %T/out/check_MainSourceFiles.yaml
23+
// RUN: cat %T/out/MainSourceFiles.yaml >>%T/out/check_MainSourceFiles.yaml
24+
// RUN: FileCheck --match-full-lines --input-file %T/out/check_MainSourceFiles.yaml %T/out/check_MainSourceFiles.yaml
25+
26+
// CHECK: #include <cmath>
27+
28+
void test() {
29+
int a = -1;
30+
// CHECK: abs(a);
31+
abs(a);
32+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// UNSUPPORTED: system-windows
2+
// RUN: echo "empty command"
3+
4+
5+
#include <iostream>
6+
#include <cuda_runtime.h>
7+
8+
// CHECK: void test_foo(){
9+
__device__ void test_foo(void){
10+
}

0 commit comments

Comments
 (0)