Skip to content

Commit 57b9c15

Browse files
authored
VectorCombine: fix logical error after m_Trunc match (#91201)
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 57b9c15

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

llvm/lib/Transforms/Vectorize/VectorCombine.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1961,17 +1961,17 @@ 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;
1970-
InstructionCost OldCost =
1971-
TTI.getCastInstrCost(Instruction::Trunc, ReductionSrcTy, TruncSrcTy,
1972-
TTI::CastContextHint::None, CostKind, Trunc) +
1973-
TTI.getArithmeticReductionCost(ReductionOpc, ReductionSrcTy, std::nullopt,
1974-
CostKind);
1969+
InstructionCost OldCost = TTI.getArithmeticReductionCost(
1970+
ReductionOpc, ReductionSrcTy, std::nullopt, CostKind);
1971+
if (auto *Trunc = dyn_cast<CastInst>(ReductionSrc))
1972+
OldCost +=
1973+
TTI.getCastInstrCost(Instruction::Trunc, ReductionSrcTy, TruncSrcTy,
1974+
TTI::CastContextHint::None, CostKind, Trunc);
19751975
InstructionCost NewCost =
19761976
TTI.getArithmeticReductionCost(ReductionOpc, TruncSrcTy, std::nullopt,
19771977
CostKind) +

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)