Skip to content

mir::Constant::span is a footgun for mir optimizations #77608

Open

Description

@oli-obk

The field is part of the PartialEq impl, and thus two constants that are equal except in their span will compare inequal. This causes problems in #77551 (comment) and #77549 (comment) and possibly in other places.

The "trivial" way to resolve this is to manually implement PartialEq and make it skip the Span field.
Downside: now we may cause weird diagnostics after such an optimization has been applied, because if one of the constants is eliminated, all diagnostics are reported on the other diagnostic's site. We'd need a multispan and properly combine it. That will be hard to ensure to happen if we just silently change PartialEq

cc @rust-lang/wg-mir-opt

Activity

  1. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    A-mir-optArea: MIR optimizations
    on Oct 6, 2020
  2. tmiasko commented on Nov 3, 2020

    @tmiasko
    Contributor

    The issue also applies to implicit const () at the end of block. Replacing it with an empty tuple aggregate in a MIR builder triggers a few extra optimizations in mir-opt test suite.

  3. added
    E-mediumCall for participation: Medium difficulty. Experience needed to fix: Intermediate.
    E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.
    on Nov 3, 2020
  4. camelid commented on Nov 15, 2020

    @camelid
    Member

    So are we going to use the "trivial" fix – adding a custom impl of PartialEq that ignores span – or some kind of more complex one – I think you mentioned a "multispan"?

  5. oli-obk commented on Nov 16, 2020

    @oli-obk
    ContributorAuthor

    Ideally we'd remove the PartialEq impl and leave a comment that it can't have that impl, but I'm not sure what the fallout of that is.

  6. camelid commented on Nov 16, 2020

    @camelid
    Member

    Perhaps we could add a method on Constant that checks equality while ignoring span?

  7. added
    A-MIRArea: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html
    on Nov 16, 2020
  8. simonvandel commented on Nov 16, 2020

    @simonvandel
    Contributor

    Only adding a new method that ignores span equality does not remove the footgun though. Other code would still use the PartialEq which still compares unintuitively because of the span.

  9. camelid commented on Nov 16, 2020

    @camelid
    Member

    I meant that we should remove PartialEq as Oli suggested and then add a new method that explicitly ignores span.

  10. camelid commented on Nov 16, 2020

    @camelid
    Member

    @spastorino Is it okay if I work on this a bit?

  11. spastorino commented on Jun 8, 2022

    @spastorino
    Member

    If someone wants to work on this, feel free to reuse my code. I don't remember how useful it is anymore since it has more than 1 year :). Anyway, feel free to contact me on a Zulip topic.

  12. removed their assignment
    on Aug 30, 2022
  13. added
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Apr 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-MIRArea: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.htmlA-diagnosticsArea: Messages for errors, warnings, and lintsA-mir-optArea: MIR optimizationsE-mediumCall for participation: Medium difficulty. Experience needed to fix: Intermediate.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions