llvm / llvm/llvm-project

[X86] Vector conditional increment needlessly lengthens the dependency chain when the condition needs inversion

Open
#167,441 2 comments 0 reactions 0 assignees View on GitHub
backend:X86 missed-optimization
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

Due to gaps in support on x86, some vector comparisons have to be implemented by inverting the opposite condition. clang avoids that when implementing a conditional increment by instead conditionally decrementing when the condition isn't met then always incrementing.

This is unoptimal as the inversion is logically independent with the value being incremented, well the current implementation will always have a longer dependency chain due to the serial decrement then increment:

```diff
shiftLeft2_incIfNotZero:
psllw xmm0, 2
pand xmm0, xmmword ptr [rip + .LCPI0_0]
pxor xmm2, xmm2
pcmpeqb xmm2, xmm1
- paddb xmm0, xmm2
pcmpeqd xmm1, xmm1
+ pxor xmm1, xmm2
psubb xmm0, xmm1
ret
```

https://godbolt.org/z/rr7jjaxdn

Note that doesn't happen when implementing a conditional decrement.

Contributor guide

Open the contributing guide

Research direction

Start with the Compiler Explorer case linked in the issue and reproduce the clang x86 output for conditional increment and conditional decrement. Trace the relevant clang lowering path; done means conditional increment avoids the serial decrement-then-increment dependency while preserving the existing behavior for conditional decrement.

Written by the indexing model from the issue text.

Assessment

Domain
compilers, performance
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.