llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

SLP Vectorizer crash due to commit ca654acc16c43191228eadfec8f7241dca10b0c3

Open ivankelarev opened this issue 2 years ago • 5 comments

ca654acc16c43191228eadfec8f7241dca10b0c3 change in SLP Vectorizer causes a crash for this test case:

target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

define void @a_(double %c__fetch.3) {
alloca_0:
  %mul.3 = fmul double 0.000000e+00, %c__fetch.3
  %mul.4 = fmul double 0.000000e+00, 0.000000e+00
  br label %loop.69

loop.69:                                          ; preds = %loop.69, %alloca_0
  %t48.0 = phi double [ 0.000000e+00, %alloca_0 ], [ %5, %loop.69 ]
  %t50.0 = phi double [ 0.000000e+00, %alloca_0 ], [ %3, %loop.69 ]
  %t52.0 = phi double [ 0.000000e+00, %alloca_0 ], [ %7, %loop.69 ]
  %0 = fmul double %t52.0, %mul.3
  %1 = fmul double %t48.0, %mul.4
  %2 = fadd double %1, %0
  %3 = fadd double %2, 0.000000e+00
  %4 = fmul double 0.000000e+00, %mul.3
  %5 = fadd double 0.000000e+00, %4
  %6 = fmul double 0.000000e+00, %mul.4
  %7 = fadd double 0.000000e+00, %6
  br label %loop.69
}

After slp-vectorizer pass IR is being transformed to as incorrect one:

define void @a_(double %c__fetch.3) {
alloca_0:
  %0 = shufflevector <2 x double> %2, <2 x double> <double 0.000000e+00, double poison>, <2 x i32> <i32 2, i32 0>
  %1 = fmul <2 x double> zeroinitializer, %0
  %2 = insertelement <2 x double> <double poison, double 0.000000e+00>, double %c__fetch.3, i32 0
  %3 = fmul <2 x double> zeroinitializer, %2
  br label %loop.69

loop.69:                                          ; preds = %loop.69, %alloca_0
  %t50.0 = phi double [ 0.000000e+00, %alloca_0 ], [ %9, %loop.69 ]
  %4 = phi <2 x double> [ zeroinitializer, %alloca_0 ], [ %11, %loop.69 ]
  %5 = fmul <2 x double> %4, %1
  %6 = extractelement <2 x double> %5, i32 0
  %7 = extractelement <2 x double> %5, i32 1
  %8 = fadd double %6, %7
  %9 = fadd double %8, 0.000000e+00
  %10 = fmul <2 x double> zeroinitializer, %3
  %11 = fadd <2 x double> zeroinitializer, %10
  br label %loop.69
}

Error message:

Instruction does not dominate all uses!
  %2 = insertelement <2 x double> <double poison, double 0.000000e+00>, double %c__fetch.3, i32 0
  %0 = shufflevector <2 x double> %2, <2 x double> <double 0.000000e+00, double poison>, <2 x i32> <i32 2, i32 0>
LLVM ERROR: Broken module found, compilation aborted!

Reverting the mentioned commit makes this test pass. @alexey-bataev could you please take a look?

ivankelarev avatar Aug 16 '24 19:08 ivankelarev

Update: It appears that the mentioned commit merely obscures a more fundamental issue. The following test continues to fail even after the commit has been reverted:

target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

define void @a_(double %c__fetch.3) {
alloca_0:
  %mul.3 = fmul double 0.000000e+00, %c__fetch.3
  %mul.4 = fmul double 0.000000e+00, 0.000000e+00
  br label %loop.69

loop.69:                                          ; preds = %loop.69, %alloca_0
  %t48.0 = phi double [ 0.000000e+00, %alloca_0 ], [ %5, %loop.69 ]
  %t50.02 = phi double [ 0.000000e+00, %alloca_0 ], [ %3, %loop.69 ]
  %t52.0 = phi double [ 0.000000e+00, %alloca_0 ], [ %7, %loop.69 ]
  %0 = fmul double %t52.0, %mul.3
  %1 = fmul double %t48.0, %mul.4
  %2 = fadd double %1, %0
  %3 = fadd double %2, %c__fetch.3
  %4 = fmul double 0.000000e+00, %mul.3
  %5 = fadd double 0.000000e+00, %4
  %6 = fmul double 0.000000e+00, %mul.4
  %7 = fadd double 0.000000e+00, %6
  br label %loop.69
}

ivankelarev avatar Aug 16 '24 20:08 ivankelarev

It appears that the actual problematic commit is the following:

commit 66775f8ccdcc8264ef349518e1c59d96d4227823
Author: Alexey Bataev <[email protected]>
Date:   Mon Oct 16 12:50:29 2023 -0700

    [SLP]Fix PR69196: Instruction does not dominate all uses
    
    During emission of the postponed gathers, need to insert them before
    user instruction to avoid use before definition crash.

For compiler builds prior to this change, the test passes, and the correct code is generated as follows:

define void @a_(double %c__fetch.3) {
alloca_0:
  %0 = insertelement <2 x double> <double poison, double 0.000000e+00>, double %c__fetch.3, i32 0
  %1 = fmul <2 x double> zeroinitializer, %0
  %2 = shufflevector <2 x double> %0, <2 x double> <double 0.000000e+00, double poison>, <2 x i32> <i32 2, i32 0>
  %3 = fmul <2 x double> zeroinitializer, %2
  br label %loop.69

loop.69:                                          ; preds = %loop.69, %alloca_0
  %t50.02 = phi double [ 0.000000e+00, %alloca_0 ], [ %9, %loop.69 ]
  %4 = phi <2 x double> [ zeroinitializer, %alloca_0 ], [ %11, %loop.69 ]
  %5 = fmul <2 x double> %4, %3
  %6 = extractelement <2 x double> %5, i32 0
  %7 = extractelement <2 x double> %5, i32 1
  %8 = fadd double %6, %7
  %9 = fadd double %8, %c__fetch.3
  %10 = fmul <2 x double> zeroinitializer, %1
  %11 = fadd <2 x double> zeroinitializer, %10
  br label %loop.69
}

Although the commit may seem quite old, there is a test case for which it causes incorrect code generation. Alexey, could you please revisit this change and address the code generation issue for the provided test case?

ivankelarev avatar Aug 16 '24 20:08 ivankelarev

It appears that the actual problematic commit is the following:

commit 66775f8ccdcc8264ef349518e1c59d96d4227823
Author: Alexey Bataev <[email protected]>
Date:   Mon Oct 16 12:50:29 2023 -0700

    [SLP]Fix PR69196: Instruction does not dominate all uses
    
    During emission of the postponed gathers, need to insert them before
    user instruction to avoid use before definition crash.

For compiler builds prior to this change, the test passes, and the correct code is generated as follows:

define void @a_(double %c__fetch.3) {
alloca_0:
  %0 = insertelement <2 x double> <double poison, double 0.000000e+00>, double %c__fetch.3, i32 0
  %1 = fmul <2 x double> zeroinitializer, %0
  %2 = shufflevector <2 x double> %0, <2 x double> <double 0.000000e+00, double poison>, <2 x i32> <i32 2, i32 0>
  %3 = fmul <2 x double> zeroinitializer, %2
  br label %loop.69

loop.69:                                          ; preds = %loop.69, %alloca_0
  %t50.02 = phi double [ 0.000000e+00, %alloca_0 ], [ %9, %loop.69 ]
  %4 = phi <2 x double> [ zeroinitializer, %alloca_0 ], [ %11, %loop.69 ]
  %5 = fmul <2 x double> %4, %3
  %6 = extractelement <2 x double> %5, i32 0
  %7 = extractelement <2 x double> %5, i32 1
  %8 = fadd double %6, %7
  %9 = fadd double %8, %c__fetch.3
  %10 = fmul <2 x double> zeroinitializer, %1
  %11 = fadd <2 x double> zeroinitializer, %10
  br label %loop.69
}

Although the commit may seem quite old, there is a test case for which it causes incorrect code generation. Alexey, could you please revisit this change and address the code generation issue for the provided test case?

The actual problem is not in this commit, the fix is almost ready

alexey-bataev avatar Aug 16 '24 20:08 alexey-bataev

/cherry-pick 4a0bbbcbcf6ebc87e794e7b86b9f4651bffcd806

alexey-bataev avatar Aug 22 '24 11:08 alexey-bataev

/cherry-pick 4a0bbbcbcf6ebc87e794e7b86b9f4651bffcd806

Error: Command failed due to missing milestone.

llvmbot avatar Aug 22 '24 11:08 llvmbot