Simplify getfield and isdefined tfuncs by treating DataType as immutable#37293
Simplify getfield and isdefined tfuncs by treating DataType as immutable#37293martinholters wants to merge 2 commits intomasterfrom
DataType as immutable#37293Conversation
37fd1e9 to
80c7374
Compare
base/compiler/tfuncs.jl
Outdated
| elseif isa(arg1, Const) | ||
| arg1v = (arg1::Const).val | ||
| if !ismutable(arg1v) || isdefined(arg1v, idx) || (isa(arg1v, DataType) && is_dt_const_field(idx)) | ||
| if !ismutable(arg1v) || isdefined(arg1v, idx) || isa(arg1v, DataType) |
There was a problem hiding this comment.
The types field can be undefined and then get set later.
There was a problem hiding this comment.
Ah. That's one of those where is_dt_const_field returned true, though.
Could we by any chance just trigger setting it here?
There was a problem hiding this comment.
So you're saying that
julia> foo() = isdefined(VersionNumber, :types)
foo (generic function with 1 method)
julia> @code_typed foo()
CodeInfo(
1 ─ return true
) => Boolis bad because foo won't get recompiled when VersionNumber.types gets cleared and foo() will then wrongly still return true? That is bad, of course, but nothing this PR introduces. It's the same on 1.0, 1.5, and master. I'm not sure how big an undertaking fixing that would be, considering that some code (e.g. fieldcount) might rely on this (and getfield) being const-inferred.
There was a problem hiding this comment.
Okay, I managed to construct a test case for the "types may get set later" case:
g37293(::Type{T}) where T = isdefined(T, :types)
struct T37293; x::Val{g37293(T37293)}; end # call g37293 on incomplete type with not-yet-set types
@test g37293(T37293) == isdefined(T37293, :types)This fails on master, but I have an update to this PR in the working that will fix it.
@vtjnash how can I provoke clearing the types field?
|
While at it, what about the fields of |
1e1d6ae to
d690166
Compare
|
I believe |
But it can become set later if undefined now? So if undefined infer Coming back to So to summarize:
Does that look ok? Then I'll implement it in this PR. |
d690166 to
b38686a
Compare
|
Replaced by better effects modeling and |
And a little cleanup: Neither
UnionAllnorSimpleVectorshould need special-casing. The former is immutable and gets handled as well as any other immutable, the latter has no fields.Technically,
DataTypecan be mutated, but actually doing that is looking for more trouble than just wrong constant propagation, and we've been doing this for some fields of it anyway. But maybe we can throw in thesetfieldbuiltin when trying to modify aDataType? Or is it even possible to declareDataTypeimmutable?