Repository navigation
Closed
Labels
area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIin-prThere is an active PR which will close this issue when it is mergedtenet-performancePerformance related issue
Milestone
Description
Description
I am not sure whether this is a bug or known issue or just that my environment is out of time. Anyway, let the code talks:
public struct Foo
{
public int a;
public int b;
}
public class StructContainer
{
Foo foo;
public void Update()
{
this.foo.a += 1;
this.foo.b += 1;
}
}
public class NormalContainer
{
int a;
int b;
public void Update()
{
this.a += 1;
this.b += 1;
}
}
[Benchmark]
public StructContainer StructContainerUpdate()
{
var sc = new StructContainer();
sc.Update();
return sc;
}
[Benchmark]
public NormalContainer NormalContainerUpdate()
{
var nc = new NormalContainer();
nc.Update();
return nc;
}Generated code:
## .NET 6.0.3 (6.0.322.12309), X64 RyuJIT
; Benchmarks.StructInline.StructContainerUpdate()
sub rsp,28
mov rcx,offset MT_Benchmarks.StructInline+StructContainer
call CORINFO_HELP_NEWSFAST
lea rdx,[rax+8] ; we do not really need this line
inc dword ptr [rdx]
lea rdx,[rax+0C]
inc dword ptr [rdx]
add rsp,28
ret
; Total bytes of code 36
; Benchmarks.StructInline.NormalContainerUpdate()
sub rsp,28
mov rcx,offset MT_Benchmarks.StructInline+NormalContainer
call CORINFO_HELP_NEWSFAST
inc dword ptr [rax+8]
inc dword ptr [rax+0C]
add rsp,28
ret
; Total bytes of code 30I know little about CLR, so I may have seriously underestimated the complexity of it. But it feels like the lea command is bit of not clever, right?
The memory layout will not change. So something like this.struct1.struct2.struct3.struct4.property could always be calculated by [rax+offset](if the offset is not too large), isn't it?
We does have this, so this problem is pretty limited, should be another peephole optimization I think.
Configuration
BenchmarkDotNet=v0.13.1, OS=Windows 10.0.22000
Intel Core i9-10900X CPU 3.70GHz, 1 CPU, 20 logical and 10 physical cores
.NET SDK=6.0.201
Regression?
Data
Analysis
category:cq
theme:optimization
Metadata
Metadata
Assignees
Labels
area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIin-prThere is an active PR which will close this issue when it is mergedtenet-performancePerformance related issue
Activity
huoyaoyuan commentedon Mar 26, 2022
This is indeed a question for the x86_64 architecture: is
[rax + constant]addressing mode available for theINCinstruction?huoyaoyuan commentedon Mar 26, 2022
The answer is yes:
INC DWORD PTR [RAX + 8]is valid, confirmed with C++ inline asm. But I'm not sure the performance.ShuiRuTian commentedon Mar 27, 2022
@huoyaoyuan Oops, sorry for not pointing it out. We could just take a look at the generated code
Benchmarks.StructInline.NormalContainerUpdate, which uses the pattern[base + offset].edit:
Delete the example, which is wrong, it should use
refto work correctly. And after the change, the result is not impressive.edit:
And not only
inc, but alsoadd,imul,andandor. I believe there are more.[-]Calculate offset directly if possible[/-][+]peephole optimization for `lea` command[/+]SingleAccretion commentedon Mar 27, 2022
The trees in question:
And the proposition is to forward-substitute
V03into both of its uses.This will be a little tricky because in general it is not simple to know whether it'll be profitable or not early (e. g. on targets that don't have an RMW form of
ADDwith support for the[base + offset]addressing mode it would be a pessimization), when the ordinary forward substitution is run, so it'd have to be done late (in lowering), probably as part of forming the RMW form itself. This in turn will run into the problem of generally not knowing whether the local in question has any downstream uses (perhaps early lowering or rationalization could do ref counting to aid in that).JIT: forward-sub cheap address temps with multiple uses
JIT: forward-sub cheap address temps with multiple uses
JIT: forward-sub cheap address temps with multiple uses
JIT: forward-sub cheap address temps with multiple uses
JIT: forward-sub cheap address temps with multiple uses (#129312)
JIT: forward-sub cheap address temps with multiple uses (#129312)
JIT: forward-sub cheap address temps with multiple uses (dotnet#129312)