Skip to content

Conversation

@xtqqczze
Copy link
Contributor

@xtqqczze xtqqczze commented Jul 11, 2024

No description provided.

@ghost ghost added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Jul 11, 2024
xtqqczze

This comment was marked as duplicate.

@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Jul 11, 2024
@xtqqczze
Copy link
Contributor Author

@MihuBot

if (BitConverter.IsLittleEndian)
{
MemoryMarshal.TryWrite(g, in this);
Unsafe.WriteUnaligned(ref MemoryMarshal.GetArrayDataReference(array), this);
Copy link
Member

Choose a reason for hiding this comment

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

does it make it any better? seems to be more verbose + bonus points for extra "Unsafe" word.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@EgorBo updated description with a diff showing bounds check elimination

Copy link
Member

Choose a reason for hiding this comment

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

I think it's better to extract the problematic part and file as an issue against JIT rather than workarounding it

Copy link
Member

Choose a reason for hiding this comment

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

MemoryMarshal.TryWrite into a newly created new byte[16] shouldn't produce any checks, if it does - jit should be improved.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The duplicate bounds check goes away with the addition of a uint cast here:

@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-runtime
See info in area-owners.md if you want to be subscribed.

@xtqqczze
Copy link
Contributor Author

Reverted the Unsafe changes in favour of changing MemoryMarshal.TryWrite to Write and adding uint casts on span length checks.

@xtqqczze
Copy link
Contributor Author

@MihuBot

public bool TryWriteBytes(Span<byte> destination)
{
if (destination.Length < 16)
if ((uint)destination.Length < 16)
Copy link
Member

Choose a reason for hiding this comment

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

Jit definitely shouldn't need this, it should be aware that Span.Length is never negative

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is an unsigned length check in MemoryMarshal.Write:

The JIT won't elide the check in the case where one length check is signed, the other unsigned:

https://csharp.godbolt.org/z/WTj89j5Mn

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The check in MemoryMarshal.Write could be made signed, but then there could be a problem calling Write with a sliced span, due to the unsigned conditionals in Span.Slice.

Copy link
Member

@EgorBo EgorBo Jul 23, 2024

Choose a reason for hiding this comment

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

I don't see bound checks in your godbolt link, I do see two similar conditions that jit didn't remove - it's a different issue that we should also fix. Like I said previously, if JIT fails to fix an obvious case - we should file an issue instead of applying hacks in the codebase. These hacks still make sense for cases where JIT likely has no ability to know that certain external argument are never negative.

Copy link
Member

Choose a reason for hiding this comment

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

I don't see bound checks

What I mean is that we currently have two different algorithms for bound checks specifically and a general redundant branch removal

Copy link
Member

Choose a reason for hiding this comment

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

@AndyAyersMS you might be interested in the godbolt link above (https://csharp.godbolt.org/z/WTj89j5Mn) for RBO/JumpThreading missing opportunity

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There are similar conditions in Guid.TryWriteBytes and MemoryMarshal.Write, but no bounds checks per se .

Copy link
Member

Choose a reason for hiding this comment

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

Btw, we have an issue to remove existing (uint) hacks - #67044 so we shouldn't do the opposite

Copy link
Contributor Author

@xtqqczze xtqqczze Jul 23, 2024

Choose a reason for hiding this comment

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

Btw, we have an issue to remove existing (uint) hacks - #67044 so we shouldn't do the opposite

xtqqczze@1520741

@dotnet-policy-service
Copy link
Contributor

Draft Pull Request was automatically closed for 30 days of inactivity. Please let us know if you'd like to reopen it.

@github-actions github-actions bot locked and limited conversation to collaborators Sep 22, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-System.Runtime community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants