Skip to content

JIT: refine arg types based on actual types when creating arg temps #8790

@AndyAyersMS

Description

@AndyAyersMS

The jit will sometimes evaluate inlining arg values to temps and use those for the args in the inlinee body. The type of the temp is chosen from the callee signature.

The jit then imports the body of the inlinee and considers devirtualizing calls within the body. So this evaluation uses the declared argument type.

Later on, when assigning the arg value to the temp to pass the argument, the jit may refine the type of the temp based on actual type of the argument. This refinement comes too late to trigger devirtualization within the body of the inlinee.

Instead the jit should look at the actual argument type when creating the temp. This will provide a sharper type when examining the inlinee body.

Note existing devirtualization cases like this that "work" today rely on constant or locals that can be directly substituted into the inlinee body, without using a temp, or take advantage of improved types "on the way out" as the call sites being devirtualized are outside of the inlinee body.

Example, thanks to @adamsitnik (and @JosephTremoulet for pointing this out)

using System;

public class J
{
    private Increment increment = new Increment();

    public int CallVirtualMethod() => increment.OperateTwice(10);

    public abstract class Operation  // abstract unary integer operation
    {
        public abstract int Operate(int input);

        public int OperateTwice(int input) => Operate(Operate(input)); // two virtual calls to Operate
    }

    public sealed class Increment : Operation // concrete, sealed operation: increment by fixed amount
    {
        public readonly int Amount;
        public Increment(int amount = 1) { Amount = amount; }

        public override int Operate(int input) => input + Amount;
    }

    public static int Main()
    {
        J j = new J();
        return j.CallVirtualMethod();     
    }
}

Should be relatively simple to fix.

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIenhancementProduct code improvement that does NOT require public API changes/additionsoptimization

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions