-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[VectorCombine] isExtractExtractCheap - specify the extract/insert shuffle mask to improve shuffle costs #114780
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 5 commits into
llvm:main
from
RKSimon:vectorcombine-extractelt-shufflemask
Nov 13, 2024
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7b99818
[VectorCombine] isExtractExtractCheap - specify the extract/insert sh…
RKSimon e374048
Cleanup SmallVector initialization
RKSimon a123ea8
Add explicit getShuffleCost arguments for CostKind and the source vector
RKSimon ac03cea
Cleanup check-prefixes
RKSimon 690a059
Use ConvertToShuffle vector source value directly
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
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
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@davemgreen The comment above talks about scalarizing to avoid 2 v4i32 loads - but the cost model still does this - which is more realistic?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello. Am I right in saying that the load is not included in the cost? It will be difficult to beat scalarization of the load if this doesn't lead to other optimizations, but I suspect that is not really what the costs are measuring.
Maybe @fhahn remembers more about this specific case. The change you have (pass the mask to the shuffle cost) seems like a sensible optimization. There is a comment that says:
Maybe it should be more aggressive in the backend at scalarizing. It looks like the costs here should be extract-lane-0 + extract-lane-2 (2) + i32 add (1) vs extract-lane-0 + shuffle (1 now?) + v8i32 add (2). If it could realize that the last v8i32 add was actually a v4i32 add, that might be more accurate (if I have those costs correct).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case it can be a v2i32 add - and maybe the shuffle will fold away entirely as a 64-bit vector subvector extract - not sure, its a long time since I worked with (aarch32) neon ops!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't believe there is a way to specify demanded elts to the cost functions, but would it be possible to manually specify the expected length of the add (maybe using the known register vector length if the length of the vector is longer)? It doesn't help with the cost/scalarization of the load, but might make the cost overall a little more accurate.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's no demanded elts - but the shuffle mask is now all -1 apart from the single insertion index. It won't help for anything but the getShuffleCost of course.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can pull out that costmodel change into its own PR if necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@davemgreen any thoughts on this - is the aarch64 costmodel change the way to go?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello. I think it should be "if the vector load after legalization has a single extract - then it will be scalarized". Multiple extracts still don't scalarize in general. I'm not sure if this is the best way forward or not though. There are limits to what the cost model can handle well, and this patch (minus the aarch64 cost model change) looks like a step in the right direction.
Am I correct that shrinking the type used to cost the vector binop, to express that the whole (possibly multi-)vector width is not needed only a single lane, does not work well in practice?
If so maybe we go with the original. I can try another test out too, to see if single element shuffles should be costed more like lane moves.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've dropped the CostModel change.
Changing types is tricky - I've always kept to using types already existing somewhere in the pattern (or very occasionally int<->fp equivalents) and not make a guess at a new 'optimal' types. The best we could do is split them if the upper half is known undef/undemanded, but I'm still unsure how well it'd work even with a cost model to guide it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah - I was thinking of getNumberOfParts / getRegisterBitWidth like the SLP vectorizer uses, and clamping the size of the vector type to one register.