Skip to content
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

effects: power-up effects analysis for array operations #47154

Merged
merged 1 commit into from
Apr 2, 2023

Conversation

aviatesk
Copy link
Sponsor Member

@aviatesk aviatesk commented Oct 13, 2022

Mostly by making use of newly added :inaccessiblememonly effect property.
Now we can fold simple vector operations like:

julia> function simple_vec_ops(T, op!, op, xs...)
           a = T[]
           op!(a, xs...)
           return op(a)
       end;
simple_vec_ops (generic function with 1 method)

julia> for T = Any[Int,Any], op! = Any[push!,pushfirst!], op = Any[length,size],
           xs = Any[(Int,), (Int,Int,)]
           let effects = Base.infer_effects(simple_vec_ops, (Type{T},typeof(op!),typeof(op),xs...))
               @test Core.Compiler.is_foldable(effects)
           end
       end

julia> code_typed() do
           simple_vec_ops(Any, push!, length, Any,nothing,Core.Const(1))
       end
1-element Vector{Any}:
 CodeInfo(
1return 3
) => Int64

@aviatesk aviatesk added arrays [a, r, r, a, y, s] compiler:effects effect analysis labels Oct 13, 2022
base/array.jl Outdated Show resolved Hide resolved
@aviatesk aviatesk force-pushed the avi/array-effects branch 2 times, most recently from 85598c2 to 6be923c Compare October 13, 2022 14:09
@aviatesk
Copy link
Sponsor Member Author

@nanosoldier runbenchmarks(!"scalar", vs=":master")

@nanosoldier
Copy link
Collaborator

Your benchmark job has completed - possible performance regressions were detected. A full report can be found here.

base/array.jl Outdated Show resolved Hide resolved
base/array.jl Outdated Show resolved Hide resolved
base/array.jl Outdated Show resolved Hide resolved
base/array.jl Outdated Show resolved Hide resolved
base/tuple.jl Outdated Show resolved Hide resolved
Copy link
Sponsor Member

@vtjnash vtjnash left a comment

Choose a reason for hiding this comment

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

SGTM overall, with questions/comments

Mostly by making use of newly added `:inaccessiblememonly` effect
property.
Now we can fold simple vector operations like:
```julia
julia> function simple_vec_ops(T, op!, op, xs...)
           a = T[]
           op!(a, xs...)
           return op(a)
       end;
simple_vec_ops (generic function with 1 method)

julia> for T = Any[Int,Any], op! = Any[push!,pushfirst!], op = Any[length,size],
           xs = Any[(Int,), (Int,Int,)]
           let effects = Base.infer_effects(simple_vec_ops, (Type{T},typeof(op!),typeof(op),xs...))
               @test Core.Compiler.is_foldable(effects)
           end
       end

julia> code_typed() do
           simple_vec_ops(Any, push!, length, Any,nothing,Core.Const(1))
       end
1-element Vector{Any}:
 CodeInfo(
1 ─     return 3
) => Int64
```
Comment on lines +456 to +465
# safe version
function getindex(::Type{T}, vals::T...) where T
@inline
@_effect_free_terminates_locally_meta
a = Vector{T}(undef, length(vals))
@_safeindex for i in 1:length(vals)
a[i] = vals[i]
end
return a
end
Copy link
Member

Choose a reason for hiding this comment

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

@aviatesk
Is there a reason to add this? It seems unneeded and cause some new ambiguity JuliaArrays/StaticArrays.jl#1153 (comment)

Copy link
Sponsor Member Author

Choose a reason for hiding this comment

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

I added this definition since otherwise we can't const-fold safe getindex operations due to the :nothrow effect tainted by the foldr branch in the unsafe definition.

Copy link
Sponsor Member Author

Choose a reason for hiding this comment

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

But it looks like this definition is no longer needed (the vals isa NTuple branch is const-folded even without constant information propagated)? I will make a PR to delete this.

Copy link
Sponsor Member Author

Choose a reason for hiding this comment

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

aviatesk added a commit that referenced this pull request Apr 10, 2023
aviatesk added a commit that referenced this pull request Apr 10, 2023
aviatesk added a commit that referenced this pull request Apr 11, 2023
Xnartharax pushed a commit to Xnartharax/julia that referenced this pull request Apr 19, 2023
)

Mostly by making use of newly added `:inaccessiblememonly` effect
property.
Now we can fold simple vector operations like:
```julia
julia> function simple_vec_ops(T, op!, op, xs...)
           a = T[]
           op!(a, xs...)
           return op(a)
       end;
simple_vec_ops (generic function with 1 method)

julia> for T = Any[Int,Any], op! = Any[push!,pushfirst!], op = Any[length,size],
           xs = Any[(Int,), (Int,Int,)]
           let effects = Base.infer_effects(simple_vec_ops, (Type{T},typeof(op!),typeof(op),xs...))
               @test Core.Compiler.is_foldable(effects)
           end
       end

julia> code_typed() do
           simple_vec_ops(Any, push!, length, Any,nothing,Core.Const(1))
       end
1-element Vector{Any}:
 CodeInfo(
1 ─     return 3
) => Int64
```
Xnartharax pushed a commit to Xnartharax/julia that referenced this pull request Apr 19, 2023
aviatesk added a commit that referenced this pull request May 11, 2023
#47154 mistakenly added `@_safeindex` macro on the
`_append!(a::AbstractVector, ::Union{HasLength,HasShape}, iter)` method,
although `@_safeindex` is only valid for builtin vectors i.e. `Vector`.

This commit adds `isa` check so that `@_safeindex` is only applied to
builtin vectors. The `isa` check should be removed at compile time, so
it should not affect the runtime performance.

closes #49748
aviatesk added a commit that referenced this pull request May 12, 2023
#47154 mistakenly added `@_safeindex` macro on the
`_append!(a::AbstractVector, ::Union{HasLength,HasShape}, iter)` method,
although `@_safeindex` is only valid for builtin vectors i.e. `Vector`.

This commit adds `isa` check so that `@_safeindex` is only applied to
builtin vectors. The `isa` check should be removed at compile time, so
it should not affect the runtime performance.

closes #49748
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arrays [a, r, r, a, y, s] compiler:effects effect analysis
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants