Skip to content

Optimizer can't remove unused code without side effects #66496

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

Closed
DmT021 opened this issue Jun 9, 2023 · 2 comments · Fixed by #66592
Closed

Optimizer can't remove unused code without side effects #66496

DmT021 opened this issue Jun 9, 2023 · 2 comments · Fixed by #66592
Assignees
Labels
compiler The Swift compiler itself DCE Area → compiler → SILOptimizer: Dead Code Elimination feature A feature request or implementation SILOptimizer Area → compiler: SIL optimization passes

Comments

@DmT021
Copy link
Contributor

DmT021 commented Jun 9, 2023

Motivation

While prototyping a solution to this issue, I encountered a strange behavior of the optimizer.

It appears that the optimizer considers init with an assignment to a class let to be optimizable. However, it does not consider init with an assignment to a class var to be optimizable.

Let's compare these two pieces of code.

final class Foo<T> {
    let v: T

    init(_ v: T) {
        self.v = v
    }
}

@inline(never)
func testFoo() {
    _ = Foo(1)
}

And

final class Bar<T> {
    var v: T

    init(_ v: T) {
        self.v = v
    }
}

@inline(never)
func testBar() {
    _ = Bar(1)
}

The first one (with let field) compiles into no code aside from ret, as expected:

output.testFoo() -> ():
        ret

And the second one (with var field) compiles into invocation of __swift_instantiateConcreteTypeFromMangledName and swift_initStackObject:

output.testBar() -> ():
        sub     rsp, 24
        lea     rdi, [rip + (demangling cache variable for type metadata for output.Bar<Swift.Int>)]
        call    __swift_instantiateConcreteTypeFromMangledName
        mov     rsi, rsp
        mov     rdi, rax
        call    swift_initStackObject@PLT
        add     rsp, 24
        ret

Live code
It feels a little like a bug somewhere.

Solution

I don't know.

Additional context
Discussion

@DmT021 DmT021 added feature A feature request or implementation triage needed This issue needs more specific labels labels Jun 9, 2023
@eeckstein eeckstein self-assigned this Jun 9, 2023
eeckstein added a commit to eeckstein/swift that referenced this issue Jun 13, 2023
…ations to a static accesses

In case of `var` initializations, SILGen creates a dynamic begin/end_access pair around the initialization store.
If it's an initialization (and not a re-assign) it's guanranteed that it's an exlusive access and we can convert the access to an `[init] [static]` access.

swiftlang#66496
eeckstein added a commit to eeckstein/swift that referenced this issue Jun 13, 2023
Don't let access instructions prevent eliminating dead allocations

swiftlang#66496
@eeckstein
Copy link
Contributor

#66592

eeckstein added a commit to eeckstein/swift that referenced this issue Jun 14, 2023
…ations to a static accesses

In case of `var` initializations, SILGen creates a dynamic begin/end_access pair around the initialization store.
If it's an initialization (and not a re-assign) it's guanranteed that it's an exlusive access and we can convert the access to an `[init] [static]` access.

swiftlang#66496
eeckstein added a commit to eeckstein/swift that referenced this issue Jun 14, 2023
Don't let access instructions prevent eliminating dead allocations

swiftlang#66496
@AnthonyLatsis AnthonyLatsis added compiler The Swift compiler itself SILOptimizer Area → compiler: SIL optimization passes DCE Area → compiler → SILOptimizer: Dead Code Elimination and removed triage needed This issue needs more specific labels labels Jun 15, 2023
@DmT021
Copy link
Contributor Author

DmT021 commented Jun 22, 2023

@eeckstein I'm not sure if you have seen updates in the thread on the forum, but in case you didn't, could you please take a look? I found two more cases when a dead object is not eliminated.

  1. ManagedBuffer - https://godbolt.org/z/TYP55Ezzx
  2. String-specialized generic class - https://godbolt.org/z/cbfss9azz

eeckstein added a commit to eeckstein/swift that referenced this issue Jun 26, 2023
This allows to eliminate dead allocations of a ManagedBuffer object.

swiftlang#66496
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler The Swift compiler itself DCE Area → compiler → SILOptimizer: Dead Code Elimination feature A feature request or implementation SILOptimizer Area → compiler: SIL optimization passes
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants