Author: Wenju He Date: 2026-01-04T17:25:22+08:00 New Revision: 1f14ed948d5c5f7d47e37294ad2568ddb90730c0
URL: https://github.com/llvm/llvm-project/commit/1f14ed948d5c5f7d47e37294ad2568ddb90730c0 DIFF: https://github.com/llvm/llvm-project/commit/1f14ed948d5c5f7d47e37294ad2568ddb90730c0.diff LOG: [Clang] Honor '#pragma STDC FENV_ROUND' in __builtin_store_half/halff (#173821) 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. Added: clang/test/CodeGenOpenCL/builtin-store-half-rounding-constrained.cl Modified: clang/lib/CodeGen/CGBuiltin.cpp Removed: ################################################################################ diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp index bd1ef267a7f22..b12f48618335e 100644 --- a/clang/lib/CodeGen/CGBuiltin.cpp +++ b/clang/lib/CodeGen/CGBuiltin.cpp @@ -6200,6 +6200,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..ba3b3165e325a --- /dev/null +++ b/clang/test/CodeGenOpenCL/builtin-store-half-rounding-constrained.cl @@ -0,0 +1,21 @@ +// RUN: %clang_cc1 %s -cl-std=cl3.0 -triple x86_64-unknown-unknown -disable-llvm-passes -emit-llvm -o - | FileCheck %s + +// CHECK-LABEL: @test_store_float( +// CHECK: [[TMP0:%.*]] = 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:%.*]] = 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); +} _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
