Skip to content

Commit ed3b64a

Browse files
committed
[Index] Add an option to compress the record and unit files
Companion of swiftlang/llvm-project#10977.
1 parent ba715db commit ed3b64a

File tree

10 files changed

+46
-18
lines changed

10 files changed

+46
-18
lines changed

include/swift/Frontend/FrontendOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ class FrontendOptions {
135135

136136
/// Include local definitions/references in the index data.
137137
bool IndexIncludeLocals = false;
138+
139+
/// Whether to compress the record and unit files in the index store.
140+
bool IndexStoreCompress;
138141

139142
bool SerializeDebugInfoSIL = false;
140143
/// If building a module from interface, ignore compiler flags

include/swift/Index/IndexRecord.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ namespace index {
5656
bool indexAndRecord(SourceFile *primarySourceFile, StringRef indexUnitToken,
5757
StringRef indexStorePath, bool indexClangModules,
5858
bool indexSystemModules, bool skipStdlib,
59-
bool includeLocals, bool isDebugCompilation,
59+
bool includeLocals, bool compress, bool isDebugCompilation,
6060
bool isExplicitModuleBuild, StringRef targetTriple,
6161
const DependencyTracker &dependencyTracker,
6262
const PathRemapper &pathRemapper);
@@ -98,7 +98,7 @@ bool indexAndRecord(SourceFile *primarySourceFile, StringRef indexUnitToken,
9898
bool indexAndRecord(ModuleDecl *module, ArrayRef<std::string> indexUnitTokens,
9999
StringRef moduleUnitToken, StringRef indexStorePath,
100100
bool indexClangModules, bool indexSystemModules,
101-
bool skipStdlib, bool includeLocals,
101+
bool skipStdlib, bool includeLocals, bool compress,
102102
bool isDebugCompilation, bool isExplicitModuleBuild,
103103
StringRef targetTriple,
104104
const DependencyTracker &dependencyTracker,

include/swift/Option/Options.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1685,6 +1685,10 @@ def index_file_path : Separate<["-"], "index-file-path">,
16851685
def index_store_path : Separate<["-"], "index-store-path">,
16861686
Flags<[FrontendOption, ArgumentIsPath, CacheInvariant]>, MetaVarName<"<path>">,
16871687
HelpText<"Store indexing data to <path>">;
1688+
1689+
def index_store_compress : Flag<["-"], "index-store-compress">,
1690+
Flags<[FrontendOption]>,
1691+
HelpText<"Compress the unit and record files in the index store">;
16881692

16891693
def index_unit_output_path : Separate<["-"], "index-unit-output-path">,
16901694
Flags<[FrontendOption, ArgumentIsPath, CacheInvariant]>, MetaVarName<"<path>">,

lib/FrontendTool/FrontendTool.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1884,6 +1884,7 @@ static void emitIndexDataForSourceFile(SourceFile *PrimarySourceFile,
18841884
opts.IndexSystemModules,
18851885
opts.IndexIgnoreStdlib,
18861886
opts.IndexIncludeLocals,
1887+
opts.IndexStoreCompress,
18871888
isDebugCompilation,
18881889
opts.DisableImplicitModules,
18891890
Invocation.getTargetTriple(),
@@ -1903,6 +1904,7 @@ static void emitIndexDataForSourceFile(SourceFile *PrimarySourceFile,
19031904
opts.IndexSystemModules,
19041905
opts.IndexIgnoreStdlib,
19051906
opts.IndexIncludeLocals,
1907+
opts.IndexStoreCompress,
19061908
isDebugCompilation,
19071909
opts.DisableImplicitModules,
19081910
Invocation.getTargetTriple(),

lib/Index/IndexRecord.cpp

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -311,9 +311,9 @@ StringRef StdlibGroupsIndexRecordingConsumer::findGroupForSymbol(const IndexSymb
311311
}
312312

313313
static bool writeRecord(SymbolTracker &record, std::string Filename,
314-
std::string indexStorePath, DiagnosticEngine *diags,
314+
std::string indexStorePath, bool compress, DiagnosticEngine *diags,
315315
std::string &outRecordFile) {
316-
IndexRecordWriter recordWriter(indexStorePath);
316+
IndexRecordWriter recordWriter(indexStorePath, compress);
317317
std::string error;
318318
auto result = recordWriter.beginRecord(
319319
Filename, record.hashRecord(), error, &outRecordFile);
@@ -360,25 +360,25 @@ static bool writeRecord(SymbolTracker &record, std::string Filename,
360360

361361
static std::unique_ptr<IndexRecordingConsumer>
362362
makeRecordingConsumer(std::string Filename, std::string indexStorePath,
363-
bool includeLocals, DiagnosticEngine *diags,
363+
bool includeLocals, bool compress, DiagnosticEngine *diags,
364364
std::string *outRecordFile,
365365
bool *outFailed) {
366366
return std::make_unique<IndexRecordingConsumer>(includeLocals,
367367
[=](SymbolTracker &record) {
368-
*outFailed = writeRecord(record, Filename, indexStorePath, diags,
368+
*outFailed = writeRecord(record, Filename, indexStorePath, compress, diags,
369369
*outRecordFile);
370370
});
371371
}
372372

373373
static bool
374374
recordSourceFile(SourceFile *SF, StringRef indexStorePath,
375-
bool includeLocals, DiagnosticEngine &diags,
375+
bool includeLocals, bool compress, DiagnosticEngine &diags,
376376
llvm::function_ref<void(StringRef, StringRef)> callback) {
377377
std::string recordFile;
378378
bool failed = false;
379379
auto consumer =
380380
makeRecordingConsumer(SF->getFilename().str(), indexStorePath.str(),
381-
includeLocals, &diags, &recordFile, &failed);
381+
includeLocals, compress, &diags, &recordFile, &failed);
382382
indexSourceFile(SF, *consumer);
383383

384384
if (!failed && !recordFile.empty())
@@ -418,6 +418,7 @@ emitDataForSwiftSerializedModule(ModuleDecl *module,
418418
bool indexSystemModules,
419419
bool skipStdlib,
420420
bool includeLocals,
421+
bool compress,
421422
bool explicitModulebuild,
422423
StringRef targetTriple,
423424
const clang::CompilerInstance &clangCI,
@@ -432,6 +433,7 @@ static void addModuleDependencies(ArrayRef<ImportedModule> imports,
432433
bool indexSystemModules,
433434
bool skipStdlib,
434435
bool includeLocals,
436+
bool compress,
435437
bool explicitModuleBuild,
436438
StringRef targetTriple,
437439
const clang::CompilerInstance &clangCI,
@@ -508,6 +510,7 @@ static void addModuleDependencies(ArrayRef<ImportedModule> imports,
508510
indexClangModules,
509511
indexSystemModules, skipStdlib,
510512
includeLocals,
513+
compress,
511514
explicitModuleBuild,
512515
targetTriple,
513516
clangCI, diags,
@@ -551,6 +554,7 @@ emitDataForSwiftSerializedModule(ModuleDecl *module,
551554
bool indexSystemModules,
552555
bool skipStdlib,
553556
bool includeLocals,
557+
bool compress,
554558
bool explicitModuleBuild,
555559
StringRef targetTriple,
556560
const clang::CompilerInstance &clangCI,
@@ -632,7 +636,7 @@ emitDataForSwiftSerializedModule(ModuleDecl *module,
632636
std::string recordFile;
633637
bool failed = false;
634638
auto consumer = makeRecordingConsumer(filename.str(), indexStorePath.str(),
635-
includeLocals, &diags, &recordFile, &failed);
639+
includeLocals, compress, &diags, &recordFile, &failed);
636640
indexModule(module, *consumer);
637641

638642
if (failed)
@@ -676,7 +680,7 @@ emitDataForSwiftSerializedModule(ModuleDecl *module,
676680
std::string outRecordFile;
677681
failed =
678682
failed || writeRecord(tracker, std::string(fileNameWithGroup.str()),
679-
indexStorePath.str(), &diags, outRecordFile);
683+
indexStorePath.str(), compress, &diags, outRecordFile);
680684
if (failed)
681685
return false;
682686
records.emplace_back(outRecordFile, moduleName.str().str());
@@ -698,7 +702,7 @@ emitDataForSwiftSerializedModule(ModuleDecl *module,
698702
auto clangRemapper = pathRemapper.asClangPathRemapper();
699703

700704
IndexUnitWriter unitWriter(
701-
fileMgr, indexStorePath, "swift", swiftVersion, filename, moduleName,
705+
fileMgr, indexStorePath, "swift", swiftVersion, compress, filename, moduleName,
702706
/*MainFile=*/{}, isSystem, /*IsModuleUnit=*/true, isDebugCompilation,
703707
targetTriple, sysrootPath, clangRemapper, getModuleInfoFromOpaqueModule);
704708

@@ -718,7 +722,7 @@ emitDataForSwiftSerializedModule(ModuleDecl *module,
718722
ModuleDecl::ImportFilterKind::Default});
719723
StringScratchSpace moduleNameScratch;
720724
addModuleDependencies(imports, indexStorePath, indexClangModules,
721-
indexSystemModules, skipStdlib, includeLocals,
725+
indexSystemModules, skipStdlib, includeLocals, compress,
722726
explicitModuleBuild,
723727
targetTriple, clangCI, diags, unitWriter,
724728
moduleNameScratch, pathRemapper, initialFile);
@@ -735,7 +739,7 @@ static bool
735739
recordSourceFileUnit(SourceFile *primarySourceFile, StringRef indexUnitToken,
736740
StringRef indexStorePath, bool indexClangModules,
737741
bool indexSystemModules, bool skipStdlib,
738-
bool includeLocals, bool isDebugCompilation,
742+
bool includeLocals, bool compress, bool isDebugCompilation,
739743
bool isExplicitModuleBuild, StringRef targetTriple,
740744
ArrayRef<clang::FileEntryRef> fileDependencies,
741745
const clang::CompilerInstance &clangCI,
@@ -754,7 +758,7 @@ recordSourceFileUnit(SourceFile *primarySourceFile, StringRef indexUnitToken,
754758
StringRef swiftVersion;
755759
StringRef sysrootPath = clangCI.getHeaderSearchOpts().Sysroot;
756760
IndexUnitWriter unitWriter(
757-
fileMgr, indexStorePath, "swift", swiftVersion, indexUnitToken,
761+
fileMgr, indexStorePath, "swift", swiftVersion, compress, indexUnitToken,
758762
module->getNameStr(), *mainFile, isSystem,
759763
/*isModuleUnit=*/false, isDebugCompilation, targetTriple, sysrootPath,
760764
clangRemapper, getModuleInfoFromOpaqueModule);
@@ -765,7 +769,7 @@ recordSourceFileUnit(SourceFile *primarySourceFile, StringRef indexUnitToken,
765769
ModuleDecl::getImportFilterLocal());
766770
StringScratchSpace moduleNameScratch;
767771
addModuleDependencies(imports, indexStorePath, indexClangModules,
768-
indexSystemModules, skipStdlib, includeLocals,
772+
indexSystemModules, skipStdlib, includeLocals, compress,
769773
isExplicitModuleBuild, targetTriple, clangCI, diags,
770774
unitWriter, moduleNameScratch, pathRemapper,
771775
primarySourceFile);
@@ -774,7 +778,7 @@ recordSourceFileUnit(SourceFile *primarySourceFile, StringRef indexUnitToken,
774778
for (auto F : fileDependencies)
775779
unitWriter.addFileDependency(F, /*FIXME:isSystem=*/false, /*Module=*/nullptr);
776780

777-
recordSourceFile(primarySourceFile, indexStorePath, includeLocals, diags,
781+
recordSourceFile(primarySourceFile, indexStorePath, includeLocals, compress, diags,
778782
[&](StringRef recordFile, StringRef filename) {
779783
if (auto file = fileMgr.getFileRef(filename)) {
780784
unitWriter.addRecordFile(
@@ -823,6 +827,7 @@ bool index::indexAndRecord(SourceFile *primarySourceFile,
823827
bool indexSystemModules,
824828
bool skipStdlib,
825829
bool includeLocals,
830+
bool compress,
826831
bool isDebugCompilation,
827832
bool isExplicitModuleBuild,
828833
StringRef targetTriple,
@@ -840,7 +845,7 @@ bool index::indexAndRecord(SourceFile *primarySourceFile,
840845

841846
return recordSourceFileUnit(primarySourceFile, indexUnitToken,
842847
indexStorePath, indexClangModules,
843-
indexSystemModules, skipStdlib, includeLocals,
848+
indexSystemModules, skipStdlib, includeLocals, compress,
844849
isDebugCompilation, isExplicitModuleBuild,
845850
targetTriple, {},
846851
clangCI, pathRemapper, diags);
@@ -854,6 +859,7 @@ bool index::indexAndRecord(ModuleDecl *module,
854859
bool indexSystemModules,
855860
bool skipStdlib,
856861
bool includeLocals,
862+
bool compress,
857863
bool isDebugCompilation,
858864
bool isExplicitModuleBuild,
859865
StringRef targetTriple,
@@ -879,7 +885,7 @@ bool index::indexAndRecord(ModuleDecl *module,
879885
}
880886
if (recordSourceFileUnit(SF, indexUnitTokens[unitIndex],
881887
indexStorePath, indexClangModules,
882-
indexSystemModules, skipStdlib, includeLocals,
888+
indexSystemModules, skipStdlib, includeLocals, compress,
883889
isDebugCompilation, isExplicitModuleBuild,
884890
targetTriple, {},
885891
clangCI, pathRemapper, diags))

test/Index/Store/index_compress.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// RUN: rm -rf %t
2+
// RUN: %target-swift-frontend -index-store-path %t/idx -index-store-compress -o %t.o -typecheck %s
3+
// RUN: c-index-test core -print-record %t/idx | %FileCheck %s
4+
5+
func foo() {}
6+
// CHECK: [[@LINE-1]]:6 | function/Swift | s:4main3fooyyF | Def | rel: 0

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -922,6 +922,7 @@ struct IndexStoreOptions {
922922
bool IgnoreStdlib = false;
923923
bool DisableImplicitModules = false;
924924
bool IncludeLocals = false;
925+
bool Compress = false;
925926
};
926927

927928
struct IndexStoreInfo{};

tools/SourceKit/lib/SwiftLang/SwiftIndexing.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,7 @@ static void emitIndexDataForSourceFile(SourceFile &PrimarySourceFile,
403403
IndexOpts.IncludeSystemModules,
404404
IndexOpts.IgnoreStdlib,
405405
IndexOpts.IncludeLocals,
406+
IndexOpts.Compress,
406407
isDebugCompilation,
407408
IndexOpts.DisableImplicitModules,
408409
Invocation.getTargetTriple(),

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1511,6 +1511,10 @@ getIndexStoreOpts(const RequestDict &Req, ResponseReceiver Rec) {
15111511
if (auto IncludeLocals = Req.getOptionalInt64(KeyIncludeLocals)) {
15121512
Opts.IncludeLocals = IncludeLocals.value() > 0;
15131513
}
1514+
1515+
if (auto Compress = Req.getOptionalInt64(KeyCompress)) {
1516+
Opts.Compress = Compress.value() > 0;
1517+
}
15141518
15151519
if (auto IgnoreClangModules = Req.getOptionalInt64(KeyIgnoreClangModules)) {
15161520
Opts.IgnoreClangModules = IgnoreClangModules.value() > 0;

utils/gyb_sourcekit_support/UIDs.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ def __init__(self, internal_name, external_name):
219219
KEY('IndexStorePath', 'key.index_store_path'),
220220
KEY('IndexUnitOutputPath', 'key.index_unit_output_path'),
221221
KEY('IncludeLocals', 'key.include_locals'),
222+
KEY('Compress', 'key.compress'),
222223
KEY('IgnoreClangModules', 'key.ignore_clang_modules'),
223224
KEY('IncludeSystemModules', 'key.include_system_modules'),
224225
KEY('IgnoreStdlib', 'key.ignore_stdlib'),

0 commit comments

Comments
 (0)