Skip to content

net10.0: Support MethodImpl.Async #19056

Open
Open

Description

@TheAngryByrd

Context

👋 .NET 10 is bringing support for Runtime-Async. I started playing around with it in an IcedTasks PR. However, I noticed some of benchmarks being off considerably. One of the requirements is having MethodImplOptions.Async set. I manually added the atttribute, but it doesn't get written to the IL.

Repro steps

Provide the steps required to reproduce the problem:

    [<MethodImpl(MethodImplOptions.Async)>]
    let fsharp_tenBindAsync_TaskBuilderRuntime () =
        IcedTasks.Polyfill.TasksRuntime.TaskBuilder.task {
            do! taskYield ()
            return 100
        }

see that the compiled DLL doesn't contain it in the IL/decompiled c#


    public static Task<int> fsharp_tenBindAsync_TaskBuilderRuntime()
    {
      YieldAwaitable.YieldAwaiter awaiter1 = Task.Yield().GetAwaiter();
      if (!awaiter1.IsCompleted)
        AsyncHelpers.UnsafeAwaitAwaiter<YieldAwaitable.YieldAwaiter>(awaiter1);
      awaiter1.GetResult();
      ValueTaskAwaiter<int> awaiter11 = ValueTask.FromResult<int>(100).GetAwaiter();
      if (!awaiter11.IsCompleted)
        AsyncHelpers.UnsafeAwaitAwaiter<ValueTaskAwaiter<int>>(awaiter11);
      return Task.FromResult<int>(awaiter11.GetResult());
    }

ilprint seems to need to have detection and output for it done. However, the current detection relies on MethodImpl being in netstandard2.0, since this is part of net10.0, there would have to be some compat layer here.

if md.IsSynchronized then
output_string os "synchronized "
if md.IsMustRun then
output_string os "/* mustrun */ "
if md.IsPreserveSig then
output_string os "preservesig "
if md.IsNoInline then
output_string os "noinlining "
if md.IsAggressiveInline then
output_string os "aggressiveinlining "


Secondly, as a separate discussion point. Having people manually add this attribute would be extremely inconvenient. What options do you see as possible for a CE to add attributes to the methods that get written out? (We can move to a separate discussion if needed)

Expected behavior

MethodImpl is preserved

Actual behavior

MethodImpl is not preserved

Known workarounds

Hand editing the IL?

Related information

Provide any related information (optional):

  • Operating system
  • .NET Runtime kind (.NET Core, .NET Framework, Mono)
  • Editing Tools (e.g. Visual Studio Version, Visual Studio)

Activity

  1. added theissue type on Nov 6, 2025
  2. added this to the Backlog milestone on Nov 6, 2025
  3. roboz0r commented on Nov 6, 2025

    @roboz0r

    Possibly this should be a separate issue (though it could be addressed at the same time) is that the int16 overload for MethodImpl appears not to be supported at all. Looking at the decompiled C#, the C# version always applies MethodImplOptions.AggressiveInlining and F# ignores the int16 version.

    using System.Runtime.CompilerServices;
    public static class MyModule
    {
        [MethodImpl(256)]
        public static int add(int x, int y)
        {
            return x + y;
        }
    
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
    
        public static int add2(int x, int y)
        {
            return x + y;
        }
    }
    
    open System.Runtime.CompilerServices
    
    module MyModule =
    
        [<MethodImpl(256s)>]
        let add x y = x + y
    
        [<MethodImpl(MethodImplOptions.AggressiveInlining)>]
        let add2 x y = x + y
    
  4. T-Gro commented on Nov 6, 2025

    @T-Gro
    Member
  5. T-Gro commented on Nov 10, 2025

    @T-Gro
    Member

    @TheAngryByrd :

    Does it make sense to solve the explicit manual declaration in isolation, or is that of lesser help and you would really want the automatic CE mechanism ?

    Definitely happy to brainstorm the alternatives (another idea would be a defined CE-keyword, like add_attributes, which would copy over attributes from the builder's method declaration over to the user-space owning IL method)

  6. TheAngryByrd commented on Nov 10, 2025

    @TheAngryByrd
    ContributorAuthor

    Ive got some local fork thats writing the IL if you add the attribute manually, but its supposed to be only generated by the compiler (C# disallows you writing it manually).

    I was playing with the idea of writing the attribute whenever a method has “AsyncHelpler.Await” or any of the other overloads. But im definitely open to other options that you think make sense.

  7. T-Gro commented on Nov 10, 2025

    @T-Gro
    Member

    I would prefer a smaller non-specialized addition to the compiler now, just to enable experimentation within libraries (since compiler is by nature slow to change due to release cycle) and be ready to changes/additions in the runtime implementation.

    Once the feature stabilizes, a compiled detection based on method calls at IL codegen time is OK (pending if/how/if at all do anything for Fable to be aware of it)

  8. TheAngryByrd commented on Nov 10, 2025

    @TheAngryByrd
    ContributorAuthor

    Ok I'll submit the small change to let people use the Attribute for now.

    Should we hide it behind a feature flag since we'll probably want to disallow people from using it directly in the future?

  9. T-Gro commented on Nov 10, 2025

    @T-Gro
    Member

    Can use the LanguageFeature mechanism and be in preview (and likely stay in preview)

  10. TheAngryByrd commented on Nov 10, 2025

    @TheAngryByrd
    ContributorAuthor

    This was the bare minimum I got to this weekend but I still need to do more verifications/testing + FeatureFlag

    #19065

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-Compiler-ImportAndInteropFeature Request

    Type

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions