You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
cmd/compile: Prove doesn't understand division of slice length #57077
Thoses examples you linked are different, they incorrectly assume that slice[:i] should prove slice[i:].
This is wrong because go use [x,y) ranging, that means :i is one past the index that will be taken, while i: is the last index that will be taken. In other words i <= cap(slice) does not prove i < cap(slice).
Here we have a different issue, actually it reproduce just like this without this double left / right reslicing:
It should BCE but it doesn't, hopefully this can convaince you, if we feed a 0 length slice in the original function we will get this after folding out all constants:
In other words feeding an empty slice produce two empty slices.
This was easy to fix, I just had to teach the compiler that for unsigned divisions z := x / y and unsigned right shifts (which are really just divisions by powers of two) z := x >> y, z is always smaller or equal to x.
changed the title [-]cmd/compile: Prove fails bce when taking the division of length as index[/-][+]cmd/compile: Prove doesn't understand division of slice length[/+]on Dec 14, 2022
Activity
gopherbot commentedon Dec 4, 2022
Change https://go.dev/cl/455095 mentions this issue:
cmd/compile: teach prove about unsigned division, modulus and rhszigo101 commentedon Dec 5, 2022
This is a correct behavior. See #16595
Also see the "Example 3" section in https://go101.org/article/bounds-check-elimination.html
zigo101 commentedon Dec 5, 2022
Ah, it looks this is a different problem.
What about the length ofsis zero?[update] Sorry, the bounds should be eliminated here.
Jorropo commentedon Dec 5, 2022
I've seen your:
But I'm responding anyway for posterity and future readers. 🙂
Thoses examples you linked are different, they incorrectly assume that
slice[:i]should proveslice[i:].This is wrong because go use
[x,y)ranging, that means:iis one past the index that will be taken, whilei:is the last index that will be taken. In other wordsi <= cap(slice)does not provei < cap(slice).Here we have a different issue, actually it reproduce just like this without this double left / right reslicing:
It should BCE but it doesn't, hopefully this can convaince you, if we feed a 0 length slice in the original function we will get this after folding out all constants:
In other words feeding an empty slice produce two empty slices.
This was easy to fix, I just had to teach the compiler that for unsigned divisions
z := x / yand unsigned right shifts (which are really just divisions by powers of two)z := x >> y,zis always smaller or equal tox.prattmic commentedon Dec 5, 2022
cc @golang/compiler
[-]cmd/compile: Prove fails bce when taking the division of length as index[/-][+]cmd/compile: Prove doesn't understand division of slice length[/+]cmd/compile: teach prove about unsigned division, modulus and rsh