Skip to content

x/tools/internal/refactor/inline: a read from a non-address-taken variable should commute with global effects #68948

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

Open
adonovan opened this issue Aug 19, 2024 · 1 comment
Labels
Analysis Issues related to static analysis (vet, x/tools/go/analysis) NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Refactoring Issues related to refactoring tools Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@adonovan
Copy link
Member

adonovan commented Aug 19, 2024

@lfolger reported a suboptimal inliner result of this form:

    var g = foo()

    func f(x, y any) { f2(x, y) }

    // before
    f(complicated(), g)

    // after
    var ( x = complicated(); y = g )
    f2(x, y)

In this case, the inliner's effects analysis assumed conservatively that the read from global var g would not commute with the call to complicated(). However, the variable g is not address-taken and is assigned only in its declaration.

The inliner's effects analysis should enumerate all non-address-taken variables (non-exported variables that appear in an lvalue position only in their own declaration) and allow reads of them to commute with global effects such as complicated(), resulting in this more optimal inlining: f2(g, complicated()).

@gopherbot gopherbot added the Tools This label describes issues relating to any tools in the x/tools repository. label Aug 19, 2024
@gopherbot gopherbot added this to the Unreleased milestone Aug 19, 2024
@adonovan
Copy link
Member Author

adonovan commented Aug 19, 2024

@lfolger raises the good point that effects analysis may be modeling reads too strictly in the first place. The spec guarantees only that calls, receives, and &&/|| operations are ordered, but not reads from memory, so if the program relies on g being read after the evaluation of complicated(), it already has a latent bug.

(The effects analysis still needs to model reads for other purposes: for example, if the callee function body repeatedly evaluates the parameter, and the argument is a read from a mutable variable, the read must not commute with some other side effect in the callee body.)

@adonovan adonovan added Refactoring Issues related to refactoring tools Analysis Issues related to static analysis (vet, x/tools/go/analysis) labels Aug 19, 2024
@cherrymui cherrymui added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Aug 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Analysis Issues related to static analysis (vet, x/tools/go/analysis) NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Refactoring Issues related to refactoring tools Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

3 participants