llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[x86-64] Conditional increment on non-carry non-overflow => speculative increment+cmov

Open Validark opened this issue 4 months ago • 12 comments

C Godbolt

#include <stdint.h>
uint64_t foo(uint64_t a, uint64_t b) {
    return a + ((b & 63) != 0);
}

Compiles to:

foo:
        and     esi, 63
        mov     rax, rdi
        cmp     rsi, 1
        sbb     rax, -1
        ret

Could be:

foo:
        lea     rax, [rdi + 1]   ; speculative a + 1
        test    esi, 63          ; ZF = 1 iff (idx & 63) == 0   (non-destructive)
        cmovz   rax, rdi         ; if zero, take the un-incremented a
        ret

Validark avatar Jul 07 '26 12:07 Validark

@llvm/issue-subscribers-backend-x86

Author: Niles Salter (Validark)

[C Godbolt](https://c.godbo.lt/#g:!((g:!((g:!((h:codeEditor,i:(filename:'1',fontScale:14,fontUsePx:'0',j:1,lang:___c,selection:(endColumn:1,endLineNumber:2,positionColumn:1,positionLineNumber:2,selectionStartColumn:1,selectionStartLineNumber:2,startColumn:1,startLineNumber:2),source:'%23include+%3Cstdint.h%3E%0Auint64_t+foo(uint64_t+a,+uint64_t+b)+%7B%0A++++return+a+%2B+((b+%26+63)+!!%3D+0)%3B%0A%7D%0A'),l:'5',n:'0',o:'C+source+%231',t:'0')),k:48.46943717911461,l:'4',n:'0',o:'',s:0,t:'0'),(g:!((h:compiler,i:(compiler:cclang_trunk,filters:(b:'0',binary:'1',binaryObject:'1',commentOnly:'0',debugCalls:'1',demangle:'0',directives:'0',execute:'1',intel:'0',libraryCode:'0',trim:'1',verboseDemangling:'0'),flagsViewOpen:'1',fontScale:14,fontUsePx:'0',j:1,lang:___c,libs:!(),options:'-O3+-march%3Dznver5',overrides:!(),selection:(endColumn:1,endLineNumber:1,positionColumn:1,positionLineNumber:1,selectionStartColumn:1,selectionStartLineNumber:1,startColumn:1,startLineNumber:1),source:1),l:'5',n:'0',o:'+x86-64+clang+(trunk)+(Editor+%231)',t:'0')),k:51.53056282088541,l:'4',m:100,n:'0',o:'',s:0,t:'0')),l:'2',n:'0',o:'',t:'0')),version:4)
#include &lt;stdint.h&gt;
uint64_t foo(uint64_t a, uint64_t b) {
    return a + ((b &amp; 63) != 0);
}

Compiles to:

foo:
        and     esi, 63
        mov     rax, rdi
        cmp     rsi, 1
        sbb     rax, -1
        ret

Could be:

foo:
        lea     rax, [rdi + 1]   ; speculative a + 1
        test    esi, 63          ; ZF = 1 iff (idx &amp; 63) == 0   (non-destructive)
        cmovz   rax, rdi         ; if zero, take the un-incremented a
        ret

define i64 @foo(i64 noundef %a, i64 noundef %b) {
  %and = and i64 %b, 63
  %cmp = icmp ne i64 %and, 0
  %conv = zext i1 %cmp to i64
  %add = add i64 %a, %conv
  ret i64 %add
}

RKSimon avatar Jul 07 '26 13:07 RKSimon

GCC bug

BreadTom avatar Jul 07 '26 17:07 BreadTom

Can I work on this?

kshitijvp avatar Jul 08 '26 20:07 kshitijvp

Can I work on this?

Sure, but I think the first thing we should do is confirm that its actually more performant - the instruction saving is minimal and cmov has a tendency to stall - do you have access to a variety of x86 boxes to compare perf?

RKSimon avatar Jul 09 '26 06:07 RKSimon

https://c.godbo.lt/z/13ebcveTM (APX)

Iterations:        100
Instructions:      400
Total Cycles:      110
Total uOps:        500

Dispatch Width:    6
uOps Per Cycle:    4.55
IPC:               3.64
Block RThroughput: 1.0

Instruction Info:
[1]: #uOps
[2]: Latency
[3]: RThroughput
[4]: MayLoad
[5]: MayStore
[6]: HasSideEffects (U)

[1]    [2]    [3]    [4]    [5]    [6]    Instructions:
 1      1     1.00                        lea   rax, [rdi + 1]
 1      2     0.20                        test  esi, 63
 1      1     0.50                        cmove rax, rdi
 2      7     0.50                  U     ret
Iterations:        100
Instructions:      400
Total Cycles:      205
Total uOps:        500

Dispatch Width:    6
uOps Per Cycle:    2.44
IPC:               1.95
Block RThroughput: 1.0


Instruction Info:
[1]: #uOps
[2]: Latency
[3]: RThroughput
[4]: MayLoad
[5]: MayStore
[6]: HasSideEffects (U)

[1]    [2]    [3]    [4]    [5]    [6]    Instructions:
 1      2     0.20                        and	esi, 63
 1      1     0.20                        cmp	rsi, 1
 1      1     0.50                        sbb	rax, rdi, -1
 2      7     0.50                  U     ret

Andarwinux avatar Jul 09 '26 06:07 Andarwinux

This is a good case of when I'd like to see comparative perf in real life examples tbh and not just rely on llvm-mca, the scheduler models often struggle with some of these nuances.

RKSimon avatar Jul 09 '26 07:07 RKSimon

LEA is tricky given each uarch behaves. Given that, I think we should start modeling the LEAs ops wise to make sure these can be captured in llvm-mca as well.

ganeshgit avatar Jul 09 '26 07:07 ganeshgit

Can I work on this?

Sure, but I think the first thing we should do is confirm that its actually more performant - the instruction saving is minimal and cmov has a tendency to stall - do you have access to a variety of x86 boxes to compare perf?

Hey I don't have a variety of x86 machines, have an AMD Zen Version Machine.

kshitijvp avatar Jul 09 '26 07:07 kshitijvp

Can I work on this?

Sure, but I think the first thing we should do is confirm that its actually more performant - the instruction saving is minimal and cmov has a tendency to stall - do you have access to a variety of x86 boxes to compare perf?

Even if cmov stalls on current architectures, I definitely think this has to be a valuable optimization at some point

Validark avatar Jul 09 '26 10:07 Validark

No arguments there - but this might need to be cost driven in a fixup pass - and that needs decent scheduler modeling

RKSimon avatar Jul 09 '26 10:07 RKSimon

Hey @RKSimon as a first time contributor to Backend I am feeling this to be too difficult for me so unassigning myself, but would be a great learning experience once someones puts the PR. Would like to solve some Good First Issues before the harder issues. Apologies for the delay.

kshitijvp avatar Jul 21 '26 05:07 kshitijvp