Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 29 additions & 27 deletions clang/lib/DPCT/APINamesMemory.inc
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,13 @@ ASSIGNABLE_FACTORY(CONDITIONAL_FACTORY_ENTRY(
"cuMemcpyDtoD_v2", CALL(MapNames::getDpctNamespace() + "dpct_memcpy",
ARG(0), ARG(1), ARG(2), ARG(MapNames::getDpctNamespace() + "automatic"))))))

ASSIGNABLE_FACTORY(
FEATURE_REQUEST_FACTORY(HelperFeatureEnum::device_ext,
ASSIGNABLE_FACTORY(FEATURE_REQUEST_FACTORY(
HelperFeatureEnum::device_ext,
ASSIGN_FACTORY_ENTRY(
"cuMemAllocPitch_v2", DEREF(makeDerefArgCreatorWithCall(0)),
CAST(getDerefedType(0), CALL(MapNames::getDpctNamespace() + "dpct_malloc", DEREF(makeCallArgCreatorWithCall(1)), ARG(2), ARG(3))))))
CAST(getDerefedType(0),
CALL(getMemoryHelperFunctionName("malloc"),
DEREF(makeCallArgCreatorWithCall(1)), ARG(2), ARG(3))))))

ASSIGNABLE_FACTORY(CONDITIONAL_FACTORY_ENTRY(
makeCheckNot(CheckArgIsDefaultCudaStream(3)),
Expand All @@ -264,7 +266,7 @@ ASSIGNABLE_FACTORY(CONDITIONAL_FACTORY_ENTRY(
MEMBER_CALL(CALL(MapNames::getDpctNamespace() + "get_device",
ARG(2)),
false, DpctGlobalInfo::getDeviceQueueName()),
false, "prefetch", ARG(0), ARG(1)))))
DpctGlobalInfo::useSYCLCompat(), "prefetch", ARG(0), ARG(1)))))

ASSIGNABLE_FACTORY(CONDITIONAL_FACTORY_ENTRY(
checkArgSpelling(3, "CU_DEVICE_CPU"),
Expand All @@ -279,7 +281,7 @@ ASSIGNABLE_FACTORY(CONDITIONAL_FACTORY_ENTRY(
MEMBER_CALL(CALL(MapNames::getDpctNamespace() +
"cpu_device"),
false, DpctGlobalInfo::getDeviceQueueName()),
false, "mem_advise", ARG(0), ARG(1), ARG("0"))),
DpctGlobalInfo::useSYCLCompat(), "mem_advise", ARG(0), ARG(1), ARG("0"))),
Diagnostics::DEFAULT_MEM_ADVICE, ARG(" and was set to 0")),

FEATURE_REQUEST_FACTORY(
Expand All @@ -288,7 +290,7 @@ ASSIGNABLE_FACTORY(CONDITIONAL_FACTORY_ENTRY(
"cuMemAdvise",
MEMBER_CALL(CALL(MapNames::getDpctNamespace() + "cpu_device"),
false, DpctGlobalInfo::getDeviceQueueName()),
false, "mem_advise", ARG(0), ARG(1), ARG(2)))),
DpctGlobalInfo::useSYCLCompat(), "mem_advise", ARG(0), ARG(1), ARG(2)))),

CONDITIONAL_FACTORY_ENTRY(
checkIsArgIntegerLiteral(2),
Expand All @@ -302,7 +304,7 @@ ASSIGNABLE_FACTORY(CONDITIONAL_FACTORY_ENTRY(
"get_device",
ARG(3)),
false, DpctGlobalInfo::getDeviceQueueName()),
false, "mem_advise", ARG(0), ARG(1), ARG("0"))),
DpctGlobalInfo::useSYCLCompat(), "mem_advise", ARG(0), ARG(1), ARG("0"))),
Diagnostics::DEFAULT_MEM_ADVICE, ARG(" and was set to 0")),
FEATURE_REQUEST_FACTORY(
HelperFeatureEnum::device_ext,
Expand All @@ -311,7 +313,7 @@ ASSIGNABLE_FACTORY(CONDITIONAL_FACTORY_ENTRY(
MEMBER_CALL(CALL(MapNames::getDpctNamespace() + "get_device",
ARG(3)),
false, DpctGlobalInfo::getDeviceQueueName()),
false, "mem_advise", ARG(0), ARG(1), ARG(2))))))
DpctGlobalInfo::useSYCLCompat(), "mem_advise", ARG(0), ARG(1), ARG(2))))))

ASSIGNABLE_FACTORY(CONDITIONAL_FACTORY_ENTRY(
checkIsUSM(),
Expand Down Expand Up @@ -426,16 +428,16 @@ ASSIGNABLE_FACTORY(FEATURE_REQUEST_FACTORY(
CheckDerefedTypeBeforeCast(0, "void *"),
ASSIGN_FACTORY_ENTRY(
"cudaMallocPitch", DEREF(makeDerefArgCreatorWithCall(0)),
CALL(MapNames::getDpctNamespace() + "dpct_malloc",
CALL(getMemoryHelperFunctionName("malloc"),
DEREF(makeCallArgCreatorWithCall(1)),
makeCallArgCreatorWithCall(2), makeDerefArgCreatorWithCall(3))),
makeCallArgCreatorWithCall(2),
makeDerefArgCreatorWithCall(3))),
ASSIGN_FACTORY_ENTRY(
"cudaMallocPitch", DEREF(makeDerefArgCreatorWithCall(0)),
CAST(getDerefedType(0),
CALL(MapNames::getDpctNamespace() + "dpct_malloc",
DEREF(makeCallArgCreatorWithCall(1)),
makeCallArgCreatorWithCall(2),
makeCallArgCreatorWithCall(3)))))))
CAST(getDerefedType(0), CALL(getMemoryHelperFunctionName("malloc"),
DEREF(makeCallArgCreatorWithCall(1)),
makeCallArgCreatorWithCall(2),
makeCallArgCreatorWithCall(3)))))))

ASSIGNABLE_FACTORY(FEATURE_REQUEST_FACTORY(
HelperFeatureEnum::device_ext,
Expand Down Expand Up @@ -621,78 +623,78 @@ ASSIGNABLE_FACTORY(FEATURE_REQUEST_FACTORY(
ASSIGNABLE_FACTORY(FEATURE_REQUEST_FACTORY(
HelperFeatureEnum::device_ext,
CALL_FACTORY_ENTRY("cuMemsetD8_v2",
CALL(MapNames::getDpctNamespace() + "dpct_memset",
CALL(getMemoryHelperFunctionName("memset"),
ARG(0), ARG(1), ARG(2)))))

ASSIGNABLE_FACTORY(FEATURE_REQUEST_FACTORY(
HelperFeatureEnum::device_ext,
CALL_FACTORY_ENTRY("cuMemsetD16_v2",
CALL(MapNames::getDpctNamespace() + "dpct_memset_d16",
CALL(getMemoryHelperFunctionName("memset_d16"),
ARG(0), ARG(1), ARG(2)))))

ASSIGNABLE_FACTORY(FEATURE_REQUEST_FACTORY(
HelperFeatureEnum::device_ext,
CALL_FACTORY_ENTRY("cuMemsetD32_v2",
CALL(MapNames::getDpctNamespace() + "dpct_memset_d32",
CALL(getMemoryHelperFunctionName("memset_d32"),
ARG(0), ARG(1), ARG(2)))))

ASSIGNABLE_FACTORY(FEATURE_REQUEST_FACTORY(
HelperFeatureEnum::device_ext,
CALL_FACTORY_ENTRY("cuMemsetD8Async",
CALL(MapNames::getDpctNamespace() + "async_dpct_memset",
CALL(getMemoryHelperFunctionName("memset_async"),
ARG(0), ARG(1), ARG(2),
DEREF(makeCallArgCreatorWithCall(3))))))

ASSIGNABLE_FACTORY(FEATURE_REQUEST_FACTORY(
HelperFeatureEnum::device_ext,
CALL_FACTORY_ENTRY("cuMemsetD16Async",
CALL(MapNames::getDpctNamespace() + "async_dpct_memset_d16",
CALL(getMemoryHelperFunctionName("memset_d16_async"),
ARG(0), ARG(1), ARG(2),
DEREF(makeCallArgCreatorWithCall(3))))))

ASSIGNABLE_FACTORY(FEATURE_REQUEST_FACTORY(
HelperFeatureEnum::device_ext,
CALL_FACTORY_ENTRY("cuMemsetD32Async",
CALL(MapNames::getDpctNamespace() + "async_dpct_memset_d32",
CALL(getMemoryHelperFunctionName("memset_d32_async"),
ARG(0), ARG(1), ARG(2),
DEREF(makeCallArgCreatorWithCall(3))))))

ASSIGNABLE_FACTORY(FEATURE_REQUEST_FACTORY(
HelperFeatureEnum::device_ext,
CALL_FACTORY_ENTRY("cuMemsetD2D8_v2",
CALL(MapNames::getDpctNamespace() + "dpct_memset",
CALL(getMemoryHelperFunctionName("memset"),
ARG(0), ARG(1), ARG(2), ARG(3), ARG(4)))))

ASSIGNABLE_FACTORY(FEATURE_REQUEST_FACTORY(
HelperFeatureEnum::device_ext,
CALL_FACTORY_ENTRY("cuMemsetD2D16_v2",
CALL(MapNames::getDpctNamespace() + "dpct_memset_d16",
CALL(getMemoryHelperFunctionName("memset_d16"),
ARG(0), ARG(1), ARG(2), ARG(3), ARG(4)))))

ASSIGNABLE_FACTORY(FEATURE_REQUEST_FACTORY(
HelperFeatureEnum::device_ext,
CALL_FACTORY_ENTRY("cuMemsetD2D32_v2",
CALL(MapNames::getDpctNamespace() + "dpct_memset_d32",
CALL(getMemoryHelperFunctionName("memset_d32"),
ARG(0), ARG(1), ARG(2), ARG(3), ARG(4)))))

ASSIGNABLE_FACTORY(FEATURE_REQUEST_FACTORY(
HelperFeatureEnum::device_ext,
CALL_FACTORY_ENTRY("cuMemsetD2D8Async",
CALL(MapNames::getDpctNamespace() + "async_dpct_memset",
CALL(getMemoryHelperFunctionName("memset_async"),
ARG(0), ARG(1), ARG(2), ARG(3), ARG(4),
DEREF(makeCallArgCreatorWithCall(5))))))

ASSIGNABLE_FACTORY(FEATURE_REQUEST_FACTORY(
HelperFeatureEnum::device_ext,
CALL_FACTORY_ENTRY("cuMemsetD2D16Async",
CALL(MapNames::getDpctNamespace() + "async_dpct_memset_d16",
CALL(getMemoryHelperFunctionName("memset_d16_async"),
ARG(0), ARG(1), ARG(2), ARG(3), ARG(4),
DEREF(makeCallArgCreatorWithCall(5))))))

ASSIGNABLE_FACTORY(FEATURE_REQUEST_FACTORY(
HelperFeatureEnum::device_ext,
CALL_FACTORY_ENTRY("cuMemsetD2D32Async",
CALL(MapNames::getDpctNamespace() + "async_dpct_memset_d32",
CALL(getMemoryHelperFunctionName("memset_d32_async"),
ARG(0), ARG(1), ARG(2), ARG(3), ARG(4),
DEREF(makeCallArgCreatorWithCall(5))))))

Expand Down
74 changes: 47 additions & 27 deletions clang/lib/DPCT/ASTTraversal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4503,14 +4503,15 @@ void BLASFunctionCallRule::runRule(const MatchFinder::MatchResult &Result) {
" = " + MapNames::getClNamespace() +
"malloc_shared<int64_t>(" + "1, " + DefaultQueue +
");" + getNL() + IndentStr;
SuffixInsertStr = SuffixInsertStr + getNL() + IndentStr + "int " +
ResultTempHost + " = (int)*" + ResultTempPtr +
";" + getNL() + IndentStr +
MapNames::getDpctNamespace() + "dpct_memcpy(" +
ExprAnalysis::ref(CE->getArg(i)) + ", &" +
ResultTempHost + ", sizeof(int));" + getNL() +
IndentStr + MapNames::getClNamespace() + "free(" +
ResultTempPtr + ", " + DefaultQueue + ");";
SuffixInsertStr =
SuffixInsertStr + getNL() + IndentStr + "int " +
ResultTempHost + " = (int)*" + ResultTempPtr + ";" + getNL() +
IndentStr +
MemoryMigrationRule::getMemoryHelperFunctionName("memcpy") +
"(" + ExprAnalysis::ref(CE->getArg(i)) + ", &" +
ResultTempHost + ", sizeof(int));" + getNL() + IndentStr +
MapNames::getClNamespace() + "free(" + ResultTempPtr + ", " +
DefaultQueue + ");";
CurrentArgumentRepl = ResultTempPtr;
} else {
CurrentArgumentRepl = ExprAnalysis::ref(CE->getArg(i));
Expand Down Expand Up @@ -4640,14 +4641,15 @@ void BLASFunctionCallRule::runRule(const MatchFinder::MatchResult &Result) {
" = " + MapNames::getClNamespace() +
"malloc_shared<int64_t>(" + "1, " + DefaultQueue +
");" + getNL() + IndentStr;
SuffixInsertStr = SuffixInsertStr + getNL() + IndentStr + "int " +
ResultTempHost + " = (int)*" + ResultTempPtr +
";" + getNL() + IndentStr +
MapNames::getDpctNamespace() + "dpct_memcpy(" +
ExprAnalysis::ref(CE->getArg(i)) + ", &" +
ResultTempHost + ", sizeof(int));" + getNL() +
IndentStr + MapNames::getClNamespace() + "free(" +
ResultTempPtr + ", " + DefaultQueue + ");";
SuffixInsertStr =
SuffixInsertStr + getNL() + IndentStr + "int " +
ResultTempHost + " = (int)*" + ResultTempPtr + ";" + getNL() +
IndentStr +
MemoryMigrationRule::getMemoryHelperFunctionName("memcpy") +
"(" + ExprAnalysis::ref(CE->getArg(i)) + ", &" +
ResultTempHost + ", sizeof(int));" + getNL() + IndentStr +
MapNames::getClNamespace() + "free(" + ResultTempPtr + ", " +
DefaultQueue + ");";
CurrentArgumentRepl = ResultTempPtr;
} else if (ReplInfo.BufferTypeInfo[IndexTemp] ==
"std::complex<float>" ||
Expand Down Expand Up @@ -5704,7 +5706,7 @@ void SOLVERFunctionCallRule::runRule(const MatchFinder::MatchResult &Result) {
if (HasDeviceAttr) {
report(CE->getBeginLoc(), Diagnostics::FUNCTION_CALL_IN_DEVICE, false,
MapNames::ITFName.at(FuncName),
MapNames::getDpctNamespace() + "dpct_memcpy");
MemoryMigrationRule::getMemoryHelperFunctionName("memcpy"));
return;
}

Expand Down Expand Up @@ -10162,7 +10164,7 @@ void MemoryMigrationRule::mallocMigration(
requestFeature(HelperFeatureEnum::device_ext);
emplaceTransformation(new InsertBeforeStmt(C, OS.str()));
emplaceTransformation(
new ReplaceCalleeName(C, MapNames::getDpctNamespace() + "dpct_malloc"));
new ReplaceCalleeName(C, MemoryMigrationRule::getMemoryHelperFunctionName("malloc")));
emplaceTransformation(removeArg(C, 0, *Result.SourceManager));
std::ostringstream OS2;
printDerefOp(OS2, C->getArg(1));
Expand Down Expand Up @@ -10248,6 +10250,8 @@ void MemoryMigrationRule::memcpyMigration(
// Detect if there is Async in the func name and crop the async substr
std::string NameRef = Name;
bool IsAsync = false;
// Whether in experimental namespace in syclcompat.
bool IsExperimentalInSYCLCompat = false;
size_t AsyncLoc = NameRef.find("Async");
if (AsyncLoc != std::string::npos) {
IsAsync = true;
Expand All @@ -10266,6 +10270,7 @@ void MemoryMigrationRule::memcpyMigration(
llvm::raw_string_ostream OS(Replacement);
DerefExpr(C->getArg(0), C).print(OS);
emplaceTransformation(new ReplaceStmt(C->getArg(0), Replacement));
IsExperimentalInSYCLCompat = true;
} else if (!NameRef.compare("cudaMemcpy") ||
NameRef.rfind("cuMemcpyDtoH", 0) == 0) {
if (!NameRef.compare("cudaMemcpy")) {
Expand Down Expand Up @@ -10343,10 +10348,12 @@ void MemoryMigrationRule::memcpyMigration(

if (ReplaceStr.empty()) {
if (IsAsync) {
ReplaceStr = MapNames::getDpctNamespace() + "async_dpct_memcpy";
ReplaceStr = MemoryMigrationRule::getMemoryHelperFunctionName(
"memcpy_async", IsExperimentalInSYCLCompat);
requestFeature(HelperFeatureEnum::device_ext);
} else {
ReplaceStr = MapNames::getDpctNamespace() + "dpct_memcpy";
ReplaceStr = MemoryMigrationRule::getMemoryHelperFunctionName(
"memcpy", IsExperimentalInSYCLCompat);
requestFeature(HelperFeatureEnum::device_ext);
}
}
Expand All @@ -10365,6 +10372,15 @@ void MemoryMigrationRule::memcpyMigration(
void MemoryMigrationRule::arrayMigration(
const ast_matchers::MatchFinder::MatchResult &Result, const CallExpr *C,
const UnresolvedLookupExpr *ULExpr, bool IsAssigned) {
if (DpctGlobalInfo::useSYCLCompat()) {
ExprAnalysis EA;
if (ULExpr)
EA.analyze(ULExpr);
else
EA.analyze(C);
emplaceTransformation(EA.getReplacement());
return;
}
std::string Name;
if (ULExpr) {
Name = ULExpr->getName().getAsString();
Expand Down Expand Up @@ -10757,10 +10773,10 @@ void MemoryMigrationRule::memsetMigration(
bool IsAsync = NameRef.ends_with("Async");
if (IsAsync) {
NameRef = NameRef.drop_back(5 /* len of "Async" */);
ReplaceStr = MapNames::getDpctNamespace() + "async_dpct_memset";
ReplaceStr = MemoryMigrationRule::getMemoryHelperFunctionName("memset_async");
requestFeature(HelperFeatureEnum::device_ext);
} else {
ReplaceStr = MapNames::getDpctNamespace() + "dpct_memset";
ReplaceStr = MemoryMigrationRule::getMemoryHelperFunctionName("memset");
requestFeature(HelperFeatureEnum::device_ext);
}

Expand Down Expand Up @@ -11456,6 +11472,10 @@ void MemoryMigrationRule::aggregate3DVectorClassCtor(
}

void MemoryMigrationRule::handleDirection(const CallExpr *C, unsigned i) {
if (DpctGlobalInfo::useSYCLCompat()) {
emplaceTransformation(removeArg(C, i, DpctGlobalInfo::getSourceManager()));
return;
}
if (C->getNumArgs() > i && !C->getArg(i)->isDefaultArgument()) {
if (auto DRE = dyn_cast<DeclRefExpr>(C->getArg(i))) {
if (auto Enum = dyn_cast<EnumConstantDecl>(DRE->getDecl())) {
Expand Down Expand Up @@ -13112,11 +13132,11 @@ void TextureRule::registerMatcher(MatchFinder &MF) {
"cudaTextureDesc", "cudaResourceDesc", "cudaResourceType",
"cudaTextureAddressMode", "cudaTextureFilterMode", "cudaArray",
"cudaArray_t", "CUarray_st", "CUarray", "CUarray_format",
"CUarray_format_enum", "CUdeviceptr", "CUresourcetype",
"CUresourcetype_enum", "CUaddress_mode", "CUaddress_mode_enum",
"CUfilter_mode", "CUfilter_mode_enum", "CUDA_RESOURCE_DESC",
"CUDA_TEXTURE_DESC", "CUtexref", "textureReference",
"cudaMipmappedArray", "cudaMipmappedArray_t"))))))
"CUarray_format_enum", "CUresourcetype", "CUresourcetype_enum",
"CUaddress_mode", "CUaddress_mode_enum", "CUfilter_mode",
"CUfilter_mode_enum", "CUDA_RESOURCE_DESC", "CUDA_TEXTURE_DESC",
"CUtexref", "textureReference", "cudaMipmappedArray",
"cudaMipmappedArray_t"))))))
.bind("texType"),
this);

Expand Down
10 changes: 10 additions & 0 deletions clang/lib/DPCT/ASTTraversal.h
Original file line number Diff line number Diff line change
Expand Up @@ -1344,6 +1344,16 @@ class MemoryMigrationRule : public NamedMigrationRule<MemoryMigrationRule> {
void registerMatcher(ast_matchers::MatchFinder &MF) override;
void runRule(const ast_matchers::MatchFinder::MatchResult &Result);

/// Get helper function name with namespace which has 'dpct_' in dpct helper
/// functions and w/o in syclcompat.
/// If has "_async" suffix, the name in dpct helper function will have
/// 'async_' prefix and remove the suffix.
/// If `ExperimentalInSYCLCompat` is true, will add `experimental` namespace
/// in syclcompat.
static std::string
getMemoryHelperFunctionName(StringRef RawName,
bool ExperimentalInSYCLCompat = false);

private:
void mallocMigration(const ast_matchers::MatchFinder::MatchResult &Result,
const CallExpr *C,
Expand Down
32 changes: 32 additions & 0 deletions clang/lib/DPCT/CallExprRewriterMemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,38 @@
namespace clang {
namespace dpct {

/// Get helper function name with namespace which has 'dpct_' in dpct helper
/// functions and w/o in syclcompat.
/// If has "_async" suffix, the name in dpct helper function will have 'async_'
/// prefix and remove the suffix.
/// If `ExperimentalInSYCLCompat` is true, will add `experimental` namespace
/// in syclcompat.
std::string getMemoryHelperFunctionName(StringRef RawName,
bool ExperimentalInSYCLCompat = false) {
const static std::string AsyncSuffix = "_async";
const static std::string AsyncPrefix = "async_";

std::string Result;
llvm::raw_string_ostream OS(Result);
OS << MapNames::getDpctNamespace();
if (!DpctGlobalInfo::useSYCLCompat()) {
if (RawName.ends_with(AsyncSuffix)) {
RawName = RawName.drop_back(AsyncSuffix.length());
OS << AsyncPrefix;
}
OS << "dpct_";
} else if (ExperimentalInSYCLCompat) {
OS << "experimental::";
}
OS << RawName;
return Result;
}

std::string MemoryMigrationRule::getMemoryHelperFunctionName(
StringRef Name, bool ExperimentalInSYCLCompat) {
return dpct::getMemoryHelperFunctionName(Name, ExperimentalInSYCLCompat);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add lit test for this PR.

// clang-format off
void CallExprRewriterFactoryBase::initRewriterMapMemory() {
RewriterMap->merge(
Expand Down
Loading