Skip to content

VectorCombine: fix logical error after m_Trunc match #91201

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions llvm/lib/Transforms/Vectorize/VectorCombine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1961,17 +1961,17 @@ bool VectorCombine::foldTruncFromReductions(Instruction &I) {
if (!match(ReductionSrc, m_OneUse(m_Trunc(m_Value(TruncSrc)))))
return false;

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

TTI::TargetCostKind CostKind = TTI::TCK_RecipThroughput;
InstructionCost OldCost =
TTI.getCastInstrCost(Instruction::Trunc, ReductionSrcTy, TruncSrcTy,
TTI::CastContextHint::None, CostKind, Trunc) +
TTI.getArithmeticReductionCost(ReductionOpc, ReductionSrcTy, std::nullopt,
CostKind);
InstructionCost OldCost = TTI.getArithmeticReductionCost(
ReductionOpc, ReductionSrcTy, std::nullopt, CostKind);
if (auto *Trunc = dyn_cast<CastInst>(ReductionSrc))
OldCost +=
TTI.getCastInstrCost(Instruction::Trunc, ReductionSrcTy, TruncSrcTy,
TTI::CastContextHint::None, CostKind, Trunc);
InstructionCost NewCost =
TTI.getArithmeticReductionCost(ReductionOpc, TruncSrcTy, std::nullopt,
CostKind) +
Expand Down
9 changes: 7 additions & 2 deletions llvm/test/Transforms/VectorCombine/pr88796.ll
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
; REQUIRES: asserts
; RUN: not --crash opt -passes=vector-combine -disable-output %s
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 4
; RUN: opt -passes=vector-combine -S %s | FileCheck %s

define i32 @test() {
; CHECK-LABEL: define i32 @test() {
; CHECK-NEXT: entry:
; 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>))
; CHECK-NEXT: ret i32 0
;
entry:
%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>))
ret i32 0
Expand Down