Bug type: crash
Component: SLPVectorizer
Bug location: llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp: computeExtractCost
Issue title: [SLPVectorizer] Assertion `(Idx + EltsPerVector) <= alignTo(NumElts, EltsPerVector) && "SK_ExtractSubvector index out of range"' failed.
Labels: llvm:SLPVectorizer
crash-on-valid
Reported by: dtcxzyw

Reproducer: https://godbolt.org/z/fxYWYnWKs
```
; bin/opt -passes=slp-vectorizer reduced.ll -S
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"

declare void @callee(i32, i64)

define i32 @main(i32 %g_274.promoted2477.i.i.i, ptr %p) {
entry:
  %g_274.promoted2477.i.i.i1 = load i32, ptr %p, align 4
  br i1 false, label %for.inc347.i.i.i, label %for.cond159.preheader.i.i.i

for.cond159.preheader.i.i.i:                      ; preds = %entry
  %tobool239.not.us.i.i.i.not = icmp eq i32 %g_274.promoted2477.i.i.i1, 0
  %tobool239.not.us.i.1.i.i = icmp eq i32 %g_274.promoted2477.i.i.i1, 0
  %tobool239.not.us.i.3.i.i = icmp eq i32 %g_274.promoted2477.i.i.i1, 0
  %0 = or i1 %tobool239.not.us.i.3.i.i, %tobool239.not.us.i.1.i.i
  %tobool239.not.us.i.4.i.i = icmp eq i32 %g_274.promoted2477.i.i.i1, 0
  %inc34.i21362404.us.v.i.4.i.i = select i1 %tobool239.not.us.i.4.i.i, i64 0, i64 0
  %tobool239.not.us.i.5.i.i = icmp eq i32 0, 0
  %inc34.i21362404.us.v.i.5.i.i = select i1 %tobool239.not.us.i.5.i.i, i64 0, i64 0
  %inc34.i21362404.us.i.5.i.i = or i64 %inc34.i21362404.us.v.i.4.i.i, %inc34.i21362404.us.v.i.5.i.i
  %1 = or i1 %tobool239.not.us.i.5.i.i, %tobool239.not.us.i.4.i.i
  %inc34.i21362404.us.v.i.6.i.i = select i1 false, i64 0, i64 0
  %inc34.i21362404.us.i.6.i.i = or i64 %inc34.i21362404.us.i.5.i.i, %inc34.i21362404.us.v.i.6.i.i
  %tobool239.not.us.i.7.i.i = icmp eq i32 0, 0
  %inc34.i21362404.us.v.i.7.i.i = select i1 false, i64 0, i64 0
  %inc34.i21362404.us.i.7.i.i = or i64 %inc34.i21362404.us.i.6.i.i, %inc34.i21362404.us.v.i.7.i.i
  %inc34.i21362404.us.v.i.8.i.i = select i1 false, i64 0, i64 0
  %inc34.i21362404.us.i.8.i.i = or i64 %inc34.i21362404.us.i.7.i.i, %inc34.i21362404.us.v.i.8.i.i
  %inc34.i21362404.us.v.i.9.i.i = select i1 false, i64 0, i64 0
  %inc34.i21362404.us.i.9.i.i = or i64 %inc34.i21362404.us.i.8.i.i, %inc34.i21362404.us.v.i.9.i.i
  %tobool239.not.us.i.10.i.i = icmp eq i32 0, 0
  %tobool239.not.us.i.11.i.i = icmp eq i32 0, 0
  %2 = or i1 %tobool239.not.us.i.11.i.i, %tobool239.not.us.i.10.i.i
  %tobool239.not.us.i.12.i.i = icmp eq i32 %g_274.promoted2477.i.i.i, 0
  %3 = or i1 %tobool239.not.us.i.12.i.i, %2
  %tobool239.not.us.i.13.i.i = icmp eq i32 0, 0
  %tobool239.not.us.i.14.i.i = icmp eq i32 0, 0
  %4 = or i1 %tobool239.not.us.i.14.i.i, %tobool239.not.us.i.13.i.i
  %tobool239.not.us.i.16.i.i = icmp eq i32 0, 0
  %tobool239.not.us.i.17.i.i = icmp eq i32 0, 0
  %5 = or i1 %tobool239.not.us.i.17.i.i, %tobool239.not.us.i.16.i.i
  %tobool239.not.us.i.18.i.i = icmp eq i32 0, 0
  %6 = or i1 %tobool239.not.us.i.18.i.i, %5
  %tobool239.not.us.i.19.i.i = icmp eq i32 0, 0
  %tobool239.not.us.i.20.i.i = icmp eq i32 0, 0
  %7 = or i1 %tobool239.not.us.i.20.i.i, %tobool239.not.us.i.19.i.i
  %tobool239.not.us.i.21.i.i = icmp eq i32 0, 0
  %8 = or i1 %tobool239.not.us.i.21.i.i, %7
  %tobool239.not.us.i.22.i.i = icmp eq i32 0, 0
  %tobool239.not.us.i.23.i.i = icmp eq i32 0, 0
  %9 = or i1 %tobool239.not.us.i.23.i.i, %tobool239.not.us.i.22.i.i
  %tobool239.not.us.i.24.i.i = icmp eq i32 0, 0
  %10 = or i1 %tobool239.not.us.i.24.i.i, %9
  %11 = select i1 %10, i1 true, i1 %8
  %12 = select i1 %11, i1 true, i1 %6
  %13 = select i1 %12, i1 true, i1 %4
  %14 = select i1 %13, i1 true, i1 %3
  %15 = select i1 %14, i1 true, i1 %tobool239.not.us.i.7.i.i
  %16 = select i1 %15, i1 true, i1 %1
  %17 = or i1 %0, %tobool239.not.us.i.i.i.not
  %18 = select i1 %16, i1 true, i1 %17
  %and252.us.i.24.i.i = select i1 %18, i32 0, i32 0
  br label %for.inc347.i.i.i

for.inc347.i.i.i:                                 ; preds = %for.cond159.preheader.i.i.i, %entry
  %g_202.promoted.i.i.i = phi i32 [ %and252.us.i.24.i.i, %for.cond159.preheader.i.i.i ], [ 0, %entry ]
  %.us-phi24612468.i.i.i = phi i64 [ %inc34.i21362404.us.i.9.i.i, %for.cond159.preheader.i.i.i ], [ 0, %entry ]
  call void @callee(i32 %g_202.promoted.i.i.i, i64 %.us-phi24612468.i.i.i)
  ret i32 0
}
```
```
opt: /root/llvm-project/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp:9844: llvm::InstructionCost llvm::slpvectorizer::BoUpSLP::ShuffleCostEstimator::computeExtractCost(llvm::ArrayRef<llvm::Value*>, llvm::ArrayRef<int>, llvm::ArrayRef<std::optional<llvm::TargetTransformInfo::ShuffleKind> >, unsigned int): Assertion `(Idx + EltsPerVector) <= alignTo(NumElts, EltsPerVector) && "SK_ExtractSubvector index out of range"' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.	Program arguments: /opt/compiler-explorer/clang-assertions-trunk/bin/opt -o /app/output.s -S -passes=slp-vectorizer <source>
1.	Running pass "function(slp-vectorizer)" on module "<source>"
2.	Running pass "slp-vectorizer" on function "main"
 #0 0x00000000050d8608 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x50d8608)
 #1 0x00000000050d5fbc SignalHandler(int) Signals.cpp:0:0
 #2 0x00007ed1b9642520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #3 0x00007ed1b96969fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc)
 #4 0x00007ed1b9642476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476)
 #5 0x00007ed1b96287f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3)
 #6 0x00007ed1b962871b (/lib/x86_64-linux-gnu/libc.so.6+0x2871b)
 #7 0x00007ed1b9639e96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
 #8 0x000000000413c75f llvm::slpvectorizer::BoUpSLP::ShuffleCostEstimator::computeExtractCost(llvm::ArrayRef<llvm::Value*>, llvm::ArrayRef<int>, llvm::ArrayRef<std::optional<llvm::TargetTransformInfo::ShuffleKind>>, unsigned int) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x413c75f)
 #9 0x0000000004140d8b llvm::slpvectorizer::BoUpSLP::ShuffleCostEstimator::adjustExtracts(llvm::slpvectorizer::BoUpSLP::TreeEntry const*, llvm::MutableArrayRef<int>, llvm::ArrayRef<std::optional<llvm::TargetTransformInfo::ShuffleKind>>, unsigned int, bool&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x4140d8b)
#10 0x000000000417b42a llvm::InstructionCost llvm::slpvectorizer::BoUpSLP::processBuildVector<llvm::slpvectorizer::BoUpSLP::ShuffleCostEstimator, llvm::InstructionCost, llvm::TargetTransformInfo, llvm::ArrayRef<llvm::Value*>, llvm::slpvectorizer::BoUpSLP, llvm::SmallPtrSetImpl<llvm::Value*>>(llvm::slpvectorizer::BoUpSLP::TreeEntry const*, llvm::Type*, llvm::TargetTransformInfo&, llvm::ArrayRef<llvm::Value*>&, llvm::slpvectorizer::BoUpSLP&, llvm::SmallPtrSetImpl<llvm::Value*>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x417b42a)
#11 0x000000000417cae7 llvm::slpvectorizer::BoUpSLP::getEntryCost(llvm::slpvectorizer::BoUpSLP::TreeEntry const*, llvm::ArrayRef<llvm::Value*>, llvm::SmallPtrSetImpl<llvm::Value*>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x417cae7)
#12 0x00000000041b6e11 llvm::slpvectorizer::BoUpSLP::getTreeCost(llvm::ArrayRef<llvm::Value*>) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x41b6e11)
#13 0x00000000041bc5df (anonymous namespace)::HorizontalReduction::tryToReduce(llvm::slpvectorizer::BoUpSLP&, llvm::DataLayout const&, llvm::TargetTransformInfo*, llvm::TargetLibraryInfo const&) SLPVectorizer.cpp:0:0
#14 0x00000000041bfb71 llvm::SLPVectorizerPass::vectorizeHorReduction(llvm::PHINode*, llvm::Instruction*, llvm::BasicBlock*, llvm::slpvectorizer::BoUpSLP&, llvm::SmallVectorImpl<llvm::WeakTrackingVH>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x41bfb71)
#15 0x00000000041c431b llvm::SLPVectorizerPass::vectorizeRootInstruction(llvm::PHINode*, llvm::Instruction*, llvm::BasicBlock*, llvm::slpvectorizer::BoUpSLP&) (.constprop.0) SLPVectorizer.cpp:0:0
#16 0x00000000041c856e llvm::SLPVectorizerPass::vectorizeChainsInBlock(llvm::BasicBlock*, llvm::slpvectorizer::BoUpSLP&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x41c856e)
#17 0x00000000041cf231 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
#18 0x00000000041cfedb llvm::SLPVectorizerPass::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x41cfedb)
#19 0x0000000002f64f0e llvm::detail::PassModel<llvm::Function, llvm::SLPVectorizerPass, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x2f64f0e)
#20 0x0000000004ed9788 llvm::PassManager<llvm::Function, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x4ed9788)
#21 0x0000000000e2773e llvm::detail::PassModel<llvm::Function, llvm::PassManager<llvm::Function, llvm::AnalysisManager<llvm::Function>>, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0xe2773e)
#22 0x0000000004ed81ae llvm::ModuleToFunctionPassAdaptor::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x4ed81ae)
#23 0x0000000000e26e8e llvm::detail::PassModel<llvm::Module, llvm::ModuleToFunctionPassAdaptor, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0xe26e8e)
#24 0x0000000004ed7be0 llvm::PassManager<llvm::Module, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x4ed7be0)
#25 0x000000000090a07a llvm::runPassPipeline(llvm::StringRef, llvm::Module&, llvm::TargetMachine*, llvm::TargetLibraryInfoImpl*, llvm::ToolOutputFile*, llvm::ToolOutputFile*, llvm::ToolOutputFile*, llvm::StringRef, llvm::ArrayRef<llvm::PassPlugin>, llvm::ArrayRef<std::function<void (llvm::PassBuilder&)>>, llvm::opt_tool::OutputKind, llvm::opt_tool::VerifierKind, bool, bool, bool, bool, bool, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x90a07a)
#26 0x00000000008fce7c optMain (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x8fce7c)
#27 0x00007ed1b9629d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#28 0x00007ed1b9629e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#29 0x00000000008f493e _start (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x8f493e)
Program terminated with signal: SIGSEGV
Compiler returned: 139
```

llvm version: 8a9921f5692ab33451d11454b40a023ca0965a69



Fix patch:
commit b65b2b4ab60763515694c740935989f908a03312
Author: Alexey Bataev <a.bataev@outlook.com>
Date:   Wed Oct 23 12:03:38 2024 -0700

    [SLP]Expand vector to the whole register size in extracts adjustment
    
    Need to expand the number of elements to the whole register to correctly
    process estimation and avoid compiler crash.
    
    Fixes #113462

diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index d824c40c401d..889c4d94ba5e 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -9840,13 +9840,14 @@ class BoUpSLP::ShuffleCostEstimator : public BaseShuffleAnalysis {
             ::getShuffleCost(TTI, *RegShuffleKind,
                              getWidenedType(ScalarTy, EltsPerVector), SubMask);
       }
+      const unsigned BaseVF = getFullVectorNumberOfElements(
+          *R.TTI, VL.front()->getType(), alignTo(NumElts, EltsPerVector));
       for (unsigned Idx : Indices) {
-        assert((Idx + EltsPerVector) <= alignTo(NumElts, EltsPerVector) &&
+        assert((Idx + EltsPerVector) <= BaseVF &&
                "SK_ExtractSubvector index out of range");
-        Cost += ::getShuffleCost(
-            TTI, TTI::SK_ExtractSubvector,
-            getWidenedType(ScalarTy, alignTo(NumElts, EltsPerVector)), {},
-            CostKind, Idx, getWidenedType(ScalarTy, EltsPerVector));
+        Cost += ::getShuffleCost(TTI, TTI::SK_ExtractSubvector,
+                                 getWidenedType(ScalarTy, BaseVF), {}, CostKind,
+                                 Idx, getWidenedType(ScalarTy, EltsPerVector));
       }
       // Second attempt to check, if just a permute is better estimated than
       // subvector extract.


Regression tests:
file: llvm/test/Transforms/SLPVectorizer/partial-register-extract.ll
commands: opt -S --passes=slp-vectorizer < %s
tests: test_name: <module>
test_body: 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"

declare void @callee(i32, i64)

define i32 @main(i32 %g_274.promoted2477.i.i.i, ptr %p) {
entry:
  %g_274.promoted2477.i.i.i1 = load i32, ptr %p, align 4
  br i1 false, label %for.inc347.i.i.i, label %for.cond159.preheader.i.i.i

for.cond159.preheader.i.i.i:                      ; preds = %entry
  %tobool239.not.us.i.i.i.not = icmp eq i32 %g_274.promoted2477.i.i.i1, 0
  %tobool239.not.us.i.1.i.i = icmp eq i32 %g_274.promoted2477.i.i.i1, 0
  %tobool239.not.us.i.3.i.i = icmp eq i32 %g_274.promoted2477.i.i.i1, 0
  %0 = or i1 %tobool239.not.us.i.3.i.i, %tobool239.not.us.i.1.i.i
  %tobool239.not.us.i.4.i.i = icmp eq i32 %g_274.promoted2477.i.i.i1, 0
  %inc34.i21362404.us.v.i.4.i.i = select i1 %tobool239.not.us.i.4.i.i, i64 0, i64 0
  %tobool239.not.us.i.5.i.i = icmp eq i32 0, 0
  %inc34.i21362404.us.v.i.5.i.i = select i1 %tobool239.not.us.i.5.i.i, i64 0, i64 0
  %inc34.i21362404.us.i.5.i.i = or i64 %inc34.i21362404.us.v.i.4.i.i, %inc34.i21362404.us.v.i.5.i.i
  %1 = or i1 %tobool239.not.us.i.5.i.i, %tobool239.not.us.i.4.i.i
  %inc34.i21362404.us.v.i.6.i.i = select i1 false, i64 0, i64 0
  %inc34.i21362404.us.i.6.i.i = or i64 %inc34.i21362404.us.i.5.i.i, %inc34.i21362404.us.v.i.6.i.i
  %tobool239.not.us.i.7.i.i = icmp eq i32 0, 0
  %inc34.i21362404.us.v.i.7.i.i = select i1 false, i64 0, i64 0
  %inc34.i21362404.us.i.7.i.i = or i64 %inc34.i21362404.us.i.6.i.i, %inc34.i21362404.us.v.i.7.i.i
  %inc34.i21362404.us.v.i.8.i.i = select i1 false, i64 0, i64 0
  %inc34.i21362404.us.i.8.i.i = or i64 %inc34.i21362404.us.i.7.i.i, %inc34.i21362404.us.v.i.8.i.i
  %inc34.i21362404.us.v.i.9.i.i = select i1 false, i64 0, i64 0
  %inc34.i21362404.us.i.9.i.i = or i64 %inc34.i21362404.us.i.8.i.i, %inc34.i21362404.us.v.i.9.i.i
  %tobool239.not.us.i.10.i.i = icmp eq i32 0, 0
  %tobool239.not.us.i.11.i.i = icmp eq i32 0, 0
  %2 = or i1 %tobool239.not.us.i.11.i.i, %tobool239.not.us.i.10.i.i
  %tobool239.not.us.i.12.i.i = icmp eq i32 %g_274.promoted2477.i.i.i, 0
  %3 = or i1 %tobool239.not.us.i.12.i.i, %2
  %tobool239.not.us.i.13.i.i = icmp eq i32 0, 0
  %tobool239.not.us.i.14.i.i = icmp eq i32 0, 0
  %4 = or i1 %tobool239.not.us.i.14.i.i, %tobool239.not.us.i.13.i.i
  %tobool239.not.us.i.16.i.i = icmp eq i32 0, 0
  %tobool239.not.us.i.17.i.i = icmp eq i32 0, 0
  %5 = or i1 %tobool239.not.us.i.17.i.i, %tobool239.not.us.i.16.i.i
  %tobool239.not.us.i.18.i.i = icmp eq i32 0, 0
  %6 = or i1 %tobool239.not.us.i.18.i.i, %5
  %tobool239.not.us.i.19.i.i = icmp eq i32 0, 0
  %tobool239.not.us.i.20.i.i = icmp eq i32 0, 0
  %7 = or i1 %tobool239.not.us.i.20.i.i, %tobool239.not.us.i.19.i.i
  %tobool239.not.us.i.21.i.i = icmp eq i32 0, 0
  %8 = or i1 %tobool239.not.us.i.21.i.i, %7
  %tobool239.not.us.i.22.i.i = icmp eq i32 0, 0
  %tobool239.not.us.i.23.i.i = icmp eq i32 0, 0
  %9 = or i1 %tobool239.not.us.i.23.i.i, %tobool239.not.us.i.22.i.i
  %tobool239.not.us.i.24.i.i = icmp eq i32 0, 0
  %10 = or i1 %tobool239.not.us.i.24.i.i, %9
  %11 = select i1 %10, i1 true, i1 %8
  %12 = select i1 %11, i1 true, i1 %6
  %13 = select i1 %12, i1 true, i1 %4
  %14 = select i1 %13, i1 true, i1 %3
  %15 = select i1 %14, i1 true, i1 %tobool239.not.us.i.7.i.i
  %16 = select i1 %15, i1 true, i1 %1
  %17 = or i1 %0, %tobool239.not.us.i.i.i.not
  %18 = select i1 %16, i1 true, i1 %17
  %and252.us.i.24.i.i = select i1 %18, i32 0, i32 0
  br label %for.inc347.i.i.i

for.inc347.i.i.i:                                 ; preds = %for.cond159.preheader.i.i.i, %entry
  %g_202.promoted.i.i.i = phi i32 [ %and252.us.i.24.i.i, %for.cond159.preheader.i.i.i ], [ 0, %entry ]
  %.us-phi24612468.i.i.i = phi i64 [ %inc34.i21362404.us.i.9.i.i, %for.cond159.preheader.i.i.i ], [ 0, %entry ]
  call void @callee(i32 %g_202.promoted.i.i.i, i64 %.us-phi24612468.i.i.i)
  ret i32 0
}