Repository navigation
[feature/damAnalyzer] Change linker dataflow to use ValueSet/MultiValue - #2401
Conversation
Currently linker uses ValueNode to represent both single and multi-values. The multi-value is stored as a special value MergePoint. This means that basically every "Reader" should call UniqueValues which enumerates all values in the value. Otherwise it may only react to single-values and not function correctly for multi-values. Also in order to share more code with the analyzer, we need to switch to the new model, where multi-values is stored in an explicit ValueSet. This change replaces all the places where multiple values can be stored with MultiValue (=ValueSet<ValueNode>). Then it removes the MergePoint values and the entire infrastructure around it. Other notable changes: - Added implicit conversion from single TValue to ValueSet<TValue> to make it much easier to work with - Changed how ArrayValue is created to use factory methods. Previously ArrayValue could store a multi-value as its size. This meant that it was necessary to enumerate UniqueValues on ArrayValue to get actual sizes (results in multiple ArrayValues). This change introduces a factory method which return a MultiValue which may contain multiple ArrayValues if there are multiple potential sizes. A bit more memory hungry in some cases, but makes the "reader" simpler and less error prone. - Added IsEmpty on ValueSet - as it's relatively common to check for empty ValueSet - This actually fixes a bug dotnet#2025 because it now correctly tracks empty MultiValue - This change finally gets rid of all possible places where ValueNode could be null before - null was used as basically Unknown value, but it sometimes caused nullrefs in weird places. This can't happen anymore, since MultiValue is a struct and thus is always - non-null.
f14fdc2 to
7e16ec9
Compare
sbomer
left a comment
There was a problem hiding this comment.
This is great, I love how this avoids nullable ValueNode in a lot of places. Sorry it took me a while to do the review.
|
|
||
| ValueSet (EnumerableValues values) => _values = values; | ||
|
|
||
| public static implicit operator ValueSet<TValue> (TValue value) => new (value); |
There was a problem hiding this comment.
How bad is it if we don't have the implicit conversion? Generally I'd prefer to be explicit, but if this is used in a lot of places it makes sense.
There was a problem hiding this comment.
It was at least 20 places before I gave up... so probably another 20 after that point :-)
I thought about it and it's actually not surprising. It's a discrepancy between production and consumption.
On the production side a MultiValue is always created with just a single value in it - intrinsics, all the visitors/scanners, ... this has to be like that since it needs to create a specific value type (Field access, method parameter, ...). (The only exception is arrays, but those are weird). The only way (except array) we create true multiple-value MultiValue is through Meet.
On the consumption side everything needs to be able to handle multiple values, so it has to take input as MultiValue. Lot of the common helpers are like that. But lot of times these helpers are also called directly from the creation path, which only has single-value at hand. All these places require the conversion. Technically this could be solved by having two overloads for each helper... but that's just messy.
There was a problem hiding this comment.
Out of curiosity, if I comment out the implicit conversion I get 57 build errors 😉
| @@ -332,20 +113,6 @@ public void Dispose () | |||
| /// </summary> | |||
| public abstract class LeafValueNode : ValueNode | |||
There was a problem hiding this comment.
Not right now, but I intentionally left the type design same as before to avoid too much churn. I will have a separate PR to rework the value type system.
| { | ||
| readonly MarkStep _markStep; | ||
| readonly MarkScopeStack _scopeStack; | ||
| static ValueSetLattice<ValueNode> MultiValueLattice => default; |
There was a problem hiding this comment.
what about making same field in base type protected?
| return result; | ||
| } | ||
|
|
||
| public static MultiValue Create (ValueNode size, TypeReference elementType) |
There was a problem hiding this comment.
As this is only helper would be better if this took int as size?
…ue (dotnet/linker#2401) Currently linker uses ValueNode to represent both single and multi-values. The multi-value is stored as a special value MergePoint. This means that basically every "Reader" should call UniqueValues which enumerates all values in the value. Otherwise it may only react to single-values and not function correctly for multi-values. Also in order to share more code with the analyzer, we need to switch to the new model, where multi-values is stored in an explicit ValueSet. This change replaces all the places where multiple values can be stored with MultiValue (=ValueSet<ValueNode>). Then it removes the MergePoint values and the entire infrastructure around it. Other notable changes: - Added implicit conversion from single TValue to ValueSet<TValue> to make it much easier to work with - Changed how ArrayValue is created to use factory methods. Previously ArrayValue could store a multi-value as its size. This meant that it was necessary to enumerate UniqueValues on ArrayValue to get actual sizes (results in multiple ArrayValues). This change introduces a factory method which return a MultiValue which may contain multiple ArrayValues if there are multiple potential sizes. A bit more memory hungry in some cases, but makes the "reader" simpler and less error prone. - Added IsEmpty on ValueSet - as it's relatively common to check for empty ValueSet - This actually fixes a bug dotnet/linker#2025 because it now correctly tracks empty MultiValue - This change finally gets rid of all possible places where ValueNode could be null before - null was used as basically Unknown value, but it sometimes caused nullrefs in weird places. This can't happen anymore, since MultiValue is a struct and thus is always - non-null. Commit migrated from dotnet/linker@06e0c68
Currently linker uses ValueNode to represent both single and multi-values. The multi-value is stored as a special value MergePoint. This means that basically every "Reader" should call UniqueValues which enumerates all values in the value. Otherwise it may only react to single-values and not function correctly for multi-values.
Also in order to share more code with the analyzer, we need to switch to the new model, where multi-values is stored in an explicit ValueSet.
This change replaces all the places where multiple values can be stored with MultiValue (=ValueSet). Then it removes the MergePoint values and the entire infrastructure around it.
Other notable changes:
Note: Currently this makes the data flow in linker relatively inefficient as ValueSet always creates a HashSet internally. #2397 should fix that and store single-values as direct references without a HashSet, basically going back to what linker did before (the only difference is that ValueNode reference is now wrapped in a ValueSet struct).