Skip to content

Commit

Permalink
Merge pull request #129 from SciML/commonworld
Browse files Browse the repository at this point in the history
Add CommonWorldInvalidations world
  • Loading branch information
ChrisRackauckas authored Jun 29, 2024
2 parents cbd29c7 + 73b4033 commit ac25c8a
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 72 deletions.
6 changes: 5 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
name = "Static"
uuid = "aedffcd0-7271-4cad-89d0-dc628f76c6d3"
authors = ["chriselrod", "ChrisRackauckas", "Tokazama"]
version = "1.0.0"
version = "1.1.0"

[deps]
CommonWorldInvalidations = "f70d9fcc-98c5-4d4a-abd7-e4cdeebd8ca8"
IfElse = "615f187c-cbe4-4ef1-ba3b-2fcf58d6d173"
PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"

[compat]
Aqua = "0.8.4"
CommonWorldInvalidations = "1"
IfElse = "0.1"
PrecompileTools = "1"
Test = "1"
julia = "1.10"

Expand Down
24 changes: 12 additions & 12 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ using Static
using Documenter

makedocs(;
modules = [Static],
authors = "chriselrod, ChrisRackauckas, Tokazama",
repo = "https://github.com/SciML/Static.jl/blob/{commit}{path}#L{line}",
sitename = "Static.jl",
format = Documenter.HTML(;
prettyurls = get(ENV, "CI", "false") == "true",
canonical = "https://SciML.github.io/Static.jl",
assets = String[]),
pages = [
"Home" => "index.md",
])
modules = [Static],
authors = "chriselrod, ChrisRackauckas, Tokazama",
repo = "https://github.com/SciML/Static.jl/blob/{commit}{path}#L{line}",
sitename = "Static.jl",
format = Documenter.HTML(;
prettyurls = get(ENV, "CI", "false") == "true",
canonical = "https://SciML.github.io/Static.jl",
assets = String[]),
pages = [
"Home" => "index.md"
])

deploydocs(;
repo = "github.com/SciML/Static.jl")
repo = "github.com/SciML/Static.jl")
13 changes: 10 additions & 3 deletions src/Static.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import IfElse: ifelse
export StaticInt, StaticFloat64, StaticSymbol, True, False, StaticBool, NDIndex
export dynamic, is_static, known, static, static_promote

import PrecompileTools: @recompile_invalidations

@recompile_invalidations begin
import CommonWorldInvalidations
end

"""
StaticSymbol
Expand Down Expand Up @@ -369,7 +375,8 @@ end
Base.@propagate_inbounds function _promote_shape(a::Tuple{A}, ::Tuple{}) where {A}
(static_promote(static(1), getfield(a, 1)),)
end
Base.@propagate_inbounds function Base.promote_shape(a::Tuple{
Base.@propagate_inbounds function Base.promote_shape(
a::Tuple{
Vararg{Union{Int, StaticInt}},
},
b::Tuple{Vararg{Union{Int, StaticInt}}
Expand Down Expand Up @@ -955,8 +962,8 @@ end
@inline function Base.to_indices(A, inds,
I::Tuple{AbstractArray{NDIndex{N, J}}, Vararg{Any}}) where {
N,
J,
}
J
}
_, indstail = Base.IteratorsMD.split(inds, Val(N))
return (Base.to_index(A, I[1]), to_indices(A, indstail, Base.tail(I))...)
end
Expand Down
75 changes: 38 additions & 37 deletions src/ranges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ struct OptionallyStaticUnitRange{F <: IntType, L <: IntType} <:
stop::L

function OptionallyStaticUnitRange(start::IntType,
stop::IntType)
stop::IntType)
new{typeof(start), typeof(stop)}(start, stop)
end
function OptionallyStaticUnitRange(start, stop)
Expand Down Expand Up @@ -57,39 +57,39 @@ static(2):static(2):10
```
"""
struct OptionallyStaticStepRange{F <: IntType, S <: IntType,
L <: IntType} <: OrdinalRange{Int, Int}
L <: IntType} <: OrdinalRange{Int, Int}
start::F
step::S
stop::L

global function _OptionallyStaticStepRange(@nospecialize(start::IntType),
@nospecialize(step::IntType),
@nospecialize(stop::IntType))
@nospecialize(step::IntType),
@nospecialize(stop::IntType))
new{typeof(start), typeof(step), typeof(stop)}(start, step, stop)
end
end
@noinline function OptionallyStaticStepRange(@nospecialize(start::IntType),
::StaticInt{0},
@nospecialize(stop::IntType))
::StaticInt{0},
@nospecialize(stop::IntType))
throw(ArgumentError("step cannot be zero"))
end
# we don't need to check the `stop` if we know it acts like a unit range
function OptionallyStaticStepRange(@nospecialize(start::IntType),
step::StaticInt{1},
@nospecialize(stop::IntType))
step::StaticInt{1},
@nospecialize(stop::IntType))
_OptionallyStaticStepRange(start, step, stop)
end
function OptionallyStaticStepRange(@nospecialize(start::IntType),
@nospecialize(step::StaticInt),
@nospecialize(stop::IntType))
@nospecialize(step::StaticInt),
@nospecialize(stop::IntType))
_OptionallyStaticStepRange(start, step, _steprange_last(start, step, stop))
end
function OptionallyStaticStepRange(start, step, stop)
OptionallyStaticStepRange(IntType(start), IntType(step), IntType(stop))
end
function OptionallyStaticStepRange(@nospecialize(start::IntType),
step::Int,
@nospecialize(stop::IntType))
step::Int,
@nospecialize(stop::IntType))
if step === 0
throw(ArgumentError("step cannot be zero"))
else
Expand All @@ -99,16 +99,16 @@ end
OptionallyStaticStepRange(@nospecialize x::OptionallyStaticStepRange) = x
function OptionallyStaticStepRange(x::AbstractRange)
_OptionallyStaticStepRange(IntType(static_first(x)), IntType(static_step(x)),
IntType(static_last(x)))
IntType(static_last(x)))
end

# to make StepRange constructor inlineable, so optimizer can see `step` value
@inline function _steprange_last(start::StaticInt, step::StaticInt, stop::StaticInt)
StaticInt(_steprange_last(Int(start), Int(step), Int(stop)))
end
@inline function _steprange_last(start::Union{StaticInt, Int},
step::Union{StaticInt, Int},
stop::Union{StaticInt, Int})
step::Union{StaticInt, Int},
stop::Union{StaticInt, Int})
_steprange_last(Int(start), Int(step), Int(stop))
end
@inline function _steprange_last(start::Int, step::Int, stop::Int)
Expand Down Expand Up @@ -147,7 +147,7 @@ SOneTo(n::Int) = SOneTo{n}()
Base.oneto(::StaticInt{N}) where {N} = SOneTo{N}()

const OptionallyStaticRange{F, L} = Union{OptionallyStaticUnitRange{F, L},
OptionallyStaticStepRange{F, <:Any, L}}
OptionallyStaticStepRange{F, <:Any, L}}

# these probide a generic method for extracting potentially static values.
static_first(x::Base.OneTo) = StaticInt(1)
Expand Down Expand Up @@ -217,13 +217,13 @@ Base.isempty(r::OptionallyStaticUnitRange) = first(r) > last(r)
end

function Base.checkindex(::Type{Bool},
::SUnitRange{F1, L1},
::SUnitRange{F2, L2}) where {F1, L1, F2, L2}
::SUnitRange{F1, L1},
::SUnitRange{F2, L2}) where {F1, L1, F2, L2}
(F1::Int <= F2::Int) && (L1::Int >= L2::Int)
end

function Base.getindex(r::OptionallyStaticUnitRange,
s::AbstractUnitRange{<:Integer})
s::AbstractUnitRange{<:Integer})
@boundscheck checkbounds(r, s)
f = static_first(r)
fnew = f - one(f)
Expand Down Expand Up @@ -299,11 +299,12 @@ end
function Base.axes1(x::OptionallyStaticStepRange)
OptionallyStaticUnitRange(StaticInt(1), length(x))
end
function Base.axes1(x::OptionallyStaticStepRange{StaticInt{F}, StaticInt{S}, StaticInt{L}}) where {
F,
S,
L
}
function Base.axes1(x::OptionallyStaticStepRange{
StaticInt{F}, StaticInt{S}, StaticInt{L}}) where {
F,
S,
L
}
OptionallyStaticUnitRange(StaticInt(1), StaticInt(_range_length(F, S, L)))
end
Base.axes1(x::Base.Slice{<:OptionallyStaticUnitRange{One}}) = x.indices
Expand Down Expand Up @@ -347,25 +348,25 @@ function Base.Broadcast.axistype(_, r::OptionallyStaticUnitRange{StaticInt{1}})
Base.OneTo(last(r))
end
function Base.Broadcast.axistype(r::OptionallyStaticUnitRange{StaticInt{1}},
::OptionallyStaticUnitRange{StaticInt{1}})
::OptionallyStaticUnitRange{StaticInt{1}})
Base.OneTo(last(r))
end
function Base.similar(::Type{<:Array{T}},
axes::Tuple{OptionallyStaticUnitRange{StaticInt{1}},
Vararg{
Union{Base.OneTo,
OptionallyStaticUnitRange{StaticInt{1}}}}}) where {
T
}
axes::Tuple{OptionallyStaticUnitRange{StaticInt{1}},
Vararg{
Union{Base.OneTo,
OptionallyStaticUnitRange{StaticInt{1}}}}}) where {
T
}
Array{T}(undef, map(last, axes))
end
function Base.similar(::Type{<:Array{T}},
axes::Tuple{Base.OneTo, OptionallyStaticUnitRange{StaticInt{1}},
Vararg{
Union{Base.OneTo,
OptionallyStaticUnitRange{StaticInt{1}}}}}) where {
T
}
axes::Tuple{Base.OneTo, OptionallyStaticUnitRange{StaticInt{1}},
Vararg{
Union{Base.OneTo,
OptionallyStaticUnitRange{StaticInt{1}}}}}) where {
T
}
Array{T}(undef, map(last, axes))
end

Expand Down
15 changes: 9 additions & 6 deletions test/ranges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,14 @@
@test UnitRange{Int}(Static.OptionallyStaticUnitRange(static(1), static(10))) ===
UnitRange(1, 10)

@test AbstractUnitRange{Int}(Static.OptionallyStaticUnitRange(static(1), static(10))) isa
@test AbstractUnitRange{Int}(Static.OptionallyStaticUnitRange(
static(1), static(10))) isa
Static.OptionallyStaticUnitRange
@test AbstractUnitRange{UInt}(Static.OptionallyStaticUnitRange(static(1), static(10))) isa
@test AbstractUnitRange{UInt}(Static.OptionallyStaticUnitRange(
static(1), static(10))) isa
Base.OneTo
@test AbstractUnitRange{UInt}(Static.OptionallyStaticUnitRange(static(2), static(10))) isa
@test AbstractUnitRange{UInt}(Static.OptionallyStaticUnitRange(
static(2), static(10))) isa
UnitRange

@test @inferred((static(1):static(10))[static(2):static(3)]) === static(2):static(3)
Expand Down Expand Up @@ -98,11 +101,11 @@ CI = CartesianIndices((static(1):static(2), static(1):static(2)))

@test @inferred(length(Static.OptionallyStaticStepRange(static(1), 2, 10))) == 5
@test @inferred(length(Static.OptionallyStaticStepRange(static(1), static(1),
static(10)))) == 10
static(10)))) == 10
@test @inferred(length(Static.OptionallyStaticStepRange(static(2), static(1),
static(10)))) == 9
static(10)))) == 9
@test @inferred(length(Static.OptionallyStaticStepRange(static(2), static(2),
static(10)))) == 5
static(10)))) == 5
end

@test @inferred(getindex(static(1):10, Base.Slice(static(1):10))) === static(1):10
Expand Down
31 changes: 18 additions & 13 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -365,11 +365,14 @@ end
# inferred is Union{Int,Nothing}
@test Static.find_first_eq(1, map(Int, y)) === 3

@testset "reduce_tup" begin for n in 2:16
x = ntuple(_ -> rand(Bool) ? rand() : (rand(Bool) ? rand(0x00:0x1f) : rand(0:31)),
n)
@test @inferred(Static.reduce_tup(+, x)) reduce(+, x)
end end
@testset "reduce_tup" begin
for n in 2:16
x = ntuple(
_ -> rand(Bool) ? rand() : (rand(Bool) ? rand(0x00:0x1f) : rand(0:31)),
n)
@test @inferred(Static.reduce_tup(+, x)) reduce(+, x)
end
end
end

@testset "invperm" begin
Expand Down Expand Up @@ -527,14 +530,16 @@ y = 1:10

@test @inferred(static(2.0)^2.0) === 2.0^2.0

@testset "trig" begin for f in [sin, cos, tan, asin, atan, acos, sinh, cosh, tanh,
asinh, atanh, exp, exp2,
exp10, expm1, log, log2, log10, log1p, exponent, sqrt, cbrt, sec, csc, cot, sech,
secd, csch, cscd, cotd, cosd, tand, asind, acosd, atand, acotd, sech, coth, asech,
acsch, deg2rad, mod2pi, sinpi, cospi]
@info "Testing $f(0.5)"
@test @inferred(f(static(0.5))) === static(f(0.5))
end end
@testset "trig" begin
for f in [sin, cos, tan, asin, atan, acos, sinh, cosh, tanh,
asinh, atanh, exp, exp2,
exp10, expm1, log, log2, log10, log1p, exponent, sqrt, cbrt, sec, csc, cot, sech,
secd, csch, cscd, cotd, cosd, tand, asind, acosd, atand, acotd, sech, coth, asech,
acsch, deg2rad, mod2pi, sinpi, cospi]
@info "Testing $f(0.5)"
@test @inferred(f(static(0.5))) === static(f(0.5))
end
end
@test @inferred(asec(static(2.0))) === static(asec(2.0))
@test @inferred(acsc(static(2.0))) === static(acsc(2.0))
@test @inferred(acot(static(2.0))) === static(acot(2.0))
Expand Down

0 comments on commit ac25c8a

Please sign in to comment.