Skip to content

Commit 9bbff63

Browse files
authored
Merge pull request #77796 from nkcsgexi/fine-grained-tracing-6.1
[🍒6.1] cherry-pick fine-grained tracing changes
2 parents 95f0252 + e540d7f commit 9bbff63

File tree

11 files changed

+74
-44
lines changed

11 files changed

+74
-44
lines changed

include/swift/Basic/FileTypes.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ TYPE("tbd", TBD, "tbd", "")
7373
// engineers can see more details on the "Swift module traces" page in the
7474
// Swift section of the internal wiki.
7575
TYPE("module-trace", ModuleTrace, "trace.json", "")
76+
TYPE("fine-module-trace", FineModuleTrace, "", "")
7677

7778
// Complete dependency information for the given Swift files as JSON.
7879
TYPE("json-dependencies", JSONDependencies, "dependencies.json", "")

include/swift/Basic/SupplementaryOutputPaths.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ OUTPUT(FixItsOutputPath, TY_SwiftFixIt)
106106
/// to each .swiftmodule that was loaded while building module NAME for target
107107
/// TARGET. This format is subject to arbitrary change, however.
108108
OUTPUT(LoadedModuleTracePath, TY_ModuleTrace)
109+
OUTPUT(FineModuleTracePath, TY_FineModuleTrace)
109110

110111
/// The path to which we should output a TBD file.
111112
///

include/swift/Frontend/InputFile.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ class InputFile final {
133133
StringRef getLoadedModuleTracePath() const {
134134
return getPrimarySpecificPaths().SupplementaryOutputs.LoadedModuleTracePath;
135135
}
136+
StringRef getFineModuleTracePath() const {
137+
return getPrimarySpecificPaths().SupplementaryOutputs.FineModuleTracePath;
138+
}
136139
StringRef getSerializedDiagnosticsPath() const {
137140
return getPrimarySpecificPaths().SupplementaryOutputs
138141
.SerializedDiagnosticsPath;

lib/Basic/FileTypes.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ bool file_types::isTextual(ID Id) {
105105
case file_types::TY_ImportedModules:
106106
case file_types::TY_TBD:
107107
case file_types::TY_ModuleTrace:
108+
case file_types::TY_FineModuleTrace:
108109
case file_types::TY_YAMLOptRecord:
109110
case file_types::TY_SwiftModuleInterfaceFile:
110111
case file_types::TY_PrivateSwiftModuleInterfaceFile:
@@ -186,6 +187,7 @@ bool file_types::isAfterLLVM(ID Id) {
186187
case file_types::TY_Remapping:
187188
case file_types::TY_IndexData:
188189
case file_types::TY_ModuleTrace:
190+
case file_types::TY_FineModuleTrace:
189191
case file_types::TY_YAMLOptRecord:
190192
case file_types::TY_BitstreamOptRecord:
191193
case file_types::TY_SwiftModuleInterfaceFile:
@@ -249,6 +251,7 @@ bool file_types::isPartOfSwiftCompilation(ID Id) {
249251
case file_types::TY_Remapping:
250252
case file_types::TY_IndexData:
251253
case file_types::TY_ModuleTrace:
254+
case file_types::TY_FineModuleTrace:
252255
case file_types::TY_YAMLOptRecord:
253256
case file_types::TY_BitstreamOptRecord:
254257
case file_types::TY_JSONDependencies:
@@ -311,6 +314,7 @@ bool file_types::isProducedFromDiagnostics(ID Id) {
311314
case file_types::TY_Remapping:
312315
case file_types::TY_IndexData:
313316
case file_types::TY_ModuleTrace:
317+
case file_types::TY_FineModuleTrace:
314318
case file_types::TY_YAMLOptRecord:
315319
case file_types::TY_BitstreamOptRecord:
316320
case file_types::TY_JSONDependencies:

lib/Driver/Driver.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1691,6 +1691,7 @@ void Driver::buildActions(SmallVectorImpl<const Action *> &TopLevelActions,
16911691
case file_types::TY_PCH:
16921692
case file_types::TY_ImportedModules:
16931693
case file_types::TY_ModuleTrace:
1694+
case file_types::TY_FineModuleTrace:
16941695
case file_types::TY_YAMLOptRecord:
16951696
case file_types::TY_BitstreamOptRecord:
16961697
case file_types::TY_SwiftModuleInterfaceFile:

lib/Driver/ToolChains.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,7 @@ const char *ToolChain::JobContext::computeFrontendModeForCompile() const {
761761
case file_types::TY_SwiftDeps:
762762
case file_types::TY_ExternalSwiftDeps:
763763
case file_types::TY_ModuleTrace:
764+
case file_types::TY_FineModuleTrace:
764765
case file_types::TY_TBD:
765766
case file_types::TY_YAMLOptRecord:
766767
case file_types::TY_BitstreamOptRecord:
@@ -1036,6 +1037,7 @@ ToolChain::constructInvocation(const BackendJobAction &job,
10361037
case file_types::TY_ExternalSwiftDeps:
10371038
case file_types::TY_Remapping:
10381039
case file_types::TY_ModuleTrace:
1040+
case file_types::TY_FineModuleTrace:
10391041
case file_types::TY_YAMLOptRecord:
10401042
case file_types::TY_BitstreamOptRecord:
10411043
case file_types::TY_SwiftModuleInterfaceFile:

lib/Frontend/ArgsToFrontendOutputsConverter.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,24 @@ SupplementaryOutputPathsComputer::computeOutputPathsForOneInput(
455455
file_types::TY_ModuleTrace, "",
456456
defaultSupplementaryOutputPathExcludingExtension);
457457

458+
// We piggy-back on the loadedModuleTracePath to decide (1) whether
459+
// to emit the fine module Trace file, and (2) where to emit the fine module
460+
// trace file if the path isn't explicitly given by
461+
// SWIFT_COMPILER_FINE_GRAINED_TRACE_PATH.
462+
// FIXME: we probably need to move this to a frontend argument.
463+
llvm::SmallString<128> FineModuleTracePath;
464+
if (!loadedModuleTracePath.empty()) {
465+
if (const char *P = ::getenv("SWIFT_COMPILER_FINE_GRAINED_TRACE_PATH")) {
466+
StringRef FilePath = P;
467+
llvm::sys::path::append(FineModuleTracePath, FilePath);
468+
} else {
469+
llvm::sys::path::append(FineModuleTracePath, loadedModuleTracePath);
470+
llvm::sys::path::remove_filename(FineModuleTracePath);
471+
llvm::sys::path::append(FineModuleTracePath,
472+
".SWIFT_FINE_DEPENDENCY_TRACE.json");
473+
}
474+
}
475+
458476
auto tbdPath = determineSupplementaryOutputFilename(
459477
OPT_emit_tbd, pathsFromArguments.TBDPath, file_types::TY_TBD, "",
460478
defaultSupplementaryOutputPathExcludingExtension);
@@ -520,6 +538,7 @@ SupplementaryOutputPathsComputer::computeOutputPathsForOneInput(
520538
sop.SerializedDiagnosticsPath = serializedDiagnosticsPath;
521539
sop.FixItsOutputPath = fixItsOutputPath;
522540
sop.LoadedModuleTracePath = loadedModuleTracePath;
541+
sop.FineModuleTracePath = FineModuleTracePath.str().str();
523542
sop.TBDPath = tbdPath;
524543
sop.ModuleInterfaceOutputPath = ModuleInterfaceOutputPath;
525544
sop.PrivateModuleInterfaceOutputPath = PrivateModuleInterfaceOutputPath;

lib/FrontendTool/Dependencies.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ bool emitLoadedModuleTraceIfNeeded(ModuleDecl *mainModule,
3232
const FrontendOptions &opts,
3333
const InputFile &input);
3434

35-
bool emitObjCMessageSendTraceIfNeeded(ModuleDecl *mainModule,
36-
const FrontendOptions &opts);
35+
bool emitFineModuleTraceIfNeeded(ModuleDecl *mainModule,
36+
const FrontendOptions &opts);
3737

3838
} // end namespace swift
3939

lib/FrontendTool/FrontendTool.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1001,7 +1001,7 @@ static void performEndOfPipelineActions(CompilerInstance &Instance) {
10011001
Instance.getMainModule(), Instance.getDependencyTracker(), opts);
10021002

10031003
dumpAPIIfNeeded(Instance);
1004-
swift::emitObjCMessageSendTraceIfNeeded(Instance.getMainModule(), opts);
1004+
swift::emitFineModuleTraceIfNeeded(Instance.getMainModule(), opts);
10051005
}
10061006

10071007
// Contains the hadError checks internally, we still want to output the

lib/FrontendTool/LoadedModuleTrace.cpp

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -853,16 +853,17 @@ class ObjcMethodReferenceCollector: public SourceEntityWalker {
853853
}
854854
};
855855

856-
static std::optional<int> createObjCMessageTraceFile(const InputFile &input,
857-
ModuleDecl *MD,
858-
std::vector<SourceFile*> &filesToWalk) {
859-
if (input.getLoadedModuleTracePath().empty()) {
856+
static void createFineModuleTraceFile(const InputFile &input, ModuleDecl *MD) {
857+
StringRef tracePath = input.getFineModuleTracePath();
858+
if (tracePath.empty()) {
860859
// we basically rely on the passing down of module trace file path
861860
// as an indicator that this job needs to emit an ObjC message trace file.
862861
// FIXME: add a separate swift-frontend flag for ObjC message trace path
863862
// specifically.
864-
return {};
863+
return;
865864
}
865+
auto &ctx = MD->getASTContext();
866+
std::vector<SourceFile*> filesToWalk;
866867
for (auto *FU : MD->getFiles()) {
867868
if (auto *SF = dyn_cast<SourceFile>(FU)) {
868869
switch (SF->Kind) {
@@ -880,50 +881,48 @@ static std::optional<int> createObjCMessageTraceFile(const InputFile &input,
880881
}
881882
// No source files to walk, abort.
882883
if (filesToWalk.empty()) {
883-
return {};
884+
return;
884885
}
885-
llvm::SmallString<128> tracePath;
886-
if (const char *P = ::getenv("SWIFT_COMPILER_OBJC_MESSAGE_TRACE_DIRECTORY")) {
887-
StringRef DirPath = P;
888-
llvm::sys::path::append(tracePath, DirPath);
889-
} else {
890-
llvm::sys::path::append(tracePath, input.getLoadedModuleTracePath());
891-
llvm::sys::path::remove_filename(tracePath);
892-
llvm::sys::path::append(tracePath, ".SWIFT_FINE_DEPENDENCY_TRACE");
886+
// Write output via atomic append.
887+
llvm::vfs::OutputConfig config;
888+
config.setAppend().setAtomicWrite();
889+
auto outputFile = ctx.getOutputBackend().createFile(tracePath, config);
890+
if (!outputFile) {
891+
ctx.Diags.diagnose(SourceLoc(), diag::error_opening_output, tracePath,
892+
toString(outputFile.takeError()));
893+
return;
893894
}
894-
if (!llvm::sys::fs::exists(tracePath)) {
895-
if (llvm::sys::fs::create_directory(tracePath))
896-
return {};
895+
ObjcMethodReferenceCollector collector(MD);
896+
for (auto *SF: filesToWalk) {
897+
collector.setFileBeforeVisiting(SF);
898+
collector.walk(*SF);
897899
}
898-
SmallString<32> fileName(MD->getNameStr());
899-
fileName.append("-%%%%-%%%%-%%%%.json");
900-
llvm::sys::path::append(tracePath, fileName);
901-
int tmpFD;
902-
if (llvm::sys::fs::createUniqueFile(tracePath.str(), tmpFD, tracePath)) {
903-
return {};
900+
901+
// print this json line.
902+
std::string stringBuffer;
903+
{
904+
llvm::raw_string_ostream memoryBuffer(stringBuffer);
905+
collector.serializeAsJson(memoryBuffer);
906+
}
907+
stringBuffer += "\n";
908+
909+
// Write output via atomic append.
910+
*outputFile << stringBuffer;
911+
if (auto err = outputFile->keep()) {
912+
ctx.Diags.diagnose(SourceLoc(), diag::error_opening_output,
913+
tracePath, toString(std::move(err)));
914+
return;
904915
}
905-
return tmpFD;
906916
}
907917

908-
bool swift::emitObjCMessageSendTraceIfNeeded(ModuleDecl *mainModule,
909-
const FrontendOptions &opts) {
918+
bool swift::emitFineModuleTraceIfNeeded(ModuleDecl *mainModule,
919+
const FrontendOptions &opts) {
910920
ASTContext &ctxt = mainModule->getASTContext();
911921
assert(!ctxt.hadError() &&
912922
"We should've already exited earlier if there was an error.");
913923

914924
opts.InputsAndOutputs.forEachInput([&](const InputFile &input) {
915-
std::vector<SourceFile*> filesToWalk;
916-
auto tmpFD = createObjCMessageTraceFile(input, mainModule, filesToWalk);
917-
if (!tmpFD)
918-
return false;
919-
// Write the contents of the buffer.
920-
llvm::raw_fd_ostream out(*tmpFD, /*shouldClose=*/true);
921-
ObjcMethodReferenceCollector collector(mainModule);
922-
for (auto *SF : filesToWalk) {
923-
collector.setFileBeforeVisiting(SF);
924-
collector.walk(*SF);
925-
}
926-
collector.serializeAsJson(out);
925+
createFineModuleTraceFile(input, mainModule);
927926
return true;
928927
});
929928
return false;

test/IDE/objc_send_collector_1.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
// RUN: %empty-directory(%t/CUSTOM_DIR)
33

44
// RUN: %target-swift-frontend -I %t/lib/swift -typecheck %s %S/Inputs/objc_send_collector_2.swift -module-name main -swift-version 5 -F %S/Inputs/mock-sdk -emit-loaded-module-trace-path %t/.MODULE_TRACE
5-
// RUN: cat %t/.SWIFT_FINE_DEPENDENCY_TRACE/* | %FileCheck %s
5+
// RUN: cat %t/.SWIFT_FINE_DEPENDENCY_TRACE.json | %{python} -c 'import json, sys; json.dump(json.loads(sys.stdin.read()), sys.stdout, sort_keys=True, indent=2)' | %FileCheck %s
66

7-
// RUN: SWIFT_COMPILER_OBJC_MESSAGE_TRACE_DIRECTORY=%t/CUSTOM_DIR %target-swift-frontend -I %t/lib/swift -typecheck %s %S/Inputs/objc_send_collector_2.swift -module-name main -swift-version 5 -F %S/Inputs/mock-sdk -emit-loaded-module-trace-path %t/.MODULE_TRACE
8-
// RUN: cat %t/CUSTOM_DIR/* | %FileCheck %s
7+
// RUN: SWIFT_COMPILER_FINE_GRAINED_TRACE_PATH=%t/given_trace.json %target-swift-frontend -I %t/lib/swift -typecheck %s %S/Inputs/objc_send_collector_2.swift -module-name main -swift-version 5 -F %S/Inputs/mock-sdk -emit-loaded-module-trace-path %t/.MODULE_TRACE
8+
// RUN: cat %t/given_trace.json | %{python} -c 'import json, sys; json.dump(json.loads(sys.stdin.read()), sys.stdout, sort_keys=True, indent=2)' | %FileCheck %s
99

1010
// REQUIRES: objc_interop
1111

0 commit comments

Comments
 (0)