-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[VectorCombine] Fold permute of intrinsics into intrinsic of permutes: shuffle(intrinsic, poison/undef) -> intrinsic(shuffle) #170052
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
RKSimon
merged 7 commits into
llvm:main
from
kuroyukiasuna:jerry/implement-170002-support-permute
Dec 5, 2025
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
647bfbd
[VectorCombine] Fold permute of intrinsics into intrinsic of permutes…
kuroyukiasuna 6662e45
Include shuffle arguments to improve cost analysis; remove undef tests
kuroyukiasuna f37f340
Merge remote-tracking branch 'upstream/main' into jerry/implement-170…
kuroyukiasuna 94b313d
regenerate test for shuffle-of-fma
kuroyukiasuna b0c79f8
Merge branch 'main' into jerry/implement-170002-support-permute
RKSimon df07ed4
Address refactor comments
kuroyukiasuna 32a3223
Merge branch 'main' into jerry/implement-170002-support-permute
RKSimon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
148 changes: 148 additions & 0 deletions
148
llvm/test/Transforms/VectorCombine/AArch64/shuffle-of-intrinsic-permute.ll
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py | ||
| ; RUN: opt -passes=vector-combine -S -mtriple=aarch64 %s | FileCheck %s | ||
|
|
||
| ; This file tests the foldPermuteOfIntrinsic optimization which transforms: | ||
| ; shuffle(intrinsic(args), poison/undef) -> intrinsic(shuffle(args)) | ||
| ; when the shuffle is a permute (operates on single vector) and cost model | ||
| ; determines the transformation is beneficial. | ||
|
|
||
| ;; ============================================================================ | ||
| ;; Positive Tests - Should Optimize | ||
| ;; ============================================================================ | ||
|
|
||
| define <4 x i32> @extract_lower_sadd_sat(<8 x i32> %v1, <8 x i32> %v2) { | ||
| ; CHECK-LABEL: @extract_lower_sadd_sat( | ||
| ; CHECK-NEXT: [[TMP1:%.*]] = shufflevector <8 x i32> [[V1:%.*]], <8 x i32> poison, <4 x i32> <i32 0, i32 1, i32 2, i32 3> | ||
| ; CHECK-NEXT: [[TMP2:%.*]] = shufflevector <8 x i32> [[V2:%.*]], <8 x i32> poison, <4 x i32> <i32 0, i32 1, i32 2, i32 3> | ||
| ; CHECK-NEXT: [[RESULT:%.*]] = call <4 x i32> @llvm.sadd.sat.v4i32(<4 x i32> [[TMP1]], <4 x i32> [[TMP2]]) | ||
| ; CHECK-NEXT: ret <4 x i32> [[RESULT]] | ||
| ; | ||
| %sat = call <8 x i32> @llvm.sadd.sat.v8i32(<8 x i32> %v1, <8 x i32> %v2) | ||
| %result = shufflevector <8 x i32> %sat, <8 x i32> poison, <4 x i32> <i32 0, i32 1, i32 2, i32 3> | ||
| ret <4 x i32> %result | ||
| } | ||
|
|
||
| define <4 x i32> @extract_lower_uadd_sat(<8 x i32> %v1, <8 x i32> %v2) { | ||
| ; CHECK-LABEL: @extract_lower_uadd_sat( | ||
| ; CHECK-NEXT: [[TMP1:%.*]] = shufflevector <8 x i32> [[V1:%.*]], <8 x i32> poison, <4 x i32> <i32 0, i32 1, i32 2, i32 3> | ||
| ; CHECK-NEXT: [[TMP2:%.*]] = shufflevector <8 x i32> [[V2:%.*]], <8 x i32> poison, <4 x i32> <i32 0, i32 1, i32 2, i32 3> | ||
| ; CHECK-NEXT: [[RESULT:%.*]] = call <4 x i32> @llvm.uadd.sat.v4i32(<4 x i32> [[TMP1]], <4 x i32> [[TMP2]]) | ||
| ; CHECK-NEXT: ret <4 x i32> [[RESULT]] | ||
| ; | ||
| %sat = call <8 x i32> @llvm.uadd.sat.v8i32(<8 x i32> %v1, <8 x i32> %v2) | ||
| %result = shufflevector <8 x i32> %sat, <8 x i32> poison, <4 x i32> <i32 0, i32 1, i32 2, i32 3> | ||
| ret <4 x i32> %result | ||
| } | ||
|
|
||
| define <4 x float> @extract_lower_fma(<8 x float> %a, <8 x float> %b, <8 x float> %c) { | ||
| ; CHECK-LABEL: @extract_lower_fma( | ||
| ; CHECK-NEXT: [[TMP1:%.*]] = shufflevector <8 x float> [[A:%.*]], <8 x float> poison, <4 x i32> <i32 0, i32 1, i32 2, i32 3> | ||
| ; CHECK-NEXT: [[TMP2:%.*]] = shufflevector <8 x float> [[B:%.*]], <8 x float> poison, <4 x i32> <i32 0, i32 1, i32 2, i32 3> | ||
| ; CHECK-NEXT: [[TMP3:%.*]] = shufflevector <8 x float> [[C:%.*]], <8 x float> poison, <4 x i32> <i32 0, i32 1, i32 2, i32 3> | ||
| ; CHECK-NEXT: [[RESULT:%.*]] = call <4 x float> @llvm.fma.v4f32(<4 x float> [[TMP1]], <4 x float> [[TMP2]], <4 x float> [[TMP3]]) | ||
| ; CHECK-NEXT: ret <4 x float> [[RESULT]] | ||
| ; | ||
| %fma = call <8 x float> @llvm.fma.v8f32(<8 x float> %a, <8 x float> %b, <8 x float> %c) | ||
| %result = shufflevector <8 x float> %fma, <8 x float> poison, <4 x i32> <i32 0, i32 1, i32 2, i32 3> | ||
| ret <4 x float> %result | ||
| } | ||
|
|
||
| define <4 x i32> @extract_lower_abs_should_not_shuffle_scalar(<8 x i32> %v) { | ||
| ; CHECK-LABEL: @extract_lower_abs_should_not_shuffle_scalar( | ||
| ; CHECK-NEXT: [[TMP1:%.*]] = shufflevector <8 x i32> [[V:%.*]], <8 x i32> poison, <4 x i32> <i32 0, i32 1, i32 2, i32 3> | ||
| ; CHECK-NEXT: [[RESULT:%.*]] = call <4 x i32> @llvm.abs.v4i32(<4 x i32> [[TMP1]], i1 false) | ||
| ; CHECK-NEXT: ret <4 x i32> [[RESULT]] | ||
| ; | ||
| %abs = call <8 x i32> @llvm.abs.v8i32(<8 x i32> %v, i1 false) | ||
| %result = shufflevector <8 x i32> %abs, <8 x i32> poison, <4 x i32> <i32 0, i32 1, i32 2, i32 3> | ||
| ret <4 x i32> %result | ||
| } | ||
|
|
||
| define <2 x i64> @extract_lower_i64(<4 x i64> %v1, <4 x i64> %v2) { | ||
| ; CHECK-LABEL: @extract_lower_i64( | ||
| ; CHECK-NEXT: [[TMP1:%.*]] = shufflevector <4 x i64> [[V1:%.*]], <4 x i64> poison, <2 x i32> <i32 0, i32 1> | ||
| ; CHECK-NEXT: [[TMP2:%.*]] = shufflevector <4 x i64> [[V2:%.*]], <4 x i64> poison, <2 x i32> <i32 0, i32 1> | ||
| ; CHECK-NEXT: [[RESULT:%.*]] = call <2 x i64> @llvm.sadd.sat.v2i64(<2 x i64> [[TMP1]], <2 x i64> [[TMP2]]) | ||
| ; CHECK-NEXT: ret <2 x i64> [[RESULT]] | ||
| ; | ||
| %sat = call <4 x i64> @llvm.sadd.sat.v4i64(<4 x i64> %v1, <4 x i64> %v2) | ||
| %result = shufflevector <4 x i64> %sat, <4 x i64> poison, <2 x i32> <i32 0, i32 1> | ||
| ret <2 x i64> %result | ||
| } | ||
|
|
||
| define <8 x i16> @extract_lower_i16(<16 x i16> %v1, <16 x i16> %v2) { | ||
| ; CHECK-LABEL: @extract_lower_i16( | ||
| ; CHECK-NEXT: [[TMP1:%.*]] = shufflevector <16 x i16> [[V1:%.*]], <16 x i16> poison, <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7> | ||
| ; CHECK-NEXT: [[TMP2:%.*]] = shufflevector <16 x i16> [[V2:%.*]], <16 x i16> poison, <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7> | ||
| ; CHECK-NEXT: [[RESULT:%.*]] = call <8 x i16> @llvm.sadd.sat.v8i16(<8 x i16> [[TMP1]], <8 x i16> [[TMP2]]) | ||
| ; CHECK-NEXT: ret <8 x i16> [[RESULT]] | ||
| ; | ||
| %sat = call <16 x i16> @llvm.sadd.sat.v16i16(<16 x i16> %v1, <16 x i16> %v2) | ||
| %result = shufflevector <16 x i16> %sat, <16 x i16> poison, <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7> | ||
| ret <8 x i16> %result | ||
| } | ||
|
|
||
| define <4 x i32> @extract_lower_with_undef(<8 x i32> %v1, <8 x i32> %v2) { | ||
| ; CHECK-LABEL: @extract_lower_with_undef( | ||
| ; CHECK-NEXT: [[TMP1:%.*]] = shufflevector <8 x i32> [[V1:%.*]], <8 x i32> poison, <4 x i32> <i32 0, i32 1, i32 2, i32 3> | ||
| ; CHECK-NEXT: [[TMP2:%.*]] = shufflevector <8 x i32> [[V2:%.*]], <8 x i32> poison, <4 x i32> <i32 0, i32 1, i32 2, i32 3> | ||
| ; CHECK-NEXT: [[RESULT:%.*]] = call <4 x i32> @llvm.sadd.sat.v4i32(<4 x i32> [[TMP1]], <4 x i32> [[TMP2]]) | ||
| ; CHECK-NEXT: ret <4 x i32> [[RESULT]] | ||
| ; | ||
| %sat = call <8 x i32> @llvm.sadd.sat.v8i32(<8 x i32> %v1, <8 x i32> %v2) | ||
| %result = shufflevector <8 x i32> %sat, <8 x i32> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3> | ||
| ret <4 x i32> %result | ||
| } | ||
|
|
||
| ;; ============================================================================ | ||
| ;; Negative Tests - Should NOT Optimize | ||
| ;; ============================================================================ | ||
|
|
||
| define <4 x i32> @same_size_permute(<4 x i32> %v1, <4 x i32> %v2) { | ||
| ; CHECK-LABEL: @same_size_permute( | ||
| ; CHECK-NEXT: [[SAT:%.*]] = call <4 x i32> @llvm.sadd.sat.v4i32(<4 x i32> [[V1:%.*]], <4 x i32> [[V2:%.*]]) | ||
| ; CHECK-NEXT: [[RESULT:%.*]] = shufflevector <4 x i32> [[SAT]], <4 x i32> poison, <4 x i32> <i32 2, i32 0, i32 3, i32 1> | ||
| ; CHECK-NEXT: ret <4 x i32> [[RESULT]] | ||
| ; | ||
| %sat = call <4 x i32> @llvm.sadd.sat.v4i32(<4 x i32> %v1, <4 x i32> %v2) | ||
| %result = shufflevector <4 x i32> %sat, <4 x i32> poison, <4 x i32> <i32 2, i32 0, i32 3, i32 1> | ||
| ret <4 x i32> %result | ||
| } | ||
|
|
||
| define <4 x i32> @not_a_permute_uses_second_operand(<4 x i32> %v1, <4 x i32> %v2, <4 x i32> %other) { | ||
| ; CHECK-LABEL: @not_a_permute_uses_second_operand( | ||
| ; CHECK-NEXT: [[SAT:%.*]] = call <4 x i32> @llvm.sadd.sat.v4i32(<4 x i32> [[V1:%.*]], <4 x i32> [[V2:%.*]]) | ||
| ; CHECK-NEXT: [[RESULT:%.*]] = shufflevector <4 x i32> [[SAT]], <4 x i32> [[OTHER:%.*]], <4 x i32> <i32 0, i32 4, i32 1, i32 5> | ||
| ; CHECK-NEXT: ret <4 x i32> [[RESULT]] | ||
| ; | ||
| %sat = call <4 x i32> @llvm.sadd.sat.v4i32(<4 x i32> %v1, <4 x i32> %v2) | ||
| %result = shufflevector <4 x i32> %sat, <4 x i32> %other, <4 x i32> <i32 0, i32 4, i32 1, i32 5> | ||
| ret <4 x i32> %result | ||
| } | ||
|
|
||
| define <4 x i32> @not_an_intrinsic(<8 x i32> %v1, <8 x i32> %v2) { | ||
| ; CHECK-LABEL: @not_an_intrinsic( | ||
| ; CHECK-NEXT: [[ADD:%.*]] = add <8 x i32> [[V1:%.*]], [[V2:%.*]] | ||
| ; CHECK-NEXT: [[RESULT:%.*]] = shufflevector <8 x i32> [[ADD]], <8 x i32> poison, <4 x i32> <i32 0, i32 1, i32 2, i32 3> | ||
| ; CHECK-NEXT: ret <4 x i32> [[RESULT]] | ||
| ; | ||
| %add = add <8 x i32> %v1, %v2 | ||
| %result = shufflevector <8 x i32> %add, <8 x i32> poison, <4 x i32> <i32 0, i32 1, i32 2, i32 3> | ||
| ret <4 x i32> %result | ||
| } | ||
|
|
||
| declare <8 x i32> @llvm.sadd.sat.v8i32(<8 x i32>, <8 x i32>) | ||
| declare <4 x i32> @llvm.sadd.sat.v4i32(<4 x i32>, <4 x i32>) | ||
| declare <4 x i64> @llvm.sadd.sat.v4i64(<4 x i64>, <4 x i64>) | ||
| declare <2 x i64> @llvm.sadd.sat.v2i64(<2 x i64>, <2 x i64>) | ||
| declare <16 x i16> @llvm.sadd.sat.v16i16(<16 x i16>, <16 x i16>) | ||
| declare <8 x i16> @llvm.sadd.sat.v8i16(<8 x i16>, <8 x i16>) | ||
|
|
||
| declare <8 x i32> @llvm.uadd.sat.v8i32(<8 x i32>, <8 x i32>) | ||
| declare <4 x i32> @llvm.uadd.sat.v4i32(<4 x i32>, <4 x i32>) | ||
|
|
||
| declare <8 x i32> @llvm.abs.v8i32(<8 x i32>, i1 immarg) | ||
| declare <4 x i32> @llvm.abs.v4i32(<4 x i32>, i1 immarg) | ||
|
|
||
| declare <8 x float> @llvm.fma.v8f32(<8 x float>, <8 x float>, <8 x float>) | ||
| declare <4 x float> @llvm.fma.v4f32(<4 x float>, <4 x float>, <4 x float>) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.