-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Conversation
@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.
Tagging subscribers to this area: @agocke, @MichalStrehovsky, @jkotas 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
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 Cc @dotnet/ilc-contrib
|
if (offset < SequenceLength) | ||
return false; | ||
|
||
if ((flags[offset - SequenceLength] & OpcodeFlags.InstructionStart) == 0) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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" } |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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" |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks
Thanks for following up on it. |
@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.
@stephentoub found out that for following code:
We end up generating
ArrayPool
s ofBar
even though it's obviously never reachable. The problem is architectural:Foo<__Canon>
needs a slot forArrayPool<!0>
.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