Issue 170828
Summary [SLP][Revec] Assertion `castIsValid(op, S, Ty) && "Invalid cast!"'
Labels vectorizers, crash-on-valid
Assignees
Reporter sjoerdmeijer
    This IR:

```
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128-Fn32"
target triple = "aarch64-unknown-linux-gnu"

define <16 x i64> @_Z1hhPb() {
entry:
  %0 = zext <16 x i8> splat (i8 1) to <16 x i64>
  %1 = sub <16 x i64> zeroinitializer, %0
  %2 = zext <16 x i8> zeroinitializer to <16 x i64>
  %3 = sub <16 x i64> zeroinitializer, %2
  %4 = call <16 x i64> @llvm.smin.v16i64(<16 x i64> %1, <16 x i64> %3)
  %5 = zext <16 x i8> zeroinitializer to <16 x i64>
  %6 = sub <16 x i64> zeroinitializer, %5
 %7 = call <16 x i64> @llvm.smin.v16i64(<16 x i64> %4, <16 x i64> %6)
  %8 = zext <16 x i8> zeroinitializer to <16 x i64>
  %9 = sub <16 x i64> zeroinitializer, %8
  %10 = call <16 x i64> @llvm.smin.v16i64(<16 x i64> %7, <16 x i64> %9)
  %11 = sub <16 x i64> zeroinitializer, zeroinitializer
 %12 = call <16 x i64> @llvm.smin.v16i64(<16 x i64> %10, <16 x i64> %11)
 %13 = sub <16 x i64> zeroinitializer, zeroinitializer
  %14 = call <16 x i64> @llvm.smin.v16i64(<16 x i64> %12, <16 x i64> %13)
  %15 = sub <16 x i64> zeroinitializer, zeroinitializer
  %16 = call <16 x i64> @llvm.smin.v16i64(<16 x i64> %14, <16 x i64> %15)
  %17 = sub <16 x i64> zeroinitializer, zeroinitializer
  %18 = call <16 x i64> @llvm.smin.v16i64(<16 x i64> %16, <16 x i64> %17)
  ret <16 x i64> %18
}

declare <16 x i64> @llvm.smin.v16i64(<16 x i64>, <16 x i64>) #0

uselistorder ptr @llvm.smin.v16i64, { 6, 5, 4, 3, 2, 1, 0 }

attributes #0 = { nocallback nocreateundeforpoison nofree nosync nounwind speculatable willreturn memory(none) }
```

Compiled with `opt -passes=slp-vectorizer  -S -slp-revec`, this triggers this assert in the SLP vectoriser:

```
opt: /root/llvm-project/llvm/lib/IR/Instructions.cpp:3048: static llvm::CastInst* llvm::CastInst::Create(llvm::Instruction::CastOps, llvm::Value*, llvm::Type*, const llvm::Twine&, llvm::InsertPosition): Assertion `castIsValid(op, S, Ty) && "Invalid cast!"' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace and instructions to reproduce the bug.
Stack dump:
0.	Program arguments: /opt/compiler-explorer/clang-assertions-trunk/bin/opt -o /app/output.s -S -passes=slp-vectorizer -S -slp-revec <source>
1.	Running pass "function(slp-vectorizer)" on module "<source>"
2.	Running pass "slp-vectorizer" on function "_Z1hhPb"
 #0 0x00000000059e50e8 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x59e50e8)
 #1 0x00000000059e1f94 SignalHandler(int, siginfo_t*, void*) Signals.cpp:0:0
 #2 0x000074b0eb642520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #3 0x000074b0eb6969fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc)
 #4 0x000074b0eb642476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476)
 #5 0x000074b0eb6287f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3)
 #6 0x000074b0eb62871b (/lib/x86_64-linux-gnu/libc.so.6+0x2871b)
 #7 0x000074b0eb639e96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
 #8 0x0000000005739846 (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x5739846)
 #9 0x00000000011bfe3b llvm::IRBuilderBase::CreateIntCast(llvm::Value*, llvm::Type*, bool, llvm::Twine const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x11bfe3b)
#10 0x00000000037a147b (anonymous namespace)::HorizontalReduction::emitReduction(llvm::IRBuilderBase&, llvm::TargetTransformInfo const&, llvm::Type*)::'lambda0'(llvm::Value*, unsigned int, bool)::operator()(llvm::Value*, unsigned int, bool) const SLPVectorizer.cpp:0:0
#11 0x000000000383243b (anonymous namespace)::HorizontalReduction::tryToReduce(llvm::slpvectorizer::BoUpSLP&, llvm::DataLayout const&, llvm::TargetTransformInfo*, llvm::TargetLibraryInfo const&, llvm::AssumptionCache*, llvm::DominatorTree&) SLPVectorizer.cpp:0:0
#12 0x0000000003833468 llvm::SLPVectorizerPass::vectorizeHorReduction(llvm::PHINode*, llvm::Instruction*, llvm::BasicBlock*, llvm::slpvectorizer::BoUpSLP&, llvm::SmallVectorImpl<llvm::WeakTrackingVH>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x3833468)
#13 0x000000000383842b llvm::SLPVectorizerPass::vectorizeRootInstruction(llvm::PHINode*, llvm::Instruction*, llvm::BasicBlock*, llvm::slpvectorizer::BoUpSLP&) (.constprop.0) SLPVectorizer.cpp:0:0
#14 0x000000000383c9c0 llvm::SLPVectorizerPass::vectorizeChainsInBlock(llvm::BasicBlock*, llvm::slpvectorizer::BoUpSLP&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x383c9c0)
#15 0x0000000003842c06 llvm::SLPVectorizerPass::runImpl(llvm::Function&, llvm::ScalarEvolution*, llvm::TargetTransformInfo*, llvm::TargetLibraryInfo*, llvm::AAResults*, llvm::LoopInfo*, llvm::DominatorTree*, llvm::AssumptionCache*, llvm::DemandedBits*, llvm::OptimizationRemarkEmitter*) (.part.0) SLPVectorizer.cpp:0:0
#16 0x000000000384382b llvm::SLPVectorizerPass::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x384382b)
```

See also: https://godbolt.org/z/hd754s4j1

This looks like a weird case, because the `<16 x i64>` are already very big, but this comes from the loop vectoriser and I guess it chooses the VL based on the smallest datatype which is an i8 vector, but anyway, this IR comes from this C code reproducer:

```
#include <algorithm>
long long a;
extern bool b[][17];
short c;
void h(unsigned char d, bool e[]) {
  for (char f = 0; f < c; f++)
    for (short g = 0; g < short(-6365127392424407750LL) - 19753; g++) {
      a = std::min(a, (long long)-std::min((unsigned char)e[f], d));
      b[f][g] = 0;
    }
}
```


_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to