extend `dataids` semantics, with the goal of obviating non-generic methods of `mightalias` #60984
TODO: one more necessary refinement here is to rely on the "extent" feature of DataID. Without that I suppose the already existing tests will fail.
I really appreciate the addition of some sort of readable state; that's a huge improvement. I'm not as convinced about putting it into the type system, but could be convinced if the benchmarks are compelling. It just seems like it'd make it harder to work with the (now heterogeneous) tuples of DataIDs... and then also harder and more expensive to subsequently resolve what I find to be the biggest shortcoming: the generic dataid method.
Using (and computing) extents can give some wins, but finding the extents themselves can also be surprisingly expensive. IMO, I don't find the non-generic mightalias methods so problematic. They're pure optimizations (and improved answers) for known common cases — including cases that still exist here with extents. For example, the common @view A[1, :] has worthless extents, but it's trivial to see that it doesn't alias against A[2, :].
I really appreciate the addition of some sort of readable state; that's a huge improvement. I'm not as convinced about putting it into the type system, but could be convinced if the benchmarks are compelling.
If the additional information is not in the type system, then necessarily additional work has to be done at run time in _isdisjoint, so there is necessarily a performance regression, for some use case at least. My goal is to enable types to opt into implementing the more powerful variant of dataids, without causing any run time performance regression for types that do not choose to opt in.
Do you disagree with this goal? Or is it something else?
It just seems like it'd make it harder to work with the (now heterogeneous) tuples of DataIDs
True, however I do not think it is possible to keep the tuples homogeneous while keeping backwards compatibility.
and then also harder and more expensive to subsequently resolve what I find to be the biggest shortcoming: the generic dataid method.
You are referring to a hypothetical fallback method dataids(::Any)/dataids(::AbstractArray) with a non-trivial implementation? That would necessarily have to use reflection, right? TBH I feel bad about that direction. I would feel better with a hypothetical dataids fallback with a trivial and conservative implementation that would guarantee safety for Base.mightalias. Something like dataids(::Any) = (missing,), with adjustments to isdisjoint to interpret missing as "aliases anything and everything". And then make dataids public API, so most types would implement it, as long as they're not already isbits or similar.
Also, I am not sure why do you say that putting the "is readonly"/"is writable" bit into the type system would make the fallback method of dataids more expensive or more difficult to implement? After all, it is always safe to fall back to "data is writable"/"data is not readonly".
Using (and computing) extents can give some wins, but finding the extents themselves can also be surprisingly expensive.
As above, putting the additional information into the type domain means that each method of dataids only opts into the new costs if they choose so explicitly. Whoever implements a dataids method may choose not to specify an extent if they deem that finding the extent might turn out to be excessively costly.
IMO, I don't find the non-generic mightalias methods so problematic.
I dislike the potential for the creeping combinatoric explosion of methods definitions, when various combinations of types are accounted for. And the fact that the special mightalias methods hijack coverage away from dataids. However, I guess you are right, I suppose we might want to add back the methods I got rid of in the initial commit, before merging this PR, as necessary, if the PR will be merged.
including cases that still exist here with extents. For example, the common@view A[1, :]has worthless extents, but it's trivial to see that it doesn't alias againstA[2, :].
I suppose dataids/DataID could handle that particular case as-is, by merely relaxing the supertype constraint for the optional_extent field from Union{Nothing, UnitRange{UInt}} to Union{Nothing, UnitRange{UInt}, StepRange{UInt, UInt}}. Or even relax it further to some supertype like Union{Nothing, OrdinalRange{UInt, UInt}}? Will play with this.
This PR now kinda depends on PR #60998, which is blocked on issue #47577.
My main concern is one of prioritization: this needs to be a very fast path, and that there are many _other_ deficiencies that I find to be simultaneously more important and harder. If this _also_ paints a path towards addressing the other challenges, then I am in! But I actually think this will make subsequent improvements harder.
My initial vision here was that the dataids intersection test could eventually become the first tier in a multi-tier system. We first check to see if any data segments are the same, and — if so — we then ask for a more refined answer. That's effectively what the non-generic mightalias methods aim to do, but in an ad-hoc manner.
Perhaps one significant improvement would be to introduce to a more concrete function whose name _actually describes_ what the generic mightalias currently does: perhaps something like shares_dataids.
Then it becomes easier in my mind to begin layering additional functions atop this atom — including ones that more clearly specify which argument will be written to.
nsajko
Very WIP.
There are multiple issues with the aliasing detection machinery. This PR only attempts to make
dataidsmore expressive, to make special methods ofmightaliasunnecessary. It attempts complete backwards compatibility, even thoughdataidsis not public API yet. Other PRs might follow, for example to replace the current unsafe defaults ofdataidsormightaliaswith something safer and more consistent.Some issues to cross-reference:
https://github.com/JuliaLang/julia/issues/8087
https://github.com/JuliaLang/julia/issues/26865
Not directly relevant, perhaps for a future PR: https://github.com/JuliaLang/julia/issues/50820
* https://github.com/JuliaLang/julia/issues/51753