[llvm-branch-commits] [clang] [CIR] Give complex FP add/sub the fenv memory effects (PR #242698)

Henrich Lauko via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Sat Nov 14 12:34:16 PST 2026


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

`cir.complex.fadd` and `cir.complex.fsub` were `Pure` even with a constrained `fenv` attribute, so they could be speculated or erased like unconstrained ops. This derives them from `CIR_FenvOp`, as the other fenv ops are: constrained ops read and write the FP environment and are only conditionally speculatable, while unconstrained ops stay effect-free. A new test covers both cases.

Stacked on https://github.com/llvm/llvm-project/pull/236704.

Assisted-by: Claude Code (Opus 5.5).


>From a353f6989ef80fc5a3d94c0b47c1663e8cef6789 Mon Sep 17 00:00:00 2001
From: Henrich Lauko <hlauko at nvidia.com>
Date: Thu, 29 Oct 2026 11:02:11 +0000
Subject: [PATCH] [CIR] Give complex FP add/sub the fenv memory effects

cir.complex.fadd and cir.complex.fsub were Pure even with a constrained
fenv attribute, so they could be speculated or erased like unconstrained
ops. Derive them from CIR_FenvOp, as the other fenv ops are: constrained
ops read and write the FP environment and are only conditionally
speculatable, unconstrained ops stay effect-free.

Assisted-by: Claude Code (Opus 5.5).
---
 clang/include/clang/CIR/Dialect/IR/CIROps.td  |  7 +++--
 .../CIR/Transforms/complex-fenv-effects.cir   | 26 +++++++++++++++++++
 2 files changed, 29 insertions(+), 4 deletions(-)
 create mode 100644 clang/test/CIR/Transforms/complex-fenv-effects.cir

diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td
index 92094ac05b940f..77bef61023b418 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIROps.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td
@@ -7647,11 +7647,10 @@ def CIR_ComplexDivOp : CIR_ComplexBinOp<"complex.div"> {
 }
 
 class CIR_ComplexFPBinOp<string mnemonic>
-    : CIR_Op<mnemonic, [Pure, SameOperandsAndResultType]> {
-  let arguments = (ins
+    : CIR_FenvOp<mnemonic, [SameOperandsAndResultType]> {
+  let prepend arguments = (ins
     CIR_ComplexOfFloatType:$lhs,
-    CIR_ComplexOfFloatType:$rhs,
-    OptionalAttr<CIR_FenvAttr>:$fenv
+    CIR_ComplexOfFloatType:$rhs
   );
 
   let results = (outs CIR_ComplexOfFloatType:$result);
diff --git a/clang/test/CIR/Transforms/complex-fenv-effects.cir b/clang/test/CIR/Transforms/complex-fenv-effects.cir
new file mode 100644
index 00000000000000..dae41b35fa5e8c
--- /dev/null
+++ b/clang/test/CIR/Transforms/complex-fenv-effects.cir
@@ -0,0 +1,26 @@
+// RUN: cir-opt %s -canonicalize -o - | FileCheck %s
+
+// Without a `fenv` attribute, complex FP add/sub 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.
+
+!complex_f32 = !cir.complex<!cir.float>
+
+// CHECK-LABEL: @dead_unconstrained_is_removed
+// CHECK-NOT:     cir.complex.fadd
+// CHECK-NOT:     cir.complex.fsub
+// 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
+  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>
+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>
+  cir.return
+}



More information about the llvm-branch-commits mailing list