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

[Support][SLP] Support for fractional InstructionCost

Open bababuck opened this issue 11 months ago • 2 comments

When requesting cost information from TargetTransformInfo in the vectorizers, particular SLP, we request reciprocal throughput (TCK_RecipThroughput). However, InstructionCost only supports integer cost amounts, which doesn't align with reciprocal throughput on high performance machines which have a reciprocal throughput of less than 1 for many scalar instructions. As a result, we end up estimating the reciprocal throughput as 1, but this leads to large inaccuracies for large vector instructions. For instance, even if the reciprocal throughput for an add instruction should be 1/4, we estimate the cost as 1. So the scalar cost for a 16 element vector is estimated as 1 x 16 = 16, even though it should be 1/4 x 16 = 4.

To address this, a couple options could be to:

  • Update InstructionCost to support fractional values (I threw together a quick implementation of this that I can push if interested.)
  • Update InstructionCost to be backed by a floating point type
  • Update TargetTransformInfo cost functions to accept a vector of scalars

bababuck avatar Jan 05 '26 16:01 bababuck

@bababuck Please can make public your draft PR for fractional values?

A long time ago I did investigate adding a unsigned Count arg to the various getInstructionCost calls to better account for Count * Scalar instructions, but it was becoming a lot of work.

Another option I've considered several times is ways to refactor InstructionCost to better account for how we accumulate costs together, which aren't necessarily the same for a RecipThroughput vs SizeLatency CostKind for instance - we don't even check when accumulating InstructionCosts that they came from the same CostKind!

RKSimon avatar Jan 05 '26 18:01 RKSimon

@bababuck Please can make public your draft PR for fractional values?

https://github.com/llvm/llvm-project/commit/86a92c1b2131f22ec438176732ce3b348d32a71b. Very much a rough draft, more optimization is possible, and lots of testing is needed. I expect that this approach would be shot down due to the extra overhead (particularly the std::gcd() call).

A long time ago I did investigate adding a unsigned Count arg to the various getInstructionCost calls to better account for Count * Scalar instructions, but it was becoming a lot of work.

I has started down that path as well!

Another option I've considered several times is ways to refactor InstructionCost to better account for how we accumulate costs together, which aren't necessarily the same for a RecipThroughput vs SizeLatency CostKind for instance - we don't even check when accumulating InstructionCosts that they came from the same CostKind!

I didn't think about that part, it is just left up to the user to make sure they don't mix up their CostKind's. This does seem like an interesting path forward, since we could then add fractional support for RecipThroughtput (via whatever method) but avoid that extra overhead for the other CostKind's that don't need this added functionality.

bababuck avatar Jan 05 '26 18:01 bababuck