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

Array allocation elimination regression #52305

Closed
nsajko opened this issue Nov 25, 2023 · 1 comment
Closed

Array allocation elimination regression #52305

nsajko opened this issue Nov 25, 2023 · 1 comment

Comments

@nsajko
Copy link
Contributor

nsajko commented Nov 25, 2023

With this code:

function f_noalias!(x, y)
  Base.mightalias(x, y) && throw("No aliasing allowed!")
  x .= y
end
using AllocCheck
versioninfo()
check_allocs(f_noalias!, Tuple{Vector{Int}, Vector{Int}})

The allocating code is eliminated on v1.10, but not on v1.11:

julia> versioninfo()
Julia Version 1.10.0-rc1
Commit 5aaa9485436 (2023-11-03 07:44 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 8 × AMD Ryzen 3 5300U with Radeon Graphics
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-15.0.7 (ORCJIT, znver2)
  Threads: 1 on 8 virtual cores
Environment:
  JULIA_NUM_PRECOMPILE_TASKS = 3
  JULIA_PKG_PRECOMPILE_AUTO = 0

julia> check_allocs(f_noalias!, Tuple{Vector{Int}, Vector{Int}})
Any[]
julia> versioninfo()
Julia Version 1.11.0-DEV.972
Commit 9884e447e79 (2023-11-23 16:16 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 8 × AMD Ryzen 3 5300U with Radeon Graphics
  WORD_SIZE: 64
  LLVM: libLLVM-15.0.7 (ORCJIT, znver2)
  Threads: 1 on 8 virtual cores
Environment:
  JULIA_NUM_PRECOMPILE_TASKS = 3
  JULIA_PKG_PRECOMPILE_AUTO = 0

julia> check_allocs(f_noalias!, Tuple{Vector{Int}, Vector{Int}})
3-element Vector{Any}:
 Allocating runtime call to "jl_genericmemory_copyto" in ./genericmemory.jl:76
  | ccall(:jl_genericmemory_copyto, Cvoid, (Any, Ptr{Cvoid}, Any, Ptr{Cvoid}, Int), dest.mem, dest.ptr_or_offset, src.mem, src.ptr_or_offset, Int(n))

Stacktrace:
 [1] unsafe_copyto!
   @ ./genericmemory.jl:76 [inlined]
 [2] _copyto_impl!
   @ ./array.jl:308 [inlined]
 [3] copyto!
   @ ./array.jl:299 [inlined]
 [4] copyto!
   @ ./array.jl:322 [inlined]
 [5] copyto!
   @ ./broadcast.jl:965 [inlined]
 [6] copyto!
   @ ./broadcast.jl:924 [inlined]
 [7] materialize!
   @ ./broadcast.jl:882 [inlined]
 [8] materialize!
   @ ./broadcast.jl:879 [inlined]
 [9] f_noalias!(x::Vector{Int64}, y::Vector{Int64})
   @ Main ./REPL[1]:3

 Allocating runtime call to "jl_genericmemory_copy_slice" in ./array.jl:347
  | newmem = ccall(:jl_genericmemory_copy_slice, Ref{Memory{T}}, (Any, Ptr{Cvoid}, Int), ref.mem, ref.ptr_or_offset, length(a))

Stacktrace:
  [1] copy
    @ ./array.jl:347 [inlined]
  [2] unaliascopy
    @ ./abstractarray.jl:1518 [inlined]
  [3] unalias
    @ ./abstractarray.jl:1502 [inlined]
  [4] broadcast_unalias
    @ ./broadcast.jl:945 [inlined]
  [5] preprocess
    @ ./broadcast.jl:952 [inlined]
  [6] preprocess_args
    @ ./broadcast.jl:955 [inlined]
  [7] preprocess
    @ ./broadcast.jl:951 [inlined]
  [8] copyto!
    @ ./broadcast.jl:968 [inlined]
  [9] copyto!
    @ ./broadcast.jl:924 [inlined]
 [10] materialize!
    @ ./broadcast.jl:882 [inlined]
 [11] materialize!
    @ ./broadcast.jl:879 [inlined]
 [12] f_noalias!(x::Vector{Int64}, y::Vector{Int64})
    @ Main ./REPL[1]:3

 Allocation of Vector{Int64} in ./array.jl:348
  | return $(Expr(:new, :(typeof(a)), :(Core.memoryref(newmem)), :(a.size)))

Stacktrace:
  [1] copy
    @ ./array.jl:348 [inlined]
  [2] unaliascopy
    @ ./abstractarray.jl:1518 [inlined]
  [3] unalias
    @ ./abstractarray.jl:1502 [inlined]
  [4] broadcast_unalias
    @ ./broadcast.jl:945 [inlined]
  [5] preprocess
    @ ./broadcast.jl:952 [inlined]
  [6] preprocess_args
    @ ./broadcast.jl:955 [inlined]
  [7] preprocess
    @ ./broadcast.jl:951 [inlined]
  [8] copyto!
    @ ./broadcast.jl:968 [inlined]
  [9] copyto!
    @ ./broadcast.jl:924 [inlined]
 [10] materialize!
    @ ./broadcast.jl:882 [inlined]
 [11] materialize!
    @ ./broadcast.jl:879 [inlined]
 [12] f_noalias!(x::Vector{Int64}, y::Vector{Int64})
    @ Main ./REPL[1]:3

Related Discourse discussion:

https://discourse.julialang.org/t/ann-alloccheck-jl-static-code-analysis-to-prove-allocation-free-behavior/106414/19?u=nsajko

@vtjnash
Copy link
Member

vtjnash commented Nov 26, 2023

jl_genericmemory_copyto doesn't allocate, so you should file this on AllocCheck instead

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

No branches or pull requests

2 participants