From e686a11523326b7161419f7c4e5723cbdd70e235 Mon Sep 17 00:00:00 2001 From: River Riddle Date: Sat, 11 May 2019 11:59:01 -0700 Subject: [PATCH] Remove several heavy includes from Diagnostics.h by a moving a couple of fields/methods to the .cpp file. -- PiperOrigin-RevId: 247768443 --- mlir/include/mlir/IR/Diagnostics.h | 34 +++++++--------- mlir/lib/IR/Diagnostics.cpp | 80 ++++++++++++++++++++++++++++---------- 2 files changed, 74 insertions(+), 40 deletions(-) diff --git a/mlir/include/mlir/IR/Diagnostics.h b/mlir/include/mlir/IR/Diagnostics.h index 04e35f2..a14838ad 100644 --- a/mlir/include/mlir/IR/Diagnostics.h +++ b/mlir/include/mlir/IR/Diagnostics.h @@ -24,9 +24,6 @@ #include "mlir/IR/Location.h" #include "mlir/Support/STLExtras.h" -#include "llvm/ADT/SmallString.h" -#include "llvm/ADT/StringMap.h" -#include "llvm/ADT/Twine.h" #include namespace llvm { @@ -200,30 +197,23 @@ public: /// Stream operator for inserting new diagnostic arguments. template - typename std::enable_if::value || - std::is_integral::value, + typename std::enable_if::value, Diagnostic &>::type operator<<(Arg &&val) { arguments.push_back(DiagnosticArgument(std::forward(val))); return *this; } + + /// Stream in a string literal. Diagnostic &operator<<(const char *val) { arguments.push_back(DiagnosticArgument(val)); return *this; } - Diagnostic &operator<<(char val) { return *this << Twine(val); } - Diagnostic &operator<<(const Twine &val) { - // Allocate memory to hold this string. - llvm::SmallString<0> data; - auto strRef = val.toStringRef(data); - strings.push_back(std::unique_ptr(new char[strRef.size()])); - memcpy(&strings.back()[0], strRef.data(), strRef.size()); - - // Add the new string to the argument list. - strRef = StringRef(&strings.back()[0], strRef.size()); - arguments.push_back(DiagnosticArgument(strRef)); - return *this; - } + + /// Stream in a Twine argument. + Diagnostic &operator<<(char val); + Diagnostic &operator<<(const Twine &val); + Diagnostic &operator<<(Twine &&val); /// Stream in an Identifier. Diagnostic &operator<<(Identifier val); @@ -435,10 +425,15 @@ private: // SourceMgrDiagnosticHandler //===----------------------------------------------------------------------===// +namespace detail { +struct SourceMgrDiagnosticHandlerImpl; +} // end namespace detail + /// This class is a utility diagnostic handler for use with llvm::SourceMgr. class SourceMgrDiagnosticHandler { public: SourceMgrDiagnosticHandler(llvm::SourceMgr &mgr, MLIRContext *ctx); + ~SourceMgrDiagnosticHandler(); /// Emit the given diagnostic information with the held source manager. void emitDiagnostic(Location loc, Twine message, DiagnosticSeverity kind); @@ -455,8 +450,7 @@ private: /// Convert a location into the given memory buffer into an SMLoc. llvm::SMLoc convertLocToSMLoc(Location loc); - /// Mapping between file name and buffer pointer. - llvm::StringMap filenameToBuf; + std::unique_ptr impl; }; //===----------------------------------------------------------------------===// diff --git a/mlir/lib/IR/Diagnostics.cpp b/mlir/lib/IR/Diagnostics.cpp index b03a30d..574a135 100644 --- a/mlir/lib/IR/Diagnostics.cpp +++ b/mlir/lib/IR/Diagnostics.cpp @@ -21,8 +21,8 @@ #include "mlir/IR/Location.h" #include "mlir/IR/MLIRContext.h" #include "mlir/IR/Types.h" +#include "llvm/ADT/SmallString.h" #include "llvm/ADT/StringMap.h" -#include "llvm/ADT/Twine.h" #include "llvm/Support/Mutex.h" #include "llvm/Support/Regex.h" #include "llvm/Support/SourceMgr.h" @@ -86,6 +86,31 @@ void DiagnosticArgument::print(raw_ostream &os) const { // Diagnostic //===----------------------------------------------------------------------===// +/// Convert a Twine to a StringRef. Memory used for generating the StringRef is +/// stored in 'strings'. +static StringRef twineToStrRef(const Twine &val, + std::vector> &strings) { + // Allocate memory to hold this string. + llvm::SmallString<64> data; + auto strRef = val.toStringRef(data); + strings.push_back(std::unique_ptr(new char[strRef.size()])); + memcpy(&strings.back()[0], strRef.data(), strRef.size()); + + // Return a reference to the new string. + return StringRef(&strings.back()[0], strRef.size()); +} + +/// Stream in a Twine argument. +Diagnostic &Diagnostic::operator<<(char val) { return *this << Twine(val); } +Diagnostic &Diagnostic::operator<<(const Twine &val) { + arguments.push_back(DiagnosticArgument(twineToStrRef(val, strings))); + return *this; +} +Diagnostic &Diagnostic::operator<<(Twine &&val) { + arguments.push_back(DiagnosticArgument(twineToStrRef(val, strings))); + return *this; +} + /// Stream in an Identifier. Diagnostic &Diagnostic::operator<<(Identifier val) { // An identifier is stored in the context, so we don't need to worry about the @@ -226,6 +251,37 @@ void DiagnosticEngine::emit(Diagnostic diag) { //===----------------------------------------------------------------------===// // SourceMgrDiagnosticHandler //===----------------------------------------------------------------------===// +namespace mlir { +namespace detail { +struct SourceMgrDiagnosticHandlerImpl { + /// Get a memory buffer for the given file, or nullptr if one is not found. + const llvm::MemoryBuffer *getBufferForFile(llvm::SourceMgr &mgr, + StringRef filename) { + // Check for an existing mapping to the buffer id for this file. + auto bufferIt = filenameToBuf.find(filename); + if (bufferIt != filenameToBuf.end()) + return bufferIt->second; + + // Look for a buffer in the manager that has this filename. + for (unsigned i = 1, e = mgr.getNumBuffers() + 1; i != e; ++i) { + auto *buf = mgr.getMemoryBuffer(i); + if (buf->getBufferIdentifier() == filename) + return filenameToBuf[filename] = buf; + } + + // Otherwise, try to load the source file. + const llvm::MemoryBuffer *newBuf = nullptr; + std::string ignored; + if (auto newBufID = mgr.AddIncludeFile(filename, llvm::SMLoc(), ignored)) + newBuf = mgr.getMemoryBuffer(newBufID); + return filenameToBuf[filename] = newBuf; + } + + /// Mapping between file name and buffer pointer. + llvm::StringMap filenameToBuf; +}; +} // end namespace detail +} // end namespace mlir /// Return a processable FileLineColLoc from the given location. static llvm::Optional getFileLineColLoc(Location loc) { @@ -264,7 +320,7 @@ static llvm::SourceMgr::DiagKind getDiagKind(DiagnosticSeverity kind) { SourceMgrDiagnosticHandler::SourceMgrDiagnosticHandler(llvm::SourceMgr &mgr, MLIRContext *ctx) - : mgr(mgr) { + : mgr(mgr), impl(new SourceMgrDiagnosticHandlerImpl()) { // Register a simple diagnostic handler. ctx->getDiagEngine().setHandler([this](Diagnostic diag) { // Emit the diagnostic. @@ -275,6 +331,7 @@ SourceMgrDiagnosticHandler::SourceMgrDiagnosticHandler(llvm::SourceMgr &mgr, emitDiagnostic(note.getLocation(), note.str(), note.getSeverity()); }); } +SourceMgrDiagnosticHandler::~SourceMgrDiagnosticHandler() {} void SourceMgrDiagnosticHandler::emitDiagnostic(Location loc, Twine message, DiagnosticSeverity kind) { @@ -284,24 +341,7 @@ void SourceMgrDiagnosticHandler::emitDiagnostic(Location loc, Twine message, /// Get a memory buffer for the given file, or nullptr if one is not found. const llvm::MemoryBuffer * SourceMgrDiagnosticHandler::getBufferForFile(StringRef filename) { - // Check for an existing mapping to the buffer id for this file. - auto bufferIt = filenameToBuf.find(filename); - if (bufferIt != filenameToBuf.end()) - return bufferIt->second; - - // Look for a buffer in the manager that has this filename. - for (unsigned i = 1, e = mgr.getNumBuffers() + 1; i != e; ++i) { - auto *buf = mgr.getMemoryBuffer(i); - if (buf->getBufferIdentifier() == filename) - return filenameToBuf[filename] = buf; - } - - // Otherwise, try to load the source file. - const llvm::MemoryBuffer *newBuf = nullptr; - std::string ignored; - if (auto newBufID = mgr.AddIncludeFile(filename, llvm::SMLoc(), ignored)) - newBuf = mgr.getMemoryBuffer(newBufID); - return filenameToBuf[filename] = newBuf; + return impl->getBufferForFile(mgr, filename); } /// Get a memory buffer for the given file, or the main file of the source -- 2.7.4