Skip to content

Commit d358082

Browse files
authored
[SYCLomatic] Migrate cudaMallocPitch with rewriter (#753)
Signed-off-by: Huang, Andy <[email protected]>
1 parent dc1b8cf commit d358082

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed

clang/lib/DPCT/APINamesMemory.inc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,3 +362,20 @@ ASSIGNABLE_FACTORY(
362362
HelperFeatureEnum::Memory_pointer_attributes,
363363
MEMBER_CALL_FACTORY_ENTRY(
364364
"cudaPointerGetAttributes", DEREF(0), false, "init", ARG(1))))
365+
366+
ASSIGNABLE_FACTORY(FEATURE_REQUEST_FACTORY(
367+
HelperFeatureEnum::Memory_dpct_malloc_2d,
368+
CONDITIONAL_FACTORY_ENTRY(
369+
CheckDerefedTypeBeforeCast(0, "void *"),
370+
ASSIGN_FACTORY_ENTRY(
371+
"cudaMallocPitch", DEREF(makeDerefArgCreatorWithCall(0)),
372+
CALL(MapNames::getDpctNamespace() + "dpct_malloc",
373+
DEREF(makeCallArgCreatorWithCall(1)),
374+
makeCallArgCreatorWithCall(2), makeDerefArgCreatorWithCall(3))),
375+
ASSIGN_FACTORY_ENTRY(
376+
"cudaMallocPitch", DEREF(makeDerefArgCreatorWithCall(0)),
377+
CAST(getDerefedType(0),
378+
CALL(MapNames::getDpctNamespace() + "dpct_malloc",
379+
DEREF(makeCallArgCreatorWithCall(1)),
380+
makeCallArgCreatorWithCall(2),
381+
makeCallArgCreatorWithCall(3)))))))

clang/lib/DPCT/ASTTraversal.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10332,7 +10332,7 @@ void MemoryMigrationRule::mallocMigration(
1033210332
}
1033310333
} else if (Name == "cudaHostAlloc" || Name == "cudaMallocHost" ||
1033410334
Name == "cuMemHostAlloc" || Name == "cuMemAllocHost_v2" ||
10335-
Name == "cuMemAllocPitch_v2") {
10335+
Name == "cuMemAllocPitch_v2" || Name == "cudaMallocPitch") {
1033610336
ExprAnalysis EA(C);
1033710337
emplaceTransformation(EA.getReplacement());
1033810338
EA.applyAllSubExprRepl();
@@ -10401,7 +10401,7 @@ void MemoryMigrationRule::mallocMigration(
1040110401
report(C->getBeginLoc(), Diagnostics::NOERROR_RETURN_COMMA_OP, false);
1040210402
}
1040310403
}
10404-
} else if (Name == "cudaMallocPitch" || Name == "cudaMalloc3D") {
10404+
} else if (Name == "cudaMalloc3D") {
1040510405
std::ostringstream OS;
1040610406
std::string Type;
1040710407
if (IsAssigned)
@@ -10420,9 +10420,6 @@ void MemoryMigrationRule::mallocMigration(
1042010420
emplaceTransformation(removeArg(C, 0, *Result.SourceManager));
1042110421
std::ostringstream OS2;
1042210422
printDerefOp(OS2, C->getArg(1));
10423-
if (Name == "cudaMallocPitch") {
10424-
emplaceTransformation(new ReplaceStmt(C->getArg(1), OS2.str()));
10425-
}
1042610423
if (IsAssigned) {
1042710424
emplaceTransformation(new InsertAfterStmt(C, ", 0)"));
1042810425
report(C->getBeginLoc(), Diagnostics::NOERROR_RETURN_COMMA_OP, false);

clang/lib/DPCT/CallExprRewriter.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,7 @@ class DerefExpr {
636636
if (!AddrOfRemoved && !IgnoreDerefOp)
637637
Stream << "*";
638638

639+
AA.setCallSpelling(C);
639640
printWithParens(Stream, AA, P);
640641
}
641642

@@ -644,7 +645,7 @@ class DerefExpr {
644645
public:
645646
template <class StreamT>
646647
void printArg(StreamT &Stream, ArgumentAnalysis &A) const {
647-
print(Stream, A, false);
648+
print(Stream);
648649
}
649650
template <class StreamT> void printMemberBase(StreamT &Stream) const {
650651
ExprAnalysis EA;
@@ -658,6 +659,7 @@ class DerefExpr {
658659
print(Stream, EA, false);
659660
} else {
660661
ArgumentAnalysis AA;
662+
661663
std::pair<const CallExpr*, const Expr*> ExprPair(C, E);
662664
print(Stream, AA, false, ExprPair);
663665
}

clang/test/dpct/memory_management.cu

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,8 @@ void checkError(cudaError_t err) {
195195

196196
void cuCheckError(CUresult err) {
197197
}
198+
//CHECK: #define PITCH(a,b,c,d) a = (float *)dpct::dpct_malloc(b, c, d);
199+
#define PITCH(a,b,c,d) cudaMallocPitch(a, b, c, d);
198200

199201
void testCommas() {
200202
size_t size = 1234567 * sizeof(float);
@@ -219,10 +221,13 @@ void testCommas() {
219221

220222
// CHECK: d_A = (float *)dpct::dpct_malloc(size, size, size);
221223
cudaMallocPitch((void **)&d_A, &size, size, size);
224+
225+
// CHECK: PITCH((void **)&d_A, &size, size, size);
226+
PITCH((void **)&d_A, &size, size, size);
222227
int sz;
223-
// CHECK: d_A = (float *)dpct::dpct_malloc(*((size_t *)&size), size, size);
228+
// CHECK: d_A = (float *)dpct::dpct_malloc(*(size_t *)&size, size, size);
224229
cudaMallocPitch((void **)&d_A, (size_t *)&size, size, size);
225-
// CHECK: d_A = (float *)dpct::dpct_malloc(*((size_t *)&sz), size, size);
230+
// CHECK: d_A = (float *)dpct::dpct_malloc(*(size_t *)&sz, size, size);
226231
cudaMallocPitch((void **)&d_A, (size_t *)&sz, size, size);
227232
// CHECK:/*
228233
// CHECK-NEXT:DPCT1003:{{[0-9]+}}: Migrated API does not return error code. (*, 0) is inserted. You may need to rewrite this code.

0 commit comments

Comments
 (0)