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


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
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to