Loop vectorizer reports unknown dependence for disjoint mixed-width accesses
LLVM's loop vectorizer appears unable to prove loop-carried independence for a simple constant-stride loop when each iteration performs a narrow load followed by a wider store to the same element.
The load and store overlap within an individual iteration, but accesses from different iterations are disjoint.
Changing the load to the same width as the store makes the dependence-legality failure disappear.
Reproducer
#include <stdint.h>
#include <stddef.h>
#include <string.h>
void replace(uint64_t *xs, size_t len, uint32_t before, uint64_t after) {
for (size_t i = 0; i < len; i++) {
uint32_t low;
memcpy(&low, &xs[i], sizeof(low));
if (low == before)
xs[i] = after;
}
}
Compiled with:
clang -O3 test.c -c \
-Rpass=loop-vectorize \
-Rpass-analysis=loop-vectorize \
-Rpass-missed=loop-vectorize
Using Clang 22.1.8, I get:
test.c:11:19: remark: loop not vectorized: unsafe dependent memory operations in loop.
Unknown data dependence. Memory location is the same as accessed at test.c:8:23
[-Rpass-analysis=loop-vectorize]
test.c:6:5: remark: loop not vectorized [-Rpass-missed=loop-vectorize]
Resulting LLVM IR
Compiling with:
clang -O3 test.c -S -emit-llvm -o test.ll
shows that the memcpy is optimized to the expected narrow load.
The relevant accesses are:
%24 = getelementptr inbounds nuw i64, ptr %0, i64 %22
%25 = load i32, ptr %24, align 8
%26 = icmp eq i32 %25, %2
br i1 %26, label %27, label %28
27:
store i64 %3, ptr %24, align 8, !tbaa !9
br label %28
The scalar loop is subsequently unrolled by two, with the second logical element accessed 8 bytes later:
%29 = getelementptr inbounds nuw i64, ptr %0, i64 %22
%30 = getelementptr inbounds nuw i8, ptr %29, i64 8
%31 = load i32, ptr %30, align 8
%32 = icmp eq i32 %31, %2
br i1 %32, label %33, label %34
33:
store i64 %3, ptr %30, align 8, !tbaa !9
Conceptually the accesses are therefore:
logical iteration i:
load [i*8, i*8 + 4)
store [i*8, i*8 + 8)
logical iteration i+1:
load [i*8 + 8, i*8 + 12)
store [i*8 + 8, i*8 + 16)
There is an intentional dependence between the load and store within one logical iteration, but I don't see a loop-carried overlap between different iterations.
Same-width control
If the load is changed to a full-width 64-bit load:
void replace64(uint64_t *xs, size_t len, uint64_t before, uint64_t after) {
for (size_t i = 0; i < len; i++) {
uint64_t value;
memcpy(&value, &xs[i], sizeof(value));
if (value == before)
xs[i] = after;
}
}
LLVM gets through dependence legality and reports only:
the cost-model indicates that vectorization is not beneficial
the cost-model indicates that interleaving is not beneficial
So the differential is approximately:
stride 8, load i32, store i64
-> Unknown data dependence
stride 8, load i64, store i64
-> dependence legality succeeds
(profitability may still reject vectorization)
Expected behavior
For the mixed-width case, the accesses overlap within each iteration, but the constant 8-byte stride appears sufficient to prove that memory touched by distinct logical iterations does not overlap.
I would expect dependence analysis to distinguish the intra-iteration dependence from a loop-carried dependence and allow the loop past vectorization legality, leaving the cost model to decide whether vectorization is worthwhile.
Question
Is this an expected limitation of the current loop dependence analysis, or could LoopAccessAnalysis / MemoryDepChecker prove independence for this mixed-width case?
The reproducer was originally found through Rust code, but the behavior reproduces directly with Clang and does not appear frontend-specific.
By the way, the code LLVM generates for this example looks quite poor compared to GCC. It is unrelated to this issue, but I’ll file a separate issue for it.
Hi, I'd like to work on this issue. Could you assign it to me?
Hi, I'd like to work on this issue. Could you assign it to me?
It seems I don't have permissions to assign it