Skip to content

Commit cab09e7

Browse files
authored
[InstCombine] Propagate FMF from fptrunc when folding fptrunc fabs(X) -> fabs(fptrunc X) (#143352)
Alive2: https://alive2.llvm.org/ce/z/DWV3G3 fptrunc yields infinity when the input cannot fit in the target type. So ninf should be propagated from fptrunc. For other intrinsics, the previous check ensures that the result is never an infinity: https://github.com/llvm/llvm-project/blob/5d3899d293e902124c3602b466031b6b799fb123/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp#L1910-L1917 Closes #143122.
1 parent c015321 commit cab09e7

File tree

4 files changed

+40
-5
lines changed

4 files changed

+40
-5
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1917,7 +1917,9 @@ Instruction *InstCombinerImpl::visitFPTrunc(FPTruncInst &FPT) {
19171917
II->getOperandBundlesAsDefs(OpBundles);
19181918
CallInst *NewCI =
19191919
CallInst::Create(Overload, {InnerTrunc}, OpBundles, II->getName());
1920-
NewCI->copyFastMathFlags(II);
1920+
// A normal value may be converted to an infinity. It means that we cannot
1921+
// propagate ninf from the intrinsic. So we propagate FMF from fptrunc.
1922+
NewCI->copyFastMathFlags(&FPT);
19211923
return NewCI;
19221924
}
19231925
}

llvm/test/Transforms/InstCombine/double-float-shrink-2.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ define float @test_shrink_intrin_fabs_fast_double_src(double %D) {
449449
; CHECK-NEXT: ret float [[F]]
450450
;
451451
%E = call fast double @llvm.fabs.f64(double %D)
452-
%F = fptrunc double %E to float
452+
%F = fptrunc fast double %E to float
453453
ret float %F
454454
}
455455

@@ -611,7 +611,7 @@ define half @test_mismatched_type_intrin_fabs_fast_double_src(double %D) {
611611
; CHECK-NEXT: ret half [[F]]
612612
;
613613
%E = call fast double @llvm.fabs.f64(double %D)
614-
%F = fptrunc double %E to half
614+
%F = fptrunc fast double %E to half
615615
ret half %F
616616
}
617617

llvm/test/Transforms/InstCombine/fabs.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1522,7 +1522,7 @@ define float @test_fabs_nsz_used_by_frem(float %x) {
15221522
define half @test_fabs_nsz_used_by_fptrunc(float %x) {
15231523
; CHECK-LABEL: @test_fabs_nsz_used_by_fptrunc(
15241524
; CHECK-NEXT: [[TMP1:%.*]] = fptrunc float [[X:%.*]] to half
1525-
; CHECK-NEXT: [[OP:%.*]] = call nsz half @llvm.fabs.f16(half [[TMP1]])
1525+
; CHECK-NEXT: [[OP:%.*]] = call half @llvm.fabs.f16(half [[TMP1]])
15261526
; CHECK-NEXT: ret half [[OP]]
15271527
;
15281528
%cmp = fcmp oge float %x, 0.000000e+00

llvm/test/Transforms/InstCombine/fpcast.ll

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,47 @@ define half @test3(float %a) {
3232
define half @test3_fast(float %a) {
3333
; CHECK-LABEL: @test3_fast(
3434
; CHECK-NEXT: [[TMP1:%.*]] = fptrunc float [[A:%.*]] to half
35-
; CHECK-NEXT: [[C:%.*]] = call half @llvm.fabs.f16(half [[TMP1]])
35+
; CHECK-NEXT: [[C:%.*]] = call fast half @llvm.fabs.f16(half [[TMP1]])
3636
; CHECK-NEXT: ret half [[C]]
3737
;
3838
%b = call float @llvm.fabs.f32(float %a)
3939
%c = fptrunc fast float %b to half
4040
ret half %c
4141
}
4242

43+
define half @test3_both_ninf(float %a) {
44+
; CHECK-LABEL: @test3_both_ninf(
45+
; CHECK-NEXT: [[TMP1:%.*]] = fptrunc float [[A:%.*]] to half
46+
; CHECK-NEXT: [[C:%.*]] = call ninf half @llvm.fabs.f16(half [[TMP1]])
47+
; CHECK-NEXT: ret half [[C]]
48+
;
49+
%b = call ninf float @llvm.fabs.f32(float %a)
50+
%c = fptrunc ninf float %b to half
51+
ret half %c
52+
}
53+
54+
define half @test3_fabs_ninf(float %a) {
55+
; CHECK-LABEL: @test3_fabs_ninf(
56+
; CHECK-NEXT: [[TMP1:%.*]] = fptrunc float [[A:%.*]] to half
57+
; CHECK-NEXT: [[C:%.*]] = call half @llvm.fabs.f16(half [[TMP1]])
58+
; CHECK-NEXT: ret half [[C]]
59+
;
60+
%b = call ninf float @llvm.fabs.f32(float %a)
61+
%c = fptrunc float %b to half
62+
ret half %c
63+
}
64+
65+
define half @test3_fptrunc_ninf(float %a) {
66+
; CHECK-LABEL: @test3_fptrunc_ninf(
67+
; CHECK-NEXT: [[TMP1:%.*]] = fptrunc float [[A:%.*]] to half
68+
; CHECK-NEXT: [[C:%.*]] = call ninf half @llvm.fabs.f16(half [[TMP1]])
69+
; CHECK-NEXT: ret half [[C]]
70+
;
71+
%b = call float @llvm.fabs.f32(float %a)
72+
%c = fptrunc ninf float %b to half
73+
ret half %c
74+
}
75+
4376
define half @fneg_fptrunc(float %a) {
4477
; CHECK-LABEL: @fneg_fptrunc(
4578
; CHECK-NEXT: [[TMP1:%.*]] = fptrunc float [[A:%.*]] to half

0 commit comments

Comments
 (0)