-
-
Couldn't load subscription status.
- Fork 17
Limit needless code gen with nospecialize #13
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
|
|
||
| """ | ||
| StaticBool(x::Bool) -> True/False | ||
|
|
||
| A statically typed `Bool`. | ||
| """ | ||
| abstract type StaticBool{bool} <: Integer end | ||
|
|
||
| struct True <: StaticBool{true} end | ||
|
|
||
| struct False <: StaticBool{false} end | ||
|
|
||
| StaticBool{true}() = True() | ||
| StaticBool{false}() = False() | ||
| StaticBool(x::StaticBool) = x | ||
| function StaticBool(x::Bool) | ||
| if x | ||
| return True() | ||
| else | ||
| return False() | ||
| end | ||
| end | ||
|
|
||
| StaticInt(x::False) = Zero() | ||
| StaticInt(x::True) = One() | ||
| Base.Bool(::True) = true | ||
| Base.Bool(::False) = false | ||
|
|
||
| Base.:(~)(::True) = False() | ||
| Base.:(~)(::False) = True() | ||
| Base.:(!)(::True) = False() | ||
| Base.:(!)(::False) = True() | ||
|
|
||
| Base.:(==)(::True, ::True) = True() | ||
| Base.:(==)(::True, ::False) = False() | ||
| Base.:(==)(::False, ::True) = False() | ||
| Base.:(==)(::False, ::False) = True() | ||
|
|
||
| Base.:(|)(x::StaticBool, y::StaticBool) = _or(x, y) | ||
| _or(::True, ::False) = True() | ||
| _or(::False, ::True) = True() | ||
| _or(::True, ::True) = True() | ||
| _or(::False, ::False) = False() | ||
| Base.:(|)(x::Bool, y::True) = y | ||
| Base.:(|)(x::Bool, y::False) = x | ||
| Base.:(|)(x::True, y::Bool) = x | ||
| Base.:(|)(x::False, y::Bool) = y | ||
|
|
||
| Base.:(&)(x::StaticBool, y::StaticBool) = _and(x, y) | ||
| _and(::True, ::False) = False() | ||
| _and(::False, ::True) = False() | ||
| _and(::True, ::True) = True() | ||
| _and(::False, ::False) = False() | ||
| Base.:(&)(x::Bool, y::True) = x | ||
| Base.:(&)(x::Bool, y::False) = y | ||
| Base.:(&)(x::True, y::Bool) = y | ||
| Base.:(&)(x::False, y::Bool) = x | ||
|
|
||
| Base.xor(y::StaticBool, x::StaticBool) = _xor(x, y) | ||
| _xor(::True, ::True) = False() | ||
| _xor(::True, ::False) = True() | ||
| _xor(::False, ::True) = True() | ||
| _xor(::False, ::False) = False() | ||
| Base.xor(x::Bool, y::StaticBool) = xor(x, Bool(y)) | ||
| Base.xor(x::StaticBool, y::Bool) = xor(Bool(x), y) | ||
|
|
||
| Base.sign(x::StaticBool) = x | ||
| Base.abs(x::StaticBool) = x | ||
| Base.abs2(x::StaticBool) = x | ||
| Base.iszero(::True) = False() | ||
| Base.iszero(::False) = True() | ||
| Base.isone(::True) = True() | ||
| Base.isone(::False) = False() | ||
|
|
||
| Base.:(<)(x::StaticBool, y::StaticBool) = _lt(x, y) | ||
| _lt(::False, ::True) = True() | ||
| _lt(::True, ::True) = False() | ||
| _lt(::False, ::False) = False() | ||
| _lt(::True, ::False) = False() | ||
|
|
||
| Base.:(<=)(x::StaticBool, y::StaticBool) = _lteq(x, y) | ||
| _lteq(::False, ::True) = True() | ||
| _lteq(::True, ::True) = True() | ||
| _lteq(::False, ::False) = True() | ||
| _lteq(::True, ::False) = False() | ||
|
|
||
| Base.:(+)(x::True) = One() | ||
| Base.:(+)(x::False) = Zero() | ||
| Base.:(-)(x::True) = -One() | ||
| Base.:(-)(x::False) = Zero() | ||
|
|
||
| Base.:(+)(x::StaticBool, y::StaticBool) = StaticInt(x) + StaticInt(y) | ||
| Base.:(-)(x::StaticBool, y::StaticBool) = StaticInt(x) - StaticInt(y) | ||
| Base.:(*)(x::StaticBool, y::StaticBool) = x & y | ||
|
|
||
| # from `^(x::Bool, y::Bool) = x | !y` | ||
| Base.:(^)(x::StaticBool, y::False) = True() | ||
| Base.:(^)(x::StaticBool, y::True) = x | ||
| Base.:(^)(x::Integer, y::False) = one(x) | ||
| Base.:(^)(x::Integer, y::True) = x | ||
| Base.:(^)(x::BigInt, y::False) = one(x) | ||
| Base.:(^)(x::BigInt, y::True) = x | ||
|
|
||
| Base.div(x::StaticBool, y::False) = throw(DivideError()) | ||
| Base.div(x::StaticBool, y::True) = x | ||
|
|
||
| Base.rem(x::StaticBool, y::False) = throw(DivideError()) | ||
| Base.rem(x::StaticBool, y::True) = False() | ||
| Base.mod(x::StaticBool, y::StaticBool) = rem(x, y) | ||
|
|
||
| Base.promote_rule(::Type{<:StaticBool}, ::Type{<:StaticBool}) = StaticBool | ||
| Base.promote_rule(::Type{<:StaticBool}, ::Type{Bool}) = Bool | ||
| Base.promote_rule(::Type{Bool}, ::Type{<:StaticBool}) = Bool | ||
|
|
||
| @generated _get_tuple(::Type{T}, ::StaticInt{i}) where {T<:Tuple, i} = T.parameters[i] | ||
|
|
||
| Base.all(::Tuple{Vararg{True}}) = true | ||
| Base.all(::Tuple{Vararg{Union{True,False}}}) = false | ||
| Base.all(::Tuple{Vararg{False}}) = false | ||
|
|
||
| Base.any(::Tuple{Vararg{True}}) = true | ||
| Base.any(::Tuple{Vararg{Union{True,False}}}) = true | ||
| Base.any(::Tuple{Vararg{False}}) = false | ||
|
|
||
| ifelse(::True, x, y) = x | ||
|
|
||
| ifelse(::False, x, y) = y | ||
|
|
||
| Base.show(io::IO, ::StaticBool{bool}) where {bool} = print(io, "static($bool)") | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
|
|
||
| """ | ||
| StaticSymbol | ||
|
|
||
| A statically typed `Symbol`. | ||
| """ | ||
| struct StaticSymbol{s} | ||
| StaticSymbol{s}() where {s} = new{s::Symbol}() | ||
| StaticSymbol(s::Symbol) = new{s}() | ||
| StaticSymbol(x::StaticSymbol) = x | ||
| StaticSymbol(x) = StaticSymbol(Symbol(x)) | ||
| end | ||
| StaticSymbol(x, y) = StaticSymbol(Symbol(x, y)) | ||
| StaticSymbol(x::StaticSymbol, y::StaticSymbol) = _cat_syms(x, y) | ||
| @generated function _cat_syms(::StaticSymbol{x}, ::StaticSymbol{y}) where {x,y} | ||
| return :(StaticSymbol{$(QuoteNode(Symbol(x, y)))}()) | ||
| end | ||
| StaticSymbol(x, y, z...) = StaticSymbol(StaticSymbol(x, y), z...) | ||
|
|
||
| Base.Symbol(::StaticSymbol{s}) where {s} = s::Symbol | ||
|
|
||
| Base.show(io::IO, ::StaticSymbol{s}) where {s} = print(io, "static(:$s)") | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@timholy , If I want to avoid generating a unique method for every variant of
StaticIntis this the right approach or will it kill inference somewhere down the line? I usedMethodAnalysis.methodinstancesand it seems to avoid make extra methods but I'm never sure with this sort of stuffThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Presumably everything still infers and optimizes fine, because the behavior doesn't actually depend on the particular types?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's the goal. I was hoping to start strategically adding these to trait functions so that we can avoid some of the pitfalls of StaticArrays.