Skip to content

Commit 116e2f9

Browse files
committed
VectorCombine: fix logical error after m_Trunc match
The matcher m_Trunc() matches an Operator with a given Opcode, which could either be an Instruction or ConstExpr. VectorCombine::foldTruncFromReductions() incorrectly assumes that the pattern matched is always an Instruction, and attempts a cast. Fix this. Fixes #88796.
1 parent 9ef28cf commit 116e2f9

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

llvm/lib/Transforms/Vectorize/VectorCombine.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1961,15 +1961,14 @@ bool VectorCombine::foldTruncFromReductions(Instruction &I) {
19611961
if (!match(ReductionSrc, m_OneUse(m_Trunc(m_Value(TruncSrc)))))
19621962
return false;
19631963

1964-
auto *Trunc = cast<CastInst>(ReductionSrc);
19651964
auto *TruncSrcTy = cast<VectorType>(TruncSrc->getType());
19661965
auto *ReductionSrcTy = cast<VectorType>(ReductionSrc->getType());
19671966
Type *ResultTy = I.getType();
19681967

19691968
TTI::TargetCostKind CostKind = TTI::TCK_RecipThroughput;
19701969
InstructionCost OldCost =
19711970
TTI.getCastInstrCost(Instruction::Trunc, ReductionSrcTy, TruncSrcTy,
1972-
TTI::CastContextHint::None, CostKind, Trunc) +
1971+
TTI::CastContextHint::None, CostKind) +
19731972
TTI.getArithmeticReductionCost(ReductionOpc, ReductionSrcTy, std::nullopt,
19741973
CostKind);
19751974
InstructionCost NewCost =

llvm/test/Transforms/VectorCombine/pr88796.ll

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
; REQUIRES: asserts
2-
; RUN: not --crash opt -passes=vector-combine -disable-output %s
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 4
2+
; RUN: opt -passes=vector-combine -S %s | FileCheck %s
33

44
define i32 @test() {
5+
; CHECK-LABEL: define i32 @test() {
6+
; CHECK-NEXT: entry:
7+
; CHECK-NEXT: [[TMP0:%.*]] = tail call i16 @llvm.vector.reduce.and.nxv8i16(<vscale x 8 x i16> trunc (<vscale x 8 x i32> shufflevector (<vscale x 8 x i32> insertelement (<vscale x 8 x i32> poison, i32 268435456, i64 0), <vscale x 8 x i32> poison, <vscale x 8 x i32> zeroinitializer) to <vscale x 8 x i16>))
8+
; CHECK-NEXT: ret i32 0
9+
;
510
entry:
611
%0 = tail call i16 @llvm.vector.reduce.and.nxv8i16(<vscale x 8 x i16> trunc (<vscale x 8 x i32> shufflevector (<vscale x 8 x i32> insertelement (<vscale x 8 x i32> poison, i32 268435456, i64 0), <vscale x 8 x i32> poison, <vscale x 8 x i32> zeroinitializer) to <vscale x 8 x i16>))
712
ret i32 0

0 commit comments

Comments
 (0)