Registry Mirror
Skip to content

ILLink substitute BitConverter.IsLittleEndian as true constant #83169

Open

Description

@EgorBo

There is a method in Ascii.Utility.cs (SPC.dll):

private static bool FirstCharInUInt32IsAscii(uint value)
{
    return (BitConverter.IsLittleEndian && (value & 0xFF80u) == 0)
        || (!BitConverter.IsLittleEndian && (value & 0xFF800000u) == 0);
}

After ILLink it looks like this:

  .method private hidebysig static bool 
      FirstCharInUInt32IsAscii(unsigned int32 'value') cil managed
  {
    .maxstack 8
    IL_0000: ldsfld       bool System.BitConverter::IsLittleEndian
    IL_0005: pop 
    IL_0006: ldarg.0      
    IL_0007: ldc.i4       65408
    IL_000c: and
    IL_000d: brfalse.s    IL_0018
    IL_000f: ldsfld       bool System.BitConverter::IsLittleEndian
    IL_0014: brtrue.s     IL_0016
    IL_0016: ldc.i4.0
    IL_0017: ret
    IL_0018: ldc.i4.1
    IL_0019: ret
  }

which is roughly:

private static bool FirstCharInUInt32IsAscii(uint value)
{
    int unused = BitConverter.IsLittleEndian;
    if ((value & 65408) == 0)
        goto RetTrue;
    if (BitConverter.IsLittleEndian)
        ; 
    return false;
RetTrue:
    return true;
}

So while it seems that ILLink did touch this method (thanks to #37615) it seems that it could do a better job here by replacing ldsfld bool System.BitConverter::IsLittleEndian with just ldc.i4.1, etc.

This doesn't let JIT to inline this small (in fact) method, because JIT doesn't resolve ldsfld tokens during IL prescan and it doesn't know that it's a special IsLittleEndian field that is always a constant) - it can be found in e.g. GetIndexOfFirstNonAsciiChar_Intrinsified

Activity

  1. ghost added
    area-CodeGen-coreclr
    on Mar 9, 2023
  2. ghost added
    untriaged
    on Mar 9, 2023
  3. added
    area-Tools-ILLink
    and removed
    area-CodeGen-coreclr
    on Mar 9, 2023
  4. jkotas commented on Mar 9, 2023

    @jkotas
    Member

    Is linker keeping the ldsfld around to preserve the cctor trigger side effect? Preserving these side-effects is important for some substitutions.

  5. jkotas commented on Mar 9, 2023

    @jkotas
    Member

    JIT doesn't resolve ldsfld tokens during IL prescan and it doesn't know that it's a special IsLittleEndian field that is always a constant)

    Should the JIT start doing that in Tier1 or AOT modes to give the right treatment to initialized readonly fields?

  6. vitek-karas commented on Mar 9, 2023

    @vitek-karas
    Member

    If it's the access to the substituted field (and not a propagated constant) I think it should be OK to remove the ldsfld completely. But I haven't debugged the linker to tell exactly why it's keeping the instruction - it could be just general "conservative" approach.

  7. EgorBo commented on Mar 9, 2023

    @EgorBo
    MemberAuthor

    JIT doesn't resolve ldsfld tokens during IL prescan and it doesn't know that it's a special IsLittleEndian field that is always a constant)

    Should the JIT start doing that in Tier1 or AOT modes to give the right treatment to initialized readonly fields?

    I'll take a look

  8. marek-safar commented on Mar 9, 2023

    @marek-safar
    Contributor

    Is linker keeping the ldsfld around to preserve the cctor trigger side effect?

    Correct, we didn't put any effort yet to teach linker how to trim static constructors bodies and for that reason we also keep anything that could static ctor even when applying the substitutions.

  9. EgorBo commented on Mar 9, 2023

    @EgorBo
    MemberAuthor

    Is linker keeping the ldsfld around to preserve the cctor trigger side effect?

    Correct, we didn't put any effort yet to teach linker how to trim static constructors bodies and for that reason we also keep anything that could static ctor even when applying the substitutions.

    It seems that even if I remove static constructor from BitConverter (IsLittleEndian = true; is not needed since that field is an intrinsic for CoreCLR/Crossgen/NAOT) - it still doesn't remove the field

  10. EgorBo commented on Mar 9, 2023

    @EgorBo
    MemberAuthor

    Ah, the lambda in https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/BitConverter.cs#L672 still keeps it 😞 - presumably, can be replaced with string.FastAllocate

  11. added this to the Future milestone on Aug 9, 2023
  12. ghost removed
    untriaged
    on Aug 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-Tools-ILLink

    Type

    No type

    Projects

    • Status
      No status

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions