Reid Kleckner af450eabb9 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.
2020-02-29 12:30:23 -08:00

70 lines
2.1 KiB
C++

//===---------- DebugUtils.cpp - Utilities for debugging ORC JITs ---------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#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"
#define DEBUG_TYPE "orc"
namespace llvm {
namespace orc {
DumpObjects::DumpObjects(std::string DumpDir, std::string IdentifierOverride)
: DumpDir(std::move(DumpDir)),
IdentifierOverride(std::move(IdentifierOverride)) {
/// Discard any trailing separators.
while (!this->DumpDir.empty() &&
sys::path::is_separator(this->DumpDir.back()))
this->DumpDir.pop_back();
}
Expected<std::unique_ptr<MemoryBuffer>>
DumpObjects::operator()(std::unique_ptr<MemoryBuffer> Obj) {
size_t Idx = 1;
std::string DumpPathStem;
raw_string_ostream(DumpPathStem)
<< DumpDir << (DumpDir.empty() ? "" : "/") << getBufferIdentifier(*Obj);
std::string DumpPath = DumpPathStem + ".o";
while (sys::fs::exists(DumpPath)) {
DumpPath.clear();
raw_string_ostream(DumpPath) << DumpPathStem << "." << (++Idx) << ".o";
}
LLVM_DEBUG({
dbgs() << "Dumping object buffer [ " << (const void *)Obj->getBufferStart()
<< " -- " << (const void *)(Obj->getBufferEnd() - 1) << " ] to "
<< DumpPath << "\n";
});
std::error_code EC;
raw_fd_ostream DumpStream(DumpPath, EC);
if (EC)
return errorCodeToError(EC);
DumpStream.write(Obj->getBufferStart(), Obj->getBufferSize());
return std::move(Obj);
}
StringRef DumpObjects::getBufferIdentifier(MemoryBuffer &B) {
if (!IdentifierOverride.empty())
return IdentifierOverride;
StringRef Identifier = B.getBufferIdentifier();
Identifier.consume_back(".o");
return Identifier;
}
} // End namespace orc.
} // End namespace llvm.