Skip to content

Support loop cloning with non-int iteration variables #85552

Open

Description

@BruceForstall

Loops with byte/short iteration variables:

for (byte i = 0; i < 100; i++)
{
    a[i] = 0;
}

create an iteration increment tree with a CAST node:

***** BB03
STMT00003 ( 0x00E[E-] ... 0x012 )
               [000025] -A---+-----                         *  ASG       int
               [000024] D----+-N---                         +--*  LCL_VAR   int    V01 loc0
               [000023] -----+-----                         \--*  CAST      int <- ubyte <- int
               [000022] -----+-----                            \--*  ADD       int
               [000020] -----+-----                               +--*  LCL_VAR   int    V01 loc0
               [000021] -----+-----                               \--*  CNS_INT   int    1

This pattern is not matched by GenTree::IsLclVarUpdateTree().

Loops with long iteration variables:

for (long i = 0; i < 100; i++)
{
    a[i] = 0;
}

create (naturally) an increment constant that is long typed:

STMT00003 ( 0x00B[E-] ... 0x00F )
               [000022] -A---+-----                         *  ASG       long
               [000021] D----+-N---                         +--*  LCL_VAR   long   V01 loc0
               [000020] -----+-----                         \--*  ADD       long
               [000017] -----+-----                            +--*  LCL_VAR   long   V01 loc0
               [000019] -----+-----                            \--*  CNS_INT   long   1

and that is screened out by Compiler::optIsLoopIncrTree():

        if ((incrVal->gtOper != GT_CNS_INT) || (incrVal->TypeGet() != TYP_INT))

category:cq
theme:loop-opt
skill-level:expert
cost:small
impact:small

Activity

  1. added
    area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI
    on Apr 29, 2023
  2. added this to the Future milestone on Apr 29, 2023
  3. BruceForstall commented on May 1, 2023

    @BruceForstall
    ContributorAuthor

    For the long iteration var case, there are (at least) these additional things:

    The array length node ARR_LENGTH has a cast on top, which Compiler::optExtractArrIndex() doesn't recognize:

    *  COMMA     byref
    +--*  BOUNDS_CHECK_Rng void
    |  +--*  LCL_VAR   long   V01 loc0
    |  \--*  CAST      long <- uint
    |     \--*  ARR_LENGTH int
    |        \--*  LCL_VAR   ref    V00 arg0
    \--*  ARR_ADDR  byref byte[]
       \--*  ADD       byref
          +--*  LCL_VAR   ref    V00 arg0
          \--*  ADD       long
             +--*  LCL_VAR   long   V01 loc0
             \--*  CNS_INT   long   16
    

    The iteration variable is expected to be TYP_INT in Compiler::optCheckIterInLoopTest().

    Compiler::optDeriveLoopCloningConditions() requires the initLcl var to be TYP_INT.

    LC_Ident constants are assumed to be TYP_INT.

    LC_Condition::ToGenTree() assumes TYP_INT.

    LC_Array::ToGenTree() uses gtNewArrLen(TYP_INT,...), which is correct, but we need to re-insert the cast up to LONG if necessary.

  4. added
    Priority:2Work that is important, but not critical for the release
    on May 7, 2024
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

    Priority:2Work that is important, but not critical for the releasearea-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions