[clang] [Clang] Honor '#pragma STDC FENV_ROUND' in __builtin_store_half/halff (PR #173821)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Dec 28 21:21:51 PST 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-codegen
Author: Wenju He (wenju-he)
<details>
<summary>Changes</summary>
Before this change, constrained fptrunc for __builtin_store_half/halff always used round.tonearest, ignoring the active pragma STDC FENV_ROUND.
This PR guards builtin emission with CGFPOptionsRAII so the current rounding mode is propagated to the generated constrained intrinsic.
---
Full diff: https://github.com/llvm/llvm-project/pull/173821.diff
2 Files Affected:
- (modified) clang/lib/CodeGen/CGBuiltin.cpp (+1)
- (added) clang/test/CodeGenOpenCL/builtin-store-half-rounding-constrained.cl (+21)
``````````diff
diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index d3abf6d2a1f2d..0f9bed130a3b0 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -6199,6 +6199,7 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
}
case Builtin::BI__builtin_store_half:
case Builtin::BI__builtin_store_halff: {
+ CodeGenFunction::CGFPOptionsRAII FPOptsRAII(*this, E);
Value *Val = EmitScalarExpr(E->getArg(0));
Address Address = EmitPointerWithAlignment(E->getArg(1));
Value *HalfVal = Builder.CreateFPTrunc(Val, Builder.getHalfTy());
diff --git a/clang/test/CodeGenOpenCL/builtin-store-half-rounding-constrained.cl b/clang/test/CodeGenOpenCL/builtin-store-half-rounding-constrained.cl
new file mode 100644
index 0000000000000..a9334d22fe05f
--- /dev/null
+++ b/clang/test/CodeGenOpenCL/builtin-store-half-rounding-constrained.cl
@@ -0,0 +1,21 @@
+// RUN: %clang_cc1 %s -cl-std=cl3.0 -emit-llvm -o - -triple x86_64-unknown-unknown | FileCheck %s
+
+// CHECK-LABEL: @test_store_float(
+// CHECK: [[TMP0:%.*]] = tail call half @llvm.experimental.constrained.fptrunc.f16.f32(float {{.*}}, metadata !"round.upward", metadata !"fpexcept.ignore")
+// CHECK-NEXT: store half [[TMP0]], ptr {{.*}}, align 2
+// CHECK-NEXT: ret void
+//
+__kernel void test_store_float(float foo, __global half* bar) {
+ #pragma STDC FENV_ROUND FE_UPWARD
+ __builtin_store_halff(foo, bar);
+}
+
+// CHECK-LABEL: @test_store_double(
+// CHECK: [[TMP0:%.*]] = tail call half @llvm.experimental.constrained.fptrunc.f16.f64(double {{.*}}, metadata !"round.downward", metadata !"fpexcept.ignore")
+// CHECK-NEXT: store half [[TMP0]], ptr {{.*}}, align 2
+// CHECK-NEXT: ret void
+//
+__kernel void test_store_double(double foo, __global half* bar) {
+ #pragma STDC FENV_ROUND FE_DOWNWARD
+ __builtin_store_half(foo, bar);
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/173821
More information about the cfe-commits
mailing list