Skip to content

Commit

Permalink
[HIPIFY][#525][perl] Fix CONVERTED refs by names per file and in total
Browse files Browse the repository at this point in the history
+ Fix errors with unreported refs
+ Remove unrelated stuff which is not reported by `hipify-clang` (kernels, warnings count)
+ Updated regenerated hipify-perl accordingly
  • Loading branch information
emankov committed Apr 8, 2022
1 parent df3687f commit cdd27e9
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 54 deletions.
53 changes: 25 additions & 28 deletions bin/hipify-perl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 (<INFILE>) {
Expand Down Expand Up @@ -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'};
Expand Down Expand Up @@ -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);
Expand All @@ -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};
}
}
56 changes: 30 additions & 26 deletions src/CUDA2HIP_Perl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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_ << "(<INFILE>) {" << endl;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit cdd27e9

Please sign in to comment.