[PATCH] D145765: Add __builtin_set_flt_rounds

xiongji90 via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 9 21:34:11 PST 2023


xiongji90 created this revision.
xiongji90 added reviewers: andrew.w.kaylor, rjmccall, sepavloff, aaron.ballman.
Herald added subscribers: pengfei, kristof.beyls.
Herald added a project: All.
xiongji90 requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

This builtin will be converted to llvm.set.rounding intrinsic in IR level and should be work with "#pragma STDC FENV_ACCESS ON" since it changes default FP environment. Users can change rounding mode via this builtin without introducing libc dependency.
This patch re-lands https://reviews.llvm.org/rG24b823554acd25009731b2519880aa18c7263550 , previous patch fails due to a test case bug. The builtin "__builtin_set_flt_rounds" are restricted to work only on x86, x86_64, arm target but previous test is in builtin.c which will run on other targets such ppc64, so it breaks other targets' build. The new patch moves the test to a separate file and use "-triple x86_64-gnu-linux", "-triple x86_64-windows-msvc", "-triple aarch64-gnu-linux", "-triple aarch64-windows-msvc" to run the lit test


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D145765

Files:
  clang/include/clang/Basic/Builtins.def
  clang/lib/CodeGen/CGBuiltin.cpp
  clang/lib/Sema/SemaChecking.cpp
  clang/test/CodeGen/builtin_set_flt_rounds.c


Index: clang/test/CodeGen/builtin_set_flt_rounds.c
===================================================================
--- /dev/null
+++ clang/test/CodeGen/builtin_set_flt_rounds.c
@@ -0,0 +1,8 @@
+// RUN: %clang_cc1  -triple x86_64-gnu-linux %s -emit-llvm -o - | FileCheck %s
+// RUN: %clang_cc1  -triple x86_64-windows-msvc %s -emit-llvm -o - | FileCheck %s
+// RUN: %clang_cc1  -triple aarch64-gnu-linux %s -emit-llvm -o - | FileCheck %s
+// RUN: %clang_cc1  -triple aarch64-windows-msvc %s -emit-llvm -o - | FileCheck %s
+void test_builtin_set_flt_rounds() {
+  __builtin_set_flt_rounds(1);
+  // CHECK: call void @llvm.set.rounding(i32 1)
+}
Index: clang/lib/Sema/SemaChecking.cpp
===================================================================
--- clang/lib/Sema/SemaChecking.cpp
+++ clang/lib/Sema/SemaChecking.cpp
@@ -2146,6 +2146,14 @@
       return ExprError();
     break;
 
+  case Builtin::BI__builtin_set_flt_rounds:
+    if (CheckBuiltinTargetInSupported(*this, BuiltinID, TheCall,
+                                      {llvm::Triple::x86, llvm::Triple::x86_64,
+                                       llvm::Triple::arm, llvm::Triple::thumb,
+                                       llvm::Triple::aarch64}))
+      return ExprError();
+    break;
+
   case Builtin::BI__builtin_isgreater:
   case Builtin::BI__builtin_isgreaterequal:
   case Builtin::BI__builtin_isless:
Index: clang/lib/CodeGen/CGBuiltin.cpp
===================================================================
--- clang/lib/CodeGen/CGBuiltin.cpp
+++ clang/lib/CodeGen/CGBuiltin.cpp
@@ -3381,6 +3381,15 @@
     return RValue::get(Result);
   }
 
+  case Builtin::BI__builtin_set_flt_rounds: {
+    Function *F = CGM.getIntrinsic(Intrinsic::set_rounding);
+
+    Value *V = EmitScalarExpr(E->getArg(0));
+    Builder.CreateCall(F, V);
+    return RValue::get(nullptr);
+  }
+
+
   case Builtin::BI__builtin_fpclassify: {
     CodeGenFunction::CGFPOptionsRAII FPOptsRAII(*this, E);
     // FIXME: for strictfp/IEEE-754 we need to not trap on SNaN here.
Index: clang/include/clang/Basic/Builtins.def
===================================================================
--- clang/include/clang/Basic/Builtins.def
+++ clang/include/clang/Basic/Builtins.def
@@ -397,6 +397,7 @@
 
 // Access to floating point environment
 BUILTIN(__builtin_flt_rounds, "i", "n")
+BUILTIN(__builtin_set_flt_rounds, "vi", "n")
 
 // C99 complex builtins
 BUILTIN(__builtin_cabs, "dXd", "Fne")


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D145765.504020.patch
Type: text/x-patch
Size: 2453 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230310/f73c3061/attachment.bin>


More information about the cfe-commits mailing list