Skip to content

Commit f5621e2

Browse files
[SYCLomatic] Added migration support for Semaphore APIs (#2695)
This PR adds support for - 4 External Semaphore Resource APIs cudaImportExternalSemaphore cudaSignalExternalSemaphoresAsync cudaWaitExternalSemaphoresAsync cudaDestroyExternalSemaphore 4 Data Types cudaExternalSemaphore_t cudaExternalSemaphoreHandleDesc cudaExternalSemaphoreSignalParams cudaExternalSemaphoreWaitParams 3 Semaphore Types opaqueFd win32_nt_handle win32_nt_dx12_resource Supported Libraries DirectX12 Vulkan
1 parent fdfa218 commit f5621e2

File tree

12 files changed

+769
-84
lines changed

12 files changed

+769
-84
lines changed

clang/lib/DPCT/Diagnostics/Diagnostics.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,8 @@ DEF_WARNING(SPARSE_NNZ, 1134, MEDIUM_LEVEL, "The tool cannot deduce the consumer
296296
DEF_COMMENT(SPARSE_NNZ, 1134, MEDIUM_LEVEL, "The tool cannot deduce the consumer API (\"dpct::sparse::csrgemm\") of this API, and this API has 2 arguments depending on the 8th and the 12th parameters of the consumer API. Please replace the 2 arguments tagged as \"dpct_placeholder\" with the corresponding value.")
297297
DEF_WARNING(JOINT_MATRIX_SHAPE, 1135, HIGH_LEVEL, "Please check if joint_matrix implementations support the combination of data type and matrix shape type in the target hardware.")
298298
DEF_COMMENT(JOINT_MATRIX_SHAPE, 1135, HIGH_LEVEL, "Please check if joint_matrix implementations support the combination of data type and matrix shape type in the target hardware.")
299-
DEF_WARNING(UNSUPPORTED_EXTMEM_WIN_HANDLE, 1136, HIGH_LEVEL, "SYCL Bindless Images extension only supports importing external resource memory using NT handle on Windows. If assert(%0.get_win32_handle()) fails, you may need to adjust the code to use (%0.get_win32_handle()).")
300-
DEF_COMMENT(UNSUPPORTED_EXTMEM_WIN_HANDLE, 1136, HIGH_LEVEL, "SYCL Bindless Images extension only supports importing external resource memory using NT handle on Windows. If assert({0}.get_win32_handle()) fails, you may need to adjust the code to use ({0}.get_win32_handle()).")
299+
DEF_WARNING(UNSUPPORTED_EXTMEM_WIN_HANDLE, 1136, HIGH_LEVEL, "SYCL Bindless Images extension only supports importing external resource using NT handle on Windows. If assert(%0.get_win32_handle()) fails, you may need to adjust the code to use (%0.get_win32_handle()).")
300+
DEF_COMMENT(UNSUPPORTED_EXTMEM_WIN_HANDLE, 1136, HIGH_LEVEL, "SYCL Bindless Images extension only supports importing external resource using NT handle on Windows. If assert({0}.get_win32_handle()) fails, you may need to adjust the code to use ({0}.get_win32_handle()).")
301301
DEF_WARNING(ASYNC_COPY_DEVICE_WARN, 1137, LOW_LEVEL, "ASM instruction \"%0\" is asynchronous copy, current it is migrated to synchronous copy operation. You may need to adjust the code to tune the performance.")
302302
DEF_COMMENT(ASYNC_COPY_DEVICE_WARN, 1137, LOW_LEVEL, "ASM instruction \"{0}\" is asynchronous copy, current it is migrated to synchronous copy operation. You may need to adjust the code to tune the performance.")
303303

clang/lib/DPCT/RuleInfra/APINamesTemplateType.inc

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,3 +593,60 @@ TYPE_REWRITE_ENTRY(
593593
WARNING_FACTORY(
594594
Diagnostics::TRY_EXPERIMENTAL_FEATURE, TYPESTR,
595595
STR("--use-experimental-features=bindless_images")))))
596+
597+
// External Semaphore Handle Descs
598+
TYPE_REWRITE_ENTRY(
599+
"cudaExternalSemaphore_t",
600+
TYPE_CONDITIONAL_FACTORY(
601+
[](const TypeLoc) { return DpctGlobalInfo::useSYCLCompat(); },
602+
WARNING_FACTORY(Diagnostics::UNSUPPORT_SYCLCOMPAT,
603+
STR("cudaExternalSemaphore_t")),
604+
TYPE_CONDITIONAL_FACTORY(
605+
checkEnableBindlessImagesForType(),
606+
TYPE_FACTORY(STR(MapNames::getDpctNamespace() +
607+
"experimental::external_sem_wrapper_ptr")),
608+
WARNING_FACTORY(
609+
Diagnostics::TRY_EXPERIMENTAL_FEATURE, TYPESTR,
610+
STR("--use-experimental-features=bindless_images")))))
611+
612+
TYPE_REWRITE_ENTRY(
613+
"cudaExternalSemaphoreHandleDesc",
614+
TYPE_CONDITIONAL_FACTORY(
615+
[](const TypeLoc) { return DpctGlobalInfo::useSYCLCompat(); },
616+
WARNING_FACTORY(Diagnostics::UNSUPPORT_SYCLCOMPAT,
617+
STR("cudaExternalSemaphoreHandleDesc")),
618+
TYPE_CONDITIONAL_FACTORY(
619+
checkEnableBindlessImagesForType(),
620+
TYPE_FACTORY(STR(MapNames::getDpctNamespace() +
621+
"experimental::external_sem_handle_desc")),
622+
WARNING_FACTORY(
623+
Diagnostics::TRY_EXPERIMENTAL_FEATURE, TYPESTR,
624+
STR("--use-experimental-features=bindless_images")))))
625+
626+
TYPE_REWRITE_ENTRY(
627+
"cudaExternalSemaphoreSignalParams",
628+
TYPE_CONDITIONAL_FACTORY(
629+
[](const TypeLoc) { return DpctGlobalInfo::useSYCLCompat(); },
630+
WARNING_FACTORY(Diagnostics::UNSUPPORT_SYCLCOMPAT,
631+
STR("cudaExternalSemaphoreSignalParams")),
632+
TYPE_CONDITIONAL_FACTORY(
633+
checkEnableBindlessImagesForType(),
634+
TYPE_FACTORY(STR(MapNames::getDpctNamespace() +
635+
"experimental::external_sem_params")),
636+
WARNING_FACTORY(
637+
Diagnostics::TRY_EXPERIMENTAL_FEATURE, TYPESTR,
638+
STR("--use-experimental-features=bindless_images")))))
639+
640+
TYPE_REWRITE_ENTRY(
641+
"cudaExternalSemaphoreWaitParams",
642+
TYPE_CONDITIONAL_FACTORY(
643+
[](const TypeLoc) { return DpctGlobalInfo::useSYCLCompat(); },
644+
WARNING_FACTORY(Diagnostics::UNSUPPORT_SYCLCOMPAT,
645+
STR("cudaExternalSemaphoreWaitParams")),
646+
TYPE_CONDITIONAL_FACTORY(
647+
checkEnableBindlessImagesForType(),
648+
TYPE_FACTORY(STR(MapNames::getDpctNamespace() +
649+
"experimental::external_sem_params")),
650+
WARNING_FACTORY(
651+
Diagnostics::TRY_EXPERIMENTAL_FEATURE, TYPESTR,
652+
STR("--use-experimental-features=bindless_images")))))

clang/lib/DPCT/RuleInfra/MapNames.cpp

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,13 @@ void MapNames::setExplicitNamespaceMap(
260260
{"cudaExternalMemoryDedicated",
261261
MacroMigrationRule("dpct_build_in_macro_rule", RulePriority::Fallback,
262262
"cudaExternalMemoryDedicated", "0")},
263+
{"cudaExternalSemaphoreSignalSkipNvSciBufMemSync",
264+
MacroMigrationRule("dpct_build_in_macro_rule", RulePriority::Fallback,
265+
"cudaExternalSemaphoreSignalSkipNvSciBufMemSync",
266+
"0")},
267+
{"cudaExternalSemaphoreWaitSkipNvSciBufMemSync",
268+
MacroMigrationRule("dpct_build_in_macro_rule", RulePriority::Fallback,
269+
"cudaExternalSemaphoreWaitSkipNvSciBufMemSync", "0")},
263270
{"cudaArrayDefault",
264271
MacroMigrationRule("dpct_build_in_macro_rule", RulePriority::Fallback,
265272
"cudaArrayDefault",
@@ -331,7 +338,8 @@ void MapNames::setExplicitNamespaceMap(
331338
{"__half2", std::make_shared<TypeNameRule>(getClNamespace() + "half2")},
332339
{"half", std::make_shared<TypeNameRule>(getClNamespace() + "half")},
333340
{"half2", std::make_shared<TypeNameRule>(getClNamespace() + "half2")},
334-
{"__nv_half2", std::make_shared<TypeNameRule>(getClNamespace() + "half2")},
341+
{"__nv_half2",
342+
std::make_shared<TypeNameRule>(getClNamespace() + "half2")},
335343
{"__nv_half", std::make_shared<TypeNameRule>(getClNamespace() + "half")},
336344
{"cudaEvent_t",
337345
std::make_shared<TypeNameRule>(getDpctNamespace() + "event_ptr",
@@ -886,6 +894,9 @@ void MapNames::setExplicitNamespaceMap(
886894
{"cudaExternalMemoryHandleType",
887895
std::make_shared<TypeNameRule>(getExpNamespace() +
888896
"external_mem_handle_type")},
897+
{"cudaExternalSemaphoreHandleType",
898+
std::make_shared<TypeNameRule>(getExpNamespace() +
899+
"external_semaphore_handle_type")},
889900
// ...
890901
};
891902
// SYCLcompat unsupport types
@@ -1521,6 +1532,24 @@ void MapNames::setExplicitNamespaceMap(
15211532
? getExpNamespace() +
15221533
"external_mem_handle_type::win32_nt_dx12_resource"
15231534
: "cudaExternalMemoryHandleTypeD3D12Resource")},
1535+
// enum cudaExternalSemaphoreHandleType
1536+
{"cudaExternalSemaphoreHandleTypeOpaqueFd",
1537+
std::make_shared<EnumNameRule>(
1538+
DpctGlobalInfo::useExtBindlessImages()
1539+
? getExpNamespace() + "external_semaphore_handle_type::opaque_fd"
1540+
: "cudaExternalSemaphoreHandleTypeOpaqueFd")},
1541+
{"cudaExternalSemaphoreHandleTypeOpaqueWin32",
1542+
std::make_shared<EnumNameRule>(
1543+
DpctGlobalInfo::useExtBindlessImages()
1544+
? getExpNamespace() +
1545+
"external_semaphore_handle_type::win32_nt_handle"
1546+
: "cudaExternalSemaphoreHandleTypeOpaqueWin32")},
1547+
{"cudaExternalSemaphoreHandleTypeD3D12Fence",
1548+
std::make_shared<EnumNameRule>(
1549+
DpctGlobalInfo::useExtBindlessImages()
1550+
? getExpNamespace() +
1551+
"external_semaphore_handle_type::win32_nt_dx12_fence"
1552+
: "cudaExternalSemaphoreHandleTypeD3D12Fence")},
15241553
// ...
15251554
};
15261555

clang/lib/DPCT/RuleInfra/TypeLocRewriters.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,10 @@ void initTypeLocSYCLCompatRewriterMap(
360360
SYCLCOMPAT_UNSUPPORT("cudaExternalMemoryHandleDesc")
361361
SYCLCOMPAT_UNSUPPORT("cudaExternalMemoryMipmappedArrayDesc")
362362
SYCLCOMPAT_UNSUPPORT("cudaExternalMemoryBufferDesc")
363+
SYCLCOMPAT_UNSUPPORT("cudaExternalSemaphore_t")
364+
SYCLCOMPAT_UNSUPPORT("cudaExternalSemaphoreHandleDesc")
365+
SYCLCOMPAT_UNSUPPORT("cudaExternalSemaphoreSignalParams")
366+
SYCLCOMPAT_UNSUPPORT("cudaExternalSemaphoreWaitParams")
363367
SYCLCOMPAT_UNSUPPORT("thrust::system::cuda::experimental::pinned_allocator")
364368
SYCLCOMPAT_UNSUPPORT("thrust::cuda::experimental::pinned_allocator")
365369
SYCLCOMPAT_UNSUPPORT("thrust::device_allocator")

clang/lib/DPCT/RulesLang/APINamesGraphicsInterop.inc

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,4 +184,54 @@ CONDITIONAL_FACTORY_ENTRY(
184184
UNSUPPORT_FACTORY_ENTRY("cudaDestroyExternalMemory", Diagnostics::TRY_EXPERIMENTAL_FEATURE,
185185
ARG("cudaDestroyExternalMemory"),
186186
ARG("--use-experimental-features=bindless_images")))
187+
188+
CONDITIONAL_FACTORY_ENTRY(
189+
UseExtBindlessImages,
190+
ASSIGNABLE_FACTORY(CALL_FACTORY_ENTRY("cudaImportExternalSemaphore",
191+
CALL(MapNames::getDpctNamespace() +
192+
"experimental::import_external_semaphore",
193+
ARG(0), ARG(1)))),
194+
UNSUPPORT_FACTORY_ENTRY("cudaImportExternalSemaphore", Diagnostics::TRY_EXPERIMENTAL_FEATURE,
195+
ARG("cudaImportExternalSemaphore"),
196+
ARG("--use-experimental-features=bindless_images")))
197+
198+
CONDITIONAL_FACTORY_ENTRY(
199+
UseExtBindlessImages,
200+
CONDITIONAL_FACTORY_ENTRY(
201+
makeCheckNot(CheckArgIsDefaultCudaStream(3)),
202+
ASSIGNABLE_FACTORY(CALL_FACTORY_ENTRY("cudaSignalExternalSemaphoresAsync_v2",
203+
CALL(MapNames::getDpctNamespace() +
204+
"experimental::signal_external_semaphore",
205+
ARG(0), ARG(1), ARG(2), ARG(3)))),
206+
ASSIGNABLE_FACTORY(CALL_FACTORY_ENTRY("cudaSignalExternalSemaphoresAsync_v2",
207+
CALL(MapNames::getDpctNamespace() +
208+
"experimental::signal_external_semaphore",
209+
ARG(0), ARG(1), ARG(2))))),
210+
UNSUPPORT_FACTORY_ENTRY("cudaSignalExternalSemaphoresAsync_v2", Diagnostics::TRY_EXPERIMENTAL_FEATURE,
211+
ARG("cudaSignalExternalSemaphoresAsync_v2"),
212+
ARG("--use-experimental-features=bindless_images")))
213+
214+
CONDITIONAL_FACTORY_ENTRY(
215+
UseExtBindlessImages,
216+
CONDITIONAL_FACTORY_ENTRY(
217+
makeCheckNot(CheckArgIsDefaultCudaStream(3)),
218+
ASSIGNABLE_FACTORY(CALL_FACTORY_ENTRY("cudaWaitExternalSemaphoresAsync_v2",
219+
CALL(MapNames::getDpctNamespace() +
220+
"experimental::wait_external_semaphore",
221+
ARG(0), ARG(1), ARG(2), ARG(3)))),
222+
ASSIGNABLE_FACTORY(CALL_FACTORY_ENTRY("cudaWaitExternalSemaphoresAsync_v2",
223+
CALL(MapNames::getDpctNamespace() +
224+
"experimental::wait_external_semaphore",
225+
ARG(0), ARG(1), ARG(2))))),
226+
UNSUPPORT_FACTORY_ENTRY("cudaWaitExternalSemaphoresAsync_v2", Diagnostics::TRY_EXPERIMENTAL_FEATURE,
227+
ARG("cudaWaitExternalSemaphoresAsync_v2"),
228+
ARG("--use-experimental-features=bindless_images")))
229+
230+
ASSIGNABLE_FACTORY(CONDITIONAL_FACTORY_ENTRY(
231+
UseExtBindlessImages,
232+
DELETER_FACTORY_ENTRY("cudaDestroyExternalSemaphore", ARG(0)),
233+
UNSUPPORT_FACTORY_ENTRY(
234+
"cudaDestroyExternalSemaphore", Diagnostics::TRY_EXPERIMENTAL_FEATURE,
235+
ARG("cudaDestroyExternalSemaphore"),
236+
ARG("--use-experimental-features=bindless_images"))))
187237
// clang-format on

clang/lib/DPCT/RulesLang/MapNamesLang.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,13 +235,18 @@ MapNamesLang::MapTy TextureRule::ResourceTypeNames{
235235
{"format", "channel_type"},
236236
{"numChannels", "channel_num"}};
237237

238-
// External memory handle descs fields mapping
239-
MapNamesLang::MapTy GraphicsInteropRule::ExtMemHandleDescNames{
238+
// External resource mem handle descs fields mapping
239+
MapNamesLang::MapTy GraphicsInteropRule::ExtResMemHandleDescNames{
240240
{"fd", "fd_handle"}, {"handle", "win32_handle"},
241241
{"name", "win32_obj_name"}, {"size", "res_size"},
242242
{"type", "handle_type"}, {"flags", "flags"},
243243
{"offset", "mem_offset"}, {"numLevels", "num_levels"},
244-
{"extent", "size"}, {"formatDesc", "image_channel"}};
244+
{"extent", "size"}, {"formatDesc", "image_channel"},
245+
{"value", "value"}};
246+
247+
// External resource sem param descs fields mapping
248+
MapNamesLang::MapTy GraphicsInteropRule::ExtResSemParamsNames{
249+
{"value", "value"}};
245250

246251
const MapNamesLang::MapTy MemoryDataTypeRule::PitchMemberNames{
247252
{"pitch", "pitch"}, {"ptr", "data_ptr"}, {"xsize", "x"}, {"ysize", "y"}};

clang/lib/DPCT/RulesLang/RulesLang.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,8 @@ void TypeInDeclRule::registerMatcher(MatchFinder &MF) {
346346
"cublasLtMatmulHeuristicResult_t", "CUjit_target",
347347
"cublasLtMatrixTransformDesc_t", "cudaGraphicsMapFlags",
348348
"cudaGraphicsRegisterFlags", "cudaExternalMemoryHandleType",
349-
"CUstreamCallback", "cudaHostFn_t", "__nv_half2",
350-
"__nv_half"))))))
349+
"cudaExternalSemaphoreHandleType", "CUstreamCallback",
350+
"cudaHostFn_t", "__nv_half2", "__nv_half"))))))
351351
.bind("cudaTypeDef"),
352352
this);
353353

@@ -360,7 +360,10 @@ void TypeInDeclRule::registerMatcher(MatchFinder &MF) {
360360
"cudaGraphicsResource_t", "CUgraphicsResource",
361361
"cudaExternalMemory_t", "cudaExternalMemoryHandleDesc",
362362
"cudaExternalMemoryMipmappedArrayDesc",
363-
"cudaExternalMemoryBufferDesc"))))))
363+
"cudaExternalMemoryBufferDesc", "cudaExternalSemaphore_t",
364+
"cudaExternalSemaphoreHandleDesc",
365+
"cudaExternalSemaphoreSignalParams",
366+
"cudaExternalSemaphoreWaitParams"))))))
364367
.bind("cudaTypeDefEA"),
365368
this);
366369
MF.addMatcher(varDecl(hasType(classTemplateSpecializationDecl(
@@ -1981,7 +1984,14 @@ void EnumConstantRule::runRule(const MatchFinder::MatchResult &Result) {
19811984
EnumName == "cudaExternalMemoryHandleTypeD3D12Heap" ||
19821985
EnumName == "cudaExternalMemoryHandleTypeD3D11Resource" ||
19831986
EnumName == "cudaExternalMemoryHandleTypeD3D11ResourceKmt" ||
1984-
EnumName == "cudaExternalMemoryHandleTypeNvSciBuf") {
1987+
EnumName == "cudaExternalMemoryHandleTypeNvSciBuf" ||
1988+
EnumName == "cudaExternalSemaphoreHandleTypeOpaqueWin32Kmt" ||
1989+
EnumName == "cudaExternalSemaphoreHandleTypeD3D11Fence" ||
1990+
EnumName == "cudaExternalSemaphoreHandleTypeNvSciSync" ||
1991+
EnumName == "cudaExternalSemaphoreHandleTypeKeyedMutex" ||
1992+
EnumName == "cudaExternalSemaphoreHandleTypeKeyedMutexKmt" ||
1993+
EnumName == "cudaExternalSemaphoreHandleTypeTimelineSemaphoreFd" ||
1994+
EnumName == "cudaExternalSemaphoreHandleTypeTimelineSemaphoreWin32") {
19851995
report(E->getBeginLoc(), Diagnostics::API_NOT_MIGRATED, false, EnumName);
19861996
return;
19871997
} else if (EnumName == "cudaComputeModeDefault" ||
@@ -2007,7 +2017,10 @@ void EnumConstantRule::runRule(const MatchFinder::MatchResult &Result) {
20072017
EnumName == "cudaGraphicsMapFlagsWriteDiscard" ||
20082018
EnumName == "cudaExternalMemoryHandleTypeOpaqueFd" ||
20092019
EnumName == "cudaExternalMemoryHandleTypeOpaqueWin32" ||
2010-
EnumName == "cudaExternalMemoryHandleTypeD3D12Resource")) {
2020+
EnumName == "cudaExternalMemoryHandleTypeD3D12Resource" ||
2021+
EnumName == "cudaExternalSemaphoreHandleTypeOpaqueFd" ||
2022+
EnumName == "cudaExternalSemaphoreHandleTypeOpaqueWin32" ||
2023+
EnumName == "cudaExternalSemaphoreHandleTypeD3D12Fence")) {
20112024
report(E->getBeginLoc(), Diagnostics::TRY_EXPERIMENTAL_FEATURE, false,
20122025
EnumName, "--use-experimental-features=bindless_images");
20132026
return;

clang/lib/DPCT/RulesLang/RulesLang.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,11 +1011,14 @@ class AssertRule : public NamedMigrationRule<AssertRule> {
10111011
};
10121012

10131013
class GraphicsInteropRule : public NamedMigrationRule<GraphicsInteropRule> {
1014-
static MapNames::MapTy ExtMemHandleDescNames;
1014+
static MapNames::MapTy ExtResMemHandleDescNames, ExtResSemParamsNames;
10151015

10161016
const Expr *getAssignedBO(const Expr *E, ASTContext &Context);
10171017
const Expr *getParentAsAssignedBO(const Expr *E, ASTContext &Context);
1018-
void replaceExtMemHandleDataExpr(const MemberExpr *ME, ASTContext &Context);
1018+
void replaceExtResMemHandleDataExpr(const MemberExpr *ME,
1019+
ASTContext &Context);
1020+
void replaceExtResSemParamsDataExpr(const MemberExpr *ME,
1021+
ASTContext &Context);
10191022
inline const MemberExpr *getParentMemberExpr(const Stmt *S) {
10201023
return DpctGlobalInfo::findParent<MemberExpr>(S);
10211024
}

0 commit comments

Comments
 (0)