Improve cost model for lengthens followed by truncates
| Bugzilla Link | 21370 |
| Version | trunk |
| OS | All |
| CC | @hfinkel |
Extended Description
The loop vectorizer does not have a model for sequences of lengthen->arithmetic->truncate. These sequences are often generated due to implicit int promotions when dealing with sub-int types, and the lengthen/truncate can be elided. One of the "obvious" solutions to this would be to question why the lengthen/truncate sequence hadn't been removed in the first place. This has been discussed on-list, and keeping it is deliberate. If the sequence is not vectorized, the lengthens and truncates would have to be generated because most architectures can't do scalar arithmetic on sub-word types. Only if it is vectorized can we do 8-bit or 16-bit arithmetic. So the model for this needs to be in the vectorizer. In fact, if the vectorizer is forced to vectorize, it does actually remove the lengthen/truncate in its generated code. It just doesn't account for this in its cost model.
char arr[5000]; char arr2[5000];
void f() { for (int i = 0; i < 5000; ++i) { int a = arr[i] + 1; arr2[i] = a; } }
./bin/clang -O3 -o - -S test-vec2.c -target arm64 -Rpass=loop-vectorize -Rpass-analysis=loop-vectorize test-vec2.c:5:3: remark: unrolled with interleaving factor 2 (vectorization not beneficial) [-Rpass=loop-vectorize] for (int i = 0; i < 5000; ++i) { ^
Thanks for the update, James. Reassigning the bug to you.
Hi,
I am looking at this, and I have a patch ready to land soon.
Cheers,
James
If no one else is working on this for the loop vectorizer, I'm happy to take a look.
They are related, yes. Bug 21369 deals with the legality of the lengthen/truncate issue (a reduction in that case), and this bug deals with the profitability issue. Then lengthen/truncate sequence doesn't need to be taken into account in the cost model if it will be removed in the vectorized version.
Matt, Isn't this a similar issue to that in bug 21369?
assigned to @jmolloy