From cdd27e9570f522ef7ba40aea5c9f4b6c038e1b92 Mon Sep 17 00:00:00 2001 From: Evgeny Mankov Date: Sat, 9 Apr 2022 00:31:45 +0300 Subject: [PATCH] [HIPIFY][#525][perl] Fix `CONVERTED refs by names` per file and in total + Fix errors with unreported refs + Remove unrelated stuff which is not reported by `hipify-clang` (kernels, warnings count) + Updated regenerated hipify-perl accordingly --- bin/hipify-perl | 53 +++++++++++++++++++--------------------- src/CUDA2HIP_Perl.cpp | 56 +++++++++++++++++++++++-------------------- 2 files changed, 55 insertions(+), 54 deletions(-) diff --git a/bin/hipify-perl b/bin/hipify-perl index 8e14fd30..7e1fde8a 100755 --- a/bin/hipify-perl +++ b/bin/hipify-perl @@ -60,6 +60,8 @@ my $fileName = ""; my $hipFileName = ""; my %ft; my %Tkernels; +my %convertedTags = (); +my %convertedTagsTotal = (); GetOptions( "examine" => \$examine # Combines -no-output and -print-stats options @@ -784,20 +786,29 @@ sub totalStats { $total += $count{$key}; } return $total; -}; +} sub printStats { - my $label = shift(); - my @statNames = @{shift()}; my %counts = %{shift()}; my $warnings = shift(); my $loc = shift(); + my $fileName = shift(); + my $global = shift(); my $total = totalStats(\%counts); - printf STDERR "%s %d CUDA->HIP refs ( ", $label, $total; - foreach $stat (@statNames) { - printf STDERR "%s:%d ", $stat, $counts{$stat}; + printf STDERR "\n[HIPIFY] info: file '$fileName' statisitics:\n"; + printf STDERR " CONVERTED refs count: $total\n"; + printf STDERR " TOTAL lines of code: $loc\n"; + printf STDERR " WARNINGS: $warnings\n"; + printf STDERR "[HIPIFY] info: CONVERTED refs by names:\n"; + if ($global) { + foreach my $key (sort keys %convertedTagsTotal) { + printf STDERR " %s %d\n", $key, $convertedTagsTotal{$key}; + } + } else { + foreach my $key (sort keys %convertedTags) { + printf STDERR " %s %d\n", $key, $convertedTags{$key}; + } } - printf STDERR ")\n warning:%d LOC:%d", $warnings, $loc; } sub addStats { @@ -7755,6 +7766,7 @@ while (@ARGV) { my $warnings = 0; my %warningTags; my $lineCount = 0; + %convertedTags = (); undef $/; # Read whole file at once, so we can match newlines while () { @@ -7799,8 +7811,9 @@ while (@ARGV) { transformKernelLaunch(); transformCubNamespace(); if ($print_stats) { - while (/(\b(hip|HIP|HIP_|hipblas|HIPBLAS_|hipcub|hipdnn|HIPDNN_|hipfft|HIPFFT_|hiprand|HIPRAND_|hiprtc|HIPRTC_|hipsparse|HIPSPARSE_)([A-Z]|_)\w+\b)/g) { + while (/(\b(hip|HIP|HIP_|hipblas|HIPBLAS_|hipcub|hipdnn|HIPDNN_|hipfft|HIPFFT_|hiprand|HIPRAND_|hiprtc|HIPRTC_|hipsparse|HIPSPARSE_)([A-Z])\w+\b)/g) { $convertedTags{$1}++; + $convertedTagsTotal{$1}++; } } my $hasDeviceCode = $countKeywords + $ft{'device_function'}; @@ -7851,8 +7864,7 @@ while (@ARGV) { } my $totalConverted = totalStats(\%ft); if (($totalConverted+$warnings) and $print_stats) { - printStats(" info: converted", \@statNames, \%ft, $warnings, $lineCount); - print STDERR " in '$fileName'\n"; + printStats(\%ft, $warnings, $lineCount, $fileName, 0); } # Update totals for all files addStats(\%tt, \%ft); @@ -7861,25 +7873,10 @@ while (@ARGV) { foreach $key (keys %warningTags) { $TwarningTags{$key} += $warningTags{$key}; } - } # Unless filtered direcotry or file + } # Unless filtered directory or file } -# Print total stats for all files processed: +# Print total stats for all files processed if ($print_stats and ($fileCount > 1)) { + printStats(\%tt, $Twarnings, $TlineCount, "GLOBAL", 1); print STDERR "\n"; - printStats(" info: TOTAL-converted", \@statNames, \%tt, $Twarnings, $TlineCount); - print STDERR "\n"; - foreach my $key (sort { $TwarningTags{$b} <=> $TwarningTags{$a} } keys %TwarningTags) { - printf STDERR " warning: unconverted %s : %d\n", $key, $TwarningTags{$key}; - } - my $kernelCnt = keys %Tkernels; - printf STDERR " kernels (%d total) : ", $kernelCnt; - foreach my $key (sort { $Tkernels{$b} <=> $Tkernels{$a} } keys %Tkernels) { - printf STDERR " %s(%d)", $key, $Tkernels{$key}; - } - print STDERR "\n\n"; -} -if ($print_stats) { - foreach my $key (sort { $convertedTags{$b} <=> $convertedTags{$a} } keys %convertedTags) { - printf STDERR " %s %d\n", $key, $convertedTags{$key}; - } } diff --git a/src/CUDA2HIP_Perl.cpp b/src/CUDA2HIP_Perl.cpp index 770706fe..2c3c2c21 100644 --- a/src/CUDA2HIP_Perl.cpp +++ b/src/CUDA2HIP_Perl.cpp @@ -198,7 +198,9 @@ namespace perl { *streamPtr.get() << my << "$fileName = \"\";" << endl; *streamPtr.get() << my << "$hipFileName = \"\";" << endl; *streamPtr.get() << my << "%ft;" << endl; - *streamPtr.get() << my << "%Tkernels;" << endl_2; + *streamPtr.get() << my << "%Tkernels;" << endl; + *streamPtr.get() << my << "%convertedTags = ();" << endl; + *streamPtr.get() << my << "%convertedTagsTotal = ();" << endl_2; *streamPtr.get() << "GetOptions(" << endl; *streamPtr.get() << tab << " \"examine\" => \\$examine # Combines -no-output and -print-stats options" << endl; *streamPtr.get() << tab << ", \"exclude-dirs=s\" => \\$exclude_dirs # Exclude directories" << endl; @@ -276,18 +278,29 @@ namespace perl { *streamPtr.get() << tab << my << "$total = 0;" << endl; *streamPtr.get() << tab << foreach << "$key (keys %count) {" << endl; *streamPtr.get() << tab_2 << "$total += $count{$key};" << endl_tab << "}" << endl; - *streamPtr.get() << tab << "return $total;" << endl << "};" << endl; + *streamPtr.get() << tab << "return $total;" << endl << "}" << endl; *streamPtr.get() << endl << sub << "printStats" << " {" << endl; - *streamPtr.get() << tab << my << "$label = shift();" << endl; - *streamPtr.get() << tab << my << "@statNames = @{shift()};" << endl; *streamPtr.get() << tab << my << "%counts = %{shift()};" << endl; *streamPtr.get() << tab << my << "$warnings = shift();" << endl; *streamPtr.get() << tab << my << "$loc = shift();" << endl; + *streamPtr.get() << tab << my << "$fileName = shift();" << endl; + *streamPtr.get() << tab << my << "$global = shift();" << endl; *streamPtr.get() << tab << my << "$total = totalStats(\\%counts);" << endl; - *streamPtr.get() << tab << printf << "\"%s %d CUDA->HIP refs ( \", $label, $total;" << endl; - *streamPtr.get() << tab << foreach << "$stat (@statNames) {" << endl; - *streamPtr.get() << tab_2 << printf << "\"%s:%d \", $stat, $counts{$stat};" << endl_tab << "}" << endl; - *streamPtr.get() << tab << printf << "\")\\n warning:%d LOC:%d\", $warnings, $loc;" << endl << "}" << endl; + *streamPtr.get() << tab << printf << "\"\\n[HIPIFY] info: file '$fileName' statisitics:\\n\";" << endl; + *streamPtr.get() << tab << printf << "\" CONVERTED refs count: $total\\n\";" << endl; + *streamPtr.get() << tab << printf << "\" TOTAL lines of code: $loc\\n\";" << endl; + *streamPtr.get() << tab << printf << "\" WARNINGS: $warnings\\n\";" << endl; + *streamPtr.get() << tab << printf << "\"[HIPIFY] info: CONVERTED refs by names:\\n\";" << endl; + *streamPtr.get() << tab << "if ($global) {" << endl; + *streamPtr.get() << tab_2 << foreach << my << "$key (sort keys %convertedTagsTotal) {" << endl; + *streamPtr.get() << tab_3 << printf << "\" %s %d\\n\", $key, $convertedTagsTotal{$key};" << endl; + *streamPtr.get() << tab_2 << "}" << endl; + *streamPtr.get() << tab << "} else {" << endl; + *streamPtr.get() << tab_2 << foreach << my << "$key (sort keys %convertedTags) { " << endl; + *streamPtr.get() << tab_3 << printf << "\" %s %d\\n\", $key, $convertedTags{$key};" << endl; + *streamPtr.get() << tab_2 << "}" << endl; + *streamPtr.get() << tab << "}" << endl; + *streamPtr.get() << "}" << endl; for (int i = 0; i < 2; ++i) { *streamPtr.get() << endl << sub << (i ? "clearStats" : "addStats") << " {" << endl; *streamPtr.get() << tab << my << "$dest_ref = shift();" << endl; @@ -697,6 +710,7 @@ namespace perl { *streamPtr.get() << tab_2 << my << "$warnings = 0;" << endl; *streamPtr.get() << tab_2 << my << "%warningTags;" << endl; *streamPtr.get() << tab_2 << my << "$lineCount = 0;" << endl; + *streamPtr.get() << tab_2 << "%convertedTags = ();" << endl; *streamPtr.get() << tab_2 << "undef $/;" << endl; *streamPtr.get() << tab_2 << "# Read whole file at once, so we can match newlines" << endl; *streamPtr.get() << tab_2 << while_ << "() {" << endl; @@ -739,8 +753,9 @@ namespace perl { *streamPtr.get() << tab_3 << sTansformKernelLaunch << "();" << endl; *streamPtr.get() << tab_3 << sTransformCubNamespace << "();" << endl; *streamPtr.get() << tab_3 << "if ($print_stats) {" << endl; - *streamPtr.get() << tab_4 << while_ << "(/(\\b(hip|HIP|HIP_|hipblas|HIPBLAS_|hipcub|hipdnn|HIPDNN_|hipfft|HIPFFT_|hiprand|HIPRAND_|hiprtc|HIPRTC_|hipsparse|HIPSPARSE_)([A-Z]|_)\\w+\\b)/g) {" << endl; - *streamPtr.get() << tab_5 << "$convertedTags{$1}++;" << endl_tab_4 << "}" << endl_tab_3 << "}" << endl; + *streamPtr.get() << tab_4 << while_ << "(/(\\b(hip|HIP|HIP_|hipblas|HIPBLAS_|hipcub|hipdnn|HIPDNN_|hipfft|HIPFFT_|hiprand|HIPRAND_|hiprtc|HIPRTC_|hipsparse|HIPSPARSE_)([A-Z])\\w+\\b)/g) {" << endl; + *streamPtr.get() << tab_5 << "$convertedTags{$1}++;" << endl; + *streamPtr.get() << tab_5 << "$convertedTagsTotal{$1}++;" << endl_tab_4 << "}" << endl_tab_3 << "}" << endl; *streamPtr.get() << tab_3 << my << "$hasDeviceCode = $countKeywords + $ft{'device_function'};" << endl; *streamPtr.get() << tab_3 << unless_ << "($quiet_warnings) {" << endl; *streamPtr.get() << tab_4 << "# Copy into array of lines, process line-by-line to show warnings" << endl; @@ -779,31 +794,20 @@ namespace perl { *streamPtr.get() << tab_3 << "$lineCount = $_ =~ tr/\\n//;" << endl_tab_2 << "}" << endl; *streamPtr.get() << tab_2 << my << "$totalConverted = totalStats(\\%ft);" << endl; *streamPtr.get() << tab_2 << "if (($totalConverted+$warnings) and $print_stats) {" << endl; - *streamPtr.get() << tab_3 << "printStats(\" info: converted\", \\@statNames, \\%ft, $warnings, $lineCount);" << endl; - *streamPtr.get() << tab_3 << print << "\" in '$fileName'\\n\";" << endl_tab_2 << "}" << endl; + *streamPtr.get() << tab_3 << "printStats(\\%ft, $warnings, $lineCount, $fileName, 0);" << endl_tab_2 << "}" << endl; *streamPtr.get() << tab_2 << "# Update totals for all files" << endl; *streamPtr.get() << tab_2 << "addStats(\\%tt, \\%ft);" << endl; *streamPtr.get() << tab_2 << "$Twarnings += $warnings;" << endl; *streamPtr.get() << tab_2 << "$TlineCount += $lineCount;" << endl; *streamPtr.get() << tab_2 << foreach << "$key (keys %warningTags) {" << endl; *streamPtr.get() << tab_3 << "$TwarningTags{$key} += $warningTags{$key};" << endl_tab_2 << "}"; - *streamPtr.get() << endl_tab << "} # Unless filtered direcotry or file " << endl; + *streamPtr.get() << endl_tab << "} # Unless filtered directory or file" << endl; *streamPtr.get() << "}" << endl; - *streamPtr.get() << "# Print total stats for all files processed:" << endl; + *streamPtr.get() << "# Print total stats for all files processed" << endl; *streamPtr.get() << "if ($print_stats and ($fileCount > 1)) {" << endl; + *streamPtr.get() << tab << "printStats(\\%tt, $Twarnings, $TlineCount, \"GLOBAL\", 1);" << endl; *streamPtr.get() << tab << print << "\"\\n\";" << endl; - *streamPtr.get() << tab << "printStats(\" info: TOTAL-converted\", \\@statNames, \\%tt, $Twarnings, $TlineCount);" << endl; - *streamPtr.get() << tab << print << "\"\\n\";" << endl; - *streamPtr.get() << tab << foreach << my << "$key (sort { $TwarningTags{$b} <=> $TwarningTags{$a} } keys %TwarningTags) {" << endl; - *streamPtr.get() << tab_2 << printf << "\" warning: unconverted %s : %d\\n\", $key, $TwarningTags{$key};" << endl_tab << "}" << endl; - *streamPtr.get() << tab << my << "$kernelCnt = keys %Tkernels;" << endl; - *streamPtr.get() << tab << printf << "\" kernels (%d total) : \", $kernelCnt;" << endl; - *streamPtr.get() << tab << foreach << my << "$key (sort { $Tkernels{$b} <=> $Tkernels{$a} } keys %Tkernels) {" << endl; - *streamPtr.get() << tab_2 << printf << "\" %s(%d)\", $key, $Tkernels{$key};" << endl_tab << "}" << endl; - *streamPtr.get() << tab << print << "\"\\n\\n\";" << endl << "}" << endl; - *streamPtr.get() << "if ($print_stats) {" << endl; - *streamPtr.get() << tab << foreach << my << "$key (sort { $convertedTags{$b} <=> $convertedTags{$a} } keys %convertedTags) {" << endl; - *streamPtr.get() << tab_2 << printf << "\" %s %d\\n\", $key, $convertedTags{$key};" << endl_tab << "}" << endl << "}" << endl; + *streamPtr.get() << "}" << endl; streamPtr.get()->flush(); bool ret = true; EC = sys::fs::copy_file(tmpFile, dstHipifyPerl);