Skip to content

Commit ef1ca4d

Browse files
committed
[AArch64] Fix incorrect use of MVT::getVectorNumElements in AArch64TTIImpl::getVectorInstrCost
If we are inserting into or extracting from a scalable vector we do not know the number of elements at runtime, so we can only let the index wrap for fixed-length vectors. Tests added here: Analysis/CostModel/AArch64/sve-insert-extract.ll Differential Revision: https://reviews.llvm.org/D117099
1 parent 54eb708 commit ef1ca4d

File tree

3 files changed

+65
-5
lines changed

3 files changed

+65
-5
lines changed

llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1594,9 +1594,12 @@ InstructionCost AArch64TTIImpl::getVectorInstrCost(unsigned Opcode, Type *Val,
15941594
if (!LT.second.isVector())
15951595
return 0;
15961596

1597-
// The type may be split. Normalize the index to the new type.
1598-
unsigned Width = LT.second.getVectorNumElements();
1599-
Index = Index % Width;
1597+
// The type may be split. For fixed-width vectors we can normalize the
1598+
// index to the new type.
1599+
if (LT.second.isFixedLengthVector()) {
1600+
unsigned Width = LT.second.getVectorNumElements();
1601+
Index = Index % Width;
1602+
}
16001603

16011604
// The element at index zero is already inside the vector.
16021605
if (Index == 0)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2+
; RUN: opt -cost-model -analyze -S < %s | FileCheck %s
3+
4+
target triple = "aarch64-unknown-linux-gnu"
5+
6+
7+
define void @ins_el0() #0 {
8+
; CHECK: Cost Model: Found an estimated cost of 0 for instruction: %v0 = insertelement <vscale x 16 x i8> zeroinitializer, i8 0, i64 0
9+
; CHECK: Cost Model: Found an estimated cost of 0 for instruction: %v1 = insertelement <vscale x 8 x i16> zeroinitializer, i16 0, i64 0
10+
; CHECK: Cost Model: Found an estimated cost of 0 for instruction: %v2 = insertelement <vscale x 4 x i32> zeroinitializer, i32 0, i64 0
11+
; CHECK: Cost Model: Found an estimated cost of 0 for instruction: %v3 = insertelement <vscale x 2 x i64> zeroinitializer, i64 0, i64 0
12+
%v0 = insertelement <vscale x 16 x i8> zeroinitializer, i8 0, i64 0
13+
%v1 = insertelement <vscale x 8 x i16> zeroinitializer, i16 0, i64 0
14+
%v2 = insertelement <vscale x 4 x i32> zeroinitializer, i32 0, i64 0
15+
%v3 = insertelement <vscale x 2 x i64> zeroinitializer, i64 0, i64 0
16+
ret void
17+
}
18+
19+
define void @ins_el1() #0 {
20+
; CHECK: Cost Model: Found an estimated cost of 3 for instruction: %v0 = insertelement <vscale x 16 x i8> zeroinitializer, i8 0, i64 1
21+
; CHECK: Cost Model: Found an estimated cost of 3 for instruction: %v1 = insertelement <vscale x 8 x i16> zeroinitializer, i16 0, i64 1
22+
; CHECK: Cost Model: Found an estimated cost of 3 for instruction: %v2 = insertelement <vscale x 4 x i32> zeroinitializer, i32 0, i64 1
23+
; CHECK: Cost Model: Found an estimated cost of 3 for instruction: %v3 = insertelement <vscale x 2 x i64> zeroinitializer, i64 0, i64 1
24+
%v0 = insertelement <vscale x 16 x i8> zeroinitializer, i8 0, i64 1
25+
%v1 = insertelement <vscale x 8 x i16> zeroinitializer, i16 0, i64 1
26+
%v2 = insertelement <vscale x 4 x i32> zeroinitializer, i32 0, i64 1
27+
%v3 = insertelement <vscale x 2 x i64> zeroinitializer, i64 0, i64 1
28+
ret void
29+
}
30+
31+
32+
define void @ext_el0() #0 {
33+
; CHECK: Cost Model: Found an estimated cost of 0 for instruction: %v0 = extractelement <vscale x 16 x i8> zeroinitializer, i64 0
34+
; CHECK: Cost Model: Found an estimated cost of 0 for instruction: %v1 = extractelement <vscale x 8 x i16> zeroinitializer, i64 0
35+
; CHECK: Cost Model: Found an estimated cost of 0 for instruction: %v2 = extractelement <vscale x 4 x i32> zeroinitializer, i64 0
36+
; CHECK: Cost Model: Found an estimated cost of 0 for instruction: %v3 = extractelement <vscale x 2 x i64> zeroinitializer, i64 0
37+
%v0 = extractelement <vscale x 16 x i8> zeroinitializer, i64 0
38+
%v1 = extractelement <vscale x 8 x i16> zeroinitializer, i64 0
39+
%v2 = extractelement <vscale x 4 x i32> zeroinitializer, i64 0
40+
%v3 = extractelement <vscale x 2 x i64> zeroinitializer, i64 0
41+
ret void
42+
}
43+
44+
define void @ext_el1() #0 {
45+
; CHECK: Cost Model: Found an estimated cost of 3 for instruction: %v0 = extractelement <vscale x 16 x i8> zeroinitializer, i64 1
46+
; CHECK: Cost Model: Found an estimated cost of 3 for instruction: %v1 = extractelement <vscale x 8 x i16> zeroinitializer, i64 1
47+
; CHECK: Cost Model: Found an estimated cost of 3 for instruction: %v2 = extractelement <vscale x 4 x i32> zeroinitializer, i64 1
48+
; CHECK: Cost Model: Found an estimated cost of 3 for instruction: %v3 = extractelement <vscale x 2 x i64> zeroinitializer, i64 1
49+
%v0 = extractelement <vscale x 16 x i8> zeroinitializer, i64 1
50+
%v1 = extractelement <vscale x 8 x i16> zeroinitializer, i64 1
51+
%v2 = extractelement <vscale x 4 x i32> zeroinitializer, i64 1
52+
%v3 = extractelement <vscale x 2 x i64> zeroinitializer, i64 1
53+
ret void
54+
}
55+
56+
57+
attributes #0 = { "target-features"="+sve" vscale_range(1, 16) }

llvm/test/Analysis/CostModel/AArch64/sve-intrinsics.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
define void @vector_insert_extract(<vscale x 4 x i32> %v0, <vscale x 16 x i32> %v1, <16 x i32> %v2) {
55
; CHECK-LABEL: 'vector_insert_extract'
6-
; CHECK-NEXT: Cost Model: Found an estimated cost of 72 for instruction: %extract_fixed_from_scalable = call <16 x i32> @llvm.experimental.vector.extract.v16i32.nxv4i32(<vscale x 4 x i32> %v0, i64 0)
7-
; CHECK-NEXT: Cost Model: Found an estimated cost of 72 for instruction: %insert_fixed_into_scalable = call <vscale x 4 x i32> @llvm.experimental.vector.insert.nxv4i32.v16i32(<vscale x 4 x i32> %v0, <16 x i32> %v2, i64 0)
6+
; CHECK-NEXT: Cost Model: Found an estimated cost of 81 for instruction: %extract_fixed_from_scalable = call <16 x i32> @llvm.experimental.vector.extract.v16i32.nxv4i32(<vscale x 4 x i32> %v0, i64 0)
7+
; CHECK-NEXT: Cost Model: Found an estimated cost of 81 for instruction: %insert_fixed_into_scalable = call <vscale x 4 x i32> @llvm.experimental.vector.insert.nxv4i32.v16i32(<vscale x 4 x i32> %v0, <16 x i32> %v2, i64 0)
88
; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %extract_scalable_from_scalable = call <vscale x 4 x i32> @llvm.experimental.vector.extract.nxv4i32.nxv16i32(<vscale x 16 x i32> %v1, i64 0)
99
; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %insert_scalable_into_scalable = call <vscale x 16 x i32> @llvm.experimental.vector.insert.nxv16i32.nxv4i32(<vscale x 16 x i32> %v1, <vscale x 4 x i32> %v0, i64 0)
1010
; CHECK-NEXT: Cost Model: Found an estimated cost of 0 for instruction: ret void

0 commit comments

Comments
 (0)