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

Mark some structs as byreflike #104870

Closed
wants to merge 3 commits into from
Closed

Conversation

EgorBo
Copy link
Member

@EgorBo EgorBo commented Jul 14, 2024

Just a CI codegen test

@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Jul 14, 2024
@EgorBo
Copy link
Member Author

EgorBo commented Jul 14, 2024

@MihuBot

@@ -654,7 +654,7 @@ internal unsafe bool WriteEvent(ref EventDescriptor eventDescriptor, IntPtr even
}

/// <summary>Workaround for inability to stackalloc object[EtwAPIMaxRefObjCount == 8].</summary>
private struct EightObjects
private ref struct EightObjects
{
internal object? _arg0;
Copy link
Member

Choose a reason for hiding this comment

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

This should be InlineArray. Can InlineArrays be ref structs?

Copy link
Contributor

@hez2010 hez2010 Jul 14, 2024

Choose a reason for hiding this comment

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

It would result in a warning:

warning CS9184: 'Inline arrays' language feature is not supported for an inline array type that is not valid as a type argument, or has element type that is not valid as a type argument.

IMO this diagnostic message is out-of-date as in C# 13 we have added the support for byref-like type arguments. /cc: @jaredpar

Copy link
Member

Choose a reason for hiding this comment

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

@AaronRobinsonMSFT is this supported in the runtime?

Copy link
Member

Choose a reason for hiding this comment

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

This should be InlineArray. Can InlineArrays be ref structs?

Yes.

@AaronRobinsonMSFT is this supported in the runtime?

As in, can ref structs be marked as InlineArray or something else? If the former, see above.

Copy link
Member Author

Choose a reason for hiding this comment

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

Applied InlineArray with #pragma warning disable CS9184

Copy link
Member

Choose a reason for hiding this comment

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

Is this one of the cases though where we have explicit guidance that Unsafe.As is safe to use in the compiler? Basically is @jkotas signing off that use extending Unsafe.As where the source is a ref struct in the InlineArray case is supported?

Copy link
Member

Choose a reason for hiding this comment

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

extending Unsafe.As where the source is a ref struct in the InlineArray case is supported

Yes, that's fine. It does not fundamentally change the special sequence to create Span from InlineArray that has to work forever.

(I would have preferred if that special sequence did not use Unsafe.As - dotnet/csharplang#7064 (comment) , but that ship sailed.)

Copy link
Member

Choose a reason for hiding this comment

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

Is that a place where we could define a new runtime helper method and adjust compiler to use that going forward? Won't fix the existing debt but going forward if there was a better helper to use we could move to it.

Copy link
Member

Choose a reason for hiding this comment

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

Something like:

public class RuntimeHelpers
{
    public static Span<Element> InlineArrayAsSpan<InlineArray, Element>(ref InlineArray inlineArray) where InlineArray: allows ref struct, Element: allows ref struct;
}

? I agree that it looks better than the current solution with Unsafe.As. It is definitely possible; it is a bunch of work to teach all codegens to expand it as intrinsic or throw exception if there is any sort of mismatch.

Copy link
Member

Choose a reason for hiding this comment

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

Opened #105586

@EgorBo EgorBo force-pushed the ref-struct-test branch from 4519def to 31f5850 Compare July 14, 2024 22:10
@EgorBo EgorBo marked this pull request as ready for review July 15, 2024 10:15
@EgorBo
Copy link
Member Author

EgorBo commented Jul 15, 2024

@jkotas can we merge as is then? I couldn't find many good candidates with codegen improvements (I wrote an adhoc roslyn analyzer to find them), diffs: MihuBot/runtime-utils#525 removed a few write barriers

@@ -2258,7 +2258,7 @@ public static bool TrueForAll<T>(T[] array, Predicate<T> match)
0X7FFFFFC7;

// Private value type used by the Sort methods.
private readonly struct SorterObjectArray
private readonly ref struct SorterObjectArray
Copy link
Member

Choose a reason for hiding this comment

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

Is it worth adding a comment about why ref exists, here and elsewhere? e.g.

Suggested change
private readonly ref struct SorterObjectArray
private readonly ref struct SorterObjectArray // ref isn't functionally necessary but enables additional JIT optimization

It's a little unfortunate we're going to start to see this sprinkled around. Is there anything more we could do in the JIT to effectively infer it?

Copy link
Member Author

@EgorBo EgorBo Jul 15, 2024

Choose a reason for hiding this comment

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

Yeah a comment makes sense.

It's a little unfortunate we're going to start to see this sprinkled around.

I don't see a difference with other things we sprinkle around like marking classes as sealed, etc 🤷

Is there anything more we could do in the JIT to effectively infer it?

Only NativeAOT might I guess..

Copy link
Member

@stephentoub stephentoub Jul 15, 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 a difference with other things we sprinkle around like marking classes as sealed, etc 🤷

Sealed says "this type wasn't designed to be derived from, please think really hard before you remove the sealed as usage might not expect it"; it has use separate from the perf angle, in fact most of the perf angle came long after it was introduced. Making these types ref doesn't IMO have any such design benefit; it's being done solely to get a JIT optimization to kick in. You typically only make a type ref out of necessity because it needs to contain something ref.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok, you're right - I don't have a strong opinion on this, I was just testing my script and hoped to see some nice codegen diffs, but they're quite small

@EgorBo EgorBo closed this Jul 15, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Aug 27, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants