-
Notifications
You must be signed in to change notification settings - Fork 18k
cmd/compile: incomplete dead code elimination for conditional #33713
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
Is this a manually constructed example or does this come up often in Go programs? Was it in hand written or generated code? It will be very slow up to impossible to find all boolean tautologies. So there likely has to be some tradeoff how much time is spend in the compiler on finding these vs just leaving them as is. Which brings the question how often this comes up in what forms in non crafted examples and where. Relevant discussion: #32713 |
I probably didn't express my intent in a clear manner. I'm not saying these kinds of constructs should be treated as tautologies, but I happened to notice that this was happening in this case https://godbolt.org/z/o3uNbH. |
As the patterns that could be optimised can vary widely and we should make sure we detect the ones (without to much overhead) that help with real world code (which ideally we use simplified as test cases) Do you know examples of similar patterns not optimised in existing codebases? If I understand the code and reply correctly the deeper issue here is that the compiler misses some dead code elimination of code that has no effect on the return value and no side effects? |
I'm not really a Go developer, I just like the language and enjoy experimenting with the compiler. That said, I don't have any concrete example of this issue (apart from these ones), but I believe this is a shortcoming of dead code elimination, given that for https://godbolt.org/z/q8MXUH and https://godbolt.org/z/tDm4z2 the compiler produces some unnecessary instructions that end up having no effect in the outcome of the function (the return value is always 0). |
Yeah, that is what I meant all along. Thanks for your help in renaming the issue accordingly, @martisch 😉 |
I don't think we want to do anything explicit for cases like this. Unless someone can demonstrate that this occurs in real code. |
I believe this is a similar example. It comes from bytes.(*Buffer).WriteByte.
I don't know enough about passes to know where this type of optimization might be done. This is from an objdump on ppc64le, but the x86 objdump looks similar with respect the unnecessary branches and compares I mentioned. |
Lynn's example feels like the shortcircuit pass should handle it. It optimizes things of the form:
shortcircuit will make Maybe the merged thing is not a boolean, but an int constant?
We could also make |
I have a shortcircuit CL outstanding. Perhaps it helps. |
Is this the one you mean @josharian https://go-review.googlesource.com/c/go/+/178197? I can give it a try. This happens in some std pkgs, I've seen it in runtime and strconv so far. |
The problem still occurs with this CL.
By itself, there should be no need for the ISEL and the CMP following it. However, the 2nd CMP has a phi operand, and the other phi operands are constant settings that should be short circuited. This happens on ppc64le as well as AMD64 AFAICT. I can post more detail, such as an objdump as above or more ssa output if helpful. |
Thanks for checking. I'm mostly AFK this cycle, but I will try to take a look at this once my CL above lands, since I've been wading through shortcircuit recently. |
Turns out I'm not realistically going to investigate this during the 1.14 cycle, so if someone else wants to take a look, go for it. |
This CL https://go-review.googlesource.com/c/go/+/239457 seems to help this issue. |
No longer reproduces |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes.
What did you do?
I compiled the following function and inspected the generated assembly (https://godbolt.org/z/q8MXUH):
What did you expect to see?
I expected the generated code to be a simple return, as in this case: https://godbolt.org/z/o3uNbH.
What did you see instead?
Instead, the generated code contains a redundant ´jump´ instruction:
The same happens for this function https://godbolt.org/z/tDm4z2.
The text was updated successfully, but these errors were encountered: