Skip to content

Commit bd9ae78

Browse files
author
marcrasi
authored
[tensorflow] make SourceKit use an inmemory filesystem for clang module cache (#25438)
1 parent 99b0ad3 commit bd9ae78

File tree

15 files changed

+132
-3
lines changed

15 files changed

+132
-3
lines changed

include/swift/ClangImporter/ClangImporterOptions.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#ifndef SWIFT_CLANGIMPORTER_CLANGIMPORTEROPTIONS_H
1414
#define SWIFT_CLANGIMPORTER_CLANGIMPORTEROPTIONS_H
1515

16+
// SWIFT_ENABLE_TENSORFLOW
17+
#include "clang/Basic/InMemoryOutputFileSystem.h"
1618
#include "llvm/ADT/Hashing.h"
1719

1820
#include <string>
@@ -100,6 +102,12 @@ class ClangImporterOptions {
100102
/// When set, don't enforce warnings with -Werror.
101103
bool DebuggerSupport = false;
102104

105+
// SWIFT_ENABLE_TENSORFLOW
106+
/// When set, clang writes its output files (module caches) to this instead
107+
/// of to the real filesystem.
108+
llvm::IntrusiveRefCntPtr<clang::InMemoryOutputFileSystem>
109+
InMemoryOutputFileSystem;
110+
103111
/// Return a hash code of any components from these options that should
104112
/// contribute to a Swift Bridging PCH hash.
105113
llvm::hash_code getPCHHashComponents() const {

lib/ClangImporter/ClangImporter.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,14 +1031,26 @@ ClangImporter::create(ASTContext &ctx,
10311031

10321032
// Set up the file manager.
10331033
{
1034-
if (ctx.SourceMgr.getFileSystem() != llvm::vfs::getRealFileSystem()) {
1034+
// SWIFT_ENABLE_TENSORFLOW
1035+
auto clangFileSystem = ctx.SourceMgr.getFileSystem();
1036+
if (importerOpts.InMemoryOutputFileSystem) {
1037+
instance.setInMemoryOutputFileSystem(
1038+
importerOpts.InMemoryOutputFileSystem);
1039+
llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> overlayFileSystem(
1040+
new llvm::vfs::OverlayFileSystem(clangFileSystem));
1041+
overlayFileSystem->pushOverlay(importerOpts.InMemoryOutputFileSystem);
1042+
clangFileSystem = overlayFileSystem;
1043+
}
1044+
if (clangFileSystem != llvm::vfs::getRealFileSystem() ||
1045+
importerOpts.InMemoryOutputFileSystem) {
10351046
// If the clang instance has overlays it means the user has provided
10361047
// -ivfsoverlay options. We're going to clobber their file system with
10371048
// the Swift file system, so warn about it.
10381049
if (!instance.getHeaderSearchOpts().VFSOverlayFiles.empty()) {
10391050
ctx.Diags.diagnose(SourceLoc(), diag::clang_vfs_overlay_is_ignored);
10401051
}
1041-
instance.setVirtualFileSystem(ctx.SourceMgr.getFileSystem());
1052+
// SWIFT_ENABLE_TENSORFLOW
1053+
instance.setVirtualFileSystem(clangFileSystem);
10421054
}
10431055
instance.createFileManager();
10441056
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
let foo: Int = 10
2+
foo
3+
4+
// Checks that the SourceKit request succeeded.
5+
// CHECK-SOURCEKIT: source.lang.swift.ref.var.global (1:5-1:8)
6+
7+
// Checks that nothing has been written into the module cache on the real
8+
// filesystem.
9+
// CHECK-LS-NOT: ModuleCache
10+
11+
// RUN: %empty-directory(%t)
12+
// RUN: %sourcekitd-test -in-memory-clang-module-cache -req=cursor -pos=2:1 %s -- %s -module-cache-path %t/ModuleCache | %FileCheck --check-prefix=CHECK-SOURCEKIT %s
13+
// RUN: ls -l %t | %FileCheck --check-prefix=CHECK-LS %s
14+
15+
// REQUIRES: sourcekit_use_inproc_library

tools/SourceKit/include/SourceKit/Core/LangSupport.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#include "llvm/ADT/Optional.h"
2222
#include "llvm/ADT/SmallString.h"
2323
#include "swift/AST/Type.h"
24+
// SWIFT_ENABLE_TENSORFLOW
25+
#include "clang/Basic/InMemoryOutputFileSystem.h"
2426
#include "llvm/Support/VirtualFileSystem.h"
2527
#include <functional>
2628
#include <memory>
@@ -638,6 +640,12 @@ class LangSupport {
638640

639641
virtual ~LangSupport() { }
640642

643+
// SWIFT_ENABLE_TENSORFLOW
644+
/// Subsequent requests will write temporary output files to this filesystem
645+
/// rather than to the real filesystem.
646+
virtual void setInMemoryOutputFileSystem(
647+
llvm::IntrusiveRefCntPtr<clang::InMemoryOutputFileSystem> FS) = 0;
648+
641649
virtual void indexSource(StringRef Filename,
642650
IndexingConsumer &Consumer,
643651
ArrayRef<const char *> Args,

tools/SourceKit/lib/SwiftLang/SwiftASTManager.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,12 @@ struct SwiftASTManager::Implementation {
375375
Cache<ASTKey, ASTProducerRef> ASTCache{ "sourcekit.swift.ASTCache" };
376376
llvm::sys::Mutex CacheMtx;
377377

378+
// SWIFT_ENABLE_TENSORFLOW
379+
/// Requests will write temporary output files to this filesystem rather than
380+
/// to the real filesystem.
381+
llvm::IntrusiveRefCntPtr<clang::InMemoryOutputFileSystem>
382+
InMemoryOutputFileSystem;
383+
378384
WorkQueue ASTBuildQueue{ WorkQueue::Dequeuing::Serial,
379385
"sourcekit.swift.ASTBuilding" };
380386

@@ -401,6 +407,12 @@ SwiftASTManager::~SwiftASTManager() {
401407
delete &Impl;
402408
}
403409

410+
// SWIFT_ENABLE_TENSORFLOW
411+
void SwiftASTManager::setInMemoryOutputFileSystem(
412+
llvm::IntrusiveRefCntPtr<clang::InMemoryOutputFileSystem> FS) {
413+
Impl.InMemoryOutputFileSystem = std::move(FS);
414+
}
415+
404416
std::unique_ptr<llvm::MemoryBuffer>
405417
SwiftASTManager::getMemoryBuffer(StringRef Filename, std::string &Error) {
406418
return Impl.getMemoryBuffer(Filename, llvm::vfs::getRealFileSystem(), Error);
@@ -503,6 +515,9 @@ bool SwiftASTManager::initCompilerInvocation(
503515
ClangImporterOptions &ImporterOpts = Invocation.getClangImporterOptions();
504516
ImporterOpts.DetailedPreprocessingRecord = true;
505517

518+
// SWIFT_ENABLE_TENSORFLOW
519+
ImporterOpts.InMemoryOutputFileSystem = Impl.InMemoryOutputFileSystem;
520+
506521
assert(!Invocation.getModuleName().empty());
507522
Invocation.getLangOptions().AttachCommentsToDecls = true;
508523
Invocation.getLangOptions().DiagnosticsEditorMode = true;

tools/SourceKit/lib/SwiftLang/SwiftASTManager.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
#include "SwiftInvocation.h"
1717
#include "SourceKit/Core/LLVM.h"
18+
// SWIFT_ENABLE_TENSORFLOW
19+
#include "clang/Basic/InMemoryOutputFileSystem.h"
1820
#include "llvm/ADT/ArrayRef.h"
1921
#include "llvm/ADT/IntrusiveRefCntPtr.h"
2022
#include "llvm/ADT/StringRef.h"
@@ -93,6 +95,12 @@ class SwiftASTManager : public std::enable_shared_from_this<SwiftASTManager> {
9395
StringRef RuntimeResourcePath);
9496
~SwiftASTManager();
9597

98+
// SWIFT_ENABLE_TENSORFLOW
99+
/// Subsequent requests will write temporary output files to this filesystem
100+
/// rather than to the real filesystem.
101+
void setInMemoryOutputFileSystem(
102+
llvm::IntrusiveRefCntPtr<clang::InMemoryOutputFileSystem> FS);
103+
96104
SwiftInvocationRef getInvocation(
97105
ArrayRef<const char *> Args, StringRef PrimaryFile, std::string &Error);
98106

tools/SourceKit/lib/SwiftLang/SwiftLangSupport.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,12 @@ SwiftLangSupport::SwiftLangSupport(SourceKit::Context &SKCtx)
211211
SwiftLangSupport::~SwiftLangSupport() {
212212
}
213213

214+
// SWIFT_ENABLE_TENSORFLOW
215+
void SwiftLangSupport::setInMemoryOutputFileSystem(
216+
llvm::IntrusiveRefCntPtr<clang::InMemoryOutputFileSystem> FS) {
217+
ASTMgr->setInMemoryOutputFileSystem(std::move(FS));
218+
}
219+
214220
std::unique_ptr<llvm::MemoryBuffer>
215221
SwiftLangSupport::makeCodeCompletionMemoryBuffer(
216222
const llvm::MemoryBuffer *origBuf, unsigned &Offset,

tools/SourceKit/lib/SwiftLang/SwiftLangSupport.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,10 @@ class SwiftLangSupport : public LangSupport {
296296
explicit SwiftLangSupport(SourceKit::Context &SKCtx);
297297
~SwiftLangSupport();
298298

299+
// SWIFT_ENABLE_TENSORFLOW
300+
void setInMemoryOutputFileSystem(
301+
llvm::IntrusiveRefCntPtr<clang::InMemoryOutputFileSystem> FS) override;
302+
299303
std::shared_ptr<NotificationCenter> getNotificationCenter() const {
300304
return NotificationCtr;
301305
}

tools/SourceKit/tools/sourcekitd-test/Options.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@ def repeat_request_EQ : Joined<["-"], "repeat-request=">, Alias<repeat_request>;
137137
def vfs_files : CommaJoined<["-"], "vfs-files=">,
138138
HelpText<"Injects a VFS into the request, overlaying files specified by the given <name>=<target> pairs over the real filesystem">;
139139

140+
// SWIFT_ENABLE_TENSORFLOW
141+
def in_memory_clang_module_cache : Flag<["-"], "in-memory-clang-module-cache">,
142+
HelpText<"Put the Clang module cache in memory">;
143+
140144
def help : Flag<["-", "--"], "help">,
141145
HelpText<"Display available options">;
142146

tools/SourceKit/tools/sourcekitd-test/TestOptions.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,11 +362,32 @@ bool TestOptions::parseArgs(llvm::ArrayRef<const char *> Args) {
362362
}
363363
break;
364364
#else
365+
// The -vfs-files option operates by making a function call to a function
366+
// defined in the SourceKit InProc library
367+
// (SourceKit::setGlobalFileSystemProvider). So this option only works
368+
// when sourcekitd-test is compiled using that library. It does not work
369+
// when sourcekitd-test uses the XPC library.
365370
llvm::errs() << "vfs-files only supported when "
366371
"SWIFT_SOURCEKIT_USE_INPROC_LIBRARY is set";
367372
return true;
368373
#endif
369374

375+
// SWIFT_ENABLE_TENSORFLOW
376+
case OPT_in_memory_clang_module_cache:
377+
#ifdef SWIFT_SOURCEKIT_USE_INPROC_LIBRARY
378+
InMemoryClangModuleCache = true;
379+
break;
380+
#else
381+
// The -in-memory-clang-module-cache option operates by making a function
382+
// call to a function defined in the SourceKit InProc library
383+
// (SourceKit::setGlobalInMemoryOutputFileSystem). So this option only
384+
// works when sourcekitd-test is compiled using that library. It does not
385+
// work when sourcekitd-test uses the XPC library.
386+
llvm::errs() << "in-memory-clang-module-cache only supported when "
387+
"SWIFT_SOURCEKIT_USE_INPROC_LIBRARY is set";
388+
return true;
389+
#endif
390+
370391
case OPT_UNKNOWN:
371392
llvm::errs() << "error: unknown argument: "
372393
<< InputArg->getAsString(ParsedArgs) << '\n'

tools/SourceKit/tools/sourcekitd-test/TestOptions.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ struct TestOptions {
112112
bool timeRequest = false;
113113
unsigned repeatRequest = 1;
114114
llvm::StringMap<std::string> VFSFiles;
115+
// SWIFT_ENABLE_TENSORFLOW
116+
bool InMemoryClangModuleCache;
115117
llvm::Optional<bool> CancelOnSubsequentRequest;
116118
bool parseArgs(llvm::ArrayRef<const char *> Args);
117119
void printHelp(bool ShowHidden) const;

tools/SourceKit/tools/sourcekitd-test/sourcekitd-test.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,16 @@ static int setExpectedTypes(const sourcekitd_test::TestOptions &Opts,
506506
}
507507

508508
static int handleTestInvocation(TestOptions Opts, TestOptions &InitOpts) {
509+
// SWIFT_ENABLE_TENSORFLOW
510+
#ifdef SWIFT_SOURCEKIT_USE_INPROC_LIBRARY
511+
if (Opts.InMemoryClangModuleCache) {
512+
SourceKit::setGlobalInMemoryOutputFileSystem(
513+
new clang::InMemoryOutputFileSystem());
514+
} else {
515+
SourceKit::setGlobalInMemoryOutputFileSystem(nullptr);
516+
}
517+
#endif
518+
509519
if (!Opts.JsonRequestPath.empty())
510520
return handleJsonRequestPath(Opts.JsonRequestPath, Opts);
511521

tools/SourceKit/tools/sourcekitd/include/sourcekitd/FileSystemProvider.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#define LLVM_SOURCEKITD_FILESYSTEMPROVIDER_H
1515

1616
#include "SourceKit/Support/FileSystemProvider.h"
17+
#include "clang/Basic/InMemoryOutputFileSystem.h"
18+
#include "llvm/ADT/IntrusiveRefCntPtr.h"
1719
#include "llvm/ADT/StringRef.h"
1820

1921
namespace SourceKit {
@@ -31,6 +33,16 @@ namespace SourceKit {
3133
void setGlobalFileSystemProvider(
3234
llvm::StringRef Name, SourceKit::FileSystemProvider *FileSystemProvider);
3335

36+
/// Subsequent requests will write temporary output files to this filesystem
37+
/// rather than to the real filesystem.
38+
///
39+
/// Is not threadsafe.
40+
///
41+
/// \param FS may be null, which makes subsequent requests start writing
42+
/// temporary output files to the real filesystem again.
43+
void setGlobalInMemoryOutputFileSystem(
44+
llvm::IntrusiveRefCntPtr<clang::InMemoryOutputFileSystem> FS);
45+
3446
} // namespace SourceKit
3547

3648
#endif

tools/SourceKit/tools/sourcekitd/lib/API/Requests.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ void setGlobalFileSystemProvider(StringRef Name,
135135
assert(FileSystemProvider);
136136
getGlobalContext().setFileSystemProvider(Name, FileSystemProvider);
137137
}
138+
139+
void setGlobalInMemoryOutputFileSystem(IntrusiveRefCntPtr<clang::InMemoryOutputFileSystem> FS) {
140+
getGlobalContext().getSwiftLangSupport().setInMemoryOutputFileSystem(std::move(FS));
141+
}
138142
} // namespace SourceKit
139143

140144
static sourcekitd_response_t demangleNames(ArrayRef<const char *> MangledNames,

utils/update_checkout/update-checkout-config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@
356356
"aliases": ["tensorflow"],
357357
"repos": {
358358
"llvm": "swift-DEVELOPMENT-SNAPSHOT-2019-06-06-a",
359-
"clang": "swift-DEVELOPMENT-SNAPSHOT-2019-06-06-a",
359+
"clang": "ea2e6942a25eb7201d32c7c21b1dd7e5658ba1e8",
360360
"swift": "tensorflow",
361361
"lldb": "4514d21fbe49a16baf26ba918c5a4906d54e8091",
362362
"cmark": "swift-DEVELOPMENT-SNAPSHOT-2019-06-06-a",

0 commit comments

Comments
 (0)