Skip to content

Commit a558dae

Browse files
authored
[SYCLomatic] Add migration for 2 virtual memory related enums by removing them(#2764)
Signed-off-by: Jiang, Zhiwei <zhiwei.jiang@intel.com>
1 parent 31692a8 commit a558dae

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

clang/lib/DPCT/RuleInfra/MapNames.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,6 +1145,8 @@ void MapNames::setExplicitNamespaceMap(
11451145
{"CU_MEM_ADVISE_UNSET_ACCESSED_BY", std::make_shared<EnumNameRule>("0")},
11461146
{"CU_MEM_ALLOCATION_TYPE_PINNED", std::make_shared<EnumNameRule>("0")},
11471147
{"CU_MEM_ALLOCATION_TYPE_INVALID", std::make_shared<EnumNameRule>("1")},
1148+
{"CU_MEM_ALLOCATION_COMP_NONE", std::make_shared<EnumNameRule>("0")},
1149+
{"CU_MEM_ALLOCATION_COMP_GENERIC", std::make_shared<EnumNameRule>("1")},
11481150
{"CU_MEM_ALLOCATION_TYPE_MAX",
11491151
std::make_shared<EnumNameRule>("0xFFFFFFFF")},
11501152
{"CU_MEM_LOCATION_TYPE_DEVICE", std::make_shared<EnumNameRule>("1")},

clang/lib/DPCT/RulesLang/RulesLang.cpp

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8167,7 +8167,8 @@ void VirtualMemRule::registerMatcher(ast_matchers::MatchFinder &MF) {
81678167
"CU_MEM_ACCESS_FLAGS_PROT_NONE", "CU_MEM_ACCESS_FLAGS_PROT_READ",
81688168
"CU_MEM_ACCESS_FLAGS_PROT_READWRITE",
81698169
"CU_MEM_ALLOC_GRANULARITY_RECOMMENDED",
8170-
"CU_MEM_ALLOC_GRANULARITY_MINIMUM");
8170+
"CU_MEM_ALLOC_GRANULARITY_MINIMUM", "CU_MEM_ALLOCATION_COMP_NONE",
8171+
"CU_MEM_ALLOCATION_COMP_GENERIC");
81718172
};
81728173
MF.addMatcher(
81738174
callExpr(callee(functionDecl(virtualmemoryAPI()))).bind("vmCall"), this);
@@ -8178,11 +8179,47 @@ void VirtualMemRule::registerMatcher(ast_matchers::MatchFinder &MF) {
81788179
MF.addMatcher(
81798180
declRefExpr(to(enumConstantDecl(virtualmemoryEnum()))).bind("vmEnum"),
81808181
this);
8182+
MF.addMatcher(
8183+
memberExpr(
8184+
hasObjectExpression(hasType(qualType(
8185+
hasCanonicalType(asString("struct CUmemAllocationProp_st"))))),
8186+
member(hasName("allocFlags")),
8187+
hasParent(memberExpr(anyOf(
8188+
hasParent(implicitCastExpr(
8189+
hasCastKind(CK_LValueToRValue),
8190+
hasParent(binaryOperator(isAssignmentOperator())))
8191+
.bind("replaceWithZero")),
8192+
hasParent(
8193+
binaryOperator(isAssignmentOperator()).bind("removeBO")))))),
8194+
this);
81818195
}
81828196

81838197
void VirtualMemRule::runRule(
81848198
const ast_matchers::MatchFinder::MatchResult &Result) {
81858199
auto &SM = DpctGlobalInfo::getSourceManager();
8200+
8201+
const Expr *ICE = getNodeAsType<ImplicitCastExpr>(Result, "replaceWithZero");
8202+
const Expr *BO = getNodeAsType<BinaryOperator>(Result, "removeBO");
8203+
const Expr *E = ICE ? ICE : BO;
8204+
if (E) {
8205+
// Process pattern like:
8206+
// prop.allocFlags.compressionType = CU_MEM_ALLOCATION_COMP_NONE;
8207+
// uc = prop.allocFlags.compressionType;
8208+
auto Range = getDefinitionRange(E->getBeginLoc(), E->getEndLoc());
8209+
auto Begin = Range.getBegin();
8210+
auto End = Range.getEnd();
8211+
auto Length = Lexer::MeasureTokenLength(
8212+
End, SM, dpct::DpctGlobalInfo::getContext().getLangOpts());
8213+
Length +=
8214+
SM.getDecomposedLoc(End).second - SM.getDecomposedLoc(Begin).second;
8215+
if (ICE) {
8216+
emplaceTransformation(new ReplaceText(Begin, Length, std::move("0")));
8217+
} else {
8218+
emplaceTransformation(
8219+
new ReplaceText(Begin, Length, std::move("(void)0")));
8220+
}
8221+
return;
8222+
}
81868223
if (const CallExpr *CE = getNodeAsType<CallExpr>(Result, "vmCall")) {
81878224
ExprAnalysis EA(CE);
81888225
emplaceTransformation(EA.getReplacement());

clang/test/dpct/virtual_memory.cu

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,20 @@ int main() {
1313
cuDeviceGet(&device, 0);
1414
CUcontext context;
1515
cuCtxCreate(&context, 0, device);
16+
unsigned char uc;
1617

1718
// CHECK: dpct::experimental::mem_prop prop = {};
1819
// CHECK: prop.type = 0;
1920
// CHECK: prop.location.type = 1;
2021
// CHECK: prop.location.id = device;
22+
// CHECK: (void)0;
23+
// CHECK: uc = 0;
2124
CUmemAllocationProp prop = {};
2225
prop.type = CU_MEM_ALLOCATION_TYPE_PINNED;
2326
prop.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
2427
prop.location.id = device;
28+
prop.allocFlags.compressionType = CU_MEM_ALLOCATION_COMP_NONE;
29+
uc = prop.allocFlags.compressionType;
2530
size_t granularity;
2631
// CHECK: granularity = sycl::ext::oneapi::experimental::get_mem_granularity(dpct::get_device(prop.location.id), dpct::get_device(prop.location.id).get_context(), sycl::ext::oneapi::experimental::granularity_mode::minimum);
2732
cuMemGetAllocationGranularity(&granularity, &prop, CU_MEM_ALLOC_GRANULARITY_MINIMUM);

0 commit comments

Comments
 (0)