Fix generic type parameter resolution for fields (#1368, #1369)#1373
Merged
leculver merged 1 commit intomicrosoft:mainfrom Feb 26, 2026
Merged
Fix generic type parameter resolution for fields (#1368, #1369)#1373leculver merged 1 commit intomicrosoft:mainfrom
leculver merged 1 commit intomicrosoft:mainfrom
Conversation
…rosoft#1369) - Filter out System.__Canon types from field type resolution in CacheFields. When a field's MethodTable resolves to __Canon (the canonical type used for shared generic instantiations), set the type to null to trigger the metadata signature fallback path. - Add concrete generic type argument resolution in ClrField.InitData(). Parse the parent type's name to extract concrete generic type arguments (e.g., 'LinkedListNode<System.String>' -> [System.String]) and pass them to GetOrCreateTypeFromSignature for Var/MVar substitution. - Extend GetOrCreateTypeFromSignature to accept optional concrete type arguments. When resolving Var(index) in field signatures, use the concrete type instead of creating a ClrGenericType placeholder. - Improve GenericInstantiation resolution to attempt looking up concrete generic types by name when concrete type arguments are available. - Fix ClrValueType.TryReadStringField missing _interior parameter in GetAddress call. - Add regression tests for LinkedListNode<string>.item field type resolution and Dictionary<string,int>._entries array reading. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
Author
|
/azp run |
|
Azure Pipelines successfully started running 1 pipeline(s). |
This was
linked to
issues
Feb 25, 2026
Closed
brianrob
approved these changes
Feb 25, 2026
max-charlamb
pushed a commit
that referenced
this pull request
Apr 10, 2026
…eholder When __Canon filtering (introduced in #1373) nulls a field's type and the metadata signature fallback resolves to a ClrGenericType placeholder (e.g. 'TStateMachine' with no fields), fall back to resolving via the field's MethodTable directly. This handles compiler-generated types nested inside open generic classes (e.g. async state machines in local functions) where GetTypeByName fails because it compares closed-generic names against open-generic TypeDefs. The MethodTable resolves to __Canon, which restores the pre-#1373 behavior: the field reports IsObjectReference=true, so consumers use ReadObjectField to get the concrete type from the heap. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
leculver
added a commit
to leculver/clrmd
that referenced
this pull request
Apr 28, 2026
ClrField.InitData was eagerly resolving the field's type signature even when
the caller only requested Name or Attributes. The type-resolution path calls
ContainingType.GetConcreteGenericTypeArguments, which calls ClrHeap.GetTypeByName,
which performs a module-wide scan and triggers expensive mscordacwks
GetMethodTableName DAC calls. This made field-heavy workloads (e.g. enumerating
async state machines) ~10x slower than ClrMD 3.1.
Split InitData into two methods:
- InitData(forName) populates _name and _attributes from metadata only.
- InitType() resolves _type from the field signature, including the
ClrGenericType + MethodTable fallback from PR microsoft#1373.
Type initialization state is tracked separately via _typeInitialized so the
old _attributes sentinel no longer doubles as a type-resolution flag, and so
deterministically failed type resolutions are not retried on every Type access.
Add regression tests verifying that reading Name (or Attributes) before Type
still produces the correct concrete generic instantiation.
leculver
added a commit
to leculver/clrmd
that referenced
this pull request
Apr 28, 2026
ClrField.InitData was eagerly resolving the field's type signature even when
the caller only requested Name or Attributes. The type-resolution path calls
ContainingType.GetConcreteGenericTypeArguments, which calls ClrHeap.GetTypeByName,
which performs a module-wide scan and triggers expensive mscordacwks
GetMethodTableName DAC calls. This made field-heavy workloads (e.g. enumerating
async state machines) ~10x slower than ClrMD 3.1.
Split InitData into two methods:
- InitData(forName) populates _name and _attributes from metadata only.
- InitType() resolves _type from the field signature, including the
ClrGenericType + MethodTable fallback from PR microsoft#1373.
Type initialization state is tracked separately via _typeInitialized so the
old _attributes sentinel no longer doubles as a type-resolution flag, and so
deterministically failed type resolutions are not retried on every Type access.
Add regression tests verifying that reading Name (or Attributes) before Type
still produces the correct concrete generic instantiation.
leculver
added a commit
to leculver/clrmd
that referenced
this pull request
Apr 28, 2026
ClrField.InitData was eagerly resolving the field's type signature even when
the caller only requested Name or Attributes. The type-resolution path calls
ContainingType.GetConcreteGenericTypeArguments, which calls ClrHeap.GetTypeByName,
which performs a module-wide scan and triggers expensive mscordacwks
GetMethodTableName DAC calls. This made field-heavy workloads (e.g. enumerating
async state machines) ~10x slower than ClrMD 3.1.
Split InitData into two methods:
- InitData(forName) populates _name and _attributes from metadata only.
- InitType() resolves _type from the field signature, including the
ClrGenericType + MethodTable fallback from PR microsoft#1373.
Type initialization state is tracked separately via _typeInitialized so the
old _attributes sentinel no longer doubles as a type-resolution flag, and so
deterministically failed type resolutions are not retried on every Type access.
Add regression tests verifying that reading Name (or Attributes) before Type
still produces the correct concrete generic instantiation.
leculver
added a commit
to leculver/clrmd
that referenced
this pull request
Apr 28, 2026
ClrField.InitData was eagerly resolving the field's type signature even when
the caller only requested Name or Attributes. The type-resolution path calls
ContainingType.GetConcreteGenericTypeArguments, which calls ClrHeap.GetTypeByName,
which performs a module-wide scan and triggers expensive mscordacwks
GetMethodTableName DAC calls. This made field-heavy workloads (e.g. enumerating
async state machines) ~10x slower than ClrMD 3.1.
Split the lazy load:
- InitData(forName) populates _name and _attributes from metadata only.
- GetClrType() / ResolveType() resolves _type from the field signature,
including the ClrGenericType + MethodTable fallback from PR microsoft#1373. Runs
only when the Type property is actually accessed.
Type-resolution state is tracked in a separate _typeInitialized field
(0 = not computed, 1 = resolved, 2 = no resolvable type) so the _attributes
sentinel no longer doubles as a type-resolution flag, and so deterministically
failed resolutions don't retry on every Type access.
No locks or Interlocked are used on the type-resolution path. Reads and writes
of int and reference are atomic in .NET, and the result is deterministic for
a given field, so racing threads always agree. GetClrType() re-reads _type
after _typeInitialized and recomputes if it sees _typeInitialized == 1 with
a null _type, which tolerates write-write reordering on weakly-ordered
architectures (ARM).
Add regression tests verifying that reading Name (or Attributes) before Type
still produces the correct concrete generic instantiation.
leculver
added a commit
that referenced
this pull request
Apr 28, 2026
ClrField.InitData was eagerly resolving the field's type signature even when
the caller only requested Name or Attributes. The type-resolution path calls
ContainingType.GetConcreteGenericTypeArguments, which calls ClrHeap.GetTypeByName,
which performs a module-wide scan and triggers expensive mscordacwks
GetMethodTableName DAC calls. This made field-heavy workloads (e.g. enumerating
async state machines) ~10x slower than ClrMD 3.1.
Split the lazy load:
- InitData(forName) populates _name and _attributes from metadata only.
- GetClrType() / ResolveType() resolves _type from the field signature,
including the ClrGenericType + MethodTable fallback from PR #1373. Runs
only when the Type property is actually accessed.
Type-resolution state is tracked in a separate _typeInitialized field
(0 = not computed, 1 = resolved, 2 = no resolvable type) so the _attributes
sentinel no longer doubles as a type-resolution flag, and so deterministically
failed resolutions don't retry on every Type access.
No locks or Interlocked are used on the type-resolution path. Reads and writes
of int and reference are atomic in .NET, and the result is deterministic for
a given field, so racing threads always agree. GetClrType() re-reads _type
after _typeInitialized and recomputes if it sees _typeInitialized == 1 with
a null _type, which tolerates write-write reordering on weakly-ordered
architectures (ARM).
Add regression tests verifying that reading Name (or Attributes) before Type
still produces the correct concrete generic instantiation.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Filter out System.__Canon types from field type resolution in CacheFields. When a field's MethodTable resolves to __Canon (the canonical type used for shared generic instantiations), set the type to null to trigger the metadata signature fallback path.
Add concrete generic type argument resolution in ClrField.InitData(). Parse the parent type's name to extract concrete generic type arguments (e.g., 'LinkedListNode<System.String>' -> [System.String]) and pass them to GetOrCreateTypeFromSignature for Var/MVar substitution.
Extend GetOrCreateTypeFromSignature to accept optional concrete type arguments. When resolving Var(index) in field signatures, use the concrete type instead of creating a ClrGenericType placeholder.
Improve GenericInstantiation resolution to attempt looking up concrete generic types by name when concrete type arguments are available.
Fix ClrValueType.TryReadStringField missing _interior parameter in GetAddress call.
Add regression tests for LinkedListNode.item field type resolution and Dictionary<string,int>._entries array reading.