[clang] 24b8235 - Add __builtin_set_flt_rounds
via cfe-commits
cfe-commits at lists.llvm.org
Wed Mar 8 19:38:50 PST 2023
Author: jinge90
Date: 2023-03-09T11:52:52+08:00
New Revision: 24b823554acd25009731b2519880aa18c7263550
URL: https://github.com/llvm/llvm-project/commit/24b823554acd25009731b2519880aa18c7263550
DIFF: https://github.com/llvm/llvm-project/commit/24b823554acd25009731b2519880aa18c7263550.diff
LOG: Add __builtin_set_flt_rounds
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.
Reviewed by: andrew.w.kaylor, rjmccall, sepavloff
Differential Revision: https://reviews.llvm.org/D144454
Signed-off-by: jinge90 <ge.jin at intel.com>
Added:
Modified:
clang/include/clang/Basic/Builtins.def
clang/lib/CodeGen/CGBuiltin.cpp
clang/lib/Sema/SemaChecking.cpp
clang/test/CodeGen/builtins.c
Removed:
################################################################################
diff --git a/clang/include/clang/Basic/Builtins.def b/clang/include/clang/Basic/Builtins.def
index 478eb59bb7712..e7c483bee7193 100644
--- a/clang/include/clang/Basic/Builtins.def
+++ b/clang/include/clang/Basic/Builtins.def
@@ -397,6 +397,7 @@ BUILTIN(__builtin_truncf16, "hh", "Fnc")
// 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")
diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index cea4727422c0b..a8e937ad66c18 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -3375,6 +3375,14 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
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.
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 485351f157fde..2e7a2e3c57594 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -2146,6 +2146,14 @@ Sema::CheckBuiltinFunctionCall(FunctionDecl *FDecl, unsigned BuiltinID,
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:
diff --git a/clang/test/CodeGen/builtins.c b/clang/test/CodeGen/builtins.c
index 1281c5e13ae3e..d523d3100c04c 100644
--- a/clang/test/CodeGen/builtins.c
+++ b/clang/test/CodeGen/builtins.c
@@ -278,6 +278,8 @@ void test_float_builtins(__fp16 *H, float F, double D, long double LD) {
res = __builtin_flt_rounds();
// CHECK: call i32 @llvm.get.rounding(
+ __builtin_set_flt_rounds(1);
+ // CHECK: call void @llvm.set.rounding(i32 1)
}
// CHECK-LABEL: define{{.*}} void @test_float_builtin_ops
More information about the cfe-commits
mailing list