[X86][AVX] separate 2x128bit loads are not being merged into a single 256bit load.
| Bugzilla Link | 21709 |
| Resolution | FIXED |
| Resolved on | Feb 03, 2015 13:19 |
| Version | trunk |
| OS | Linux |
| CC | @hfinkel,@RKSimon,@rotateright |
Extended Description
Example 1.
/// __m256 unaligned_loads(const float *ptr) { __m128 lo = _mm_loadu_ps( ptr + 0 ); __m128 hi = _mm_loadu_ps( ptr + 4 ); return _mm256_insertf128_ps( _mm256_castps128_ps256( lo ), hi, 1); } ///
clang -march=btver2 -O2 -S -o -
vmovups (%rdi), %xmm0 vinsertf128 $1, 16(%rdi), %ymm0, %ymm0 retq
Ideally, it should generate a single 32B load: vmovups (%rdi), %ymm0
Basically the backend should generate a single unaligned 32B load instead of the sequence vmovups + vinsertf128 (+ folded load). I think this should be done for AVX targets with feature 'FastUAMem' and not 'SlowUAMem32'.
Also (probably a minor/separate issue?) the vinsertf128 may cause an exception if alignment checking is enabled and the current privilege level is 3.
Example 2. /// __m256 aligned_loads(const float *ptr) { __m128 lo = _mm_load_ps( ptr + 0 ); __m128 hi = _mm_load_ps( ptr + 4 ); return _mm256_insertf128_ps( _mm256_castps128_ps256( lo ), hi, 1); } ///
clang -march=btver2 -O2 -S -o -
vmovaps (%rdi), %xmm0 vinsertf128 $1, 16(%rdi), %ymm0, %ymm0 retq
Again, this could be folded into: vmovaps (%rdi), %ymm0
As a side note:
the code from Example 1. is equivalent to the following code:
/// __m256 unaligned_loads_v2(const float *ptr) { __m128 lo = _mm_loadu_ps( ptr + 0 ); __m128 hi = _mm_loadu_ps( ptr + 4 ); return (__m256) __builtin_shufflevector(lo, hi, 0, 1, 2, 3, 4, 5, 6, 7); } ///
Where the call to the x86 intrinsic _mm256_insertf128_ps has been replaced with a __builtin_shufflevector call.
What I am trying to say here is that we could teach the instruction combiner that a call to _mm256_insertf128_ps is actually equivalent to a shuffle that performs a concat_vector. Basically, the instruction combiner could early replace that call with a shuffle before we even reach the backend.
The codegen for 'unaligned_loads_v2' is still the same as 'unaligned_loads': vmovups (%rdi), %xmm0 vinsertf128 $1, 16(%rdi), %ymm0, %ymm0 retq
Patch checked in: http://reviews.llvm.org/rL224344
Note that the patch doesn't bother with the aligned case because there's probably no benefit in handling that special case to generate vmovaps/vmovdqa.
Yes using movups should be fine - IIRC it will still work with stack reload folding as well if the instruction alignment permits (which it should for AVX instructions).
Technically btver2 only checks for up to 16-byte alignment as its a 128-bit FPU unit and treats 32-byte load/stores as 2 x 16-byte load/stores.
Example 2. /// __m256 aligned_loads(const float *ptr) { __m128 lo = _mm_load_ps( ptr + 0 ); __m128 hi = _mm_load_ps( ptr + 4 ); return _mm256_insertf128_ps( _mm256_castps128_ps256( lo ), hi, 1); } ///
clang -march=btver2 -O2 -S -o -
vmovaps (%rdi), %xmm0 vinsertf128 $1, 16(%rdi), %ymm0, %ymm0 retq
Again, this could be folded into: vmovaps (%rdi), %ymm0
For this example, I don't think we can use 'vmovaps' because it requires 32-byte alignment when used with a ymm operand. We only have a 16-byte alignment guarantee from the _mm_load_ps intrinsics.
AFAIK, the hardware performance for all recent AMD and Intel chips of 'vmovups' (the unaligned instruction) will be identical to 'vmovaps' if the operand is in fact aligned when the instruction is executed, so there is no performance loss from using 'vmovups' here.
Please let me know if that matches your understanding.
Patch updated with more test cases.
The good news is that the pattern matches either direct intrinsic usage via vinsertf128 or the shufflevector IR case because both of those are transformed into 'insert_subvector'.
First attempt at a patch is up for review here: http://reviews.llvm.org/D6492
assigned to @rotateright