diff --git a/hipamd/hipify-clang/src/Statistics.cpp b/hipamd/hipify-clang/src/Statistics.cpp index 474acf724d..c37226ed66 100644 --- a/hipamd/hipify-clang/src/Statistics.cpp +++ b/hipamd/hipify-clang/src/Statistics.cpp @@ -130,8 +130,12 @@ Statistics::Statistics(const std::string& name): fileName(name) { std::ifstream src_file(name, std::ios::binary | std::ios::ate); src_file.clear(); src_file.seekg(0); - totalLines = (int) std::count(std::istreambuf_iterator(src_file), std::istreambuf_iterator(), '\n'); + totalLines = (unsigned) std::count(std::istreambuf_iterator(src_file), std::istreambuf_iterator(), '\n'); totalBytes = (int) src_file.tellg(); + if (totalBytes < 0) { + totalBytes = 0; + hasErrors = true; + } startTime = chr::steady_clock::now(); } @@ -171,23 +175,24 @@ void Statistics::print(std::ostream* csv, llvm::raw_ostream* printOut, bool skip std::string str = "file \'" + fileName + "\' statistics:\n"; conditionalPrint(csv, printOut, "\n" + str, "\n[HIPIFY] info: " + str); } + if (hasErrors || totalBytes <= 0 || totalLines <= 0) { + std::string str = "\n ERROR: Statistics is invalid due to failed hipification.\n\n"; + conditionalPrint(csv, printOut, str, str); + } size_t changedLines = touchedLines.size(); // Total number of (un)supported refs that were converted. int supportedSum = supported.getConvSum(); int unsupportedSum = unsupported.getConvSum(); + int allSum = supportedSum + unsupportedSum; printStat(csv, printOut, "CONVERTED refs count", supportedSum); printStat(csv, printOut, "UNCONVERTED refs count", unsupportedSum); - printStat(csv, printOut, "CONVERSION %", 100 - std::lround(double(unsupportedSum * 100) / double(supportedSum + unsupportedSum))); + printStat(csv, printOut, "CONVERSION %", 100 - (0 == allSum ? 100 : std::lround(double(unsupportedSum * 100) / double(allSum)))); printStat(csv, printOut, "REPLACED bytes", touchedBytes); printStat(csv, printOut, "TOTAL bytes", totalBytes); printStat(csv, printOut, "CHANGED lines of code", changedLines); printStat(csv, printOut, "TOTAL lines of code", totalLines); - if (totalBytes > 0) { - printStat(csv, printOut, "CODE CHANGED (in bytes) %", std::lround(double(touchedBytes * 100) / double(totalBytes))); - } - if (totalLines > 0) { - printStat(csv, printOut, "CODE CHANGED (in lines) %", std::lround(double(changedLines * 100) / double(totalLines))); - } + printStat(csv, printOut, "CODE CHANGED (in bytes) %", 0 == totalBytes ? 0 : std::lround(double(touchedBytes * 100) / double(totalBytes))); + printStat(csv, printOut, "CODE CHANGED (in lines) %", 0 == totalLines ? 0 : std::lround(double(changedLines * 100) / double(totalLines))); typedef std::chrono::duration duration; duration elapsed = completionTime - startTime; std::stringstream stream; @@ -199,6 +204,7 @@ void Statistics::print(std::ostream* csv, llvm::raw_ostream* printOut, bool skip void Statistics::printAggregate(std::ostream *csv, llvm::raw_ostream* printOut) { Statistics globalStats = getAggregate(); + globalStats.hasErrors = false; conditionalPrint(csv, printOut, "\nTOTAL statistics:\n", "\n[HIPIFY] info: TOTAL statistics:\n"); // A file is considered "converted" if we made any changes to it. int convertedFiles = 0; @@ -206,16 +212,23 @@ void Statistics::printAggregate(std::ostream *csv, llvm::raw_ostream* printOut) if (!p.second.touchedLines.empty()) { convertedFiles++; } + if (!globalStats.hasErrors && p.second.hasErrors) { + globalStats.hasErrors = true; + } + if (globalStats.startTime > p.second.startTime) { + globalStats.startTime = p.second.startTime; + } } printStat(csv, printOut, "CONVERTED files", convertedFiles); printStat(csv, printOut, "PROCESSED files", stats.size()); + globalStats.markCompletion(); globalStats.print(csv, printOut); } //// Static state management //// Statistics Statistics::getAggregate() { - Statistics globalStats("global"); + Statistics globalStats("GLOBAL"); for (const auto& p : stats) { globalStats.add(p.second); } diff --git a/hipamd/hipify-clang/src/Statistics.h b/hipamd/hipify-clang/src/Statistics.h index c20278d176..2b3dcf9e9c 100644 --- a/hipamd/hipify-clang/src/Statistics.h +++ b/hipamd/hipify-clang/src/Statistics.h @@ -158,8 +158,8 @@ class Statistics { StatCounter unsupported; std::string fileName; std::set touchedLines = {}; - int touchedBytes = 0; - int totalLines = 0; + unsigned touchedBytes = 0; + unsigned totalLines = 0; int totalBytes = 0; chr::steady_clock::time_point startTime; chr::steady_clock::time_point completionTime; @@ -202,4 +202,6 @@ public: * timestamp into the currently active one. */ static void setActive(const std::string& name); + // Set this flag in case of hipification errors + bool hasErrors = false; }; diff --git a/hipamd/hipify-clang/src/main.cpp b/hipamd/hipify-clang/src/main.cpp index f7541ff4c6..951bffd5e8 100644 --- a/hipamd/hipify-clang/src/main.cpp +++ b/hipamd/hipify-clang/src/main.cpp @@ -117,6 +117,8 @@ int main(int argc, const char **argv) { Tool.appendArgumentsAdjuster(ct::getClangSyntaxOnlyAdjuster()); // Hipify _all_ the things! if (Tool.runAndSave(&actionFactory)) { + Statistics& currentStat = Statistics::current(); + currentStat.hasErrors = true; LLVM_DEBUG(llvm::dbgs() << "Skipped some replacements.\n"); } // Either move the tmpfile to the output, or remove it.