Skip to content

JIT: Block clonning for subsequent bounds checks #112524

@EgorBo

Description

@EgorBo

If we have:

void Test(int[] arr, int i)
{
    arr[i + 0] = 0;
    arr[i + 1] = 0;
    arr[i + 2] = 0;
    arr[i + 3] = 0;
}

Which generates a single BasicBlock with either multiple statements or a single statement with multiple COMMAs containing STOREIND node, we should be able to just clone it and remove all bound checks in the fast path:

public void Test(int[] arr, int i)
{
    if (i >= 0 && i < arr.Length - 3)
    {
        // Fast path: no bounds checks
        arr[i + 0] = 0;
        arr[i + 1] = 0;
        arr[i + 2] = 0;
        arr[i + 3] = 0;
    }
    else
    {
        // Slow fallback
        arr[i + 0] = 0;
        arr[i + 1] = 0;
        arr[i + 2] = 0;
        arr[i + 3] = 0;
    }
}

So, essentially, a loop clonning but for non-loop stuff.

cc @dotnet/jit-contrib opinions? such patterns aren't uncommon in BCL

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIin-prThere is an active PR which will close this issue when it is merged

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions