Commit 87b470c
authored
JIT: Devirtualize non-shared generic virtual methods (#122023)
Enable devirtualization support for generic virtual methods.
When we see a base method having a method instantiation, we use
`FindOrCreateAssociatedMethodDesc` to obtain the devirted method.
Also introduced a jit knob so that it can be turned off at any time.
AOT support is not included in this PR, which needs additional work in
managed type system.
Also, if we end up with an instantiating stub (i.e. a shared generic
method that requires runtime lookup), we don't have the correct generic
context so we need to bail out for now.
Codegen example:
```cs
public class IntProcessor : VirtualGenericClass, IVritualGenericInterface
{
public override void Process<T>(T item)
{
Console.WriteLine(item.ToString());
}
}
public interface IVritualGenericInterface
{
void Process<T>(T item) where T : notnull;
}
public static void Test<T>(IVritualGenericInterface ifce, T item) where T : notnull
{
ifce.Process(item);
}
public static void Test<T>(VirtualGenericClass baseClass, T item) where T : notnull
{
baseClass.Process(item);
}
static void Test()
{
IVritualGenericInterface i = new IntProcessor();
Test(i, 42);
VirtualGenericClass c = new IntProcessor();
Test(c, 42);
}
```
Codegen diff:
```diff
G_M27646_IG01: ;; offset=0x0000
- push rsi
push rbx
- sub rsp, 40
+ sub rsp, 32
- ;; size=6 bbWeight=1 PerfScore 2.25
+ ;; size=5 bbWeight=1 PerfScore 1.25
-G_M27646_IG02: ;; offset=0x0006
+G_M27646_IG02: ;; offset=0x0005
- mov rbx, 0x7FFE0803F318 ; Program+IntProcessor
+ mov rbx, 0x221CA429C38 ; 'System.Int32'
mov rcx, rbx
- call CORINFO_HELP_NEWSFAST
+ call [System.Console:WriteLine(System.Object)]
- mov rsi, rax
+ mov ecx, 42
- mov rcx, rsi
+ call [System.Number:Int32ToDecStr(int):System.String]
- mov rdx, 0x7FFE0803F100 ; Program+IVritualGenericInterface
+ mov rcx, rax
- mov r8, 0x7FFE0803F6A8 ; token handle
+ call [System.Console:WriteLine(System.String)]
- call CORINFO_HELP_VIRTUAL_FUNC_PTR
- mov rcx, rsi
- mov edx, 42
- call rax
mov rcx, rbx
- call CORINFO_HELP_NEWSFAST
+ call [System.Console:WriteLine(System.Object)]
- mov rbx, rax
+ mov ecx, 42
- mov rcx, rbx
+ call [System.Number:Int32ToDecStr(int):System.String]
- mov rdx, 0x7FFE0803EF30 ; Program+VirtualGenericClass
+ mov rcx, rax
- mov r8, 0x7FFE0803F8A8 ; token handle
+ call [System.Console:WriteLine(System.String)]
- call CORINFO_HELP_VIRTUAL_FUNC_PTR
- mov rcx, rbx
- mov edx, 42
- call rax
nop
- ;; size=109 bbWeight=1 PerfScore 14.00
+ ;; size=69 bbWeight=1 PerfScore 20.00
-G_M27646_IG03: ;; offset=0x0073
+G_M27646_IG03: ;; offset=0x004A
- add rsp, 40
+ add rsp, 32
pop rbx
- pop rsi
ret
- ;; size=7 bbWeight=1 PerfScore 2.25
+ ;; size=6 bbWeight=1 PerfScore 1.75
```
Contributes to #112596
cc: @dotnet/jit-contrib1 parent 34c3fee commit 87b470c
File tree
18 files changed
+888
-101
lines changed- src
- coreclr
- inc
- jit
- tools
- Common/JitInterface
- vm
- tests/JIT/Generics/VirtualMethods
18 files changed
+888
-101
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1540 | 1540 | | |
1541 | 1541 | | |
1542 | 1542 | | |
1543 | | - | |
| 1543 | + | |
1544 | 1544 | | |
1545 | 1545 | | |
1546 | 1546 | | |
| |||
1556 | 1556 | | |
1557 | 1557 | | |
1558 | 1558 | | |
| 1559 | + | |
1559 | 1560 | | |
1560 | 1561 | | |
1561 | 1562 | | |
| |||
1578 | 1579 | | |
1579 | 1580 | | |
1580 | 1581 | | |
1581 | | - | |
| 1582 | + | |
1582 | 1583 | | |
1583 | 1584 | | |
1584 | 1585 | | |
| |||
1587 | 1588 | | |
1588 | 1589 | | |
1589 | 1590 | | |
1590 | | - | |
| 1591 | + | |
1591 | 1592 | | |
1592 | 1593 | | |
1593 | 1594 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
37 | 37 | | |
38 | 38 | | |
39 | 39 | | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | | - | |
44 | | - | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
45 | 45 | | |
46 | 46 | | |
47 | 47 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10510 | 10510 | | |
10511 | 10511 | | |
10512 | 10512 | | |
| 10513 | + | |
| 10514 | + | |
10513 | 10515 | | |
10514 | 10516 | | |
10515 | 10517 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
544 | 544 | | |
545 | 545 | | |
546 | 546 | | |
547 | | - | |
548 | | - | |
549 | | - | |
550 | | - | |
551 | | - | |
552 | | - | |
553 | | - | |
554 | | - | |
555 | | - | |
556 | | - | |
557 | | - | |
558 | | - | |
559 | | - | |
560 | | - | |
561 | | - | |
562 | | - | |
563 | | - | |
564 | | - | |
565 | | - | |
566 | | - | |
567 | | - | |
568 | | - | |
569 | | - | |
570 | | - | |
571 | | - | |
572 | | - | |
573 | 547 | | |
574 | 548 | | |
575 | 549 | | |
| |||
612 | 586 | | |
613 | 587 | | |
614 | 588 | | |
615 | | - | |
616 | | - | |
617 | | - | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
618 | 592 | | |
619 | 593 | | |
620 | 594 | | |
| |||
632 | 606 | | |
633 | 607 | | |
634 | 608 | | |
635 | | - | |
636 | 609 | | |
637 | 610 | | |
638 | 611 | | |
639 | 612 | | |
| 613 | + | |
| 614 | + | |
640 | 615 | | |
641 | 616 | | |
642 | 617 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2367 | 2367 | | |
2368 | 2368 | | |
2369 | 2369 | | |
2370 | | - | |
| 2370 | + | |
| 2371 | + | |
2371 | 2372 | | |
2372 | 2373 | | |
2373 | 2374 | | |
2374 | 2375 | | |
2375 | | - | |
| 2376 | + | |
2376 | 2377 | | |
2377 | | - | |
2378 | | - | |
2379 | | - | |
| 2378 | + | |
| 2379 | + | |
| 2380 | + | |
| 2381 | + | |
| 2382 | + | |
| 2383 | + | |
| 2384 | + | |
| 2385 | + | |
| 2386 | + | |
| 2387 | + | |
| 2388 | + | |
| 2389 | + | |
| 2390 | + | |
| 2391 | + | |
| 2392 | + | |
| 2393 | + | |
| 2394 | + | |
| 2395 | + | |
| 2396 | + | |
| 2397 | + | |
| 2398 | + | |
| 2399 | + | |
| 2400 | + | |
| 2401 | + | |
| 2402 | + | |
| 2403 | + | |
| 2404 | + | |
| 2405 | + | |
| 2406 | + | |
| 2407 | + | |
| 2408 | + | |
| 2409 | + | |
| 2410 | + | |
| 2411 | + | |
| 2412 | + | |
| 2413 | + | |
2380 | 2414 | | |
2381 | 2415 | | |
2382 | 2416 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5262 | 5262 | | |
5263 | 5263 | | |
5264 | 5264 | | |
| 5265 | + | |
| 5266 | + | |
| 5267 | + | |
| 5268 | + | |
| 5269 | + | |
5265 | 5270 | | |
5266 | | - | |
| 5271 | + | |
5267 | 5272 | | |
5268 | 5273 | | |
5269 | 5274 | | |
| |||
0 commit comments