Skip to content

Smaller funclet prologs/epilogs (x64)#115284

Merged
jkotas merged 6 commits into
dotnet:mainfrom
jkotas:funclets
May 10, 2025
Merged

Smaller funclet prologs/epilogs (x64)#115284
jkotas merged 6 commits into
dotnet:mainfrom
jkotas:funclets

Conversation

@jkotas

@jkotas jkotas commented May 4, 2025

Copy link
Copy Markdown
Member
  • Allow funclets to trash non-volatile registers to make funclet prologs/epilogs smaller
  • Restore non-volatile registers before non-filter funclet calls
  • Unify funclet ABI between NAOT and non-NAOT

- Allow funclets to trash non-volatile registers to make funclet prologs/epilogs smaller
- Restore non-volatile registers before non-filter funclet calls
- Unify funclet ABI between NAOT and non-NAOT
Copilot AI review requested due to automatic review settings May 4, 2025 18:57
@jkotas jkotas requested a review from MichalStrehovsky as a code owner May 4, 2025 18:57
@ghost ghost added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label May 4, 2025
@jkotas jkotas added area-ExceptionHandling-coreclr only use for closed issues and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels May 4, 2025

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull Request Overview

This PR refactors the funclet prolog/epilog sequences for x64 to reduce their size while unifying the ABI between NAOT and non-NAOT builds.

  • Removed redundant saving/restoring of non-volatile registers in funclets
  • Updated stack frame allocation and alignment computations
  • Adjusted documentation to reflect the revised behavior and design decisions

Reviewed Changes

Copilot reviewed 2 out of 6 changed files in this pull request and generated 2 comments.

File Description
src/coreclr/jit/codegenxarch.cpp Removed EBP and callee-register save/restore logic; updated frame allocation calculation using REG_NA
docs/design/coreclr/botr/clr-abi.md Revised description of register preservation behavior in funclets
Files not reviewed (4)
  • src/coreclr/nativeaot/Runtime/amd64/ExceptionHandling.S: Language not supported
  • src/coreclr/nativeaot/Runtime/amd64/ExceptionHandling.asm: Language not supported
  • src/coreclr/vm/amd64/AsmHelpers.asm: Language not supported
  • src/coreclr/vm/amd64/asmhelpers.S: Language not supported

Comment thread src/coreclr/jit/codegenxarch.cpp Outdated
Comment thread docs/design/coreclr/botr/clr-abi.md
@jkotas jkotas changed the title Smaller funclets prologs/epilogs (x64) Smaller funclet prologs/epilogs (x64) May 4, 2025
@jkotas

jkotas commented May 4, 2025

Copy link
Copy Markdown
Member Author

@EgorBot -windows_intel -amd

using System.Reflection;
using BenchmarkDotNet.Attributes;

public class Bench
{
    static int x;

    [Benchmark]
    public void Finally()
    {
        try
        {
            try
            {
                try
                {
                    try
                    {
                        try
                        {
                            try
                            {
                                try
                                {
                                    try
                                    {
                                        try
                                        {
                                            try
                                            {
                                                throw new Exception();
                                            }
                                            finally { x++; }
                                        }
                                        finally { x++; }
                                    }
                                    finally { x++; }
                                }
                                finally { x++; }
                            }
                            finally { x++; }
                        }
                        finally { x++; }
                    }
                    finally { x++; }
                }
                finally { x++; }
            }
            finally { x++; }
        }
        catch
        {
        }
    }
}

@jkotas

jkotas commented May 4, 2025

Copy link
Copy Markdown
Member Author

@EgorBot -windows_intel -intel

using System.Reflection;
using BenchmarkDotNet.Attributes;

public class Bench
{
    static int x;

    [Benchmark]
    public void Finally()
    {
        try
        {
            try
            {
                try
                {
                    try
                    {
                        try
                        {
                            try
                            {
                                try
                                {
                                    try
                                    {
                                        try
                                        {
                                            try
                                            {
                                                throw new Exception();
                                            }
                                            finally { x++; }
                                        }
                                        finally { x++; }
                                    }
                                    finally { x++; }
                                }
                                finally { x++; }
                            }
                            finally { x++; }
                        }
                        finally { x++; }
                    }
                    finally { x++; }
                }
                finally { x++; }
            }
            finally { x++; }
        }
        catch
        {
        }
    }
}

@jkotas

jkotas commented May 4, 2025

Copy link
Copy Markdown
Member Author

@MihuBot

@jkotas

jkotas commented May 5, 2025

Copy link
Copy Markdown
Member Author

Perf results summary:

  • 0.6% code size improvement on average
  • Artificial micro-benchmark to measure the cost of saving and restoring all registers around funclet invocation is very noisy. The results vary between 10% improvement and 5% regression.

@jkotas

jkotas commented May 5, 2025

Copy link
Copy Markdown
Member Author

@janvorli @dotnet/jit-contrib PTLA. This PR is for x64 only. I plan to switch other platforms to the unified scheme in subsequent PRs.

Comment thread src/coreclr/vm/amd64/AsmHelpers.asm
Comment thread src/coreclr/nativeaot/Runtime/amd64/ExceptionHandling.asm Outdated

@janvorli janvorli left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, thank you!

Comment thread src/coreclr/jit/codegenxarch.cpp
@BruceForstall

Copy link
Copy Markdown
Contributor

I suggest running outerloop tests.

@jkotas

jkotas commented May 6, 2025

Copy link
Copy Markdown
Member Author

/azp run runtime-coreclr outerloop

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@jkotas jkotas merged commit 33b5215 into dotnet:main May 10, 2025
99 checks passed
@jkotas jkotas deleted the funclets branch May 10, 2025 05:41
@github-actions github-actions Bot locked and limited conversation to collaborators Jun 9, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-ExceptionHandling-coreclr only use for closed issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants