dotnet / dotnet/runtime

JIT: Hoist common mem access out of Then/Else case

Open
#132,451 2 comments 0 reactions 0 assignees View on GitHub
area-CodeGen-coreclr
Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Description

Simple example: https://godbolt.org/z/v9788PWh9
`mem` is accessed first in both branches. We should hoist it and consequently do if-conversion.
```cs
static int Hoist(bool cond, ref int mem)
{
int op2;
if (cond)
{
op2 = mem + 1;
}
else
{
op2 = mem + 2;
}
return op2;
}
```
```assembly
Program:Hoist(bool,byref):int (FullOpts):
push rbp
mov rbp, rsp
test dil, dil
jne SHORT G_M31389_IG04
mov eax, dword ptr [rsi]
add eax, 2
jmp SHORT G_M31389_IG05
G_M31389_IG04: ;; offset=0x0010
mov eax, dword ptr [rsi]
inc eax
G_M31389_IG05: ;; offset=0x0014
pop rbp
ret
```

Contributor guide

Open the contributing guide

Research direction

Start with the C# example and its Compiler Explorer output at the linked Godbolt URL, then trace the JIT optimization area responsible for conditional memory accesses. Done means the shared mem load is hoisted before the branches and the generated assembly shows if-conversion without changing the example's result.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
compilers, performance
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.