Author: Henrich Lauko
Date: 2026-11-27T13:55:02+01:00
New Revision: 400d6801506c1f05de3f5559b1d7372049bf847f

URL: 
https://github.com/llvm/llvm-project/commit/400d6801506c1f05de3f5559b1d7372049bf847f
DIFF: 
https://github.com/llvm/llvm-project/commit/400d6801506c1f05de3f5559b1d7372049bf847f.diff

LOG: [CIR] Use the FP environment of a compound assignment for its conversions 
(#246035)

`ScalarExprEmitter::emitCompoundAssignLValue` emitted the conversions of
a compound assignment, from the left operand to the computation type and
back, without a `CIRGenFPOptionsRAII`. Under `#pragma STDC FENV_ACCESS
ON` the arithmetic was constrained but the conversions of `x *= 0.1` or
`i += x` got the default environment, and lowered to plain `fpext`,
`fptrunc`, `sitofp` and `fptosi` where classic codegen emits constrained
intrinsics.

Install the RAII with the features of the assignment after the left
operand is loaded, as classic codegen does.

Assisted-by: Claude Code (Fable 5.2).

Added: 
    

Modified: 
    clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
    clang/test/CIR/CodeGen/pragma-fenv_access.c

Removed: 
    


################################################################################
diff  --git a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp 
b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
index 8c7736242facdc..f56cb196360f5e 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
@@ -1769,6 +1769,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/test/CIR/CodeGen/pragma-fenv_access.c 
b/clang/test/CIR/CodeGen/pragma-fenv_access.c
index b947d1cb6ec46f..a3f68c5278e584 100644
--- a/clang/test/CIR/CodeGen/pragma-fenv_access.c
+++ b/clang/test/CIR/CodeGen/pragma-fenv_access.c
@@ -338,6 +338,20 @@ void test_except_off_fenv_access_off(float x, float y, 
float z, int i, double d)
   sf = __builtin_fmodf(x, y);
 // CIR: cir.fmod {{.*}} : !cir.float fenv<dynamic_rounding_mode = tonearest, 
except_mode = masked, strict_except = false>
 // LLVM: call float @llvm.experimental.constrained.frem.f32({{.*}}, metadata 
!"round.tonearest", metadata !"fpexcept.ignore")
+  x *= 0.1;
+// CIR: cir.cast floating {{.*}} : !cir.float -> !cir.double 
[[FENV:fenv<[^>]*>]]
+// CIR: cir.fmul {{.*}} : !cir.double [[FENV]]
+// CIR: cir.cast floating {{.*}} : !cir.double -> !cir.float [[FENV]]
+// LLVM: call double @llvm.experimental.constrained.fpext.f64.f32({{.*}}, 
metadata !"fpexcept.ignore")
+// LLVM: call double @llvm.experimental.constrained.fmul.f64({{.*}}, metadata 
!"round.tonearest", metadata !"fpexcept.ignore")
+// LLVM: call float @llvm.experimental.constrained.fptrunc.f32.f64({{.*}}, 
metadata !"round.tonearest", metadata !"fpexcept.ignore")
+  i += x;
+// CIR: cir.cast int_to_float {{.*}} : !s32i -> !cir.float [[FENV]]
+// CIR: cir.fadd {{.*}} : !cir.float [[FENV]]
+// CIR: cir.cast float_to_int {{.*}} : !cir.float -> !s32i [[FENV]]
+// LLVM: call float @llvm.experimental.constrained.sitofp.f32.i32({{.*}}, 
metadata !"round.tonearest", metadata !"fpexcept.ignore")
+// LLVM: call float @llvm.experimental.constrained.fadd.f32({{.*}}, metadata 
!"round.tonearest", metadata !"fpexcept.ignore")
+// LLVM: call i32 @llvm.experimental.constrained.fptosi.i32.f32({{.*}}, 
metadata !"fpexcept.ignore")
 }
 
 void test_fenv_access_on_after_local_off(float x, float y, float z, int i, 
double d) {
@@ -1801,6 +1815,50 @@ void test_file_scope_fenv_access_off(float x, float y, 
float z, int i, double d)
 // LLVM-DEFAULT-RND: frem float
 }
 
+// The implicit conversions of a compound assignment use the environment of
+// the assignment, like its arithmetic operation.
+void test_compound_assign_fenv_access_on(float x, int i) {
+#pragma STDC FENV_ACCESS ON
+// CIR-LABEL: cir.func {{.*}}@test_compound_assign_fenv_access_on(
+// LLVM-LABEL: define {{.*}}@test_compound_assign_fenv_access_on(
+  x *= 0.1;
+// CIR: cir.cast floating {{.*}} : !cir.float -> !cir.double 
fenv<dynamic_rounding_mode = unknown, except_mode = unknown, strict_except = 
true>
+// CIR: cir.fmul {{.*}} : !cir.double fenv<dynamic_rounding_mode = unknown, 
except_mode = unknown, strict_except = true>
+// CIR: cir.cast floating {{.*}} : !cir.double -> !cir.float 
fenv<dynamic_rounding_mode = unknown, except_mode = unknown, strict_except = 
true>
+// LLVM: call double @llvm.experimental.constrained.fpext.f64.f32({{.*}}, 
metadata !"fpexcept.strict")
+// LLVM: call double @llvm.experimental.constrained.fmul.f64({{.*}}, metadata 
!"round.dynamic", metadata !"fpexcept.strict")
+// LLVM: call float @llvm.experimental.constrained.fptrunc.f32.f64({{.*}}, 
metadata !"round.dynamic", metadata !"fpexcept.strict")
+  i += x;
+// CIR: cir.cast int_to_float {{.*}} : !s32i -> !cir.float 
fenv<dynamic_rounding_mode = unknown, except_mode = unknown, strict_except = 
true>
+// CIR: cir.fadd {{.*}} : !cir.float fenv<dynamic_rounding_mode = unknown, 
except_mode = unknown, strict_except = true>
+// CIR: cir.cast float_to_int {{.*}} : !cir.float -> !s32i 
fenv<dynamic_rounding_mode = unknown, except_mode = unknown, strict_except = 
true>
+// LLVM: call float @llvm.experimental.constrained.sitofp.f32.i32({{.*}}, 
metadata !"round.dynamic", metadata !"fpexcept.strict")
+// LLVM: call float @llvm.experimental.constrained.fadd.f32({{.*}}, metadata 
!"round.dynamic", metadata !"fpexcept.strict")
+// LLVM: call i32 @llvm.experimental.constrained.fptosi.i32.f32({{.*}}, 
metadata !"fpexcept.strict")
+}
+
+// The exception behavior comes from the command line here; the conversions and
+// the arithmetic operation agree on it.
+void test_compound_assign_fenv_round_upward(float x, int i) {
+#pragma STDC FENV_ROUND FE_UPWARD
+// CIR-LABEL: cir.func {{.*}}@test_compound_assign_fenv_round_upward(
+// LLVM-LABEL: define {{.*}}@test_compound_assign_fenv_round_upward(
+  x *= 0.1;
+// CIR: cir.cast floating {{.*}} : !cir.float -> !cir.double 
[[FENV:fenv<dynamic_rounding_mode = upward[^>]*>]]
+// CIR: cir.fmul {{.*}} : !cir.double [[FENV]]
+// CIR: cir.cast floating {{.*}} : !cir.double -> !cir.float [[FENV]]
+// LLVM: call double @llvm.experimental.constrained.fpext.f64.f32({{.*}}, 
metadata ![[EXCEPT:"fpexcept\.[a-z]+"]])
+// LLVM: call double @llvm.experimental.constrained.fmul.f64({{.*}}, metadata 
!"round.upward", metadata ![[EXCEPT]])
+// LLVM: call float @llvm.experimental.constrained.fptrunc.f32.f64({{.*}}, 
metadata !"round.upward", metadata ![[EXCEPT]])
+  i += x;
+// CIR: cir.cast int_to_float {{.*}} : !s32i -> !cir.float [[FENV]]
+// CIR: cir.fadd {{.*}} : !cir.float [[FENV]]
+// CIR: cir.cast float_to_int {{.*}} : !cir.float -> !s32i [[FENV]]
+// LLVM: call float @llvm.experimental.constrained.sitofp.f32.i32({{.*}}, 
metadata !"round.upward", metadata ![[EXCEPT]])
+// LLVM: call float @llvm.experimental.constrained.fadd.f32({{.*}}, metadata 
!"round.upward", metadata ![[EXCEPT]])
+// LLVM: call i32 @llvm.experimental.constrained.fptosi.i32.f32({{.*}}, 
metadata ![[EXCEPT]])
+}
+
 // LLVM-STRICT: attributes #[[$STRICT_ATTR]] = { {{.*}}strictfp{{.*}} }
 // LLVM-STRICT-RND: attributes #[[$STRICT_ATTR]] = { {{.*}}strictfp{{.*}} }
 // LLVM-DEFAULT-RND: attributes #[[$STRICT_ATTR]] = { {{.*}}strictfp{{.*}} }


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to