[HIPIFY][python] Initial support of hipify-python generation from hipify-clang
+ Only a generation of transformation map of CUDA entities is implemented.
+ 2 hipify-clang options are added: -python, -o-python-map-dir.
+ Explicitly set -roc option for cuda_to_hip_mappings.py generation.
+ Generated file already might be used by pytorch team.
[ROCm/hip commit: 6b370e7743]
This commit is contained in:
@@ -132,7 +132,7 @@ bool generatePerl(bool Generate = true) {
|
||||
continue;
|
||||
}
|
||||
if (i == ma.second.type) {
|
||||
*perlStreamPtr.get() << "$ft{'" + std::string(counterNames[ma.second.type]) + "'} += s/\\b" + std::string(ma.first) + "\\b/" + std::string(ma.second.hipName) + "/g;\n";
|
||||
*perlStreamPtr.get() << "$ft{'" << counterNames[ma.second.type] << "'} += s/\\b" << ma.first.str() << "\\b/" << ma.second.hipName.str() << "/g;\n";
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@@ -141,7 +141,7 @@ bool generatePerl(bool Generate = true) {
|
||||
continue;
|
||||
}
|
||||
if (i == ma.second.type) {
|
||||
*perlStreamPtr.get() << "$ft{'" + std::string(counterNames[ma.second.type]) + "'} += s/\\b" + std::string(ma.first) + "\\b/" + std::string(ma.second.hipName) + "/g;\n";
|
||||
*perlStreamPtr.get() << "$ft{'" << counterNames[ma.second.type] << "'} += s/\\b" << ma.first.str() << "\\b/" << ma.second.hipName.str() << "/g;\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -159,6 +159,72 @@ bool generatePerl(bool Generate = true) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool generatePython(bool Generate = true) {
|
||||
if (!Generate) {
|
||||
return true;
|
||||
}
|
||||
std::string dstPythonMap = "cuda_to_hip_mappings.py", dstPythonMapDir = OutputPythonMapDir;
|
||||
std::error_code EC;
|
||||
if (!dstPythonMapDir.empty()) {
|
||||
std::string sOutputPythonMapDirAbsPath = getAbsoluteDirectoryPath(OutputPythonMapDir, EC, "output hipify-python map");
|
||||
if (EC) {
|
||||
return false;
|
||||
}
|
||||
dstPythonMap = sOutputPythonMapDirAbsPath + "/" + dstPythonMap;
|
||||
}
|
||||
SmallString<128> tmpFile;
|
||||
StringRef ext = "hipify-tmp";
|
||||
EC = sys::fs::createTemporaryFile(dstPythonMap, ext, tmpFile);
|
||||
if (EC) {
|
||||
llvm::errs() << "\n" << sHipify << sError << EC.message() << ": " << tmpFile << "\n";
|
||||
return false;
|
||||
}
|
||||
std::unique_ptr<std::ostream> pythonStreamPtr = std::unique_ptr<std::ostream>(new std::ofstream(tmpFile.c_str(), std::ios_base::trunc));
|
||||
*pythonStreamPtr.get() << "import collections\n\n";
|
||||
*pythonStreamPtr.get() << "from pyHIPIFY.constants import *\n\n";
|
||||
*pythonStreamPtr.get() << "CUDA_RENAMES_MAP = collections.OrderedDict([\n";
|
||||
const std::string sHIP_UNS = ", HIP_UNSUPPORTED";
|
||||
for (int i = 0; i < NUM_CONV_TYPES; i++) {
|
||||
if (i == CONV_INCLUDE_CUDA_MAIN_H || i == CONV_INCLUDE) {
|
||||
for (auto& ma : CUDA_INCLUDE_MAP) {
|
||||
if (i == ma.second.type) {
|
||||
std::string sUnsupported;
|
||||
if (Statistics::isUnsupported(ma.second)) {
|
||||
sUnsupported = sHIP_UNS;
|
||||
}
|
||||
StringRef repName = Statistics::isToRoc(ma.second) ? ma.second.rocName : ma.second.hipName;
|
||||
*pythonStreamPtr.get() << " (\"" << ma.first.str() << "\", (\"" << repName.str() << "\", " << counterTypes[i] << ", " << apiTypes[ma.second.apiType] << sUnsupported << ")),\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
for (auto& ma : CUDA_RENAMES_MAP()) {
|
||||
if (i == ma.second.type) {
|
||||
std::string sUnsupported;
|
||||
if (Statistics::isUnsupported(ma.second)) {
|
||||
sUnsupported = sHIP_UNS;
|
||||
}
|
||||
StringRef repName = Statistics::isToRoc(ma.second) ? ma.second.rocName : ma.second.hipName;
|
||||
*pythonStreamPtr.get() << " (\"" << ma.first.str() << "\", (\"" << repName.str() << "\", " << counterTypes[i] << ", " << apiTypes[ma.second.apiType] << sUnsupported << ")),\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
*pythonStreamPtr.get() << "])\n\n";
|
||||
*pythonStreamPtr.get() << "CUDA_TO_HIP_MAPPINGS = [CUDA_RENAMES_MAP, C10_MAPPINGS, PYTORCH_SPECIFIC_MAPPINGS]\n";
|
||||
pythonStreamPtr.get()->flush();
|
||||
bool ret = true;
|
||||
EC = sys::fs::copy_file(tmpFile, dstPythonMap);
|
||||
if (EC) {
|
||||
llvm::errs() << "\n" << sHipify << sError << EC.message() << ": while copying " << tmpFile << " to " << dstPythonMap << "\n";
|
||||
ret = false;
|
||||
}
|
||||
if (!SaveTemps) {
|
||||
sys::fs::remove(tmpFile);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
int main(int argc, const char **argv) {
|
||||
std::vector<const char*> new_argv(argv, argv + argc);
|
||||
if (std::find(new_argv.begin(), new_argv.end(), std::string("--")) == new_argv.end()) {
|
||||
@@ -170,7 +236,7 @@ int main(int argc, const char **argv) {
|
||||
llcompat::PrintStackTraceOnErrorSignal();
|
||||
ct::CommonOptionsParser OptionsParser(argc, argv, ToolTemplateCategory, llvm::cl::Optional);
|
||||
std::vector<std::string> fileSources = OptionsParser.getSourcePathList();
|
||||
if (fileSources.empty() && !GeneratePerl) {
|
||||
if (fileSources.empty() && !GeneratePerl && !GeneratePython) {
|
||||
llvm::errs() << "\n" << sHipify << sError << "Must specify at least 1 positional argument for source file." << "\n";
|
||||
return 1;
|
||||
}
|
||||
@@ -178,6 +244,14 @@ int main(int argc, const char **argv) {
|
||||
llvm::errs() << "\n" << sHipify << sError << "hipify-perl generating failed." << "\n";
|
||||
return 1;
|
||||
}
|
||||
bool bToRoc = TranslateToRoc;
|
||||
TranslateToRoc = true;
|
||||
bool bToPython = generatePython(GeneratePython);
|
||||
TranslateToRoc = bToRoc;
|
||||
if (!bToPython) {
|
||||
llvm::errs() << "\n" << sHipify << sError << "hipify-python generating failed." << "\n";
|
||||
return 1;
|
||||
}
|
||||
if (fileSources.empty()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user