Skip to content

JIT: Fold constant access to [ReadOnly]Span<T> especially for char and byte #64823

Closed

Description

@EgorBo

In order to be able to declare unrolling rules in pure C# the way it was proposed in #64821 for strings we need to be able to fold this in JIT:

T SecondChar<T>(ReadOnlySpan<T> span) => span[1];
T GetLength<T>(ReadOnlySpan<T> span) => span.Length;

char Test1() => SecondChar<char>("hello"); // => 'e'
int  Test2() => GetLength<char>("hello"); // => 5

Both Test1 and Test2 has to be folded to constants.
Ideally we should be able to also handle spans of bytes (utf8)

Also, fold constant accesses to RVA data (from #64612). Example:

static ReadOnlySpan<byte> data => new byte[] { 1, 2, 3, 4, 5 };

byte Test() => data[3];

Current codegen:

; Method Prog:Test():ubyte:this
       48B8A32BAD36FD010000 mov      rax, 0x1FD36AD2BA3
       0FB600               movzx    rax, byte  ptr [rax]
       C3                   ret      
; Total bytes of code: 14

Expected codegen:

; Method Prog:Test():ubyte:this
       B804000000           mov      eax, 4
       C3                   ret      
; Total bytes of code: 6

NOTES:

  • JIT is already able to do it for string literals e.g. "hello"[2] (see fgMorphArrayIndex)
  • C# should soon get a support for ROS<any primitive> = RVA, not just byte/bool as it is currently
  • We probably can't do it for RVAs in C++/CLI/CX which are mutable (check for mixed-mode)
  • This task most likely needs a new JIT-EE API or maybe the existing getArrayInitializationData is enough

Activity

  1. ghost added
    untriaged
    on Feb 4, 2022
  2. added
    area-CodeGen-coreclr
    help wanted
    on Feb 4, 2022
  3. added this to the Future milestone on Feb 4, 2022
  4. removed
    untriaged
    on Feb 4, 2022
  5. ghost added
    in-pr
    on Nov 23, 2022
  6. ghost removed
    in-pr
    on Nov 29, 2022
  7. ghost locked as resolved and limited conversation to collaborators on Dec 29, 2022
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-CodeGen-coreclrhelp wanted

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions