Skip to content

Commit

Permalink
Merge pull request #38784 from JuliaLang/kf/ccsisdefined
Browse files Browse the repository at this point in the history
Add isdefined check `count_const_size`
  • Loading branch information
Keno authored Dec 9, 2020
2 parents e4da832 + 793f875 commit 811e2c0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
12 changes: 8 additions & 4 deletions base/compiler/utilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,21 @@ function quoted(@nospecialize(x))
return is_self_quoting(x) ? x : QuoteNode(x)
end

function count_const_size(@nospecialize(x))
function count_const_size(@nospecialize(x), count_self::Bool = true)
(x isa Type || x isa Symbol) && return 0
ismutable(x) && return MAX_INLINE_CONST_SIZE + 1
isbits(x) && return Core.sizeof(x)
dt = typeof(x)
sz = sizeof(dt)
sz = count_self ? sizeof(dt) : 0
sz > MAX_INLINE_CONST_SIZE && return MAX_INLINE_CONST_SIZE + 1
dtfd = DataTypeFieldDesc(dt)
for i = 1:nfields(x)
dtfd[i].isptr || continue
sz += count_const_size(getfield(x, i))
isdefined(x, i) || continue
f = getfield(x, i)
if !dtfd[i].isptr && datatype_pointerfree(typeof(f))
continue
end
sz += count_const_size(f, dtfd[i].isptr)
sz > MAX_INLINE_CONST_SIZE && return MAX_INLINE_CONST_SIZE + 1
end
return sz
Expand Down
7 changes: 7 additions & 0 deletions test/compiler/inline.jl
Original file line number Diff line number Diff line change
Expand Up @@ -324,3 +324,10 @@ let ci = code_typed(NonIsBitsDims, Tuple{})[1].first
@test length(ci.code) == 1 && isa(ci.code[1], ReturnNode) &&
ci.code[1].val.value == NonIsBitsDims()
end

struct NonIsBitsDimsUndef
dims::NTuple{N, Int} where N
NonIsBitsDimsUndef() = new()
end
@test Core.Compiler.is_inlineable_constant(NonIsBitsDimsUndef())
@test !Core.Compiler.is_inlineable_constant((("a"^1000, "b"^1000), nothing))

0 comments on commit 811e2c0

Please sign in to comment.