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

[VectorCombine] foldShuffleOfBinops - failure to track OperandValueInfo data in getArithmeticInstrCost calls

Open RKSimon opened this issue 1 year ago • 7 comments

define <16 x i16> @concat_uniform_shift_v16i16_v8i16(<8 x i16> %a0, <8 x i16> %a1) {
  %v0 = shl <8 x i16> %a0, splat (i16 7)
  %v1 = shl <8 x i16> %a1, splat (i16 7)
  %res  = shufflevector <8 x i16> %v0, <8 x i16> %v1, <16 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15>
  ret <16 x i16> %res
}

-passes=vector-combine -mtriple=x86_64-- -mcpu=znver2

define <16 x i16> @concat_uniform_shift_v16i16_v8i16(<8 x i16> %a0, <8 x i16> %a1) #0 {
  %v0 = shl <8 x i16> %a0, splat (i16 7)
  %v1 = shl <8 x i16> %a1, splat (i16 7)
  %res = shufflevector <8 x i16> %v0, <8 x i16> %v1, <16 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15>
  ret <16 x i16> %res
}

when it should be:

define <16 x i16> @concat_uniform_shift_v16i16_v8i16(<8 x i16> %a0, <8 x i16> %a1) #0 {
  %v = shufflevector <8 x i16> %a0, <8 x i16> %a1, <16 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15>
  %res = shl <16 x i16> %v, splat (i16 7)
  ret <16 x i16> %res
}

Vectorcombine fails to concatenate these shifts as the "NewCost" getArithmeticInstrCost calls fail to include any TTI::OperandValueInfo data - both of the shift amounts are OK_UniformConstantValue, so at the very least we should merge these into OK_NonUniformConstantValue.

A possible starting point would be to add TTI::getOperandInfo calls in the binop NewCost calculation for both LHS/RHS ops - and create some kind of OperandValueInfo::mergeInfo(X, Y) static helper that merge common info data (all constant, all powerof2 etc - although all uniform can't be done this way!).

But for this particular case, the RHS operands are identical - so we don't need to merge, we can just use the original OperandValueInfo.

foldPermuteOfBinops has a similar problem but it looks like OldCost and NewCost both use getArithmeticInstrCost - which is weird as I'd expect OldCost to use getInstructionCost at least.

RKSimon avatar Dec 03 '25 16:12 RKSimon

Hi!

This issue may be a good introductory issue for people new to working on LLVM. If you would like to work on this issue, your first steps are:

  1. Check that no other contributor is working on this issue. If someone is assigned to the issue or claimed to be working on it, ping the person. After one week without a response, the assignee may be changed.
  2. Leave a comment indicating that you are working on the issue, or just create a pull request after following the steps below. Mention this issue in the description of the pull request.
  3. Fix the issue locally.
  4. Run the test suite locally. Remember that the subdirectories under test/ create fine-grained testing targets, so you can e.g. use make check-clang-ast to only run Clang's AST tests.
  5. Create a Git commit.
  6. Run git clang-format HEAD~1 to format your changes.
  7. Open a pull request to the upstream repository on GitHub. Detailed instructions can be found in GitHub's documentation. Mention this issue in the description of the pull request.

If you have any further questions about this issue, don't hesitate to ask via a comment in the thread below.

llvmbot avatar Dec 03 '25 16:12 llvmbot

@llvm/issue-subscribers-good-first-issue

Author: Simon Pilgrim (RKSimon)

``` define <16 x i16> @concat_uniform_shift_v16i16_v8i16(<8 x i16> %a0, <8 x i16> %a1) { %v0 = shl <8 x i16> %a0, splat (i16 7) %v1 = shl <8 x i16> %a1, splat (i16 7) %res = shufflevector <8 x i16> %v0, <8 x i16> %v1, <16 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15> ret <16 x i16> %res } ``` -passes=vector-combine -mtriple=x86_64-- -mcpu=znver2 ``` define <16 x i16> @concat_uniform_shift_v16i16_v8i16(<8 x i16> %a0, <8 x i16> %a1) #0 { %v0 = shl <8 x i16> %a0, splat (i16 7) %v1 = shl <8 x i16> %a1, splat (i16 7) %res = shufflevector <8 x i16> %v0, <8 x i16> %v1, <16 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15> ret <16 x i16> %res } ``` when it should be: ``` define <16 x i16> @concat_uniform_shift_v16i16_v8i16(<8 x i16> %a0, <8 x i16> %a1) #0 { %v = shufflevector <8 x i16> %a0, <8 x i16> %a1, <16 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15> %res = shl <16 x i16> %v, splat (i16 7) ret <16 x i16> %res } ```

Vectorcombine fails to concatenate these shifts as the "NewCost" getArithmeticInstrCost calls fail to include any TTI::OperandValueInfo data - both of the shift amounts are OK_UniformConstantValue, so at the very least we should merge these into OK_NonUniformConstantValue.

A possible starting point would be to add TTI::getOperandInfo calls in the binop NewCost calculation for both LHS/RHS ops - and create some kind of OperandValueInfo::mergeInfo(X, Y) static helper that merge common info data (all constant, all powerof2 etc - although all uniform can't be done this way!).

foldPermuteOfBinops has a similar problem but it looks like OldCost and NewCost both use getArithmeticInstrCost - which is weird as I'd expect OldCost to use getInstructionCost at least.

llvmbot avatar Dec 03 '25 16:12 llvmbot

Hi, may I take on this issue?

KavinTheG avatar Dec 03 '25 16:12 KavinTheG

Hi, may I assist on this issue?

Arka-009 avatar Dec 06 '25 06:12 Arka-009

Sorry for the late response, and thanks for offering to assist! But I'd prefer to work by myself if that's alright.

KavinTheG avatar Dec 11 '25 22:12 KavinTheG

I’d like to work on this . It is in my reach .

volalpha avatar Dec 18 '25 17:12 volalpha

I’d like to work on this . It is in my reach .

#171934 is almost done, but I'll see if I can create some more GFIs soon

RKSimon avatar Dec 18 '25 17:12 RKSimon