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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,13 @@ 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.

&& method.OwningType is MetadataType mdt
&& mdt.Name == "Type" && mdt.Namespace == "System" && mdt.Module == mdt.Context.SystemModule
&& TryExpandTypeIsValueType(methodIL, body, flags, currentOffset, out constant))
{
return true;
}
else
{
constant = 0;
Expand Down Expand Up @@ -745,6 +752,40 @@ private string GetResourceStringForAccessor(EcmaMethod method)
return null;
}

private static bool TryExpandTypeIsValueType(MethodIL methodIL, byte[] body, OpcodeFlags[] flags, int offset, out int constant)
{
// We expect to see a sequence:
// ldtoken Foo
// call GetTypeFromHandle
// -> offset points here
constant = 0;
const int SequenceLength = 10;
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

return false;

ILReader reader = new ILReader(body, offset - SequenceLength);

TypeDesc type = ReadLdToken(ref reader, methodIL, flags);
if (type == null)
return false;

if (!ReadGetTypeFromHandle(ref reader, methodIL, flags))
return false;

// Dataflow runs on top of uninstantiated IL and we can't answer some questions there.
// Unfortunately this means dataflow will still see code that the rest of the system
// might have optimized away. It should not be a problem in practice.
if (type.IsSignatureVariable)
return false;

constant = type.IsValueType ? 1 : 0;

return true;
}

private static bool TryExpandTypeEquality(MethodIL methodIL, byte[] body, OpcodeFlags[] flags, int offset, string op, out int constant)
{
// We expect to see a sequence:
Expand Down Expand Up @@ -793,37 +834,37 @@ private static bool TryExpandTypeEquality(MethodIL methodIL, byte[] body, Opcode
constant ^= 1;

return true;
}

static TypeDesc ReadLdToken(ref ILReader reader, MethodIL methodIL, OpcodeFlags[] flags)
{
ILOpcode opcode = reader.ReadILOpcode();
if (opcode != ILOpcode.ldtoken)
return null;
private static TypeDesc ReadLdToken(ref ILReader reader, MethodIL methodIL, OpcodeFlags[] flags)
{
ILOpcode opcode = reader.ReadILOpcode();
if (opcode != ILOpcode.ldtoken)
return null;

TypeDesc t = (TypeDesc)methodIL.GetObject(reader.ReadILToken());
TypeDesc t = (TypeDesc)methodIL.GetObject(reader.ReadILToken());

if ((flags[reader.Offset] & OpcodeFlags.BasicBlockStart) != 0)
return null;
if ((flags[reader.Offset] & OpcodeFlags.BasicBlockStart) != 0)
return null;

return t;
}
return t;
}

static bool ReadGetTypeFromHandle(ref ILReader reader, MethodIL methodIL, OpcodeFlags[] flags)
{
ILOpcode opcode = reader.ReadILOpcode();
if (opcode != ILOpcode.call)
return false;
private static bool ReadGetTypeFromHandle(ref ILReader reader, MethodIL methodIL, OpcodeFlags[] flags)
{
ILOpcode opcode = reader.ReadILOpcode();
if (opcode != ILOpcode.call)
return false;

MethodDesc method = (MethodDesc)methodIL.GetObject(reader.ReadILToken());
MethodDesc method = (MethodDesc)methodIL.GetObject(reader.ReadILToken());

if (!method.IsIntrinsic || method.Name != "GetTypeFromHandle")
return false;
if (!method.IsIntrinsic || method.Name != "GetTypeFromHandle")
return false;

if ((flags[reader.Offset] & OpcodeFlags.BasicBlockStart) != 0)
return false;
if ((flags[reader.Offset] & OpcodeFlags.BasicBlockStart) != 0)
return false;

return true;
}
return true;
}

private sealed class SubstitutedMethodIL : MethodIL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public static int Run()
TestArrayElementTypeOperations.Run();
TestStaticVirtualMethodOptimizations.Run();
TestTypeEquals.Run();
TestTypeIsValueType.Run();
TestBranchesInGenericCodeRemoval.Run();
TestUnmodifiableStaticFieldOptimization.Run();
TestUnmodifiableInstanceFieldOptimization.Run();
Expand Down Expand Up @@ -355,6 +356,31 @@ public static void Run()
}
}

class TestTypeIsValueType
{
class Never { }

class Ever { }

static void Generic<T>()
{
if (typeof(T).IsValueType)
{
Activator.CreateInstance(typeof(Never));
}

Activator.CreateInstance(typeof(Ever));
}

public static void Run()
{
Generic<object>();

ThrowIfPresent(typeof(TestTypeIsValueType), nameof(Never));
ThrowIfNotPresent(typeof(TestTypeIsValueType), nameof(Ever));
}
}

class TestBranchesInGenericCodeRemoval
{
class ClassWithUnusedVirtual
Expand Down
Loading