[PATCH] D155430: [clang][Interp] Implement __arithmethic_fence for floating types

Timm Bäder via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Jul 17 02:31:26 PDT 2023


tbaeder created this revision.
tbaeder added reviewers: aaron.ballman, erichkeane, shafik, cor3ntin.
Herald added a project: All.
tbaeder requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D155430

Files:
  clang/lib/AST/Interp/InterpBuiltin.cpp
  clang/test/AST/Interp/builtin-functions.cpp


Index: clang/test/AST/Interp/builtin-functions.cpp
===================================================================
--- clang/test/AST/Interp/builtin-functions.cpp
+++ clang/test/AST/Interp/builtin-functions.cpp
@@ -133,3 +133,16 @@
 namespace fabs {
   static_assert(__builtin_fabs(-14.0) == 14.0, "");
 }
+
+namespace ArithmeticFence {
+  constexpr double d = __arithmetic_fence(13.0);
+  static_assert(d == 13.0, "");
+  constexpr float f = __arithmetic_fence(16.0);
+  static_assert(f == 16.0f, "");
+
+  /// TODO: Support complex and vector types in __arithmethic_fence.
+#if 0
+  _Complex double CD2 = {1.0, 2.0};
+  _Complex double CD = __arithmetic_fence(CD);
+#endif
+}
Index: clang/lib/AST/Interp/InterpBuiltin.cpp
===================================================================
--- clang/lib/AST/Interp/InterpBuiltin.cpp
+++ clang/lib/AST/Interp/InterpBuiltin.cpp
@@ -299,6 +299,25 @@
   return true;
 }
 
+/// The argument can be a floating, vector or complex type.
+/// The return type must be the same.
+static bool interp__builtin_arithmetic_fence(InterpState &S, CodePtr OpPC,
+                                             const InterpFrame *Frame,
+                                             const Function *Func) {
+  const Expr *E = S.Current->getExpr(OpPC);
+  const CallExpr *CE = cast<CallExpr>(E);
+  QualType ReturnType = CE->getType();
+
+  // TODO: Support vector and complex types.
+  if (!ReturnType->isFloatingType())
+    return false;
+
+  // We just ignore this.
+  const Floating &Val = S.Stk.peek<Floating>();
+  S.Stk.push<Floating>(Val);
+  return true;
+}
+
 bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const Function *F) {
   InterpFrame *Frame = S.Current;
   APValue Dummy;
@@ -409,6 +428,11 @@
       return Ret<PT_Float>(S, OpPC, Dummy);
     break;
 
+  case Builtin::BI__arithmetic_fence:
+    if (interp__builtin_arithmetic_fence(S, OpPC, Frame, F))
+      return Ret<PT_Float>(S, OpPC, Dummy);
+    break;
+
   default:
     return false;
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D155430.540917.patch
Type: text/x-patch
Size: 2007 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230717/9758d0fa/attachment.bin>


More information about the cfe-commits mailing list