Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions clang/tools/driver/cc1as_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,8 @@ getOutputStream(StringRef Path, DiagnosticsEngine &Diags, bool Binary) {
}

static bool ExecuteAssemblerImpl(AssemblerInvocation &Opts,
DiagnosticsEngine &Diags) {
DiagnosticsEngine &Diags,
IntrusiveRefCntPtr<vfs::FileSystem> VFS) {
// Get the target specific parser.
std::string Error;
const Target *TheTarget = TargetRegistry::lookupTarget(Opts.Triple, Error);
Expand All @@ -440,6 +441,7 @@ static bool ExecuteAssemblerImpl(AssemblerInvocation &Opts,
// Record the location of the include directories so that the lexer can find
// it later.
SrcMgr.setIncludeDirs(Opts.IncludePaths);
SrcMgr.setVirtualFileSystem(VFS);

std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(Opts.Triple));
assert(MRI && "Unable to create target register info!");
Expand Down Expand Up @@ -632,8 +634,9 @@ static bool ExecuteAssemblerImpl(AssemblerInvocation &Opts,
}

static bool ExecuteAssembler(AssemblerInvocation &Opts,
DiagnosticsEngine &Diags) {
bool Failed = ExecuteAssemblerImpl(Opts, Diags);
DiagnosticsEngine &Diags,
IntrusiveRefCntPtr<vfs::FileSystem> VFS) {
bool Failed = ExecuteAssemblerImpl(Opts, Diags, VFS);

// Delete output file if there were errors.
if (Failed) {
Expand Down Expand Up @@ -714,7 +717,7 @@ int cc1as_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {
}

// Execute the invocation, unless there were parsing errors.
bool Failed = Diags.hasErrorOccurred() || ExecuteAssembler(Asm, Diags);
bool Failed = Diags.hasErrorOccurred() || ExecuteAssembler(Asm, Diags, VFS);

// If any timers were active but haven't been destroyed yet, print their
// results now.
Expand Down
8 changes: 8 additions & 0 deletions llvm/include/llvm/CodeGen/AsmPrinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#define LLVM_CODEGEN_ASMPRINTER_H

#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/IntrusiveRefCntPtr.h"
#include "llvm/ADT/MapVector.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/SmallVector.h"
Expand Down Expand Up @@ -87,6 +88,10 @@ namespace remarks {
class RemarkStreamer;
}

namespace vfs {
class FileSystem;
}

/// This class is intended to be used as a driving class for all asm writers.
class LLVM_ABI AsmPrinter : public MachineFunctionPass {
public:
Expand All @@ -105,6 +110,9 @@ class LLVM_ABI AsmPrinter : public MachineFunctionPass {
/// generating (such as the current section etc).
std::unique_ptr<MCStreamer> OutStreamer;

/// The VFS to resolve asm include directives.
IntrusiveRefCntPtr<vfs::FileSystem> VFS;

/// The current machine function.
MachineFunction *MF = nullptr;

Expand Down
23 changes: 19 additions & 4 deletions llvm/include/llvm/Support/SourceMgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#ifndef LLVM_SUPPORT_SOURCEMGR_H
#define LLVM_SUPPORT_SOURCEMGR_H

#include "llvm/ADT/IntrusiveRefCntPtr.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/MemoryBuffer.h"
Expand All @@ -23,6 +24,10 @@

namespace llvm {

namespace vfs {
class FileSystem;
} // end namespace vfs

class raw_ostream;
class SMDiagnostic;
class SMFixIt;
Expand Down Expand Up @@ -91,15 +96,25 @@ class SourceMgr {
DiagHandlerTy DiagHandler = nullptr;
void *DiagContext = nullptr;

// Optional file system for finding include files.
IntrusiveRefCntPtr<vfs::FileSystem> FS;

bool isValidBufferID(unsigned i) const { return i && i <= Buffers.size(); }

public:
SourceMgr() = default;
/// Create new source manager without support for include files.
SourceMgr();
/// Create new source manager with the capability of finding include files
/// via the provided file system.
explicit SourceMgr(IntrusiveRefCntPtr<vfs::FileSystem> FS);
SourceMgr(const SourceMgr &) = delete;
SourceMgr &operator=(const SourceMgr &) = delete;
SourceMgr(SourceMgr &&) = default;
SourceMgr &operator=(SourceMgr &&) = default;
~SourceMgr() = default;
SourceMgr(SourceMgr &&);
SourceMgr &operator=(SourceMgr &&);
~SourceMgr();

IntrusiveRefCntPtr<vfs::FileSystem> getVirtualFileSystem() const;
void setVirtualFileSystem(IntrusiveRefCntPtr<vfs::FileSystem> FS);

/// Return the include directories of this source manager.
ArrayRef<std::string> getIncludeDirs() const { return IncludeDirectories; }
Expand Down
2 changes: 2 additions & 0 deletions llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/VCSRevision.h"
#include "llvm/Support/VirtualFileSystem.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Target/TargetLoweringObjectFile.h"
#include "llvm/Target/TargetMachine.h"
Expand Down Expand Up @@ -476,6 +477,7 @@ void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const {
}

bool AsmPrinter::doInitialization(Module &M) {
VFS = vfs::getRealFileSystem();
auto *MMIWP = getAnalysisIfAvailable<MachineModuleInfoWrapperPass>();
MMI = MMIWP ? &MMIWP->getMMI() : nullptr;
HasSplitStack = false;
Expand Down
2 changes: 2 additions & 0 deletions llvm/lib/CodeGen/AsmPrinter/AsmPrinterInlineAsm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/VirtualFileSystem.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Target/TargetMachine.h"
using namespace llvm;
Expand Down Expand Up @@ -98,6 +99,7 @@ void AsmPrinter::emitInlineAsm(StringRef Str, const MCSubtargetInfo &STI,
unsigned BufNum = addInlineAsmDiagBuffer(Str, LocMDNode);
SourceMgr &SrcMgr = *MMI->getContext().getInlineSourceManager();
SrcMgr.setIncludeDirs(MCOptions.IASSearchPaths);
SrcMgr.setVirtualFileSystem(VFS);

std::unique_ptr<MCAsmParser> Parser(
createMCAsmParser(SrcMgr, OutContext, *OutStreamer, *MAI, BufNum));
Expand Down
24 changes: 22 additions & 2 deletions llvm/lib/Support/SourceMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/SMLoc.h"
#include "llvm/Support/VirtualFileSystem.h"
#include "llvm/Support/WithColor.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
Expand All @@ -38,6 +39,22 @@ using namespace llvm;

static const size_t TabStop = 8;

// Out of line to avoid needing definition of vfs::FileSystem in header.
SourceMgr::SourceMgr() = default;
SourceMgr::SourceMgr(IntrusiveRefCntPtr<vfs::FileSystem> FS)
: FS(std::move(FS)) {}
SourceMgr::SourceMgr(SourceMgr &&) = default;
SourceMgr &SourceMgr::operator=(SourceMgr &&) = default;
SourceMgr::~SourceMgr() = default;

IntrusiveRefCntPtr<vfs::FileSystem> SourceMgr::getVirtualFileSystem() const {
return FS;
}

void SourceMgr::setVirtualFileSystem(IntrusiveRefCntPtr<vfs::FileSystem> FS) {
this->FS = std::move(FS);
}

unsigned SourceMgr::AddIncludeFile(const std::string &Filename,
SMLoc IncludeLoc,
std::string &IncludedFile) {
Expand All @@ -52,16 +69,19 @@ unsigned SourceMgr::AddIncludeFile(const std::string &Filename,
ErrorOr<std::unique_ptr<MemoryBuffer>>
SourceMgr::OpenIncludeFile(const std::string &Filename,
std::string &IncludedFile) {
if (!FS)
reportFatalInternalError("Opening include file from SourceMgr without VFS");

ErrorOr<std::unique_ptr<MemoryBuffer>> NewBufOrErr =
MemoryBuffer::getFile(Filename);
FS->getBufferForFile(Filename);

SmallString<64> Buffer(Filename);
// If the file didn't exist directly, see if it's in an include path.
for (unsigned i = 0, e = IncludeDirectories.size(); i != e && !NewBufOrErr;
++i) {
Buffer = IncludeDirectories[i];
sys::path::append(Buffer, Filename);
NewBufOrErr = MemoryBuffer::getFile(Buffer);
NewBufOrErr = FS->getBufferForFile(Buffer);
}

if (NewBufOrErr)
Expand Down
2 changes: 2 additions & 0 deletions llvm/lib/TableGen/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "llvm/Support/SMLoc.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/ToolOutputFile.h"
#include "llvm/Support/VirtualFileSystem.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/TableGen/Error.h"
#include "llvm/TableGen/Record.h"
Expand Down Expand Up @@ -128,6 +129,7 @@ int llvm::TableGenMain(const char *argv0,
// Record the location of the include directory so that the lexer can find
// it later.
SrcMgr.setIncludeDirs(IncludeDirs);
SrcMgr.setVirtualFileSystem(vfs::getRealFileSystem());

TGParser Parser(SrcMgr, MacroNames, Records, NoWarnOnUnusedTemplateArgs);

Expand Down
2 changes: 2 additions & 0 deletions llvm/lib/TableGen/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "llvm/TableGen/Parser.h"
#include "TGParser.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/VirtualFileSystem.h"
#include "llvm/TableGen/Record.h"

using namespace llvm;
Expand All @@ -21,6 +22,7 @@ bool llvm::TableGenParseFile(SourceMgr &InputSrcMgr, RecordKeeper &Records) {
SrcMgr = SourceMgr();
SrcMgr.takeSourceBuffersFrom(InputSrcMgr);
SrcMgr.setIncludeDirs(InputSrcMgr.getIncludeDirs());
SrcMgr.setVirtualFileSystem(InputSrcMgr.getVirtualFileSystem());
SrcMgr.setDiagHandler(InputSrcMgr.getDiagHandler(),
InputSrcMgr.getDiagContext());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/ToolOutputFile.h"
#include "llvm/Support/VirtualFileSystem.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/TargetParser/Host.h"
#include "llvm/TargetParser/SubtargetFeature.h"
Expand Down Expand Up @@ -142,6 +143,7 @@ int AssembleOneInput(const uint8_t *Data, size_t Size) {

static const std::vector<std::string> NoIncludeDirs;
SrcMgr.setIncludeDirs(NoIncludeDirs);
SrcMgr.setVirtualFileSystem(vfs::getRealFileSystem());

static std::string ArchName;
std::string Error;
Expand Down
2 changes: 2 additions & 0 deletions llvm/tools/llvm-mc/llvm-mc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/TimeProfiler.h"
#include "llvm/Support/ToolOutputFile.h"
#include "llvm/Support/VirtualFileSystem.h"
#include "llvm/Support/WithColor.h"
#include "llvm/TargetParser/Host.h"
#include <memory>
Expand Down Expand Up @@ -439,6 +440,7 @@ int main(int argc, char **argv) {
// Record the location of the include directories so that the lexer can find
// it later.
SrcMgr.setIncludeDirs(IncludeDirs);
SrcMgr.setVirtualFileSystem(vfs::getRealFileSystem());

std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TheTriple));
assert(MRI && "Unable to create target register info!");
Expand Down
2 changes: 2 additions & 0 deletions llvm/tools/llvm-ml/llvm-ml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/ToolOutputFile.h"
#include "llvm/Support/VirtualFileSystem.h"
#include "llvm/Support/WithColor.h"
#include "llvm/TargetParser/Host.h"
#include <ctime>
Expand Down Expand Up @@ -313,6 +314,7 @@ int llvm_ml_main(int Argc, char **Argv, const llvm::ToolContext &) {
}
}
SrcMgr.setIncludeDirs(IncludeDirs);
SrcMgr.setVirtualFileSystem(vfs::getRealFileSystem());

std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TheTriple));
assert(MRI && "Unable to create target register info!");
Expand Down
11 changes: 11 additions & 0 deletions llvm/unittests/Support/SourceMgrTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/VirtualFileSystem.h"
#include "llvm/Support/raw_ostream.h"
#include "gtest/gtest.h"

Expand Down Expand Up @@ -506,3 +507,13 @@ TEST_F(SourceMgrTest, PrintWithoutLoc) {
Diag.print(nullptr, OS, false, false, false);
EXPECT_EQ("message\n", Output);
}

TEST_F(SourceMgrTest, IncludeDirs) {
auto VFS = makeIntrusiveRefCnt<vfs::InMemoryFileSystem>();
VFS->addFile("include/file", 0, MemoryBuffer::getMemBuffer("contents"));
SM.setVirtualFileSystem(std::move(VFS));
SM.setIncludeDirs({"include"});
std::string ResolvedPath;
unsigned NumBuffers = SM.AddIncludeFile("file", SMLoc(), ResolvedPath);
EXPECT_EQ(NumBuffers, 1u);
}
6 changes: 4 additions & 2 deletions mlir/lib/IR/Diagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -378,8 +378,10 @@ struct SourceMgrDiagnosticHandlerImpl {
}

// Otherwise, try to load the source file.
std::string ignored;
unsigned id = mgr.AddIncludeFile(std::string(filename), SMLoc(), ignored);
auto bufferOrErr = llvm::MemoryBuffer::getFile(filename);
if (!bufferOrErr)
return 0;
unsigned id = mgr.AddNewSourceBuffer(std::move(*bufferOrErr), SMLoc());
filenameToBufId[filename] = id;
Comment on lines +381 to 385
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello! Is this change intentional? It broke a downstream tool ;) I think a new code doesn't search include directories (and also not using VFS as well?).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean the change in this file in particular? How does it break downstream? The change is intentional. The intention of this PR was to force clients to set up SourceMgr with VFS to use includes, or load files themselves however they please and just pass MemoryBuffer to SourceMgr. In the end, I needed to roll this back a bit: #163862.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mlir/lib/IR/Diagnostics.cpp specifically. AddIncludeFile uses OpenIncludeFile which searches files under include directories. I understand the intention of the PR but the change in mlir/lib/IR/Diagnostics.cpp looks rather unrelated to VFS. I think AddIncludeFile also uses VFS now (#163862) so I don't think this change is necessary. Maybe I'm missing something?

Note that setting setVirtualFileSystem(llvm::vfs::getRealFileSystem()) didn't fix our issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that makes sense, sorry for the breakage. With #163862 this is no longer necessary. Fix is here: #164464.

return id;
}
Expand Down
2 changes: 2 additions & 0 deletions mlir/lib/Tools/PDLL/Parser/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/SaveAndRestore.h"
#include "llvm/Support/ScopedPrinter.h"
#include "llvm/Support/VirtualFileSystem.h"
#include "llvm/TableGen/Error.h"
#include "llvm/TableGen/Parser.h"
#include <optional>
Expand Down Expand Up @@ -828,6 +829,7 @@ LogicalResult Parser::parseTdInclude(StringRef filename, llvm::SMRange fileLoc,
llvm::SourceMgr tdSrcMgr;
tdSrcMgr.AddNewSourceBuffer(std::move(*includeBuffer), SMLoc());
tdSrcMgr.setIncludeDirs(parserSrcMgr.getIncludeDirs());
tdSrcMgr.setVirtualFileSystem(llvm::vfs::getRealFileSystem());

// This class provides a context argument for the llvm::SourceMgr diagnostic
// handler.
Expand Down
2 changes: 2 additions & 0 deletions mlir/lib/Tools/mlir-pdll-lsp-server/PDLLServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/LSP/Logging.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/VirtualFileSystem.h"
#include <optional>

using namespace mlir;
Expand Down Expand Up @@ -402,6 +403,7 @@ PDLDocument::PDLDocument(const llvm::lsp::URIForFile &uri, StringRef contents,
llvm::append_range(includeDirs, extraDirs);

sourceMgr.setIncludeDirs(includeDirs);
sourceMgr.setVirtualFileSystem(llvm::vfs::getRealFileSystem());
sourceMgr.AddNewSourceBuffer(std::move(memBuffer), SMLoc());

astContext.getDiagEngine().setHandlerFn([&](const ast::Diagnostic &diag) {
Expand Down
2 changes: 2 additions & 0 deletions mlir/lib/Tools/tblgen-lsp-server/TableGenServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "llvm/Support/LSP/Logging.h"
#include "llvm/Support/LSP/Protocol.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/VirtualFileSystem.h"
#include "llvm/TableGen/Parser.h"
#include "llvm/TableGen/Record.h"
#include <optional>
Expand Down Expand Up @@ -448,6 +449,7 @@ void TableGenTextFile::initialize(
return;
}
sourceMgr.setIncludeDirs(includeDirs);
sourceMgr.setVirtualFileSystem(llvm::vfs::getRealFileSystem());
sourceMgr.AddNewSourceBuffer(std::move(memBuffer), SMLoc());

// This class provides a context argument for the SourceMgr diagnostic
Expand Down
2 changes: 2 additions & 0 deletions mlir/tools/mlir-pdll/mlir-pdll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "llvm/Support/InitLLVM.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/ToolOutputFile.h"
#include "llvm/Support/VirtualFileSystem.h"
#include <set>

using namespace mlir;
Expand All @@ -41,6 +42,7 @@ processBuffer(raw_ostream &os, std::unique_ptr<llvm::MemoryBuffer> chunkBuffer,
bool dumpODS, std::set<std::string> *includedFiles) {
llvm::SourceMgr sourceMgr;
sourceMgr.setIncludeDirs(includeDirs);
sourceMgr.setVirtualFileSystem(llvm::vfs::getRealFileSystem());
sourceMgr.AddNewSourceBuffer(std::move(chunkBuffer), SMLoc());

// If we are dumping ODS information, also enable documentation to ensure the
Expand Down