Issue 238838
Summary [SelectOpt] `shouldTreatInstructionLikeSelect` is only checked for the first select of a group
Labels
Assignees
Reporter timurgol007
    SelectOptimize groups consecutive selects that share a condition, and `collectSelectGroups` asks `TTI::shouldTreatInstructionLikeSelect` only about the first one. The other selects with the same condition join the group without that check, but they still take part in the decision whether to turn the group into a branch.

In the inner-loop analysis, every member of a group is costed as if it were already a branch. In the example below, `%k.next` is a select of two constants, which the base TTI hook rejects. It would never start a group on its own, but here it follows `%s`, so it is costed as a branch. It also sits on the loop-carried chain `%k -> sdiv -> icmp -> %k.next`, so treating it as a branch takes that whole chain off the loop's critical path, and that is what makes the conversion look profitable:

```llvm
define void @g(ptr %a, ptr %b, ptr %out, i64 %n, i32 %y) {
entry:
  br label %loop

loop:
 %i = phi i64 [ 0, %entry ], [ %i.next, %loop ]
  %k = phi i32 [ 5, %entry ], [ %k.next, %loop ]
  %pa = getelementptr i32, ptr %a, i64 %i
  %x = load i32, ptr %pa
  %pb = getelementptr i32, ptr %b, i64 %i
  %ld = load i32, ptr %pb
  %t = sdiv i32 %x, %k
  %c = icmp sgt i32 %t, %y
  %s = select i1 %c, i32 %x, i32 %ld
  %k.next = select i1 %c, i32 5, i32 7
  %po = getelementptr i32, ptr %out, i64 %i
  store i32 %s, ptr %po
  %i.next = add i64 %i, 1
  %ec = icmp eq i64 %i.next, %n
  br i1 %ec, label %exit, label %loop

exit:
  ret void
}
```

With `llc -O3 -mtriple=aarch64 -mcpu=neoverse-v2 -pass-remarks=select-optimize` the pass reports:

```
Profitable to convert to branch (loop analysis). BranchCost=6.5, SelectCost=16.0.
```

If `%k.next` uses a different condition (so it is not part of `%s`'s group), the same loop is left alone:

```
No select conversion in the loop due to small reduction of loop's critical path. Gain=1.0
```

Generated code with the pass enabled (`-aarch64-select-opt=true`, the default):
```asm
g:
	mov	w8, #5
	b	.LBB0_2
.LBB0_1:                                // %select.false.sink
	ldr	w9, [x1], #4
	mov	w8, #7
	str	w9, [x2], #4
	subs	x3, x3, #1
	add	x0, x0, #4
	b.eq	.LBB0_4
.LBB0_2: // %loop
	ldr	w9, [x0]
	sdiv	w8, w9, w8
	cmp	w8, w4
	b.le	.LBB0_1
	mov	w8, #5
	str	w9, [x2], #4
	subs	x3, x3, #1
	add	x1, x1, #4
	add	x0, x0, #4
	b.ne	.LBB0_2
.LBB0_4:                                // %exit
	ret
```

And with the pass disabled (`-aarch64-select-opt=false`):
```asm
g:
	mov	w8, #5
	mov	w9, #7
	mov	w10, #5
.LBB0_1:                                // %loop
	ldr	w11, [x0], #4
	ldr	w12, [x1], #4
	sdiv	w10, w11, w10
	cmp	w10, w4
	csel	w11, w11, w12, gt
	csel	w10, w8, w9, gt
	str	w11, [x2], #4
	subs	x3, x3, #1
	b.ne	.LBB0_1
	ret
```

For a rough idea of the cost, I ran `llvm-mca -mtriple=aarch64 -mcpu=neoverse-v2` on the loop bodies. The `csel` loop takes 9.08 cycles per iteration, and either path of the branch version takes 7.28. llvm-mca assumes every branch is predicted correctly, though. With Neoverse-V2's misprediction penalty of 10 cycles, the branch version only comes out ahead if fewer than about 18% of the branches mispredict. At the 25% rate the pass itself assumes by default, it is already slower (7.28 + 0.25 * 10 = 9.78 cycles), and here the branch depends on freshly loaded data.

So the question is what should happen to a follower that the target doesn't want to treat as a select. I see three options:
1. Keep doing what we do now: convert it together with the group and cost it as a branch. `selectopt-not.ll` (the minloc tests) relies on this. There, the rejected follower is a logical-or flag, and converting it is exactly what breaks the loop-carried dependency and makes the transform worthwhile.
2. Leave it out of the group and cost it as a plain select. Then `@g` above isn't converted, but the minloc loops aren't either. Also, the pass moves non-member instructions above the group when converting it. That isn't possible for a follower that uses an earlier member of the group, so the group would have to be split in that case.
3. Let it block the whole group. `X86CmovConversion` does something similar: it gives up on a CMOV group if any CMOV in it looks like part of a tree search. This would cover selects that should never become branches, but it probably needs a separate signal rather than reusing `shouldTreatInstructionLikeSelect`.

I'd be happy to prepare a patch for this and would like to hear opinions on which direction to take.

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to