Skip to content
This repository was archived by the owner on Sep 25, 2026. It is now read-only.

[feature/damAnalyzer] Change linker dataflow to use ValueSet/MultiValue - #2401

Merged
vitek-karas merged 4 commits into
dotnet:feature/damAnalyzerfrom
vitek-karas:ILLinkMultiValue
Dec 3, 2021
Merged

vitek-karas merged 4 commits into
dotnet:feature/damAnalyzerfrom
vitek-karas:ILLinkMultiValue

Conversation

@vitek-karas

Copy link
Copy Markdown
Member

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:

  • Added implicit conversion from single TValue to ValueSet 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 Linker warns on method which always throws but has annotated return value #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.

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).

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.

@sbomer sbomer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/linker/Linker.Dataflow/ValueNode.cs Resolved

ValueSet (EnumerableValues values) => _values = values;

public static implicit operator ValueSet<TValue> (TValue value) => new (value);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the type needed?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about making same field in base type protected?

Comment thread src/linker/Linker.Dataflow/ValueNode.cs Outdated
return result;
}

public static MultiValue Create (ValueNode size, TypeReference elementType)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As this is only helper would be better if this took int as size?

@vitek-karas
vitek-karas merged commit 06e0c68 into dotnet:feature/damAnalyzer Dec 3, 2021
@vitek-karas
vitek-karas deleted the ILLinkMultiValue branch December 3, 2021 19:41
agocke pushed a commit to dotnet/runtime that referenced this pull request Nov 16, 2022
…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
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants