Skip to content

Commit 19b0e12

Browse files
authored
[ConstantFolding] Fold sqrt poison -> poison (#141821)
I noticed this when a sqrt produced by VectorCombine with a poison operand wasn't getting folded away to poison. Most intrinsics in general could probably be folded to poison if one of their arguments are poison too. Are there any exceptions to this we need to be aware of?
1 parent 40cc7b4 commit 19b0e12

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

llvm/lib/Analysis/ConstantFolding.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2223,8 +2223,13 @@ static Constant *ConstantFoldScalarCall1(StringRef Name,
22232223

22242224
if (isa<PoisonValue>(Operands[0])) {
22252225
// TODO: All of these operations should probably propagate poison.
2226-
if (IntrinsicID == Intrinsic::canonicalize)
2226+
switch (IntrinsicID) {
2227+
case Intrinsic::canonicalize:
2228+
case Intrinsic::sqrt:
22272229
return PoisonValue::get(Ty);
2230+
default:
2231+
break;
2232+
}
22282233
}
22292234

22302235
if (isa<UndefValue>(Operands[0])) {

llvm/test/Transforms/InstSimplify/fp-undef-poison.ll

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,3 +293,53 @@ define double @fmul_nnan_inf_op1(double %x) {
293293
%r = fmul nnan double %x, 0xfff0000000000000
294294
ret double %r
295295
}
296+
297+
define float @sqrt_poison() {
298+
; CHECK-LABEL: @sqrt_poison(
299+
; CHECK-NEXT: ret float poison
300+
;
301+
%sqrt = call float @llvm.sqrt(float poison)
302+
ret float %sqrt
303+
}
304+
305+
define <2 x float> @sqrt_poison_fixed_vec() {
306+
; CHECK-LABEL: @sqrt_poison_fixed_vec(
307+
; CHECK-NEXT: ret <2 x float> poison
308+
;
309+
%sqrt = call <2 x float> @llvm.sqrt(<2 x float> poison)
310+
ret <2 x float> %sqrt
311+
}
312+
313+
define <2 x float> @sqrt_poison_elt_fixed_vec() {
314+
; CHECK-LABEL: @sqrt_poison_elt_fixed_vec(
315+
; CHECK-NEXT: ret <2 x float> <float 1.000000e+00, float poison>
316+
;
317+
%sqrt = call <2 x float> @llvm.sqrt(<2 x float> <float 1.0, float poison>)
318+
ret <2 x float> %sqrt
319+
}
320+
321+
define <vscale x 2 x float> @sqrt_poison_scalable_vec() {
322+
; CHECK-LABEL: @sqrt_poison_scalable_vec(
323+
; CHECK-NEXT: ret <vscale x 2 x float> poison
324+
;
325+
%sqrt = call <vscale x 2 x float> @llvm.sqrt(<vscale x 2 x float> poison)
326+
ret <vscale x 2 x float> %sqrt
327+
}
328+
329+
define float @sqrt_nnan_nan() {
330+
; CHECK-LABEL: @sqrt_nnan_nan(
331+
; CHECK-NEXT: [[SQRT:%.*]] = call nnan float @llvm.sqrt.f32(float 0x7FF8000000000000)
332+
; CHECK-NEXT: ret float [[SQRT]]
333+
;
334+
%sqrt = call nnan float @llvm.sqrt(float 0x7ff8000000000000)
335+
ret float %sqrt
336+
}
337+
338+
define float @sqrt_ninf_inf() {
339+
; CHECK-LABEL: @sqrt_ninf_inf(
340+
; CHECK-NEXT: [[SQRT:%.*]] = call ninf float @llvm.sqrt.f32(float 0xFFF0000000000000)
341+
; CHECK-NEXT: ret float [[SQRT]]
342+
;
343+
%sqrt = call ninf float @llvm.sqrt(float 0xfff0000000000000)
344+
ret float %sqrt
345+
}

0 commit comments

Comments
 (0)