Skip to content

Eagerly emit getters at Onone. #75847

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
merged 1 commit into from
Aug 14, 2024
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
4 changes: 4 additions & 0 deletions include/swift/SIL/SILDeclRef.h
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,10 @@ struct SILDeclRef {
/// for e.g a lazy variable getter.
bool hasUserWrittenCode() const;

/// Returns true if this is a function that should be emitted because it is
/// accessible in the debugger.
bool shouldBeEmittedForDebugger() const;

/// Return the scope in which the parent class of a method (i.e. class
/// containing this declaration) can be subclassed, returning NotApplicable if
/// this is not a method, there is no such class, or the class cannot be
Expand Down
33 changes: 33 additions & 0 deletions lib/SIL/IR/SILDeclRef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,39 @@ bool SILDeclRef::hasUserWrittenCode() const {
llvm_unreachable("Unhandled case in switch!");
}

bool SILDeclRef::shouldBeEmittedForDebugger() const {
if (!isFunc())
return false;

if (getASTContext().SILOpts.OptMode != OptimizationMode::NoOptimization)
return false;;

if (!getASTContext().SILOpts.ShouldFunctionsBePreservedToDebugger)
return false;

if (getASTContext().LangOpts.hasFeature(Feature::Embedded))
return false;

ValueDecl *decl = getDecl();
DeclAttributes &attrs = decl->getAttrs();
if (attrs.hasSemanticsAttr("no.preserve.debugger"))
return false;

if (getLinkage(ForDefinition) == SILLinkage::Shared)
return false;

if (auto decl = getDecl())
if (!decl->isImplicit())
return true;

// Synthesized getters are still callable in the debugger.
if (auto *accessor = dyn_cast_or_null<AccessorDecl>(getFuncDecl())) {
return accessor->isSynthesized() && accessor->isGetterOrSetter();
};

return false;
}

namespace {
enum class LinkageLimit {
/// No limit.
Expand Down
5 changes: 3 additions & 2 deletions lib/SILGen/SILGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1198,8 +1198,9 @@ void SILGenModule::emitOrDelayFunction(SILDeclRef constant) {
auto emitAfter = lastEmittedFunction;

// Implicit decls may be delayed if they can't be used externally.
auto linkage = constant.getLinkage(ForDefinition);
bool mayDelay = !constant.hasUserWrittenCode() &&
auto linkage = constant.getLinkage(ForDefinition);;
bool mayDelay = !constant.shouldBeEmittedForDebugger() &&
!constant.hasUserWrittenCode() &&
!constant.isDynamicallyReplaceable() &&
!isPossiblyUsedExternally(linkage, M.isWholeModule());

Expand Down
3 changes: 3 additions & 0 deletions test/IRGen/preserve_for_debugger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ class Baz: Foo {

struct Qux {
@Bar(wrappedValue: Baz()) private var baz: Baz
// Baz instance that is never accessed.
@Bar(wrappedValue: Baz()) private var baz2: Baz

func f() {
print(self.baz) // break here
Expand All @@ -64,3 +66,4 @@ let qux = Qux()
qux.f()

// CHECK: !DISubprogram(name: "baz.get"
// CHECK: !DISubprogram(name: "baz2.get"