-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
cmd/compile: boolean operations not constant folded #32713
Comments
Where those examples constructed or found in real code? Im not sure how often they would trigger. What is the scope of the feature request:
Im asking because if its the former we want to collect cases that are easy to detect and happen frequently and if its the later whether Go gc needs to contain a full propositional prover (e.g. SAT solver) applied to boolean expressions that also keeps side effects if any. The later might be too complex and takes to much compile time vs the gain achieved on real code. Looking at gcc C++ it seems to detect Constant folding (the above dont contain constants) in Go gc should generally work e.g.: Interestingly gcc Go seems to not completely optimize |
I found these examples out of curiosity, because I wanted to know how the Go compile would behave, but I imagine code like this being auto-generated. |
You need to specify optimization level. The default is -O0, which does not optimize. -O1 and above do. |
As @martisch mentioned, |
Oops. Thanks for the correction. I was distracted from adding flags by GCC C++ seemingly optimizing this even without any additional flags in godbolt: https://godbolt.org/z/SMYxsz |
Personally, I've never seen code like this. I was just curious about the output of the compiler, given this two functions. |
Since this seems like an edge case that won't appear in the wild or cause people any issues, it sounds like we should close this. @martisch can you confirm/close this if so? |
TL;DR I think we should wait to see an actual use case where this would help and then implement some optimizations for propositional tautologies/contradicions preferably in the compiler backend. This might very well come up after some optimizations triggered in the compiler backend but then detecting tautologies/contradictions in the frontend for simple cases like shown wont help. To not solve the made up cases and not detect the actual cases where this triggers I would think we should wait for code in the wild to show up that could use this. If somebody has an example that can not be just simplified by code review without compromising readability I think we can close this. Linters and static code checks meanwhile can detect simple cases such as @marigonzes thank you for the report. Filing reports of optimization opportunities especially if you can find existing code that can benefit from them is much appreciated. |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes.
What did you do?
I inspected the assembly code generated for the following functions:
What did you expect to see?
I expected them to be compiled to a simple return: false in the first case, true in the second.
What did you see instead?
Instead, boolean operations are performed on the variable
b
.The text was updated successfully, but these errors were encountered: