[clang] [clang] Use the operator's FP environment for promoted operands (PR #246075)

Henrich Lauko via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 25 08:50:02 PST 2026


https://github.com/xlauko created https://github.com/llvm/llvm-project/pull/246075

An operator evaluated in a wider type converts its operands to that type and its result back: `_Float16` and `__bf16` without native support, and complex division with the promoted complex range. The binary operators and unary plus and minus did these conversions outside the scope of their FP options, and a scalar compound assignment promoted its right-hand side before opening its scope, so the conversions took the ambient options. For `z = x * y` on `_Float16`, or `c = a / b` on `_Complex float` with `-complex-range=promoted`, under a file-scope `#pragma STDC FENV_ACCESS ON` that is the default environment of the function:

| | Before | After |
|---|---|---|
| `fpext` | `fpexcept.ignore` | `fpexcept.strict` |
| `fptrunc` | `round.tonearest`, `fpexcept.ignore` | `round.dynamic`, `fpexcept.strict` |

`c /= b` and `f(a / b)` already had the strict metadata, because the compound assignment or the call had its scope open. These operators now open their scope before they promote, and only when they promote, so integer operators do not merge FP function attributes they did not merge before. The new test covers scalar, vector, `__bf16` and complex operators.

Assisted-by: Claude Code (Opus 5.5).


>From 91597e1a3de615a93c2b99b4f43cd2451726e339 Mon Sep 17 00:00:00 2001
From: Henrich Lauko <hlauko at nvidia.com>
Date: Wed, 25 Nov 2026 16:41:56 +0000
Subject: [PATCH] [clang] Use the operator's FP environment for promoted
 operands

An operator evaluated in a wider type converts its operands to that type
and its result back. This happens for `_Float16` and `__bf16` without
native support, and for complex division with the promoted complex
range. The binary operators and unary plus and minus did these
conversions outside the scope of their FP options, so the conversions
took the ambient options. The right-hand side of a scalar compound
assignment was promoted before its scope too. For `z = x * y` on
`_Float16`, or `c = a / b` on `_Complex float` with the promoted range,
under a file-scope `#pragma STDC FENV_ACCESS ON`, that is the default
environment of the function:

  fpext    fpexcept.ignore                   -> fpexcept.strict
  fptrunc  round.tonearest, fpexcept.ignore  -> round.dynamic, fpexcept.strict

`c /= b` and `f(a / b)` already had the strict metadata, because the
scope of the compound assignment or of the call was open. These
operators now open their scope before they promote, when they promote.
An operator that does not promote is unchanged, so integer operators do
not merge FP function attributes they did not merge before.

Assisted-by: Claude Code (Opus 5.5).
---
 clang/lib/CodeGen/CGExprComplex.cpp     | 13 ++++
 clang/lib/CodeGen/CGExprScalar.cpp      | 21 +++++-
 clang/test/CodeGen/promotion-strictfp.c | 98 +++++++++++++++++++++++++
 3 files changed, 129 insertions(+), 3 deletions(-)
 create mode 100644 clang/test/CodeGen/promotion-strictfp.c

diff --git a/clang/lib/CodeGen/CGExprComplex.cpp b/clang/lib/CodeGen/CGExprComplex.cpp
index 59746b7801fa85..2a27af055d8a2b 100644
--- a/clang/lib/CodeGen/CGExprComplex.cpp
+++ b/clang/lib/CodeGen/CGExprComplex.cpp
@@ -20,6 +20,7 @@
 #include "llvm/IR/Instructions.h"
 #include "llvm/IR/MDBuilder.h"
 #include "llvm/IR/Metadata.h"
+#include <optional>
 using namespace clang;
 using namespace CodeGen;
 
@@ -337,12 +338,18 @@ class ComplexExprEmitter
     return QualType();
   }
 
+  // An operation evaluated in a wider type converts its operands to that type
+  // and its result back. The conversions are part of the operation, so they use
+  // its floating-point options.
 #define HANDLEBINOP(OP)                                                        \
   ComplexPairTy VisitBin##OP(const BinaryOperator *E) {                        \
     QualType promotionTy =                                                     \
         getPromotionType(E->getStoredFPFeaturesOrDefault(), E->getType(),      \
                          (E->getOpcode() == BinaryOperatorKind::BO_Div &&      \
                           E->getRHS()->getType()->isAnyComplexType()));        \
+    std::optional<CodeGenFunction::CGFPOptionsRAII> FPOptsRAII;                \
+    if (!promotionTy.isNull())                                                 \
+      FPOptsRAII.emplace(CGF, E);                                              \
     ComplexPairTy result = EmitBin##OP(EmitBinOps(E, promotionTy));            \
     if (!promotionTy.isNull())                                                 \
       result = CGF.EmitUnPromotedValue(result, E->getType());                  \
@@ -663,6 +670,9 @@ ComplexPairTy ComplexExprEmitter::VisitUnaryPlus(const UnaryOperator *E,
                              E->getSubExpr()->getType(),
                              /*IsComplexDivisor=*/false)
           : PromotionType;
+  std::optional<CodeGenFunction::CGFPOptionsRAII> FPOptsRAII;
+  if (!promotionTy.isNull())
+    FPOptsRAII.emplace(CGF, E);
   ComplexPairTy result = VisitPlus(E, promotionTy);
   if (!promotionTy.isNull())
     return CGF.EmitUnPromotedValue(result, E->getSubExpr()->getType());
@@ -686,6 +696,9 @@ ComplexPairTy ComplexExprEmitter::VisitUnaryMinus(const UnaryOperator *E,
                              E->getSubExpr()->getType(),
                              /*IsComplexDivisor=*/false)
           : PromotionType;
+  std::optional<CodeGenFunction::CGFPOptionsRAII> FPOptsRAII;
+  if (!promotionTy.isNull())
+    FPOptsRAII.emplace(CGF, E);
   ComplexPairTy result = VisitMinus(E, promotionTy);
   if (!promotionTy.isNull())
     return CGF.EmitUnPromotedValue(result, E->getSubExpr()->getType());
diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp
index 1378db5715fbf2..951daeba5bd24c 100644
--- a/clang/lib/CodeGen/CGExprScalar.cpp
+++ b/clang/lib/CodeGen/CGExprScalar.cpp
@@ -966,10 +966,16 @@ class ScalarExprEmitter
     return QualType();
   }
 
-  // Binary operators and binary compound assignment operators.
+  // Binary operators and binary compound assignment operators. An operation
+  // evaluated in a wider type converts its operands to that type and its result
+  // back. The conversions are part of the operation, so they use its
+  // floating-point options.
 #define HANDLEBINOP(OP)                                                        \
   Value *VisitBin##OP(const BinaryOperator *E) {                               \
     QualType promotionTy = getPromotionType(E->getType());                     \
+    std::optional<CodeGenFunction::CGFPOptionsRAII> FPOptsRAII;                \
+    if (!promotionTy.isNull())                                                 \
+      FPOptsRAII.emplace(CGF, E);                                              \
     auto result = Emit##OP(EmitBinOps(E, promotionTy));                        \
     if (result && !promotionTy.isNull())                                       \
       result = EmitUnPromotedValue(result, E->getType());                      \
@@ -3684,6 +3690,9 @@ Value *ScalarExprEmitter::VisitUnaryPlus(const UnaryOperator *E,
   QualType promotionTy = PromotionType.isNull()
                              ? getPromotionType(E->getSubExpr()->getType())
                              : PromotionType;
+  std::optional<CodeGenFunction::CGFPOptionsRAII> FPOptsRAII;
+  if (!promotionTy.isNull())
+    FPOptsRAII.emplace(CGF, E);
   Value *result = VisitPlus(E, promotionTy);
   if (result && !promotionTy.isNull())
     result = EmitUnPromotedValue(result, E->getType());
@@ -3704,6 +3713,9 @@ Value *ScalarExprEmitter::VisitUnaryMinus(const UnaryOperator *E,
   QualType promotionTy = PromotionType.isNull()
                              ? getPromotionType(E->getSubExpr()->getType())
                              : PromotionType;
+  std::optional<CodeGenFunction::CGFPOptionsRAII> FPOptsRAII;
+  if (!promotionTy.isNull())
+    FPOptsRAII.emplace(CGF, E);
   Value *result = VisitMinus(E, promotionTy);
   if (result && !promotionTy.isNull())
     result = EmitUnPromotedValue(result, E->getType());
@@ -4093,9 +4105,12 @@ LValue ScalarExprEmitter::EmitCompoundAssignLValue(
       PromotionTypeCR = E->getComputationResultType();
   QualType PromotionTypeLHS = getPromotionType(E->getComputationLHSType());
   QualType PromotionTypeRHS = getPromotionType(E->getRHS()->getType());
-  if (!PromotionTypeRHS.isNull())
+  if (!PromotionTypeRHS.isNull()) {
+    // The promotion is part of the operation, so it uses its floating-point
+    // options.
+    CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, E);
     OpInfo.RHS = CGF.EmitPromotedScalarExpr(E->getRHS(), PromotionTypeRHS);
-  else
+  } else
     OpInfo.RHS = Visit(E->getRHS());
   OpInfo.Ty = PromotionTypeCR;
   OpInfo.Opcode = E->getOpcode();
diff --git a/clang/test/CodeGen/promotion-strictfp.c b/clang/test/CodeGen/promotion-strictfp.c
new file mode 100644
index 00000000000000..92ed5d61d56864
--- /dev/null
+++ b/clang/test/CodeGen/promotion-strictfp.c
@@ -0,0 +1,98 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -complex-range=promoted \
+// RUN:   -ffp-exception-behavior=maytrap -emit-llvm -o - %s \
+// RUN:   | FileCheck --implicit-check-not=fpexcept.maytrap \
+// RUN:               --implicit-check-not=round.tonearest %s
+
+// An operation that is evaluated in a wider type converts its operands to that
+// type and its result back. Test that these conversions pick up the exception
+// and rounding metadata of the operation from the AST instead of the global
+// default from the command line.
+// Any cases of "fpexcept.maytrap" in this test are clang bugs.
+// Any cases of "round.tonearest" in this test are clang bugs.
+
+#pragma float_control(except, on)
+#pragma STDC FENV_ROUND FE_UPWARD
+
+_Complex float a, b, c;
+_Complex _Float16 ha, hb, hc;
+_Float16 x, y, z;
+__bf16 bx, by, bz;
+typedef _Float16 half4 __attribute__((ext_vector_type(4)));
+half4 vx, vy, vz;
+
+// The promoted complex range divides in double.
+
+// CHECK-LABEL: @div(
+// CHECK-COUNT-4: call double @llvm.experimental.constrained.fpext.f64.f32(float %{{.*}}, metadata !"fpexcept.strict")
+// CHECK-COUNT-11: call double @llvm.experimental.constrained.f{{mul|add|sub|div}}.f64(double %{{.*}}, double %{{.*}}, metadata !"round.upward", metadata !"fpexcept.strict")
+// CHECK-COUNT-2: call float @llvm.experimental.constrained.fptrunc.f32.f64(double %{{.*}}, metadata !"round.upward", metadata !"fpexcept.strict")
+void div(void) {
+  c = a / b;
+}
+
+// Without AVX512-FP16, _Float16 arithmetic is done in float. __bf16 arithmetic
+// is done in float too.
+
+// CHECK-LABEL: @complex_mul(
+// CHECK-COUNT-4: call float @llvm.experimental.constrained.fpext.f32.f16(half %{{.*}}, metadata !"fpexcept.strict")
+// CHECK-COUNT-2: call half @llvm.experimental.constrained.fptrunc.f16.f32(float %{{.*}}, metadata !"round.upward", metadata !"fpexcept.strict")
+void complex_mul(void) {
+  hc = ha * hb;
+}
+
+// CHECK-LABEL: @complex_neg(
+// CHECK-COUNT-2: call float @llvm.experimental.constrained.fpext.f32.f16(half %{{.*}}, metadata !"fpexcept.strict")
+// CHECK-COUNT-2: call half @llvm.experimental.constrained.fptrunc.f16.f32(float %{{.*}}, metadata !"round.upward", metadata !"fpexcept.strict")
+void complex_neg(void) {
+  hc = -ha;
+}
+
+// CHECK-LABEL: @complex_plus(
+// CHECK-COUNT-2: call float @llvm.experimental.constrained.fpext.f32.f16(half %{{.*}}, metadata !"fpexcept.strict")
+// CHECK-COUNT-2: call half @llvm.experimental.constrained.fptrunc.f16.f32(float %{{.*}}, metadata !"round.upward", metadata !"fpexcept.strict")
+void complex_plus(void) {
+  hc = +ha;
+}
+
+// CHECK-LABEL: @mul(
+// CHECK-COUNT-2: call float @llvm.experimental.constrained.fpext.f32.f16(half %{{.*}}, metadata !"fpexcept.strict")
+// CHECK: call float @llvm.experimental.constrained.fmul.f32(float %{{.*}}, float %{{.*}}, metadata !"round.upward", metadata !"fpexcept.strict")
+// CHECK: call half @llvm.experimental.constrained.fptrunc.f16.f32(float %{{.*}}, metadata !"round.upward", metadata !"fpexcept.strict")
+void mul(void) {
+  z = x * y;
+}
+
+// CHECK-LABEL: @neg(
+// CHECK: call float @llvm.experimental.constrained.fpext.f32.f16(half %{{.*}}, metadata !"fpexcept.strict")
+// CHECK: call half @llvm.experimental.constrained.fptrunc.f16.f32(float %{{.*}}, metadata !"round.upward", metadata !"fpexcept.strict")
+void neg(void) {
+  z = -x;
+}
+
+// CHECK-LABEL: @plus(
+// CHECK: call float @llvm.experimental.constrained.fpext.f32.f16(half %{{.*}}, metadata !"fpexcept.strict")
+// CHECK: call half @llvm.experimental.constrained.fptrunc.f16.f32(float %{{.*}}, metadata !"round.upward", metadata !"fpexcept.strict")
+void plus(void) {
+  z = +x;
+}
+
+// CHECK-LABEL: @mul_assign(
+// CHECK-COUNT-2: call float @llvm.experimental.constrained.fpext.f32.f16(half %{{.*}}, metadata !"fpexcept.strict")
+// CHECK: call half @llvm.experimental.constrained.fptrunc.f16.f32(float %{{.*}}, metadata !"round.upward", metadata !"fpexcept.strict")
+void mul_assign(void) {
+  z *= y;
+}
+
+// CHECK-LABEL: @bf16_mul(
+// CHECK-COUNT-2: call float @llvm.experimental.constrained.fpext.f32.bf16(bfloat %{{.*}}, metadata !"fpexcept.strict")
+// CHECK: call bfloat @llvm.experimental.constrained.fptrunc.bf16.f32(float %{{.*}}, metadata !"round.upward", metadata !"fpexcept.strict")
+void bf16_mul(void) {
+  bz = bx * by;
+}
+
+// CHECK-LABEL: @vector_mul(
+// CHECK-COUNT-2: call <4 x float> @llvm.experimental.constrained.fpext.v4f32.v4f16(<4 x half> %{{.*}}, metadata !"fpexcept.strict")
+// CHECK: call <4 x half> @llvm.experimental.constrained.fptrunc.v4f16.v4f32(<4 x float> %{{.*}}, metadata !"round.upward", metadata !"fpexcept.strict")
+void vector_mul(void) {
+  vz = vx * vy;
+}



More information about the cfe-commits mailing list