Skip to content

No inline of method on value type injected to generic method: Fail Reason: noinline per IL/cached result #35791

Closed

Description

@nietras

I have a rather simple value type wrapping a Comparison<object> like below.

        readonly struct OpenDelegateObjectComparer : IComparer<object>
        {
            readonly Comparison<object> m_compare;

            public OpenDelegateObjectComparer(Comparison<object> compare) =>
                m_compare = compare;

            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            public int Compare(object x, object y) => m_compare(x, y);
        }

This is called in a method similar to below:

    public class Sample<T, TComparer>
        where TComparer : IComparer<T>
    {
        public int Do(Span<T> s, T value, TComparer comparer)
        {
            int count = 0;
            foreach (var v in s)
            {
                if (comparer.Compare(v, value) < 0)
                {
                    ++count;
                }
            }
            return count;
        }
    }

where TComparer is the above mentioned value type comparer.

The project in question is defined as below, and hence tiered compilation should be off.

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFrameworks>netcoreapp5.0</TargetFrameworks>
    <DebugType>pdbonly</DebugType>
    <DebugSymbols>true</DebugSymbols>
    <LangVersion>7.3</LangVersion>
    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
    <TieredCompilation>false</TieredCompilation>
  </PropertyGroup>

This fails to inline the comparer.Compare(v, value) call with:

Fail Reason: noinline per IL/cached result

And I don't understand why?

This is for running with and InliningDiagnoser and T being a reference type:

BenchmarkDotNet=v0.12.1, OS=Windows 10.0.18363.720 (1909/November2018Update/19H2)
Intel Core i7-8700 CPU 3.20GHz (Coffee Lake), 1 CPU, 12 logical and 6 physical cores
.NET Core SDK=5.0.100-preview.2.20176.6
  [Host] : .NET Core 5.0.0 (CoreCLR 5.0.20.16006, CoreFX 5.0.20.16006), X64 RyuJIT

The most similar question I could find was:

Activity

  1. added
    area-CodeGen-coreclr
    untriaged
    on May 4, 2020
  2. nietras commented on May 4, 2020

    @nietras
    ContributorAuthor

    Hmm I guess the following issue touches upon it #10048, which means I should just give up and this is probably a duplicate. Unfortunate. This would reduce code, not increase it. The other fail reason probably was more relevant:

    Fail Reason: delegate invoke
    

    :)

  3. benaadams commented on May 4, 2020

    @benaadams
    Member

    Why is it a delegate invoke? Looks like a regular method call?

  4. benaadams commented on May 4, 2020

    @benaadams
    Member

    Ah is it m_compare(x, y) that's not getting inlined rather than comparer.Compare(v, value) ?

  5. nietras commented on May 4, 2020

    @nietras
    ContributorAuthor

    Ah is it m_compare(x, y) that's not getting inlined rather than comparer.Compare(v, value) ?

    @benaadams might be 😄 From #10048 I thought it might be the regular method it self. Perhaps I can change code so easier to distinguish the two. Or other suggestions?

  6. nietras commented on May 4, 2020

    @nietras
    ContributorAuthor

    @benaadams actually the fail reason is pretty clear:

    Inlinee: OpenDelegateObjectComparer.Compare - instance int32  (class System.Object,class System.Object)
    Fail Reason: delegate invoke
    

    if I understand it correctly.

  7. benaadams commented on May 4, 2020

    @benaadams
    Member

    Ah, so calling a delegate blocks the inlining of the method that calls it. Seems a bit weird as it isn't a very exciting method

    OpenDelegateObjectComparer.Compare(System.Object, System.Object)
        L0000: mov rax, [rcx]
        L0003: mov rcx, [rax+0x8]
        L0007: mov rax, [rax+0x18]
        L000b: jmp rax

    If you change it to an interface call rather than a delegate its happy enough to inline it

    readonly struct OpenDelegateObjectComparer : IComparer<object>
    {
        readonly IComparer<object> m_compare;
    
        public OpenDelegateObjectComparer(IComparer<object> compare) =>
            m_compare = compare;
    
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public int Compare(object x, object y) => m_compare.Compare(x, y);
    }

    Even though that's a slightly more complex call site

    OpenDelegateObjectComparer.Compare(System.Object, System.Object)
        L0000: mov rcx, [rcx]
        L0003: mov r11, 0x7ffa239c7020
        L000d: mov rax, [rip+0x6b6c]
        L0014: cmp [rcx], ecx
        L0016: jmp rax
  8. nietras commented on May 4, 2020

    @nietras
    ContributorAuthor

    Seems a bit weird as it isn't a very exciting method

    @benaadams indeed 😄 I probably have to revert to calli 🙄 In general I could wish for better support around this form of inlineable "functor" pattern.

  9. benaadams commented on May 4, 2020

    @benaadams
    Member
  10. nietras commented on May 4, 2020

    @nietras
    ContributorAuthor

    Sounds like you want Function Pointers for C# 😉

    @benaadams I definitely do 😉 BUT that is a pointer and cannot be inlined itself, what I want is the ability to do both. Using a value type "functor" fulfilling a given interface as the base for all code paths 😃 That is so I could write something a ala:

    public struct SingleLess : ILess<float> 
    { 
        public bool Less(float x, float y) => x < y;
    }
    public struct FunctionPointerLess<T> : ILess<T> 
    {
        FunctionPointerLess(/* function pointer */ ptr) { ... }
        public bool Less(T x, T y) => ptr(x, y);
    }
    // Etc.

    When T is a reference type there are still issues around the canonical representation and that Less then still can't be inlined then, but with hackery/unsafe code, one can perhaps define this as:

    public struct ObjectFunctionPointerLess : ILess<object> 
    {
        ObjectFunctionPointerLess(/* function pointer */ ptr) { ... }
        public bool Less(object x, object y) => ptr(x, y);
    }

    so in reality I want generic, inlineable functors, but that conflicts with the canonical representation of reference type code. Wish one could opt out of that for some scenarios.

    Ideas are welcome! 😃

  11. nietras commented on May 4, 2020

    @nietras
    ContributorAuthor

    I've probably been using the STL too much but it's basically just that. I'd like STL-like code in C# for some things. :)

  12. nietras commented on May 4, 2020

    @nietras
    ContributorAuthor

    If you change it to an interface call rather than a delegate its happy enough to inline it

    @benaadams if delegates would implement an interface and such calls were as fast as delegate calls then that would have been a path too. Many paths explored I have 😁

  13. hypeartist commented on May 5, 2020

    @hypeartist

    Seems a bit weird as it isn't a very exciting method

    @benaadams indeed 😄 I probably have to revert to calli 🙄 In general I could wish for better support around this form of inlineable "functor" pattern.

    But calli also isn't getting inlined 🙄

  14. nietras commented on May 5, 2020

    @nietras
    ContributorAuthor

    calli also isn't getting inlined 🙄

    No of course not :) But a method containing a calli should now be inlined. bing uses calli and I think I remember they called out that methods with that could now be inlined which was a benefit for them. Unfortunately, what I am doing should work well on older runtimes so I can't use calli and hence I have to resort to code duplication.

    I am closing this PR since it is a duplicate of #10048

  15. ghost locked as resolved and limited conversation to collaborators on Dec 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-CodeGen-coreclruntriaged

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions