Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

EgorBo
Copy link
Member

@EgorBo EgorBo commented Feb 3, 2024

This PR does:

  1. Removes bound checks for Redundant bound check "(uint)c > 'f' ? 0xFF : CharToHexLookup[c]" #81160 (closes it)
  2. Improves assertions around MOD/UMOD, e.g:
static int Test(int x)
{
    if ((uint)x < 100)
        return x % 8; // x is proven to be never-negative, can be optimized
    return 0;
}

Codegen diff:

; Method Program:Test(int):int (FullOpts)
G_M46009_IG01:
G_M46009_IG02:
       xor      eax, eax
       mov      edx, ecx
-      sar      edx, 31
       and      edx, 7
-      add      edx, ecx
-      and      edx, -8
-      mov      r8d, ecx
-      sub      r8d, edx
       cmp      ecx, 100
-      cmovb    eax, r8d
+      cmovb    eax, edx
G_M46009_IG03:
       ret      
-; Total bytes of code: 29
+; Total bytes of code: 14

@ghost ghost added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Feb 3, 2024
@ghost ghost assigned EgorBo Feb 3, 2024
@ghost
Copy link

ghost commented Feb 3, 2024

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.

Issue Details

This PR does:

  1. Removes bound checks for Redundant bound check "(uint)c > 'f' ? 0xFF : CharToHexLookup[c]" #81160 (Closes Redundant bound check "(uint)c > 'f' ? 0xFF : CharToHexLookup[c]" #81160)
  2. Improves assertions around MOD/UMOD, e.g:
static int Test(int x)
{
    if ((uint)x < 100)
        return x % 8;
    return 0;
}

Codegen diff:

; Method Program:Test(int):int (FullOpts)
G_M46009_IG01:
G_M46009_IG02:
       xor      eax, eax
       mov      edx, ecx
-      sar      edx, 31
       and      edx, 7
-      add      edx, ecx
-      and      edx, -8
-      mov      r8d, ecx
-      sub      r8d, edx
       cmp      ecx, 100
-      cmovb    eax, r8d
+      cmovb    eax, edx
G_M46009_IG03:
       ret      
-; Total bytes of code: 29
+; Total bytes of code: 14
Author: EgorBo
Assignees: -
Labels:

area-CodeGen-coreclr

Milestone: -

@EgorBo EgorBo marked this pull request as ready for review February 3, 2024 20:08
@EgorBo
Copy link
Member Author

EgorBo commented Feb 3, 2024

Diffs

PTAL @AndyAyersMS @jakobbotsch @dotnet/jit-contrib

// Note this tracks at most only 256 assertions.
//
static const AssertionIndex countFunc[] = {64, 128, 256, 64};
static const AssertionIndex countFunc[] = {64, 128, 256, 128, 64};
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Allowed a bit more assertions for large methods - it was done mainly to fix asm regressions introduced by new asserts where old ones were thrown away due to this limit.

// Cases where op1 holds the upper bound arithmetic and op2 is 0.
// Loop condition like: "i < bnd +/-k == 0"
// Assertion: "i < bnd +/- k == 0"
if (hasTestAgainstZero && vnStore->IsVNCompareCheckedBoundArith(op1VN))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

testAgainstZero asserts have no diffs (x relop y) ==/!= 0. All asserts are just x relop y.
So I removed them.

{
// (uint)index < CNS
// (uint)index >= CNS
return (funcApp.m_func == VNF_LT_UN) || (funcApp.m_func == VNF_GE_UN);
Copy link
Member Author

@EgorBo EgorBo Feb 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IsVNConstantBoundUnsigned was a bit conservative and only tried to detect useful cases. But it forgot that we create complimentary asserts as well which may turn useless ones into useful too.

// OAK_[NOT]_EQUAL assertion with op1 being O1K_CONSTANT_LOOP_BND
// representing "(X relop CNS) ==/!= 0" assertion.
if (!curAssertion->IsConstantBound())
if (!curAssertion->IsConstantBound() && !curAssertion->IsConstantBoundUnsigned())
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here for optAssertionProp_ModDiv I added IsConstantBoundUnsigned (unsigned comparisons against constants).

Previously we could optimize div only for x > cns assert. This adds (uint)x < cns case as well.

@EgorBo
Copy link
Member Author

EgorBo commented Feb 3, 2024

Btw, just FYI: Diffs if we bump max assertions threshold to just 256.

@jakobbotsch
Copy link
Member

/azp run Fuzzlyn

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@EgorBo EgorBo merged commit 64822a6 into dotnet:main Feb 5, 2024
@EgorBo EgorBo deleted the assertprop-2 branch February 5, 2024 11:10
@EgorBo
Copy link
Member Author

EgorBo commented Feb 5, 2024

CI Failure is known

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.

2 participants