Skip to content

Conversation

xtqqczze
Copy link
Contributor

@xtqqczze xtqqczze commented Jan 4, 2025

We should use Unsafe.ReadUnaligned instead of Unsafe.As<T, ushort>(ref tmp) as the alignment of a type T may be less than its size.

Zero code generation diffs.

@xtqqczze xtqqczze force-pushed the use-unsafe-bitcast branch from 7ec2876 to 93e46f0 Compare January 4, 2025 23:12
@xtqqczze
Copy link
Contributor Author

xtqqczze commented Jan 5, 2025

@MihuBot

@xtqqczze xtqqczze changed the title Use Unsafe.BitCast to avoid taking address Use Unsafe.BitCast in SpanHelprs.Fill Jan 6, 2025
@xtqqczze xtqqczze changed the title Use Unsafe.BitCast in SpanHelprs.Fill Use Unsafe.BitCast in SpanHelpers.Fill Jan 9, 2025
@xtqqczze xtqqczze changed the title Use Unsafe.BitCast in SpanHelpers.Fill Refactor unsafe vector creation in SpanHelpers.Fill Feb 5, 2025
@xtqqczze xtqqczze marked this pull request as ready for review February 6, 2025 19:42
@xtqqczze xtqqczze changed the title Refactor unsafe vector creation in SpanHelpers.Fill Use Unsafe.ReadUnaligned for vector creation in SpanHelpers.Fill Feb 6, 2025
if (sizeof(T) == 1)
{
vector = new Vector<byte>(Unsafe.As<T, byte>(ref tmp));
vector = Vector.Create(Unsafe.As<T, byte>(ref tmp));
Copy link
Member

Choose a reason for hiding this comment

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

these should rather use Vector.LoadUnsafe(ref tmp).AsByte() or similar.

We should be explicit that its a load API and not do extra or other things that the JIT has to elide or reason about

Copy link
Member

Choose a reason for hiding this comment

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

Ah nevermind, I see the issue here. This is doing a broadcast and is reinterpreting the ref to a supported T

This pattern in general is not great and something that we probably should have a centralized helper or some other API around...

Copy link
Member

Choose a reason for hiding this comment

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

@xtqqczze could you please introduce such a helper (internal)? so it won't look like replacing one unsafe code with another

Copy link
Member

Choose a reason for hiding this comment

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

we probably should have a centralized helper or some other API around...

Is there another place where this helper can be used? It looks very specific to implementation of this method.

it won't look like replacing one unsafe code with another

This is trying to replace unsafe code with more correct and less dangerous unsafe code. I think this should just use Unsafe.BitCast: #116221.

If we factor this out to a helper, the helper is going to be as unsafe as Unsafe.BitCast, so still unsafe.

@stephentoub
Copy link
Member

Thanks for trying to improve this.

Given the conversation in #111091 (comment), I'm going to close this, and it can be revisited when such a helper exists.

@xtqqczze
Copy link
Contributor Author

xtqqczze commented Jun 7, 2025

Superseded by #116221.

@xtqqczze xtqqczze deleted the use-unsafe-bitcast branch June 7, 2025 15:52
@github-actions github-actions bot locked and limited conversation to collaborators Jul 8, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Memory 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.

6 participants