improve type inference of Base.aligned_sizeof#49801
Merged
Conversation
This commit includes a bit of refactoring of `Base.aligned_sizeof` to
make it more inference-friendly, especially in cases like
`Base.aligned_sizeof(::Union{DataType,Union})`.
In particular, it eliminates the chance of inference accounting for a
method error of `datatype_alignment(::Union)` in the second branch.
xref: <aviatesk/JET.jl#512>
Contributor
|
Would it be helpful if |
Member
Author
|
Maybe you meant |
aviatesk
added a commit
to aviatesk/JET.jl
that referenced
this pull request
May 13, 2023
aviatesk
added a commit
to aviatesk/JET.jl
that referenced
this pull request
May 13, 2023
Contributor
|
I just meant that function datatype_inline_layout(t::DataType, ptrfree::Bool, asfield::Bool)
((t.name.flags & 0x04) === 0x04) || return (0, 0, 0)
lyt = unsafe_load(convert(Ptr{DataTypeLayout}, t.layout))
(lyt.npointers > 0 && ptrfree) && return (0, 0, 0)
(t.name.n_uninitialized != 0) && return (0, 0, 0)
(((lyt.flags >> 1) & 3) > 1) && return (0, 0, 0)
al = Int(lyt.alignment)
# primitive types in struct slots need their sizes aligned. issue #37974
sz = (asfield && isprimitivetype(t)) ? LLT_ALIGN(Int(lyt.size), al) : Int(lyt.size)
return (1, sz, al)
end
function union_isinlinable(@nospecialize(t::Type), ptrfree::Bool, asfield::Bool)
@_foldable_meta
if isa(t, DataType)
return datatype_inline_layout(t, ptrfree, asfield)
elseif isa(t, Union)
ta = t.a
if isa(ta, DataType)
cnt, nb, align = datatype_inline_layout(ta, ptrfree, asfield)
cnt === 0 && return (0, 0, 0)
t = t.b
while true
if isa(t, Union)
ta = t.a
isa(ta, DataType) || return (0, 0, 0)
n, sz, al = datatype_inline_layout(ta, ptrfree, asfield)
n === 0 && return (0, 0, 0)
nb = nb < sz ? sz : nb
align = align < al ? al : align
cnt += 1
t = t.b
elseif isa(t, DataType)
n, sz, al = datatype_inline_layout(t, ptrfree, asfield)
n === 0 && return (0, 0, 0)
nb = nb < sz ? sz : nb
align = align < al ? al : align
return (cnt + 1, nb, align)
else
return (0, 0, 0)
end
end
else
return (0, 0, 0)
end
else
return (0, 0, 0)
end
end
function aligned_sizeof(@nospecialize T::Type)
n, sz, al = union_isinlinable(T, false, false)
if n === 0
return Core.sizeof(Ptr{Cvoid})
else
return LLT_ALIGN(sz, al)
end
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This commit includes a bit of refactoring of
Base.aligned_sizeofto make it more inference-friendly, especially in cases likeBase.aligned_sizeof(::Union{DataType,Union}).In particular, it eliminates the chance of inference accounting for a method error of
datatype_alignment(::Union)in the second branch.xref: aviatesk/JET.jl#512