Skip to content

Commit 358303a

Browse files
committed
Merge branch 'SYCLomatic' into sorthalf
2 parents 9b51b63 + 9af6b57 commit 358303a

29 files changed

Lines changed: 573 additions & 116 deletions

File tree

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/AnalysisInfo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2947,7 +2947,7 @@ void MemVarInfo::migrateToDeviceGlobal(const VarDecl *MemVar) {
29472947
auto &Ctx = DpctGlobalInfo::getContext();
29482948
auto &MacroArgMap = DpctGlobalInfo::getMacroArgRecordMap();
29492949
auto TSI = MemVar->getTypeSourceInfo();
2950-
auto OriginTL = TSI->getTypeLoc();
2950+
auto OriginTL = TSI->getTypeLoc().getUnqualifiedLoc().getAs<TypeLoc>();
29512951
auto TL = OriginTL;
29522952
auto BegLoc = MemVar->getBeginLoc();
29532953
if (BegLoc.isMacroID()) {

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/lib/DPCT/RuleInfra/CallExprRewriterCommon.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ template <class SubExprT> class CastIfNotSameExprPrinter {
149149
clang::QualType ArgType = InputArg->getType().getCanonicalType();
150150
ArgType.removeLocalFastQualifiers(clang::Qualifiers::CVRMask);
151151
bool NeedParen = false;
152+
std::cout << "Arg type: " << ArgType.getAsString() << "\n";
153+
std::cout << "Given type " << TypeInfo << "\n";
152154
if (ArgType.getAsString() != TypeInfo) {
153155
NeedParen = needExtraParens(SubExpr);
154156
Stream << "(" << TypeInfo << ")";
@@ -2132,6 +2134,16 @@ class IsDefinedInCUDA {
21322134
return isFromCUDA(FD);
21332135
}
21342136
};
2137+
class IsDefinedByUser {
2138+
public:
2139+
IsDefinedByUser() {}
2140+
bool operator()(const CallExpr *C) {
2141+
auto FD = C->getDirectCallee();
2142+
if (!FD)
2143+
return false;
2144+
return DpctGlobalInfo::isInRoot(FD->getLocation());
2145+
}
2146+
};
21352147
} // namespace math
21362148
} // namespace dpct
21372149

clang/lib/DPCT/RulesLang/APINamesDriver.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ FEATURE_REQUEST_FACTORY(
4747
ASSIGNABLE_FACTORY(ASSIGN_FACTORY_ENTRY("cuModuleGetFunction", DEREF(0),
4848
CALL(MapNames::getDpctNamespace() +
4949
"get_kernel_function",
50-
ARG(1), ARG(2)))))
50+
ARG(1), CAST_IF_NOT_SAME(makeLiteral("const char *"), ARG(2))))))
5151

5252
FEATURE_REQUEST_FACTORY(
5353
HelperFeatureEnum::device_ext,

clang/lib/DPCT/RulesLang/Math/CallExprRewriterMath.h

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -388,29 +388,29 @@ class MathRewriterFactory final : public CallExprRewriterFactoryBase {
388388
return Rewriter.value();
389389
}
390390
}
391-
if (math::IsUnresolvedLookupExpr(C)) {
392-
if (math::IsDirectCallerPureDevice(C)) {
393-
if (Rewriter = getDeviceRewriter(C))
394-
return Rewriter.value();
395-
}
396-
}
397391
if (math::IsDefinedInCUDA()(C)) {
398392
if (Rewriter = getDeviceRewriter(C))
399393
return Rewriter.value();
400394
}
401395
}
402-
403-
// Host and device
404-
if (HostDeviceRewriter && HostDeviceRewriter.value().first(C))
405-
return HostDeviceRewriter.value().second.second->create(C);
406-
407-
if (EmulationRewriter && EmulationRewriter.value().first(C))
408-
return EmulationRewriter.value().second.second->create(C);
409-
410-
if (UnsupportedWarningRewriter &&
411-
UnsupportedWarningRewriter.value().first(C))
412-
return UnsupportedWarningRewriter.value().second.second->create(C);
413-
396+
if (math::IsUnresolvedLookupExpr(C)) {
397+
if (math::IsDirectCallerPureDevice(C)) {
398+
if (auto Rewriter = getDeviceRewriter(C))
399+
return Rewriter.value();
400+
} else if (math::IsDirectCallerPureHost(C)) {
401+
return NoRewriteRewriter.value().second.second->create(C);
402+
}
403+
}
404+
if (!math::IsDefinedByUser()(C)) {
405+
// Host and device
406+
if (HostDeviceRewriter && HostDeviceRewriter.value().first(C))
407+
return HostDeviceRewriter.value().second.second->create(C);
408+
if (EmulationRewriter && EmulationRewriter.value().first(C))
409+
return EmulationRewriter.value().second.second->create(C);
410+
if (UnsupportedWarningRewriter &&
411+
UnsupportedWarningRewriter.value().first(C))
412+
return UnsupportedWarningRewriter.value().second.second->create(C);
413+
}
414414
return NoRewriteRewriter.value().second.second->create(C);
415415
}
416416
};

0 commit comments

Comments
 (0)