Skip to content

Commit 7ae3464

Browse files
committed
[CostModel] avoid crashing while finding scalarization overhead
The constrained intrinsics have metadata arguments, so the tests here were crashing as noted in D90554 (and that was reverted even though this bug exists independently of that change).
1 parent e4f9b5d commit 7ae3464

File tree

3 files changed

+67
-3
lines changed

3 files changed

+67
-3
lines changed

llvm/include/llvm/CodeGen/BasicTTIImpl.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -602,24 +602,30 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
602602
return thisT()->getScalarizationOverhead(Ty, DemandedElts, Insert, Extract);
603603
}
604604

605-
/// Estimate the overhead of scalarizing an instructions unique
605+
/// Estimate the overhead of scalarizing an instruction's unique
606606
/// non-constant operands. The types of the arguments are ordinarily
607607
/// scalar, in which case the costs are multiplied with VF.
608608
unsigned getOperandsScalarizationOverhead(ArrayRef<const Value *> Args,
609609
unsigned VF) {
610610
unsigned Cost = 0;
611611
SmallPtrSet<const Value*, 4> UniqueOperands;
612612
for (const Value *A : Args) {
613+
// Disregard things like metadata arguments.
614+
Type *Ty = A->getType();
615+
if (!Ty->isIntOrIntVectorTy() && !Ty->isFPOrFPVectorTy() &&
616+
!Ty->isPtrOrPtrVectorTy())
617+
continue;
618+
613619
if (!isa<Constant>(A) && UniqueOperands.insert(A).second) {
614-
auto *VecTy = dyn_cast<VectorType>(A->getType());
620+
auto *VecTy = dyn_cast<VectorType>(Ty);
615621
if (VecTy) {
616622
// If A is a vector operand, VF should be 1 or correspond to A.
617623
assert((VF == 1 ||
618624
VF == cast<FixedVectorType>(VecTy)->getNumElements()) &&
619625
"Vector argument does not match VF");
620626
}
621627
else
622-
VecTy = FixedVectorType::get(A->getType(), VF);
628+
VecTy = FixedVectorType::get(Ty, VF);
623629

624630
Cost += getScalarizationOverhead(VecTy, false, true);
625631
}

llvm/test/Analysis/CostModel/ARM/intrinsic-cost-kinds.ll

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ declare <16 x float> @llvm.fmuladd.v16f32(<16 x float>, <16 x float>, <16 x floa
1919
declare float @llvm.log2.f32(float)
2020
declare <16 x float> @llvm.log2.v16f32(<16 x float>)
2121

22+
declare float @llvm.experimental.constrained.fadd.f32(float, float, metadata, metadata)
23+
declare <16 x float> @llvm.experimental.constrained.fadd.v16f32(<16 x float>, <16 x float>, metadata, metadata)
24+
2225
declare i32 @llvm.cttz.i32(i32, i1)
2326
declare <16 x i32> @llvm.cttz.v16i32(<16 x i32>, i1)
2427

@@ -112,6 +115,32 @@ define void @log2(float %a, <16 x float> %va) {
112115
ret void
113116
}
114117

118+
define void @constrained_fadd(float %a, <16 x float> %va) {
119+
; THRU-LABEL: 'constrained_fadd'
120+
; THRU-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %s = call float @llvm.experimental.constrained.fadd.f32(float %a, float %a, metadata !"round.dynamic", metadata !"fpexcept.ignore")
121+
; THRU-NEXT: Cost Model: Found an estimated cost of 528 for instruction: %t = call <16 x float> @llvm.experimental.constrained.fadd.v16f32(<16 x float> %va, <16 x float> %va, metadata !"round.dynamic", metadata !"fpexcept.ignore")
122+
; THRU-NEXT: Cost Model: Found an estimated cost of 0 for instruction: ret void
123+
;
124+
; LATE-LABEL: 'constrained_fadd'
125+
; LATE-NEXT: Cost Model: Found an estimated cost of 3 for instruction: %s = call float @llvm.experimental.constrained.fadd.f32(float %a, float %a, metadata !"round.dynamic", metadata !"fpexcept.ignore")
126+
; LATE-NEXT: Cost Model: Found an estimated cost of 3 for instruction: %t = call <16 x float> @llvm.experimental.constrained.fadd.v16f32(<16 x float> %va, <16 x float> %va, metadata !"round.dynamic", metadata !"fpexcept.ignore")
127+
; LATE-NEXT: Cost Model: Found an estimated cost of 1 for instruction: ret void
128+
;
129+
; SIZE-LABEL: 'constrained_fadd'
130+
; SIZE-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %s = call float @llvm.experimental.constrained.fadd.f32(float %a, float %a, metadata !"round.dynamic", metadata !"fpexcept.ignore")
131+
; SIZE-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %t = call <16 x float> @llvm.experimental.constrained.fadd.v16f32(<16 x float> %va, <16 x float> %va, metadata !"round.dynamic", metadata !"fpexcept.ignore")
132+
; SIZE-NEXT: Cost Model: Found an estimated cost of 1 for instruction: ret void
133+
;
134+
; SIZE_LATE-LABEL: 'constrained_fadd'
135+
; SIZE_LATE-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %s = call float @llvm.experimental.constrained.fadd.f32(float %a, float %a, metadata !"round.dynamic", metadata !"fpexcept.ignore")
136+
; SIZE_LATE-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %t = call <16 x float> @llvm.experimental.constrained.fadd.v16f32(<16 x float> %va, <16 x float> %va, metadata !"round.dynamic", metadata !"fpexcept.ignore")
137+
; SIZE_LATE-NEXT: Cost Model: Found an estimated cost of 1 for instruction: ret void
138+
;
139+
%s = call float @llvm.experimental.constrained.fadd.f32(float %a, float %a, metadata !"round.dynamic", metadata !"fpexcept.ignore")
140+
%t = call <16 x float> @llvm.experimental.constrained.fadd.v16f32(<16 x float> %va, <16 x float> %va, metadata !"round.dynamic", metadata !"fpexcept.ignore")
141+
ret void
142+
}
143+
115144
define void @cttz(i32 %a, <16 x i32> %va) {
116145
; THRU-LABEL: 'cttz'
117146
; THRU-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %s = call i32 @llvm.cttz.i32(i32 %a, i1 false)

llvm/test/Analysis/CostModel/X86/intrinsic-cost-kinds.ll

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ declare <16 x float> @llvm.fmuladd.v16f32(<16 x float>, <16 x float>, <16 x floa
2222
declare float @llvm.log2.f32(float)
2323
declare <16 x float> @llvm.log2.v16f32(<16 x float>)
2424

25+
declare float @llvm.experimental.constrained.fadd.f32(float, float, metadata, metadata)
26+
declare <16 x float> @llvm.experimental.constrained.fadd.v16f32(<16 x float>, <16 x float>, metadata, metadata)
27+
2528
declare i32 @llvm.cttz.i32(i32, i1)
2629
declare <16 x i32> @llvm.cttz.v16i32(<16 x i32>, i1)
2730

@@ -143,6 +146,32 @@ define void @log2(float %a, <16 x float> %va) {
143146
ret void
144147
}
145148

149+
define void @constrained_fadd(float %a, <16 x float> %va) {
150+
; THRU-LABEL: 'constrained_fadd'
151+
; THRU-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %s = call float @llvm.experimental.constrained.fadd.f32(float %a, float %a, metadata !"round.dynamic", metadata !"fpexcept.ignore")
152+
; THRU-NEXT: Cost Model: Found an estimated cost of 40 for instruction: %t = call <16 x float> @llvm.experimental.constrained.fadd.v16f32(<16 x float> %va, <16 x float> %va, metadata !"round.dynamic", metadata !"fpexcept.ignore")
153+
; THRU-NEXT: Cost Model: Found an estimated cost of 0 for instruction: ret void
154+
;
155+
; LATE-LABEL: 'constrained_fadd'
156+
; LATE-NEXT: Cost Model: Found an estimated cost of 3 for instruction: %s = call float @llvm.experimental.constrained.fadd.f32(float %a, float %a, metadata !"round.dynamic", metadata !"fpexcept.ignore")
157+
; LATE-NEXT: Cost Model: Found an estimated cost of 3 for instruction: %t = call <16 x float> @llvm.experimental.constrained.fadd.v16f32(<16 x float> %va, <16 x float> %va, metadata !"round.dynamic", metadata !"fpexcept.ignore")
158+
; LATE-NEXT: Cost Model: Found an estimated cost of 1 for instruction: ret void
159+
;
160+
; SIZE-LABEL: 'constrained_fadd'
161+
; SIZE-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %s = call float @llvm.experimental.constrained.fadd.f32(float %a, float %a, metadata !"round.dynamic", metadata !"fpexcept.ignore")
162+
; SIZE-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %t = call <16 x float> @llvm.experimental.constrained.fadd.v16f32(<16 x float> %va, <16 x float> %va, metadata !"round.dynamic", metadata !"fpexcept.ignore")
163+
; SIZE-NEXT: Cost Model: Found an estimated cost of 1 for instruction: ret void
164+
;
165+
; SIZE_LATE-LABEL: 'constrained_fadd'
166+
; SIZE_LATE-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %s = call float @llvm.experimental.constrained.fadd.f32(float %a, float %a, metadata !"round.dynamic", metadata !"fpexcept.ignore")
167+
; SIZE_LATE-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %t = call <16 x float> @llvm.experimental.constrained.fadd.v16f32(<16 x float> %va, <16 x float> %va, metadata !"round.dynamic", metadata !"fpexcept.ignore")
168+
; SIZE_LATE-NEXT: Cost Model: Found an estimated cost of 1 for instruction: ret void
169+
;
170+
%s = call float @llvm.experimental.constrained.fadd.f32(float %a, float %a, metadata !"round.dynamic", metadata !"fpexcept.ignore")
171+
%t = call <16 x float> @llvm.experimental.constrained.fadd.v16f32(<16 x float> %va, <16 x float> %va, metadata !"round.dynamic", metadata !"fpexcept.ignore")
172+
ret void
173+
}
174+
146175
define void @cttz(i32 %a, <16 x i32> %va) {
147176
; THRU-LABEL: 'cttz'
148177
; THRU-NEXT: Cost Model: Found an estimated cost of 3 for instruction: %s = call i32 @llvm.cttz.i32(i32 %a, i1 false)

0 commit comments

Comments
 (0)