-
Notifications
You must be signed in to change notification settings - Fork 18k
cmd/compile: unnecessary bounds check #29872
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
Comments
Please use the issue template. You don't specify what version of Go, for one, or which compiler. |
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 |
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. |
I think this would just involve adding a new case for unsigned remainder to the prove pass. |
cc @rasky |
@randall77 Where should I look at in the source to fix this? I see the current behavior convert the If I assigned |
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. |
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. |
cc also @zdjones |
@randall77 @rasky how to make new rules applied? I tried removing rule at line 1161, rebuild, then run ssa dump, |
@cuonglm : You need to regenerate the Go code from the rules. |
@randall77 yes, I re-gererated already, forgot to mention that.
|
@randall77 ah, nevermind, it's browser cache issue. |
@randall77 but even with that rule removed, bound check is still added. |
Well, yes, with that rule removed, The Mod64U should now appear unmolested to the prove pass. You'd then need to add to the prove pass facts about the result of Mod64U. |
@randall77 so base on rasky's comment above, is it ok to split div reduction to its own pass? |
I'd rather not have a whole new pass just for div. |
I see Also, for clarification, is |
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
Yes, most of the bounds check removal happens in the prove pass. |
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?) |
@josharian I think your CL would leave the mod in place, then could the IsInBounds be removed with (no need for prove)?
Also, won't some of the other Mod rules still trigger whenever the constant is a power of two? |
Yes, some of the (At that point, you're well on your way to taking over my efforts to delete
Yes, but boundedness and |
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.)? |
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. |
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. |
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
The text was updated successfully, but these errors were encountered: