llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
29.27k stars 12.1k forks source link

Vectorized code computes a different result from the unvectorized code (see later comments) #63185

Open wsmoses opened 1 year ago

wsmoses commented 1 year ago

Edit: the bug is not related to address spaces, see later comments:

The following code examples are equivalent except for one having a non-zero address space for the first arg or not. The non-zero address space version is incorrectly marked as vectorizable whereas the second one is not.

Illegal case: https://godbolt.org/z/Wjodd6n5x Correct case: https://godbolt.org/z/svPhcT84x

cc @vchuravy @jdoerfert

llvmbot commented 1 year ago

@llvm/issue-subscribers-julialang

jdoerfert commented 1 year ago

The good version is not vectorized because:

LAA: Runtime check would require comparison between different address spaces
LAA: We can't vectorize because we can't find the array bounds.
wsmoses commented 1 year ago

Yeah I minimized this one slightly too far, a moment.

wsmoses commented 1 year ago

In essence this code (https://godbolt.org/z/11drEMfjx) when run through Julia's LLVM pass pipeline creates a bug. It should print 1.0, but optimizes to 2.0

wsmoses commented 1 year ago

out_a2.txt

wsmoses commented 1 year ago

Here is it post vectorization (see it prints 2): https://godbolt.org/z/4vqzxMbd8 Here is it pre vectorization (see it prints 1): https://godbolt.org/z/aGM6Td4T6

Here is the vectorization itself as an llvm pass on trunc: https://godbolt.org/z/431xo6Tfc

wsmoses commented 1 year ago

@fhahn if you might have any insights here.

fhahn commented 1 year ago

Hmm interesting, let me take a look in a bit

artagnon commented 1 year ago

The issue seems to be fixed now. I ran the optimization pipeline you showed minus the -opaque-pointers=0, since that option no longer exists, and got the same output of 1.