Skip to content

Loop cloning can't eliminate bounds check when assigning to multi-dim jagged array #54074

@BruceForstall

Description

@BruceForstall

The core of the src\tests\JIT\Performance\CodeQuality\Benchstones\BenchI\Array2\Array2.cs benchmark is this loop:

for (; loop != 0; loop--) {
    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
            for (int k = 0; k < 10; k++) {
                d[i][j][k] = s[i][j][k];
            }
        }
    }
}

Some observations:

  1. The inner three loops are all loop invariant w.r.t. the outer loop. Can't we just eliminate the outer loop?
  2. The outer loop is not cloned because it is not in the canonical form accepted by loop cloning -- "for (var=init; var < limit; var++". The i and j loops are cloned. The inner k loop is not cloned. Why?
  3. Without cloning, there are 5 bounds checks in the inner loop; 1 bounds check get hoisted. With cloning, there are 2 bounds checks in the inner loop; the rest become cloned loop pre-conditions.
    • If the inner two loops are also cloned, can we eliminate all the bounds checks?
    • The 1st and 2nd cloned loops appear to have duplicate cloning conditions (i.e., it appears they check the same array bounds multiple times
    • In the non-cloned case, why can't we hoist more checks?

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-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