From 80428fb35ff10da174aad17d1b10af5149262c37 Mon Sep 17 00:00:00 2001 From: Reid Kleckner Date: Sat, 29 Feb 2020 10:23:54 -0800 Subject: [PATCH] Avoid including FileSystem.h from MemoryBuffer.h Lots of headers pass around MemoryBuffer objects, but very few open them. Let those that do include FileSystem.h. Saves ~250 includes of Chrono.h & FileSystem.h: $ diff -u thedeps-before.txt thedeps-after.txt | grep '^[-+] ' | sort | uniq -c | sort -nr 254 - ../llvm/include/llvm/Support/FileSystem.h 253 - ../llvm/include/llvm/Support/Chrono.h 237 - ../llvm/include/llvm/Support/NativeFormatting.h 237 - ../llvm/include/llvm/Support/FormatProviders.h 192 - ../llvm/include/llvm/ADT/StringSwitch.h 190 - ../llvm/include/llvm/Support/FormatVariadicDetails.h ... This requires duplicating the file_t typedef, which is unfortunate. I sunk the choice of mapping mode down into the cpp file using variable template specializations instead of class members in headers. --- include/llvm/BinaryFormat/MsgPackReader.h | 1 + include/llvm/Bitstream/BitstreamReader.h | 1 + include/llvm/Support/MemoryBuffer.h | 22 +++++++++++---------- lib/BinaryFormat/AMDGPUMetadataVerifier.cpp | 1 + lib/ExecutionEngine/Orc/DebugUtils.cpp | 1 + lib/IR/Attributes.cpp | 1 + lib/IR/LLVMRemarkStreamer.cpp | 1 + lib/Support/MemoryBuffer.cpp | 16 ++++++++++++++- tools/llvm-exegesis/lib/BenchmarkResult.h | 2 ++ tools/llvm-exegesis/lib/Clustering.cpp | 1 + tools/llvm-mca/CodeRegion.h | 2 ++ unittests/ProfileData/SampleProfTest.cpp | 1 + unittests/Support/YAMLIOTest.cpp | 1 + utils/TableGen/GlobalISel/GIMatchTree.cpp | 1 + utils/TableGen/OptRSTEmitter.cpp | 1 + 15 files changed, 42 insertions(+), 11 deletions(-) diff --git a/include/llvm/BinaryFormat/MsgPackReader.h b/include/llvm/BinaryFormat/MsgPackReader.h index 2d332f531b2..bd760f7d7c8 100644 --- a/include/llvm/BinaryFormat/MsgPackReader.h +++ b/include/llvm/BinaryFormat/MsgPackReader.h @@ -33,6 +33,7 @@ #ifndef LLVM_SUPPORT_MSGPACKREADER_H #define LLVM_SUPPORT_MSGPACKREADER_H +#include "llvm/Support/Error.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/raw_ostream.h" #include diff --git a/include/llvm/Bitstream/BitstreamReader.h b/include/llvm/Bitstream/BitstreamReader.h index c476f60420f..0393d1a5186 100644 --- a/include/llvm/Bitstream/BitstreamReader.h +++ b/include/llvm/Bitstream/BitstreamReader.h @@ -18,6 +18,7 @@ #include "llvm/ADT/SmallVector.h" #include "llvm/Bitstream/BitCodes.h" #include "llvm/Support/Endian.h" +#include "llvm/Support/Error.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/MathExtras.h" #include "llvm/Support/MemoryBuffer.h" diff --git a/include/llvm/Support/MemoryBuffer.h b/include/llvm/Support/MemoryBuffer.h index b5196cd84cb..f47a8d2d334 100644 --- a/include/llvm/Support/MemoryBuffer.h +++ b/include/llvm/Support/MemoryBuffer.h @@ -19,7 +19,6 @@ #include "llvm/ADT/Twine.h" #include "llvm/Support/CBindingWrapping.h" #include "llvm/Support/ErrorOr.h" -#include "llvm/Support/FileSystem.h" #include #include #include @@ -28,6 +27,18 @@ namespace llvm { class MemoryBufferRef; +namespace sys { +namespace fs { +// Duplicated from FileSystem.h to avoid a dependency. +#if defined(_WIN32) +// A Win32 HANDLE is a typedef of void* +using file_t = void *; +#else +using file_t = int; +#endif +} // namespace fs +} // namespace sys + /// This interface provides simple read-only access to a block of memory, and /// provides simple methods for reading files and standard input into a memory /// buffer. In addition to basic access to the characters in the file, this @@ -48,9 +59,6 @@ protected: void init(const char *BufStart, const char *BufEnd, bool RequiresNullTerminator); - static constexpr sys::fs::mapped_file_region::mapmode Mapmode = - sys::fs::mapped_file_region::readonly; - public: MemoryBuffer(const MemoryBuffer &) = delete; MemoryBuffer &operator=(const MemoryBuffer &) = delete; @@ -156,9 +164,6 @@ class WritableMemoryBuffer : public MemoryBuffer { protected: WritableMemoryBuffer() = default; - static constexpr sys::fs::mapped_file_region::mapmode Mapmode = - sys::fs::mapped_file_region::priv; - public: using MemoryBuffer::getBuffer; using MemoryBuffer::getBufferEnd; @@ -218,9 +223,6 @@ class WriteThroughMemoryBuffer : public MemoryBuffer { protected: WriteThroughMemoryBuffer() = default; - static constexpr sys::fs::mapped_file_region::mapmode Mapmode = - sys::fs::mapped_file_region::readwrite; - public: using MemoryBuffer::getBuffer; using MemoryBuffer::getBufferEnd; diff --git a/lib/BinaryFormat/AMDGPUMetadataVerifier.cpp b/lib/BinaryFormat/AMDGPUMetadataVerifier.cpp index d927171d556..e8b9e12ce4c 100644 --- a/lib/BinaryFormat/AMDGPUMetadataVerifier.cpp +++ b/lib/BinaryFormat/AMDGPUMetadataVerifier.cpp @@ -12,6 +12,7 @@ //===----------------------------------------------------------------------===// #include "llvm/BinaryFormat/AMDGPUMetadataVerifier.h" +#include "llvm/ADT/StringSwitch.h" #include "llvm/Support/AMDGPUMetadata.h" namespace llvm { diff --git a/lib/ExecutionEngine/Orc/DebugUtils.cpp b/lib/ExecutionEngine/Orc/DebugUtils.cpp index c9e87ff737f..8d4a8107a71 100644 --- a/lib/ExecutionEngine/Orc/DebugUtils.cpp +++ b/lib/ExecutionEngine/Orc/DebugUtils.cpp @@ -8,6 +8,7 @@ #include "llvm/ExecutionEngine/Orc/DebugUtils.h" #include "llvm/Support/Debug.h" +#include "llvm/Support/FileSystem.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/Path.h" #include "llvm/Support/raw_ostream.h" diff --git a/lib/IR/Attributes.cpp b/lib/IR/Attributes.cpp index c41d6c8260b..6c3464abd36 100644 --- a/lib/IR/Attributes.cpp +++ b/lib/IR/Attributes.cpp @@ -23,6 +23,7 @@ #include "llvm/ADT/StringExtras.h" #include "llvm/ADT/StringRef.h" #include "llvm/ADT/Twine.h" +#include "llvm/ADT/StringSwitch.h" #include "llvm/Config/llvm-config.h" #include "llvm/IR/Function.h" #include "llvm/IR/LLVMContext.h" diff --git a/lib/IR/LLVMRemarkStreamer.cpp b/lib/IR/LLVMRemarkStreamer.cpp index 326523eaa10..96001ab42c3 100644 --- a/lib/IR/LLVMRemarkStreamer.cpp +++ b/lib/IR/LLVMRemarkStreamer.cpp @@ -15,6 +15,7 @@ #include "llvm/IR/DiagnosticInfo.h" #include "llvm/IR/Function.h" #include "llvm/IR/GlobalValue.h" +#include "llvm/Support/FileSystem.h" using namespace llvm; diff --git a/lib/Support/MemoryBuffer.cpp b/lib/Support/MemoryBuffer.cpp index e4027ca7bbf..e467daf42a3 100644 --- a/lib/Support/MemoryBuffer.cpp +++ b/lib/Support/MemoryBuffer.cpp @@ -162,6 +162,20 @@ MemoryBuffer::getFileSlice(const Twine &FilePath, uint64_t MapSize, //===----------------------------------------------------------------------===// namespace { + +template +constexpr sys::fs::mapped_file_region::mapmode Mapmode = + sys::fs::mapped_file_region::readonly; +template <> +constexpr sys::fs::mapped_file_region::mapmode Mapmode = + sys::fs::mapped_file_region::readonly; +template <> +constexpr sys::fs::mapped_file_region::mapmode Mapmode = + sys::fs::mapped_file_region::priv; +template <> +constexpr sys::fs::mapped_file_region::mapmode + Mapmode = sys::fs::mapped_file_region::readwrite; + /// Memory maps a file descriptor using sys::fs::mapped_file_region. /// /// This handles converting the offset into a legal offset on the platform. @@ -184,7 +198,7 @@ class MemoryBufferMMapFile : public MB { public: MemoryBufferMMapFile(bool RequiresNullTerminator, sys::fs::file_t FD, uint64_t Len, uint64_t Offset, std::error_code &EC) - : MFR(FD, MB::Mapmode, getLegalMapSize(Len, Offset), + : MFR(FD, Mapmode, getLegalMapSize(Len, Offset), getLegalMapOffset(Offset), EC) { if (!EC) { const char *Start = getStart(Len, Offset); diff --git a/tools/llvm-exegesis/lib/BenchmarkResult.h b/tools/llvm-exegesis/lib/BenchmarkResult.h index 29bf9963c99..1788e749068 100644 --- a/tools/llvm-exegesis/lib/BenchmarkResult.h +++ b/tools/llvm-exegesis/lib/BenchmarkResult.h @@ -28,6 +28,8 @@ #include namespace llvm { +class Error; + namespace exegesis { struct InstructionBenchmarkKey { diff --git a/tools/llvm-exegesis/lib/Clustering.cpp b/tools/llvm-exegesis/lib/Clustering.cpp index 33a8e018f51..c9070ef554d 100644 --- a/tools/llvm-exegesis/lib/Clustering.cpp +++ b/tools/llvm-exegesis/lib/Clustering.cpp @@ -14,6 +14,7 @@ #include #include #include +#include namespace llvm { namespace exegesis { diff --git a/tools/llvm-mca/CodeRegion.h b/tools/llvm-mca/CodeRegion.h index cabb4a5d448..d2b05fa80c5 100644 --- a/tools/llvm-mca/CodeRegion.h +++ b/tools/llvm-mca/CodeRegion.h @@ -35,8 +35,10 @@ #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/StringMap.h" #include "llvm/ADT/StringRef.h" #include "llvm/MC/MCInst.h" +#include "llvm/Support/Error.h" #include "llvm/Support/SMLoc.h" #include "llvm/Support/SourceMgr.h" #include diff --git a/unittests/ProfileData/SampleProfTest.cpp b/unittests/ProfileData/SampleProfTest.cpp index 4c58d25cedb..866a9ec21f7 100644 --- a/unittests/ProfileData/SampleProfTest.cpp +++ b/unittests/ProfileData/SampleProfTest.cpp @@ -16,6 +16,7 @@ #include "llvm/ProfileData/SampleProfWriter.h" #include "llvm/Support/Casting.h" #include "llvm/Support/ErrorOr.h" +#include "llvm/Support/FileSystem.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/raw_ostream.h" #include "gtest/gtest.h" diff --git a/unittests/Support/YAMLIOTest.cpp b/unittests/Support/YAMLIOTest.cpp index df111e7f6ea..d86489cf756 100644 --- a/unittests/Support/YAMLIOTest.cpp +++ b/unittests/Support/YAMLIOTest.cpp @@ -9,6 +9,7 @@ #include "llvm/ADT/BitmaskEnum.h" #include "llvm/ADT/StringMap.h" #include "llvm/ADT/StringRef.h" +#include "llvm/ADT/StringSwitch.h" #include "llvm/ADT/Twine.h" #include "llvm/Support/Casting.h" #include "llvm/Support/Endian.h" diff --git a/utils/TableGen/GlobalISel/GIMatchTree.cpp b/utils/TableGen/GlobalISel/GIMatchTree.cpp index 4884bdadea9..8eef93393a1 100644 --- a/utils/TableGen/GlobalISel/GIMatchTree.cpp +++ b/utils/TableGen/GlobalISel/GIMatchTree.cpp @@ -10,6 +10,7 @@ #include "../CodeGenInstruction.h" +#include "llvm/Support/Debug.h" #include "llvm/Support/Format.h" #include "llvm/Support/ScopedPrinter.h" #include "llvm/Support/raw_ostream.h" diff --git a/utils/TableGen/OptRSTEmitter.cpp b/utils/TableGen/OptRSTEmitter.cpp index 3102f378bc1..5e44d033109 100644 --- a/utils/TableGen/OptRSTEmitter.cpp +++ b/utils/TableGen/OptRSTEmitter.cpp @@ -9,6 +9,7 @@ #include "OptEmitter.h" #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/SmallString.h" +#include "llvm/ADT/StringMap.h" #include "llvm/ADT/Twine.h" #include "llvm/TableGen/Error.h" #include "llvm/TableGen/Record.h"