This is an archive of the discontinued LLVM Phabricator instance.

[TableGen][SourceMgr] Correctly append filename to include directories
ClosedPublic

Authored by zero9178 on Jan 8 2023, 4:10 AM.

Details

Summary

The current implementation unconditionally appends the system path separator with the filename to the include directory. This is not correct in edge cases however, such as when specifying / as include directory (on Unix systems) or just \ on Windows.
This patch fixes that by using sys::path::append, which already has the required logic to correctly implement this.

While this is technically only a change in the SourceMgr class, I think the main user of that class, and the include mechanism, is TableGen.
No test attached because no behavioral difference is observable without trying to access the root directory of the users filesystem.

The motivation for this change is a rather funny story, as this actually fixes a performance problem when running check-mlir on Windows.
Some tests for mlir-pdll-lsp-server lead to adding \ as include directory in TableGen (which is a valid absolute path on Windows!). Due to the unconditional append, the created filepath would then be of the form \\<dir>\... which is also a valid path on Windows, but is a network path. On my machine it'd then attempt to access the network and find a machine with the name <dir> and the file there. This call would take several seconds, leading to some tests in mlir-pdll-lsp-server taking 2 minutes on my machine.

Running check-mlir after this patch reduces the runtime on my machine from 161 seconds to 6 seconds.

Diff Detail

Event Timeline

zero9178 created this revision.Jan 8 2023, 4:10 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 8 2023, 4:10 AM
zero9178 requested review of this revision.Jan 8 2023, 4:10 AM
jpienaar added inline comments.Jan 8 2023, 12:12 PM
llvm/lib/Support/SourceMgr.cpp
58

How does this compare to using std::filesystem::path? (Creating a path and concatenating with operation/ and then using string method - seems like all could be (std::filesystem::path(IncludeDirectories[i]) / Filename).string().

66

Shouldn't this be only set if there was no error?

zero9178 marked an inline comment as done.Jan 8 2023, 12:24 PM
zero9178 added inline comments.
llvm/lib/Support/SourceMgr.cpp
58

The std::filesystem solution you proposed acts the same way as llvm::sys::path::append (problematic case tested with both MSVC STL and libc++ on Windows).
That said, this would be the first usage of std::filesystem in the monorepo (minus libc++ ofc), and not something that is currently supported by the minimum compiler versions I believe.

zero9178 updated this revision to Diff 487217.Jan 8 2023, 12:24 PM
zero9178 marked an inline comment as done.

Address review comments:

  • Only update IncludeFile if no errors occured
zero9178 marked an inline comment as done.Jan 8 2023, 12:25 PM
jpienaar accepted this revision.Jan 8 2023, 5:17 PM

Looks reasonable to me, thanks.

This revision is now accepted and ready to land.Jan 8 2023, 5:17 PM

Sorry, I'm reverting this change because it introduced a behavior change.

Previously, the function tried to open the file Filename starting from the current working directory. Now it tries to open IncludedFile starting from the current working directory, and if that fails, searches for Filename in the include path.

Sorry, I'm reverting this change because it introduced a behavior change.

Previously, the function tried to open the file Filename starting from the current working directory. Now it tries to open IncludedFile starting from the current working directory, and if that fails, searches for Filename in the include path.

I should be sorry not you! This was an obvious oversight from me.

I have pushed a commit to fix this, hope I am not racing with your revert commit

Wow, great catch, thank you for fixing this! SourceMgr is used by more than just tblgen btw, so fixing this is great. 161 seconds to 6 seconds is quite the speedup too :-)