From ef23cdda4198f650b3c407126155d97114a9d5a3 Mon Sep 17 00:00:00 2001 From: Evgeny Mankov Date: Sat, 21 Sep 2019 07:33:17 +0300 Subject: [PATCH] [HIPIFY][#1435][perl] Add HIP_SYMBOL wrapper to the templated Device Symbol argument of the following functions: cudaMemcpyToSymbol, cudaMemcpyToSymbolAsync, cudaGetSymbolSize, cudaGetSymbolAddress, cudaMemcpyFromSymbol, cudaMemcpyFromSymbolAsync + Perl part of [#1441] + Implement function generateSymbolFunctions() in hipify-clang for that purposes + Update hipify-perl TODO: Eliminate dim3() issue in hipify-perl as well [ROCm/clr commit: 72a658295f143e292064d3f40b84436e127a9cf6] --- projects/clr/hipamd/bin/hipify-perl | 26 ++++++++++++ .../hipamd/hipify-clang/src/CUDA2HIP_Perl.cpp | 41 ++++++++++++++++++- .../hipify-clang/src/CUDA2HIP_Scripting.h | 3 ++ .../hipamd/hipify-clang/src/HipifyAction.cpp | 4 +- 4 files changed, 70 insertions(+), 4 deletions(-) diff --git a/projects/clr/hipamd/bin/hipify-perl b/projects/clr/hipamd/bin/hipify-perl index 8094b96add..02a540b133 100755 --- a/projects/clr/hipamd/bin/hipify-perl +++ b/projects/clr/hipamd/bin/hipify-perl @@ -160,6 +160,8 @@ while (@ARGV) { undef $/; # Read whole file at once, so we can match newlines. while () { + # chomp; + # next if /^(\s*(#.*)?)?$/; $ft{'error'} += s/\bcudaGetErrorName\b/hipGetErrorName/g; $ft{'error'} += s/\bcudaGetErrorString\b/hipGetErrorString/g; $ft{'error'} += s/\bcudaGetLastError\b/hipGetLastError/g; @@ -1761,6 +1763,8 @@ while (@ARGV) { $ft{'kernel_func'} += countSupportedDeviceFunctions(); } + $ft{'memory'} += transformSymbolFunctions(); + # Print it! # TODO - would like to move this code outside loop but it uses $_ which contains the whole file. unless ($no_output) { @@ -1815,6 +1819,28 @@ if ($count_conversions) { } } +sub transformSymbolFunctions +{ + my $m = 0; + foreach $func ( + "hipMemcpyToSymbol", + "hipMemcpyToSymbolAsync" + ) + { + $m += s/(?& perlStreamPtr) { + *perlStreamPtr.get() << "\n" << sSub << " transformSymbolFunctions\n" << "{\n" << space << sMy; + std::string sCommon = space + sForeach; + *perlStreamPtr.get() << sCommon; + unsigned int count = 0; + for (auto& dsf : DeviceSymbolFunctions0) { + const auto found = CUDA_RENAMES_MAP().find(dsf); + if (found != CUDA_RENAMES_MAP().end()) { + *perlStreamPtr.get() << (count ? ",\n" : "") << double_space << "\"" << found->second.hipName.str() << "\""; + count++; + } + } + *perlStreamPtr.get() << "\n" << space << ")\n"; + *perlStreamPtr.get() << space << "{\n"; + *perlStreamPtr.get() << double_space << "$m += s/(?second.hipName.str() << "\""; + count++; + } + } + *perlStreamPtr.get() << "\n" << space << ")\n"; + *perlStreamPtr.get() << space << "{\n"; + *perlStreamPtr.get() << double_space << "$m += s/(?& perlStreamPtr) { unsigned int countUnsupported = 0; @@ -59,7 +95,7 @@ namespace perl { std::stringstream subCountSupported; std::stringstream subWarnUnsupported; std::stringstream subCommon; - std::string sCommon = space + "my $m = 0;\n" + space + "foreach $func (\n"; + std::string sCommon = space + sMy + space + sForeach; subCountSupported << "\n" << sSub << " countSupportedDeviceFunctions\n" << "{\n" << (countSupported ? sCommon : space + sReturn_0); subWarnUnsupported << "\n" << sSub << " warnUnsupportedDeviceFunctions\n" << "{\n" << (countUnsupported ? space + "my $line_num = shift;\n" + sCommon : space + sReturn_0); if (countSupported) { @@ -85,7 +121,7 @@ namespace perl { subWarnUnsupported << triple_space << "print STDERR \" warning: $fileName:$line_num: unsupported device function \\\"$func\\\": $_\\n\";\n"; } if (countSupported || countUnsupported) { - sCommon = double_space + "}\n" + space + "}\n" + space + "return $m;\n"; + sCommon = double_space + "}\n" + space + "}\n" + space + sReturn_m; } if (countSupported) { subCountSupported << sCommon; @@ -157,6 +193,7 @@ namespace perl { } } } + generateSymbolFunctions(perlStreamPtr); generateDeviceFunctions(perlStreamPtr); perlStreamPtr.get()->flush(); bool ret = true; diff --git a/projects/clr/hipamd/hipify-clang/src/CUDA2HIP_Scripting.h b/projects/clr/hipamd/hipify-clang/src/CUDA2HIP_Scripting.h index c585a47d47..9469f25845 100644 --- a/projects/clr/hipamd/hipify-clang/src/CUDA2HIP_Scripting.h +++ b/projects/clr/hipamd/hipify-clang/src/CUDA2HIP_Scripting.h @@ -22,6 +22,9 @@ THE SOFTWARE. #pragma once +extern std::set DeviceSymbolFunctions0; +extern std::set DeviceSymbolFunctions1; + namespace perl { bool generate(bool Generate = true); diff --git a/projects/clr/hipamd/hipify-clang/src/HipifyAction.cpp b/projects/clr/hipamd/hipify-clang/src/HipifyAction.cpp index 6cb75f8911..4acbb7e78e 100644 --- a/projects/clr/hipamd/hipify-clang/src/HipifyAction.cpp +++ b/projects/clr/hipamd/hipify-clang/src/HipifyAction.cpp @@ -43,12 +43,12 @@ const std::string sCudaGetSymbolAddress = "cudaGetSymbolAddress"; const std::string sCudaMemcpyFromSymbol = "cudaMemcpyFromSymbol"; const std::string sCudaMemcpyFromSymbolAsync = "cudaMemcpyFromSymbolAsync"; -const std::set DeviceSymbolFunctions0 { +std::set DeviceSymbolFunctions0 { {sCudaMemcpyToSymbol}, {sCudaMemcpyToSymbolAsync} }; -const std::set DeviceSymbolFunctions1 { +std::set DeviceSymbolFunctions1 { {sCudaGetSymbolSize}, {sCudaGetSymbolAddress}, {sCudaMemcpyFromSymbol},