dotnet / dotnet/runtime

JIT: Simple float comparison with conditional assign is not hoisted out of loop

Open
#120,963 6 comments 1 reaction 0 assignees View on GitHub
area-CodeGen-coreclr
Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Description

I expect `if (a < b) axis = 1;` to be moved before the loop (and then generate `vucomiss` + `seta`).
Note: Moving `int axis = 0;` into the loop does not change it.
https://godbolt.org/z/EWbsbE967
```cs
static void Test(float a, float b, int count)
{
int axis = 0;
for (int i = 0; i < count; i++)
{
if (a < b) axis = 1;

Consume(axis);
}

static extern void Consume(int _);
}
```
Assembly:
```assembly
Program:Test(float,float,int) (FullOpts):
push rbp
push r15
push rbx
sub rsp, 16
lea rbp, [rsp+0x20]
xor ebx, ebx
test edi, edi
jle SHORT G_M43824_IG07
mov r15d, edi
G_M43824_IG04: ;; offset=0x0016
vmovss dword ptr [rbp-0x18], xmm1
vmovss dword ptr [rbp-0x14], xmm0
vucomiss xmm1, xmm0
jbe SHORT G_M43824_IG06
mov ebx, 1
G_M43824_IG06: ;; offset=0x002B
mov edi, ebx
call [Program:g__Consume|0_0(int)]
dec r15d
vmovss xmm0, dword ptr [rbp-0x14]
vmovss xmm1, dword ptr [rbp-0x18]
jne SHORT G_M43824_IG04
G_M43824_IG07: ;; offset=0x0042
add rsp, 16
pop rbx
pop r15
pop rbp
ret
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.