Closed
Description
This issue will track work on supporting object stack allocation in the jit. See dotnet/coreclr#20251 for the document describing the work.
The initial goal is to be able to remove heap allocation in a simple example like this:
class Foo
{
public int f1;
public int f2;
public Foo(int f1, int f2)
{
this.f1 = f1;
this.f2 = f2;
}
}
class Test
{
static int f1;
static int f2;
public static int Main()
{
Foo foo = new Foo(f1, f2);
return foo.f1 + foo.f2;
}
}
and then in a similar example where the class has gc fields.
Proposed initial steps are:
- add getHeapClassSize jit interface method and its implementations JitEE interface additions to support object stack allocation. coreclr#20283
- add canAllocateOnStack jit interface method and its implementations JitEE interface additions to support object stack allocation. coreclr#20283
- modify getClassGCLayout jit interface method to work on reference types Initial implementation of object stack allocation coreclr#20814
- add COMPlus_JitObjectStackAllocation environment variable to control this optimization (off by default) Initial implementation of object stack allocation coreclr#20814
- move ObjectAllocator phase to be closer to inlining Move ObjectAllocator phase to run right after inlining. coreclr#20377
- modify lvaSetStruct to allow creating locals corresponding to stack-allocated classes Initial implementation of object stack allocation coreclr#20814
- update types of references in methods with stack allocated objects to TYP_REF (when always pointing to the heap) or TYP_I_IMPL (when always pointing to the stack) or TYP_BYREF (when pointing to the heap or to the stack) Improvements for object stack allocation. coreclr#21950
- modify gc reporting to properly report gc fields of stack-allocated objects
- modify gc writebarrier logic to apply appropriate barriers when assigning to fields of (possibly) stack-allocated objects Improvements for object stack allocation. coreclr#21950
- add simple conservative escape analysis sufficient for the example above Initial implementation of object stack allocation coreclr#20814
- make the analysis more sophisticated to handle increasingly more complex examples (eg fields of value classes, or fields of unescaped ref classes, see eg Fold GDV checks for inlined delegates #84872, JIT - Folding unwanted foreach with empty list. #61455)
- special case calls to helpers where arguments don't escape (this will require ensuring that the helpers report gc arguments as interior to gc) (some in Stack allocate unescaped boxes #103361)
- enable promotion of fields of stack-allocated objects (via physical promotion)
- enable the optimization for x86 Improvements for object stack allocation. coreclr#21950
- enable stack allocation of boxed structs (Stack allocate unescaped boxes #103361)
- enable stack allocation of constant-sized arrays
- enable stack allocation of strings
- enable object stack allocation in R2R mode Enable object stack allocation in R2R mode. coreclr#21533
- make sure object stack allocation doesn't block fast tail call optimization unnecessarily (currently fast tail call optimization is disabled if there are any locals with lvAddrExposed set)
- R2R/NAOT support for new jit interface method / helper introduced by Stack allocate unescaped boxes #103361
I will be modifying and extending this list as the work progresses.
cc @dotnet/jit-contrib
category:cq
theme:object-stack-allocation
skill-level:expert
cost:extra-large
impact:large