dotnet / dotnet/runtime

JIT: If-conversion costing rejects `(cond ? 1 << a : 0)` even on RISCV where this gets explicitly targeted

Open
#127,110 5 comments 0 reactions 1 assignee Claimed by @jakobbotsch View on GitHub
area-CodeGen-coreclr
Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Description

We have this optimization in if-conversion that turns `SELECT(cond, 1 << a, 0)` into `cond << a`. It only runs on RISCV:
https://github.com/dotnet/runtime/blob/b70c35ed8a2e7ae0d91de76f4f5d26c2e7d2c6cd/src/coreclr/jit/ifconversion.cpp#L721-L728

However our current cost-analysis always rejects such patterns. Example:
```cs
int Cond1Shift(bool cond, int a)
{
return cond ? (1 << a) : 0;
}
```
If we look at JitDump:
```md
***** BB03 [0002]
STMT00001 ( 0x005[E--] ... 0x00B )
N006 ( 11, 9) [000009] -----+----- * RETURN int $VN.Void
N005 ( 10, 8) [000008] -----+--R-- \--* LSH int $143
N004 ( 1, 2) [000004] -----+----- +--* CNS_INT int 1 $41
N003 ( 5, 5) [000007] -----+----- \--* AND int $142
N001 ( 3, 2) [000005] -----+----- +--* LCL_VAR int V01 arg1 u:1 (last use) $c0
N002 ( 1, 2) [000006] -----+----- \--* CNS_INT int 31 $44
Skipping if-conversion that will evaluate RHS unconditionally at costs 1,10
```

So the optimization above for RISCV is essentially dead code. We always bail out earlier which is probably not wanted.

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.