Skip to content

Commit 4f02493

Browse files
committed
[LoopVectorize] Refine hasIrregularType predicate
The `hasIrregularType` predicate checks whether an array of N values of type Ty is "bitcast-compatible" with a <N x Ty> vector. The previous check returned invalid results in some cases where there's some padding between the array elements: eg. a 4-element array of u7 values is considered as compatible with <4 x u7>, even though the vector is only loading/storing 28 bits instead of 32. The problem causes LLVM to generate incorrect code for some targets: for AArch64 the vector loads/stores are lowered in terms of ubfx/bfi, effectively losing the top (N * padding bits). Reviewed By: lebedev.ri Differential Revision: https://reviews.llvm.org/D97465
1 parent 6b025da commit 4f02493

File tree

2 files changed

+34
-15
lines changed

2 files changed

+34
-15
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

+7-15
Original file line numberDiff line numberDiff line change
@@ -371,19 +371,11 @@ static Type *getMemInstValueType(Value *I) {
371371

372372
/// A helper function that returns true if the given type is irregular. The
373373
/// type is irregular if its allocated size doesn't equal the store size of an
374-
/// element of the corresponding vector type at the given vectorization factor.
375-
static bool hasIrregularType(Type *Ty, const DataLayout &DL, ElementCount VF) {
376-
// Determine if an array of VF elements of type Ty is "bitcast compatible"
377-
// with a <VF x Ty> vector.
378-
if (VF.isVector()) {
379-
auto *VectorTy = VectorType::get(Ty, VF);
380-
return TypeSize::get(VF.getKnownMinValue() *
381-
DL.getTypeAllocSize(Ty).getFixedValue(),
382-
VF.isScalable()) != DL.getTypeStoreSize(VectorTy);
383-
}
384-
385-
// If the vectorization factor is one, we just check if an array of type Ty
386-
// requires padding between elements.
374+
/// element of the corresponding vector type.
375+
static bool hasIrregularType(Type *Ty, const DataLayout &DL) {
376+
// Determine if an array of N elements of type Ty is "bitcast compatible"
377+
// with a <N x Ty> vector.
378+
// This is only true if there is no padding between the array elements.
387379
return DL.getTypeAllocSizeInBits(Ty) != DL.getTypeSizeInBits(Ty);
388380
}
389381

@@ -5245,7 +5237,7 @@ bool LoopVectorizationCostModel::interleavedAccessCanBeWidened(
52455237
// requires padding and will be scalarized.
52465238
auto &DL = I->getModule()->getDataLayout();
52475239
auto *ScalarTy = getMemInstValueType(I);
5248-
if (hasIrregularType(ScalarTy, DL, VF))
5240+
if (hasIrregularType(ScalarTy, DL))
52495241
return false;
52505242

52515243
// Check if masking is required.
@@ -5292,7 +5284,7 @@ bool LoopVectorizationCostModel::memoryInstructionCanBeWidened(
52925284
// requires padding and will be scalarized.
52935285
auto &DL = I->getModule()->getDataLayout();
52945286
auto *ScalarTy = LI ? LI->getType() : SI->getValueOperand()->getType();
5295-
if (hasIrregularType(ScalarTy, DL, VF))
5287+
if (hasIrregularType(ScalarTy, DL))
52965288
return false;
52975289

52985290
return true;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
; RUN: opt %s -loop-vectorize -force-vector-width=4 -S | FileCheck %s
2+
3+
; Ensure the array loads/stores are not optimized into vector operations when
4+
; the element type has padding bits.
5+
6+
; CHECK: foo
7+
; CHECK: vector.body
8+
; CHECK-NOT: load <4 x i7>
9+
; CHECK-NOT: store <4 x i7>
10+
; CHECK: for.body
11+
define void @foo(i7* %a, i64 %n) {
12+
entry:
13+
br label %for.body
14+
15+
for.body:
16+
%indvars.iv = phi i64 [ 0, %entry ], [ %indvars.iv.next, %for.body ]
17+
%arrayidx = getelementptr inbounds i7, i7* %a, i64 %indvars.iv
18+
%0 = load i7, i7* %arrayidx, align 1
19+
%sub = add nuw nsw i7 %0, 0
20+
store i7 %sub, i7* %arrayidx, align 1
21+
%indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
22+
%cmp = icmp eq i64 %indvars.iv.next, %n
23+
br i1 %cmp, label %for.exit, label %for.body
24+
25+
for.exit:
26+
ret void
27+
}

0 commit comments

Comments
 (0)