Skip to content

Optimizer: support statically initialized Atomic and Mutex globals #83505

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

Merged
merged 10 commits into from
Aug 4, 2025

Conversation

eeckstein
Copy link
Contributor

For example:

let global = Atomic<Int>(0)

This PR adds support for @_rawLayout structs and non-loadable types in InitializeStaticGlobals.

@eeckstein
Copy link
Contributor Author

@swift-ci test

@eeckstein
Copy link
Contributor Author

@swift-ci apple silicon benchmark

@eeckstein eeckstein requested review from Azoy, atrick, meg-gupta and nate-chandler and removed request for xedin, slavapestov, jckarter, kavon and hborla August 2, 2025 18:13
Copy link
Contributor

@Azoy Azoy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️ 🎉

case let injectEnum as InjectEnumAddrInst:
if injectEnum.element.hasAssociatedValues {
if !injectEnum.operand.value.type.isLoadable(in: injectEnum.parentFunction) {
// TODO: we don't support non-loadable enum cases with payload yet, because IRGen support is missing.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What IRGen support is missing here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FixedTypeInfo (= used for non-loadable types) is missing the ability to pack a payload into an enum.
I added this in a comment.

@Azoy
Copy link
Contributor

Azoy commented Aug 2, 2025

It would also be nice to see if this patch allows statically initializing Mutex (on at least Darwin, Linux, and Windows)

which allows e.g. eliminating dead `Atomic` values
…er` when generating a value constructor for an empty non-copyable type

`zeroInitializer` is blocking optimizations. It is not needed because the constructor will initialize the memory anyway.
We only need to tell mandatory passes that this memory should be treated as initialized.
@eeckstein eeckstein force-pushed the non-loadable-global-inits branch from 763d3a4 to 6149e2e Compare August 3, 2025 09:10
@eeckstein eeckstein requested a review from rjmccall as a code owner August 3, 2025 09:10
… struct with `@_rawLayout`

Even if the global declaration is a `let`.
Raw-layout storage may be mutated even for let-variables.
Treating such variables as non-let ensures that optimization don't assume that they are not mutated.
…ialized globals

Just use the same method to create the zero initializer constant as in `emitBuiltinCall`.
…ut` types, like `Atomic`

Look through `@_rawLayout` projections, which "type casts" a raw-layout struct to it's content, which must match the like-type of the raw-layout, e.g.

```
@_rawLayout(like: T)
struct S {}

  %2 = builtin "addressOfRawLayout"<S>(%1 : $*S) : $Builtin.RawPointer
  %3 = pointer_to_address %2 to $*T
```
TODO: we don't support non-loadable enum cases with payload, yet, because IRGen support is missing.
@eeckstein eeckstein force-pushed the non-loadable-global-inits branch from 6149e2e to eaf38da Compare August 3, 2025 15:32
@eeckstein eeckstein changed the title InitializeStaticGlobals: support statically initialized Atomic globals Optimizer: support statically initialized Atomic and Mutex globals Aug 3, 2025
@eeckstein
Copy link
Contributor Author

eeckstein commented Aug 3, 2025

It would also be nice to see if this patch allows statically initializing Mutex

This is a good point and in fact there was missing a small thing to make this happen. I fixed it and added a test

@eeckstein
Copy link
Contributor Author

@swift-ci test

@eeckstein eeckstein merged commit 143d8ff into swiftlang:main Aug 4, 2025
5 checks passed
@eeckstein eeckstein deleted the non-loadable-global-inits branch August 4, 2025 06:44
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.

2 participants