swift-atomics icon indicating copy to clipboard operation
swift-atomics copied to clipboard

Add support for `@_noLocks` and `@_noAllocation` Swift attributes

Open tcldr opened this issue 2 years ago • 4 comments

Description

For real time programming the @_noLocks and @_noAllocation Swift attributes provide programmers with a convenient way of ensuring the safety of their code in a real time environment. Currently, when using these attributes with Swift Atomics, the following error message is generated.

Called function is not available in this module and can have unpredictable performance

Steps to Reproduce

import Atomics

public struct WrappedInt {
  
  private let wrappedInt = UnsafeAtomic<Int>.create(0)
  
  @_noLocks
  @_noAllocation
  public func value() -> Int {
    // ERROR: Called function is not available in this module and can have unpredictable performance
    wrappedInt.load(ordering: .relaxed)
  }
}

Expected behavior

The @_noLocks and @_noAllocation attributes generate no errors indicating code is free from locks and allocations and may be suitable for execution within a real time environment.

Actual behavior

The @_noLocks and @_noAllocation attributes generate errors indicating the code may contain locks or allocations and may be unsafe for execution within a real time environment.

Environment

Swift 5.8 Dev Snapshot 2022-12-29

tcldr avatar Jan 05 '23 17:01 tcldr

It looks like these attributes have been present since 5.6, so we can safely add them once #67 lands.

lorentey avatar Mar 17 '23 21:03 lorentey

Great, thanks, @lorentey !

tcldr avatar Mar 17 '23 21:03 tcldr

Ah, @_noLocks and @_noAllocation require the -experimental-performance-annotations compiler flag. Unfortunately the package manager considers such flags "unsafe", so this package cannot use them. 😞

lorentey avatar Mar 17 '23 23:03 lorentey

Of course - yes, forgot about that. Fingers-crossed they’ll lose their underscores soon. Will be a powerful tool for real-time stuff. Thanks for investigating.

tcldr avatar Mar 18 '23 07:03 tcldr