Skip to content

Redesign Block support to make it work with UnmanagedCallersOnlyAttribute #15783

Closed

Description

@rolfbjarne
  • Add support to the BlockLiteral struct to accept function pointers.
  • Add support to the generator for the new block implementation.
  • Add support to our optimizations to handle the new block implementation.
  • Update all our manual binding code to use the new block implementation.

Ref: #10470 (comment)

Ideally any changes would also cover (which I've closed as duplicates of this issue):

Activity

  1. added
    enhancement
    performance
    dotnet
    feature
    on Aug 26, 2022
  2. added this to the .NET 8 milestone on Aug 26, 2022
  3. rolfbjarne commented on Oct 19, 2022

    @rolfbjarne
    MemberAuthor

    Tentative implementation: rolfbjarne@3139dfb
    Example update: rolfbjarne@5f41593

    This is the important part:

    unsafe {
    	var trampoline = (delegate* unmanaged<IntPtr, IntPtr, byte, byte>) &TrampolineRegistrationHandlerCallback;
    	using var block = new BlockLiteral (trampoline, registrationHandler, typeof (CTFontManager), nameof (TrampolineRegistrationHandlerCallback));
    	CTFontManagerRegisterFontURLs (arr.Handle, scope, enabled, &block);
    }

    it's a bit more verbose than I wished, but I don't think there's a way around that:

    1. There's no way to convert a function to a function pointer using the unmanaged signature anywhere.

      So either this:

      var trampoline = (delegate* unmanaged<IntPtr, IntPtr, byte, byte>) &TrampolineRegistrationHandlerCallback;

      or:

      delegate* unmanaged<IntPtr, IntPtr, byte, byte> trampoline = &TrampolineRegistrationHandlerCallback;

      Both are kind of ugh.

    2. 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:

      unsafe {
      	var trampoline = (delegate* unmanaged<IntPtr, IntPtr, byte, byte>) &TrampolineRegistrationHandlerCallback;
      	using var block = new BlockLiteral (trampoline, registrationHandler, typeof (CTFontManager).GetMethod (nameof (TrampolineRegistrationHandlerCallback), BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
      	CTFontManagerRegisterFontURLs (arr.Handle, scope, enabled, &block);
      }

      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).

  4. rolfbjarne commented on Feb 8, 2023

    @rolfbjarne
    MemberAuthor

    If dotnet/designs#282 is ever approved and implemented, a minor improvement might be something like this:

    unsafe {
    	var trampoline = (delegate* unmanaged<IntPtr, IntPtr, byte, byte>) &TrampolineRegistrationHandlerCallback;
        var trampolineType = typeof (delegate* unmanaged<IntPtr, IntPtr, byte, byte>);
    	using var block = new BlockLiteral (trampoline, registrationHandler, trampolineType);
    	CTFontManagerRegisterFontURLs (arr.Handle, scope, enabled, &block);
    }
  5. added
    request-for-comments
    on Feb 13, 2023
  6. stephen-hawley commented on Feb 13, 2023

    @stephen-hawley
    Contributor

    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.

  7. rolfbjarne commented on Feb 14, 2023

    @rolfbjarne
    MemberAuthor

    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.

  8. dalexsoto commented on Feb 14, 2023

    @dalexsoto
    Member

    from the options to step 1 I think I lean more for

    delegate* unmanaged<IntPtr, IntPtr, byte, byte> trampoline = &TrampolineRegistrationHandlerCallback;

    less parenthesis heh, it feels just slightly more readable to me but just slightly 😃 the rest 👍

  9. rolfbjarne commented on Feb 17, 2023

    @rolfbjarne
    MemberAuthor

    It seems we'll be able to do something like this in the future:

    using Signature = delegate* unmanaged<IntPtr, IntPtr, byte, byte>;
    unsafe {
    	Signature trampoline = &TrampolineRegistrationHandlerCallback;
    	using var block = new BlockLiteral (trampoline, registrationHandler, typeof (CTFontManager), nameof (TrampolineRegistrationHandlerCallback));
    	CTFontManagerRegisterFontURLs (arr.Handle, scope, enabled, &block);
    }

    which, together with the function pointer type change, could become:

    using Signature = delegate* unmanaged<IntPtr, IntPtr, byte, byte>;
    unsafe {
    	using var block = new BlockLiteral ((Signature) &trampoline, registrationHandler, typeof (Signature));
    	CTFontManagerRegisterFontURLs (arr.Handle, scope, enabled, &block);
    }

    Ref: dotnet/csharplang#4284

  10. 2 remaining items

  11. self-assigned this
    on Mar 7, 2023
  12. added a commit that references this issue on Mar 15, 2023
    0faf60c
  13. ghost locked as resolved and limited conversation to collaborators on Apr 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

dotnetenhancementfeatureperformancerequest-for-comments

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions