Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Draft]Refine Codepin #2608

Draft
wants to merge 6 commits into
base: SYCLomatic
Choose a base branch
from
Draft
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
43 changes: 27 additions & 16 deletions clang/lib/DPCT/FileGenerator/GenFiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -845,33 +845,44 @@ void genCodePinDumpFunc(dpct::RawFDOStream &RS, bool IsForCUDADebug) {
RS << "template <> class data_ser<" << CodepinTypeName << "> {" << getNL()
<< "public:" << getNL()
<< " static void dump(dpctexp::codepin::detail::json_stringstream "
"&ss, "
"&ss, std::ofstream &ofst, "
<< CodepinTypeName << " &value," << getNL()
<< " dpctexp::codepin::queue_t queue) {" << getNL();
RS << " auto arr = ss.array();" << getNL();

<< " dpctexp::codepin::queue_t queue, bool top_call = true) {" << getNL();
// RS << " auto arr = ss.array();" << getNL();
RS << " if (top_call) {" << getNL();
RS << " size_t size = sizeof(" << CodepinTypeName << ");" << getNL();
RS << " ofst.write(get_demangle_type_name<" << CodepinTypeName << ">().c_str(), get_demangle_type_name<" << CodepinTypeName << ">().length() + 1);" << getNL();
RS << " ofst.write(reinterpret_cast<char*>(&size), sizeof(size_t));" << getNL();
RS << " }" << getNL();
RS << " ";
RS << " auto obj = ss.object();" << getNL();
if (int MemberNum = Info.Members.size()) {
for (int i = 0; i < MemberNum; i++) {
RS << " {" << getNL() << " auto obj" << i << " = arr.object();"
<< getNL() << " obj" << i << ".key(\""
RS << " {"
<< getNL() << " obj.key(\""
<< Info.Members[i].MemberName << "\");" << getNL()
<< " auto value" << i << " = obj" << i
<< ".value<dpctexp::codepin::detail::json_stringstream::json_obj>("
<< " auto value" << i << " = obj.value<dpctexp::codepin::detail::json_stringstream::json_obj>("
");"
<< getNL() << " dpctexp::codepin::detail::data_ser<"
<< getCodePinPostfixName(Info.Members[i], IsForCUDADebug);
for (auto &D : Info.Members[i].Dims) {
RS << "[" << std::to_string(D) << "]";
}
RS << ">::print_type_name(value" << i << ");" << getNL() << " obj"
<< i << ".key(\"Data\");" << getNL()
<< " dpctexp::codepin::detail::data_ser<"
RS << ">::print_type_name(value" << i << ");" << getNL() << " obj.key(\"Offset\");" << getNL();
RS << " obj.value(static_cast<size_t>(ofst.tellp()));" << getNL();
RS << " if(!is_expand_to_dump<"
<< getCodePinPostfixName(Info.Members[i], IsForCUDADebug);
for (auto &D : Info.Members[i].Dims) {
RS << "[" << std::to_string(D) << "]";
}
RS << ">()) obj.key(\"Data\");" << getNL();
RS << " dpctexp::codepin::detail::data_ser<"
<< getCodePinPostfixName(Info.Members[i], IsForCUDADebug);
for (auto &D : Info.Members[i].Dims) {
RS << "[" << std::to_string(D) << "]";
}
RS << ">::dump(ss, value." << Info.Members[i].CodePinMemberName
<< ", queue);" << getNL() << " }" << getNL();
RS << ">::dump(ss, ofst, value." << Info.Members[i].CodePinMemberName
<< ", queue, true);" << getNL() << " }" << getNL();
}
}
RS << getNL() << " }" << getNL();
Expand All @@ -883,13 +894,13 @@ void genCodePinDumpFunc(dpct::RawFDOStream &RS, bool IsForCUDADebug) {
RS << "template <> class data_ser<" << Name << "> {" << getNL()
<< "public:" << getNL()
<< " static void dump(dpctexp::codepin::detail::json_stringstream "
"&ss, "
"&ss, std::ofstream &ofst, "
<< Name << " &value," << getNL()
<< " dpctexp::codepin::queue_t queue) {" << getNL();
<< " dpctexp::codepin::queue_t queue, bool top_call = true) {" << getNL();
RS << " " + CodepinTypeName << "& temp = reinterpret_cast<"
<< CodepinTypeName << "&>(value);" << getNL();
RS << " dpctexp::codepin::detail::data_ser<" << CodepinTypeName
<< ">::dump(ss, temp, queue);" << getNL() << " }" << getNL()
<< ">::dump(ss, ofst, temp, queue, top_call);" << getNL() << " }" << getNL()
<< " static void print_type_name(json_stringstream::json_obj &obj) {"
<< getNL() << " obj.key(\"Type\");" << getNL() << " obj.value(\""
<< Name << "\");" << getNL() << " }" << getNL() << "};" << getNL()
Expand Down
22 changes: 12 additions & 10 deletions clang/lib/DPCT/RulesLang/RulesLang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5429,11 +5429,11 @@ bool MemoryMigrationRule::canUseTemplateStyleMigration(
}

void MemoryMigrationRule::instrumentAddressToSizeRecordForCodePin(
const CallExpr *C, int PtrArgLoc, int AllocMemSizeLoc) {
auto &SM = dpct::DpctGlobalInfo::getSourceManager();
const CallExpr *C, std::string ReplaceText, int AllocMemSizeLoc) {
auto &SM = dpct::DpctGlobalInfo::getSourceManager();
if (DpctGlobalInfo::isCodePinEnabled()) {
SourceLocation CallEnd = C->getEndLoc();
if(SM.isMacroArgExpansion(C->getEndLoc())){
if (SM.isMacroArgExpansion(C->getEndLoc())) {
CallEnd = SM.getExpansionRange(C->getEndLoc()).getEnd();
} else {
CallEnd = getDefinitionRange(C->getBeginLoc(), C->getEndLoc()).getEnd();
Expand All @@ -5445,19 +5445,20 @@ void MemoryMigrationRule::instrumentAddressToSizeRecordForCodePin(

emplaceTransformation(new InsertText(
PtrSizeLoc,
std::string(getNL()) + "dpctexp::codepin::get_ptr_size_map()[" +
getDrefName(C->getArg(PtrArgLoc)) + "] = " +
std::string(getNL()) + "dpctexp::codepin::set_ptr_size_map(" +
ReplaceText + ", " +
std::string(Lexer::getSourceText(
CharSourceRange::getTokenRange(
C->getArg(AllocMemSizeLoc)->getSourceRange()),
DpctGlobalInfo::getSourceManager(), LangOptions())) +
";",
");",
0, RT_CUDAWithCodePin));

emplaceTransformation(new InsertText(
PtrSizeLoc,
std::string(getNL()) + "dpctexp::codepin::get_ptr_size_map()[" +
getDrefName(C->getArg(PtrArgLoc)) +
"] = " + ExprAnalysis::ref(C->getArg(AllocMemSizeLoc)) + ";",
std::string(getNL()) + "dpctexp::codepin::set_ptr_size_map(" +
ReplaceText + "," + ExprAnalysis::ref(C->getArg(AllocMemSizeLoc)) +
");",
0, RT_ForSYCLMigration));
DpctGlobalInfo::getInstance().insertHeader(
C->getBeginLoc(), HT_DPCT_CodePin_CUDA, RT_CUDAWithCodePin);
Expand Down Expand Up @@ -5653,7 +5654,7 @@ void MemoryMigrationRule::mallocMigration(
// to the post process. At that time, the MainFile is invalid.
DpctGlobalInfo::getInstance().insertHeader(C->getBeginLoc(),
HeaderType::HT_SYCL);
instrumentAddressToSizeRecordForCodePin(C,0,1);
instrumentAddressToSizeRecordForCodePin(C, getDrefName(C->getArg(0)) ,1);
} else if (Name == "cudaHostAlloc" || Name == "cudaMallocHost" ||
Name == "cuMemHostAlloc" || Name == "cuMemAllocHost_v2" ||
Name == "cuMemAllocPitch_v2" || Name == "cudaMallocPitch" ||
Expand Down Expand Up @@ -5821,6 +5822,7 @@ void MemoryMigrationRule::memcpyMigration(
if (!CallExprRewriterFactoryBase::RewriterMap)
return;
auto Itr = CallExprRewriterFactoryBase::RewriterMap->find(Name);
instrumentAddressToSizeRecordForCodePin(C, ExprAnalysis::ref(C->getArg(0)), 2);
if (Itr != CallExprRewriterFactoryBase::RewriterMap->end()) {
ExprAnalysis EA(C);
emplaceTransformation(EA.getReplacement());
Expand Down
3 changes: 2 additions & 1 deletion clang/lib/DPCT/RulesLang/RulesLang.h
Original file line number Diff line number Diff line change
Expand Up @@ -662,8 +662,9 @@ class MemoryMigrationRule : public NamedMigrationRule<MemoryMigrationRule> {
emplaceTransformation(new InsertBeforeStmt(C->getArg(InsertArgIndex),
std::string(InsertedText)));
}
void instrumentAddressToSizeRecordForCodePin(const CallExpr *C, int PtrArgLoc,
void instrumentAddressToSizeRecordForCodePin(const CallExpr *C, std::string ReplaceText,
int AllocMemSizeLoc);

};

class MemoryDataTypeRule : public NamedMigrationRule<MemoryDataTypeRule> {
Expand Down
Loading
Loading