You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Redesign Block support to make it work with UnmanagedCallersOnlyAttribute #15783
dotnetAn issue or pull request related to .NETenhancementThe issue or pull request is an enhancementfeatureA feature to be implementedperformanceIf an issue or pull request is related to performancerequest-for-commentsThe issue is a suggested idea seeking feedback
We need to get the signature of the trampoline at runtime (to compute the objc encoding of the signature, which we have to include in the block). We can't reflect on the function pointer (it's just an IntPtr), so we need to pass the MethodInfo of the trampoline method (we can't create a delegate either, because you can't create a delegate of an [UnmanagedCallersOnly] function). In the code sample I'm passing a type and the method name, which turned out slightly smaller than getting the MethodInfo using reflection in code creating the block, but it's not much of a difference:
The linker will compute the signature and replace the reflection with a plain string (of the signature) - this has not been implemented in the POC (it would have to be for a production fix).
How gross are we allowed to get in the code? Like, can we assume that a C# delegate is just a struct with two machine pointers in it and pass that through along with the delegate type then on the receiving end of the trampoline hard cast it back and invoke it? I feel like I did something like this in BTfS.
How gross are we allowed to get in the code? Like, can we assume that a C# delegate is just a struct with two machine pointers in it and pass that through along with the delegate type then on the receiving end of the trampoline hard cast it back and invoke it? I feel like I did something like this in BTfS.
Ideally the code would be nice, because it'll be how users use blocks (which isn't very common, but once in a while it happens). Also, we can't use a delegate to pass the signature, because you can't create a delegate of a method with an [UnmanagedCallersOnly] attribute.
dotnetAn issue or pull request related to .NETenhancementThe issue or pull request is an enhancementfeatureA feature to be implementedperformanceIf an issue or pull request is related to performancerequest-for-commentsThe issue is a suggested idea seeking feedback
Activity
rolfbjarne commentedon Oct 19, 2022
Tentative implementation: rolfbjarne@3139dfb
Example update: rolfbjarne@5f41593
This is the important part:
it's a bit more verbose than I wished, but I don't think there's a way around that:
There's no way to convert a function to a function pointer using the unmanaged signature anywhere.
So either this:
or:
Both are kind of ugh.
We need to get the signature of the trampoline at runtime (to compute the objc encoding of the signature, which we have to include in the block). We can't reflect on the function pointer (it's just an IntPtr), so we need to pass the MethodInfo of the trampoline method (we can't create a delegate either, because you can't create a delegate of an [UnmanagedCallersOnly] function). In the code sample I'm passing a type and the method name, which turned out slightly smaller than getting the MethodInfo using reflection in code creating the block, but it's not much of a difference:
The linker will compute the signature and replace the reflection with a plain string (of the signature) - this has not been implemented in the POC (it would have to be for a production fix).
rolfbjarne commentedon Feb 8, 2023
If dotnet/designs#282 is ever approved and implemented, a minor improvement might be something like this:
stephen-hawley commentedon Feb 13, 2023
How gross are we allowed to get in the code? Like, can we assume that a C# delegate is just a struct with two machine pointers in it and pass that through along with the delegate type then on the receiving end of the trampoline hard cast it back and invoke it? I feel like I did something like this in BTfS.
rolfbjarne commentedon Feb 14, 2023
Ideally the code would be nice, because it'll be how users use blocks (which isn't very common, but once in a while it happens). Also, we can't use a delegate to pass the signature, because you can't create a delegate of a method with an [UnmanagedCallersOnly] attribute.
dalexsoto commentedon Feb 14, 2023
from the options to step 1 I think I lean more for
less parenthesis heh, it feels just slightly more readable to me but just slightly 😃 the rest 👍
rolfbjarne commentedon Feb 17, 2023
It seems we'll be able to do something like this in the future:
which, together with the function pointer type change, could become:
Ref: dotnet/csharplang#4284
[src] Make BlockLiteral disposable and update usage accordingly. (#17597
[generator] Use the new Block API that makes BlockLiteral disposable.
[src] Refactor block code to use blittable callbacks.
2 remaining items
[ImageIO] Refactor block code to use blittable callbacks.
[ImageIO] Refactor block code to use blittable callbacks.
[ImageIO] Refactor block code to use blittable callbacks. (#17654)
Add support for function pointers to BlockLiteral. (#17672)
[generator] Generate block callback signatures using only blittable t…
[generator] Generate block callback signatures using only blittable t…
[generator] Generate block callback signatures using only blittable t…
[generator] Generate block callback signatures using only blittable t…
[generator] Make the block callbacks UnmanagedCallersOnly functions i…
[generator] Make the block callbacks UnmanagedCallersOnly functions i…
[tests] Add new cecil tests to verify correct block literal usage in …
[tests] Add new cecil tests to verify correct block literal usage in …