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

Allow mutable-for-identity-only types to be inlined into IR #54034

Merged
merged 2 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
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
22 changes: 21 additions & 1 deletion base/compiler/utilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,27 @@ const MAX_INLINE_CONST_SIZE = 256

function count_const_size(@nospecialize(x), count_self::Bool = true)
(x isa Type || x isa Core.TypeName || x isa Symbol) && return 0
ismutable(x) && return MAX_INLINE_CONST_SIZE + 1
if ismutable(x)
# No definite size
(isa(x, GenericMemory) || isa(x, String) || isa(x, SimpleVector)) &&
return MAX_INLINE_CONST_SIZE + 1
if isa(x, Module)
# We allow modules, because we already assume they are externally
# rooted, so we count their contents as 0 size.
return sizeof(Ptr{Cvoid})
end
# We allow mutable types with no mutable fields (i.e. those mutable
# types used for identity only). The intent of this function is to
# prevent the rooting of large amounts of data that may have been
# speculatively computed. If the struct can get mutated later, we
# cannot assess how much data we might end up rooting. However, if
# the struct is mutable only for identity, the query still works.
for i = 1:nfields(x)
if !isconst(typeof(x), i)
return MAX_INLINE_CONST_SIZE + 1
end
end
end
isbits(x) && return Core.sizeof(x)
dt = typeof(x)
sz = count_self ? sizeof(dt) : 0
Expand Down
9 changes: 9 additions & 0 deletions test/scopedvalues.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# This file is a part of Julia. License is MIT: https://julialang.org/license

using Base.ScopedValues

include("compiler/irutils.jl")

@testset "errors" begin
@test ScopedValue{Float64}(1)[] == 1.0
@test_throws InexactError ScopedValue{Int}(1.5)
Expand Down Expand Up @@ -160,3 +163,9 @@ end
let code = code_typed(with_macro_slot_cross)[1][1].code
@test !any(x->isa(x, Core.PhiCNode), code)
end

# inline constant scoped values
const inlineable_const_sv = ScopedValue(1)
@test fully_eliminated(; retval=(inlineable_const_sv => 1)) do
inlineable_const_sv => 1
end