Skip to content

Commit a612220

Browse files
authored
effects: add docstrings to the type-based-effect-analysis queries (#49222)
1 parent a20a3d0 commit a612220

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

base/compiler/typeinfer.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ function adjust_effects(sv::InferenceState)
447447
end
448448
ipo_effects = Effects(ipo_effects; inaccessiblememonly=ALWAYS_TRUE)
449449
end
450-
if is_consistent_if_notreturned(ipo_effects) && is_consistent_argtype(rt)
450+
if is_consistent_if_notreturned(ipo_effects) && is_identity_free_argtype(rt)
451451
# in a case when the :consistent-cy here is only tainted by mutable allocations
452452
# (indicated by `CONSISTENT_IF_NOTRETURNED`), we may be able to refine it if the return
453453
# type guarantees that the allocations are never returned

base/compiler/typeutils.jl

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -340,13 +340,29 @@ function unwraptv_lb(@nospecialize t)
340340
end
341341
const unwraptv = unwraptv_ub
342342

343-
# this query is specially written for `adjust_effects` and returns true if a value of this type
344-
# never involves inconsistency of mutable objects that are allocated somewhere within a call graph
345-
is_consistent_argtype(@nospecialize ty) =
346-
is_consistent_type(widenconst(ignorelimited(ty)))
347-
is_consistent_type(@nospecialize ty) = isidentityfree(ty)
343+
"""
344+
is_identity_free_argtype(argtype) -> Bool
345+
346+
Return `true` if the `argtype` object is identity free in the sense that this type or any
347+
reachable through its fields has non-content-based identity (see `Base.isidentityfree`).
348+
This query is specifically designed for `adjust_effects`, enabling it to refine the
349+
`:consistent` effect property tainted by mutable allocation(s) within the analyzed call
350+
graph when the return value type is `is_identity_free_argtype`, ensuring that the allocated
351+
mutable objects are never returned.
352+
"""
353+
is_identity_free_argtype(@nospecialize ty) = is_identity_free_type(widenconst(ignorelimited(ty)))
354+
is_identity_free_type(@nospecialize ty) = isidentityfree(ty)
355+
356+
"""
357+
is_immutable_argtype(argtype) -> Bool
348358
349-
is_immutable_argtype(@nospecialize ty) = is_immutable_type(widenconst(ignorelimited(ty)))
359+
Return `true` if the `argtype` object is known to be immutable.
360+
This query is specifically designed for `getfield_effects` and `isdefined_effects`, allowing
361+
them to prove `:consistent`-cy of `getfield` / `isdefined` calls when applied to immutable
362+
objects. Otherwise, we need to additionally prove that the non-immutable object is not a
363+
global object to prove the `:consistent`-cy.
364+
"""
365+
is_immutable_argtype(@nospecialize argtype) = is_immutable_type(widenconst(ignorelimited(argtype)))
350366
is_immutable_type(@nospecialize ty) = _is_immutable_type(unwrap_unionall(ty))
351367
function _is_immutable_type(@nospecialize ty)
352368
if isa(ty, Union)
@@ -355,6 +371,16 @@ function _is_immutable_type(@nospecialize ty)
355371
return !isabstracttype(ty) && !ismutabletype(ty)
356372
end
357373

358-
is_mutation_free_argtype(@nospecialize argtype) =
374+
"""
375+
is_mutation_free_argtype(argtype) -> Bool
376+
377+
Return `true` if `argtype` object is mutation free in the sense that no mutable memory
378+
is reachable from this type (either in the type itself) or through any fields
379+
(see `Base.ismutationfree`).
380+
This query is specifically written for analyzing the `:inaccessiblememonly` effect property
381+
and is supposed to improve the analysis accuracy by not tainting the `:inaccessiblememonly`
382+
property when there is access to mutation-free global object.
383+
"""
384+
is_mutation_free_argtype(@nospecialize(argtype)) =
359385
is_mutation_free_type(widenconst(ignorelimited(argtype)))
360386
is_mutation_free_type(@nospecialize ty) = ismutationfree(ty)

0 commit comments

Comments
 (0)