Skip to content

Managed ilasm: avoid unused ValueType references - #136679

Merged
jkoritzinsky merged 1 commit into
dotnet:mainfrom
pcshrosbree:ilasm/valuetype-typeref-on-demand
Oct 30, 2026
Merged

jkoritzinsky merged 1 commit into
dotnet:mainfrom
pcshrosbree:ilasm/valuetype-typeref-on-demand

Conversation

@pcshrosbree

Copy link
Copy Markdown
Contributor

TL;DR: The managed ilasm adds an unused System.ValueType reference to images that declare an unsealed class but do not use that type. This PR changes the value type check so it no longer creates the reference. The check also reports some unsealed value types that it missed before. The PR changes one line, adds one method and adds four tests.

The managed ilasm adds a TypeRef to System.ValueType to every image that declares an unsealed class, even when nothing in the image uses it. Native ilasm does not create the reference just to check a class.

I found the difference while comparing managed and native ilasm output for #135314. The extra reference is valid metadata. In images that do not need it, it adds an unnecessary TypeRef row and name. It also adds a difference between the two assemblers' output.

The cause is the unsealed value type check in InitializeTypeDefinition. For an unsealed class, it compares the base type with EntityRegistry.SystemValueTypeType, and that getter creates the reference on first use. The check now calls a new EntityRegistry.IsSystemValueType. It recognizes a System.ValueType reference whose scope is one of the core library references (System.Private.CoreLib, System.Runtime, mscorlib or netstandard). The helper uses FindAssemblyReference to find the core library references, preserving the existing handling of case and aliases. It creates no references.

The old check compared the base type with the one reference the getter cached. The old check missed an unsealed type extending another core library's System.ValueType, for example when a later .assembly extern changed the preferred core library. The new check reports UnsealedValueType for those types too. Every type the old check reported is still reported.

Tests, in TypeDefinitionTests:

  • an image with two classes and no value types has no System.ValueType reference;
  • an unsealed type extending System.ValueType is reported for each core library name, including one with different capitalization and one referenced through an alias;
  • an unsealed type extending System.ValueType through a .typedef is reported even when a later core library reference becomes preferred;
  • an unsealed type extending System.ValueType is reported when a core library reference is declared after an earlier class or value type.

ILAssembler.Tests: 5728 passed, 0 failed. I also checked which tests fail with the old comparison and with deliberately incorrect versions of the new check:

  • the old comparison fails the first test and both cases where a core library reference is declared after an earlier class or value type;
  • comparing the scope's name with the core library names fails both the capitalization and alias tests;
  • recognizing only the current preferred core library fails both .typedef test cases;
  • leaving out any one of the four core library names fails at least one test;
  • a check that always returns false fails 11 test cases.

I assembled three src/tests cases with a previously built baseline managed ilasm and with this change. With this change, Conv_I4 and dev10_710121 no longer contain the ValueType string. cpobj (unaligned/1) declares a value type and keeps the string. All three return 100 on the shared framework with either build.

Note

This change was prepared with AI assistance (Anthropic Claude and OpenAI Codex) under my direction. AI agents ran the builds and tests on my machine. I reviewed the change and this description before posting.

For an unsealed class, the unsealed value type check read
EntityRegistry.SystemValueTypeType. That getter creates a TypeRef to
System.ValueType on first use, so an image with an unsealed class
carried the reference even when nothing used it. Native ilasm does not
create the reference just to check a class.

Recognize System.ValueType by name and check its scope against the core
library references. Find those references with the registry's lookup,
which creates nothing. The old check compared against the getter's
single cached reference. It missed an unsealed value type whose base
type used a different core library reference, such as one declared
later. The new check reports it.
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Oct 30, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 4 pipeline(s).
12 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @JulieLeeMSFT, @dotnet/jit-contrib
See info in area-owners.md if you want to be subscribed.

@jkoritzinsky
jkoritzinsky merged commit f1fdb60 into dotnet:main Oct 30, 2026
81 checks passed
@pcshrosbree

Copy link
Copy Markdown
Contributor Author

Thank you for the review and merge!

@pcshrosbree
pcshrosbree deleted the ilasm/valuetype-typeref-on-demand branch October 30, 2026 17:55
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-ILTools-coreclr community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants