Skip to content

[DO NOT MERGE][SYCLomatic] reMigration #2857

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 70 commits into
base: SYCLomatic
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
70 commits
Select commit Hold shift + click to select a range
f737620
[SYCLomatic] gitdiff2yaml
zhiweij1 May 26, 2025
900b2ab
Add tests
zhiweij1 May 26, 2025
60fcdaf
Merge remote-tracking branch 'origin/SYCLomatic' into git_to_yaml
zhiweij1 May 26, 2025
22e4cf4
refine repl
zhiweij1 May 27, 2025
59227ff
format
zhiweij1 May 27, 2025
2154ad5
Update
zhiweij1 May 27, 2025
6601a02
Fix
zhiweij1 May 27, 2025
9bbe00a
Fix build
zhiweij1 May 28, 2025
5a6147d
Fix load
zhiweij1 May 28, 2025
92b1399
Update
zhiweij1 May 28, 2025
8d4a783
Update
zhiweij1 May 28, 2025
63f6f8e
Update
zhiweij1 May 28, 2025
81ac3b6
Fix
zhiweij1 May 28, 2025
4577dd0
Merge remote-tracking branch 'origin/SYCLomatic' into git_to_yaml
zhiweij1 May 28, 2025
1303478
Merge remote-tracking branch 'origin/SYCLomatic' into repl
zhiweij1 May 28, 2025
c1f1834
Fix build
zhiweij1 May 28, 2025
ce62ba8
Update
zhiweij1 May 29, 2025
b4dc41d
Save yaml into globalinfo
zhiweij1 May 29, 2025
94b7268
Fix path
zhiweij1 May 29, 2025
f31055b
Format
zhiweij1 May 29, 2025
8f4f284
Merge branch 'git_to_yaml' into repl
zhiweij1 May 29, 2025
63e6fbc
Format
zhiweij1 May 29, 2025
89edb93
WIP
zhiweij1 Jun 3, 2025
8eb1e47
1
zhiweij1 Jun 4, 2025
c4397d0
remove unused changes
zhiweij1 Jun 4, 2025
989a1a3
Update
zhiweij1 Jun 4, 2025
ba244bc
1
zhiweij1 Jun 4, 2025
4e2dca3
Update
zhiweij1 Jun 4, 2025
023a3bb
1
zhiweij1 Jun 4, 2025
1a80e3d
Merge branch 'repl_WIP' into repl
zhiweij1 Jun 4, 2025
4752c67
WIP
zhiweij1 Jun 4, 2025
a1fe561
add merge
zhiweij1 Jun 4, 2025
00d66d7
fix build
zhiweij1 Jun 4, 2025
574f529
Merge remote-tracking branch 'origin/SYCLomatic' into repl
zhiweij1 Jun 10, 2025
f4a7e1c
Merge remote-tracking branch 'origin/SYCLomatic' into repl
zhiweij1 Jun 11, 2025
bcd7e6e
Merge remote-tracking branch 'origin/SYCLomatic' into repl
zhiweij1 Jun 12, 2025
95db140
Merge remote-tracking branch 'origin/SYCLomatic' into repl
zhiweij1 Jun 13, 2025
fd108b8
Merge remote-tracking branch 'origin/SYCLomatic' into repl
zhiweij1 Jun 16, 2025
b80321e
Merge remote-tracking branch 'origin/SYCLomatic' into repl
zhiweij1 Jun 17, 2025
f4e07f9
Add test for calculateUpdatedRanges
zhiweij1 Jun 17, 2025
005db06
Add test for groupReplcementsByFile and convertReplcementsLineString
zhiweij1 Jun 18, 2025
cd62439
add test for splitReplInOrderToNotCrossLines
zhiweij1 Jun 19, 2025
9dfe19b
add test for mergeMapsByLine
zhiweij1 Jun 19, 2025
6774b83
Merge remote-tracking branch 'origin/SYCLomatic' into repl
zhiweij1 Jun 19, 2025
82ca75f
Update test
zhiweij1 Jun 19, 2025
882661f
Small fix
zhiweij1 Jun 19, 2025
2873a73
Add test for mergeC1AndC2
zhiweij1 Jun 20, 2025
50bca90
Update
zhiweij1 Jun 20, 2025
c700ece
Update
zhiweij1 Jun 20, 2025
bf2dd55
All tests passed
zhiweij1 Jun 26, 2025
aaf2fe6
Merge remote-tracking branch 'origin/SYCLomatic' into repl
zhiweij1 Jun 26, 2025
f2bfc1a
Merge remote-tracking branch 'origin/SYCLomatic' into repl
zhiweij1 Jun 26, 2025
ecd447b
Small update
zhiweij1 Jun 26, 2025
29b373e
Refine the conflict mark
zhiweij1 Jun 27, 2025
487096c
Try to integrate, but has bug
zhiweij1 Jul 3, 2025
5c74a69
Some fix (other bugs are in gitdiff2yaml tool (double quote/single qu…
zhiweij1 Jul 3, 2025
3c42608
intergrate
zhiweij1 Jul 3, 2025
fb4f64b
Update
zhiweij1 Jul 4, 2025
0ab1eb2
Merge remote-tracking branch 'origin/SYCLomatic' into repl
zhiweij1 Jul 4, 2025
1d76652
Update
zhiweij1 Jul 4, 2025
88db20d
Update
zhiweij1 Jul 7, 2025
f427384
Update
zhiweij1 Jul 8, 2025
2789382
[SYCLomatic] Fix CMake script migration
zhiweij1 Jul 7, 2025
29c72b7
Fix
zhiweij1 Jul 8, 2025
f622dc3
Merge remote-tracking branch 'origin/SYCLomatic' into repl
zhiweij1 Jul 9, 2025
26444ff
Fix unittest
zhiweij1 Jul 9, 2025
8b181fc
Update fix
zhiweij1 Jul 9, 2025
39dea43
remove some tests
zhiweij1 Jul 9, 2025
6b8619c
Fix a bug in gitdiff2yaml
zhiweij1 Jul 11, 2025
33929fd
Merge remote-tracking branch 'origin/SYCLomatic' into repl
zhiweij1 Jul 11, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions clang/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,7 @@ if (NOT CLANG_BUILT_STANDALONE)
list(APPEND DPCT_RUN
install-dpct-intercept-build
install-dpct-pattern-rewriter
install-dpct-gitdiff2yaml
install-dpct-headers
install-clang-resource-headers
install-dpct-binary
Expand Down
6 changes: 6 additions & 0 deletions clang/include/clang/DPCT/DPCTOptions.inc
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,12 @@ DPCT_FLAG_OPTION(
"--out-root directory. Default: off."),
llvm::cl::cat(CtHelpCatAll), llvm::cl::cat(CtHelpCatCodeGen))

DPCT_FLAG_OPTION(
ReMigration, clang::dpct::DpctOptionClass::OC_Attribute,
DPCT_OPTION_ACTIONS(clang::dpct::DpctActionKind::DAK_Migration),
"remigration", llvm::cl::desc("Enable remigration. Default: off."),
llvm::cl::cat(CtHelpCatAll), llvm::cl::cat(CtHelpCatCodeGen))

DPCT_FLAG_OPTION(
PathToHelperFunction, clang::dpct::DpctOptionClass::OC_Action,
DPCT_OPTION_ACTIONS(clang::dpct::DpctActionKind::DAK_Help),
Expand Down
1 change: 1 addition & 0 deletions clang/lib/DPCT/AnalysisInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2513,6 +2513,7 @@ unsigned DpctGlobalInfo::ExperimentalFlag = 0;
unsigned DpctGlobalInfo::HelperFuncPreferenceFlag = 0;
bool DpctGlobalInfo::AnalysisModeFlag = false;
bool DpctGlobalInfo::UseSYCLCompatFlag = false;
bool DpctGlobalInfo::ReMigrationFlag = false;
bool DpctGlobalInfo::CVersionCUDALaunchUsedFlag = false;
unsigned int DpctGlobalInfo::ColorOption = 1;
std::unordered_map<int, std::shared_ptr<DeviceFunctionInfo>>
Expand Down
3 changes: 3 additions & 0 deletions clang/lib/DPCT/AnalysisInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -1349,6 +1349,8 @@ class DpctGlobalInfo {
static bool isCVersionCUDALaunchUsed() { return CVersionCUDALaunchUsedFlag; }
static void setUseSYCLCompat(bool Flag = true) { UseSYCLCompatFlag = Flag; }
static bool useSYCLCompat() { return UseSYCLCompatFlag; }
static void setReMigration(bool Flag = true) { ReMigrationFlag = Flag; }
static bool useReMigration() { return ReMigrationFlag; }
static bool useEnqueueBarrier() {
return getUsingExtensionDE(
DPCPPExtensionsDefaultEnabled::ExtDE_EnqueueBarrier);
Expand Down Expand Up @@ -1682,6 +1684,7 @@ class DpctGlobalInfo {
static unsigned HelperFuncPreferenceFlag;
static bool AnalysisModeFlag;
static bool UseSYCLCompatFlag;
static bool ReMigrationFlag;
static bool CVersionCUDALaunchUsedFlag;
static unsigned int ColorOption;
static std::unordered_map<int, std::shared_ptr<DeviceFunctionInfo>>
Expand Down
1 change: 1 addition & 0 deletions clang/lib/DPCT/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ add_clang_library(DPCT
MigrateScript/GenMakefile.cpp
RulesInclude/InclusionHeaders.cpp
IncMigration/IncrementalMigrationUtility.cpp
IncMigration/ReMigration.cpp
UserDefinedRules/UserDefinedRules.cpp
UserDefinedRules/PatternRewriter.cpp
MigrateScript/MigrateBuildScript.cpp
Expand Down
7 changes: 5 additions & 2 deletions clang/lib/DPCT/DPCT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ using namespace clang::tooling;
using namespace llvm::cl;

extern bool isDPCT;

extern bool ReMigrationReady;
namespace clang {
namespace tooling {
UnifiedPath getFormatSearchPath();
Expand Down Expand Up @@ -516,7 +516,7 @@ static void loadMainSrcFileInfo(clang::tooling::UnifiedPath YamlFilePath) {
if (!llvm::sys::fs::exists(YamlFilePath.getCanonicalPath()))
return;
auto PreTU = std::make_shared<clang::tooling::TranslationUnitReplacements>();
if (loadFromYaml(YamlFilePath, *PreTU) != 0) {
if (loadTUFromYaml(YamlFilePath, *PreTU) != 0) {
llvm::errs() << getLoadYamlFailWarning();
}
DpctGlobalInfo::setMainSourceYamlTUR(PreTU);
Expand Down Expand Up @@ -804,6 +804,7 @@ int runDPCT(int argc, const char **argv) {
CudaIncludePath = CudaInclude;
SDKPath = SDKPathOpt;

DpctGlobalInfo::setReMigration(ReMigration);
loadMainSrcFileInfo(OutRootPath.getCanonicalPath() + "/MainSourceFiles.yaml");

std::transform(
Expand Down Expand Up @@ -1391,6 +1392,8 @@ int runDPCT(int argc, const char **argv) {
dpctExit(MigrationSucceeded, false);
}

ReMigrationReady = tryLoadingUpstreamChangesAndUserChanges();

ReplTy ReplCUDA, ReplSYCL;
volatile int RunCount = 0;
do {
Expand Down
77 changes: 62 additions & 15 deletions clang/lib/DPCT/FileGenerator/GenFiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ extern DpctOption<clang::dpct::opt, bool> ProcessAll;
extern DpctOption<dpct::opt, std::string> BuildScriptFile;
extern DpctOption<dpct::opt, bool> GenBuildScript;
extern std::map<std::string, uint64_t> ErrorCnt;
bool ReMigrationReady = false;
extern std::set<std::string> MainSrcFilesHasCudaSyntex;

namespace clang {
namespace tooling {
Expand Down Expand Up @@ -188,18 +190,10 @@ bool rewriteCanonicalDir(clang::tooling::UnifiedPath &FilePath,
return Result;
}

void rewriteFileName(clang::tooling::UnifiedPath &FileName) {
rewriteFileName(FileName, FileName);
}

void rewriteFileName(clang::tooling::UnifiedPath &FileName,
const clang::tooling::UnifiedPath &FullPathName) {
std::string FilePath = FileName.getPath().str();
rewriteFileName(FilePath, FullPathName.getPath().str());
FileName = FilePath;
}

void rewriteFileName(std::string &FileName, const std::string &FullPathName) {
static void
rewriteFileName(std::string &FileName, const std::string &FullPathName,
std::function<bool(tooling::UnifiedPath)> HasCUDASyntax =
DpctGlobalInfo::hasCUDASyntax) {
SmallString<512> CanonicalPathStr(FullPathName);
const auto Extension = path::extension(CanonicalPathStr);
SourceProcessType FileType = GetSourceFileType(FullPathName);
Expand All @@ -210,8 +204,7 @@ void rewriteFileName(std::string &FileName, const std::string &FullPathName) {
if (FileType & SPT_CudaSource) {
path::replace_extension(CanonicalPathStr,
DpctGlobalInfo::getSYCLSourceExtension());
} else if ((FileType & SPT_CppSource) &&
DpctGlobalInfo::hasCUDASyntax(FileName)) {
} else if ((FileType & SPT_CppSource) && HasCUDASyntax(FileName)) {
path::replace_extension(CanonicalPathStr,
Extension +
DpctGlobalInfo::getSYCLSourceExtension());
Expand All @@ -224,6 +217,17 @@ void rewriteFileName(std::string &FileName, const std::string &FullPathName) {
FileName = CanonicalPathStr.c_str();
}

void rewriteFileName(clang::tooling::UnifiedPath &FileName,
const clang::tooling::UnifiedPath &FullPathName) {
std::string FilePath = FileName.getPath().str();
rewriteFileName(FilePath, FullPathName.getPath().str());
FileName = FilePath;
}

void rewriteFileName(clang::tooling::UnifiedPath &FileName) {
rewriteFileName(FileName, FileName);
}

static std::vector<std::string> FilesNotInCompilationDB;

std::map<std::string, std::string> OutFilePath2InFilePath;
Expand Down Expand Up @@ -506,7 +510,7 @@ static void getReplsFromTUR(const std::string &FilePath,
}

int writeReplacementsToFiles(
ReplTy &Replset, Rewriter &Rewrite, const std::string &Folder,
ReplTy &Replset2, Rewriter &Rewrite, const std::string &Folder,
clang::tooling::UnifiedPath &InRoot,
std::unordered_map<std::string, bool> &MainSrcFileMap,
std::vector<clang::tooling::Replacement> &AllFilesRepls,
Expand All @@ -520,6 +524,48 @@ int writeReplacementsToFiles(
volatile ProcessStatus status = MigrationSucceeded;
clang::tooling::UnifiedPath OutPath;

ReplTy Replset;
if (ReMigrationReady) {
std::vector<clang::tooling::Replacement> Repl_B;
std::vector<clang::tooling::Replacement> Repl_C1;
for (const auto &Entry : Replset2) {
for (const auto &Repl : Entry.second) {
Repl_B.push_back(Repl);
}
}
for (const auto &Repl : clang::dpct::getLastMigration()->Replacements) {
Repl_C1.push_back(Repl);
}

std::map<std::string /*SYCL name*/, std::string /*CUDA name*/> FileNameMap;
auto hasCUDASyntax = [](tooling::UnifiedPath Path) -> bool {
if (MainSrcFilesHasCudaSyntex.find(Path.getCanonicalPath().str()) !=
MainSrcFilesHasCudaSyntex.end())
return true;
return false;
};
for (const auto &Entry : Repl_C1) {
std::string CUDAFilePath = Entry.getFilePath().str();
std::string SYCLFilePath;
rewriteFileName(SYCLFilePath, CUDAFilePath, hasCUDASyntax);
FileNameMap[SYCLFilePath] = CUDAFilePath;
}
std::map<std::string, std::vector<clang::tooling::Replacement>> Result =
clang::dpct::reMigrationMerge(clang::dpct::getUpstreamChanges(), Repl_B,
Repl_C1, clang::dpct::getUserChanges(),
FileNameMap);
for (const auto &Entry : Result) {
clang::tooling::Replacements Repls;
for (const auto &Repl : Entry.second) {
llvm::cantFail(Repls.add(Repl));
}
Replset.insert(std::make_pair(Entry.first, Repls));
}
} else {
// If not re-migration, just use the original Replset.
Replset = Replset2;
}

for (auto &Entry : Replset) {
OutPath = StringRef(DpctGlobalInfo::removeSymlinks(
Rewrite.getSourceMgr().getFileManager(), Entry.first));
Expand Down Expand Up @@ -633,6 +679,7 @@ int writeReplacementsToFiles(
// We have written a migrated file; Update the output file path info
OutFilePath2InFilePath[OutPath.getCanonicalPath().str()] = Entry.first;
}
Replset2 = Replset;
return status;
}

Expand Down
3 changes: 0 additions & 3 deletions clang/lib/DPCT/FileGenerator/GenFiles.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,6 @@ void rewriteFileName(clang::tooling::UnifiedPath &FileName);
void rewriteFileName(clang::tooling::UnifiedPath &FileName,
const clang::tooling::UnifiedPath &FullPathName);

// Replace file name \p FileName with new migrated name.
void rewriteFileName(std::string &FileName, const std::string &FullPathName);

// A mapping from output file path to it's corresponding input file.
extern std::map<std::string, std::string> OutFilePath2InFilePath;

Expand Down
43 changes: 32 additions & 11 deletions clang/lib/DPCT/IncMigration/ExternalReplacement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@
// -Load replacement from external (disk file)
// -Merge replacement in current migration with previous migration.

#include "ExternalReplacement.h"
#include "AnalysisInfo.h"
#include "IncMigration/IncrementalMigrationUtility.h"
#include "Utility.h"

#include "clang/Tooling/Core/Diagnostic.h"
#include "clang/Tooling/Core/Replacement.h"
#include "clang/Tooling/Refactoring.h"
#include "clang/Tooling/ReplacementsYaml.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"

#include "ExternalReplacement.h"
#include "IncMigration/IncrementalMigrationUtility.h"
#include "clang/Tooling/Core/Diagnostic.h"
#include "clang/Tooling/Refactoring.h"
#include "clang/Tooling/ReplacementsYaml.h"
#include "llvm/Support/YAMLTraits.h"
#include "llvm/Support/raw_os_ostream.h"

Expand Down Expand Up @@ -77,18 +77,29 @@ int save2Yaml(
return 0;
}

int loadFromYaml(const clang::tooling::UnifiedPath &Input,
clang::tooling::TranslationUnitReplacements &TU) {
template <class T>
static int loadFromYaml(const clang::tooling::UnifiedPath &Input, T &Content) {
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Buffer =
llvm::MemoryBuffer::getFile(Input.getCanonicalPath());
if (!Buffer) {
llvm::errs() << "error: failed to read " << Input.getCanonicalPath() << ": "
<< Buffer.getError().message() << "\n";
return -1;
}

llvm::yaml::Input YAMLIn(Buffer.get()->getBuffer());
YAMLIn >> TU;
YAMLIn >> Content;
if (YAMLIn.error()) {
Content = T();
return -1;
}
return 0;
}

int loadTUFromYaml(const clang::tooling::UnifiedPath &Input,
clang::tooling::TranslationUnitReplacements &TU) {
int Status = loadFromYaml(Input, TU);
if (Status)
return Status;

bool IsSrcFileChanged = false;
for (const auto &digest : TU.MainSourceFilesDigest) {
Expand All @@ -100,7 +111,7 @@ int loadFromYaml(const clang::tooling::UnifiedPath &Input,
}
}

if (IsSrcFileChanged || YAMLIn.error()) {
if (IsSrcFileChanged && !DpctGlobalInfo::useReMigration()) {
// File doesn't appear to be a header change description. Ignore it.
TU = clang::tooling::TranslationUnitReplacements();
return -1;
Expand All @@ -109,6 +120,16 @@ int loadFromYaml(const clang::tooling::UnifiedPath &Input,
return 0;
}

void loadGDCFromYaml(const clang::tooling::UnifiedPath &Input,
clang::dpct::GitDiffChanges &GDC) {
int status = loadFromYaml(Input, GDC);
if (status) {
llvm::errs() << "Failed to load git diff Changes from "
<< Input.getCanonicalPath() << "\n";
GDC = clang::dpct::GitDiffChanges();
}
}

void mergeAndUniqueReps(
Replacements &Replaces,
const std::vector<clang::tooling::Replacement> &PreRepls) {
Expand Down
11 changes: 7 additions & 4 deletions clang/lib/DPCT/IncMigration/ExternalReplacement.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
#ifndef __EXTERNAL_REPLACEMENT_H__
#define __EXTERNAL_REPLACEMENT_H__

#include "clang/Tooling/Core/Replacement.h"
#include "llvm/ADT/StringRef.h"
#include "IncMigration/ReMigration.h"

#include <map>
#include <vector>

Expand All @@ -27,8 +27,10 @@ class Replacements;

namespace clang {
namespace dpct {
int loadFromYaml(const clang::tooling::UnifiedPath &Input,
clang::tooling::TranslationUnitReplacements &TU);
int loadTUFromYaml(const clang::tooling::UnifiedPath &Input,
clang::tooling::TranslationUnitReplacements &TU);
void loadGDCFromYaml(const clang::tooling::UnifiedPath &Input,
clang::dpct::GitDiffChanges &GDC);
int save2Yaml(
const std::vector<clang::tooling::Replacement> &Replaces,
const std::map<clang::tooling::UnifiedPath,
Expand All @@ -39,6 +41,7 @@ void mergeAndUniqueReps(
clang::tooling::Replacements &Replaces,
const std::vector<clang::tooling::Replacement> &PreRepls);

bool tryLoadingUpstreamChangesAndUserChanges();
} // namespace dpct
} // namespace clang

Expand Down
Loading
Loading