Skip to content

Commit

Permalink
JIT: Advance cursors through stores in strength reduction (dotnet#105267
Browse files Browse the repository at this point in the history
)

This allows us to look through CSE'd locals when an IV was CSE'd.

Example diff:
```csharp
private static void CopyInts(int[] arr1, int[] arr2)
{
    if (arr2.Length != arr1.Length)
        return;

    for (int i = 0; i < arr1.Length; i++)
    {
        arr2[i] = arr1[i];
    }
}
```

```diff
@@ -1,14 +1,16 @@
 G_M55903_IG03:  ;; offset=0x000C
-       xor      eax, eax
        test     r8d, r8d
-       jle      SHORT G_M55903_IG05
-						;; size=7 bbWeight=0.50 PerfScore 0.75
+       jle      SHORT G_M55903_IG06
+						;; size=5 bbWeight=0.50 PerfScore 0.62
 
-G_M55903_IG04:  ;; offset=0x0013
-       mov      r10d, eax
-       mov      r9d, dword ptr [rcx+4*r10+0x10]
-       mov      dword ptr [rdx+4*r10+0x10], r9d
-       inc      eax
-       cmp      r8d, eax
-       jg       SHORT G_M55903_IG04
-						;; size=20 bbWeight=3.96 PerfScore 18.81
+G_M55903_IG04:  ;; offset=0x0011
+       mov      eax, 16
+						;; size=5 bbWeight=0.25 PerfScore 0.06
+
+G_M55903_IG05:  ;; offset=0x0016
+       mov      r10d, dword ptr [rcx+rax]
+       mov      dword ptr [rdx+rax], r10d
+       add      rax, 4
+       dec      r8d
+       jne      SHORT G_M55903_IG05
+						;; size=17 bbWeight=3.96 PerfScore 17.82
```

arm64 especially benefits from strength reduction, e.g. a benchmark for the above:

```
BenchmarkDotNet v0.13.12, Ubuntu 22.04.4 LTS (Jammy Jellyfish)
Unknown processor
  Job-GXMAGJ : .NET 9.0.0 (42.42.42.42424), Arm64 RyuJIT AdvSIMD
  Job-MLUOLU : .NET 9.0.0 (42.42.42.42424), Arm64 RyuJIT AdvSIMD
```

| Method   | Toolchain               | Mean     | Error     | Ratio |
|--------- |------------------------ |---------:|----------:|------:|
| CopyInts | Main | 6.683 μs | 0.0010 μs |  1.00 |
| CopyInts | PR | 5.014 μs | 0.0003 μs |  0.75 |
  • Loading branch information
jakobbotsch authored Oct 15, 2024
1 parent 8ef27e2 commit 87a35d6
Show file tree
Hide file tree
Showing 2 changed files with 211 additions and 51 deletions.
2 changes: 1 addition & 1 deletion src/coreclr/jit/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -7698,7 +7698,7 @@ class Compiler
BasicBlock* exiting,
LoopLocalOccurrences* loopLocals);
bool optCanAndShouldChangeExitTest(GenTree* cond, bool dump);
bool optPrimaryIVHasNonLoopUses(unsigned lclNum, FlowGraphNaturalLoop* loop, LoopLocalOccurrences* loopLocals);
bool optLocalHasNonLoopUses(unsigned lclNum, FlowGraphNaturalLoop* loop, LoopLocalOccurrences* loopLocals);

bool optWidenIVs(ScalarEvolutionContext& scevContext, FlowGraphNaturalLoop* loop, LoopLocalOccurrences* loopLocals);
bool optWidenPrimaryIV(FlowGraphNaturalLoop* loop,
Expand Down
Loading

0 comments on commit 87a35d6

Please sign in to comment.