[PATCH] D81178: [FPEnv] Initialization of C++ globals not strictfp aware

Kevin P. Neal via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Jun 4 11:00:29 PDT 2020


kpn created this revision.
kpn added reviewers: rjmccall, pcc, mibintc.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

The rules of the strictfp attribute say that if it is used inside a function then it must also be in the function definition. Initialization of C++ globals breaks that rule. This patch corrects the failing test but doesn't attempt to correct it on other ABIs.

Error spotted with use of D68233 <https://reviews.llvm.org/D68233>.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D81178

Files:
  clang/lib/CodeGen/CGDeclCXX.cpp
  clang/lib/CodeGen/CodeGenFunction.h
  clang/test/CodeGen/fp-floatcontrol-stack.cpp


Index: clang/test/CodeGen/fp-floatcontrol-stack.cpp
===================================================================
--- clang/test/CodeGen/fp-floatcontrol-stack.cpp
+++ clang/test/CodeGen/fp-floatcontrol-stack.cpp
@@ -251,3 +251,8 @@
 #endif
 };
 OFF off;
+
+//CHECK-DEBSTRICT: define internal void @_GLOBAL__sub_I_fp_floatcontrol_stack.cpp() [[STRICTDEF:#[0-9]+]]
+//CHECK-DEBSTRICT: call void @__cxx_global_var_init() [[STRICTFPONLY:#[0-9]+]]
+//CHECK-DEBSTRICT: attributes [[STRICTDEF]] = { noinline{{.*}}strictfp{{.*}} }
+//CHECK-DEBSTRICT: attributes [[STRICTFPONLY]] = { strictfp }
Index: clang/lib/CodeGen/CodeGenFunction.h
===================================================================
--- clang/lib/CodeGen/CodeGenFunction.h
+++ clang/lib/CodeGen/CodeGenFunction.h
@@ -4344,6 +4344,9 @@
   /// Set the codegen fast-math flags.
   void SetFastMathFlags(FPOptions FPFeatures);
 
+  /// Query for the use of constrained floating point math
+  bool isStrictFP() { return Builder.getIsFPConstrained(); }
+
 private:
   llvm::MDNode *getRangeForLoadFromType(QualType Ty);
   void EmitReturnOfRValue(RValue RV, QualType Ty);
Index: clang/lib/CodeGen/CGDeclCXX.cpp
===================================================================
--- clang/lib/CodeGen/CGDeclCXX.cpp
+++ clang/lib/CodeGen/CGDeclCXX.cpp
@@ -594,7 +594,12 @@
   llvm::Function *Fn = CreateGlobalInitOrDestructFunction(
       FTy, llvm::Twine("_GLOBAL__sub_I_", FileName), FI);
 
-  CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn, CXXGlobalInits);
+  CodeGenFunction CGFn(*this);
+  CGFn.GenerateCXXGlobalInitFunc(Fn, CXXGlobalInits);
+  if (CGFn.isStrictFP())
+    if (!Fn->hasFnAttribute(llvm::Attribute::StrictFP))
+      Fn->addFnAttr(llvm::Attribute::StrictFP);
+
   AddGlobalCtor(Fn);
 
   // In OpenCL global init functions must be converted to kernels in order to


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D81178.268525.patch
Type: text/x-patch
Size: 1854 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200604/03c8a4b1/attachment.bin>


More information about the cfe-commits mailing list