Skip to content

Conversation

@EgorBo
Copy link
Member

@EgorBo EgorBo commented Jan 11, 2025

Closes #111304

static ulong Foo1(int x)
{
    if ((uint)x < 10)
    {
        return (ulong)x << 60;
    }
    return 0;
}
; Method Bench:Foo1(int):ulong (FullOpts)
       xor      eax, eax
-      movsxd   rdx, ecx
+      mov      edx, ecx
       shl      rdx, 60
       cmp      ecx, 10
       cmovb    rax, rdx
       ret      
-; Total bytes of code: 17
+; Total bytes of code: 16

@ghost ghost added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Jan 11, 2025
@EgorBo
Copy link
Member Author

EgorBo commented Jan 11, 2025

So this optimization just repeats what we do in morph, but this time with help of global assertions.

Diffs - no size improvements on arm64 due to fixed size encoding, but perf wise mov should be better than sxtw (the latter can't be handled via register renaming, also, on some hw it has worse TP).

cc @dotnet/jit-contrib

@xtqqczze
Copy link
Contributor

@MihuBot

@xtqqczze
Copy link
Contributor

@EgorBo Will this enable the removal of tricks to force zero-extension like the following:

return new Span<T>(ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(array), (nint)(uint)start /* force zero-extension */), array.Length - start);

@EgorBo
Copy link
Member Author

EgorBo commented Jan 12, 2025

@EgorBo Will this enable the removal of tricks to force zero-extension like the following:

return new Span<T>(ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(array), (nint)(uint)start /* force zero-extension */), array.Length - start);

I would expect so

@EgorBo EgorBo merged commit dde00d3 into dotnet:main Jan 16, 2025
109 checks passed
@EgorBo EgorBo deleted the jit-make-cast-unsigned-assertprop branch January 16, 2025 12:36
@xtqqczze
Copy link
Contributor

@EgorBo Will this enable the removal of tricks to force zero-extension like the following:

return new Span<T>(ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(array), (nint)(uint)start /* force zero-extension */), array.Length - start);

I would expect so

@EgorBo Not quite: https://csharp.godbolt.org/z/fq15ch7EM

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JIT: Redundant sign extension for checked bound

3 participants