Uh oh!
There was an error while loading. Please reload this page.
RuntimeHelpers.CreateSpan<T> - #61079
Conversation
* Add non-intrinsic implementation for CreateSpan<T>. * Validate RVA field for Span<T> Co-authored-by: David Wrighton <davidwr@microsoft.com>
* Implement CreateSpan<T> intrinsic.
…t is legal for them to be of a primitive type. Fix the CreateSpan intrinsic to handle that case (dotnet#60525)
… does not support CreateSpan
ghost
commented
Nov 2, 2021
Note regarding the This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, to please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change. |
ghost
commented
Nov 2, 2021
I couldn't figure out the best area label to add to this PR. If you have write-permissions please help me learn by adding exactly one area label. |
davidwrighton
commented
Nov 2, 2021
@echesakov Could you help us out with the implementation of the intrinsic? I'd like to remove the hackathon comments and make the codegen as clean as possible. (Currently the codegen creates a ReadOnlySpan local, and then copies from that local whenever its used, but that seems like it should be fixable. Also, there are some hackathon comments in the IL intrinsic implementation that I'd like guidance on. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Co-authored-by: Jan Kotas <jkotas@microsoft.com>
Co-authored-by: Jan Kotas <jkotas@microsoft.com>
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
jaredpar
commented
Nov 2, 2021
Couple items I didn't see covered in the issue / PR
|
EgorBo
commented
Nov 2, 2021
Judging by the current implementation it throws ArgumentException for anything other than primitives and enums
Will it protect the API from passing structs with reference types inside? if not why bother 🙂 It needs a sort of "T : nogc" constraint, similar to T : unamanged |
jaredpar
commented
Nov 2, 2021
That is what
This is an argument I'm honestly having a hard time responding to. The goal of the typed languages is to move errors that occur at runtime to compile time. There is no path I'm aware of that would ever allow this to work with reference types hence why allow it as an argument when there is an easy option to stop it? |
jkotas
commented
Nov 2, 2021
The constraint does not add much here since this API can be only ever meaningfully used by compiler generated code. We can add it. It does not matter.
Yes, relaxing constraint is not considered a breaking change.
Yes, it is what runtime feature flags are for. |
EgorBo
commented
Nov 2, 2021
oops, yeah it makes my comments completely irrelevant, sorry for my 5 cents @jaredpar |
davidwrighton
commented
Nov 2, 2021
@jaredpar, @jkotas, @EgorBo I don't see why we would want to use a constraint here. We don't have the ability to actually express a useful constraint that actually covers the actual behavior, all usage of this api is actually compiler controlled, and while a struct or unmanaged constraint seems like it might make sense, I can also see counterexamples. For instance, what if we decided to come up with an encoding for a ReadOnlySpan that specified that the PE file would hold the data as a series of null terminated utf8 strings and make that useable from While not in the api proposal, violations of the preconditions are documented in the api comments (which will turn into documentation) to specify that violations will turn into |
jaredpar
commented
Nov 2, 2021
All of the intended usage is compiler controlled. There is nothing stopping users from calling this directly. I do understand and agree with the reasons of there isn't a perfect constraint here and hence we should do none. I would lean towards I don't buy the reasoning in this thread about "this is a compiler API" factoring into the decision though. It's an API, if we have the right tools we should be expressing the right contracts.
Sounds reasonable. |
Uh oh!
There was an error while loading. Please reload this page.
| public static ReadOnlySpan<T> CreateSpan<T>(RuntimeFieldHandle fldHandle) | ||
| { | ||
| unsafe | ||
| { | ||
| void* data = default; | ||
| int count = default; | ||
| GetSpanDataFrom(fldHandle, typeof(T).TypeHandle, &data, &count); | ||
| return new ReadOnlySpan<T>(data, count); | ||
| } | ||
| } |
There was a problem hiding this comment.
| publicstaticReadOnlySpan<T>CreateSpan<T>(RuntimeFieldHandlefldHandle) | |
| { | |
| unsafe | |
| { | |
| void*data=default; | |
| intcount=default; | |
| GetSpanDataFrom(fldHandle,typeof(T).TypeHandle,&data,&count); | |
| returnnewReadOnlySpan<T>(data,count); | |
| } | |
| } | |
| publicstaticunsafeReadOnlySpan<T>CreateSpan<T>(RuntimeFieldHandlefldHandle) | |
| =>newReadOnlySpan<T>(GetSpanDataFrom(fldHandle,typeof(T).TypeHandle,outintlength),length); |
Nit: You can make it one-liner if you change GetSpanDataFrom to return the pointer.
Uh oh!
There was an error while loading. Please reload this page.
| const char *rvaData; | ||
| if (!image_is_dynamic (m_class_get_image (klass))) | ||
| { |
There was a problem hiding this comment.
This and other places should use K&R indentation.
stephentoub
commented
Nov 30, 2021
Awesome to this go in! Is someone on point to follow-up on productizing the prototype on the Roslyn side of things? |
Implement
RuntimeHelpers.CreateSpan<T>#60948Implementation provides for