Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues with the Crc32 intrinsic in the Jit #62692

Closed
SingleAccretion opened this issue Dec 12, 2021 · 6 comments · Fixed by #73005
Closed

Issues with the Crc32 intrinsic in the Jit #62692

SingleAccretion opened this issue Dec 12, 2021 · 6 comments · Fixed by #73005
Assignees
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI bug
Milestone

Comments

@SingleAccretion
Copy link
Contributor

SingleAccretion commented Dec 12, 2021

1): The assert here is wrong because it does not account for small types. Reproduction:

static uint Problem(byte* pSrc, uint data)
{
    return Sse42.Crc32(*pSrc, data);
}

2) The same assert is also wrong because in lowering we purposefully remove narrowing long -> int casts to avoid generating redundant movs. Reproduction:

static uint Problem(uint crc, ulong data)
{
    return Sse42.Crc32(crc, (uint)(data >> 16));
}

3) The code in lowering is wrong because it can discard meaningful casts. Reproduction (data here will not be zero-extended):

static uint Problem(uint crc, uint data)
{
    return Sse42.Crc32(crc, (uint)(byte)data);
}

4) The code in lowering is also wrong because it can discard long -> int casts on x86, and end up with a GT_LONG operand, which is not handled downstream. Reproduction, run on x86:

[MethodImpl(MethodImplOptions.NoOptimization)]
static uint Problem(uint crc, ulong data)
{
    return Sse42.Crc32(crc, (uint)(data >> 16));
}

5) The code in lowering is also wrong because it can discard double/float -> int casts. Applies to all use sites of TryRemoveCastIfPresent. Reproduction:

static uint Problem(uint crc, double data)
{
    return Sse42.Crc32(crc, (uint)data);
}

Curiously enough, if we disable asserts, this will happily emit crc32 eax, xmm1.

6) The code in lowering is also wrong because it can discard checked casts. Applies to all use sites of TryRemoveCastIfPresent. Reproduction (observe the generated code will have no check):

static uint Problem(uint crc, int data)
{
    return Sse42.Crc32(crc, checked((uint)data));
}
@dotnet-issue-labeler dotnet-issue-labeler bot added area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI untriaged New issue has not been triaged by the area owner labels Dec 12, 2021
@ghost
Copy link

ghost commented Dec 12, 2021

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

Issue Details

1): The assert here is wrong because it does not account for small types. Reproduction:

static uint Problem(byte* pSrc, uint data)
{
    return Sse42.Crc32(*pSrc, data);
}

2) The same assert is also wrong because in lowering we purposefully remove narrowing long -> int casts to avoid generating redundant movs. Reproduction:

static uint Problem(uint crc, ulong data)
{
    return Sse42.Crc32(crc, (uint)(data >> 16));
}

3) The code in lowering is also wrong because it can discard meaningful casts. Reproduction (data here will not be zero-extended):

static uint Problem(uint crc, uint data)
{
    return Sse42.Crc32(crc, (uint)(byte)data);
}
Author: SingleAccretion
Assignees: -
Labels:

area-CodeGen-coreclr, untriaged

Milestone: -

@EgorBo EgorBo added this to the 7.0.0 milestone Dec 12, 2021
@EgorBo EgorBo removed the untriaged New issue has not been triaged by the area owner label Dec 12, 2021
@EgorBo
Copy link
Member

EgorBo commented Dec 12, 2021

The LSRA assert is the 4th case?

@SingleAccretion
Copy link
Contributor Author

Yep.

@deeprobin
Copy link
Contributor

@EgorBo Do you know approximately when you're going to get around to looking at this issue?

It's probably nothing big, but the issue is blocking me (but nothing critical) :)

@EgorBo
Copy link
Member

EgorBo commented Jul 28, 2022

Looking at it now, sorry for the delay

@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Jul 28, 2022
@deeprobin
Copy link
Contributor

Looking at it now, sorry for the delay

no problem. thanks

EgorBo added a commit that referenced this issue Jul 29, 2022
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Jul 29, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Aug 29, 2022
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 bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants