Skip to content

Commit 6047b5a

Browse files
committed
[flang] Set fast math related function attributes for -Ofast/-ffast-math
The implemented logic matches the logic used for Clang in emitting these attributes. Although it's hoped that function attributes won't be needed in the future (vs using fast math flags in individual IR instructions), there are codegen differences currently with/without these attributes, as can be seen in issues like #79257 or by hacking Clang to avoid producing these attributes and observing codegen changes.
1 parent 748c295 commit 6047b5a

File tree

7 files changed

+94
-8
lines changed

7 files changed

+94
-8
lines changed

flang/include/flang/Optimizer/Transforms/Passes.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ struct FunctionAttrTypes {
9191

9292
std::unique_ptr<mlir::Pass> createFunctionAttrPass();
9393
std::unique_ptr<mlir::Pass>
94-
createFunctionAttrPass(FunctionAttrTypes &functionAttr);
94+
createFunctionAttrPass(FunctionAttrTypes &functionAttr, bool noInfsFPMath,
95+
bool noNaNsFPMath, bool approxFuncFPMath,
96+
bool noSignedZerosFPMath, bool unsafeFPMath);
9597

9698
// declarative passes
9799
#define GEN_PASS_REGISTRATION

flang/include/flang/Optimizer/Transforms/Passes.td

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,21 @@ def FunctionAttr : Pass<"function-attr", "mlir::func::FuncOp"> {
366366
"mlir::LLVM::framePointerKind::FramePointerKind",
367367
/*default=*/"mlir::LLVM::framePointerKind::FramePointerKind{}",
368368
"frame pointer">,
369+
Option<"noInfsFPMath", "no-infs-fp-math",
370+
"bool", /*default=*/"false",
371+
"Set the no-infs-fp-math attribute on functions in the module.">,
372+
Option<"noNaNsFPMath", "no-nans-fp-math",
373+
"bool", /*default=*/"false",
374+
"Set the no-nans-fp-math attribute on functions in the module.">,
375+
Option<"approxFuncFPMath", "approx-func-fp-math",
376+
"bool", /*default=*/"false",
377+
"Set the approx-func-fp-math attribute on functions in the module.">,
378+
Option<"noSignedZerosFPMath", "no-signed-zeros-fp-math",
379+
"bool", /*default=*/"false",
380+
"Set the no-signed-zeros-fp-math attribute on functions in the module.">,
381+
Option<"unsafeFPMath", "unsafe-fp-math",
382+
"bool", /*default=*/"false",
383+
"Set the unsafe-fp-math attribute on functions in the module.">,
369384
];
370385
let constructor = "::fir::createFunctionAttrPass()";
371386
}

flang/include/flang/Tools/CLOptions.inc

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -315,15 +315,22 @@ inline void createDefaultFIRCodeGenPassPipeline(
315315
// Add function attributes
316316
fir::FunctionAttrTypes functionAttrs;
317317

318-
if (config.FramePointerKind != llvm::FramePointerKind::None) {
318+
if (config.FramePointerKind != llvm::FramePointerKind::None ||
319+
config.NoInfsFPMath || config.NoNaNsFPMath || config.ApproxFuncFPMath ||
320+
config.NoSignedZerosFPMath || config.UnsafeFPMath) {
319321
if (config.FramePointerKind == llvm::FramePointerKind::NonLeaf)
320322
functionAttrs.framePointerKind =
321323
mlir::LLVM::framePointerKind::FramePointerKind::NonLeaf;
322-
else
324+
else if (config.FramePointerKind == llvm::FramePointerKind::All)
323325
functionAttrs.framePointerKind =
324326
mlir::LLVM::framePointerKind::FramePointerKind::All;
327+
else
328+
functionAttrs.framePointerKind =
329+
mlir::LLVM::framePointerKind::FramePointerKind::None;
325330

326-
pm.addPass(fir::createFunctionAttrPass(functionAttrs));
331+
pm.addPass(fir::createFunctionAttrPass(functionAttrs, config.NoInfsFPMath,
332+
config.NoNaNsFPMath, config.ApproxFuncFPMath,
333+
config.NoSignedZerosFPMath, config.UnsafeFPMath));
327334
}
328335

329336
fir::addFIRToLLVMPass(pm, config);

flang/include/flang/Tools/CrossToolHelpers.h

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#ifndef FORTRAN_TOOLS_CROSS_TOOL_HELPERS_H
1414
#define FORTRAN_TOOLS_CROSS_TOOL_HELPERS_H
1515

16+
#include "flang/Common/MathOptionsBase.h"
1617
#include "flang/Frontend/CodeGenOptions.h"
1718
#include "flang/Frontend/LangOptions.h"
1819
#include <cstdint>
@@ -28,14 +29,24 @@ struct MLIRToLLVMPassPipelineConfig {
2829
OptLevel = level;
2930
}
3031
explicit MLIRToLLVMPassPipelineConfig(llvm::OptimizationLevel level,
31-
const Fortran::frontend::CodeGenOptions &opts) {
32+
const Fortran::frontend::CodeGenOptions &opts,
33+
const Fortran::common::MathOptionsBase &mathOpts) {
3234
OptLevel = level;
3335
StackArrays = opts.StackArrays;
3436
Underscoring = opts.Underscoring;
3537
LoopVersioning = opts.LoopVersioning;
3638
DebugInfo = opts.getDebugInfo();
3739
AliasAnalysis = opts.AliasAnalysis;
3840
FramePointerKind = opts.getFramePointer();
41+
// The logic for setting these attributes is intended to match the logic
42+
// used in Clang.
43+
NoInfsFPMath = mathOpts.getNoHonorInfs();
44+
NoNaNsFPMath = mathOpts.getNoHonorNaNs();
45+
ApproxFuncFPMath = mathOpts.getApproxFunc();
46+
NoSignedZerosFPMath = mathOpts.getNoSignedZeros();
47+
UnsafeFPMath = mathOpts.getAssociativeMath() &&
48+
mathOpts.getReciprocalMath() && NoSignedZerosFPMath &&
49+
ApproxFuncFPMath && mathOpts.getFPContractEnabled();
3950
}
4051

4152
llvm::OptimizationLevel OptLevel; ///< optimisation level
@@ -49,6 +60,13 @@ struct MLIRToLLVMPassPipelineConfig {
4960
llvm::FramePointerKind::None; ///< Add frame pointer to functions.
5061
unsigned VScaleMin = 0; ///< SVE vector range minimum.
5162
unsigned VScaleMax = 0; ///< SVE vector range maximum.
63+
bool NoInfsFPMath = false; ///< Set no-infs-fp-math attribute for functions.
64+
bool NoNaNsFPMath = false; ///< Set no-nans-fp-math attribute for functions.
65+
bool ApproxFuncFPMath =
66+
false; ///< Set approx-func-fp-math attribute for functions.
67+
bool NoSignedZerosFPMath =
68+
false; ///< Set no-signed-zeros-fp-math attribute for functions.
69+
bool UnsafeFPMath = false; ///< Set unsafe-fp-math attribute for functions.
5270
};
5371

5472
struct OffloadModuleOpts {

flang/lib/Frontend/FrontendActions.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,7 @@ void CodeGenAction::generateLLVMIR() {
785785

786786
CompilerInstance &ci = this->getInstance();
787787
auto opts = ci.getInvocation().getCodeGenOpts();
788+
auto mathOpts = ci.getInvocation().getLoweringOpts().getMathOptions();
788789
llvm::OptimizationLevel level = mapToLevel(opts);
789790

790791
fir::support::loadDialects(*mlirCtx);
@@ -797,7 +798,7 @@ void CodeGenAction::generateLLVMIR() {
797798
pm.addPass(std::make_unique<Fortran::lower::VerifierPass>());
798799
pm.enableVerifier(/*verifyPasses=*/true);
799800

800-
MLIRToLLVMPassPipelineConfig config(level, opts);
801+
MLIRToLLVMPassPipelineConfig config(level, opts, mathOpts);
801802

802803
if (auto vsr = getVScaleRange(ci)) {
803804
config.VScaleMin = vsr->first;

flang/lib/Optimizer/Transforms/FunctionAttr.cpp

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ class FunctionAttrPass : public fir::impl::FunctionAttrBase<FunctionAttrPass> {
2727
public:
2828
FunctionAttrPass(const fir::FunctionAttrOptions &options) {
2929
framePointerKind = options.framePointerKind;
30+
noInfsFPMath = options.noInfsFPMath;
31+
noNaNsFPMath = options.noNaNsFPMath;
32+
approxFuncFPMath = options.approxFuncFPMath;
33+
noSignedZerosFPMath = options.noSignedZerosFPMath;
34+
unsafeFPMath = options.unsafeFPMath;
3035
}
3136
FunctionAttrPass() {}
3237
void runOnOperation() override;
@@ -45,14 +50,32 @@ void FunctionAttrPass::runOnOperation() {
4550
func->setAttr("frame_pointer", mlir::LLVM::FramePointerKindAttr::get(
4651
context, framePointerKind));
4752

53+
if (noInfsFPMath)
54+
func->setAttr("no_infs_fp_math", mlir::BoolAttr::get(context, true));
55+
if (noNaNsFPMath)
56+
func->setAttr("no_nans_fp_math", mlir::BoolAttr::get(context, true));
57+
if (approxFuncFPMath)
58+
func->setAttr("approx_func_fp_math", mlir::BoolAttr::get(context, true));
59+
if (noSignedZerosFPMath)
60+
func->setAttr("no_signed_zeros_fp_math",
61+
mlir::BoolAttr::get(context, true));
62+
if (unsafeFPMath)
63+
func->setAttr("unsafe_fp_math", mlir::BoolAttr::get(context, true));
64+
4865
LLVM_DEBUG(llvm::dbgs() << "=== End " DEBUG_TYPE " ===\n");
4966
}
5067

51-
std::unique_ptr<mlir::Pass>
52-
fir::createFunctionAttrPass(fir::FunctionAttrTypes &functionAttr) {
68+
std::unique_ptr<mlir::Pass> fir::createFunctionAttrPass(
69+
fir::FunctionAttrTypes &functionAttr, bool noInfsFPMath, bool noNaNsFPMath,
70+
bool approxFuncFPMath, bool noSignedZerosFPMath, bool unsafeFPMath) {
5371
FunctionAttrOptions opts;
5472
// Frame pointer
5573
opts.framePointerKind = functionAttr.framePointerKind;
74+
opts.noInfsFPMath = noInfsFPMath;
75+
opts.noNaNsFPMath = noNaNsFPMath;
76+
opts.approxFuncFPMath = approxFuncFPMath;
77+
opts.noSignedZerosFPMath = noSignedZerosFPMath;
78+
opts.unsafeFPMath = unsafeFPMath;
5679

5780
return std::make_unique<FunctionAttrPass>(opts);
5881
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
! Test that -mframe-pointer can accept only specific values and when given an invalid value, check it raises an error.
2+
3+
! RUN: %flang -O1 -emit-llvm -S -o - %s 2>&1| FileCheck %s --check-prefix=CHECK-NOFASTMATH
4+
! RUN: %flang -Ofast -emit-llvm -S -o - %s 2>&1| FileCheck %s --check-prefix=CHECK-OFAST
5+
! RUN: %flang -O1 -ffast-math -emit-llvm -S -o - %s 2>&1| FileCheck %s --check-prefix=CHECK-FFAST-MATH
6+
7+
subroutine func
8+
end subroutine func
9+
10+
! CHECK-NOFASTMATH-LABEL: define void @func_() local_unnamed_addr
11+
! CHECK-NOFASTMATH-SAME: #[[ATTRS:[0-9]+]]
12+
! CHECK-NOFASTMATH: attributes #[[ATTRS]] = { mustprogress nofree norecurse nosync nounwind willreturn memory(none) }
13+
!
14+
! CHECK-OFAST-LABEL: define void @func_() local_unnamed_addr
15+
! CHECK-OFAST-SAME: #[[ATTRS:[0-9]+]]
16+
! CHECK-OFAST: attributes #[[ATTRS]] = { mustprogress nofree norecurse nosync nounwind willreturn memory(none) "approx-func-fp-math"="true" "no-infs-fp-math"="true" "no-nans-fp-math"="true" "no-signed-zeros-fp-math"="true" "unsafe-fp-math"="true" }
17+
18+
! CHECK-FFAST-MATH-LABEL: define void @func_() local_unnamed_addr
19+
! CHECK-FFAST-MATH-SAME: #[[ATTRS:[0-9]+]]
20+
! CHECK-FFAST-MATH: attributes #[[ATTRS]] = { mustprogress nofree norecurse nosync nounwind willreturn memory(none) "approx-func-fp-math"="true" "no-infs-fp-math"="true" "no-nans-fp-math"="true" "no-signed-zeros-fp-math"="true" "unsafe-fp-math"="true" }

0 commit comments

Comments
 (0)