[PATCH] D30922: [Builtins] Synchronize the definition of fma/fmaf/fmal in Builtins.def with the implementation in CGBuiltins.cpp

Craig Topper via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Mar 13 16:51:05 PDT 2017

craig.topper created this revision.

The fma libcalls are defined in Builtins.def using the 'e' attribute that says that its only const if -fno-math-errno. It was apparently marked this way because that's what the posix spec says. This determines whether the call gets marked as const or not in SemaDecl.cpp. But in CGBuiltins.cpp we are blindly emitting an intrinsic no matter whether the call is const or not.

This patch changes Builtins.def to match the current CGBuiltins behavior and updates CGBuiltins to check if the function is const. So if anyone ever changes Builtins.def in the future it will do the right thing.



Index: lib/CodeGen/CGBuiltin.cpp
--- lib/CodeGen/CGBuiltin.cpp
+++ lib/CodeGen/CGBuiltin.cpp
@@ -1898,6 +1898,8 @@
   case Builtin::BI__builtin_fmaf:
   case Builtin::BI__builtin_fmal: {
     // Rewrite fma to intrinsic.
+    if (!FD->hasAttr<ConstAttr>())
+      break;
     Value *FirstArg = EmitScalarExpr(E->getArg(0));
     llvm::Type *ArgType = FirstArg->getType();
     Value *F = CGM.getIntrinsic(Intrinsic::fma, ArgType);
Index: include/clang/Basic/Builtins.def
--- include/clang/Basic/Builtins.def
+++ include/clang/Basic/Builtins.def
@@ -1067,9 +1067,10 @@
 LIBBUILTIN(floorf, "ff", "fnc", "math.h", ALL_LANGUAGES)
 LIBBUILTIN(floorl, "LdLd", "fnc", "math.h", ALL_LANGUAGES)
-LIBBUILTIN(fma, "dddd", "fne", "math.h", ALL_LANGUAGES)
-LIBBUILTIN(fmaf, "ffff", "fne", "math.h", ALL_LANGUAGES)
-LIBBUILTIN(fmal, "LdLdLdLd", "fne", "math.h", ALL_LANGUAGES)
+// Posix standard says this updates errno, but we always emit an intrinsic.
+LIBBUILTIN(fma, "dddd", "fnc", "math.h", ALL_LANGUAGES)
+LIBBUILTIN(fmaf, "ffff", "fnc", "math.h", ALL_LANGUAGES)
+LIBBUILTIN(fmal, "LdLdLdLd", "fnc", "math.h", ALL_LANGUAGES)
 LIBBUILTIN(fmax, "ddd", "fnc", "math.h", ALL_LANGUAGES)
 LIBBUILTIN(fmaxf, "fff", "fnc", "math.h", ALL_LANGUAGES)

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D30922.91648.patch
Type: text/x-patch
Size: 1372 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170313/04f2ae66/attachment.bin>

More information about the cfe-commits mailing list