Skip to content

Use builtins instead of shims in FloatingPointTypes. #3454

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 9 commits into from
Jul 14, 2016
Merged

Use builtins instead of shims in FloatingPointTypes. #3454

merged 9 commits into from
Jul 14, 2016

Conversation

stephentyrone
Copy link
Contributor

What's in this pull request?

Replace shims for ceil, floor, round, etc. with Builtins to eliminate a level of indirection.

Resolved bug number: (SR-2045)


Before merging this pull request to apple/swift repository:

  • Test pull request on Swift continuous integration.

Triggering Swift CI

The swift-ci is triggered by writing a comment on this PR addressed to the GitHub user @swift-ci. Different tests will run depending on the specific comment that you use. The currently available comments are:

Smoke Testing

Platform Comment
All supported platforms @swift-ci Please smoke test
All supported platforms @swift-ci Please smoke test and merge
OS X platform @swift-ci Please smoke test OS X platform
Linux platform @swift-ci Please smoke test Linux platform

Validation Testing

Platform Comment
All supported platforms @swift-ci Please test
All supported platforms @swift-ci Please test and merge
OS X platform @swift-ci Please test OS X platform
OS X platform @swift-ci Please benchmark
Linux platform @swift-ci Please test Linux platform

Lint Testing

Language Comment
Python @swift-ci Please Python lint

Note: Only members of the Apple organization can trigger swift-ci.

@stephentyrone stephentyrone changed the title Map .round onto builtins instead of shims for Float and Double. Map .round onto builtins instead of shims. Jul 11, 2016
@jckarter
Copy link
Contributor

Don't the LLVM intrinsics have different semantics from the C functions in some cases? Are there differences we care about? (Not setting errno is for the better, at least.)

@stephentyrone
Copy link
Contributor Author

@jckarter So they're all defined to do the right thing except for llvm.sqrt, which is annoyingly undef for non-zero negative inputs. Given that, how should we go about getting the instruction we actually want instead of a call for squareRoot?

@jckarter
Copy link
Contributor

Dunno, is the backend smart enough to do the right thing if you write the test-and-br-or-select combo?

@jckarter
Copy link
Contributor

jckarter commented Jul 12, 2016

Doesn't appear to do so for something like this, alas:

declare double @llvm.sqrt.f64(double %x)

define double @sqrtOrNaN(double %x) {
entry:
  %z = fcmp ult double %x, 0.0
  %b = call double @llvm.sqrt.f64(double %x)
  %c = select i1 %z, double 0x7fff000000000000, double %b
  ret double %c
}

But maybe we could ask the backend to recognize that sort of pattern.

@stephentyrone
Copy link
Contributor Author

@jckarter Yeah, I would expect it not to. The backend knows how to lower an actual sqrt[f] call correctly, but (I don't think) we can use that in the standard lib, right?

@jckarter
Copy link
Contributor

@stephentyrone Not without dirty tricks.

@gribozavr Would it be possible for the stub implementation to be static inline and call through to the real sqrt in the stubs header, something like this?

// Forward-declare sqrt to avoid including the real Darwin module.
double sqrt(double);

// Stub out a call to it.
static inline double _swift_stdlib_sqrt(double x) { return sqrt(x); }

@jckarter
Copy link
Contributor

…though sqrt still couldn't be reduced to a machine instruction on platforms where the libc function sets errno or calls _matherr or has other nonstandard side effects (as in MSVC for instance), could it?

@gribozavr
Copy link
Contributor

I'd rather not forward-declare sqrt(), since it would be imported by Clang importer into the global namespace.

@stephentyrone
Copy link
Contributor Author

stephentyrone commented Jul 12, 2016

@jckarter @gribozavr Worked around using __builtin_sqrt. I believe this solves all the problems.

@jckarter
Copy link
Contributor

SGTM.

@stephentyrone stephentyrone changed the title Map .round onto builtins instead of shims. Use builtins instead of shims in FloatingPointTypes. Jul 12, 2016
@stephentyrone
Copy link
Contributor Author

@swift-ci please test Linux.

@stephentyrone
Copy link
Contributor Author

stephentyrone commented Jul 12, 2016

@jckarter @gribozavr @rjmccall Do you know what attributes we need on these static inlines to placate the linux build gods?

@gribozavr
Copy link
Contributor

@stephentyrone This looks like a compiler bug to me.

@@ -84,35 +84,35 @@ __swift_uint32_t
_swift_stdlib_cxx11_mt19937_uniform(__swift_uint32_t upper_bound);

// Math library functions
SWIFT_RUNTIME_STDLIB_INLINE
SWIFT_RUNTIME_STDLIB_INTERFACE
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think you should need any attributes at all on these functions.

@jckarter
Copy link
Contributor

Agree with @gribozavr, looks like SILGen's neglecting to emit something.

@stephentyrone
Copy link
Contributor Author

Ok, I'll add a workaround for non-macOS once I have my current world-is-on-fire P1 dealt with.

@stephentyrone
Copy link
Contributor Author

@swift-ci please test linux.

@stephentyrone stephentyrone merged commit 999885f into swiftlang:master Jul 14, 2016
atrick added a commit that referenced this pull request Jul 15, 2016
This reverts commit 999885f.

This breaks the stdlib serialization tests:

Assertion failed: (!hasSharedVisibility(F->getLinkage()) && "external declaration of SILFunction with shared visibility is not " "allowed"), function visitSILFunction, file /s/sptr/swift/lib/SIL/SILVerifier.cpp, line 3267.
@atrick
Copy link
Contributor

atrick commented Jul 15, 2016

Please see https://ci.swift.org/job/oss-swift_tools-RA_stdlib-RDA_long-test/507/

Assertion failed: (!hasSharedVisibility(F->getLinkage()) && "external declaration of SILFunction with shared visibility is not " "allowed"), function visitSILFunction, file /s/sptr/swift/lib/SIL/SILVerifier.cpp, line 3267.

While verifying SIL function @_swift_stdlib_sqrtf

Sorry, I don't know enough about the shims to fix it on the spot. But apparently SIL verifier cannot handle these static inline functions ??

@stephentyrone
Copy link
Contributor Author

stephentyrone commented Jul 15, 2016

@atrick Why didn't we see this failure in the pre-submission test?

@stephentyrone
Copy link
Contributor Author

stephentyrone commented Jul 15, 2016

Rather than reverting this, a much better fix would be to remove the #if defined __APPLE__ versions of the shims so we don't lose all the other performance wins.

@gribozavr
Copy link
Contributor

Pre-submission test does not build the library in all modes, probably Release+DebugInfo+Assertions trips a bug in the compiler.

@gribozavr
Copy link
Contributor

Rather than reverting this, a much better fix would be to remove the #if defined __APPLE__ versions of the shims so we don't lose all the other performance wins.

My concern is that if the compiler choked on one, then it is likely that others will also break in a similar way. If not in our tests, then in users' code.

@stephentyrone
Copy link
Contributor Author

stephentyrone commented Jul 15, 2016

@gribozavr I don't understand your comment. There are no "others" for it to choke on. Those are the only static inlines.

@gribozavr
Copy link
Contributor

I meant that the compiler didn't like _swift_stdlib_sqrtf. If we switch that one to use an out-of-line function (that's how I understood your suggestion), I think other functions (e.g., _swift_stdlib_remainder) are likely to trigger similar failures.

@atrick
Copy link
Contributor

atrick commented Jul 15, 2016

I don't think the presubmission tests run the "check-swift-long" does it? I was actually hitting this locally and it was blocking my work yesterday because I specifically needed to verify stdlib parsing.

@stephentyrone
Copy link
Contributor Author

My suggestion is that we switch all six inlines to out-of-lines. That's what removing the #if defined __APPLE__ parts would do, without introducing large perf regressions for fma, floor, ceil, and trunc, which are some of the more useful math operations.

@gribozavr
Copy link
Contributor

@stephentyrone Sorry, I misunderstood you then!

@stephentyrone stephentyrone mentioned this pull request Jul 15, 2016
1 task
@stephentyrone
Copy link
Contributor Author

Reworked as #3530 without the static inlines.

@stephentyrone stephentyrone deleted the slimmed-down-round branch July 15, 2016 17:04
@atrick
Copy link
Contributor

atrick commented Jul 15, 2016

@stephentyrone Can you file a bug on the static inline problem referencing this PR?

@stephentyrone
Copy link
Contributor Author

stephentyrone commented Jul 15, 2016

@atrick https://bugs.swift.org/browse/SR-2089. A bit vague since I don't actually understand the source of the error =)

kateinoigakukun pushed a commit to kateinoigakukun/swift that referenced this pull request Sep 5, 2021
Resolve conflicts with the `main` branch
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.

4 participants