Skip to content

cmd/compile: unnecessary bounds check #29872

Closed

Description

@mariecurried

What did you do?

I compiled the following program to see its assembly code: https://godbolt.org/z/OcRvPE

What did you expect to see?

I expected the access to the slice to not be bounds checked

What did you see instead?

A bounds check was generated by the Go compiler

Activity

  1. bradfitz commented on Jan 22, 2019

    @bradfitz
    Contributor

    Please use the issue template.

    You don't specify what version of Go, for one, or which compiler.

  2. added
    WaitingForInfoIssue is not actionable because of missing required information, which needs to be provided.
    on Jan 22, 2019
  3. bradfitz commented on Jan 22, 2019

    @bradfitz
    Contributor

    Oh, I see your link goes to a site where "gc (tip)" is selected. But it's faster for us if you put all the relevant information in the bug. And conventionally we share play.golang.org code snippets. But in this case you can just inline the code:

    package test
    
    func test(slc []byte, i uint) {
    	if len(slc) >= 3 {
    		_ = slc[i%3]
    	}
    }

    /cc @randall77 @aclements

  4. added
    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
    and removed
    WaitingForInfoIssue is not actionable because of missing required information, which needs to be provided.
    on Jan 22, 2019
  5. added this to the Go1.13 milestone on Jan 22, 2019
  6. mariecurried commented on Jan 22, 2019

    @mariecurried
    Author

    Sorry, @bradfitz. I just assumed that you were familiar with Godbolt and, as you found out, the version is displayed there. But I'll take note for future reference.

  7. randall77 commented on Jan 22, 2019

    @randall77
    Contributor

    I think this would just involve adding a new case for unsigned remainder to the prove pass.

  8. josharian commented on Jan 22, 2019

    @josharian
    Contributor
  9. cuonglm commented on Apr 4, 2019

    @cuonglm
    Member

    @randall77 Where should I look at in the source to fix this?

    I see the current behavior convert the Mod64u to a Sub64, so prove failed.

    If I assigned i = <any non negative number> before if, prove success, as it sees Const64.

  10. randall77 commented on Apr 4, 2019

    @randall77
    Contributor

    True, that makes it trickier. We'd either have to recognize the results of the div->mul lowering (generic.rules:1161), or push the div->mul lowering later. Both seem challenging.

  11. rasky commented on Apr 4, 2019

    @rasky
    Member

    Maybe we should split div reduction out of generic into its own pass that runs late. It sounds something that later passes aren't dependent upon, and it tends to make code harder to analyze for everybody.

  12. josharian commented on Apr 4, 2019

    @josharian
    Contributor

    cc also @zdjones

  13. 8 remaining items

  14. cuonglm commented on Apr 22, 2019

    @cuonglm
    Member

    Can we move div reduction to be part of late opt?

    I see opt and late opt use the same function opt. It seems to be hard without break current function signature, or using a global variable to toggle apply div reduction for it.

    Also, for clarification, is IsInBounds will be applied in prove pass?

  15. randall77 commented on Apr 22, 2019

    @randall77
    Contributor

    I see opt and late opt use the same function opt. It seems to be hard without break current function signature, or using a global variable to toggle apply div reduction for it.

    They use the same rule file, yes. But you can condition the rules on the pass. I thought we had an example of this already, but apparently not. I think just adding && v.Block.Func.pass.name == "..." would do it.

    Also, for clarification, is IsInBounds will be applied in prove pass?

    Yes, most of the bounds check removal happens in the prove pass.

  16. josharian commented on Apr 22, 2019

    @josharian
    Contributor

    I thought we had an example of this already, but apparently not.

    This is https://go-review.googlesource.com/c/go/+/129379, which is on hold because it was part of an effort to move some BCE from walk.go to SSA, and I never finished the project. I can dust off and complete that particular CL, though, if that would be helpful. Also, @bmkessler's modulus changes may do something similar. (https://go-review.googlesource.com/c/go/+/168037?)

  17. zdjones commented on Apr 22, 2019

    @zdjones
    Contributor

    I can dust off and complete that particular CL, though, if that would be helpful

    @josharian I think your CL would leave the mod in place, then could the IsInBounds be removed with (no need for prove)?

    (IsInBounds (Mod32u _ x) y) && x <= y -> (ConstBool [1])
    (IsInBounds (Mod64u _ x) y) && x <= y -> (ConstBool [1])
    

    Also, won't some of the other Mod rules still trigger whenever the constant is a power of two?
    (Mod32u <t> n (Const32 [c])) && isPowerOfTwo(c&0xffffffff) -> (And32 n (Const32 <t> [(c&0xffffffff)-1]))
    If those are also not needed by passes between opt and lateopt, they could also be skipped and then removed by the above rule? I'll try, but would love a sanity check first.

  18. josharian commented on Apr 22, 2019

    @josharian
    Contributor

    Yes, some of the IsInBounds could be removed during the rewrite rules. I started on this in (e.g.) https://go-review.googlesource.com/c/go/+/129383 and https://go-review.googlesource.com/c/go/+/129380. You might also want something like https://go-review.googlesource.com/c/go/+/129384 so that you can easily add tests.

    (At that point, you're well on your way to taking over my efforts to delete func bounded from walk.go, which I would be delighted by. See also the other CLs linked in the "Relation chain" from any of those CLs.)

    Also, won't some of the other Mod rules still trigger whenever the constant is a power of two?

    Yes, but boundedness and And are easy to reason about in a way that magic division is not; we should just add additional rules to remove IsInBounds based on bit masks. I believe some of the CLs linked above do some of that.

  19. zdjones commented on Apr 22, 2019

    @zdjones
    Contributor

    Yeah, when I first saw this issue, I started trying to reason my way back through the reduced mod...and abandoned that idea pretty quickly.

    Is there any preference in using a rewrite rule vs the prove pass (i.e. due to differences in performance, complexity/maintenance, etc.)?

  20. josharian commented on Apr 22, 2019

    @josharian
    Contributor

    IMO, for simple things adding them to the rewrite pass is preferable, since it provides the opportunity for other rewrite rules to keep working on the simplified form. And rules are pretty easy to read and maintain.

  21. cuonglm commented on Apr 23, 2019

    @cuonglm
    Member

    @zdjones

    I think your CL would leave the mod in place, then could the IsInBounds be removed with (no need for prove)?

    I tried but the bounds check is still there. I think we have to add some thing to fact table, like @randall77 said in comment above.

  22. modified the milestones: Go1.13, Go1.14 on Jun 4, 2019
  23. modified the milestones: Go1.14, Backlog on Oct 9, 2019
  24. fice-t commented on Nov 19, 2025

    @fice-t

    @randall77 This appears to have been solved on the latest dev branch (note that the code needs to be rewritten to avoid being optimized out completely).

  25. randall77 commented on Nov 19, 2025

    @randall77
    Contributor

    @fice-t thanks. Probably CL 714160 or one of the CLs around it.
    Closing.

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

    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.Performancehelp wanted

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions