Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Expand unboxing for Nullable<> in JIT#105073

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
EgorBo merged 6 commits intodotnet:mainfromEgorBo:nullable-expand
Jul 19, 2024
Merged

Conversation

@EgorBo
Copy link
Member

@EgorBoEgorBo commentedJul 18, 2024
edited
Loading

Closes#104954

Currently, unboxing for Nullable<> always go through a helper call, while we can inline it in JIT.

int?Test(objecto)=>(int?)o;

Current codegen:

; Method Proga:Test(System.Object):System.Nullable`1[int]:this (FullOpts)G_M11066_IG01:subrsp,40G_M11066_IG02:learcx,[rsp+0x20]movr8,rdxmovrdx,0x7FF7E261B750      ; System.Nullable`1[int]call     CORINFO_HELP_UNBOX_NULLABLEmovrax, qword ptr[rsp+0x20]G_M11066_IG03:addrsp,40ret; Total bytes of code: 37

New codegen:

; Method Proga:Test(System.Object):System.Nullable`1[int]:this (FullOpts)G_M11066_IG01:subrsp,40G_M11066_IG02:testrdx,rdx ;; is null?je       SHORT G_M11066_IG04G_M11066_IG03:movrax,0x7FF7E23674D0      ; System.Int32cmp      qword ptr[rdx],rax ;; is object's type int32?je       SHORT G_M11066_IG05jmp      SHORT G_M11066_IG08G_M11066_IG04:xoredx,edxmov      qword ptr[rsp+0x20],rdxjmp      SHORT G_M11066_IG06G_M11066_IG05:mov      byte  ptr[rsp+0x20],1moveax, dword ptr[rdx+0x08]mov      dword ptr[rsp+0x24],eaxG_M11066_IG06:movrax, qword ptr[rsp+0x20]G_M11066_IG07:addrsp,40ret; cold pathG_M11066_IG08:learcx,[rsp+0x20]movr8,rdxmovrdx,0x7FF7E263B750      ; System.Nullable`1[int]call     CORINFO_HELP_UNBOX_NULLABLEjmp      SHORT G_M11066_IG06; Total bytes of code: 82

Benchmark:

usingBenchmarkDotNet.Attributes;publicclassMyBench{[Benchmark][Arguments(42)][Arguments(null)]publicint?Unbox(objecto)=>(int?)o;}
BenchmarkDotNet v0.13.12, Ubuntu 22.04.4 LTS (Jammy Jellyfish)Unknown processor (which is "Ampere Altra")  Job-MMNLPR : .NET 9.0.0 (42.42.42.42424), Arm64 RyuJIT AdvSIMD  Job-QXOQTU : .NET 9.0.0 (42.42.42.42424), Arm64 RyuJIT AdvSIMD
MethodToolchainoMeanErrorRatio
UnboxMain?7.1195 ns0.0098 ns1.00
UnboxPR?0.6698 ns0.0072 ns0.09
UnboxMain4210.9406 ns0.0175 ns1.00
UnboxPR422.7002 ns0.0001 ns0.25

karb0f0s, h3xds1nz, and Suchiman reacted with rocket emoji
@ghostghost added the area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI labelJul 18, 2024
@EgorBo
Copy link
MemberAuthor

@EgorBot -intel -arm64

usingBenchmarkDotNet.Attributes;publicclassMyBench{[Benchmark][Arguments(42)][Arguments(null)]publicint?Unbox(objecto)=>(int?)o;}
EgorBot reacted with thumbs up emoji

@huoyaoyuan
Copy link
Member

Does it worth to recognizeRuntimeHelpers.Unbox_Nullable and do the same expansion?

Or, can we write the method in C# and make helpers inlinable? This will help more around nullables.

@EgorBo
Copy link
MemberAuthor

EgorBo commentedJul 18, 2024
edited
Loading

Or, can we write the method in C# and make helpers inlinable? This will help more around nullables.

Yes, that is an option, requires some effort to make helpers inlineable, they're currently not. I decided to do it in JIT since the actual change is like 30 LOC, the rest are comments. If we move it to an inlineable helper we can delete this I guess..
I'll take a look at that next week probably.

@EgorBot
Copy link

Benchmark results on Intel
BenchmarkDotNet v0.13.12, Ubuntu 22.04.4 LTS (Jammy Jellyfish)Intel Xeon Platinum 8370C CPU 2.80GHz, 1 CPU, 8 logical and 4 physical cores  Job-XHSURK : .NET 9.0.0 (42.42.42.42424), X64 RyuJIT AVX-512F+CD+BW+DQ+VL+VBMI  Job-ZDCIDK : .NET 9.0.0 (42.42.42.42424), X64 RyuJIT AVX-512F+CD+BW+DQ+VL+VBMI
MethodToolchainoMeanErrorRatio
UnboxMain?4.4659 ns0.0093 ns1.00
UnboxPR?0.5740 ns0.0003 ns0.13
UnboxMain427.1867 ns0.0395 ns1.00
UnboxPR423.9114 ns0.0016 ns0.54

BDN_Artifacts.zip

@EgorBot
Copy link

Benchmark results on Arm64
BenchmarkDotNet v0.13.12, Ubuntu 22.04.4 LTS (Jammy Jellyfish)Unknown processor  Job-MMNLPR : .NET 9.0.0 (42.42.42.42424), Arm64 RyuJIT AdvSIMD  Job-QXOQTU : .NET 9.0.0 (42.42.42.42424), Arm64 RyuJIT AdvSIMD
MethodToolchainoMeanErrorRatio
UnboxMain?7.1195 ns0.0098 ns1.00
UnboxPR?0.6698 ns0.0072 ns0.09
UnboxMain4210.9406 ns0.0175 ns1.00
UnboxPR422.7002 ns0.0001 ns0.25

BDN_Artifacts.zip

@EgorBo
Copy link
MemberAuthor

@MihuBot

@EgorBo
Copy link
MemberAuthor

@jakobbotsch@AndyAyersMS cc @dotnet/jit-contrib PTAL, the actual change is 40 LOC, benchmarks attached. Jit-diff:MihuBot/runtime-utils#541 obviously, a size regression, but there were also a few cases where jit can now fold theif we emit here, e.g. previously we never tried to foldCORINFO_HELP_UNBOX_NULLABLE(null) etc.

// Unbox the object to the result local:
//
// result._hasValue = true;
// result._value = MethodTableLookup(obj);
Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

oops, wrong comment, it'sobj->RawData

Copy link
Member

@AndyAyersMSAndyAyersMS left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Would it be worth limiting this to relatively small types, since will create both a zeroing and a copy?

@EgorBo
Copy link
MemberAuthor

EgorBo commentedJul 18, 2024
edited
Loading

Might be a good idea to limit it, yeah

@EgorBot -intel -arm64

usingSystem.Runtime.CompilerServices;usingBenchmarkDotNet.Attributes;publicclassMyBench2{[InlineArray(100)]publicstructLargeStruct{publiclongFld;}[Benchmark][Arguments(42)][Arguments(null)]publicLargeStruct?Unbox(objecto)=>(LargeStruct?)o;}
EgorBot reacted with thumbs up emoji

@dotnetdotnet deleted a comment fromEgorBotJul 18, 2024
@dotnetdotnet deleted a comment fromEgorBotJul 18, 2024
@EgorBot
Copy link

Benchmark results on Intel
BenchmarkDotNet v0.13.12, Ubuntu 22.04.4 LTS (Jammy Jellyfish)Intel Xeon Platinum 8370C CPU 2.80GHz, 1 CPU, 8 logical and 4 physical cores  Job-BUSFSY : .NET 9.0.0 (42.42.42.42424), X64 RyuJIT AVX-512F+CD+BW+DQ+VL+VBMI  Job-NIMINM : .NET 9.0.0 (42.42.42.42424), X64 RyuJIT AVX-512F+CD+BW+DQ+VL+VBMI
MethodToolchainoMeanErrorRatio
UnboxMain?27.91 ns0.013 ns1.00
UnboxPR?20.32 ns0.010 ns0.73
UnboxMain42NANA?
UnboxPR42NANA?
Benchmarks with issues:
MyBench2.Unbox: Job-BUSFSY(Toolchain=Main) [o=42]
MyBench2.Unbox: Job-NIMINM(Toolchain=PR) [o=42]

BDN_Artifacts.zip

@EgorBo
Copy link
MemberAuthor

@EgorBot -intel -arm64

usingSystem.Runtime.CompilerServices;usingBenchmarkDotNet.Attributes;publicclassMyBench2{[InlineArray(100)]publicstructLargeStruct{publiclongFld;}publicIEnumerable<object>TestData(){yieldreturnnull;yieldreturnnewLargeStruct();}[Benchmark][ArgumentsSource(nameof(TestData))]publicLargeStruct?Unbox(objecto)=>(LargeStruct?)o;}
EgorBot reacted with thumbs up emoji

@EgorBot
Copy link

Benchmark results on Intel
BenchmarkDotNet v0.13.12, Ubuntu 22.04.4 LTS (Jammy Jellyfish)Intel Xeon Platinum 8370C CPU 2.80GHz, 1 CPU, 8 logical and 4 physical cores  Job-GNOQYR : .NET 9.0.0 (42.42.42.42424), X64 RyuJIT AVX-512F+CD+BW+DQ+VL+VBMI  Job-LOMSQK : .NET 9.0.0 (42.42.42.42424), X64 RyuJIT AVX-512F+CD+BW+DQ+VL+VBMI
MethodToolchainoMeanErrorRatio
UnboxMain?31.62 ns0.038 ns1.00
UnboxPR?21.39 ns0.013 ns0.68
UnboxMainMyBench2+LargeStruct29.85 ns0.010 ns1.00
UnboxPRMyBench2+LargeStruct26.19 ns0.005 ns0.88

BDN_Artifacts.zip

@EgorBot
Copy link

Benchmark results on Arm64
BenchmarkDotNet v0.13.12, Ubuntu 22.04.4 LTS (Jammy Jellyfish)Unknown processor  Job-WWWDAN : .NET 9.0.0 (42.42.42.42424), Arm64 RyuJIT AdvSIMD  Job-YJHBTM : .NET 9.0.0 (42.42.42.42424), Arm64 RyuJIT AdvSIMD
MethodToolchainoMeanErrorRatio
UnboxMain?89.28 ns0.041 ns1.00
UnboxPR?39.46 ns0.023 ns0.44
UnboxMainMyBench2+LargeStruct48.70 ns0.052 ns1.00
UnboxPRMyBench2+LargeStruct41.85 ns0.209 ns0.86

BDN_Artifacts.zip

@dotnetdotnet deleted a comment fromEgorBotJul 18, 2024
Sign up for freeto subscribe to this conversation on GitHub. Already have an account?Sign in.

Reviewers

@AndyAyersMSAndyAyersMSAndyAyersMS approved these changes

@jakobbotschjakobbotschAwaiting requested review from jakobbotsch

Assignees

@EgorBoEgorBo

Labels

area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Optimize unboxing for nullable

4 participants

@EgorBo@huoyaoyuan@EgorBot@AndyAyersMS

[8]ページ先頭

©2009-2025 Movatter.jp