[llvm] [InlineCost] Consider the default branch when calculating cost (PR #77856)

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 12 21:44:24 PDT 2024


topperc wrote:

> > Someone just reported to me internally that this caused a benchmark regression in our downstream.
> 
> I have created #81723 to track some regressions. I haven't started my research yet because this case is too big. Can you provide a reproducible example? (It's okay if you can't.)
> 
> > I wonder if the default case was originally missing because it should just be a fallthrough after all the other cases are picked off. It shouldn't have any code. Given N destinations for a switch you only need N-1 compares and branches. Maybe an unreachable default should reduce the number of case clusters since one case cluster check wouldn't be needed? Though I'm not sure exactly what the backend generates.
> 
> I believe that we will create extra instructions if the default branch is reachable. See https://llvm.godbolt.org/z/x7azoP7eY.
> 
> ```assembly
> bar1: # @bar1
>   cmp rdi, 6
>   ja .LBB0_6
>   ...
> ```
> 
> This regression is likely coming from the select lookup vs compare instructions if I guess it right. See https://llvm.godbolt.org/z/cYdoef4cT.
> 
> I will prioritize the issue because it's becoming important. :) I'll report back my progress this weekend.

You're right that cmp/ja does come from the default when we use jump table lowering.  There is `4*InstrCost` on this line that may been intended to account for the cmp+ja+load+jmp? Otherwise I'm not sure why `4`.

```
      int64_t JTCost =
          static_cast<int64_t>(JumpTableSize) * InstrCost + 4 * InstrCost;
```

https://github.com/llvm/llvm-project/pull/77856


More information about the llvm-commits mailing list