Skip to content
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

Enable "Allocate on stack" for NAOT/R2R #104411

Merged
merged 21 commits into from
Jul 14, 2024
Merged

Conversation

EgorBo
Copy link
Member

@EgorBo EgorBo commented Jul 4, 2024

Closes #104350
Fixes #104337
This PR adds NAOT/R2R support for #103361

Example:

static int Test1()
{
    MyClass obj = new MyClass(42); // MyClass allocation never escapes
    return obj.X;
}

static int Test2()
{
    object o = 42; // boxing (allocation)
    return (int)o; // unboxing
}

public class MyClass(int x)
{
    public int X => x;
}

Old codegen for Test() on NAOT (and R2R):

; Assembly listing for method Proga:Test1():int (FullOpts)
; NativeAOT compilation
       sub      rsp, 40
       lea      rcx, [(reloc 0x40000000004211a8)]      ; MyClass
       call     CORINFO_HELP_NEWSFAST
       mov      dword ptr [rax+0x08], 42
       mov      eax, dword ptr [rax+0x08]
       add      rsp, 40
       ret

; Assembly listing for method Proga:Test2():int (FullOpts)
; NativeAOT compilation
       sub      rsp, 40
       lea      rcx, [(reloc 0x4000000000421008)]      ; Int32
       call     CORINFO_HELP_NEWSFAST
       mov      dword ptr [rax+0x08], 42
       mov      eax, dword ptr [rax+0x08]
       add      rsp, 40
       ret

New codegen:

; Assembly listing for method Proga:Test1():int (FullOpts)
; NativeAOT compilation
       mov      eax, 42
       ret      

; Assembly listing for method Proga:Test2():int (FullOpts)
; NativeAOT compilation
       mov      eax, 42
       ret      

@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Jul 4, 2024
@ShreyasJejurkar
Copy link
Contributor

Is the .NET main branch broken on CE somehow? Checking the codegen for the example, yields older code.

https://godbolt.org/z/aveWzrfvv

image

@EgorBo
Copy link
Member Author

EgorBo commented Jul 4, 2024

Is the .NET main branch broken on CE somehow? Checking the codegen for the example, yields older code.

#104442

@AndyAyersMS
Copy link
Member

I'm surprised that you don't need to do anything on the runtime side to implement CORINFO_HELP_UNBOX_TYPETEST.

@EgorBo
Copy link
Member Author

EgorBo commented Jul 4, 2024

I'm surprised that you don't need to do anything on the runtime side to implement CORINFO_HELP_UNBOX_TYPETEST.

CI is currently failing with:

EXEC : error : One or more errors occurred. (Code generation failed for method '[Microsoft.Extensions.Logging.Configuration]Microsoft.Extensions.Configuration.Binder.SourceGeneration.<BindingExtensions_g>F7FD99DEF4D1E9898A126F6C62635605FA12A7F83D698AD1B212E573AF4334BC7__BindingExtensions.GetValue<bool>(IConfiguration,string,bool)') [D:\a\_work\1\s\artifacts\bin\aotTests\projects\Microsoft.Extensions.Logging.Console.TrimmingTests\JsonFormattingTests\win-x64\project.csproj]
##[error]EXEC(0,0): error : (NETCORE_ENGINEERING_TELEMETRY=Build) One or more errors occurred. (Code generation failed for method '[Microsoft.Extensions.Logging.Configuration]Microsoft.Extensions.Configuration.Binder.SourceGeneration.<BindingExtensions_g>F7FD99DEF4D1E9898A126F6C62635605FA12A7F83D698AD1B212E573AF4334BC7__BindingExtensions.GetValue<bool>(IConfiguration,string,bool)')
  System.AggregateException: One or more errors occurred. (Code generation failed for method '[Microsoft.Extensions.Logging.Configuration]Microsoft.Extensions.Configuration.Binder.SourceGeneration.<BindingExtensions_g>F7FD99DEF4D1E9898A126F6C62635605FA12A7F83D698AD1B212E573AF4334BC7__BindingExtensions.GetValue<bool>(IConfiguration,string,bool)')
   ---> ILCompiler.CodeGenerationFailedException: Code generation failed for method '[Microsoft.Extensions.Logging.Configuration]Microsoft.Extensions.Configuration.Binder.SourceGeneration.<BindingExtensions_g>F7FD99DEF4D1E9898A126F6C62635605FA12A7F83D698AD1B212E573AF4334BC7__BindingExtensions.GetValue<bool>(IConfiguration,string,bool)'
   ---> System.NotImplementedException: CORINFO_HELP_UNBOX_TYPETEST
     at Internal.JitInterface.CorInfoImpl.GetHelperFtnUncached(CorInfoHelpFunc) + 0xa60
     at Internal.JitInterface.CorInfoImpl.getHelperFtn(CorInfoHelpFunc, Void*&) + 0x34

🙂

@EgorBo
Copy link
Member Author

EgorBo commented Jul 5, 2024

/azp run runtime-nativeaot-outerloop

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@EgorBo EgorBo marked this pull request as ready for review July 5, 2024 11:25
@EgorBo EgorBo requested a review from MichalStrehovsky as a code owner July 5, 2024 11:25
@EgorBo
Copy link
Member Author

EgorBo commented Jul 5, 2024

I think this is ready, PTAL @jkotas @MichalStrehovsky @AndyAyersMS

@EgorBo
Copy link
Member Author

EgorBo commented Jul 12, 2024

Outerloops:

@MichalStrehovsky
Copy link
Member

  • also a weird AVE in System.Net.Sockets.Tests but it's not my PR specific

This is #104500.

Comment on lines 182 to 183
if (attribs.Length == 0)
return true; // Assume corelib is optimized in this case
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding <DebuggerSupport>true</DebuggerSupport> to the project file would make the attribute visible, but ILC has its own optimization switch so CoreLib having this attribute still doesn't necessarily imply how optimized it is.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MichalStrehovsky do we run runtime tests with NativeAOT in full debug mode?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So can you tell me how to properly check it here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MichalStrehovsky do we run runtime tests with NativeAOT in full debug mode?

We do have such legs, I'm not sure if they include this test too. It would be in the runtime or runtime-nativeaot-outerloop if it exists.

So can you tell me how to properly check it here?

Native AOT will use the same settings for optimizing both CoreLib and user code so enabling the attribute and looking for the attribute on Assembly.GetEntryAssembly would give the answer.

I guess I don't understand why this is looking at whether corelib is optimized. Why does that affect stack allocations in this test?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess I don't understand why this is looking at whether corelib is optimized. Why does that affect stack allocations in this test?

Maybe the way we build the tests, optimization of CoreLib always matches optimization of the test?

If that's the case, all that's needed is to add <DebuggerSupport>true</DebuggerSupport> to the test project and this will work the same. It will also fail the same if there's ever mismatch between test optimization and corelib optimization.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess I don't understand why this is looking at whether corelib is optimized. Why does that affect stack allocations in this test?

I am not the original author (I presume the authors no longer work in the team), but looks like the test tries to be agile and validate that nothing is stack-allocated under debug maybe? 🤷

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not the original author (I presume the authors no longer work in the team), but looks like the test tries to be agile and validate that nothing is stack-allocated under debug maybe?

Right, but for that one would check whether the test is optimized, not corelib. One can build the test with <Optimize>true</Optimize> and have corelib from a debug build. I assume the test would fail in that case.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I mean in the JIT case. It would also fail in the AOT case because I believe the test is looking at the wrong assembly.)

Copy link
Member Author

@EgorBo EgorBo Jul 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ended up removing these bits and the test is still working fine (it's always optimized due to <Optimize>true</Optimize>)

@EgorBo
Copy link
Member Author

EgorBo commented Jul 12, 2024

/azp run runtime-nativeaot-outerloop

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@EgorBo
Copy link
Member Author

EgorBo commented Jul 13, 2024

/azp run runtime-coreclr runtime-nativeaot-outerloop

Copy link

No pipelines are associated with this pull request.

@EgorBo
Copy link
Member Author

EgorBo commented Jul 13, 2024

/azp run runtime-nativeaot-outerloop

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@EgorBo
Copy link
Member Author

EgorBo commented Jul 14, 2024

@jkotas @MichalStrehovsky can this be merged then? All nativeaot-outerloop failures are either #104862 or #104500

Copy link
Member

@jkotas jkotas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM otherwise. Thank you!

@EgorBo EgorBo merged commit af5e715 into dotnet:main Jul 14, 2024
167 checks passed
@EgorBo EgorBo deleted the alloc-stack-naot branch July 14, 2024 23:12
@github-actions github-actions bot locked and limited conversation to collaborators Aug 14, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
5 participants