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

Avoid unnecessary return val copying in tailcall-via-help #72720

Merged
merged 3 commits into from
Jul 24, 2022

Conversation

jakobbotsch
Copy link
Member

My initial implementation of this did not handle the fact that retbuf
can point to GC heap during reflection invoke. It was fixed in #39815,
but the way it was fixed was by copying it into a local. This changes
the fix so that we simply report the return value pointer as a byref
throughout the mechanism, which simplifies the JIT's handling and is a
perf improvement as well.

Benchmark:

[Benchmark]
public long Bench()
{
    return Tail(100_000_000, new int[5]).Length;
}

private static Span<int> Tail(int count, Span<int> s)
{
    if (count == 0)
        return s;

    IL.Push(count - 1);
    IL.Emit.Ldarg(nameof(s));
    IL.Emit.Tail();
    IL.Emit.Call(new MethodRef(typeof(Benchmark), nameof(Tail)));
    IL.Emit.Ret();
    throw IL.Unreachable();
}

Result:

Method Job Toolchain Mean Error StdDev Ratio RatioSD
Bench Job-BWZGON \base\corerun.exe 3.949 s 0.0788 s 0.0809 s 1.00 0.00
Bench Job-SJJEVX \diff\corerun.exe 3.785 s 0.0154 s 0.0120 s 0.96 0.02

The initial implementation of this did not handle the fact that retbuf
can point to GC heap during reflection invoke. It was fixed in dotnet#39815,
but the way it was fixed was by copying it into a local. This changes
the fix so that we simply report the return value pointer as a byref
throughout the mechanism, which simplifies the JIT's handling and is a
perf improvement as well.
@dotnet-issue-labeler
Copy link

I couldn't figure out the best area label to add to this PR. If you have write-permissions please help me learn by adding exactly one area label.

@ghost ghost assigned jakobbotsch Jul 23, 2022
@jakobbotsch jakobbotsch added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Jul 23, 2022
@ghost
Copy link

ghost commented Jul 23, 2022

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.

Issue Details

My initial implementation of this did not handle the fact that retbuf
can point to GC heap during reflection invoke. It was fixed in #39815,
but the way it was fixed was by copying it into a local. This changes
the fix so that we simply report the return value pointer as a byref
throughout the mechanism, which simplifies the JIT's handling and is a
perf improvement as well.

Benchmark:

[Benchmark]
public long Bench()
{
    return Tail(100_000_000, new int[5]).Length;
}

private static Span<int> Tail(int count, Span<int> s)
{
    if (count == 0)
        return s;

    IL.Push(count - 1);
    IL.Emit.Ldarg(nameof(s));
    IL.Emit.Tail();
    IL.Emit.Call(new MethodRef(typeof(Benchmark), nameof(Tail)));
    IL.Emit.Ret();
    throw IL.Unreachable();
}

Result:

Method Job Toolchain Mean Error StdDev Ratio RatioSD
Bench Job-BWZGON \base\corerun.exe 3.949 s 0.0788 s 0.0809 s 1.00 0.00
Bench Job-SJJEVX \diff\corerun.exe 3.785 s 0.0154 s 0.0120 s 0.96 0.02
Author: jakobbotsch
Assignees: jakobbotsch
Labels:

area-CodeGen-coreclr

Milestone: -

@jakobbotsch jakobbotsch merged commit c9c27d4 into dotnet:main Jul 24, 2022
@jakobbotsch jakobbotsch deleted the avoid-tailcall-retbuf-copying branch July 24, 2022 17:38
@ghost ghost locked as resolved and limited conversation to collaborators Aug 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants