Skip to content

Commit 89c7451

Browse files
Justice Adamsrnk
Justice Adams
authored andcommitted
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
1 parent 0274c79 commit 89c7451

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

llvm/include/llvm/Support/GraphWriter.h

+1-4
Original file line numberDiff line numberDiff line change
@@ -330,11 +330,8 @@ std::string WriteGraph(const GraphType &G, const Twine &Name,
330330
const Twine &Title = "",
331331
std::string Filename = "") {
332332
int FD;
333-
// Windows can't always handle long paths, so limit the length of the name.
334-
std::string N = Name.str();
335-
N = N.substr(0, std::min<std::size_t>(N.size(), 140));
336333
if (Filename.empty()) {
337-
Filename = createGraphFilename(N, FD);
334+
Filename = createGraphFilename(Name.str(), FD);
338335
} else {
339336
std::error_code EC = sys::fs::openFileForWrite(Filename, FD);
340337

llvm/lib/Support/GraphWriter.cpp

+26-1
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,35 @@ StringRef llvm::DOT::getColorString(unsigned ColorNumber) {
7676
return Colors[ColorNumber % NumColors];
7777
}
7878

79+
static std::string replaceIllegalFilenameChars(std::string Filename,
80+
const char ReplacementChar) {
81+
#ifdef _WIN32
82+
std::string IllegalChars = "\\/:?\"<>|";
83+
#else
84+
std::string IllegalChars = "/";
85+
#endif
86+
87+
for (char IllegalChar : IllegalChars) {
88+
std::replace(Filename.begin(), Filename.end(), IllegalChar,
89+
ReplacementChar);
90+
}
91+
92+
return Filename;
93+
}
94+
7995
std::string llvm::createGraphFilename(const Twine &Name, int &FD) {
8096
FD = -1;
8197
SmallString<128> Filename;
82-
std::error_code EC = sys::fs::createTemporaryFile(Name, "dot", FD, Filename);
98+
99+
// Windows can't always handle long paths, so limit the length of the name.
100+
std::string N = Name.str();
101+
N = N.substr(0, std::min<std::size_t>(N.size(), 140));
102+
103+
// Replace illegal characters in graph Filename with '_' if needed
104+
std::string CleansedName = replaceIllegalFilenameChars(N, '_');
105+
106+
std::error_code EC =
107+
sys::fs::createTemporaryFile(CleansedName, "dot", FD, Filename);
83108
if (EC) {
84109
errs() << "Error: " << EC.message() << "\n";
85110
return "";

0 commit comments

Comments
 (0)