llvm / llvm/llvm-project

[LAA] WAW hazard not detected for multiple `A[I * stride]` writes if `stride` can be zero

Open
#187,402 0 comments 0 reactions 0 assignees View on GitHub
vectorizers
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

```llvm
; $ opt < print.ll -S | lli
; 101
;
; $ opt < print.ll -enable-mem-access-versioning=false -p loop-vectorize -force-vector-width=2 -S | lli
; 1000

@.str = private unnamed_addr constant [4 x i8] c"%d\0A\00", align 1
declare noundef i32 @printf(ptr noundef readonly captures(none), ...)
define dso_local void @p(i64 noundef %0) local_unnamed_addr {
%2 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i64 noundef %0)
ret void
}

define void @waw_mask(ptr %p, i64 %stride, i64 %n0, i64 %n1) {
entry:
br label %header

header:
%iv = phi i64 [ 0, %entry ], [ %iv.next, %latch ]
%iv.next = add nsw i64 %iv, 1

%s0 = add i64 %iv, 100
%s1 = add i64 %iv, 1000
%idx = mul nsw nuw i64 %iv, %stride

%c0 = icmp sle i64 %iv, %n0
%c1 = icmp sle i64 %iv, %n1

%gep = getelementptr inbounds i64, ptr %p, i64 %idx
br i1 %c0, label %store0, label %merge

store0:
store i64 %s0, ptr %gep
br label %merge

merge:
br i1 %c1, label %store1, label %latch

store1:
store i64 %s1, ptr %gep
br label %latch

latch:
%exitcond = icmp eq i64 %iv.next, 2
br i1 %exitcond, label %exit, label %header

exit:
ret void
}

define i64 @main() {
%p = alloca i64, i64 1
store i64 42, ptr %p

call void @waw_mask(ptr %p, i64 0, i64 2, i64 0)

%x = load i64, ptr %p
tail call void @p(i64 %x)
ret i64 0
}
```

LAA output (https://godbolt.org/z/E635bbnxa):

```
Printing analysis 'Loop Access Analysis' for function 'p':
Printing analysis 'Loop Access Analysis' for function 'waw_mask':
header:
Memory dependences are safe
Dependences:
Run-time memory checks:
Grouped accesses:

Non vectorizable stores to invariant address were not found in loop.
SCEV assumptions:

Expressions re-written:
Printing analysis 'Loop Access Analysis' for function 'main':
Compiler returned: 0
```

Contributor guide

Open the contributing guide

Research direction

Start by running the supplied LLVM IR reproducer with opt and lli, comparing Loop Access Analysis output with and without mem-access versioning. Trace the loop-vectorize and Loop Access Analysis entry points for the WAW dependence check on the two conditional stores. Done means LAA reports the hazard when stride can be zero and the reproducer no longer produces divergent results.

Written by the indexing model from the issue text.

Assessment

Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.