From 32fccde381726452e66434c94b21b06794f528b3 Mon Sep 17 00:00:00 2001 From: Chad Scherrer Date: Wed, 11 Aug 2021 16:44:45 -0700 Subject: [PATCH 1/5] Fix some Base.@propagate_inbounds annotations --- src/FillArrays.jl | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/FillArrays.jl b/src/FillArrays.jl index 9e6b0a7d..42006262 100644 --- a/src/FillArrays.jl +++ b/src/FillArrays.jl @@ -33,21 +33,21 @@ abstract type AbstractFill{T, N, Axes} <: AbstractArray{T, N} end ==(a::AbstractFill, b::AbstractFill) = axes(a) == axes(b) && getindex_value(a) == getindex_value(b) -Base.@propagate_inbounds @inline function _fill_getindex(F::AbstractFill, kj::Integer...) +Base.@propagate_inbounds function _fill_getindex(F::AbstractFill, kj::Integer...) @boundscheck checkbounds(F, kj...) getindex_value(F) end -getindex(F::AbstractFill, k::Integer) = _fill_getindex(F, k) -getindex(F::AbstractFill{T, N}, kj::Vararg{<:Integer, N}) where {T, N} = _fill_getindex(F, kj...) +Base.@propagate_inbounds getindex(F::AbstractFill, k::Integer) = _fill_getindex(F, k) +Base.@propagate_inbounds getindex(F::AbstractFill{T, N}, kj::Vararg{<:Integer, N}) where {T, N} = _fill_getindex(F, kj...) -@inline function setindex!(F::AbstractFill, v, k::Integer) +Base.@propagate_inbounds function setindex!(F::AbstractFill, v, k::Integer) @boundscheck checkbounds(F, k) v == getindex_value(F) || throw(ArgumentError("Cannot setindex! to $v for an AbstractFill with value $(getindex_value(F)).")) F end -@inline function setindex!(F::AbstractFill{T, N}, v, kj::Vararg{<:Integer, N}) where {T, N} +Base.@propagate_inbounds function setindex!(F::AbstractFill{T, N}, v, kj::Vararg{<:Integer, N}) where {T, N} @boundscheck checkbounds(F, kj...) v == getindex_value(F) || throw(ArgumentError("Cannot setindex! to $v for an AbstractFill with value $(getindex_value(F)).")) F @@ -163,26 +163,26 @@ convert(::Type{T}, F::T) where T<:Fill = F -getindex(F::Fill{<:Any,0}) = getindex_value(F) +Base.@propagate_inbounds getindex(F::Fill{<:Any,0}) = getindex_value(F) -Base.@propagate_inbounds @inline function _fill_getindex(A::AbstractFill, I::Vararg{Union{Real, AbstractArray}, N}) where N +Base.@propagate_inbounds function _fill_getindex(A::AbstractFill, I::Vararg{Union{Real, AbstractArray}, N}) where N @boundscheck checkbounds(A, I...) shape = Base.index_shape(I...) fillsimilar(A, shape) end -Base.@propagate_inbounds @inline function _fill_getindex(A::AbstractFill, kr::AbstractArray{Bool}) +Base.@propagate_inbounds function _fill_getindex(A::AbstractFill, kr::AbstractArray{Bool}) @boundscheck checkbounds(A, kr) fillsimilar(A, count(kr)) end -Base.@propagate_inbounds @inline Base._unsafe_getindex(::IndexStyle, F::AbstractFill, I::Vararg{Union{Real, AbstractArray}, N}) where N = +Base._unsafe_getindex(::IndexStyle, F::AbstractFill, I::Vararg{Union{Real, AbstractArray}, N}) where N = @inbounds(return _fill_getindex(F, I...)) -getindex(A::AbstractFill, kr::AbstractVector{Bool}) = _fill_getindex(A, kr) -getindex(A::AbstractFill, kr::AbstractArray{Bool}) = _fill_getindex(A, kr) +Base.@propagate_inbounds getindex(A::AbstractFill, kr::AbstractVector{Bool}) = _fill_getindex(A, kr) +Base.@propagate_inbounds getindex(A::AbstractFill, kr::AbstractArray{Bool}) = _fill_getindex(A, kr) sort(a::AbstractFill; kwds...) = a sort!(a::AbstractFill; kwds...) = a @@ -329,7 +329,7 @@ axes(T::AbstractTriangular{<:Any,<:AbstractFill}) = axes(parent(T)) axes(rd::RectDiagonal) = rd.axes size(rd::RectDiagonal) = length.(rd.axes) -@inline function getindex(rd::RectDiagonal{T}, i::Integer, j::Integer) where T +Base.@propagate_inbounds function getindex(rd::RectDiagonal{T}, i::Integer, j::Integer) where T @boundscheck checkbounds(rd, i, j) if i == j @inbounds r = rd.diag[i] @@ -339,7 +339,7 @@ size(rd::RectDiagonal) = length.(rd.axes) return r end -function setindex!(rd::RectDiagonal, v, i::Integer, j::Integer) +Base.@propagate_inbounds function setindex!(rd::RectDiagonal, v, i::Integer, j::Integer) @boundscheck checkbounds(rd, i, j) if i == j @inbounds rd.diag[i] = v From 7f693bf192f1feaecf2cb45b2d1bcf537f01c34d Mon Sep 17 00:00:00 2001 From: Chad Scherrer Date: Thu, 12 Aug 2021 07:57:31 -0700 Subject: [PATCH 2/5] add tests --- Project.toml | 3 ++- test/runtests.jl | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 16e8e213..db3b1389 100644 --- a/Project.toml +++ b/Project.toml @@ -13,8 +13,9 @@ julia = "1" [extras] Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" +InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240" StaticArrays = "90137ffa-7385-5640-81b9-e52037218182" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" [targets] -test = ["Test", "Base64", "StaticArrays"] +test = ["Test", "Base64", "InteractiveUtils", "StaticArrays"] diff --git a/test/runtests.jl b/test/runtests.jl index a688ef54..0864ced6 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,5 +1,6 @@ using FillArrays, LinearAlgebra, SparseArrays, StaticArrays, Random, Base64, Test, Statistics import FillArrays: AbstractFill, RectDiagonal, SquareEye +using InteractiveUtils: code_llvm @testset "fill array constructors and convert" begin for (Typ, funcs) in ((:Zeros, :zeros), (:Ones, :ones)) @@ -1323,3 +1324,17 @@ end @test cor(Fill(3, 4, 5)) ≈ cor(fill(3, 4, 5)) nans=true @test cor(Fill(3, 4, 5), dims=2) ≈ cor(fill(3, 4, 5), dims=2) nans=true end + +@testset "Inbounds optimization" begin + function llvm_lines(a) + f(x,j) = @inbounds x[j] + io = IOBuffer() + code_llvm(io, f, (typeof(a), Int); debuginfo=:none) + # countlines(io) -- doesn't work for some reason + count(==('\n'), String(take!(io))) + end + + @test llvm_lines(Zeros(10)) < 10 + @test llvm_lines(Ones(10)) < 10 + @test llvm_lines(Fill(2.0,10)) < 10 +end From 0df91c31c0063d6dbcd7391d1c8ae6148ddc40b4 Mon Sep 17 00:00:00 2001 From: Chad Scherrer Date: Thu, 12 Aug 2021 09:49:39 -0700 Subject: [PATCH 3/5] comment out inbounds tests (maybe it can work later) --- Project.toml | 3 +-- test/runtests.jl | 27 +++++++++++++++------------ 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/Project.toml b/Project.toml index db3b1389..16e8e213 100644 --- a/Project.toml +++ b/Project.toml @@ -13,9 +13,8 @@ julia = "1" [extras] Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" -InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240" StaticArrays = "90137ffa-7385-5640-81b9-e52037218182" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" [targets] -test = ["Test", "Base64", "InteractiveUtils", "StaticArrays"] +test = ["Test", "Base64", "StaticArrays"] diff --git a/test/runtests.jl b/test/runtests.jl index 0864ced6..b0129055 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,6 +1,7 @@ using FillArrays, LinearAlgebra, SparseArrays, StaticArrays, Random, Base64, Test, Statistics import FillArrays: AbstractFill, RectDiagonal, SquareEye using InteractiveUtils: code_llvm +using PerformanceTestTools @testset "fill array constructors and convert" begin for (Typ, funcs) in ((:Zeros, :zeros), (:Ones, :ones)) @@ -1325,16 +1326,18 @@ end @test cor(Fill(3, 4, 5), dims=2) ≈ cor(fill(3, 4, 5), dims=2) nans=true end -@testset "Inbounds optimization" begin - function llvm_lines(a) - f(x,j) = @inbounds x[j] - io = IOBuffer() - code_llvm(io, f, (typeof(a), Int); debuginfo=:none) - # countlines(io) -- doesn't work for some reason - count(==('\n'), String(take!(io))) - end + +# # Works at REPL, but broken in CI +# @testset "Inbounds optimization" begin +# function llvm_lines(a) +# f(x,j) = @inbounds x[j] +# io = IOBuffer() +# code_llvm(io, f, (typeof(a), Int); debuginfo=:none) +# # countlines(io) -- doesn't work for some reason +# count(==('\n'), String(take!(io))) +# end - @test llvm_lines(Zeros(10)) < 10 - @test llvm_lines(Ones(10)) < 10 - @test llvm_lines(Fill(2.0,10)) < 10 -end +# @test llvm_lines(Zeros(10)) < 10 +# @test llvm_lines(Ones(10)) < 10 +# @test llvm_lines(Fill(2.0,10)) < 10 +# end From 035c0b90df225c333c4748ba41c0505514c91561 Mon Sep 17 00:00:00 2001 From: Chad Scherrer Date: Thu, 12 Aug 2021 09:50:59 -0700 Subject: [PATCH 4/5] drop old `using` statements --- test/runtests.jl | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/runtests.jl b/test/runtests.jl index b0129055..70e58e70 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,7 +1,5 @@ using FillArrays, LinearAlgebra, SparseArrays, StaticArrays, Random, Base64, Test, Statistics import FillArrays: AbstractFill, RectDiagonal, SquareEye -using InteractiveUtils: code_llvm -using PerformanceTestTools @testset "fill array constructors and convert" begin for (Typ, funcs) in ((:Zeros, :zeros), (:Ones, :ones)) From 9ad0a64ea64303bd51d9b0dd1d93fc4224aed91d Mon Sep 17 00:00:00 2001 From: Sheehan Olver Date: Thu, 12 Aug 2021 11:47:17 -0700 Subject: [PATCH 5/5] Update test/runtests.jl Co-authored-by: Matt Bauman --- test/runtests.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/runtests.jl b/test/runtests.jl index 70e58e70..52b17c4b 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1331,8 +1331,8 @@ end # f(x,j) = @inbounds x[j] # io = IOBuffer() # code_llvm(io, f, (typeof(a), Int); debuginfo=:none) -# # countlines(io) -- doesn't work for some reason -# count(==('\n'), String(take!(io))) +# seekstart(io) +# countlines(io) # end # @test llvm_lines(Zeros(10)) < 10