Skip to content

[LibraryImportGenerator] Reduce unnecessary casting/locals in pinning path #69804

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

Merged
merged 2 commits into from
May 26, 2022

Conversation

elinor-fung
Copy link
Member

Contributes to #69608: Style item (6)

UTF-16 string:

[LibraryImport("lib", StringMarshalling = StringMarshalling.Utf16)]
internal static partial void ManyStrings(string s1, string s2, string s3, string s4, string s5);

// Generated
internal static partial void ManyStrings(string s1, string s2, string s3, string s4, string s5)
{
    // Pin - Pin data in preparation for calling the P/Invoke.
    fixed (void* __s1_native = s1)
        fixed (void* __s2_native = s2)
            fixed (void* __s3_native = s3)
                fixed (void* __s4_native = s4)
                    fixed (void* __s5_native = s5)
                    {
                        __PInvoke(__s1_native, __s2_native, __s3_native, __s4_native, __s5_native);
                    }

    // Local P/Invoke
    [System.Runtime.InteropServices.DllImportAttribute("lib", EntryPoint = "ManyStrings", ExactSpelling = true)]
    static extern unsafe void __PInvoke(void* s1, void* s2, void* s3, void* s4, void* s5);
}

User-defined:

[NativeMarshalling(typeof(IntWrapperMarshaler))]
public class IntWrapper
{
public int i;
public ref int GetPinnableReference() => ref i;
}

[LibraryImport("lib")]
public static partial void UserDefinedPinnable(SharedTypes.IntWrapper i1, SharedTypes.IntWrapper i2);

// Generated
public static partial void UserDefinedPinnable(global::SharedTypes.IntWrapper i1, global::SharedTypes.IntWrapper i2)
{
    // Pin - Pin data in preparation for calling the P/Invoke.
    fixed (void* __i1_native = i1)
        fixed (void* __i2_native = i2)
        {
            __PInvoke((int*)__i1_native, (int*)__i2_native);
        }

    // Local P/Invoke
    [System.Runtime.InteropServices.DllImportAttribute("lib", EntryPoint = "UserDefinedPinnable", ExactSpelling = true)]
    static extern unsafe void __PInvoke(int* i1, int* i2);
}

@elinor-fung elinor-fung added area-System.Runtime.InteropServices source-generator Indicates an issue with a source generator feature labels May 25, 2022
@elinor-fung elinor-fung added this to the 7.0.0 milestone May 25, 2022
@ghost ghost assigned elinor-fung May 25, 2022
@ghost
Copy link

ghost commented May 25, 2022

Tagging subscribers to this area: @dotnet/interop-contrib
See info in area-owners.md if you want to be subscribed.

Issue Details

Contributes to #69608: Style item (6)

UTF-16 string:

[LibraryImport("lib", StringMarshalling = StringMarshalling.Utf16)]
internal static partial void ManyStrings(string s1, string s2, string s3, string s4, string s5);

// Generated
internal static partial void ManyStrings(string s1, string s2, string s3, string s4, string s5)
{
    // Pin - Pin data in preparation for calling the P/Invoke.
    fixed (void* __s1_native = s1)
        fixed (void* __s2_native = s2)
            fixed (void* __s3_native = s3)
                fixed (void* __s4_native = s4)
                    fixed (void* __s5_native = s5)
                    {
                        __PInvoke(__s1_native, __s2_native, __s3_native, __s4_native, __s5_native);
                    }

    // Local P/Invoke
    [System.Runtime.InteropServices.DllImportAttribute("lib", EntryPoint = "ManyStrings", ExactSpelling = true)]
    static extern unsafe void __PInvoke(void* s1, void* s2, void* s3, void* s4, void* s5);
}

User-defined:

[NativeMarshalling(typeof(IntWrapperMarshaler))]
public class IntWrapper
{
public int i;
public ref int GetPinnableReference() => ref i;
}

[LibraryImport("lib")]
public static partial void UserDefinedPinnable(SharedTypes.IntWrapper i1, SharedTypes.IntWrapper i2);

// Generated
public static partial void UserDefinedPinnable(global::SharedTypes.IntWrapper i1, global::SharedTypes.IntWrapper i2)
{
    // Pin - Pin data in preparation for calling the P/Invoke.
    fixed (void* __i1_native = i1)
        fixed (void* __i2_native = i2)
        {
            __PInvoke((int*)__i1_native, (int*)__i2_native);
        }

    // Local P/Invoke
    [System.Runtime.InteropServices.DllImportAttribute("lib", EntryPoint = "UserDefinedPinnable", ExactSpelling = true)]
    static extern unsafe void __PInvoke(int* i1, int* i2);
}
Author: elinor-fung
Assignees: -
Labels:

area-System.Runtime.InteropServices, source-generator

Milestone: 7.0.0

@am11
Copy link
Member

am11 commented May 25, 2022

Do we have such control over indentation which would make it (conveniently) generate stacked fixed statements like this:

    // Pin - Pin data in preparation for calling the P/Invoke.
    fixed (void* __s1_native = s1)
    fixed (void* __s2_native = s2)
    fixed (void* __s3_native = s3)
    fixed (void* __s4_native = s4)
    fixed (void* __s5_native = s5)
    {
        __PInvoke(__s1_native, __s2_native, __s3_native, __s4_native, __s5_native);
    }

@elinor-fung
Copy link
Member Author

We are relying on Roslyn's NormalizeWhitespace to format / apply indentation accordingly to the entire block of generated code, so we don't fully control the indentation like that. I opened dotnet/roslyn#61518 to track potentially improving the behaviour around cases like this.

@elinor-fung
Copy link
Member Author

Failures are #69792.

@elinor-fung elinor-fung merged commit 5ba2911 into dotnet:main May 26, 2022
@elinor-fung elinor-fung deleted the lessCasting branch May 26, 2022 19:08
@ghost ghost locked as resolved and limited conversation to collaborators Jun 26, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Runtime.InteropServices source-generator Indicates an issue with a source generator feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants