Skip to content

[lldb] Prefer triple from module in precise invocations #8941

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
Original file line number Diff line number Diff line change
Expand Up @@ -731,8 +731,15 @@ exe_scope = exe_ctx.GetBestExecutionContextScope();
return error("could not create a Swift scratch context: ",
m_err.AsCString());

const SymbolContext *sc =
&frame->GetSymbolContext(lldb::eSymbolContextFunction);
// For playgrounds, the target triple should be used for expression
// evaluation, not the current module. This requires disabling precise
// compiler invocations.
//
// To disable precise compiler invocations, pass a null SymbolContext.
const SymbolContext *sc = nullptr;
if (!m_runs_in_playground_or_repl)
sc = &frame->GetSymbolContext(lldb::eSymbolContextFunction);

auto *swift_ast_ctx = m_swift_scratch_ctx->get()->GetSwiftASTContext(sc);
m_swift_ast_ctx =
llvm::dyn_cast_or_null<SwiftASTContextForExpressions>(swift_ast_ctx);
Expand Down
44 changes: 33 additions & 11 deletions lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2909,20 +2909,42 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(
return exe_module_sp->GetArchitecture().GetTriple();
};

llvm::Triple computed_triple;
llvm::Triple target_triple = target.GetArchitecture().GetTriple();
ArchSpec module_arch;
llvm::Triple module_triple;
if (sc.module_sp) {
module_arch = sc.module_sp->GetArchitecture();
module_triple = module_arch.GetTriple();
}

if (target.GetArchitecture().IsFullySpecifiedTriple()) {
ArchSpec target_arch = target.GetArchitecture();
llvm::Triple target_triple = target_arch.GetTriple();

ArchSpec preferred_arch;
llvm::Triple preferred_triple;
if (module_arch && module_arch.IsFullySpecifiedTriple()) {
LOG_PRINTF(GetLog(LLDBLog::Types),
"Preferring module triple %s over target triple %s.",
module_triple.str().c_str(), target_triple.str().c_str());
preferred_arch = module_arch;
preferred_triple = module_triple;
} else {
// When no viable module triple, fallback to the target triple.
preferred_arch = target_arch;
preferred_triple = target_triple;
}

llvm::Triple computed_triple;
if (preferred_arch.IsFullySpecifiedTriple()) {
// If a fully specified triple was passed in, for example
// through CreateTargetWithFileAndTargetTriple(), prefer that.
LOG_PRINTF(GetLog(LLDBLog::Types), "Fully specified target triple %s.",
target_triple.str().c_str());
computed_triple = target_triple;
preferred_triple.str().c_str());
computed_triple = preferred_triple;
} else {
// Underspecified means that one or more of vendor, os, or os
// version (Darwin only) is missing.
LOG_PRINTF(GetLog(LLDBLog::Types), "Underspecified target triple %s.",
target_triple.str().c_str());
preferred_triple.str().c_str());
llvm::VersionTuple platform_version;
PlatformSP platform_sp(target.GetPlatform());
if (platform_sp)
Expand All @@ -2939,16 +2961,16 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(
// present, since there might be some ambiguity about the
// plaform (e.g., ios-macabi runs on the macOS, but uses iOS
// version numbers).
if (!platform_version.empty() &&
target_triple.getEnvironment() == llvm::Triple::UnknownEnvironment) {
if (!platform_version.empty() && preferred_triple.getEnvironment() ==
llvm::Triple::UnknownEnvironment) {
LOG_PRINTF(GetLog(LLDBLog::Types), "Completing triple based on platform.");

llvm::SmallString<32> buffer;
{
llvm::raw_svector_ostream os(buffer);
os << target_triple.getArchName() << '-';
os << target_triple.getVendorName() << '-';
os << llvm::Triple::getOSTypeName(target_triple.getOS());
os << preferred_triple.getArchName() << '-';
os << preferred_triple.getVendorName() << '-';
os << llvm::Triple::getOSTypeName(preferred_triple.getOS());
os << platform_version.getAsString();
}
computed_triple = llvm::Triple(buffer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,31 @@ def test_swift_deployment_target_from_macho(self):
self.filecheck('platform shell cat ""%s"' % log, __file__)
# CHECK: SwiftASTContextForExpressions::SetTriple({{.*}}apple-macosx11.0.0
# CHECK-NOT: SwiftASTContextForExpressions::RegisterSectionModules("a.out"){{.*}} AST Data blobs

@skipUnlessDarwin # This test uses macOS triples explicitly.
@skipIfDarwinEmbedded
@skipIf(macos_version=["<", "11.1"])
@skipIf(setting=("symbols.swift-precise-compiler-invocation", "false"))
@swiftTest
def test_swift_precise_compiler_invocation_triple(self):
"""
Ensure expressions prefer the target triple of their module, as it may
differ from the target triple of the target. This is necessary for
explicitly built modules.
"""
self.build()
log = self.getBuildArtifact("types.log")
self.runCmd(f'log enable lldb types -f "{log}"')
lldbutil.run_to_source_breakpoint(
self, "break here", lldb.SBFileSpec("NewerTarget.swift")
)
self.expect(
"image list -t libNewerTarget.dylib",
substrs=["-apple-macosx11.1.0"],
)
self.expect("expression self", substrs=["i = 23"])
self.filecheck(
f'platform shell cat "{log}"', __file__, "-check-prefix=CHECK-PRECISE"
)
# CHECK-PRECISE: SwiftASTContextForExpressions(module: "NewerTarget", cu: "NewerTarget.swift")::CreateInstance() -- Fully specified target triple {{.*}}-apple-macosx11.1.0
# CHECK-PRECISE: SwiftASTContextForExpressions(module: "NewerTarget", cu: "NewerTarget.swift")::SetTriple("{{.*}}-apple-macosx11.1.0")