types: memoize structural facts about a UnionAll in a flag word (rebased #63778) #63932
From @vtjnash on the original PR (https://github.com/JuliaLang/julia/pull/63778#discussion_r4272730920):
Why do we need to hide this? Hidden fields are annoying, and it seems reasonable to make this visible (unless you think it'll get deleted again with other work in changing representations?)
Sorry, I was traveling. We need to hide it because it can change in ways that aren't modeled by our object model. The fields can be set upon access by the runtime and reset by swap-in of an egal pointer. If this is runtime visible, it will exacerbate the getfield egality problem that the unionall refactor is trying to remedy. Possibly in the future we can add the notion of a memorized field, assuming the result of the computation is stable under egality (which I think it should be, but didn't 100% verify), but for now, I think this is best.
@vtjnash does that sound reasonable to you?
Claude's thoughts:
---
Review notes:
1. Generic constructors still make UnionAlls without room for the flag word. The layout size of UnionAll is still 16 bytes, so jl_new_structv, jl_new_struct_uninit, jl_new_bits (via unsafe_load(::Ptr{UnionAll})) and codegen's :new all produce a node that ends where flags begins. Core.eval(Expr(:new, UnionAll, TypeVar(:T), Any)) works today, and the old test in test/subtype.jl built nodes this way. The first type query then reads and fetch_ors 4 bytes past the object. Today those bytes are padding in the 32-byte GC slot, so nothing else is overwritten. But they are not zeroed: I saw one fresh node in 1000 that already had a nonzero flag word, so a stale "known" bit could give a wrong has_free_typevars or occurs-invariant answer. Two possible fixes: reject UnionAll in the generic constructors and in emit_new_struct, or make the layout size sizeof(jl_unionall_t) and zero the extra bytes. Method has the same shape, but its hidden fields are only read on methods the runtime made itself.
2. typeeq_vars_bound_in_env now depends on whether the memo is set. It skips a sub-UnionAll that is known to be closed. A cold walk descends into it and asks lookup_binding for the inner binder. Unlike the other walkers it ignores binders, so if that binder is currently in e->vars, the cold walk returns 1 and the memoized walk returns 0. Probably harmless, but it makes typeeq_is_dangling_key depend on query history.
3. Minor, in commit 1: the "behavior is unchanged except TypeApp" claim also misses that Intersect nodes are now entered by may_substitute_ub, typeeq_vars_bound_in_env and type_references_deferred. All three changes are in the conservative direction.
KristofferC
Rebased version of #63778 by @Keno onto current
master(the original conflicts and isn't maintainer-editable).From the original description:
Conflict resolution notes:
jltypes.c: master addedDataTypefast paths tohas_free_typevars/jl_has_bound_typevars; those are subsumed by the visitors here (free_typevar_visit/bound_typevar_visitalready return early on closed datatypes).subtype.c: master (#63668) madebody_occurs_invlazy insubtype_unionallviavb_body_occurs_inv. This keeps it lazy but computes it through the newUnionAllflag memo (unionall_body_occurs_inv), so the varbinding now stores theUnionAll(vb.ua) instead of its body.🤖 Generated with Claude Code