Skip to content

Partially revert 70d2ba016129e54dbb35947d5c44d8ae5f17e47c. #4083

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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
2 changes: 2 additions & 0 deletions lldb/include/lldb/Target/Target.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ class TargetProperties : public Properties {

llvm::StringRef GetSwiftExtraClangFlags() const;

bool GetSwiftCreateModuleContextsInParallel() const;

bool GetSwiftReadMetadataFromFileCache() const;

bool GetSwiftUseReflectionSymbols() const;
Expand Down
54 changes: 52 additions & 2 deletions lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2077,14 +2077,45 @@ ProcessModule(ModuleSP module_sp, std::string m_description,
module_search_paths.insert(module_search_paths.end(),
opts.getImportSearchPaths().begin(),
opts.getImportSearchPaths().end());
for (const auto &fwsp : opts.getFrameworkSearchPaths())
framework_search_paths.push_back({fwsp.Path, fwsp.IsSystem});
auto &clang_opts = invocation.getClangImporterOptions().ExtraArgs;
for (const std::string &arg : clang_opts) {
extra_clang_args.push_back(arg);
LOG_VERBOSE_PRINTF(GetLog(LLDBLog::Types), "adding Clang argument \"%s\".",
arg.c_str());
}
// FIXME: Unfortunately this:
//
// for (const auto &fwsp : opts.getFrameworkSearchPaths())
// framework_search_paths.push_back({fwsp.Path, fwsp.IsSystem});
//
// is insufficient, as ClangImporter can discover more framework
// search paths on the fly. Once a better solution is found,
// warmup_contexts can be retired (again).
{
SymbolFile *sym_file = module_sp->GetSymbolFile();
if (!sym_file)
return;
Status sym_file_error;
auto type_system_or_err =
sym_file->GetTypeSystemForLanguage(lldb::eLanguageTypeSwift);
if (!type_system_or_err) {
llvm::consumeError(type_system_or_err.takeError());
return;
}
auto ts = llvm::dyn_cast_or_null<TypeSystemSwift>(&*type_system_or_err);
if (!ts)
return;

SwiftASTContext *ast_context = ts->GetSwiftASTContext();
if (ast_context && !ast_context->HasErrors()) {
if (use_all_compiler_flags ||
target.GetExecutableModulePointer() == module_sp.get()) {
const auto &opts = ast_context->GetSearchPathOptions();
for (const auto &fwsp : opts.getFrameworkSearchPaths())
framework_search_paths.push_back({fwsp.Path, fwsp.IsSystem});
}
}
}
}

lldb::TypeSystemSP SwiftASTContext::CreateInstance(
Expand Down Expand Up @@ -2144,7 +2175,26 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(
handled_sdk_path = true;
}

auto warmup_astcontexts = [&]() {
Copy link

@jimingham jimingham Mar 17, 2022

Choose a reason for hiding this comment

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

As a standalone routine, this would be an inefficient way to make sure that all the ASTContexts which we know about have been made, since once they have been made, you're spinning up a thread pool and adding a job to it for each module, when the call GetModuleSwiftASTContext which will just return the already made AST context.

The routine isn't a problem in this context, so far as I can tell, because its actual use is gated by handled_sdk_path, and so it only happens for the first module of the first big load where we discover the SDK. But the structure of the code doesn't make that clear.

You could make that clear by moving the definition of warmup_astcontexts into the if (!handled_sdk_path) scope so it can't be misused, or maybe just add a better comment.

You could make warmup_astcontexts cheap by having a GetModuleSwiftASTContext(bool can_build) which you call passing false before starting up the thread pool, so that you only set up the thread pool if there's work to do. But I don't think you intend this as a generally useful routine so that seems overkill.

Copy link
Author

Choose a reason for hiding this comment

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

For context, the warmup code I put back in in this commit is identical to the code I removed a month ago.

Choose a reason for hiding this comment

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

Okay. As long as this only gets run once it's okay. I had to think some to ensure that it wasn't, but since it's a temporary hack not many other people will have to warm up their brain pans over it so that's fine.

if (target.GetSwiftCreateModuleContextsInParallel()) {
// The first call to GetTypeSystemForLanguage() on a module will
// trigger the import (and thus most likely the rebuild) of all
// the Clang modules that were imported in this module. This can
// be a lot of work (potentially ten seconds per module), but it
// can be performed in parallel.
llvm::ThreadPool pool(llvm::hardware_concurrency());
for (size_t mi = 0; mi != num_images; ++mi) {
auto module_sp = target.GetImages().GetModuleAtIndex(mi);
pool.async([=] {
GetModuleSwiftASTContext(*module_sp);
});
}
pool.wait();
}
};

if (!handled_sdk_path) {
warmup_astcontexts();
for (size_t mi = 0; mi != num_images; ++mi) {
ModuleSP module_sp = target.GetImages().GetModuleAtIndex(mi);
if (!HasSwiftModules(*module_sp))
Expand Down
12 changes: 12 additions & 0 deletions lldb/source/Target/Target.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4164,6 +4164,18 @@ void TargetProperties::SetInjectLocalVariables(ExecutionContext *exe_ctx,
true);
}

bool TargetProperties::GetSwiftCreateModuleContextsInParallel() const {
const Property *exp_property = m_collection_sp->GetPropertyAtIndex(
nullptr, false, ePropertyExperimental);
OptionValueProperties *exp_values =
exp_property->GetValue()->GetAsProperties();
if (exp_values)
return exp_values->GetPropertyAtIndexAsBoolean(
nullptr, ePropertySwiftCreateModuleContextsInParallel, true);
else
return true;
}

bool TargetProperties::GetSwiftReadMetadataFromFileCache() const {
const Property *exp_property = m_collection_sp->GetPropertyAtIndex(
nullptr, false, ePropertyExperimental);
Expand Down
3 changes: 3 additions & 0 deletions lldb/source/Target/TargetProperties.td
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ let Definition = "target_experimental" in {
def InjectLocalVars : Property<"inject-local-vars", "Boolean">,
Global, DefaultTrue,
Desc<"If true, inject local variables explicitly into the expression text. This will fix symbol resolution when there are name collisions between ivars and local variables. But it can make expressions run much more slowly.">;
def SwiftCreateModuleContextsInParallel : Property<"swift-create-module-contexts-in-parallel", "Boolean">,
DefaultTrue,
Desc<"Create the per-module Swift AST contexts in parallel.">;
def SwiftReadMetadataFromFileCache: Property<"swift-read-metadata-from-file-cache", "Boolean">,
DefaultTrue,
Desc<"Read Swift reflection metadata from the file cache instead of the process when possible">;
Expand Down
5 changes: 5 additions & 0 deletions lldb/test/API/lang/swift/framework_paths/Direct.swift.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@_implementationOnly import Discovered
public class D {
public init() { member = Invisible() }
private let member : Invisible
}
1 change: 1 addition & 0 deletions lldb/test/API/lang/swift/framework_paths/Discovered.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
struct Invisible {};
1 change: 1 addition & 0 deletions lldb/test/API/lang/swift/framework_paths/Discovered.m
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "Discovered.h"
29 changes: 29 additions & 0 deletions lldb/test/API/lang/swift/framework_paths/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
SWIFT_SOURCES := main.swift

SWIFTFLAGS_EXTRAS = -F $(BUILDDIR) -framework Direct -Xlinker -rpath -Xlinker $(BUILDDIR)

all: Direct.framework $(EXE)

include Makefile.rules

Discovered.framework: Discovered.h
$(MAKE) -f $(MAKEFILE_RULES) \
DYLIB_ONLY=YES \
DYLIB_NAME=Discovered \
DYLIB_OBJC_SOURCES=Discovered.m \
FRAMEWORK_HEADERS=$(SRCDIR)/Discovered.h \
FRAMEWORK_MODULES=$(SRCDIR)/module.modulemap \
FRAMEWORK=Discovered

Direct.framework: $(SRCDIR)/Direct.swift.in Discovered.framework
mkdir -p $(BUILDDIR)/secret_path
cp $< $(BUILDDIR)/Direct.swift
mv Discovered.framework $(BUILDDIR)/secret_path
$(MAKE) -f $(MAKEFILE_RULES) \
DYLIB_NAME=Direct \
DYLIB_SWIFT_SOURCES=Direct.swift \
DYLIB_MODULENAME=Direct \
FRAMEWORK=Direct \
SWIFTFLAGS_EXTRAS=-F$(BUILDDIR)/secret_path
rm -f $(BUILDDIR)/Direct.swiftmodule $(BUILDDIR)/Direct.swiftinterface $(BUILDDIR)/Direct.swift
ln -s $(BUILDDIR)/Direct.framework/Direct $(BUILDDIR)/Direct # FIXME
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import lldb
from lldbsuite.test.decorators import *
import lldbsuite.test.lldbtest as lldbtest
import lldbsuite.test.lldbutil as lldbutil
import os
import unittest2


class TestSwiftSystemFramework(lldbtest.TestBase):

NO_DEBUG_INFO_TESTCASE = True
mydir = lldbtest.TestBase.compute_mydir(__file__)

@swiftTest
@skipIf(oslist=no_match(["macosx"]))
def test_system_framework(self):
"""Test the discovery of framework search paths from framework dependencies."""
self.build()
target, process, thread, bkpt = lldbutil.run_to_source_breakpoint(
self, 'break here', lldb.SBFileSpec('main.swift'))

log = self.getBuildArtifact("types.log")
self.runCmd('log enable lldb types -f "%s"' % log)
self.expect("expression -- 0")
pos = 0
import io
with open(log, "r", encoding='utf-8') as logfile:
for line in logfile:
if "SwiftASTContextForExpressions::LogConfiguration()" in line and \
"/secret_path" in line:
pos += 1
self.assertEqual(pos, 1, "framework search path discovery is broken")
4 changes: 4 additions & 0 deletions lldb/test/API/lang/swift/framework_paths/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import Direct
let d = D()
print("break here")
print(d)
1 change: 1 addition & 0 deletions lldb/test/API/lang/swift/framework_paths/module.modulemap
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
framework module Discovered { header "Discovered.h" }