Skip to content

Commit 3538ca3

Browse files
committed
[flang] Propagate more FastMath flags to lowering.
Plugged in propagation of nnan/nsz/arcp/afn/reassoc related options to lowering/FirOpBuilder. Reviewed By: jeanPerier, tblah, awarzynski Differential Revision: https://reviews.llvm.org/D137580
1 parent 72f8955 commit 3538ca3

File tree

5 files changed

+51
-2
lines changed

5 files changed

+51
-2
lines changed

flang/include/flang/Common/MathOptionsBase.def

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,20 @@ ENUM_MATHOPT(FPContractEnabled, unsigned, 1, 0)
2222
/// Permit floating point optimizations without regard to infinities.
2323
ENUM_MATHOPT(NoHonorInfs, unsigned, 1, 0)
2424

25+
/// Permit floating point optimization without regard to NaN
26+
ENUM_MATHOPT(NoHonorNaNs, unsigned, 1, 0)
27+
28+
/// Allow math functions to be replaced with an approximately equivalent
29+
/// calculation
30+
ENUM_MATHOPT(ApproxFunc, unsigned, 1, 0)
31+
32+
/// Allow optimizations that ignore the sign of floating point zeros
33+
ENUM_MATHOPT(NoSignedZeros, unsigned, 1, 0)
34+
35+
/// Allow reassociation transformations for floating-point instructions
36+
ENUM_MATHOPT(AssociativeMath, unsigned, 1, 0)
37+
38+
/// Allow division operations to be reassociated
39+
ENUM_MATHOPT(ReciprocalMath, unsigned, 1, 0)
40+
2541
#undef ENUM_MATHOPT

flang/include/flang/Frontend/LangOptions.def

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ ENUM_LANGOPT(FPContractMode, FPModeKind, 2, FPM_Off) ///< FP Contract Mode (off/
2525
LANGOPT(NoHonorInfs, 1, false)
2626
/// Permit floating point optimization without regard to NaN
2727
LANGOPT(NoHonorNaNs, 1, false)
28-
/// Allow math functions to be replaced with an approximately equivalent calculation
28+
/// Allow math functions to be replaced with an approximately equivalent
29+
/// calculation
2930
LANGOPT(ApproxFunc, 1, false)
3031
/// Allow optimizations that ignore the sign of floating point zeros
3132
LANGOPT(NoSignedZeros, 1, false)

flang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -957,5 +957,10 @@ void CompilerInvocation::setLoweringOptions() {
957957
mathOpts
958958
.setFPContractEnabled(langOptions.getFPContractMode() ==
959959
LangOptions::FPM_Fast)
960-
.setNoHonorInfs(langOptions.NoHonorInfs);
960+
.setNoHonorInfs(langOptions.NoHonorInfs)
961+
.setNoHonorNaNs(langOptions.NoHonorNaNs)
962+
.setApproxFunc(langOptions.ApproxFunc)
963+
.setNoSignedZeros(langOptions.NoSignedZeros)
964+
.setAssociativeMath(langOptions.AssociativeMath)
965+
.setReciprocalMath(langOptions.ReciprocalMath);
961966
}

flang/lib/Optimizer/Builder/FIRBuilder.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,21 @@ void fir::FirOpBuilder::setFastMathFlags(
592592
if (options.getNoHonorInfs()) {
593593
arithFMF = arithFMF | mlir::arith::FastMathFlags::ninf;
594594
}
595+
if (options.getNoHonorNaNs()) {
596+
arithFMF = arithFMF | mlir::arith::FastMathFlags::nnan;
597+
}
598+
if (options.getApproxFunc()) {
599+
arithFMF = arithFMF | mlir::arith::FastMathFlags::afn;
600+
}
601+
if (options.getNoSignedZeros()) {
602+
arithFMF = arithFMF | mlir::arith::FastMathFlags::nsz;
603+
}
604+
if (options.getAssociativeMath()) {
605+
arithFMF = arithFMF | mlir::arith::FastMathFlags::reassoc;
606+
}
607+
if (options.getReciprocalMath()) {
608+
arithFMF = arithFMF | mlir::arith::FastMathFlags::arcp;
609+
}
595610
setFastMathFlags(arithFMF);
596611
}
597612

flang/test/Lower/fast-math-arithmetic.f90

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
11
! RUN: %flang_fc1 -emit-fir -ffp-contract=fast %s -o - 2>&1 | FileCheck --check-prefixes=CONTRACT,ALL %s
22
! RUN: %flang_fc1 -emit-fir -menable-no-infs %s -o - 2>&1 | FileCheck --check-prefixes=NINF,ALL %s
3+
! RUN: %flang_fc1 -emit-fir -menable-no-nans %s -o - 2>&1 | FileCheck --check-prefixes=NNAN,ALL %s
4+
! RUN: %flang_fc1 -emit-fir -fapprox-func %s -o - 2>&1 | FileCheck --check-prefixes=AFN,ALL %s
5+
! RUN: %flang_fc1 -emit-fir -fno-signed-zeros %s -o - 2>&1 | FileCheck --check-prefixes=NSZ,ALL %s
6+
! RUN: %flang_fc1 -emit-fir -mreassociate %s -o - 2>&1 | FileCheck --check-prefixes=REASSOC,ALL %s
7+
! RUN: %flang_fc1 -emit-fir -freciprocal-math %s -o - 2>&1 | FileCheck --check-prefixes=ARCP,ALL %s
8+
! RUN: %flang_fc1 -emit-fir -ffp-contract=fast -menable-no-infs -menable-no-nans -fapprox-func -fno-signed-zeros -mreassociate -freciprocal-math %s -o - 2>&1 | FileCheck --check-prefixes=FAST,ALL %s
39

410
! ALL-LABEL: func.func @_QPtest
511
subroutine test(x)
612
real x
713
! CONTRACT: arith.mulf{{.*}}, {{.*}} fastmath<[[ATTRS:contract]]> : f32
814
! NINF: arith.mulf{{.*}}, {{.*}} fastmath<[[ATTRS:ninf]]> : f32
15+
! NNAN: arith.mulf{{.*}}, {{.*}} fastmath<[[ATTRS:nnan]]> : f32
16+
! AFN: arith.mulf{{.*}}, {{.*}} fastmath<[[ATTRS:afn]]> : f32
17+
! NSZ: arith.mulf{{.*}}, {{.*}} fastmath<[[ATTRS:nsz]]> : f32
18+
! REASSOC: arith.mulf{{.*}}, {{.*}} fastmath<[[ATTRS:reassoc]]> : f32
19+
! ARCP: arith.mulf{{.*}}, {{.*}} fastmath<[[ATTRS:arcp]]> : f32
20+
! FAST: arith.mulf{{.*}}, {{.*}} fastmath<[[ATTRS:fast]]> : f32
921
! ALL: arith.divf{{.*}}, {{.*}} fastmath<[[ATTRS]]> : f32
1022
! ALL: arith.addf{{.*}}, {{.*}} fastmath<[[ATTRS]]> : f32
1123
! ALL: arith.subf{{.*}}, {{.*}} fastmath<[[ATTRS]]> : f32

0 commit comments

Comments
 (0)