From 89c7451c35919a95aa654c6ff5204d3ceb37dd3d Mon Sep 17 00:00:00 2001 From: Justice Adams Date: Tue, 5 May 2020 16:58:44 -0700 Subject: [PATCH] Fix SelectionDAG Graph Printing on Windows Currently, when compiling to IR (presumably at the clang level) LLVM mangles symbols and sometimes they have illegal file characters including `?`'s in them. This causes a problem when building a graph via llc on Windows because the code currently passes the machine function name all the way down to the Windows API which frequently returns error 123 **ERROR_INVALID_NAME** https://docs.microsoft.com/en-us/windows/win32/debug/system-error-codes--0-499- Thus, we need to remove those illegal characters from the machine function name before generating a graph, which is the purpose of this patch. https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file I've created a static helper function replace_illegal_filename_chars which within GraphWriter.cpp to help with replacing illegal file character names before generating a dot graph filename. Reviewed By: rnk Differential Revision: https://reviews.llvm.org/D76863 --- llvm/include/llvm/Support/GraphWriter.h | 5 +---- llvm/lib/Support/GraphWriter.cpp | 27 ++++++++++++++++++++++++- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/llvm/include/llvm/Support/GraphWriter.h b/llvm/include/llvm/Support/GraphWriter.h index 0179670456a7..bd5c3f182209 100644 --- a/llvm/include/llvm/Support/GraphWriter.h +++ b/llvm/include/llvm/Support/GraphWriter.h @@ -330,11 +330,8 @@ std::string WriteGraph(const GraphType &G, const Twine &Name, const Twine &Title = "", std::string Filename = "") { int FD; - // Windows can't always handle long paths, so limit the length of the name. - std::string N = Name.str(); - N = N.substr(0, std::min(N.size(), 140)); if (Filename.empty()) { - Filename = createGraphFilename(N, FD); + Filename = createGraphFilename(Name.str(), FD); } else { std::error_code EC = sys::fs::openFileForWrite(Filename, FD); diff --git a/llvm/lib/Support/GraphWriter.cpp b/llvm/lib/Support/GraphWriter.cpp index 214a9032af78..d8aae9260323 100644 --- a/llvm/lib/Support/GraphWriter.cpp +++ b/llvm/lib/Support/GraphWriter.cpp @@ -76,10 +76,35 @@ StringRef llvm::DOT::getColorString(unsigned ColorNumber) { return Colors[ColorNumber % NumColors]; } +static std::string replaceIllegalFilenameChars(std::string Filename, + const char ReplacementChar) { +#ifdef _WIN32 + std::string IllegalChars = "\\/:?\"<>|"; +#else + std::string IllegalChars = "/"; +#endif + + for (char IllegalChar : IllegalChars) { + std::replace(Filename.begin(), Filename.end(), IllegalChar, + ReplacementChar); + } + + return Filename; +} + std::string llvm::createGraphFilename(const Twine &Name, int &FD) { FD = -1; SmallString<128> Filename; - std::error_code EC = sys::fs::createTemporaryFile(Name, "dot", FD, Filename); + + // Windows can't always handle long paths, so limit the length of the name. + std::string N = Name.str(); + N = N.substr(0, std::min(N.size(), 140)); + + // Replace illegal characters in graph Filename with '_' if needed + std::string CleansedName = replaceIllegalFilenameChars(N, '_'); + + std::error_code EC = + sys::fs::createTemporaryFile(CleansedName, "dot", FD, Filename); if (EC) { errs() << "Error: " << EC.message() << "\n"; return "";