Skip to content

Commit 27f5cf0

Browse files
authored
[SYCLomatic][CMake] Refine dpct.cmake file and migration of CUDA_VERSION and set_target_properties (#2792)
Signed-off-by: chenwei.sun <chenwei.sun@intel.com>
1 parent 8513308 commit 27f5cf0

File tree

15 files changed

+292
-31
lines changed

15 files changed

+292
-31
lines changed

clang/include/clang/Tooling/Core/Replacement.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,8 @@ struct TranslationUnitReplacements {
436436
#ifdef SYCLomatic_CUSTOMIZATION
437437
std::vector<MainSourceFileInfo> MainSourceFilesDigest;
438438
std::string DpctVersion = "";
439+
std::string SDKVersionMajor = "";
440+
std::string SDKVersionMinor = "";
439441
std::string MainHelperFileName = "";
440442
std::string USMLevel = ""; // deprecated
441443

clang/include/clang/Tooling/ReplacementsYaml.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,8 @@ template <> struct MappingTraits<clang::tooling::TranslationUnitReplacements> {
269269
#ifdef SYCLomatic_CUSTOMIZATION
270270
Io.mapOptional("MainSourceFilesDigest", Doc.MainSourceFilesDigest);
271271
Io.mapOptional("DpctVersion", Doc.DpctVersion);
272+
Io.mapOptional("SDKVersionMajor", Doc.SDKVersionMajor);
273+
Io.mapOptional("SDKVersionMinor", Doc.SDKVersionMinor);
272274
Io.mapOptional("MainHelperFileName", Doc.MainHelperFileName);
273275
Io.mapOptional("USMLevel", Doc.USMLevel);
274276
// Keep here only for backward compatibility - begin

clang/lib/DPCT/DPCT.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,59 @@ void parseFormatStyle() {
463463
DpctGlobalInfo::setCodeFormatStyle(Style);
464464
}
465465

466+
void updateCompatibilityVersionInfo(clang::tooling::UnifiedPath OutRoot,
467+
std::string Major, std::string Minor) {
468+
const std::string CmakeHelpFile =
469+
appendPath(OutRoot.getCanonicalPath().str(), "dpct.cmake");
470+
std::ifstream InFile(CmakeHelpFile);
471+
if (!InFile) {
472+
std::string ErrMsg = "Failed to open file: " + CmakeHelpFile;
473+
ShowStatus(MigrationErrorReadWriteCMakeHelperFile, std::move(ErrMsg));
474+
dpctExit(MigrationErrorReadWriteCMakeHelperFile);
475+
}
476+
477+
const std::string VersionStr = Major + "." + Minor;
478+
const int CompatibilityValue = std::stoi(Major) * 10 + std::stoi(Minor);
479+
std::vector<std::string> Lines;
480+
std::string Line;
481+
bool Inserted = false;
482+
483+
// Constant block of content to insert
484+
const std::vector<std::string> CompatibilityBlock = {
485+
"set(COMPATIBILITY_VERSION " + VersionStr + ")",
486+
"set(COMPATIBILITY_VALUE " + std::to_string(CompatibilityValue) + ")",
487+
"set(COMPATIBILITY_VERSION_MAJOR " + Major + ")",
488+
"set(COMPATIBILITY_VERSION_MINOR " + Minor + ")",
489+
"" // Add an empty line for good format
490+
};
491+
492+
auto isCommentOrEmpty = [](const std::string &Line) {
493+
return Line.empty() || Line[0] == '#';
494+
};
495+
496+
while (std::getline(InFile, Line)) {
497+
// Insert the compatibility definition block after the first comment section
498+
if (!Inserted && !isCommentOrEmpty(Line)) {
499+
Lines.insert(Lines.end(), CompatibilityBlock.begin(),
500+
CompatibilityBlock.end());
501+
Inserted = true;
502+
}
503+
Lines.push_back(Line);
504+
}
505+
InFile.close();
506+
507+
std::ofstream OutFile(CmakeHelpFile);
508+
if (!OutFile) {
509+
std::string ErrMsg = "Failed to write to file: " + CmakeHelpFile;
510+
ShowStatus(MigrationErrorReadWriteCMakeHelperFile, std::move(ErrMsg));
511+
dpctExit(MigrationErrorReadWriteCMakeHelperFile);
512+
}
513+
for (const auto &Line : Lines) {
514+
OutFile << Line << "\n";
515+
}
516+
OutFile.close();
517+
}
518+
466519
static void loadMainSrcFileInfo(clang::tooling::UnifiedPath OutRoot) {
467520
std::string YamlFilePath = appendPath(OutRoot.getCanonicalPath().str(),
468521
DpctGlobalInfo::getYamlFileName());
@@ -471,7 +524,16 @@ static void loadMainSrcFileInfo(clang::tooling::UnifiedPath OutRoot) {
471524
if (loadFromYaml(YamlFilePath, *PreTU) != 0) {
472525
llvm::errs() << getLoadYamlFailWarning(YamlFilePath);
473526
}
527+
528+
if (MigrateBuildScriptOnly || DpctGlobalInfo::migrateCMakeScripts()) {
529+
std::string Major = PreTU->SDKVersionMajor;
530+
std::string Minor = PreTU->SDKVersionMinor;
531+
if (!Major.empty() && !Minor.empty()) {
532+
updateCompatibilityVersionInfo(OutRoot, Major, Minor);
533+
}
534+
}
474535
}
536+
475537
for (auto &Entry : PreTU->MainSourceFilesDigest) {
476538
if (Entry.HasCUDASyntax)
477539
MainSrcFilesHasCudaSyntex.insert(Entry.MainSourceFile);

clang/lib/DPCT/ErrorHandle/Error.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,10 @@ void ShowStatus(int Status, std::string Message) {
220220
case MigrationErrorInvalidInstallPath:
221221
StatusString = "Error: " + Message + " not found.";
222222
break;
223+
case MigrationErrorReadWriteCMakeHelperFile:
224+
StatusString =
225+
"Error: " + Message + ". Please check the file access permission.";
226+
break;
223227
default:
224228
DpctLog() << "Unknown error.\n";
225229
dpctExit(-1);

clang/lib/DPCT/ErrorHandle/Error.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ enum ProcessStatus {
6767
MigrationErrorCannotWrite = -52,
6868
MigratePythonBuildScriptOnlyNotSpecifed = -53,
6969
MigratePythonBuildScriptSpecifiedButPythonRuleFileNotSpecified = -54,
70+
MigrationErrorReadWriteCMakeHelperFile = -55,
7071
};
7172

7273
namespace clang {

clang/lib/DPCT/IncMigration/ExternalReplacement.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ int save2Yaml(
6262
}
6363

6464
TUR.DpctVersion = clang::dpct::getDpctVersionStr();
65+
66+
auto Ver = clang::getCudaVersionPair(DpctGlobalInfo::getSDKVersion());
67+
TUR.SDKVersionMajor = std::to_string(Ver.first);
68+
TUR.SDKVersionMinor = std::to_string(Ver.second);
69+
6570
TUR.OptionMap = clang::dpct::DpctGlobalInfo::getCurrentOptMap();
6671

6772
YAMLOut << TUR;

clang/test/dpct/cmake_migration/case_015/expected.txt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,23 @@ set_target_properties(target_one PROPERTIES CXX_Standard 14)
2424
set_target_properties(target_one PROPERTIES CXX_STANDARD 17)
2525

2626
# Test CUDA_SEPARABLE_COMPILATION
27-
#[[
28-
set_target_properties(target_one PROPERTIES CUDA_SEPARABLE_COMPILATION Off)]]
27+
set_target_properties(target_one PROPERTIES "" "")
2928

3029
# No change unless property matches exactly
3130
set_target_properties(target_one PROPERTIES Cuda_SEPARABLE_COMPILATION Off)
3231

3332
# Test CUDA_ARCHITECHTURES
34-
#[[
35-
set_target_properties(target_one PROPERTIES CUDA_ARCHITECHTURES Auto)]]
33+
set_target_properties(target_one PROPERTIES "" "")
3634

3735
# No change unless property matches exactly
3836
set_target_properties(target_one PROPERTIES cuda_architechtures All)
3937

4038
add_library(target_two MODULE nosrc2.dp.cpp)
4139

4240
# Test two target specs
43-
#[[
44-
set_target_properties(target_one target_two PROPERTIES CUDA_ARCHITECHTURES Kepler+Tesla)]]
41+
set_target_properties(target_one target_two PROPERTIES "" "")
4542

4643
# Make sure other properties are intact
4744
set_target_properties(target_one PROPERTIES OTHER_PROPERTY 1)
4845

46+
set_target_properties(target PROPERTIES "" "")

clang/test/dpct/cmake_migration/case_015/input.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,7 @@ set_target_properties(target_one
6464
CUDA_ARCHITECHTURES Kepler+Tesla
6565
OTHER_PROPERTY 1)
6666

67+
set_target_properties(target
68+
PROPERTIES
69+
CUDA_SEPARABLE_COMPILATION ON
70+
)

clang/test/dpct/cmake_migration/case_019/expected.txt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
if (COMPATIBILITY_VERSION VERSION_LESS 10.2)
1+
if (COMPATIBILITY_VALUE VERSION_LESS 10.2)
22
message(FATAL_ERROR "low version!.")
33
endif()
44

55
string(REGEX REPLACE "([0-9]+)\\.([0-9]+).*" "\\1" VERSION_MAJOR "${COMPATIBILITY_VERSION}")
66

7-
list(APPEND LIBS ${CUDA_HOME}/${COMPATIBILITY_VERSION}/lib64)
8-
9-
set(CMAKE_CUDA_VERSION ${COMPATIBILITY_VERSION})
7+
set(CMAKE_CUDA_VERSION ${COMPATIBILITY_VALUE})
108

119
if (COMPATIBILITY_VERSION_STRING STRGREATER "9.0")
1210
find_package(Eigen3 3.3.5 REQUIRED NO_MODULE)

clang/test/dpct/cmake_migration/case_019/input.cmake

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ if (CUDA_VERSION VERSION_LESS 10.2)
22
message(FATAL_ERROR "low version!.")
33
endif()
44

5-
string(REGEX REPLACE "([0-9]+)\\.([0-9]+).*" "\\1" VERSION_MAJOR "${CUDA_VERSION}")
6-
7-
list(APPEND LIBS ${CUDA_HOME}/${CUDA_VERSION}/lib64)
5+
string(REGEX REPLACE "([0-9]+)\\.([0-9]+).*" "\\1" VERSION_MAJOR "${CMAKE_CUDA_COMPILER_VERSION}")
86

97
set(CMAKE_CUDA_VERSION ${CUDA_VERSION})
108

0 commit comments

Comments
 (0)