-
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: shallow copy of a struct does not always compile as MOVL #53810
Comments
This doesn't need to be a proposal - it seems like an instance where the compiler could do a better job. |
cc @randall77 |
The compiler already does the optimization you're requesting:
compiles to 2 There is also the semantics question:
I don't think it is mentioned in the memory model spec, but each field of a struct is its own variable. The Go spec hints at that:
As a result, the memory model does not forbid a result of x:1,y:2 or x:2,y:1. In other words, struct reads and writes are not atomic. If you want to avoid tearing of a 64-bit value, you need to use a 64-bit type, not a struct with 2 32-bit fields. |
yes, it's true that write a struct of two variable are not atomic. it confuse me that write value to struct(a := Example{...}) and copy value to struct(a:= b) behave not same. code below:
will be compile to
go env:
|
Ah, ok. Here's a simpler reproducer, using 2 loads/stores instead of one.
I think a vardef for the temporary used to pass into |
Change https://go.dev/cl/419320 mentions this issue: |
the
y:=x
will be compiled to two movl while thex:=Example{x:1, y:1}
compiled to single movq.it confuse me about the memory bevavior, assumpt we have one routine write
x=Example{x:1, y:1}
and another writex=Example{x:2, y:2}
, according to the memory model, x will either bex:1,y:1
orx:2, y:2
. how ever if we dothen, we could get
x:1, y:2
!!!The text was updated successfully, but these errors were encountered: