Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Dead code elimination forif (typeof(T).IsValueType)#97080

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
MichalStrehovsky merged 2 commits intodotnet:mainfromMichalStrehovsky:isvaluetype
Jan 18, 2024

Conversation

@MichalStrehovsky
Copy link
Member

@stephentoub found out that for following code:

usingSystem.Buffers;Foo<Bar>();staticT[]Foo<T>(){if(typeof(T).IsValueType){returnArrayPool<T>.Shared.Rent(42);}returnnull!;}classBar{}

We end up generatingArrayPools ofBar even though it's obviously never reachable. The problem is architectural:

  • We run a whole program analysis phase that tries to figure out things like generic dictionary layouts so that later, in code generation phase, we can inline offsets into generic dictionaries into codegen.
  • For the above code, whole program analysis decides that the dictionary layout ofFoo<__Canon> needs a slot forArrayPool<!0>.
  • Codegen then optimizes out theIsValueType branch because__Canon is never a valuetype. But we already allocated the dictionary slot and will fill it out, even though it ends up unused due to the optimization.

We're going to run into issues like this until#83021 is addressed. Whole program analysis cannot currently assume a certain optimization happens because we don't know whether RyuJIT will do it. The only way we can "optimize" during whole program analysis is if we rewrite IL and give RyuJIT no saying in whether to do an optimization or not. Rewriting the IL is not great because it e.g. causes PGO data to not match. I don't like doing it, but there's nothing else we can do.

This change extends dead block elimination to understandtypeof(X).IsValueType. If we recognize a branch is reachable under this condition, we evaluate whether this is true or false and replace the basic block with nops.

Cc @dotnet/ilc-contrib

am11, neon-sunset, antonheryanto, and eerhardt reacted with thumbs up emoji
@stephentoub found out that for following code:```csharpusing System.Buffers;Foo<Bar>();static T[] Foo<T>(){    if (typeof(T).IsValueType)    {        return ArrayPool<T>.Shared.Rent(42);    }    return null!;}class Bar {}```We end up generating `ArrayPool`s of `Bar` even though it's obviously never reachable. The problem is architectural:* We run a whole program analysis phase that tries to figure out things like generic dictionary layouts so that later, in code generation phase, we can inline offsets into generic dictionaries into codegen.* For the above code, whole program analysis decides that the dictionary layout of `Foo<__Canon>` needs a slot for `ArrayPool<!0>`.* Codegen then optimizes out the `IsValueType` branch because `__Canon` is never a valuetype. But we already allocated the dictionary slot and will fill it out, even though it ends up unused due to the optimization.We're going to run into issues like this untildotnet#83021 is addressed. Whole program analysis cannot currently assume a certain optimization happens because we don't know whether RyuJIT will do it. The only way we can "optimize" during whole program analysis is if we rewrite IL and give RyuJIT no saying in whether to do an optimization or not. Rewriting the IL is not great because it e.g. causes PGO data to not match. I don't like doing it, but there's nothing else we can do.This change extends dead block elimination to understand `typeof(X).IsValueType`. If we recognize a branch is reachable under this condition, we evaluate whether this is true or false and replace the basic block with nops.
@ghost
Copy link

Tagging subscribers to this area:@agocke,@MichalStrehovsky,@jkotas
See info inarea-owners.md if you want to be subscribed.

Issue Details

@stephentoub found out that for following code:

usingSystem.Buffers;Foo<Bar>();staticT[]Foo<T>(){if(typeof(T).IsValueType){returnArrayPool<T>.Shared.Rent(42);}returnnull!;}classBar{}

We end up generatingArrayPools ofBar even though it's obviously never reachable. The problem is architectural:

  • We run a whole program analysis phase that tries to figure out things like generic dictionary layouts so that later, in code generation phase, we can inline offsets into generic dictionaries into codegen.
  • For the above code, whole program analysis decides that the dictionary layout ofFoo<__Canon> needs a slot forArrayPool<!0>.
  • Codegen then optimizes out theIsValueType branch because__Canon is never a valuetype. But we already allocated the dictionary slot and will fill it out, even though it ends up unused due to the optimization.

We're going to run into issues like this until#83021 is addressed. Whole program analysis cannot currently assume a certain optimization happens because we don't know whether RyuJIT will do it. The only way we can "optimize" during whole program analysis is if we rewrite IL and give RyuJIT no saying in whether to do an optimization or not. Rewriting the IL is not great because it e.g. causes PGO data to not match. I don't like doing it, but there's nothing else we can do.

This change extends dead block elimination to understandtypeof(X).IsValueType. If we recognize a branch is reachable under this condition, we evaluate whether this is true or false and replace the basic block with nops.

Cc @dotnet/ilc-contrib

Author:MichalStrehovsky
Assignees:-
Labels:

area-NativeAOT-coreclr

Milestone:-

if(offset<SequenceLength)
returnfalse;

if((flags[offset-SequenceLength]&OpcodeFlags.InstructionStart)==0)
Copy link
Member

Choose a reason for hiding this comment

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

Do we need to check that there are no jump targets in the sequence?

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

ReadLdToken and ReadGetTypeFromHandle already do that check

returntrue;
}
elseif(method.IsIntrinsic&&method.Nameis"get_IsValueType"
&&method.OwningTypeisMetadataType{Name:"Type",Namespace:"System"}
Copy link
Member

Choose a reason for hiding this comment

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

We check mdType.Context.SystemModule in the existing case a few lines above. Do we need to do the same here?

It would be nice if both checks use the same style.

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

I had that, then I learned{..., Module: method.Context.SystemModule } doesn't compile, became frustrated at how useless the pattern matching in C# is, and forgot. Fixed.

{
returntrue;
}
elseif(method.IsIntrinsic&&method.Nameis"get_IsValueType"
Copy link
Member

Choose a reason for hiding this comment

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

method.IsIntrinsic can be de-duplicated with the existing case above.

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

We need this to land in theelse branch all the way below if it's an intrinsic but not the one we recognize.

Copy link
Member

@jkotasjkotas left a comment

Choose a reason for hiding this comment

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

Thanks

@stephentoub
Copy link
Member

Thanks for following up on it.

tmds pushed a commit to tmds/runtime that referenced this pull requestJan 23, 2024
@stephentoub found out that for following code:```csharpusing System.Buffers;Foo<Bar>();static T[] Foo<T>(){    if (typeof(T).IsValueType)    {        return ArrayPool<T>.Shared.Rent(42);    }    return null!;}class Bar {}```We end up generating `ArrayPool`s of `Bar` even though it's obviously never reachable. The problem is architectural:* We run a whole program analysis phase that tries to figure out things like generic dictionary layouts so that later, in code generation phase, we can inline offsets into generic dictionaries into codegen.* For the above code, whole program analysis decides that the dictionary layout of `Foo<__Canon>` needs a slot for `ArrayPool<!0>`.* Codegen then optimizes out the `IsValueType` branch because `__Canon` is never a valuetype. But we already allocated the dictionary slot and will fill it out, even though it ends up unused due to the optimization.We're going to run into issues like this untildotnet#83021 is addressed. Whole program analysis cannot currently assume a certain optimization happens because we don't know whether RyuJIT will do it. The only way we can "optimize" during whole program analysis is if we rewrite IL and give RyuJIT no saying in whether to do an optimization or not. Rewriting the IL is not great because it e.g. causes PGO data to not match. I don't like doing it, but there's nothing else we can do.This change extends dead block elimination to understand `typeof(X).IsValueType`. If we recognize a branch is reachable under this condition, we evaluate whether this is true or false and replace the basic block with nops.
@github-actionsgithub-actionsbot locked and limited conversation to collaboratorsFeb 18, 2024
Sign up for freeto subscribe to this conversation on GitHub. Already have an account?Sign in.

Reviewers

@jkotasjkotasjkotas approved these changes

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

3 participants

@MichalStrehovsky@stephentoub@jkotas

[8]ページ先頭

©2009-2025 Movatter.jp