JIT: Interprocedural analysis for out parameters

Open
#138,133 2 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
12/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Active
Tech stack
csharp
Domain
compilers

Research direction

This is open-ended JIT design work: the body asks whether to compile specialized versions of methods with out parameters or to strengthen the inliner, and names no file. Start by reading the linked PR 138016 to see the local-variable rewrite it used, and the JIT inliner sources under src/coreclr/jit. Done means a design agreed with the JIT maintainers (@jkotas, @dotnet/jit-contrib), not a patch.

Written by the indexing model from the issue text.

Description

area-CodeGen-coreclr untriaged

It seems that in the vast majority of calls with out parameters, callers declare the corresponding arguments inline:

void Foo(..., out int written)
{
    ...
    written++;
    ...
    written++;
}

// typical usage
Foo(..., out int written);

Knowing that an out argument is on the stack and is not aliased can help the JIT generate better code. For example, https://github.com/dotnet/runtime/pull/138016 made one benchmark nearly 2.5x faster by updating a local variable instead of modifying the out argument directly.

Suggestion

Could we compile two versions of each method with out parameters: a default version and a specialized version that uses this hint? The JIT could then choose the appropriate version at each call site. The ReJIT concern still applies, but is there anything else I'm missing?

Alternatively, we can boost the inliner additionally.

cc @jkotas @dotnet/jit-contrib

Data

Roslyn syntax scan over the product code in src/libraries and src/coreclr/System.Private.CoreLib, excluding tests and ref folders (12,463 files, 15,780 out arguments at call sites):

out argument kind Share
inline decl (out var x / out T x) 37.4%
pre-declared local 26.6%
forwarded out param (TryX -> TryXCore) 23.4%
discard (out _ / out var _) 7.2%
field / this.x / arr[i] / *p / ref param / other 5.4%

Inline declarations are about 37%. All non-aliased stack locals (inline + pre-declared + discard) add up to about 71%.

Dominant language
C#
Stars
18.5k
Forks
5.8k
Avg merge
2d 18h
Merged PRs (30d)
588

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dotnet/runtime

All issues in dotnet/runtime

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.