240 Commits

Author SHA1 Message Date
Simon Pilgrim
7e054c33d4 [VectorCombine] foldShuffleOfCastops - don't restrict to oneuse but compare total costs instead
Some casts (especially bitcasts but others as well) are incredibly cheap (or free), so don't limit the shuffle(cast(x),cast(y)) -> cast(shuffle(x,y)) to oneuse cases, but instead compare the total before/after costs of possibly repeating some casts.
2024-07-08 14:57:51 +01:00
Elvis Wang
4762f3bab0
[RISCV][TTI] Add cost of type based binOp VP intrinsics with functionalOPC. (#93435)
Intrinsics not supported in the backend will fall Into BasicTTIImpl,
which will check if the VP intrinsic is a type based instruction.
All type based instruction will fall into the
`getTypeBasedIntrinsicInstrCost()` which doesn't support instruction
with scalable vector type.

This patch adds the instruction cost for type based binOp VP intrinsic
instructions in the backend to get the valid instruction costs.
The cost of type based binOp VP intrinsics will be same as their non-VP
counterpart.
2024-07-05 08:13:18 +08:00
David Green
76c8e1d857 [VectorCombine] Guard against the lane zero select predicate being scalar
All but the first lane was being checked, but this could leave the first lane
with a scalar select predicate. This just extends the check to make sure the
types are all the same
2024-06-28 17:27:16 +01:00
David Green
efa8463ab9
[VectorCombine] Add free concats to shuffleToIdentity. (#94954)
This is another relatively small adjustment to shuffleToIdentity, which
has had a few knock-one effects to need a few more changes. It attempts
to detect free concats, that will be legalized to multiple vector
operations. For example if the lanes are '[a[0], a[1], b[0], b[1]]' and
a and b are v2f64 under aarch64.

In order to do this:
- isFreeConcat detects whether the input has piece-wise identities from
multiple inputs that can become a concat.
- A tree of concat shuffles is created to concatenate the input values
into a single vector. This is a little different to most other inputs as
there are created from multiple values that are being combined together,
and we cannot rely on the Lane0 insert location always being valid.
- The insert location is changed to the original location instead of
updating per item, which ensure it is valid due to the order that we
visit and create items.
2024-06-25 07:55:08 +01:00
David Green
a1bdb01656 [VectorCombine] Change shuffleToIdentity to use Use. NFC
When looking up through shuffles, a Value can be multiple different leaf types
(for example an identity from one position, a splat from another). We currently
detect this by recalculating which type of leaf it is when generating, but as
more types of leafs are added (#94954) this doesn't scale very well.

This patch switches it to use Use, not Value, to more accurately detect which
type of leaf each Use should have.
2024-06-17 15:25:33 +01:00
Simon Pilgrim
22530e7985 [CostModel][X86] Update vXi8 mul costs for AVX512BW/AVX2/AVX1/SSE
Later levels were inheriting some of the worst case costs from SSE/AVX1 etc.

Based off llvm-mca numbers from the check_cost_tables.py script in https://github.com/RKSimon/llvm-scripts

Cleanup prep work for #90748
2024-06-16 07:27:35 +01:00
David Green
d1b5a4b0c5 [VectorCombine] Add tests for shuffleToIdentity of concats. NFC 2024-06-07 22:28:00 +01:00
Henry Jiang
b5b61cce96
[VectorCombine] Preserves the maximal legal FPMathFlags during foldShuffleToIdentity (#94295)
The `VectorCombine::foldShuffleToIdentity` does not preserve fast math
flags when folding the shuffle, leading to unexpected vectorized result
and missed optimizations with FMA instructions.

We can conservatively take the maximal legal set of fast math flags
whenever we fold shuffles to identity to enable further optimizations in
the backend.

---------

Co-authored-by: Henry Jiang <henry.jiang1@ibm.com>
2024-06-05 11:35:37 -04:00
David Green
93d8d74ae6
[VectorCombine] Remove requirement for Instructions in shuffleToIdentity (#93543)
This removes the check that both operands of the original shuffle are
instructions, which is a relic from a previous version that held more
variables as Instructions.
2024-05-29 09:36:53 +01:00
David Green
1c6746e2db
[VectorCombine] Add support for zext/sext/trunc to shuffleToIdentity (#92696)
This is one of the simple additions to shuffleToIdentity that help it
look through intermediate zext/sext instructions.
2024-05-29 08:56:41 +01:00
David Green
516a9f5183
[VectorCombine] Add Cmp and Select for shuffleToIdentity (#92794)
Other than some additional checks needed for compare predicates and
selects with scalar condition operands, these are relatively simple
additions to what already exists.
2024-05-28 13:10:19 +01:00
David Green
f53f2a8c92
[VectorCombine] Add constant splat handling for shuffleToIdentity (#92797)
This just adds splat constants, which can be treated like any other
splat which hopefully makes them very simple. It does not try to handle
more complex constant vectors yet, just the more common splats.
2024-05-28 10:55:57 +01:00
Ramkumar Ramachandra
63d81311a2
VectorCombine: add tests written for InstSimplify (#92776)
2141907 (InstSimplify: increase shufflevector test coverage) was
recently merged as a pre-commit test for some work that was misguided.
It turns out that InstSimplify can never work on those tests, but the
tests are useful nevertheless; move them to VectorCombine to support the
development of VectorCombine::foldShuffleToIdentity.
2024-05-21 08:06:34 +01:00
David Green
285f1392da [VectorCombine] Some more tests for different cmp's and fp consts. NFC 2024-05-20 18:27:33 +01:00
David Green
8b8a38a7b4 [VectorCombine] Additional extend tests for shuffleToIdentity. NFC 2024-05-19 10:18:26 +01:00
David Green
c3677e4522 [VectorCombine] Don't transform single shuffles in shuffleToIdentity
This will help in later patches where the checks for operands being
instructions is removed, and might help not remove unnecessary poison lanes.
2024-05-18 23:37:55 +01:00
Jay Foad
1650f1b3d7
Fix typo "indicies" (#92232) 2024-05-15 13:10:16 +01:00
David Green
b7ed097f29
[VectorCombine] Add intrinsics handling to shuffleToIdentity (#91000)
This is probably the most involved addition, as it tries to make use of
isTriviallyVectorizable with isVectorIntrinsicWithScalarOpAtArg to handle a
number of different intrinsics that are all lane-wise. Additional tests have
been added for some of the different intrinsics from
isVectorIntrinsicWithScalarOpAtArg / isVectorIntrinsicWithOverloadTypeAtArg.
2024-05-12 20:31:11 +01:00
Ramkumar Ramachandra
57b9c15227
VectorCombine: fix logical error after m_Trunc match (#91201)
The matcher m_Trunc() matches an Operator with a given Opcode, which
could either be an Instruction or ConstExpr.
VectorCombine::foldTruncFromReductions() incorrectly assumes that the
pattern matched is always an Instruction, and attempts a cast. Fix this.

Fixes #88796.
2024-05-08 09:47:55 +01:00
Ramkumar Ramachandra
9ef28cf88c
VectorCombine: add test for crash #88796 (#91200) 2024-05-08 09:43:49 +01:00
David Green
d145f40963 [VectorCombine] shuffleToIdentity - guard against call instructions.
The shuffleToIdentity fold needs to be a bit more careful about the difference
between call instructions and intrinsics. The second can be handled, but the
first should result in bailing out. This patch also adds some extra intrinsic
tests from #91000.

Fixes #91078
2024-05-05 10:47:11 +01:00
David Green
a4d10266d2
[VectorCombine] Add foldShuffleToIdentity (#88693)
This patch adds a basic version of a combine that attempts to remove
shuffles that when combined simplify away to an identity shuffle. For
example:
%ab = shufflevector <8 x half> %a, <8 x half> poison, <4 x i32> <i32 3,
i32 2, i32 1, i32 0>
%at = shufflevector <8 x half> %a, <8 x half> poison, <4 x i32> <i32 7,
i32 6, i32 5, i32 4>
  %abt = fneg <4 x half> %at
  %abb = fneg <4 x half> %ab
%r = shufflevector <4 x half> %abt, <4 x half> %abb, <8 x i32> <i32 7,
i32 6, i32 5, i32 4, i32 3, i32 2, i32 1, i32 0>
By looking through the shuffles and fneg, it can be simplified to:
  %r = fneg <8 x half> %a

The code tracks each lane starting from the original shuffle, keeping a
track of a vector of {src, idx}. As we propagate up through the
instructions we will either look through intermediate instructions
(binops and unops) or see a collections of lanes that all have the same
src and incrementing idx (an identity). We can also see a single value
with identical lanes, which we can treat like a splat.

Only the basic version is added here, handling identities, splats,
binops and unops. In follow-up patches other instructions can be added
such as constants, intrinsics, cmp/sel and zext/sext/trunc.
2024-05-03 19:14:38 +01:00
Simon Pilgrim
282b56f43d
[VectorCombine] foldShuffleOfBinops - add support for length changing shuffles (#88899)
Refactor to be closer to foldShuffleOfCastops - sibling patch to #88743 that can be used to address some of the issues identified in #88693
2024-04-24 10:18:49 +01:00
Simon Pilgrim
c45fbfdb8e [VectorCombine][X86] shuffle-of-binops.ll - adjust no matching operand test to use FDIV
Use of FDIV allows us to show a definite cost improvement with #88899
2024-04-23 17:31:01 +01:00
Simon Pilgrim
3197146cc6 [VectorCombine][AArch64] shuffletoidentity.ll - regenerate checks
Reduce diffs in #88899
2024-04-23 17:06:18 +01:00
Simon Pilgrim
7f4f237cd8 [VectorCombine] foldShuffleOfShuffles - add missing arguments to getShuffleCost calls.
Ensure the getShuffleCost arguments/instruction args are populated - minor extension to #88743 to help improve shuffle costs for certain corner cases (e.g. shuffles of loads)
2024-04-23 11:53:08 +01:00
Simon Pilgrim
b4c6607add [VectorCombine][X86] Add test showing foldShuffleOfShuffles folding shuffles that would be better separate
On AVX+ targets a broadcast load can be treated as free.
2024-04-23 11:11:14 +01:00
Simon Pilgrim
bddfbe748b
[VectorCombine] foldShuffleOfShuffles - fold "shuffle (shuffle x, undef), (shuffle y, undef)" -> "shuffle x, y" (#88743)
Another step towards cleaning up shuffles that have been split, often across bitcasts between SSE intrinsic.

Strip shuffles entirely if we fold to an identity shuffle.
2024-04-22 15:57:59 +01:00
Simon Pilgrim
4cc9c6d98d [VectorCombine] foldShuffleOfBinops - don't fold shuffle(divrem(x,y),divrem(z,w)) if mask contains poison
Fixes #89390
2024-04-22 09:00:38 +01:00
Simon Pilgrim
3fbaad5a0c [VectorCombine] Add test coverage for #89390 2024-04-22 09:00:37 +01:00
Nikita Popov
5ff44dbaea [VectorCombine] Fix incorrect intrinsic signature (NFC) 2024-04-18 15:08:29 +09:00
Nikita Popov
a5d2ed240d [VectorCombine] Fix typo in test (NFC)
The llvm.smix should be llvm.smin.
2024-04-18 15:08:29 +09:00
Simon Pilgrim
4a5ab13bf5 [VectorCombine] Remove single quotes from "-passes=vector-combine"
These confuse the update_test_checks.py script when run by DOS cmd.exe
2024-04-17 15:46:39 +01:00
Simon Pilgrim
bf1ad1d267 [VectorCombine][X86] Add initial shuffle-of-shuffles.ll test cover for #88743 2024-04-16 16:11:09 +01:00
Simon Pilgrim
254df2e35c [VectorCombine][X86] shuffle-of-binops.ll - split off foldShuffleOfBinops tests from shuffle.ll 2024-04-16 14:52:24 +01:00
Simon Pilgrim
e185978321 [VectorCombine][X86] Regenerate shuffle.ll + shuffle-of-casts.ll
Use v4 of UTC to improve regex matching of argument names to fix a filecheck matching in a future patch
2024-04-16 14:46:30 +01:00
David Green
302d0f3476 [VectorCombine] Add some tests for converting shuffles to identities. NFC 2024-04-15 08:33:14 +01:00
Simon Pilgrim
ea3d0db130 [VectorCombine] foldShuffleOfCastops - ensure we can scale shuffle masks between bitcasted vector types
Don't just assert that the src/dst vector element counts are multiples of one another - in general IR this can actually happen.

Reported by @mikaelholmen
2024-04-12 13:53:02 +01:00
Simon Pilgrim
ff74236f34 [VectorCombine] foldShuffleOfCastops - ensure we add all new instructions onto the worklist
When creating cast(shuffle(x,y)) we were only adding the cast() to the worklist, not the new shuffle, preventing recursive combines.

foldShuffleOfBinops is also failing to do this, but I still need to add test coverage for this.
2024-04-11 15:47:09 +01:00
Simon Pilgrim
61ea1bc23a [VectorCombine][X86] Add test coverage for #67803
We are still missing a fold for shuffle(bitcast(sext(x)),bitcast(sext(y))) -> bitcast(sext(shuffle(x,y))) due to foldShuffleOfCastops failing to add new instructions back onto the worklist
2024-04-11 15:42:15 +01:00
Simon Pilgrim
6fd2fdccf2 [VectorCombine] foldShuffleOfCastops - extend shuffle(bitcast(x),bitcast(y)) -> bitcast(shuffle(x,y)) support
Handle shuffle mask scaling handling for cases where the bitcast src/dst element counts are different
2024-04-11 14:02:56 +01:00
Simon Pilgrim
717d3f3974 [VectorCombine] foldShuffleOfCastops - add initial shuffle(bitcast(x),bitcast(y)) -> bitcast(shuffle(x,y)) support
Just handle cases where the bitcast src/dst element counts are the same (future patches will add shuffle mask scaling)
2024-04-11 11:43:11 +01:00
Simon Pilgrim
a403ad9336 [VectorCombine] foldBitcastShuffle - limit bitcast(shuffle(x,y)) -> shuffle(bitcast(x),bitcast(y))
Only fold bitcast(shuffle(x,y)) -> shuffle(bitcast(x),bitcast(y)) if we won't actually increase the number of bitcasts (i.e. x or y is already bitcasted from the correct type).
2024-04-11 11:43:11 +01:00
Simon Pilgrim
b60974dc9e [VectorCombine][X86] Extend bitcast(shuffle(x,y)) test coverage
As discussed on #87510 the intention is only to fold bitcast(shuffle(x,y)) -> shuffle(bitcast(x),bitcast(y)) if we won't actually increase the number of bitcasts (i.e. x or y is already bitcasted from the correct type).
2024-04-11 11:13:12 +01:00
Simon Pilgrim
32b95a3708 [VectorCombine][X86] Extend shuffle(bitcast(x),bitcast(y)) test coverage
As discussed on #87510 the intention is to fold shuffle(bitcast(x),bitcast(y)) -> bitcast(shuffle(x,y)), but it must not interfere with existing bitcast(shuffle(bitcast(x),bitcast(y))) folds.
2024-04-11 10:08:03 +01:00
David Green
a53674359d
[AArch64] Add ZIP and UZP shuffle costs. (#88150)
This adds some costs for the shuffle instructions that should be lowered
to zip1/zip2/uzp1/uzp2 instructions.
2024-04-11 08:45:28 +01:00
Simon Pilgrim
212b2bbcd1
[VectorCombine][X86] foldShuffleOfCastops - fold shuffle(cast(x),cast(y)) -> cast(shuffle(x,y)) iff cost efficient (#87510)
Based off the existing foldShuffleOfBinops fold

Fixes #67803
2024-04-04 11:22:37 +01:00
Simon Pilgrim
d53b8291bf [VectorCombine][X86] shuffle-of-casts.ll - adjust zext nneg tests to improve costs for testing
Improves SSE vs AVX test results for #87510
2024-04-03 22:27:14 +01:00
Simon Pilgrim
b15d27e249 [VectorCombine][X86] Add additional tests for #87510
Add zext nneg tests and check we don't fold casts with different src types
2024-04-03 19:29:15 +01:00
Simon Pilgrim
4d8a3f5b35 [VectorCombine][X86] Add some tests showing failure to fold shuffle(cast(x),cast(y)) -> cast(shuffle(x,y))
Part of #67803
2024-04-03 16:34:52 +01:00