Skip to content
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

Rock. Paper. blackHole(_:) #69

Closed
oscbyspro opened this issue Apr 5, 2023 · 9 comments
Closed

Rock. Paper. blackHole(_:) #69

oscbyspro opened this issue Apr 5, 2023 · 9 comments
Labels
brrr such code, much wow
Milestone

Comments

@oscbyspro
Copy link
Owner

oscbyspro commented Apr 5, 2023

I want to optimize some things, and need a way of telling the compiler to not erase my benchmarks. I mean, the current setup can identify some qualities, like whether the compiler can optimize it at all, but not much beyond that when it does. I have tried using the following function, but I saw no change in behavior.

@inline(never) func blackHole(_ value: some Any) { } // 😞
@oscbyspro oscbyspro added brrr such code, much wow help please send help :( labels Apr 5, 2023
@oscbyspro
Copy link
Owner Author

oscbyspro commented Apr 10, 2023

Got a bit excited about this, but it seldom differentiates UInt192 and UInt256. Sad.

@_optimize(none) @inline(never) @discardableResult func blackHole(_ x: @autoclosure () -> some Any) -> some Any { x() }

@oscbyspro
Copy link
Owner Author

oscbyspro commented Apr 11, 2023

That moment when you realize you have spent an hour trying various combinations of:

@inline(never)
@_optimize(none)
@discardableResult
@_semantics("optimize.no.crossmodule")
public func blackHole<T>(_ x: T) {  }
public func blackHole<T>(_ x: T) {  _ = x }
public func blackHole<T>(_ x: T) -> T { x }
public func blackHole<T>(_ x: @autoclosure () -> T) {  _ = x() }
public func blackHole<T>(_ x: @autoclosure () -> T) -> T { x() }

@oscbyspro
Copy link
Owner Author

oscbyspro commented Apr 11, 2023

With optimize(none) and/or @_semantics("optimize.no.crossmodule") I can get something that takes time proportional to the number of loops, and to some extent to how expensive the operation is. I'm struggling, however, with reliably getting something with a predictable (or any) difference between UInt192 and UInt256.

@oscbyspro
Copy link
Owner Author

oscbyspro commented Apr 11, 2023

/.../TestsUtils.swift uses this function:

@inline(never)
@_semantics("optimize.no.crossmodule")
public func blackHole<T>(_ x: T) { }

@oscbyspro
Copy link
Owner Author

oscbyspro commented Apr 11, 2023

Hm. So far, I've only tried using the blackHole(_:) method to obfuscate the loops, but when I have the time I might also try using the identity(_:) method to obfuscate all the constants and variables. I suppose that could be a problem.

@inline(never)
@_semantics("optimize.no.crossmodule")
@inlinable public func identity<T>(_ x: T) -> T { x }

oscbyspro added a commit that referenced this issue Apr 11, 2023
@oscbyspro
Copy link
Owner Author

oscbyspro commented Apr 11, 2023

Well, I remain skeptical. But at least they are no longer reduced to nothing.

@oscbyspro
Copy link
Owner Author

oscbyspro commented Apr 11, 2023

I wonder if I also need to trick the compiler into thinking that constants could change between loops. Maybe it knows enough to copy-paste some initial computation into the black hole? 💭

oscbyspro added a commit that referenced this issue Apr 11, 2023
@oscbyspro
Copy link
Owner Author

oscbyspro commented Apr 11, 2023

I inserted fake mutations because if I can do it, the compiler can do it (probably).

@oscbyspro
Copy link
Owner Author

oscbyspro commented Apr 12, 2023

I'm not sure how I would measure @inlinable constant expressions, but oh well.

oscbyspro added a commit that referenced this issue Apr 12, 2023
@oscbyspro oscbyspro added this to the v1.1.0 milestone Apr 14, 2023
@oscbyspro oscbyspro removed the help please send help :( label Apr 14, 2023
@oscbyspro oscbyspro changed the title blackHole(_:) Rock. Paper. blackHole(_:) Apr 18, 2023
@oscbyspro oscbyspro mentioned this issue May 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
brrr such code, much wow
Projects
None yet
Development

No branches or pull requests

1 participant