Skip to content

[SYCLomatic] Migration of cudaGraphKernelNodeParams, cudaGraphExecUpdateResult #2816

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 6 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
1 change: 1 addition & 0 deletions clang/lib/DPCT/ASTTraversal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ REGISTER_RULE(TypeRemoveRule, PassKind::PK_Analysis)
REGISTER_RULE(CompatWithClangRule, PassKind::PK_Migration)
REGISTER_RULE(AssertRule, PassKind::PK_Migration)
REGISTER_RULE(GraphRule, PassKind::PK_Migration)
REGISTER_RULE(GraphAnalysisRule, PassKind::PK_Analysis)
REGISTER_RULE(GraphicsInteropRule, PassKind::PK_Migration)
REGISTER_RULE(RulesLangAddrSpaceConvRule, PassKind::PK_Migration)

Expand Down
2 changes: 1 addition & 1 deletion clang/lib/DPCT/AnalysisInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2513,7 +2513,7 @@ unsigned DpctGlobalInfo::ExperimentalFlag = 0;
unsigned DpctGlobalInfo::HelperFuncPreferenceFlag = 0;
bool DpctGlobalInfo::AnalysisModeFlag = false;
bool DpctGlobalInfo::UseSYCLCompatFlag = false;
bool DpctGlobalInfo::CVersionCUDALaunchUsedFlag = false;
bool DpctGlobalInfo::UseWrapperRegisterFnPtrFlag = false;
unsigned int DpctGlobalInfo::ColorOption = 1;
std::unordered_map<int, std::shared_ptr<DeviceFunctionInfo>>
DpctGlobalInfo::CubPlaceholderIndexMap;
Expand Down
8 changes: 5 additions & 3 deletions clang/lib/DPCT/AnalysisInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -1358,8 +1358,10 @@ class DpctGlobalInfo {
static bool useNoQueueDevice() {
return getHelperFuncPreference(HelperFuncPreference::NoQueueDevice);
}
static void setCVersionCUDALaunchUsed() { CVersionCUDALaunchUsedFlag = true; }
static bool isCVersionCUDALaunchUsed() { return CVersionCUDALaunchUsedFlag; }
static void setUseWrapperRegisterFnPtr() {
UseWrapperRegisterFnPtrFlag = true;
}
static bool useWrapperRegisterFnPtr() { return UseWrapperRegisterFnPtrFlag; }
static void setUseSYCLCompat(bool Flag = true) { UseSYCLCompatFlag = Flag; }
static bool useSYCLCompat() { return UseSYCLCompatFlag; }
static bool useEnqueueBarrier() {
Expand Down Expand Up @@ -1689,7 +1691,7 @@ class DpctGlobalInfo {
static unsigned HelperFuncPreferenceFlag;
static bool AnalysisModeFlag;
static bool UseSYCLCompatFlag;
static bool CVersionCUDALaunchUsedFlag;
static bool UseWrapperRegisterFnPtrFlag;
static unsigned int ColorOption;
static std::unordered_map<int, std::shared_ptr<DeviceFunctionInfo>>
CubPlaceholderIndexMap;
Expand Down
16 changes: 16 additions & 0 deletions clang/lib/DPCT/RuleInfra/APINamesTemplateType.inc
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,22 @@ TYPE_REWRITE_ENTRY(
WARNING_FACTORY(Diagnostics::TRY_EXPERIMENTAL_FEATURE, TYPESTR,
STR("--use-experimental-features=graph"))))

TYPE_REWRITE_ENTRY(
"cudaGraphExecUpdateResultInfo",
TYPE_CONDITIONAL_FACTORY(
checkEnableGraphForType(), TYPE_FACTORY(STR("int")),
WARNING_FACTORY(Diagnostics::TRY_EXPERIMENTAL_FEATURE, TYPESTR,
STR("--use-experimental-features=graph"))))

TYPE_REWRITE_ENTRY(
"cudaKernelNodeParams",
TYPE_CONDITIONAL_FACTORY(
checkEnableGraphForType(),
TYPE_FACTORY(STR(MapNames::getDpctNamespace() +
"experimental::kernel_node_params")),
WARNING_FACTORY(Diagnostics::TRY_EXPERIMENTAL_FEATURE, TYPESTR,
STR("--use-experimental-features=graph"))))

// Graphics Interop Handle
TYPE_REWRITE_ENTRY(
"cudaGraphicsResource",
Expand Down
45 changes: 45 additions & 0 deletions clang/lib/DPCT/RuleInfra/MapNames.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,10 @@ void MapNames::setExplicitNamespaceMap(
DpctGlobalInfo::useExtGraph()
? getClNamespace() + "ext::oneapi::experimental::node_type"
: "cudaGraphNodeType")},
{"cudaGraphExecUpdateResult",
std::make_shared<TypeNameRule>(DpctGlobalInfo::useExtGraph()
? "int"
: "cudaGraphExecUpdateResult")},
{"CUmem_advise", std::make_shared<TypeNameRule>("int")},
{"CUmemorytype",
std::make_shared<TypeNameRule>(getClNamespace() + "usm::alloc")},
Expand Down Expand Up @@ -1154,6 +1158,47 @@ void MapNames::setExplicitNamespaceMap(
? getClNamespace() +
"ext::oneapi::experimental::node_type::empty"
: "cudaGraphNodeTypeEmpty")},
{"cudaGraphExecUpdateSuccess",
std::make_shared<EnumNameRule>(
DpctGlobalInfo::useExtGraph() ? "1" : "cudaGraphExecUpdateSuccess")},
{"cudaGraphExecUpdateError",
std::make_shared<EnumNameRule>(
DpctGlobalInfo::useExtGraph() ? "0" : "cudaGraphExecUpdateError")},
{"cudaGraphExecUpdateErrorTopologyChanged",
std::make_shared<EnumNameRule>(
DpctGlobalInfo::useExtGraph()
? "0"
: "cudaGraphExecUpdateErrorTopologyChanged")},
{"cudaGraphExecUpdateErrorNodeTypeChanged",
std::make_shared<EnumNameRule>(
DpctGlobalInfo::useExtGraph()
? "0"
: "cudaGraphExecUpdateErrorNodeTypeChanged")},
{"cudaGraphExecUpdateErrorFunctionChanged",
std::make_shared<EnumNameRule>(
DpctGlobalInfo::useExtGraph()
? "0"
: "cudaGraphExecUpdateErrorFunctionChanged")},
{"cudaGraphExecUpdateErrorParametersChanged",
std::make_shared<EnumNameRule>(
DpctGlobalInfo::useExtGraph()
? "0"
: "cudaGraphExecUpdateErrorParametersChanged")},
{"cudaGraphExecUpdateErrorNotSupported",
std::make_shared<EnumNameRule>(
DpctGlobalInfo::useExtGraph()
? "0"
: "cudaGraphExecUpdateErrorNotSupported")},
{"cudaGraphExecUpdateErrorUnsupportedFunctionChange",
std::make_shared<EnumNameRule>(
DpctGlobalInfo::useExtGraph()
? "0"
: "cudaGraphExecUpdateErrorUnsupportedFunctionChange")},
{"cudaGraphExecUpdateErrorAttributesChanged",
std::make_shared<EnumNameRule>(
DpctGlobalInfo::useExtGraph()
? "0"
: "cudaGraphExecUpdateErrorAttributesChanged")},
// enum CUmem_advise_enum
{"CU_MEM_ADVISE_SET_READ_MOSTLY", std::make_shared<EnumNameRule>("0")},
{"CU_MEM_ADVISE_UNSET_READ_MOSTLY", std::make_shared<EnumNameRule>("0")},
Expand Down
6 changes: 4 additions & 2 deletions clang/lib/DPCT/RulesLang/APINamesGraph.inc
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@ ASSIGNABLE_FACTORY(CONDITIONAL_FACTORY_ENTRY(

ASSIGNABLE_FACTORY(CONDITIONAL_FACTORY_ENTRY(
UseExtGraph,
MEMBER_CALL_FACTORY_ENTRY("cudaGraphExecUpdate", ARG(0), true, "update",
DEREF(1)),
CALL_FACTORY_ENTRY("cudaGraphExecUpdate",
CALL(MapNames::getDpctNamespace() +
"experimental::update",
ARG(0), ARG(1), ARG(2))),
UNSUPPORT_FACTORY_ENTRY("cudaGraphExecUpdate",
Diagnostics::TRY_EXPERIMENTAL_FEATURE,
ARG("cudaGraphExecUpdate"),
Expand Down
10 changes: 9 additions & 1 deletion clang/lib/DPCT/RulesLang/MapNamesLang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,5 +362,13 @@ const std::unordered_map<std::string, HelperFeatureEnum>
{"sampler", HelperFeatureEnum::device_ext},
};

// Graph kernel node params mapping
MapNamesLang::MapTy GraphRule::KernelNodeParamNames{
{"gridDim", "grid_dim"},
{"blockDim", "block_dim"},
{"kernelParams", "kernel_params"},
{"sharedMemBytes", "shared_mem_bytes"},
{"func", "func"}};

} // namespace dpct
} // namespace clang
} // namespace clang
77 changes: 67 additions & 10 deletions clang/lib/DPCT/RulesLang/RulesLang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,8 @@ void TypeInDeclRule::registerMatcher(MatchFinder &MF) {
"cudaExternalMemoryBufferDesc", "cudaExternalSemaphore_t",
"cudaExternalSemaphoreHandleDesc",
"cudaExternalSemaphoreSignalParams",
"cudaExternalSemaphoreWaitParams"))))))
"cudaExternalSemaphoreWaitParams", "cudaKernelNodeParams",
"cudaGraphExecUpdateResultInfo"))))))
.bind("cudaTypeDefEA"),
this);
MF.addMatcher(varDecl(hasType(classTemplateSpecializationDecl(
Expand Down Expand Up @@ -937,9 +938,11 @@ void TypeInDeclRule::runRule(const MatchFinder::MatchResult &Result) {
}

if (CanonicalTypeStr == "cudaGraphExecUpdateResult") {
report(TL->getBeginLoc(), Diagnostics::API_NOT_MIGRATED, false,
CanonicalTypeStr);
return;
if (!DpctGlobalInfo::useExtGraph()) {
report(TL->getBeginLoc(), Diagnostics::TRY_EXPERIMENTAL_FEATURE, false,
"cudaGraphExecUpdateResult",
"--use-experimental-features=graph");
}
}

if (CanonicalTypeStr == "cudaGraphicsRegisterFlags" ||
Expand Down Expand Up @@ -1941,7 +1944,8 @@ void EnumConstantRule::registerMatcher(MatchFinder &MF) {
"cufftType", "cudaMemoryType", "CUctx_flags_enum",
"CUpointer_attribute_enum", "CUmemorytype_enum",
"cudaGraphicsMapFlags", "cudaGraphicsRegisterFlags",
"cudaGraphNodeType", "CUdevice_P2PAttribute_enum"))),
"cudaGraphNodeType", "CUdevice_P2PAttribute_enum",
"cudaGraphExecUpdateResult"))),
matchesName("CUDNN_.*"), matchesName("CUSOLVER_.*")))))
.bind("EnumConstant"),
this);
Expand Down Expand Up @@ -2061,7 +2065,16 @@ void EnumConstantRule::runRule(const MatchFinder::MatchResult &Result) {
EnumName == "cudaGraphNodeTypeMemset" ||
EnumName == "cudaGraphNodeTypeHost" ||
EnumName == "cudaGraphNodeTypeGraph" ||
EnumName == "cudaGraphNodeTypeEmpty")) {
EnumName == "cudaGraphNodeTypeEmpty" ||
EnumName == "cudaGraphExecUpdateSuccess" ||
EnumName == "cudaGraphExecUpdateError" ||
EnumName == "cudaGraphExecUpdateErrorTopologyChanged" ||
EnumName == "cudaGraphExecUpdateErrorNodeTypeChanged" ||
EnumName == "cudaGraphExecUpdateErrorFunctionChanged" ||
EnumName == "cudaGraphExecUpdateErrorParametersChanged" ||
EnumName == "cudaGraphExecUpdateErrorNotSupported" ||
EnumName == "cudaGraphExecUpdateErrorUnsupportedFunctionChange" ||
EnumName == "cudaGraphExecUpdateErrorAttributesChanged")) {
report(E->getBeginLoc(), Diagnostics::TRY_EXPERIMENTAL_FEATURE, false,
EnumName, "--use-experimental-features=graph");
return;
Expand Down Expand Up @@ -2724,6 +2737,50 @@ const VarDecl *getAssignTargetDecl(const Stmt *E) {
return nullptr;
}

const Expr *getParentAsAssignedBO(const Expr *E, ASTContext &Context,
MigrationRule *Rule) {
auto Parents = Context.getParents(*E);
if (Parents.size() > 0)
return getAssignedBO(Parents[0].get<Expr>(), Context, Rule);
return nullptr;
}

// Return the binary operator if E is the lhs of an assign expression,
// otherwise nullptr.
const Expr *getAssignedBO(const Expr *E, ASTContext &Context,
MigrationRule *Rule) {
if (dyn_cast<MemberExpr>(E)) {
// Continue finding parents when E is MemberExpr.
return getParentAsAssignedBO(E, Context, Rule);
} else if (auto ICE = dyn_cast<ImplicitCastExpr>(E)) {
// Stop finding parents and return nullptr when E is ImplicitCastExpr,
// except for ArrayToPointerDecay cast.
if (ICE->getCastKind() == CK_ArrayToPointerDecay) {
return getParentAsAssignedBO(E, Context, Rule);
}
} else if (auto ASE = dyn_cast<ArraySubscriptExpr>(E)) {
// Continue finding parents when E is ArraySubscriptExpr, and remove
// subscript operator anyway for texture object's member.
Rule->emplaceTransformation(new ReplaceToken(
Lexer::getLocForEndOfToken(ASE->getLHS()->getEndLoc(), 0,
Context.getSourceManager(),
Context.getLangOpts()),
ASE->getRBracketLoc(), ""));
return getParentAsAssignedBO(E, Context, Rule);
} else if (auto BO = dyn_cast<BinaryOperator>(E)) {
// If E is BinaryOperator, return E only when it is assign expression,
// otherwise return nullptr.
auto Opcode = BO->getOpcode();
if (Opcode == BO_Assign || Opcode == BO_OrAssign)
return BO;
} else if (auto COCE = dyn_cast<CXXOperatorCallExpr>(E)) {
if (COCE->getOperator() == OO_Equal) {
return COCE;
}
}
return nullptr;
}

const VarDecl *EventQueryTraversal::getAssignTarget(const CallExpr *Call) {
auto ParentMap = Context.getParents(*Call);
if (ParentMap.size() == 0)
Expand Down Expand Up @@ -4638,7 +4695,7 @@ void KernelCallRefRule::runRule(
(OuterFD->getTemplatedKind() ==
FunctionDecl::TemplatedKind::TK_FunctionTemplate)) {
std::string TypeRepl;
if (DpctGlobalInfo::isCVersionCUDALaunchUsed()) {
if (DpctGlobalInfo::useWrapperRegisterFnPtr()) {
if ((IsTemplateRelated &&
(!DRE->hasExplicitTemplateArgs() ||
(DRE->getNumTemplateArgs() <= TemplateParamNum))) ||
Expand All @@ -4647,7 +4704,7 @@ void KernelCallRefRule::runRule(
}
}
insertWrapperPostfix<DeclRefExpr>(
DRE, std::move(TypeRepl), DpctGlobalInfo::isCVersionCUDALaunchUsed());
DRE, std::move(TypeRepl), DpctGlobalInfo::useWrapperRegisterFnPtr());
}
}
if (auto ULE =
Expand Down Expand Up @@ -4684,7 +4741,7 @@ void KernelCallRefRule::runRule(
}
}
insertWrapperPostfix<UnresolvedLookupExpr>(
ULE, getTypeRepl(ULE), DpctGlobalInfo::isCVersionCUDALaunchUsed());
ULE, getTypeRepl(ULE), DpctGlobalInfo::useWrapperRegisterFnPtr());
}
}

Expand Down Expand Up @@ -4957,7 +5014,7 @@ void KernelCallRule::runRule(

if (!getAddressedRef(CalleeDRE)) {
if (IsFuncTypeErased) {
DpctGlobalInfo::setCVersionCUDALaunchUsed();
DpctGlobalInfo::setUseWrapperRegisterFnPtr();
}
std::string ReplStr;
llvm::raw_string_ostream OS(ReplStr);
Expand Down
18 changes: 13 additions & 5 deletions clang/lib/DPCT/RulesLang/RulesLang.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ TextModification *ReplaceMemberAssignAsSetMethod(const Expr *E,
StringRef ExtraArg = "",
StringRef ExtraFeild = "");

const Expr *getAssignedBO(const Expr *E, ASTContext &Context,
MigrationRule *Rule);
const Expr *getParentAsAssignedBO(const Expr *E, ASTContext &Context,
MigrationRule *Rule);

/// Migration rule for iteration space built-in variables (threadIdx, etc).
class IterationSpaceBuiltinRule
: public NamedMigrationRule<IterationSpaceBuiltinRule> {
Expand Down Expand Up @@ -852,9 +857,6 @@ class TextureMemberSetRule : public NamedMigrationRule<TextureMemberSetRule> {

/// Texture migration rule
class TextureRule : public NamedMigrationRule<TextureRule> {
// Get the binary operator if E is lhs of an assign expression.
const Expr *getAssignedBO(const Expr *E, ASTContext &Context);
const Expr *getParentAsAssignedBO(const Expr *E, ASTContext &Context);
bool removeExtraMemberAccess(const MemberExpr *ME);
void replaceTextureMember(const MemberExpr *ME, ASTContext &Context,
SourceManager &SM);
Expand Down Expand Up @@ -998,7 +1000,15 @@ class CompatWithClangRule : public NamedMigrationRule<CompatWithClangRule> {
void runRule(const ast_matchers::MatchFinder::MatchResult &Result);
};

class GraphAnalysisRule : public NamedMigrationRule<GraphAnalysisRule> {
public:
void registerMatcher(ast_matchers::MatchFinder &MF) override;
void runRule(const ast_matchers::MatchFinder::MatchResult &Result);
};

class GraphRule : public NamedMigrationRule<GraphRule> {
static MapNames::MapTy KernelNodeParamNames;

public:
void registerMatcher(ast_matchers::MatchFinder &MF) override;
void runRule(const ast_matchers::MatchFinder::MatchResult &Result);
Expand All @@ -1013,8 +1023,6 @@ class AssertRule : public NamedMigrationRule<AssertRule> {
class GraphicsInteropRule : public NamedMigrationRule<GraphicsInteropRule> {
static MapNames::MapTy ExtResMemHandleDescNames, ExtResSemParamsNames;

const Expr *getAssignedBO(const Expr *E, ASTContext &Context);
const Expr *getParentAsAssignedBO(const Expr *E, ASTContext &Context);
void replaceExtResMemHandleDataExpr(const MemberExpr *ME,
ASTContext &Context);
void replaceExtResSemParamsDataExpr(const MemberExpr *ME,
Expand Down
Loading