Skip to content

FloatingPoint.radix should be transparent. #25789

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 2 commits into from
Jun 26, 2019

Conversation

stephentyrone
Copy link
Contributor

Not sure why we missed this one, but there it is.

@stephentyrone stephentyrone requested a review from lorentey June 26, 2019 18:59
@stephentyrone
Copy link
Contributor Author

@swift-ci please smoke test

@stephentyrone stephentyrone changed the title FloatingPoint.radix should be inlinable. FloatingPoint.radix should be transparent. Jun 26, 2019
@jrose-apple
Copy link
Contributor

jrose-apple commented Jun 26, 2019

What's the reasoning for making this transparent? (cf. docs/TransparentAttr.rst)

@stephentyrone
Copy link
Contributor Author

stephentyrone commented Jun 26, 2019

What's the reasoning for making this transparent?

It is occasionally necessary to select between generic algorithms in code that should be compiling down to just a few instructions. Going through a witness table in those cases is catastrophic.

E.g. if we need to rescale a computation to avoid overflow, we want to divide by the radix (which is always exact for large values), not a fixed value (which would introduce an extra rounding for either radix-2 or radix-10 or both).

@inlinable would maybe be acceptable, but none of the downsides to @_transparent are an issue here, and it would be nicer if it is always inlined, even in -Onone, because it's always a known integer constant, so we can benefit from constant propagation and dead-code elimination.

@jrose-apple
Copy link
Contributor

FWIW, _transparent won't actually help you in a generic context, but okay.

@stephentyrone
Copy link
Contributor Author

stephentyrone commented Jun 26, 2019

FWIW, _transparent won't actually help you in a generic context, but okay.

It will still help when the compiler can specialize a generic function.

E.g.

internal func a<T: FloatingPoint>(_ x: T) -> T {
  if T.radix == 2 {
    // do something with a simpler algorithm
  }
  // do the more complex thing
}

func b(_ x: Float) -> Float {
  return a(x)/2
}

We should be able to specialize and inline a here such that we only end up emitting the code for radix == 2 while maintaining correct behavior of the utility function a for all cases.

@jrose-apple
Copy link
Contributor

Yes, but @inlinable (possibly with @inline(__always)) will do that too.

@stephentyrone
Copy link
Contributor Author

stephentyrone commented Jun 26, 2019

@inlinable is definitely not sufficient on its own. @inlinable + @inline(__always) may be.

@stephentyrone
Copy link
Contributor Author

@swift-ci please smoke test

@stephentyrone stephentyrone merged commit 166d0fa into swiftlang:master Jun 26, 2019
@stephentyrone stephentyrone deleted the inlinable-radix branch June 26, 2019 22:20
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.

3 participants