[clang] [CIR] 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 03:52:06 PST 2026
https://github.com/xlauko created https://github.com/llvm/llvm-project/pull/245977
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 gives both operations the optional `fenv` attribute:
- CIRGen attaches it. The operations get the fenv traits of the scalar operations, so an unused constrained one is not erased.
- 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`. A `cir.cmp ge` with `fenv` lowers to the signaling `fcmps`, which raises an invalid exception for a quiet NaN divisor.
The LLVM IR now has the constrained intrinsics and metadata of classic codegen, with one deliberate difference. With the promoted range, the conversions to and from the wider type get the environment of the division, where classic codegen emits them with the default environment of the function.
The tests cover every range kind at the pass level, the `FENV_ACCESS` pragma against classic codegen, and that an unused constrained operation survives canonicalization.
Assisted-by: Claude Code (Fable 5.2).
>From e001dcea7ade3181824d505cbea128d2110774df Mon Sep 17 00:00:00 2001
From: Henrich Lauko <hlauko at nvidia.com>
Date: Wed, 25 Nov 2026 11:25:31 +0000
Subject: [PATCH] [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 gives both operations 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 LLVM IR now has the constrained
intrinsics and metadata of classic codegen, with two differences:
- The range check of the improved division keeps its `oge` predicate,
where classic codegen uses `ugt`. It becomes a quiet `cir.fcmp`,
because a `cir.cmp ge` with `fenv` lowers to the signaling `fcmps`
and would raise an invalid exception for a quiet NaN divisor.
- With the promoted range, the conversions to and from the wider type
get the environment of the division. Classic codegen emits them
outside the scope of the division's FP options, with the default
environment of the function.
Assisted-by: Claude Code (Fable 5.2).
---
.../CIR/Dialect/Builder/CIRBaseBuilder.h | 6 +-
clang/include/clang/CIR/Dialect/IR/CIROps.td | 19 +-
.../Dialect/Transforms/LoweringPrepare.cpp | 168 ++++++++++++------
.../CIR/CodeGen/complex-mul-div-fenv-access.c | 128 +++++++++++++
clang/test/CIR/CodeGen/complex-mul-div.cpp | 4 +-
clang/test/CIR/CodeGen/complex-strict-fp.cpp | 167 +++++++++++++++++
.../complex-mul-div-fenv-effects.cir | 27 +++
.../lowering-prepare-complex-fenv.cir | 94 ++++++++++
8 files changed, 551 insertions(+), 62 deletions(-)
create mode 100644 clang/test/CIR/CodeGen/complex-mul-div-fenv-access.c
create mode 100644 clang/test/CIR/Transforms/complex-mul-div-fenv-effects.cir
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 01e4d9bfb6d202..2f7eb4aa7ed88a 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIROps.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td
@@ -7976,19 +7976,24 @@ def CIR_ComplexRangeKind : CIR_I32Enum<
def CIR_ComplexRangeKindAttr
: CIR_EnumAttr<CIR_ComplexRangeKind, "complex_range">;
+// The optional `fenv` attribute describes constraints on the floating-point
+// handling of the operation. LoweringPrepare gives it to the scalar arithmetic,
+// comparisons and conversions the complex operation expands into.
class CIR_ComplexRangeBinOp<string mnemonic>
- : CIR_Op<mnemonic, [Pure, SameOperandsAndResultType]> {
+ : CIR_Op<mnemonic,
+ !listconcat([SameOperandsAndResultType], CIR_FenvOpTraits)> {
let arguments = (ins
CIR_ComplexOfFloatType:$lhs,
CIR_ComplexOfFloatType:$rhs,
- CIR_ComplexRangeKindAttr:$range
+ CIR_ComplexRangeKindAttr:$range,
+ OptionalAttr<CIR_FenvAttr>:$fenv
);
let results = (outs CIR_ComplexOfFloatType:$result);
let assemblyFormat = [{
$lhs `,` $rhs `range` `(` enum($range) `)` `:` qualified(type($result))
- attr-dict
+ (`fenv` `` $fenv^)? attr-dict
}];
let hasLLVMLowering = false;
@@ -8007,11 +8012,15 @@ 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<>
```
}];
}
@@ -8034,11 +8043,15 @@ 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<>
```
}];
}
diff --git a/clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp b/clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp
index 753b712fc3c445..e41586f046c221 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 fadd(mlir::Value lhs, mlir::Value rhs) const {
+ return binOp<cir::FAddOp>(lhs, rhs);
+ }
+ mlir::Value fsub(mlir::Value lhs, mlir::Value rhs) const {
+ return binOp<cir::FSubOp>(lhs, rhs);
+ }
+ mlir::Value fmul(mlir::Value lhs, mlir::Value rhs) const {
+ return binOp<cir::FMulOp>(lhs, rhs);
+ }
+ mlir::Value fdiv(mlir::Value lhs, mlir::Value rhs) const {
+ return binOp<cir::FDivOp>(lhs, rhs);
+ }
+
+ /// Creates a quiet comparison, which raises no exception for a quiet NaN
+ /// operand.
+ mlir::Value fcmp(cir::FCmpOpKind kind, mlir::Value lhs,
+ mlir::Value rhs) const {
+ return cir::FCmpOp::create(builder, loc, kind, lhs, rhs,
+ /*signaling=*/false, fenv);
+ }
+
+ mlir::Value isNaN(mlir::Value operand) const {
+ return fcmp(cir::FCmpOpKind::uno, operand, operand);
+ }
+
+ mlir::Value floatingCast(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 binOp(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.fmul(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.fadd(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.fsub(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.fdiv(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.fdiv(d, c); // r := d / c
+ mlir::Value rd = fp.fmul(r, d); // r*d
+ mlir::Value tmp = fp.fadd(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.fmul(b, r); // b*r
+ mlir::Value abr = fp.fadd(a, br); // a + b*r
+ mlir::Value e = fp.fdiv(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.fmul(a, r); // a*r
+ mlir::Value bar = fp.fsub(b, ar); // b - a*r
+ mlir::Value f = fp.fdiv(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.fdiv(c, d); // r := c / d
+ mlir::Value rc = fp.fmul(r, c); // r*c
+ mlir::Value tmp = fp.fadd(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.fmul(a, r); // a*r
+ mlir::Value arb = fp.fadd(ar, b); // a*r + b
+ mlir::Value e = fp.fdiv(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.fmul(b, r); // b*r
+ mlir::Value bra = fp.fsub(br, a); // b*r - a
+ mlir::Value f = fp.fdiv(bra, tmp);
mlir::Value result = builder.createComplexCreate(loc, e, f);
builder.createYield(loc, result);
@@ -932,8 +994,9 @@ 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);
+ // A quiet NaN in the divisor must not raise an invalid exception here, so
+ // this is a quiet comparison and not the signaling `cir.cmp ge`.
+ mlir::Value cmpResult = fp.fcmp(cir::FCmpOpKind::oge, cFabs, dFabs);
auto ternary = cir::TernaryOp::create(builder, loc, cmpResult,
trueBranchBuilder, falseBranchBuilder);
@@ -1032,6 +1095,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 +1104,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 +1115,27 @@ 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.floatingCast(lhsReal, higherPrecisionElementType);
+ lhsImag = fp.floatingCast(lhsImag, higherPrecisionElementType);
+ rhsReal = fp.floatingCast(rhsReal, higherPrecisionElementType);
+ rhsImag = fp.floatingCast(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);
- mlir::Value finalImag =
- builder.createCast(floatingCastKind, resultImag, originalElementType);
+ mlir::Value finalReal = fp.floatingCast(resultReal, originalElementType);
+ mlir::Value finalImag = fp.floatingCast(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 +1171,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 +1201,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.fmul(lhsReal, rhsReal); // ac
+ mlir::Value resultRealRhs = fp.fmul(lhsImag, rhsImag); // bd
+ mlir::Value resultImagLhs = fp.fmul(lhsReal, rhsImag); // ad
+ mlir::Value resultImagRhs = fp.fmul(lhsImag, rhsReal); // bc
+ mlir::Value resultReal = fp.fsub(resultRealLhs, resultRealRhs);
+ mlir::Value resultImag = fp.fadd(resultImagLhs, resultImagRhs);
mlir::Value algebraicResult =
builder.createComplexCreate(loc, resultReal, resultImag);
@@ -1167,8 +1225,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.isNaN(resultReal);
+ mlir::Value resultImagIsNaN = fp.isNaN(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..96fb75ca4cf376
--- /dev/null
+++ b/clang/test/CIR/CodeGen/complex-mul-div-fenv-access.c
@@ -0,0 +1,128 @@
+// 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-prefixes=FULL,LLVM-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-prefixes=FULL,OGCG-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-prefixes=IMPROVED,LLVM-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-prefixes=IMPROVED,OGCG-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=LLVM-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=OGCG-PROMOTED
+
+// Complex multiplication and division under FENV_ACCESS. The arithmetic,
+// comparisons and conversions of the expansion are constrained intrinsics.
+// The FULL and IMPROVED lines are shared by ClangIR and classic codegen, so
+// both emit the same intrinsics with the same metadata.
+
+#pragma STDC FENV_ACCESS ON
+
+_Complex float a, b, c;
+
+void mul(void) { c = a * b; }
+
+// FULL-LABEL: define{{.*}} void @mul()
+
+// LLVM-FULL: %[[A:.*]] = load { float, float }, ptr @a
+// LLVM-FULL: %[[B:.*]] = load { float, float }, ptr @b
+// LLVM-FULL: %[[A_REAL:.*]] = extractvalue { float, float } %[[A]], 0
+// LLVM-FULL: %[[A_IMAG:.*]] = extractvalue { float, float } %[[A]], 1
+// LLVM-FULL: %[[B_REAL:.*]] = extractvalue { float, float } %[[B]], 0
+// LLVM-FULL: %[[B_IMAG:.*]] = extractvalue { float, float } %[[B]], 1
+
+// OGCG-FULL: %[[A_REAL:.*]] = load float, ptr @a
+// OGCG-FULL: %[[A_IMAG:.*]] = load float, ptr getelementptr inbounds nuw (i8, ptr @a, i64 4)
+// OGCG-FULL: %[[B_REAL:.*]] = load float, ptr @b
+// OGCG-FULL: %[[B_IMAG:.*]] = load float, ptr getelementptr inbounds nuw (i8, ptr @b, i64 4)
+
+// FULL: %[[AC:.*]] = call float @llvm.experimental.constrained.fmul.f32(float %[[A_REAL]], float %[[B_REAL]], metadata !"round.dynamic", metadata !"fpexcept.strict")
+// FULL: %[[BD:.*]] = call float @llvm.experimental.constrained.fmul.f32(float %[[A_IMAG]], float %[[B_IMAG]], metadata !"round.dynamic", metadata !"fpexcept.strict")
+// FULL: %[[AD:.*]] = call float @llvm.experimental.constrained.fmul.f32(float %[[A_REAL]], float %[[B_IMAG]], metadata !"round.dynamic", metadata !"fpexcept.strict")
+// FULL: %[[BC:.*]] = call float @llvm.experimental.constrained.fmul.f32(float %[[A_IMAG]], float %[[B_REAL]], metadata !"round.dynamic", metadata !"fpexcept.strict")
+// FULL: %[[REAL:.*]] = call float @llvm.experimental.constrained.fsub.f32(float %[[AC]], float %[[BD]], metadata !"round.dynamic", metadata !"fpexcept.strict")
+// FULL: %[[IMAG:.*]] = call float @llvm.experimental.constrained.fadd.f32(float %[[AD]], float %[[BC]], metadata !"round.dynamic", metadata !"fpexcept.strict")
+// FULL: call i1 @llvm.experimental.constrained.fcmp.f32(float %[[REAL]], float %[[REAL]], metadata !"uno", metadata !"fpexcept.strict")
+// FULL: call i1 @llvm.experimental.constrained.fcmp.f32(float %[[IMAG]], float %[[IMAG]], metadata !"uno", metadata !"fpexcept.strict")
+// FULL: call {{.*}}<2 x float> @__mulsc3(float {{.*}}%[[A_REAL]], float {{.*}}%[[A_IMAG]], float {{.*}}%[[B_REAL]], float {{.*}}%[[B_IMAG]])
+
+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 in both codegens. Its predicate differs.
+
+// IMPROVED-LABEL: define{{.*}} void @div()
+
+// LLVM-IMPROVED: %[[A:.*]] = load { float, float }, ptr @a
+// LLVM-IMPROVED: %[[B:.*]] = load { float, float }, ptr @b
+// LLVM-IMPROVED: %[[A_REAL:.*]] = extractvalue { float, float } %[[A]], 0
+// LLVM-IMPROVED: %[[A_IMAG:.*]] = extractvalue { float, float } %[[A]], 1
+// LLVM-IMPROVED: %[[B_REAL:.*]] = extractvalue { float, float } %[[B]], 0
+// LLVM-IMPROVED: %[[B_IMAG:.*]] = extractvalue { float, float } %[[B]], 1
+
+// OGCG-IMPROVED: %[[A_REAL:.*]] = load float, ptr @a
+// OGCG-IMPROVED: %[[A_IMAG:.*]] = load float, ptr getelementptr inbounds nuw (i8, ptr @a, i64 4)
+// OGCG-IMPROVED: %[[B_REAL:.*]] = load float, ptr @b
+// OGCG-IMPROVED: %[[B_IMAG:.*]] = load float, ptr getelementptr inbounds nuw (i8, ptr @b, i64 4)
+
+// IMPROVED: %[[ABS_C:.*]] = call float @llvm.fabs.f32(float %[[B_REAL]])
+// IMPROVED: %[[ABS_D:.*]] = call float @llvm.fabs.f32(float %[[B_IMAG]])
+// LLVM-IMPROVED: call i1 @llvm.experimental.constrained.fcmp.f32(float %[[ABS_C]], float %[[ABS_D]], metadata !"oge", metadata !"fpexcept.strict")
+// OGCG-IMPROVED: call i1 @llvm.experimental.constrained.fcmp.f32(float %[[ABS_C]], float %[[ABS_D]], metadata !"ugt", metadata !"fpexcept.strict")
+
+// IMPROVED: %[[R1:.*]] = call float @llvm.experimental.constrained.fdiv.f32(float %[[B_IMAG]], float %[[B_REAL]], metadata !"round.dynamic", metadata !"fpexcept.strict")
+// IMPROVED: %[[RD:.*]] = call float @llvm.experimental.constrained.fmul.f32(float %[[R1]], float %[[B_IMAG]], metadata !"round.dynamic", metadata !"fpexcept.strict")
+// IMPROVED: %[[TMP1:.*]] = call float @llvm.experimental.constrained.fadd.f32(float %[[B_REAL]], float %[[RD]], metadata !"round.dynamic", metadata !"fpexcept.strict")
+// IMPROVED: %[[BR1:.*]] = call float @llvm.experimental.constrained.fmul.f32(float %[[A_IMAG]], float %[[R1]], metadata !"round.dynamic", metadata !"fpexcept.strict")
+// IMPROVED: %[[ABR:.*]] = call float @llvm.experimental.constrained.fadd.f32(float %[[A_REAL]], float %[[BR1]], metadata !"round.dynamic", metadata !"fpexcept.strict")
+// IMPROVED: call float @llvm.experimental.constrained.fdiv.f32(float %[[ABR]], float %[[TMP1]], metadata !"round.dynamic", metadata !"fpexcept.strict")
+// IMPROVED: %[[AR1:.*]] = call float @llvm.experimental.constrained.fmul.f32(float %[[A_REAL]], float %[[R1]], metadata !"round.dynamic", metadata !"fpexcept.strict")
+// IMPROVED: %[[BAR:.*]] = call float @llvm.experimental.constrained.fsub.f32(float %[[A_IMAG]], float %[[AR1]], metadata !"round.dynamic", metadata !"fpexcept.strict")
+// IMPROVED: call float @llvm.experimental.constrained.fdiv.f32(float %[[BAR]], float %[[TMP1]], metadata !"round.dynamic", metadata !"fpexcept.strict")
+
+// IMPROVED: %[[R2:.*]] = call float @llvm.experimental.constrained.fdiv.f32(float %[[B_REAL]], float %[[B_IMAG]], metadata !"round.dynamic", metadata !"fpexcept.strict")
+// IMPROVED: %[[RC:.*]] = call float @llvm.experimental.constrained.fmul.f32(float %[[R2]], float %[[B_REAL]], metadata !"round.dynamic", metadata !"fpexcept.strict")
+// IMPROVED: %[[TMP2:.*]] = call float @llvm.experimental.constrained.fadd.f32(float %[[B_IMAG]], float %[[RC]], metadata !"round.dynamic", metadata !"fpexcept.strict")
+// IMPROVED: %[[AR2:.*]] = call float @llvm.experimental.constrained.fmul.f32(float %[[A_REAL]], float %[[R2]], metadata !"round.dynamic", metadata !"fpexcept.strict")
+// IMPROVED: %[[ARB:.*]] = call float @llvm.experimental.constrained.fadd.f32(float %[[AR2]], float %[[A_IMAG]], metadata !"round.dynamic", metadata !"fpexcept.strict")
+// IMPROVED: call float @llvm.experimental.constrained.fdiv.f32(float %[[ARB]], float %[[TMP2]], metadata !"round.dynamic", metadata !"fpexcept.strict")
+// IMPROVED: %[[BR2:.*]] = call float @llvm.experimental.constrained.fmul.f32(float %[[A_IMAG]], float %[[R2]], metadata !"round.dynamic", metadata !"fpexcept.strict")
+// IMPROVED: %[[BRA:.*]] = call float @llvm.experimental.constrained.fsub.f32(float %[[BR2]], float %[[A_REAL]], metadata !"round.dynamic", metadata !"fpexcept.strict")
+// IMPROVED: call float @llvm.experimental.constrained.fdiv.f32(float %[[BRA]], float %[[TMP2]], metadata !"round.dynamic", metadata !"fpexcept.strict")
+
+// With the promoted range, division is done in a wider type. Classic codegen
+// converts the operands and the result outside the scope of the division's
+// FP options, so its conversions get the default environment of the
+// function. ClangIR converts as part of the division and gives the
+// conversions its environment, which assumes less.
+
+// LLVM-PROMOTED-LABEL: define{{.*}} void @div()
+// LLVM-PROMOTED-COUNT-4: call double @llvm.experimental.constrained.fpext.f64.f32(float %{{.*}}, metadata !"fpexcept.strict")
+// LLVM-PROMOTED-COUNT-4: call double @llvm.experimental.constrained.fmul.f64(double %{{.*}}, double %{{.*}}, metadata !"round.dynamic", metadata !"fpexcept.strict")
+// LLVM-PROMOTED-COUNT-2: call double @llvm.experimental.constrained.fadd.f64(double %{{.*}}, double %{{.*}}, metadata !"round.dynamic", metadata !"fpexcept.strict")
+// LLVM-PROMOTED: call double @llvm.experimental.constrained.fdiv.f64(double %{{.*}}, double %{{.*}}, metadata !"round.dynamic", metadata !"fpexcept.strict")
+// LLVM-PROMOTED-COUNT-2: call double @llvm.experimental.constrained.fmul.f64(double %{{.*}}, double %{{.*}}, metadata !"round.dynamic", metadata !"fpexcept.strict")
+// LLVM-PROMOTED: call double @llvm.experimental.constrained.fsub.f64(double %{{.*}}, double %{{.*}}, metadata !"round.dynamic", metadata !"fpexcept.strict")
+// LLVM-PROMOTED: call double @llvm.experimental.constrained.fdiv.f64(double %{{.*}}, double %{{.*}}, metadata !"round.dynamic", metadata !"fpexcept.strict")
+// LLVM-PROMOTED-COUNT-2: call float @llvm.experimental.constrained.fptrunc.f32.f64(double %{{.*}}, metadata !"round.dynamic", metadata !"fpexcept.strict")
+
+// OGCG-PROMOTED-LABEL: define{{.*}} void @div()
+// OGCG-PROMOTED-COUNT-4: call double @llvm.experimental.constrained.fpext.f64.f32(float %{{.*}}, metadata !"fpexcept.ignore")
+// OGCG-PROMOTED-COUNT-2: call double @llvm.experimental.constrained.fmul.f64(double %{{.*}}, double %{{.*}}, metadata !"round.dynamic", metadata !"fpexcept.strict")
+// OGCG-PROMOTED: call double @llvm.experimental.constrained.fadd.f64(double %{{.*}}, double %{{.*}}, metadata !"round.dynamic", metadata !"fpexcept.strict")
+// OGCG-PROMOTED-COUNT-2: call double @llvm.experimental.constrained.fmul.f64(double %{{.*}}, double %{{.*}}, metadata !"round.dynamic", metadata !"fpexcept.strict")
+// OGCG-PROMOTED: call double @llvm.experimental.constrained.fadd.f64(double %{{.*}}, double %{{.*}}, metadata !"round.dynamic", metadata !"fpexcept.strict")
+// OGCG-PROMOTED-COUNT-2: call double @llvm.experimental.constrained.fmul.f64(double %{{.*}}, double %{{.*}}, metadata !"round.dynamic", metadata !"fpexcept.strict")
+// OGCG-PROMOTED: call double @llvm.experimental.constrained.fsub.f64(double %{{.*}}, double %{{.*}}, metadata !"round.dynamic", metadata !"fpexcept.strict")
+// OGCG-PROMOTED-COUNT-2: call double @llvm.experimental.constrained.fdiv.f64(double %{{.*}}, double %{{.*}}, metadata !"round.dynamic", metadata !"fpexcept.strict")
+// OGCG-PROMOTED-COUNT-2: call float @llvm.experimental.constrained.fptrunc.f32.f64(double %{{.*}}, metadata !"round.tonearest", metadata !"fpexcept.ignore")
diff --git a/clang/test/CIR/CodeGen/complex-mul-div.cpp b/clang/test/CIR/CodeGen/complex-mul-div.cpp
index 63ccbf895ef4fa..25056f766235fc 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 oge %[[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
@@ -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 oge %[[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
diff --git a/clang/test/CIR/CodeGen/complex-strict-fp.cpp b/clang/test/CIR/CodeGen/complex-strict-fp.cpp
index 9a7f0a145b28be..864b677336a47a 100644
--- a/clang/test/CIR/CodeGen/complex-strict-fp.cpp
+++ b/clang/test/CIR/CodeGen/complex-strict-fp.cpp
@@ -4,6 +4,7 @@
// RUN: FileCheck --input-file=%t-strict.ll %s -check-prefix=LLVM
// 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: %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,169 @@ 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. The NaN check, the range check and the runtime library
+// calls of the other ranges are tested in complex-mul-div-fenv-access.c.
+//
+// LoweringPrepare does the expansion, so the CIR prefix sees the scalar
+// operations. Each of them carries the attribute of the complex operation.
+
+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: %[[A_ADDR:.*]] = cir.alloca "a" {{.*}} : !cir.ptr<!cir.complex<!cir.float>>
+// CIR: %[[B_ADDR:.*]] = cir.alloca "b" {{.*}} : !cir.ptr<!cir.complex<!cir.float>>
+// CIR: %[[C_ADDR:.*]] = cir.alloca "c" {{.*}} init : !cir.ptr<!cir.complex<!cir.float>>
+// CIR: %[[TMP_A:.*]] = cir.load {{.*}} %[[A_ADDR]] : !cir.ptr<!cir.complex<!cir.float>>, !cir.complex<!cir.float>
+// CIR: %[[TMP_B:.*]] = cir.load {{.*}} %[[B_ADDR]] : !cir.ptr<!cir.complex<!cir.float>>, !cir.complex<!cir.float>
+// CIR: %[[A_REAL:.*]] = cir.complex.real %[[TMP_A]] : !cir.complex<!cir.float> -> !cir.float
+// CIR: %[[A_IMAG:.*]] = cir.complex.imag %[[TMP_A]] : !cir.complex<!cir.float> -> !cir.float
+// CIR: %[[B_REAL:.*]] = cir.complex.real %[[TMP_B]] : !cir.complex<!cir.float> -> !cir.float
+// CIR: %[[B_IMAG:.*]] = cir.complex.imag %[[TMP_B]] : !cir.complex<!cir.float> -> !cir.float
+// CIR: %[[MUL_AC:.*]] = cir.fmul %[[A_REAL]], %[[B_REAL]] : !cir.float [[FENV:fenv<.*except_mode = unknown, strict_except = true>]]
+// CIR: %[[MUL_BD:.*]] = cir.fmul %[[A_IMAG]], %[[B_IMAG]] : !cir.float [[FENV]]
+// CIR: %[[MUL_AD:.*]] = cir.fmul %[[A_REAL]], %[[B_IMAG]] : !cir.float [[FENV]]
+// CIR: %[[MUL_BC:.*]] = cir.fmul %[[A_IMAG]], %[[B_REAL]] : !cir.float [[FENV]]
+// CIR: %[[RESULT_REAL:.*]] = cir.fsub %[[MUL_AC]], %[[MUL_BD]] : !cir.float [[FENV]]
+// CIR: %[[RESULT_IMAG:.*]] = cir.fadd %[[MUL_AD]], %[[MUL_BC]] : !cir.float [[FENV]]
+// CIR: %[[RESULT:.*]] = cir.complex.create %[[RESULT_REAL]], %[[RESULT_IMAG]] : !cir.float -> !cir.complex<!cir.float>
+// CIR: cir.store {{.*}} %[[RESULT]], %[[C_ADDR]] : !cir.complex<!cir.float>, !cir.ptr<!cir.complex<!cir.float>>
+
+// LLVM: define {{.*}} @_Z11complex_mulv()
+// LLVM: %[[A_ADDR:.*]] = alloca { float, float }, align 4
+// LLVM: %[[B_ADDR:.*]] = alloca { float, float }, align 4
+// LLVM: %[[C_ADDR:.*]] = alloca { float, float }, align 4
+// LLVM: %[[TMP_A:.*]] = load { float, float }, ptr %[[A_ADDR]], align 4
+// LLVM: %[[TMP_B:.*]] = load { float, float }, ptr %[[B_ADDR]], align 4
+// LLVM: %[[A_REAL:.*]] = extractvalue { float, float } %[[TMP_A]], 0
+// LLVM: %[[A_IMAG:.*]] = extractvalue { float, float } %[[TMP_A]], 1
+// LLVM: %[[B_REAL:.*]] = extractvalue { float, float } %[[TMP_B]], 0
+// LLVM: %[[B_IMAG:.*]] = extractvalue { float, float } %[[TMP_B]], 1
+// LLVM: %[[MUL_AC:.*]] = call float @llvm.experimental.constrained.fmul.f32(float %[[A_REAL]], float %[[B_REAL]], metadata !"round.tonearest", metadata !"fpexcept.strict")
+// LLVM: %[[MUL_BD:.*]] = call float @llvm.experimental.constrained.fmul.f32(float %[[A_IMAG]], float %[[B_IMAG]], metadata !"round.tonearest", metadata !"fpexcept.strict")
+// LLVM: %[[MUL_AD:.*]] = call float @llvm.experimental.constrained.fmul.f32(float %[[A_REAL]], float %[[B_IMAG]], metadata !"round.tonearest", metadata !"fpexcept.strict")
+// LLVM: %[[MUL_BC:.*]] = call float @llvm.experimental.constrained.fmul.f32(float %[[A_IMAG]], float %[[B_REAL]], metadata !"round.tonearest", metadata !"fpexcept.strict")
+// LLVM: %[[RESULT_REAL:.*]] = call float @llvm.experimental.constrained.fsub.f32(float %[[MUL_AC]], float %[[MUL_BD]], metadata !"round.tonearest", metadata !"fpexcept.strict")
+// LLVM: %[[RESULT_IMAG:.*]] = call float @llvm.experimental.constrained.fadd.f32(float %[[MUL_AD]], float %[[MUL_BC]], metadata !"round.tonearest", metadata !"fpexcept.strict")
+// LLVM: %[[TMP_RESULT:.*]] = insertvalue { float, float } {{.*}}, float %[[RESULT_REAL]], 0
+// LLVM: %[[RESULT:.*]] = insertvalue { float, float } %[[TMP_RESULT]], float %[[RESULT_IMAG]], 1
+// LLVM: store { float, float } %[[RESULT]], ptr %[[C_ADDR]], align 4
+
+// OGCG: define {{.*}} @_Z11complex_mulv()
+// OGCG: %[[A_ADDR:.*]] = alloca { float, float }, align 4
+// OGCG: %[[B_ADDR:.*]] = alloca { float, float }, align 4
+// OGCG: %[[C_ADDR:.*]] = alloca { float, float }, align 4
+// OGCG: %[[A_REAL_PTR:.*]] = getelementptr inbounds nuw { float, float }, ptr %[[A_ADDR]], i32 0, i32 0
+// OGCG: %[[A_REAL:.*]] = load float, ptr %[[A_REAL_PTR]], align 4
+// OGCG: %[[A_IMAG_PTR:.*]] = getelementptr inbounds nuw { float, float }, ptr %[[A_ADDR]], i32 0, i32 1
+// OGCG: %[[A_IMAG:.*]] = load float, ptr %[[A_IMAG_PTR]], align 4
+// OGCG: %[[B_REAL_PTR:.*]] = getelementptr inbounds nuw { float, float }, ptr %[[B_ADDR]], i32 0, i32 0
+// OGCG: %[[B_REAL:.*]] = load float, ptr %[[B_REAL_PTR]], align 4
+// OGCG: %[[B_IMAG_PTR:.*]] = getelementptr inbounds nuw { float, float }, ptr %[[B_ADDR]], i32 0, i32 1
+// OGCG: %[[B_IMAG:.*]] = load float, ptr %[[B_IMAG_PTR]], align 4
+// OGCG: %[[MUL_AC:.*]] = call float @llvm.experimental.constrained.fmul.f32(float %[[A_REAL]], float %[[B_REAL]], metadata !"round.tonearest", metadata !"fpexcept.strict")
+// OGCG: %[[MUL_BD:.*]] = call float @llvm.experimental.constrained.fmul.f32(float %[[A_IMAG]], float %[[B_IMAG]], metadata !"round.tonearest", metadata !"fpexcept.strict")
+// OGCG: %[[MUL_AD:.*]] = call float @llvm.experimental.constrained.fmul.f32(float %[[A_REAL]], float %[[B_IMAG]], metadata !"round.tonearest", metadata !"fpexcept.strict")
+// OGCG: %[[MUL_BC:.*]] = call float @llvm.experimental.constrained.fmul.f32(float %[[A_IMAG]], float %[[B_REAL]], metadata !"round.tonearest", metadata !"fpexcept.strict")
+// OGCG: %[[RESULT_REAL:.*]] = call float @llvm.experimental.constrained.fsub.f32(float %[[MUL_AC]], float %[[MUL_BD]], metadata !"round.tonearest", metadata !"fpexcept.strict")
+// OGCG: %[[RESULT_IMAG:.*]] = call float @llvm.experimental.constrained.fadd.f32(float %[[MUL_AD]], float %[[MUL_BC]], metadata !"round.tonearest", metadata !"fpexcept.strict")
+// OGCG: %[[C_REAL_PTR:.*]] = getelementptr inbounds nuw { float, float }, ptr %[[C_ADDR]], i32 0, i32 0
+// 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
+
+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: %[[A_ADDR:.*]] = cir.alloca "a" {{.*}} : !cir.ptr<!cir.complex<!cir.float>>
+// CIR: %[[B_ADDR:.*]] = cir.alloca "b" {{.*}} : !cir.ptr<!cir.complex<!cir.float>>
+// CIR: %[[C_ADDR:.*]] = cir.alloca "c" {{.*}} init : !cir.ptr<!cir.complex<!cir.float>>
+// CIR: %[[TMP_A:.*]] = cir.load {{.*}} %[[A_ADDR]] : !cir.ptr<!cir.complex<!cir.float>>, !cir.complex<!cir.float>
+// CIR: %[[TMP_B:.*]] = cir.load {{.*}} %[[B_ADDR]] : !cir.ptr<!cir.complex<!cir.float>>, !cir.complex<!cir.float>
+// CIR: %[[A_REAL:.*]] = cir.complex.real %[[TMP_A]] : !cir.complex<!cir.float> -> !cir.float
+// CIR: %[[A_IMAG:.*]] = cir.complex.imag %[[TMP_A]] : !cir.complex<!cir.float> -> !cir.float
+// CIR: %[[B_REAL:.*]] = cir.complex.real %[[TMP_B]] : !cir.complex<!cir.float> -> !cir.float
+// CIR: %[[B_IMAG:.*]] = cir.complex.imag %[[TMP_B]] : !cir.complex<!cir.float> -> !cir.float
+// CIR: %[[MUL_AC:.*]] = cir.fmul %[[A_REAL]], %[[B_REAL]] : !cir.float [[FENV:fenv<.*except_mode = unknown, strict_except = true>]]
+// CIR: %[[MUL_BD:.*]] = cir.fmul %[[A_IMAG]], %[[B_IMAG]] : !cir.float [[FENV]]
+// CIR: %[[MUL_CC:.*]] = cir.fmul %[[B_REAL]], %[[B_REAL]] : !cir.float [[FENV]]
+// CIR: %[[MUL_DD:.*]] = cir.fmul %[[B_IMAG]], %[[B_IMAG]] : !cir.float [[FENV]]
+// CIR: %[[ADD_ACBD:.*]] = cir.fadd %[[MUL_AC]], %[[MUL_BD]] : !cir.float [[FENV]]
+// CIR: %[[ADD_CCDD:.*]] = cir.fadd %[[MUL_CC]], %[[MUL_DD]] : !cir.float [[FENV]]
+// CIR: %[[RESULT_REAL:.*]] = cir.fdiv %[[ADD_ACBD]], %[[ADD_CCDD]] : !cir.float [[FENV]]
+// CIR: %[[MUL_BC:.*]] = cir.fmul %[[A_IMAG]], %[[B_REAL]] : !cir.float [[FENV]]
+// CIR: %[[MUL_AD:.*]] = cir.fmul %[[A_REAL]], %[[B_IMAG]] : !cir.float [[FENV]]
+// CIR: %[[SUB_BCAD:.*]] = cir.fsub %[[MUL_BC]], %[[MUL_AD]] : !cir.float [[FENV]]
+// CIR: %[[RESULT_IMAG:.*]] = cir.fdiv %[[SUB_BCAD]], %[[ADD_CCDD]] : !cir.float [[FENV]]
+// CIR: %[[RESULT:.*]] = cir.complex.create %[[RESULT_REAL]], %[[RESULT_IMAG]] : !cir.float -> !cir.complex<!cir.float>
+// CIR: cir.store {{.*}} %[[RESULT]], %[[C_ADDR]] : !cir.complex<!cir.float>, !cir.ptr<!cir.complex<!cir.float>>
+
+// LLVM: define {{.*}} @_Z11complex_divv()
+// LLVM: %[[A_ADDR:.*]] = alloca { float, float }, align 4
+// LLVM: %[[B_ADDR:.*]] = alloca { float, float }, align 4
+// LLVM: %[[C_ADDR:.*]] = alloca { float, float }, align 4
+// LLVM: %[[TMP_A:.*]] = load { float, float }, ptr %[[A_ADDR]], align 4
+// LLVM: %[[TMP_B:.*]] = load { float, float }, ptr %[[B_ADDR]], align 4
+// LLVM: %[[A_REAL:.*]] = extractvalue { float, float } %[[TMP_A]], 0
+// LLVM: %[[A_IMAG:.*]] = extractvalue { float, float } %[[TMP_A]], 1
+// LLVM: %[[B_REAL:.*]] = extractvalue { float, float } %[[TMP_B]], 0
+// LLVM: %[[B_IMAG:.*]] = extractvalue { float, float } %[[TMP_B]], 1
+// LLVM: %[[MUL_AC:.*]] = call float @llvm.experimental.constrained.fmul.f32(float %[[A_REAL]], float %[[B_REAL]], metadata !"round.tonearest", metadata !"fpexcept.strict")
+// LLVM: %[[MUL_BD:.*]] = call float @llvm.experimental.constrained.fmul.f32(float %[[A_IMAG]], float %[[B_IMAG]], metadata !"round.tonearest", metadata !"fpexcept.strict")
+// LLVM: %[[MUL_CC:.*]] = call float @llvm.experimental.constrained.fmul.f32(float %[[B_REAL]], float %[[B_REAL]], metadata !"round.tonearest", metadata !"fpexcept.strict")
+// LLVM: %[[MUL_DD:.*]] = call float @llvm.experimental.constrained.fmul.f32(float %[[B_IMAG]], float %[[B_IMAG]], metadata !"round.tonearest", metadata !"fpexcept.strict")
+// LLVM: %[[ADD_ACBD:.*]] = call float @llvm.experimental.constrained.fadd.f32(float %[[MUL_AC]], float %[[MUL_BD]], metadata !"round.tonearest", metadata !"fpexcept.strict")
+// LLVM: %[[ADD_CCDD:.*]] = call float @llvm.experimental.constrained.fadd.f32(float %[[MUL_CC]], float %[[MUL_DD]], metadata !"round.tonearest", metadata !"fpexcept.strict")
+// LLVM: %[[RESULT_REAL:.*]] = call float @llvm.experimental.constrained.fdiv.f32(float %[[ADD_ACBD]], float %[[ADD_CCDD]], metadata !"round.tonearest", metadata !"fpexcept.strict")
+// LLVM: %[[MUL_BC:.*]] = call float @llvm.experimental.constrained.fmul.f32(float %[[A_IMAG]], float %[[B_REAL]], metadata !"round.tonearest", metadata !"fpexcept.strict")
+// LLVM: %[[MUL_AD:.*]] = call float @llvm.experimental.constrained.fmul.f32(float %[[A_REAL]], float %[[B_IMAG]], metadata !"round.tonearest", metadata !"fpexcept.strict")
+// LLVM: %[[SUB_BCAD:.*]] = call float @llvm.experimental.constrained.fsub.f32(float %[[MUL_BC]], float %[[MUL_AD]], metadata !"round.tonearest", metadata !"fpexcept.strict")
+// LLVM: %[[RESULT_IMAG:.*]] = call float @llvm.experimental.constrained.fdiv.f32(float %[[SUB_BCAD]], float %[[ADD_CCDD]], metadata !"round.tonearest", metadata !"fpexcept.strict")
+// LLVM: %[[TMP_RESULT:.*]] = insertvalue { float, float } {{.*}}, float %[[RESULT_REAL]], 0
+// LLVM: %[[RESULT:.*]] = insertvalue { float, float } %[[TMP_RESULT]], float %[[RESULT_IMAG]], 1
+// LLVM: store { float, float } %[[RESULT]], ptr %[[C_ADDR]], align 4
+
+// OGCG: define {{.*}} @_Z11complex_divv()
+// OGCG: %[[A_ADDR:.*]] = alloca { float, float }, align 4
+// OGCG: %[[B_ADDR:.*]] = alloca { float, float }, align 4
+// OGCG: %[[C_ADDR:.*]] = alloca { float, float }, align 4
+// OGCG: %[[A_REAL_PTR:.*]] = getelementptr inbounds nuw { float, float }, ptr %[[A_ADDR]], i32 0, i32 0
+// OGCG: %[[A_REAL:.*]] = load float, ptr %[[A_REAL_PTR]], align 4
+// OGCG: %[[A_IMAG_PTR:.*]] = getelementptr inbounds nuw { float, float }, ptr %[[A_ADDR]], i32 0, i32 1
+// OGCG: %[[A_IMAG:.*]] = load float, ptr %[[A_IMAG_PTR]], align 4
+// OGCG: %[[B_REAL_PTR:.*]] = getelementptr inbounds nuw { float, float }, ptr %[[B_ADDR]], i32 0, i32 0
+// OGCG: %[[B_REAL:.*]] = load float, ptr %[[B_REAL_PTR]], align 4
+// OGCG: %[[B_IMAG_PTR:.*]] = getelementptr inbounds nuw { float, float }, ptr %[[B_ADDR]], i32 0, i32 1
+// OGCG: %[[B_IMAG:.*]] = load float, ptr %[[B_IMAG_PTR]], align 4
+// OGCG: %[[MUL_AC:.*]] = call float @llvm.experimental.constrained.fmul.f32(float %[[A_REAL]], float %[[B_REAL]], metadata !"round.tonearest", metadata !"fpexcept.strict")
+// OGCG: %[[MUL_BD:.*]] = call float @llvm.experimental.constrained.fmul.f32(float %[[A_IMAG]], float %[[B_IMAG]], metadata !"round.tonearest", metadata !"fpexcept.strict")
+// OGCG: %[[ADD_ACBD:.*]] = call float @llvm.experimental.constrained.fadd.f32(float %[[MUL_AC]], float %[[MUL_BD]], metadata !"round.tonearest", metadata !"fpexcept.strict")
+// OGCG: %[[MUL_CC:.*]] = call float @llvm.experimental.constrained.fmul.f32(float %[[B_REAL]], float %[[B_REAL]], metadata !"round.tonearest", metadata !"fpexcept.strict")
+// OGCG: %[[MUL_DD:.*]] = call float @llvm.experimental.constrained.fmul.f32(float %[[B_IMAG]], float %[[B_IMAG]], metadata !"round.tonearest", metadata !"fpexcept.strict")
+// OGCG: %[[ADD_CCDD:.*]] = call float @llvm.experimental.constrained.fadd.f32(float %[[MUL_CC]], float %[[MUL_DD]], metadata !"round.tonearest", metadata !"fpexcept.strict")
+// OGCG: %[[MUL_BC:.*]] = call float @llvm.experimental.constrained.fmul.f32(float %[[A_IMAG]], float %[[B_REAL]], metadata !"round.tonearest", metadata !"fpexcept.strict")
+// OGCG: %[[MUL_AD:.*]] = call float @llvm.experimental.constrained.fmul.f32(float %[[A_REAL]], float %[[B_IMAG]], metadata !"round.tonearest", metadata !"fpexcept.strict")
+// OGCG: %[[SUB_BCAD:.*]] = call float @llvm.experimental.constrained.fsub.f32(float %[[MUL_BC]], float %[[MUL_AD]], metadata !"round.tonearest", metadata !"fpexcept.strict")
+// OGCG: %[[RESULT_REAL:.*]] = call float @llvm.experimental.constrained.fdiv.f32(float %[[ADD_ACBD]], float %[[ADD_CCDD]], metadata !"round.tonearest", metadata !"fpexcept.strict")
+// OGCG: %[[RESULT_IMAG:.*]] = call float @llvm.experimental.constrained.fdiv.f32(float %[[SUB_BCAD]], float %[[ADD_CCDD]], metadata !"round.tonearest", metadata !"fpexcept.strict")
+// OGCG: %[[C_REAL_PTR:.*]] = getelementptr inbounds nuw { float, float }, ptr %[[C_ADDR]], i32 0, i32 0
+// 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
diff --git a/clang/test/CIR/Transforms/complex-mul-div-fenv-effects.cir b/clang/test/CIR/Transforms/complex-mul-div-fenv-effects.cir
new file mode 100644
index 00000000000000..106a5a972ec1f1
--- /dev/null
+++ b/clang/test/CIR/Transforms/complex-mul-div-fenv-effects.cir
@@ -0,0 +1,27 @@
+// RUN: cir-opt %s -canonicalize -o - | FileCheck %s
+
+// Without a `fenv` attribute, complex FP multiplication and division have no
+// memory effects, so unused results are erased. A `fenv` attribute gives them
+// read and write effects on the floating-point environment, which keeps them
+// alive.
+
+!complex_f32 = !cir.complex<!cir.float>
+
+// CHECK-LABEL: @dead_unconstrained_is_removed
+// 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.fmul %a, %b range(full) : !complex_f32
+ %1 = cir.complex.fdiv %a, %b range(full) : !complex_f32
+ cir.return
+}
+
+// CHECK-LABEL: @dead_constrained_is_not_removed
+// 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.fmul %a, %b range(full) : !complex_f32 fenv<dynamic_rounding_mode = unknown, except_mode = unknown, strict_except = true>
+ %1 = cir.complex.fdiv %a, %b range(full) : !complex_f32 fenv<dynamic_rounding_mode = unknown, except_mode = unknown, 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..3af1b5dc71ba57
--- /dev/null
+++ b/clang/test/CIR/Transforms/lowering-prepare-complex-fenv.cir
@@ -0,0 +1,94 @@
+// RUN: cir-opt %s --cir-lowering-prepare -o - | FileCheck %s \
+// RUN: -DSTRICT="fenv<dynamic_rounding_mode = unknown, except_mode = unknown, strict_except = true>" \
+// RUN: -DUPWARD="fenv<dynamic_rounding_mode = upward, except_mode = unknown, strict_except = true>" \
+// 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 [[STRICT]]
+// CHECK: = cir.fsub %{{.*}}, %{{.*}} : !cir.float [[STRICT]]
+// CHECK: = cir.fadd %{{.*}}, %{{.*}} : !cir.float [[STRICT]]
+// CHECK-COUNT-2: = cir.fcmp uno %{{.*}}, %{{.*}} : !cir.float [[STRICT]]
+// 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<dynamic_rounding_mode = unknown, except_mode = unknown, strict_except = true>
+ cir.return %0 : !complex_f32
+}
+
+// CHECK-LABEL: cir.func @fmul_basic(
+// CHECK-COUNT-4: = cir.fmul %{{.*}}, %{{.*}} : !cir.float [[STRICT]]
+// CHECK: = cir.fsub %{{.*}}, %{{.*}} : !cir.float [[STRICT]]
+// CHECK: = cir.fadd %{{.*}}, %{{.*}} : !cir.float [[STRICT]]
+cir.func @fmul_basic(%a: !complex_f32, %b: !complex_f32) -> !complex_f32 {
+ %0 = cir.complex.fmul %a, %b range(basic) : !complex_f32 fenv<dynamic_rounding_mode = unknown, except_mode = unknown, strict_except = true>
+ cir.return %0 : !complex_f32
+}
+
+// CHECK-LABEL: cir.func @fdiv_full(
+// CHECK: cir.call @__divsc3(
+cir.func @fdiv_full(%a: !complex_f32, %b: !complex_f32) -> !complex_f32 {
+ %0 = cir.complex.fdiv %a, %b range(full) : !complex_f32 fenv<dynamic_rounding_mode = unknown, except_mode = unknown, 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 [[STRICT]]
+cir.func @fdiv_basic(%a: !complex_f32, %b: !complex_f32) -> !complex_f32 {
+ %0 = cir.complex.fdiv %a, %b range(basic) : !complex_f32 fenv<dynamic_rounding_mode = unknown, except_mode = unknown, strict_except = true>
+ cir.return %0 : !complex_f32
+}
+
+// The range check is a quiet comparison. `cir.fabs` cannot raise an exception
+// and takes no attribute.
+
+// CHECK-LABEL: cir.func @fdiv_improved(
+// CHECK-COUNT-2: = cir.fabs %{{.*}} : !cir.float{{$}}
+// CHECK: = cir.fcmp oge %{{.*}}, %{{.*}} : !cir.float [[STRICT]]
+// CHECK-COUNT-18: = cir.f{{mul|add|sub|div}} %{{.*}}, %{{.*}} : !cir.float [[STRICT]]
+cir.func @fdiv_improved(%a: !complex_f32, %b: !complex_f32) -> !complex_f32 {
+ %0 = cir.complex.fdiv %a, %b range(improved) : !complex_f32 fenv<dynamic_rounding_mode = unknown, except_mode = unknown, strict_except = true>
+ cir.return %0 : !complex_f32
+}
+
+// CHECK-LABEL: cir.func @fdiv_promoted(
+// CHECK-COUNT-4: = cir.cast floating %{{.*}} : !cir.float -> !cir.double [[STRICT]]
+// CHECK-COUNT-11: = cir.f{{mul|add|sub|div}} %{{.*}}, %{{.*}} : !cir.double [[STRICT]]
+// CHECK-COUNT-2: = cir.cast floating %{{.*}} : !cir.double -> !cir.float [[STRICT]]
+cir.func @fdiv_promoted(%a: !complex_f32, %b: !complex_f32) -> !complex_f32 {
+ %0 = cir.complex.fdiv %a, %b range(promoted) : !complex_f32 fenv<dynamic_rounding_mode = unknown, except_mode = unknown, 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 oge %{{.*}}, %{{.*}} : !cir.long_double<!cir.f80> [[STRICT]]
+// CHECK-COUNT-18: = cir.f{{mul|add|sub|div}} %{{.*}}, %{{.*}} : !cir.long_double<!cir.f80> [[STRICT]]
+cir.func @fdiv_promoted_fallback(%a: !complex_f80, %b: !complex_f80) -> !complex_f80 {
+ %0 = cir.complex.fdiv %a, %b range(promoted) : !complex_f80 fenv<dynamic_rounding_mode = unknown, except_mode = unknown, 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 [[STRICT]]
+// CHECK-COUNT-11: = cir.f{{mul|add|sub|div}} %{{.*}}, %{{.*}} : !cir.float [[UPWARD]]
+cir.func @two_environments(%a: !complex_f32, %b: !complex_f32) -> !complex_f32 {
+ %0 = cir.complex.fmul %a, %b range(basic) : !complex_f32 fenv<dynamic_rounding_mode = unknown, except_mode = unknown, strict_except = true>
+ %1 = cir.complex.fdiv %0, %b range(basic) : !complex_f32 fenv<dynamic_rounding_mode = upward, except_mode = unknown, strict_except = true>
+ cir.return %1 : !complex_f32
+}
+
+}
More information about the cfe-commits
mailing list