Skip to content

Commit 22544e2

Browse files
authored
[flang] Set fast math related function attributes for -Ofast/-ffast-math (#79301)
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 64a317a commit 22544e2

File tree

7 files changed

+104
-8
lines changed

7 files changed

+104
-8
lines changed

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

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

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

9799
// declarative passes
98100
#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
@@ -378,6 +378,21 @@ def FunctionAttr : Pass<"function-attr", "mlir::func::FuncOp"> {
378378
"mlir::LLVM::framePointerKind::FramePointerKind",
379379
/*default=*/"mlir::LLVM::framePointerKind::FramePointerKind{}",
380380
"frame pointer">,
381+
Option<"noInfsFPMath", "no-infs-fp-math",
382+
"bool", /*default=*/"false",
383+
"Set the no-infs-fp-math attribute on functions in the module.">,
384+
Option<"noNaNsFPMath", "no-nans-fp-math",
385+
"bool", /*default=*/"false",
386+
"Set the no-nans-fp-math attribute on functions in the module.">,
387+
Option<"approxFuncFPMath", "approx-func-fp-math",
388+
"bool", /*default=*/"false",
389+
"Set the approx-func-fp-math attribute on functions in the module.">,
390+
Option<"noSignedZerosFPMath", "no-signed-zeros-fp-math",
391+
"bool", /*default=*/"false",
392+
"Set the no-signed-zeros-fp-math attribute on functions in the module.">,
393+
Option<"unsafeFPMath", "unsafe-fp-math",
394+
"bool", /*default=*/"false",
395+
"Set the unsafe-fp-math attribute on functions in the module.">,
381396
];
382397
let constructor = "::fir::createFunctionAttrPass()";
383398
}

flang/include/flang/Tools/CLOptions.inc

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

319-
if (config.FramePointerKind != llvm::FramePointerKind::None) {
319+
if (config.FramePointerKind != llvm::FramePointerKind::None ||
320+
config.NoInfsFPMath || config.NoNaNsFPMath || config.ApproxFuncFPMath ||
321+
config.NoSignedZerosFPMath || config.UnsafeFPMath) {
320322
if (config.FramePointerKind == llvm::FramePointerKind::NonLeaf)
321323
functionAttrs.framePointerKind =
322324
mlir::LLVM::framePointerKind::FramePointerKind::NonLeaf;
323-
else
325+
else if (config.FramePointerKind == llvm::FramePointerKind::All)
324326
functionAttrs.framePointerKind =
325327
mlir::LLVM::framePointerKind::FramePointerKind::All;
328+
else
329+
functionAttrs.framePointerKind =
330+
mlir::LLVM::framePointerKind::FramePointerKind::None;
326331

327-
pm.addPass(fir::createFunctionAttrPass(functionAttrs));
332+
pm.addPass(fir::createFunctionAttrPass(functionAttrs, config.NoInfsFPMath,
333+
config.NoNaNsFPMath, config.ApproxFuncFPMath,
334+
config.NoSignedZerosFPMath, config.UnsafeFPMath));
328335
}
329336

330337
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
@@ -782,6 +782,7 @@ void CodeGenAction::generateLLVMIR() {
782782

783783
CompilerInstance &ci = this->getInstance();
784784
auto opts = ci.getInvocation().getCodeGenOpts();
785+
auto mathOpts = ci.getInvocation().getLoweringOpts().getMathOptions();
785786
llvm::OptimizationLevel level = mapToLevel(opts);
786787

787788
fir::support::loadDialects(*mlirCtx);
@@ -794,7 +795,7 @@ void CodeGenAction::generateLLVMIR() {
794795
pm.addPass(std::make_unique<Fortran::lower::VerifierPass>());
795796
pm.enableVerifier(/*verifyPasses=*/true);
796797

797-
MLIRToLLVMPassPipelineConfig config(level, opts);
798+
MLIRToLLVMPassPipelineConfig config(level, opts, mathOpts);
798799

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

flang/lib/Optimizer/Transforms/FunctionAttr.cpp

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
//===----------------------------------------------------------------------===//
1313
#include "flang/Optimizer/Transforms/Passes.h"
1414
#include "mlir/Dialect/LLVMIR/LLVMAttrs.h"
15+
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
1516

1617
namespace fir {
1718
#define GEN_PASS_DECL_FUNCTIONATTR
@@ -27,6 +28,11 @@ class FunctionAttrPass : public fir::impl::FunctionAttrBase<FunctionAttrPass> {
2728
public:
2829
FunctionAttrPass(const fir::FunctionAttrOptions &options) {
2930
framePointerKind = options.framePointerKind;
31+
noInfsFPMath = options.noInfsFPMath;
32+
noNaNsFPMath = options.noNaNsFPMath;
33+
approxFuncFPMath = options.approxFuncFPMath;
34+
noSignedZerosFPMath = options.noSignedZerosFPMath;
35+
unsafeFPMath = options.unsafeFPMath;
3036
}
3137
FunctionAttrPass() {}
3238
void runOnOperation() override;
@@ -45,14 +51,43 @@ void FunctionAttrPass::runOnOperation() {
4551
func->setAttr("frame_pointer", mlir::LLVM::FramePointerKindAttr::get(
4652
context, framePointerKind));
4753

54+
auto llvmFuncOpName =
55+
mlir::OperationName(mlir::LLVM::LLVMFuncOp::getOperationName(), context);
56+
if (noInfsFPMath)
57+
func->setAttr(
58+
mlir::LLVM::LLVMFuncOp::getNoInfsFpMathAttrName(llvmFuncOpName),
59+
mlir::BoolAttr::get(context, true));
60+
if (noNaNsFPMath)
61+
func->setAttr(
62+
mlir::LLVM::LLVMFuncOp::getNoNansFpMathAttrName(llvmFuncOpName),
63+
mlir::BoolAttr::get(context, true));
64+
if (approxFuncFPMath)
65+
func->setAttr(
66+
mlir::LLVM::LLVMFuncOp::getApproxFuncFpMathAttrName(llvmFuncOpName),
67+
mlir::BoolAttr::get(context, true));
68+
if (noSignedZerosFPMath)
69+
func->setAttr(
70+
mlir::LLVM::LLVMFuncOp::getNoSignedZerosFpMathAttrName(llvmFuncOpName),
71+
mlir::BoolAttr::get(context, true));
72+
if (unsafeFPMath)
73+
func->setAttr(
74+
mlir::LLVM::LLVMFuncOp::getUnsafeFpMathAttrName(llvmFuncOpName),
75+
mlir::BoolAttr::get(context, true));
76+
4877
LLVM_DEBUG(llvm::dbgs() << "=== End " DEBUG_TYPE " ===\n");
4978
}
5079

51-
std::unique_ptr<mlir::Pass>
52-
fir::createFunctionAttrPass(fir::FunctionAttrTypes &functionAttr) {
80+
std::unique_ptr<mlir::Pass> fir::createFunctionAttrPass(
81+
fir::FunctionAttrTypes &functionAttr, bool noInfsFPMath, bool noNaNsFPMath,
82+
bool approxFuncFPMath, bool noSignedZerosFPMath, bool unsafeFPMath) {
5383
FunctionAttrOptions opts;
5484
// Frame pointer
5585
opts.framePointerKind = functionAttr.framePointerKind;
86+
opts.noInfsFPMath = noInfsFPMath;
87+
opts.noNaNsFPMath = noNaNsFPMath;
88+
opts.approxFuncFPMath = approxFuncFPMath;
89+
opts.noSignedZerosFPMath = noSignedZerosFPMath;
90+
opts.unsafeFPMath = unsafeFPMath;
5691

5792
return std::make_unique<FunctionAttrPass>(opts);
5893
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
! RUN: %flang -O1 -emit-llvm -S -o - %s 2>&1| FileCheck %s --check-prefix=CHECK-NOFASTMATH
2+
! RUN: %flang -Ofast -emit-llvm -S -o - %s 2>&1| FileCheck %s --check-prefix=CHECK-OFAST
3+
! RUN: %flang -O1 -ffast-math -emit-llvm -S -o - %s 2>&1| FileCheck %s --check-prefix=CHECK-FFAST-MATH
4+
5+
subroutine func
6+
end subroutine func
7+
8+
! CHECK-NOFASTMATH-LABEL: define void @func_() local_unnamed_addr
9+
! CHECK-NOFASTMATH-SAME: #[[ATTRS:[0-9]+]]
10+
! CHECK-NOT fp-math"=
11+
12+
! CHECK-OFAST-LABEL: define void @func_() local_unnamed_addr
13+
! CHECK-OFAST-SAME: #[[ATTRS:[0-9]+]]
14+
! CHECK-OFAST: attributes #[[ATTRS]] = { {{.*}}"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"{{.*}} }
15+
16+
! CHECK-FFAST-MATH-LABEL: define void @func_() local_unnamed_addr
17+
! CHECK-FFAST-MATH-SAME: #[[ATTRS:[0-9]+]]
18+
! CHECK-FFAST-MATH: attributes #[[ATTRS]] = { {{.*}}"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)