-
Notifications
You must be signed in to change notification settings - Fork 4.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ILLink analyzer] Add branch analysis #94123
Merged
Merged
Changes from 7 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
532bb45
Add branch analysis
sbomer 20cc6d0
Update APICompat suppressions
sbomer 9a441ef
Merge remote-tracking branch 'origin/main' into branchAnalysisRebase
sbomer 69f11bd
Feedback from discussion
sbomer e1fda9b
Merge remote-tracking branch 'origin/main' into branchAnalysisRebase
sbomer 0a80a16
Merge remote-tracking branch 'origin/main' into branchAnalysisRebase
sbomer c29dfe8
PR feedback
sbomer abbc54f
PR feedback
sbomer 73275d2
PR feedback
sbomer 9d1cb21
Align ILLink/ILCompiler test behavior
sbomer 2114f20
PR feedback
sbomer 3672801
Merge remote-tracking branch 'origin/main' into branchAnalysisRebase
sbomer 2574f85
Fix merge
sbomer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
src/tools/illink/src/ILLink.RoslynAnalyzer/DataFlow/FeatureCheckVisitor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
// Copyright (c) .NET Foundation and contributors. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Collections.Immutable; | ||
using System.Diagnostics; | ||
using System.Diagnostics.CodeAnalysis; | ||
using ILLink.Shared.DataFlow; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.FlowAnalysis; | ||
using Microsoft.CodeAnalysis.Operations; | ||
using ILLink.Shared.TypeSystemProxy; | ||
|
||
using StateValue = ILLink.RoslynAnalyzer.DataFlow.LocalDataFlowState< | ||
ILLink.Shared.DataFlow.ValueSet<ILLink.Shared.DataFlow.SingleValue>, | ||
ILLink.RoslynAnalyzer.DataFlow.FeatureContext, | ||
ILLink.Shared.DataFlow.ValueSetLattice<ILLink.Shared.DataFlow.SingleValue>, | ||
ILLink.RoslynAnalyzer.DataFlow.FeatureContextLattice | ||
>; | ||
|
||
namespace ILLink.RoslynAnalyzer.DataFlow | ||
{ | ||
// Visits a conditional expression to optionally produce a 'FeatureChecksValue' | ||
// (a set features that are checked to be enabled or disabled). | ||
// The visitor takes a LocalDataFlowState as an argument, allowing for checks that | ||
// depend on the current dataflow state. | ||
public class FeatureChecksVisitor : OperationVisitor<StateValue, FeatureChecksValue?> | ||
{ | ||
DataFlowAnalyzerContext _dataFlowAnalyzerContext; | ||
|
||
public FeatureChecksVisitor (DataFlowAnalyzerContext dataFlowAnalyzerContext) | ||
{ | ||
_dataFlowAnalyzerContext = dataFlowAnalyzerContext; | ||
} | ||
|
||
public override FeatureChecksValue? VisitArgument (IArgumentOperation operation, StateValue state) | ||
{ | ||
return Visit (operation.Value, state); | ||
} | ||
|
||
public override FeatureChecksValue? VisitPropertyReference (IPropertyReferenceOperation operation, StateValue state) | ||
{ | ||
foreach (var analyzer in _dataFlowAnalyzerContext.EnabledRequiresAnalyzers) { | ||
if (analyzer.IsRequiresCheck (_dataFlowAnalyzerContext.Compilation, operation.Property)) { | ||
return new FeatureChecksValue (analyzer.FeatureName); | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public override FeatureChecksValue? VisitUnaryOperator (IUnaryOperation operation, StateValue state) | ||
{ | ||
if (operation.OperatorKind is not UnaryOperatorKind.Not) | ||
return null; | ||
|
||
FeatureChecksValue? context = Visit (operation.Operand, state); | ||
if (context == null) | ||
return null; | ||
|
||
return context.Value.Negate (); | ||
} | ||
|
||
public bool? GetLiteralBool (IOperation operation) | ||
{ | ||
if (operation is not ILiteralOperation literal) | ||
return null; | ||
|
||
return GetConstantBool (literal.ConstantValue); | ||
} | ||
|
||
static bool? GetConstantBool (Optional<object?> constantValue) | ||
{ | ||
if (!constantValue.HasValue || constantValue.Value is not bool value) | ||
return null; | ||
|
||
return value; | ||
} | ||
|
||
public override FeatureChecksValue? VisitBinaryOperator (IBinaryOperation operation, StateValue state) | ||
{ | ||
bool expectEqual; | ||
switch (operation.OperatorKind) { | ||
case BinaryOperatorKind.Equals: | ||
expectEqual = true; | ||
break; | ||
case BinaryOperatorKind.NotEquals: | ||
expectEqual = false; | ||
break; | ||
default: | ||
return null; | ||
} | ||
|
||
if (GetLiteralBool (operation.LeftOperand) is bool leftBool) { | ||
if (Visit (operation.RightOperand, state) is not FeatureChecksValue rightValue) | ||
return null; | ||
return leftBool == expectEqual | ||
? rightValue | ||
: rightValue.Negate (); | ||
} | ||
|
||
if (GetLiteralBool (operation.RightOperand) is bool rightBool) { | ||
if (Visit (operation.LeftOperand, state) is not FeatureChecksValue leftValue) | ||
return null; | ||
return rightBool == expectEqual | ||
? leftValue | ||
: leftValue.Negate (); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public override FeatureChecksValue? VisitIsPattern (IIsPatternOperation operation, StateValue state) | ||
{ | ||
if (GetExpectedValueFromPattern (operation.Pattern) is not bool patternValue) | ||
return null; | ||
|
||
if (Visit (operation.Value, state) is not FeatureChecksValue value) | ||
return null; | ||
|
||
return patternValue | ||
? value | ||
: value.Negate (); | ||
|
||
|
||
static bool? GetExpectedValueFromPattern (IPatternOperation pattern) | ||
{ | ||
switch (pattern) { | ||
case IConstantPatternOperation constantPattern: | ||
return GetConstantBool (constantPattern.Value.ConstantValue); | ||
case INegatedPatternOperation negatedPattern: | ||
return !GetExpectedValueFromPattern (negatedPattern.Pattern); | ||
default: | ||
return null; | ||
} | ||
} | ||
} | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
src/tools/illink/src/ILLink.RoslynAnalyzer/DataFlow/FeatureChecksValue.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright (c) .NET Foundation and contributors. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using ILLink.Shared.DataFlow; | ||
|
||
namespace ILLink.RoslynAnalyzer.DataFlow | ||
{ | ||
// Represents the feature conditions checked in a conditional expression, | ||
// such as | ||
// Debug.Assert (RuntimeFeatures.IsDynamicCodeSupported) | ||
// or | ||
// if (!RuntimeFeatures.IsDynamicCodeSupported) | ||
// For now, this is only designed to track the built-in "features"/"capabilities" | ||
// like RuntimeFeatures.IsDynamicCodeSupported, where a true return value | ||
// indicates that a feature/capability is available. | ||
public record struct FeatureChecksValue : INegate<FeatureChecksValue> | ||
{ | ||
public ValueSet<string> EnabledFeatures; | ||
public ValueSet<string> DisabledFeatures; | ||
|
||
public FeatureChecksValue (string enabledFeature) | ||
{ | ||
EnabledFeatures = new ValueSet<string> (enabledFeature); | ||
DisabledFeatures = ValueSet<string>.Empty; | ||
} | ||
|
||
private FeatureChecksValue (ValueSet<string> enabled, ValueSet<string> disabled) | ||
{ | ||
EnabledFeatures = enabled; | ||
DisabledFeatures = disabled; | ||
} | ||
|
||
public FeatureChecksValue And (FeatureChecksValue other) | ||
{ | ||
return new FeatureChecksValue ( | ||
ValueSet<string>.Union (EnabledFeatures.DeepCopy (), other.EnabledFeatures.DeepCopy ()), | ||
ValueSet<string>.Union (DisabledFeatures.DeepCopy (), other.DisabledFeatures.DeepCopy ())); | ||
} | ||
|
||
public FeatureChecksValue Or (FeatureChecksValue other) | ||
{ | ||
return new FeatureChecksValue ( | ||
ValueSet<string>.Intersection (EnabledFeatures.DeepCopy (), other.EnabledFeatures.DeepCopy ()), | ||
ValueSet<string>.Intersection (DisabledFeatures.DeepCopy (), other.DisabledFeatures.DeepCopy ())); | ||
} | ||
|
||
public FeatureChecksValue Negate () | ||
{ | ||
return new FeatureChecksValue (DisabledFeatures.DeepCopy (), EnabledFeatures.DeepCopy ()); | ||
} | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
src/tools/illink/src/ILLink.RoslynAnalyzer/DataFlow/FeatureContextLattice.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright (c) .NET Foundation and contributors. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Runtime.InteropServices; | ||
using ILLink.Shared; | ||
using ILLink.Shared.DataFlow; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
|
||
namespace ILLink.RoslynAnalyzer.DataFlow | ||
{ | ||
public struct FeatureContext : IEquatable<FeatureContext>, IDeepCopyValue<FeatureContext> | ||
{ | ||
// The set of features known to be enabled in this context. | ||
// Null represents "all possible features". | ||
public ValueSet<string>? EnabledFeatures; | ||
|
||
public static readonly FeatureContext All = new FeatureContext (null); | ||
|
||
public static readonly FeatureContext None = new FeatureContext (ValueSet<string>.Empty); | ||
|
||
public FeatureContext (ValueSet<string>? enabled) | ||
{ | ||
EnabledFeatures = enabled; | ||
} | ||
|
||
public bool IsEnabled (string feature) | ||
{ | ||
return EnabledFeatures == null || EnabledFeatures.Value.Contains (feature); | ||
} | ||
|
||
public bool Equals (FeatureContext other) => EnabledFeatures == other.EnabledFeatures; | ||
public override bool Equals (object? obj) => obj is FeatureContext other && Equals (other); | ||
public override int GetHashCode () => EnabledFeatures?.GetHashCode () ?? typeof (FeatureContext).GetHashCode (); | ||
|
||
public static bool operator == (FeatureContext left, FeatureContext right) => left.Equals (right); | ||
public static bool operator != (FeatureContext left, FeatureContext right) => !left.Equals (right); | ||
|
||
public FeatureContext DeepCopy () | ||
{ | ||
return new FeatureContext (EnabledFeatures?.DeepCopy ()); | ||
} | ||
|
||
public FeatureContext Intersection (FeatureContext other) | ||
{ | ||
if (EnabledFeatures == null) | ||
return other.DeepCopy (); | ||
if (other.EnabledFeatures == null) | ||
return this.DeepCopy (); | ||
return new FeatureContext (ValueSet<string>.Intersection (EnabledFeatures.Value, other.EnabledFeatures.Value)); | ||
} | ||
|
||
public FeatureContext Union (FeatureContext other) | ||
{ | ||
if (EnabledFeatures == null) | ||
return this.DeepCopy (); | ||
if (other.EnabledFeatures == null) | ||
return other.DeepCopy (); | ||
return new FeatureContext (ValueSet<string>.Union (EnabledFeatures.Value, other.EnabledFeatures.Value)); | ||
} | ||
} | ||
|
||
public readonly struct FeatureContextLattice : ILattice<FeatureContext> | ||
{ | ||
public FeatureContextLattice () { } | ||
|
||
// The top value is the identity of meet (intersection), the set of all features. | ||
public FeatureContext Top { get; } = FeatureContext.All; | ||
|
||
// We are interested in features which are known to be enabled for all paths through the CFG, | ||
// so the meet operator is set intersection. | ||
public FeatureContext Meet (FeatureContext left, FeatureContext right) => left.Intersection (right); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I renamed
Meet
on ValueSet toUnion
since I also added anIntersection
method. I'm thinking it's most readable if we use set operation names for ValueSet, and the lattice operation names (Meet) when using ValueSetLattice. In the case of this method, I think the fact thatDereferenceValue
computes a union of dereferenced values for the items in the ValueSet has more to do with the semantics of the dereference operation and ValueSet, than with ValueSetLattice, so I think it makes sense to useUnion
here.