Skip to content
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

Dead code elimination for if (typeof(T).IsValueType) #97080

Merged
merged 2 commits into from
Jan 18, 2024

Conversation

MichalStrehovsky
Copy link
Member

@stephentoub found out that for following code:

using 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 ArrayPools 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 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 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.

Cc @dotnet/ilc-contrib

@stephentoub found out that for following code:

```csharp
using 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 until dotnet#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

ghost commented Jan 17, 2024

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

Issue Details

@stephentoub found out that for following code:

using 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 ArrayPools 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 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 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.

Cc @dotnet/ilc-contrib

Author: MichalStrehovsky
Assignees: -
Labels:

area-NativeAOT-coreclr

Milestone: -

if (offset < SequenceLength)
return false;

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
Member Author

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

@@ -591,6 +591,12 @@ private bool TryGetConstantArgument(MethodIL methodIL, byte[] body, OpcodeFlags[
{
return true;
}
else if (method.IsIntrinsic && method.Name is "get_IsValueType"
&& method.OwningType is MetadataType { 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
Member Author

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.

@@ -591,6 +591,12 @@ private bool TryGetConstantArgument(MethodIL methodIL, byte[] body, OpcodeFlags[
{
return true;
}
else if (method.IsIntrinsic && method.Name is "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
Member Author

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 the else branch all the way below if it's an intrinsic but not the one we recognize.

Copy link
Member

@jkotas jkotas left a comment

Choose a reason for hiding this comment

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

Thanks

@MichalStrehovsky MichalStrehovsky merged commit 77d643d into dotnet:main Jan 18, 2024
118 of 120 checks passed
@MichalStrehovsky MichalStrehovsky deleted the isvaluetype branch January 18, 2024 07:39
@stephentoub
Copy link
Member

Thanks for following up on it.

tmds pushed a commit to tmds/runtime that referenced this pull request Jan 23, 2024
@stephentoub found out that for following code:

```csharp
using 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 until dotnet#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-actions github-actions bot locked and limited conversation to collaborators Feb 18, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants