-
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: missing BCE in range expression #54753
Comments
cc @golang/runtime |
This is a consequence of the interaction between global variables and the details of the for loop. For case 1, the two reads of the global variable ( For the third loop, For the fourth loop, there is no global variable so there's no possibility of In the second loop, the read of Moral of the story: don't use global variables. The compiler needs to assume that they might get modified. In theory, we can assume that global variables don't change if no synchronization operations occur between two loads. But in practice, the compiler "approximates" that by assuming any write operation could be a synchronization. So two reads from the same global are only assumed to be equal if no write separates them. If you have to use global variables, do:
|
Makes sense, thanks for explaining that. |
I'm going to close this, as I don't think there's anything to do here. We could improve the compiler to change its CSE rule from "any write might be synchronization" to something more complicated, but that's a really big lift for an issue that's pretty easy to work around. |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes.
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
compiler output visible on godbolt
What did you expect to see?
No bounds checks.
What did you see instead?
Bounds check when ranging over a variable declared and initialized separately.
The text was updated successfully, but these errors were encountered: