Skip to content

[SYCLomatic] Emit the verified component version. #2855

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 11 commits into
base: SYCLomatic
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 8 additions & 0 deletions clang/include/clang/DPCT/DPCTOptions.inc
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,14 @@ DPCT_FLAG_OPTION(
"<dir>_codepin_sycl, where <dir> is specified by --out-root option."),
llvm::cl::cat(CtHelpCatAll))

DPCT_FLAG_OPTION(
CompatibleComps, clang::dpct::DpctOptionClass::OC_Attribute,
DPCT_OPTION_ACTIONS(clang::dpct::DpctActionKind::DAK_Migration),
"supported-components",
llvm::cl::desc("Show the supported components and their compatibility "
"version information.\n"),
llvm::cl::cat(CtHelpCatAll))

DPCT_FLAG_OPTION(
EnableCTAD, clang::dpct::DpctOptionClass::OC_Attribute,
DPCT_OPTION_ACTIONS(clang::dpct::DpctActionKind::DAK_Migration,
Expand Down
2 changes: 2 additions & 0 deletions clang/lib/DPCT/AnalysisInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2410,6 +2410,7 @@ std::shared_ptr<clang::tooling::TranslationUnitReplacements>
clang::tooling::UnifiedPath DpctGlobalInfo::InRoot;
clang::tooling::UnifiedPath DpctGlobalInfo::OutRoot;
std::vector<clang::tooling::UnifiedPath> DpctGlobalInfo::AnalysisScope;
std::vector<std::shared_ptr<CompStatus>> DpctGlobalInfo::CompatibleCompsStatus;
std::unordered_set<std::string> DpctGlobalInfo::ChangeExtensions = {};
std::string DpctGlobalInfo::SYCLSourceExtension = std::string();
std::string DpctGlobalInfo::SYCLHeaderExtension = std::string();
Expand All @@ -2428,6 +2429,7 @@ format::FormatRange DpctGlobalInfo::FmtRng = format::FormatRange::none;
DPCTFormatStyle DpctGlobalInfo::FmtST = DPCTFormatStyle::FS_LLVM;
bool DpctGlobalInfo::EnableCtad = false;
bool DpctGlobalInfo::EnableCodePin = false;
bool DpctGlobalInfo::CompatibleComps = false;
bool DpctGlobalInfo::IsMLKHeaderUsed = false;
bool DpctGlobalInfo::GenBuildScript = false;
bool DpctGlobalInfo::MigrateBuildScriptOnly = false;
Expand Down
21 changes: 21 additions & 0 deletions clang/lib/DPCT/AnalysisInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "TextModification.h"
#include "Utility.h"
#include "CommandOption/ValidateArguments.h"
#include "ComponentVersion/ComponentVersion.h"
#include <bitset>
#include <memory>
#include <optional>
Expand Down Expand Up @@ -736,6 +737,24 @@ class DpctGlobalInfo {
static const std::vector<clang::tooling::UnifiedPath> &getAnalysisScope() {
return AnalysisScope;
}
static void
setCompatibleCompsStatus(const std::vector<std::shared_ptr<CompStatus>> &CompStatus) {
CompatibleCompsStatus = CompStatus;
}
static const std::vector<std::shared_ptr<CompStatus>> &getCompatibleCompsStatus() {
return CompatibleCompsStatus;
}
static const std::unordered_map<ComponentType, ComponentInfo> &
getSupportedComponentInfo() {
static std::unordered_map<ComponentType, ComponentInfo> Components = {
{ComponentType::DPCPP, ComponentInfo("DPCPP")},
{ComponentType::oneDPL, ComponentInfo("oneDPL")},
{ComponentType::oneMKL, ComponentInfo("oneMKL")},
{ComponentType::oneDNNL, ComponentInfo("oneDNNL")},
{ComponentType::oneCCL, ComponentInfo("oneCCL")},
{ComponentType::ISHMEM, ComponentInfo("ISHMEM")}};
return Components;
}
static void addChangeExtensions(const std::string &Extension) {
assert(!Extension.empty());
ChangeExtensions.insert(Extension);
Expand Down Expand Up @@ -1576,6 +1595,7 @@ class DpctGlobalInfo {
static clang::tooling::UnifiedPath InRoot;
static clang::tooling::UnifiedPath OutRoot;
static std::vector<clang::tooling::UnifiedPath> AnalysisScope;
static std::vector<std::shared_ptr<CompStatus>> CompatibleCompsStatus;
static std::unordered_set<std::string> ChangeExtensions;
static std::string SYCLSourceExtension;
static std::string SYCLHeaderExtension;
Expand All @@ -1594,6 +1614,7 @@ class DpctGlobalInfo {
static DPCTFormatStyle FmtST;
static bool EnableCtad;
static bool EnableCodePin;
static bool CompatibleComps;
static bool IsMLKHeaderUsed;
static bool GenBuildScript;
static bool MigrateBuildScriptOnly;
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
ComponentVersion/ComponentVersion.cpp
UserDefinedRules/UserDefinedRules.cpp
UserDefinedRules/PatternRewriter.cpp
MigrateScript/MigrateBuildScript.cpp
Expand Down
163 changes: 163 additions & 0 deletions clang/lib/DPCT/ComponentVersion/ComponentVersion.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
//===--------------- ComponentVersion.cpp --------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "ComponentVersion.h"
#include "AnalysisInfo.h"
#include <iostream>
#include <sstream>

using namespace llvm;

template <>
struct llvm::yaml::SequenceTraits<std::vector<clang::dpct::CompStatus>> {
static size_t size(IO &io, std::vector<clang::dpct::CompStatus> &seq) {
return seq.size();
}

static clang::dpct::CompStatus &
element(IO &io, std::vector<clang::dpct::CompStatus> &seq, size_t index) {
if (index >= seq.size())
seq.resize(index + 1);
return seq[index];
}
};

template <>
struct llvm::yaml::MappingTraits<std::shared_ptr<clang::dpct::CompStatus>> {
static void mapping(IO &io,
std::shared_ptr<clang::dpct::CompStatus> &Status) {
Status = std::make_shared<clang::dpct::CompStatus>();
io.mapRequired("Feature", Status->Feature);
io.mapRequired("SupportedVersion", Status->SupportedVersion);
io.mapRequired("ComponentType", Status->CompType);
io.mapOptional("Link", Status->Link);
io.mapRequired("ReplacementText", Status->ReplacementText);
io.mapOptional("IsOpenSource", Status->IsOpenSource);
io.mapRequired("IsInNextOneAPIVersion", Status->IsInNextOneAPIVersion);
io.mapOptional("Description", Status->Description);
}
};
template <> struct llvm::yaml::ScalarEnumerationTraits<ComponentType> {
static void enumeration(llvm::yaml::IO &Io, ComponentType &Value) {
Io.enumCase(Value, "Compiler", ComponentType::DPCPP);
Io.enumCase(Value, "DPC++", ComponentType::DPCPP);
Io.enumCase(Value, "DPCPP", ComponentType::DPCPP);
Io.enumCase(Value, "oneDPL", ComponentType::oneDPL);
Io.enumCase(Value, "oneMKL", ComponentType::oneMKL);
Io.enumCase(Value, "oneCCL", ComponentType::oneCCL);
Io.enumCase(Value, "oneDNNL", ComponentType::oneDNNL);
Io.enumCase(Value, "ISHMEM", ComponentType::ISHMEM);
}
};

namespace clang {
namespace dpct {

void displayOverallComponentInfo(
const std::unordered_map<ComponentType, ComponentInfo>
&SupportedComponentInfo) {
std::stringstream ComponentOverallLog;
ComponentOverallLog << "Compatible components:\n";
for (const auto &Component : SupportedComponentInfo) {
ComponentOverallLog << " - " << Component.second.ComponentName << ": "
<< Component.second.ComponentVersion << ".\n";
}
std::cout << ComponentOverallLog.str();
}

void displayComponentDetailsInfo(
const std::unordered_map<ComponentType, ComponentInfo>
&SupportedComponentInfo) {
std::stringstream ComponentDetailLog;
ComponentDetailLog << "\nCompatible Component details:\n";
for (const auto &Component : SupportedComponentInfo) {
for (const auto &Des : Component.second.ComponentDes) {
ComponentDetailLog << " - " << Des << "\n";
}
}
std::cout << ComponentDetailLog.str();
}

void displaySupportedComponents(bool isPrintOverall) {
auto CompsStatus = DpctGlobalInfo::getCompatibleCompsStatus();
std::unordered_map<ComponentType, ComponentInfo> Components =
dpct::DpctGlobalInfo::getSupportedComponentInfo();
if (isPrintOverall) {
for (auto &CompStatus : CompsStatus) {
updateComInfoByCompStatus(Components[CompStatus->CompType],
CompStatus);
}
displayOverallComponentInfo(Components);
}
displayComponentDetailsInfo(Components);
}
std::string
generateDescription(const ComponentInfo &Info,
const std::shared_ptr<clang::dpct::CompStatus> &Status) {
std::ostringstream oss;
oss << "The feature " << Status->Feature;
if (Status->IsOpenSource) {
oss << " is supported in " << Info.ComponentName << " open source "
<< Status->SupportedVersion << ". ";
} else if (Status->IsInNextOneAPIVersion) {
oss << " is supported in the next oneAPI version. ";
}
if (!Status->Link.empty()) {
oss << "For more details, please refer to the provided link: "
<< Status->Link << ". \n";
}
return oss.str();
}

std::string getContentAfterSpace(const std::string &str) {
size_t pos = str.find(' ');
if (pos != std::string::npos) {
return str.substr(pos + 1);
}
return str;
}
void updateComInfoByCompStatus(
ComponentInfo &Info,
const std::shared_ptr<clang::dpct::CompStatus> &Status) {
std::string Description = generateDescription(Info, Status);
Info.ComponentDes.push_back(Description);
if (Status->IsOpenSource && !Info.ComponentVersion.empty() &&
!Status->SupportedVersion.empty()) {
try {
float OrgDate = std::stof(getContentAfterSpace(Info.ComponentVersion));
float NewDate = std::stof(getContentAfterSpace(Status->SupportedVersion));
if (OrgDate < NewDate ||
Info.ComponentVersion.find("oneAPI") != std::string::npos) {
Info.ComponentVersion = ">= " + Status->SupportedVersion;
}
} catch (...) {
std::cerr << "DPCT Exit: Error in component_version.yaml. The "
"'SupportedVersion' field must be a valid number. Please "
"check and correct it.\n";
dpctExit(MigrationError);
}
}
}

void parseSupportComponentStatus(
std::vector<clang::tooling::UnifiedPath> &RuleFiles,
bool IsPrintComponentOpt) {
auto file = llvm::MemoryBuffer::getFile(RuleFiles[0].getCanonicalPath());
if (!file) {
llvm::errs() << "Error: failed to read " << RuleFiles[0].getCanonicalPath()
<< ": " << file.getError().message() << "\n";
return;
}
std::vector<std::shared_ptr<clang::dpct::CompStatus>> CompsStatus;
yaml::Input yin(file.get()->getBuffer());
yin >> CompsStatus;
DpctGlobalInfo::setCompatibleCompsStatus(CompsStatus);
}

} // namespace dpct
} // namespace clang
58 changes: 58 additions & 0 deletions clang/lib/DPCT/ComponentVersion/ComponentVersion.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//===--------------- ComponentVersion.h
//----------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef DPCT_COMPONENT_VERSION
#define DPCT_COMPONENT_VERSION
#include "clang/Tooling/Tooling.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/YAMLTraits.h"
#include <string>
#include <vector>

namespace clang {
namespace dpct {
enum ComponentType { DPCPP, oneDPL, oneMKL, oneCCL, oneDNNL, ISHMEM };

struct CompStatus {
std::string Feature;
std::string SupportedVersion;
std::string ReplacementText;
ComponentType CompType;
bool IsOpenSource;
bool IsInNextOneAPIVersion;
std::string Link;
std::string Description;
};

const std::string OneAPIVersion = "2025.1";
class ComponentInfo {
public:
ComponentInfo(const std::string &Name,
const std::string &Version = "oneAPI " + OneAPIVersion)
: ComponentName(Name), ComponentVersion(Version) {}
ComponentInfo()
: ComponentName(""), ComponentVersion("oneAPI " + OneAPIVersion) {}
std::string ComponentName;
std::string ComponentVersion;
std::vector<std::string> ComponentDes;
};

void parseSupportComponentStatus(
std::vector<clang::tooling::UnifiedPath> &RuleFiles,
bool IsPrintComponentOpt = false);
void displayComponentDetailsInfo(
const std::unordered_map<ComponentType, ComponentInfo>
&SupportedComponentInfo);
void displaySupportedComponents(bool isPrintOverall = false);
void updateComInfoByCompStatus(ComponentInfo &Info,
const std::shared_ptr<clang::dpct::CompStatus> &Status);
} // namespace dpct
} // namespace clang

#endif // DPCT_COMPONENT_VERSION
18 changes: 18 additions & 0 deletions clang/lib/DPCT/DPCT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "AnalysisInfo.h"
#include "CommandOption/ValidateArguments.h"
#include "Config.h"
#include "ComponentVersion/ComponentVersion.h"
#include "ErrorHandle/CrashRecovery.h"
#include "ErrorHandle/Error.h"
#include "FileGenerator/GenFiles.h"
Expand Down Expand Up @@ -975,7 +976,24 @@ int runDPCT(int argc, const char **argv) {
showReportHeader();

ExtraIncPaths = OptParser->getExtraIncPathList();

{
SmallString<128> FilePath1(DpctInstallPath.getCanonicalPath());
llvm::sys::path::append(FilePath1,
Twine("extensions/supported_components/component_version.yaml"));
SmallString<128> FilePath2(DpctInstallPath.getCanonicalPath());
llvm::sys::path::append(FilePath2,
Twine("opt/dpct/extensions/supported_components/component_version.yaml"));

std::vector<clang::tooling::UnifiedPath> SupportedComponents{
llvm::sys::fs::exists(FilePath1) ? FilePath1.c_str()
: FilePath2.c_str()};
parseSupportComponentStatus(SupportedComponents, CompatibleComps);
}
if (CompatibleComps) {
displaySupportedComponents(CompatibleComps);
return 0;
}
if (isCUDAHeaderRequired()) {
// TODO: implement one of this for each source language.
CudaPath = getCudaInstallPath(OriginalArgc, argv);
Expand Down
17 changes: 17 additions & 0 deletions clang/lib/DPCT/FileGenerator/GenFiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,22 @@ void genCodePinHeader(dpct::RawFDOStream &RS, bool IsForCUDADebug) {
RS << "#endif" << getNL();
}

void GenVerifiedCmpVer(
const std::vector<clang::tooling::Replacement> &MainSrcFilesRepls) {
auto CompStatusList = dpct::DpctGlobalInfo::getCompatibleCompsStatus();
auto CompsInfo = dpct::DpctGlobalInfo::getSupportedComponentInfo();
for (auto CompStatus : CompStatusList) {
for (auto Repl : MainSrcFilesRepls) {
Copy link
Preview

Copilot AI Jun 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This loop copies each Replacement by value. Change to for (const auto &Repl : MainSrcFilesRepls) to avoid unnecessary copies.

Suggested change
for (auto Repl : MainSrcFilesRepls) {
for (const auto &Repl : MainSrcFilesRepls) {

Copilot uses AI. Check for mistakes.

if (Repl.getReplacementText().str().find(CompStatus->ReplacementText) !=
std::string::npos) {
// If the replacement text is already in the list, skip it.
updateComInfoByCompStatus(CompsInfo[CompStatus->CompType], CompStatus);
break;
}
}
}
displayComponentDetailsInfo(CompsInfo);
}
/// Apply all generated replacements, and immediately save the results to files
/// in output directory.
///
Expand Down Expand Up @@ -996,6 +1012,7 @@ int saveNewFiles(clang::tooling::RefactoringTool &Tool,
clang::dpct::RT_CUDAWithCodePin))
return RewriteStatus;
}
GenVerifiedCmpVer(MainSrcFilesRepls);
// Print the in-root path and the number of processed files
size_t ProcessedFileNumber;
if (ProcessAll) {
Expand Down
10 changes: 10 additions & 0 deletions clang/tools/dpct/extensions/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ set(dpct_pytorch_api_rule_files
${CMAKE_SOURCE_DIR}/../clang/tools/dpct/extensions/pytorch_api_rules/pytorch_api.yaml
)

set(dpct_supported_components_files
${CMAKE_SOURCE_DIR}/../clang/tools/dpct/extensions/supported_components/component_version.yaml
)

set(dpct_rule_template_files
${CMAKE_SOURCE_DIR}/../clang/tools/dpct/extensions/rule_templates/api_rule.yaml
${CMAKE_SOURCE_DIR}/../clang/tools/dpct/extensions/rule_templates/class_rule.yaml
Expand Down Expand Up @@ -55,6 +59,12 @@ install(
PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ
DESTINATION ./extensions/pytorch_api_rules)

install(
FILES ${dpct_supported_components_files}
COMPONENT dpct-rules
PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ
DESTINATION ./extensions/supported_components)

install(
FILES ${dpct_rule_template_files}
COMPONENT dpct-rules
Expand Down
Loading
Loading