Skip to content

Commit f333af7

Browse files
Migratiion of cudaGraphSetKernelNodeParams
Signed-off-by: Daiyaan Ahmed <daiyaan.ahmed@intel.com>
1 parent 8513308 commit f333af7

File tree

9 files changed

+201
-23
lines changed

9 files changed

+201
-23
lines changed

clang/lib/DPCT/RuleInfra/APINamesTemplateType.inc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,15 @@ TYPE_REWRITE_ENTRY(
507507
WARNING_FACTORY(Diagnostics::TRY_EXPERIMENTAL_FEATURE, TYPESTR,
508508
STR("--use-experimental-features=graph"))))
509509

510+
TYPE_REWRITE_ENTRY(
511+
"cudaKernelNodeParams",
512+
TYPE_CONDITIONAL_FACTORY(
513+
checkEnableGraphForType(),
514+
TYPE_FACTORY(STR(MapNames::getDpctNamespace() +
515+
"experimental::kernel_node_params")),
516+
WARNING_FACTORY(Diagnostics::TRY_EXPERIMENTAL_FEATURE, TYPESTR,
517+
STR("--use-experimental-features=graph"))))
518+
510519
// Graphics Interop Handle
511520
TYPE_REWRITE_ENTRY(
512521
"cudaGraphicsResource",

clang/lib/DPCT/RulesLang/APINamesGraph.inc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,25 @@ ASSIGNABLE_FACTORY(CONDITIONAL_FACTORY_ENTRY(
104104
Diagnostics::TRY_EXPERIMENTAL_FEATURE,
105105
ARG("cudaGraphDestroy"),
106106
ARG("--use-experimental-features=graph"))))
107+
108+
ASSIGNABLE_FACTORY(CONDITIONAL_FACTORY_ENTRY(
109+
UseExtGraph,
110+
CALL_FACTORY_ENTRY("cudaGraphKernelNodeGetParams",
111+
CALL(MapNames::getDpctNamespace() +
112+
"experimental::get_kernel_node_params",
113+
ARG(0), ARG(1))),
114+
UNSUPPORT_FACTORY_ENTRY("cudaGraphKernelNodeGetParams",
115+
Diagnostics::TRY_EXPERIMENTAL_FEATURE,
116+
ARG("cudaGraphKernelNodeGetParams"),
117+
ARG("--use-experimental-features=graph"))))
118+
119+
ASSIGNABLE_FACTORY(CONDITIONAL_FACTORY_ENTRY(
120+
UseExtGraph,
121+
CALL_FACTORY_ENTRY("cudaGraphKernelNodeSetParams",
122+
CALL(MapNames::getDpctNamespace() +
123+
"experimental::set_kernel_node_params",
124+
ARG(0), ARG(1))),
125+
UNSUPPORT_FACTORY_ENTRY("cudaGraphKernelNodeSetParams",
126+
Diagnostics::TRY_EXPERIMENTAL_FEATURE,
127+
ARG("cudaGraphKernelNodeSetParams"),
128+
ARG("--use-experimental-features=graph"))))

clang/lib/DPCT/RulesLang/MapNamesLang.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,5 +362,13 @@ const std::unordered_map<std::string, HelperFeatureEnum>
362362
{"sampler", HelperFeatureEnum::device_ext},
363363
};
364364

365+
// Graph kernel node params mapping
366+
MapNamesLang::MapTy GraphRule::KernelNodeParamNames{
367+
{"gridDim", "grid_dim"},
368+
{"blockDim", "block_dim"},
369+
{"kernelParams", "kernel_params"},
370+
{"sharedMemBytes", "shared_mem_bytes"},
371+
{"func", "func"}};
372+
365373
} // namespace dpct
366374
} // namespace clang

clang/lib/DPCT/RulesLang/RulesLang.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -353,18 +353,19 @@ void TypeInDeclRule::registerMatcher(MatchFinder &MF) {
353353
this);
354354

355355
MF.addMatcher(
356-
typeLoc(loc(qualType(hasDeclaration(namedDecl(hasAnyName(
357-
"cooperative_groups::__v1::coalesced_group",
358-
"cooperative_groups::__v1::grid_group",
359-
"cooperative_groups::__v1::thread_block_tile", "cudaGraph_t",
360-
"cudaGraphExec_t", "cudaGraphNode_t", "cudaGraphicsResource",
361-
"cudaGraphicsResource_t", "CUgraphicsResource",
362-
"cudaExternalMemory_t", "cudaExternalMemoryHandleDesc",
363-
"cudaExternalMemoryMipmappedArrayDesc",
364-
"cudaExternalMemoryBufferDesc", "cudaExternalSemaphore_t",
365-
"cudaExternalSemaphoreHandleDesc",
366-
"cudaExternalSemaphoreSignalParams",
367-
"cudaExternalSemaphoreWaitParams"))))))
356+
typeLoc(
357+
loc(qualType(hasDeclaration(namedDecl(hasAnyName(
358+
"cooperative_groups::__v1::coalesced_group",
359+
"cooperative_groups::__v1::grid_group",
360+
"cooperative_groups::__v1::thread_block_tile", "cudaGraph_t",
361+
"cudaGraphExec_t", "cudaGraphNode_t", "cudaGraphicsResource",
362+
"cudaGraphicsResource_t", "CUgraphicsResource",
363+
"cudaExternalMemory_t", "cudaExternalMemoryHandleDesc",
364+
"cudaExternalMemoryMipmappedArrayDesc",
365+
"cudaExternalMemoryBufferDesc", "cudaExternalSemaphore_t",
366+
"cudaExternalSemaphoreHandleDesc",
367+
"cudaExternalSemaphoreSignalParams",
368+
"cudaExternalSemaphoreWaitParams", "cudaKernelNodeParams"))))))
368369
.bind("cudaTypeDefEA"),
369370
this);
370371
MF.addMatcher(varDecl(hasType(classTemplateSpecializationDecl(

clang/lib/DPCT/RulesLang/RulesLang.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -999,6 +999,10 @@ class CompatWithClangRule : public NamedMigrationRule<CompatWithClangRule> {
999999
};
10001000

10011001
class GraphRule : public NamedMigrationRule<GraphRule> {
1002+
static MapNames::MapTy KernelNodeParamNames;
1003+
const Expr *getAssignedBO(const Expr *E, ASTContext &Context);
1004+
const Expr *getParentAsAssignedBO(const Expr *E, ASTContext &Context);
1005+
10021006
public:
10031007
void registerMatcher(ast_matchers::MatchFinder &MF) override;
10041008
void runRule(const ast_matchers::MatchFinder::MatchResult &Result);

clang/lib/DPCT/RulesLang/RulesLangGraph.cpp

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,48 @@ void GraphRule::registerMatcher(MatchFinder &MF) {
3434
"cudaGraphExecDestroy", "cudaGraphAddEmptyNode",
3535
"cudaGraphAddDependencies", "cudaGraphExecUpdate",
3636
"cudaGraphNodeGetType", "cudaGraphGetNodes",
37-
"cudaGraphGetRootNodes", "cudaGraphDestroy");
37+
"cudaGraphGetRootNodes", "cudaGraphDestroy", "cudaGraphKernelNodeGetParams",
38+
"cudaGraphKernelNodeSetParams");
3839
};
3940
MF.addMatcher(
4041
callExpr(callee(functionDecl(functionName()))).bind("FunctionCall"),
4142
this);
43+
44+
auto typeName = [&]() { return hasAnyName("cudaKernelNodeParams"); };
45+
MF.addMatcher(
46+
memberExpr(hasObjectExpression(hasType(type(hasUnqualifiedDesugaredType(
47+
recordType(hasDeclaration(recordDecl(typeName()))))))))
48+
.bind("Type"),
49+
this);
4250
}
4351

4452
void GraphRule::runRule(const MatchFinder::MatchResult &Result) {
53+
if (auto ME = getNodeAsType<MemberExpr>(Result, "Type")) {
54+
auto BaseTy = DpctGlobalInfo::getUnqualifiedTypeName(
55+
ME->getBase()->getType().getDesugaredType(*Result.Context),
56+
*Result.Context);
57+
auto MemberName = ME->getMemberNameInfo().getAsString();
58+
if (BaseTy == "cudaKernelNodeParams") {
59+
60+
auto FieldName = KernelNodeParamNames[MemberName];
61+
if (FieldName.empty()) {
62+
report(ME->getBeginLoc(), Diagnostics::API_NOT_MIGRATED, false,
63+
DpctGlobalInfo::getOriginalTypeName(ME->getBase()->getType()) +
64+
"::" + ME->getMemberDecl()->getName().str());
65+
return;
66+
}
67+
requestFeature(HelperFeatureEnum::device_ext);
68+
if (auto BO = getParentAsAssignedBO(ME, *Result.Context)) {
69+
StringRef ReplacedArg = "";
70+
emplaceTransformation(
71+
ReplaceMemberAssignAsSetMethod(BO, ME, FieldName, ReplacedArg));
72+
} else {
73+
emplaceTransformation(new RenameFieldInMemberExpr(
74+
ME, buildString("get_", FieldName, "()")));
75+
}
76+
}
77+
return;
78+
}
4579
const CallExpr *CE = getNodeAsType<CallExpr>(Result, "FunctionCall");
4680
if (!CE) {
4781
return;
@@ -51,5 +85,47 @@ void GraphRule::runRule(const MatchFinder::MatchResult &Result) {
5185
EA.applyAllSubExprRepl();
5286
}
5387

88+
const Expr *GraphRule::getParentAsAssignedBO(const Expr *E,
89+
ASTContext &Context) {
90+
auto Parents = Context.getParents(*E);
91+
if (Parents.size() > 0)
92+
return getAssignedBO(Parents[0].get<Expr>(), Context);
93+
return nullptr;
94+
}
95+
96+
// Return the binary operator if E is the lhs of an assign expression, otherwise
97+
// nullptr.
98+
const Expr *GraphRule::getAssignedBO(const Expr *E, ASTContext &Context) {
99+
if (dyn_cast<MemberExpr>(E)) {
100+
// Continue finding parents when E is MemberExpr.
101+
return getParentAsAssignedBO(E, Context);
102+
} else if (auto ICE = dyn_cast<ImplicitCastExpr>(E)) {
103+
// Stop finding parents and return nullptr when E is ImplicitCastExpr,
104+
// except for ArrayToPointerDecay cast.
105+
if (ICE->getCastKind() == CK_ArrayToPointerDecay) {
106+
return getParentAsAssignedBO(E, Context);
107+
}
108+
} else if (auto ASE = dyn_cast<ArraySubscriptExpr>(E)) {
109+
// Continue finding parents when E is ArraySubscriptExpr, and remove
110+
// subscript operator anyway for texture object's member.
111+
emplaceTransformation(new ReplaceToken(
112+
Lexer::getLocForEndOfToken(ASE->getLHS()->getEndLoc(), 0,
113+
Context.getSourceManager(),
114+
Context.getLangOpts()),
115+
ASE->getRBracketLoc(), ""));
116+
return getParentAsAssignedBO(E, Context);
117+
} else if (auto BO = dyn_cast<BinaryOperator>(E)) {
118+
// If E is BinaryOperator, return E only when it is assign expression,
119+
// otherwise return nullptr.
120+
if (BO->getOpcode() == BO_Assign)
121+
return BO;
122+
} else if (auto COCE = dyn_cast<CXXOperatorCallExpr>(E)) {
123+
if (COCE->getOperator() == OO_Equal) {
124+
return COCE;
125+
}
126+
}
127+
return nullptr;
128+
}
129+
54130
} // namespace dpct
55131
} // namespace clang

clang/lib/DPCT/SrcAPI/APINames.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -456,12 +456,12 @@ ENTRY(cudaGraphInstantiateWithFlags, cudaGraphInstantiateWithFlags, false, NO_FL
456456
ENTRY(cudaGraphInstantiateWithParams, cudaGraphInstantiateWithParams, false, NO_FLAG, P4, "comment")
457457
ENTRY(cudaGraphKernelNodeCopyAttributes, cudaGraphKernelNodeCopyAttributes, false, NO_FLAG, P4, "comment")
458458
ENTRY(cudaGraphKernelNodeGetAttribute, cudaGraphKernelNodeGetAttribute, false, NO_FLAG, P4, "comment")
459-
ENTRY(cudaGraphKernelNodeGetParams, cudaGraphKernelNodeGetParams, false, NO_FLAG, P4, "comment")
459+
ENTRY(cudaGraphKernelNodeGetParams, cudaGraphKernelNodeGetParams, true, NO_FLAG, P4, "Successful/DPCT1119")
460460
ENTRY(cudaGraphKernelNodeSetAttribute, cudaGraphKernelNodeSetAttribute, false, NO_FLAG, P4, "comment")
461461
ENTRY(cudaGraphKernelNodeSetEnabled, cudaGraphKernelNodeSetEnabled, false, NO_FLAG, P4, "comment")
462462
ENTRY(cudaGraphKernelNodeSetGridDim, cudaGraphKernelNodeSetGridDim, false, NO_FLAG, P4, "comment")
463463
ENTRY(cudaGraphKernelNodeSetParam, cudaGraphKernelNodeSetParam, false, NO_FLAG, P4, "comment")
464-
ENTRY(cudaGraphKernelNodeSetParams, cudaGraphKernelNodeSetParams, false, NO_FLAG, P4, "comment")
464+
ENTRY(cudaGraphKernelNodeSetParams, cudaGraphKernelNodeSetParams, true, NO_FLAG, P4, "Successful/DPCT1119")
465465
ENTRY(cudaGraphKernelNodeUpdatesApply, cudaGraphKernelNodeUpdatesApply, false, NO_FLAG, P4, "comment")
466466
ENTRY(cudaGraphLaunch, cudaGraphLaunch, true, NO_FLAG, P4, "Successful/DPCT1119")
467467
ENTRY(cudaGraphMemAllocNodeGetParams, cudaGraphMemAllocNodeGetParams, false, NO_FLAG, P4, "comment")

clang/lib/DPCT/SrcAPI/TypeNames.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ ENTRY_TYPE(CUgraphNode, false, NO_FLAG, P4, "comment")
4141
ENTRY_TYPE(CUgraphicsResource, true, NO_FLAG, P4, "successful")
4242

4343
// CUDA Runtime Library
44-
ENTRY_TYPE(cudaKernelNodeParams, false, NO_FLAG, P4, "comment")
44+
ENTRY_TYPE(cudaKernelNodeParams, true, NO_FLAG, P4, "Successful/DPCT1119")
4545

4646
// cuDNN Library
4747
ENTRY_TYPE(cudnnReduceTensorIndices_t, false, NO_FLAG, P4, "comment")

clang/runtime/dpct-rt/include/dpct/graph.hpp

Lines changed: 65 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@
88

99
#pragma once
1010

11+
#include "dpct/util.hpp"
12+
#include "sycl/handler.hpp"
1113
#include <sycl/ext/oneapi/experimental/graph.hpp>
1214
#include <sycl/sycl.hpp>
1315
#include <unordered_map>
16+
#include <unordered_map>
1417

1518
namespace dpct {
1619
namespace experimental {
@@ -25,6 +28,28 @@ typedef sycl::ext::oneapi::experimental::command_graph<
2528

2629
typedef sycl::ext::oneapi::experimental::node *node_ptr;
2730

31+
struct kernel_node_params {
32+
dpct::dim3 block_dim;
33+
dpct::dim3 grid_dim;
34+
void *kernel_params;
35+
void* func;
36+
unsigned int shared_mem_bytes;
37+
38+
public:
39+
void set_block_dim(dpct::dim3 block_dim) { block_dim = block_dim; }
40+
void set_grid_dim(dpct::dim3 grid_dim) { grid_dim = grid_dim; }
41+
void set_kernel_params(void *kernel_params) { kernel_params = kernel_params; }
42+
void set_func(void *func) { func = func; }
43+
void set_shared_mem_bytes(unsigned int shared_mem_bytes) {
44+
shared_mem_bytes = shared_mem_bytes;
45+
}
46+
dpct::dim3 get_block_dim() { return block_dim; }
47+
dpct::dim3 get_grid_dim() { return grid_dim; }
48+
void *get_kernel_params() { return kernel_params; }
49+
void *get_func() { return func; }
50+
unsigned int get_shared_mem_bytes() { return shared_mem_bytes; }
51+
};
52+
2853
namespace detail {
2954
class graph_mgr {
3055
public:
@@ -39,6 +64,10 @@ class graph_mgr {
3964
return instance;
4065
}
4166

67+
std::unordered_map<dpct::experimental::node_ptr,
68+
dpct::experimental::kernel_node_params>
69+
kernel_node_params_map;
70+
4271
void begin_recording(sycl::queue *queue_ptr) {
4372
// Calling begin_recording on an already recording queue is a no-op in SYCL
4473
if (queue_graph_map.find(queue_ptr) != queue_graph_map.end()) {
@@ -94,6 +123,18 @@ class graph_mgr {
94123
}
95124
}
96125

126+
void kernel_node_set_params(
127+
dpct::experimental::node_ptr node,
128+
dpct::experimental::kernel_node_params *kernel_node_params) {
129+
kernel_node_params_map[node] = kernel_node_params;
130+
}
131+
132+
void get_kernel_node_get_params(
133+
dpct::experimental::node_ptr node,
134+
dpct::experimental::kernel_node_params *kernel_node_params) {
135+
kernel_node_params = kernel_node_params_map[node];
136+
}
137+
97138
private:
98139
std::unordered_map<sycl::queue *, command_graph_ptr> queue_graph_map;
99140
std::unordered_map<dpct::experimental::command_graph_ptr,
@@ -174,9 +215,9 @@ static void add_dependencies(dpct::experimental::command_graph_ptr graph,
174215
/// nodes will be assigned.
175216
/// \param [out] numberOfNodes The number of nodes in the graph.
176217
static void get_nodes(dpct::experimental::command_graph_ptr graph,
177-
dpct::experimental::node_ptr *nodesArray,
178-
std::size_t *numberOfNodes) {
179-
detail::graph_mgr::instance().get_nodes(graph, nodesArray, numberOfNodes);
218+
dpct::experimental::node_ptr *nodesArray,
219+
std::size_t *numberOfNodes) {
220+
detail::graph_mgr::instance().get_nodes(graph, nodesArray, numberOfNodes);
180221
}
181222

182223
/// Gets the root nodes in the command graph.
@@ -185,10 +226,27 @@ static void get_nodes(dpct::experimental::command_graph_ptr graph,
185226
/// root nodes will be assigned.
186227
/// \param [out] numberOfNodes The number of root nodes in the graph.
187228
static void get_root_nodes(dpct::experimental::command_graph_ptr graph,
188-
dpct::experimental::node_ptr *nodesArray,
189-
std::size_t *numberOfNodes) {
190-
detail::graph_mgr::instance().get_root_nodes(graph, nodesArray,
191-
numberOfNodes);
229+
dpct::experimental::node_ptr *nodesArray,
230+
std::size_t *numberOfNodes) {
231+
detail::graph_mgr::instance().get_root_nodes(graph, nodesArray,
232+
numberOfNodes);
233+
}
234+
235+
static void
236+
kernel_node_set_params(dpct::experimental::node_ptr node,
237+
dpct::experimental::kernel_node_params *params) {
238+
detail::graph_mgr::instance().kernel_node_set_params(node, params);
239+
}
240+
241+
static void
242+
kernel_node_get_params(dpct::experimental::node_ptr node,
243+
dpct::experimental::kernel_node_params *params) {
244+
detail::graph_mgr::instance().kernel_node_set_params(node, params);
245+
}
246+
247+
248+
static void add_kernel_node(dpct::experimental::node_ptr node, dpct::experimental::command_graph_ptr graph, dpct::experimental::node_ptr *dependencies, std::size_t numberOfDependencies, dpct::experimental::kernel_node_params &kernelNodeParams){
249+
192250
}
193251

194252
} // namespace experimental

0 commit comments

Comments
 (0)