Skip to content

Commit c4f723a

Browse files
authored
[LV] Strip unmaintainable MinBWs assert (#136858)
tryToWiden attempts to replace an Instruction with a Constant from SCEV, but forgets to erase the Instruction from the MinBWs map, leading to an assert in VPlanTransforms::truncateToMinimalBitwidths. Going forward, the assertion in truncateToMinimalBitwidths is unmaintainable, as LV could simplify the expression at any point: fix the bug by stripping the unmaintable assertion. Fixes #125278.
1 parent 1484f82 commit c4f723a

File tree

2 files changed

+59
-40
lines changed

2 files changed

+59
-40
lines changed

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1632,11 +1632,6 @@ static void licm(VPlan &Plan) {
16321632

16331633
void VPlanTransforms::truncateToMinimalBitwidths(
16341634
VPlan &Plan, const MapVector<Instruction *, uint64_t> &MinBWs) {
1635-
#ifndef NDEBUG
1636-
// Count the processed recipes and cross check the count later with MinBWs
1637-
// size, to make sure all entries in MinBWs have been handled.
1638-
unsigned NumProcessedRecipes = 0;
1639-
#endif
16401635
// Keep track of created truncates, so they can be re-used. Note that we
16411636
// cannot use RAUW after creating a new truncate, as this would could make
16421637
// other uses have different types for their operands, making them invalidly
@@ -1659,38 +1654,12 @@ void VPlanTransforms::truncateToMinimalBitwidths(
16591654
if (!NewResSizeInBits)
16601655
continue;
16611656

1662-
#ifndef NDEBUG
1663-
NumProcessedRecipes++;
1664-
#endif
16651657
// If the value wasn't vectorized, we must maintain the original scalar
16661658
// type. Skip those here, after incrementing NumProcessedRecipes. Also
16671659
// skip casts which do not need to be handled explicitly here, as
16681660
// redundant casts will be removed during recipe simplification.
1669-
if (isa<VPReplicateRecipe, VPWidenCastRecipe>(&R)) {
1670-
#ifndef NDEBUG
1671-
// If any of the operands is a live-in and not used by VPWidenRecipe or
1672-
// VPWidenSelectRecipe, but in MinBWs, make sure it is counted as
1673-
// processed as well. When MinBWs is currently constructed, there is no
1674-
// information about whether recipes are widened or replicated and in
1675-
// case they are reciplicated the operands are not truncated. Counting
1676-
// them them here ensures we do not miss any recipes in MinBWs.
1677-
// TODO: Remove once the analysis is done on VPlan.
1678-
for (VPValue *Op : R.operands()) {
1679-
if (!Op->isLiveIn())
1680-
continue;
1681-
auto *UV = dyn_cast_or_null<Instruction>(Op->getUnderlyingValue());
1682-
if (UV && MinBWs.contains(UV) && !ProcessedTruncs.contains(Op) &&
1683-
none_of(Op->users(),
1684-
IsaPred<VPWidenRecipe, VPWidenSelectRecipe>)) {
1685-
// Add an entry to ProcessedTruncs to avoid counting the same
1686-
// operand multiple times.
1687-
ProcessedTruncs[Op] = nullptr;
1688-
NumProcessedRecipes += 1;
1689-
}
1690-
}
1691-
#endif
1661+
if (isa<VPReplicateRecipe, VPWidenCastRecipe>(&R))
16921662
continue;
1693-
}
16941663

16951664
Type *OldResTy = TypeInfo.inferScalarType(ResultVPV);
16961665
unsigned OldResSizeInBits = OldResTy->getScalarSizeInBits();
@@ -1749,19 +1718,11 @@ void VPlanTransforms::truncateToMinimalBitwidths(
17491718
NewOp->insertBefore(&R);
17501719
} else {
17511720
PH->appendRecipe(NewOp);
1752-
#ifndef NDEBUG
1753-
auto *OpInst = dyn_cast<Instruction>(Op->getLiveInIRValue());
1754-
bool IsContained = MinBWs.contains(OpInst);
1755-
NumProcessedRecipes += IsContained;
1756-
#endif
17571721
}
17581722
}
17591723

17601724
}
17611725
}
1762-
1763-
assert(MinBWs.size() == NumProcessedRecipes &&
1764-
"some entries in MinBWs haven't been processed");
17651726
}
17661727

17671728
/// Remove BranchOnCond recipes with true conditions together with removing
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals none --version 5
2+
; RUN: opt -passes=loop-vectorize -force-vector-width=4 -S %s | FileCheck %s
3+
4+
define void @pr125278(ptr %dst, i64 %n) {
5+
; CHECK-LABEL: define void @pr125278(
6+
; CHECK-SAME: ptr [[DST:%.*]], i64 [[N:%.*]]) {
7+
; CHECK-NEXT: [[ENTRY:.*:]]
8+
; CHECK-NEXT: [[TRUE_EXT:%.*]] = zext i1 true to i32
9+
; CHECK-NEXT: [[UMAX:%.*]] = call i64 @llvm.umax.i64(i64 [[N]], i64 1)
10+
; CHECK-NEXT: br label %[[COND:.*]]
11+
; CHECK: [[COND_LOOPEXIT:.*]]:
12+
; CHECK-NEXT: br label %[[COND]]
13+
; CHECK: [[COND]]:
14+
; CHECK-NEXT: [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 [[UMAX]], 4
15+
; CHECK-NEXT: br i1 [[MIN_ITERS_CHECK]], label %[[SCALAR_PH:.*]], label %[[VECTOR_PH:.*]]
16+
; CHECK: [[VECTOR_PH]]:
17+
; CHECK-NEXT: [[N_MOD_VF:%.*]] = urem i64 [[UMAX]], 4
18+
; CHECK-NEXT: [[N_VEC:%.*]] = sub i64 [[UMAX]], [[N_MOD_VF]]
19+
; CHECK-NEXT: br label %[[VECTOR_BODY:.*]]
20+
; CHECK: [[VECTOR_BODY]]:
21+
; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY]] ]
22+
; CHECK-NEXT: store i8 1, ptr [[DST]], align 1
23+
; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 4
24+
; CHECK-NEXT: [[TMP0:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
25+
; CHECK-NEXT: br i1 [[TMP0]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP0:![0-9]+]]
26+
; CHECK: [[MIDDLE_BLOCK]]:
27+
; CHECK-NEXT: [[CMP_N:%.*]] = icmp eq i64 [[UMAX]], [[N_VEC]]
28+
; CHECK-NEXT: br i1 [[CMP_N]], label %[[COND_LOOPEXIT]], label %[[SCALAR_PH]]
29+
; CHECK: [[SCALAR_PH]]:
30+
; CHECK-NEXT: [[BC_RESUME_VAL:%.*]] = phi i64 [ [[N_VEC]], %[[MIDDLE_BLOCK]] ], [ 0, %[[COND]] ]
31+
; CHECK-NEXT: br label %[[LOOP:.*]]
32+
; CHECK: [[LOOP]]:
33+
; CHECK-NEXT: [[IV:%.*]] = phi i64 [ [[BC_RESUME_VAL]], %[[SCALAR_PH]] ], [ [[IV_NEXT:%.*]], %[[LOOP]] ]
34+
; CHECK-NEXT: [[FALSE_EXT:%.*]] = zext i1 false to i32
35+
; CHECK-NEXT: [[XOR:%.*]] = xor i32 [[FALSE_EXT]], [[TRUE_EXT]]
36+
; CHECK-NEXT: [[XOR_TRUNC:%.*]] = trunc i32 [[XOR]] to i8
37+
; CHECK-NEXT: store i8 [[XOR_TRUNC]], ptr [[DST]], align 1
38+
; CHECK-NEXT: [[IV_NEXT]] = add i64 [[IV]], 1
39+
; CHECK-NEXT: [[CMP:%.*]] = icmp ult i64 [[IV_NEXT]], [[N]]
40+
; CHECK-NEXT: br i1 [[CMP]], label %[[LOOP]], label %[[COND_LOOPEXIT]], !llvm.loop [[LOOP3:![0-9]+]]
41+
;
42+
entry:
43+
%true.ext = zext i1 true to i32
44+
br label %cond
45+
46+
cond:
47+
br label %loop
48+
49+
loop:
50+
%iv = phi i64 [ 0, %cond ], [ %iv.next, %loop ]
51+
%false.ext = zext i1 false to i32
52+
%xor = xor i32 %false.ext, %true.ext
53+
%xor.trunc = trunc i32 %xor to i8
54+
store i8 %xor.trunc, ptr %dst, align 1
55+
%iv.next = add i64 %iv, 1
56+
%cmp = icmp ult i64 %iv.next, %n
57+
br i1 %cmp, label %loop, label %cond
58+
}

0 commit comments

Comments
 (0)