Skip to content

cmd/compile: avoid stack temporary for struct literal assignment to addressable targets#78598

Open
chabbimilind wants to merge 1 commit intogolang:masterfrom
chabbimilind:complit-addr-optimization
Open

cmd/compile: avoid stack temporary for struct literal assignment to addressable targets#78598
chabbimilind wants to merge 1 commit intogolang:masterfrom
chabbimilind:complit-addr-optimization

Conversation

@chabbimilind
Copy link
Copy Markdown

@chabbimilind chabbimilind commented Apr 9, 2026

When assigning a struct/array literal to a non-local addressable target
(slice index, global, pointer deref, struct field through pointer), the
compiler previously created a stack temporary, zeroed it, filled fields,
then bulk-copied to the destination. This was because oaslit() required
LHS to be isSimpleName() (stack-local variable).

Extend oaslit with oaslitAddr, which takes the address of the destination
once and decomposes the literal into direct field stores through that
pointer. For partially-initialized literals, the destination is zeroed
in-place before filling fields, eliminating the bulk copy entirely.

Safety is ensured by compLitFieldsSafe/exprSafeForDirectStore, which
conservatively accept only constants, nil, and stack-local non-addrtaken
names as RHS values -- these cannot alias with any addressable destination.
When nested struct literals are partially initialized, compLitAllFieldsSet
detects this and ensures the destination is zeroed before field stores.

Benchmark results (arm64, Apple M1 Max):

                             |  baseline   |           optimized            |
                             |   sec/op    |   sec/op     vs base           |

StructLitAssign/SliceLiteral 8.91n +/- 1% 4.57n +/- 2% -48.75% (p=0.000)
StructLitAssign/Global 8.04n +/- 1% 3.97n +/- 1% -50.55% (p=0.000)
StructLitAssign/PtrDeref 8.22n +/- 0% 3.84n +/- 1% -53.37% (p=0.000)
StructLitAssign/Nested 3.99n +/- 1% 2.93n +/- 2% -26.62% (p=0.000)
StructLitAssign/Embedded 4.01n +/- 1% 2.91n +/- 1% -27.32% (p=0.000)
StructLitAssign/FuncCallRHS 9.17n +/- 1% 5.62n +/- 1% -38.75% (p=0.000)
StructLitAssign/PartialHalf 5.79n +/- 1% 4.12n +/- 1% -28.74% (p=0.000)
StructLitAssign/AliasRHS 7.93n +/- 1% 7.94n +/- 2% ~ (no regression)

Test plan:

  • go test cmd/compile/... -count=1 -- all pass
  • go test cmd/internal/testdir -run Test -count=1 -- asmcheck tests pass
  • ./all.bash -- all pass (only pre-existing TestFortran failure due to local gfortran arch mismatch)
  • Benchmarks with benchstat -count=8 -benchtime=5s show 27-53% improvement, no regressions

Fixes #78597

@google-cla
Copy link
Copy Markdown

google-cla bot commented Apr 9, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@gopherbot
Copy link
Copy Markdown
Contributor

This PR (HEAD: 846ac1c) has been imported to Gerrit for code review.

Please visit Gerrit at https://go-review.googlesource.com/c/go/+/764680.

Important tips:

  • Don't comment on this PR. All discussion takes place in Gerrit.
  • You need a Gmail or other Google account to log in to Gerrit.
  • To change your code in response to feedback:
    • Push a new commit to the branch used by your GitHub PR.
    • A new "patch set" will then appear in Gerrit.
    • Respond to each comment by marking as Done in Gerrit if implemented as suggested. You can alternatively write a reply.
    • Critical: you must click the blue Reply button near the top to publish your Gerrit responses.
    • Multiple commits in the PR will be squashed by GerritBot.
  • The title and description of the GitHub PR are used to construct the final commit message.
    • Edit these as needed via the GitHub web interface (not via Gerrit or git).
    • You should word wrap the PR description at ~76 characters unless you need longer lines (e.g., for tables or URLs).
  • See the Sending a change via GitHub and Reviews sections of the Contribution Guide as well as the FAQ for details.

@gopherbot
Copy link
Copy Markdown
Contributor

Message from Gopher Robot:

Patch Set 1:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/764680.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Copy Markdown
Contributor

Message from Daniel Morsing:

Patch Set 2:

(2 comments)


Please don’t reply on this GitHub thread. Visit golang.org/cl/764680.
After addressing review feedback, remember to publish your drafts!

@chabbimilind chabbimilind force-pushed the complit-addr-optimization branch from 846ac1c to ad20214 Compare April 13, 2026 19:17
@gopherbot
Copy link
Copy Markdown
Contributor

This PR (HEAD: ad20214) has been imported to Gerrit for code review.

Please visit Gerrit at https://go-review.googlesource.com/c/go/+/764680.

Important tips:

  • Don't comment on this PR. All discussion takes place in Gerrit.
  • You need a Gmail or other Google account to log in to Gerrit.
  • To change your code in response to feedback:
    • Push a new commit to the branch used by your GitHub PR.
    • A new "patch set" will then appear in Gerrit.
    • Respond to each comment by marking as Done in Gerrit if implemented as suggested. You can alternatively write a reply.
    • Critical: you must click the blue Reply button near the top to publish your Gerrit responses.
    • Multiple commits in the PR will be squashed by GerritBot.
  • The title and description of the GitHub PR are used to construct the final commit message.
    • Edit these as needed via the GitHub web interface (not via Gerrit or git).
    • You should word wrap the PR description at ~76 characters unless you need longer lines (e.g., for tables or URLs).
  • See the Sending a change via GitHub and Reviews sections of the Contribution Guide as well as the FAQ for details.

…ddressable targets

When assigning a struct/array literal to a non-local addressable target
(slice index, global, pointer deref, struct field through pointer), the
compiler previously created a stack temporary, zeroed it, filled fields,
then bulk-copied to the destination. This was because oaslit() required
LHS to be isSimpleName() (stack-local variable).

Extend oaslit with oaslitAddr, which takes the address of the destination
once and decomposes the literal into direct field stores through that
pointer. For partially-initialized literals, the destination is zeroed
in-place before filling fields, eliminating the bulk copy entirely.

Safety is ensured by compLitFieldsSafe/exprSafeForDirectStore, which
conservatively accept only constants, nil, and stack-local non-addrtaken
names as RHS values — these cannot alias with any addressable destination.
When nested struct literals are partially initialized, compLitAllFieldsSet
detects this and ensures the destination is zeroed before field stores.

                                 │  baseline   │           optimized            │
                                 │   sec/op    │   sec/op     vs base           │
StructLitAssign/SliceLiteral       8.91n ± 1%    4.57n ± 2%  -48.75% (p=0.000)
StructLitAssign/Global             8.04n ± 1%    3.97n ± 1%  -50.55% (p=0.000)
StructLitAssign/PtrDeref           8.22n ± 0%    3.84n ± 1%  -53.37% (p=0.000)
StructLitAssign/Nested             3.99n ± 1%    2.93n ± 2%  -26.62% (p=0.000)
StructLitAssign/Embedded           4.01n ± 1%    2.91n ± 1%  -27.32% (p=0.000)
StructLitAssign/FuncCallRHS        9.17n ± 1%    5.62n ± 1%  -38.75% (p=0.000)
StructLitAssign/PartialHalf        5.79n ± 1%    4.12n ± 1%  -28.74% (p=0.000)

Fixes golang#78597
@chabbimilind chabbimilind force-pushed the complit-addr-optimization branch from ad20214 to b958db1 Compare April 13, 2026 19:40
@gopherbot
Copy link
Copy Markdown
Contributor

This PR (HEAD: b958db1) has been imported to Gerrit for code review.

Please visit Gerrit at https://go-review.googlesource.com/c/go/+/764680.

Important tips:

  • Don't comment on this PR. All discussion takes place in Gerrit.
  • You need a Gmail or other Google account to log in to Gerrit.
  • To change your code in response to feedback:
    • Push a new commit to the branch used by your GitHub PR.
    • A new "patch set" will then appear in Gerrit.
    • Respond to each comment by marking as Done in Gerrit if implemented as suggested. You can alternatively write a reply.
    • Critical: you must click the blue Reply button near the top to publish your Gerrit responses.
    • Multiple commits in the PR will be squashed by GerritBot.
  • The title and description of the GitHub PR are used to construct the final commit message.
    • Edit these as needed via the GitHub web interface (not via Gerrit or git).
    • You should word wrap the PR description at ~76 characters unless you need longer lines (e.g., for tables or URLs).
  • See the Sending a change via GitHub and Reviews sections of the Contribution Guide as well as the FAQ for details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cmd/compile: struct literal assignment to non-local targets generates unnecessary stack temporary

2 participants