runtime icon indicating copy to clipboard operation
runtime copied to clipboard

RyuJIT reassociates expressions in a way that sometimes prevents LICM

Open mikedn opened this issue 10 years ago • 11 comments

Consider the following sample

static int Test(int x1, int y1, int x2, int y2)
{
    int sum = 0;

    for (int i = 0; i < 1000; i++)
        sum += x1 * y1 + x2 * y2;

    return sum;
}

The RHS of += is invariant but JIT's fgMoveOpsLeft changes the code to sum = (sum + x1 * y1) + x2 * y2 and generates the following code:

G_M4316_IG02:
       33C0                 xor      eax, eax
       4533D2               xor      r10d, r10d
       0FAFD1               imul     edx, ecx
       418BC8               mov      ecx, r8d
       410FAFC9             imul     ecx, r9d

G_M4316_IG03:
       448D040A             lea      r8d, [rdx+rcx]
       4103C0               add      eax, r8d
       41FFC2               inc      r10d
       4181FAE8030000       cmp      r10d, 0x3E8
       7CED                 jl       SHORT G_M4316_IG03

Usually such reassociation is a good thing because it minimizes register usage but in loops it can have the opposite side effect.

category:cq theme:loop-opt skill-level:intermediate cost:medium

mikedn avatar Feb 13 '16 17:02 mikedn

@dotnet/jit-contrib discuss

RussKeldorph avatar Feb 15 '16 04:02 RussKeldorph

LICH should be setting value number on sub-expr [x1 * y1 + x2 * y2] regardless of sum being on lhs and determine that sub-expr to be loop-invariant and hoist it.

choikwa avatar Jul 06 '16 17:07 choikwa

LICH should be setting value number on sub-expr [x1 * y1 + x2 * y2] regardless of sum being on lhs and determine that sub-expr to be loop-invariant and hoist it.

Except that VN can't do that because there's no x1 * y1 + x2 * y2 sub-expression to begin with.

mikedn avatar Jul 06 '16 17:07 mikedn

Ok I see why you made it the issue. Even still, it should have caught x1 * y1 and x2 * y2 sub-exprs since they don't involve induction var sum. This points to a smell in LICH.

choikwa avatar Jul 06 '16 18:07 choikwa

Even still, it should have caught x1 * y1 and x2 * y2 sub-exprs since they don't involve induction var sum

It does that already, it's the addition that isn't hoisted.

mikedn avatar Jul 06 '16 18:07 mikedn

:( It appears that I can't read asm. @mikedn, is shuffling expr to isolate induction vars a possible remedy?

choikwa avatar Jul 06 '16 18:07 choikwa

is shuffling expr to isolate induction vars a possible remedy

Something like that. Usually terms are grouped together depending on where their SSA definitions are in the loop tree. Unlike "isolate induction vars" this also takes care of nested loops.

mikedn avatar Jul 06 '16 18:07 mikedn

@mikedn yeah, based on the analysis in the issue mentioned. LICH does hoisting of the subtree to the right of the assignment. It creates the unused tree in the loop pre-head for that.

if then cse is supposed to understand that the expressions in the pre-head and the original one are the same, then it fails to do anything with it anyway.

papaslavik avatar Jul 13 '16 10:07 papaslavik

Even for such a case you won't get anything good in either CSE or LICH:

       for (int i = 0; i < b; ++i) {
          int res1 = (b + d + e) + (b + d + e);
          res += res1;
        }

The ARM code for the loop:

G_M10576_IG03:
000042  EB03 0E01      add     lr, r3, r1
000046  44A6           add     lr, r4
000048  449E           add     lr, r3
00004A  448E           add     lr, r1
00004C  44A6           add     lr, r4
00004E  4470           add     r0, lr
000050  3201           adds    r2, 1
000052  429A           cmp     r2, r3
000054  DBF5           blt     SHORT G_M10576_IG03

papaslavik avatar Jul 13 '16 10:07 papaslavik

int res1 = (b + d + e) + (b + d + e);

AFAIR adding parenthesis won't do anything, the JIT re-associates anyway.

mikedn avatar Jul 13 '16 11:07 mikedn

Suspect this won't make 7.0 so moving to future.

AndyAyersMS avatar Apr 28 '22 18:04 AndyAyersMS