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

fix CyclePadding(::DataType) #50719

Merged
merged 1 commit into from
Jul 31, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion base/reinterpretarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,9 @@ function CyclePadding(T::DataType)
a, s = datatype_alignment(T), sizeof(T)
as = s + (a - (s % a)) % a
pad = padding(T)
s != as && push!(pad, Padding(s, as - s))
if s != as
pad = Core.svec(pad..., Padding(s, as - s))
Copy link
Member

Choose a reason for hiding this comment

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

Is this function still inferable? It seems like as - s might need to be an argument to padding

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, I think it's still inferrable:

julia> only(code_typed(Base.CyclePadding, (DataType,); optimize=false))
CodeInfo(
1%1  = Base.datatype_alignment(T)::Int64%2  = Base.sizeof(T)::Int64
│         (a = %1)::Int64
│         (s = %2)::Int64%5  = s::Int64%6  = a::Int64%7  = (s % a)::Int64%8  = (%6 - %7)::Int64%9  = (%8 % a)::Int64
│         (as = %5 + %9)::Int64
│         (pad = Base.padding(T))::Core.SimpleVector%12 = (s != as)::Bool
└──       goto #3 if not %12
2%14 = Core.svec::Core.Const(Core.svec)
│   %15 = pad::Core.SimpleVector%16 = s::Int64%17 = (as - s)::Int64%18 = Base.Padding(%16, %17)::Base.Padding%19 = Core.tuple(%18)::Tuple{Base.Padding}
└──       (pad = Core._apply_iterate(Base.iterate, %14, %15, %19))::Core.SimpleVector
3%21 = Base.CyclePadding(pad, as)::Base.CyclePadding{Core.SimpleVector}
└──       return %21
) => Base.CyclePadding{Core.SimpleVector}

julia> code_typed(; optimize=false) do
           Base.CyclePadding(Int)
       end
1-element Vector{Any}:
 CodeInfo(
1%1 = Base.CyclePadding::Core.Const(Base.CyclePadding)
│   %2 = (%1)(Main.Int)::Core.Const(Base.CyclePadding{Core.SimpleVector}(svec(), 8))
└──      return %2
) => Base.CyclePadding{Core.SimpleVector}

Copy link
Member

@vtjnash vtjnash Jul 31, 2023

Choose a reason for hiding this comment

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

This code isn't reachable for as == s, but generally seems pretty hard to trigger anyways? I don't fully understand this code's (expected/observed) behavior:

julia> code_typed(; optimize=false) do
                  Base.CyclePadding(Tuple{Int64,Int8,Int16})
              end
1-element Vector{Any}:
 CodeInfo(
    @ REPL[12]:2 within `#19`
1%1 = Base.CyclePadding::Core.Const(Base.CyclePadding)
│   %2 = Core.apply_type(Main.Tuple, Main.Int64, Main.Int8, Main.Int16)::Core.Const(Tuple{Int64, Int8, Int16})
│   %3 = (%1)(%2)::Core.Const(Base.CyclePadding{Core.SimpleVector}(svec(Base.Padding(10, 1), Base.Padding(16, 4)), 16))
└──      return %3
) => Base.CyclePadding{Core.SimpleVector}
julia> primitive type Int24 24 end

julia> Base.CyclePadding(Int24)
ERROR: MethodError: no method matching push!(::Core.SimpleVector, ::Base.Padding)

Closest candidates are:
  push!(::Any, ::Any, ::Any)
   @ Base abstractarray.jl:3399
  push!(::Any, ::Any, ::Any, ::Any...)
   @ Base abstractarray.jl:3400
  push!(::BitVector, ::Any)
   @ Base bitarray.jl:752
  ...

Stacktrace:
 [1] Base.CyclePadding(T::DataType)
   @ Base ./reinterpretarray.jl:723
 [2] top-level scope
   @ REPL[11]:1

end
CyclePadding(pad, as)
end

Expand Down