[ILLink] Should the trim analyzer use full dataflow analysis, or single assignment of DAM

Open
#118,853 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
25/100
Issue type
Feature
Clarity
Needs clarification
Activity status
Stale
Tech stack
csharp
Domain
compilers

Research direction

Start by reading the DAM analyzer discussion and comparing the proposed full dataflow, first-assignment, and inferred-requirement approaches. Check how illink and ilc would need to match the selected behavior; done means choosing and documenting a consistent analysis model, including branching assignments and warning behavior.

Written by the indexing model from the issue text.

Description

area-Tools-ILLink

Currently the DAM analyzer does full backwards-branching dataflow analysis for tracking DAM values. This gives it the best analysis (sometimes better than illink), but it does require significant complexity, and can lead to performance issues. Adding matching dataflow analysis to the linker for matching behavior would create similar problems in the linker.

An alternative discussed was to have each local assume the annotation of the first value assigned to it, and warn if a different annotation is assigned to it, much like type analysis in the C# language. This would simplify implementation, but would require workarounds by code expecting to be able to assign differently annotated values to the same local.

We also run into potential issues when a local is first assigned in if/else blocks, a ternary statement, or a switch. We could work around this by taking the Meet of the potential values, or warning if all assignments to a local do not have the same DAM annotations.

Another way to look at it is to assign the requirements on the local variable (determine by assignments where the local is the source and an annotated location is the target), and require that all assignments to that local meet that requirement. This also makes the behavior of local variables analogous to properly annotated fields. Fields must be annotated with the union of the requirements of all the locations that are targets of assignments from the field, while locals would have those annotations inferred.

For example, consider the warning behavior between assuming first assignment

// okay
var myLocal = MyMethodReturnsWithPublicMethods();
myFieldWithPublicMethods = myLocal;

// warn: myLocal requires PublicFields
myLocal = MyMethodReturnsWithPublicFields;
myFieldWithPublicFields = myLocal;

vs. assuming the union of all requirements

// warn: myLocal requires both PublicFields and PublicMethods
var myLocal = MyMethodReturnsWithPublicMethods();
myFieldWithPublicMethods = myLocal;

// okay
myLocal = MyMethodReturnsWithPublicFieldsAndPublicMethods();
myFieldWithPublicFields = myLocal;

Regardless, if we do change the analyzer behavior to either of these, it's likely that illink and ilc will also need to match it for consistency in warnings. This would be a significant regression in the ergonomics of making code trim-safe, but it would greatly simplify analysis and likely increase the speed of the tools.

cc. @dotnet/illink @dotnet/ilc-contrib for discussion

Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dotnet/runtime

All issues in dotnet/runtime

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.