[cfe-commits] r159794 - in /cfe/trunk: include/clang/Basic/LangOptions.def include/clang/Basic/LangOptions.h include/clang/Driver/Options.td lib/CodeGen/BackendUtil.cpp lib/Driver/Tools.cpp lib/Frontend/CompilerInvocation.cpp test/CodeGen/fp-contract.c test/Driver/clang_f_opts.c
Lang Hames
lhames at gmail.com
Thu Jul 5 17:59:19 PDT 2012
Author: lhames
Date: Thu Jul 5 19:59:19 2012
New Revision: 159794
URL: http://llvm.org/viewvc/llvm-project?rev=159794&view=rev
Log:
Add -ffp-contract = { fast | on | off } command line option support.
This flag sets the 'fp-contract' mode, which controls the formation of fused
floating point operations. Available modes are:
- Fast: Form fused operations anywhere.
- On: Form fused operations where allowed by FP_CONTRACT. This is the default
mode.
- Off: Don't form fused operations (in future this may be relaxed to forming
fused operations where it can be proved that the result won't be
affected).
Currently clang doesn't support the FP_CONTRACT pragma, so the 'On' and 'Off'
modes are equivalent.
Added:
cfe/trunk/test/CodeGen/fp-contract.c
Modified:
cfe/trunk/include/clang/Basic/LangOptions.def
cfe/trunk/include/clang/Basic/LangOptions.h
cfe/trunk/include/clang/Driver/Options.td
cfe/trunk/lib/CodeGen/BackendUtil.cpp
cfe/trunk/lib/Driver/Tools.cpp
cfe/trunk/lib/Frontend/CompilerInvocation.cpp
cfe/trunk/test/Driver/clang_f_opts.c
Modified: cfe/trunk/include/clang/Basic/LangOptions.def
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/LangOptions.def?rev=159794&r1=159793&r2=159794&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/LangOptions.def (original)
+++ cfe/trunk/include/clang/Basic/LangOptions.def Thu Jul 5 19:59:19 2012
@@ -148,6 +148,7 @@
"stack protector mode")
ENUM_LANGOPT(SignedOverflowBehavior, SignedOverflowBehaviorTy, 2, SOB_Undefined,
"signed integer overflow handling")
+ENUM_LANGOPT(FPContractMode, FPContractModeTy, 2, FPC_On, "FP_CONTRACT mode")
BENIGN_LANGOPT(InstantiationDepth, 32, 512,
"maximum template instantiation depth")
Modified: cfe/trunk/include/clang/Basic/LangOptions.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/LangOptions.h?rev=159794&r1=159793&r2=159794&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/LangOptions.h (original)
+++ cfe/trunk/include/clang/Basic/LangOptions.h Thu Jul 5 19:59:19 2012
@@ -56,6 +56,12 @@
SOB_Trapping // -ftrapv
};
+ enum FPContractModeKind {
+ FPC_Off, // Form fused FP ops only where result will not be affected.
+ FPC_On, // Form fused FP ops according to FP_CONTRACT rules.
+ FPC_Fast // Aggressively fuse FP ops (E.g. FMA).
+ };
+
public:
clang::ObjCRuntime ObjCRuntime;
Modified: cfe/trunk/include/clang/Driver/Options.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Driver/Options.td?rev=159794&r1=159793&r2=159794&view=diff
==============================================================================
--- cfe/trunk/include/clang/Driver/Options.td (original)
+++ cfe/trunk/include/clang/Driver/Options.td Thu Jul 5 19:59:19 2012
@@ -410,6 +410,9 @@
def fno_honor_infinites : Flag<"-fno-honor-infinites">, Alias<fno_honor_infinities>;
def ftrapping_math : Flag<"-ftrapping-math">, Group<f_Group>;
def fno_trapping_math : Flag<"-fno-trapping-math">, Group<f_Group>;
+def ffp_contract : Joined<"-ffp-contract=">, Group<f_Group>,
+ Flags<[CC1Option]>, HelpText<"Form fused FP ops (e.g. FMAs): fast (everywhere)"
+ " | on (according to FP_CONTRACT pragma, default) | off (never fuse)">;
def ffor_scope : Flag<"-ffor-scope">, Group<f_Group>;
def fno_for_scope : Flag<"-fno-for-scope">, Group<f_Group>;
Modified: cfe/trunk/lib/CodeGen/BackendUtil.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/BackendUtil.cpp?rev=159794&r1=159793&r2=159794&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/BackendUtil.cpp (original)
+++ cfe/trunk/lib/CodeGen/BackendUtil.cpp Thu Jul 5 19:59:19 2012
@@ -351,6 +351,19 @@
Options.FloatABIType = llvm::FloatABI::Default;
}
+ // Set FP fusion mode.
+ switch (LangOpts.getFPContractMode()) {
+ case LangOptions::FPC_Off:
+ Options.AllowFPOpFusion = llvm::FPOpFusion::Strict;
+ break;
+ case LangOptions::FPC_On:
+ Options.AllowFPOpFusion = llvm::FPOpFusion::Standard;
+ break;
+ case LangOptions::FPC_Fast:
+ Options.AllowFPOpFusion = llvm::FPOpFusion::Fast;
+ break;
+ }
+
Options.LessPreciseFPMADOption = CodeGenOpts.LessPreciseFPMAD;
Options.NoInfsFPMath = CodeGenOpts.NoInfsFPMath;
Options.NoNaNsFPMath = CodeGenOpts.NoNaNsFPMath;
Modified: cfe/trunk/lib/Driver/Tools.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Driver/Tools.cpp?rev=159794&r1=159793&r2=159794&view=diff
==============================================================================
--- cfe/trunk/lib/Driver/Tools.cpp (original)
+++ cfe/trunk/lib/Driver/Tools.cpp Thu Jul 5 19:59:19 2012
@@ -1805,6 +1805,24 @@
!TrappingMath)
CmdArgs.push_back("-menable-unsafe-fp-math");
+
+ // Validate and pass through -fp-contract option.
+ if (Arg *A = Args.getLastArg(options::OPT_ffast_math,
+ options::OPT_ffp_contract)) {
+ if (A->getOption().getID() == options::OPT_ffp_contract) {
+ StringRef Val = A->getValue(Args);
+ if (Val == "fast" || Val == "on" || Val == "off") {
+ CmdArgs.push_back(Args.MakeArgString("-ffp-contract=" + Val));
+ } else {
+ D.Diag(diag::err_drv_unsupported_option_argument)
+ << A->getOption().getName() << Val;
+ }
+ } else { // A is OPT_ffast_math
+ // If fast-math is set then set the fp-contract mode to fast.
+ CmdArgs.push_back(Args.MakeArgString("-ffp-contract=fast"));
+ }
+ }
+
// We separately look for the '-ffast-math' flag, and if we find it, tell the
// frontend to provide the appropriate preprocessor macros. This is distinct
// from enabling any optimizations as it induces a language change which must
Modified: cfe/trunk/lib/Frontend/CompilerInvocation.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/CompilerInvocation.cpp?rev=159794&r1=159793&r2=159794&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/CompilerInvocation.cpp (original)
+++ cfe/trunk/lib/Frontend/CompilerInvocation.cpp Thu Jul 5 19:59:19 2012
@@ -759,6 +759,11 @@
Res.push_back("-ftrapv-handler", Opts.OverflowHandler);
break;
}
+ switch (Opts.getFPContractMode()) {
+ case LangOptions::FPC_Off: Res.push_back("-ffp-contract=off"); break;
+ case LangOptions::FPC_On: Res.push_back("-ffp-contract=on"); break;
+ case LangOptions::FPC_Fast: Res.push_back("-ffp-contract=fast"); break;
+ }
if (Opts.HeinousExtensions)
Res.push_back("-fheinous-gnu-extensions");
// Optimize is implicit.
@@ -1975,6 +1980,18 @@
Diags.Report(diag::err_drv_invalid_value)
<< Args.getLastArg(OPT_fvisibility)->getAsString(Args) << Vis;
+ if (Arg *A = Args.getLastArg(OPT_ffp_contract)) {
+ StringRef Val = A->getValue(Args);
+ if (Val == "fast")
+ Opts.setFPContractMode(LangOptions::FPC_Fast);
+ else if (Val == "on")
+ Opts.setFPContractMode(LangOptions::FPC_On);
+ else if (Val == "off")
+ Opts.setFPContractMode(LangOptions::FPC_Off);
+ else
+ Diags.Report(diag::err_drv_invalid_value) << A->getAsString(Args) << Val;
+ }
+
if (Args.hasArg(OPT_fvisibility_inlines_hidden))
Opts.InlineVisibilityHidden = 1;
Added: cfe/trunk/test/CodeGen/fp-contract.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/fp-contract.c?rev=159794&view=auto
==============================================================================
--- cfe/trunk/test/CodeGen/fp-contract.c (added)
+++ cfe/trunk/test/CodeGen/fp-contract.c Thu Jul 5 19:59:19 2012
@@ -0,0 +1,8 @@
+// RUN: %clang_cc1 -O3 -ffp-contract=fast -triple=powerpc-apple-darwin10 -S -o - %s | FileCheck %s
+
+float fma_test1(float a, float b, float c) {
+// CHECK: fmadds
+ float x = a * b;
+ float y = x + c;
+ return y;
+}
Modified: cfe/trunk/test/Driver/clang_f_opts.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Driver/clang_f_opts.c?rev=159794&r1=159793&r2=159794&view=diff
==============================================================================
--- cfe/trunk/test/Driver/clang_f_opts.c (original)
+++ cfe/trunk/test/Driver/clang_f_opts.c Thu Jul 5 19:59:19 2012
@@ -29,3 +29,9 @@
// RUN: %clang -### -c -Wdeprecated %s 2>&1 | FileCheck -check-prefix=DEPRECATED-OFF-CHECK %s
// DEPRECATED-ON-CHECK: -fdeprecated-macro
// DEPRECATED-OFF-CHECK-NOT: -fdeprecated-macro
+
+// RUN: %clang -### -S -ffp-contract=fast %s 2>&1 | FileCheck -check-prefix=FP-CONTRACT-FAST-CHECK %s
+// RUN: %clang -### -S -ffast-math %s 2>&1 | FileCheck -check-prefix=FP-CONTRACT-FAST-CHECK %s
+// RUN: %clang -### -S -ffp-contract=off %s 2>&1 | FileCheck -check-prefix=FP-CONTRACT-OFF-CHECK %s
+// FP-CONTRACT-FAST-CHECK: -ffp-contract=fast
+// FP-CONTRACT-OFF-CHECK: -ffp-contract=off
More information about the cfe-commits
mailing list