Skip to content

Reapply "Merge pull request #1725 from atrick/specialize" #1743

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 1 commit into from
Mar 22, 2016
Merged

Reapply "Merge pull request #1725 from atrick/specialize" #1743

merged 1 commit into from
Mar 22, 2016

Conversation

atrick
Copy link
Contributor

@atrick atrick commented Mar 19, 2016

Reapply "Merge pull request #1725 from atrick/specialize"

The revert appeared to have triggered a failure on the
incremental linux builder during IRGen:

While emitting associated type metadata '_NSCFDictionary' at Foundation/NSCFDictionary.swift:13:16
swift::NormalProtocolConformance::getTypeWitnessSubstAndDecl

@atrick
Copy link
Contributor Author

atrick commented Mar 19, 2016

@swift-ci Please smoke test Linux

@atrick atrick changed the title [WIP] Reapply "Merge pull request #1725 from atrick/specialize" Reapply "Merge pull request #1725 from atrick/specialize" Mar 21, 2016
This was mistakenly reverted in an attempt to fix buildbots.
Unfortunately it's now smashed into one commit.

---
Introduce @_specialize(<type list>) internal attribute.

This attribute can be attached to generic functions. The attribute's
arguments must be a list of concrete types to be substituted in the
function's generic signature. Any number of specializations may be
associated with a generic function.

This attribute provides a hint to the compiler. At -O, the compiler
will generate the specified specializations and emit calls to the
specialized code in the original generic function guarded by type
checks.

The current attribute is designed to be an internal tool for
performance experimentation. It does not affect the language or
API. This work may be extended in the future to add user-visible
attributes that do provide API guarantees and/or direct dispatch to
specialized code.

This attribute works on any generic function: a freestanding function
with generic type parameters, a nongeneric method declared in a
generic class, a generic method in a nongeneric class or a generic
method in a generic class. A function's generic signature is a
concatenation of the generic context and the function's own generic
type parameters.

e.g.

struct S<T> {
var x: T
@_specialize(Int, Float)
mutating func exchangeSecond<U>(u: U, _ t: T) -> (U, T) {
x = t
return (u, x)
}
}
// Substitutes: <T, U> with <Int, Float> producing:
// S<Int>::exchangeSecond<Float>(u: Float, t: Int) -> (Float, Int)

---
[SILOptimizer] Introduce an eager-specializer pass.

This pass finds generic functions with @_specialized attributes and
generates specialized code for the attribute's concrete types. It
inserts type checks and guarded dispatch at the beginning of the
generic function for each specialization. Since we don't currently
expose this attribute as API and don't specialize vtables and witness
tables yet, the only way to reach the specialized code is by calling
the generic function which performs the guarded dispatch.

In the future, we can build on this work in several ways:
- cross module dispatch directly to specialized code
- dynamic dispatch directly to specialized code
- automated specialization based on less specific hints
- partial specialization
- and so on...

I reorganized and refactored the optimizer's generic utilities to
support direct function specialization as opposed to apply
specialization.
@atrick
Copy link
Contributor Author

atrick commented Mar 21, 2016

@swift-ci Please test.

atrick added a commit that referenced this pull request Mar 22, 2016
Reapply "Merge pull request #1725 from atrick/specialize"
@atrick atrick merged commit acdf28f into swiftlang:master Mar 22, 2016
@atrick atrick deleted the reapply-specialize branch May 26, 2017 02:07
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.

1 participant