# Atomic with @\_noLocks can cause metadata allocation or locks

**URL:** <https://forums.swift.org/t/atomic-with-nolocks-can-cause-metadata-allocation-or-locks/69956>\
**Category:** Using Swift\
**Tags:** swift-atomics\
**Created:** [February 10, 2024, 1:45pm UTC](https://forums.swift.org/t/atomic-with-nolocks-can-cause-metadata-allocation-or-locks/69956 "2024-02-10T13:45:08Z")\
**Posts on this page:** 7\
**Page:** 2

<div class="post-metadata">

**Author:** ![jcavar](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jcavar/32/28246_2.png) [@jcavar](https://forums.swift.org/u/jcavar)\
**Post date:** [February 15, 2024, 9:17pm UTC](https://forums.swift.org/t/atomic-with-nolocks-can-cause-metadata-allocation-or-locks/69956/21 "2024-02-15T21:17:14Z")

</div>

Thank you! But I think I am still a bit confused what should we take out from this.

It seems to me that relying on optimiser when using `Atomic`s is not guaranteed predictable performance which was one of the main goals of ownership features?

I don't know if this was discussed elsewhere, but is the goal of `Atomic`s to be free of metadata access in a guaranteed and predictable way?

---

<div class="post-metadata">

**Author:** ![Alejandro](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/alejandro/32/46197_2.png) [@Alejandro](https://forums.swift.org/u/Alejandro)\
**Post date:** [February 15, 2024, 9:47pm UTC](https://forums.swift.org/t/atomic-with-nolocks-can-cause-metadata-allocation-or-locks/69956/22 "2024-02-15T21:47:11Z")

</div>

The only thing preventing `Atomic` from being used in `@_noLocks` or `@_noAllocation` is the deinit devirtualizer. Once that's turned on, you should be able to use atomics in those contexts without any problem.

---

<div class="post-metadata">

**Author:** ![jcavar](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jcavar/32/28246_2.png) [@jcavar](https://forums.swift.org/u/jcavar)\
**Post date:** [February 16, 2024, 12:51pm UTC](https://forums.swift.org/t/atomic-with-nolocks-can-cause-metadata-allocation-or-locks/69956/23 "2024-02-16T12:51:40Z")

</div>

Thank you @Alejandro! I think I am starting to understand, but few things are still not clear to me.

My current understanding is that deinit devirtualizer is [guaranteed optimisation pass](https://github.com/apple/swift/blob/main/docs/SIL.rst#L46-L49) that is happening before performance diagnostic pass. Therefore, diagnostics are performed after problematic code is transformed. Is this right?

If yes, I have few questions:

- In your godbolt snippet you used -O. Without -O, there are still metadata accesses. My understanding is that this should not be the case for mandatory optimisations?
- I don't seem to be able to get `@_noLocks` to compile even with -O with `-Xllvm -enable-deinit-devirtualizer`.
- Is there a plan to enable deinit devirtulizer by default?

---

<div class="post-metadata">

**Author:** ![Alejandro](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/alejandro/32/46197_2.png) [@Alejandro](https://forums.swift.org/u/Alejandro)\
**Post date:** [February 20, 2024, 7:56pm UTC](https://forums.swift.org/t/atomic-with-nolocks-can-cause-metadata-allocation-or-locks/69956/24 "2024-02-20T19:56:06Z")

</div>

cc: @Erik_Eckstein

---

<div class="post-metadata">

**Author:** ![jcavar](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jcavar/32/28246_2.png) [@jcavar](https://forums.swift.org/u/jcavar)\
**Post date:** [May 28, 2024, 8:35pm UTC](https://forums.swift.org/t/atomic-with-nolocks-can-cause-metadata-allocation-or-locks/69956/25 "2024-05-28T20:35:29Z")

</div>

A part of the issue is fixed [here](https://github.com/apple/swift/issues/73582).

The other part seems to be related to `@_rawLayout` and is filed as a bug [here](https://github.com/apple/swift/issues/73951).

---

<div class="post-metadata">

**Author:** ![jcavar](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jcavar/32/28246_2.png) [@jcavar](https://forums.swift.org/u/jcavar)\
**Post date:** [June 13, 2024, 9:05pm UTC](https://forums.swift.org/t/atomic-with-nolocks-can-cause-metadata-allocation-or-locks/69956/26 "2024-06-13T21:05:01Z")

</div>

The other part of the issue related to `rawLayout` is also fixed.

There is another problem now filed as a bug [here](https://github.com/apple/swift/issues/74407).

---

<div class="post-metadata">

**Author:** ![jcavar](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jcavar/32/28246_2.png) [@jcavar](https://forums.swift.org/u/jcavar)\
**Post date:** [September 25, 2024, 7:49am UTC](https://forums.swift.org/t/atomic-with-nolocks-can-cause-metadata-allocation-or-locks/69956/27 "2024-09-25T07:49:33Z")

</div>

Fixed [here](https://github.com/swiftlang/swift/pull/75615).

Thanks everyone!

[Previous page](https://forums.swift.org/t/atomic-with-nolocks-can-cause-metadata-allocation-or-locks/69956.md?page=1)
