[PATCH] D143241: [Clang] Reset FP options before function instantiations

Serge Pavlov via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Feb 2 22:23:44 PST 2023


sepavloff created this revision.
sepavloff added reviewers: rsmith, rjmccall, aaron.ballman, efriedma.
Herald added a project: All.
sepavloff requested review of this revision.
Herald added a project: clang.

Previously function template instantiations occurred with FP options
that were in effect at the end of translation unit. It was an problem
for late template parsing as these options were used as attributes of
AST nodes and may result in crash. With this change the FP options are
reset to the set defined by command line options before every
function instatntiation.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D143241

Files:
  clang/include/clang/Sema/Sema.h
  clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
  clang/test/CodeGen/fp-template.cpp


Index: clang/test/CodeGen/fp-template.cpp
===================================================================
--- /dev/null
+++ clang/test/CodeGen/fp-template.cpp
@@ -0,0 +1,18 @@
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -fdelayed-template-parsing -emit-llvm -o - %s | FileCheck %s
+
+template <typename Ty>
+Ty func_01(Ty x, Ty y) {
+  return x + y;
+}
+
+float func_02(float x, float y) {
+  return func_01(x, y);
+}
+
+// This pragma sets non-default rounding mode and then delayed parsing occurs.
+// Check that the parsing uses FP options defined by command line options and
+// not by this pragma.
+#pragma STDC FENV_ROUND FE_TOWARDZERO
+
+// CHECK-LABEL: define {{.*}} float @_Z7func_01IfET_S0_S0_
+// CHECK:       %add = fadd float %0, %1
Index: clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
===================================================================
--- clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
+++ clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
@@ -4903,6 +4903,11 @@
                                                      /*Enabled=*/Recursive);
   LocalEagerInstantiationScope LocalInstantiations(*this);
 
+  // Reset FP option to the state specified by command line.
+  FpPragmaStack.clear();
+  FPFeaturesStateRAII SavedFPFeatures(*this);
+  CurFPFeatures = FPOptions(getLangOpts());
+
   // Call the LateTemplateParser callback if there is a need to late parse
   // a templated function definition.
   if (!Pattern && PatternDecl->isLateTemplateParsed() &&
Index: clang/include/clang/Sema/Sema.h
===================================================================
--- clang/include/clang/Sema/Sema.h
+++ clang/include/clang/Sema/Sema.h
@@ -660,6 +660,11 @@
 
     bool hasValue() const { return CurrentValue != DefaultValue; }
 
+    void clear() {
+      Stack.clear();
+      CurrentValue = DefaultValue;
+    }
+
     SmallVector<Slot, 2> Stack;
     ValueType DefaultValue; // Value used for PSK_Reset action.
     ValueType CurrentValue;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D143241.494509.patch
Type: text/x-patch
Size: 1979 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230203/39c801bf/attachment.bin>


More information about the cfe-commits mailing list