Skip to content

Commit 5b1ef99

Browse files
authored
[SYCLomatic] Enable migration of 8 CUDA driver virtual memory management API (#2396)
Signed-off-by: intwanghao <[email protected]>
1 parent eab9c7e commit 5b1ef99

File tree

12 files changed

+410
-18
lines changed

12 files changed

+410
-18
lines changed

clang/include/clang/DPCT/DPCTOptions.inc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,11 @@ DPCT_ENUM_OPTION(
792792
"be accessed within a kernel using syntax similar to C++ global "
793793
"variables.\n",
794794
false),
795+
DPCT_OPTION_ENUM_VALUE(
796+
"virtual_mem", int(ExperimentalFeatures::Exp_VirtualMemory),
797+
"Experimental extension that allows map an address range onto "
798+
"multiple allocations of physical memory.",
799+
false),
795800
DPCT_OPTION_ENUM_VALUE(
796801
"non-stdandard-sycl-builtins",
797802
int(ExperimentalFeatures::Exp_NonStandardSYCLBuiltins),

clang/lib/DPCT/APINames.inc

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
*
3333
****************************************************************************/
3434

35+
// clang-format off
36+
3537
// API Names refer to "v12.2"
3638
// CUDA runtime API
3739
// Device management functions of runtime API
@@ -1717,20 +1719,20 @@ ENTRY(cuMipmappedArrayGetMemoryRequirements, cuMipmappedArrayGetMemoryRequiremen
17171719
ENTRY(cuMipmappedArrayGetSparseProperties, cuMipmappedArrayGetSparseProperties, false, NO_FLAG, P7, "comment")
17181720

17191721
// Virtual Memory Management
1720-
ENTRY(cuMemAddressFree, cuMemAddressFree, false, NO_FLAG, P4, "comment")
1721-
ENTRY(cuMemAddressReserve, cuMemAddressReserve, false, NO_FLAG, P4, "comment")
1722-
ENTRY(cuMemCreate, cuMemCreate, false, NO_FLAG, P4, "comment")
1722+
ENTRY(cuMemAddressFree, cuMemAddressFree, true, NO_FLAG, P4, "comment")
1723+
ENTRY(cuMemAddressReserve, cuMemAddressReserve, true, NO_FLAG, P4, "comment")
1724+
ENTRY(cuMemCreate, cuMemCreate, true, NO_FLAG, P4, "comment")
17231725
ENTRY(cuMemExportToShareableHandle, cuMemExportToShareableHandle, false, NO_FLAG, P4, "comment")
17241726
ENTRY(cuMemGetAccess, cuMemGetAccess, false, NO_FLAG, P4, "comment")
1725-
ENTRY(cuMemGetAllocationGranularity, cuMemGetAllocationGranularity, false, NO_FLAG, P4, "comment")
1727+
ENTRY(cuMemGetAllocationGranularity, cuMemGetAllocationGranularity, true, NO_FLAG, P4, "comment")
17261728
ENTRY(cuMemGetAllocationPropertiesFromHandle, cuMemGetAllocationPropertiesFromHandle, false, NO_FLAG, P4, "comment")
17271729
ENTRY(cuMemImportFromShareableHandle, cuMemImportFromShareableHandle, false, NO_FLAG, P4, "comment")
1728-
ENTRY(cuMemMap, cuMemMap, false, NO_FLAG, P4, "comment")
1730+
ENTRY(cuMemMap, cuMemMap, true, NO_FLAG, P4, "comment")
17291731
ENTRY(cuMemMapArrayAsync, cuMemMapArrayAsync, false, NO_FLAG, P7, "comment")
1730-
ENTRY(cuMemRelease, cuMemRelease, false, NO_FLAG, P4, "comment")
1732+
ENTRY(cuMemRelease, cuMemRelease, true, NO_FLAG, P4, "comment")
17311733
ENTRY(cuMemRetainAllocationHandle, cuMemRetainAllocationHandle, false, NO_FLAG, P7, "comment")
1732-
ENTRY(cuMemSetAccess, cuMemSetAccess, false, NO_FLAG, P4, "comment")
1733-
ENTRY(cuMemUnmap, cuMemUnmap, false, NO_FLAG, P4, "comment")
1734+
ENTRY(cuMemSetAccess, cuMemSetAccess, true, NO_FLAG, P4, "comment")
1735+
ENTRY(cuMemUnmap, cuMemUnmap, true, NO_FLAG, P4, "comment")
17341736

17351737
// Stream Ordered Memory Allocator
17361738
ENTRY(cuMemAllocAsync, cuMemAllocAsync, false, NO_FLAG, P7, "comment")
@@ -2307,3 +2309,5 @@ ENTRY(__assert_fail, __assert_fail, true, NO_FLAG, P4, "Successful")
23072309
ENTRY(__assertfail, __assertfail, true, NO_FLAG, P4, "Successful")
23082310

23092311
ENTRY(cuGetExportTable, cuGetExportTable, true, NO_FLAG, P7, "Partial")
2312+
2313+
// clang-format on

clang/lib/DPCT/APINamesMemory.inc

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,137 @@ ASSIGNABLE_FACTORY(FEATURE_REQUEST_FACTORY(
698698
ARG(0), ARG(1), ARG(2), ARG(3), ARG(4),
699699
DEREF(makeCallArgCreatorWithCall(5))))))
700700

701+
CONDITIONAL_FACTORY_ENTRY(
702+
UseExpVirtualMemory,
703+
ASSIGNABLE_FACTORY(FEATURE_REQUEST_FACTORY(
704+
HelperFeatureEnum::device_ext,
705+
ASSIGN_FACTORY_ENTRY(
706+
"cuMemCreate", DEREF(0),
707+
NEW(MapNames::getClNamespace() +
708+
"ext::oneapi::experimental::physical_mem",
709+
CALL(MapNames::getDpctNamespace() + "get_device",
710+
MEMBER_EXPR(DEREF(2), false, LITERAL("location.id"))),
711+
MEMBER_CALL(CALL(MapNames::getDpctNamespace() + "get_device",
712+
MEMBER_EXPR(DEREF(2), false,
713+
LITERAL("location.id"))),
714+
false, "get_context"),
715+
ARG(1))))),
716+
UNSUPPORT_FACTORY_ENTRY("cuMemCreate",
717+
Diagnostics::TRY_EXPERIMENTAL_FEATURE,
718+
ARG("cuMemCreate"),
719+
ARG("--use-experimental-features=virtual_memory")))
720+
721+
CONDITIONAL_FACTORY_ENTRY(
722+
UseExpVirtualMemory,
723+
ASSIGNABLE_FACTORY(FEATURE_REQUEST_FACTORY(
724+
HelperFeatureEnum::device_ext,
725+
ASSIGN_FACTORY_ENTRY(
726+
"cuMemAddressReserve", DEREF(0),
727+
CALL("(" + MapNames::getDpctNamespace() + "device_ptr)" +
728+
MapNames::getClNamespace() +
729+
"ext::oneapi::experimental::reserve_virtual_mem",
730+
CAST(LITERAL("uintptr_t"), ARG(3)), ARG(1),
731+
LITERAL(MapNames::getDpctNamespace() +
732+
"get_current_device().get_context()"))))),
733+
UNSUPPORT_FACTORY_ENTRY("cuMemAddressReserve",
734+
Diagnostics::TRY_EXPERIMENTAL_FEATURE,
735+
ARG("cuMemAddressReserve"),
736+
ARG("--use-experimental-features=virtual_memory")))
737+
738+
CONDITIONAL_FACTORY_ENTRY(
739+
UseExpVirtualMemory,
740+
ASSIGNABLE_FACTORY(FEATURE_REQUEST_FACTORY(
741+
HelperFeatureEnum::device_ext,
742+
CALL_FACTORY_ENTRY(
743+
"cuMemAddressFree",
744+
CALL(MapNames::getClNamespace() +
745+
"ext::oneapi::experimental::free_virtual_mem",
746+
CAST(LITERAL("uintptr_t"), ARG(0)), ARG(1),
747+
LITERAL(MapNames::getDpctNamespace() +
748+
"get_current_device().get_context()"))))),
749+
UNSUPPORT_FACTORY_ENTRY("cuMemAddressFree",
750+
Diagnostics::TRY_EXPERIMENTAL_FEATURE,
751+
ARG("cuMemAddressFree"),
752+
ARG("--use-experimental-features=virtual_memory")))
753+
754+
CONDITIONAL_FACTORY_ENTRY(
755+
UseExpVirtualMemory,
756+
ASSIGNABLE_FACTORY(FEATURE_REQUEST_FACTORY(
757+
HelperFeatureEnum::device_ext,
758+
ASSIGN_FACTORY_ENTRY(
759+
"cuMemGetAllocationGranularity", DEREF(0),
760+
CALL(MapNames::getClNamespace() +
761+
"ext::oneapi::experimental::get_mem_granularity",
762+
CALL(MapNames::getDpctNamespace() + "get_device",
763+
MEMBER_EXPR(DEREF(1), false, LITERAL("location.id"))),
764+
MEMBER_CALL(CALL(MapNames::getDpctNamespace() + "get_device",
765+
MEMBER_EXPR(DEREF(1), false,
766+
LITERAL("location.id"))),
767+
false, "get_context"),
768+
ARG(2))))),
769+
UNSUPPORT_FACTORY_ENTRY("cuMemGetAllocationGranularity",
770+
Diagnostics::TRY_EXPERIMENTAL_FEATURE,
771+
ARG("cuMemGetAllocationGranularity"),
772+
ARG("--use-experimental-features=virtual_memory")))
773+
774+
CONDITIONAL_FACTORY_ENTRY(
775+
UseExpVirtualMemory,
776+
ASSIGNABLE_FACTORY(
777+
FEATURE_REQUEST_FACTORY(HelperFeatureEnum::device_ext,
778+
DELETE_FACTORY_ENTRY("cuMemRelease", ARG(0)))),
779+
UNSUPPORT_FACTORY_ENTRY("cuMemRelease",
780+
Diagnostics::TRY_EXPERIMENTAL_FEATURE,
781+
ARG("cuMemRelease"),
782+
ARG("--use-experimental-features=virtual_memory")))
783+
784+
CONDITIONAL_FACTORY_ENTRY(
785+
UseExpVirtualMemory,
786+
ASSIGNABLE_FACTORY(FEATURE_REQUEST_FACTORY(
787+
HelperFeatureEnum::device_ext,
788+
MEMBER_CALL_FACTORY_ENTRY(
789+
"cuMemMap", ARG(3), true, "map", CAST(LITERAL("uintptr_t"), ARG(0)),
790+
ARG(1),
791+
LITERAL(
792+
MapNames::getClNamespace() +
793+
"ext::oneapi::experimental::address_access_mode::read_write"),
794+
ARG(2)))),
795+
UNSUPPORT_FACTORY_ENTRY("cuMemMap", Diagnostics::TRY_EXPERIMENTAL_FEATURE,
796+
ARG("cuMemMap"),
797+
ARG("--use-experimental-features=virtual_memory")))
798+
799+
CONDITIONAL_FACTORY_ENTRY(
800+
UseExpVirtualMemory,
801+
ASSIGNABLE_FACTORY(FEATURE_REQUEST_FACTORY(
802+
HelperFeatureEnum::device_ext,
803+
CALL_FACTORY_ENTRY(
804+
"cuMemUnmap",
805+
CALL(MapNames::getClNamespace() +
806+
"ext::oneapi::experimental::unmap",
807+
ARG(0), ARG(1),
808+
LITERAL(MapNames::getDpctNamespace() +
809+
"get_current_device().get_context()"))))),
810+
UNSUPPORT_FACTORY_ENTRY("cuMemUnmap", Diagnostics::TRY_EXPERIMENTAL_FEATURE,
811+
ARG("cuMemUnmap"),
812+
ARG("--use-experimental-features=virtual_memory")))
813+
814+
CONDITIONAL_FACTORY_ENTRY(
815+
UseExpVirtualMemory,
816+
ASSIGNABLE_FACTORY(FEATURE_REQUEST_FACTORY(
817+
HelperFeatureEnum::device_ext,
818+
CALL_FACTORY_ENTRY(
819+
"cuMemSetAccess",
820+
CALL(MapNames::getClNamespace() +
821+
"ext::oneapi::experimental::set_access_mode",
822+
ARG(0), ARG(1), MEMBER_EXPR(DEREF(2), false, LITERAL("flags")),
823+
MEMBER_CALL(CALL(MapNames::getDpctNamespace() + "get_device",
824+
MEMBER_EXPR(DEREF(2), false,
825+
LITERAL("location.id"))),
826+
false, "get_context"))))),
827+
UNSUPPORT_FACTORY_ENTRY("cuMemSetAccess",
828+
Diagnostics::TRY_EXPERIMENTAL_FEATURE,
829+
ARG("cuMemSetAccess"),
830+
ARG("--use-experimental-features=virtual_memory")))
831+
701832
CONDITIONAL_FACTORY_ENTRY(
702833
UsePeerAccess(),
703834
ASSIGNABLE_FACTORY(ASSIGN_FACTORY_ENTRY(

clang/lib/DPCT/ASTTraversal.cpp

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13971,6 +13971,96 @@ void FFTFunctionCallRule::runRule(const MatchFinder::MatchResult &Result) {
1397113971
REGISTER_RULE(FFTFunctionCallRule, PassKind::PK_Migration,
1397213972
RuleGroupKind::RK_FFT)
1397313973

13974+
void VirtualMemRule::registerMatcher(ast_matchers::MatchFinder &MF) {
13975+
auto virtualmemoryAPI = [&]() {
13976+
return hasAnyName("cuMemCreate", "cuMemAddressReserve", "cuMemMap",
13977+
"cuMemUnmap", "cuMemAddressFree", "cuMemRelease",
13978+
"cuMemSetAccess", "cuMemGetAllocationGranularity");
13979+
};
13980+
auto virtualmemoryType = [&]() {
13981+
return hasAnyName("CUmemAllocationProp", "CUmemGenericAllocationHandle",
13982+
"CUmemAccessDesc", "CUmemLocationType",
13983+
"CUmemAllocationType", "CUmemAllocationGranularity_flags",
13984+
"CUmemAccess_flags");
13985+
};
13986+
auto virtualmemoryEnum = [&]() {
13987+
return hasAnyName(
13988+
"CU_MEM_ALLOCATION_TYPE_PINNED", "CU_MEM_ALLOCATION_TYPE_INVALID",
13989+
"CU_MEM_ALLOCATION_TYPE_MAX", "CU_MEM_LOCATION_TYPE_DEVICE",
13990+
"CU_MEM_LOCATION_TYPE_INVALID", "CU_MEM_LOCATION_TYPE_MAX",
13991+
"CU_MEM_ACCESS_FLAGS_PROT_NONE", "CU_MEM_ACCESS_FLAGS_PROT_READ",
13992+
"CU_MEM_ACCESS_FLAGS_PROT_READWRITE",
13993+
"CU_MEM_ALLOC_GRANULARITY_RECOMMENDED",
13994+
"CU_MEM_ALLOC_GRANULARITY_MINIMUM");
13995+
};
13996+
MF.addMatcher(
13997+
callExpr(callee(functionDecl(virtualmemoryAPI()))).bind("vmCall"), this);
13998+
MF.addMatcher(
13999+
typeLoc(loc(qualType(hasDeclaration(namedDecl(virtualmemoryType())))))
14000+
.bind("vmType"),
14001+
this);
14002+
MF.addMatcher(
14003+
declRefExpr(to(enumConstantDecl(virtualmemoryEnum()))).bind("vmEnum"),
14004+
this);
14005+
}
14006+
14007+
void VirtualMemRule::runRule(
14008+
const ast_matchers::MatchFinder::MatchResult &Result) {
14009+
auto &SM = DpctGlobalInfo::getSourceManager();
14010+
if (const CallExpr *CE = getNodeAsType<CallExpr>(Result, "vmCall")) {
14011+
ExprAnalysis EA(CE);
14012+
emplaceTransformation(EA.getReplacement());
14013+
EA.applyAllSubExprRepl();
14014+
}
14015+
if (auto TL = getNodeAsType<TypeLoc>(Result, "vmType")) {
14016+
auto TypeStr =
14017+
DpctGlobalInfo::getTypeName(TL->getType().getUnqualifiedType());
14018+
if (!DpctGlobalInfo::useExpVirtualMemory()) {
14019+
report(TL->getBeginLoc(), Diagnostics::TRY_EXPERIMENTAL_FEATURE, false,
14020+
TypeStr, "--use-experimental-features=virtual_memory");
14021+
return;
14022+
}
14023+
if (!DpctGlobalInfo::isInAnalysisScope(
14024+
SM.getSpellingLoc(TL->getBeginLoc()))) {
14025+
return;
14026+
}
14027+
auto Range = getDefinitionRange(TL->getBeginLoc(), TL->getEndLoc());
14028+
auto BeginLoc = Range.getBegin();
14029+
auto EndLoc = Range.getEnd();
14030+
14031+
if (SM.isWrittenInScratchSpace(SM.getSpellingLoc(TL->getBeginLoc()))) {
14032+
BeginLoc = SM.getExpansionRange(TL->getBeginLoc()).getBegin();
14033+
EndLoc = SM.getExpansionRange(TL->getBeginLoc()).getEnd();
14034+
}
14035+
std::string Str =
14036+
MapNames::findReplacedName(MapNames::TypeNamesMap, TypeStr);
14037+
if (!Str.empty()) {
14038+
auto Len = Lexer::MeasureTokenLength(
14039+
EndLoc, SM, DpctGlobalInfo::getContext().getLangOpts());
14040+
Len += SM.getDecomposedLoc(EndLoc).second -
14041+
SM.getDecomposedLoc(BeginLoc).second;
14042+
emplaceTransformation(new ReplaceText(BeginLoc, Len, std::move(Str)));
14043+
return;
14044+
}
14045+
}
14046+
if (auto *E = getNodeAsType<DeclRefExpr>(Result, "vmEnum")) {
14047+
std::string EnumName = E->getNameInfo().getName().getAsString();
14048+
if (!DpctGlobalInfo::useExpVirtualMemory()) {
14049+
report(E->getBeginLoc(), Diagnostics::TRY_EXPERIMENTAL_FEATURE, false,
14050+
EnumName, "--use-experimental-features=virtual_memory");
14051+
return;
14052+
}
14053+
auto Search = EnumConstantRule::EnumNamesMap.find(EnumName);
14054+
if (Search == EnumConstantRule::EnumNamesMap.end()) {
14055+
report(E->getBeginLoc(), Diagnostics::API_NOT_MIGRATED, false, EnumName);
14056+
return;
14057+
}
14058+
emplaceTransformation(new ReplaceStmt(E, Search->second->NewName));
14059+
}
14060+
}
14061+
14062+
REGISTER_RULE(VirtualMemRule, PassKind::PK_Migration)
14063+
1397414064
void DriverModuleAPIRule::registerMatcher(ast_matchers::MatchFinder &MF) {
1397514065
auto DriverModuleAPI = [&]() {
1397614066
return hasAnyName("cuModuleLoad", "cuModuleLoadData", "cuModuleLoadDataEx",

clang/lib/DPCT/ASTTraversal.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,6 +1390,12 @@ class DriverModuleAPIRule : public NamedMigrationRule<DriverModuleAPIRule> {
13901390
void runRule(const ast_matchers::MatchFinder::MatchResult &Result);
13911391
};
13921392

1393+
class VirtualMemRule : public NamedMigrationRule<VirtualMemRule> {
1394+
public:
1395+
void registerMatcher(ast_matchers::MatchFinder &MF) override;
1396+
void runRule(const ast_matchers::MatchFinder::MatchResult &Result);
1397+
};
1398+
13931399
class DriverDeviceAPIRule : public NamedMigrationRule<DriverDeviceAPIRule> {
13941400
public:
13951401
void registerMatcher(ast_matchers::MatchFinder &MF) override;

clang/lib/DPCT/AnalysisInfo.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,6 +1302,9 @@ class DpctGlobalInfo {
13021302
static bool useExpDeviceGlobal() {
13031303
return getUsingExperimental<ExperimentalFeatures::Exp_DeviceGlobal>();
13041304
}
1305+
static bool useExpVirtualMemory() {
1306+
return getUsingExperimental<ExperimentalFeatures::Exp_VirtualMemory>();
1307+
}
13051308
static bool useExpNonStandardSYCLBuiltins() {
13061309
return getUsingExperimental<
13071310
ExperimentalFeatures::Exp_NonStandardSYCLBuiltins>();

clang/lib/DPCT/CallExprRewriterCommon.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1716,6 +1716,10 @@ inline auto UseSYCLCompat = [](const CallExpr *C) -> bool {
17161716
return DpctGlobalInfo::useSYCLCompat();
17171717
};
17181718

1719+
inline auto UseExpVirtualMemory = [](const CallExpr *C) -> bool {
1720+
return DpctGlobalInfo::useExpVirtualMemory();
1721+
};
1722+
17191723
class CheckDerefedTypeBeforeCast {
17201724
unsigned Idx;
17211725
std::string TypeName;

0 commit comments

Comments
 (0)