[clang] [CIR][CodeGen] Keep complex mul and div constrained in strict FP functions (PR #245977)

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


https://github.com/xlauko updated https://github.com/llvm/llvm-project/pull/245977

>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 1/2] [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;
+}

>From a0b0c8bd5cf66c6f3ba3fa852c82eff0c9a56fbd 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 2/2] [CIR] Keep complex mul and div constrained in strict FP
 functions

In a strictfp function ClangIR emitted plain `fmul`, `fsub`, `fadd`,
`fdiv` and `fcmp` for complex multiplication and division, where
classic codegen emits constrained intrinsics. `cir.complex.fmul` and
`cir.complex.fdiv` had no `fenv` attribute, and LoweringPrepare
expanded them with a builder that has no floating-point state.

This moves both operations onto `CIR_FenvOp`, which gives them the
optional `fenv` attribute and the fenv traits of the scalar operations.
CIRGen attaches the attribute, and LoweringPrepare passes it to the
scalar arithmetic, comparisons and conversions of the expansion.

The range check of the improved division becomes a quiet
`cir.fcmp ugt`, the predicate classic codegen uses. A `cir.cmp ge` with
`fenv` lowers to the signaling `fcmps` and would raise an invalid
exception for a quiet NaN divisor. Without `fenv` the check changes
from `fcmp oge` to `fcmp ugt`.

CIRGen gets the change that classic codegen gets in the parent commit:
an operator evaluated in a wider type opens the scope of its FP options
before it promotes its operands. CIRGen's scalar compound assignment
also opens the scope classic codegen opens before it converts the
left-hand side, so `f += d` on `float` and `double` gets the strict
environment too.

The expansions of ClangIR and classic codegen now have the same
constrained intrinsics and metadata. Two older ClangIR gaps remain
around them. Call sites in a strictfp function are not marked
`strictfp`, so the `__mulsc3` and `__divsc3` calls lack the attribute
classic codegen gives them. The conversions of a `_Float16` complex
operand are not constrained.

Assisted-by: Claude Code (Opus 5.5).
---
 .../CIR/Dialect/Builder/CIRBaseBuilder.h      |   6 +-
 clang/include/clang/CIR/Dialect/IR/CIROps.td  |  16 +-
 clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp   |  13 ++
 clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp    |  23 ++-
 .../Dialect/Transforms/LoweringPrepare.cpp    | 167 ++++++++++++------
 .../CIR/CodeGen/complex-mul-div-fenv-access.c |  65 +++++++
 clang/test/CIR/CodeGen/complex-mul-div.cpp    |   8 +-
 clang/test/CIR/CodeGen/complex-strict-fp.cpp  |  47 ++++-
 .../test/CIR/CodeGen/promotion-fenv-access.c  |  64 +++++++
 .../CIR/Transforms/complex-fenv-effects.cir   |  10 +-
 .../lowering-prepare-complex-fenv.cir         |  83 +++++++++
 11 files changed, 434 insertions(+), 68 deletions(-)
 create mode 100644 clang/test/CIR/CodeGen/complex-mul-div-fenv-access.c
 create mode 100644 clang/test/CIR/CodeGen/promotion-fenv-access.c
 create mode 100644 clang/test/CIR/Transforms/lowering-prepare-complex-fenv.cir

diff --git a/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h b/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
index 55b6f2ca24a313..f01dd37cf276e4 100644
--- a/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
+++ b/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
@@ -358,7 +358,8 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
                                cir::ComplexRangeKind rangeKind) {
     auto complexTy = mlir::cast<cir::ComplexType>(lhs.getType());
     if (cir::isAnyFloatingPointType(complexTy.getElementType()))
-      return cir::ComplexFMulOp::create(*this, loc, lhs, rhs, rangeKind);
+      return cir::ComplexFMulOp::create(*this, loc, lhs, rhs, rangeKind,
+                                        getConstrainedFPAttr());
     return cir::ComplexMulOp::create(*this, loc, lhs, rhs);
   }
 
@@ -367,7 +368,8 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
                                cir::ComplexRangeKind rangeKind) {
     auto complexTy = mlir::cast<cir::ComplexType>(lhs.getType());
     if (cir::isAnyFloatingPointType(complexTy.getElementType()))
-      return cir::ComplexFDivOp::create(*this, loc, lhs, rhs, rangeKind);
+      return cir::ComplexFDivOp::create(*this, loc, lhs, rhs, rangeKind,
+                                        getConstrainedFPAttr());
     return cir::ComplexDivOp::create(*this, loc, lhs, rhs);
   }
 
diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td
index a0d7b57b46ab70..640344b7af6ffb 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIROps.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td
@@ -7976,8 +7976,8 @@ def CIR_ComplexRangeKindAttr
     : CIR_EnumAttr<CIR_ComplexRangeKind, "complex_range">;
 
 class CIR_ComplexRangeBinOp<string mnemonic>
-    : CIR_Op<mnemonic, [Pure, SameOperandsAndResultType]> {
-  let arguments = (ins
+    : CIR_FenvOp<mnemonic, [SameOperandsAndResultType]> {
+  let prepend arguments = (ins
     CIR_ComplexOfFloatType:$lhs,
     CIR_ComplexOfFloatType:$rhs,
     CIR_ComplexRangeKindAttr:$range
@@ -7987,7 +7987,7 @@ class CIR_ComplexRangeBinOp<string mnemonic>
 
   let assemblyFormat = [{
     $lhs `,` $rhs `range` `(` enum($range) `)` `:` qualified(type($result))
-    attr-dict
+    (`fenv` `` $fenv^)? attr-dict
   }];
 
   let hasLLVMLowering = false;
@@ -8006,11 +8006,16 @@ def CIR_ComplexFMulOp : CIR_ComplexRangeBinOp<"complex.fmul"> {
     function is called if one of the intermediate calculations produced
     a NaN value.
 
+    The optional `fenv` attribute describes constraints on the floating-point
+    handling of the operation.
+
     Example:
 
     ```
     %2 = cir.complex.fmul %0, %1 range(basic) : !cir.complex<!cir.float>
     %2 = cir.complex.fmul %0, %1 range(full) : !cir.complex<!cir.float>
+    %2 = cir.complex.fmul %0, %1 range(full) : !cir.complex<!cir.float>
+        fenv<except_mode = unknown, strict_except = true>
     ```
   }];
 }
@@ -8033,11 +8038,16 @@ def CIR_ComplexFDivOp : CIR_ComplexRangeBinOp<"complex.fdiv"> {
     no additional handling for the NaN value will be used. For integers types
     `range` attribute will be ignored.
 
+    The optional `fenv` attribute describes constraints on the floating-point
+    handling of the operation.
+
     Example:
 
     ```
     %2 = cir.complex.fdiv %0, %1 range(basic) : !cir.complex<!cir.float>
     %2 = cir.complex.fdiv %0, %1 range(full) : !cir.complex<!cir.float>
+    %2 = cir.complex.fdiv %0, %1 range(full) : !cir.complex<!cir.float>
+        fenv<except_mode = unknown, strict_except = true>
     ```
   }];
 }
diff --git a/clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp b/clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp
index be645e1e931da1..4c81ac31fac159 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp
@@ -3,6 +3,7 @@
 #include "CIRGenFunction.h"
 
 #include "clang/AST/StmtVisitor.h"
+#include <optional>
 
 using namespace clang;
 using namespace clang::CIRGen;
@@ -255,10 +256,16 @@ class ComplexExprEmitter : public StmtVisitor<ComplexExprEmitter, mlir::Value> {
     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)                                                        \
   mlir::Value VisitBin##OP(const BinaryOperator *e) {                          \
     QualType promotionTy = getPromotionType(                                   \
         e->getType(), e->getOpcode() == BinaryOperatorKind::BO_Div);           \
+    std::optional<CIRGenFunction::CIRGenFPOptionsRAII> fpOptsRAII;             \
+    if (!promotionTy.isNull())                                                 \
+      fpOptsRAII.emplace(cgf, e);                                              \
     mlir::Value result = emitBin##OP(emitBinOps(e, promotionTy));              \
     if (!promotionTy.isNull())                                                 \
       result = cgf.emitUnPromotedValue(result, e->getType());                  \
@@ -564,6 +571,9 @@ mlir::Value ComplexExprEmitter::emitCast(CastKind ck, Expr *op,
 
 mlir::Value ComplexExprEmitter::VisitUnaryPlus(const UnaryOperator *e) {
   QualType promotionTy = getPromotionType(e->getSubExpr()->getType());
+  std::optional<CIRGenFunction::CIRGenFPOptionsRAII> fpOptsRAII;
+  if (!promotionTy.isNull())
+    fpOptsRAII.emplace(cgf, e);
   mlir::Value result = VisitUnaryPlus(e, promotionTy);
   if (!promotionTy.isNull())
     return cgf.emitUnPromotedValue(result, e->getSubExpr()->getType());
@@ -579,6 +589,9 @@ mlir::Value ComplexExprEmitter::VisitUnaryPlus(const UnaryOperator *e,
 
 mlir::Value ComplexExprEmitter::VisitUnaryMinus(const UnaryOperator *e) {
   QualType promotionTy = getPromotionType(e->getSubExpr()->getType());
+  std::optional<CIRGenFunction::CIRGenFPOptionsRAII> fpOptsRAII;
+  if (!promotionTy.isNull())
+    fpOptsRAII.emplace(cgf, e);
   mlir::Value result = VisitUnaryMinus(e, promotionTy);
   if (!promotionTy.isNull())
     return cgf.emitUnPromotedValue(result, e->getSubExpr()->getType());
diff --git a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
index c49459c5020fbe..b0089a6cb0a9ec 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
@@ -859,6 +859,9 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
 
   mlir::Value VisitUnaryPlus(const UnaryOperator *e) {
     QualType promotionType = getPromotionType(e->getSubExpr()->getType());
+    std::optional<CIRGenFunction::CIRGenFPOptionsRAII> fpOptsRAII;
+    if (!promotionType.isNull())
+      fpOptsRAII.emplace(cgf, e);
     mlir::Value result = VisitUnaryPlus(e, promotionType);
     if (result && !promotionType.isNull())
       return emitUnPromotedValue(result, e->getType());
@@ -874,6 +877,9 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
 
   mlir::Value VisitUnaryMinus(const UnaryOperator *e) {
     QualType promotionType = getPromotionType(e->getSubExpr()->getType());
+    std::optional<CIRGenFunction::CIRGenFPOptionsRAII> fpOptsRAII;
+    if (!promotionType.isNull())
+      fpOptsRAII.emplace(cgf, e);
     mlir::Value result = VisitUnaryMinus(e, promotionType);
     if (result && !promotionType.isNull())
       return emitUnPromotedValue(result, e->getType());
@@ -1252,10 +1258,16 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
     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)                                                        \
   mlir::Value VisitBin##OP(const BinaryOperator *e) {                          \
     QualType promotionTy = getPromotionType(e->getType());                     \
+    std::optional<CIRGenFunction::CIRGenFPOptionsRAII> fpOptsRAII;             \
+    if (!promotionTy.isNull())                                                 \
+      fpOptsRAII.emplace(cgf, e);                                              \
     auto result = emit##OP(emitBinOps(e, promotionTy));                        \
     if (result && !promotionTy.isNull())                                       \
       result = emitUnPromotedValue(result, e->getType());                      \
@@ -1745,10 +1757,14 @@ LValue ScalarExprEmitter::emitCompoundAssignLValue(
   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.
+    CIRGenFunction::CIRGenFPOptionsRAII fpOptsRAII(cgf, e);
     opInfo.rhs = cgf.emitPromotedScalarExpr(e->getRHS(), promotionTypeRHS);
-  else
+  } else {
     opInfo.rhs = Visit(e->getRHS());
+  }
 
   opInfo.fullType = promotionTypeCR;
   opInfo.compType = opInfo.fullType;
@@ -1769,6 +1785,7 @@ LValue ScalarExprEmitter::emitCompoundAssignLValue(
 
   opInfo.lhs = emitLoadOfLValue(lhsLV, e->getExprLoc());
 
+  CIRGenFunction::CIRGenFPOptionsRAII fpOptsRAII(cgf, opInfo.fpFeatures);
   CIRGenFunction::SourceLocRAIIObject sourceloc{cgf, e->getSourceRange()};
   SourceLocation loc = e->getExprLoc();
   if (!promotionTypeLHS.isNull())
diff --git a/clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp b/clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp
index 753b712fc3c445..1f77ff922e9fd9 100644
--- a/clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp
+++ b/clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp
@@ -822,10 +822,68 @@ getComplexDivLibCallName(llvm::APFloat::Semantics semantics) {
   }
 }
 
+namespace {
+/// Creates the scalar floating-point arithmetic, comparisons and conversions
+/// that a complex multiplication or division expands into. Each of them gets
+/// the `fenv` attribute of the complex operation, so a constrained operation
+/// expands into constrained ones. With a null attribute the operations are
+/// unconstrained.
+class FenvOpBuilder {
+public:
+  FenvOpBuilder(CIRBaseBuilderTy &builder, mlir::Location loc,
+                cir::FenvAttr fenv)
+      : builder(builder), loc(loc), fenv(fenv) {}
+
+  mlir::Value createFAdd(mlir::Value lhs, mlir::Value rhs) const {
+    return createFPBinOp<cir::FAddOp>(lhs, rhs);
+  }
+  mlir::Value createFSub(mlir::Value lhs, mlir::Value rhs) const {
+    return createFPBinOp<cir::FSubOp>(lhs, rhs);
+  }
+  mlir::Value createFMul(mlir::Value lhs, mlir::Value rhs) const {
+    return createFPBinOp<cir::FMulOp>(lhs, rhs);
+  }
+  mlir::Value createFDiv(mlir::Value lhs, mlir::Value rhs) const {
+    return createFPBinOp<cir::FDivOp>(lhs, rhs);
+  }
+
+  /// Creates a quiet comparison, which raises no exception for a quiet NaN
+  /// operand.
+  mlir::Value createFCompare(cir::FCmpOpKind kind, mlir::Value lhs,
+                             mlir::Value rhs) const {
+    return cir::FCmpOp::create(builder, loc, kind, lhs, rhs,
+                               /*signaling=*/false, fenv);
+  }
+
+  mlir::Value createIsNaN(mlir::Value operand) const {
+    return createFCompare(cir::FCmpOpKind::uno, operand, operand);
+  }
+
+  mlir::Value createFloatingCast(mlir::Value src, mlir::Type dstTy) const {
+    return cir::CastOp::create(builder, loc, dstTy, cir::CastKind::floating,
+                               src, fenv);
+  }
+
+private:
+  template <typename OpTy>
+  mlir::Value createFPBinOp(mlir::Value lhs, mlir::Value rhs) const {
+    assert(!cir::MissingFeatures::fastMathFlags());
+    return OpTy::create(builder, loc, lhs, rhs, fenv, cir::FastMathFlagsAttr{});
+  }
+
+  CIRBaseBuilderTy &builder;
+  mlir::Location loc;
+  cir::FenvAttr fenv;
+};
+} // namespace
+
+/// \p fenv is the floating-point environment of a floating-point division. It
+/// is null for an unconstrained one and for an integer division.
 static mlir::Value
 buildAlgebraicComplexDiv(CIRBaseBuilderTy &builder, mlir::Location loc,
                          mlir::Value lhsReal, mlir::Value lhsImag,
-                         mlir::Value rhsReal, mlir::Value rhsImag) {
+                         mlir::Value rhsReal, mlir::Value rhsImag,
+                         cir::FenvAttr fenv) {
   // (a+bi) / (c+di) = ((ac+bd)/(cc+dd)) + ((bc-ad)/(cc+dd))i
   mlir::Value &a = lhsReal;
   mlir::Value &b = lhsImag;
@@ -835,17 +893,18 @@ buildAlgebraicComplexDiv(CIRBaseBuilderTy &builder, mlir::Location loc,
   // The element type of the complex (lhs/rhs) determines whether floating
   // point or integer ops are needed.
   bool isFP = cir::isFPOrVectorOfFPType(a.getType());
+  FenvOpBuilder fp(builder, loc, fenv);
   auto mul = [&](mlir::Location l, mlir::Value x, mlir::Value y) {
-    return isFP ? builder.createFMul(l, x, y) : builder.createMul(l, x, y);
+    return isFP ? fp.createFMul(x, y) : builder.createMul(l, x, y);
   };
   auto add = [&](mlir::Location l, mlir::Value x, mlir::Value y) {
-    return isFP ? builder.createFAdd(l, x, y) : builder.createAdd(l, x, y);
+    return isFP ? fp.createFAdd(x, y) : builder.createAdd(l, x, y);
   };
   auto sub = [&](mlir::Location l, mlir::Value x, mlir::Value y) {
-    return isFP ? builder.createFSub(l, x, y) : builder.createSub(l, x, y);
+    return isFP ? fp.createFSub(x, y) : builder.createSub(l, x, y);
   };
   auto div = [&](mlir::Location l, mlir::Value x, mlir::Value y) {
-    return isFP ? builder.createFDiv(l, x, y) : builder.createDiv(l, x, y);
+    return isFP ? fp.createFDiv(x, y) : builder.createDiv(l, x, y);
   };
 
   mlir::Value ac = mul(loc, a, c);     // a*c
@@ -866,7 +925,8 @@ buildAlgebraicComplexDiv(CIRBaseBuilderTy &builder, mlir::Location loc,
 static mlir::Value
 buildRangeReductionComplexDiv(CIRBaseBuilderTy &builder, mlir::Location loc,
                               mlir::Value lhsReal, mlir::Value lhsImag,
-                              mlir::Value rhsReal, mlir::Value rhsImag) {
+                              mlir::Value rhsReal, mlir::Value rhsImag,
+                              cir::FenvAttr fenv) {
   // Implements Smith's algorithm for complex division.
   // SMITH, R. L. Algorithm 116: Complex division. Commun. ACM 5, 8 (1962).
 
@@ -896,35 +956,37 @@ buildRangeReductionComplexDiv(CIRBaseBuilderTy &builder, mlir::Location loc,
   assert(cir::isFPOrVectorOfFPType(a.getType()) &&
          "range-reduction complex divide expects floating-point operands");
 
+  FenvOpBuilder fp(builder, loc, fenv);
+
   auto trueBranchBuilder = [&](mlir::OpBuilder &, mlir::Location) {
-    mlir::Value r = builder.createFDiv(loc, d, c);    // r := d / c
-    mlir::Value rd = builder.createFMul(loc, r, d);   // r*d
-    mlir::Value tmp = builder.createFAdd(loc, c, rd); // tmp := c + r*d
+    mlir::Value r = fp.createFDiv(d, c);    // r := d / c
+    mlir::Value rd = fp.createFMul(r, d);   // r*d
+    mlir::Value tmp = fp.createFAdd(c, rd); // tmp := c + r*d
 
-    mlir::Value br = builder.createFMul(loc, b, r);   // b*r
-    mlir::Value abr = builder.createFAdd(loc, a, br); // a + b*r
-    mlir::Value e = builder.createFDiv(loc, abr, tmp);
+    mlir::Value br = fp.createFMul(b, r);   // b*r
+    mlir::Value abr = fp.createFAdd(a, br); // a + b*r
+    mlir::Value e = fp.createFDiv(abr, tmp);
 
-    mlir::Value ar = builder.createFMul(loc, a, r);   // a*r
-    mlir::Value bar = builder.createFSub(loc, b, ar); // b - a*r
-    mlir::Value f = builder.createFDiv(loc, bar, tmp);
+    mlir::Value ar = fp.createFMul(a, r);   // a*r
+    mlir::Value bar = fp.createFSub(b, ar); // b - a*r
+    mlir::Value f = fp.createFDiv(bar, tmp);
 
     mlir::Value result = builder.createComplexCreate(loc, e, f);
     builder.createYield(loc, result);
   };
 
   auto falseBranchBuilder = [&](mlir::OpBuilder &, mlir::Location) {
-    mlir::Value r = builder.createFDiv(loc, c, d);    // r := c / d
-    mlir::Value rc = builder.createFMul(loc, r, c);   // r*c
-    mlir::Value tmp = builder.createFAdd(loc, d, rc); // tmp := d + r*c
+    mlir::Value r = fp.createFDiv(c, d);    // r := c / d
+    mlir::Value rc = fp.createFMul(r, c);   // r*c
+    mlir::Value tmp = fp.createFAdd(d, rc); // tmp := d + r*c
 
-    mlir::Value ar = builder.createFMul(loc, a, r);   // a*r
-    mlir::Value arb = builder.createFAdd(loc, ar, b); // a*r + b
-    mlir::Value e = builder.createFDiv(loc, arb, tmp);
+    mlir::Value ar = fp.createFMul(a, r);   // a*r
+    mlir::Value arb = fp.createFAdd(ar, b); // a*r + b
+    mlir::Value e = fp.createFDiv(arb, tmp);
 
-    mlir::Value br = builder.createFMul(loc, b, r);   // b*r
-    mlir::Value bra = builder.createFSub(loc, br, a); // b*r - a
-    mlir::Value f = builder.createFDiv(loc, bra, tmp);
+    mlir::Value br = fp.createFMul(b, r);   // b*r
+    mlir::Value bra = fp.createFSub(br, a); // b*r - a
+    mlir::Value f = fp.createFDiv(bra, tmp);
 
     mlir::Value result = builder.createComplexCreate(loc, e, f);
     builder.createYield(loc, result);
@@ -932,8 +994,10 @@ buildRangeReductionComplexDiv(CIRBaseBuilderTy &builder, mlir::Location loc,
 
   auto cFabs = cir::FAbsOp::create(builder, loc, c);
   auto dFabs = cir::FAbsOp::create(builder, loc, d);
-  cir::CmpOp cmpResult =
-      builder.createCompare(loc, cir::CmpOpKind::ge, cFabs, dFabs);
+  // This is a quiet comparison, so a quiet NaN in the divisor raises no invalid
+  // exception. Like classic codegen, it tests `ugt` and not the `>=` of the
+  // pseudocode above.
+  mlir::Value cmpResult = fp.createFCompare(cir::FCmpOpKind::ugt, cFabs, dFabs);
   auto ternary = cir::TernaryOp::create(builder, loc, cmpResult,
                                         trueBranchBuilder, falseBranchBuilder);
 
@@ -1032,6 +1096,7 @@ static mlir::Value lowerComplexFDiv(LoweringPreparePass &pass,
                                     const clang::TargetInfo &targetInfo) {
   cir::ComplexType complexTy = op.getType();
   cir::ComplexRangeKind range = op.getRange();
+  cir::FenvAttr fenv = op.getFenvAttr();
   switch (range) {
   case ComplexRangeKind::Full: {
     return buildComplexBinOpLibCall(pass, builder, &getComplexDivLibCallName,
@@ -1040,7 +1105,7 @@ static mlir::Value lowerComplexFDiv(LoweringPreparePass &pass,
   }
   case ComplexRangeKind::Improved: {
     return buildRangeReductionComplexDiv(builder, loc, lhsReal, lhsImag,
-                                         rhsReal, rhsImag);
+                                         rhsReal, rhsImag, fenv);
   }
   case ComplexRangeKind::Promoted: {
     mlir::Type originalElementType = complexTy.getElementType();
@@ -1051,33 +1116,29 @@ static mlir::Value lowerComplexFDiv(LoweringPreparePass &pass,
 
     if (!higherPrecisionElementType)
       return buildRangeReductionComplexDiv(builder, loc, lhsReal, lhsImag,
-                                           rhsReal, rhsImag);
-
-    cir::CastKind floatingCastKind = cir::CastKind::floating;
-    lhsReal = builder.createCast(floatingCastKind, lhsReal,
-                                 higherPrecisionElementType);
-    lhsImag = builder.createCast(floatingCastKind, lhsImag,
-                                 higherPrecisionElementType);
-    rhsReal = builder.createCast(floatingCastKind, rhsReal,
-                                 higherPrecisionElementType);
-    rhsImag = builder.createCast(floatingCastKind, rhsImag,
-                                 higherPrecisionElementType);
+                                           rhsReal, rhsImag, fenv);
+
+    FenvOpBuilder fp(builder, loc, fenv);
+    lhsReal = fp.createFloatingCast(lhsReal, higherPrecisionElementType);
+    lhsImag = fp.createFloatingCast(lhsImag, higherPrecisionElementType);
+    rhsReal = fp.createFloatingCast(rhsReal, higherPrecisionElementType);
+    rhsImag = fp.createFloatingCast(rhsImag, higherPrecisionElementType);
 
     mlir::Value algebraicResult = buildAlgebraicComplexDiv(
-        builder, loc, lhsReal, lhsImag, rhsReal, rhsImag);
+        builder, loc, lhsReal, lhsImag, rhsReal, rhsImag, fenv);
 
     mlir::Value resultReal = builder.createComplexReal(loc, algebraicResult);
     mlir::Value resultImag = builder.createComplexImag(loc, algebraicResult);
 
     mlir::Value finalReal =
-        builder.createCast(floatingCastKind, resultReal, originalElementType);
+        fp.createFloatingCast(resultReal, originalElementType);
     mlir::Value finalImag =
-        builder.createCast(floatingCastKind, resultImag, originalElementType);
+        fp.createFloatingCast(resultImag, originalElementType);
     return builder.createComplexCreate(loc, finalReal, finalImag);
   }
   case ComplexRangeKind::Basic: {
     return buildAlgebraicComplexDiv(builder, loc, lhsReal, lhsImag, rhsReal,
-                                    rhsImag);
+                                    rhsImag, fenv);
   }
   }
   llvm_unreachable("fully covered switch");
@@ -1113,7 +1174,7 @@ void LoweringPreparePass::lowerComplexDivOp(cir::ComplexDivOp op) {
   mlir::Value rhsImag = builder.createComplexImag(loc, rhs);
 
   mlir::Value loweredResult = buildAlgebraicComplexDiv(
-      builder, loc, lhsReal, lhsImag, rhsReal, rhsImag);
+      builder, loc, lhsReal, lhsImag, rhsReal, rhsImag, /*fenv=*/{});
   op.replaceAllUsesWith(loweredResult);
   op.erase();
 }
@@ -1143,15 +1204,15 @@ static mlir::Value lowerComplexFMul(LoweringPreparePass &pass,
                                     mlir::Location loc, cir::ComplexFMulOp op,
                                     mlir::Value lhsReal, mlir::Value lhsImag,
                                     mlir::Value rhsReal, mlir::Value rhsImag) {
+  FenvOpBuilder fp(builder, loc, op.getFenvAttr());
+
   // (a+bi) * (c+di) = (ac-bd) + (ad+bc)i
-  mlir::Value resultRealLhs = builder.createFMul(loc, lhsReal, rhsReal); // ac
-  mlir::Value resultRealRhs = builder.createFMul(loc, lhsImag, rhsImag); // bd
-  mlir::Value resultImagLhs = builder.createFMul(loc, lhsReal, rhsImag); // ad
-  mlir::Value resultImagRhs = builder.createFMul(loc, lhsImag, rhsReal); // bc
-  mlir::Value resultReal =
-      builder.createFSub(loc, resultRealLhs, resultRealRhs);
-  mlir::Value resultImag =
-      builder.createFAdd(loc, resultImagLhs, resultImagRhs);
+  mlir::Value resultRealLhs = fp.createFMul(lhsReal, rhsReal); // ac
+  mlir::Value resultRealRhs = fp.createFMul(lhsImag, rhsImag); // bd
+  mlir::Value resultImagLhs = fp.createFMul(lhsReal, rhsImag); // ad
+  mlir::Value resultImagRhs = fp.createFMul(lhsImag, rhsReal); // bc
+  mlir::Value resultReal = fp.createFSub(resultRealLhs, resultRealRhs);
+  mlir::Value resultImag = fp.createFAdd(resultImagLhs, resultImagRhs);
   mlir::Value algebraicResult =
       builder.createComplexCreate(loc, resultReal, resultImag);
 
@@ -1167,8 +1228,8 @@ static mlir::Value lowerComplexFMul(LoweringPreparePass &pass,
   // Check whether the real part and the imaginary part of the result are both
   // NaN. If so, emit a library call to compute the multiplication instead.
   // We check a value against NaN by comparing the value against itself.
-  mlir::Value resultRealIsNaN = builder.createIsNaN(loc, resultReal);
-  mlir::Value resultImagIsNaN = builder.createIsNaN(loc, resultImag);
+  mlir::Value resultRealIsNaN = fp.createIsNaN(resultReal);
+  mlir::Value resultImagIsNaN = fp.createIsNaN(resultImag);
   mlir::Value resultRealAndImagAreNaN =
       builder.createLogicalAnd(loc, resultRealIsNaN, resultImagIsNaN);
 
diff --git a/clang/test/CIR/CodeGen/complex-mul-div-fenv-access.c b/clang/test/CIR/CodeGen/complex-mul-div-fenv-access.c
new file mode 100644
index 00000000000000..367d15ed2870fc
--- /dev/null
+++ b/clang/test/CIR/CodeGen/complex-mul-div-fenv-access.c
@@ -0,0 +1,65 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t-cir.ll
+// RUN: FileCheck --input-file=%t-cir.ll %s --check-prefix=FULL \
+// RUN:   --implicit-check-not="{{= f(add|sub|mul|div|cmp|pext|ptrunc) }}"
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm %s -o %t.ll
+// RUN: FileCheck --input-file=%t.ll %s --check-prefix=FULL
+
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -complex-range=improved -fclangir -emit-llvm %s -o %t-improved-cir.ll
+// RUN: FileCheck --input-file=%t-improved-cir.ll %s --check-prefix=IMPROVED \
+// RUN:   --implicit-check-not="{{= f(add|sub|mul|div|cmp|pext|ptrunc) }}"
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -complex-range=improved -emit-llvm %s -o %t-improved.ll
+// RUN: FileCheck --input-file=%t-improved.ll %s --check-prefix=IMPROVED
+
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -complex-range=promoted -fclangir -emit-llvm %s -o %t-promoted-cir.ll
+// RUN: FileCheck --input-file=%t-promoted-cir.ll %s --check-prefix=PROMOTED \
+// RUN:   --implicit-check-not="{{= f(add|sub|mul|div|cmp|pext|ptrunc) }}"
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -complex-range=promoted -emit-llvm %s -o %t-promoted.ll
+// RUN: FileCheck --input-file=%t-promoted.ll %s --check-prefix=PROMOTED
+
+// Complex multiplication and division under FENV_ACCESS. The arithmetic,
+// comparisons and conversions of the expansion are constrained intrinsics.
+// ClangIR and classic codegen share the check lines, so both emit the same
+// intrinsics with the same metadata. The implicit check rejects an
+// unconstrained instruction in the ClangIR output.
+//
+// The check lines end before the call-site attributes.
+// FIXME: ClangIR does not mark the calls in a strictfp function `strictfp`.
+// FIXME: ClangIR omits `nounwind` on the library calls and `noundef` on their
+// arguments.
+
+#pragma STDC FENV_ACCESS ON
+
+_Complex float a, b, c;
+
+void mul(void) { c = a * b; }
+
+// With the full range, a NaN check of the product guards a call to the runtime
+// library.
+
+// FULL-LABEL: define{{.*}} void @mul()
+// FULL-COUNT-6: call float @llvm.experimental.constrained.f{{mul|sub|add}}.f32(float %{{.*}}, float %{{.*}}, metadata !"round.dynamic", metadata !"fpexcept.strict")
+// FULL-COUNT-2: call i1 @llvm.experimental.constrained.fcmp.f32(float %{{.*}}, float %{{.*}}, metadata !"uno", metadata !"fpexcept.strict")
+// FULL: call <2 x float> @__mulsc3(
+
+void div(void) { c = a / b; }
+
+// With the full range, division is a call to the runtime library.
+
+// FULL-LABEL: define{{.*}} void @div()
+// FULL: call <2 x float> @__divsc3(
+
+// With the improved range, division uses Smith's algorithm. The range check
+// is a quiet comparison.
+
+// IMPROVED-LABEL: define{{.*}} void @div()
+// IMPROVED-COUNT-2: call float @llvm.fabs.f32(float %{{.*}})
+// IMPROVED: call i1 @llvm.experimental.constrained.fcmp.f32(float %{{.*}}, float %{{.*}}, metadata !"ugt", metadata !"fpexcept.strict")
+// IMPROVED-COUNT-18: call float @llvm.experimental.constrained.f{{mul|add|sub|div}}.f32(float %{{.*}}, float %{{.*}}, metadata !"round.dynamic", metadata !"fpexcept.strict")
+
+// With the promoted range, division is done in a wider type. The conversions
+// to and from that type are part of the division and take its environment.
+
+// PROMOTED-LABEL: define{{.*}} void @div()
+// PROMOTED-COUNT-4: call double @llvm.experimental.constrained.fpext.f64.f32(float %{{.*}}, metadata !"fpexcept.strict")
+// PROMOTED-COUNT-11: call double @llvm.experimental.constrained.f{{mul|add|sub|div}}.f64(double %{{.*}}, double %{{.*}}, metadata !"round.dynamic", metadata !"fpexcept.strict")
+// PROMOTED-COUNT-2: call float @llvm.experimental.constrained.fptrunc.f32.f64(double %{{.*}}, metadata !"round.dynamic", metadata !"fpexcept.strict")
diff --git a/clang/test/CIR/CodeGen/complex-mul-div.cpp b/clang/test/CIR/CodeGen/complex-mul-div.cpp
index 63ccbf895ef4fa..83956d9f7563a7 100644
--- a/clang/test/CIR/CodeGen/complex-mul-div.cpp
+++ b/clang/test/CIR/CodeGen/complex-mul-div.cpp
@@ -425,7 +425,7 @@ void foo3() {
 // CIR-AFTER-IMPROVED: %[[B_IMAG:.*]] = cir.complex.imag %[[TMP_B]] : !cir.complex<!cir.float> -> !cir.float
 // CIR-AFTER-IMPROVED: %[[ABS_B_REAL:.*]] = cir.fabs %[[B_REAL]] : !cir.float
 // CIR-AFTER-IMPROVED: %[[ABS_B_IMAG:.*]] = cir.fabs %[[B_IMAG]] : !cir.float
-// CIR-AFTER-IMPROVED: %[[ABS_B_CMP:.*]] = cir.cmp ge %[[ABS_B_REAL]], %[[ABS_B_IMAG]] : !cir.float
+// CIR-AFTER-IMPROVED: %[[ABS_B_CMP:.*]] = cir.fcmp ugt %[[ABS_B_REAL]], %[[ABS_B_IMAG]] : !cir.float
 // CIR-AFTER-IMPROVED: %[[RESULT:.*]] = cir.ternary(%[[ABS_B_CMP]], true {
 // CIR-AFTER-IMPROVED:   %[[DIV_BI_BR:.*]] = cir.fdiv %[[B_IMAG]], %[[B_REAL]] : !cir.float
 // CIR-AFTER-IMPROVED:   %[[MUL_DIV_BIBR_BI:.*]] = cir.fmul %[[DIV_BI_BR]], %[[B_IMAG]] : !cir.float
@@ -464,7 +464,7 @@ void foo3() {
 // LLVM-IMPROVED: %[[B_IMAG:.*]] = extractvalue { float, float } %[[TMP_B]], 1
 // LLVM-IMPROVED: %[[ABS_B_REAL:.*]] = call float @llvm.fabs.f32(float %[[B_REAL]])
 // LLVM-IMPROVED: %[[ABS_B_IMAG:.*]] = call float @llvm.fabs.f32(float %[[B_IMAG]])
-// LLVM-IMPROVED: %[[ABS_B_CMP:.*]] = fcmp oge float %[[ABS_B_REAL]], %[[ABS_B_IMAG]]
+// LLVM-IMPROVED: %[[ABS_B_CMP:.*]] = fcmp ugt float %[[ABS_B_REAL]], %[[ABS_B_IMAG]]
 // LLVM-IMPROVED: br i1 %[[ABS_B_CMP]], label %[[ABS_BR_GT_ABS_BI:.*]], label %[[ABS_BR_LT_ABS_BI:.*]]
 // LLVM-IMPROVED: [[ABS_BR_GT_ABS_BI]]:
 // LLVM-IMPROVED:  %[[DIV_BI_BR:.*]] = fdiv float %[[B_IMAG]], %[[B_REAL]]
@@ -926,7 +926,7 @@ void foo6() {
 // CIR-AFTER-IMPROVED: %[[B_IMAG:.*]] = cir.complex.imag %[[TMP_B]] : !cir.complex<!cir.float> -> !cir.float
 // CIR-AFTER-IMPROVED: %[[ABS_B_REAL:.*]] = cir.fabs %[[B_REAL]] : !cir.float
 // CIR-AFTER-IMPROVED: %[[ABS_B_IMAG:.*]] = cir.fabs %[[B_IMAG]] : !cir.float
-// CIR-AFTER-IMPROVED: %[[ABS_B_CMP:.*]] = cir.cmp ge %[[ABS_B_REAL]], %[[ABS_B_IMAG]] : !cir.float
+// CIR-AFTER-IMPROVED: %[[ABS_B_CMP:.*]] = cir.fcmp ugt %[[ABS_B_REAL]], %[[ABS_B_IMAG]] : !cir.float
 // CIR-AFTER-IMPROVED: %[[RESULT:.*]] = cir.ternary(%[[ABS_B_CMP]], true {
 // CIR-AFTER-IMPROVED:   %[[DIV_BI_BR:.*]] = cir.fdiv %[[B_IMAG]], %[[B_REAL]] : !cir.float
 // CIR-AFTER-IMPROVED:   %[[MUL_DIV_BIBR_BI:.*]] = cir.fmul %[[DIV_BI_BR]], %[[B_IMAG]] : !cir.float
@@ -965,7 +965,7 @@ void foo6() {
 // LLVM-IMPROVED: %[[B_IMAG:.*]] = extractvalue { float, float } %[[TMP_B]], 1
 // LLVM-IMPROVED: %[[ABS_B_REAL:.*]] = call float @llvm.fabs.f32(float %[[B_REAL]])
 // LLVM-IMPROVED: %[[ABS_B_IMAG:.*]] = call float @llvm.fabs.f32(float %[[B_IMAG]])
-// LLVM-IMPROVED: %[[ABS_B_CMP:.*]] = fcmp oge float %[[ABS_B_REAL]], %[[ABS_B_IMAG]]
+// LLVM-IMPROVED: %[[ABS_B_CMP:.*]] = fcmp ugt float %[[ABS_B_REAL]], %[[ABS_B_IMAG]]
 // LLVM-IMPROVED: br i1 %[[ABS_B_CMP]], label %[[ABS_BR_GT_ABS_BI:.*]], label %[[ABS_BR_LT_ABS_BI:.*]]
 // LLVM-IMPROVED: [[ABS_BR_GT_ABS_BI]]:
 // LLVM-IMPROVED:  %[[DIV_BI_BR:.*]] = fdiv float %[[B_IMAG]], %[[B_REAL]]
diff --git a/clang/test/CIR/CodeGen/complex-strict-fp.cpp b/clang/test/CIR/CodeGen/complex-strict-fp.cpp
index 9a7f0a145b28be..a0501aca63b024 100644
--- a/clang/test/CIR/CodeGen/complex-strict-fp.cpp
+++ b/clang/test/CIR/CodeGen/complex-strict-fp.cpp
@@ -1,9 +1,10 @@
 // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -Wno-unused-value -fclangir -ffp-contract=on -fexperimental-strict-floating-point -ffp-exception-behavior=strict -emit-cir %s -o %t-strict.cir
 // RUN: FileCheck --input-file=%t-strict.cir %s -check-prefix=CIR
 // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -Wno-unused-value -fclangir -ffp-contract=on -fexperimental-strict-floating-point -ffp-exception-behavior=strict -emit-llvm %s -o %t-strict.ll
-// RUN: FileCheck --input-file=%t-strict.ll %s -check-prefix=LLVM
+// RUN: FileCheck --input-file=%t-strict.ll %s -check-prefixes=LLVM,SHARED
 // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -Wno-unused-value -ffp-contract=on -fexperimental-strict-floating-point -ffp-exception-behavior=strict -emit-llvm %s -o %t-strict-ogcg.ll
-// RUN: FileCheck --input-file=%t-strict-ogcg.ll %s -check-prefix=OGCG
+// RUN: FileCheck --input-file=%t-strict-ogcg.ll %s -check-prefixes=OGCG,SHARED
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -Wno-unused-value -fclangir -ffp-contract=on -fexperimental-strict-floating-point -ffp-exception-behavior=strict -emit-cir -mmlir --mlir-print-ir-before=cir-canonicalize %s -o %t-strict-before.cir 2>&1 | FileCheck %s -check-prefix=CIR-BEFORE
 
 void complex_add() {
   _Complex float a;
@@ -98,3 +99,45 @@ void complex_sub() {
 // OGCG: %[[C_IMAG_PTR:.*]] = getelementptr inbounds nuw { float, float }, ptr %[[C_ADDR]], i32 0, i32 1
 // OGCG: store float %[[RESULT_REAL]], ptr %[[C_REAL_PTR]], align 4
 // OGCG: store float %[[RESULT_IMAG]], ptr %[[C_IMAG_PTR]], align 4
+
+// With CX_LIMITED_RANGE, multiplication and division expand into scalar
+// arithmetic only. LoweringPrepare does the expansion, so the CIR prefix sees
+// the scalar operations. Each of them carries the attribute of the complex
+// operation. The NaN check, the range check and the runtime library calls of
+// the other ranges are tested in complex-mul-div-fenv-access.c.
+//
+// ClangIR and classic codegen emit the same constrained intrinsics, so the
+// SHARED lines check both. They end before the call-site attributes.
+// FIXME: ClangIR does not mark the calls in a strictfp function `strictfp`.
+
+void complex_mul() {
+#pragma STDC CX_LIMITED_RANGE ON
+  _Complex float a;
+  _Complex float b;
+  _Complex float c = a * b;
+}
+
+// CIR-BEFORE: cir.func {{.*}}@_Z11complex_mulv()
+// CIR-BEFORE: %{{.*}} = cir.complex.fmul %{{.*}}, %{{.*}} range(basic) : !cir.complex<!cir.float> fenv<{{.*}}except_mode = unknown, strict_except = true>
+
+// CIR: cir.func {{.*}}@_Z11complex_mulv()
+// CIR-COUNT-6: = cir.f{{mul|sub|add}} %{{.*}}, %{{.*}} : !cir.float fenv<{{.*}}except_mode = unknown, strict_except = true>
+
+// SHARED: define {{.*}} @_Z11complex_mulv()
+// SHARED-COUNT-6: call float @llvm.experimental.constrained.f{{mul|sub|add}}.f32(float %{{.*}}, float %{{.*}}, metadata !"round.tonearest", metadata !"fpexcept.strict")
+
+void complex_div() {
+#pragma STDC CX_LIMITED_RANGE ON
+  _Complex float a;
+  _Complex float b;
+  _Complex float c = a / b;
+}
+
+// CIR-BEFORE: cir.func {{.*}}@_Z11complex_divv()
+// CIR-BEFORE: %{{.*}} = cir.complex.fdiv %{{.*}}, %{{.*}} range(basic) : !cir.complex<!cir.float> fenv<{{.*}}except_mode = unknown, strict_except = true>
+
+// CIR: cir.func {{.*}}@_Z11complex_divv()
+// CIR-COUNT-11: = cir.f{{mul|add|sub|div}} %{{.*}}, %{{.*}} : !cir.float fenv<{{.*}}except_mode = unknown, strict_except = true>
+
+// SHARED: define {{.*}} @_Z11complex_divv()
+// SHARED-COUNT-11: call float @llvm.experimental.constrained.f{{mul|add|sub|div}}.f32(float %{{.*}}, float %{{.*}}, metadata !"round.tonearest", metadata !"fpexcept.strict")
diff --git a/clang/test/CIR/CodeGen/promotion-fenv-access.c b/clang/test/CIR/CodeGen/promotion-fenv-access.c
new file mode 100644
index 00000000000000..65b7878c591158
--- /dev/null
+++ b/clang/test/CIR/CodeGen/promotion-fenv-access.c
@@ -0,0 +1,64 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t-cir.ll
+// RUN: FileCheck --input-file=%t-cir.ll %s --implicit-check-not=fpexcept.ignore
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm %s -o %t.ll
+// RUN: FileCheck --input-file=%t.ll %s --implicit-check-not=fpexcept.ignore
+
+// An operation that is evaluated in a wider type converts its operands to that
+// type and its result back. The conversions are part of the operation and take
+// its environment, not the default environment of the function. ClangIR and
+// classic codegen share the check lines.
+//
+// Without AVX512-FP16, _Float16 arithmetic is done in float.
+//
+// FIXME: ClangIR does not constrain the conversions of a complex operand and of
+// a complex result.
+
+#pragma STDC FENV_ACCESS ON
+
+_Complex _Float16 a, c;
+_Float16 x, y, z;
+typedef _Float16 half4 __attribute__((ext_vector_type(4)));
+half4 vx, vy, vz;
+float f;
+double d;
+
+void complex_mul(void) { c = a * x; }
+
+// CHECK-LABEL: define{{.*}} void @complex_mul()
+// CHECK: call float @llvm.experimental.constrained.fpext.f32.f16(half %{{.*}}, metadata !"fpexcept.strict")
+// CHECK-COUNT-2: call float @llvm.experimental.constrained.fmul.f32(float %{{.*}}, float %{{.*}}, metadata !"round.dynamic", metadata !"fpexcept.strict")
+
+void mul(void) { z = x * y; }
+
+// CHECK-LABEL: define{{.*}} void @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.dynamic", metadata !"fpexcept.strict")
+// CHECK: call half @llvm.experimental.constrained.fptrunc.f16.f32(float %{{.*}}, metadata !"round.dynamic", metadata !"fpexcept.strict")
+
+void neg(void) { z = -x; }
+
+// CHECK-LABEL: define{{.*}} void @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.dynamic", metadata !"fpexcept.strict")
+
+void mul_assign(void) { z *= y; }
+
+// CHECK-LABEL: define{{.*}} void @mul_assign()
+// 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.dynamic", metadata !"fpexcept.strict")
+// CHECK: call half @llvm.experimental.constrained.fptrunc.f16.f32(float %{{.*}}, metadata !"round.dynamic", metadata !"fpexcept.strict")
+
+void vector_mul(void) { vz = vx * vy; }
+
+// CHECK-LABEL: define{{.*}} void @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.dynamic", metadata !"fpexcept.strict")
+
+// The conversions of a compound assignment are part of it too.
+
+void float_add_assign(void) { f += d; }
+
+// CHECK-LABEL: define{{.*}} void @float_add_assign()
+// CHECK: call double @llvm.experimental.constrained.fpext.f64.f32(float %{{.*}}, metadata !"fpexcept.strict")
+// CHECK: call double @llvm.experimental.constrained.fadd.f64(double %{{.*}}, double %{{.*}}, metadata !"round.dynamic", metadata !"fpexcept.strict")
+// CHECK: call float @llvm.experimental.constrained.fptrunc.f32.f64(double %{{.*}}, metadata !"round.dynamic", metadata !"fpexcept.strict")
diff --git a/clang/test/CIR/Transforms/complex-fenv-effects.cir b/clang/test/CIR/Transforms/complex-fenv-effects.cir
index dae41b35fa5e8c..a17c2a97bf0cf1 100644
--- a/clang/test/CIR/Transforms/complex-fenv-effects.cir
+++ b/clang/test/CIR/Transforms/complex-fenv-effects.cir
@@ -1,6 +1,6 @@
 // RUN: cir-opt %s -canonicalize -o - | FileCheck %s
 
-// Without a `fenv` attribute, complex FP add/sub have no memory effects, so
+// Without a `fenv` attribute, complex FP binary ops have no memory effects, so
 // unused results are erased. A constrained `fenv` gives them Read/Write
 // effects on the floating-point environment, which keeps them alive.
 
@@ -9,18 +9,26 @@
 // CHECK-LABEL: @dead_unconstrained_is_removed
 // CHECK-NOT:     cir.complex.fadd
 // CHECK-NOT:     cir.complex.fsub
+// CHECK-NOT:     cir.complex.fmul
+// CHECK-NOT:     cir.complex.fdiv
 // CHECK:         cir.return
 cir.func @dead_unconstrained_is_removed(%a: !complex_f32, %b: !complex_f32) {
   %0 = cir.complex.fadd %a, %b : !complex_f32
   %1 = cir.complex.fsub %a, %b : !complex_f32
+  %2 = cir.complex.fmul %a, %b range(full) : !complex_f32
+  %3 = cir.complex.fdiv %a, %b range(full) : !complex_f32
   cir.return
 }
 
 // CHECK-LABEL: @dead_constrained_is_not_removed
 // CHECK:         cir.complex.fadd %{{.*}}, %{{.*}} : !cir.complex<!cir.float> fenv<strict_except = true>
 // CHECK:         cir.complex.fsub %{{.*}}, %{{.*}} : !cir.complex<!cir.float> fenv<strict_except = true>
+// CHECK:         cir.complex.fmul %{{.*}}, %{{.*}} range(full) : !cir.complex<!cir.float> fenv<strict_except = true>
+// CHECK:         cir.complex.fdiv %{{.*}}, %{{.*}} range(full) : !cir.complex<!cir.float> fenv<strict_except = true>
 cir.func @dead_constrained_is_not_removed(%a: !complex_f32, %b: !complex_f32) {
   %0 = cir.complex.fadd %a, %b : !complex_f32 fenv<strict_except = true>
   %1 = cir.complex.fsub %a, %b : !complex_f32 fenv<strict_except = true>
+  %2 = cir.complex.fmul %a, %b range(full) : !complex_f32 fenv<strict_except = true>
+  %3 = cir.complex.fdiv %a, %b range(full) : !complex_f32 fenv<strict_except = true>
   cir.return
 }
diff --git a/clang/test/CIR/Transforms/lowering-prepare-complex-fenv.cir b/clang/test/CIR/Transforms/lowering-prepare-complex-fenv.cir
new file mode 100644
index 00000000000000..bb4f12c22e366b
--- /dev/null
+++ b/clang/test/CIR/Transforms/lowering-prepare-complex-fenv.cir
@@ -0,0 +1,83 @@
+// RUN: cir-opt %s --cir-lowering-prepare -o - | FileCheck %s \
+// RUN:   --implicit-check-not="{{cir.(f(add|sub|mul|div|cmp)|cast floating) .*(float|double|f80>)$}}"
+
+// LoweringPrepare expands complex FP multiplication and division into scalar
+// operations. The scalar arithmetic, comparisons and conversions carry the
+// `fenv` attribute of the complex operation they come from. The implicit
+// check rejects one whose line ends at its type, that is, one without the
+// attribute.
+
+!complex_f32 = !cir.complex<!cir.float>
+!complex_f80 = !cir.complex<!cir.long_double<!cir.f80>>
+
+module attributes {cir.triple = "x86_64-unknown-linux-gnu"} {
+
+// CHECK-LABEL: cir.func @fmul_full(
+// CHECK-COUNT-4: = cir.fmul %{{.*}}, %{{.*}} : !cir.float fenv<strict_except = true>
+// CHECK:         = cir.fsub %{{.*}}, %{{.*}} : !cir.float fenv<strict_except = true>
+// CHECK:         = cir.fadd %{{.*}}, %{{.*}} : !cir.float fenv<strict_except = true>
+// CHECK-COUNT-2: = cir.fcmp uno %{{.*}}, %{{.*}} : !cir.float fenv<strict_except = true>
+// CHECK:         cir.call @__mulsc3(
+cir.func @fmul_full(%a: !complex_f32, %b: !complex_f32) -> !complex_f32 {
+  %0 = cir.complex.fmul %a, %b range(full) : !complex_f32 fenv<strict_except = true>
+  cir.return %0 : !complex_f32
+}
+
+// CHECK-LABEL: cir.func @fmul_basic(
+// CHECK-COUNT-6: = cir.f{{mul|sub|add}} %{{.*}}, %{{.*}} : !cir.float fenv<strict_except = true>
+cir.func @fmul_basic(%a: !complex_f32, %b: !complex_f32) -> !complex_f32 {
+  %0 = cir.complex.fmul %a, %b range(basic) : !complex_f32 fenv<strict_except = true>
+  cir.return %0 : !complex_f32
+}
+
+// CHECK-LABEL: cir.func @fdiv_basic(
+// CHECK-COUNT-11: = cir.f{{mul|add|sub|div}} %{{.*}}, %{{.*}} : !cir.float fenv<strict_except = true>
+cir.func @fdiv_basic(%a: !complex_f32, %b: !complex_f32) -> !complex_f32 {
+  %0 = cir.complex.fdiv %a, %b range(basic) : !complex_f32 fenv<strict_except = true>
+  cir.return %0 : !complex_f32
+}
+
+// The range check is a quiet comparison. `cir.fabs` cannot raise an
+// exception, so the expansion gives it no attribute.
+
+// CHECK-LABEL: cir.func @fdiv_improved(
+// CHECK-COUNT-2: = cir.fabs %{{.*}} : !cir.float{{$}}
+// CHECK:         = cir.fcmp ugt %{{.*}}, %{{.*}} : !cir.float fenv<strict_except = true>
+// CHECK-COUNT-18: = cir.f{{mul|add|sub|div}} %{{.*}}, %{{.*}} : !cir.float fenv<strict_except = true>
+cir.func @fdiv_improved(%a: !complex_f32, %b: !complex_f32) -> !complex_f32 {
+  %0 = cir.complex.fdiv %a, %b range(improved) : !complex_f32 fenv<strict_except = true>
+  cir.return %0 : !complex_f32
+}
+
+// CHECK-LABEL: cir.func @fdiv_promoted(
+// CHECK-COUNT-4: = cir.cast floating %{{.*}} : !cir.float -> !cir.double fenv<strict_except = true>
+// CHECK-COUNT-11: = cir.f{{mul|add|sub|div}} %{{.*}}, %{{.*}} : !cir.double fenv<strict_except = true>
+// CHECK-COUNT-2: = cir.cast floating %{{.*}} : !cir.double -> !cir.float fenv<strict_except = true>
+cir.func @fdiv_promoted(%a: !complex_f32, %b: !complex_f32) -> !complex_f32 {
+  %0 = cir.complex.fdiv %a, %b range(promoted) : !complex_f32 fenv<strict_except = true>
+  cir.return %0 : !complex_f32
+}
+
+// x87 long double has no wider type to promote to, so this falls back to
+// range reduction.
+
+// CHECK-LABEL: cir.func @fdiv_promoted_fallback(
+// CHECK:         = cir.fcmp ugt %{{.*}}, %{{.*}} : !cir.long_double<!cir.f80> fenv<strict_except = true>
+// CHECK-COUNT-18: = cir.f{{mul|add|sub|div}} %{{.*}}, %{{.*}} : !cir.long_double<!cir.f80> fenv<strict_except = true>
+cir.func @fdiv_promoted_fallback(%a: !complex_f80, %b: !complex_f80) -> !complex_f80 {
+  %0 = cir.complex.fdiv %a, %b range(promoted) : !complex_f80 fenv<strict_except = true>
+  cir.return %0 : !complex_f80
+}
+
+// Each expansion uses the attribute of its own operation.
+
+// CHECK-LABEL: cir.func @two_environments(
+// CHECK-COUNT-6: = cir.f{{mul|add|sub}} %{{.*}}, %{{.*}} : !cir.float fenv<strict_except = true>
+// CHECK-COUNT-11: = cir.f{{mul|add|sub|div}} %{{.*}}, %{{.*}} : !cir.float fenv<dynamic_rounding_mode = upward>
+cir.func @two_environments(%a: !complex_f32, %b: !complex_f32) -> !complex_f32 {
+  %0 = cir.complex.fmul %a, %b range(basic) : !complex_f32 fenv<strict_except = true>
+  %1 = cir.complex.fdiv %0, %b range(basic) : !complex_f32 fenv<dynamic_rounding_mode = upward>
+  cir.return %1 : !complex_f32
+}
+
+}



More information about the cfe-commits mailing list