[clang] [llvm] [X86] EmitX86BuiltinExpr - attempt to convert SSE41/AVX1 roundps/d/ss/sd builtins to regular rounding modes (PR #171227)

Phoebe Wang via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 9 06:02:53 PST 2025


================
@@ -75,6 +75,80 @@ static Value *getMaskVecValue(CodeGenFunction &CGF, Value *Mask,
   return MaskVec;
 }
 
+static Value *emitX86Round(CodeGenFunction &CGF, Value *X, unsigned M) {
+  unsigned RoundingMask = 0b11;
+  unsigned UpdatePEBit = 0b100;
+  unsigned UseMXCSRBit = 0b1000;
+
+  unsigned roundingMode = M & RoundingMask;
+  bool updatePE = M & UpdatePEBit;
+  bool useMXCSR = M & UseMXCSRBit;
+
+  Intrinsic::ID ID = Intrinsic::not_intrinsic;
+  LLVMContext &Ctx = CGF.CGM.getLLVMContext();
+
+  if (useMXCSR) {
+    ID = Intrinsic::experimental_constrained_nearbyint;
+
+    auto PE_metatadata = updatePE ? "fpexcept.strict" : "fpexcept.ignore";
+
+    Value *ExceptMode =
+        MetadataAsValue::get(Ctx, MDString::get(Ctx, PE_metatadata));
+
+    Value *RoundingMode =
+        MetadataAsValue::get(Ctx, MDString::get(Ctx, "rounding.dynamic"));
+
+    Function *F = CGF.CGM.getIntrinsic(ID, X->getType());
+    return CGF.Builder.CreateCall(F, {X, ExceptMode, RoundingMode});
+  }
+
+  if (updatePE) {
+    switch (roundingMode) {
+    case 0b00:
+      ID = Intrinsic::experimental_constrained_roundeven;
----------------
phoebewang wrote:

We don't care of exceptions in default FP mode. It's correctly handled only in strict FP mode.

https://github.com/llvm/llvm-project/pull/171227


More information about the llvm-commits mailing list